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 benjamin202410
parent bbaba9f6af
commit 072f2dc7e2

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,