Browse Source

merkle: use amino for byteslice encoding

pull/1782/head
Ethan Buchman 7 years ago
parent
commit
52bd867fd9
3 changed files with 12 additions and 26 deletions
  1. +7
    -8
      merkle/simple_map.go
  2. +1
    -3
      merkle/simple_map_test.go
  3. +4
    -15
      merkle/types.go

+ 7
- 8
merkle/simple_map.go View File

@ -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)
}


+ 1
- 3
merkle/simple_map_test.go View File

@ -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) {


+ 4
- 15
merkle/types.go View File

@ -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…
Cancel
Save