From 023cec4d7fa438bceda331dbcc046f7a094ecfc4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=8B=87?= Date: Fri, 16 Oct 2020 12:18:39 +0800 Subject: [PATCH 1/2] add support for teminate secotrs --- cmd/lotus-storage-miner/proving.go | 109 +++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) diff --git a/cmd/lotus-storage-miner/proving.go b/cmd/lotus-storage-miner/proving.go index 377b81d328..d0383ec19d 100644 --- a/cmd/lotus-storage-miner/proving.go +++ b/cmd/lotus-storage-miner/proving.go @@ -4,17 +4,24 @@ import ( "fmt" "os" "strconv" + "strings" "text/tabwriter" "github.com/fatih/color" "github.com/urfave/cli/v2" "golang.org/x/xerrors" + "github.com/filecoin-project/go-bitfield" + "github.com/filecoin-project/go-state-types/abi" + "github.com/filecoin-project/go-state-types/big" "github.com/filecoin-project/lotus/api/apibstore" + "github.com/filecoin-project/lotus/chain/actors" "github.com/filecoin-project/lotus/chain/actors/builtin/miner" "github.com/filecoin-project/lotus/chain/store" "github.com/filecoin-project/lotus/chain/types" lcli "github.com/filecoin-project/lotus/cli" + "github.com/filecoin-project/specs-actors/actors/builtin" + miner0 "github.com/filecoin-project/specs-actors/v2/actors/builtin/miner" ) var provingCmd = &cli.Command{ @@ -25,6 +32,7 @@ var provingCmd = &cli.Command{ provingDeadlinesCmd, provingDeadlineInfoCmd, provingFaultsCmd, + provingTeminateCmd, }, } @@ -371,3 +379,104 @@ var provingDeadlineInfoCmd = &cli.Command{ return nil }, } + +var provingTeminateCmd = &cli.Command{ + Name: "terminate", + Usage: "terminate specied sector early", + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "sectors", + Usage: "specied sectors[xx,xx,xx]", + }, + }, + Action: func(cctx *cli.Context) error { + + nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx) + if err != nil { + return err + } + defer closer() + + api, acloser, err := lcli.GetFullNodeAPI(cctx) + if err != nil { + return err + } + defer acloser() + + ctx := lcli.ReqContext(cctx) + + maddr, err := nodeApi.ActorAddress(ctx) + if err != nil { + return err + } + + mi, err := api.StateMinerInfo(ctx, maddr, types.EmptyTSK) + if err != nil { + return err + } + + if cctx.String("sectors") == "" { + return xerrors.Errorf("sectors must be specied") + } + + var sectorStr []string + if strings.Contains(cctx.String("sectors"), ",") { + sectorStr = strings.Split(cctx.String("sectors"), ",") + } else { + sectorStr = append(sectorStr, cctx.String("sectors")) + } + + if len(sectorStr) == 0 { + return xerrors.Errorf("sectors length is empty") + } + + param := []miner0.TerminationDeclaration{} + for _, s := range sectorStr { + sint, err := strconv.ParseUint(s, 10, 64) + if err != nil { + return err + } + + sectorbit := bitfield.New() + sectorbit.Set(sint) + + loca, err := api.StateSectorPartition(ctx, maddr, abi.SectorNumber(sint), types.EmptyTSK) + if err != nil { + return xerrors.Errorf("get sector partition %s", err) + } + + para := miner0.TerminationDeclaration{ + Deadline: loca.Deadline, + Partition: loca.Partition, + Sectors: sectorbit, + } + + param = append(param, para) + } + + paras := &miner0.TerminateSectorsParams{ + Terminations: param, + } + + sp, err := actors.SerializeParams(paras) + if err != nil { + return xerrors.Errorf("serializing params: %w", err) + } + + smsg, err := api.MpoolPushMessage(ctx, &types.Message{ + From: mi.Owner, + To: maddr, + Method: builtin.MethodsMiner.TerminateSectors, + + Value: big.Zero(), + Params: sp, + }, nil) + if err != nil { + return xerrors.Errorf("mpool push: %w", err) + } + + fmt.Println("Message CID:", smsg.Cid()) + + return nil + }, +} From 25410eeb080f0f819b03e97509fda5d6e7d78828 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E5=8B=87?= Date: Fri, 16 Oct 2020 13:31:52 +0800 Subject: [PATCH 2/2] add really-do-it flag --- cmd/lotus-storage-miner/proving.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cmd/lotus-storage-miner/proving.go b/cmd/lotus-storage-miner/proving.go index d0383ec19d..cc872aaefb 100644 --- a/cmd/lotus-storage-miner/proving.go +++ b/cmd/lotus-storage-miner/proving.go @@ -384,12 +384,19 @@ var provingTeminateCmd = &cli.Command{ Name: "terminate", Usage: "terminate specied sector early", Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "really-do-it", + Usage: "pass this flag if you know what you are doing", + }, &cli.StringFlag{ Name: "sectors", Usage: "specied sectors[xx,xx,xx]", }, }, Action: func(cctx *cli.Context) error { + if !cctx.Bool("really-do-it") { + return xerrors.Errorf("this is a command for advanced users, only use it if you are sure of what you are doing") + } nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx) if err != nil {