Merge pull request #2763 from filecoin-project/feat/chain-list-gas-stats

Feat/chain list gas stats
This commit is contained in:
Łukasz Magiera 2020-08-01 23:47:05 +02:00 committed by GitHub
commit cb9406c2ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -15,6 +15,7 @@ import (
"github.com/filecoin-project/go-address"
cborutil "github.com/filecoin-project/go-cbor-util"
"github.com/filecoin-project/specs-actors/actors/abi"
"github.com/filecoin-project/specs-actors/actors/abi/big"
"github.com/filecoin-project/specs-actors/actors/builtin"
"github.com/filecoin-project/specs-actors/actors/builtin/account"
"github.com/filecoin-project/specs-actors/actors/builtin/market"
@ -27,6 +28,7 @@ import (
"golang.org/x/xerrors"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/actors"
types "github.com/filecoin-project/lotus/chain/types"
)
@ -365,6 +367,10 @@ var chainListCmd = &cli.Command{
Usage: "specify the format to print out tipsets",
Value: "<height>: (<time>) <blocks>",
},
&cli.BoolFlag{
Name: "gas-stats",
Usage: "view gas statistics for the chain",
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
@ -406,9 +412,65 @@ var chainListCmd = &cli.Command{
tss = append(tss, head)
}
if cctx.Bool("gas-stats") {
otss := make([]*types.TipSet, 0, len(tss))
for i := len(tss) - 1; i >= 0; i-- {
otss = append(otss, tss[i])
}
tss = otss
for i, ts := range tss {
fmt.Printf("%d: %d blocks\n", ts.Height(), len(ts.Blocks()))
for _, b := range ts.Blocks() {
msgs, err := api.ChainGetBlockMessages(ctx, b.Cid())
if err != nil {
return err
}
var limitSum int64
psum := big.NewInt(0)
for _, m := range msgs.BlsMessages {
limitSum += m.GasLimit
psum = big.Add(psum, m.GasPrice)
}
for _, m := range msgs.SecpkMessages {
limitSum += m.Message.GasLimit
psum = big.Add(psum, m.Message.GasPrice)
}
avgprice := big.Div(psum, big.NewInt(int64(len(msgs.BlsMessages)+len(msgs.SecpkMessages))))
fmt.Printf("\t%s: \t%d msgs, gasLimit: %d / %d (%0.2f%%), avgPrice: %s\n", b.Miner, len(msgs.BlsMessages)+len(msgs.SecpkMessages), limitSum, build.BlockGasLimit, 100*float64(limitSum)/float64(build.BlockGasLimit), avgprice)
}
if i < len(tss)-1 {
msgs, err := api.ChainGetParentMessages(ctx, tss[i+1].Blocks()[0].Cid())
if err != nil {
return err
}
var limitSum int64
for _, m := range msgs {
limitSum += m.Message.GasLimit
}
recpts, err := api.ChainGetParentReceipts(ctx, tss[i+1].Blocks()[0].Cid())
if err != nil {
return err
}
var gasUsed int64
for _, r := range recpts {
gasUsed += r.GasUsed
}
fmt.Printf("\ttipset: \t%d msgs, %d / %d (%0.2f%%)\n", len(msgs), gasUsed, limitSum, 100*float64(gasUsed)/float64(limitSum))
}
fmt.Println()
}
} else {
for i := len(tss) - 1; i >= 0; i-- {
printTipSet(cctx.String("format"), tss[i])
}
}
return nil
},
}