Skip to content

Commit 750e44e

Browse files
committed
Add builder test for removal of watched addresses
1 parent f3761ef commit 750e44e

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

statediff/builder_test.go

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,168 @@ func TestBuilderWithRemovedNonWatchedAccount(t *testing.T) {
16751675
}
16761676
}
16771677

1678+
func TestBuilderWithRemovedWatchedAccount(t *testing.T) {
1679+
blocks, chain := test_helpers.MakeChain(6, test_helpers.Genesis, test_helpers.TestChainGen)
1680+
contractLeafKey = test_helpers.AddressToLeafKey(test_helpers.ContractAddr)
1681+
defer chain.Stop()
1682+
block3 = blocks[2]
1683+
block4 = blocks[3]
1684+
block5 = blocks[4]
1685+
block6 = blocks[5]
1686+
params := statediff.Params{
1687+
WatchedAddresses: []common.Address{test_helpers.Account1Addr, test_helpers.ContractAddr},
1688+
}
1689+
params.ComputeWatchedAddressesLeafKeys()
1690+
builder = statediff.NewBuilder(chain.StateCache())
1691+
1692+
var tests = []struct {
1693+
name string
1694+
startingArguments statediff.Args
1695+
expected *types2.StateObject
1696+
}{
1697+
{
1698+
"testBlock4",
1699+
statediff.Args{
1700+
OldStateRoot: block3.Root(),
1701+
NewStateRoot: block4.Root(),
1702+
BlockNumber: block4.Number(),
1703+
BlockHash: block4.Hash(),
1704+
},
1705+
&types2.StateObject{
1706+
BlockNumber: block4.Number(),
1707+
BlockHash: block4.Hash(),
1708+
Nodes: []types2.StateNode{
1709+
{
1710+
Path: []byte{'\x06'},
1711+
NodeType: types2.Leaf,
1712+
LeafKey: contractLeafKey,
1713+
NodeValue: contractAccountAtBlock4LeafNode,
1714+
StorageNodes: []types2.StorageNode{
1715+
{
1716+
Path: []byte{'\x04'},
1717+
NodeType: types2.Leaf,
1718+
LeafKey: slot2StorageKey.Bytes(),
1719+
NodeValue: slot2StorageLeafNode,
1720+
},
1721+
{
1722+
Path: []byte{'\x0b'},
1723+
NodeType: types2.Removed,
1724+
LeafKey: slot1StorageKey.Bytes(),
1725+
NodeValue: []byte{},
1726+
},
1727+
{
1728+
Path: []byte{'\x0c'},
1729+
NodeType: types2.Removed,
1730+
LeafKey: slot3StorageKey.Bytes(),
1731+
NodeValue: []byte{},
1732+
},
1733+
},
1734+
},
1735+
},
1736+
},
1737+
},
1738+
{
1739+
"testBlock5",
1740+
statediff.Args{
1741+
OldStateRoot: block4.Root(),
1742+
NewStateRoot: block5.Root(),
1743+
BlockNumber: block5.Number(),
1744+
BlockHash: block5.Hash(),
1745+
},
1746+
&types2.StateObject{
1747+
BlockNumber: block5.Number(),
1748+
BlockHash: block5.Hash(),
1749+
Nodes: []types2.StateNode{
1750+
{
1751+
Path: []byte{'\x06'},
1752+
NodeType: types2.Leaf,
1753+
LeafKey: contractLeafKey,
1754+
NodeValue: contractAccountAtBlock5LeafNode,
1755+
StorageNodes: []types2.StorageNode{
1756+
{
1757+
Path: []byte{},
1758+
NodeType: types2.Leaf,
1759+
LeafKey: slot0StorageKey.Bytes(),
1760+
NodeValue: slot0StorageLeafRootNode,
1761+
},
1762+
{
1763+
Path: []byte{'\x02'},
1764+
NodeType: types2.Removed,
1765+
LeafKey: slot0StorageKey.Bytes(),
1766+
NodeValue: []byte{},
1767+
},
1768+
{
1769+
Path: []byte{'\x04'},
1770+
NodeType: types2.Removed,
1771+
LeafKey: slot2StorageKey.Bytes(),
1772+
NodeValue: []byte{},
1773+
},
1774+
},
1775+
},
1776+
{
1777+
Path: []byte{'\x0e'},
1778+
NodeType: types2.Leaf,
1779+
LeafKey: test_helpers.Account1LeafKey,
1780+
NodeValue: account1AtBlock5LeafNode,
1781+
StorageNodes: emptyStorage,
1782+
},
1783+
},
1784+
},
1785+
},
1786+
{
1787+
"testBlock6",
1788+
statediff.Args{
1789+
OldStateRoot: block5.Root(),
1790+
NewStateRoot: block6.Root(),
1791+
BlockNumber: block6.Number(),
1792+
BlockHash: block6.Hash(),
1793+
},
1794+
&types2.StateObject{
1795+
BlockNumber: block6.Number(),
1796+
BlockHash: block6.Hash(),
1797+
Nodes: []types2.StateNode{
1798+
{
1799+
Path: []byte{'\x06'},
1800+
NodeType: types2.Removed,
1801+
LeafKey: contractLeafKey,
1802+
NodeValue: []byte{},
1803+
},
1804+
{
1805+
Path: []byte{'\x0e'},
1806+
NodeType: types2.Leaf,
1807+
LeafKey: test_helpers.Account1LeafKey,
1808+
NodeValue: account1AtBlock6LeafNode,
1809+
StorageNodes: emptyStorage,
1810+
},
1811+
},
1812+
},
1813+
},
1814+
}
1815+
1816+
for _, test := range tests {
1817+
diff, err := builder.BuildStateDiffObject(test.startingArguments, params)
1818+
if err != nil {
1819+
t.Error(err)
1820+
}
1821+
receivedStateDiffRlp, err := rlp.EncodeToBytes(diff)
1822+
if err != nil {
1823+
t.Error(err)
1824+
}
1825+
1826+
expectedStateDiffRlp, err := rlp.EncodeToBytes(test.expected)
1827+
if err != nil {
1828+
t.Error(err)
1829+
}
1830+
1831+
sort.Slice(receivedStateDiffRlp, func(i, j int) bool { return receivedStateDiffRlp[i] < receivedStateDiffRlp[j] })
1832+
sort.Slice(expectedStateDiffRlp, func(i, j int) bool { return expectedStateDiffRlp[i] < expectedStateDiffRlp[j] })
1833+
if !bytes.Equal(receivedStateDiffRlp, expectedStateDiffRlp) {
1834+
t.Logf("Test failed: %s", test.name)
1835+
t.Errorf("actual state diff: %+v\r\n\r\n\r\nexpected state diff: %+v", diff, test.expected)
1836+
}
1837+
}
1838+
}
1839+
16781840
var (
16791841
slot00StorageValue = common.Hex2Bytes("9471562b71999873db5b286df957af199ec94617f7") // prefixed TestBankAddress
16801842

0 commit comments

Comments
 (0)