Skip to content

Commit

Permalink
chore(trie): rename Node Value field to SubValue (#2654)
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 authored Jul 28, 2022
1 parent 75fcd75 commit b42b867
Show file tree
Hide file tree
Showing 23 changed files with 890 additions and 890 deletions.
6 changes: 3 additions & 3 deletions dot/state/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ func TestGetStorageChildAndGetStorageFromChild(t *testing.T) {
))

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

Expand Down
8 changes: 4 additions & 4 deletions dot/state/tries_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,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},
Value: []byte{1},
Key: []byte{1, 2, 3},
SubValue: []byte{1},
}),
},
},
root: common.Hash{1, 2, 3},
trie: trie.NewTrie(&node.Node{
Key: []byte{1, 2, 3},
Value: []byte{1},
Key: []byte{1, 2, 3},
SubValue: []byte{1},
}),
},
"not found in map": {
Expand Down
44 changes: 22 additions & 22 deletions internal/trie/node/branch_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ func populateChildren(valueSize, depth int) (children []*Node) {
if depth == 0 {
for i := range children {
children[i] = &Node{
Key: someValue,
Value: someValue,
Key: someValue,
SubValue: someValue,
}
}
return children
Expand All @@ -198,7 +198,7 @@ func populateChildren(valueSize, depth int) (children []*Node) {
for i := range children {
children[i] = &Node{
Key: someValue,
Value: someValue,
SubValue: someValue,
Children: populateChildren(valueSize, depth-1),
}
}
Expand All @@ -218,7 +218,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
"no children": {},
"first child not nil": {
children: []*Node{
{Key: []byte{1}, Value: []byte{2}},
{Key: []byte{1}, SubValue: []byte{2}},
},
writes: []writeCall{
{
Expand All @@ -231,7 +231,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}, Value: []byte{2}},
{Key: []byte{1}, SubValue: []byte{2}},
},
writes: []writeCall{
{
Expand All @@ -241,8 +241,8 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
},
"first two children not nil": {
children: []*Node{
{Key: []byte{1}, Value: []byte{2}},
{Key: []byte{3}, Value: []byte{4}},
{Key: []byte{1}, SubValue: []byte{2}},
{Key: []byte{3}, SubValue: []byte{4}},
},
writes: []writeCall{
{
Expand All @@ -258,7 +258,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
nil, nil, nil, nil,
nil, nil, nil, nil,
nil, nil, nil,
{Key: []byte{1}, Value: []byte{2}},
{Key: []byte{1}, SubValue: []byte{2}},
nil, nil, nil, nil,
},
writes: []writeCall{
Expand All @@ -278,7 +278,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
{
Key: []byte{1},
Children: []*Node{
{Key: []byte{1}, Value: []byte{2}},
{Key: []byte{1}, SubValue: []byte{2}},
},
},
},
Expand Down Expand Up @@ -360,7 +360,7 @@ func Test_encodeChildrenSequentially(t *testing.T) {
"no children": {},
"first child not nil": {
children: []*Node{
{Key: []byte{1}, Value: []byte{2}},
{Key: []byte{1}, SubValue: []byte{2}},
},
writes: []writeCall{
{
Expand All @@ -373,7 +373,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}, Value: []byte{2}},
{Key: []byte{1}, SubValue: []byte{2}},
},
writes: []writeCall{
{
Expand All @@ -383,8 +383,8 @@ func Test_encodeChildrenSequentially(t *testing.T) {
},
"first two children not nil": {
children: []*Node{
{Key: []byte{1}, Value: []byte{2}},
{Key: []byte{3}, Value: []byte{4}},
{Key: []byte{1}, SubValue: []byte{2}},
{Key: []byte{3}, SubValue: []byte{4}},
},
writes: []writeCall{
{
Expand All @@ -400,7 +400,7 @@ func Test_encodeChildrenSequentially(t *testing.T) {
nil, nil, nil, nil,
nil, nil, nil, nil,
nil, nil, nil,
{Key: []byte{1}, Value: []byte{2}},
{Key: []byte{1}, SubValue: []byte{2}},
nil, nil, nil, nil,
},
writes: []writeCall{
Expand Down Expand Up @@ -480,8 +480,8 @@ func Test_encodeChild(t *testing.T) {
},
"leaf child": {
child: &Node{
Key: []byte{1},
Value: []byte{2},
Key: []byte{1},
SubValue: []byte{2},
},
writeCall: true,
write: writeCall{
Expand All @@ -490,11 +490,11 @@ func Test_encodeChild(t *testing.T) {
},
"branch child": {
child: &Node{
Key: []byte{1},
Value: []byte{2},
Key: []byte{1},
SubValue: []byte{2},
Children: []*Node{
nil, nil, {Key: []byte{5},
Value: []byte{6},
SubValue: []byte{6},
},
},
},
Expand Down Expand Up @@ -542,10 +542,10 @@ func Test_scaleEncodeHash(t *testing.T) {
}{
"branch": {
node: &Node{
Key: []byte{1, 2},
Value: []byte{3, 4},
Key: []byte{1, 2},
SubValue: []byte{3, 4},
Children: []*Node{
nil, nil, {Key: []byte{9}, Value: []byte{1}},
nil, nil, {Key: []byte{9}, SubValue: []byte{1}},
},
},
encoding: []byte{0x30, 0xc2, 0x12, 0x4, 0x0, 0x8, 0x3, 0x4, 0x10, 0x41, 0x9, 0x4, 0x1},
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 @@ -88,9 +88,9 @@ func (n *Node) Copy(settings CopySettings) *Node {
}

// nil and []byte{} are encoded differently, watch out!
if settings.CopyValue && n.Value != nil {
cpy.Value = make([]byte, len(n.Value))
copy(cpy.Value, n.Value)
if settings.CopyValue && n.SubValue != nil {
cpy.SubValue = make([]byte, len(n.SubValue))
copy(cpy.SubValue, n.SubValue)
}

if settings.CopyCached {
Expand Down
54 changes: 27 additions & 27 deletions internal/trie/node/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ func Test_Node_Copy(t *testing.T) {
},
"non empty branch": {
node: &Node{
Key: []byte{1, 2},
Value: []byte{3, 4},
Key: []byte{1, 2},
SubValue: []byte{3, 4},
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
Value: []byte{1},
Key: []byte{9},
SubValue: []byte{1},
},
}),
Dirty: true,
Expand All @@ -52,12 +52,12 @@ func Test_Node_Copy(t *testing.T) {
},
settings: DefaultCopySettings,
expectedNode: &Node{
Key: []byte{1, 2},
Value: []byte{3, 4},
Key: []byte{1, 2},
SubValue: []byte{3, 4},
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
Value: []byte{1},
Key: []byte{9},
SubValue: []byte{1},
},
}),
Dirty: true,
Expand All @@ -67,8 +67,8 @@ func Test_Node_Copy(t *testing.T) {
node: &Node{
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
Value: []byte{1},
Key: []byte{9},
SubValue: []byte{1},
},
}),
},
Expand All @@ -78,20 +78,20 @@ func Test_Node_Copy(t *testing.T) {
expectedNode: &Node{
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
Value: []byte{1},
Key: []byte{9},
SubValue: []byte{1},
},
}),
},
},
"deep copy branch": {
node: &Node{
Key: []byte{1, 2},
Value: []byte{3, 4},
Key: []byte{1, 2},
SubValue: []byte{3, 4},
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
Value: []byte{1},
Key: []byte{9},
SubValue: []byte{1},
},
}),
Dirty: true,
Expand All @@ -100,12 +100,12 @@ func Test_Node_Copy(t *testing.T) {
},
settings: DeepCopySettings,
expectedNode: &Node{
Key: []byte{1, 2},
Value: []byte{3, 4},
Key: []byte{1, 2},
SubValue: []byte{3, 4},
Children: padRightChildren([]*Node{
nil, nil, {
Key: []byte{9},
Value: []byte{1},
Key: []byte{9},
SubValue: []byte{1},
},
}),
Dirty: true,
Expand All @@ -116,30 +116,30 @@ func Test_Node_Copy(t *testing.T) {
"non empty leaf": {
node: &Node{
Key: []byte{1, 2},
Value: []byte{3, 4},
SubValue: []byte{3, 4},
Dirty: true,
HashDigest: []byte{5},
Encoding: []byte{6},
},
settings: DefaultCopySettings,
expectedNode: &Node{
Key: []byte{1, 2},
Value: []byte{3, 4},
Dirty: true,
Key: []byte{1, 2},
SubValue: []byte{3, 4},
Dirty: true,
},
},
"deep copy leaf": {
node: &Node{
Key: []byte{1, 2},
Value: []byte{3, 4},
SubValue: []byte{3, 4},
Dirty: true,
HashDigest: []byte{5},
Encoding: []byte{6},
},
settings: DeepCopySettings,
expectedNode: &Node{
Key: []byte{1, 2},
Value: []byte{3, 4},
SubValue: []byte{3, 4},
Dirty: true,
HashDigest: []byte{5},
Encoding: []byte{6},
Expand All @@ -156,7 +156,7 @@ func Test_Node_Copy(t *testing.T) {

assert.Equal(t, testCase.expectedNode, nodeCopy)
testForSliceModif(t, testCase.node.Key, nodeCopy.Key)
testForSliceModif(t, testCase.node.Value, nodeCopy.Value)
testForSliceModif(t, testCase.node.SubValue, nodeCopy.SubValue)
testForSliceModif(t, testCase.node.HashDigest, nodeCopy.HashDigest)
testForSliceModif(t, testCase.node.Encoding, nodeCopy.Encoding)

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 @@ -84,7 +84,7 @@ func decodeBranch(reader io.Reader, variant byte, partialKeyLength uint16) (
sd := scale.NewDecoder(reader)

if variant == branchWithValueVariant.bits {
err := sd.Decode(&node.Value)
err := sd.Decode(&node.SubValue)
if err != nil {
return nil, fmt.Errorf("%w: %s", ErrDecodeValue, err)
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func decodeLeaf(reader io.Reader, partialKeyLength uint16) (node *Node, err erro
}

if len(value) > 0 {
node.Value = value
node.SubValue = value
}

return node, nil
Expand Down
22 changes: 11 additions & 11 deletions internal/trie/node/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ func Test_Decode(t *testing.T) {
),
),
n: &Node{
Key: []byte{9},
Value: []byte{1, 2, 3},
Dirty: true,
Key: []byte{9},
SubValue: []byte{1, 2, 3},
Dirty: true,
},
},
"branch decoding error": {
Expand Down Expand Up @@ -215,8 +215,8 @@ func Test_decodeBranch(t *testing.T) {
variant: branchWithValueVariant.bits,
partialKeyLength: 1,
branch: &Node{
Key: []byte{9},
Value: []byte{7, 8, 9},
Key: []byte{9},
SubValue: []byte{7, 8, 9},
Children: padRightChildren([]*Node{
nil, nil, nil, nil, nil,
nil, nil, nil, nil, nil,
Expand Down Expand Up @@ -272,14 +272,14 @@ func Test_decodeBranch(t *testing.T) {
Key: []byte{1},
Descendants: 3,
Children: padRightChildren([]*Node{
{Key: []byte{2}, Value: []byte{2}, Dirty: true},
{Key: []byte{2}, SubValue: []byte{2}, Dirty: true},
{
Key: []byte{3},
Value: []byte{3},
SubValue: []byte{3},
Dirty: true,
Descendants: 1,
Children: padRightChildren([]*Node{
{Key: []byte{4}, Value: []byte{4}, Dirty: true},
{Key: []byte{4}, SubValue: []byte{4}, Dirty: true},
}),
},
}),
Expand Down Expand Up @@ -357,9 +357,9 @@ func Test_decodeLeaf(t *testing.T) {
variant: leafVariant.bits,
partialKeyLength: 1,
leaf: &Node{
Key: []byte{9},
Value: []byte{1, 2, 3, 4, 5},
Dirty: true,
Key: []byte{9},
SubValue: []byte{1, 2, 3, 4, 5},
Dirty: true,
},
},
}
Expand Down
Loading

0 comments on commit b42b867

Please sign in to comment.