eth/gasprice: ensure cache purging goroutine terminates with subscription #31025, close XFN-89 (#1687)

This commit is contained in:
Daniel Liu 2025-11-01 14:35:24 +08:00 committed by GitHub
parent 336d1f9f34
commit 2fd4030cb8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -119,16 +119,23 @@ func NewOracle(backend OracleBackend, params Config, startPrice *big.Int) *Oracl
cache := lru.NewCache[cacheKey, processedFees](2048)
headEvent := make(chan core.ChainHeadEvent, 1)
backend.SubscribeChainHeadEvent(headEvent)
go func() {
var lastHead common.Hash
for ev := range headEvent {
if ev.Block.ParentHash() != lastHead {
cache.Purge()
sub := backend.SubscribeChainHeadEvent(headEvent)
if sub != nil { // the gasprice testBackend doesn't support subscribing to head events
go func() {
var lastHead common.Hash
for {
select {
case ev := <-headEvent:
if ev.Block.ParentHash() != lastHead {
cache.Purge()
}
lastHead = ev.Block.Hash()
case <-sub.Err():
return
}
}
lastHead = ev.Block.Hash()
}
}()
}()
}
return &Oracle{
backend: backend,