mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 02:42:27 +00:00
cmd/geth, cmd/utils, eth/downloader: Cleaned up code based on recommendations
This commit is contained in:
parent
41ebfe2d3c
commit
b3e63046db
4 changed files with 21 additions and 28 deletions
|
|
@ -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(ðereum); 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:
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
|
||||||
|
|
@ -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{
|
||||||
|
|
|
||||||
|
|
@ -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})
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
|
||||||
|
|
@ -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 }
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue