Skip to content

Commit 67ac5f0

Browse files
roberto-bayardofjl
andauthored
core, core/types: plain Message struct (#25977)
Here, the core.Message interface turns into a plain struct and types.Message gets removed. This is a breaking change to packages core and core/types. While we do not promise API stability for package core, we do for core/types. An exception can be made for types.Message, since it doesn't have any purpose apart from invoking the state transition in package core. types.Message was also marked deprecated by the same commit it got added in, 4dca5d4 (November 2016). The core.Message interface was added in December 2014, in commit db49417, for the purpose of 'testing' state transitions. It's the same change that made transaction struct fields private. Before that, the state transition used *types.Transaction directly. Over time, multiple implementations of the interface accrued across different packages, since constructing a Message is required whenever one wants to invoke the state transition. These implementations all looked very similar, a struct with private fields exposing the fields as accessor methods. By changing Message into a struct with public fields we can remove all these useless interface implementations. It will also hopefully simplify future changes to the type with less updates to apply across all of go-ethereum when a field is added to Message. --------- Co-authored-by: Felix Lange <[email protected]>
1 parent 08f6a2a commit 67ac5f0

25 files changed

+265
-281
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -644,20 +644,33 @@ func (b *SimulatedBackend) callContract(ctx context.Context, call ethereum.CallM
644644
if call.Value == nil {
645645
call.Value = new(big.Int)
646646
}
647+
647648
// Set infinite balance to the fake caller account.
648649
from := stateDB.GetOrNewStateObject(call.From)
649650
from.SetBalance(math.MaxBig256)
651+
650652
// Execute the call.
651-
msg := callMsg{call}
653+
msg := &core.Message{
654+
From: call.From,
655+
To: call.To,
656+
Value: call.Value,
657+
GasLimit: call.Gas,
658+
GasPrice: call.GasPrice,
659+
GasFeeCap: call.GasFeeCap,
660+
GasTipCap: call.GasTipCap,
661+
Data: call.Data,
662+
AccessList: call.AccessList,
663+
SkipAccountChecks: true,
664+
}
652665

653-
txContext := core.NewEVMTxContext(msg)
654-
evmContext := core.NewEVMBlockContext(header, b.blockchain, nil)
655666
// Create a new environment which holds all relevant information
656667
// about the transaction and calling mechanisms.
668+
txContext := core.NewEVMTxContext(msg)
669+
evmContext := core.NewEVMBlockContext(header, b.blockchain, nil)
657670
vmEnv := vm.NewEVM(evmContext, txContext, stateDB, b.config, vm.Config{NoBaseFee: true})
658671
gasPool := new(core.GasPool).AddGas(math.MaxUint64)
659672

660-
return core.NewStateTransition(vmEnv, msg, gasPool).TransitionDb()
673+
return core.ApplyMessage(vmEnv, msg, gasPool)
661674
}
662675

663676
// SendTransaction updates the pending block to include the given transaction.
@@ -821,23 +834,6 @@ func (b *SimulatedBackend) Blockchain() *core.BlockChain {
821834
return b.blockchain
822835
}
823836

824-
// callMsg implements core.Message to allow passing it as a transaction simulator.
825-
type callMsg struct {
826-
ethereum.CallMsg
827-
}
828-
829-
func (m callMsg) From() common.Address { return m.CallMsg.From }
830-
func (m callMsg) Nonce() uint64 { return 0 }
831-
func (m callMsg) IsFake() bool { return true }
832-
func (m callMsg) To() *common.Address { return m.CallMsg.To }
833-
func (m callMsg) GasPrice() *big.Int { return m.CallMsg.GasPrice }
834-
func (m callMsg) GasFeeCap() *big.Int { return m.CallMsg.GasFeeCap }
835-
func (m callMsg) GasTipCap() *big.Int { return m.CallMsg.GasTipCap }
836-
func (m callMsg) Gas() uint64 { return m.CallMsg.Gas }
837-
func (m callMsg) Value() *big.Int { return m.CallMsg.Value }
838-
func (m callMsg) Data() []byte { return m.CallMsg.Data }
839-
func (m callMsg) AccessList() types.AccessList { return m.CallMsg.AccessList }
840-
841837
// filterBackend implements filters.Backend to support filtering for logs without
842838
// taking bloom-bits acceleration structures into account.
843839
type filterBackend struct {

cmd/evm/internal/t8ntool/execution.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
163163
}
164164

165165
for i, tx := range txs {
166-
msg, err := tx.AsMessage(signer, pre.Env.BaseFee)
166+
msg, err := core.TransactionToMessage(tx, signer, pre.Env.BaseFee)
167167
if err != nil {
168168
log.Warn("rejected tx", "index", i, "hash", tx.Hash(), "error", err)
169169
rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
@@ -188,7 +188,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
188188
msgResult, err := core.ApplyMessage(evm, msg, gaspool)
189189
if err != nil {
190190
statedb.RevertToSnapshot(snapshot)
191-
log.Info("rejected tx", "index", i, "hash", tx.Hash(), "from", msg.From(), "error", err)
191+
log.Info("rejected tx", "index", i, "hash", tx.Hash(), "from", msg.From, "error", err)
192192
rejectedTxs = append(rejectedTxs, &rejectedTx{i, err.Error()})
193193
gaspool.SetGas(prevGas)
194194
continue
@@ -220,7 +220,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
220220
receipt.GasUsed = msgResult.UsedGas
221221

222222
// If the transaction created a contract, store the creation address in the receipt.
223-
if msg.To() == nil {
223+
if msg.To == nil {
224224
receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce())
225225
}
226226

core/evm.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ func NewEVMBlockContext(header *types.Header, chain ChainContext, author *common
7070
}
7171

7272
// NewEVMTxContext creates a new transaction context for a single transaction.
73-
func NewEVMTxContext(msg Message) vm.TxContext {
73+
func NewEVMTxContext(msg *Message) vm.TxContext {
7474
return vm.TxContext{
75-
Origin: msg.From(),
76-
GasPrice: new(big.Int).Set(msg.GasPrice()),
75+
Origin: msg.From,
76+
GasPrice: new(big.Int).Set(msg.GasPrice),
7777
}
7878
}
7979

core/state_prefetcher.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, c
6363
return
6464
}
6565
// Convert the transaction into an executable message and pre-cache its sender
66-
msg, err := tx.AsMessage(signer, header.BaseFee)
66+
msg, err := TransactionToMessage(tx, signer, header.BaseFee)
6767
if err != nil {
6868
return // Also invalid block, bail out
6969
}
@@ -85,7 +85,7 @@ func (p *statePrefetcher) Prefetch(block *types.Block, statedb *state.StateDB, c
8585
// precacheTransaction attempts to apply a transaction to the given state database
8686
// and uses the input parameters for its environment. The goal is not to execute
8787
// the transaction successfully, rather to warm up touched data slots.
88-
func precacheTransaction(msg types.Message, config *params.ChainConfig, gaspool *GasPool, statedb *state.StateDB, header *types.Header, evm *vm.EVM) error {
88+
func precacheTransaction(msg *Message, config *params.ChainConfig, gaspool *GasPool, statedb *state.StateDB, header *types.Header, evm *vm.EVM) error {
8989
// Update the evm with the new transaction context.
9090
evm.Reset(NewEVMTxContext(msg), statedb)
9191
// Add addresses to access list if applicable

core/state_processor.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
7474
vmenv := vm.NewEVM(blockContext, vm.TxContext{}, statedb, p.config, cfg)
7575
// Iterate over and process the individual transactions
7676
for i, tx := range block.Transactions() {
77-
msg, err := tx.AsMessage(types.MakeSigner(p.config, header.Number), header.BaseFee)
77+
msg, err := TransactionToMessage(tx, types.MakeSigner(p.config, header.Number), header.BaseFee)
7878
if err != nil {
7979
return nil, nil, 0, fmt.Errorf("could not apply tx %d [%v]: %w", i, tx.Hash().Hex(), err)
8080
}
@@ -97,7 +97,7 @@ func (p *StateProcessor) Process(block *types.Block, statedb *state.StateDB, cfg
9797
return receipts, allLogs, *usedGas, nil
9898
}
9999

100-
func applyTransaction(msg types.Message, config *params.ChainConfig, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, error) {
100+
func applyTransaction(msg *Message, config *params.ChainConfig, gp *GasPool, statedb *state.StateDB, blockNumber *big.Int, blockHash common.Hash, tx *types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, error) {
101101
// Create a new context to be used in the EVM environment.
102102
txContext := NewEVMTxContext(msg)
103103
evm.Reset(txContext, statedb)
@@ -129,7 +129,7 @@ func applyTransaction(msg types.Message, config *params.ChainConfig, gp *GasPool
129129
receipt.GasUsed = result.UsedGas
130130

131131
// If the transaction created a contract, store the creation address in the receipt.
132-
if msg.To() == nil {
132+
if msg.To == nil {
133133
receipt.ContractAddress = crypto.CreateAddress(evm.TxContext.Origin, tx.Nonce())
134134
}
135135

@@ -147,7 +147,7 @@ func applyTransaction(msg types.Message, config *params.ChainConfig, gp *GasPool
147147
// for the transaction, gas used and an error if the transaction failed,
148148
// indicating the block was invalid.
149149
func ApplyTransaction(config *params.ChainConfig, bc ChainContext, author *common.Address, gp *GasPool, statedb *state.StateDB, header *types.Header, tx *types.Transaction, usedGas *uint64, cfg vm.Config) (*types.Receipt, error) {
150-
msg, err := tx.AsMessage(types.MakeSigner(config, header.Number), header.BaseFee)
150+
msg, err := TransactionToMessage(tx, types.MakeSigner(config, header.Number), header.BaseFee)
151151
if err != nil {
152152
return nil, err
153153
}

0 commit comments

Comments
 (0)