mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-03-30 14:52:59 +00:00
eth/catalyst: fetch header on forkchoiceUpdated (#31928)
closes https://github.com/ethereum/go-ethereum/issues/31254 --------- Co-authored-by: Gary Rong <garyrong0905@gmail.com>
This commit is contained in:
parent
fa8f391db3
commit
4ea9eea75f
2 changed files with 30 additions and 18 deletions
|
|
@ -283,8 +283,14 @@ func (api *ConsensusAPI) forkchoiceUpdated(update engine.ForkchoiceStateV1, payl
|
||||||
// that should be fixed, not papered over.
|
// that should be fixed, not papered over.
|
||||||
header := api.remoteBlocks.get(update.HeadBlockHash)
|
header := api.remoteBlocks.get(update.HeadBlockHash)
|
||||||
if header == nil {
|
if header == nil {
|
||||||
log.Warn("Forkchoice requested unknown head", "hash", update.HeadBlockHash)
|
log.Warn("Fetching the unknown forkchoice head from network", "hash", update.HeadBlockHash)
|
||||||
return engine.STATUS_SYNCING, nil
|
retrievedHead, err := api.eth.Downloader().GetHeader(update.HeadBlockHash)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn("Could not retrieve unknown head from peers")
|
||||||
|
return engine.STATUS_SYNCING, nil
|
||||||
|
}
|
||||||
|
api.remoteBlocks.put(retrievedHead.Hash(), retrievedHead)
|
||||||
|
header = retrievedHead
|
||||||
}
|
}
|
||||||
// If the finalized hash is known, we can direct the downloader to move
|
// If the finalized hash is known, we can direct the downloader to move
|
||||||
// potentially more data to the freezer from the get go.
|
// potentially more data to the freezer from the get go.
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -48,34 +49,39 @@ func (d *Downloader) BeaconDevSync(mode SyncMode, hash common.Hash, stop chan st
|
||||||
return errors.New("stop requested")
|
return errors.New("stop requested")
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
// Pick a random peer to sync from and keep retrying if none are yet
|
header, err := d.GetHeader(hash)
|
||||||
// available due to fresh startup
|
if err != nil {
|
||||||
d.peers.lock.RLock()
|
|
||||||
var peer *peerConnection
|
|
||||||
for _, peer = range d.peers.peers {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
d.peers.lock.RUnlock()
|
|
||||||
|
|
||||||
if peer == nil {
|
|
||||||
time.Sleep(time.Second)
|
time.Sleep(time.Second)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
return d.BeaconSync(mode, header, header)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetHeader tries to retrieve the header with a given hash from a random peer.
|
||||||
|
func (d *Downloader) GetHeader(hash common.Hash) (*types.Header, error) {
|
||||||
|
// Pick a random peer to sync from and keep retrying if none are yet
|
||||||
|
// available due to fresh startup
|
||||||
|
d.peers.lock.RLock()
|
||||||
|
defer d.peers.lock.RUnlock()
|
||||||
|
|
||||||
|
for _, peer := range d.peers.peers {
|
||||||
|
if peer == nil {
|
||||||
|
return nil, errors.New("could not find peer")
|
||||||
|
}
|
||||||
// Found a peer, attempt to retrieve the header whilst blocking and
|
// Found a peer, attempt to retrieve the header whilst blocking and
|
||||||
// retry if it fails for whatever reason
|
// retry if it fails for whatever reason
|
||||||
log.Info("Attempting to retrieve sync target", "peer", peer.id)
|
log.Debug("Attempting to retrieve sync target", "peer", peer.id, "hash", hash)
|
||||||
headers, metas, err := d.fetchHeadersByHash(peer, hash, 1, 0, false)
|
headers, metas, err := d.fetchHeadersByHash(peer, hash, 1, 0, false)
|
||||||
if err != nil || len(headers) != 1 {
|
if err != nil || len(headers) != 1 {
|
||||||
log.Warn("Failed to fetch sync target", "headers", len(headers), "err", err)
|
|
||||||
time.Sleep(time.Second)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
// Head header retrieved, if the hash matches, start the actual sync
|
// Head header retrieved, if the hash matches, start the actual sync
|
||||||
if metas[0] != hash {
|
if metas[0] != hash {
|
||||||
log.Error("Received invalid sync target", "want", hash, "have", metas[0])
|
log.Warn("Received invalid sync target", "peer", peer.id, "want", hash, "have", metas[0])
|
||||||
time.Sleep(time.Second)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
return d.BeaconSync(mode, headers[0], headers[0])
|
return headers[0], nil
|
||||||
}
|
}
|
||||||
|
return nil, errors.New("failed to fetch sync target")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue