2021-12-13 03:05:43 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-12-13 09:18:31 +00:00
|
|
|
"strconv"
|
2021-12-13 03:05:43 +00:00
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
|
|
|
cbor "github.com/ipfs/go-ipld-cbor"
|
|
|
|
"github.com/urfave/cli/v2"
|
2021-12-13 09:18:31 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/builtin"
|
2023-11-14 00:06:11 +00:00
|
|
|
miner2 "github.com/filecoin-project/specs-actors/actors/builtin/miner"
|
|
|
|
|
2021-12-13 03:05:43 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/adt"
|
2022-06-14 15:00:51 +00:00
|
|
|
lbuiltin "github.com/filecoin-project/lotus/chain/actors/builtin"
|
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/market"
|
2021-12-13 03:05:43 +00:00
|
|
|
"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"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2022-09-14 19:38:15 +00:00
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
2021-12-13 03:05:43 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
var terminationsCmd = &cli.Command{
|
|
|
|
Name: "terminations",
|
|
|
|
Description: "Lists terminated deals from the past 2 days",
|
2021-12-13 09:18:31 +00:00
|
|
|
ArgsUsage: "[block to look back from] [lookback period (epochs)]",
|
2021-12-13 03:05:43 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
2023-02-15 16:09:24 +00:00
|
|
|
Name: "repo",
|
|
|
|
Value: "~/.lotus",
|
2021-12-13 03:05:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
ctx := context.TODO()
|
|
|
|
|
2021-12-13 09:18:31 +00:00
|
|
|
if cctx.NArg() != 2 {
|
2022-09-14 19:38:15 +00:00
|
|
|
return lcli.IncorrectNumArgs(cctx)
|
2021-12-13 03:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-12-17 12:05:44 +00:00
|
|
|
blk, err := cs.GetBlock(ctx, blkCid)
|
2021-12-13 03:05:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-12-13 09:18:31 +00:00
|
|
|
lbp, err := strconv.Atoi(cctx.Args().Get(1))
|
2021-12-13 03:05:43 +00:00
|
|
|
if err != nil {
|
2021-12-13 09:18:31 +00:00
|
|
|
return fmt.Errorf("failed to parse input: %w", err)
|
2021-12-13 03:05:43 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 09:18:31 +00:00
|
|
|
cutoff := blk.Height - abi.ChainEpoch(lbp)
|
|
|
|
|
|
|
|
for blk.Height > cutoff {
|
2021-12-17 12:05:44 +00:00
|
|
|
pts, err := cs.LoadTipSet(ctx, types.NewTipSetKey(blk.Parents...))
|
2021-12-13 03:05:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
blk = pts.Blocks()[0]
|
|
|
|
|
2021-12-17 12:05:44 +00:00
|
|
|
msgs, err := cs.MessagesForTipset(ctx, pts)
|
2021-12-13 03:05:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range msgs {
|
|
|
|
msg := v.VMMessage()
|
2022-04-20 21:34:28 +00:00
|
|
|
if msg.Method != builtin.MethodsMiner.TerminateSectors {
|
2021-12-13 03:05:43 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
tree, err := state.LoadStateTree(cst, blk.ParentStateRoot)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
minerAct, err := tree.GetActor(msg.To)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
if !lbuiltin.IsStorageMinerActor(minerAct.Code) {
|
2021-12-13 03:05:43 +00:00
|
|
|
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)
|
2021-12-13 09:18:31 +00:00
|
|
|
if err != nil {
|
2021-12-13 03:05:43 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-04-08 14:03:14 +00:00
|
|
|
label, err := prop.Label.ToString()
|
|
|
|
if err != nil {
|
|
|
|
labelBs, err := prop.Label.ToBytes()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
label = string(labelBs)
|
|
|
|
}
|
2021-12-13 09:18:31 +00:00
|
|
|
if find {
|
2022-04-08 14:03:14 +00:00
|
|
|
fmt.Printf("%s, %d, %d, %s, %s, %s\n", msg.To, sector.SectorNumber, deal, prop.Client, prop.PieceCID, label)
|
2021-12-13 09:18:31 +00:00
|
|
|
}
|
2021-12-13 03:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|