Skip to content

Commit

Permalink
refactor FastSync disable impl
Browse files Browse the repository at this point in the history
  • Loading branch information
zongyuan committed May 31, 2019
1 parent e702369 commit 4f3f852
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
6 changes: 0 additions & 6 deletions cmd/efsn/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"github.com/FusionFoundation/efsn/cmd/utils"
"github.com/FusionFoundation/efsn/dashboard"
"github.com/FusionFoundation/efsn/eth"
"github.com/FusionFoundation/efsn/eth/downloader"
"github.com/FusionFoundation/efsn/node"
"github.com/FusionFoundation/efsn/params"
whisper "github.com/FusionFoundation/efsn/whisper/whisperv6"
Expand Down Expand Up @@ -138,11 +137,6 @@ func makeConfigNode(ctx *cli.Context) (*node.Node, gethConfig) {
utils.SetShhConfig(ctx, stack, &cfg.Shh)
utils.SetDashboardConfig(ctx, &cfg.Dashboard)

// Disable fast sync mode
if cfg.Eth.SyncMode == downloader.FastSync {
cfg.Eth.SyncMode = downloader.FullSync
}

// set to archival mode by default
// necessary to get ticket state back
cfg.Eth.NoPruning = true
Expand Down
5 changes: 5 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,10 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {

if ctx.GlobalIsSet(SyncModeFlag.Name) {
cfg.SyncMode = *GlobalTextMarshaler(ctx, SyncModeFlag.Name).(*downloader.SyncMode)
if cfg.SyncMode == downloader.FastSync && downloader.FastSyncSupported() == false {
log.Warn("SetEthConfig: 'fast' sync mode is not supported, change to 'full' sync mode.")
cfg.SyncMode = downloader.FullSync
}
}
if ctx.GlobalIsSet(LightServFlag.Name) {
cfg.LightServ = ctx.GlobalInt(LightServFlag.Name)
Expand Down Expand Up @@ -1251,6 +1255,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
if gen := ctx.GlobalInt(TrieCacheGenFlag.Name); gen > 0 {
state.MaxTrieCacheGen = uint16(gen)
}
log.Info("SetEthConfig finished", "NetworkId", cfg.NetworkId, "SyncMode", cfg.SyncMode, "NoPruning", cfg.NoPruning)
}

// SetDashboardConfig applies dashboard related command line flags to the config.
Expand Down
2 changes: 1 addition & 1 deletion eth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (

// DefaultConfig contains default settings for use on the Ethereum main net.
var DefaultConfig = Config{
SyncMode: downloader.FastSync,
SyncMode: downloader.DefaultSyncMode(),
Ethash: ethash.Config{
CacheDir: "ethash",
CachesInMem: 2,
Expand Down
11 changes: 11 additions & 0 deletions eth/downloader/modes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ const (
LightSync // Download only the headers and terminate afterwards
)

func FastSyncSupported() bool {
return false
}

func DefaultSyncMode() SyncMode {
if FastSyncSupported() {
return FastSync
}
return FullSync
}

func (mode SyncMode) IsValid() bool {
return mode >= FullSync && mode <= LightSync
}
Expand Down
34 changes: 18 additions & 16 deletions eth/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,23 +176,25 @@ func (pm *ProtocolManager) synchronise(peer *peer) {
}
// Otherwise try to sync with the downloader
mode := downloader.FullSync
if atomic.LoadUint32(&pm.fastSync) == 1 {
// Fast sync was explicitly requested, and explicitly granted
mode = downloader.FastSync
} else if currentBlock.NumberU64() == 0 && pm.blockchain.CurrentFastBlock().NumberU64() > 0 {
// The database seems empty as the current block is the genesis. Yet the fast
// block is ahead, so fast sync was enabled for this node at a certain point.
// The only scenario where this can happen is if the user manually (or via a
// bad block) rolled back a fast sync node below the sync point. In this case
// however it's safe to reenable fast sync.
atomic.StoreUint32(&pm.fastSync, 1)
mode = downloader.FastSync
}
if downloader.FastSyncSupported() {
if atomic.LoadUint32(&pm.fastSync) == 1 {
// Fast sync was explicitly requested, and explicitly granted
mode = downloader.FastSync
} else if currentBlock.NumberU64() == 0 && pm.blockchain.CurrentFastBlock().NumberU64() > 0 {
// The database seems empty as the current block is the genesis. Yet the fast
// block is ahead, so fast sync was enabled for this node at a certain point.
// The only scenario where this can happen is if the user manually (or via a
// bad block) rolled back a fast sync node below the sync point. In this case
// however it's safe to reenable fast sync.
atomic.StoreUint32(&pm.fastSync, 1)
mode = downloader.FastSync
}

if mode == downloader.FastSync {
// Make sure the peer's total difficulty we are synchronizing is higher.
if pm.blockchain.GetTdByHash(pm.blockchain.CurrentFastBlock().Hash()).Cmp(pTd) >= 0 {
return
if mode == downloader.FastSync {
// Make sure the peer's total difficulty we are synchronizing is higher.
if pm.blockchain.GetTdByHash(pm.blockchain.CurrentFastBlock().Hash()).Cmp(pTd) >= 0 {
return
}
}
}

Expand Down

0 comments on commit 4f3f852

Please sign in to comment.