Skip to content

Instantly share code, notes, and snippets.

@rjl493456442
Created January 24, 2019 06:10
Show Gist options
  • Save rjl493456442/3eef956754751befc1f401f700ece628 to your computer and use it in GitHub Desktop.
Save rjl493456442/3eef956754751befc1f401f700ece628 to your computer and use it in GitHub Desktop.
diff --git a/cmd/geth/main.go b/cmd/geth/main.go
index 23bdce560..87d6f0779 100644
--- a/cmd/geth/main.go
+++ b/cmd/geth/main.go
@@ -31,13 +31,12 @@ import (
 	"github.com/ethereum/go-ethereum/accounts"
 	"github.com/ethereum/go-ethereum/accounts/keystore"
 	"github.com/ethereum/go-ethereum/cmd/utils"
+	"github.com/ethereum/go-ethereum/common"
 	"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"
 	"github.com/ethereum/go-ethereum/log"
 	"github.com/ethereum/go-ethereum/metrics"
 	"github.com/ethereum/go-ethereum/node"
@@ -333,33 +332,28 @@ 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(&ethereum); 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
+					}
+					done, ok := event.Data.(downloader.DoneEvent)
+					if !ok {
+						continue
 					}
-					latest :=headers.Data.(*types.Header).Time
-					if 600 >= time.Now().Unix()-latest.Int64() {
-						log.Info("Synchronisation completed, checking", "check", exitWhenSynced)
+					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 7c7f82390..dc371e30d 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 8dd7aeb02..fc9d34cf0 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 b6329b801..bdbee0d5f 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 {
+	Latest *types.Header // The latest block header owned locally when synchronization is complete
+}
 type StartEvent struct{}
 type FailedEvent struct{ Err error }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment