Skip to content

Commit 85d7012

Browse files
committed
eth/downloader: use full sync mode for devsync
1 parent 5a94d36 commit 85d7012

File tree

4 files changed

+45
-32
lines changed

4 files changed

+45
-32
lines changed

eth/downloader/beacondevsync.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"github.com/ethereum/go-ethereum/common"
2323
"github.com/ethereum/go-ethereum/core/types"
24+
"github.com/ethereum/go-ethereum/eth/ethconfig"
2425
"github.com/ethereum/go-ethereum/log"
2526
)
2627

@@ -34,6 +35,9 @@ import (
3435
// to use this instead of giving us the payload first, then essentially nobody
3536
// in the network would have the block yet that we'd attempt to retrieve.
3637
func (d *Downloader) BeaconDevSync(header *types.Header) error {
38+
if !d.moder.setMode(ethconfig.FullSync) {
39+
return errors.New("full sync is not allowed")
40+
}
3741
// Be very loud that this code should not be used in a live node
3842
log.Warn("----------------------------------")
3943
log.Warn("Beacon syncing with hash as target", "number", header.Number, "hash", header.Hash())

eth/downloader/downloader.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,7 @@ func (d *Downloader) synchronise(beaconPing chan struct{}) (err error) {
364364
mode := d.moder.getMode()
365365
defer func() {
366366
if err == nil && mode == ethconfig.SnapSync {
367-
d.moder.disableSnap()
368-
log.Info("Disabled the snap sync after the initial sync cycle")
367+
d.moder.setMode(ethconfig.FullSync)
369368
}
370369
}()
371370
if mode == ethconfig.SnapSync {

eth/downloader/syncmode.go

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,24 @@ type moder struct {
3636
}
3737

3838
func newSyncModer(mode ethconfig.SyncMode, chain BlockChain, disk ethdb.KeyValueReader) *moder {
39+
m := &moder{
40+
chain: chain,
41+
disk: disk,
42+
}
43+
valid, reason, suggest := m.validate(mode)
44+
if !valid {
45+
m.mode = suggest
46+
log.Info("Switched sync mode", "want", mode, "use", suggest, "reason", reason)
47+
} else {
48+
m.mode = mode
49+
log.Info("Initialized sync mode", "mode", mode)
50+
}
51+
return m
52+
}
53+
54+
// validate checks if the specified sync mode is compatible with local chain and
55+
// returns the suggested mode if it's not along with the reason.
56+
func (m *moder) validate(mode ethconfig.SyncMode) (bool, string, ethconfig.SyncMode) {
3957
if mode == ethconfig.FullSync {
4058
// The database seems empty as the current block is the genesis. Yet the snap
4159
// block is ahead, so snap sync was enabled for this node at a certain point.
@@ -45,32 +63,24 @@ func newSyncModer(mode ethconfig.SyncMode, chain BlockChain, disk ethdb.KeyValue
4563
// * the last snap sync is not finished while user specifies a full sync this
4664
// time. But we don't have any recent state for full sync.
4765
// In these cases however it's safe to reenable snap sync.
48-
fullBlock, snapBlock := chain.CurrentBlock(), chain.CurrentSnapBlock()
66+
fullBlock, snapBlock := m.chain.CurrentBlock(), m.chain.CurrentSnapBlock()
4967
if fullBlock.Number.Uint64() == 0 && snapBlock.Number.Uint64() > 0 {
50-
mode = ethconfig.SnapSync
51-
log.Warn("Switch sync mode from full sync to snap sync", "reason", "snap sync incomplete")
52-
} else if !chain.HasState(fullBlock.Root) {
53-
mode = ethconfig.SnapSync
54-
log.Warn("Switch sync mode from full sync to snap sync", "reason", "head state missing")
68+
return false, "snap sync incomplete", ethconfig.SnapSync
69+
} else if !m.chain.HasState(fullBlock.Root) {
70+
return false, "head state missing", ethconfig.SnapSync
5571
} else {
56-
// Grant the full sync mode
57-
log.Info("Enabled full sync", "head", fullBlock.Number, "hash", fullBlock.Hash())
72+
// If full sync was requested and our chain is stateful, grant it
73+
return true, "", ethconfig.FullSync
5874
}
5975
} else {
60-
head := chain.CurrentBlock()
61-
if head.Number.Uint64() > 0 && chain.HasState(head.Root) {
62-
mode = ethconfig.FullSync
63-
log.Info("Switch sync mode from snap sync to full sync", "reason", "snap sync complete")
76+
head := m.chain.CurrentBlock()
77+
if head.Number.Uint64() > 0 && m.chain.HasState(head.Root) {
78+
return false, "snap sync complete", ethconfig.FullSync
6479
} else {
6580
// If snap sync was requested and our database is empty, grant it
66-
log.Info("Enabled snap sync", "head", head.Number, "hash", head.Hash())
81+
return true, "", ethconfig.SnapSync
6782
}
6883
}
69-
return &moder{
70-
mode: mode,
71-
chain: chain,
72-
disk: disk,
73-
}
7484
}
7585

7686
// getMode retrieves the current sync mode, either explicitly set, or derived
@@ -103,14 +113,19 @@ func (m *moder) getMode() ethconfig.SyncMode {
103113
return ethconfig.FullSync
104114
}
105115

106-
// disableSnap disables the snap sync mode, usually it's called after a successful
107-
// snap sync.
108-
func (m *moder) disableSnap() {
116+
// setMode sets the sync mode with the specified one.
117+
func (m *moder) setMode(mode ethconfig.SyncMode) bool {
109118
m.lock.Lock()
110119
defer m.lock.Unlock()
111120

112-
if m.mode == ethconfig.FullSync {
113-
return
121+
if m.mode == mode {
122+
return true
123+
}
124+
if valid, _, _ := m.validate(mode); !valid {
125+
return false
114126
}
115-
m.mode = ethconfig.FullSync
127+
old := m.mode
128+
m.mode = mode
129+
log.Info("Switched sync mode", "old", old, "new", mode)
130+
return true
116131
}

eth/syncer/syncer.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"github.com/ethereum/go-ethereum/common"
2626
"github.com/ethereum/go-ethereum/core/types"
2727
"github.com/ethereum/go-ethereum/eth"
28-
"github.com/ethereum/go-ethereum/eth/ethconfig"
2928
"github.com/ethereum/go-ethereum/log"
3029
"github.com/ethereum/go-ethereum/node"
3130
"github.com/ethereum/go-ethereum/rpc"
@@ -129,11 +128,7 @@ func (s *Syncer) run() {
129128
break
130129
}
131130
if resync {
132-
if mode := s.backend.Downloader().GetSyncMode(); mode != ethconfig.FullSync {
133-
req.errc <- fmt.Errorf("unsupported syncmode %v", mode)
134-
} else {
135-
req.errc <- s.backend.Downloader().BeaconDevSync(target)
136-
}
131+
req.errc <- s.backend.Downloader().BeaconDevSync(target)
137132
}
138133

139134
case <-ticker.C:

0 commit comments

Comments
 (0)