From b8a6eb870cc0c5c1122587046f806796a38ee345 Mon Sep 17 00:00:00 2001 From: Ismail Khoffi Date: Sat, 13 Feb 2021 21:05:39 +0100 Subject: [PATCH] Register custom hasher from plugin code instead of in go-multihash fork - use hasher from nmt instead of redefining the logic in multihash or here (see https://github.com/lazyledger/nmt/pull/19) - remove multihash fork and use updated go-verifcid - minor renaming and refactoring --- go.mod | 5 +- go.sum | 18 ++++-- p2p/ipld/plugin/go.mod | 9 +-- p2p/ipld/plugin/go.sum | 26 ++++++--- p2p/ipld/plugin/nodes/nodes.go | 86 ++++++++++++++++++++++------- p2p/ipld/plugin/nodes/nodes_test.go | 20 ++----- 6 files changed, 111 insertions(+), 53 deletions(-) diff --git a/go.mod b/go.mod index 1d06229d46..ff19672d62 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,9 @@ require ( github.com/ipfs/go-ipfs-config v0.9.0 github.com/ipfs/interface-go-ipfs-core v0.4.0 github.com/lazyledger/lazyledger-core/p2p/ipld/plugin v0.0.0-00010101000000-000000000000 - github.com/lazyledger/nmt v0.1.0 + // TODO(ismail): update to tagged version after + // https://github.com/lazyledger/nmt/pull/19 gets merged + github.com/lazyledger/nmt v0.1.1-0.20210212160145-d6f4312f0a8d github.com/lazyledger/rsmt2d v0.0.0-20201215203123-e5ec7910ddd4 github.com/libp2p/go-buffer-pool v0.0.2 github.com/minio/highwayhash v1.0.1 @@ -48,5 +50,4 @@ replace ( github.com/ipfs/go-ipfs v0.7.0 => github.com/lazyledger/go-ipfs v0.7.1-0.20210205233505-656642674e11 github.com/ipfs/go-verifcid => github.com/lazyledger/go-verifcid v0.0.2-0.20210205232850-c3e21cfe4064 github.com/lazyledger/lazyledger-core/p2p/ipld/plugin => ./p2p/ipld/plugin - github.com/multiformats/go-multihash => github.com/lazyledger/go-multihash v0.0.15-0.20210205224750-88bad1265973 ) diff --git a/go.sum b/go.sum index 1b2177aa0a..7216270b82 100644 --- a/go.sum +++ b/go.sum @@ -616,14 +616,12 @@ github.com/lazyledger/go-ipfs v0.7.1-0.20210205233505-656642674e11 h1:4NH5iOp5WQ github.com/lazyledger/go-ipfs v0.7.1-0.20210205233505-656642674e11/go.mod h1:2Bj1K5NX/KTpCziiP9RNDfgdwHnaytIlr4fCi0ZwJQo= github.com/lazyledger/go-leopard v0.0.0-20200604113236-298f93361181 h1:mUeCGuCgjZVadW4CzA2dMBq7p2BqaoCfpnKjxMmSaSE= github.com/lazyledger/go-leopard v0.0.0-20200604113236-298f93361181/go.mod h1:v1o1CRihQ9i7hizx23KK4aR79lxA6VDUIzUCfDva0XQ= -github.com/lazyledger/go-multihash v0.0.15-0.20210205224750-88bad1265973 h1:qERqAEXJ28G9Hhk9GBiGRwXynqZ5hbDqMhyvP5XOJS4= -github.com/lazyledger/go-multihash v0.0.15-0.20210205224750-88bad1265973/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= github.com/lazyledger/go-verifcid v0.0.2-0.20210205232850-c3e21cfe4064 h1:qFcXjQi5GY8UFQH1li/aGFJ/PIXNADoNek9i57ha3gg= github.com/lazyledger/go-verifcid v0.0.2-0.20210205232850-c3e21cfe4064/go.mod h1:i0oimuqPNkw9y4DWtt1G+aPznQkyaYPvr9SRXEOZtb0= github.com/lazyledger/merkletree v0.0.0-20201214195110-6901c4c3c75f h1:jbyPAH6o6hGte4RtZBaqWs2n4Fl6hS7qJGXX3qnjiy4= github.com/lazyledger/merkletree v0.0.0-20201214195110-6901c4c3c75f/go.mod h1:10PA0NlnYtB8HrtwIDQAyTKWp8TEZ0zBZCGlYC/7+QE= -github.com/lazyledger/nmt v0.1.0 h1:GSpAmBc3B7cwJSYTjh/v+vPQCsNCqKzSklu8LzYxwaU= -github.com/lazyledger/nmt v0.1.0/go.mod h1:tY7ypPX26Sbkt6F8EbPl3AT33B5N0BJe4OVPbq849YI= +github.com/lazyledger/nmt v0.1.1-0.20210212160145-d6f4312f0a8d h1:Q45qTAaYay8r+1kndwjo1EDvpxbqgE7D6xKlaPNeVXk= +github.com/lazyledger/nmt v0.1.1-0.20210212160145-d6f4312f0a8d/go.mod h1:tY7ypPX26Sbkt6F8EbPl3AT33B5N0BJe4OVPbq849YI= github.com/lazyledger/rsmt2d v0.0.0-20201215203123-e5ec7910ddd4 h1:r2mI8AnDTOSwL9GWGjgHC4rwzaR56jRCo1GLpG2lwIw= github.com/lazyledger/rsmt2d v0.0.0-20201215203123-e5ec7910ddd4/go.mod h1:ORR2U7THCNr1fpUhwYqZN7QCFJ20iR2uiIWfXKz3KJ4= github.com/libp2p/go-addr-util v0.0.1/go.mod h1:4ac6O7n9rIAKB1dnd+s8IbbMXkt+oBpzX4/+RACcnlQ= @@ -1007,6 +1005,15 @@ github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/g github.com/multiformats/go-multibase v0.0.2/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs= github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk= github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc= +github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U= +github.com/multiformats/go-multihash v0.0.5/go.mod h1:lt/HCbqlQwlPBz7lv0sQCdtfcMtlJvakRUn/0Ual8po= +github.com/multiformats/go-multihash v0.0.7/go.mod h1:XuKXPp8VHcTygube3OWZC+aZrA+H1IhmjoCDtJc7PXM= +github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= +github.com/multiformats/go-multihash v0.0.9/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= +github.com/multiformats/go-multihash v0.0.10/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= +github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= +github.com/multiformats/go-multihash v0.0.14 h1:QoBceQYQQtNUuf6s7wHxnE2c8bhbMqhfGzNI032se/I= +github.com/multiformats/go-multihash v0.0.14/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= github.com/multiformats/go-multistream v0.0.1/go.mod h1:fJTiDfXJVmItycydCnNx4+wSzZ5NwG2FEVAI30fiovg= github.com/multiformats/go-multistream v0.0.4/go.mod h1:fJTiDfXJVmItycydCnNx4+wSzZ5NwG2FEVAI30fiovg= github.com/multiformats/go-multistream v0.1.0/go.mod h1:fJTiDfXJVmItycydCnNx4+wSzZ5NwG2FEVAI30fiovg= @@ -1336,10 +1343,12 @@ golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnf golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190225124518-7f87c0fbb88b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190228161510-8dd112bcdc25/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -1466,6 +1475,7 @@ golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190228124157-a34e9553db1e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190302025703-b6889370fb10/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/p2p/ipld/plugin/go.mod b/p2p/ipld/plugin/go.mod index b1f2ff3b8d..7b083f6521 100644 --- a/p2p/ipld/plugin/go.mod +++ b/p2p/ipld/plugin/go.mod @@ -8,14 +8,15 @@ require ( github.com/ipfs/go-ipfs v0.7.0 github.com/ipfs/go-ipfs-api v0.2.0 github.com/ipfs/go-ipld-format v0.2.0 - github.com/lazyledger/nmt v0.1.0 + // TODO(ismail): change this to a tagged version after + // https://github.com/lazyledger/nmt/pull/19 gets merged + github.com/lazyledger/nmt v0.1.1-0.20210212160145-d6f4312f0a8d // rsmt2d is only used in tests: github.com/lazyledger/rsmt2d v0.0.0-20201215203123-e5ec7910ddd4 github.com/multiformats/go-multihash v0.0.14 ) replace ( - github.com/ipfs/go-ipfs v0.7.0 => github.com/lazyledger/go-ipfs v0.7.1-0.20210205233505-656642674e11 - github.com/ipfs/go-verifcid => github.com/lazyledger/go-verifcid v0.0.2-0.20210205232850-c3e21cfe4064 - github.com/multiformats/go-multihash => github.com/lazyledger/go-multihash v0.0.15-0.20210205224750-88bad1265973 + github.com/ipfs/go-ipfs v0.7.0 => github.com/lazyledger/go-ipfs v0.7.1-0.20210213193504-c65604ccb2ed + github.com/ipfs/go-verifcid => github.com/lazyledger/go-verifcid v0.0.2-0.20210213182512-19890b0d114d ) diff --git a/p2p/ipld/plugin/go.sum b/p2p/ipld/plugin/go.sum index 6131405cf3..487f56d7c9 100644 --- a/p2p/ipld/plugin/go.sum +++ b/p2p/ipld/plugin/go.sum @@ -458,18 +458,16 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.3/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/lazyledger/go-ipfs v0.7.1-0.20210205233505-656642674e11 h1:4NH5iOp5WQKbvH39l5lv3z3McXbdLIEpQc8CC3G8INs= -github.com/lazyledger/go-ipfs v0.7.1-0.20210205233505-656642674e11/go.mod h1:2Bj1K5NX/KTpCziiP9RNDfgdwHnaytIlr4fCi0ZwJQo= +github.com/lazyledger/go-ipfs v0.7.1-0.20210213193504-c65604ccb2ed h1:/0LMtgKop0LGb8/gX8O3rALeYasqSkbotbHNk47Mdyc= +github.com/lazyledger/go-ipfs v0.7.1-0.20210213193504-c65604ccb2ed/go.mod h1:AXxQDfXMEZtkNCWaI+okNaK1D47EGGooDkyeFMcoTiA= github.com/lazyledger/go-leopard v0.0.0-20200604113236-298f93361181 h1:mUeCGuCgjZVadW4CzA2dMBq7p2BqaoCfpnKjxMmSaSE= github.com/lazyledger/go-leopard v0.0.0-20200604113236-298f93361181/go.mod h1:v1o1CRihQ9i7hizx23KK4aR79lxA6VDUIzUCfDva0XQ= -github.com/lazyledger/go-multihash v0.0.15-0.20210205224750-88bad1265973 h1:qERqAEXJ28G9Hhk9GBiGRwXynqZ5hbDqMhyvP5XOJS4= -github.com/lazyledger/go-multihash v0.0.15-0.20210205224750-88bad1265973/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= -github.com/lazyledger/go-verifcid v0.0.2-0.20210205232850-c3e21cfe4064 h1:qFcXjQi5GY8UFQH1li/aGFJ/PIXNADoNek9i57ha3gg= -github.com/lazyledger/go-verifcid v0.0.2-0.20210205232850-c3e21cfe4064/go.mod h1:i0oimuqPNkw9y4DWtt1G+aPznQkyaYPvr9SRXEOZtb0= +github.com/lazyledger/go-verifcid v0.0.2-0.20210213182512-19890b0d114d h1:zvUbSnIsdnBIc1fDOKXubVcTKqw64cvaSoIwOAD1wcU= +github.com/lazyledger/go-verifcid v0.0.2-0.20210213182512-19890b0d114d/go.mod h1:kXPYu0XqTNUKWA1h3M95UHjUqBzDwXVVt/RXZDjKJmQ= github.com/lazyledger/merkletree v0.0.0-20201214195110-6901c4c3c75f h1:jbyPAH6o6hGte4RtZBaqWs2n4Fl6hS7qJGXX3qnjiy4= github.com/lazyledger/merkletree v0.0.0-20201214195110-6901c4c3c75f/go.mod h1:10PA0NlnYtB8HrtwIDQAyTKWp8TEZ0zBZCGlYC/7+QE= -github.com/lazyledger/nmt v0.1.0 h1:GSpAmBc3B7cwJSYTjh/v+vPQCsNCqKzSklu8LzYxwaU= -github.com/lazyledger/nmt v0.1.0/go.mod h1:tY7ypPX26Sbkt6F8EbPl3AT33B5N0BJe4OVPbq849YI= +github.com/lazyledger/nmt v0.1.1-0.20210212160145-d6f4312f0a8d h1:Q45qTAaYay8r+1kndwjo1EDvpxbqgE7D6xKlaPNeVXk= +github.com/lazyledger/nmt v0.1.1-0.20210212160145-d6f4312f0a8d/go.mod h1:tY7ypPX26Sbkt6F8EbPl3AT33B5N0BJe4OVPbq849YI= github.com/lazyledger/rsmt2d v0.0.0-20201215203123-e5ec7910ddd4 h1:r2mI8AnDTOSwL9GWGjgHC4rwzaR56jRCo1GLpG2lwIw= github.com/lazyledger/rsmt2d v0.0.0-20201215203123-e5ec7910ddd4/go.mod h1:ORR2U7THCNr1fpUhwYqZN7QCFJ20iR2uiIWfXKz3KJ4= github.com/libp2p/go-addr-util v0.0.1/go.mod h1:4ac6O7n9rIAKB1dnd+s8IbbMXkt+oBpzX4/+RACcnlQ= @@ -833,6 +831,15 @@ github.com/multiformats/go-multibase v0.0.1/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/g github.com/multiformats/go-multibase v0.0.2/go.mod h1:bja2MqRZ3ggyXtZSEDKpl0uO/gviWFaSteVbWT51qgs= github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk= github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc= +github.com/multiformats/go-multihash v0.0.1/go.mod h1:w/5tugSrLEbWqlcgJabL3oHFKTwfvkofsjW2Qa1ct4U= +github.com/multiformats/go-multihash v0.0.5/go.mod h1:lt/HCbqlQwlPBz7lv0sQCdtfcMtlJvakRUn/0Ual8po= +github.com/multiformats/go-multihash v0.0.7/go.mod h1:XuKXPp8VHcTygube3OWZC+aZrA+H1IhmjoCDtJc7PXM= +github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= +github.com/multiformats/go-multihash v0.0.9/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= +github.com/multiformats/go-multihash v0.0.10/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew= +github.com/multiformats/go-multihash v0.0.13/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= +github.com/multiformats/go-multihash v0.0.14 h1:QoBceQYQQtNUuf6s7wHxnE2c8bhbMqhfGzNI032se/I= +github.com/multiformats/go-multihash v0.0.14/go.mod h1:VdAWLKTwram9oKAatUcLxBNUjdtcVwxObEQBtRfuyjc= github.com/multiformats/go-multistream v0.0.1/go.mod h1:fJTiDfXJVmItycydCnNx4+wSzZ5NwG2FEVAI30fiovg= github.com/multiformats/go-multistream v0.0.4/go.mod h1:fJTiDfXJVmItycydCnNx4+wSzZ5NwG2FEVAI30fiovg= github.com/multiformats/go-multistream v0.1.0/go.mod h1:fJTiDfXJVmItycydCnNx4+wSzZ5NwG2FEVAI30fiovg= @@ -1047,10 +1054,12 @@ golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnf golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190225124518-7f87c0fbb88b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190228161510-8dd112bcdc25/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190313024323-a1f597ede03a/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190513172903-22d7a77e9e5f/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -1155,6 +1164,7 @@ golang.org/x/sys v0.0.0-20181029174526-d69651ed3497/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190219092855-153ac476189d/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190228124157-a34e9553db1e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190302025703-b6889370fb10/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/p2p/ipld/plugin/nodes/nodes.go b/p2p/ipld/plugin/nodes/nodes.go index 90a3e81831..2cdafa3d12 100644 --- a/p2p/ipld/plugin/nodes/nodes.go +++ b/p2p/ipld/plugin/nodes/nodes.go @@ -19,11 +19,15 @@ import ( ) const ( - // NMT is the codec used for this plugin. - // 0x77 seems to be free: + // Below used multiformats (one codec, one multihash) seem free: // https://github.com/multiformats/multicodec/blob/master/table.csv - NMT = 0x7700 - NamespaceTaggedSha256 = 0x7701 + + // Nmt is the codec used for leaf and inner nodes of an Namespaced Merkle Tree. + Nmt = 0x7700 + // Sha256Namespace8Flagged is the multihash code used to hash blocks + // that contain an NMT node (inner and leaf nodes). + Sha256Namespace8Flagged = 0x7701 + // DagParserFormatName can be used when putting into the IPLD Dag DagParserFormatName = "extended-square-row-or-col" @@ -35,12 +39,50 @@ const ( shareSize = 256 ) +func init() { + registerNamespacedCodecOrPanic( + Sha256Namespace8Flagged, + "sha2-256-namespace8-flagged", + 2*namespaceSize+sha256.Size, + sumSha256Namespace8Flagged, + ) +} + +func registerNamespacedCodecOrPanic( + codec uint64, + name string, + defaulLength int, + hashFunc mh.HashFunc, +) { + if _, ok := mh.Codes[codec]; !ok { + // add to mh.Codes map first, otherwise mh.RegisterHashFunc would err: + mh.Codes[codec] = name + mh.Names[name] = codec + mh.DefaultLengths[codec] = defaulLength + + if err := mh.RegisterHashFunc(codec, hashFunc); err != nil { + panic(fmt.Sprintf("could not register hash function: %v", mh.Codes[codec])) + } + } +} + +// sumSha256Namespace8Flagged is the mh.HashFunc used to hash leaf and inner nodes. +// It is registered as a mh.HashFunc in the go-multihash module. +func sumSha256Namespace8Flagged(data []byte, _length int) ([]byte, error) { + isLeafData := data[0] == nmt.LeafPrefix + if isLeafData { + return nmt.Sha256Namespace8FlaggedLeaf(data[1:]), nil + } else { + return nmt.Sha256Namespace8FlaggedInner(data[1:]), nil + } +} + var _ plugin.PluginIPLD = &LazyLedgerPlugin{} type LazyLedgerPlugin struct{} func (l LazyLedgerPlugin) RegisterBlockDecoders(dec format.BlockDecoder) error { - dec.Register(NMT, NmtNodeParser) + dec.Register(Nmt, NmtNodeParser) return nil } @@ -73,10 +115,7 @@ func DataSquareRowOrColumnRawInputParser(r io.Reader, _mhType uint64, _mhLen int br := bufio.NewReader(r) nodes := make([]node.Node, 0, extendedSquareSize) nodeCollector := func(hash []byte, children ...[]byte) { - cid, err := cidFromNamespacedSha256(hash) - if err != nil { - panic(fmt.Sprintf("nmt lib created a malformed hash: %s", err)) - } + cid := mustCidFromNamespacedSha256(hash) switch len(children) { case 1: prependNode(nmtLeafNode{ @@ -240,14 +279,9 @@ func (n nmtNode) Copy() node.Node { } func (n nmtNode) Links() []*node.Link { - leftCid, err := cidFromNamespacedSha256(n.l) - if err != nil { - panic(fmt.Errorf("malformed nmtNode, cidFromNamespacedSha256(): %w", err)) - } - rightCid, err := cidFromNamespacedSha256(n.r) - if err != nil { - panic(fmt.Errorf("malformed nmtNode, cidFromNamespacedSha256(): %w", err)) - } + leftCid := mustCidFromNamespacedSha256(n.l) + rightCid := mustCidFromNamespacedSha256(n.r) + return []*node.Link{{Cid: leftCid}, {Cid: rightCid}} } @@ -338,9 +372,23 @@ func cidFromNamespacedSha256(namespacedHash []byte) (cid.Cid, error) { if got, want := len(namespacedHash), 2*namespaceSize+sha256.Size; got != want { return cid.Cid{}, fmt.Errorf("invalid namespaced hash lenght, got: %v, want: %v", got, want) } - buf, err := mh.Encode(namespacedHash, mh.SHA2_256_NAMESPACE_TAGGED) + buf, err := mh.Encode(namespacedHash, Sha256Namespace8Flagged) if err != nil { return cid.Undef, err } - return cid.NewCidV1(NMT, mh.Multihash(buf)), nil + return cid.NewCidV1(Nmt, mh.Multihash(buf)), nil +} + +// mustCidFromNamespacedSha256 is a wrapper around cidFromNamespacedSha256 that panics +// in case of an error. Use with care and only in places where no error should occur. +func mustCidFromNamespacedSha256(hash []byte) cid.Cid { + cid, err := cidFromNamespacedSha256(hash) + if err != nil { + panic( + fmt.Sprintf("malformed hash: %s, codec: %v", + err, + mh.Codes[Sha256Namespace8Flagged]), + ) + } + return cid } diff --git a/p2p/ipld/plugin/nodes/nodes_test.go b/p2p/ipld/plugin/nodes/nodes_test.go index 0285205ceb..65a661f226 100644 --- a/p2p/ipld/plugin/nodes/nodes_test.go +++ b/p2p/ipld/plugin/nodes/nodes_test.go @@ -48,7 +48,7 @@ func TestDataSquareRowOrColumnRawInputParserCidEqNmtRoot(t *testing.T) { t.Errorf("hashes don't match\ngot: %v\nwant: %v", got, want) } lastNodeCid := gotNodes[len(gotNodes)-1].Cid() - if gotHash, wantHash := lastNodeCid.Hash(), hashLeaf(tt.leafData[0]); !bytes.Equal(gotHash[multiHashOverhead:], wantHash) { + if gotHash, wantHash := lastNodeCid.Hash(), nmt.Sha256Namespace8FlaggedLeaf(tt.leafData[0]); !bytes.Equal(gotHash[multiHashOverhead:], wantHash) { t.Errorf("first node's hash does not match the Cid\ngot: %v\nwant: %v", gotHash[multiHashOverhead:], wantHash) } nodePrefixOffset := 1 // leaf / inner node prefix is one byte @@ -84,14 +84,14 @@ func TestDagPutWithPlugin(t *testing.T) { if err != nil { t.Fatalf("DagPut() failed: %v", err) } - // convert NMT tree root to CID and verify it matches the CID returned by DagPut + // convert Nmt tree root to CID and verify it matches the CID returned by DagPut treeRootBytes := n.Root().Bytes() nmtCid, err := cidFromNamespacedSha256(treeRootBytes) if err != nil { t.Fatalf("cidFromNamespacedSha256() failed: %v", err) } if nmtCid.String() != cid { - t.Errorf("CIDs from NMT and plugin do not match: got %v, want: %v", cid, nmtCid.String()) + t.Errorf("CIDs from Nmt and plugin do not match: got %v, want: %v", cid, nmtCid.String()) } // print out cid s.t. it can be used on the commandline t.Logf("Stored with cid: %v\n", cid) @@ -162,7 +162,7 @@ func (n nmtWrapper) Prove(idx int) (merkleRoot []byte, proofSet [][]byte, proofI return n.NamespacedMerkleTree.Root().Bytes(), proof.Nodes(), uint64(proof.Start()), - 0 // TODO: NMT doesn't return the number of leaves + 0 // TODO: Nmt doesn't return the number of leaves } func (n nmtWrapper) Root() []byte { @@ -188,18 +188,6 @@ func createByteBufFromRawData(t *testing.T, leafData [][]byte) *bytes.Buffer { return buf } -// this snippet of the nmt internals is copied here: -func hashLeaf(data []byte) []byte { - h := sha256.New() - nID := data[:namespaceSize] - toCommittToDataWithoutNID := data[namespaceSize:] - - res := append(append(make([]byte, 0), nID...), nID...) - data = append([]byte{nmt.LeafPrefix}, toCommittToDataWithoutNID...) - h.Write(data) - return h.Sum(res) -} - func generateRandNamespacedRawData(total int, nidSize int, leafSize int) [][]byte { data := make([][]byte, total) for i := 0; i < total; i++ {