Skip to content

Commit 407f946

Browse files
committed
feat: cleanup code
1 parent 414d48c commit 407f946

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

oracle/pkg/updater/updater.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ func (u *Updater) addSettlement(
458458

459459
return nil
460460
}
461+
461462
func (u *Updater) getL1Txns(ctx context.Context, blockNum uint64) (map[string]TxMetadata, error) {
462463
txns, ok := u.l1BlockCache.Get(blockNum)
463464
if ok {
@@ -467,30 +468,43 @@ func (u *Updater) getL1Txns(ctx context.Context, blockNum uint64) (map[string]Tx
467468

468469
u.metrics.BlockTxnCacheMisses.Inc()
469470

470-
blk, err := u.l1Client.BlockByNumber(ctx, big.NewInt(0).SetUint64(blockNum))
471+
block, err := u.l1Client.BlockByNumber(ctx, big.NewInt(0).SetUint64(blockNum))
471472
if err != nil {
472473
return nil, fmt.Errorf("failed to get block by number: %w", err)
473474
}
474475

475476
txnsInBlock := make(map[string]TxMetadata)
476477
var wg sync.WaitGroup
477478
var mu sync.Mutex
478-
for posInBlock, tx := range blk.Transactions() {
479-
wg.Add(1)
480-
go func(posInBlock int, tx *types.Transaction) {
481-
defer wg.Done()
482-
receipt, err := u.l1Client.TransactionReceipt(ctx, tx.Hash())
483-
if err != nil {
484-
u.logger.Error("failed to get transaction receipt", "txHash", tx.Hash().Hex(), "error", err)
485-
return
486-
}
487-
txSucceeded := receipt.Status == 1
479+
var receiptErr error
480+
481+
processTransactionMetadata := func(posInBlock int, tx *types.Transaction) {
482+
defer wg.Done()
483+
receipt, err := u.l1Client.TransactionReceipt(ctx, tx.Hash())
484+
if err != nil {
485+
u.logger.Error("failed to get transaction receipt", "txHash", tx.Hash().Hex(), "error", err)
488486
mu.Lock()
489-
txnsInBlock[strings.TrimPrefix(tx.Hash().Hex(), "0x")] = TxMetadata{PosInBlock: posInBlock, Succeeded: txSucceeded}
487+
receiptErr = err
490488
mu.Unlock()
491-
}(posInBlock, tx)
489+
return
490+
}
491+
txSucceeded := receipt.Status == 1
492+
mu.Lock()
493+
txnsInBlock[strings.TrimPrefix(tx.Hash().Hex(), "0x")] = TxMetadata{PosInBlock: posInBlock, Succeeded: txSucceeded}
494+
mu.Unlock()
495+
}
496+
497+
for posInBlock, tx := range block.Transactions() {
498+
wg.Add(1)
499+
go processTransactionMetadata(posInBlock, tx)
492500
}
501+
493502
wg.Wait()
503+
504+
if receiptErr != nil {
505+
return nil, receiptErr
506+
}
507+
494508
_ = u.l1BlockCache.Add(blockNum, txnsInBlock)
495509

496510
return txnsInBlock, nil

0 commit comments

Comments
 (0)