Skip to content

Commit ddb4dc8

Browse files
committed
core: refactor IntrinsicGas function to accept rules parameter
Updated the IntrinsicGas function signature to take a pointer to rules instead of multiple boolean flags for gas calculations. This change simplifies the function calls across various components, including transaction validation and gas computation in state transitions, enhancing code readability and maintainability.
1 parent d71b6cc commit ddb4dc8

5 files changed

Lines changed: 39 additions & 3 deletions

File tree

cmd/evm/internal/t8ntool/transaction.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,14 @@ func Transaction(ctx *cli.Context) error {
133133
r.Address = sender
134134
}
135135
// Check intrinsic gas
136+
<<<<<<< HEAD
136137
if gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil,
137138
chainConfig.IsHomestead(new(big.Int)), chainConfig.IsIstanbul(new(big.Int)), chainConfig.IsShanghai(new(big.Int), 0)); err != nil {
139+
=======
140+
rules := chainConfig.Rules(common.Big0, true, 0)
141+
gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, &rules)
142+
if err != nil {
143+
>>>>>>> 6f1b514f2 (core: refactor IntrinsicGas function to accept rules parameter)
138144
r.Error = err
139145
results = append(results, r)
140146
continue

core/bench_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,24 @@ var (
8080
// value-transfer transaction with n bytes of extra data in each
8181
// block.
8282
func genValueTx(nbytes int) func(int, *BlockGen) {
83+
<<<<<<< HEAD
8384
return func(i int, gen *BlockGen) {
8485
toaddr := common.Address{}
8586
data := make([]byte, nbytes)
8687
gas, _ := IntrinsicGas(data, nil, false, false, false, false)
88+
=======
89+
// We can reuse the data for all transactions.
90+
// During signing, the method tx.WithSignature(s, sig)
91+
// performs:
92+
// cpy := tx.inner.copy()
93+
// cpy.setSignatureValues(signer.ChainID(), v, r, s)
94+
// After this operation, the data can be reused by the caller.
95+
data := make([]byte, nbytes)
96+
rules := params.NonActivatedConfig.Rules(common.Big0, false, 0)
97+
return func(i int, gen *BlockGen) {
98+
toaddr := common.Address{}
99+
gas, _ := IntrinsicGas(data, nil, nil, false, &rules)
100+
>>>>>>> 6f1b514f2 (core: refactor IntrinsicGas function to accept rules parameter)
87101
signer := gen.Signer()
88102
gasPrice := big.NewInt(0)
89103
if gen.header.BaseFee != nil {

core/state_transition.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,14 @@ func (result *ExecutionResult) Revert() []byte {
6767
}
6868

6969
// IntrinsicGas computes the 'intrinsic gas' for a message with the given data.
70+
<<<<<<< HEAD
7071
func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation bool, isHomestead, isEIP2028, isEIP3860 bool) (uint64, error) {
72+
=======
73+
func IntrinsicGas(data []byte, accessList types.AccessList, authList []types.SetCodeAuthorization, isContractCreation bool, rules *params.Rules) (uint64, error) {
74+
>>>>>>> 6f1b514f2 (core: refactor IntrinsicGas function to accept rules parameter)
7175
// Set the starting gas for the raw transaction
7276
var gas uint64
73-
if isContractCreation && isHomestead {
77+
if isContractCreation && rules.IsHomestead {
7478
gas = params.TxGasContractCreation
7579
} else {
7680
gas = params.TxGas
@@ -87,7 +91,7 @@ func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation b
8791
}
8892
// Make sure we don't exceed uint64 for all data combinations
8993
nonZeroGas := params.TxDataNonZeroGasFrontier
90-
if isEIP2028 {
94+
if rules.IsIstanbul {
9195
nonZeroGas = params.TxDataNonZeroGasEIP2028
9296
}
9397
if (math.MaxUint64-gas)/nonZeroGas < nz {
@@ -101,7 +105,7 @@ func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation b
101105
}
102106
gas += z * params.TxDataZeroGas
103107

104-
if isContractCreation && isEIP3860 {
108+
if isContractCreation && rules.IsShanghai {
105109
lenWords := toWordSize(dataLen)
106110
if (math.MaxUint64-gas)/params.InitCodeWordGas < lenWords {
107111
return 0, ErrGasUintOverflow
@@ -395,7 +399,11 @@ func (st *StateTransition) transitionDb() (*ExecutionResult, error) {
395399
)
396400

397401
// Check clauses 4-5, subtract intrinsic gas if everything is correct
402+
<<<<<<< HEAD
398403
gas, err := IntrinsicGas(msg.Data, msg.AccessList, contractCreation, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai)
404+
=======
405+
gas, err := IntrinsicGas(msg.Data, msg.AccessList, msg.SetCodeAuthorizations, contractCreation, &rules)
406+
>>>>>>> 6f1b514f2 (core: refactor IntrinsicGas function to accept rules parameter)
399407
if err != nil {
400408
return nil, err
401409
}

core/txpool/validation.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
102102
}
103103
// Ensure the transaction has more gas than the bare minimum needed to cover
104104
// the transaction metadata
105+
<<<<<<< HEAD
105106
intrGas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, true, opts.Config.IsIstanbul(head.Number), opts.Config.IsShanghai(head.Number, head.Time))
107+
=======
108+
intrGas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, &rules)
109+
>>>>>>> 6f1b514f2 (core: refactor IntrinsicGas function to accept rules parameter)
106110
if err != nil {
107111
return err
108112
}

tests/transaction_test_util.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ func (tt *TransactionTest) Run(config *params.ChainConfig) error {
5555
return nil, nil, err
5656
}
5757
// Intrinsic gas
58+
<<<<<<< HEAD
5859
requiredGas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, isHomestead, isIstanbul, false)
60+
=======
61+
requiredGas, err = core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), tx.To() == nil, rules)
62+
>>>>>>> 6f1b514f2 (core: refactor IntrinsicGas function to accept rules parameter)
5963
if err != nil {
6064
return nil, nil, err
6165
}

0 commit comments

Comments
 (0)