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-09-09 07:42:03 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2019-10-13 07:33:25 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
2020-06-02 18:12:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2019-09-30 21:06:47 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2021-04-03 10:55:29 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v0api"
|
2019-11-16 22:34:05 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2019-09-30 21:06:47 +00:00
|
|
|
)
|
|
|
|
|
2021-03-23 23:23:22 +00:00
|
|
|
var SyncCmd = &cli.Command{
|
2019-09-30 21:06:47 +00:00
|
|
|
Name: "sync",
|
|
|
|
Usage: "Inspect or interact with the chain syncer",
|
|
|
|
Subcommands: []*cli.Command{
|
2021-03-23 23:23:22 +00:00
|
|
|
SyncStatusCmd,
|
|
|
|
SyncWaitCmd,
|
|
|
|
SyncMarkBadCmd,
|
|
|
|
SyncUnmarkBadCmd,
|
|
|
|
SyncCheckBadCmd,
|
|
|
|
SyncCheckpointCmd,
|
2019-09-30 21:06:47 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:23:22 +00:00
|
|
|
var SyncStatusCmd = &cli.Command{
|
2019-09-30 21:06:47 +00:00
|
|
|
Name: "status",
|
|
|
|
Usage: "check sync status",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2022-02-11 16:45:19 +00:00
|
|
|
afmt := NewAppFmt(cctx.App)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-02-11 16:45:19 +00:00
|
|
|
afmt.Println("sync status:")
|
2020-10-28 19:22:07 +00:00
|
|
|
for _, ss := range state.ActiveSyncs {
|
2022-02-11 16:45:19 +00:00
|
|
|
afmt.Printf("worker %d:\n", ss.WorkerID)
|
2019-11-16 01:05:16 +00:00
|
|
|
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
|
|
|
}
|
2022-02-11 16:45:19 +00:00
|
|
|
afmt.Printf("\tBase:\t%s\n", base)
|
|
|
|
afmt.Printf("\tTarget:\t%s (%d)\n", target, theight)
|
|
|
|
afmt.Printf("\tHeight diff:\t%d\n", heightDiff)
|
|
|
|
afmt.Printf("\tStage: %s\n", ss.Stage)
|
|
|
|
afmt.Printf("\tHeight: %d\n", ss.Height)
|
2019-12-04 04:59:41 +00:00
|
|
|
if ss.End.IsZero() {
|
|
|
|
if !ss.Start.IsZero() {
|
2022-02-11 16:45:19 +00:00
|
|
|
afmt.Printf("\tElapsed: %s\n", time.Since(ss.Start))
|
2019-12-04 04:59:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
2022-02-11 16:45:19 +00:00
|
|
|
afmt.Printf("\tElapsed: %s\n", ss.End.Sub(ss.Start))
|
2019-12-04 04:59:41 +00:00
|
|
|
}
|
|
|
|
if ss.Stage == api.StageSyncErrored {
|
2022-02-11 16:45:19 +00:00
|
|
|
afmt.Printf("\tError: %s\n", ss.Message)
|
2019-12-04 04:59:41 +00:00
|
|
|
}
|
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
|
|
|
|
2021-03-23 23:23:22 +00:00
|
|
|
var SyncWaitCmd = &cli.Command{
|
2019-10-13 07:33:25 +00:00
|
|
|
Name: "wait",
|
|
|
|
Usage: "Wait for sync to be complete",
|
2020-10-14 16:09:31 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "watch",
|
|
|
|
Usage: "don't exit after node is synced",
|
|
|
|
},
|
|
|
|
},
|
2019-10-13 07:33:25 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
napi, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2020-10-14 16:09:31 +00:00
|
|
|
return SyncWait(ctx, napi, cctx.Bool("watch"))
|
2019-12-04 00:25:18 +00:00
|
|
|
},
|
|
|
|
}
|
2019-10-13 07:33:25 +00:00
|
|
|
|
2021-03-23 23:23:22 +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)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:23:22 +00:00
|
|
|
var SyncUnmarkBadCmd = &cli.Command{
|
2020-10-10 18:46:06 +00:00
|
|
|
Name: "unmark-bad",
|
|
|
|
Usage: "Unmark the given block as bad, makes it possible to sync to a chain containing it",
|
2020-10-10 08:26:42 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
2020-10-10 18:46:06 +00:00
|
|
|
Name: "all",
|
2020-10-10 08:26:42 +00:00
|
|
|
Usage: "drop the entire bad block cache",
|
|
|
|
},
|
|
|
|
},
|
2020-09-09 07:25:19 +00:00
|
|
|
ArgsUsage: "[blockCid]",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
napi, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2020-10-10 08:26:42 +00:00
|
|
|
if cctx.Bool("all") {
|
|
|
|
return napi.SyncUnmarkAllBad(ctx)
|
|
|
|
}
|
|
|
|
|
2020-09-09 07:25:19 +00:00
|
|
|
if !cctx.Args().Present() {
|
|
|
|
return fmt.Errorf("must specify block cid to unmark")
|
|
|
|
}
|
|
|
|
|
|
|
|
bcid, err := cid.Decode(cctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to decode input as a cid: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return napi.SyncUnmarkBad(ctx, bcid)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:23:22 +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 {
|
2022-02-11 17:08:36 +00:00
|
|
|
afmt := NewAppFmt(cctx.App)
|
|
|
|
|
2020-02-12 07:44:55 +00:00
|
|
|
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 == "" {
|
2022-02-11 17:08:36 +00:00
|
|
|
afmt.Println("block was not marked as bad")
|
2020-02-12 07:44:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-11 17:08:36 +00:00
|
|
|
afmt.Println(reason)
|
2020-02-12 07:44:55 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-23 23:23:22 +00:00
|
|
|
var SyncCheckpointCmd = &cli.Command{
|
2020-09-09 07:42:03 +00:00
|
|
|
Name: "checkpoint",
|
|
|
|
Usage: "mark a certain tipset as checkpointed; the node will never fork away from this tipset",
|
|
|
|
ArgsUsage: "[tipsetKey]",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.Uint64Flag{
|
|
|
|
Name: "epoch",
|
|
|
|
Usage: "checkpoint the tipset at the given epoch",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
napi, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
var ts *types.TipSet
|
|
|
|
|
|
|
|
if cctx.IsSet("epoch") {
|
|
|
|
ts, err = napi.ChainGetTipSetByHeight(ctx, abi.ChainEpoch(cctx.Uint64("epoch")), types.EmptyTSK)
|
|
|
|
}
|
|
|
|
if ts == nil {
|
|
|
|
ts, err = parseTipSet(ctx, napi, cctx.Args().Slice())
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ts == nil {
|
|
|
|
return fmt.Errorf("must pass cids for tipset to set as head, or specify epoch flag")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := napi.SyncCheckpoint(ctx, ts.Key()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-04-03 10:55:29 +00:00
|
|
|
func SyncWait(ctx context.Context, napi v0api.FullNode, watch bool) error {
|
2020-09-24 11:35:45 +00:00
|
|
|
tick := time.Second / 4
|
|
|
|
|
|
|
|
lastLines := 0
|
|
|
|
ticker := time.NewTicker(tick)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
samples := 8
|
|
|
|
i := 0
|
2020-10-08 20:48:21 +00:00
|
|
|
var firstApp, app, lastApp uint64
|
|
|
|
|
|
|
|
state, err := napi.SyncState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
firstApp = state.VMApplied
|
2020-09-24 11:35:45 +00:00
|
|
|
|
2019-12-04 00:25:18 +00:00
|
|
|
for {
|
|
|
|
state, err := napi.SyncState(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-30 17:28:32 +00:00
|
|
|
if len(state.ActiveSyncs) == 0 {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-12-04 00:25:18 +00:00
|
|
|
head, err := napi.ChainHead(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-16 22:34:05 +00:00
|
|
|
|
2020-10-30 17:45:59 +00:00
|
|
|
working := -1
|
2019-12-04 00:25:18 +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
|
2019-11-16 01:05:16 +00:00
|
|
|
}
|
2019-12-04 00:25:18 +00:00
|
|
|
}
|
2019-11-16 01:05:16 +00:00
|
|
|
|
2020-10-30 17:45:59 +00:00
|
|
|
if working == -1 {
|
|
|
|
working = len(state.ActiveSyncs) - 1
|
|
|
|
}
|
|
|
|
|
2019-12-04 00:25:18 +00:00
|
|
|
ss := state.ActiveSyncs[working]
|
2020-10-30 17:28:32 +00:00
|
|
|
workerID := ss.WorkerID
|
2019-11-16 01:05:16 +00:00
|
|
|
|
2020-09-19 06:04:48 +00:00
|
|
|
var baseHeight abi.ChainEpoch
|
2019-12-04 00:25:18 +00:00
|
|
|
var target []cid.Cid
|
2020-08-31 05:43:58 +00:00
|
|
|
var theight abi.ChainEpoch
|
2020-09-19 06:04:48 +00:00
|
|
|
var heightDiff int64
|
|
|
|
|
|
|
|
if ss.Base != nil {
|
|
|
|
baseHeight = ss.Base.Height()
|
|
|
|
heightDiff = int64(ss.Base.Height())
|
|
|
|
}
|
2019-12-04 00:25:18 +00:00
|
|
|
if ss.Target != nil {
|
|
|
|
target = ss.Target.Cids()
|
2020-08-31 05:43:58 +00:00
|
|
|
theight = ss.Target.Height()
|
2020-09-19 06:04:48 +00:00
|
|
|
heightDiff = int64(ss.Target.Height()) - heightDiff
|
|
|
|
} else {
|
|
|
|
heightDiff = 0
|
2019-12-04 00:25:18 +00:00
|
|
|
}
|
2019-10-13 07:33:25 +00:00
|
|
|
|
2020-09-24 11:35:45 +00:00
|
|
|
for i := 0; i < lastLines; i++ {
|
|
|
|
fmt.Print("\r\x1b[2K\x1b[A")
|
|
|
|
}
|
|
|
|
|
2020-10-30 17:28:32 +00:00
|
|
|
fmt.Printf("Worker: %d; Base: %d; Target: %d (diff: %d)\n", workerID, baseHeight, theight, heightDiff)
|
2020-09-24 11:35:45 +00:00
|
|
|
fmt.Printf("State: %s; Current Epoch: %d; Todo: %d\n", ss.Stage, ss.Height, theight-ss.Height)
|
|
|
|
lastLines = 2
|
|
|
|
|
|
|
|
if i%samples == 0 {
|
|
|
|
lastApp = app
|
2020-10-08 20:48:21 +00:00
|
|
|
app = state.VMApplied - firstApp
|
2020-09-24 11:35:45 +00:00
|
|
|
}
|
|
|
|
if i > 0 {
|
2020-10-08 20:48:21 +00:00
|
|
|
fmt.Printf("Validated %d messages (%d per second)\n", state.VMApplied-firstApp, (app-lastApp)*uint64(time.Second/tick)/uint64(samples))
|
2020-09-24 11:35:45 +00:00
|
|
|
lastLines++
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = target // todo: maybe print? (creates a bunch of line wrapping issues with most tipsets)
|
2019-11-16 22:34:05 +00:00
|
|
|
|
2020-10-14 16:09:31 +00:00
|
|
|
if !watch && time.Now().Unix()-int64(head.MinTimestamp()) < int64(build.BlockDelaySecs) {
|
2019-12-04 00:25:18 +00:00
|
|
|
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
|
2020-09-24 11:35:45 +00:00
|
|
|
case <-ticker.C:
|
2019-10-13 07:33:25 +00:00
|
|
|
}
|
2020-09-24 11:35:45 +00:00
|
|
|
|
|
|
|
i++
|
2019-12-04 00:25:18 +00:00
|
|
|
}
|
2019-10-13 07:33:25 +00:00
|
|
|
}
|