Skip to content

Commit

Permalink
Simplify fee currency intrinsic cost calculation
Browse files Browse the repository at this point in the history
This also fixes rejection of underprices fee currency transactions.
  • Loading branch information
palango committed Jan 10, 2024
1 parent 9fc6f45 commit f6f4c13
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/evm/internal/t8ntool/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func Transaction(ctx *cli.Context) error {
// Check intrinsic gas
// TODO(pl): Adapt fee currency
if gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil,
chainConfig.IsHomestead(new(big.Int)), chainConfig.IsIstanbul(new(big.Int)), chainConfig.IsShanghai(new(big.Int), 0), nil, 0); err != nil {
chainConfig.IsHomestead(new(big.Int)), chainConfig.IsIstanbul(new(big.Int)), chainConfig.IsShanghai(new(big.Int), 0), nil); err != nil {
r.Error = err
results = append(results, r)
continue
Expand Down
2 changes: 1 addition & 1 deletion core/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func genValueTx(nbytes int) func(int, *BlockGen) {
return func(i int, gen *BlockGen) {
toaddr := common.Address{}
data := make([]byte, nbytes)
gas, _ := IntrinsicGas(data, nil, false, false, false, false, nil, 0)
gas, _ := IntrinsicGas(data, nil, false, false, false, false, nil)
signer := types.MakeSigner(gen.config, big.NewInt(int64(i)), gen.header.Time)
gasPrice := big.NewInt(0)
if gen.header.BaseFee != nil {
Expand Down
13 changes: 4 additions & 9 deletions core/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func (result *ExecutionResult) Revert() []byte {
}

// IntrinsicGas computes the 'intrinsic gas' for a message with the given data.
func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation bool, isHomestead, isEIP2028 bool, isEIP3860 bool, feeCurrency *common.Address, gasForAlternativeCurrency uint64) (uint64, error) {
func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation bool, isHomestead, isEIP2028 bool, isEIP3860 bool, feeCurrency *common.Address) (uint64, error) {
// Set the starting gas for the raw transaction
var gas uint64
if isContractCreation && isHomestead {
Expand Down Expand Up @@ -126,10 +126,10 @@ func IntrinsicGas(data []byte, accessList types.AccessList, isContractCreation b
// In this case, however, the user always ends up paying `maxGasForDebitAndCreditTransactions`
// keeping it consistent.
if feeCurrency != nil {
if (math.MaxUint64 - gas) < gasForAlternativeCurrency {
if (math.MaxUint64 - gas) < fee_currencies.IntrinsicGasForAlternativeFeeCurrency {
return 0, ErrGasUintOverflow
}
gas += gasForAlternativeCurrency
gas += fee_currencies.IntrinsicGasForAlternativeFeeCurrency
}

if accessList != nil {
Expand Down Expand Up @@ -545,13 +545,8 @@ func (st *StateTransition) innerTransitionDb() (*ExecutionResult, error) {
contractCreation = msg.To == nil
)

// If the fee currency is nil, do not retrieve the intrinsic gas adjustment from the chain state, as it will not be used.
gasForAlternativeCurrency := uint64(0)
if msg.FeeCurrency != nil {
gasForAlternativeCurrency = fee_currencies.IntrinsicGasForAlternativeFeeCurrency
}
// Check clauses 4-5, subtract intrinsic gas if everything is correct
gas, err := IntrinsicGas(msg.Data, msg.AccessList, contractCreation, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai, msg.FeeCurrency, gasForAlternativeCurrency)
gas, err := IntrinsicGas(msg.Data, msg.AccessList, contractCreation, rules.IsHomestead, rules.IsIstanbul, rules.IsShanghai, msg.FeeCurrency)
if err != nil {
return nil, err
}
Expand Down
4 changes: 1 addition & 3 deletions core/txpool/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ func ValidateTransaction(tx *types.Transaction, head *types.Header, signer types
}
// Ensure the transaction has more gas than the bare minimum needed to cover
// the transaction metadata
gasForAlternativeCurrency := uint64(0)
// TODO(pl): Adapt gas for fee currency
intrGas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, true, opts.Config.IsIstanbul(head.Number), opts.Config.IsShanghai(head.Number, head.Time), tx.FeeCurrency(), gasForAlternativeCurrency)
intrGas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, true, opts.Config.IsIstanbul(head.Number), opts.Config.IsShanghai(head.Number, head.Time), tx.FeeCurrency())
if err != nil {
return err
}
Expand Down
4 changes: 1 addition & 3 deletions light/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,10 +385,8 @@ func (pool *TxPool) validateTx(ctx context.Context, tx *types.Transaction) error
return core.ErrInsufficientFunds
}

// TODO(pl): Add correct intrinsic gas
// Should supply enough intrinsic gas
gasForAlternativeCurrency := uint64(0)
gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, true, pool.istanbul, pool.shanghai, tx.FeeCurrency(), gasForAlternativeCurrency)
gas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, true, pool.istanbul, pool.shanghai, tx.FeeCurrency())
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion tests/transaction_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (tt *TransactionTest) Run(config *params.ChainConfig) error {
return nil, nil, err
}
// Intrinsic gas
requiredGas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, isHomestead, isIstanbul, false, nil, 0)
requiredGas, err := core.IntrinsicGas(tx.Data(), tx.AccessList(), tx.To() == nil, isHomestead, isIstanbul, false, nil)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit f6f4c13

Please sign in to comment.