From 31a09b73ccec773d2bdfc5cfbad6acd394ee518d Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Tue, 2 Aug 2022 16:40:39 -0400 Subject: [PATCH 1/3] Add lotus-shed cmd to get total active deal storage --- chain/actors/builtin/market/market.go | 4 +++ cmd/lotus-shed/market.go | 43 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/chain/actors/builtin/market/market.go b/chain/actors/builtin/market/market.go index 2540d71ca..8c0e4e91a 100644 --- a/chain/actors/builtin/market/market.go +++ b/chain/actors/builtin/market/market.go @@ -240,6 +240,10 @@ func GetDealFees(deal market8.DealProposal, height abi.ChainEpoch) (abi.TokenAmo return ef, big.Sub(tf, ef) } +func IsDealActive(state market8.DealState) bool { + return state.SectorStartEpoch > -1 && state.SlashEpoch == -1 +} + func labelFromGoString(s string) (market8.DealLabel, error) { if utf8.ValidString(s) { return market8.NewLabelFromString(s) diff --git a/cmd/lotus-shed/market.go b/cmd/lotus-shed/market.go index 29b2fb112..e12752552 100644 --- a/cmd/lotus-shed/market.go +++ b/cmd/lotus-shed/market.go @@ -32,6 +32,7 @@ var marketCmd = &cli.Command{ marketDealFeesCmd, marketExportDatastoreCmd, marketImportDatastoreCmd, + marketDealsTotalStorageCmd, }, } @@ -283,6 +284,48 @@ var marketImportDatastoreCmd = &cli.Command{ }, } +var marketDealsTotalStorageCmd = &cli.Command{ + Name: "get-deals-total-storage", + Usage: "View the total storage available in all active market deals", + Flags: []cli.Flag{}, + Action: func(cctx *cli.Context) error { + api, closer, err := lcli.GetFullNodeAPI(cctx) + if err != nil { + return err + } + defer closer() + + ctx := lcli.ReqContext(cctx) + + ts, err := lcli.LoadTipSet(ctx, cctx, api) + if err != nil { + return err + } + + deals, err := api.StateMarketDeals(ctx, ts.Key()) + if err != nil { + return err + } + + total := big.Zero() + count := 0 + + for _, deal := range deals { + if market.IsDealActive(deal.State) { + dealStorage := big.NewIntUnsigned(uint64(deal.Proposal.PieceSize)) + total = big.Add(total, dealStorage) + count++ + } + + } + + fmt.Println("Total deals: ", count) + fmt.Println("Total storage: ", total) + + return nil + }, +} + func openLockedRepo(path string) (repo.LockedRepo, error) { // Open the repo at the repo path rpo, err := repo.NewFS(path) From 2f65a20d1608613aeb2c445e55acd41204e3b415 Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Tue, 2 Aug 2022 18:54:42 -0400 Subject: [PATCH 2/3] Add IsDealActive func to template wq --- chain/actors/builtin/market/actor.go.template | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/chain/actors/builtin/market/actor.go.template b/chain/actors/builtin/market/actor.go.template index a96c62e43..66298a18a 100644 --- a/chain/actors/builtin/market/actor.go.template +++ b/chain/actors/builtin/market/actor.go.template @@ -185,6 +185,10 @@ func GetDealFees(deal market{{.latestVersion}}.DealProposal, height abi.ChainEpo return ef, big.Sub(tf, ef) } +func IsDealActive(state market{{.latestVersion}}.DealState) bool { + return state.SectorStartEpoch > -1 && state.SlashEpoch == -1 +} + func labelFromGoString(s string) (market{{.latestVersion}}.DealLabel, error) { if utf8.ValidString(s) { return market{{.latestVersion}}.NewLabelFromString(s) From 654a3c976e92fd295986a17dfe4685d949ba3205 Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Wed, 3 Aug 2022 10:08:59 -0400 Subject: [PATCH 3/3] remove tipset retrieval to use default --- cmd/lotus-shed/market.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/cmd/lotus-shed/market.go b/cmd/lotus-shed/market.go index e12752552..0c5e7c81d 100644 --- a/cmd/lotus-shed/market.go +++ b/cmd/lotus-shed/market.go @@ -19,6 +19,7 @@ import ( "github.com/filecoin-project/go-state-types/big" "github.com/filecoin-project/lotus/chain/actors/builtin/market" + "github.com/filecoin-project/lotus/chain/types" lcli "github.com/filecoin-project/lotus/cli" "github.com/filecoin-project/lotus/lib/backupds" "github.com/filecoin-project/lotus/node/repo" @@ -287,7 +288,6 @@ var marketImportDatastoreCmd = &cli.Command{ var marketDealsTotalStorageCmd = &cli.Command{ Name: "get-deals-total-storage", Usage: "View the total storage available in all active market deals", - Flags: []cli.Flag{}, Action: func(cctx *cli.Context) error { api, closer, err := lcli.GetFullNodeAPI(cctx) if err != nil { @@ -297,12 +297,7 @@ var marketDealsTotalStorageCmd = &cli.Command{ ctx := lcli.ReqContext(cctx) - ts, err := lcli.LoadTipSet(ctx, cctx, api) - if err != nil { - return err - } - - deals, err := api.StateMarketDeals(ctx, ts.Key()) + deals, err := api.StateMarketDeals(ctx, types.EmptyTSK) if err != nil { return err }