2019-09-30 21:06:47 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2019-12-04 00:25:18 +00:00
|
|
|
"context"
|
2019-09-30 21:06:47 +00:00
|
|
|
"fmt"
|
2019-10-13 07:33:25 +00:00
|
|
|
"time"
|
2019-09-30 21:06:47 +00:00
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
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"
|
2019-11-16 22:34:05 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-10-18 04:47:41 +00:00
|
|
|
"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-12-21 06:10:40 +00:00
|
|
|
syncMarkBadCmd,
|
2020-02-12 07:44:55 +00:00
|
|
|
syncCheckBadCmd,
|
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-12-04 04:59:41 +00:00
|
|
|
apic, 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)
|
|
|
|
|
2019-12-04 04:59:41 +00:00
|
|
|
state, err := apic.SyncState(ctx)
|
2019-09-30 21:06:47 +00:00
|
|
|
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
|
2019-11-20 19:44:38 +00:00
|
|
|
var heightDiff int64
|
2020-02-08 02:18:32 +00:00
|
|
|
var theight abi.ChainEpoch
|
2019-11-16 01:05:16 +00:00
|
|
|
if ss.Base != nil {
|
|
|
|
base = ss.Base.Cids()
|
2019-11-20 19:44:38 +00:00
|
|
|
heightDiff = int64(ss.Base.Height())
|
2019-11-16 01:05:16 +00:00
|
|
|
}
|
|
|
|
if ss.Target != nil {
|
|
|
|
target = ss.Target.Cids()
|
2019-11-20 19:44:38 +00:00
|
|
|
heightDiff = int64(ss.Target.Height()) - heightDiff
|
2019-12-04 04:59:41 +00:00
|
|
|
theight = ss.Target.Height()
|
2019-11-20 19:44:38 +00:00
|
|
|
} else {
|
|
|
|
heightDiff = 0
|
2019-11-16 01:05:16 +00:00
|
|
|
}
|
|
|
|
fmt.Printf("\tBase:\t%s\n", base)
|
2019-12-04 04:59:41 +00:00
|
|
|
fmt.Printf("\tTarget:\t%s (%d)\n", target, theight)
|
2019-11-20 19:44:38 +00:00
|
|
|
fmt.Printf("\tHeight diff:\t%d\n", heightDiff)
|
2019-11-16 01:05:16 +00:00
|
|
|
fmt.Printf("\tStage: %s\n", chain.SyncStageString(ss.Stage))
|
|
|
|
fmt.Printf("\tHeight: %d\n", ss.Height)
|
2019-12-04 04:59:41 +00:00
|
|
|
if ss.End.IsZero() {
|
|
|
|
if !ss.Start.IsZero() {
|
|
|
|
fmt.Printf("\tElapsed: %s\n", time.Since(ss.Start))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fmt.Printf("\tElapsed: %s\n", ss.End.Sub(ss.Start))
|
|
|
|
}
|
|
|
|
if ss.Stage == api.StageSyncErrored {
|
|
|
|
fmt.Printf("\tError: %s\n", ss.Message)
|
|
|
|
}
|
2019-11-16 01:05:16 +00:00
|
|
|
}
|
2019-09-30 21:06:47 +00:00
|
|
|
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)
|
|
|
|
|
2019-12-04 00:25:18 +00:00
|
|
|
return SyncWait(ctx, napi)
|
|
|
|
},
|
|
|
|
}
|
2019-10-13 07:33:25 +00:00
|
|
|
|
2019-12-21 06:10:40 +00:00
|
|
|
var syncMarkBadCmd = &cli.Command{
|
2020-03-06 19:01:28 +00:00
|
|
|
Name: "mark-bad",
|
|
|
|
Usage: "Mark the given block as bad, will prevent syncing to a chain that contains it",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[blockCid]",
|
2019-12-21 06:10:40 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
napi, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
if !cctx.Args().Present() {
|
|
|
|
return fmt.Errorf("must specify block cid to mark")
|
|
|
|
}
|
|
|
|
|
|
|
|
bcid, err := cid.Decode(cctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decode input as a cid: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return napi.SyncMarkBad(ctx, bcid)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-12 07:44:55 +00:00
|
|
|
var syncCheckBadCmd = &cli.Command{
|
2020-03-06 19:01:28 +00:00
|
|
|
Name: "check-bad",
|
|
|
|
Usage: "check if the given block was marked bad, and for what reason",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[blockCid]",
|
2020-02-12 07:44:55 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
napi, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
if !cctx.Args().Present() {
|
|
|
|
return fmt.Errorf("must specify block cid to check")
|
|
|
|
}
|
|
|
|
|
|
|
|
bcid, err := cid.Decode(cctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decode input as a cid: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
reason, err := napi.SyncCheckBad(ctx, bcid)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if reason == "" {
|
|
|
|
fmt.Println("block was not marked as bad")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(reason)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-12-04 00:25:18 +00:00
|
|
|
func SyncWait(ctx context.Context, napi api.FullNode) error {
|
|
|
|
for {
|
|
|
|
state, err := napi.SyncState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
head, err := napi.ChainHead(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-16 22:34:05 +00:00
|
|
|
|
2019-12-04 00:25:18 +00:00
|
|
|
working := 0
|
|
|
|
for i, ss := range state.ActiveSyncs {
|
|
|
|
switch ss.Stage {
|
|
|
|
case api.StageSyncComplete:
|
|
|
|
default:
|
|
|
|
working = i
|
|
|
|
case api.StageIdle:
|
|
|
|
// not complete, not actively working
|
2019-11-16 01:05:16 +00:00
|
|
|
}
|
2019-12-04 00:25:18 +00:00
|
|
|
}
|
2019-11-16 01:05:16 +00:00
|
|
|
|
2019-12-04 00:25:18 +00:00
|
|
|
ss := state.ActiveSyncs[working]
|
2019-11-16 01:05:16 +00:00
|
|
|
|
2019-12-04 00:25:18 +00:00
|
|
|
var target []cid.Cid
|
|
|
|
if ss.Target != nil {
|
|
|
|
target = ss.Target.Cids()
|
|
|
|
}
|
2019-10-13 07:33:25 +00:00
|
|
|
|
2019-12-04 00:25:18 +00:00
|
|
|
fmt.Printf("\r\x1b[2KWorker %d: Target: %s\tState: %s\tHeight: %d", working, target, chain.SyncStageString(ss.Stage), ss.Height)
|
2019-11-16 22:34:05 +00:00
|
|
|
|
2019-12-04 00:25:18 +00:00
|
|
|
if time.Now().Unix()-int64(head.MinTimestamp()) < build.BlockDelay {
|
|
|
|
fmt.Println("\nDone!")
|
|
|
|
return nil
|
|
|
|
}
|
2019-10-13 07:33:25 +00:00
|
|
|
|
2019-12-04 00:25:18 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
fmt.Println("\nExit by user")
|
|
|
|
return nil
|
|
|
|
case <-time.After(1 * time.Second):
|
2019-10-13 07:33:25 +00:00
|
|
|
}
|
2019-12-04 00:25:18 +00:00
|
|
|
}
|
2019-10-13 07:33:25 +00:00
|
|
|
}
|