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/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:
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
|
|
|||
|
|
@ -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{
|
||||
|
|
|
|||
|
|
@ -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})
|
||||
}
|
||||
}()
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
|
|||
Loading…
Reference in a new issue