Skip to content

Commit d2d56b2

Browse files
mask-ppRogerLamTd
authored andcommitted
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
1 parent 57b4454 commit d2d56b2

File tree

2 files changed

+68
-8
lines changed

2 files changed

+68
-8
lines changed

miner/taiko_worker.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -294,19 +294,29 @@ loop:
294294
env.tcount++
295295
txs.Shift()
296296

297-
// Encode and compress the txList, if the byte length is > maxBytesPerTxList, remove the latest tx and break.
298-
b, err := encodeAndCompressTxList(env.txs)
297+
data, err := rlp.EncodeToBytes(env.txs)
299298
if err != nil {
300-
log.Trace("Failed to rlp encode and compress the pending transaction %s: %w", tx.Hash(), err)
299+
log.Trace("Failed to rlp encode the pending transaction %s: %w", tx.Hash(), err)
301300
txs.Pop()
302301
continue
303302
}
304-
if len(b) > int(maxBytesPerTxList) {
305-
env.txs = env.txs[0 : env.tcount-1]
306-
env.state.RevertToSnapshot(snap)
307-
env.gasPool.SetGas(gasPool)
308-
break loop
303+
304+
if len(data) >= int(maxBytesPerTxList) {
305+
// Encode and compress the txList, if the byte length is > maxBytesPerTxList, remove the latest tx and break.
306+
b, err := compress(data)
307+
if err != nil {
308+
log.Trace("Failed to rlp encode and compress the pending transaction %s: %w", tx.Hash(), err)
309+
txs.Pop()
310+
continue
311+
}
312+
if len(b) > int(maxBytesPerTxList) {
313+
env.txs = env.txs[0 : env.tcount-1]
314+
env.state.RevertToSnapshot(snap)
315+
env.gasPool.SetGas(gasPool)
316+
break loop
317+
}
309318
}
319+
310320
default:
311321
// Transaction is regarded as invalid, drop all consecutive transactions from
312322
// the same sender because of `nonce-too-high` clause.

miner/taiko_worker_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package miner
2+
3+
import (
4+
"testing"
5+
6+
"github.com/ethereum/go-ethereum/consensus/clique"
7+
"github.com/ethereum/go-ethereum/core/rawdb"
8+
"github.com/ethereum/go-ethereum/core/types"
9+
"github.com/ethereum/go-ethereum/params"
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func testGenerateWorker(t *testing.T, txCount int) *worker {
14+
t.Parallel()
15+
var (
16+
db = rawdb.NewMemoryDatabase()
17+
config = *params.AllCliqueProtocolChanges
18+
)
19+
config.Taiko = true
20+
config.Clique = &params.CliqueConfig{Period: 1, Epoch: 30000}
21+
engine := clique.New(config.Clique, db)
22+
23+
w, b := newTestWorker(t, &config, engine, db, 0)
24+
//defer w.close()
25+
26+
for i := 0; i < txCount; i++ {
27+
b.txPool.Add([]*types.Transaction{b.newRandomTx(true)}, true, false)
28+
b.txPool.Add([]*types.Transaction{b.newRandomTx(false)}, true, false)
29+
}
30+
31+
return w
32+
}
33+
34+
func TestBuildTransactionsLists(t *testing.T) {
35+
w := testGenerateWorker(t, 1000)
36+
defer w.close()
37+
38+
maxBytesPerTxList := (params.BlobTxBytesPerFieldElement - 1) * params.BlobTxFieldElementsPerBlob
39+
txLst, err := w.BuildTransactionsLists(
40+
testBankAddress,
41+
nil,
42+
240_000_000,
43+
uint64(maxBytesPerTxList),
44+
nil,
45+
1,
46+
0)
47+
assert.NoError(t, err)
48+
assert.LessOrEqual(t, 1, len(txLst))
49+
assert.LessOrEqual(t, txLst[0].BytesLength, uint64(maxBytesPerTxList))
50+
}

0 commit comments

Comments
 (0)