Update builder unit tests

This commit is contained in:
Prathamesh Musale 2022-06-22 16:45:53 +05:30
parent cedbd2497e
commit 0438809cb4
3 changed files with 96 additions and 13 deletions

View File

@ -300,13 +300,13 @@ func (sdb *StateDiffBuilder) BuildStateDiffWithoutIntermediateStateNodes(iterPai
// and a slice of the paths for all of the nodes included in both // 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) { func (sdb *StateDiffBuilder) createdAndUpdatedState(a, b trie.NodeIterator, watchedAddressesLeafPaths [][]byte) (types2.AccountMap, map[string]bool, error) {
diffPathsAtB := make(map[string]bool) diffPathsAtB := make(map[string]bool)
diffAcountsAtB := make(types2.AccountMap) diffAccountsAtB := make(types2.AccountMap)
watchingAddresses := len(watchedAddressesLeafPaths) > 0 watchingAddresses := len(watchedAddressesLeafPaths) > 0
it, _ := trie.NewDifferenceIterator(a, b) it, _ := trie.NewDifferenceIterator(a, b)
for it.Next(true) { for it.Next(true) {
// ignore node if it is not along paths of interest // ignore node if it is not along paths of interest
if watchingAddresses && !isValidPath(watchedAddressesLeafPaths, it.Path()) { if watchingAddresses && !isValidPrefixPath(watchedAddressesLeafPaths, it.Path()) {
continue continue
} }
@ -336,7 +336,7 @@ func (sdb *StateDiffBuilder) createdAndUpdatedState(a, b trie.NodeIterator, watc
encodedPath := trie.HexToCompact(valueNodePath) encodedPath := trie.HexToCompact(valueNodePath)
leafKey := encodedPath[1:] leafKey := encodedPath[1:]
diffAcountsAtB[common.Bytes2Hex(leafKey)] = types2.AccountWrapper{ diffAccountsAtB[common.Bytes2Hex(leafKey)] = types2.AccountWrapper{
NodeType: node.NodeType, NodeType: node.NodeType,
Path: node.Path, Path: node.Path,
NodeValue: node.NodeValue, 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 // add both intermediate and leaf node paths to the list of diffPathsAtB
diffPathsAtB[common.Bytes2Hex(node.Path)] = true diffPathsAtB[common.Bytes2Hex(node.Path)] = true
} }
return diffAcountsAtB, diffPathsAtB, it.Error() return diffAccountsAtB, diffPathsAtB, it.Error()
} }
// createdAndUpdatedStateWithIntermediateNodes returns // createdAndUpdatedStateWithIntermediateNodes returns
@ -362,7 +362,7 @@ func (sdb *StateDiffBuilder) createdAndUpdatedStateWithIntermediateNodes(a, b tr
it, _ := trie.NewDifferenceIterator(a, b) it, _ := trie.NewDifferenceIterator(a, b)
for it.Next(true) { for it.Next(true) {
// ignore node if it is not along paths of interest // ignore node if it is not along paths of interest
if watchingAddresses && !isValidPath(watchedAddressesLeafPaths, it.Path()) { if watchingAddresses && !isValidPrefixPath(watchedAddressesLeafPaths, it.Path()) {
continue continue
} }
@ -427,7 +427,7 @@ func (sdb *StateDiffBuilder) deletedOrUpdatedState(a, b trie.NodeIterator, diffA
it, _ := trie.NewDifferenceIterator(b, a) it, _ := trie.NewDifferenceIterator(b, a)
for it.Next(true) { for it.Next(true) {
// ignore node if it is not along paths of interest // ignore node if it is not along paths of interest
if watchingAddresses && !isValidPath(watchedAddressesLeafPaths, it.Path()) { if watchingAddresses && !isValidPrefixPath(watchedAddressesLeafPaths, it.Path()) {
continue continue
} }
@ -864,8 +864,8 @@ func (sdb *StateDiffBuilder) deletedOrUpdatedStorage(a, b trie.NodeIterator, dif
return it.Error() 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 // 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 isValidPath(watchedAddressesLeafPaths [][]byte, currentPath []byte) bool { func isValidPrefixPath(watchedAddressesLeafPaths [][]byte, currentPath []byte) bool {
for _, watchedAddressPath := range watchedAddressesLeafPaths { for _, watchedAddressPath := range watchedAddressesLeafPaths {
if bytes.HasPrefix(watchedAddressPath, currentPath) { if bytes.HasPrefix(watchedAddressPath, currentPath) {
return true return true

View File

@ -1001,7 +1001,9 @@ func TestBuilderWithWatchedAddressList(t *testing.T) {
block2 = blocks[1] block2 = blocks[1]
block3 = blocks[2] block3 = blocks[2]
params := statediff.Params{ 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() params.ComputeWatchedAddressesLeafPaths()
builder = statediff.NewBuilder(chain.StateCache()) builder = statediff.NewBuilder(chain.StateCache())
@ -1053,6 +1055,12 @@ func TestBuilderWithWatchedAddressList(t *testing.T) {
BlockNumber: block1.Number(), BlockNumber: block1.Number(),
BlockHash: block1.Hash(), BlockHash: block1.Hash(),
Nodes: []types2.StateNode{ Nodes: []types2.StateNode{
{
Path: []byte{},
NodeType: types2.Branch,
NodeValue: block1BranchRootNode,
StorageNodes: emptyStorage,
},
{ {
Path: []byte{'\x0e'}, Path: []byte{'\x0e'},
NodeType: types2.Leaf, NodeType: types2.Leaf,
@ -1077,12 +1085,23 @@ func TestBuilderWithWatchedAddressList(t *testing.T) {
BlockNumber: block2.Number(), BlockNumber: block2.Number(),
BlockHash: block2.Hash(), BlockHash: block2.Hash(),
Nodes: []types2.StateNode{ Nodes: []types2.StateNode{
{
Path: []byte{},
NodeType: types2.Branch,
NodeValue: block2BranchRootNode,
StorageNodes: emptyStorage,
},
{ {
Path: []byte{'\x06'}, Path: []byte{'\x06'},
NodeType: types2.Leaf, NodeType: types2.Leaf,
LeafKey: contractLeafKey, LeafKey: contractLeafKey,
NodeValue: contractAccountAtBlock2LeafNode, NodeValue: contractAccountAtBlock2LeafNode,
StorageNodes: []types2.StorageNode{ StorageNodes: []types2.StorageNode{
{
Path: []byte{},
NodeType: types2.Branch,
NodeValue: block2StorageBranchRootNode,
},
{ {
Path: []byte{'\x02'}, Path: []byte{'\x02'},
NodeType: types2.Leaf, NodeType: types2.Leaf,
@ -1127,12 +1146,23 @@ func TestBuilderWithWatchedAddressList(t *testing.T) {
BlockNumber: block3.Number(), BlockNumber: block3.Number(),
BlockHash: block3.Hash(), BlockHash: block3.Hash(),
Nodes: []types2.StateNode{ Nodes: []types2.StateNode{
{
Path: []byte{},
NodeType: types2.Branch,
NodeValue: block3BranchRootNode,
StorageNodes: emptyStorage,
},
{ {
Path: []byte{'\x06'}, Path: []byte{'\x06'},
NodeType: types2.Leaf, NodeType: types2.Leaf,
LeafKey: contractLeafKey, LeafKey: contractLeafKey,
NodeValue: contractAccountAtBlock3LeafNode, NodeValue: contractAccountAtBlock3LeafNode,
StorageNodes: []types2.StorageNode{ StorageNodes: []types2.StorageNode{
{
Path: []byte{},
NodeType: types2.Branch,
NodeValue: block3StorageBranchRootNode,
},
{ {
Path: []byte{'\x0c'}, Path: []byte{'\x0c'},
NodeType: types2.Leaf, NodeType: types2.Leaf,
@ -1606,7 +1636,9 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) {
block5 = blocks[4] block5 = blocks[4]
block6 = blocks[5] block6 = blocks[5]
params := statediff.Params{ 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() params.ComputeWatchedAddressesLeafPaths()
builder = statediff.NewBuilder(chain.StateCache()) builder = statediff.NewBuilder(chain.StateCache())
@ -1628,6 +1660,12 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) {
BlockNumber: block4.Number(), BlockNumber: block4.Number(),
BlockHash: block4.Hash(), BlockHash: block4.Hash(),
Nodes: []types2.StateNode{ Nodes: []types2.StateNode{
{
Path: []byte{},
NodeType: types2.Branch,
NodeValue: block4BranchRootNode,
StorageNodes: emptyStorage,
},
{ {
Path: []byte{'\x0c'}, Path: []byte{'\x0c'},
NodeType: types2.Leaf, NodeType: types2.Leaf,
@ -1650,6 +1688,12 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) {
BlockNumber: block5.Number(), BlockNumber: block5.Number(),
BlockHash: block5.Hash(), BlockHash: block5.Hash(),
Nodes: []types2.StateNode{ Nodes: []types2.StateNode{
{
Path: []byte{},
NodeType: types2.Branch,
NodeValue: block5BranchRootNode,
StorageNodes: emptyStorage,
},
{ {
Path: []byte{'\x0e'}, Path: []byte{'\x0e'},
NodeType: types2.Leaf, NodeType: types2.Leaf,
@ -1672,6 +1716,12 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) {
BlockNumber: block6.Number(), BlockNumber: block6.Number(),
BlockHash: block6.Hash(), BlockHash: block6.Hash(),
Nodes: []types2.StateNode{ Nodes: []types2.StateNode{
{
Path: []byte{},
NodeType: types2.Branch,
NodeValue: block6BranchRootNode,
StorageNodes: emptyStorage,
},
{ {
Path: []byte{'\x0c'}, Path: []byte{'\x0c'},
NodeType: types2.Leaf, NodeType: types2.Leaf,
@ -1724,7 +1774,9 @@ func TestBuilderWithRemovedWatchedAccount(t *testing.T) {
block5 = blocks[4] block5 = blocks[4]
block6 = blocks[5] block6 = blocks[5]
params := statediff.Params{ 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() params.ComputeWatchedAddressesLeafPaths()
builder = statediff.NewBuilder(chain.StateCache()) builder = statediff.NewBuilder(chain.StateCache())
@ -1746,12 +1798,23 @@ func TestBuilderWithRemovedWatchedAccount(t *testing.T) {
BlockNumber: block4.Number(), BlockNumber: block4.Number(),
BlockHash: block4.Hash(), BlockHash: block4.Hash(),
Nodes: []types2.StateNode{ Nodes: []types2.StateNode{
{
Path: []byte{},
NodeType: types2.Branch,
NodeValue: block4BranchRootNode,
StorageNodes: emptyStorage,
},
{ {
Path: []byte{'\x06'}, Path: []byte{'\x06'},
NodeType: types2.Leaf, NodeType: types2.Leaf,
LeafKey: contractLeafKey, LeafKey: contractLeafKey,
NodeValue: contractAccountAtBlock4LeafNode, NodeValue: contractAccountAtBlock4LeafNode,
StorageNodes: []types2.StorageNode{ StorageNodes: []types2.StorageNode{
{
Path: []byte{},
NodeType: types2.Branch,
NodeValue: block4StorageBranchRootNode,
},
{ {
Path: []byte{'\x04'}, Path: []byte{'\x04'},
NodeType: types2.Leaf, NodeType: types2.Leaf,
@ -1787,12 +1850,23 @@ func TestBuilderWithRemovedWatchedAccount(t *testing.T) {
BlockNumber: block5.Number(), BlockNumber: block5.Number(),
BlockHash: block5.Hash(), BlockHash: block5.Hash(),
Nodes: []types2.StateNode{ Nodes: []types2.StateNode{
{
Path: []byte{},
NodeType: types2.Branch,
NodeValue: block5BranchRootNode,
StorageNodes: emptyStorage,
},
{ {
Path: []byte{'\x06'}, Path: []byte{'\x06'},
NodeType: types2.Leaf, NodeType: types2.Leaf,
LeafKey: contractLeafKey, LeafKey: contractLeafKey,
NodeValue: contractAccountAtBlock5LeafNode, NodeValue: contractAccountAtBlock5LeafNode,
StorageNodes: []types2.StorageNode{ StorageNodes: []types2.StorageNode{
{
Path: []byte{},
NodeType: types2.Branch,
NodeValue: block5StorageBranchRootNode,
},
{ {
Path: []byte{'\x0c'}, Path: []byte{'\x0c'},
NodeType: types2.Leaf, NodeType: types2.Leaf,
@ -1829,12 +1903,23 @@ func TestBuilderWithRemovedWatchedAccount(t *testing.T) {
BlockNumber: block6.Number(), BlockNumber: block6.Number(),
BlockHash: block6.Hash(), BlockHash: block6.Hash(),
Nodes: []types2.StateNode{ Nodes: []types2.StateNode{
{
Path: []byte{},
NodeType: types2.Branch,
NodeValue: block6BranchRootNode,
StorageNodes: emptyStorage,
},
{ {
Path: []byte{'\x06'}, Path: []byte{'\x06'},
NodeType: types2.Removed, NodeType: types2.Removed,
LeafKey: contractLeafKey, LeafKey: contractLeafKey,
NodeValue: []byte{}, NodeValue: []byte{},
StorageNodes: []types2.StorageNode{ StorageNodes: []types2.StorageNode{
{
Path: []byte{},
NodeType: types2.Removed,
NodeValue: []byte{},
},
{ {
Path: []byte{'\x02'}, Path: []byte{'\x02'},
NodeType: types2.Removed, NodeType: types2.Removed,

View File

@ -434,7 +434,5 @@ func testGetSyncStatus(t *testing.T) {
} else { } else {
t.Log("Test Passed!") t.Log("Test Passed!")
} }
} }
} }