Skip to content

Commit 45cb1a5

Browse files
authored
eth, les: add new config field SyncFromCheckpoint (#22123)
This PR introduces a new config field SyncFromCheckpoint for light client. In some special scenarios, it's required to start synchronization from some arbitrary checkpoint or even from the scratch. So this PR offers this flexibility to users so that the synchronization start point can be configured. There are two relevant configs: SyncFromCheckpoint and Checkpoint. - If the SyncFromCheckpoint is true, the light client will try to sync from the specified checkpoint. - If the Checkpoint is not configured, then the light client will sync from the scratch(from the latest header if the database is not empty) Additional notes: these two configs are not visible in the CLI flags but only accessable in the config file. Example Usage: [Eth] SyncFromCheckpoint = true [Eth.Checkpoint] SectionIndex = 100 SectionHead = "0xabc" CHTRoot = "0xabc" BloomRoot = "0xabc" PS. Historical checkpoint can be retrieved from the synced full node or light client via les_getCheckpoint API.
1 parent 24c1e30 commit 45cb1a5

7 files changed

Lines changed: 209 additions & 35 deletions

File tree

‎eth/config.go‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,12 @@ type Config struct {
127127
Whitelist map[uint64]common.Hash `toml:"-"`
128128

129129
// Light client options
130-
LightServ int `toml:",omitempty"` // Maximum percentage of time allowed for serving LES requests
131-
LightIngress int `toml:",omitempty"` // Incoming bandwidth limit for light servers
132-
LightEgress int `toml:",omitempty"` // Outgoing bandwidth limit for light servers
133-
LightPeers int `toml:",omitempty"` // Maximum number of LES client peers
134-
LightNoPrune bool `toml:",omitempty"` // Whether to disable light chain pruning
130+
LightServ int `toml:",omitempty"` // Maximum percentage of time allowed for serving LES requests
131+
LightIngress int `toml:",omitempty"` // Incoming bandwidth limit for light servers
132+
LightEgress int `toml:",omitempty"` // Outgoing bandwidth limit for light servers
133+
LightPeers int `toml:",omitempty"` // Maximum number of LES client peers
134+
LightNoPrune bool `toml:",omitempty"` // Whether to disable light chain pruning
135+
SyncFromCheckpoint bool `toml:",omitempty"` // Whether to sync the header chain from the configured checkpoint
135136

136137
// Ultra Light client options
137138
UltraLightServers []string `toml:",omitempty"` // List of trusted ultra light servers

‎eth/gen_config.go‎

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎les/client_handler.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ type clientHandler struct {
4444
downloader *downloader.Downloader
4545
backend *LightEthereum
4646

47-
closeCh chan struct{}
48-
wg sync.WaitGroup // WaitGroup used to track all connected peers.
49-
syncDone func() // Test hooks when syncing is done.
47+
closeCh chan struct{}
48+
wg sync.WaitGroup // WaitGroup used to track all connected peers.
49+
50+
// Hooks used in the testing
51+
syncStart func(header *types.Header) // Hook called when the syncing is started
52+
syncEnd func(header *types.Header) // Hook called when the syncing is done
5053
}
5154

5255
func newClientHandler(ulcServers []string, ulcFraction int, checkpoint *params.TrustedCheckpoint, backend *LightEthereum) *clientHandler {

‎les/commons.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,17 @@ func (c *lesCommons) setupOracle(node *node.Node, genesis common.Hash, ethconfig
157157
config = params.CheckpointOracles[genesis]
158158
}
159159
if config == nil {
160-
log.Info("Checkpoint registrar is not enabled")
160+
log.Info("Checkpoint oracle is not enabled")
161161
return nil
162162
}
163163
if config.Address == (common.Address{}) || uint64(len(config.Signers)) < config.Threshold {
164-
log.Warn("Invalid checkpoint registrar config")
164+
log.Warn("Invalid checkpoint oracle config")
165165
return nil
166166
}
167167
oracle := checkpointoracle.New(config, c.localCheckpoint)
168168
rpcClient, _ := node.Attach()
169169
client := ethclient.NewClient(rpcClient)
170170
oracle.Start(client)
171-
log.Info("Configured checkpoint registrar", "address", config.Address, "signers", len(config.Signers), "threshold", config.Threshold)
171+
log.Info("Configured checkpoint oracle", "address", config.Address, "signers", len(config.Signers), "threshold", config.Threshold)
172172
return oracle
173173
}

‎les/sync.go‎

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/ethereum/go-ethereum/eth/downloader"
2727
"github.com/ethereum/go-ethereum/light"
2828
"github.com/ethereum/go-ethereum/log"
29+
"github.com/ethereum/go-ethereum/params"
2930
)
3031

3132
var errInvalidCheckpoint = errors.New("invalid advertised checkpoint")
@@ -98,30 +99,41 @@ func (h *clientHandler) synchronise(peer *serverPeer) {
9899
if currentTd != nil && peer.Td().Cmp(currentTd) < 0 {
99100
return
100101
}
101-
// Recap the checkpoint.
102-
//
103-
// The light client may be connected to several different versions of the server.
104-
// (1) Old version server which can not provide stable checkpoint in the handshake packet.
105-
// => Use hardcoded checkpoint or empty checkpoint
106-
// (2) New version server but simple checkpoint syncing is not enabled(e.g. mainnet, new testnet or private network)
107-
// => Use hardcoded checkpoint or empty checkpoint
108-
// (3) New version server but the provided stable checkpoint is even lower than the hardcoded one.
109-
// => Use hardcoded checkpoint
102+
// Recap the checkpoint. The light client may be connected to several different
103+
// versions of the server.
104+
// (1) Old version server which can not provide stable checkpoint in the
105+
// handshake packet.
106+
// => Use local checkpoint or empty checkpoint
107+
// (2) New version server but simple checkpoint syncing is not enabled
108+
// (e.g. mainnet, new testnet or private network)
109+
// => Use local checkpoint or empty checkpoint
110+
// (3) New version server but the provided stable checkpoint is even lower
111+
// than the local one.
112+
// => Use local checkpoint
110113
// (4) New version server with valid and higher stable checkpoint
111114
// => Use provided checkpoint
112-
var checkpoint = &peer.checkpoint
113-
var hardcoded bool
115+
var (
116+
local bool
117+
checkpoint = &peer.checkpoint
118+
)
114119
if h.checkpoint != nil && h.checkpoint.SectionIndex >= peer.checkpoint.SectionIndex {
115-
checkpoint = h.checkpoint // Use the hardcoded one.
116-
hardcoded = true
120+
local, checkpoint = true, h.checkpoint
121+
}
122+
// Replace the checkpoint with locally configured one If it's required by
123+
// users. Nil checkpoint means synchronization from the scratch.
124+
if h.backend.config.SyncFromCheckpoint {
125+
local, checkpoint = true, h.backend.config.Checkpoint
126+
if h.backend.config.Checkpoint == nil {
127+
checkpoint = &params.TrustedCheckpoint{}
128+
}
117129
}
118130
// Determine whether we should run checkpoint syncing or normal light syncing.
119131
//
120132
// Here has four situations that we will disable the checkpoint syncing:
121133
//
122134
// 1. The checkpoint is empty
123135
// 2. The latest head block of the local chain is above the checkpoint.
124-
// 3. The checkpoint is hardcoded(recap with local hardcoded checkpoint)
136+
// 3. The checkpoint is local(replaced with local checkpoint)
125137
// 4. For some networks the checkpoint syncing is not activated.
126138
mode := checkpointSync
127139
switch {
@@ -131,7 +143,7 @@ func (h *clientHandler) synchronise(peer *serverPeer) {
131143
case latest.Number.Uint64() >= (checkpoint.SectionIndex+1)*h.backend.iConfig.ChtSize-1:
132144
mode = lightSync
133145
log.Debug("Disable checkpoint syncing", "reason", "local chain beyond the checkpoint")
134-
case hardcoded:
146+
case local:
135147
mode = legacyCheckpointSync
136148
log.Debug("Disable checkpoint syncing", "reason", "checkpoint is hardcoded")
137149
case h.backend.oracle == nil || !h.backend.oracle.IsRunning():
@@ -143,12 +155,14 @@ func (h *clientHandler) synchronise(peer *serverPeer) {
143155
}
144156
log.Debug("Disable checkpoint syncing", "reason", "checkpoint syncing is not activated")
145157
}
158+
146159
// Notify testing framework if syncing has completed(for testing purpose).
147160
defer func() {
148-
if h.syncDone != nil {
149-
h.syncDone()
161+
if h.syncEnd != nil {
162+
h.syncEnd(h.backend.blockchain.CurrentHeader())
150163
}
151164
}()
165+
152166
start := time.Now()
153167
if mode == checkpointSync || mode == legacyCheckpointSync {
154168
// Validate the advertised checkpoint
@@ -177,6 +191,10 @@ func (h *clientHandler) synchronise(peer *serverPeer) {
177191
return
178192
}
179193
}
194+
195+
if h.syncStart != nil {
196+
h.syncStart(h.backend.blockchain.CurrentHeader())
197+
}
180198
// Fetch the remaining block headers based on the current chain header.
181199
if err := h.downloader.Synchronise(peer.id, peer.Head(), peer.Td(), downloader.LightSync); err != nil {
182200
log.Debug("Synchronise failed", "reason", err)

0 commit comments

Comments
 (0)