Skip to content

Commit 11fe44b

Browse files
committed
fix transaction submission before network start
1 parent f2d2499 commit 11fe44b

File tree

7 files changed

+109
-82
lines changed

7 files changed

+109
-82
lines changed

pkg/clients/execution/pool.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,21 @@ func (pool *Pool) AwaitReadyEndpoint(ctx context.Context, clientType ClientType)
108108
}
109109
}
110110

111+
func (pool *Pool) AwaitReadyEndpoints(ctx context.Context, shuffle bool) []*Client {
112+
for {
113+
clients := pool.GetReadyEndpoints(shuffle)
114+
if len(clients) > 0 {
115+
return clients
116+
}
117+
118+
select {
119+
case <-ctx.Done():
120+
return nil
121+
case <-time.After(1 * time.Second):
122+
}
123+
}
124+
}
125+
111126
func (pool *Pool) GetReadyEndpoints(shuffle bool) []*Client {
112127
canonicalFork := pool.GetCanonicalFork(-1)
113128
if canonicalFork == nil {

pkg/tasks/generate_blob_transactions/task.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -286,30 +286,15 @@ func (t *Task) generateTransaction(ctx context.Context, transactionIdx uint64, c
286286
txData = t.transactionData
287287
}
288288

289-
blobTx, err := txbuilder.BuildBlobTx(&txbuilder.TxMetadata{
290-
GasFeeCap: uint256.MustFromBig(t.config.FeeCap),
291-
GasTipCap: uint256.MustFromBig(t.config.TipCap),
292-
BlobFeeCap: uint256.MustFromBig(t.config.BlobFeeCap),
293-
Gas: t.config.GasLimit,
294-
To: &toAddr,
295-
Value: uint256.MustFromBig(txAmount),
296-
Data: txData,
297-
}, blobRefs)
298-
if err != nil {
299-
return fmt.Errorf("cannot build blob tx data: %w", err)
300-
}
301-
302-
tx, err := txWallet.BuildBlobTx(blobTx)
303-
if err != nil {
304-
return fmt.Errorf("cannot build blob transaction: %w", err)
305-
}
306-
307289
var clients []*execution.Client
308290

309291
clientPool := t.ctx.Scheduler.GetServices().ClientPool()
310292

311293
if t.config.ClientPattern == "" && t.config.ExcludeClientPattern == "" {
312-
clients = clientPool.GetExecutionPool().GetReadyEndpoints(true)
294+
clients = clientPool.GetExecutionPool().AwaitReadyEndpoints(ctx, true)
295+
if len(clients) == 0 {
296+
return ctx.Err()
297+
}
313298
} else {
314299
poolClients := clientPool.GetClientsByNamePatterns(t.config.ClientPattern, t.config.ExcludeClientPattern)
315300
if len(poolClients) == 0 {
@@ -322,14 +307,28 @@ func (t *Task) generateTransaction(ctx context.Context, transactionIdx uint64, c
322307
}
323308
}
324309

325-
if len(clients) == 0 {
326-
return fmt.Errorf("no ready clients available")
310+
blobTx, err := txbuilder.BuildBlobTx(&txbuilder.TxMetadata{
311+
GasFeeCap: uint256.MustFromBig(t.config.FeeCap),
312+
GasTipCap: uint256.MustFromBig(t.config.TipCap),
313+
BlobFeeCap: uint256.MustFromBig(t.config.BlobFeeCap),
314+
Gas: t.config.GasLimit,
315+
To: &toAddr,
316+
Value: uint256.MustFromBig(txAmount),
317+
Data: txData,
318+
}, blobRefs)
319+
if err != nil {
320+
return fmt.Errorf("cannot build blob tx data: %w", err)
321+
}
322+
323+
tx, err := txWallet.BuildBlobTx(blobTx)
324+
if err != nil {
325+
return fmt.Errorf("cannot build blob transaction: %w", err)
327326
}
328327

329328
walletMgr := t.ctx.Scheduler.GetServices().WalletManager()
330329
client := walletMgr.GetClient(clients[transactionIdx%uint64(len(clients))])
331330

332-
return walletMgr.GetTxPool().SendTransaction(ctx, txWallet, tx, &spamoor.SendTransactionOptions{
331+
err = walletMgr.GetTxPool().SendTransaction(ctx, txWallet, tx, &spamoor.SendTransactionOptions{
333332
Client: client,
334333
Rebroadcast: true,
335334
OnComplete: completeFn,
@@ -353,4 +352,10 @@ func (t *Task) generateTransaction(ctx context.Context, transactionIdx uint64, c
353352
logEntry.Infof("submitted blob transaction %v (nonce: %v, attempt: %v)", transactionIdx, tx.Nonce(), retry)
354353
},
355354
})
355+
if err != nil {
356+
txWallet.MarkSkippedNonce(tx.Nonce())
357+
return err
358+
}
359+
360+
return nil
356361
}

pkg/tasks/generate_consolidations/task.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,10 @@ func (t *Task) generateConsolidation(ctx context.Context, accountIdx uint64, onC
359359
var clients []*execution.Client
360360

361361
if t.config.ClientPattern == "" && t.config.ExcludeClientPattern == "" {
362-
clients = clientPool.GetExecutionPool().GetReadyEndpoints(true)
362+
clients = clientPool.GetExecutionPool().AwaitReadyEndpoints(ctx, true)
363+
if len(clients) == 0 {
364+
return nil, ctx.Err()
365+
}
363366
} else {
364367
poolClients := clientPool.GetClientsByNamePatterns(t.config.ClientPattern, t.config.ExcludeClientPattern)
365368
if len(poolClients) == 0 {
@@ -372,10 +375,6 @@ func (t *Task) generateConsolidation(ctx context.Context, accountIdx uint64, onC
372375
}
373376
}
374377

375-
if len(clients) == 0 {
376-
return nil, fmt.Errorf("no ready clients available")
377-
}
378-
379378
walletMgr := t.ctx.Scheduler.GetServices().WalletManager()
380379

381380
txWallet, err := walletMgr.GetWalletByPrivkey(ctx, t.walletPrivKey)
@@ -429,6 +428,7 @@ func (t *Task) generateConsolidation(ctx context.Context, accountIdx uint64, onC
429428
},
430429
})
431430
if err != nil {
431+
txWallet.MarkSkippedNonce(tx.Nonce())
432432
return nil, fmt.Errorf("failed sending consolidation transaction: %w", err)
433433
}
434434

pkg/tasks/generate_deposits/task.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,10 @@ func (t *Task) generateDeposit(ctx context.Context, accountIdx uint64, onComplet
504504
var clients []*execution.Client
505505

506506
if t.config.ClientPattern == "" && t.config.ExcludeClientPattern == "" {
507-
clients = clientPool.GetExecutionPool().GetReadyEndpoints(true)
507+
clients = clientPool.GetExecutionPool().AwaitReadyEndpoints(ctx, true)
508+
if len(clients) == 0 {
509+
return nil, nil, ctx.Err()
510+
}
508511
} else {
509512
poolClients := clientPool.GetClientsByNamePatterns(t.config.ClientPattern, t.config.ExcludeClientPattern)
510513
if len(poolClients) == 0 {
@@ -517,10 +520,6 @@ func (t *Task) generateDeposit(ctx context.Context, accountIdx uint64, onComplet
517520
}
518521
}
519522

520-
if len(clients) == 0 {
521-
return nil, nil, fmt.Errorf("no ready clients available")
522-
}
523-
524523
depositContract, err := depositcontract.NewDepositContract(t.depositContractAddr, clients[0].GetRPCClient().GetEthClient())
525524
if err != nil {
526525
return nil, nil, fmt.Errorf("cannot create bound instance of DepositContract: %w", err)
@@ -575,6 +574,7 @@ func (t *Task) generateDeposit(ctx context.Context, accountIdx uint64, onComplet
575574
},
576575
})
577576
if err != nil {
577+
txWallet.MarkSkippedNonce(tx.Nonce())
578578
return nil, nil, fmt.Errorf("failed sending deposit transaction: %w", err)
579579
}
580580

pkg/tasks/generate_eoa_transactions/task.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,27 @@ func (t *Task) generateTransaction(ctx context.Context, transactionIdx uint64, c
293293
txData = t.transactionData
294294
}
295295

296+
var clients []*execution.Client
297+
298+
clientPool := t.ctx.Scheduler.GetServices().ClientPool()
299+
300+
if t.config.ClientPattern == "" && t.config.ExcludeClientPattern == "" {
301+
clients = clientPool.GetExecutionPool().AwaitReadyEndpoints(ctx, true)
302+
if len(clients) == 0 {
303+
return ctx.Err()
304+
}
305+
} else {
306+
poolClients := clientPool.GetClientsByNamePatterns(t.config.ClientPattern, t.config.ExcludeClientPattern)
307+
if len(poolClients) == 0 {
308+
return fmt.Errorf("no client found with pattern %v", t.config.ClientPattern)
309+
}
310+
311+
clients = make([]*execution.Client, len(poolClients))
312+
for i, c := range poolClients {
313+
clients[i] = c.ExecutionClient
314+
}
315+
}
316+
296317
var tx *ethtypes.Transaction
297318

298319
var err error
@@ -330,32 +351,10 @@ func (t *Task) generateTransaction(ctx context.Context, transactionIdx uint64, c
330351
return fmt.Errorf("cannot build transaction: %w", err)
331352
}
332353

333-
var clients []*execution.Client
334-
335-
clientPool := t.ctx.Scheduler.GetServices().ClientPool()
336-
337-
if t.config.ClientPattern == "" && t.config.ExcludeClientPattern == "" {
338-
clients = clientPool.GetExecutionPool().GetReadyEndpoints(true)
339-
} else {
340-
poolClients := clientPool.GetClientsByNamePatterns(t.config.ClientPattern, t.config.ExcludeClientPattern)
341-
if len(poolClients) == 0 {
342-
return fmt.Errorf("no client found with pattern %v", t.config.ClientPattern)
343-
}
344-
345-
clients = make([]*execution.Client, len(poolClients))
346-
for i, c := range poolClients {
347-
clients[i] = c.ExecutionClient
348-
}
349-
}
350-
351-
if len(clients) == 0 {
352-
return fmt.Errorf("no ready clients available")
353-
}
354-
355354
walletMgr := t.ctx.Scheduler.GetServices().WalletManager()
356355
client := walletMgr.GetClient(clients[transactionIdx%uint64(len(clients))])
357356

358-
return walletMgr.GetTxPool().SendTransaction(ctx, txWallet, tx, &spamoor.SendTransactionOptions{
357+
err = walletMgr.GetTxPool().SendTransaction(ctx, txWallet, tx, &spamoor.SendTransactionOptions{
359358
Client: client,
360359
Rebroadcast: true,
361360
OnComplete: completeFn,
@@ -383,4 +382,10 @@ func (t *Task) generateTransaction(ctx context.Context, transactionIdx uint64, c
383382
logEntry.Infof("submitted tx %v: %v", transactionIdx, tx.Hash().Hex())
384383
},
385384
})
385+
if err != nil {
386+
txWallet.MarkSkippedNonce(tx.Nonce())
387+
return err
388+
}
389+
390+
return nil
386391
}

pkg/tasks/generate_transaction/task.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -163,32 +163,17 @@ func (t *Task) Execute(ctx context.Context) error {
163163

164164
t.logger.Infof("wallet: %v [nonce: %v] %v ETH", t.wallet.GetAddress().Hex(), t.wallet.GetNonce(), t.wallet.GetReadableBalance(18, 0, 4, false, false))
165165

166-
t.ctx.ReportProgress(0, "Generating transaction...")
167-
168-
tx, err := t.generateTransaction()
169-
if err != nil {
170-
return err
171-
}
172-
173-
if txData, err2 := vars.GeneralizeData(tx); err2 == nil {
174-
t.ctx.Outputs.SetVar("transaction", txData)
175-
} else {
176-
t.logger.Warnf("Failed setting `transaction` output: %v", err2)
177-
}
178-
179-
txBytes, err := tx.MarshalBinary()
180-
if err != nil {
181-
t.logger.Warnf("Failed setting `transactionHex` output: %v", err)
182-
} else {
183-
t.ctx.Outputs.SetVar("transactionHex", hexutil.Encode(txBytes))
184-
}
166+
t.ctx.ReportProgress(0, "Waiting for ready clients...")
185167

186168
var clients []*execution.Client
187169

188170
clientPool := t.ctx.Scheduler.GetServices().ClientPool()
189171

190172
if t.config.ClientPattern == "" && t.config.ExcludeClientPattern == "" {
191-
clients = clientPool.GetExecutionPool().GetReadyEndpoints(true)
173+
clients = clientPool.GetExecutionPool().AwaitReadyEndpoints(ctx, true)
174+
if len(clients) == 0 {
175+
return ctx.Err()
176+
}
192177
} else {
193178
poolClients := clientPool.GetClientsByNamePatterns(t.config.ClientPattern, t.config.ExcludeClientPattern)
194179
if len(poolClients) == 0 {
@@ -201,8 +186,24 @@ func (t *Task) Execute(ctx context.Context) error {
201186
}
202187
}
203188

204-
if len(clients) == 0 {
205-
return fmt.Errorf("no ready clients available")
189+
t.ctx.ReportProgress(0, "Generating transaction...")
190+
191+
tx, err := t.generateTransaction()
192+
if err != nil {
193+
return err
194+
}
195+
196+
if txData, err2 := vars.GeneralizeData(tx); err2 == nil {
197+
t.ctx.Outputs.SetVar("transaction", txData)
198+
} else {
199+
t.logger.Warnf("Failed setting `transaction` output: %v", err2)
200+
}
201+
202+
txBytes, err := tx.MarshalBinary()
203+
if err != nil {
204+
t.logger.Warnf("Failed setting `transactionHex` output: %v", err)
205+
} else {
206+
t.ctx.Outputs.SetVar("transactionHex", hexutil.Encode(txBytes))
206207
}
207208

208209
walletMgr := t.ctx.Scheduler.GetServices().WalletManager()
@@ -224,6 +225,7 @@ func (t *Task) Execute(ctx context.Context) error {
224225
}
225226

226227
if err != nil {
228+
t.wallet.MarkSkippedNonce(tx.Nonce())
227229
return err
228230
}
229231

pkg/tasks/generate_withdrawal_requests/task.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,10 @@ func (t *Task) generateWithdrawal(ctx context.Context, accountIdx uint64, onComp
348348
var clients []*execution.Client
349349

350350
if t.config.ClientPattern == "" && t.config.ExcludeClientPattern == "" {
351-
clients = clientPool.GetExecutionPool().GetReadyEndpoints(true)
351+
clients = clientPool.GetExecutionPool().AwaitReadyEndpoints(ctx, true)
352+
if len(clients) == 0 {
353+
return nil, ctx.Err()
354+
}
352355
} else {
353356
poolClients := clientPool.GetClientsByNamePatterns(t.config.ClientPattern, t.config.ExcludeClientPattern)
354357
if len(poolClients) == 0 {
@@ -361,10 +364,6 @@ func (t *Task) generateWithdrawal(ctx context.Context, accountIdx uint64, onComp
361364
}
362365
}
363366

364-
if len(clients) == 0 {
365-
return nil, fmt.Errorf("no ready clients available")
366-
}
367-
368367
walletMgr := t.ctx.Scheduler.GetServices().WalletManager()
369368

370369
txWallet, err := walletMgr.GetWalletByPrivkey(ctx, t.walletPrivKey)
@@ -421,6 +420,7 @@ func (t *Task) generateWithdrawal(ctx context.Context, accountIdx uint64, onComp
421420
},
422421
})
423422
if err != nil {
423+
txWallet.MarkSkippedNonce(tx.Nonce())
424424
return nil, fmt.Errorf("failed sending withdrawal transaction: %w", err)
425425
}
426426

0 commit comments

Comments
 (0)