lotus/cli/state.go

254 lines
4.8 KiB
Go
Raw Normal View History

package cli
import (
"fmt"
"github.com/filecoin-project/lotus/chain/address"
"github.com/filecoin-project/lotus/chain/types"
2019-09-20 06:46:15 +00:00
2019-09-19 20:25:18 +00:00
"github.com/ipfs/go-cid"
2019-09-20 06:46:15 +00:00
"gopkg.in/urfave/cli.v2"
)
var stateCmd = &cli.Command{
Name: "state",
Usage: "Interact with and query filecoin chain state",
Subcommands: []*cli.Command{
statePowerCmd,
stateSectorsCmd,
stateProvingSetCmd,
2019-09-27 14:53:34 +00:00
statePledgeCollateralCmd,
stateListActorsCmd,
stateListMinersCmd,
},
}
var statePowerCmd = &cli.Command{
Name: "power",
Usage: "Query network or miner power",
Action: func(cctx *cli.Context) error {
2019-10-03 18:12:30 +00:00
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer closer()
ctx := ReqContext(cctx)
var maddr address.Address
if cctx.Args().Present() {
maddr, err = address.NewFromString(cctx.Args().First())
if err != nil {
return err
}
}
power, err := api.StateMinerPower(ctx, maddr, nil)
if err != nil {
return err
}
res := power.TotalPower
if cctx.Args().Present() {
res = power.MinerPower
}
fmt.Println(res.String())
return nil
},
}
var stateSectorsCmd = &cli.Command{
Name: "sectors",
Usage: "Query the sector set of a miner",
Action: func(cctx *cli.Context) error {
2019-10-03 18:12:30 +00:00
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer closer()
ctx := ReqContext(cctx)
if !cctx.Args().Present() {
return fmt.Errorf("must specify miner to list sectors for")
}
maddr, err := address.NewFromString(cctx.Args().First())
if err != nil {
return err
}
2019-10-27 10:27:21 +00:00
sectors, err := api.StateMinerSectors(ctx, maddr, nil)
if err != nil {
return err
}
for _, s := range sectors {
fmt.Printf("%d: %x %x\n", s.SectorID, s.CommR, s.CommD)
}
return nil
},
}
var stateProvingSetCmd = &cli.Command{
Name: "proving",
Usage: "Query the proving set of a miner",
Action: func(cctx *cli.Context) error {
2019-10-03 18:12:30 +00:00
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer closer()
ctx := ReqContext(cctx)
if !cctx.Args().Present() {
return fmt.Errorf("must specify miner to list sectors for")
}
maddr, err := address.NewFromString(cctx.Args().First())
if err != nil {
return err
}
sectors, err := api.StateMinerProvingSet(ctx, maddr, nil)
if err != nil {
return err
}
for _, s := range sectors {
fmt.Printf("%d: %x %x\n", s.SectorID, s.CommR, s.CommD)
}
return nil
},
}
2019-09-19 20:25:18 +00:00
var stateReplaySetCmd = &cli.Command{
Name: "replay",
Usage: "Replay a particular message within a tipset",
Action: func(cctx *cli.Context) error {
if cctx.Args().Len() < 2 {
fmt.Println("usage: <tipset> <message cid>")
fmt.Println("The last cid passed will be used as the message CID")
fmt.Println("All preceding ones will be used as the tipset")
return nil
}
args := cctx.Args().Slice()
mcid, err := cid.Decode(args[len(args)-1])
if err != nil {
return fmt.Errorf("message cid was invalid: %s", err)
}
var tscids []cid.Cid
for _, s := range args[:len(args)-1] {
c, err := cid.Decode(s)
if err != nil {
return fmt.Errorf("tipset cid was invalid: %s", err)
}
tscids = append(tscids, c)
}
2019-10-03 18:12:30 +00:00
api, closer, err := GetFullNodeAPI(cctx)
2019-09-19 20:25:18 +00:00
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer closer()
2019-09-19 20:25:18 +00:00
ctx := ReqContext(cctx)
var headers []*types.BlockHeader
for _, c := range tscids {
h, err := api.ChainGetBlock(ctx, c)
if err != nil {
return err
}
headers = append(headers, h)
}
ts, err := types.NewTipSet(headers)
if err != nil {
return err
}
api.StateReplay(ctx, ts, mcid)
return nil
},
}
2019-09-27 14:53:34 +00:00
var statePledgeCollateralCmd = &cli.Command{
Name: "pledge-collateral",
Usage: "Get minimum miner pledge collateral",
Action: func(cctx *cli.Context) error {
2019-10-03 18:12:30 +00:00
api, closer, err := GetFullNodeAPI(cctx)
2019-09-27 14:53:34 +00:00
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer closer()
2019-09-27 14:53:34 +00:00
ctx := ReqContext(cctx)
coll, err := api.StatePledgeCollateral(ctx, nil)
if err != nil {
return err
}
fmt.Println(types.FIL(coll).String())
2019-09-27 14:53:34 +00:00
return nil
},
}
var stateListMinersCmd = &cli.Command{
Name: "list-miners",
Usage: "list all miners in the network",
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
miners, err := api.StateListMiners(ctx, nil)
if err != nil {
return err
}
for _, m := range miners {
fmt.Println(m.String())
}
return nil
},
}
var stateListActorsCmd = &cli.Command{
Name: "list-actors",
Usage: "list all actors in the network",
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
actors, err := api.StateListActors(ctx, nil)
if err != nil {
return err
}
for _, a := range actors {
fmt.Println(a.String())
}
return nil
},
}