Skip to content

Commit

Permalink
Split MerkleValue in MerkleValue + MerkleValueRoot to satisfy D…
Browse files Browse the repository at this point in the history
…eepSource
  • Loading branch information
qdm12 committed Aug 1, 2022
1 parent 5be342e commit ef713c4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 21 deletions.
32 changes: 18 additions & 14 deletions internal/trie/node/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,32 @@ func MerkleValueRoot(rootEncoding []byte, writer io.Writer) (err error) {
return nil
}

// MerkleValue returns the Merkle value of the node.
// If the node encoding is less or equal to 32 bytes,
// the encoding is the Merkle value.
// Otherwise, the Blake2b hash digest of the encoding
// is returned as the Merkle value.
func (n *Node) MerkleValue(isRoot bool) (merkleValue []byte, err error) {
// MerkleValue returns the Merkle value of the non-root node.
func (n *Node) MerkleValue() (merkleValue []byte, err error) {
if !n.Dirty && n.HashDigest != nil {
return n.HashDigest, nil
}

if isRoot {
_, merkleValue, err = n.EncodeAndHashRoot()
if err != nil {
return nil, fmt.Errorf("encoding and hashing root node: %w", err)
}
return merkleValue, nil
}

_, merkleValue, err = n.EncodeAndHash()
if err != nil {
return nil, fmt.Errorf("encoding and hashing node: %w", err)
}

return merkleValue, nil
}

// MerkleValueRoot returns the Merkle value of the root node.
func (n *Node) MerkleValueRoot() (merkleValue []byte, err error) {
const rootMerkleValueLength = 32
if !n.Dirty && len(n.HashDigest) == rootMerkleValueLength {
return n.HashDigest, nil
}

_, merkleValue, err = n.EncodeAndHashRoot()
if err != nil {
return nil, fmt.Errorf("encoding and hashing root node: %w", err)
}

return merkleValue, nil
}

Expand Down
56 changes: 52 additions & 4 deletions internal/trie/node/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ func Test_Node_MerkleValue(t *testing.T) {

testCases := map[string]struct {
node Node
isRoot bool
merkleValue []byte
errWrapped error
errMessage string
Expand All @@ -184,17 +183,66 @@ func Test_Node_MerkleValue(t *testing.T) {
},
merkleValue: []byte{1},
},
"non root small encoding": {
"small encoding": {
node: Node{
Encoding: []byte{1},
},
merkleValue: []byte{1},
},
}

for name, testCase := range testCases {
testCase := testCase
t.Run(name, func(t *testing.T) {
t.Parallel()

merkleValue, err := testCase.node.MerkleValue()

assert.ErrorIs(t, err, testCase.errWrapped)
if testCase.errWrapped != nil {
assert.EqualError(t, err, testCase.errMessage)
}
assert.Equal(t, testCase.merkleValue, merkleValue)
})
}
}

func Test_Node_MerkleValueRoot(t *testing.T) {
t.Parallel()

some32BHashDigest := []byte{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x74, 0xcb, 0x6a, 0xff, 0x8c, 0x9c, 0xcd, 0xd2,
0x73, 0xc8, 0x16, 0x48, 0xff, 0x11, 0x49, 0xef,
0x36, 0xbc, 0xea, 0x6e, 0xbb, 0x8a, 0x3e, 0x25}

testCases := map[string]struct {
node Node
merkleValue []byte
errWrapped error
errMessage string
}{
"cached merkle value 32 bytes": {
node: Node{
HashDigest: some32BHashDigest,
},
merkleValue: some32BHashDigest,
},
"cached merkle value not 32 bytes": {
node: Node{
Encoding: []byte{1},
HashDigest: []byte{1},
},
merkleValue: []byte{
0xee, 0x15, 0x5a, 0xce, 0x9c, 0x40, 0x29, 0x20,
0x74, 0xcb, 0x6a, 0xff, 0x8c, 0x9c, 0xcd, 0xd2,
0x73, 0xc8, 0x16, 0x48, 0xff, 0x11, 0x49, 0xef,
0x36, 0xbc, 0xea, 0x6e, 0xbb, 0x8a, 0x3e, 0x25},
},
"root small encoding": {
node: Node{
Encoding: []byte{1},
},
isRoot: true,
merkleValue: []byte{
0xee, 0x15, 0x5a, 0xce, 0x9c, 0x40, 0x29, 0x20,
0x74, 0xcb, 0x6a, 0xff, 0x8c, 0x9c, 0xcd, 0xd2,
Expand All @@ -208,7 +256,7 @@ func Test_Node_MerkleValue(t *testing.T) {
t.Run(name, func(t *testing.T) {
t.Parallel()

merkleValue, err := testCase.node.MerkleValue(testCase.isRoot)
merkleValue, err := testCase.node.MerkleValueRoot()

assert.ErrorIs(t, err, testCase.errWrapped)
if testCase.errWrapped != nil {
Expand Down
10 changes: 7 additions & 3 deletions lib/trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,7 @@ func (t *Trie) loadNode(db Database, n *Node) error {
if len(hash) == 0 {
// node has already been loaded inline
// just set encoding + hash digest
const isRoot = false
_, err := child.MerkleValue(isRoot)
_, err := child.MerkleValue()
if err != nil {
return fmt.Errorf("merkle value: %w", err)
}
Expand Down Expand Up @@ -398,7 +397,12 @@ func (t *Trie) getInsertedNodeHashesAtNode(n *Node, hashes map[common.Hash]struc
return nil
}

hash, err := n.MerkleValue(n == t.root)
var hash []byte
if n == t.root {
hash, err = n.MerkleValueRoot()
} else {
hash, err = n.MerkleValue()
}
if err != nil {
return fmt.Errorf(
"cannot encode and hash node with hash 0x%x: %w",
Expand Down

0 comments on commit ef713c4

Please sign in to comment.