From 0438809cb493d990e52cdb1310c22745b679a517 Mon Sep 17 00:00:00 2001 From: prathamesh0 Date: Wed, 22 Jun 2022 16:45:53 +0530 Subject: [PATCH] Update builder unit tests --- statediff/builder.go | 16 +++---- statediff/builder_test.go | 91 +++++++++++++++++++++++++++++++++++++-- statediff/service_test.go | 2 - 3 files changed, 96 insertions(+), 13 deletions(-) diff --git a/statediff/builder.go b/statediff/builder.go index ad460ccb4..c3ecbf117 100644 --- a/statediff/builder.go +++ b/statediff/builder.go @@ -300,13 +300,13 @@ func (sdb *StateDiffBuilder) BuildStateDiffWithoutIntermediateStateNodes(iterPai // and a slice of the paths for all of the nodes included in both func (sdb *StateDiffBuilder) createdAndUpdatedState(a, b trie.NodeIterator, watchedAddressesLeafPaths [][]byte) (types2.AccountMap, map[string]bool, error) { diffPathsAtB := make(map[string]bool) - diffAcountsAtB := make(types2.AccountMap) + diffAccountsAtB := make(types2.AccountMap) watchingAddresses := len(watchedAddressesLeafPaths) > 0 it, _ := trie.NewDifferenceIterator(a, b) for it.Next(true) { // ignore node if it is not along paths of interest - if watchingAddresses && !isValidPath(watchedAddressesLeafPaths, it.Path()) { + if watchingAddresses && !isValidPrefixPath(watchedAddressesLeafPaths, it.Path()) { continue } @@ -336,7 +336,7 @@ func (sdb *StateDiffBuilder) createdAndUpdatedState(a, b trie.NodeIterator, watc encodedPath := trie.HexToCompact(valueNodePath) leafKey := encodedPath[1:] - diffAcountsAtB[common.Bytes2Hex(leafKey)] = types2.AccountWrapper{ + diffAccountsAtB[common.Bytes2Hex(leafKey)] = types2.AccountWrapper{ NodeType: node.NodeType, Path: node.Path, NodeValue: node.NodeValue, @@ -347,7 +347,7 @@ func (sdb *StateDiffBuilder) createdAndUpdatedState(a, b trie.NodeIterator, watc // add both intermediate and leaf node paths to the list of diffPathsAtB diffPathsAtB[common.Bytes2Hex(node.Path)] = true } - return diffAcountsAtB, diffPathsAtB, it.Error() + return diffAccountsAtB, diffPathsAtB, it.Error() } // createdAndUpdatedStateWithIntermediateNodes returns @@ -362,7 +362,7 @@ func (sdb *StateDiffBuilder) createdAndUpdatedStateWithIntermediateNodes(a, b tr it, _ := trie.NewDifferenceIterator(a, b) for it.Next(true) { // ignore node if it is not along paths of interest - if watchingAddresses && !isValidPath(watchedAddressesLeafPaths, it.Path()) { + if watchingAddresses && !isValidPrefixPath(watchedAddressesLeafPaths, it.Path()) { continue } @@ -427,7 +427,7 @@ func (sdb *StateDiffBuilder) deletedOrUpdatedState(a, b trie.NodeIterator, diffA it, _ := trie.NewDifferenceIterator(b, a) for it.Next(true) { // ignore node if it is not along paths of interest - if watchingAddresses && !isValidPath(watchedAddressesLeafPaths, it.Path()) { + if watchingAddresses && !isValidPrefixPath(watchedAddressesLeafPaths, it.Path()) { continue } @@ -864,8 +864,8 @@ func (sdb *StateDiffBuilder) deletedOrUpdatedStorage(a, b trie.NodeIterator, dif return it.Error() } -// isValidPath is used to check if a node is a parent | ancestor to one of the addresses the builder is configured to watch -func isValidPath(watchedAddressesLeafPaths [][]byte, currentPath []byte) bool { +// 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 +func isValidPrefixPath(watchedAddressesLeafPaths [][]byte, currentPath []byte) bool { for _, watchedAddressPath := range watchedAddressesLeafPaths { if bytes.HasPrefix(watchedAddressPath, currentPath) { return true diff --git a/statediff/builder_test.go b/statediff/builder_test.go index c8ca9f759..4ff6ed9fc 100644 --- a/statediff/builder_test.go +++ b/statediff/builder_test.go @@ -1001,7 +1001,9 @@ func TestBuilderWithWatchedAddressList(t *testing.T) { block2 = blocks[1] block3 = blocks[2] params := statediff.Params{ - WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.ContractAddr}, + IntermediateStateNodes: true, + IntermediateStorageNodes: true, + WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.ContractAddr}, } params.ComputeWatchedAddressesLeafPaths() builder = statediff.NewBuilder(chain.StateCache()) @@ -1053,6 +1055,12 @@ func TestBuilderWithWatchedAddressList(t *testing.T) { BlockNumber: block1.Number(), BlockHash: block1.Hash(), Nodes: []types2.StateNode{ + { + Path: []byte{}, + NodeType: types2.Branch, + NodeValue: block1BranchRootNode, + StorageNodes: emptyStorage, + }, { Path: []byte{'\x0e'}, NodeType: types2.Leaf, @@ -1077,12 +1085,23 @@ func TestBuilderWithWatchedAddressList(t *testing.T) { BlockNumber: block2.Number(), BlockHash: block2.Hash(), Nodes: []types2.StateNode{ + { + Path: []byte{}, + NodeType: types2.Branch, + NodeValue: block2BranchRootNode, + StorageNodes: emptyStorage, + }, { Path: []byte{'\x06'}, NodeType: types2.Leaf, LeafKey: contractLeafKey, NodeValue: contractAccountAtBlock2LeafNode, StorageNodes: []types2.StorageNode{ + { + Path: []byte{}, + NodeType: types2.Branch, + NodeValue: block2StorageBranchRootNode, + }, { Path: []byte{'\x02'}, NodeType: types2.Leaf, @@ -1127,12 +1146,23 @@ func TestBuilderWithWatchedAddressList(t *testing.T) { BlockNumber: block3.Number(), BlockHash: block3.Hash(), Nodes: []types2.StateNode{ + { + Path: []byte{}, + NodeType: types2.Branch, + NodeValue: block3BranchRootNode, + StorageNodes: emptyStorage, + }, { Path: []byte{'\x06'}, NodeType: types2.Leaf, LeafKey: contractLeafKey, NodeValue: contractAccountAtBlock3LeafNode, StorageNodes: []types2.StorageNode{ + { + Path: []byte{}, + NodeType: types2.Branch, + NodeValue: block3StorageBranchRootNode, + }, { Path: []byte{'\x0c'}, NodeType: types2.Leaf, @@ -1606,7 +1636,9 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) { block5 = blocks[4] block6 = blocks[5] params := statediff.Params{ - WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.Account2Addr}, + IntermediateStateNodes: true, + IntermediateStorageNodes: true, + WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.Account2Addr}, } params.ComputeWatchedAddressesLeafPaths() builder = statediff.NewBuilder(chain.StateCache()) @@ -1628,6 +1660,12 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) { BlockNumber: block4.Number(), BlockHash: block4.Hash(), Nodes: []types2.StateNode{ + { + Path: []byte{}, + NodeType: types2.Branch, + NodeValue: block4BranchRootNode, + StorageNodes: emptyStorage, + }, { Path: []byte{'\x0c'}, NodeType: types2.Leaf, @@ -1650,6 +1688,12 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) { BlockNumber: block5.Number(), BlockHash: block5.Hash(), Nodes: []types2.StateNode{ + { + Path: []byte{}, + NodeType: types2.Branch, + NodeValue: block5BranchRootNode, + StorageNodes: emptyStorage, + }, { Path: []byte{'\x0e'}, NodeType: types2.Leaf, @@ -1672,6 +1716,12 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) { BlockNumber: block6.Number(), BlockHash: block6.Hash(), Nodes: []types2.StateNode{ + { + Path: []byte{}, + NodeType: types2.Branch, + NodeValue: block6BranchRootNode, + StorageNodes: emptyStorage, + }, { Path: []byte{'\x0c'}, NodeType: types2.Leaf, @@ -1724,7 +1774,9 @@ func TestBuilderWithRemovedWatchedAccount(t *testing.T) { block5 = blocks[4] block6 = blocks[5] params := statediff.Params{ - WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.ContractAddr}, + IntermediateStateNodes: true, + IntermediateStorageNodes: true, + WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.ContractAddr}, } params.ComputeWatchedAddressesLeafPaths() builder = statediff.NewBuilder(chain.StateCache()) @@ -1746,12 +1798,23 @@ func TestBuilderWithRemovedWatchedAccount(t *testing.T) { BlockNumber: block4.Number(), BlockHash: block4.Hash(), Nodes: []types2.StateNode{ + { + Path: []byte{}, + NodeType: types2.Branch, + NodeValue: block4BranchRootNode, + StorageNodes: emptyStorage, + }, { Path: []byte{'\x06'}, NodeType: types2.Leaf, LeafKey: contractLeafKey, NodeValue: contractAccountAtBlock4LeafNode, StorageNodes: []types2.StorageNode{ + { + Path: []byte{}, + NodeType: types2.Branch, + NodeValue: block4StorageBranchRootNode, + }, { Path: []byte{'\x04'}, NodeType: types2.Leaf, @@ -1787,12 +1850,23 @@ func TestBuilderWithRemovedWatchedAccount(t *testing.T) { BlockNumber: block5.Number(), BlockHash: block5.Hash(), Nodes: []types2.StateNode{ + { + Path: []byte{}, + NodeType: types2.Branch, + NodeValue: block5BranchRootNode, + StorageNodes: emptyStorage, + }, { Path: []byte{'\x06'}, NodeType: types2.Leaf, LeafKey: contractLeafKey, NodeValue: contractAccountAtBlock5LeafNode, StorageNodes: []types2.StorageNode{ + { + Path: []byte{}, + NodeType: types2.Branch, + NodeValue: block5StorageBranchRootNode, + }, { Path: []byte{'\x0c'}, NodeType: types2.Leaf, @@ -1829,12 +1903,23 @@ func TestBuilderWithRemovedWatchedAccount(t *testing.T) { BlockNumber: block6.Number(), BlockHash: block6.Hash(), Nodes: []types2.StateNode{ + { + Path: []byte{}, + NodeType: types2.Branch, + NodeValue: block6BranchRootNode, + StorageNodes: emptyStorage, + }, { Path: []byte{'\x06'}, NodeType: types2.Removed, LeafKey: contractLeafKey, NodeValue: []byte{}, StorageNodes: []types2.StorageNode{ + { + Path: []byte{}, + NodeType: types2.Removed, + NodeValue: []byte{}, + }, { Path: []byte{'\x02'}, NodeType: types2.Removed, diff --git a/statediff/service_test.go b/statediff/service_test.go index 6d6ed44ef..3f80f1d2e 100644 --- a/statediff/service_test.go +++ b/statediff/service_test.go @@ -434,7 +434,5 @@ func testGetSyncStatus(t *testing.T) { } else { t.Log("Test Passed!") } - } - }