From 7747f5a3fafe282212769cec5bdfdf3d9b3ddd96 Mon Sep 17 00:00:00 2001 From: whyrusleeping Date: Wed, 9 Sep 2020 11:23:48 -0700 Subject: [PATCH 1/2] add basic command for printing gas performance of messages in the mempool --- cli/mpool.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/cli/mpool.go b/cli/mpool.go index 6561e2cbc..add3af7bd 100644 --- a/cli/mpool.go +++ b/cli/mpool.go @@ -29,6 +29,7 @@ var mpoolCmd = &cli.Command{ mpoolReplaceCmd, mpoolFindCmd, mpoolConfig, + mpoolGasPerfCmd, }, } @@ -516,3 +517,65 @@ var mpoolConfig = &cli.Command{ return nil }, } + +var mpoolGasPerfCmd = &cli.Command{ + Name: "gas-perf", + Usage: "Check gas performance of messages in mempool", + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "all", + Usage: "print gas performance for all mempool messages (default only prints for local)", + }, + }, + Action: func(cctx *cli.Context) error { + api, closer, err := GetFullNodeAPI(cctx) + if err != nil { + return err + } + defer closer() + + ctx := ReqContext(cctx) + + msgs, err := api.MpoolPending(ctx, types.EmptyTSK) + if err != nil { + return err + } + + var filter map[address.Address]struct{} + if !cctx.Bool("all") { + filter = map[address.Address]struct{}{} + + addrss, err := api.WalletList(ctx) + if err != nil { + return xerrors.Errorf("getting local addresses: %w", err) + } + + for _, a := range addrss { + filter[a] = struct{}{} + } + + var filtered []*types.SignedMessage + for _, msg := range msgs { + if _, has := filter[msg.Message.From]; !has { + continue + } + filtered = append(filtered, msg) + } + msgs = filtered + } + + ts, err := api.ChainHead(ctx) + if err != nil { + return xerrors.Errorf("failed to get chain head: %w", err) + } + + baseFee := ts.Blocks()[0].ParentBaseFee + for _, m := range msgs { + feeCapSlack := types.BigSub(m.Message.GasFeeCap, baseFee) + + fmt.Printf("%s\t%d\t%s\n", m.Message.From, m.Message.Nonce, feeCapSlack) + } + + return nil + }, +} From f7d3d371f7eb7de5b8700fdb288f0fb733e6d634 Mon Sep 17 00:00:00 2001 From: vyzo Date: Wed, 9 Sep 2020 22:11:51 +0300 Subject: [PATCH 2/2] print reward and actual gas performancec --- cli/mpool.go | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/cli/mpool.go b/cli/mpool.go index add3af7bd..6ae94356a 100644 --- a/cli/mpool.go +++ b/cli/mpool.go @@ -3,6 +3,7 @@ package cli import ( "encoding/json" "fmt" + stdbig "math/big" "sort" "strconv" @@ -14,6 +15,7 @@ import ( "github.com/filecoin-project/go-state-types/big" lapi "github.com/filecoin-project/lotus/api" + "github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/chain/messagepool" "github.com/filecoin-project/lotus/chain/types" ) @@ -570,10 +572,31 @@ var mpoolGasPerfCmd = &cli.Command{ } baseFee := ts.Blocks()[0].ParentBaseFee - for _, m := range msgs { - feeCapSlack := types.BigSub(m.Message.GasFeeCap, baseFee) - fmt.Printf("%s\t%d\t%s\n", m.Message.From, m.Message.Nonce, feeCapSlack) + bigBlockGasLimit := big.NewInt(build.BlockGasLimit) + + getGasReward := func(msg *types.SignedMessage) big.Int { + maxPremium := types.BigSub(msg.Message.GasFeeCap, baseFee) + if types.BigCmp(maxPremium, msg.Message.GasPremium) < 0 { + maxPremium = msg.Message.GasPremium + } + return types.BigMul(maxPremium, types.NewInt(uint64(msg.Message.GasLimit))) + } + + getGasPerf := func(gasReward big.Int, gasLimit int64) float64 { + // gasPerf = gasReward * build.BlockGasLimit / gasLimit + a := new(stdbig.Rat).SetInt(new(stdbig.Int).Mul(gasReward.Int, bigBlockGasLimit.Int)) + b := stdbig.NewRat(1, gasLimit) + c := new(stdbig.Rat).Mul(a, b) + r, _ := c.Float64() + return r + } + + for _, m := range msgs { + gasReward := getGasReward(m) + gasPerf := getGasPerf(gasReward, m.Message.GasLimit) + + fmt.Printf("%s\t%d\t%s\t%f\n", m.Message.From, m.Message.Nonce, gasReward, gasPerf) } return nil