From 324f90b83aa59db4fa3c23fe0993ae6eb411cd41 Mon Sep 17 00:00:00 2001 From: Ian Shim <100327837+ian-shim@users.noreply.github.com> Date: Mon, 13 Nov 2023 19:56:56 -0800 Subject: [PATCH] [finalizer] Create new ctx for every retry (#18) --- disperser/batcher/finalizer.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/disperser/batcher/finalizer.go b/disperser/batcher/finalizer.go index a85995fce9..61f70b59a7 100644 --- a/disperser/batcher/finalizer.go +++ b/disperser/batcher/finalizer.go @@ -64,9 +64,7 @@ func (f *finalizer) Start(ctx context.Context) { // block number is less than or equal to the latest finalized block number. // If it failes to process some blobs, it will log the error, skip the failed blobs, and will not return an error. The function should be invoked again to retry. func (f *finalizer) FinalizeBlobs(ctx context.Context) error { - ctxWithTimeout, cancel := context.WithTimeout(ctx, f.timeout) - defer cancel() - finalizedHeader, err := f.getLatestFinalizedBlock(ctxWithTimeout) + finalizedHeader, err := f.getLatestFinalizedBlock(ctx) if err != nil { return fmt.Errorf("FinalizeBlobs: error getting latest finalized block: %w", err) } @@ -92,7 +90,7 @@ func (f *finalizer) FinalizeBlobs(ctx context.Context) error { } // confirmation block number may have changed due to reorg - confirmationBlockNumber, err := f.getTransactionBlockNumber(ctxWithTimeout, confirmationMetadata.ConfirmationInfo.ConfirmationTxnHash) + confirmationBlockNumber, err := f.getTransactionBlockNumber(ctx, confirmationMetadata.ConfirmationInfo.ConfirmationTxnHash) if err != nil { f.logger.Error("FinalizeBlobs: error getting transaction block number", "err", err) continue @@ -115,10 +113,14 @@ func (f *finalizer) FinalizeBlobs(ctx context.Context) error { } func (f *finalizer) getTransactionBlockNumber(ctx context.Context, hash gcommon.Hash) (uint64, error) { + var ctxWithTimeout context.Context + var cancel context.CancelFunc var txReceipt *types.Receipt var err error for i := 0; i < maxRetries; i++ { - txReceipt, err = f.ethClient.TransactionReceipt(ctx, hash) + ctxWithTimeout, cancel = context.WithTimeout(ctx, f.timeout) + defer cancel() + txReceipt, err = f.ethClient.TransactionReceipt(ctxWithTimeout, hash) if err == nil { break } @@ -136,10 +138,14 @@ func (f *finalizer) getTransactionBlockNumber(ctx context.Context, hash gcommon. } func (f *finalizer) getLatestFinalizedBlock(ctx context.Context) (*types.Header, error) { + var ctxWithTimeout context.Context + var cancel context.CancelFunc var header = types.Header{} var err error for i := 0; i < maxRetries; i++ { - err := f.rpcClient.CallContext(ctx, &header, "eth_getBlockByNumber", "finalized", false) + ctxWithTimeout, cancel = context.WithTimeout(ctx, f.timeout) + defer cancel() + err := f.rpcClient.CallContext(ctxWithTimeout, &header, "eth_getBlockByNumber", "finalized", false) if err == nil { break }