From 86965f8a48574d4cba78638781f5126a363411a8 Mon Sep 17 00:00:00 2001 From: colin <102356659+colinlyguo@users.noreply.github.com> Date: Tue, 17 Sep 2024 23:43:54 +0800 Subject: [PATCH] feat(tx-pool): fast reject transactions that cannot fit into a block (#1042) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- core/tx_pool.go | 4 ++++ core/tx_pool_test.go | 29 +++++++++++++++++++++++++++++ params/version.go | 2 +- 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/core/tx_pool.go b/core/tx_pool.go index 39e3d35010b5..e7d5062fd523 100644 --- a/core/tx_pool.go +++ b/core/tx_pool.go @@ -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) diff --git a/core/tx_pool_test.go b/core/tx_pool_test.go index 22e498718fc6..525deda66b9b 100644 --- a/core/tx_pool_test.go +++ b/core/tx_pool_test.go @@ -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) + } + }) + } +} diff --git a/params/version.go b/params/version.go index db5ab7081c53..9da09c308064 100644 --- a/params/version.go +++ b/params/version.go @@ -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 )