cmd/geth, cmd/utils, eth/downloader: Cleaned up code based on recommendations

This commit is contained in:
Lucas Hendren 2019-01-24 21:52:58 -05:00
parent 41ebfe2d3c
commit b3e63046db
4 changed files with 21 additions and 28 deletions

View file

@ -33,8 +33,8 @@ import (
"github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/console" "github.com/ethereum/go-ethereum/console"
"github.com/ethereum/go-ethereum/core/types" "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"
"github.com/ethereum/go-ethereum/eth/downloader"
"github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/internal/debug" "github.com/ethereum/go-ethereum/internal/debug"
"github.com/ethereum/go-ethereum/les" "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() { go func() {
if ctx.GlobalString(utils.SyncModeFlag.Name) == "light" { sub := stack.EventMux().Subscribe(downloader.DoneEvent{})
var lightEthereum *les.LightEthereum defer sub.Unsubscribe()
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{})
for { for {
select { select {
case headers := <-sub.Chan(): case event := <-sub.Chan():
if headers == nil { if event == nil {
return continue
} }
latest :=headers.Data.(*types.Header).Time done, ok := event.Data.(downloader.DoneEvent)
if 600 >= time.Now().Unix()-latest.Int64() { if !ok {
log.Info("Synchronisation completed, checking", "check", exitWhenSynced) 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() stack.Stop()
} }
default:
} }
} }
}() }()

View file

@ -160,7 +160,6 @@ var (
ExitWhenSyncedFlag = cli.BoolFlag{ ExitWhenSyncedFlag = cli.BoolFlag{
Name: "exitwhensynced", Name: "exitwhensynced",
Usage: "Exists syncing by given time (default 0) after block synchronisation", Usage: "Exists syncing by given time (default 0) after block synchronisation",
Value: -1,
} }
defaultSyncMode = eth.DefaultConfig.SyncMode defaultSyncMode = eth.DefaultConfig.SyncMode
SyncModeFlag = TextMarshalerFlag{ SyncModeFlag = TextMarshalerFlag{

View file

@ -414,7 +414,7 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.I
if err != nil { if err != nil {
d.mux.Post(FailedEvent{err}) d.mux.Post(FailedEvent{err})
} else { } else {
latest := d.blockchain.CurrentHeader() latest := d.lightchain.CurrentHeader()
d.mux.Post(DoneEvent{latest}) d.mux.Post(DoneEvent{latest})
} }
}() }()

View file

@ -16,10 +16,10 @@
package downloader package downloader
import ( import "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/types"
)
type DoneEvent struct{ *types.Header } type DoneEvent struct {
*types.Header
}
type StartEvent struct{} type StartEvent struct{}
type FailedEvent struct{ Err error } type FailedEvent struct{ Err error }