merkle: use amino for byteslice encoding
This commit is contained in:
parent
862d3c342a
commit
52bd867fd9
|
@ -1,8 +1,8 @@
|
|||
package merkle
|
||||
|
||||
import (
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
"github.com/tendermint/go-crypto/tmhash"
|
||||
cmn "github.com/tendermint/tmlibs/common"
|
||||
)
|
||||
|
||||
// Merkle tree from a map.
|
||||
|
@ -65,21 +65,20 @@ func (sm *simpleMap) KVPairs() cmn.KVPairs {
|
|||
//----------------------------------------
|
||||
|
||||
// A local extension to KVPair that can be hashed.
|
||||
// XXX: key and value do not need to already be hashed -
|
||||
// the kvpair ("abc", "def") would not give the same result
|
||||
// as ("ab", "cdef") as we're using length-prefixing.
|
||||
// Key and value are length prefixed and concatenated,
|
||||
// then hashed.
|
||||
type kvPair cmn.KVPair
|
||||
|
||||
func (kv kvPair) Hash() []byte {
|
||||
hasher := tmhash.New()
|
||||
err := encodeByteSlice(hasher, kv.Key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
err = encodeByteSlice(hasher, kv.Value)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
panic(err)
|
||||
}
|
||||
return hasher.Sum(nil)
|
||||
}
|
||||
|
||||
|
|
|
@ -11,9 +11,7 @@ import (
|
|||
type strHasher string
|
||||
|
||||
func (str strHasher) Hash() []byte {
|
||||
h := tmhash.New()
|
||||
h.Write([]byte(str))
|
||||
return h.Sum(nil)
|
||||
return tmhash.Sum([]byte(str))
|
||||
}
|
||||
|
||||
func TestSimpleMap(t *testing.T) {
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package merkle
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
amino "github.com/tendermint/go-amino"
|
||||
)
|
||||
|
||||
// Tree is a Merkle tree interface.
|
||||
|
@ -30,20 +31,8 @@ type Hasher interface {
|
|||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
// NOTE: these are duplicated from go-amino so we dont need go-amino as a dep
|
||||
|
||||
// Uvarint length prefixed byteslice
|
||||
func encodeByteSlice(w io.Writer, bz []byte) (err error) {
|
||||
err = encodeUvarint(w, uint64(len(bz)))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
_, err = w.Write(bz)
|
||||
return
|
||||
}
|
||||
|
||||
func encodeUvarint(w io.Writer, i uint64) (err error) {
|
||||
var buf [10]byte
|
||||
n := binary.PutUvarint(buf[:], i)
|
||||
_, err = w.Write(buf[0:n])
|
||||
return
|
||||
return amino.EncodeByteSlice(w, bz)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue