Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Oct 5, 2022
1 parent 7aa5ea7 commit f848961
Show file tree
Hide file tree
Showing 6 changed files with 450 additions and 89 deletions.
3 changes: 2 additions & 1 deletion dot/state/offline_pruner.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ func (p *OfflinePruner) SetBloomFilter() (err error) {
return err
}

trie.PopulateMerkleValues(tr.RootNode(), merkleValues)
const excludeInlined = false
trie.PopulateMerkleValues(tr.RootNode(), merkleValues, excludeInlined)

// get parent header of current block
header, err = p.blockState.GetHeader(header.ParentHash)
Expand Down
9 changes: 6 additions & 3 deletions lib/trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (t *Trie) loadNode(db Database, n *Node) error {
// all its descendant nodes as keys to the map merkleValues.
// It is assumed the node and its descendant nodes have their Merkle value already
// computed.
func PopulateMerkleValues(n *Node, merkleValues map[string]struct{}) {
func PopulateMerkleValues(n *Node, merkleValues map[string]struct{}, excludeInlined bool) {
if n == nil {
return
}
Expand All @@ -198,15 +198,18 @@ func PopulateMerkleValues(n *Node, merkleValues map[string]struct{}) {
panic(fmt.Sprintf("node with key 0x%x has no Merkle value computed", n.Key))
}

merkleValues[string(n.MerkleValue)] = struct{}{}
isInlined := len(n.MerkleValue) < 32
if !excludeInlined || !isInlined {
merkleValues[string(n.MerkleValue)] = struct{}{}
}

if n.Kind() == node.Leaf {
return
}

branch := n
for _, child := range branch.Children {
PopulateMerkleValues(child, merkleValues)
PopulateMerkleValues(child, merkleValues, excludeInlined)
}
}

Expand Down
21 changes: 16 additions & 5 deletions lib/trie/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ func Test_PopulateMerkleValues(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
node *Node
merkleValues map[string]struct{}
panicValue interface{}
node *Node
excludeInlined bool
merkleValues map[string]struct{}
panicValue interface{}
}{
"nil node": {
merkleValues: map[string]struct{}{},
Expand All @@ -191,6 +192,16 @@ func Test_PopulateMerkleValues(t *testing.T) {
"b": {},
},
},
"exclude inlined": {
node: &Node{
MerkleValue: []byte("a"),
Children: padRightChildren([]*Node{
{MerkleValue: []byte("b")},
}),
},
excludeInlined: true,
merkleValues: map[string]struct{}{},
},
"nested branch node": {
node: &Node{
MerkleValue: []byte("a"),
Expand Down Expand Up @@ -222,12 +233,12 @@ func Test_PopulateMerkleValues(t *testing.T) {

if testCase.panicValue != nil {
assert.PanicsWithValue(t, testCase.panicValue, func() {
PopulateMerkleValues(testCase.node, merkleValues)
PopulateMerkleValues(testCase.node, merkleValues, testCase.excludeInlined)
})
return
}

PopulateMerkleValues(testCase.node, merkleValues)
PopulateMerkleValues(testCase.node, merkleValues, testCase.excludeInlined)

assert.Equal(t, testCase.merkleValues, merkleValues)
})
Expand Down
Loading

0 comments on commit f848961

Please sign in to comment.