Skip to content

Commit fd872cd

Browse files
authored
Read prev value sequential executor (#4517)
* This PR modifies the `StateChangeSet` in Giga to add the previous EVM value (prev nonce, prev balance, prev code, prev slot). * It also modifies the sequential executor to include that value in the `StateChangeSet`. * Note that this value is read from the `base` mapping in the executor, which keeps the previous state of the account throughout the execution of the block. Once the block is executed, it's cleaned up. * Note: For the OCC executor, previous EVM values are not read. This will be added in a follow up PR. Edit: PR with OCC changes merged here.
1 parent 68dd155 commit fd872cd

10 files changed

Lines changed: 295 additions & 161 deletions

‎giga/evmonly/account_reader_test.go‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ func TestSnapshotReaderFetchesCodeOnlyWhenTheAccountHasSome(t *testing.T) {
7979
require.Equal(t, []byte{0x60, 0x00}, account.Code)
8080
}
8181

82-
// The merge compares balance, nonce and code against the same row, and resolves that row once per
83-
// touched account across the pool's workers.
84-
func TestParallelMergeResolvesEveryTouchedAccountOnce(t *testing.T) {
82+
// The merge takes priors from the accepted writes, so it reads no account row, on the pool's workers
83+
// or on the calling goroutine.
84+
func TestParallelMergeReadsNoAccount(t *testing.T) {
8585
snapshot := newMemoryGigaSnapshot(7)
8686
addrs := make([]common.Address, 0, 512)
8787
for i := range 512 {
@@ -103,9 +103,9 @@ func TestParallelMergeResolvesEveryTouchedAccountOnce(t *testing.T) {
103103
var changes StateChangeSet
104104
require.NoError(t, state.changeSetIntoParallel(t.Context(), pool, &changes))
105105

106-
require.Equal(t, int64(len(addrs)), reading.reads.Load(), "each row must be read once")
106+
require.Zero(t, reading.reads.Load(), "the parallel merge must read no row")
107107
require.Equal(t, state.ChangeSet(), changes, "the parallel merge must match the serial one")
108-
require.Equal(t, int64(2*len(addrs)), reading.reads.Load(), "the serial merge reads each row once too")
108+
require.Zero(t, reading.reads.Load(), "the serial merge must read no row")
109109
require.Len(t, changes.Balances, len(addrs))
110110
require.Len(t, changes.Nonces, len(addrs))
111111
for i := 1; i < len(changes.Balances); i++ {

‎giga/evmonly/executor_test.go‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
17121853
func TestStateDBStorageClearThenSameValueWriteIsEmitted(t *testing.T) {
17131854
contract := testAddress(0xc5)
17141855
key := testHash(0x01)

‎giga/evmonly/giga_store_test.go‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/ethereum/go-ethereum/common"
1010
"github.com/ethereum/go-ethereum/crypto"
11+
"github.com/holiman/uint256"
1112
"github.com/stretchr/testify/require"
1213

1314
"github.com/sei-protocol/sei-chain/sei-db/proto"
@@ -239,7 +240,7 @@ func TestExecutorCommitsGigaStoreStateChanges(t *testing.T) {
239240
require.Equal(t, 1, snapshot.closeCount)
240241
require.Equal(t, []int64{41}, store.commitBlock)
241242
require.Equal(t, [][]*proto.NamedChangeSet{wantChangesets}, store.commits)
242-
require.Contains(t, result.ChangeSet.Balances, BalanceChange{Address: recipient, Balance: big.NewInt(7)})
243+
require.Contains(t, result.ChangeSet.Balances, BalanceChange{Address: recipient, Balance: big.NewInt(7), PriorBalance: new(uint256.Int).ToBig()})
243244
result.Release()
244245
}
245246

@@ -264,8 +265,9 @@ func TestExecutorGigaStoreSnapshotFeedsOCCExecution(t *testing.T) {
264265
encoder := func(changes StateChangeSet) ([]*proto.NamedChangeSet, error) {
265266
for i, recipient := range recipients {
266267
require.Contains(t, changes.Balances, BalanceChange{
267-
Address: recipient,
268-
Balance: big.NewInt(int64(i + 1)),
268+
Address: recipient,
269+
Balance: big.NewInt(int64(i + 1)),
270+
PriorBalance: new(uint256.Int).ToBig(),
269271
})
270272
}
271273
return []*proto.NamedChangeSet{}, nil

0 commit comments

Comments
 (0)