From 2d09a48e9afca848c8e03886cdaa9e9a4c6089c9 Mon Sep 17 00:00:00 2001 From: lukesolo Date: Tue, 28 Aug 2018 10:54:12 +0300 Subject: [PATCH] One node hash fix --- .gitignore | 4 ++++ trie.go | 3 ++- trie_test.go | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1feae78..9fb26ca 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,5 @@ +# IDE +.idea + +# Binary *.exe diff --git a/trie.go b/trie.go index 52cdc36..c9a783d 100644 --- a/trie.go +++ b/trie.go @@ -71,7 +71,8 @@ func (t *MerkleTrie) MaxDepth() byte { func hash(n *node) []byte { if n.key != nil { h := sha256.New() - return h.Sum(n.value) + h.Write(n.value) + return h.Sum(nil) } if n.left == nil { return hash(n.right) diff --git a/trie_test.go b/trie_test.go index a28d8de..0b51e8f 100644 --- a/trie_test.go +++ b/trie_test.go @@ -29,6 +29,15 @@ func ExampleEmptyMerkleTrieHash() { // e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 } +func ExampleOneNode() { + tree := trie.NewMerkleTrie() + tree.Add([]byte{1}, []byte{2}) + fmt.Printf("%x\n", tree.Hash()) + + // Output: + // dbc1b4c900ffe48d575b5da5c638040125f65db0fe3e24494b76ea986457d986 +} + func ExampleSetHighBit() { tree := trie.NewMerkleTrie() tree.Add(zero(), nil)