diff --git a/core/state_prefetcher.go b/core/state_prefetcher.go index fcd680a19..04d0b9716 100644 --- a/core/state_prefetcher.go +++ b/core/state_prefetcher.go @@ -52,7 +52,7 @@ func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, c header = block.Header() gaspool = new(GasPool).AddGas(block.GasLimit()) blockContext = NewCVMBlockContext(header, p.bc, nil) - cvm = vm.NewCVM(blockContext, vm.TxContext{}, statedb, p.config, cfg) + cvm = vm.NewCVM(blockContext, statedb, p.config, cfg) signer = types.MakeSigner(p.config, header.Number, header.Time) quotaPool = NewQuotaPool(header.Quota) ) diff --git a/core/state_processor.go b/core/state_processor.go index abc379696..89b3c29e1 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -77,7 +77,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg //} var ( blockContext = NewCVMBlockContext(header, p.bc, nil) - vmenv = vm.NewCVM(blockContext, vm.TxContext{}, statedb, p.config, cfg) + vmenv = vm.NewCVM(blockContext, statedb, p.config, cfg) signer = types.MakeSigner(p.config, header.Number, header.Time) ) // Iterate over and process the individual transactions @@ -166,8 +166,6 @@ func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *commo return nil, err } // Create a new context to be used in the CVM environment - blockContext := NewCVMBlockContext(header, bc, author) - txContext := NewCVMTxContext(msg) - vmenv := vm.NewCVM(blockContext, txContext, statedb, config, cfg) + vmenv := vm.NewCVM(NewCVMBlockContext(header, bc, author), statedb, config, cfg) return applyTransaction(msg, config, gp, qp, statedb, header, header.Number, header.Hash(), tx, usedGas, vmenv) } diff --git a/core/state_transition.go b/core/state_transition.go index 668b30b6a..2e84a873b 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -193,7 +193,8 @@ func NewStateTransition(cvm *vm.CVM, msg *Message, gp *GasPool, qp *QuotaPool) * // indicates a core error meaning that the message would always fail for that particular // state and would never be accepted within a block. func ApplyMessage(cvm *vm.CVM, msg *Message, gp *GasPool, qp *QuotaPool) (*ExecutionResult, error) { - return NewStateTransition(cvm, msg, gp, qp).TransitionDb() + cvm.SetTxContext(NewCVMTxContext(msg)) + return NewStateTransition(cvm, msg, gp, qp).execute() } // to returns the recipient of the message. @@ -291,10 +292,10 @@ func (st *StateTransition) TorrentSync(meta common.Address, dir string, errCh ch return } }*/ -// TransitionDb will transition the state by applying the current message and +// execute will transition the state by applying the current message and // returning the result including the used gas. It returns an error if failed. // An error indicates a consensus issue. -func (st *StateTransition) TransitionDb() (*ExecutionResult, error) { +func (st *StateTransition) execute() (*ExecutionResult, error) { if err := st.preCheck(); err != nil { return nil, err } diff --git a/core/vm/cvm.go b/core/vm/cvm.go index 9cead484f..40994bcb1 100644 --- a/core/vm/cvm.go +++ b/core/vm/cvm.go @@ -127,11 +127,10 @@ type CVM struct { // NewCVM returns a new CVM. The returned CVM is not thread safe and should // only ever be used *once*. -func NewCVM(blockCtx BlockContext, txCtx TxContext, statedb StateDB, chainConfig *params.ChainConfig, vmConfig Config) *CVM { +func NewCVM(blockCtx BlockContext, statedb StateDB, chainConfig *params.ChainConfig, vmConfig Config) *CVM { cvm := &CVM{ Context: blockCtx, - TxContext: txCtx, StateDB: statedb, vmConfig: vmConfig, chainConfig: chainConfig, @@ -151,6 +150,10 @@ func (cvm *CVM) Reset(txCtx TxContext, statedb StateDB) { cvm.StateDB = statedb } +func (cvm *CVM) SetTxContext(txCtx TxContext) { + cvm.TxContext = txCtx +} + // Cancel cancels any running CVM operation. This may be called concurrently and // it's safe to be called multiple times. func (cvm *CVM) Cancel() { diff --git a/core/vm/gas_table_test.go b/core/vm/gas_table_test.go index a219919ea..baa3efb0a 100644 --- a/core/vm/gas_table_test.go +++ b/core/vm/gas_table_test.go @@ -92,7 +92,7 @@ func TestEIP2200(t *testing.T) { CanTransfer: func(StateDB, common.Address, *big.Int) bool { return true }, Transfer: func(StateDB, common.Address, common.Address, *big.Int) {}, } - vmenv := NewCVM(vmctx, TxContext{}, statedb, params.AllCuckooProtocolChanges, Config{ExtraEips: []int{2200}}) + vmenv := NewCVM(vmctx, statedb, params.AllCuckooProtocolChanges, Config{ExtraEips: []int{2200}}) _, gas, _, err := vmenv.Call(AccountRef(common.Address{}), address, nil, tt.gaspool, new(big.Int)) if !errors.Is(err, tt.failure) { diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index e86a2e533..fd74a27d8 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -92,7 +92,7 @@ func init() { func testTwoOperandOp(t *testing.T, tests []TwoOperandTestcase, opFn executionFunc, name string) { var ( - env = NewCVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) + env = NewCVM(BlockContext{}, nil, params.TestChainConfig, Config{}) stack = newstack() pc = uint64(0) cvmInterpreter = env.interpreter @@ -191,7 +191,7 @@ func TestSAR(t *testing.T) { func TestAddMod(t *testing.T) { var ( - env = NewCVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) + env = NewCVM(BlockContext{}, nil, params.TestChainConfig, Config{}) stack = newstack() cvmInterpreter = NewCVMInterpreter(env) pc = uint64(0) @@ -230,7 +230,7 @@ func TestAddMod(t *testing.T) { // getResult is a convenience function to generate the expected values func getResult(args []*twoOperandParams, opFn executionFunc) []TwoOperandTestcase { var ( - env = NewCVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) + env = NewCVM(BlockContext{}, nil, params.TestChainConfig, Config{}) stack = newstack() pc = uint64(0) interpreter = env.interpreter @@ -280,7 +280,7 @@ func TestJsonTestcases(t *testing.T) { func opBenchmark(bench *testing.B, op executionFunc, args ...string) { var ( - env = NewCVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) + env = NewCVM(BlockContext{}, nil, params.TestChainConfig, Config{}) stack = newstack() cvmInterpreter = NewCVMInterpreter(env) ) @@ -514,7 +514,7 @@ func BenchmarkOpIsZero(b *testing.B) { func TestOpMstore(t *testing.T) { var ( - env = NewCVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) + env = NewCVM(BlockContext{}, nil, params.TestChainConfig, Config{}) stack = newstack() mem = NewMemory() cvmInterpreter = NewCVMInterpreter(env) @@ -538,7 +538,7 @@ func TestOpMstore(t *testing.T) { func BenchmarkOpMstore(bench *testing.B) { var ( - env = NewCVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) + env = NewCVM(BlockContext{}, nil, params.TestChainConfig, Config{}) stack = newstack() mem = NewMemory() cvmInterpreter = NewCVMInterpreter(env) @@ -559,7 +559,7 @@ func BenchmarkOpMstore(bench *testing.B) { func BenchmarkOpSHA3(bench *testing.B) { var ( - env = NewCVM(BlockContext{}, TxContext{}, nil, params.TestChainConfig, Config{}) + env = NewCVM(BlockContext{}, nil, params.TestChainConfig, Config{}) stack = newstack() mem = NewMemory() cvmInterpreter = NewCVMInterpreter(env) diff --git a/core/vm/runtime/env.go b/core/vm/runtime/env.go index 6e9ffa0fb..46eb96321 100644 --- a/core/vm/runtime/env.go +++ b/core/vm/runtime/env.go @@ -22,10 +22,6 @@ import ( ) func NewEnv(cfg *Config) *vm.CVM { - txContext := vm.TxContext{ - Origin: cfg.Origin, - GasPrice: cfg.GasPrice, - } blockContext := vm.BlockContext{ CanTransfer: core.CanTransfer, Transfer: core.Transfer, @@ -38,5 +34,5 @@ func NewEnv(cfg *Config) *vm.CVM { Random: cfg.Random, } - return vm.NewCVM(blockContext, txContext, cfg.State, cfg.ChainConfig, cfg.CVMConfig) + return vm.NewCVM(blockContext, cfg.State, cfg.ChainConfig, cfg.CVMConfig) } diff --git a/ctxc/api.go b/ctxc/api.go index 04f006271..7e46bf607 100644 --- a/ctxc/api.go +++ b/ctxc/api.go @@ -416,13 +416,12 @@ func (api *PrivateDebugAPI) computeTxEnv(blockHash common.Hash, txIndex int, ree for idx, tx := range block.Transactions() { // Assemble the transaction call message and return if the requested offset msg, _ := core.TransactionToMessage(tx, signer) - txContext := core.NewCVMTxContext(msg) context := core.NewCVMBlockContext(block.Header(), api.ctxc.blockchain, nil) if idx == txIndex { return msg, context, statedb, nil } // Not yet the searched for transaction, execute on top of the current state - vmenv := vm.NewCVM(context, txContext, statedb, api.config, vm.Config{}) + vmenv := vm.NewCVM(context, statedb, api.config, vm.Config{}) if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas()), new(core.QuotaPool).AddQuota(math.MaxUint64)); err != nil { return nil, vm.BlockContext{}, nil, fmt.Errorf("tx %x failed: %v", tx.Hash(), err) } diff --git a/ctxc/api_backend.go b/ctxc/api_backend.go index 3779d4aff..1e72cbd29 100644 --- a/ctxc/api_backend.go +++ b/ctxc/api_backend.go @@ -232,9 +232,8 @@ func (b *CortexAPIBackend) GetTd(ctx context.Context, blockHash common.Hash) *bi func (b *CortexAPIBackend) GetCVM(ctx context.Context, msg *core.Message, state *state.StateDB, header *types.Header, vmCfg vm.Config) *vm.CVM { - txContext := core.NewCVMTxContext(msg) context := core.NewCVMBlockContext(header, b.ctxc.BlockChain(), nil) - return vm.NewCVM(context, txContext, state, b.ctxc.chainConfig, vmCfg) + return vm.NewCVM(context, state, b.ctxc.chainConfig, vmCfg) } func (b *CortexAPIBackend) SubscribeRemovedLogsEvent(ch chan<- core.RemovedLogsEvent) event.Subscription { diff --git a/ctxc/state_accessor.go b/ctxc/state_accessor.go index 251a83cee..3411aba19 100644 --- a/ctxc/state_accessor.go +++ b/ctxc/state_accessor.go @@ -206,13 +206,12 @@ func (ctxc *Cortex) stateAtTransaction(block *types.Block, txIndex int, reexec u for idx, tx := range block.Transactions() { // Assemble the transaction call message and return if the requested offset msg, _ := core.TransactionToMessage(tx, signer) - txContext := core.NewCVMTxContext(msg) context := core.NewCVMBlockContext(block.Header(), ctxc.blockchain, nil) if idx == txIndex { return msg, context, statedb, release, nil } // Not yet the searched for transaction, execute on top of the current state - vmenv := vm.NewCVM(context, txContext, statedb, ctxc.blockchain.Config(), vm.Config{}) + vmenv := vm.NewCVM(context, statedb, ctxc.blockchain.Config(), vm.Config{}) statedb.SetTxContext(tx.Hash(), idx) if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(tx.Gas()), new(core.QuotaPool).AddQuota(block.Quota())); err != nil { return nil, vm.BlockContext{}, nil, nil, fmt.Errorf("transaction %#x failed: %v", tx.Hash(), err) diff --git a/ctxc/tracers/api.go b/ctxc/tracers/api.go index 9502c342c..2d02d59e4 100644 --- a/ctxc/tracers/api.go +++ b/ctxc/tracers/api.go @@ -561,9 +561,8 @@ func (api *API) IntermediateRoots(ctx context.Context, hash common.Hash, config ) for i, tx := range block.Transactions() { var ( - msg, _ = core.TransactionToMessage(tx, signer) - txContext = core.NewCVMTxContext(msg) - vmenv = vm.NewCVM(vmctx, txContext, statedb, chainConfig, vm.Config{}) + msg, _ = core.TransactionToMessage(tx, signer) + vmenv = vm.NewCVM(vmctx, statedb, chainConfig, vm.Config{}) ) statedb.SetTxContext(tx.Hash(), i) if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.GasLimit), new(core.QuotaPool).AddQuota(block.Quota())); err != nil { @@ -661,7 +660,7 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac // Generate the next state snapshot fast without tracing msg, _ := core.TransactionToMessage(tx, signer) statedb.SetTxContext(tx.Hash(), i) - vmenv := vm.NewCVM(blockCtx, core.NewCVMTxContext(msg), statedb, api.backend.ChainConfig(), vm.Config{}) + vmenv := vm.NewCVM(blockCtx, statedb, api.backend.ChainConfig(), vm.Config{}) if _, err := core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.GasLimit), new(core.QuotaPool).AddQuota(block.Quota())); err != nil { failed = err break @@ -742,12 +741,11 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block for i, tx := range block.Transactions() { // Prepare the transaction for un-traced execution var ( - msg, _ = core.TransactionToMessage(tx, signer) - txContext = core.NewCVMTxContext(msg) - vmConf vm.Config - dump *os.File - writer *bufio.Writer - err error + msg, _ = core.TransactionToMessage(tx, signer) + vmConf vm.Config + dump *os.File + writer *bufio.Writer + err error ) // If the transaction needs tracing, swap out the configs if tx.Hash() == txHash || txHash == (common.Hash{}) { @@ -770,7 +768,7 @@ func (api *API) standardTraceBlockToFile(ctx context.Context, block *types.Block } } // Execute the transaction and flush any traces to disk - vmenv := vm.NewCVM(vmctx, txContext, statedb, chainConfig, vmConf) + vmenv := vm.NewCVM(vmctx, statedb, chainConfig, vmConf) statedb.SetTxContext(tx.Hash(), i) _, err = core.ApplyMessage(vmenv, msg, new(core.GasPool).AddGas(msg.GasLimit), new(core.QuotaPool).AddQuota(block.Quota())) if writer != nil { @@ -912,10 +910,9 @@ func (api *API) TraceCall(ctx context.Context, args ctxcapi.CallArgs, blockNrOrH // be tracer dependent. func (api *API) traceTx(ctx context.Context, message *core.Message, txctx *Context, vmctx vm.BlockContext, statedb *state.StateDB, config *TraceConfig) (any, error) { var ( - tracer Tracer - err error - timeout = defaultTraceTimeout - txContext = core.NewCVMTxContext(message) + tracer Tracer + err error + timeout = defaultTraceTimeout ) if config == nil { config = &TraceConfig{} @@ -928,7 +925,7 @@ func (api *API) traceTx(ctx context.Context, message *core.Message, txctx *Conte return nil, err } } - vmenv := vm.NewCVM(vmctx, txContext, statedb, api.backend.ChainConfig(), vm.Config{Tracer: tracer}) + vmenv := vm.NewCVM(vmctx, statedb, api.backend.ChainConfig(), vm.Config{Tracer: tracer}) // Define a meaningful timeout of a single transaction trace if config.Timeout != nil { if timeout, err = time.ParseDuration(*config.Timeout); err != nil {