From 9a1a89d98c1b4bb08806b79483da05fce7e6eb8c Mon Sep 17 00:00:00 2001 From: Aayush Date: Sat, 12 Mar 2022 18:11:10 -0500 Subject: [PATCH] Shed: Add a tool to check UTF-8 validity of labels --- cmd/lotus-shed/deal-label.go | 118 +++++++++++++++++++++++++++++++++++ cmd/lotus-shed/main.go | 1 + 2 files changed, 119 insertions(+) create mode 100644 cmd/lotus-shed/deal-label.go diff --git a/cmd/lotus-shed/deal-label.go b/cmd/lotus-shed/deal-label.go new file mode 100644 index 000000000..483415987 --- /dev/null +++ b/cmd/lotus-shed/deal-label.go @@ -0,0 +1,118 @@ +package main + +import ( + "context" + "fmt" + "io" + "unicode/utf8" + + "github.com/filecoin-project/lotus/chain/actors/builtin/market" + + "github.com/filecoin-project/lotus/chain/consensus/filcns" + + "github.com/filecoin-project/go-state-types/abi" + "github.com/filecoin-project/lotus/chain/state" + "github.com/filecoin-project/lotus/chain/store" + "github.com/filecoin-project/lotus/node/repo" + "github.com/filecoin-project/specs-actors/v4/actors/util/adt" + "github.com/ipfs/go-cid" + cbor "github.com/ipfs/go-ipld-cbor" + "github.com/urfave/cli/v2" +) + +var dealLabelCmd = &cli.Command{ + Name: "deal-label", + Usage: "Scrape state to report on how many deals have non UTF-8 labels", + 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 state root") + } + + sroot, 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) + + tree, err := state.LoadStateTree(cst, sroot) + if err != nil { + return err + } + + ma, err := tree.GetActor(market.Address) + if err != nil { + return err + } + + ms, err := market.Load(store, ma) + if err != nil { + return err + } + + ps, err := ms.Proposals() + if err != nil { + return err + } + + var deals []abi.DealID + if err = ps.ForEach(func(id abi.DealID, dp market.DealProposal) error { + if !utf8.Valid([]byte(dp.Label)) { + deals = append(deals, id) + } + + return nil + }); err != nil { + return err + } + + fmt.Println("there are ", len(deals), " bad labels") + for _, d := range deals { + fmt.Print(d, " ") + } + + return nil + }, +} diff --git a/cmd/lotus-shed/main.go b/cmd/lotus-shed/main.go index c1facfe7b..cfbc73a14 100644 --- a/cmd/lotus-shed/main.go +++ b/cmd/lotus-shed/main.go @@ -22,6 +22,7 @@ func main() { bitFieldCmd, cronWcCmd, frozenMinersCmd, + dealLabelCmd, keyinfoCmd, jwtCmd, noncefix,