Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move tx context set to apply msg #2219

Merged
merged 1 commit into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/state_prefetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand Down
6 changes: 2 additions & 4 deletions core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
7 changes: 4 additions & 3 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 5 additions & 2 deletions core/vm/cvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion core/vm/gas_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 7 additions & 7 deletions core/vm/instructions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions core/vm/runtime/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
3 changes: 1 addition & 2 deletions ctxc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
3 changes: 1 addition & 2 deletions ctxc/api_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions ctxc/state_accessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
29 changes: 13 additions & 16 deletions ctxc/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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{}) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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{}
Expand All @@ -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 {
Expand Down
Loading