Skip to content

Commit

Permalink
chore(trie): Key -> PartialKey for nodes (#2916)
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 authored Nov 15, 2022
1 parent b2a8a86 commit 94dcef8
Show file tree
Hide file tree
Showing 25 changed files with 1,025 additions and 1,025 deletions.
6 changes: 3 additions & 3 deletions dot/state/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ func TestGetStorageChildAndGetStorageFromChild(t *testing.T) {
))

trieRoot := &node.Node{
Key: []byte{1, 2},
SubValue: []byte{3, 4},
Dirty: true,
PartialKey: []byte{1, 2},
SubValue: []byte{3, 4},
Dirty: true,
}
testChildTrie := trie.NewTrie(trieRoot)

Expand Down
10 changes: 5 additions & 5 deletions dot/state/tries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Test_Tries_SetEmptyTrie(t *testing.T) {
func Test_Tries_SetTrie(t *testing.T) {
t.Parallel()

tr := trie.NewTrie(&node.Node{Key: []byte{1}})
tr := trie.NewTrie(&node.Node{PartialKey: []byte{1}})

tries := NewTries()
tries.SetTrie(tr)
Expand Down Expand Up @@ -201,15 +201,15 @@ func Test_Tries_get(t *testing.T) {
tries: &Tries{
rootToTrie: map[common.Hash]*trie.Trie{
{1, 2, 3}: trie.NewTrie(&node.Node{
Key: []byte{1, 2, 3},
SubValue: []byte{1},
PartialKey: []byte{1, 2, 3},
SubValue: []byte{1},
}),
},
},
root: common.Hash{1, 2, 3},
trie: trie.NewTrie(&node.Node{
Key: []byte{1, 2, 3},
SubValue: []byte{1},
PartialKey: []byte{1, 2, 3},
SubValue: []byte{1},
}),
},
"not found in map": {
Expand Down
40 changes: 20 additions & 20 deletions internal/trie/node/branch_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ func populateChildren(valueSize, depth int) (children []*Node) {
if depth == 0 {
for i := range children {
children[i] = &Node{
Key: someValue,
SubValue: someValue,
PartialKey: someValue,
SubValue: someValue,
}
}
return children
}

for i := range children {
children[i] = &Node{
Key: someValue,
SubValue: someValue,
Children: populateChildren(valueSize, depth-1),
PartialKey: someValue,
SubValue: someValue,
Children: populateChildren(valueSize, depth-1),
}
}

Expand All @@ -65,7 +65,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
"no children": {},
"first child not nil": {
children: []*Node{
{Key: []byte{1}, SubValue: []byte{2}},
{PartialKey: []byte{1}, SubValue: []byte{2}},
},
writes: []writeCall{
{
Expand All @@ -78,7 +78,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
{Key: []byte{1}, SubValue: []byte{2}},
{PartialKey: []byte{1}, SubValue: []byte{2}},
},
writes: []writeCall{
{
Expand All @@ -88,8 +88,8 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
},
"first two children not nil": {
children: []*Node{
{Key: []byte{1}, SubValue: []byte{2}},
{Key: []byte{3}, SubValue: []byte{4}},
{PartialKey: []byte{1}, SubValue: []byte{2}},
{PartialKey: []byte{3}, SubValue: []byte{4}},
},
writes: []writeCall{
{
Expand All @@ -105,7 +105,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
nil, nil, nil, nil,
nil, nil, nil, nil,
nil, nil, nil,
{Key: []byte{1}, SubValue: []byte{2}},
{PartialKey: []byte{1}, SubValue: []byte{2}},
nil, nil, nil, nil,
},
writes: []writeCall{
Expand Down Expand Up @@ -192,7 +192,7 @@ func Test_encodeChildrenSequentially(t *testing.T) {
"no children": {},
"first child not nil": {
children: []*Node{
{Key: []byte{1}, SubValue: []byte{2}},
{PartialKey: []byte{1}, SubValue: []byte{2}},
},
writes: []writeCall{
{written: []byte{16}},
Expand All @@ -204,7 +204,7 @@ func Test_encodeChildrenSequentially(t *testing.T) {
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
{Key: []byte{1}, SubValue: []byte{2}},
{PartialKey: []byte{1}, SubValue: []byte{2}},
},
writes: []writeCall{
{written: []byte{16}},
Expand All @@ -213,8 +213,8 @@ func Test_encodeChildrenSequentially(t *testing.T) {
},
"first two children not nil": {
children: []*Node{
{Key: []byte{1}, SubValue: []byte{2}},
{Key: []byte{3}, SubValue: []byte{4}},
{PartialKey: []byte{1}, SubValue: []byte{2}},
{PartialKey: []byte{3}, SubValue: []byte{4}},
},
writes: []writeCall{
{written: []byte{16}},
Expand All @@ -228,7 +228,7 @@ func Test_encodeChildrenSequentially(t *testing.T) {
nil, nil, nil, nil,
nil, nil, nil, nil,
nil, nil, nil,
{Key: []byte{1}, SubValue: []byte{2}},
{PartialKey: []byte{1}, SubValue: []byte{2}},
nil, nil, nil, nil,
},
writes: []writeCall{
Expand Down Expand Up @@ -306,8 +306,8 @@ func Test_encodeChild(t *testing.T) {
},
"leaf child": {
child: &Node{
Key: []byte{1},
SubValue: []byte{2},
PartialKey: []byte{1},
SubValue: []byte{2},
},
writes: []writeCall{
{written: []byte{16}},
Expand All @@ -316,10 +316,10 @@ func Test_encodeChild(t *testing.T) {
},
"branch child": {
child: &Node{
Key: []byte{1},
SubValue: []byte{2},
PartialKey: []byte{1},
SubValue: []byte{2},
Children: []*Node{
nil, nil, {Key: []byte{5},
nil, nil, {PartialKey: []byte{5},
SubValue: []byte{6},
},
},
Expand Down
6 changes: 3 additions & 3 deletions internal/trie/node/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ func (n *Node) Copy(settings CopySettings) *Node {
}
}

if settings.CopyKey && n.Key != nil {
cpy.Key = make([]byte, len(n.Key))
copy(cpy.Key, n.Key)
if settings.CopyKey && n.PartialKey != nil {
cpy.PartialKey = make([]byte, len(n.PartialKey))
copy(cpy.PartialKey, n.PartialKey)
}

// nil and []byte{} values for branches result in a different node encoding,
Expand Down
56 changes: 28 additions & 28 deletions internal/trie/node/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ func Test_Node_Copy(t *testing.T) {
},
"non empty branch": {
node: &Node{
Key: []byte{1, 2},
SubValue: []byte{3, 4},
PartialKey: []byte{1, 2},
SubValue: []byte{3, 4},
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
SubValue: []byte{1},
PartialKey: []byte{9},
SubValue: []byte{1},
},
}),
Dirty: true,
MerkleValue: []byte{5},
},
settings: DefaultCopySettings,
expectedNode: &Node{
Key: []byte{1, 2},
SubValue: []byte{3, 4},
PartialKey: []byte{1, 2},
SubValue: []byte{3, 4},
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
SubValue: []byte{1},
PartialKey: []byte{9},
SubValue: []byte{1},
},
}),
Dirty: true,
Expand All @@ -66,8 +66,8 @@ func Test_Node_Copy(t *testing.T) {
node: &Node{
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
SubValue: []byte{1},
PartialKey: []byte{9},
SubValue: []byte{1},
},
}),
},
Expand All @@ -77,33 +77,33 @@ func Test_Node_Copy(t *testing.T) {
expectedNode: &Node{
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
SubValue: []byte{1},
PartialKey: []byte{9},
SubValue: []byte{1},
},
}),
},
},
"deep copy branch": {
node: &Node{
Key: []byte{1, 2},
SubValue: []byte{3, 4},
PartialKey: []byte{1, 2},
SubValue: []byte{3, 4},
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
SubValue: []byte{1},
PartialKey: []byte{9},
SubValue: []byte{1},
},
}),
Dirty: true,
MerkleValue: []byte{5},
},
settings: DeepCopySettings,
expectedNode: &Node{
Key: []byte{1, 2},
SubValue: []byte{3, 4},
PartialKey: []byte{1, 2},
SubValue: []byte{3, 4},
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
SubValue: []byte{1},
PartialKey: []byte{9},
SubValue: []byte{1},
},
}),
Dirty: true,
Expand All @@ -112,28 +112,28 @@ func Test_Node_Copy(t *testing.T) {
},
"non empty leaf": {
node: &Node{
Key: []byte{1, 2},
PartialKey: []byte{1, 2},
SubValue: []byte{3, 4},
Dirty: true,
MerkleValue: []byte{5},
},
settings: DefaultCopySettings,
expectedNode: &Node{
Key: []byte{1, 2},
SubValue: []byte{3, 4},
Dirty: true,
PartialKey: []byte{1, 2},
SubValue: []byte{3, 4},
Dirty: true,
},
},
"deep copy leaf": {
node: &Node{
Key: []byte{1, 2},
PartialKey: []byte{1, 2},
SubValue: []byte{3, 4},
Dirty: true,
MerkleValue: []byte{5},
},
settings: DeepCopySettings,
expectedNode: &Node{
Key: []byte{1, 2},
PartialKey: []byte{1, 2},
SubValue: []byte{3, 4},
Dirty: true,
MerkleValue: []byte{5},
Expand All @@ -149,12 +149,12 @@ func Test_Node_Copy(t *testing.T) {
nodeCopy := testCase.node.Copy(testCase.settings)

assert.Equal(t, testCase.expectedNode, nodeCopy)
testForSliceModif(t, testCase.node.Key, nodeCopy.Key)
testForSliceModif(t, testCase.node.PartialKey, nodeCopy.PartialKey)
testForSliceModif(t, testCase.node.SubValue, nodeCopy.SubValue)
testForSliceModif(t, testCase.node.MerkleValue, nodeCopy.MerkleValue)

if testCase.node.Kind() == Branch {
testCase.node.Children[15] = &Node{Key: []byte("modified")}
testCase.node.Children[15] = &Node{PartialKey: []byte("modified")}
assert.NotEqual(t, nodeCopy.Children, testCase.node.Children)
}
})
Expand Down
4 changes: 2 additions & 2 deletions internal/trie/node/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func decodeBranch(reader io.Reader, variant byte, partialKeyLength uint16) (
Children: make([]*Node, ChildrenCapacity),
}

node.Key, err = decodeKey(reader, partialKeyLength)
node.PartialKey, err = decodeKey(reader, partialKeyLength)
if err != nil {
return nil, fmt.Errorf("cannot decode key: %w", err)
}
Expand Down Expand Up @@ -126,7 +126,7 @@ func decodeBranch(reader io.Reader, variant byte, partialKeyLength uint16) (
func decodeLeaf(reader io.Reader, partialKeyLength uint16) (node *Node, err error) {
node = &Node{}

node.Key, err = decodeKey(reader, partialKeyLength)
node.PartialKey, err = decodeKey(reader, partialKeyLength)
if err != nil {
return nil, fmt.Errorf("cannot decode key: %w", err)
}
Expand Down
Loading

0 comments on commit 94dcef8

Please sign in to comment.