mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-02-26 07:37:20 +00:00
eth/syncer: advance safe and finalized block (#33038)
This commit is contained in:
parent
eb8f32588b
commit
f23d506b7d
1 changed files with 28 additions and 5 deletions
|
|
@ -22,6 +22,7 @@ import (
|
|||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/beacon/params"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/eth"
|
||||
|
|
@ -133,13 +134,35 @@ func (s *Syncer) run() {
|
|||
}
|
||||
|
||||
case <-ticker.C:
|
||||
if target == nil || !s.exitWhenSynced {
|
||||
if target == nil {
|
||||
continue
|
||||
}
|
||||
if block := s.backend.BlockChain().GetBlockByHash(target.Hash()); block != nil {
|
||||
log.Info("Sync target reached", "number", block.NumberU64(), "hash", block.Hash())
|
||||
go s.stack.Close() // async since we need to close ourselves
|
||||
return
|
||||
|
||||
// Terminate the node if the target has been reached
|
||||
if s.exitWhenSynced {
|
||||
if block := s.backend.BlockChain().GetBlockByHash(target.Hash()); block != nil {
|
||||
log.Info("Sync target reached", "number", block.NumberU64(), "hash", block.Hash())
|
||||
go s.stack.Close() // async since we need to close ourselves
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Set the finalized and safe markers relative to the current head.
|
||||
// The finalized marker is set two epochs behind the target,
|
||||
// and the safe marker is set one epoch behind the target.
|
||||
head := s.backend.BlockChain().CurrentHeader()
|
||||
if head == nil {
|
||||
continue
|
||||
}
|
||||
if header := s.backend.BlockChain().GetHeaderByNumber(head.Number.Uint64() - params.EpochLength*2); header != nil {
|
||||
if final := s.backend.BlockChain().CurrentFinalBlock(); final == nil || final.Number.Cmp(header.Number) < 0 {
|
||||
s.backend.BlockChain().SetFinalized(header)
|
||||
}
|
||||
}
|
||||
if header := s.backend.BlockChain().GetHeaderByNumber(head.Number.Uint64() - params.EpochLength); header != nil {
|
||||
if safe := s.backend.BlockChain().CurrentSafeBlock(); safe == nil || safe.Number.Cmp(header.Number) < 0 {
|
||||
s.backend.BlockChain().SetSafe(header)
|
||||
}
|
||||
}
|
||||
|
||||
case <-s.closed:
|
||||
|
|
|
|||
Loading…
Reference in a new issue