Merge pull request #45 from sei-protocol/tony/versioned-precompiles

extend custom precompiles interface
This commit is contained in:
codchen 2025-04-04 18:17:39 +08:00 committed by GitHub
commit 137651b12b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 21 additions and 13 deletions

View file

@ -259,7 +259,7 @@ func (b *EthAPIBackend) GetEVM(ctx context.Context, msg *core.Message, state vm.
} else {
context = core.NewEVMBlockContext(header, b.eth.BlockChain(), nil)
}
return vm.NewEVM(context, txContext, state, b.eth.blockchain.Config(), *vmConfig, b.GetCustomPrecompiles())
return vm.NewEVM(context, txContext, state, b.eth.blockchain.Config(), *vmConfig, b.GetCustomPrecompiles(header.Number.Int64()))
}
func (b *EthAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription {
@ -415,4 +415,6 @@ func (b *EthAPIBackend) StateAtTransaction(ctx context.Context, block *types.Blo
return b.eth.stateAtTransaction(ctx, block, txIndex, reexec)
}
func (b *EthAPIBackend) GetCustomPrecompiles() map[common.Address]vm.PrecompiledContract { return nil }
func (b *EthAPIBackend) GetCustomPrecompiles(int64) map[common.Address]vm.PrecompiledContract {
return nil
}

View file

@ -89,7 +89,7 @@ type Backend interface {
ChainDb() ethdb.Database
StateAtBlock(ctx context.Context, block *types.Block, reexec uint64, base vm.StateDB, readOnly bool, preferDisk bool) (vm.StateDB, StateReleaseFunc, error)
StateAtTransaction(ctx context.Context, block *types.Block, txIndex int, reexec uint64) (*types.Transaction, vm.BlockContext, vm.StateDB, StateReleaseFunc, error)
GetCustomPrecompiles() map[common.Address]vm.PrecompiledContract
GetCustomPrecompiles(int64) map[common.Address]vm.PrecompiledContract
}
// API is the collection of tracing APIs exposed over the private debugging endpoint.
@ -534,7 +534,7 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config
var (
msg, _ = core.TransactionToMessage(tx, signer, block.BaseFee())
txContext = core.NewEVMTxContext(msg)
vmenv = vm.NewEVM(vmctx, txContext, statedb, chainConfig, vm.Config{}, api.backend.GetCustomPrecompiles())
vmenv = vm.NewEVM(vmctx, txContext, statedb, chainConfig, vm.Config{}, api.backend.GetCustomPrecompiles(block.Number().Int64()))
)
statedb.SetTxContext(tx.Hash(), i)
if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.GasLimit)); err != nil {
@ -684,7 +684,7 @@ txloop:
// Generate the next state snapshot fast without tracing
msg, _ := core.TransactionToMessage(tx, signer, block.BaseFee())
statedb.SetTxContext(tx.Hash(), i)
vmenv := vm.NewEVM(blockCtx, core.NewEVMTxContext(msg), statedb, api.backend.ChainConfig(), vm.Config{}, api.backend.GetCustomPrecompiles())
vmenv := vm.NewEVM(blockCtx, core.NewEVMTxContext(msg), statedb, api.backend.ChainConfig(), vm.Config{}, api.backend.GetCustomPrecompiles(block.Number().Int64()))
if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.GasLimit)); err != nil {
failed = err
break txloop
@ -790,7 +790,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block
}
}
// Execute the transaction and flush any traces to disk
vmenv := vm.NewEVM(vmctx, txContext, statedb, chainConfig, vmConf, api.backend.GetCustomPrecompiles())
vmenv := vm.NewEVM(vmctx, txContext, statedb, chainConfig, vmConf, api.backend.GetCustomPrecompiles(block.Number().Int64()))
statedb.SetTxContext(tx.Hash(), i)
vmConf.Tracer.OnTxStart(vmenv.GetVMContext(), tx, msg.From)
vmRet, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.GasLimit))
@ -988,7 +988,7 @@ func (api *API) traceTx(ctx context.Context, tx *types.Transaction, message *cor
return nil, err
}
}
vmenv := vm.NewEVM(vmctx, vm.TxContext{GasPrice: big.NewInt(0)}, statedb, api.backend.ChainConfig(), vm.Config{Tracer: tracer.Hooks, NoBaseFee: true}, api.backend.GetCustomPrecompiles())
vmenv := vm.NewEVM(vmctx, vm.TxContext{GasPrice: big.NewInt(0)}, statedb, api.backend.ChainConfig(), vm.Config{Tracer: tracer.Hooks, NoBaseFee: true}, api.backend.GetCustomPrecompiles(txctx.BlockNumber.Int64()))
statedb.SetLogger(tracer.Hooks)
// Define a meaningful timeout of a single transaction trace

View file

@ -184,7 +184,9 @@ func (b *testBackend) StateAtTransaction(ctx context.Context, block *types.Block
return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction index %d out of range for block %#x", txIndex, block.Hash())
}
func (b *testBackend) GetCustomPrecompiles() map[common.Address]vm.PrecompiledContract { return nil }
func (b *testBackend) GetCustomPrecompiles(int64) map[common.Address]vm.PrecompiledContract {
return nil
}
func TestTraceCall(t *testing.T) {
t.Parallel()

View file

@ -1199,7 +1199,7 @@ func DoEstimateGas(ctx context.Context, b Backend, args TransactionArgs, blockNr
Header: header,
State: state,
ErrorRatio: estimateGasErrorRatio,
CustomPrecompiles: b.GetCustomPrecompiles(),
CustomPrecompiles: b.GetCustomPrecompiles(header.Number.Int64()),
}
if err := args.CallDefaults(gasCap, header.BaseFee, b.ChainConfig().ChainID); err != nil {
@ -1239,7 +1239,7 @@ func DoEstimateGasAfterCalls(ctx context.Context, b Backend, args TransactionArg
Header: header,
State: state,
ErrorRatio: estimateGasErrorRatio,
CustomPrecompiles: b.GetCustomPrecompiles(),
CustomPrecompiles: b.GetCustomPrecompiles(header.Number.Int64()),
}
if err := args.CallDefaults(gasCap, header.BaseFee, b.ChainConfig().ChainID); err != nil {

View file

@ -595,7 +595,9 @@ func (b testBackend) BloomStatus() (uint64, uint64) { panic("implement me") }
func (b testBackend) ServiceFilter(ctx context.Context, session *bloombits.MatcherSession) {
panic("implement me")
}
func (b testBackend) GetCustomPrecompiles() map[common.Address]vm.PrecompiledContract { return nil }
func (b testBackend) GetCustomPrecompiles(int64) map[common.Address]vm.PrecompiledContract {
return nil
}
func TestEstimateGas(t *testing.T) {
t.Parallel()

View file

@ -97,7 +97,7 @@ type Backend interface {
BloomStatus() (uint64, uint64)
ServiceFilter(ctx context.Context, session *bloombits.MatcherSession)
GetCustomPrecompiles() map[common.Address]vm.PrecompiledContract
GetCustomPrecompiles(int64) map[common.Address]vm.PrecompiledContract
}
func GetAPIs(apiBackend Backend) []rpc.API {

View file

@ -343,4 +343,6 @@ func (b *backendMock) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent)
func (b *backendMock) Engine() consensus.Engine { return nil }
func (b *backendMock) GetCustomPrecompiles() map[common.Address]vm.PrecompiledContract { return nil }
func (b *backendMock) GetCustomPrecompiles(int64) map[common.Address]vm.PrecompiledContract {
return nil
}