Skip to content

Commit 44a10bc

Browse files
committed
flatten evm-only executor package
1 parent b38217d commit 44a10bc

7 files changed

Lines changed: 43 additions & 62 deletions

File tree

giga/evmonly/README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tracking without reintroducing Cosmos keeper dependencies.
2121

2222
## Current implementation
2323

24-
The `seiv3` package currently provides:
24+
The `evmonly` package currently provides:
2525

2626
- sequential execution of the ordered block transaction list
2727
- RLP decoding and sender recovery through go-ethereum signers
@@ -47,9 +47,8 @@ ExecuteBlock(context.Context, BlockRequest) (*BlockResult, error)
4747
The executor should be commit-neutral. It executes an ordered EVM block and
4848
returns the state writes and receipts produced by that block. The caller owns
4949
durable persistence, state commitment, block indexing, and receipt publication.
50-
The `seiv3` implementation accepts a `StateReader` backend through
51-
`WithState(...)`; callers can persist the returned `ChangeSet` with a matching
52-
`StateWriter`.
50+
The concrete `Executor` accepts a `StateReader` backend through `WithState(...)`;
51+
callers can persist the returned `ChangeSet` with a matching `StateWriter`.
5352

5453
## Input block format
5554

@@ -116,7 +115,7 @@ state as contract storage owned by that precompile address. With no range reads
116115
and no side state, precompile reads and writes can then flow through ordinary
117116
`(address, slot)` storage tracking.
118117

119-
Until that design is implemented, the `seiv3` executor accepts a custom
118+
Until that design is implemented, the `evmonly` executor accepts a custom
120119
precompile registry only as a fail-closed placeholder. Calls to registered
121120
custom precompile addresses return `ErrCustomPrecompilesOpen`.
122121

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package seiv3
1+
package evmonly
22

33
import (
44
"math/big"
Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package seiv3
1+
package evmonly
22

33
import (
44
"context"
@@ -13,19 +13,18 @@ import (
1313
"github.com/ethereum/go-ethereum/core/vm"
1414
"github.com/ethereum/go-ethereum/crypto"
1515
"github.com/ethereum/go-ethereum/params"
16-
"github.com/sei-protocol/sei-chain/giga/evmonly"
1716
"github.com/sei-protocol/sei-chain/giga/evmonly/precompiles"
1817
)
1918

2019
// Executor runs raw EVM transactions against an EVM-native state backend.
2120
type Executor struct {
2221
cfg Config
23-
state evmonly.StateReader
22+
state StateReader
2423
}
2524

2625
type Option func(*Executor)
2726

28-
func WithState(state evmonly.StateReader) Option {
27+
func WithState(state StateReader) Option {
2928
return func(e *Executor) {
3029
if state != nil {
3130
e.state = state
@@ -36,7 +35,7 @@ func WithState(state evmonly.StateReader) Option {
3635
func NewExecutor(cfg Config, opts ...Option) *Executor {
3736
e := &Executor{
3837
cfg: cfg.WithDefaults(),
39-
state: evmonly.NewMemoryState(),
38+
state: NewMemoryState(),
4039
}
4140
for _, opt := range opts {
4241
opt(e)
@@ -48,9 +47,9 @@ func (e *Executor) Config() Config {
4847
return e.cfg
4948
}
5049

51-
func (e *Executor) ExecuteBlock(ctx context.Context, req evmonly.BlockRequest) (*evmonly.BlockResult, error) {
50+
func (e *Executor) ExecuteBlock(ctx context.Context, req BlockRequest) (*BlockResult, error) {
5251
if len(req.Txs) == 0 {
53-
return &evmonly.BlockResult{}, nil
52+
return &BlockResult{}, nil
5453
}
5554

5655
chainConfig := e.chainConfig(req.Context)
@@ -72,8 +71,8 @@ func (e *Executor) ExecuteBlock(ctx context.Context, req evmonly.BlockRequest) (
7271
gasPool := new(core.GasPool).AddGas(gasLimit)
7372
baseFee := cloneBig(req.Context.BaseFee)
7473

75-
result := &evmonly.BlockResult{
76-
Txs: make([]evmonly.TxResult, 0, len(parsed)),
74+
result := &BlockResult{
75+
Txs: make([]TxResult, 0, len(parsed)),
7776
Receipts: make(ethtypes.Receipts, 0, len(parsed)),
7877
}
7978
for txIndex, p := range parsed {
@@ -101,31 +100,31 @@ func (e *Executor) executeTx(
101100
evm *vm.EVM,
102101
stateDB *nativeStateDB,
103102
gasPool *core.GasPool,
104-
block evmonly.BlockContext,
103+
block BlockContext,
105104
p parsedTx,
106105
txIndex int,
107106
baseFee *big.Int,
108107
signer ethtypes.Signer,
109-
) (evmonly.TxResult, *ethtypes.Receipt, error) {
108+
) (TxResult, *ethtypes.Receipt, error) {
110109
tx := p.tx
111110
if e.cfg.CustomPrecompiles != nil && tx.To() != nil {
112111
if _, ok := e.cfg.CustomPrecompiles.Get(*tx.To()); ok {
113-
return evmonly.TxResult{Hash: tx.Hash(), Sender: p.sender, To: tx.To(), Err: precompiles.ErrCustomPrecompilesOpen},
112+
return TxResult{Hash: tx.Hash(), Sender: p.sender, To: tx.To(), Err: precompiles.ErrCustomPrecompilesOpen},
114113
nil,
115114
precompiles.ErrCustomPrecompilesOpen
116115
}
117116
}
118117
if !e.cfg.DisableGasPriceCheck && e.cfg.MinGasPrice != nil {
119118
if effectiveGasPrice(tx, baseFee).Cmp(e.cfg.MinGasPrice) < 0 {
120-
return evmonly.TxResult{Hash: tx.Hash(), Sender: p.sender, To: tx.To(), Err: errInsufficientGasPrice},
119+
return TxResult{Hash: tx.Hash(), Sender: p.sender, To: tx.To(), Err: errInsufficientGasPrice},
121120
nil,
122121
errInsufficientGasPrice
123122
}
124123
}
125124

126125
msg, err := core.TransactionToMessage(tx, signer, baseFee)
127126
if err != nil {
128-
return evmonly.TxResult{Hash: tx.Hash(), Sender: p.sender, To: tx.To(), Err: err}, nil, err
127+
return TxResult{Hash: tx.Hash(), Sender: p.sender, To: tx.To(), Err: err}, nil, err
129128
}
130129
msg.SkipNonceChecks = e.cfg.DisableNonceCheck
131130

@@ -134,7 +133,7 @@ func (e *Executor) executeTx(
134133
evm.SetTxContext(core.NewEVMTxContext(msg))
135134
execResult, err := core.ApplyMessage(evm, msg, gasPool)
136135
if err != nil {
137-
return evmonly.TxResult{Hash: tx.Hash(), Sender: p.sender, To: tx.To(), Err: err}, nil, err
136+
return TxResult{Hash: tx.Hash(), Sender: p.sender, To: tx.To(), Err: err}, nil, err
138137
}
139138

140139
txLogs := append([]*ethtypes.Log(nil), stateDB.logs[logStart:]...)
@@ -165,7 +164,7 @@ func (e *Executor) executeTx(
165164
}
166165
receipt.Bloom = ethtypes.CreateBloom(receipt)
167166

168-
txResult := evmonly.TxResult{
167+
txResult := TxResult{
169168
Hash: tx.Hash(),
170169
Sender: p.sender,
171170
To: tx.To(),
@@ -179,7 +178,7 @@ func (e *Executor) executeTx(
179178
return txResult, receipt, nil
180179
}
181180

182-
func buildBlockContext(ctx evmonly.BlockContext) vm.BlockContext {
181+
func buildBlockContext(ctx BlockContext) vm.BlockContext {
183182
prevRandao := ctx.PrevRandao
184183
baseFee := cloneBig(ctx.BaseFee)
185184
blobBaseFee := cloneBig(ctx.BlobBaseFee)
@@ -232,7 +231,7 @@ func customPrecompileMap(registry precompiles.Registry) map[common.Address]vm.Pr
232231
return contracts
233232
}
234233

235-
func (e *Executor) chainConfig(ctx evmonly.BlockContext) *params.ChainConfig {
234+
func (e *Executor) chainConfig(ctx BlockContext) *params.ChainConfig {
236235
var cfg params.ChainConfig
237236
if e.cfg.ChainConfig != nil {
238237
cfg = *e.cfg.ChainConfig
@@ -259,11 +258,4 @@ func effectiveGasPrice(tx *ethtypes.Transaction, baseFee *big.Int) *big.Int {
259258
return tx.GasPrice()
260259
}
261260

262-
func cloneBig(v *big.Int) *big.Int {
263-
if v == nil {
264-
return new(big.Int)
265-
}
266-
return new(big.Int).Set(v)
267-
}
268-
269261
var errInsufficientGasPrice = fmt.Errorf("insufficient gas price")
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package seiv3
1+
package evmonly
22

33
import (
44
"context"
@@ -12,14 +12,13 @@ import (
1212
"github.com/ethereum/go-ethereum/crypto"
1313
"github.com/stretchr/testify/require"
1414

15-
"github.com/sei-protocol/sei-chain/giga/evmonly"
1615
"github.com/sei-protocol/sei-chain/giga/evmonly/precompiles"
1716
)
1817

1918
func TestExecutorEmptyBlock(t *testing.T) {
2019
executor := NewExecutor(Config{})
2120

22-
result, err := executor.ExecuteBlock(context.Background(), evmonly.BlockRequest{})
21+
result, err := executor.ExecuteBlock(context.Background(), BlockRequest{})
2322

2423
require.NoError(t, err)
2524
require.NotNil(t, result)
@@ -32,13 +31,13 @@ func TestExecutorTransferTx(t *testing.T) {
3231
sender := crypto.PubkeyToAddress(key.PublicKey)
3332
recipient := common.HexToAddress("0x00000000000000000000000000000000000000a1")
3433

35-
state := evmonly.NewMemoryState()
34+
state := NewMemoryState()
3635
state.SetBalance(sender, big.NewInt(200_000_000_000_000))
3736

3837
rawTx := signLegacyTx(t, key, chainID, 0, &recipient, big.NewInt(7), nil)
3938
executor := NewExecutor(Config{}, WithState(state))
4039

41-
result, err := executor.ExecuteBlock(context.Background(), evmonly.BlockRequest{
40+
result, err := executor.ExecuteBlock(context.Background(), BlockRequest{
4241
Context: blockContext(chainID),
4342
Txs: [][]byte{rawTx},
4443
})
@@ -62,15 +61,15 @@ func TestExecutorCustomPrecompilePlaceholder(t *testing.T) {
6261
sender := crypto.PubkeyToAddress(key.PublicKey)
6362
customAddr := common.HexToAddress("0x0000000000000000000000000000000000001001")
6463

65-
state := evmonly.NewMemoryState()
64+
state := NewMemoryState()
6665
state.SetBalance(sender, big.NewInt(200_000_000_000_000))
6766

6867
rawTx := signLegacyTx(t, key, chainID, 0, &customAddr, big.NewInt(0), []byte{0x01})
6968
executor := NewExecutor(Config{
7069
CustomPrecompiles: staticPrecompileRegistry{addr: customAddr},
7170
}, WithState(state))
7271

73-
_, err = executor.ExecuteBlock(context.Background(), evmonly.BlockRequest{
72+
_, err = executor.ExecuteBlock(context.Background(), BlockRequest{
7473
Context: blockContext(chainID),
7574
Txs: [][]byte{rawTx},
7675
})
@@ -96,8 +95,8 @@ func signLegacyTx(t *testing.T, key *ecdsa.PrivateKey, chainID *big.Int, nonce u
9695
return raw
9796
}
9897

99-
func blockContext(chainID *big.Int) evmonly.BlockContext {
100-
return evmonly.BlockContext{
98+
func blockContext(chainID *big.Int) BlockContext {
99+
return BlockContext{
101100
Number: 1,
102101
Time: 1,
103102
GasLimit: 30_000_000,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package seiv3
1+
package evmonly
22

33
import (
44
"context"
Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package seiv3
1+
package evmonly
22

33
import (
44
"bytes"
@@ -15,14 +15,12 @@ import (
1515
"github.com/ethereum/go-ethereum/params"
1616
ethutils "github.com/ethereum/go-ethereum/trie/utils"
1717
"github.com/holiman/uint256"
18-
19-
"github.com/sei-protocol/sei-chain/giga/evmonly"
2018
)
2119

2220
var errInsufficientBalance = errors.New("insufficient balance")
2321

2422
type nativeStateDB struct {
25-
source evmonly.StateReader
23+
source StateReader
2624

2725
accounts map[common.Address]*nativeAccount
2826
base map[common.Address]*nativeAccount
@@ -65,9 +63,9 @@ type accessList struct {
6563
slots map[common.Address]map[common.Hash]struct{}
6664
}
6765

68-
func newNativeStateDB(source evmonly.StateReader) *nativeStateDB {
66+
func newNativeStateDB(source StateReader) *nativeStateDB {
6967
if source == nil {
70-
source = evmonly.NewMemoryState()
68+
source = NewMemoryState()
7169
}
7270
return &nativeStateDB{
7371
source: source,
@@ -79,7 +77,7 @@ func newNativeStateDB(source evmonly.StateReader) *nativeStateDB {
7977
}
8078
}
8179

82-
func (s *nativeStateDB) ChangeSet() evmonly.StateChangeSet {
80+
func (s *nativeStateDB) ChangeSet() StateChangeSet {
8381
addresses := make([]common.Address, 0, len(s.accounts))
8482
for addr := range s.accounts {
8583
addresses = append(addresses, addr)
@@ -88,25 +86,25 @@ func (s *nativeStateDB) ChangeSet() evmonly.StateChangeSet {
8886
return bytes.Compare(addresses[i][:], addresses[j][:]) < 0
8987
})
9088

91-
var changes evmonly.StateChangeSet
89+
var changes StateChangeSet
9290
for _, addr := range addresses {
9391
acct := s.accounts[addr]
9492
base := s.baseAccount(addr)
9593

9694
if !acct.Balance.Eq(base.Balance) {
97-
changes.Balances = append(changes.Balances, evmonly.BalanceChange{
95+
changes.Balances = append(changes.Balances, BalanceChange{
9896
Address: addr,
9997
Balance: acct.Balance.ToBig(),
10098
})
10199
}
102100
if acct.Nonce != base.Nonce {
103-
changes.Nonces = append(changes.Nonces, evmonly.NonceChange{
101+
changes.Nonces = append(changes.Nonces, NonceChange{
104102
Address: addr,
105103
Nonce: acct.Nonce,
106104
})
107105
}
108106
if !bytes.Equal(acct.Code, base.Code) {
109-
changes.Code = append(changes.Code, evmonly.CodeChange{
107+
changes.Code = append(changes.Code, CodeChange{
110108
Address: addr,
111109
Code: cloneBytes(acct.Code),
112110
Delete: len(acct.Code) == 0,
@@ -119,7 +117,7 @@ func (s *nativeStateDB) ChangeSet() evmonly.StateChangeSet {
119117
if oldValue == newValue {
120118
continue
121119
}
122-
changes.Storage = append(changes.Storage, evmonly.StorageChange{
120+
changes.Storage = append(changes.Storage, StorageChange{
123121
Address: addr,
124122
Key: key,
125123
Value: newValue,
@@ -639,10 +637,3 @@ func uint256FromBig(v *big.Int) *uint256.Int {
639637
}
640638
return u
641639
}
642-
643-
func cloneBytes(v []byte) []byte {
644-
if len(v) == 0 {
645-
return nil
646-
}
647-
return append([]byte(nil), v...)
648-
}

giga/evmonly/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
ethtypes "github.com/ethereum/go-ethereum/core/types"
99
)
1010

11-
// Executor is the Cosmos-free block execution boundary for the EVM-only path.
12-
type Executor interface {
11+
// BlockExecutor is the Cosmos-free block execution boundary for the EVM-only path.
12+
type BlockExecutor interface {
1313
ExecuteBlock(context.Context, BlockRequest) (*BlockResult, error)
1414
}
1515

0 commit comments

Comments
 (0)