Skip to content

Commit

Permalink
Merge pull request #4177 from filecoin-project/feat/chain-usage-cmd
Browse files Browse the repository at this point in the history
allow usage inspection on a chain segment
  • Loading branch information
magik6k authored Oct 5, 2020
2 parents dd1661c + e941756 commit c8822fc
Showing 1 changed file with 41 additions and 16 deletions.
57 changes: 41 additions & 16 deletions cli/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit c8822fc

Please sign in to comment.