From 7ea50b72646bee779bd4bd5ee81d542c30cb8abb Mon Sep 17 00:00:00 2001 From: Lucas Hendren Date: Thu, 26 Jul 2018 01:55:35 -0400 Subject: [PATCH] cmd, eth: Added in the flag to step geth once sync based on input --- cmd/geth/main.go | 19 +++++++++++++++++++ cmd/geth/usage.go | 1 + cmd/utils/flags.go | 9 ++++++++- eth/config.go | 3 +++ eth/downloader/downloader.go | 6 ++++++ 5 files changed, 37 insertions(+), 1 deletion(-) diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 1c618de352..3e454c8b4a 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -83,6 +83,7 @@ var ( utils.TxPoolGlobalQueueFlag, utils.TxPoolLifetimeFlag, utils.FastSyncFlag, + utils.ExitWhenSynced, utils.LightModeFlag, utils.SyncModeFlag, utils.GCModeFlag, @@ -317,6 +318,24 @@ func startNode(ctx *cli.Context, stack *node.Node) { } } }() + + if exitWhenSynced := ctx.GlobalDuration(utils.ExitWhenSynced.Name); exitWhenSynced >= 0 { + go func() { + if ctx.GlobalBool(utils.LightModeFlag.Name) || ctx.GlobalString(utils.SyncModeFlag.Name) == "light" { + log.Warn("Exit after block synchronisation disabled for light mode") + } else { + var ethereum *eth.Ethereum + if err := stack.Service(ðereum); err != nil { + utils.Fatalf("Ethereum service not running: %v", err) + } + <-ethereum.Downloader().SyncedCh + log.Info("Synchronisation completed, exitting", "countdown", exitWhenSynced) + time.Sleep(exitWhenSynced) + stack.Stop() + } + }() + } + // Start auxiliary services if enabled if ctx.GlobalBool(utils.MiningEnabledFlag.Name) || ctx.GlobalBool(utils.DeveloperFlag.Name) { // Mining only makes sense if a full Ethereum node is running diff --git a/cmd/geth/usage.go b/cmd/geth/usage.go index 6a12a66cc2..4e08c9e94a 100644 --- a/cmd/geth/usage.go +++ b/cmd/geth/usage.go @@ -75,6 +75,7 @@ var AppHelpFlagGroups = []flagGroup{ utils.TestnetFlag, utils.RinkebyFlag, utils.SyncModeFlag, + utils.ExitWhenSynced, utils.GCModeFlag, utils.EthStatsURLFlag, utils.IdentityFlag, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index fb12213651..76cb405705 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -162,6 +162,11 @@ var ( Name: "fast", Usage: "Enable fast syncing through state downloads (replaced by --syncmode)", } + ExitWhenSynced = cli.DurationFlag{ + Name: "exitwhensynced", + Usage: "Exists syncing by given time (default 0) after block synchronisation", + Value: -1, + } LightModeFlag = cli.BoolFlag{ Name: "light", Usage: "Enable light client mode (replaced by --syncmode)", @@ -1077,7 +1082,9 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) { if ctx.GlobalIsSet(NetworkIdFlag.Name) { cfg.NetworkId = ctx.GlobalUint64(NetworkIdFlag.Name) } - + if ctx.GlobalIsSet(ExitWhenSynced.Name) { + cfg.NetworkId = ctx.GlobalUint64(ExitWhenSynced.Name) + } if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheDatabaseFlag.Name) { cfg.DatabaseCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheDatabaseFlag.Name) / 100 } diff --git a/eth/config.go b/eth/config.go index 426d2bf1ef..78b582083a 100644 --- a/eth/config.go +++ b/eth/config.go @@ -114,6 +114,9 @@ type Config struct { // Miscellaneous options DocRoot string `toml:"-"` + + // specify time to exit after syncing + ExitInSync time.Duration } type configMarshaling struct { diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index fbde9c6caa..17dbc9dbc2 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -128,6 +128,7 @@ type Downloader struct { bodyWakeCh chan bool // [eth/62] Channel to signal the block body fetcher of new tasks receiptWakeCh chan bool // [eth/63] Channel to signal the receipt fetcher of new tasks headerProcCh chan []*types.Header // [eth/62] Channel to feed the header processor new tasks + SyncedCh chan bool // for stateFetcher stateSyncStart chan *stateSync @@ -226,6 +227,7 @@ func New(mode SyncMode, stateDb ethdb.Database, mux *event.TypeMux, chain BlockC syncStatsState: stateSyncStats{ processed: rawdb.ReadFastTrieProgress(stateDb), }, + SyncedCh: make(chan bool, 1), trackStateReq: make(chan *stateReq), } go dl.qosTuner() @@ -317,6 +319,10 @@ func (d *Downloader) Synchronise(id string, head common.Hash, td *big.Int, mode err := d.synchronise(id, head, td, mode) switch err { case nil: + select { + case d.SyncedCh <- true: + default: + } case errBusy: case errTimeout, errBadPeer, errStallingPeer,