From cad6a76b3fb8dc686eb97390705de9c2f3435ed3 Mon Sep 17 00:00:00 2001 From: William Hua Date: Wed, 11 Sep 2024 10:43:47 -0400 Subject: [PATCH] ethreceipts: lock when mutating NumBlocksToFinality ReceiptsListener.lazyInit writes to NumBlocksToFinality, we need to lock everywhere we read from it. --- ethreceipts/ethreceipts.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ethreceipts/ethreceipts.go b/ethreceipts/ethreceipts.go index 30abc0f6..98aa5116 100644 --- a/ethreceipts/ethreceipts.go +++ b/ethreceipts/ethreceipts.go @@ -151,6 +151,9 @@ func NewReceiptsListener(log logger.Logger, provider ethrpc.Interface, monitor * } func (l *ReceiptsListener) lazyInit(ctx context.Context) error { + l.mu.Lock() + defer l.mu.Unlock() + if l.options.NumBlocksToFinality <= 0 { chainID, err := getChainID(ctx, l.provider) if err != nil { @@ -776,6 +779,9 @@ func (l *ReceiptsListener) getMaxWaitBlocks(maxWait *int) uint64 { if maxWait == nil { return uint64(l.options.FilterMaxWaitNumBlocks) } else if *maxWait < 0 { + l.mu.RLock() + defer l.mu.RUnlock() + return uint64(l.options.NumBlocksToFinality * 2) } else { return uint64(*maxWait) @@ -788,6 +794,10 @@ func (l *ReceiptsListener) isBlockFinal(blockNum *big.Int) bool { return false } diff := big.NewInt(0).Sub(latestBlockNum, blockNum) + + l.mu.RLock() + defer l.mu.RUnlock() + return diff.Cmp(big.NewInt(int64(l.options.NumBlocksToFinality))) >= 0 }