Add command to sort miners by deal counts

This commit is contained in:
Jeromy 2020-05-21 15:52:20 -07:00
parent 0d3f602d58
commit 805686be2e
2 changed files with 50 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"reflect" "reflect"
"sort"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -493,6 +494,12 @@ var stateGetDealSetCmd = &cli.Command{
var stateListMinersCmd = &cli.Command{ var stateListMinersCmd = &cli.Command{
Name: "list-miners", Name: "list-miners",
Usage: "list all miners in the network", Usage: "list all miners in the network",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "sort-by",
Usage: "criteria to sort miners by (none, num-deals)",
},
},
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx) api, closer, err := GetFullNodeAPI(cctx)
if err != nil { if err != nil {
@ -512,6 +519,26 @@ var stateListMinersCmd = &cli.Command{
return err return err
} }
switch cctx.String("sort-by") {
case "num-deals":
ndm, err := getDealsCounts(ctx, api)
if err != nil {
return err
}
sort.Slice(miners, func(i, j int) bool {
return ndm[miners[i]] > ndm[miners[j]]
})
for i := 0; i < 50 && i < len(miners); i++ {
fmt.Printf("%s %d\n", miners[i], ndm[miners[i]])
}
return nil
default:
return fmt.Errorf("unrecognized sorting order")
case "", "none":
}
for _, m := range miners { for _, m := range miners {
fmt.Println(m.String()) fmt.Println(m.String())
} }
@ -520,6 +547,22 @@ var stateListMinersCmd = &cli.Command{
}, },
} }
func getDealsCounts(ctx context.Context, lapi api.FullNode) (map[address.Address]int, error) {
allDeals, err := lapi.StateMarketDeals(ctx, types.EmptyTSK)
if err != nil {
return nil, err
}
out := make(map[address.Address]int)
for _, d := range allDeals {
if d.State.SectorStartEpoch != -1 {
out[d.Proposal.Provider]++
}
}
return out, nil
}
var stateListActorsCmd = &cli.Command{ var stateListActorsCmd = &cli.Command{
Name: "list-actors", Name: "list-actors",
Usage: "list all actors in the network", Usage: "list all actors in the network",

View File

@ -445,7 +445,13 @@ func (a *StateAPI) StateMarketDeals(ctx context.Context, tsk types.TipSetKey) (m
var s market.DealState var s market.DealState
if err := sa.Get(ctx, i, &s); err != nil { if err := sa.Get(ctx, i, &s); err != nil {
return err if err != nil {
if _, ok := err.(*amt.ErrNotFound); !ok {
return xerrors.Errorf("failed to get state for deal in proposals array: %w", err)
}
s.SectorStartEpoch = -1
}
} }
out[strconv.FormatInt(int64(i), 10)] = api.MarketDeal{ out[strconv.FormatInt(int64(i), 10)] = api.MarketDeal{
Proposal: d, Proposal: d,