@@ -1709,6 +1709,147 @@ func TestStateDBCreateAccountPreservesStorageClear(t *testing.T) {
17091709 require .Equal (t , common.Hash {}, state .GetState (contract , unreadKey ))
17101710}
17111711
1712+ // Executing a block reports, for every balance, nonce, code and storage change, the value its key held
1713+ // before the block: for a slot written twice, a slot deleted, a contract created and a fresh account.
1714+ func TestExecuteBlockChangeSetCarriesPriorValues (t * testing.T ) {
1715+ chainID := big .NewInt (testChainID )
1716+ key , err := crypto .HexToECDSA ("45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8" )
1717+ require .NoError (t , err )
1718+ sender := crypto .PubkeyToAddress (key .PublicKey )
1719+ counter , clearer , fresh := testAddress (0xc8 ), testAddress (0xc9 ), testAddress (0xca )
1720+ coinbase := blockContext (chainID ).Coinbase
1721+ created := crypto .CreateAddress (sender , 2 )
1722+ slot := testHash (0x01 )
1723+ runtime := storeCode (slot , testHash (0x2a ))
1724+ funded := big .NewInt (10 * testFundedBalanceWei )
1725+
1726+ state := NewMemoryState ()
1727+ state .SetBalance (sender , funded )
1728+ state .SetBalance (counter , big .NewInt (1_000 ))
1729+ state .SetCode (counter , counterRuntime (slot ))
1730+ state .SetState (counter , slot , testHash (0x05 ))
1731+ state .SetCode (clearer , storeCode (slot , common.Hash {}))
1732+ state .SetState (clearer , slot , testHash (0x09 ))
1733+ executor := NewExecutor (Config {}, withTestState (state ))
1734+ defer executor .Close ()
1735+ result , err := executor .ExecuteBlock (t .Context (), BlockRequest {Context : blockContext (chainID ), Txs : [][]byte {
1736+ signLegacyTx (t , key , chainID , 0 , & counter , big .NewInt (10 ), nil ),
1737+ signLegacyTx (t , key , chainID , 1 , & counter , big .NewInt (10 ), nil ),
1738+ signLegacyTx (t , key , chainID , 2 , nil , big .NewInt (0 ), initCode (runtime )),
1739+ signLegacyTx (t , key , chainID , 3 , & clearer , big .NewInt (0 ), nil ),
1740+ signLegacyTx (t , key , chainID , 4 , & fresh , big .NewInt (5 ), nil ),
1741+ }})
1742+ require .NoError (t , err )
1743+ require .False (t , result .OCCStats .Attempted )
1744+ for _ , tx := range result .Txs {
1745+ require .Equal (t , uint64 (1 ), tx .Status )
1746+ }
1747+
1748+ fees := new (big.Int ).SetUint64 (result .GasUsed * testGasPriceWei )
1749+ spent := new (big.Int ).Add (fees , big .NewInt (25 ))
1750+ want := StateChangeSet {
1751+ Balances : []BalanceChange {
1752+ {Address : counter , Balance : big .NewInt (1_020 ), PriorBalance : big .NewInt (1_000 )},
1753+ {Address : fresh , Balance : big .NewInt (5 ), PriorBalance : big .NewInt (0 )},
1754+ {Address : coinbase , Balance : fees , PriorBalance : big .NewInt (0 )},
1755+ {Address : sender , Balance : new (big.Int ).Sub (funded , spent ), PriorBalance : funded },
1756+ },
1757+ Nonces : []NonceChange {
1758+ {Address : created , Nonce : 1 , PriorNonce : 0 },
1759+ {Address : sender , Nonce : 5 , PriorNonce : 0 },
1760+ },
1761+ Code : []CodeChange {
1762+ {Address : created , Code : runtime },
1763+ },
1764+ Storage : []StorageChange {
1765+ {Address : counter , Key : slot , Value : testHash (0x07 ), PriorValue : testHash (0x05 )},
1766+ {Address : clearer , Key : slot , Delete : true , PriorValue : testHash (0x09 )},
1767+ },
1768+ }
1769+ require .Equal (t , withCanonicalBalances (want ), withCanonicalBalances (result .ChangeSet ))
1770+ }
1771+
1772+ // A slot written after its account is cleared reports the value it held before the block, even though
1773+ // Under OCC, where every transaction after the first reruns on what the earlier ones wrote, executing
1774+ // a block still reports each change beside the value its key held before the block.
1775+ func TestExecuteBlockOCCChangeSetCarriesPriorValues (t * testing.T ) {
1776+ chainID := big .NewInt (testChainID )
1777+ key , err := crypto .HexToECDSA ("45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8" )
1778+ require .NoError (t , err )
1779+ sender := crypto .PubkeyToAddress (key .PublicKey )
1780+ counter , clearer , fresh := testAddress (0xc8 ), testAddress (0xc9 ), testAddress (0xca )
1781+ coinbase := blockContext (chainID ).Coinbase
1782+ created := crypto .CreateAddress (sender , 2 )
1783+ slot := testHash (0x01 )
1784+ runtime := storeCode (slot , testHash (0x2a ))
1785+ funded := big .NewInt (10 * testFundedBalanceWei )
1786+
1787+ state := NewMemoryState ()
1788+ state .SetBalance (sender , funded )
1789+ state .SetBalance (counter , big .NewInt (1_000 ))
1790+ state .SetCode (counter , counterRuntime (slot ))
1791+ state .SetState (counter , slot , testHash (0x05 ))
1792+ state .SetCode (clearer , storeCode (slot , common.Hash {}))
1793+ state .SetState (clearer , slot , testHash (0x09 ))
1794+ executor := NewExecutor (Config {OCCWorkers : 4 }, withTestState (state ))
1795+ defer executor .Close ()
1796+ result , err := executor .ExecuteBlock (t .Context (), BlockRequest {Context : blockContext (chainID ), Txs : [][]byte {
1797+ signLegacyTx (t , key , chainID , 0 , & counter , big .NewInt (10 ), nil ),
1798+ signLegacyTx (t , key , chainID , 1 , & counter , big .NewInt (10 ), nil ),
1799+ signLegacyTx (t , key , chainID , 2 , nil , big .NewInt (0 ), initCode (runtime )),
1800+ signLegacyTx (t , key , chainID , 3 , & clearer , big .NewInt (0 ), nil ),
1801+ signLegacyTx (t , key , chainID , 4 , & fresh , big .NewInt (5 ), nil ),
1802+ }})
1803+ require .NoError (t , err )
1804+ require .True (t , result .OCCStats .Attempted )
1805+ require .False (t , result .OCCStats .Fallback , result .OCCStats .FallbackReason )
1806+ require .Positive (t , result .OCCStats .RerunCount )
1807+ for _ , tx := range result .Txs {
1808+ require .Equal (t , uint64 (1 ), tx .Status )
1809+ }
1810+
1811+ fees := new (big.Int ).SetUint64 (result .GasUsed * testGasPriceWei )
1812+ spent := new (big.Int ).Add (fees , big .NewInt (25 ))
1813+ want := StateChangeSet {
1814+ Balances : []BalanceChange {
1815+ {Address : counter , Balance : big .NewInt (1_020 ), PriorBalance : big .NewInt (1_000 )},
1816+ {Address : fresh , Balance : big .NewInt (5 ), PriorBalance : big .NewInt (0 )},
1817+ {Address : coinbase , Balance : fees , PriorBalance : big .NewInt (0 )},
1818+ {Address : sender , Balance : new (big.Int ).Sub (funded , spent ), PriorBalance : funded },
1819+ },
1820+ Nonces : []NonceChange {
1821+ {Address : created , Nonce : 1 , PriorNonce : 0 },
1822+ {Address : sender , Nonce : 5 , PriorNonce : 0 },
1823+ },
1824+ Code : []CodeChange {
1825+ {Address : created , Code : runtime },
1826+ },
1827+ Storage : []StorageChange {
1828+ {Address : counter , Key : slot , Value : testHash (0x07 ), PriorValue : testHash (0x05 )},
1829+ {Address : clearer , Key : slot , Delete : true , PriorValue : testHash (0x09 )},
1830+ },
1831+ }
1832+ require .Equal (t , withCanonicalBalances (want ), withCanonicalBalances (result .ChangeSet ))
1833+ }
1834+
1835+ // nothing read it.
1836+ func TestStateDBStorageClearKeepsPriorValue (t * testing.T ) {
1837+ contract := testAddress (0xc5 )
1838+ key := testHash (0x01 )
1839+
1840+ state := NewMemoryState ()
1841+ state .SetState (contract , key , testHash (0x05 ))
1842+ stateDB := newNativeStateDB (state )
1843+
1844+ stateDB .SelfDestruct (contract )
1845+ stateDB .Finalise (true )
1846+ stateDB .SetState (contract , key , testHash (0x07 ))
1847+
1848+ changes := stateDB .ChangeSet ()
1849+ require .Equal (t , []common.Address {contract }, changes .StorageClears )
1850+ require .Equal (t , []StorageChange {{Address : contract , Key : key , Value : testHash (0x07 ), PriorValue : testHash (0x05 )}}, changes .Storage )
1851+ }
1852+
17121853func TestStateDBStorageClearThenSameValueWriteIsEmitted (t * testing.T ) {
17131854 contract := testAddress (0xc5 )
17141855 key := testHash (0x01 )
0 commit comments