lotus/cli/chain.go
2019-08-07 20:01:22 -07:00

116 lines
2.0 KiB
Go

package cli
import (
"encoding/json"
"fmt"
"gopkg.in/urfave/cli.v2"
types "github.com/filecoin-project/go-lotus/chain/types"
cid "github.com/ipfs/go-cid"
)
var chainCmd = &cli.Command{
Name: "chain",
Usage: "Interact with filecoin blockchain",
Subcommands: []*cli.Command{
chainHeadCmd,
chainGetBlock,
},
}
var chainHeadCmd = &cli.Command{
Name: "head",
Usage: "Print chain head",
Action: func(cctx *cli.Context) error {
api, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
ctx := ReqContext(cctx)
head, err := api.ChainHead(ctx)
if err != nil {
return err
}
for _, c := range head.Cids() {
fmt.Println(c)
}
return nil
},
}
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 {
api, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
ctx := ReqContext(cctx)
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
}
recpts, err := api.ChainGetBlockReceipts(ctx, bcid)
if err != nil {
return err
}
cblock := struct {
types.BlockHeader
BlsMessages []*types.Message
SecpkMessages []*types.SignedMessage
MessageReceipts []*types.MessageReceipt
}{}
cblock.BlockHeader = *blk
cblock.BlsMessages = msgs.BlsMessages
cblock.SecpkMessages = msgs.SecpkMessages
cblock.MessageReceipts = recpts
out, err := json.MarshalIndent(cblock, "", " ")
if err != nil {
return err
}
fmt.Println(string(out))
return nil
},
}