Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(op-node): l1 client chan stuck when closed in ELSync mode #241

Merged
merged 5 commits into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions op-service/sources/l1_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/ethereum/go-ethereum"
Expand Down Expand Up @@ -62,9 +63,12 @@ type L1Client struct {
l1BlockRefsCache *caching.LRUCache[common.Hash, eth.L1BlockRef]

//ensure pre-fetch receipts only once
preFetchReceiptsOnce sync.Once
preFetchReceiptsOnce sync.Once
isPreFetchReceiptsRunning atomic.Bool
//start block for pre-fetch receipts
preFetchReceiptsStartBlockChan chan uint64
preFetchReceiptsClosedChan chan struct{}

//max concurrent requests
maxConcurrentRequests int
//done chan
Expand All @@ -83,6 +87,7 @@ func NewL1Client(client client.RPC, log log.Logger, metrics caching.Metrics, con
l1BlockRefsCache: caching.NewLRUCache[common.Hash, eth.L1BlockRef](metrics, "blockrefs", config.L1BlockRefsCacheSize),
preFetchReceiptsOnce: sync.Once{},
preFetchReceiptsStartBlockChan: make(chan uint64, 1),
preFetchReceiptsClosedChan: make(chan struct{}),
maxConcurrentRequests: config.MaxConcurrentRequests,
done: make(chan struct{}),
}, nil
Expand Down Expand Up @@ -140,13 +145,15 @@ func (s *L1Client) GoOrUpdatePreFetchReceipts(ctx context.Context, l1Start uint6
s.preFetchReceiptsStartBlockChan <- l1Start
s.preFetchReceiptsOnce.Do(func() {
s.log.Info("pre-fetching receipts start", "startBlock", l1Start)
s.isPreFetchReceiptsRunning.Store(true)
go func() {
var currentL1Block uint64
var parentHash common.Hash
for {
select {
case <-s.done:
s.log.Info("pre-fetching receipts done")
s.preFetchReceiptsClosedChan <- struct{}{}
return
case currentL1Block = <-s.preFetchReceiptsStartBlockChan:
s.log.Debug("pre-fetching receipts currentL1Block changed", "block", currentL1Block)
Expand Down Expand Up @@ -259,6 +266,9 @@ func (s *L1Client) ClearReceiptsCacheBefore(blockNumber uint64) {
}

func (s *L1Client) Close() {
s.done <- struct{}{}
if s.isPreFetchReceiptsRunning.Load() {
close(s.done)
<-s.preFetchReceiptsClosedChan
}
s.EthClient.Close()
}
Loading