From 2dece926b70a1224b77aed080fd75075b2ce1089 Mon Sep 17 00:00:00 2001 From: maskpp Date: Sun, 29 Sep 2024 16:16:38 +0800 Subject: [PATCH 1/3] reduce the frequency of zlib compression when fetching txpool content --- miner/taiko_worker.go | 26 +++++++++++++------- miner/taiko_worker_test.go | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 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..75047ad383e6 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 := encodeAndCompressTxList(env.txs) + 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..6c1f7ac59509 --- /dev/null +++ b/miner/taiko_worker_test.go @@ -0,0 +1,49 @@ +package miner + +import ( + "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" + "testing" +) + +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)) +} From 25229476b9af2caba5090dd4db139375925e0531 Mon Sep 17 00:00:00 2001 From: maskpp Date: Sun, 29 Sep 2024 16:47:44 +0800 Subject: [PATCH 2/3] fix go import order --- miner/taiko_worker_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/miner/taiko_worker_test.go b/miner/taiko_worker_test.go index 6c1f7ac59509..95bab25315f3 100644 --- a/miner/taiko_worker_test.go +++ b/miner/taiko_worker_test.go @@ -1,12 +1,13 @@ 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" - "testing" ) func testGenerateWorker(t *testing.T, txCount int) *worker { From f6cc9564805f6290c66de757384a475364a98131 Mon Sep 17 00:00:00 2001 From: maskpp Date: Mon, 30 Sep 2024 16:39:32 +0800 Subject: [PATCH 3/3] fix comment --- miner/taiko_worker.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miner/taiko_worker.go b/miner/taiko_worker.go index 75047ad383e6..d59d29cd9130 100644 --- a/miner/taiko_worker.go +++ b/miner/taiko_worker.go @@ -303,7 +303,7 @@ 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 := encodeAndCompressTxList(env.txs) + 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()