Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: miner cli: sectors list upgrade-bounds tool #10923

Merged
merged 1 commit into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions chain/actors/policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const (
MaxPreCommitRandomnessLookback = builtin11.EpochsInDay + SealRandomnessLookback
)

var (
MarketDefaultAllocationTermBuffer = market11.MarketDefaultAllocationTermBuffer
)

// SetSupportedProofTypes sets supported proof types, across all actor versions.
// This should only be used for testing.
func SetSupportedProofTypes(types ...abi.RegisteredSealProof) {
Expand Down
4 changes: 4 additions & 0 deletions chain/actors/policy/policy.go.template
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ const (
MaxPreCommitRandomnessLookback = builtin{{.latestVersion}}.EpochsInDay + SealRandomnessLookback
)

var (
MarketDefaultAllocationTermBuffer = market{{.latestVersion}}.MarketDefaultAllocationTermBuffer
)

// SetSupportedProofTypes sets supported proof types, across all actor versions.
// This should only be used for testing.
func SetSupportedProofTypes(types ...abi.RegisteredSealProof) {
Expand Down
167 changes: 167 additions & 0 deletions cmd/lotus-miner/sectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -318,6 +319,9 @@ var sectorsListCmd = &cli.Command{
Value: parallelSectorChecks,
},
},
Subcommands: []*cli.Command{
sectorsListUpgradeBoundsCmd,
},
Action: func(cctx *cli.Context) error {
// http mode allows for parallel json decoding/encoding, which was a bottleneck here
minerApi, closer, err := lcli.GetStorageMinerAPI(cctx, cliutil.StorageMinerUseHttp)
Expand Down Expand Up @@ -585,6 +589,169 @@ var sectorsListCmd = &cli.Command{
},
}

var sectorsListUpgradeBoundsCmd = &cli.Command{
Name: "upgrade-bounds",
Usage: "Output upgrade bounds for available sectors",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "buckets",
Value: 25,
},
&cli.BoolFlag{
Name: "csv",
Usage: "output machine-readable values",
},
&cli.BoolFlag{
Name: "deal-terms",
Usage: "bucket by how many deal-sectors can start at a given expiration",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I fully understand this. We don't pass in an expiration here so what does this statement mean?
I initially took this to mean filter all deal sectors which can start past a certain time

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Basically when you're packing datacap deals into sectors, the sector expiration must fit two constraints:

  • Sector expiration must be larger than deal end - so that the sector is alive for the whole duration of the deal
  • Sector expiration must NOT be longer than 90 days after deal expiration - so that you don't get the datacap boost for too much longer than the deal is requesting to be stored for

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(the --deal-terms mode shows how many deals you can accept at a given deal end epoch)

},
},
Action: func(cctx *cli.Context) error {
minerApi, closer, err := lcli.GetStorageMinerAPI(cctx, cliutil.StorageMinerUseHttp)
if err != nil {
return err
}
defer closer()

fullApi, closer2, err := lcli.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer2()

ctx := lcli.ReqContext(cctx)

list, err := minerApi.SectorsListInStates(ctx, []api.SectorState{
api.SectorState(sealing.Available),
})
if err != nil {
return xerrors.Errorf("getting sector list: %w", err)
}

head, err := fullApi.ChainHead(ctx)
if err != nil {
return xerrors.Errorf("getting chain head: %w", err)
}

filter := bitfield.New()

for _, s := range list {
filter.Set(uint64(s))
}

maddr, err := minerApi.ActorAddress(ctx)
if err != nil {
return err
}

sset, err := fullApi.StateMinerSectors(ctx, maddr, &filter, head.Key())
if err != nil {
return err
}

if len(sset) == 0 {
return nil
}

var minExpiration, maxExpiration abi.ChainEpoch

for _, s := range sset {
if s.Expiration < minExpiration || minExpiration == 0 {
minExpiration = s.Expiration
}
if s.Expiration > maxExpiration {
maxExpiration = s.Expiration
}
}

buckets := cctx.Int("buckets")
bucketSize := (maxExpiration - minExpiration) / abi.ChainEpoch(buckets)
bucketCounts := make([]int, buckets+1)

for b := range bucketCounts {
bucketMin := minExpiration + abi.ChainEpoch(b)*bucketSize
bucketMax := minExpiration + abi.ChainEpoch(b+1)*bucketSize

if cctx.Bool("deal-terms") {
bucketMax = bucketMax + policy.MarketDefaultAllocationTermBuffer
}

for _, s := range sset {
isInBucket := s.Expiration >= bucketMin && s.Expiration < bucketMax

if isInBucket {
bucketCounts[b]++
}
}

}

// Creating CSV writer
writer := csv.NewWriter(os.Stdout)

// Writing CSV headers
err = writer.Write([]string{"Max Expiration in Bucket", "Sector Count"})
if err != nil {
return xerrors.Errorf("writing csv headers: %w", err)
}

// Writing bucket details

if cctx.Bool("csv") {
for i := 0; i < buckets; i++ {
maxExp := minExpiration + abi.ChainEpoch(i+1)*bucketSize

timeStr := strconv.FormatInt(int64(maxExp), 10)

err = writer.Write([]string{
timeStr,
strconv.Itoa(bucketCounts[i]),
})
if err != nil {
return xerrors.Errorf("writing csv row: %w", err)
}
}

// Flush to make sure all data is written to the underlying writer
writer.Flush()

if err := writer.Error(); err != nil {
return xerrors.Errorf("flushing csv writer: %w", err)
}

return nil
}

tw := tablewriter.New(
tablewriter.Col("Bucket Expiration"),
tablewriter.Col("Sector Count"),
tablewriter.Col("Bar"),
)

var barCols = 40
var maxCount int

for _, c := range bucketCounts {
if c > maxCount {
maxCount = c
}
}

for i := 0; i < buckets; i++ {
maxExp := minExpiration + abi.ChainEpoch(i+1)*bucketSize
timeStr := cliutil.EpochTime(head.Height(), maxExp)

tw.Write(map[string]interface{}{
"Bucket Expiration": timeStr,
"Sector Count": color.YellowString("%d", bucketCounts[i]),
"Bar": "[" + color.GreenString(strings.Repeat("|", bucketCounts[i]*barCols/maxCount)) + strings.Repeat(" ", barCols-bucketCounts[i]*barCols/maxCount) + "]",
})
}

return tw.Flush(os.Stdout)
},
}

var sectorsRefsCmd = &cli.Command{
Name: "refs",
Usage: "List References to sectors",
Expand Down
28 changes: 24 additions & 4 deletions documentation/en/cli-lotus-miner.md
Original file line number Diff line number Diff line change
Expand Up @@ -654,17 +654,37 @@ NAME:
lotus-miner sectors list - List sectors

USAGE:
lotus-miner sectors list [command options] [arguments...]
lotus-miner sectors list command [command options] [arguments...]

COMMANDS:
upgrade-bounds Output upgrade bounds for available sectors
help, h Shows a list of commands or help for one command

OPTIONS:
--check-parallelism value number of parallel requests to make for checking sector states (default: 300)
--events, -e display number of events the sector has received (default: false)
--show-removed, -r show removed sectors (default: false)
--fast, -f don't show on-chain info for better performance (default: false)
--events, -e display number of events the sector has received (default: false)
--initial-pledge, -p display initial pledge (default: false)
--seal-time, -t display how long it took for the sector to be sealed (default: false)
--show-removed, -r show removed sectors (default: false)
--states value filter sectors by a comma-separated list of states
--unproven, -u only show sectors which aren't in the 'Proving' state (default: false)
--check-parallelism value number of parallel requests to make for checking sector states (default: 300)
--help, -h show help (default: false)

```

#### lotus-miner sectors list upgrade-bounds
```
NAME:
lotus-miner sectors list upgrade-bounds - Output upgrade bounds for available sectors

USAGE:
lotus-miner sectors list upgrade-bounds [command options] [arguments...]

OPTIONS:
--buckets value (default: 25)
--csv output machine-readable values (default: false)
--deal-terms bucket by how many deal-sectors can start at a given expiration (default: false)

```

Expand Down