Skip to content

Commit f23d506

Browse files
authored
eth/syncer: advance safe and finalized block (#33038)
1 parent eb8f325 commit f23d506

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

eth/syncer/syncer.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"sync"
2323
"time"
2424

25+
"github.com/ethereum/go-ethereum/beacon/params"
2526
"github.com/ethereum/go-ethereum/common"
2627
"github.com/ethereum/go-ethereum/core/types"
2728
"github.com/ethereum/go-ethereum/eth"
@@ -133,13 +134,35 @@ func (s *Syncer) run() {
133134
}
134135

135136
case <-ticker.C:
136-
if target == nil || !s.exitWhenSynced {
137+
if target == nil {
137138
continue
138139
}
139-
if block := s.backend.BlockChain().GetBlockByHash(target.Hash()); block != nil {
140-
log.Info("Sync target reached", "number", block.NumberU64(), "hash", block.Hash())
141-
go s.stack.Close() // async since we need to close ourselves
142-
return
140+
141+
// Terminate the node if the target has been reached
142+
if s.exitWhenSynced {
143+
if block := s.backend.BlockChain().GetBlockByHash(target.Hash()); block != nil {
144+
log.Info("Sync target reached", "number", block.NumberU64(), "hash", block.Hash())
145+
go s.stack.Close() // async since we need to close ourselves
146+
return
147+
}
148+
}
149+
150+
// Set the finalized and safe markers relative to the current head.
151+
// The finalized marker is set two epochs behind the target,
152+
// and the safe marker is set one epoch behind the target.
153+
head := s.backend.BlockChain().CurrentHeader()
154+
if head == nil {
155+
continue
156+
}
157+
if header := s.backend.BlockChain().GetHeaderByNumber(head.Number.Uint64() - params.EpochLength*2); header != nil {
158+
if final := s.backend.BlockChain().CurrentFinalBlock(); final == nil || final.Number.Cmp(header.Number) < 0 {
159+
s.backend.BlockChain().SetFinalized(header)
160+
}
161+
}
162+
if header := s.backend.BlockChain().GetHeaderByNumber(head.Number.Uint64() - params.EpochLength); header != nil {
163+
if safe := s.backend.BlockChain().CurrentSafeBlock(); safe == nil || safe.Number.Cmp(header.Number) < 0 {
164+
s.backend.BlockChain().SetSafe(header)
165+
}
143166
}
144167

145168
case <-s.closed:

0 commit comments

Comments
 (0)