Skip to content

Commit 4afab7e

Browse files
authored
eth/downloader: move SyncMode to package eth/ethconfig (#30847)
Lots of packages depend on eth/downloader just for the SyncMode type. Since we have a dedicated package for eth protocol configuration, it makes more sense to define SyncMode there, turning eth/downloader into more of a leaf package.
1 parent ae5a16f commit 4afab7e

File tree

17 files changed

+64
-55
lines changed

17 files changed

+64
-55
lines changed

cmd/utils/flags.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ import (
5050
"github.com/ethereum/go-ethereum/crypto/kzg4844"
5151
"github.com/ethereum/go-ethereum/eth"
5252
"github.com/ethereum/go-ethereum/eth/catalyst"
53-
"github.com/ethereum/go-ethereum/eth/downloader"
5453
"github.com/ethereum/go-ethereum/eth/ethconfig"
5554
"github.com/ethereum/go-ethereum/eth/filters"
5655
"github.com/ethereum/go-ethereum/eth/gasprice"
@@ -1606,7 +1605,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16061605
godebug.SetGCPercent(int(gogc))
16071606

16081607
if ctx.IsSet(SyncTargetFlag.Name) {
1609-
cfg.SyncMode = downloader.FullSync // dev sync target forces full sync
1608+
cfg.SyncMode = ethconfig.FullSync // dev sync target forces full sync
16101609
} else if ctx.IsSet(SyncModeFlag.Name) {
16111610
if err = cfg.SyncMode.UnmarshalText([]byte(ctx.String(SyncModeFlag.Name))); err != nil {
16121611
Fatalf("invalid --syncmode flag: %v", err)
@@ -1677,7 +1676,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
16771676
}
16781677
if !ctx.Bool(SnapshotFlag.Name) || cfg.SnapshotCache == 0 {
16791678
// If snap-sync is requested, this flag is also required
1680-
if cfg.SyncMode == downloader.SnapSync {
1679+
if cfg.SyncMode == ethconfig.SnapSync {
16811680
if !ctx.Bool(SnapshotFlag.Name) {
16821681
log.Warn("Snap sync requested, enabling --snapshot")
16831682
}
@@ -1743,7 +1742,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
17431742
if !ctx.IsSet(NetworkIdFlag.Name) {
17441743
cfg.NetworkId = 1337
17451744
}
1746-
cfg.SyncMode = downloader.FullSync
1745+
cfg.SyncMode = ethconfig.FullSync
17471746
// Create new developer account or reuse existing one
17481747
var (
17491748
developer accounts.Account

eth/backend.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -424,26 +424,26 @@ func (s *Ethereum) Stop() error {
424424

425425
// SyncMode retrieves the current sync mode, either explicitly set, or derived
426426
// from the chain status.
427-
func (s *Ethereum) SyncMode() downloader.SyncMode {
427+
func (s *Ethereum) SyncMode() ethconfig.SyncMode {
428428
// If we're in snap sync mode, return that directly
429429
if s.handler.snapSync.Load() {
430-
return downloader.SnapSync
430+
return ethconfig.SnapSync
431431
}
432432
// We are probably in full sync, but we might have rewound to before the
433433
// snap sync pivot, check if we should re-enable snap sync.
434434
head := s.blockchain.CurrentBlock()
435435
if pivot := rawdb.ReadLastPivotNumber(s.chainDb); pivot != nil {
436436
if head.Number.Uint64() < *pivot {
437-
return downloader.SnapSync
437+
return ethconfig.SnapSync
438438
}
439439
}
440440
// We are in a full sync, but the associated head state is missing. To complete
441441
// the head state, forcefully rerun the snap sync. Note it doesn't mean the
442442
// persistent state is corrupted, just mismatch with the head block.
443443
if !s.blockchain.HasState(head.Root) {
444444
log.Info("Reenabled snap sync as chain is stateless")
445-
return downloader.SnapSync
445+
return ethconfig.SnapSync
446446
}
447447
// Nope, we're really full syncing
448-
return downloader.FullSync
448+
return ethconfig.FullSync
449449
}

eth/catalyst/api.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
"github.com/ethereum/go-ethereum/core/types"
3434
"github.com/ethereum/go-ethereum/core/vm"
3535
"github.com/ethereum/go-ethereum/eth"
36-
"github.com/ethereum/go-ethereum/eth/downloader"
36+
"github.com/ethereum/go-ethereum/eth/ethconfig"
3737
"github.com/ethereum/go-ethereum/internal/version"
3838
"github.com/ethereum/go-ethereum/log"
3939
"github.com/ethereum/go-ethereum/miner"
@@ -918,7 +918,7 @@ func (api *ConsensusAPI) newPayload(params engine.ExecutableData, versionedHashe
918918
// tries to make it import a block. That should be denied as pushing something
919919
// into the database directly will conflict with the assumptions of snap sync
920920
// that it has an empty db that it can fill itself.
921-
if api.eth.SyncMode() != downloader.FullSync {
921+
if api.eth.SyncMode() != ethconfig.FullSync {
922922
return api.delayPayloadImport(block), nil
923923
}
924924
if !api.eth.BlockChain().HasBlockAndState(block.ParentHash(), block.NumberU64()-1) {
@@ -1031,7 +1031,7 @@ func (api *ConsensusAPI) delayPayloadImport(block *types.Block) engine.PayloadSt
10311031
// payload as non-integratable on top of the existing sync. We'll just
10321032
// have to rely on the beacon client to forcefully update the head with
10331033
// a forkchoice update request.
1034-
if api.eth.SyncMode() == downloader.FullSync {
1034+
if api.eth.SyncMode() == ethconfig.FullSync {
10351035
// In full sync mode, failure to import a well-formed block can only mean
10361036
// that the parent state is missing and the syncer rejected extending the
10371037
// current cycle with the new payload.

eth/catalyst/api_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ import (
4040
"github.com/ethereum/go-ethereum/crypto"
4141
"github.com/ethereum/go-ethereum/crypto/kzg4844"
4242
"github.com/ethereum/go-ethereum/eth"
43-
"github.com/ethereum/go-ethereum/eth/downloader"
4443
"github.com/ethereum/go-ethereum/eth/ethconfig"
4544
"github.com/ethereum/go-ethereum/internal/version"
4645
"github.com/ethereum/go-ethereum/miner"
@@ -452,7 +451,7 @@ func startEthService(t *testing.T, genesis *core.Genesis, blocks []*types.Block)
452451
}
453452

454453
mcfg := miner.DefaultConfig
455-
ethcfg := &ethconfig.Config{Genesis: genesis, SyncMode: downloader.FullSync, TrieTimeout: time.Minute, TrieDirtyCache: 256, TrieCleanCache: 256, Miner: mcfg}
454+
ethcfg := &ethconfig.Config{Genesis: genesis, SyncMode: ethconfig.FullSync, TrieTimeout: time.Minute, TrieDirtyCache: 256, TrieCleanCache: 256, Miner: mcfg}
456455
ethservice, err := eth.New(n, ethcfg)
457456
if err != nil {
458457
t.Fatal("can't create eth service:", err)

eth/catalyst/simulated_beacon_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/ethereum/go-ethereum/core/types"
2828
"github.com/ethereum/go-ethereum/crypto"
2929
"github.com/ethereum/go-ethereum/eth"
30-
"github.com/ethereum/go-ethereum/eth/downloader"
3130
"github.com/ethereum/go-ethereum/eth/ethconfig"
3231
"github.com/ethereum/go-ethereum/miner"
3332
"github.com/ethereum/go-ethereum/node"
@@ -49,7 +48,7 @@ func startSimulatedBeaconEthService(t *testing.T, genesis *core.Genesis, period
4948
t.Fatal("can't create node:", err)
5049
}
5150

52-
ethcfg := &ethconfig.Config{Genesis: genesis, SyncMode: downloader.FullSync, TrieTimeout: time.Minute, TrieDirtyCache: 256, TrieCleanCache: 256, Miner: miner.DefaultConfig}
51+
ethcfg := &ethconfig.Config{Genesis: genesis, SyncMode: ethconfig.FullSync, TrieTimeout: time.Minute, TrieDirtyCache: 256, TrieCleanCache: 256, Miner: miner.DefaultConfig}
5352
ethservice, err := eth.New(n, ethcfg)
5453
if err != nil {
5554
t.Fatal("can't create eth service:", err)

eth/catalyst/tester.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
"github.com/ethereum/go-ethereum/common"
2424
"github.com/ethereum/go-ethereum/eth"
25-
"github.com/ethereum/go-ethereum/eth/downloader"
25+
"github.com/ethereum/go-ethereum/eth/ethconfig"
2626
"github.com/ethereum/go-ethereum/log"
2727
"github.com/ethereum/go-ethereum/node"
2828
)
@@ -62,7 +62,7 @@ func (tester *FullSyncTester) Start() error {
6262

6363
// Trigger beacon sync with the provided block hash as trusted
6464
// chain head.
65-
err := tester.backend.Downloader().BeaconDevSync(downloader.FullSync, tester.target, tester.closed)
65+
err := tester.backend.Downloader().BeaconDevSync(ethconfig.FullSync, tester.target, tester.closed)
6666
if err != nil {
6767
log.Info("Failed to trigger beacon sync", "err", err)
6868
}

eth/downloader/beaconsync.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/ethereum/go-ethereum/common"
2525
"github.com/ethereum/go-ethereum/core/rawdb"
2626
"github.com/ethereum/go-ethereum/core/types"
27+
"github.com/ethereum/go-ethereum/eth/ethconfig"
2728
"github.com/ethereum/go-ethereum/log"
2829
)
2930

@@ -198,9 +199,9 @@ func (d *Downloader) findBeaconAncestor() (uint64, error) {
198199
var chainHead *types.Header
199200

200201
switch d.getMode() {
201-
case FullSync:
202+
case ethconfig.FullSync:
202203
chainHead = d.blockchain.CurrentBlock()
203-
case SnapSync:
204+
case ethconfig.SnapSync:
204205
chainHead = d.blockchain.CurrentSnapBlock()
205206
default:
206207
panic("unknown sync mode")
@@ -218,9 +219,9 @@ func (d *Downloader) findBeaconAncestor() (uint64, error) {
218219
}
219220
var linked bool
220221
switch d.getMode() {
221-
case FullSync:
222+
case ethconfig.FullSync:
222223
linked = d.blockchain.HasBlock(beaconTail.ParentHash, beaconTail.Number.Uint64()-1)
223-
case SnapSync:
224+
case ethconfig.SnapSync:
224225
linked = d.blockchain.HasFastBlock(beaconTail.ParentHash, beaconTail.Number.Uint64()-1)
225226
default:
226227
panic("unknown sync mode")
@@ -253,9 +254,9 @@ func (d *Downloader) findBeaconAncestor() (uint64, error) {
253254

254255
var known bool
255256
switch d.getMode() {
256-
case FullSync:
257+
case ethconfig.FullSync:
257258
known = d.blockchain.HasBlock(h.Hash(), n)
258-
case SnapSync:
259+
case ethconfig.SnapSync:
259260
known = d.blockchain.HasFastBlock(h.Hash(), n)
260261
default:
261262
panic("unknown sync mode")

eth/downloader/downloader.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/ethereum/go-ethereum/core/rawdb"
3131
"github.com/ethereum/go-ethereum/core/state/snapshot"
3232
"github.com/ethereum/go-ethereum/core/types"
33+
"github.com/ethereum/go-ethereum/eth/ethconfig"
3334
"github.com/ethereum/go-ethereum/eth/protocols/snap"
3435
"github.com/ethereum/go-ethereum/ethdb"
3536
"github.com/ethereum/go-ethereum/event"
@@ -69,6 +70,17 @@ var (
6970
errNoPivotHeader = errors.New("pivot header is not found")
7071
)
7172

73+
// SyncMode defines the sync method of the downloader.
74+
// Deprecated: use ethconfig.SyncMode instead
75+
type SyncMode = ethconfig.SyncMode
76+
77+
const (
78+
// Deprecated: use ethconfig.FullSync
79+
FullSync = ethconfig.FullSync
80+
// Deprecated: use ethconfig.SnapSync
81+
SnapSync = ethconfig.SnapSync
82+
)
83+
7284
// peerDropFn is a callback type for dropping a peer detected as malicious.
7385
type peerDropFn func(id string)
7486

@@ -230,9 +242,9 @@ func (d *Downloader) Progress() ethereum.SyncProgress {
230242
current := uint64(0)
231243
mode := d.getMode()
232244
switch mode {
233-
case FullSync:
245+
case ethconfig.FullSync:
234246
current = d.blockchain.CurrentBlock().Number.Uint64()
235-
case SnapSync:
247+
case ethconfig.SnapSync:
236248
current = d.blockchain.CurrentSnapBlock().Number.Uint64()
237249
default:
238250
log.Error("Unknown downloader mode", "mode", mode)
@@ -326,7 +338,7 @@ func (d *Downloader) synchronise(mode SyncMode, beaconPing chan struct{}) error
326338
if d.notified.CompareAndSwap(false, true) {
327339
log.Info("Block synchronisation started")
328340
}
329-
if mode == SnapSync {
341+
if mode == ethconfig.SnapSync {
330342
// Snap sync will directly modify the persistent state, making the entire
331343
// trie database unusable until the state is fully synced. To prevent any
332344
// subsequent state reads, explicitly disable the trie database and state
@@ -434,7 +446,7 @@ func (d *Downloader) syncToHead() (err error) {
434446
// threshold (i.e. new chain). In that case we won't really snap sync
435447
// anyway, but still need a valid pivot block to avoid some code hitting
436448
// nil panics on access.
437-
if mode == SnapSync && pivot == nil {
449+
if mode == ethconfig.SnapSync && pivot == nil {
438450
pivot = d.blockchain.CurrentBlock()
439451
}
440452
height := latest.Number.Uint64()
@@ -452,7 +464,7 @@ func (d *Downloader) syncToHead() (err error) {
452464
d.syncStatsLock.Unlock()
453465

454466
// Ensure our origin point is below any snap sync pivot point
455-
if mode == SnapSync {
467+
if mode == ethconfig.SnapSync {
456468
if height <= uint64(fsMinFullBlocks) {
457469
origin = 0
458470
} else {
@@ -466,10 +478,10 @@ func (d *Downloader) syncToHead() (err error) {
466478
}
467479
}
468480
d.committed.Store(true)
469-
if mode == SnapSync && pivot.Number.Uint64() != 0 {
481+
if mode == ethconfig.SnapSync && pivot.Number.Uint64() != 0 {
470482
d.committed.Store(false)
471483
}
472-
if mode == SnapSync {
484+
if mode == ethconfig.SnapSync {
473485
// Set the ancient data limitation. If we are running snap sync, all block
474486
// data older than ancientLimit will be written to the ancient store. More
475487
// recent data will be written to the active database and will wait for the
@@ -523,13 +535,13 @@ func (d *Downloader) syncToHead() (err error) {
523535
func() error { return d.fetchReceipts(origin + 1) }, // Receipts are retrieved during snap sync
524536
func() error { return d.processHeaders(origin + 1) },
525537
}
526-
if mode == SnapSync {
538+
if mode == ethconfig.SnapSync {
527539
d.pivotLock.Lock()
528540
d.pivotHeader = pivot
529541
d.pivotLock.Unlock()
530542

531543
fetchers = append(fetchers, func() error { return d.processSnapSyncContent() })
532-
} else if mode == FullSync {
544+
} else if mode == ethconfig.FullSync {
533545
fetchers = append(fetchers, func() error { return d.processFullSyncContent() })
534546
}
535547
return d.spawnSync(fetchers)
@@ -676,7 +688,7 @@ func (d *Downloader) processHeaders(origin uint64) error {
676688
chunkHashes := hashes[:limit]
677689

678690
// In case of header only syncing, validate the chunk immediately
679-
if mode == SnapSync {
691+
if mode == ethconfig.SnapSync {
680692
// Although the received headers might be all valid, a legacy
681693
// PoW/PoA sync must not accept post-merge headers. Make sure
682694
// that any transition is rejected at this point.

eth/downloader/queue.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"github.com/ethereum/go-ethereum/common/prque"
3131
"github.com/ethereum/go-ethereum/core/types"
3232
"github.com/ethereum/go-ethereum/crypto/kzg4844"
33+
"github.com/ethereum/go-ethereum/eth/ethconfig"
3334
"github.com/ethereum/go-ethereum/log"
3435
"github.com/ethereum/go-ethereum/metrics"
3536
"github.com/ethereum/go-ethereum/params"
@@ -180,7 +181,7 @@ func (q *queue) Reset(blockCacheLimit int, thresholdInitialSize int) {
180181
defer q.lock.Unlock()
181182

182183
q.closed = false
183-
q.mode = FullSync
184+
q.mode = ethconfig.FullSync
184185

185186
q.headerHead = common.Hash{}
186187
q.headerPendPool = make(map[string]*fetchRequest)
@@ -328,7 +329,7 @@ func (q *queue) Schedule(headers []*types.Header, hashes []common.Hash, from uin
328329
q.blockTaskQueue.Push(header, -int64(header.Number.Uint64()))
329330
}
330331
// Queue for receipt retrieval
331-
if q.mode == SnapSync && !header.EmptyReceipts() {
332+
if q.mode == ethconfig.SnapSync && !header.EmptyReceipts() {
332333
if _, ok := q.receiptTaskPool[hash]; ok {
333334
log.Warn("Header already scheduled for receipt fetch", "number", header.Number, "hash", hash)
334335
} else {
@@ -523,7 +524,7 @@ func (q *queue) reserveHeaders(p *peerConnection, count int, taskPool map[common
523524
// we can ask the resultcache if this header is within the
524525
// "prioritized" segment of blocks. If it is not, we need to throttle
525526

526-
stale, throttle, item, err := q.resultCache.AddFetch(header, q.mode == SnapSync)
527+
stale, throttle, item, err := q.resultCache.AddFetch(header, q.mode == ethconfig.SnapSync)
527528
if stale {
528529
// Don't put back in the task queue, this item has already been
529530
// delivered upstream

eth/ethconfig/config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/ethereum/go-ethereum/core"
3030
"github.com/ethereum/go-ethereum/core/txpool/blobpool"
3131
"github.com/ethereum/go-ethereum/core/txpool/legacypool"
32-
"github.com/ethereum/go-ethereum/eth/downloader"
3332
"github.com/ethereum/go-ethereum/eth/gasprice"
3433
"github.com/ethereum/go-ethereum/ethdb"
3534
"github.com/ethereum/go-ethereum/log"
@@ -49,7 +48,7 @@ var FullNodeGPO = gasprice.Config{
4948

5049
// Defaults contains default settings for use on the Ethereum main net.
5150
var Defaults = Config{
52-
SyncMode: downloader.SnapSync,
51+
SyncMode: SnapSync,
5352
NetworkId: 0, // enable auto configuration of networkID == chainID
5453
TxLookupLimit: 2350000,
5554
TransactionHistory: 2350000,
@@ -80,7 +79,7 @@ type Config struct {
8079
// Network ID separates blockchains on the peer-to-peer networking level. When left
8180
// zero, the chain ID is used as network ID.
8281
NetworkId uint64
83-
SyncMode downloader.SyncMode
82+
SyncMode SyncMode
8483

8584
// This can be set to list of enrtree:// URLs which will be queried for
8685
// nodes to connect to.

0 commit comments

Comments
 (0)