Add missing tests for direct indexing mode
This commit is contained in:
parent
b4a899cc8c
commit
257b16e184
@ -1337,6 +1337,25 @@ func TestBuilderWithRemovedAccountAndStorage(t *testing.T) {
|
||||
NodeType: sdtypes.Removed,
|
||||
LeafKey: contractLeafKey,
|
||||
NodeValue: []byte{},
|
||||
StorageNodes: []sdtypes.StorageNode{
|
||||
{
|
||||
Path: []byte{},
|
||||
NodeType: sdtypes.Removed,
|
||||
NodeValue: []byte{},
|
||||
},
|
||||
{
|
||||
Path: []byte{'\x02'},
|
||||
NodeType: sdtypes.Removed,
|
||||
LeafKey: slot0StorageKey.Bytes(),
|
||||
NodeValue: []byte{},
|
||||
},
|
||||
{
|
||||
Path: []byte{'\x0c'},
|
||||
NodeType: sdtypes.Removed,
|
||||
LeafKey: slot3StorageKey.Bytes(),
|
||||
NodeValue: []byte{},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Path: []byte{'\x0c'},
|
||||
@ -1584,6 +1603,294 @@ func TestBuilderWithRemovedAccountAndStorageWithoutIntermediateNodes(t *testing.
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) {
|
||||
blocks, chain := test_helpers.MakeChain(6, test_helpers.Genesis, test_helpers.TestChainGen)
|
||||
contractLeafKey = test_helpers.AddressToLeafKey(test_helpers.ContractAddr)
|
||||
defer chain.Stop()
|
||||
block3 = blocks[2]
|
||||
block4 = blocks[3]
|
||||
block5 = blocks[4]
|
||||
block6 = blocks[5]
|
||||
params := statediff.Params{
|
||||
WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.Account2Addr},
|
||||
}
|
||||
params.ComputeWatchedAddressesLeafKeys()
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
startingArguments statediff.Args
|
||||
expected *sdtypes.StateObject
|
||||
}{
|
||||
{
|
||||
"testBlock4",
|
||||
statediff.Args{
|
||||
OldStateRoot: block3.Root(),
|
||||
NewStateRoot: block4.Root(),
|
||||
BlockNumber: block4.Number(),
|
||||
BlockHash: block4.Hash(),
|
||||
},
|
||||
&sdtypes.StateObject{
|
||||
BlockNumber: block4.Number(),
|
||||
BlockHash: block4.Hash(),
|
||||
Nodes: []sdtypes.StateNode{
|
||||
{
|
||||
Path: []byte{'\x0c'},
|
||||
NodeType: sdtypes.Leaf,
|
||||
LeafKey: test_helpers.Account2LeafKey,
|
||||
NodeValue: account2AtBlock4LeafNode,
|
||||
StorageNodes: emptyStorage,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"testBlock5",
|
||||
statediff.Args{
|
||||
OldStateRoot: block4.Root(),
|
||||
NewStateRoot: block5.Root(),
|
||||
BlockNumber: block5.Number(),
|
||||
BlockHash: block5.Hash(),
|
||||
},
|
||||
&sdtypes.StateObject{
|
||||
BlockNumber: block5.Number(),
|
||||
BlockHash: block5.Hash(),
|
||||
Nodes: []sdtypes.StateNode{
|
||||
{
|
||||
Path: []byte{'\x0e'},
|
||||
NodeType: sdtypes.Leaf,
|
||||
LeafKey: test_helpers.Account1LeafKey,
|
||||
NodeValue: account1AtBlock5LeafNode,
|
||||
StorageNodes: emptyStorage,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"testBlock6",
|
||||
statediff.Args{
|
||||
OldStateRoot: block5.Root(),
|
||||
NewStateRoot: block6.Root(),
|
||||
BlockNumber: block6.Number(),
|
||||
BlockHash: block6.Hash(),
|
||||
},
|
||||
&sdtypes.StateObject{
|
||||
BlockNumber: block6.Number(),
|
||||
BlockHash: block6.Hash(),
|
||||
Nodes: []sdtypes.StateNode{
|
||||
{
|
||||
Path: []byte{'\x0c'},
|
||||
NodeType: sdtypes.Leaf,
|
||||
LeafKey: test_helpers.Account2LeafKey,
|
||||
NodeValue: account2AtBlock6LeafNode,
|
||||
StorageNodes: emptyStorage,
|
||||
},
|
||||
{
|
||||
Path: []byte{'\x0e'},
|
||||
NodeType: sdtypes.Leaf,
|
||||
LeafKey: test_helpers.Account1LeafKey,
|
||||
NodeValue: account1AtBlock6LeafNode,
|
||||
StorageNodes: emptyStorage,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, workers := range workerCounts {
|
||||
builder, _ = pkg.NewBuilder(chain.StateCache(), workers)
|
||||
for _, test := range tests {
|
||||
diff, err := builder.BuildStateDiffObject(test.startingArguments, params)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
receivedStateDiffRlp, err := rlp.EncodeToBytes(&diff)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
expectedStateDiffRlp, err := rlp.EncodeToBytes(test.expected)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
sort.Slice(receivedStateDiffRlp, func(i, j int) bool { return receivedStateDiffRlp[i] < receivedStateDiffRlp[j] })
|
||||
sort.Slice(expectedStateDiffRlp, func(i, j int) bool { return expectedStateDiffRlp[i] < expectedStateDiffRlp[j] })
|
||||
if !bytes.Equal(receivedStateDiffRlp, expectedStateDiffRlp) {
|
||||
t.Logf("Test failed: %s", test.name)
|
||||
t.Errorf("actual state diff: %+v\r\n\r\n\r\nexpected state diff: %+v", diff, test.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuilderWithRemovedWatchedAccount(t *testing.T) {
|
||||
blocks, chain := test_helpers.MakeChain(6, test_helpers.Genesis, test_helpers.TestChainGen)
|
||||
contractLeafKey = test_helpers.AddressToLeafKey(test_helpers.ContractAddr)
|
||||
defer chain.Stop()
|
||||
block3 = blocks[2]
|
||||
block4 = blocks[3]
|
||||
block5 = blocks[4]
|
||||
block6 = blocks[5]
|
||||
params := statediff.Params{
|
||||
WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.ContractAddr},
|
||||
}
|
||||
params.ComputeWatchedAddressesLeafKeys()
|
||||
|
||||
var tests = []struct {
|
||||
name string
|
||||
startingArguments statediff.Args
|
||||
expected *sdtypes.StateObject
|
||||
}{
|
||||
{
|
||||
"testBlock4",
|
||||
statediff.Args{
|
||||
OldStateRoot: block3.Root(),
|
||||
NewStateRoot: block4.Root(),
|
||||
BlockNumber: block4.Number(),
|
||||
BlockHash: block4.Hash(),
|
||||
},
|
||||
&sdtypes.StateObject{
|
||||
BlockNumber: block4.Number(),
|
||||
BlockHash: block4.Hash(),
|
||||
Nodes: []sdtypes.StateNode{
|
||||
{
|
||||
Path: []byte{'\x06'},
|
||||
NodeType: sdtypes.Leaf,
|
||||
LeafKey: contractLeafKey,
|
||||
NodeValue: contractAccountAtBlock4LeafNode,
|
||||
StorageNodes: []sdtypes.StorageNode{
|
||||
{
|
||||
Path: []byte{'\x04'},
|
||||
NodeType: sdtypes.Leaf,
|
||||
LeafKey: slot2StorageKey.Bytes(),
|
||||
NodeValue: slot2StorageLeafNode,
|
||||
},
|
||||
{
|
||||
Path: []byte{'\x0b'},
|
||||
NodeType: sdtypes.Removed,
|
||||
LeafKey: slot1StorageKey.Bytes(),
|
||||
NodeValue: []byte{},
|
||||
},
|
||||
{
|
||||
Path: []byte{'\x0c'},
|
||||
NodeType: sdtypes.Removed,
|
||||
LeafKey: slot3StorageKey.Bytes(),
|
||||
NodeValue: []byte{},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"testBlock5",
|
||||
statediff.Args{
|
||||
OldStateRoot: block4.Root(),
|
||||
NewStateRoot: block5.Root(),
|
||||
BlockNumber: block5.Number(),
|
||||
BlockHash: block5.Hash(),
|
||||
},
|
||||
&sdtypes.StateObject{
|
||||
BlockNumber: block5.Number(),
|
||||
BlockHash: block5.Hash(),
|
||||
Nodes: []sdtypes.StateNode{
|
||||
{
|
||||
Path: []byte{'\x06'},
|
||||
NodeType: sdtypes.Leaf,
|
||||
LeafKey: contractLeafKey,
|
||||
NodeValue: contractAccountAtBlock5LeafNode,
|
||||
StorageNodes: []sdtypes.StorageNode{
|
||||
{
|
||||
Path: []byte{'\x0c'},
|
||||
NodeType: sdtypes.Leaf,
|
||||
LeafKey: slot3StorageKey.Bytes(),
|
||||
NodeValue: slot3StorageLeafNode,
|
||||
},
|
||||
{
|
||||
Path: []byte{'\x04'},
|
||||
NodeType: sdtypes.Removed,
|
||||
LeafKey: slot2StorageKey.Bytes(),
|
||||
NodeValue: []byte{},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Path: []byte{'\x0e'},
|
||||
NodeType: sdtypes.Leaf,
|
||||
LeafKey: test_helpers.Account1LeafKey,
|
||||
NodeValue: account1AtBlock5LeafNode,
|
||||
StorageNodes: emptyStorage,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
"testBlock6",
|
||||
statediff.Args{
|
||||
OldStateRoot: block5.Root(),
|
||||
NewStateRoot: block6.Root(),
|
||||
BlockNumber: block6.Number(),
|
||||
BlockHash: block6.Hash(),
|
||||
},
|
||||
&sdtypes.StateObject{
|
||||
BlockNumber: block6.Number(),
|
||||
BlockHash: block6.Hash(),
|
||||
Nodes: []sdtypes.StateNode{
|
||||
{
|
||||
Path: []byte{'\x06'},
|
||||
NodeType: sdtypes.Removed,
|
||||
LeafKey: contractLeafKey,
|
||||
NodeValue: []byte{},
|
||||
StorageNodes: []sdtypes.StorageNode{
|
||||
{
|
||||
Path: []byte{'\x02'},
|
||||
NodeType: sdtypes.Removed,
|
||||
LeafKey: slot0StorageKey.Bytes(),
|
||||
NodeValue: []byte{},
|
||||
},
|
||||
{
|
||||
Path: []byte{'\x0c'},
|
||||
NodeType: sdtypes.Removed,
|
||||
LeafKey: slot3StorageKey.Bytes(),
|
||||
NodeValue: []byte{},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Path: []byte{'\x0e'},
|
||||
NodeType: sdtypes.Leaf,
|
||||
LeafKey: test_helpers.Account1LeafKey,
|
||||
NodeValue: account1AtBlock6LeafNode,
|
||||
StorageNodes: emptyStorage,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, workers := range workerCounts {
|
||||
builder, _ = pkg.NewBuilder(chain.StateCache(), workers)
|
||||
for _, test := range tests {
|
||||
diff, err := builder.BuildStateDiffObject(test.startingArguments, params)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
receivedStateDiffRlp, err := rlp.EncodeToBytes(&diff)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
expectedStateDiffRlp, err := rlp.EncodeToBytes(test.expected)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
sort.Slice(receivedStateDiffRlp, func(i, j int) bool { return receivedStateDiffRlp[i] < receivedStateDiffRlp[j] })
|
||||
sort.Slice(expectedStateDiffRlp, func(i, j int) bool { return expectedStateDiffRlp[i] < expectedStateDiffRlp[j] })
|
||||
if !bytes.Equal(receivedStateDiffRlp, expectedStateDiffRlp) {
|
||||
t.Logf("Test failed: %s", test.name)
|
||||
t.Errorf("actual state diff: %+v\r\n\r\n\r\nexpected state diff: %+v", diff, test.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var (
|
||||
slot00StorageValue = common.Hex2Bytes("9471562b71999873db5b286df957af199ec94617f7") // prefixed TestBankAddress
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user