Skip to content

Commit c2af35d

Browse files
committed
Determine node type according to children field
1 parent b36873b commit c2af35d

23 files changed

+402
-797
lines changed

dot/state/storage_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,6 @@ func TestGetStorageChildAndGetStorageFromChild(t *testing.T) {
181181
))
182182

183183
trieRoot := &node.Node{
184-
Type: node.Leaf,
185184
Key: []byte{1, 2},
186185
Value: []byte{3, 4},
187186
Dirty: true,

dot/state/tries_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,13 @@ func Test_Tries_get(t *testing.T) {
169169
tries: &Tries{
170170
rootToTrie: map[common.Hash]*trie.Trie{
171171
{1, 2, 3}: trie.NewTrie(&node.Node{
172-
Type: node.Leaf,
173-
Key: []byte{1, 2, 3},
172+
Key: []byte{1, 2, 3},
174173
}),
175174
},
176175
},
177176
root: common.Hash{1, 2, 3},
178177
trie: trie.NewTrie(&node.Node{
179-
Type: node.Leaf,
180-
Key: []byte{1, 2, 3},
178+
Key: []byte{1, 2, 3},
181179
}),
182180
},
183181
"not found in map": {

internal/trie/node/branch_encode.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func encodeChildrenOpportunisticParallel(children []*Node, buffer io.Writer) (er
5656
resultsCh := make(chan encodingAsyncResult, ChildrenCapacity)
5757

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

154154
err = hashNode(node, buffer)
155155
if err != nil {
156-
return nil, fmt.Errorf("cannot hash %s: %w", node.Type, err)
156+
return nil, fmt.Errorf("cannot hash %s: %w", node.Type(), err)
157157
}
158158

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

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

172172
err = node.Encode(encodingBuffer)
173173
if err != nil {
174-
return fmt.Errorf("cannot encode %s: %w", node.Type, err)
174+
return fmt.Errorf("cannot encode %s: %w", node.Type(), err)
175175
}
176176

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

197197
_, err = digestWriter.Write(hasher.Sum(nil))
198198
if err != nil {
199-
return fmt.Errorf("cannot write hash sum of %s to buffer: %w", node.Type, err)
199+
return fmt.Errorf("cannot write hash sum of %s to buffer: %w", node.Type(), err)
200200
}
201201
return nil
202202
}

internal/trie/node/branch_encode_test.go

Lines changed: 31 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ func Test_hashNode(t *testing.T) {
2424
}{
2525
"small leaf buffer write error": {
2626
node: &Node{
27-
Type: Leaf,
2827
Encoding: []byte{1, 2, 3},
2928
},
3029
write: writeCall{
@@ -37,7 +36,6 @@ func Test_hashNode(t *testing.T) {
3736
},
3837
"small leaf success": {
3938
node: &Node{
40-
Type: Leaf,
4139
Encoding: []byte{1, 2, 3},
4240
},
4341
write: writeCall{
@@ -46,7 +44,6 @@ func Test_hashNode(t *testing.T) {
4644
},
4745
"leaf hash sum buffer write error": {
4846
node: &Node{
49-
Type: Leaf,
5047
Encoding: []byte{
5148
1, 2, 3, 4, 5, 6, 7, 8,
5249
1, 2, 3, 4, 5, 6, 7, 8,
@@ -70,7 +67,6 @@ func Test_hashNode(t *testing.T) {
7067
},
7168
"leaf hash sum success": {
7269
node: &Node{
73-
Type: Leaf,
7470
Encoding: []byte{
7571
1, 2, 3, 4, 5, 6, 7, 8,
7672
1, 2, 3, 4, 5, 6, 7, 8,
@@ -90,25 +86,25 @@ func Test_hashNode(t *testing.T) {
9086
},
9187
"empty branch": {
9288
node: &Node{
93-
Type: Branch,
89+
Children: make([]*Node, ChildrenCapacity),
9490
},
9591
write: writeCall{
9692
written: []byte{128, 0, 0},
9793
},
9894
},
9995
"less than 32 bytes encoding": {
10096
node: &Node{
101-
Type: Branch,
102-
Key: []byte{1, 2},
97+
Children: make([]*Node, ChildrenCapacity),
98+
Key: []byte{1, 2},
10399
},
104100
write: writeCall{
105101
written: []byte{130, 18, 0, 0},
106102
},
107103
},
108104
"less than 32 bytes encoding write error": {
109105
node: &Node{
110-
Type: Branch,
111-
Key: []byte{1, 2},
106+
Children: make([]*Node, ChildrenCapacity),
107+
Key: []byte{1, 2},
112108
},
113109
write: writeCall{
114110
written: []byte{130, 18, 0, 0},
@@ -119,8 +115,8 @@ func Test_hashNode(t *testing.T) {
119115
},
120116
"more than 32 bytes encoding": {
121117
node: &Node{
122-
Type: Branch,
123-
Key: repeatBytes(100, 1),
118+
Children: make([]*Node, ChildrenCapacity),
119+
Key: repeatBytes(100, 1),
124120
},
125121
write: writeCall{
126122
written: []byte{
@@ -132,8 +128,8 @@ func Test_hashNode(t *testing.T) {
132128
},
133129
"more than 32 bytes encoding write error": {
134130
node: &Node{
135-
Type: Branch,
136-
Key: repeatBytes(100, 1),
131+
Children: make([]*Node, ChildrenCapacity),
132+
Key: repeatBytes(100, 1),
137133
},
138134
write: writeCall{
139135
written: []byte{
@@ -192,7 +188,6 @@ func populateChildren(valueSize, depth int) (children []*Node) {
192188
if depth == 0 {
193189
for i := range children {
194190
children[i] = &Node{
195-
Type: Leaf,
196191
Key: someValue,
197192
Value: someValue,
198193
}
@@ -202,7 +197,6 @@ func populateChildren(valueSize, depth int) (children []*Node) {
202197

203198
for i := range children {
204199
children[i] = &Node{
205-
Type: Branch,
206200
Key: someValue,
207201
Value: someValue,
208202
Children: populateChildren(valueSize, depth-1),
@@ -224,7 +218,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
224218
"no children": {},
225219
"first child not nil": {
226220
children: []*Node{
227-
{Type: Leaf, Key: []byte{1}},
221+
{Key: []byte{1}},
228222
},
229223
writes: []writeCall{
230224
{
@@ -237,7 +231,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
237231
nil, nil, nil, nil, nil,
238232
nil, nil, nil, nil, nil,
239233
nil, nil, nil, nil, nil,
240-
{Type: Leaf, Key: []byte{1}},
234+
{Key: []byte{1}},
241235
},
242236
writes: []writeCall{
243237
{
@@ -247,8 +241,8 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
247241
},
248242
"first two children not nil": {
249243
children: []*Node{
250-
{Type: Leaf, Key: []byte{1}},
251-
{Type: Leaf, Key: []byte{2}},
244+
{Key: []byte{1}},
245+
{Key: []byte{2}},
252246
},
253247
writes: []writeCall{
254248
{
@@ -264,9 +258,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
264258
nil, nil, nil, nil,
265259
nil, nil, nil, nil,
266260
nil, nil, nil,
267-
{Type: Leaf,
268-
Key: []byte{1},
269-
},
261+
{Key: []byte{1}},
270262
nil, nil, nil, nil,
271263
},
272264
writes: []writeCall{
@@ -284,10 +276,9 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
284276
// running in parallel.
285277
children: []*Node{
286278
{
287-
Type: Branch,
288-
Key: []byte{1},
279+
Key: []byte{1},
289280
Children: []*Node{
290-
{Type: Leaf, Key: []byte{1}},
281+
{Key: []byte{1}},
291282
},
292283
},
293284
},
@@ -335,7 +326,7 @@ func Test_encodeChildrenOpportunisticParallel(t *testing.T) {
335326
children := make([]*Node, ChildrenCapacity)
336327
for i := range children {
337328
children[i] = &Node{
338-
Type: Branch,
329+
Children: make([]*Node, ChildrenCapacity),
339330
}
340331
}
341332

@@ -369,7 +360,7 @@ func Test_encodeChildrenSequentially(t *testing.T) {
369360
"no children": {},
370361
"first child not nil": {
371362
children: []*Node{
372-
{Type: Leaf, Key: []byte{1}},
363+
{Key: []byte{1}},
373364
},
374365
writes: []writeCall{
375366
{
@@ -382,7 +373,7 @@ func Test_encodeChildrenSequentially(t *testing.T) {
382373
nil, nil, nil, nil, nil,
383374
nil, nil, nil, nil, nil,
384375
nil, nil, nil, nil, nil,
385-
{Type: Leaf, Key: []byte{1}},
376+
{Key: []byte{1}},
386377
},
387378
writes: []writeCall{
388379
{
@@ -392,8 +383,8 @@ func Test_encodeChildrenSequentially(t *testing.T) {
392383
},
393384
"first two children not nil": {
394385
children: []*Node{
395-
{Type: Leaf, Key: []byte{1}},
396-
{Type: Leaf, Key: []byte{2}},
386+
{Key: []byte{1}},
387+
{Key: []byte{2}},
397388
},
398389
writes: []writeCall{
399390
{
@@ -409,9 +400,7 @@ func Test_encodeChildrenSequentially(t *testing.T) {
409400
nil, nil, nil, nil,
410401
nil, nil, nil, nil,
411402
nil, nil, nil,
412-
{Type: Leaf,
413-
Key: []byte{1},
414-
},
403+
{Key: []byte{1}},
415404
nil, nil, nil, nil,
416405
},
417406
writes: []writeCall{
@@ -469,23 +458,25 @@ func Test_encodeChild(t *testing.T) {
469458
}{
470459
"nil node": {},
471460
"empty leaf child": {
472-
child: &Node{Type: Leaf},
461+
child: &Node{},
473462
writeCall: true,
474463
write: writeCall{
475464
written: []byte{8, 64, 0},
476465
},
477466
},
478467
"empty branch child": {
479468
child: &Node{
480-
Type: Branch},
469+
Children: make([]*Node, ChildrenCapacity),
470+
},
481471
writeCall: true,
482472
write: writeCall{
483473
written: []byte{12, 128, 0, 0},
484474
},
485475
},
486476
"buffer write error": {
487477
child: &Node{
488-
Type: Branch},
478+
Children: make([]*Node, ChildrenCapacity),
479+
},
489480
writeCall: true,
490481
write: writeCall{
491482
written: []byte{12, 128, 0, 0},
@@ -496,7 +487,6 @@ func Test_encodeChild(t *testing.T) {
496487
},
497488
"leaf child": {
498489
child: &Node{
499-
Type: Leaf,
500490
Key: []byte{1},
501491
Value: []byte{2},
502492
},
@@ -507,12 +497,10 @@ func Test_encodeChild(t *testing.T) {
507497
},
508498
"branch child": {
509499
child: &Node{
510-
Type: Branch,
511500
Key: []byte{1},
512501
Value: []byte{2},
513502
Children: []*Node{
514-
nil, nil, {Type: Leaf,
515-
Key: []byte{5},
503+
nil, nil, {Key: []byte{5},
516504
Value: []byte{6},
517505
},
518506
},
@@ -560,24 +548,21 @@ func Test_scaleEncodeHash(t *testing.T) {
560548
errMessage string
561549
}{
562550
"empty leaf": {
563-
node: &Node{
564-
Type: Leaf,
565-
},
551+
node: &Node{},
566552
encoding: []byte{0x8, 0x40, 0},
567553
},
568554
"empty branch": {
569555
node: &Node{
570-
Type: Branch,
556+
Children: make([]*Node, ChildrenCapacity),
571557
},
572558
encoding: []byte{0xc, 0x80, 0x0, 0x0},
573559
},
574560
"non empty branch": {
575561
node: &Node{
576-
Type: Branch,
577562
Key: []byte{1, 2},
578563
Value: []byte{3, 4},
579564
Children: []*Node{
580-
nil, nil, {Type: Leaf, Key: []byte{9}},
565+
nil, nil, {Key: []byte{9}},
581566
},
582567
},
583568
encoding: []byte{0x2c, 0xc2, 0x12, 0x4, 0x0, 0x8, 0x3, 0x4, 0xc, 0x41, 0x9, 0x0},

internal/trie/node/copy.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,12 @@ type CopySettings struct {
5757
// children as well.
5858
func (n *Node) Copy(settings CopySettings) *Node {
5959
cpy := &Node{
60-
Type: n.Type,
6160
Dirty: n.Dirty,
6261
Generation: n.Generation,
6362
Descendants: n.Descendants,
6463
}
6564

66-
if n.Type == Branch {
65+
if n.Type() == Branch {
6766
if settings.CopyChildren {
6867
// Copy all fields of children if we deep copy children
6968
childSettings := settings

0 commit comments

Comments
 (0)