diff --git a/cmd/geth/main.go b/cmd/geth/main.go index 23bdce5601..42322d7b56 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -33,8 +33,8 @@ import ( "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/console" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/eth" + "github.com/ethereum/go-ethereum/eth/downloader" "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/internal/debug" "github.com/ethereum/go-ethereum/les" @@ -333,33 +333,27 @@ func startNode(ctx *cli.Context, stack *node.Node) { } }() - if exitWhenSynced := ctx.GlobalDuration(utils.ExitWhenSyncedFlag.Name); exitWhenSynced == true { + // Spawn a standalone goroutine for status synchronization monitoring, + // close the node when synchronization is complete if user required. + if ctx.GlobalBool(utils.ExitWhenSyncedFlag.Name) { go func() { - if ctx.GlobalString(utils.SyncModeFlag.Name) == "light" { - var lightEthereum *les.LightEthereum - if err := stack.Service(&lightEthereum); err != nil { - utils.Fatalf("LightEthereum service not running: %v", err) - } - } else { - var ethereum *eth.Ethereum - if err := stack.Service(ðereum); err != nil { - utils.Fatalf("Ethereum service not running: %v", err) - } - } - var mux = stack.EventMux() - var sub = mux.Subscribe(downloader.DoneEvent{}) + sub := stack.EventMux().Subscribe(downloader.DoneEvent{}) + defer sub.Unsubscribe() for { select { - case headers := <-sub.Chan(): - if headers == nil { - return + case event := <-sub.Chan(): + if event == nil { + continue } - latest :=headers.Data.(*types.Header).Time - if 600 >= time.Now().Unix()-latest.Int64() { - log.Info("Synchronisation completed, checking", "check", exitWhenSynced) + done, ok := event.Data.(downloader.DoneEvent) + if !ok { + continue + } + if timestamp := time.Unix(done.Latest.Time.Int64(), 0); time.Since(timestamp) < 10*time.Minute { + log.Info("Synchronisation completed", "latestnum", done.Latest.Number, "latesthash", done.Latest.Hash(), + "age", common.PrettyAge(timestamp)) stack.Stop() } - default: } } }() diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 7c7f823909..dc371e30d4 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -160,7 +160,6 @@ var ( ExitWhenSyncedFlag = cli.BoolFlag{ Name: "exitwhensynced", Usage: "Exists syncing by given time (default 0) after block synchronisation", - Value: -1, } defaultSyncMode = eth.DefaultConfig.SyncMode SyncModeFlag = TextMarshalerFlag{ diff --git a/eth/downloader/downloader.go b/eth/downloader/downloader.go index 8dd7aeb02d..fc9d34cf0c 100644 --- a/eth/downloader/downloader.go +++ b/eth/downloader/downloader.go @@ -414,7 +414,7 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.I if err != nil { d.mux.Post(FailedEvent{err}) } else { - latest := d.blockchain.CurrentHeader() + latest := d.lightchain.CurrentHeader() d.mux.Post(DoneEvent{latest}) } }() diff --git a/eth/downloader/events.go b/eth/downloader/events.go index b6329b8015..95b3087e07 100644 --- a/eth/downloader/events.go +++ b/eth/downloader/events.go @@ -16,10 +16,10 @@ package downloader -import ( - "github.com/ethereum/go-ethereum/core/types" -) +import "github.com/ethereum/go-ethereum/core/types" -type DoneEvent struct{ *types.Header } +type DoneEvent struct { + *types.Header +} type StartEvent struct{} type FailedEvent struct{ Err error }