2021-05-01 04:15:45 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2021-05-14 01:27:58 +00:00
|
|
|
"math/big"
|
|
|
|
|
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"
|
|
|
|
"golang.org/x/xerrors"
|
2021-05-01 04:15:45 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2022-06-14 15:00:51 +00:00
|
|
|
big2 "github.com/filecoin-project/go-state-types/big"
|
|
|
|
builtin4 "github.com/filecoin-project/specs-actors/v4/actors/builtin"
|
|
|
|
"github.com/filecoin-project/specs-actors/v4/actors/util/adt"
|
|
|
|
|
2021-05-01 04:15:45 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/miner"
|
2021-05-14 01:27:58 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/consensus/filcns"
|
2021-05-01 04:15:45 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/state"
|
|
|
|
"github.com/filecoin-project/lotus/chain/store"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
var minerTypesCmd = &cli.Command{
|
|
|
|
Name: "miner-types",
|
2023-01-27 16:20:01 +00:00
|
|
|
Usage: "Scrape state to report on how many miners of each WindowPoStProofType exist",
|
|
|
|
Flags: []cli.Flag{
|
2021-05-01 04:15:45 +00:00
|
|
|
&cli.StringFlag{
|
2023-01-27 16:20:01 +00:00
|
|
|
Name: "repo",
|
|
|
|
Value: "~/.lotus",
|
|
|
|
EnvVars: []string{"LOTUS_PATH"},
|
2021-05-01 04:15:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-09-02 16:07:23 +00:00
|
|
|
cs := store.NewChainStore(bs, bs, mds, filcns.Weight, nil)
|
2021-05-01 04:15:45 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
typeMap := make(map[abi.RegisteredPoStProof]int64)
|
2021-05-14 01:27:58 +00:00
|
|
|
pa, err := tree.GetActor(power.Address)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ps, err := power.Load(store, pa)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dc := 0
|
|
|
|
dz := power.Claim{
|
|
|
|
RawBytePower: abi.NewStoragePower(0),
|
|
|
|
QualityAdjPower: abi.NewStoragePower(0),
|
|
|
|
}
|
2021-05-01 04:15:45 +00:00
|
|
|
|
|
|
|
err = tree.ForEach(func(addr address.Address, act *types.Actor) error {
|
|
|
|
if act.Code == builtin4.StorageMinerActorCodeID {
|
|
|
|
ms, err := miner.Load(store, act)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mi, err := ms.Info()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-14 01:27:58 +00:00
|
|
|
if mi.WindowPoStProofType == abi.RegisteredPoStProof_StackedDrgWindow64GiBV1 {
|
|
|
|
mp, f, err := ps.MinerPower(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if f && mp.RawBytePower.Cmp(big.NewInt(10<<40)) >= 0 && mp.RawBytePower.Cmp(big.NewInt(20<<40)) < 0 {
|
|
|
|
dc = dc + 1
|
|
|
|
dz.RawBytePower = big2.Add(dz.RawBytePower, mp.RawBytePower)
|
|
|
|
dz.QualityAdjPower = big2.Add(dz.QualityAdjPower, mp.QualityAdjPower)
|
|
|
|
}
|
2021-05-01 04:15:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c, f := typeMap[mi.WindowPoStProofType]
|
|
|
|
if !f {
|
|
|
|
typeMap[mi.WindowPoStProofType] = 1
|
|
|
|
} else {
|
|
|
|
typeMap[mi.WindowPoStProofType] = c + 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to loop over actors: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range typeMap {
|
|
|
|
fmt.Println("Type:", k, " Count: ", v)
|
|
|
|
}
|
|
|
|
|
2021-05-14 01:27:58 +00:00
|
|
|
fmt.Println("Mismatched power (raw, QA): ", dz.RawBytePower, " ", dz.QualityAdjPower)
|
|
|
|
fmt.Println("Mismatched 64 GiB miner count: ", dc)
|
|
|
|
|
2021-05-01 04:15:45 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|