From d2d56b2418bc2b3500582859b217fc25b28f2c31 Mon Sep 17 00:00:00 2001 From: maskpp Date: Mon, 30 Sep 2024 16:54:28 +0800 Subject: [PATCH] feat(taiko_api): reduce the frequency of `zlib` compression when fetching txpool content (#323) * reduce the frequency of zlib compression when fetching txpool content * fix go import order * fix comment --- miner/taiko_worker.go | 26 ++++++++++++++------ miner/taiko_worker_test.go | 50 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 miner/taiko_worker_test.go diff --git a/miner/taiko_worker.go b/miner/taiko_worker.go index 031a97ab5812..d59d29cd9130 100644 --- a/miner/taiko_worker.go +++ b/miner/taiko_worker.go @@ -294,19 +294,29 @@ loop: env.tcount++ txs.Shift() - // Encode and compress the txList, if the byte length is > maxBytesPerTxList, remove the latest tx and break. - b, err := encodeAndCompressTxList(env.txs) + data, err := rlp.EncodeToBytes(env.txs) if err != nil { - log.Trace("Failed to rlp encode and compress the pending transaction %s: %w", tx.Hash(), err) + log.Trace("Failed to rlp encode the pending transaction %s: %w", tx.Hash(), err) txs.Pop() continue } - if len(b) > int(maxBytesPerTxList) { - env.txs = env.txs[0 : env.tcount-1] - env.state.RevertToSnapshot(snap) - env.gasPool.SetGas(gasPool) - break loop + + if len(data) >= int(maxBytesPerTxList) { + // Encode and compress the txList, if the byte length is > maxBytesPerTxList, remove the latest tx and break. + b, err := compress(data) + if err != nil { + log.Trace("Failed to rlp encode and compress the pending transaction %s: %w", tx.Hash(), err) + txs.Pop() + continue + } + if len(b) > int(maxBytesPerTxList) { + env.txs = env.txs[0 : env.tcount-1] + env.state.RevertToSnapshot(snap) + env.gasPool.SetGas(gasPool) + break loop + } } + default: // Transaction is regarded as invalid, drop all consecutive transactions from // the same sender because of `nonce-too-high` clause. diff --git a/miner/taiko_worker_test.go b/miner/taiko_worker_test.go new file mode 100644 index 000000000000..95bab25315f3 --- /dev/null +++ b/miner/taiko_worker_test.go @@ -0,0 +1,50 @@ +package miner + +import ( + "testing" + + "github.com/ethereum/go-ethereum/consensus/clique" + "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/params" + "github.com/stretchr/testify/assert" +) + +func testGenerateWorker(t *testing.T, txCount int) *worker { + t.Parallel() + var ( + db = rawdb.NewMemoryDatabase() + config = *params.AllCliqueProtocolChanges + ) + config.Taiko = true + config.Clique = ¶ms.CliqueConfig{Period: 1, Epoch: 30000} + engine := clique.New(config.Clique, db) + + w, b := newTestWorker(t, &config, engine, db, 0) + //defer w.close() + + for i := 0; i < txCount; i++ { + b.txPool.Add([]*types.Transaction{b.newRandomTx(true)}, true, false) + b.txPool.Add([]*types.Transaction{b.newRandomTx(false)}, true, false) + } + + return w +} + +func TestBuildTransactionsLists(t *testing.T) { + w := testGenerateWorker(t, 1000) + defer w.close() + + maxBytesPerTxList := (params.BlobTxBytesPerFieldElement - 1) * params.BlobTxFieldElementsPerBlob + txLst, err := w.BuildTransactionsLists( + testBankAddress, + nil, + 240_000_000, + uint64(maxBytesPerTxList), + nil, + 1, + 0) + assert.NoError(t, err) + assert.LessOrEqual(t, 1, len(txLst)) + assert.LessOrEqual(t, txLst[0].BytesLength, uint64(maxBytesPerTxList)) +}