Skip to content

Commit f633759

Browse files
committed
Transaction Gas Limit Cap
1 parent 78d1949 commit f633759

9 files changed

Lines changed: 60 additions & 7 deletions

File tree

core/error.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,6 @@ var (
6868

6969
//ErrMetaInfoNotMature = errors.New("cvm: errMetaInfoNotMature")
7070
ErrInsufficientFundsForTransfer = errors.New("insufficient funds for transfer")
71+
72+
ErrGasLimitTooHigh = errors.New("transaction gas limit too high")
7173
)

core/state_transition.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ func (st *stateTransition) preCheck() error {
245245
return err
246246
}
247247

248+
// Verify tx gas limit does not exceed EIP-7825 cap.
249+
if st.cvm.ChainConfig().IsOsaka(st.cvm.Context.BlockNumber, st.cvm.Context.Time) && msg.GasLimit > params.MaxTxGas {
250+
return fmt.Errorf("%w (cap: %d, tx: %d)", ErrGasLimitTooHigh, params.MaxTxGas, msg.GasLimit)
251+
}
252+
248253
return st.buyGas()
249254
}
250255

core/txpool/txpool.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ func (config *Config) sanitize() Config {
231231
return conf
232232
}
233233

234+
type PendingFilter struct {
235+
GasLimitCap uint64 // Maximum gas can be used for a single transaction execution (0 means no limit)
236+
}
237+
234238
// TxPool contains all currently known transactions. Transactions
235239
// enter the pool when they are received from the network or submitted
236240
// locally. They exit the pool when they are included in the blockchain.
@@ -524,13 +528,27 @@ func (pool *TxPool) ContentFrom(addr common.Address) (types.Transactions, types.
524528
// Pending retrieves all currently processable transactions, grouped by origin
525529
// account and sorted by nonce. The returned transaction set is a copy and can be
526530
// freely modified by calling code.
527-
func (pool *TxPool) Pending(enforceTips bool) map[common.Address]types.Transactions {
531+
func (pool *TxPool) Pending(filter PendingFilter) map[common.Address]types.Transactions {
528532
pool.mu.Lock()
529533
defer pool.mu.Unlock()
530534

531535
pending := make(map[common.Address]types.Transactions, len(pool.pending))
532536
for addr, list := range pool.pending {
533-
pending[addr] = list.Flatten()
537+
//pending[addr] = list.Flatten()
538+
txs := list.Flatten()
539+
if filter.GasLimitCap != 0 {
540+
for i, tx := range txs {
541+
if filter.GasLimitCap != 0 {
542+
if tx.Gas() > filter.GasLimitCap {
543+
txs = txs[:i]
544+
break
545+
}
546+
}
547+
}
548+
}
549+
if len(txs) > 0 {
550+
pending[addr] = txs
551+
}
534552
}
535553
return pending
536554
}
@@ -568,6 +586,9 @@ func (pool *TxPool) validateTxBasics(tx *types.Transaction, local bool) error {
568586
if tx.Size() > txMaxSize {
569587
return ErrOversizedData
570588
}
589+
//if pool.chainconfig.IsOsaka && tx.Gas() > params.MaxTxGas {
590+
// return fmt.Errorf("%w (cap: %d, tx: %d)", core.ErrGasLimitTooHigh, params.MaxTxGas, tx.Gas())
591+
//}
571592
// Transactions can't be negative. This may never happen using RLP decoded
572593
// transactions but may occur if you create a transaction using the RPC.
573594
if tx.Value().Sign() < 0 {
@@ -1191,6 +1212,21 @@ func (pool *TxPool) runReorg(done chan struct{}, reset *txpoolResetRequest, dirt
11911212
}
11921213
pool.mu.Lock()
11931214
if reset != nil {
1215+
if reset.newHead != nil && reset.oldHead != nil {
1216+
// Discard the transactions with the gas limit higher than the cap.
1217+
if pool.chainconfig.IsOsaka(reset.newHead.Number, reset.newHead.Time) && !pool.chainconfig.IsOsaka(reset.oldHead.Number, reset.oldHead.Time) {
1218+
var hashes []common.Hash
1219+
pool.all.Range(func(hash common.Hash, tx *types.Transaction, local bool) bool {
1220+
if tx.Gas() > params.MaxTxGas {
1221+
hashes = append(hashes, hash)
1222+
}
1223+
return true
1224+
}, false, true)
1225+
for _, hash := range hashes {
1226+
pool.removeTx(hash, true)
1227+
}
1228+
}
1229+
}
11941230
// Reset from the old head to the new, rescheduling any reorged transactions
11951231
pool.reset(reset.oldHead, reset.newHead)
11961232

ctxc/api_backend.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (b *CortexAPIBackend) SendTx(ctx context.Context, signedTx *types.Transacti
266266
}
267267

268268
func (b *CortexAPIBackend) GetPoolTransactions() (types.Transactions, error) {
269-
pending := b.ctxc.txPool.Pending(false)
269+
pending := b.ctxc.txPool.Pending(txpool.PendingFilter{})
270270
var txs types.Transactions
271271
for _, batch := range pending {
272272
txs = append(txs, batch...)

ctxc/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/CortexFoundation/CortexTheseus/common"
3030
"github.com/CortexFoundation/CortexTheseus/core"
3131
"github.com/CortexFoundation/CortexTheseus/core/forkid"
32+
"github.com/CortexFoundation/CortexTheseus/core/txpool"
3233
"github.com/CortexFoundation/CortexTheseus/core/types"
3334
"github.com/CortexFoundation/CortexTheseus/crypto"
3435
"github.com/CortexFoundation/CortexTheseus/ctxc/downloader"
@@ -95,7 +96,7 @@ type txPool interface {
9596

9697
// Pending should return pending transactions.
9798
// The slice should be modifiable by the caller.
98-
Pending(enforceTips bool) map[common.Address]types.Transactions
99+
Pending(filter txpool.PendingFilter) map[common.Address]types.Transactions
99100

100101
// SubscribeNewTxsEvent should return an event subscription of
101102
// NewTxsEvent and send events to the given channel.

ctxc/helper_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/CortexFoundation/CortexTheseus/core"
3333
"github.com/CortexFoundation/CortexTheseus/core/forkid"
3434
"github.com/CortexFoundation/CortexTheseus/core/rawdb"
35+
"github.com/CortexFoundation/CortexTheseus/core/txpool"
3536
"github.com/CortexFoundation/CortexTheseus/core/types"
3637
"github.com/CortexFoundation/CortexTheseus/core/vm"
3738
"github.com/CortexFoundation/CortexTheseus/crypto"
@@ -142,7 +143,7 @@ func (p *testTxPool) AddRemotes(txs []*types.Transaction) []error {
142143
}
143144

144145
// Pending returns all the transactions known to the pool
145-
func (p *testTxPool) Pending(b bool) map[common.Address]types.Transactions {
146+
func (p *testTxPool) Pending(b txpool.PendingFilter) map[common.Address]types.Transactions {
146147
p.lock.RLock()
147148
defer p.lock.RUnlock()
148149

ctxc/sync.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/CortexFoundation/CortexTheseus/common"
2525
"github.com/CortexFoundation/CortexTheseus/core/rawdb"
26+
"github.com/CortexFoundation/CortexTheseus/core/txpool"
2627
"github.com/CortexFoundation/CortexTheseus/core/types"
2728
"github.com/CortexFoundation/CortexTheseus/ctxc/downloader"
2829
"github.com/CortexFoundation/CortexTheseus/log"
@@ -52,7 +53,7 @@ func (pm *ProtocolManager) syncTransactions(p *peer) {
5253
//
5354
// TODO(karalabe): Figure out if we could get away with random order somehow
5455
var txs types.Transactions
55-
pending := pm.txpool.Pending(false)
56+
pending := pm.txpool.Pending(txpool.PendingFilter{})
5657
for _, batch := range pending {
5758
txs = append(txs, batch...)
5859
}

miner/worker.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/CortexFoundation/CortexTheseus/consensus"
2828
"github.com/CortexFoundation/CortexTheseus/core"
2929
"github.com/CortexFoundation/CortexTheseus/core/state"
30+
"github.com/CortexFoundation/CortexTheseus/core/txpool"
3031
"github.com/CortexFoundation/CortexTheseus/core/types"
3132
"github.com/CortexFoundation/CortexTheseus/event"
3233
"github.com/CortexFoundation/CortexTheseus/log"
@@ -998,9 +999,13 @@ func (w *worker) commitNewWork(interrupt *atomic.Int32, noempty bool, timestamp
998999
// into the given sealing block. The transaction selection and ordering strategy can
9991000
// be customized with the plugin in the future.
10001001
func (w *worker) fillTransactions(interrupt *atomic.Int32, env *environment) error {
1002+
filter := txpool.PendingFilter{}
1003+
if w.chainConfig.IsOsaka(env.header.Number, env.header.Time) {
1004+
filter.GasLimitCap = params.MaxTxGas
1005+
}
10011006
// Split the pending transactions into locals and remotes
10021007
// Fill the block with all available pending transactions.
1003-
pending := w.ctxc.TxPool().Pending(true)
1008+
pending := w.ctxc.TxPool().Pending(filter)
10041009
localTxs, remoteTxs := make(map[common.Address]types.Transactions), pending
10051010
for _, account := range w.ctxc.TxPool().Locals() {
10061011
if txs := remoteTxs[account]; len(txs) > 0 {

params/protocol_params.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const (
3030
MinerGasFloor uint64 = MinGasLimit
3131
MinerGasCeil uint64 = 160000000
3232

33+
MaxTxGas uint64 = 30_000_000 // Maximum transaction gas limit after eip-7825.
34+
3335
MaximumExtraDataSize uint64 = 32 // Maximum size extra data may be after Genesis.
3436
ExpByteGas uint64 = 10 // Times ceil(log256(exponent)) for the EXP instruction.
3537
SloadGas uint64 = 50 // Multiplied by the number of 32-byte words that are copied (round up) for any *COPY operation and added.

0 commit comments

Comments
 (0)