5
5
"fmt"
6
6
"strings"
7
7
"sync"
8
+ "sync/atomic"
8
9
"time"
9
10
10
11
"github.com/ethereum/go-ethereum"
@@ -62,9 +63,12 @@ type L1Client struct {
62
63
l1BlockRefsCache * caching.LRUCache [common.Hash , eth.L1BlockRef ]
63
64
64
65
//ensure pre-fetch receipts only once
65
- preFetchReceiptsOnce sync.Once
66
+ preFetchReceiptsOnce sync.Once
67
+ isPreFetchReceiptsRunning atomic.Bool
66
68
//start block for pre-fetch receipts
67
69
preFetchReceiptsStartBlockChan chan uint64
70
+ preFetchReceiptsClosedChan chan struct {}
71
+
68
72
//max concurrent requests
69
73
maxConcurrentRequests int
70
74
//done chan
@@ -83,6 +87,7 @@ func NewL1Client(client client.RPC, log log.Logger, metrics caching.Metrics, con
83
87
l1BlockRefsCache : caching .NewLRUCache [common.Hash , eth.L1BlockRef ](metrics , "blockrefs" , config .L1BlockRefsCacheSize ),
84
88
preFetchReceiptsOnce : sync.Once {},
85
89
preFetchReceiptsStartBlockChan : make (chan uint64 , 1 ),
90
+ preFetchReceiptsClosedChan : make (chan struct {}),
86
91
maxConcurrentRequests : config .MaxConcurrentRequests ,
87
92
done : make (chan struct {}),
88
93
}, nil
@@ -140,13 +145,15 @@ func (s *L1Client) GoOrUpdatePreFetchReceipts(ctx context.Context, l1Start uint6
140
145
s .preFetchReceiptsStartBlockChan <- l1Start
141
146
s .preFetchReceiptsOnce .Do (func () {
142
147
s .log .Info ("pre-fetching receipts start" , "startBlock" , l1Start )
148
+ s .isPreFetchReceiptsRunning .Store (true )
143
149
go func () {
144
150
var currentL1Block uint64
145
151
var parentHash common.Hash
146
152
for {
147
153
select {
148
154
case <- s .done :
149
155
s .log .Info ("pre-fetching receipts done" )
156
+ s .preFetchReceiptsClosedChan <- struct {}{}
150
157
return
151
158
case currentL1Block = <- s .preFetchReceiptsStartBlockChan :
152
159
s .log .Debug ("pre-fetching receipts currentL1Block changed" , "block" , currentL1Block )
@@ -259,6 +266,9 @@ func (s *L1Client) ClearReceiptsCacheBefore(blockNumber uint64) {
259
266
}
260
267
261
268
func (s * L1Client ) Close () {
262
- s .done <- struct {}{}
269
+ if s .isPreFetchReceiptsRunning .Load () {
270
+ close (s .done )
271
+ <- s .preFetchReceiptsClosedChan
272
+ }
263
273
s .EthClient .Close ()
264
274
}
0 commit comments