2019-09-30 21:06:47 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2019-10-13 07:33:25 +00:00
|
|
|
"time"
|
2019-09-30 21:06:47 +00:00
|
|
|
|
2019-10-13 07:33:25 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
2019-09-30 21:06:47 +00:00
|
|
|
"gopkg.in/urfave/cli.v2"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/chain"
|
2019-09-30 21:06:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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,
|
2019-09-30 21:06:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-09-30 21:06:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-09-30 21:06:47 +00:00
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
ss, err := api.SyncState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var base, target []cid.Cid
|
|
|
|
if ss.Base != nil {
|
|
|
|
base = ss.Base.Cids()
|
|
|
|
}
|
|
|
|
if ss.Target != nil {
|
|
|
|
target = ss.Target.Cids()
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("sync status:")
|
|
|
|
fmt.Printf("Base:\t%s\n", base)
|
|
|
|
fmt.Printf("Target:\t%s\n", target)
|
|
|
|
fmt.Printf("Stage: %s\n", chain.SyncStageString(ss.Stage))
|
|
|
|
fmt.Printf("Height: %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 {
|
|
|
|
ss, err := napi.SyncState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var target []cid.Cid
|
|
|
|
if ss.Target != nil {
|
|
|
|
target = ss.Target.Cids()
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("\r\x1b[2KTarget: %s\tState: %s\tHeight: %d", target, chain.SyncStageString(ss.Stage), ss.Height)
|
|
|
|
if ss.Stage == api.StageSyncComplete {
|
|
|
|
fmt.Println("\nDone")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|