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
This commit is contained in:
maskpp 2024-09-30 16:54:28 +08:00 committed by GitHub
parent 96296fb42e
commit 27b4d6ebf9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 68 additions and 8 deletions

View file

@ -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.

View file

@ -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))
}