Add dump code

Signed-off-by: Jakub Sztandera <kubuxu@protocol.ai>
This commit is contained in:
Jakub Sztandera 2021-11-25 20:53:49 +01:00
parent ab55a6fbbe
commit af113f867d
No known key found for this signature in database
GPG Key ID: 9A9AF56F8B3879BA

View File

@ -2,11 +2,14 @@ package main
import ( import (
"bytes" "bytes"
"context"
"encoding/base64" "encoding/base64"
"encoding/binary"
"fmt" "fmt"
"image" "image"
"image/color" "image/color"
"image/png" "image/png"
"io"
"os" "os"
"sort" "sort"
"strconv" "strconv"
@ -23,6 +26,7 @@ import (
miner2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/miner" miner2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/miner"
"github.com/filecoin-project/lotus/api/v0api"
"github.com/filecoin-project/lotus/chain/actors" "github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/actors/builtin/miner" "github.com/filecoin-project/lotus/chain/actors/builtin/miner"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
@ -38,6 +42,7 @@ var sectorsCmd = &cli.Command{
terminateSectorCmd, terminateSectorCmd,
terminateSectorPenaltyEstimationCmd, terminateSectorPenaltyEstimationCmd,
visAllocatedSectorsCmd, visAllocatedSectorsCmd,
dumpRLESectorCmd,
}, },
} }
@ -275,21 +280,10 @@ var terminateSectorPenaltyEstimationCmd = &cli.Command{
}, },
} }
var visAllocatedSectorsCmd = &cli.Command{ func activeMiners(ctx context.Context, api v0api.FullNode) ([]address.Address, error) {
Name: "vis-allocated", miners, err := api.StateListMiners(ctx, types.EmptyTSK)
Usage: "Produces a html with visualisation of allocated sectors",
Action: func(cctx *cli.Context) error {
api, closer, err := lcli.GetFullNodeAPI(cctx)
if err != nil { if err != nil {
return err return nil, err
}
defer closer()
ctx := lcli.ReqContext(cctx)
var miners []address.Address
if cctx.NArg() == 0 {
miners, err = api.StateListMiners(ctx, types.EmptyTSK)
if err != nil {
return err
} }
powCache := make(map[address.Address]types.BigInt) powCache := make(map[address.Address]types.BigInt)
var lk sync.Mutex var lk sync.Mutex
@ -309,10 +303,106 @@ var visAllocatedSectorsCmd = &cli.Command{
}) })
n := sort.Search(len(miners), func(i int) bool { n := sort.Search(len(miners), func(i int) bool {
pow := powCache[miners[i]] pow := powCache[miners[i]]
log.Infof("pow @%d = %s", i, pow)
return pow.IsZero() return pow.IsZero()
}) })
miners = miners[:n] return append(miners[0:0:0], miners[:n]...), nil
}
var dumpRLESectorCmd = &cli.Command{
Name: "dump-rles",
Usage: "",
Action: func(cctx *cli.Context) error {
api, closer, err := lcli.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
var miners []address.Address
if cctx.NArg() == 0 {
miners, err = activeMiners(ctx, api)
if err != nil {
return xerrors.Errorf("getting active miners: %w", err)
}
} else {
for _, mS := range cctx.Args().Slice() {
mA, err := address.NewFromString(mS)
if err != nil {
return xerrors.Errorf("parsing address '%s': %w", mS, err)
}
miners = append(miners, mA)
}
}
wbuf := make([]byte, 8)
buf := &bytes.Buffer{}
for i := 0; i < len(miners); i++ {
buf.Reset()
err := func() error {
state, err := api.StateReadState(ctx, miners[i], types.EmptyTSK)
if err != nil {
return xerrors.Errorf("getting state: %+v", err)
}
allocSString := state.State.(map[string]interface{})["AllocatedSectors"].(map[string]interface{})["/"].(string)
allocCid, err := cid.Decode(allocSString)
if err != nil {
return xerrors.Errorf("decoding cid: %+v", err)
}
rle, err := api.ChainReadObj(ctx, allocCid)
if err != nil {
return xerrors.Errorf("reading AllocatedSectors: %+v", err)
}
var bf bitfield.BitField
err = bf.UnmarshalCBOR(bytes.NewReader(rle))
if err != nil {
return xerrors.Errorf("decoding bitfield: %w", err)
}
ri, err := bf.RunIterator()
if err != nil {
return xerrors.Errorf("creating interator: %w", err)
}
for ri.HasNext() {
run, err := ri.NextRun()
if err != nil {
return xerrors.Errorf("getting run: %w", err)
}
binary.LittleEndian.PutUint64(wbuf, run.Len)
buf.Write(wbuf)
}
_, err = io.Copy(os.Stdout, buf)
if err != nil {
return xerrors.Errorf("copy: %w", err)
}
return nil
}()
if err != nil {
log.Errorf("miner %d: %s: %+v", i, miners[i], err)
}
}
return nil
},
}
var visAllocatedSectorsCmd = &cli.Command{
Name: "vis-allocated",
Usage: "Produces a html with visualisation of allocated sectors",
Action: func(cctx *cli.Context) error {
api, closer, err := lcli.GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
var miners []address.Address
if cctx.NArg() == 0 {
miners, err = activeMiners(ctx, api)
if err != nil {
return xerrors.Errorf("getting active miners: %w", err)
}
} else { } else {
for _, mS := range cctx.Args().Slice() { for _, mS := range cctx.Args().Slice() {
mA, err := address.NewFromString(mS) mA, err := address.NewFromString(mS)