Skip to content

Commit

Permalink
ensureMerkleValueIsCalculated changes
Browse files Browse the repository at this point in the history
- Pure function without trie receiver
- Add missing test cases
  • Loading branch information
qdm12 committed Oct 11, 2022
1 parent 11815d9 commit d4808ec
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 11 deletions.
8 changes: 4 additions & 4 deletions lib/trie/trie.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (t *Trie) registerDeletedMerkleValue(node *Node, isRoot bool) (err error) {
return nil
}

err = t.ensureMerkleValueIsCalculated(node, isRoot)
err = ensureMerkleValueIsCalculated(node, isRoot)
if err != nil {
return fmt.Errorf("ensuring Merkle value is calculated: %w", err)
}
Expand Down Expand Up @@ -1004,7 +1004,7 @@ func (t *Trie) deleteNodesLimit(parent *Node, limit uint32) (
func (t *Trie) ClearPrefix(prefixLE []byte) (err error) {
if len(prefixLE) == 0 {
const isRoot = true
err = t.ensureMerkleValueIsCalculated(t.root, isRoot)
err = ensureMerkleValueIsCalculated(t.root, isRoot)
if err != nil {
return fmt.Errorf("ensuring Merkle values are calculated: %w", err)
}
Expand Down Expand Up @@ -1035,7 +1035,7 @@ func (t *Trie) clearPrefixAtNode(parent *Node, prefix []byte) (

if bytes.HasPrefix(parent.Key, prefix) {
isRoot := parent == t.root
err = t.ensureMerkleValueIsCalculated(parent, isRoot)
err = ensureMerkleValueIsCalculated(parent, isRoot)
if err != nil {
nodesRemoved = 0
return parent, nodesRemoved, fmt.Errorf("ensuring Merkle values are calculated: %w", err)
Expand Down Expand Up @@ -1345,7 +1345,7 @@ func (t *Trie) handleDeletion(branch *Node, key []byte) (
// to ensure the parent node and all its descendant nodes have their Merkle
// value computed and ready to be used. This has a close to zero performance
// impact if the parent node Merkle value is already computed.
func (t *Trie) ensureMerkleValueIsCalculated(parent *Node, isRoot bool) (err error) {
func ensureMerkleValueIsCalculated(parent *Node, isRoot bool) (err error) {
if parent == nil {
return nil
}
Expand Down
95 changes: 88 additions & 7 deletions lib/trie/trie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4053,25 +4053,105 @@ func Test_handleDeletion(t *testing.T) {
}
}

func Test_Trie_ensureMerkleValueIsCalculated(t *testing.T) {
func Test_ensureMerkleValueIsCalculated(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
trie Trie
parent *Node
isRoot bool
errSentinel error
errMessage string
parent *Node
isRoot bool
errSentinel error
errMessage string
expectedNode *Node
}{
"nil parent": {},
"root node without Merkle value": {
parent: &Node{
Key: []byte{1},
SubValue: []byte{2},
},
isRoot: true,
expectedNode: &Node{
Key: []byte{1},
SubValue: []byte{2},
Encoding: []byte{0x41, 0x01, 0x04, 0x02},
MerkleValue: []byte{
0x60, 0x51, 0x6d, 0xb, 0xb6, 0xe1, 0xbb, 0xfb,
0x12, 0x93, 0xf1, 0xb2, 0x76, 0xea, 0x95, 0x5,
0xe9, 0xf4, 0xa4, 0xe7, 0xd9, 0x8f, 0x62, 0xd,
0x5, 0x11, 0x5e, 0xb, 0x85, 0x27, 0x4a, 0xe1},
},
},
"root node with inlined Merkle value": {
parent: &Node{
Key: []byte{1},
SubValue: []byte{2},
MerkleValue: []byte{3},
},
isRoot: true,
expectedNode: &Node{
Key: []byte{1},
SubValue: []byte{2},
Encoding: []byte{0x41, 0x01, 0x04, 0x02},
MerkleValue: []byte{
0x60, 0x51, 0x6d, 0xb, 0xb6, 0xe1, 0xbb, 0xfb,
0x12, 0x93, 0xf1, 0xb2, 0x76, 0xea, 0x95, 0x5,
0xe9, 0xf4, 0xa4, 0xe7, 0xd9, 0x8f, 0x62, 0xd,
0x5, 0x11, 0x5e, 0xb, 0x85, 0x27, 0x4a, 0xe1},
},
},
"root node with hash Merkle value": {
parent: &Node{
Key: []byte{1},
SubValue: []byte{2},
MerkleValue: []byte{
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8},
},
isRoot: true,
expectedNode: &Node{
Key: []byte{1},
SubValue: []byte{2},
MerkleValue: []byte{
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8},
},
},
"non root node without Merkle value": {
parent: &Node{
Key: []byte{1},
SubValue: []byte{2},
},
expectedNode: &Node{
Key: []byte{1},
SubValue: []byte{2},
Encoding: []byte{0x41, 0x01, 0x04, 0x02},
MerkleValue: []byte{0x41, 0x1, 0x4, 0x2},
},
},
"non root node with Merkle value": {
parent: &Node{
Key: []byte{1},
SubValue: []byte{2},
MerkleValue: []byte{3},
},
expectedNode: &Node{
Key: []byte{1},
SubValue: []byte{2},
MerkleValue: []byte{3},
},
},
}

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

err := testCase.trie.ensureMerkleValueIsCalculated(testCase.parent, testCase.isRoot)
err := ensureMerkleValueIsCalculated(testCase.parent, testCase.isRoot)

// Ensure all Merkle values are set in the trie rooted
// at the parent node.
Expand All @@ -4085,6 +4165,7 @@ func Test_Trie_ensureMerkleValueIsCalculated(t *testing.T) {
if testCase.errSentinel != nil {
assert.EqualError(t, err, testCase.errMessage)
}
assert.Equal(t, testCase.expectedNode, testCase.parent)
})
}
}
Expand Down

0 comments on commit d4808ec

Please sign in to comment.