Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions sei-tendermint/internal/evmonlyapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ func (a *evmOnlyApplication) CheckTx(_ context.Context, req *proxy.RequestCheckT
if !ok {
return &proxy.ResponseCheckTxV2{ResponseCheckTx: &proxy.ResponseCheckTx{Code: 1, Log: "transaction gas limit exceeds int64"}}
}
if err := a.checkGasCoversIntrinsic(tx, sender); err != nil {
return &proxy.ResponseCheckTxV2{ResponseCheckTx: &proxy.ResponseCheckTx{Code: 1, Log: err.Error()}}
}
if err := a.checkSenderCanPay(tx, sender); err != nil {
return &proxy.ResponseCheckTxV2{ResponseCheckTx: &proxy.ResponseCheckTx{Code: 1, Log: err.Error()}}
}
Expand All @@ -445,6 +448,44 @@ func (a *evmOnlyApplication) CheckTx(_ context.Context, req *proxy.RequestCheckT
}
}

// checkGasCoversIntrinsic refuses tx when its gas limit is below the intrinsic gas, or the EIP-7623
// calldata floor, that the next block charges it before execution.
func (a *evmOnlyApplication) checkGasCoversIntrinsic(tx *ethtypes.Transaction, sender common.Address) error {
value, overflow := uint256.FromBig(tx.Value())
if overflow {
return errors.New("ethereum transaction value exceeds 256 bits")
}
rules := a.nextBlockRules()
intrinsic, err := ethcore.IntrinsicGas(tx.Data(), tx.AccessList(), tx.SetCodeAuthorizations(), sender, tx.To(), value, rules)
if err != nil {
return err
}
if tx.Gas() < intrinsic {
return fmt.Errorf("%w: have %d, want %d", ethcore.ErrIntrinsicGas, tx.Gas(), intrinsic)
}
if !rules.IsPrague {
return nil
}
floor, err := ethcore.FloorDataGas(rules, sender, tx.To(), value, tx.Data(), tx.AccessList())
if err != nil {
return err
}
if tx.Gas() < floor {
return fmt.Errorf("%w: have %d, want %d", ethcore.ErrFloorDataGas, tx.Gas(), floor)
}
return nil
}

// nextBlockRules returns the fork rules of the block after the committed one, at the committed
// block's time.
func (a *evmOnlyApplication) nextBlockRules() params.Rules {
for state := range a.cursor.Lock() {
// Every block's context sets PrevRandao, so the executor always runs post-merge rules.
return a.chainConfig.Rules(big.NewInt(state.committed.height+1), true, state.lastBlockTime)
}
panic("unreachable")
}

// checkSenderCanPay refuses tx when sender's committed balance does not cover its gas limit at its fee
// cap plus its value. Before the alloc's block commits, a sender holds its alloc.
func (a *evmOnlyApplication) checkSenderCanPay(tx *ethtypes.Transaction, sender common.Address) error {
Expand Down
45 changes: 45 additions & 0 deletions sei-tendermint/internal/evmonlyapp/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1653,3 +1653,48 @@ func TestEVMOnlyApplicationAdmitsOnlyTxsTheSenderCanPayFor(t *testing.T) {
check(fmt.Sprintf("after block %d's state commit", height))
}
}

func TestEVMOnlyApplicationAdmitsOnlyTxsWhoseGasCoversIntrinsicGas(t *testing.T) {
funder, err := crypto.GenerateKey()
require.NoError(t, err)
unfunded, err := crypto.GenerateKey()
require.NoError(t, err)
app := newEVMOnlyTestAppWithAlloc(t, GenesisAlloc{InitialHeight: 1, Balances: map[common.Address]*uint256.Int{
crypto.PubkeyToAddress(funder.PublicKey): new(uint256.Int).Lsh(uint256.NewInt(1), 70),
}})
_, err = app.InitChain(evmOnlyTestInitChain())
require.NoError(t, err)
recipient := common.HexToAddress("0x1000000000000000000000000000000000000001")
calldata := slices.Repeat([]byte{0xff}, 100)
// 100 non-zero bytes cost 21_000+16*100 intrinsic gas and a 21_000+10*4*100 calldata floor.
const calldataFloor = 25_000

for _, tc := range []struct {
name string
key *ecdsa.PrivateKey
to *common.Address
gas uint64
data []byte
value int64
ok bool
}{
{name: "zero gas from an unfunded sender", key: unfunded, to: &recipient},
{name: "zero gas", key: funder, to: &recipient},
{name: "below transfer intrinsic gas", key: funder, to: &recipient, gas: 20_999, value: 1},
{name: "transfer intrinsic gas", key: funder, to: &recipient, gas: 21_000, value: 1, ok: true},
{name: "below create intrinsic gas", key: funder, gas: 52_999},
{name: "create intrinsic gas", key: funder, gas: 53_000, ok: true},
{name: "below calldata floor", key: funder, to: &recipient, gas: calldataFloor - 1, data: calldata},
{name: "calldata floor", key: funder, to: &recipient, gas: calldataFloor, data: calldata, ok: true},
} {
t.Run(tc.name, func(t *testing.T) {
signed, err := ethtypes.SignTx(ethtypes.NewTx(&ethtypes.LegacyTx{
GasPrice: big.NewInt(evmOnlyMinGasPrice), Gas: tc.gas, To: tc.to, Data: tc.data, Value: big.NewInt(tc.value),
}), ethtypes.LatestSignerForChainID(new(big.Int).SetUint64(evmOnlyTestChainID)), tc.key)
require.NoError(t, err)
raw, err := signed.MarshalBinary()
require.NoError(t, err)
require.Equal(t, tc.ok, app.CheckTx(t.Context(), &proxy.RequestCheckTxV2{Tx: raw}).IsOK())
})
}
}
Loading