2019-07-09 15:19:27 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2020-01-30 21:30:21 +00:00
|
|
|
"bytes"
|
2019-10-10 03:50:50 +00:00
|
|
|
"context"
|
2019-07-23 00:54:27 +00:00
|
|
|
"encoding/json"
|
2019-07-09 15:19:27 +00:00
|
|
|
"fmt"
|
2020-01-20 23:51:02 +00:00
|
|
|
"os"
|
2020-01-30 21:30:21 +00:00
|
|
|
"os/exec"
|
2020-04-01 00:06:26 +00:00
|
|
|
"path"
|
2020-01-30 21:30:21 +00:00
|
|
|
"strconv"
|
2019-10-16 08:01:41 +00:00
|
|
|
"strings"
|
2019-10-14 04:39:42 +00:00
|
|
|
"time"
|
2019-07-09 15:19:27 +00:00
|
|
|
|
2020-03-04 23:52:28 +00:00
|
|
|
"github.com/docker/go-units"
|
2020-03-09 06:45:24 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-02-13 03:50:37 +00:00
|
|
|
cborutil "github.com/filecoin-project/go-cbor-util"
|
2020-02-08 02:18:32 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-02-13 03:50:37 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin"
|
2020-04-01 01:34:23 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/account"
|
2020-02-13 03:50:37 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/builtin/power"
|
2020-03-09 03:09:45 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/util/adt"
|
2019-10-06 00:37:28 +00:00
|
|
|
cid "github.com/ipfs/go-cid"
|
2020-03-07 02:47:19 +00:00
|
|
|
cbg "github.com/whyrusleeping/cbor-gen"
|
2019-10-03 20:22:21 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-07-09 15:19:27 +00:00
|
|
|
"gopkg.in/urfave/cli.v2"
|
2019-07-23 00:54:27 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2019-12-18 15:37:47 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors"
|
2019-10-18 04:47:41 +00:00
|
|
|
types "github.com/filecoin-project/lotus/chain/types"
|
2019-07-09 15:19:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var chainCmd = &cli.Command{
|
|
|
|
Name: "chain",
|
|
|
|
Usage: "Interact with filecoin blockchain",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
chainHeadCmd,
|
2019-07-23 00:54:27 +00:00
|
|
|
chainGetBlock,
|
2019-10-01 16:28:07 +00:00
|
|
|
chainReadObjCmd,
|
2020-03-04 23:52:28 +00:00
|
|
|
chainStatObjCmd,
|
2019-10-09 07:26:48 +00:00
|
|
|
chainGetMsgCmd,
|
2019-10-10 03:59:32 +00:00
|
|
|
chainSetHeadCmd,
|
2019-10-11 06:25:25 +00:00
|
|
|
chainListCmd,
|
2019-12-19 15:50:18 +00:00
|
|
|
chainGetCmd,
|
2020-01-30 21:30:21 +00:00
|
|
|
chainBisectCmd,
|
2020-01-20 23:51:02 +00:00
|
|
|
chainExportCmd,
|
2019-12-18 15:37:47 +00:00
|
|
|
slashConsensusFault,
|
2019-07-09 15:19:27 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var chainHeadCmd = &cli.Command{
|
|
|
|
Name: "head",
|
|
|
|
Usage: "Print chain head",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-07-10 17:28:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-07-18 23:16:23 +00:00
|
|
|
ctx := ReqContext(cctx)
|
2019-07-09 15:19:27 +00:00
|
|
|
|
|
|
|
head, err := api.ChainHead(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-11 02:36:43 +00:00
|
|
|
for _, c := range head.Cids() {
|
2019-07-09 15:19:27 +00:00
|
|
|
fmt.Println(c)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2019-07-23 00:54:27 +00:00
|
|
|
|
|
|
|
var chainGetBlock = &cli.Command{
|
2020-03-06 00:20:57 +00:00
|
|
|
Name: "getblock",
|
|
|
|
Usage: "Get a block and print its details",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[blockCid]",
|
2019-07-23 00:54:27 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "raw",
|
|
|
|
Usage: "print just the raw block header",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-07-23 00:54:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-07-18 23:16:23 +00:00
|
|
|
ctx := ReqContext(cctx)
|
2019-07-23 00:54:27 +00:00
|
|
|
|
|
|
|
if !cctx.Args().Present() {
|
|
|
|
return fmt.Errorf("must pass cid of block to print")
|
|
|
|
}
|
|
|
|
|
|
|
|
bcid, err := cid.Decode(cctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
blk, err := api.ChainGetBlock(ctx, bcid)
|
|
|
|
if err != nil {
|
2019-10-03 20:22:21 +00:00
|
|
|
return xerrors.Errorf("get block failed: %w", err)
|
2019-07-23 00:54:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if cctx.Bool("raw") {
|
|
|
|
out, err := json.MarshalIndent(blk, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(out))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs, err := api.ChainGetBlockMessages(ctx, bcid)
|
|
|
|
if err != nil {
|
2019-10-03 20:22:21 +00:00
|
|
|
return xerrors.Errorf("failed to get messages: %w", err)
|
2019-07-23 00:54:27 +00:00
|
|
|
}
|
|
|
|
|
2019-10-03 20:22:21 +00:00
|
|
|
pmsgs, err := api.ChainGetParentMessages(ctx, bcid)
|
2019-08-07 23:22:35 +00:00
|
|
|
if err != nil {
|
2019-10-03 20:22:21 +00:00
|
|
|
return xerrors.Errorf("failed to get parent messages: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
recpts, err := api.ChainGetParentReceipts(ctx, bcid)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
//return xerrors.Errorf("failed to get receipts: %w", err)
|
2019-08-07 23:22:35 +00:00
|
|
|
}
|
|
|
|
|
2019-07-23 00:54:27 +00:00
|
|
|
cblock := struct {
|
2019-07-25 22:15:03 +00:00
|
|
|
types.BlockHeader
|
2019-10-03 20:22:21 +00:00
|
|
|
BlsMessages []*types.Message
|
|
|
|
SecpkMessages []*types.SignedMessage
|
|
|
|
ParentReceipts []*types.MessageReceipt
|
|
|
|
ParentMessages []cid.Cid
|
2019-07-23 00:54:27 +00:00
|
|
|
}{}
|
|
|
|
|
|
|
|
cblock.BlockHeader = *blk
|
2019-08-02 03:51:34 +00:00
|
|
|
cblock.BlsMessages = msgs.BlsMessages
|
|
|
|
cblock.SecpkMessages = msgs.SecpkMessages
|
2019-10-03 20:22:21 +00:00
|
|
|
cblock.ParentReceipts = recpts
|
2019-10-06 00:37:28 +00:00
|
|
|
cblock.ParentMessages = apiMsgCids(pmsgs)
|
2019-07-23 00:54:27 +00:00
|
|
|
|
|
|
|
out, err := json.MarshalIndent(cblock, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(out))
|
|
|
|
return nil
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
2019-10-01 16:28:07 +00:00
|
|
|
|
2019-10-06 00:37:28 +00:00
|
|
|
func apiMsgCids(in []api.Message) []cid.Cid {
|
|
|
|
out := make([]cid.Cid, len(in))
|
|
|
|
for k, v := range in {
|
|
|
|
out[k] = v.Cid
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-10-01 16:28:07 +00:00
|
|
|
var chainReadObjCmd = &cli.Command{
|
2020-03-06 00:20:57 +00:00
|
|
|
Name: "read-obj",
|
|
|
|
Usage: "Read the raw bytes of an object",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[objectCid]",
|
2019-10-01 16:28:07 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2019-10-03 18:12:30 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
2019-10-01 16:28:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-03 18:12:30 +00:00
|
|
|
defer closer()
|
2019-10-01 16:28:07 +00:00
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2019-10-01 16:37:31 +00:00
|
|
|
c, err := cid.Decode(cctx.Args().First())
|
2019-10-01 16:28:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse cid input: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err := api.ChainReadObj(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%x\n", obj)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2019-10-09 07:26:48 +00:00
|
|
|
|
2020-03-04 23:52:28 +00:00
|
|
|
var chainStatObjCmd = &cli.Command{
|
|
|
|
Name: "stat-obj",
|
|
|
|
Usage: "Collect size and ipld link counts for objs",
|
|
|
|
ArgsUsage: "[cid]",
|
|
|
|
Description: `Collect object size and ipld link count for an object.
|
|
|
|
|
|
|
|
When a base is provided it will be walked first, and all links visisted
|
|
|
|
will be ignored when the passed in object is walked.
|
|
|
|
`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "base",
|
|
|
|
Usage: "ignore links found in this obj",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
obj, err := cid.Decode(cctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse cid input: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
base := cid.Undef
|
|
|
|
if cctx.IsSet("base") {
|
|
|
|
base, err = cid.Decode(cctx.String("base"))
|
|
|
|
}
|
|
|
|
|
|
|
|
stats, err := api.ChainStatObj(ctx, obj, base)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Links: %d\n", stats.Links)
|
|
|
|
fmt.Printf("Size: %s (%d)\n", units.BytesSize(float64(stats.Size)), stats.Size)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-10-09 07:26:48 +00:00
|
|
|
var chainGetMsgCmd = &cli.Command{
|
2020-03-06 00:20:57 +00:00
|
|
|
Name: "getmessage",
|
|
|
|
Usage: "Get and print a message by its cid",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[messageCid]",
|
2019-10-09 07:26:48 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2020-01-15 21:24:01 +00:00
|
|
|
if !cctx.Args().Present() {
|
|
|
|
return fmt.Errorf("must pass a cid of a message to get")
|
|
|
|
}
|
|
|
|
|
2019-10-09 07:26:48 +00:00
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
c, err := cid.Decode(cctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to parse cid input: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mb, err := api.ChainReadObj(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to read object: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var i interface{}
|
|
|
|
m, err := types.DecodeMessage(mb)
|
|
|
|
if err != nil {
|
|
|
|
sm, err := types.DecodeSignedMessage(mb)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to decode object as a message: %w", err)
|
|
|
|
}
|
|
|
|
i = sm
|
|
|
|
} else {
|
|
|
|
i = m
|
|
|
|
}
|
|
|
|
|
|
|
|
enc, err := json.MarshalIndent(i, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(enc))
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2019-10-10 03:50:50 +00:00
|
|
|
|
2019-10-10 03:59:32 +00:00
|
|
|
var chainSetHeadCmd = &cli.Command{
|
2020-03-06 00:20:57 +00:00
|
|
|
Name: "sethead",
|
|
|
|
Usage: "manually set the local nodes head tipset (Caution: normally only used for recovery)",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[tipsetkey]",
|
2019-10-11 02:14:22 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "genesis",
|
|
|
|
Usage: "reset head to genesis",
|
|
|
|
},
|
2019-12-16 19:35:07 +00:00
|
|
|
&cli.Uint64Flag{
|
|
|
|
Name: "epoch",
|
|
|
|
Usage: "reset head to given epoch",
|
|
|
|
},
|
2019-10-11 02:14:22 +00:00
|
|
|
},
|
2019-10-10 03:50:50 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2019-12-16 19:35:07 +00:00
|
|
|
var ts *types.TipSet
|
2019-10-11 02:14:22 +00:00
|
|
|
|
2019-12-16 19:35:07 +00:00
|
|
|
if cctx.Bool("genesis") {
|
|
|
|
ts, err = api.ChainGetGenesis(ctx)
|
|
|
|
}
|
|
|
|
if ts == nil && cctx.IsSet("epoch") {
|
2020-02-24 17:32:02 +00:00
|
|
|
ts, err = api.ChainGetTipSetByHeight(ctx, abi.ChainEpoch(cctx.Uint64("epoch")), types.EmptyTSK)
|
2019-12-16 19:35:07 +00:00
|
|
|
}
|
|
|
|
if ts == nil {
|
|
|
|
ts, err = parseTipSet(api, ctx, cctx.Args().Slice())
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-10-10 03:50:50 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 19:35:07 +00:00
|
|
|
if ts == nil {
|
|
|
|
return fmt.Errorf("must pass cids for tipset to set as head")
|
2019-10-10 03:50:50 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
if err := api.ChainSetHead(ctx, ts.Key()); err != nil {
|
2019-10-10 03:50:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseTipSet(api api.FullNode, ctx context.Context, vals []string) (*types.TipSet, error) {
|
|
|
|
var headers []*types.BlockHeader
|
|
|
|
for _, c := range vals {
|
|
|
|
blkc, err := cid.Decode(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bh, err := api.ChainGetBlock(ctx, blkc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
headers = append(headers, bh)
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.NewTipSet(headers)
|
|
|
|
}
|
2019-10-11 06:25:25 +00:00
|
|
|
|
|
|
|
var chainListCmd = &cli.Command{
|
|
|
|
Name: "list",
|
|
|
|
Usage: "View a segment of the chain",
|
2019-10-12 23:10:24 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.Uint64Flag{Name: "height"},
|
2019-10-16 08:01:41 +00:00
|
|
|
&cli.IntFlag{Name: "count", Value: 30},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "format",
|
|
|
|
Usage: "specify the format to print out tipsets",
|
|
|
|
Value: "<height>: (<time>) <blocks>",
|
|
|
|
},
|
2019-10-12 23:10:24 +00:00
|
|
|
},
|
2019-10-11 06:25:25 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2019-10-13 00:55:45 +00:00
|
|
|
var head *types.TipSet
|
|
|
|
|
|
|
|
if cctx.IsSet("height") {
|
2020-02-24 17:32:02 +00:00
|
|
|
head, err = api.ChainGetTipSetByHeight(ctx, abi.ChainEpoch(cctx.Uint64("height")), types.EmptyTSK)
|
2019-10-13 00:55:45 +00:00
|
|
|
} else {
|
|
|
|
head, err = api.ChainHead(ctx)
|
|
|
|
}
|
2019-10-11 06:25:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-16 08:01:41 +00:00
|
|
|
count := cctx.Int("count")
|
2019-10-13 00:55:45 +00:00
|
|
|
if count < 1 {
|
|
|
|
return nil
|
2019-10-12 23:10:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 08:01:41 +00:00
|
|
|
tss := make([]*types.TipSet, 0, count)
|
|
|
|
tss = append(tss, head)
|
2019-10-12 23:10:24 +00:00
|
|
|
|
2019-10-16 08:01:41 +00:00
|
|
|
for i := 1; i < count; i++ {
|
2019-10-13 00:55:45 +00:00
|
|
|
if head.Height() == 0 {
|
2019-10-11 06:25:25 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2019-12-16 19:22:56 +00:00
|
|
|
head, err = api.ChainGetTipSet(ctx, head.Parents())
|
2019-10-11 06:25:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-16 08:01:41 +00:00
|
|
|
tss = append(tss, head)
|
2019-10-11 06:25:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := len(tss) - 1; i >= 0; i-- {
|
2019-10-16 08:01:41 +00:00
|
|
|
printTipSet(cctx.String("format"), tss[i])
|
2019-10-11 06:25:25 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2019-10-16 08:01:41 +00:00
|
|
|
|
2019-12-19 15:50:18 +00:00
|
|
|
var chainGetCmd = &cli.Command{
|
2020-03-06 00:20:57 +00:00
|
|
|
Name: "get",
|
|
|
|
Usage: "Get chain DAG node by path",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[path]",
|
2020-03-07 02:47:19 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "as-type",
|
|
|
|
Usage: "specify type to interpret output as",
|
|
|
|
},
|
2020-04-01 00:06:26 +00:00
|
|
|
&cli.BoolFlag{
|
2020-04-01 00:09:19 +00:00
|
|
|
Name: "verbose",
|
|
|
|
Value: false,
|
2020-04-01 00:06:26 +00:00
|
|
|
},
|
2020-03-07 02:47:19 +00:00
|
|
|
},
|
2019-12-19 15:50:18 +00:00
|
|
|
Description: `Get ipld node under a specified path:
|
|
|
|
|
|
|
|
lotus chain get /ipfs/[cid]/some/path
|
|
|
|
|
2020-04-01 00:06:26 +00:00
|
|
|
Path prefixes:
|
|
|
|
- /ipfs/[cid], /ipld/[cid] - traverse IPLD path
|
|
|
|
- /pstate - traverse from head.ParentStateRoot
|
|
|
|
|
2019-12-19 15:50:18 +00:00
|
|
|
Note:
|
|
|
|
You can use special path elements to traverse through some data structures:
|
|
|
|
- /ipfs/[cid]/@H:elem - get 'elem' from hamt
|
|
|
|
- /ipfs/[cid]/@Ha:t01 - get element under Addr(t01).Bytes
|
|
|
|
- /ipfs/[cid]/@A:10 - get 10th amt element
|
|
|
|
`,
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2020-04-01 00:06:26 +00:00
|
|
|
p := path.Clean(cctx.Args().First())
|
|
|
|
if strings.HasPrefix(p, "/pstate") {
|
|
|
|
p = p[len("/pstate"):]
|
|
|
|
head, err := api.ChainHead(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
p = "/ipfs/" + head.ParentState().String() + p
|
|
|
|
if cctx.Bool("verbose") {
|
|
|
|
fmt.Println(p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err := api.ChainGetNode(ctx, p)
|
2020-03-07 02:47:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
t := strings.ToLower(cctx.String("as-type"))
|
|
|
|
if t == "" {
|
|
|
|
b, err := json.MarshalIndent(obj.Obj, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(string(b))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var cbu cbg.CBORUnmarshaler
|
|
|
|
switch t {
|
2020-03-10 21:06:23 +00:00
|
|
|
case "raw":
|
|
|
|
cbu = nil
|
2020-03-07 02:47:19 +00:00
|
|
|
case "block":
|
|
|
|
cbu = new(types.BlockHeader)
|
|
|
|
case "message":
|
|
|
|
cbu = new(types.Message)
|
|
|
|
case "smessage", "signedmessage":
|
|
|
|
cbu = new(types.SignedMessage)
|
|
|
|
case "actor":
|
|
|
|
cbu = new(types.Actor)
|
2020-03-09 03:09:45 +00:00
|
|
|
case "amt":
|
|
|
|
return handleAmt(ctx, api, obj.Cid)
|
|
|
|
case "hamt-epoch":
|
|
|
|
return handleHamtEpoch(ctx, api, obj.Cid)
|
2020-03-09 06:45:24 +00:00
|
|
|
case "hamt-address":
|
|
|
|
return handleHamtAddress(ctx, api, obj.Cid)
|
2020-03-09 03:09:45 +00:00
|
|
|
case "cronevent":
|
|
|
|
cbu = new(power.CronEvent)
|
2020-04-01 01:34:23 +00:00
|
|
|
case "account-state":
|
|
|
|
cbu = new(account.State)
|
2020-03-07 02:47:19 +00:00
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown type: %q", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
raw, err := api.ChainReadObj(ctx, obj.Cid)
|
2019-12-19 15:50:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-10 21:06:23 +00:00
|
|
|
if cbu == nil {
|
|
|
|
fmt.Printf("%x", raw)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-07 02:47:19 +00:00
|
|
|
if err := cbu.UnmarshalCBOR(bytes.NewReader(raw)); err != nil {
|
|
|
|
return fmt.Errorf("failed to unmarshal as %q", t)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.MarshalIndent(cbu, "", "\t")
|
2019-12-19 15:50:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Println(string(b))
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-03-09 03:09:45 +00:00
|
|
|
type apiIpldStore struct {
|
|
|
|
ctx context.Context
|
|
|
|
api api.FullNode
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *apiIpldStore) Context() context.Context {
|
|
|
|
return ht.ctx
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *apiIpldStore) Get(ctx context.Context, c cid.Cid, out interface{}) error {
|
|
|
|
raw, err := ht.api.ChainReadObj(ctx, c)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cu, ok := out.(cbg.CBORUnmarshaler)
|
|
|
|
if ok {
|
|
|
|
if err := cu.UnmarshalCBOR(bytes.NewReader(raw)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Object does not implement CBORUnmarshaler")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ht *apiIpldStore) Put(ctx context.Context, v interface{}) (cid.Cid, error) {
|
|
|
|
panic("No mutations allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleAmt(ctx context.Context, api api.FullNode, r cid.Cid) error {
|
|
|
|
s := &apiIpldStore{ctx, api}
|
|
|
|
mp := adt.AsArray(s, r)
|
|
|
|
return mp.ForEach(nil, func(key int64) error {
|
|
|
|
fmt.Printf("%d\n", key)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleHamtEpoch(ctx context.Context, api api.FullNode, r cid.Cid) error {
|
|
|
|
s := &apiIpldStore{ctx, api}
|
|
|
|
mp := adt.AsMap(s, r)
|
|
|
|
return mp.ForEach(nil, func(key string) error {
|
|
|
|
ik, err := adt.ParseIntKey(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%d\n", ik)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-03-09 06:45:24 +00:00
|
|
|
func handleHamtAddress(ctx context.Context, api api.FullNode, r cid.Cid) error {
|
|
|
|
s := &apiIpldStore{ctx, api}
|
|
|
|
mp := adt.AsMap(s, r)
|
|
|
|
return mp.ForEach(nil, func(key string) error {
|
|
|
|
addr, err := address.NewFromBytes([]byte(key))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("%s\n", addr)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-16 08:01:41 +00:00
|
|
|
func printTipSet(format string, ts *types.TipSet) {
|
|
|
|
format = strings.ReplaceAll(format, "<height>", fmt.Sprint(ts.Height()))
|
|
|
|
format = strings.ReplaceAll(format, "<time>", time.Unix(int64(ts.MinTimestamp()), 0).Format(time.Stamp))
|
|
|
|
blks := "[ "
|
|
|
|
for _, b := range ts.Blocks() {
|
|
|
|
blks += fmt.Sprintf("%s: %s,", b.Cid(), b.Miner)
|
|
|
|
}
|
|
|
|
blks += " ]"
|
2020-01-30 22:01:15 +00:00
|
|
|
|
|
|
|
sCids := make([]string, 0, len(blks))
|
|
|
|
|
|
|
|
for _, c := range ts.Cids() {
|
|
|
|
sCids = append(sCids, c.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
format = strings.ReplaceAll(format, "<tipset>", strings.Join(sCids, ","))
|
2019-10-16 08:01:41 +00:00
|
|
|
format = strings.ReplaceAll(format, "<blocks>", blks)
|
|
|
|
format = strings.ReplaceAll(format, "<weight>", fmt.Sprint(ts.Blocks()[0].ParentWeight))
|
|
|
|
|
|
|
|
fmt.Println(format)
|
|
|
|
}
|
2020-01-16 18:05:07 +00:00
|
|
|
|
2020-01-30 21:30:21 +00:00
|
|
|
var chainBisectCmd = &cli.Command{
|
2020-03-06 00:20:57 +00:00
|
|
|
Name: "bisect",
|
|
|
|
Usage: "bisect chain for an event",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[minHeight maxHeight path shellCommand <shellCommandArgs (if any)>]",
|
2020-01-30 21:30:21 +00:00
|
|
|
Description: `Bisect the chain state tree:
|
|
|
|
|
2020-02-04 03:40:49 +00:00
|
|
|
lotus chain bisect [min height] [max height] '1/2/3/state/path' 'shell command' 'args'
|
2020-01-30 21:30:21 +00:00
|
|
|
|
2020-02-04 03:40:49 +00:00
|
|
|
Returns the first tipset in which condition is true
|
2020-01-30 21:30:21 +00:00
|
|
|
v
|
|
|
|
[start] FFFFFFFTTT [end]
|
|
|
|
|
|
|
|
Example: find height at which deal ID 100 000 appeared
|
2020-02-04 03:40:49 +00:00
|
|
|
- lotus chain bisect 1 32000 '@Ha:t03/1' jq -e '.[2] > 100000'
|
2020-01-30 21:30:21 +00:00
|
|
|
|
|
|
|
For special path elements see 'chain get' help
|
|
|
|
`,
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2020-02-04 03:40:49 +00:00
|
|
|
if cctx.Args().Len() < 4 {
|
|
|
|
return xerrors.New("need at least 4 args")
|
2020-01-30 21:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
start, err := strconv.ParseUint(cctx.Args().Get(0), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
end, err := strconv.ParseUint(cctx.Args().Get(1), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
subPath := cctx.Args().Get(2)
|
|
|
|
|
2020-02-24 17:32:02 +00:00
|
|
|
highest, err := api.ChainGetTipSetByHeight(ctx, abi.ChainEpoch(end), types.EmptyTSK)
|
2020-01-30 21:30:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
prev := highest.Height()
|
|
|
|
|
|
|
|
for {
|
|
|
|
mid := (start + end) / 2
|
2020-02-04 06:55:57 +00:00
|
|
|
if end-start == 1 {
|
2020-01-30 21:30:21 +00:00
|
|
|
mid = end
|
|
|
|
start = end
|
|
|
|
}
|
|
|
|
|
2020-02-24 17:32:02 +00:00
|
|
|
midTs, err := api.ChainGetTipSetByHeight(ctx, abi.ChainEpoch(mid), highest.Key())
|
2020-01-30 21:30:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
path := "/ipld/" + midTs.ParentState().String() + "/" + subPath
|
|
|
|
fmt.Printf("* Testing %d (%d - %d) (%s): ", mid, start, end, path)
|
|
|
|
|
|
|
|
nd, err := api.ChainGetNode(ctx, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.MarshalIndent(nd, "", "\t")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-04 03:40:49 +00:00
|
|
|
cmd := exec.CommandContext(ctx, cctx.Args().Get(3), cctx.Args().Slice()[4:]...)
|
2020-01-30 21:30:21 +00:00
|
|
|
cmd.Stdin = bytes.NewReader(b)
|
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
cmd.Stdout = &out
|
|
|
|
|
2020-02-04 03:40:49 +00:00
|
|
|
switch cmd.Run().(type) {
|
|
|
|
case nil:
|
2020-01-30 21:30:21 +00:00
|
|
|
// it's lower
|
|
|
|
end = mid
|
|
|
|
highest = midTs
|
2020-02-04 03:40:49 +00:00
|
|
|
fmt.Println("true")
|
|
|
|
case *exec.ExitError:
|
2020-01-30 21:30:21 +00:00
|
|
|
start = mid
|
2020-02-04 03:40:49 +00:00
|
|
|
fmt.Println("false")
|
|
|
|
default:
|
|
|
|
return err
|
2020-01-30 21:30:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if start == end {
|
|
|
|
if strings.TrimSpace(out.String()) == "true" {
|
|
|
|
fmt.Println(midTs.Height())
|
|
|
|
} else {
|
|
|
|
fmt.Println(prev)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-08 02:18:32 +00:00
|
|
|
prev = abi.ChainEpoch(mid)
|
2020-01-30 21:30:21 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-01-20 23:51:02 +00:00
|
|
|
var chainExportCmd = &cli.Command{
|
2020-03-06 00:20:57 +00:00
|
|
|
Name: "export",
|
|
|
|
Usage: "export chain to a car file",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[outputPath]",
|
2020-01-21 01:53:55 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "tipset",
|
|
|
|
},
|
|
|
|
},
|
2020-01-16 18:05:07 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
2020-01-20 23:51:02 +00:00
|
|
|
if !cctx.Args().Present() {
|
|
|
|
return fmt.Errorf("must specify filename to export chain to")
|
|
|
|
}
|
|
|
|
|
2020-01-21 01:53:55 +00:00
|
|
|
fi, err := os.Create(cctx.Args().First())
|
2020-01-20 23:51:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-21 01:53:55 +00:00
|
|
|
defer fi.Close()
|
2020-01-20 23:51:02 +00:00
|
|
|
|
2020-03-07 00:46:20 +00:00
|
|
|
ts, err := LoadTipSet(ctx, cctx, api)
|
2020-01-20 23:51:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
stream, err := api.ChainExport(ctx, ts.Key())
|
2020-01-20 23:51:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for b := range stream {
|
|
|
|
_, err := fi.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-01-16 18:05:07 +00:00
|
|
|
},
|
|
|
|
}
|
2019-12-18 15:37:47 +00:00
|
|
|
|
|
|
|
var slashConsensusFault = &cli.Command{
|
2020-03-06 00:20:57 +00:00
|
|
|
Name: "slash-consensus",
|
|
|
|
Usage: "Report consensus fault",
|
2020-03-04 21:46:00 +00:00
|
|
|
ArgsUsage: "[blockCid1 blockCid2]",
|
2019-12-18 15:37:47 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
c1, err := cid.Parse(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("parsing cid 1: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b1, err := api.ChainGetBlock(ctx, c1)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting block 1: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-02-01 01:07:37 +00:00
|
|
|
c2, err := cid.Parse(cctx.Args().Get(1))
|
2019-12-18 15:37:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("parsing cid 2: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
b2, err := api.ChainGetBlock(ctx, c2)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting block 2: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
def, err := api.WalletDefaultAddress(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-13 03:50:37 +00:00
|
|
|
bh1, err := cborutil.Dump(b1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
bh2, err := cborutil.Dump(b2)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
params, err := actors.SerializeParams(&power.ReportConsensusFaultParams{
|
|
|
|
BlockHeader1: bh1,
|
|
|
|
BlockHeader2: bh2,
|
2019-12-18 15:37:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
msg := &types.Message{
|
2020-02-25 20:54:58 +00:00
|
|
|
To: builtin.StoragePowerActorAddr,
|
2019-12-18 15:37:47 +00:00
|
|
|
From: def,
|
|
|
|
Value: types.NewInt(0),
|
|
|
|
GasPrice: types.NewInt(1),
|
2020-03-18 20:45:37 +00:00
|
|
|
GasLimit: 10000000,
|
2020-02-13 03:50:37 +00:00
|
|
|
Method: builtin.MethodsPower.ReportConsensusFault,
|
2019-12-18 15:37:47 +00:00
|
|
|
Params: params,
|
|
|
|
}
|
|
|
|
|
|
|
|
smsg, err := api.MpoolPushMessage(ctx, msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(smsg.Cid())
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|