Skip to content

Commit

Permalink
Determine node type according to children field
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed May 2, 2022
1 parent b634cf2 commit 91ffe79
Show file tree
Hide file tree
Showing 24 changed files with 400 additions and 795 deletions.
1 change: 0 additions & 1 deletion dot/state/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ func TestGetStorageChildAndGetStorageFromChild(t *testing.T) {
))

trieRoot := &node.Node{
Type: node.Leaf,
Key: []byte{1, 2},
Value: []byte{3, 4},
Dirty: true,
Expand Down
6 changes: 2 additions & 4 deletions dot/state/tries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,13 @@ func Test_Tries_get(t *testing.T) {
tries: &Tries{
rootToTrie: map[common.Hash]*trie.Trie{
{1, 2, 3}: trie.NewTrie(&node.Node{
Type: node.Leaf,
Key: []byte{1, 2, 3},
Key: []byte{1, 2, 3},
}),
},
},
root: common.Hash{1, 2, 3},
trie: trie.NewTrie(&node.Node{
Type: node.Leaf,
Key: []byte{1, 2, 3},
Key: []byte{1, 2, 3},
}),
},
"not found in map": {
Expand Down
14 changes: 7 additions & 7 deletions internal/trie/node/branch_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func encodeChildrenOpportunisticParallel(children []*Node, buffer io.Writer) (er
resultsCh := make(chan encodingAsyncResult, ChildrenCapacity)

for i, child := range children {
if child == nil || child.Type == Leaf {
if child == nil || child.Type() == Leaf {
runEncodeChild(child, i, resultsCh, nil)
continue
}
Expand Down Expand Up @@ -153,12 +153,12 @@ func scaleEncodeHash(node *Node) (encoding []byte, err error) {

err = hashNode(node, buffer)
if err != nil {
return nil, fmt.Errorf("cannot hash %s: %w", node.Type, err)
return nil, fmt.Errorf("cannot hash %s: %w", node.Type(), err)
}

encoding, err = scale.Marshal(buffer.Bytes())
if err != nil {
return nil, fmt.Errorf("cannot scale encode hashed %s: %w", node.Type, err)
return nil, fmt.Errorf("cannot scale encode hashed %s: %w", node.Type(), err)
}

return encoding, nil
Expand All @@ -171,14 +171,14 @@ func hashNode(node *Node, digestWriter io.Writer) (err error) {

err = node.Encode(encodingBuffer)
if err != nil {
return fmt.Errorf("cannot encode %s: %w", node.Type, err)
return fmt.Errorf("cannot encode %s: %w", node.Type(), err)
}

// if length of encoded leaf is less than 32 bytes, do not hash
if encodingBuffer.Len() < 32 {
_, err = digestWriter.Write(encodingBuffer.Bytes())
if err != nil {
return fmt.Errorf("cannot write encoded %s to buffer: %w", node.Type, err)
return fmt.Errorf("cannot write encoded %s to buffer: %w", node.Type(), err)
}
return nil
}
Expand All @@ -191,12 +191,12 @@ func hashNode(node *Node, digestWriter io.Writer) (err error) {
// Note: using the sync.Pool's buffer is useful here.
_, err = hasher.Write(encodingBuffer.Bytes())
if err != nil {
return fmt.Errorf("cannot hash encoding of %s: %w", node.Type, err)
return fmt.Errorf("cannot hash encoding of %s: %w", node.Type(), err)
}

_, err = digestWriter.Write(hasher.Sum(nil))
if err != nil {
return fmt.Errorf("cannot write hash sum of %s to buffer: %w", node.Type, err)
return fmt.Errorf("cannot write hash sum of %s to buffer: %w", node.Type(), err)
}
return nil
}
77 changes: 31 additions & 46 deletions internal/trie/node/branch_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ func Test_hashNode(t *testing.T) {
}{
"small leaf buffer write error": {
node: &Node{
Type: Leaf,
Encoding: []byte{1, 2, 3},
},
write: writeCall{
Expand All @@ -37,7 +36,6 @@ func Test_hashNode(t *testing.T) {
},
"small leaf success": {
node: &Node{
Type: Leaf,
Encoding: []byte{1, 2, 3},
},
write: writeCall{
Expand All @@ -46,7 +44,6 @@ func Test_hashNode(t *testing.T) {
},
"leaf hash sum buffer write error": {
node: &Node{
Type: Leaf,
Encoding: []byte{
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
Expand All @@ -70,7 +67,6 @@ func Test_hashNode(t *testing.T) {
},
"leaf hash sum success": {
node: &Node{
Type: Leaf,
Encoding: []byte{
1, 2, 3, 4, 5, 6, 7, 8,
1, 2, 3, 4, 5, 6, 7, 8,
Expand All @@ -90,25 +86,25 @@ func Test_hashNode(t *testing.T) {
},
"empty branch": {
node: &Node{
Type: Branch,
Children: make([]*Node, ChildrenCapacity),
},
write: writeCall{
written: []byte{128, 0, 0},
},
},
"less than 32 bytes encoding": {
node: &Node{
Type: Branch,
Key: []byte{1, 2},
Children: make([]*Node, ChildrenCapacity),
Key: []byte{1, 2},
},
write: writeCall{
written: []byte{130, 18, 0, 0},
},
},
"less than 32 bytes encoding write error": {
node: &Node{
Type: Branch,
Key: []byte{1, 2},
Children: make([]*Node, ChildrenCapacity),
Key: []byte{1, 2},
},
write: writeCall{
written: []byte{130, 18, 0, 0},
Expand All @@ -119,8 +115,8 @@ func Test_hashNode(t *testing.T) {
},
"more than 32 bytes encoding": {
node: &Node{
Type: Branch,
Key: repeatBytes(100, 1),
Children: make([]*Node, ChildrenCapacity),
Key: repeatBytes(100, 1),
},
write: writeCall{
written: []byte{
Expand All @@ -132,8 +128,8 @@ func Test_hashNode(t *testing.T) {
},
"more than 32 bytes encoding write error": {
node: &Node{
Type: Branch,
Key: repeatBytes(100, 1),
Children: make([]*Node, ChildrenCapacity),
Key: repeatBytes(100, 1),
},
write: writeCall{
written: []byte{
Expand Down Expand Up @@ -192,7 +188,6 @@ func populateChildren(valueSize, depth int) (children []*Node) {
if depth == 0 {
for i := range children {
children[i] = &Node{
Type: Leaf,
Key: someValue,
Value: someValue,
}
Expand All @@ -202,7 +197,6 @@ func populateChildren(valueSize, depth int) (children []*Node) {

for i := range children {
children[i] = &Node{
Type: Branch,
Key: someValue,
Value: someValue,
Children: populateChildren(valueSize, depth-1),
Expand All @@ -224,7 +218,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
"no children": {},
"first child not nil": {
children: []*Node{
{Type: Leaf, Key: []byte{1}},
{Key: []byte{1}},
},
writes: []writeCall{
{
Expand All @@ -237,7 +231,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
{Type: Leaf, Key: []byte{1}},
{Key: []byte{1}},
},
writes: []writeCall{
{
Expand All @@ -247,8 +241,8 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
},
"first two children not nil": {
children: []*Node{
{Type: Leaf, Key: []byte{1}},
{Type: Leaf, Key: []byte{2}},
{Key: []byte{1}},
{Key: []byte{2}},
},
writes: []writeCall{
{
Expand All @@ -264,9 +258,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
nil, nil, nil, nil,
nil, nil, nil, nil,
nil, nil, nil,
{Type: Leaf,
Key: []byte{1},
},
{Key: []byte{1}},
nil, nil, nil, nil,
},
writes: []writeCall{
Expand All @@ -284,10 +276,9 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
// running in parallel.
children: []*Node{
{
Type: Branch,
Key: []byte{1},
Key: []byte{1},
Children: []*Node{
{Type: Leaf, Key: []byte{1}},
{Key: []byte{1}},
},
},
},
Expand Down Expand Up @@ -335,7 +326,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
children := make([]*Node, ChildrenCapacity)
for i := range children {
children[i] = &Node{
Type: Branch,
Children: make([]*Node, ChildrenCapacity),
}
}

Expand Down Expand Up @@ -369,7 +360,7 @@ func Test_encodeChildrenSequentially(t *testing.T) {
"no children": {},
"first child not nil": {
children: []*Node{
{Type: Leaf, Key: []byte{1}},
{Key: []byte{1}},
},
writes: []writeCall{
{
Expand All @@ -382,7 +373,7 @@ func Test_encodeChildrenSequentially(t *testing.T) {
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
{Type: Leaf, Key: []byte{1}},
{Key: []byte{1}},
},
writes: []writeCall{
{
Expand All @@ -392,8 +383,8 @@ func Test_encodeChildrenSequentially(t *testing.T) {
},
"first two children not nil": {
children: []*Node{
{Type: Leaf, Key: []byte{1}},
{Type: Leaf, Key: []byte{2}},
{Key: []byte{1}},
{Key: []byte{2}},
},
writes: []writeCall{
{
Expand All @@ -409,9 +400,7 @@ func Test_encodeChildrenSequentially(t *testing.T) {
nil, nil, nil, nil,
nil, nil, nil, nil,
nil, nil, nil,
{Type: Leaf,
Key: []byte{1},
},
{Key: []byte{1}},
nil, nil, nil, nil,
},
writes: []writeCall{
Expand Down Expand Up @@ -469,23 +458,25 @@ func Test_encodeChild(t *testing.T) {
}{
"nil node": {},
"empty leaf child": {
child: &Node{Type: Leaf},
child: &Node{},
writeCall: true,
write: writeCall{
written: []byte{8, 64, 0},
},
},
"empty branch child": {
child: &Node{
Type: Branch},
Children: make([]*Node, ChildrenCapacity),
},
writeCall: true,
write: writeCall{
written: []byte{12, 128, 0, 0},
},
},
"buffer write error": {
child: &Node{
Type: Branch},
Children: make([]*Node, ChildrenCapacity),
},
writeCall: true,
write: writeCall{
written: []byte{12, 128, 0, 0},
Expand All @@ -496,7 +487,6 @@ func Test_encodeChild(t *testing.T) {
},
"leaf child": {
child: &Node{
Type: Leaf,
Key: []byte{1},
Value: []byte{2},
},
Expand All @@ -507,12 +497,10 @@ func Test_encodeChild(t *testing.T) {
},
"branch child": {
child: &Node{
Type: Branch,
Key: []byte{1},
Value: []byte{2},
Children: []*Node{
nil, nil, {Type: Leaf,
Key: []byte{5},
nil, nil, {Key: []byte{5},
Value: []byte{6},
},
},
Expand Down Expand Up @@ -560,24 +548,21 @@ func Test_scaleEncodeHash(t *testing.T) {
errMessage string
}{
"empty leaf": {
node: &Node{
Type: Leaf,
},
node: &Node{},
encoding: []byte{0x8, 0x40, 0},
},
"empty branch": {
node: &Node{
Type: Branch,
Children: make([]*Node, ChildrenCapacity),
},
encoding: []byte{0xc, 0x80, 0x0, 0x0},
},
"non empty branch": {
node: &Node{
Type: Branch,
Key: []byte{1, 2},
Value: []byte{3, 4},
Children: []*Node{
nil, nil, {Type: Leaf, Key: []byte{9}},
nil, nil, {Key: []byte{9}},
},
},
encoding: []byte{0x2c, 0xc2, 0x12, 0x4, 0x0, 0x8, 0x3, 0x4, 0xc, 0x41, 0x9, 0x0},
Expand Down
4 changes: 0 additions & 4 deletions internal/trie/node/branch_test.go

This file was deleted.

3 changes: 1 addition & 2 deletions internal/trie/node/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ type CopySettings struct {
// children as well.
func (n *Node) Copy(settings CopySettings) *Node {
cpy := &Node{
Type: n.Type,
Dirty: n.Dirty,
Generation: n.Generation,
}

if n.Type == Branch {
if n.Type() == Branch {
if settings.CopyChildren {
// Copy all fields of children if we deep copy children
childSettings := settings
Expand Down
Loading

0 comments on commit 91ffe79

Please sign in to comment.