lotus/cmd/lotus-shed/miner-peerid.go

120 lines
2.7 KiB
Go
Raw Normal View History

package main
import (
"context"
"fmt"
"io"
2022-06-14 15:00:51 +00:00
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
2022-08-25 18:20:41 +00:00
"github.com/libp2p/go-libp2p/core/peer"
2022-06-14 15:00:51 +00:00
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
2022-06-14 15:00:51 +00:00
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/specs-actors/v4/actors/util/adt"
builtin5 "github.com/filecoin-project/specs-actors/v5/actors/builtin"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
2022-06-14 15:00:51 +00:00
"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/chain/types"
lcli "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/node/repo"
)
var minerPeeridCmd = &cli.Command{
Name: "miner-peerid",
2023-01-27 16:20:01 +00:00
Usage: "Scrape state to find a miner based on peerid",
Flags: []cli.Flag{
&cli.StringFlag{
2023-01-27 16:20:01 +00:00
Name: "repo",
Value: "~/.lotus",
EnvVars: []string{"LOTUS_PATH"},
},
},
Action: func(cctx *cli.Context) error {
ctx := context.TODO()
if cctx.NArg() != 2 {
return lcli.IncorrectNumArgs(cctx)
}
pid, err := peer.Decode(cctx.Args().Get(0))
if err != nil {
return fmt.Errorf("failed to parse input as a peerId: %w", err)
}
sroot, err := cid.Decode(cctx.Args().Get(1))
if err != nil {
return fmt.Errorf("failed to parse state root: %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
}
err = tree.ForEach(func(addr address.Address, act *types.Actor) error {
if act.Code == builtin5.StorageMinerActorCodeID {
ms, err := miner.Load(store, act)
if err != nil {
return err
}
mi, err := ms.Info()
if err != nil {
return err
}
2022-04-20 21:34:28 +00:00
if mi.PeerId != nil && peer.ID(mi.PeerId) == pid {
fmt.Println(addr)
}
}
return nil
})
if err != nil {
return xerrors.Errorf("failed to loop over actors: %w", err)
}
return nil
},
}