Merge pull request #196 from tendermint/joon/simple-proofs-from-map
add SimpleProofsFromMap
This commit is contained in:
commit
40a73fa75c
|
@ -60,9 +60,9 @@ func (sm *SimpleMap) KVPairs() cmn.KVPairs {
|
||||||
//----------------------------------------
|
//----------------------------------------
|
||||||
|
|
||||||
// A local extension to KVPair that can be hashed.
|
// 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()
|
hasher := ripemd160.New()
|
||||||
err := encodeByteSlice(hasher, kv.Key)
|
err := encodeByteSlice(hasher, kv.Key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -78,7 +78,7 @@ func (kv kvPair) Hash() []byte {
|
||||||
func hashKVPairs(kvs cmn.KVPairs) []byte {
|
func hashKVPairs(kvs cmn.KVPairs) []byte {
|
||||||
kvsH := make([]Hasher, 0, len(kvs))
|
kvsH := make([]Hasher, 0, len(kvs))
|
||||||
for _, kvp := range kvs {
|
for _, kvp := range kvs {
|
||||||
kvsH = append(kvsH, kvPair(kvp))
|
kvsH = append(kvsH, KVPair(kvp))
|
||||||
}
|
}
|
||||||
return SimpleHashFromHashers(kvsH)
|
return SimpleHashFromHashers(kvsH)
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,20 @@ func SimpleProofsFromHashers(items []Hasher) (rootHash []byte, proofs []*SimpleP
|
||||||
return
|
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
|
// Verify that leafHash is a leaf hash of the simple-merkle-tree
|
||||||
// which hashes to rootHash.
|
// which hashes to rootHash.
|
||||||
func (sp *SimpleProof) Verify(index int, total int, leafHash []byte, rootHash []byte) bool {
|
func (sp *SimpleProof) Verify(index int, total int, leafHash []byte, rootHash []byte) bool {
|
||||||
|
|
|
@ -3,7 +3,7 @@ package merkle
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
|
||||||
. "github.com/tendermint/tmlibs/common"
|
cmn "github.com/tendermint/tmlibs/common"
|
||||||
. "github.com/tendermint/tmlibs/test"
|
. "github.com/tendermint/tmlibs/test"
|
||||||
|
|
||||||
"testing"
|
"testing"
|
||||||
|
@ -21,7 +21,7 @@ func TestSimpleProof(t *testing.T) {
|
||||||
|
|
||||||
items := make([]Hasher, total)
|
items := make([]Hasher, total)
|
||||||
for i := 0; i < total; i++ {
|
for i := 0; i < total; i++ {
|
||||||
items[i] = testItem(RandBytes(32))
|
items[i] = testItem(cmn.RandBytes(32))
|
||||||
}
|
}
|
||||||
|
|
||||||
rootHash := SimpleHashFromHashers(items)
|
rootHash := SimpleHashFromHashers(items)
|
||||||
|
@ -53,7 +53,7 @@ func TestSimpleProof(t *testing.T) {
|
||||||
|
|
||||||
// Trail too long should make it fail
|
// Trail too long should make it fail
|
||||||
origAunts := proof.Aunts
|
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)
|
ok = proof.Verify(i, total, itemHash, rootHash)
|
||||||
if ok {
|
if ok {
|
||||||
|
|
Loading…
Reference in New Issue