lotus/cli/chain.go

109 lines
1.8 KiB
Go
Raw Normal View History

2019-07-09 15:19:27 +00:00
package cli
import (
2019-07-23 00:54:27 +00:00
"encoding/json"
2019-07-09 15:19:27 +00:00
"fmt"
"gopkg.in/urfave/cli.v2"
2019-07-23 00:54:27 +00:00
types "github.com/filecoin-project/go-lotus/chain/types"
2019-07-23 00:54:27 +00:00
cid "github.com/ipfs/go-cid"
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-07-09 15:19:27 +00:00
},
}
var chainHeadCmd = &cli.Command{
Name: "head",
Usage: "Print chain head",
Action: func(cctx *cli.Context) error {
2019-07-18 23:16:23 +00:00
api, err := GetAPI(cctx)
2019-07-10 17:28:49 +00:00
if err != nil {
return err
}
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{
Name: "getblock",
Usage: "Get a block and print its details",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "raw",
Usage: "print just the raw block header",
},
},
Action: func(cctx *cli.Context) error {
2019-07-18 23:16:23 +00:00
api, err := GetAPI(cctx)
2019-07-23 00:54:27 +00:00
if err != nil {
return err
}
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 {
return err
}
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 {
return err
}
cblock := struct {
types.BlockHeader
2019-08-02 03:51:34 +00:00
BlsMessages []*types.Message
SecpkMessages []*types.SignedMessage
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-07-23 00:54:27 +00:00
out, err := json.MarshalIndent(cblock, "", " ")
if err != nil {
return err
}
fmt.Println(string(out))
return nil
},
}