Skip to content

Commit 8a96e01

Browse files
committed
fix: send fewer transactions if all not included
1 parent d403f29 commit 8a96e01

6 files changed

Lines changed: 56 additions & 27 deletions

File tree

runner/network/sequencer_benchmark.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,10 +238,12 @@ func (nb *sequencerBenchmark) Run(ctx context.Context, metricsCollector metrics.
238238

239239
blockMetrics := metrics.NewBlockMetrics()
240240

241+
pendingTxs := 0
242+
241243
// run for a few blocks
242244
for i := 0; i < params.NumBlocks; i++ {
243245
blockMetrics.SetBlockNumber(uint64(i) + 1)
244-
err := transactionWorker.SendTxs(benchmarkCtx)
246+
txsSent, err := transactionWorker.SendTxs(benchmarkCtx, pendingTxs)
245247
if err != nil {
246248
nb.log.Warn("failed to send transactions", "err", err)
247249
errChan <- err
@@ -259,6 +261,17 @@ func (nb *sequencerBenchmark) Run(ctx context.Context, metricsCollector metrics.
259261
return
260262
}
261263

264+
// Track how many user txs are still pending in the node's mempool.
265+
// payload.Transactions includes the L1 info deposit tx, so user txs = total - 1.
266+
userTxsIncluded := len(payload.Transactions) - 1
267+
if userTxsIncluded < 0 {
268+
userTxsIncluded = 0
269+
}
270+
pendingTxs = pendingTxs + txsSent - userTxsIncluded
271+
if pendingTxs < 0 {
272+
pendingTxs = 0
273+
}
274+
262275
time.Sleep(1000 * time.Millisecond)
263276

264277
err = metricsCollector.Collect(benchmarkCtx, blockMetrics)

runner/payload/contract/contract_worker.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,13 @@ func (t *contractPayloadWorker) sendContractTx(ctx context.Context) error {
286286
return nil
287287
}
288288

289-
func (t *contractPayloadWorker) SendTxs(ctx context.Context) error {
289+
func (t *contractPayloadWorker) SendTxs(ctx context.Context, _ int) (int, error) {
290290
for i := 0; i < t.params.CallsPerBlock; i++ {
291291
err := t.sendContractTx(ctx)
292292

293293
if err != nil {
294294
t.log.Error("Failed to send transaction", "error", err)
295-
return err
295+
return 0, err
296296
}
297297

298298
debugResult, err := t.debugContract()
@@ -301,5 +301,5 @@ func (t *contractPayloadWorker) SendTxs(ctx context.Context) error {
301301
}
302302
}
303303

304-
return nil
304+
return t.params.CallsPerBlock, nil
305305
}

runner/payload/simulator/worker.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -574,14 +574,22 @@ func (t *simulatorPayloadWorker) waitForReceipt(ctx context.Context, txHash comm
574574
})
575575
}
576576

577-
func (t *simulatorPayloadWorker) sendTxs(ctx context.Context) error {
577+
func (t *simulatorPayloadWorker) sendTxs(ctx context.Context, pendingTxs int) (int, error) {
578578
txs := make([]*types.Transaction, 0, t.numCallers)
579579

580580
gas := t.params.GasLimit - 100_000
581581

582582
sendTxsStartTime := time.Now()
583583

584-
for i := uint64(0); i < uint64(math.Ceil(float64(t.numCallsPerBlock)*t.scaleFactor)); i++ {
584+
targetCalls := uint64(math.Ceil(float64(t.numCallsPerBlock) * t.scaleFactor))
585+
callsToSend := targetCalls
586+
if uint64(pendingTxs) >= targetCalls {
587+
callsToSend = 0
588+
} else {
589+
callsToSend = targetCalls - uint64(pendingTxs)
590+
}
591+
592+
for i := uint64(0); i < callsToSend; i++ {
585593
actual := t.actualNumConfig
586594
expected := t.payloadParams.Mul(float64(t.numCalls+1) * t.scaleFactor)
587595

@@ -600,7 +608,7 @@ func (t *simulatorPayloadWorker) sendTxs(ctx context.Context) error {
600608
transferTx, err := t.createCallTx(t.transactors[callerIdx], t.callerKeys[callerIdx], blockCounts)
601609
if err != nil {
602610
t.log.Error("Failed to create transfer transaction", "err", err)
603-
return err
611+
return 0, err
604612
}
605613

606614
t.gasUsedCache[blockCounts.Hash()] = transferTx.Gas()
@@ -625,8 +633,8 @@ func (t *simulatorPayloadWorker) sendTxs(ctx context.Context) error {
625633

626634
t.mempool.AddTransactions(txs)
627635
sendTxsDuration := time.Since(sendTxsStartTime)
628-
log.Info("Send transactions duration", "duration", sendTxsDuration, "numCalls", uint64(math.Ceil(float64(t.numCallsPerBlock)*t.scaleFactor)))
629-
return nil
636+
log.Info("Send transactions duration", "duration", sendTxsDuration, "targetCalls", targetCalls, "callsSent", len(txs), "pendingTxs", pendingTxs)
637+
return len(txs), nil
630638
}
631639

632640
func (t *simulatorPayloadWorker) createCallTx(transactor *bind.TransactOpts, fromPriv *ecdsa.PrivateKey, config *simulatorstats.Stats) (*types.Transaction, error) {
@@ -663,10 +671,11 @@ func (t *simulatorPayloadWorker) createDeployTx(fromPriv *ecdsa.PrivateKey) (*co
663671
return &deployAddr, deployTx, nil
664672
}
665673

666-
func (t *simulatorPayloadWorker) SendTxs(ctx context.Context) error {
667-
if err := t.sendTxs(ctx); err != nil {
674+
func (t *simulatorPayloadWorker) SendTxs(ctx context.Context, pendingTxs int) (int, error) {
675+
n, err := t.sendTxs(ctx, pendingTxs)
676+
if err != nil {
668677
t.log.Error("Failed to send transactions", "err", err)
669-
return err
678+
return 0, err
670679
}
671-
return nil
680+
return n, nil
672681
}

runner/payload/transferonly/worker.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,14 +246,16 @@ func (t *transferOnlyPayloadWorker) waitForReceipt(ctx context.Context, txHash c
246246
})
247247
}
248248

249-
func (t *transferOnlyPayloadWorker) sendTxs(ctx context.Context) error {
249+
func (t *transferOnlyPayloadWorker) sendTxs(ctx context.Context, pendingTxs int) (int, error) {
250250
numAccounts := t.numAccounts()
251-
gasUsed := uint64(0)
252251
txs := make([]*types.Transaction, 0, numAccounts)
253252
acctIdx := 0
254253

255254
randomInt := rand.Uint64()
256255

256+
// Account for gas that pending transactions (still in the node mempool) will consume.
257+
gasUsed := uint64(pendingTxs) * 21000
258+
257259
for gasUsed < (t.params.GasLimit - 100_000) {
258260

259261
dest := t.addresses[(acctIdx+1)%numAccounts]
@@ -273,20 +275,19 @@ func (t *transferOnlyPayloadWorker) sendTxs(ctx context.Context) error {
273275
transferTx, err := t.createTransferTx(t.privateKeys[acctIdx], t.nextNonce[t.addresses[acctIdx]], dest, big.NewInt(1))
274276
if err != nil {
275277
t.log.Error("Failed to create transfer transaction", "err", err)
276-
return err
278+
return 0, err
277279
}
278280

279281
txs = append(txs, transferTx)
280282

281283
gasUsed += transferTx.Gas()
282284

283285
t.nextNonce[t.addresses[acctIdx]]++
284-
// 21000 gas per transfer
285286
acctIdx = (acctIdx + 1) % numAccounts
286287
}
287288

288289
t.mempool.AddTransactions(txs)
289-
return nil
290+
return len(txs), nil
290291
}
291292

292293
func (t *transferOnlyPayloadWorker) createTransferTx(fromPriv *ecdsa.PrivateKey, nonce uint64, toAddr common.Address, amount *big.Int) (*types.Transaction, error) {
@@ -305,10 +306,11 @@ func (t *transferOnlyPayloadWorker) createTransferTx(fromPriv *ecdsa.PrivateKey,
305306
return tx, nil
306307
}
307308

308-
func (t *transferOnlyPayloadWorker) SendTxs(ctx context.Context) error {
309-
if err := t.sendTxs(ctx); err != nil {
309+
func (t *transferOnlyPayloadWorker) SendTxs(ctx context.Context, pendingTxs int) (int, error) {
310+
n, err := t.sendTxs(ctx, pendingTxs)
311+
if err != nil {
310312
t.log.Error("Failed to send transactions", "err", err)
311-
return err
313+
return 0, err
312314
}
313-
return nil
315+
return n, nil
314316
}

runner/payload/txfuzz/tx_fuzz_worker.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ func (t *txFuzzPayloadWorker) Stop(ctx context.Context) error {
8181
return nil
8282
}
8383

84-
func (t *txFuzzPayloadWorker) SendTxs(ctx context.Context) error {
84+
func (t *txFuzzPayloadWorker) SendTxs(ctx context.Context, _ int) (int, error) {
8585
t.log.Info("Sending txs in tx-fuzz mode")
86-
pendingTxs := t.proxyServer.PendingTxs()
86+
pending := t.proxyServer.PendingTxs()
8787
t.proxyServer.ClearPendingTxs()
8888

89-
t.mempool.AddTransactions(pendingTxs)
90-
return nil
89+
t.mempool.AddTransactions(pending)
90+
return len(pending), nil
9191
}

runner/payload/worker/types.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import (
99
// Note: Payload workers are responsible keeping track of gas in a block and sending transactions to the mempool.
1010
type Worker interface {
1111
Setup(ctx context.Context) error
12-
SendTxs(ctx context.Context) error
12+
// SendTxs generates and queues transactions for the next block.
13+
// pendingTxs is the number of previously-sent transactions still in the node's
14+
// mempool; implementations should reduce their output accordingly so the
15+
// mempool stays close to one block's worth of work.
16+
// Returns the number of transactions actually queued.
17+
SendTxs(ctx context.Context, pendingTxs int) (int, error)
1318
Stop(ctx context.Context) error
1419
Mempool() mempool.FakeMempool
1520
}

0 commit comments

Comments
 (0)