From d7528557dfac0e4ae91cf24bc4e10ccb1a272979 Mon Sep 17 00:00:00 2001 From: Aayush Rajasekaran Date: Sun, 12 Dec 2021 22:05:43 -0500 Subject: [PATCH 1/3] Shed: Add a util to list terminated deals --- cmd/lotus-shed/main.go | 1 + cmd/lotus-shed/terminations.go | 173 +++++++++++++++++++++++++++++++++ 2 files changed, 174 insertions(+) create mode 100644 cmd/lotus-shed/terminations.go diff --git a/cmd/lotus-shed/main.go b/cmd/lotus-shed/main.go index d35fb56dd8b..d189313124f 100644 --- a/cmd/lotus-shed/main.go +++ b/cmd/lotus-shed/main.go @@ -65,6 +65,7 @@ func main() { fr32Cmd, chainCmd, balancerCmd, + terminationsCmd, } app := &cli.App{ diff --git a/cmd/lotus-shed/terminations.go b/cmd/lotus-shed/terminations.go new file mode 100644 index 00000000000..0cc0800e429 --- /dev/null +++ b/cmd/lotus-shed/terminations.go @@ -0,0 +1,173 @@ +package main + +import ( + "bytes" + "context" + "fmt" + "io" + + "github.com/filecoin-project/lotus/chain/types" + + "github.com/filecoin-project/lotus/chain/actors/builtin/market" + + "github.com/filecoin-project/lotus/chain/actors" + "github.com/filecoin-project/lotus/chain/actors/adt" + "github.com/filecoin-project/lotus/chain/actors/builtin/miner" + "github.com/filecoin-project/lotus/chain/consensus/filcns" + "github.com/filecoin-project/lotus/chain/state" + "github.com/filecoin-project/lotus/chain/store" + "github.com/filecoin-project/lotus/node/repo" + miner2 "github.com/filecoin-project/specs-actors/actors/builtin/miner" + "github.com/ipfs/go-cid" + cbor "github.com/ipfs/go-ipld-cbor" + "github.com/urfave/cli/v2" +) + +var terminationsCmd = &cli.Command{ + Name: "terminations", + Description: "Lists terminated deals from the past 2 days", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "repo", + Value: "~/.lotus", + }, + }, + Action: func(cctx *cli.Context) error { + ctx := context.TODO() + + if !cctx.Args().Present() { + return fmt.Errorf("must pass block cid") + } + + blkCid, err := cid.Decode(cctx.Args().First()) + if err != nil { + return fmt.Errorf("failed to parse input: %w", err) + } + + fsrepo, err := repo.NewFS(cctx.String("repo")) + if err != nil { + return err + } + + lkrepo, err := fsrepo.Lock(repo.FullNode) + if err != nil { + return err + } + + defer lkrepo.Close() //nolint:errcheck + + bs, err := lkrepo.Blockstore(ctx, repo.UniversalBlockstore) + if err != nil { + return fmt.Errorf("failed to open blockstore: %w", err) + } + + defer func() { + if c, ok := bs.(io.Closer); ok { + if err := c.Close(); err != nil { + log.Warnf("failed to close blockstore: %s", err) + } + } + }() + + mds, err := lkrepo.Datastore(context.Background(), "/metadata") + if err != nil { + return err + } + + cs := store.NewChainStore(bs, bs, mds, filcns.Weight, nil) + defer cs.Close() //nolint:errcheck + + cst := cbor.NewCborStore(bs) + store := adt.WrapStore(ctx, cst) + + blk, err := cs.GetBlock(blkCid) + if err != nil { + return err + } + + minerCode, err := miner.GetActorCodeID(actors.Version6) + if err != nil { + return err + } + + for i := 0; i < 2880*2; i++ { + pts, err := cs.LoadTipSet(types.NewTipSetKey(blk.Parents...)) + if err != nil { + return err + } + + blk = pts.Blocks()[0] + + msgs, err := cs.MessagesForTipset(pts) + if err != nil { + return err + } + + for _, v := range msgs { + msg := v.VMMessage() + if msg.Method != miner.Methods.TerminateSectors { + continue + } + + tree, err := state.LoadStateTree(cst, blk.ParentStateRoot) + if err != nil { + return err + } + + minerAct, err := tree.GetActor(msg.To) + if err != nil { + return err + } + + if minerAct.Code != minerCode { + continue + } + + minerSt, err := miner.Load(store, minerAct) + if err != nil { + return err + } + + marketAct, err := tree.GetActor(market.Address) + if err != nil { + return err + } + + marketSt, err := market.Load(store, marketAct) + if err != nil { + return err + } + + proposals, err := marketSt.Proposals() + if err != nil { + return err + } + + var termParams miner2.TerminateSectorsParams + err = termParams.UnmarshalCBOR(bytes.NewBuffer(msg.Params)) + if err != nil { + return err + } + + for _, t := range termParams.Terminations { + sectors, err := minerSt.LoadSectors(&t.Sectors) + if err != nil { + return err + } + + for _, sector := range sectors { + for _, deal := range sector.DealIDs { + prop, find, err := proposals.Get(deal) + if err != nil || !find { + return err + } + fmt.Printf("%s, %d, %d, %s, %s\n", msg.To, sector.SectorNumber, deal, prop.PieceCID, prop.Label) + } + } + } + } + } + + return nil + }, +} From 8c4c2eeffc4b5a53d7de5a5a8020acb44e681125 Mon Sep 17 00:00:00 2001 From: Jennifer Wang Date: Mon, 13 Dec 2021 03:35:56 -0500 Subject: [PATCH 2/3] add client address --- cmd/lotus-shed/terminations.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/lotus-shed/terminations.go b/cmd/lotus-shed/terminations.go index 0cc0800e429..c28d595b4e1 100644 --- a/cmd/lotus-shed/terminations.go +++ b/cmd/lotus-shed/terminations.go @@ -161,7 +161,7 @@ var terminationsCmd = &cli.Command{ if err != nil || !find { return err } - fmt.Printf("%s, %d, %d, %s, %s\n", msg.To, sector.SectorNumber, deal, prop.PieceCID, prop.Label) + fmt.Printf("%s, %d, %d, %s, %s, %s\n", msg.To, sector.SectorNumber, deal, prop.Client, prop.PieceCID, prop.Label) } } } From fac769ff17f03731520476bd8a0db49a9a3a4081 Mon Sep 17 00:00:00 2001 From: Jennifer Wang Date: Mon, 13 Dec 2021 04:18:31 -0500 Subject: [PATCH 3/3] i forced pushed and brought a bug that was fixed back so im fixing it back --- cmd/lotus-shed/terminations.go | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/cmd/lotus-shed/terminations.go b/cmd/lotus-shed/terminations.go index c28d595b4e1..a209e459f1f 100644 --- a/cmd/lotus-shed/terminations.go +++ b/cmd/lotus-shed/terminations.go @@ -5,12 +5,15 @@ import ( "context" "fmt" "io" + "strconv" + "github.com/filecoin-project/lotus/chain/actors/builtin" + + "github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/actors/builtin/market" - "github.com/filecoin-project/lotus/chain/actors" "github.com/filecoin-project/lotus/chain/actors/adt" "github.com/filecoin-project/lotus/chain/actors/builtin/miner" "github.com/filecoin-project/lotus/chain/consensus/filcns" @@ -26,6 +29,7 @@ import ( var terminationsCmd = &cli.Command{ Name: "terminations", Description: "Lists terminated deals from the past 2 days", + ArgsUsage: "[block to look back from] [lookback period (epochs)]", Flags: []cli.Flag{ &cli.StringFlag{ Name: "repo", @@ -35,8 +39,8 @@ var terminationsCmd = &cli.Command{ Action: func(cctx *cli.Context) error { ctx := context.TODO() - if !cctx.Args().Present() { - return fmt.Errorf("must pass block cid") + if cctx.NArg() != 2 { + return fmt.Errorf("must pass block cid && lookback period") } blkCid, err := cid.Decode(cctx.Args().First()) @@ -85,12 +89,14 @@ var terminationsCmd = &cli.Command{ return err } - minerCode, err := miner.GetActorCodeID(actors.Version6) + lbp, err := strconv.Atoi(cctx.Args().Get(1)) if err != nil { - return err + return fmt.Errorf("failed to parse input: %w", err) } - for i := 0; i < 2880*2; i++ { + cutoff := blk.Height - abi.ChainEpoch(lbp) + + for blk.Height > cutoff { pts, err := cs.LoadTipSet(types.NewTipSetKey(blk.Parents...)) if err != nil { return err @@ -119,7 +125,7 @@ var terminationsCmd = &cli.Command{ return err } - if minerAct.Code != minerCode { + if !builtin.IsStorageMinerActor(minerAct.Code) { continue } @@ -158,10 +164,12 @@ var terminationsCmd = &cli.Command{ for _, sector := range sectors { for _, deal := range sector.DealIDs { prop, find, err := proposals.Get(deal) - if err != nil || !find { + if err != nil { return err } - fmt.Printf("%s, %d, %d, %s, %s, %s\n", msg.To, sector.SectorNumber, deal, prop.Client, prop.PieceCID, prop.Label) + if find { + fmt.Printf("%s, %d, %d, %s, %s, %s\n", msg.To, sector.SectorNumber, deal, prop.Client, prop.PieceCID, prop.Label) + } } } }