Update builder unit tests
This commit is contained in:
parent
cedbd2497e
commit
0438809cb4
@ -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
|
||||
|
@ -1001,6 +1001,8 @@ func TestBuilderWithWatchedAddressList(t *testing.T) {
|
||||
block2 = blocks[1]
|
||||
block3 = blocks[2]
|
||||
params := statediff.Params{
|
||||
IntermediateStateNodes: true,
|
||||
IntermediateStorageNodes: true,
|
||||
WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.ContractAddr},
|
||||
}
|
||||
params.ComputeWatchedAddressesLeafPaths()
|
||||
@ -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,6 +1636,8 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) {
|
||||
block5 = blocks[4]
|
||||
block6 = blocks[5]
|
||||
params := statediff.Params{
|
||||
IntermediateStateNodes: true,
|
||||
IntermediateStorageNodes: true,
|
||||
WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.Account2Addr},
|
||||
}
|
||||
params.ComputeWatchedAddressesLeafPaths()
|
||||
@ -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,6 +1774,8 @@ func TestBuilderWithRemovedWatchedAccount(t *testing.T) {
|
||||
block5 = blocks[4]
|
||||
block6 = blocks[5]
|
||||
params := statediff.Params{
|
||||
IntermediateStateNodes: true,
|
||||
IntermediateStorageNodes: true,
|
||||
WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.ContractAddr},
|
||||
}
|
||||
params.ComputeWatchedAddressesLeafPaths()
|
||||
@ -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,
|
||||
|
@ -434,7 +434,5 @@ func testGetSyncStatus(t *testing.T) {
|
||||
} else {
|
||||
t.Log("Test Passed!")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user