Skip to content

Commit 0438809

Browse files
committed
Update builder unit tests
1 parent cedbd24 commit 0438809

File tree

3 files changed

+96
-13
lines changed

3 files changed

+96
-13
lines changed

Diff for: statediff/builder.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -300,13 +300,13 @@ func (sdb *StateDiffBuilder) BuildStateDiffWithoutIntermediateStateNodes(iterPai
300300
// and a slice of the paths for all of the nodes included in both
301301
func (sdb *StateDiffBuilder) createdAndUpdatedState(a, b trie.NodeIterator, watchedAddressesLeafPaths [][]byte) (types2.AccountMap, map[string]bool, error) {
302302
diffPathsAtB := make(map[string]bool)
303-
diffAcountsAtB := make(types2.AccountMap)
303+
diffAccountsAtB := make(types2.AccountMap)
304304
watchingAddresses := len(watchedAddressesLeafPaths) > 0
305305

306306
it, _ := trie.NewDifferenceIterator(a, b)
307307
for it.Next(true) {
308308
// ignore node if it is not along paths of interest
309-
if watchingAddresses && !isValidPath(watchedAddressesLeafPaths, it.Path()) {
309+
if watchingAddresses && !isValidPrefixPath(watchedAddressesLeafPaths, it.Path()) {
310310
continue
311311
}
312312

@@ -336,7 +336,7 @@ func (sdb *StateDiffBuilder) createdAndUpdatedState(a, b trie.NodeIterator, watc
336336

337337
encodedPath := trie.HexToCompact(valueNodePath)
338338
leafKey := encodedPath[1:]
339-
diffAcountsAtB[common.Bytes2Hex(leafKey)] = types2.AccountWrapper{
339+
diffAccountsAtB[common.Bytes2Hex(leafKey)] = types2.AccountWrapper{
340340
NodeType: node.NodeType,
341341
Path: node.Path,
342342
NodeValue: node.NodeValue,
@@ -347,7 +347,7 @@ func (sdb *StateDiffBuilder) createdAndUpdatedState(a, b trie.NodeIterator, watc
347347
// add both intermediate and leaf node paths to the list of diffPathsAtB
348348
diffPathsAtB[common.Bytes2Hex(node.Path)] = true
349349
}
350-
return diffAcountsAtB, diffPathsAtB, it.Error()
350+
return diffAccountsAtB, diffPathsAtB, it.Error()
351351
}
352352

353353
// createdAndUpdatedStateWithIntermediateNodes returns
@@ -362,7 +362,7 @@ func (sdb *StateDiffBuilder) createdAndUpdatedStateWithIntermediateNodes(a, b tr
362362
it, _ := trie.NewDifferenceIterator(a, b)
363363
for it.Next(true) {
364364
// ignore node if it is not along paths of interest
365-
if watchingAddresses && !isValidPath(watchedAddressesLeafPaths, it.Path()) {
365+
if watchingAddresses && !isValidPrefixPath(watchedAddressesLeafPaths, it.Path()) {
366366
continue
367367
}
368368

@@ -427,7 +427,7 @@ func (sdb *StateDiffBuilder) deletedOrUpdatedState(a, b trie.NodeIterator, diffA
427427
it, _ := trie.NewDifferenceIterator(b, a)
428428
for it.Next(true) {
429429
// ignore node if it is not along paths of interest
430-
if watchingAddresses && !isValidPath(watchedAddressesLeafPaths, it.Path()) {
430+
if watchingAddresses && !isValidPrefixPath(watchedAddressesLeafPaths, it.Path()) {
431431
continue
432432
}
433433

@@ -864,8 +864,8 @@ func (sdb *StateDiffBuilder) deletedOrUpdatedStorage(a, b trie.NodeIterator, dif
864864
return it.Error()
865865
}
866866

867-
// isValidPath is used to check if a node is a parent | ancestor to one of the addresses the builder is configured to watch
868-
func isValidPath(watchedAddressesLeafPaths [][]byte, currentPath []byte) bool {
867+
// isValidPrefixPath is used to check if a node at currentPath is a parent | ancestor to one of the addresses the builder is configured to watch
868+
func isValidPrefixPath(watchedAddressesLeafPaths [][]byte, currentPath []byte) bool {
869869
for _, watchedAddressPath := range watchedAddressesLeafPaths {
870870
if bytes.HasPrefix(watchedAddressPath, currentPath) {
871871
return true

Diff for: statediff/builder_test.go

+88-3
Original file line numberDiff line numberDiff line change
@@ -1001,7 +1001,9 @@ func TestBuilderWithWatchedAddressList(t *testing.T) {
10011001
block2 = blocks[1]
10021002
block3 = blocks[2]
10031003
params := statediff.Params{
1004-
WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.ContractAddr},
1004+
IntermediateStateNodes: true,
1005+
IntermediateStorageNodes: true,
1006+
WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.ContractAddr},
10051007
}
10061008
params.ComputeWatchedAddressesLeafPaths()
10071009
builder = statediff.NewBuilder(chain.StateCache())
@@ -1053,6 +1055,12 @@ func TestBuilderWithWatchedAddressList(t *testing.T) {
10531055
BlockNumber: block1.Number(),
10541056
BlockHash: block1.Hash(),
10551057
Nodes: []types2.StateNode{
1058+
{
1059+
Path: []byte{},
1060+
NodeType: types2.Branch,
1061+
NodeValue: block1BranchRootNode,
1062+
StorageNodes: emptyStorage,
1063+
},
10561064
{
10571065
Path: []byte{'\x0e'},
10581066
NodeType: types2.Leaf,
@@ -1077,12 +1085,23 @@ func TestBuilderWithWatchedAddressList(t *testing.T) {
10771085
BlockNumber: block2.Number(),
10781086
BlockHash: block2.Hash(),
10791087
Nodes: []types2.StateNode{
1088+
{
1089+
Path: []byte{},
1090+
NodeType: types2.Branch,
1091+
NodeValue: block2BranchRootNode,
1092+
StorageNodes: emptyStorage,
1093+
},
10801094
{
10811095
Path: []byte{'\x06'},
10821096
NodeType: types2.Leaf,
10831097
LeafKey: contractLeafKey,
10841098
NodeValue: contractAccountAtBlock2LeafNode,
10851099
StorageNodes: []types2.StorageNode{
1100+
{
1101+
Path: []byte{},
1102+
NodeType: types2.Branch,
1103+
NodeValue: block2StorageBranchRootNode,
1104+
},
10861105
{
10871106
Path: []byte{'\x02'},
10881107
NodeType: types2.Leaf,
@@ -1127,12 +1146,23 @@ func TestBuilderWithWatchedAddressList(t *testing.T) {
11271146
BlockNumber: block3.Number(),
11281147
BlockHash: block3.Hash(),
11291148
Nodes: []types2.StateNode{
1149+
{
1150+
Path: []byte{},
1151+
NodeType: types2.Branch,
1152+
NodeValue: block3BranchRootNode,
1153+
StorageNodes: emptyStorage,
1154+
},
11301155
{
11311156
Path: []byte{'\x06'},
11321157
NodeType: types2.Leaf,
11331158
LeafKey: contractLeafKey,
11341159
NodeValue: contractAccountAtBlock3LeafNode,
11351160
StorageNodes: []types2.StorageNode{
1161+
{
1162+
Path: []byte{},
1163+
NodeType: types2.Branch,
1164+
NodeValue: block3StorageBranchRootNode,
1165+
},
11361166
{
11371167
Path: []byte{'\x0c'},
11381168
NodeType: types2.Leaf,
@@ -1606,7 +1636,9 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) {
16061636
block5 = blocks[4]
16071637
block6 = blocks[5]
16081638
params := statediff.Params{
1609-
WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.Account2Addr},
1639+
IntermediateStateNodes: true,
1640+
IntermediateStorageNodes: true,
1641+
WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.Account2Addr},
16101642
}
16111643
params.ComputeWatchedAddressesLeafPaths()
16121644
builder = statediff.NewBuilder(chain.StateCache())
@@ -1628,6 +1660,12 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) {
16281660
BlockNumber: block4.Number(),
16291661
BlockHash: block4.Hash(),
16301662
Nodes: []types2.StateNode{
1663+
{
1664+
Path: []byte{},
1665+
NodeType: types2.Branch,
1666+
NodeValue: block4BranchRootNode,
1667+
StorageNodes: emptyStorage,
1668+
},
16311669
{
16321670
Path: []byte{'\x0c'},
16331671
NodeType: types2.Leaf,
@@ -1650,6 +1688,12 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) {
16501688
BlockNumber: block5.Number(),
16511689
BlockHash: block5.Hash(),
16521690
Nodes: []types2.StateNode{
1691+
{
1692+
Path: []byte{},
1693+
NodeType: types2.Branch,
1694+
NodeValue: block5BranchRootNode,
1695+
StorageNodes: emptyStorage,
1696+
},
16531697
{
16541698
Path: []byte{'\x0e'},
16551699
NodeType: types2.Leaf,
@@ -1672,6 +1716,12 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) {
16721716
BlockNumber: block6.Number(),
16731717
BlockHash: block6.Hash(),
16741718
Nodes: []types2.StateNode{
1719+
{
1720+
Path: []byte{},
1721+
NodeType: types2.Branch,
1722+
NodeValue: block6BranchRootNode,
1723+
StorageNodes: emptyStorage,
1724+
},
16751725
{
16761726
Path: []byte{'\x0c'},
16771727
NodeType: types2.Leaf,
@@ -1724,7 +1774,9 @@ func TestBuilderWithRemovedWatchedAccount(t *testing.T) {
17241774
block5 = blocks[4]
17251775
block6 = blocks[5]
17261776
params := statediff.Params{
1727-
WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.ContractAddr},
1777+
IntermediateStateNodes: true,
1778+
IntermediateStorageNodes: true,
1779+
WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.ContractAddr},
17281780
}
17291781
params.ComputeWatchedAddressesLeafPaths()
17301782
builder = statediff.NewBuilder(chain.StateCache())
@@ -1746,12 +1798,23 @@ func TestBuilderWithRemovedWatchedAccount(t *testing.T) {
17461798
BlockNumber: block4.Number(),
17471799
BlockHash: block4.Hash(),
17481800
Nodes: []types2.StateNode{
1801+
{
1802+
Path: []byte{},
1803+
NodeType: types2.Branch,
1804+
NodeValue: block4BranchRootNode,
1805+
StorageNodes: emptyStorage,
1806+
},
17491807
{
17501808
Path: []byte{'\x06'},
17511809
NodeType: types2.Leaf,
17521810
LeafKey: contractLeafKey,
17531811
NodeValue: contractAccountAtBlock4LeafNode,
17541812
StorageNodes: []types2.StorageNode{
1813+
{
1814+
Path: []byte{},
1815+
NodeType: types2.Branch,
1816+
NodeValue: block4StorageBranchRootNode,
1817+
},
17551818
{
17561819
Path: []byte{'\x04'},
17571820
NodeType: types2.Leaf,
@@ -1787,12 +1850,23 @@ func TestBuilderWithRemovedWatchedAccount(t *testing.T) {
17871850
BlockNumber: block5.Number(),
17881851
BlockHash: block5.Hash(),
17891852
Nodes: []types2.StateNode{
1853+
{
1854+
Path: []byte{},
1855+
NodeType: types2.Branch,
1856+
NodeValue: block5BranchRootNode,
1857+
StorageNodes: emptyStorage,
1858+
},
17901859
{
17911860
Path: []byte{'\x06'},
17921861
NodeType: types2.Leaf,
17931862
LeafKey: contractLeafKey,
17941863
NodeValue: contractAccountAtBlock5LeafNode,
17951864
StorageNodes: []types2.StorageNode{
1865+
{
1866+
Path: []byte{},
1867+
NodeType: types2.Branch,
1868+
NodeValue: block5StorageBranchRootNode,
1869+
},
17961870
{
17971871
Path: []byte{'\x0c'},
17981872
NodeType: types2.Leaf,
@@ -1829,12 +1903,23 @@ func TestBuilderWithRemovedWatchedAccount(t *testing.T) {
18291903
BlockNumber: block6.Number(),
18301904
BlockHash: block6.Hash(),
18311905
Nodes: []types2.StateNode{
1906+
{
1907+
Path: []byte{},
1908+
NodeType: types2.Branch,
1909+
NodeValue: block6BranchRootNode,
1910+
StorageNodes: emptyStorage,
1911+
},
18321912
{
18331913
Path: []byte{'\x06'},
18341914
NodeType: types2.Removed,
18351915
LeafKey: contractLeafKey,
18361916
NodeValue: []byte{},
18371917
StorageNodes: []types2.StorageNode{
1918+
{
1919+
Path: []byte{},
1920+
NodeType: types2.Removed,
1921+
NodeValue: []byte{},
1922+
},
18381923
{
18391924
Path: []byte{'\x02'},
18401925
NodeType: types2.Removed,

Diff for: statediff/service_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,5 @@ func testGetSyncStatus(t *testing.T) {
434434
} else {
435435
t.Log("Test Passed!")
436436
}
437-
438437
}
439-
440438
}

0 commit comments

Comments
 (0)