lotus/cli/mpool.go

157 lines
2.6 KiB
Go
Raw Normal View History

2019-07-09 22:58:51 +00:00
package cli
import (
"encoding/json"
2019-07-09 22:58:51 +00:00
"fmt"
2019-11-23 01:26:32 +00:00
"golang.org/x/xerrors"
2019-07-09 22:58:51 +00:00
"gopkg.in/urfave/cli.v2"
2019-11-23 01:26:32 +00:00
"github.com/filecoin-project/go-address"
2019-11-23 01:26:32 +00:00
"github.com/filecoin-project/lotus/chain/types"
2019-07-09 22:58:51 +00:00
)
var mpoolCmd = &cli.Command{
Name: "mpool",
Usage: "Manage message pool",
Subcommands: []*cli.Command{
mpoolPending,
2019-11-17 07:44:06 +00:00
mpoolSub,
2019-11-23 01:26:32 +00:00
mpoolStat,
2019-07-09 22:58:51 +00:00
},
}
var mpoolPending = &cli.Command{
Name: "pending",
Usage: "Get pending messages",
Action: func(cctx *cli.Context) error {
2019-10-03 18:12:30 +00:00
api, closer, err := GetFullNodeAPI(cctx)
2019-07-12 04:09:04 +00:00
if err != nil {
return err
}
2019-10-03 18:12:30 +00:00
defer closer()
2019-07-12 04:09:04 +00:00
2019-07-18 23:16:23 +00:00
ctx := ReqContext(cctx)
2019-07-09 22:58:51 +00:00
2019-07-11 02:36:43 +00:00
msgs, err := api.MpoolPending(ctx, nil)
2019-07-09 22:58:51 +00:00
if err != nil {
return err
}
for _, msg := range msgs {
out, err := json.MarshalIndent(msg, "", " ")
if err != nil {
return err
}
fmt.Println(string(out))
2019-07-09 22:58:51 +00:00
}
return nil
},
}
2019-11-17 07:44:06 +00:00
var mpoolSub = &cli.Command{
Name: "sub",
Usage: "Subscibe to mpool changes",
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
sub, err := api.MpoolSub(ctx)
if err != nil {
return err
}
for {
select {
case update := <-sub:
out, err := json.MarshalIndent(update, "", " ")
if err != nil {
return err
}
fmt.Println(string(out))
case <-ctx.Done():
return nil
}
}
},
}
2019-11-23 01:26:32 +00:00
type statBucket struct {
msgs map[uint64]*types.SignedMessage
}
var mpoolStat = &cli.Command{
Name: "stat",
Usage: "print mempool stats",
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := ReqContext(cctx)
ts, err := api.ChainHead(ctx)
if err != nil {
return xerrors.Errorf("getting chain head: %w", err)
}
msgs, err := api.MpoolPending(ctx, nil)
if err != nil {
return err
}
buckets := map[address.Address]*statBucket{}
for _, v := range msgs {
bkt, ok := buckets[v.Message.From]
if !ok {
bkt = &statBucket{
msgs: map[uint64]*types.SignedMessage{},
}
buckets[v.Message.From] = bkt
}
bkt.msgs[v.Message.Nonce] = v
}
for a, bkt := range buckets {
act, err := api.StateGetActor(ctx, a, ts)
if err != nil {
fmt.Printf("%s, err: %s\n", a, err)
continue
2019-11-23 01:26:32 +00:00
}
cur := act.Nonce
for {
_, ok := bkt.msgs[cur]
if !ok {
break
}
cur++
}
2019-11-23 19:01:56 +00:00
past := 0
future := 0
for _, m := range bkt.msgs {
if m.Message.Nonce < act.Nonce {
past++
}
if m.Message.Nonce > cur {
future++
}
}
fmt.Printf("%s, past: %d, cur: %d, future: %d\n", a, past, cur-act.Nonce, future)
2019-11-23 01:26:32 +00:00
}
return nil
},
}