lotus/cmd/lotus-shed/cron-count.go

351 lines
9.0 KiB
Go
Raw Permalink Normal View History

2021-04-23 03:03:53 +00:00
package main
import (
2023-07-10 21:28:14 +00:00
"encoding/json"
2021-04-23 03:03:53 +00:00
"fmt"
2023-07-10 21:28:14 +00:00
"os"
2021-04-23 03:03:53 +00:00
"github.com/ipfs/go-cid"
ipldcbor "github.com/ipfs/go-ipld-cbor"
2022-06-14 15:00:51 +00:00
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
2021-04-23 03:03:53 +00:00
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-bitfield"
2023-07-10 21:28:14 +00:00
"github.com/filecoin-project/go-state-types/abi"
miner11 "github.com/filecoin-project/go-state-types/builtin/v11/miner"
"github.com/filecoin-project/go-state-types/builtin/v11/util/adt"
2023-07-31 16:53:07 +00:00
2023-07-10 21:28:14 +00:00
"github.com/filecoin-project/lotus/api"
2021-04-23 03:03:53 +00:00
"github.com/filecoin-project/lotus/build"
2023-07-10 21:28:14 +00:00
"github.com/filecoin-project/lotus/chain/types"
2021-04-23 03:03:53 +00:00
lcli "github.com/filecoin-project/lotus/cli"
)
var cronWcCmd = &cli.Command{
Name: "cron-wc",
Description: "cron stats",
Subcommands: []*cli.Command{
minerDeadlineCronCountCmd,
2023-07-10 21:28:14 +00:00
minerDeadlinePartitionMeasurementCmd,
},
}
type DeadlineRef struct {
2023-07-10 21:59:19 +00:00
To string
Height abi.ChainEpoch
2023-07-10 22:32:37 +00:00
Gas json.RawMessage
2023-07-10 21:28:14 +00:00
}
type DeadlineSummary struct {
Partitions []PartitionSummary
PreCommitExpiry PreCommitExpiry
VestingDiff VestingDiff
}
type PreCommitExpiry struct {
Expired []uint64
}
type VestingDiff struct {
PrevTableSize int
NewTableSize int
2023-07-10 21:28:14 +00:00
}
type PartitionSummary struct {
2023-07-21 19:07:25 +00:00
Live int
Dead int
Faulty int
Diff PartitionDiff
2023-07-10 21:28:14 +00:00
}
type PartitionDiff struct {
Faulted int
Recovered int
Killed int
}
var minerDeadlinePartitionMeasurementCmd = &cli.Command{
Name: "deadline-summary",
Description: "",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "json",
Usage: "read input as json",
Value: true,
},
&cli.StringFlag{
Name: "tipset",
Usage: "specify tipset state to search on (pass comma separated array of cids)",
},
},
Action: func(c *cli.Context) error {
// read in values to process
if !c.Bool("json") {
return xerrors.Errorf("unsupported non json input format")
}
var refStream []DeadlineRef
if err := json.NewDecoder(os.Stdin).Decode(&refStream); err != nil {
return xerrors.Errorf("failed to parse input: %w", err)
}
// go from height and sp addr to deadline partition data
n, acloser, err := lcli.GetFullNodeAPI(c)
if err != nil {
return err
}
defer acloser()
ctx := lcli.ReqContext(c)
bs := ReadOnlyAPIBlockstore{n}
adtStore := adt.WrapStore(ctx, ipldcbor.NewCborStore(&bs))
2023-07-10 21:28:14 +00:00
dSummaries := make([]DeadlineSummary, len(refStream))
2023-07-10 22:32:37 +00:00
for j, ref := range refStream {
2023-07-10 21:28:14 +00:00
// get miner's deadline
tsBefore, err := n.ChainGetTipSetByHeight(ctx, ref.Height, types.EmptyTSK)
if err != nil {
return xerrors.Errorf("failed to get tipset at epoch: %d: %w", ref.Height, err)
}
tsAfter, err := n.ChainGetTipSetByHeight(ctx, ref.Height+1, types.EmptyTSK)
if err != nil {
return xerrors.Errorf("failed to get tipset at epoch %d: %w", ref.Height, err)
}
2023-07-10 21:59:19 +00:00
addr, err := address.NewFromString(ref.To)
2023-07-10 21:54:07 +00:00
if err != nil {
return xerrors.Errorf("faield to get address from input string: %w", err)
}
dline, err := n.StateMinerProvingDeadline(ctx, addr, tsBefore.Key())
2023-07-10 21:28:14 +00:00
if err != nil {
return xerrors.Errorf("failed to read proving deadline: %w", err)
}
// iterate through all partitions at epoch of processing
var pSummaries []PartitionSummary
2023-07-10 21:54:07 +00:00
psBefore, err := n.StateMinerPartitions(ctx, addr, dline.Index, tsBefore.Key())
2023-07-10 21:28:14 +00:00
if err != nil {
return xerrors.Errorf("failed to get partitions: %w", err)
}
2023-07-10 21:54:07 +00:00
psAfter, err := n.StateMinerPartitions(ctx, addr, dline.Index, tsAfter.Key())
2023-07-10 21:28:14 +00:00
if err != nil {
return xerrors.Errorf("failed to get partitions: %w", err)
}
if len(psBefore) != len(psAfter) {
return xerrors.Errorf("faield")
}
type partitionCount struct {
live int
dead int
faulty int
recovering int
}
countPartition := func(p api.Partition) (partitionCount, error) {
liveSectors, err := p.LiveSectors.All(abi.MaxSectorNumber)
if err != nil {
return partitionCount{}, xerrors.Errorf("failed to count live sectors in partition: %w", err)
}
allSectors, err := p.AllSectors.All(abi.MaxSectorNumber)
if err != nil {
return partitionCount{}, xerrors.Errorf("failed to count all sectors in partition: %w", err)
}
faultySectors, err := p.FaultySectors.All(abi.MaxSectorNumber)
if err != nil {
return partitionCount{}, xerrors.Errorf("failed to count faulty sectors in partition: %w", err)
}
recoveringSectors, err := p.RecoveringSectors.All(abi.MaxSectorNumber)
if err != nil {
return partitionCount{}, xerrors.Errorf("failed to count recovering sectors in partition: %w", err)
}
return partitionCount{
live: len(liveSectors),
dead: len(allSectors) - len(liveSectors),
faulty: len(faultySectors),
recovering: len(recoveringSectors),
}, nil
}
countVestingTable := func(table cid.Cid) (int, error) {
var vestingTable miner11.VestingFunds
if err := adtStore.Get(ctx, table, &vestingTable); err != nil {
return 0, err
}
return len(vestingTable.Funds), nil
}
2023-07-10 21:28:14 +00:00
for i := 0; i < len(psBefore); i++ {
cntBefore, err := countPartition(psBefore[i])
if err != nil {
return err
}
cntAfter, err := countPartition(psAfter[i])
if err != nil {
return err
}
pSummaries = append(pSummaries, PartitionSummary{
2023-07-21 19:07:25 +00:00
Live: cntBefore.live,
Dead: cntBefore.dead,
Faulty: cntBefore.faulty,
2023-07-10 21:28:14 +00:00
Diff: PartitionDiff{
Faulted: cntAfter.faulty - cntBefore.faulty,
Recovered: cntBefore.recovering - cntAfter.recovering,
Killed: cntAfter.dead - cntBefore.dead,
},
})
}
// Precommit and vesting table data
// Before
aBefore, err := n.StateGetActor(ctx, addr, tsBefore.Key())
if err != nil {
return err
}
var st miner11.State
err = adtStore.Get(ctx, aBefore.Head, &st)
if err != nil {
return err
}
expiryQArray, err := adt.AsArray(adtStore, st.PreCommittedSectorsCleanUp, miner11.PrecommitCleanUpAmtBitwidth)
if err != nil {
return err
}
var sectorsBf bitfield.BitField
var accumulator []uint64
2023-07-31 17:12:56 +00:00
h := ref.Height
if err := expiryQArray.ForEach(&sectorsBf, func(i int64) error {
2023-07-31 17:12:56 +00:00
if abi.ChainEpoch(i) > h {
return nil
}
sns, err := sectorsBf.All(abi.MaxSectorNumber)
if err != nil {
return err
}
accumulator = append(accumulator, sns...)
return nil
}); err != nil {
return err
}
vestingBefore, err := countVestingTable(st.VestingFunds)
if err != nil {
return err
}
// After
aAfter, err := n.StateGetActor(ctx, addr, tsAfter.Key())
if err != nil {
return err
}
var stAfter miner11.State
err = adtStore.Get(ctx, aAfter.Head, &stAfter)
if err != nil {
return err
}
vestingAfter, err := countVestingTable(stAfter.VestingFunds)
if err != nil {
return err
}
dSummaries[j] = DeadlineSummary{
Partitions: pSummaries,
PreCommitExpiry: PreCommitExpiry{
Expired: accumulator,
},
VestingDiff: VestingDiff{
PrevTableSize: vestingBefore,
NewTableSize: vestingAfter,
},
}
2023-07-10 21:28:14 +00:00
}
// output partition info
2023-07-31 17:12:56 +00:00
if err := json.NewEncoder(os.Stdout).Encode(dSummaries); err != nil {
return err
}
2023-07-10 21:28:14 +00:00
return nil
2021-04-23 03:03:53 +00:00
},
}
var minerDeadlineCronCountCmd = &cli.Command{
Name: "deadline",
Description: "list all addresses of miners with active deadline crons",
Action: func(c *cli.Context) error {
return countDeadlineCrons(c)
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "tipset",
Usage: "specify tipset state to search on (pass comma separated array of cids)",
},
},
}
func findDeadlineCrons(c *cli.Context) (map[address.Address]struct{}, error) {
api, acloser, err := lcli.GetFullNodeAPI(c)
if err != nil {
return nil, err
}
defer acloser()
ctx := lcli.ReqContext(c)
ts, err := lcli.LoadTipSet(ctx, c, api)
if err != nil {
return nil, err
}
if ts == nil {
ts, err = api.ChainHead(ctx)
if err != nil {
return nil, err
}
}
mAddrs, err := api.StateListMiners(ctx, ts.Key())
if err != nil {
return nil, err
}
activeMiners := make(map[address.Address]struct{})
for _, mAddr := range mAddrs {
// All miners have active cron before v4.
// v4 upgrade epoch is last epoch running v3 epoch and api.StateReadState reads
// parent state, so v4 state isn't read until upgrade epoch + 2
2021-05-31 19:43:21 +00:00
if ts.Height() <= build.UpgradeTurboHeight+1 {
2021-04-23 03:03:53 +00:00
activeMiners[mAddr] = struct{}{}
continue
}
st, err := api.StateReadState(ctx, mAddr, ts.Key())
if err != nil {
return nil, err
}
minerState, ok := st.State.(map[string]interface{})
if !ok {
return nil, xerrors.Errorf("internal error: failed to cast miner state to expected map type")
}
activeDlineIface, ok := minerState["DeadlineCronActive"]
if !ok {
return nil, xerrors.Errorf("miner %s had no deadline state, is this a v3 state root?", mAddr)
}
active := activeDlineIface.(bool)
if active {
activeMiners[mAddr] = struct{}{}
}
}
return activeMiners, nil
}
func countDeadlineCrons(c *cli.Context) error {
activeMiners, err := findDeadlineCrons(c)
if err != nil {
return err
}
2021-05-06 14:17:25 +00:00
for addr := range activeMiners {
2021-04-23 03:03:53 +00:00
fmt.Printf("%s\n", addr)
}
return nil
}