lotus/cli/sync.go

110 lines
2.2 KiB
Go
Raw Normal View History

package cli
import (
"fmt"
2019-10-13 07:33:25 +00:00
"time"
2019-10-13 07:33:25 +00:00
cid "github.com/ipfs/go-cid"
"gopkg.in/urfave/cli.v2"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain"
)
var syncCmd = &cli.Command{
Name: "sync",
Usage: "Inspect or interact with the chain syncer",
Subcommands: []*cli.Command{
syncStatusCmd,
2019-10-13 07:33:25 +00:00
syncWaitCmd,
},
}
var syncStatusCmd = &cli.Command{
Name: "status",
Usage: "check sync status",
Action: func(cctx *cli.Context) error {
2019-10-03 18:12:30 +00:00
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer closer()
ctx := ReqContext(cctx)
2019-11-16 01:05:16 +00:00
state, err := api.SyncState(ctx)
if err != nil {
return err
}
fmt.Println("sync status:")
2019-11-16 01:05:16 +00:00
for i, ss := range state.ActiveSyncs {
fmt.Printf("worker %d:\n", i)
var base, target []cid.Cid
if ss.Base != nil {
base = ss.Base.Cids()
}
if ss.Target != nil {
target = ss.Target.Cids()
}
fmt.Printf("\tBase:\t%s\n", base)
fmt.Printf("\tTarget:\t%s\n", target)
fmt.Printf("\tStage: %s\n", chain.SyncStageString(ss.Stage))
fmt.Printf("\tHeight: %d\n", ss.Height)
}
return nil
},
}
2019-10-13 07:33:25 +00:00
var syncWaitCmd = &cli.Command{
Name: "wait",
Usage: "Wait for sync to be complete",
Action: func(cctx *cli.Context) error {
napi, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
for {
2019-11-16 01:05:16 +00:00
state, err := napi.SyncState(ctx)
2019-10-13 07:33:25 +00:00
if err != nil {
return err
}
head, err := napi.ChainHead(ctx)
if err != nil {
return err
}
working := 0
2019-11-16 01:05:16 +00:00
for i, ss := range state.ActiveSyncs {
switch ss.Stage {
case api.StageSyncComplete:
default:
working = i
case api.StageIdle:
// not complete, not actively working
}
}
ss := state.ActiveSyncs[working]
2019-10-13 07:33:25 +00:00
var target []cid.Cid
if ss.Target != nil {
target = ss.Target.Cids()
}
2019-11-16 01:05:16 +00:00
fmt.Printf("\r\x1b[2KWorker %d: Target: %s\tState: %s\tHeight: %d", working, target, chain.SyncStageString(ss.Stage), ss.Height)
if time.Now().Unix()-int64(head.MinTimestamp()) < build.BlockDelay {
fmt.Println("Done!")
2019-10-13 07:33:25 +00:00
return nil
}
time.Sleep(1 * time.Second)
}
},
}