Skip to content

Commit

Permalink
feat(tx-pool): fast reject transactions that cannot fit into a block (#…
Browse files Browse the repository at this point in the history
…1042)

* feat(tx pool): fast reject transactions that cannot fit into a block

* add a TestValidateTxBlockSize unit test

* chore: auto version bump [bot]

---------

Co-authored-by: colinlyguo <[email protected]>
  • Loading branch information
colinlyguo and colinlyguo authored Sep 17, 2024
1 parent 89639dd commit 86965f8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,10 @@ func (pool *TxPool) validateTx(tx *types.Transaction, local bool) error {
if uint64(tx.Size()) > txMaxSize {
return ErrOversizedData
}
// Reject transactions that cannot fit into a block even as a single transaction
if !pool.chainconfig.Scroll.IsValidBlockSize(tx.Size()) {
return ErrOversizedData
}
// Check whether the init code size has been exceeded.
if pool.shanghai && tx.To() == nil && len(tx.Data()) > params.MaxInitCodeSize {
return fmt.Errorf("%w: code size %v limit %v", ErrMaxInitCodeSizeExceeded, len(tx.Data()), params.MaxInitCodeSize)
Expand Down
29 changes: 29 additions & 0 deletions core/tx_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2677,3 +2677,32 @@ func TestStatsWithMinBaseFee(t *testing.T) {
}
}
}

func TestValidateTxBlockSize(t *testing.T) {
pool, key := setupTxPoolWithConfig(params.ScrollMainnetChainConfig)
defer pool.Stop()

account := crypto.PubkeyToAddress(key.PublicKey)
testAddBalance(pool, account, big.NewInt(1000000000000000000))

validTx := pricedDataTransaction(1, 2100000, big.NewInt(1), key, uint64(*pool.chainconfig.Scroll.MaxTxPayloadBytesPerBlock)-128)
oversizedTx := pricedDataTransaction(2, 2100000, big.NewInt(1), key, uint64(*pool.chainconfig.Scroll.MaxTxPayloadBytesPerBlock))

tests := []struct {
name string
tx *types.Transaction
want error
}{
{"Valid transaction", validTx, nil},
{"Oversized transaction", oversizedTx, ErrOversizedData},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := pool.validateTx(tt.tx, false)
if err != tt.want {
t.Errorf("validateTx() error = %v, want %v", err, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion params/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
const (
VersionMajor = 5 // Major version component of the current release
VersionMinor = 7 // Minor version component of the current release
VersionPatch = 16 // Patch version component of the current release
VersionPatch = 17 // Patch version component of the current release
VersionMeta = "mainnet" // Version metadata to append to the version string
)

Expand Down

0 comments on commit 86965f8

Please sign in to comment.