Merge pull request #4177 from filecoin-project/feat/chain-usage-cmd

allow usage inspection on a chain segment
This commit is contained in:
Łukasz Magiera 2020-10-06 01:21:01 +02:00 committed by GitHub
commit c8822fc9b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -28,7 +28,7 @@ import (
cbg "github.com/whyrusleeping/cbor-gen"
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/api"
lapi "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors"
"github.com/filecoin-project/lotus/chain/stmgr"
@ -162,7 +162,7 @@ var chainGetBlock = &cli.Command{
},
}
func apiMsgCids(in []api.Message) []cid.Cid {
func apiMsgCids(in []lapi.Message) []cid.Cid {
out := make([]cid.Cid, len(in))
for k, v := range in {
out[k] = v.Cid
@ -387,6 +387,16 @@ var chainInspectUsage = &cli.Command{
Usage: "specify tipset to view block space usage of",
Value: "@head",
},
&cli.IntFlag{
Name: "length",
Usage: "length of chain to inspect block space usage for",
Value: 1,
},
&cli.IntFlag{
Name: "num-results",
Usage: "number of results to print per category",
Value: 10,
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
@ -401,9 +411,22 @@ var chainInspectUsage = &cli.Command{
return err
}
pmsgs, err := api.ChainGetParentMessages(ctx, ts.Blocks()[0].Cid())
if err != nil {
return err
cur := ts
var msgs []lapi.Message
for i := 0; i < cctx.Int("length"); i++ {
pmsgs, err := api.ChainGetParentMessages(ctx, cur.Blocks()[0].Cid())
if err != nil {
return err
}
msgs = append(msgs, pmsgs...)
next, err := api.ChainGetTipSet(ctx, cur.Parents())
if err != nil {
return err
}
cur = next
}
codeCache := make(map[address.Address]cid.Cid)
@ -428,7 +451,7 @@ var chainInspectUsage = &cli.Command{
byMethod := make(map[string]int64)
var sum int64
for _, m := range pmsgs {
for _, m := range msgs {
bySender[m.Message.From.String()] += m.Message.GasLimit
byDest[m.Message.To.String()] += m.Message.GasLimit
sum += m.Message.GasLimit
@ -467,23 +490,25 @@ var chainInspectUsage = &cli.Command{
destVals := mapToSortedKvs(byDest)
methodVals := mapToSortedKvs(byMethod)
numRes := cctx.Int("num-results")
fmt.Printf("Total Gas Limit: %d\n", sum)
fmt.Printf("By Sender:\n")
for i := 0; i < 10 && i < len(senderVals); i++ {
for i := 0; i < numRes && i < len(senderVals); i++ {
sv := senderVals[i]
fmt.Printf("%s\t%0.2f\t(%d)\n", sv.Key, (100*float64(sv.Gas))/float64(sum), sv.Gas)
fmt.Printf("%s\t%0.2f%%\t(%d)\n", sv.Key, (100*float64(sv.Gas))/float64(sum), sv.Gas)
}
fmt.Println()
fmt.Printf("By Receiver:\n")
for i := 0; i < 10 && i < len(destVals); i++ {
for i := 0; i < numRes && i < len(destVals); i++ {
sv := destVals[i]
fmt.Printf("%s\t%0.2f\t(%d)\n", sv.Key, (100*float64(sv.Gas))/float64(sum), sv.Gas)
fmt.Printf("%s\t%0.2f%%\t(%d)\n", sv.Key, (100*float64(sv.Gas))/float64(sum), sv.Gas)
}
fmt.Println()
fmt.Printf("By Method:\n")
for i := 0; i < 10 && i < len(methodVals); i++ {
for i := 0; i < numRes && i < len(methodVals); i++ {
sv := methodVals[i]
fmt.Printf("%s\t%0.2f\t(%d)\n", sv.Key, (100*float64(sv.Gas))/float64(sum), sv.Gas)
fmt.Printf("%s\t%0.2f%%\t(%d)\n", sv.Key, (100*float64(sv.Gas))/float64(sum), sv.Gas)
}
return nil
@ -763,7 +788,7 @@ var chainGetCmd = &cli.Command{
type apiIpldStore struct {
ctx context.Context
api api.FullNode
api lapi.FullNode
}
func (ht *apiIpldStore) Context() context.Context {
@ -791,7 +816,7 @@ func (ht *apiIpldStore) Put(ctx context.Context, v interface{}) (cid.Cid, error)
panic("No mutations allowed")
}
func handleAmt(ctx context.Context, api api.FullNode, r cid.Cid) error {
func handleAmt(ctx context.Context, api lapi.FullNode, r cid.Cid) error {
s := &apiIpldStore{ctx, api}
mp, err := adt.AsArray(s, r)
if err != nil {
@ -804,7 +829,7 @@ func handleAmt(ctx context.Context, api api.FullNode, r cid.Cid) error {
})
}
func handleHamtEpoch(ctx context.Context, api api.FullNode, r cid.Cid) error {
func handleHamtEpoch(ctx context.Context, api lapi.FullNode, r cid.Cid) error {
s := &apiIpldStore{ctx, api}
mp, err := adt.AsMap(s, r)
if err != nil {
@ -822,7 +847,7 @@ func handleHamtEpoch(ctx context.Context, api api.FullNode, r cid.Cid) error {
})
}
func handleHamtAddress(ctx context.Context, api api.FullNode, r cid.Cid) error {
func handleHamtAddress(ctx context.Context, api lapi.FullNode, r cid.Cid) error {
s := &apiIpldStore{ctx, api}
mp, err := adt.AsMap(s, r)
if err != nil {