Skip to content

Commit

Permalink
feat(taiko_api): reduce the frequency of zlib compression when fetc…
Browse files Browse the repository at this point in the history
…hing txpool content (#323)

* reduce the frequency of zlib compression when fetching txpool content

* fix go import order

* fix comment
  • Loading branch information
mask-pp authored Sep 30, 2024
1 parent 96296fb commit 27b4d6e
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 8 deletions.
26 changes: 18 additions & 8 deletions miner/taiko_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
50 changes: 50 additions & 0 deletions miner/taiko_worker_test.go
Original file line number Diff line number Diff line change
@@ -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 = &params.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))
}

0 comments on commit 27b4d6e

Please sign in to comment.