Skip to content

Commit 5e54fe0

Browse files
authored
Move msg.SetIsFree to core.ApplyMessage (#13880)
See Issue #9259
1 parent 0ca5c6d commit 5e54fe0

32 files changed

+134
-173
lines changed

accounts/abi/bind/backends/simulated.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@ func (m callMsg) Data() []byte { return m.CallMsg.Data
856856
func (m callMsg) AccessList() types.AccessList { return m.CallMsg.AccessList }
857857
func (m callMsg) Authorizations() []types.Authorization { return m.CallMsg.Authorizations }
858858
func (m callMsg) IsFree() bool { return false }
859+
func (m callMsg) SetIsFree(_ bool) {}
859860

860861
func (m callMsg) BlobGas() uint64 { return misc.GetBlobGasUsed(len(m.CallMsg.BlobHashes)) }
861862
func (m callMsg) MaxFeePerBlobGas() *uint256.Int { return m.CallMsg.MaxFeePerBlobGas }

cmd/state/exec3/historical_trace_worker.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,6 @@ func (rw *HistoricalTraceWorker) RunTxTask(txTask *state.TxTask) {
194194
ibs.SetTxContext(txTask.TxIndex)
195195
msg := txTask.TxAsMessage
196196
msg.SetCheckNonce(!rw.vmConfig.StatelessExec)
197-
if msg.FeeCap().IsZero() {
198-
// Only zero-gas transactions may be service ones
199-
syscall := func(contract common.Address, data []byte) ([]byte, error) {
200-
return core.SysCallContract(contract, data, rw.execArgs.ChainConfig, ibs, header, rw.execArgs.Engine, true /* constCall */)
201-
}
202-
msg.SetIsFree(rw.execArgs.Engine.IsServiceTransaction(msg.From(), syscall))
203-
}
204197

205198
txContext := core.NewEVMTxContext(msg)
206199
if rw.vmConfig.TraceJumpDest {
@@ -209,7 +202,7 @@ func (rw *HistoricalTraceWorker) RunTxTask(txTask *state.TxTask) {
209202
rw.evm.ResetBetweenBlocks(txTask.EvmBlockContext, txContext, ibs, *rw.vmConfig, rules)
210203

211204
// MA applytx
212-
applyRes, err := core.ApplyMessage(rw.evm, msg, rw.taskGasPool, true /* refunds */, false /* gasBailout */)
205+
applyRes, err := core.ApplyMessage(rw.evm, msg, rw.taskGasPool, true /* refunds */, false /* gasBailout */, rw.execArgs.Engine)
213206
if err != nil {
214207
txTask.Error = err
215208
} else {

cmd/state/exec3/state.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,18 +268,11 @@ func (rw *Worker) RunTxTaskNoLock(txTask *state.TxTask, isMining bool) {
268268
rw.vmCfg.SkipAnalysis = txTask.SkipAnalysis
269269
ibs.SetTxContext(txTask.TxIndex)
270270
msg := txTask.TxAsMessage
271-
if msg.FeeCap().IsZero() && rw.engine != nil {
272-
// Only zero-gas transactions may be service ones
273-
syscall := func(contract libcommon.Address, data []byte) ([]byte, error) {
274-
return core.SysCallContract(contract, data, rw.chainConfig, ibs, header, rw.engine, true /* constCall */)
275-
}
276-
msg.SetIsFree(rw.engine.IsServiceTransaction(msg.From(), syscall))
277-
}
278271

279272
rw.evm.ResetBetweenBlocks(txTask.EvmBlockContext, core.NewEVMTxContext(msg), ibs, rw.vmCfg, rules)
280273

281274
// MA applytx
282-
applyRes, err := core.ApplyMessage(rw.evm, msg, rw.taskGasPool, true /* refunds */, false /* gasBailout */)
275+
applyRes, err := core.ApplyMessage(rw.evm, msg, rw.taskGasPool, true /* refunds */, false /* gasBailout */, rw.engine)
283276
if err != nil {
284277
txTask.Error = err
285278
} else {

cmd/state/exec3/trace_worker.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,6 @@ func (e *TraceWorker) ExecTxn(txNum uint64, txIndex int, txn types.Transaction,
114114
return nil, err
115115
}
116116
msg.SetCheckNonce(!e.vmConfig.StatelessExec)
117-
if msg.FeeCap().IsZero() {
118-
// Only zero-gas transactions may be service ones
119-
syscall := func(contract common.Address, data []byte) ([]byte, error) {
120-
return core.SysCallContract(contract, data, e.chainConfig, e.ibs, e.header, e.engine, true /* constCall */)
121-
}
122-
msg.SetIsFree(e.engine.IsServiceTransaction(msg.From(), syscall))
123-
}
124117

125118
txContext := core.NewEVMTxContext(msg)
126119
if e.vmConfig.TraceJumpDest {
@@ -129,7 +122,7 @@ func (e *TraceWorker) ExecTxn(txNum uint64, txIndex int, txn types.Transaction,
129122
e.evm.ResetBetweenBlocks(*e.blockCtx, txContext, e.ibs, *e.vmConfig, e.rules)
130123

131124
gp := new(core.GasPool).AddGas(txn.GetGas()).AddBlobGas(txn.GetBlobGas())
132-
res, err := core.ApplyMessage(e.evm, msg, gp, true /* refunds */, gasBailout /* gasBailout */)
125+
res, err := core.ApplyMessage(e.evm, msg, gp, true /* refunds */, gasBailout /* gasBailout */, e.engine)
133126
if err != nil {
134127
return nil, fmt.Errorf("%w: blockNum=%d, txNum=%d, %s", err, e.blockNum, txNum, e.ibs.Error())
135128
}

core/blockchain.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,19 @@ func rlpHash(x interface{}) (h libcommon.Hash) {
250250
return h
251251
}
252252

253-
func SysCallContract(contract libcommon.Address, data []byte, chainConfig *chain.Config, ibs *state.IntraBlockState, header *types.Header, engine consensus.EngineReader, constCall bool) (result []byte, err error) {
253+
func SysCallContract(contract libcommon.Address, data []byte, chainConfig *chain.Config, ibs evmtypes.IntraBlockState, header *types.Header, engine consensus.EngineReader, constCall bool) (result []byte, err error) {
254+
isBor := chainConfig.Bor != nil
255+
var author *libcommon.Address
256+
if isBor {
257+
author = &header.Coinbase
258+
} else {
259+
author = &state.SystemAddress
260+
}
261+
blockContext := NewEVMBlockContext(header, GetHashFn(header, nil), engine, author, chainConfig)
262+
return SysCallContractWithBlockContext(contract, data, chainConfig, ibs, blockContext, engine, constCall)
263+
}
264+
265+
func SysCallContractWithBlockContext(contract libcommon.Address, data []byte, chainConfig *chain.Config, ibs evmtypes.IntraBlockState, blockContext evmtypes.BlockContext, engine consensus.EngineReader, constCall bool) (result []byte, err error) {
254266
msg := types.NewMessage(
255267
state.SystemAddress,
256268
&contract,
@@ -266,15 +278,11 @@ func SysCallContract(contract libcommon.Address, data []byte, chainConfig *chain
266278
// Create a new context to be used in the EVM environment
267279
isBor := chainConfig.Bor != nil
268280
var txContext evmtypes.TxContext
269-
var author *libcommon.Address
270281
if isBor {
271-
author = &header.Coinbase
272282
txContext = evmtypes.TxContext{}
273283
} else {
274-
author = &state.SystemAddress
275284
txContext = NewEVMTxContext(msg)
276285
}
277-
blockContext := NewEVMBlockContext(header, GetHashFn(header, nil), engine, author, chainConfig)
278286
evm := vm.NewEVM(blockContext, txContext, ibs, chainConfig, vmConfig)
279287

280288
ret, _, err := evm.Call(

core/state/txtask.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@ import (
2323
"sync"
2424
"time"
2525

26-
"github.com/erigontech/erigon-lib/common/dbg"
27-
"github.com/erigontech/erigon-lib/log/v3"
28-
29-
"github.com/erigontech/erigon-lib/kv"
30-
"github.com/erigontech/erigon/core/rawdb/rawtemporaldb"
3126
"github.com/holiman/uint256"
3227

3328
"github.com/erigontech/erigon-lib/chain"
3429
libcommon "github.com/erigontech/erigon-lib/common"
30+
"github.com/erigontech/erigon-lib/common/dbg"
31+
"github.com/erigontech/erigon-lib/kv"
32+
"github.com/erigontech/erigon-lib/log/v3"
3533
"github.com/erigontech/erigon-lib/state"
3634
"github.com/erigontech/erigon-lib/types/accounts"
35+
"github.com/erigontech/erigon/core/rawdb/rawtemporaldb"
3736
"github.com/erigontech/erigon/core/types"
3837
"github.com/erigontech/erigon/core/vm/evmtypes"
3938
)
@@ -58,7 +57,7 @@ type TxTask struct {
5857
Failed bool
5958
Tx types.Transaction
6059
GetHashFn func(n uint64) libcommon.Hash
61-
TxAsMessage types.Message
60+
TxAsMessage *types.Message
6261
EvmBlockContext evmtypes.BlockContext
6362

6463
HistoryExecution bool // use history reader for that txn instead of state reader

core/state_processor.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,14 @@ func applyTransaction(config *chain.Config, engine consensus.EngineReader, gp *G
4545
}
4646
msg.SetCheckNonce(!cfg.StatelessExec)
4747

48-
if msg.FeeCap().IsZero() && engine != nil {
49-
// Only zero-gas transactions may be service ones
50-
syscall := func(contract libcommon.Address, data []byte) ([]byte, error) {
51-
return SysCallContract(contract, data, config, ibs, header, engine, true /* constCall */)
52-
}
53-
msg.SetIsFree(engine.IsServiceTransaction(msg.From(), syscall))
54-
}
55-
5648
txContext := NewEVMTxContext(msg)
5749
if cfg.TraceJumpDest {
5850
txContext.TxHash = txn.Hash()
5951
}
6052

6153
// Update the evm with the new transaction context.
6254
evm.Reset(txContext, ibs)
63-
result, err := ApplyMessage(evm, msg, gp, true /* refunds */, false /* gasBailout */)
55+
result, err := ApplyMessage(evm, msg, gp, true /* refunds */, false /* gasBailout */, engine)
6456
if err != nil {
6557
return nil, nil, err
6658
}

core/state_transition.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
"github.com/erigontech/erigon-lib/common/u256"
3434
"github.com/erigontech/erigon-lib/crypto"
3535
"github.com/erigontech/erigon-lib/log/v3"
36+
"github.com/erigontech/erigon/consensus"
37+
"github.com/erigontech/erigon/core/state"
3638
"github.com/erigontech/erigon/core/tracing"
3739
"github.com/erigontech/erigon/core/types"
3840
"github.com/erigontech/erigon/core/vm"
@@ -102,7 +104,8 @@ type Message interface {
102104
BlobHashes() []libcommon.Hash
103105
Authorizations() []types.Authorization
104106

105-
IsFree() bool
107+
IsFree() bool // service transactions on Gnosis are exempt from EIP-1559 mandatory fees
108+
SetIsFree(bool)
106109
}
107110

108111
// NewStateTransition initialises and returns a new state transition object.
@@ -133,7 +136,17 @@ func NewStateTransition(evm *vm.EVM, msg Message, gp *GasPool) *StateTransition
133136
// `refunds` is false when it is not required to apply gas refunds
134137
// `gasBailout` is true when it is not required to fail transaction if the balance is not enough to pay gas.
135138
// for trace_call to replicate OE/Parity behaviour
136-
func ApplyMessage(evm *vm.EVM, msg Message, gp *GasPool, refunds bool, gasBailout bool) (*evmtypes.ExecutionResult, error) {
139+
func ApplyMessage(evm *vm.EVM, msg Message, gp *GasPool, refunds bool, gasBailout bool, engine consensus.EngineReader) (
140+
*evmtypes.ExecutionResult, error) {
141+
// Only zero-gas transactions may be service ones
142+
if msg.FeeCap().IsZero() && !msg.IsFree() && engine != nil {
143+
blockContext := evm.Context
144+
blockContext.Coinbase = state.SystemAddress
145+
syscall := func(contract libcommon.Address, data []byte) ([]byte, error) {
146+
return SysCallContractWithBlockContext(contract, data, evm.ChainConfig(), evm.IntraBlockState(), blockContext, engine, true /* constCall */)
147+
}
148+
msg.SetIsFree(engine.IsServiceTransaction(msg.From(), syscall))
149+
}
137150
return NewStateTransition(evm, msg, gp).TransitionDb(refunds, gasBailout)
138151
}
139152

core/types/access_list_tx.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ func (tx *AccessListTx) DecodeRLP(s *rlp.Stream) error {
411411
}
412412

413413
// AsMessage returns the transaction as a core.Message.
414-
func (tx *AccessListTx) AsMessage(s Signer, _ *big.Int, rules *chain.Rules) (Message, error) {
414+
func (tx *AccessListTx) AsMessage(s Signer, _ *big.Int, rules *chain.Rules) (*Message, error) {
415415
msg := Message{
416416
nonce: tx.Nonce,
417417
gasLimit: tx.Gas,
@@ -426,12 +426,12 @@ func (tx *AccessListTx) AsMessage(s Signer, _ *big.Int, rules *chain.Rules) (Mes
426426
}
427427

428428
if !rules.IsBerlin {
429-
return msg, errors.New("eip-2930 transactions require Berlin")
429+
return nil, errors.New("eip-2930 transactions require Berlin")
430430
}
431431

432432
var err error
433433
msg.from, err = tx.Sender(s)
434-
return msg, err
434+
return &msg, err
435435
}
436436

437437
func (tx *AccessListTx) WithSignature(signer Signer, sig []byte) (Transaction, error) {

core/types/blob_tx.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (stx *BlobTx) GetBlobGas() uint64 {
6262
return fixedgas.BlobGasPerBlob * uint64(len(stx.BlobVersionedHashes))
6363
}
6464

65-
func (stx *BlobTx) AsMessage(s Signer, baseFee *big.Int, rules *chain.Rules) (Message, error) {
65+
func (stx *BlobTx) AsMessage(s Signer, baseFee *big.Int, rules *chain.Rules) (*Message, error) {
6666
msg := Message{
6767
nonce: stx.Nonce,
6868
gasLimit: stx.Gas,
@@ -76,12 +76,12 @@ func (stx *BlobTx) AsMessage(s Signer, baseFee *big.Int, rules *chain.Rules) (Me
7676
checkNonce: true,
7777
}
7878
if !rules.IsCancun {
79-
return msg, errors.New("BlobTx transactions require Cancun")
79+
return nil, errors.New("BlobTx transactions require Cancun")
8080
}
8181
if baseFee != nil {
8282
overflow := msg.gasPrice.SetFromBig(baseFee)
8383
if overflow {
84-
return msg, errors.New("gasPrice higher than 2^256-1")
84+
return nil, errors.New("gasPrice higher than 2^256-1")
8585
}
8686
}
8787
msg.gasPrice.Add(&msg.gasPrice, stx.Tip)
@@ -92,7 +92,7 @@ func (stx *BlobTx) AsMessage(s Signer, baseFee *big.Int, rules *chain.Rules) (Me
9292
msg.from, err = stx.Sender(s)
9393
msg.maxFeePerBlobGas = *stx.MaxFeePerBlobGas
9494
msg.blobHashes = stx.BlobVersionedHashes
95-
return msg, err
95+
return &msg, err
9696
}
9797

9898
func (stx *BlobTx) cachedSender() (sender libcommon.Address, ok bool) {

core/types/blob_tx_wrapper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func (txw *BlobTxWrapper) GetBlobGas() uint64 { return txw.Tx.GetBlobGas(
312312
func (txw *BlobTxWrapper) GetValue() *uint256.Int { return txw.Tx.GetValue() }
313313
func (txw *BlobTxWrapper) GetTo() *libcommon.Address { return txw.Tx.GetTo() }
314314

315-
func (txw *BlobTxWrapper) AsMessage(s Signer, baseFee *big.Int, rules *chain.Rules) (Message, error) {
315+
func (txw *BlobTxWrapper) AsMessage(s Signer, baseFee *big.Int, rules *chain.Rules) (*Message, error) {
316316
return txw.Tx.AsMessage(s, baseFee, rules)
317317
}
318318
func (txw *BlobTxWrapper) WithSignature(signer Signer, sig []byte) (Transaction, error) {

core/types/dynamic_fee_tx.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func (tx *DynamicFeeTransaction) DecodeRLP(s *rlp.Stream) error {
329329
}
330330

331331
// AsMessage returns the transaction as a core.Message.
332-
func (tx *DynamicFeeTransaction) AsMessage(s Signer, baseFee *big.Int, rules *chain.Rules) (Message, error) {
332+
func (tx *DynamicFeeTransaction) AsMessage(s Signer, baseFee *big.Int, rules *chain.Rules) (*Message, error) {
333333
msg := Message{
334334
nonce: tx.Nonce,
335335
gasLimit: tx.Gas,
@@ -343,12 +343,12 @@ func (tx *DynamicFeeTransaction) AsMessage(s Signer, baseFee *big.Int, rules *ch
343343
checkNonce: true,
344344
}
345345
if !rules.IsLondon {
346-
return msg, errors.New("eip-1559 transactions require London")
346+
return nil, errors.New("eip-1559 transactions require London")
347347
}
348348
if baseFee != nil {
349349
overflow := msg.gasPrice.SetFromBig(baseFee)
350350
if overflow {
351-
return msg, errors.New("gasPrice higher than 2^256-1")
351+
return nil, errors.New("gasPrice higher than 2^256-1")
352352
}
353353
}
354354
msg.gasPrice.Add(&msg.gasPrice, tx.Tip)
@@ -358,7 +358,7 @@ func (tx *DynamicFeeTransaction) AsMessage(s Signer, baseFee *big.Int, rules *ch
358358

359359
var err error
360360
msg.from, err = tx.Sender(s)
361-
return msg, err
361+
return &msg, err
362362
}
363363

364364
// Hash computes the hash (but not for signatures!)

core/types/legacy_tx.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func (tx *LegacyTx) DecodeRLP(s *rlp.Stream) error {
343343
}
344344

345345
// AsMessage returns the transaction as a core.Message.
346-
func (tx *LegacyTx) AsMessage(s Signer, _ *big.Int, _ *chain.Rules) (Message, error) {
346+
func (tx *LegacyTx) AsMessage(s Signer, _ *big.Int, _ *chain.Rules) (*Message, error) {
347347
msg := Message{
348348
nonce: tx.Nonce,
349349
gasLimit: tx.Gas,
@@ -359,7 +359,7 @@ func (tx *LegacyTx) AsMessage(s Signer, _ *big.Int, _ *chain.Rules) (Message, er
359359

360360
var err error
361361
msg.from, err = tx.Sender(s)
362-
return msg, err
362+
return &msg, err
363363
}
364364

365365
func (tx *LegacyTx) WithSignature(signer Signer, sig []byte) (Transaction, error) {

core/types/set_code_tx.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (tx *SetCodeTransaction) MarshalBinary(w io.Writer) error {
114114
return nil
115115
}
116116

117-
func (tx *SetCodeTransaction) AsMessage(s Signer, baseFee *big.Int, rules *chain.Rules) (Message, error) {
117+
func (tx *SetCodeTransaction) AsMessage(s Signer, baseFee *big.Int, rules *chain.Rules) (*Message, error) {
118118
msg := Message{
119119
nonce: tx.Nonce,
120120
gasLimit: tx.Gas,
@@ -128,12 +128,12 @@ func (tx *SetCodeTransaction) AsMessage(s Signer, baseFee *big.Int, rules *chain
128128
checkNonce: true,
129129
}
130130
if !rules.IsPrague {
131-
return msg, errors.New("SetCodeTransaction is only supported in Prague")
131+
return nil, errors.New("SetCodeTransaction is only supported in Prague")
132132
}
133133
if baseFee != nil {
134134
overflow := msg.gasPrice.SetFromBig(baseFee)
135135
if overflow {
136-
return msg, errors.New("gasPrice higher than 2^256-1")
136+
return nil, errors.New("gasPrice higher than 2^256-1")
137137
}
138138
}
139139
msg.gasPrice.Add(&msg.gasPrice, tx.Tip)
@@ -142,13 +142,13 @@ func (tx *SetCodeTransaction) AsMessage(s Signer, baseFee *big.Int, rules *chain
142142
}
143143

144144
if len(tx.Authorizations) == 0 {
145-
return msg, errors.New("SetCodeTransaction without authorizations is invalid")
145+
return nil, errors.New("SetCodeTransaction without authorizations is invalid")
146146
}
147147
msg.authorizations = tx.Authorizations
148148

149149
var err error
150150
msg.from, err = tx.Sender(s)
151-
return msg, err
151+
return &msg, err
152152
}
153153

154154
func (tx *SetCodeTransaction) Sender(signer Signer) (libcommon.Address, error) {

0 commit comments

Comments
 (0)