From 2861f795f5c9acf32fba52c1004467ee86e5c202 Mon Sep 17 00:00:00 2001 From: mossid Date: Thu, 5 Apr 2018 21:30:15 +0200 Subject: [PATCH 1/2] add SimpleProofsFromMap --- merkle/simple_proof.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/merkle/simple_proof.go b/merkle/simple_proof.go index c81ed674a..7eb3a77e6 100644 --- a/merkle/simple_proof.go +++ b/merkle/simple_proof.go @@ -22,6 +22,20 @@ func SimpleProofsFromHashers(items []Hasher) (rootHash []byte, proofs []*SimpleP return } +func SimpleProofsFromMap(m map[string]Hasher) (rootHash []byte, proofs []*SimpleProof) { + sm := NewSimpleMap() + for k, v := range m { + sm.Set(k, v) + } + sm.Sort() + kvs := sm.kvs + kvsH := make([]Hasher, 0, len(kvs)) + for _, kvp := range kvs { + kvsH = append(kvsH, kvPair(kvp)) + } + return SimpleProofsFromHashers(kvsH) +} + // Verify that leafHash is a leaf hash of the simple-merkle-tree // which hashes to rootHash. func (sp *SimpleProof) Verify(index int, total int, leafHash []byte, rootHash []byte) bool { From 50c521e706e8e2e99706990f320bcf3167e7f792 Mon Sep 17 00:00:00 2001 From: mossid Date: Thu, 5 Apr 2018 21:56:29 +0200 Subject: [PATCH 2/2] expose KVPair --- merkle/simple_map.go | 6 +++--- merkle/simple_proof.go | 2 +- merkle/simple_tree_test.go | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/merkle/simple_map.go b/merkle/simple_map.go index b59e3b4b6..cd38de761 100644 --- a/merkle/simple_map.go +++ b/merkle/simple_map.go @@ -60,9 +60,9 @@ func (sm *SimpleMap) KVPairs() cmn.KVPairs { //---------------------------------------- // A local extension to KVPair that can be hashed. -type kvPair cmn.KVPair +type KVPair cmn.KVPair -func (kv kvPair) Hash() []byte { +func (kv KVPair) Hash() []byte { hasher := ripemd160.New() err := encodeByteSlice(hasher, kv.Key) if err != nil { @@ -78,7 +78,7 @@ func (kv kvPair) Hash() []byte { func hashKVPairs(kvs cmn.KVPairs) []byte { kvsH := make([]Hasher, 0, len(kvs)) for _, kvp := range kvs { - kvsH = append(kvsH, kvPair(kvp)) + kvsH = append(kvsH, KVPair(kvp)) } return SimpleHashFromHashers(kvsH) } diff --git a/merkle/simple_proof.go b/merkle/simple_proof.go index 7eb3a77e6..ca6ccf372 100644 --- a/merkle/simple_proof.go +++ b/merkle/simple_proof.go @@ -31,7 +31,7 @@ func SimpleProofsFromMap(m map[string]Hasher) (rootHash []byte, proofs []*Simple kvs := sm.kvs kvsH := make([]Hasher, 0, len(kvs)) for _, kvp := range kvs { - kvsH = append(kvsH, kvPair(kvp)) + kvsH = append(kvsH, KVPair(kvp)) } return SimpleProofsFromHashers(kvsH) } diff --git a/merkle/simple_tree_test.go b/merkle/simple_tree_test.go index 26f35c807..8c4ed01f8 100644 --- a/merkle/simple_tree_test.go +++ b/merkle/simple_tree_test.go @@ -3,7 +3,7 @@ package merkle import ( "bytes" - . "github.com/tendermint/tmlibs/common" + cmn "github.com/tendermint/tmlibs/common" . "github.com/tendermint/tmlibs/test" "testing" @@ -21,7 +21,7 @@ func TestSimpleProof(t *testing.T) { items := make([]Hasher, total) for i := 0; i < total; i++ { - items[i] = testItem(RandBytes(32)) + items[i] = testItem(cmn.RandBytes(32)) } rootHash := SimpleHashFromHashers(items) @@ -53,7 +53,7 @@ func TestSimpleProof(t *testing.T) { // Trail too long should make it fail origAunts := proof.Aunts - proof.Aunts = append(proof.Aunts, RandBytes(32)) + proof.Aunts = append(proof.Aunts, cmn.RandBytes(32)) { ok = proof.Verify(i, total, itemHash, rootHash) if ok {