Skip to content

Commit 90b7acf

Browse files
authored
Check intrinsic gas in EVM-only CheckTx (#4895)
EVM-only `CheckTx` validated the signature, fee cap and that the sender could cover `gas * price + value`, but it did not check the gas limit against the intrinsic gas the executor deducts before running a transaction. A transaction with too little gas for that was admitted and only rejected once it reached block execution. `CheckTx` now computes the intrinsic gas with go-ethereum's `core.IntrinsicGas`, and the EIP-7623 calldata floor with `core.FloorDataGas` when Prague is active, under the rules of the block after the committed one. It refuses a transaction whose gas limit is below either, using the same errors the state transition returns, before the balance check runs.
1 parent bb99199 commit 90b7acf

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

‎sei-tendermint/internal/evmonlyapp/app.go‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,9 @@ func (a *evmOnlyApplication) CheckTx(_ context.Context, req *proxy.RequestCheckT
427427
if !ok {
428428
return &proxy.ResponseCheckTxV2{ResponseCheckTx: &proxy.ResponseCheckTx{Code: 1, Log: "transaction gas limit exceeds int64"}}
429429
}
430+
if err := a.checkGasCoversIntrinsic(tx, sender); err != nil {
431+
return &proxy.ResponseCheckTxV2{ResponseCheckTx: &proxy.ResponseCheckTx{Code: 1, Log: err.Error()}}
432+
}
430433
if err := a.checkSenderCanPay(tx, sender); err != nil {
431434
return &proxy.ResponseCheckTxV2{ResponseCheckTx: &proxy.ResponseCheckTx{Code: 1, Log: err.Error()}}
432435
}
@@ -445,6 +448,44 @@ func (a *evmOnlyApplication) CheckTx(_ context.Context, req *proxy.RequestCheckT
445448
}
446449
}
447450

451+
// checkGasCoversIntrinsic refuses tx when its gas limit is below the intrinsic gas, or the EIP-7623
452+
// calldata floor, that the next block charges it before execution.
453+
func (a *evmOnlyApplication) checkGasCoversIntrinsic(tx *ethtypes.Transaction, sender common.Address) error {
454+
value, overflow := uint256.FromBig(tx.Value())
455+
if overflow {
456+
return errors.New("ethereum transaction value exceeds 256 bits")
457+
}
458+
rules := a.nextBlockRules()
459+
intrinsic, err := ethcore.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), sender, tx.To(), value, rules)
460+
if err != nil {
461+
return err
462+
}
463+
if tx.Gas() < intrinsic {
464+
return fmt.Errorf("%w: have %d, want %d", ethcore.ErrIntrinsicGas, tx.Gas(), intrinsic)
465+
}
466+
if !rules.IsPrague {
467+
return nil
468+
}
469+
floor, err := ethcore.FloorDataGas(rules, sender, tx.To(), value, tx.Data(), tx.AccessList())
470+
if err != nil {
471+
return err
472+
}
473+
if tx.Gas() < floor {
474+
return fmt.Errorf("%w: have %d, want %d", ethcore.ErrFloorDataGas, tx.Gas(), floor)
475+
}
476+
return nil
477+
}
478+
479+
// nextBlockRules returns the fork rules of the block after the committed one, at the committed
480+
// block's time.
481+
func (a *evmOnlyApplication) nextBlockRules() params.Rules {
482+
for state := range a.cursor.Lock() {
483+
// Every block's context sets PrevRandao, so the executor always runs post-merge rules.
484+
return a.chainConfig.Rules(big.NewInt(state.committed.height+1), true, state.lastBlockTime)
485+
}
486+
panic("unreachable")
487+
}
488+
448489
// checkSenderCanPay refuses tx when sender's committed balance does not cover its gas limit at its fee
449490
// cap plus its value. Before the alloc's block commits, a sender holds its alloc.
450491
func (a *evmOnlyApplication) checkSenderCanPay(tx *ethtypes.Transaction, sender common.Address) error {

‎sei-tendermint/internal/evmonlyapp/app_test.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,3 +1653,48 @@ func TestEVMOnlyApplicationAdmitsOnlyTxsTheSenderCanPayFor(t *testing.T) {
16531653
check(fmt.Sprintf("after block %d's state commit", height))
16541654
}
16551655
}
1656+
1657+
func TestEVMOnlyApplicationAdmitsOnlyTxsWhoseGasCoversIntrinsicGas(t *testing.T) {
1658+
funder, err := crypto.GenerateKey()
1659+
require.NoError(t, err)
1660+
unfunded, err := crypto.GenerateKey()
1661+
require.NoError(t, err)
1662+
app := newEVMOnlyTestAppWithAlloc(t, GenesisAlloc{InitialHeight: 1, Balances: map[common.Address]*uint256.Int{
1663+
crypto.PubkeyToAddress(funder.PublicKey): new(uint256.Int).Lsh(uint256.NewInt(1), 70),
1664+
}})
1665+
_, err = app.InitChain(evmOnlyTestInitChain())
1666+
require.NoError(t, err)
1667+
recipient := common.HexToAddress("0x1000000000000000000000000000000000000001")
1668+
calldata := slices.Repeat([]byte{0xff}, 100)
1669+
// 100 non-zero bytes cost 21_000+16*100 intrinsic gas and a 21_000+10*4*100 calldata floor.
1670+
const calldataFloor = 25_000
1671+
1672+
for _, tc := range []struct {
1673+
name string
1674+
key *ecdsa.PrivateKey
1675+
to *common.Address
1676+
gas uint64
1677+
data []byte
1678+
value int64
1679+
ok bool
1680+
}{
1681+
{name: "zero gas from an unfunded sender", key: unfunded, to: &recipient},
1682+
{name: "zero gas", key: funder, to: &recipient},
1683+
{name: "below transfer intrinsic gas", key: funder, to: &recipient, gas: 20_999, value: 1},
1684+
{name: "transfer intrinsic gas", key: funder, to: &recipient, gas: 21_000, value: 1, ok: true},
1685+
{name: "below create intrinsic gas", key: funder, gas: 52_999},
1686+
{name: "create intrinsic gas", key: funder, gas: 53_000, ok: true},
1687+
{name: "below calldata floor", key: funder, to: &recipient, gas: calldataFloor - 1, data: calldata},
1688+
{name: "calldata floor", key: funder, to: &recipient, gas: calldataFloor, data: calldata, ok: true},
1689+
} {
1690+
t.Run(tc.name, func(t *testing.T) {
1691+
signed, err := ethtypes.SignTx(ethtypes.NewTx(&ethtypes.LegacyTx{
1692+
GasPrice: big.NewInt(evmOnlyMinGasPrice), Gas: tc.gas, To: tc.to, Data: tc.data, Value: big.NewInt(tc.value),
1693+
}), ethtypes.LatestSignerForChainID(new(big.Int).SetUint64(evmOnlyTestChainID)), tc.key)
1694+
require.NoError(t, err)
1695+
raw, err := signed.MarshalBinary()
1696+
require.NoError(t, err)
1697+
require.Equal(t, tc.ok, app.CheckTx(t.Context(), &proxy.RequestCheckTxV2{Tx: raw}).IsOK())
1698+
})
1699+
}
1700+
}

0 commit comments

Comments
 (0)