2019-07-09 22:58:51 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2019-10-09 05:39:49 +00:00
|
|
|
"encoding/json"
|
2019-07-09 22:58:51 +00:00
|
|
|
"fmt"
|
2020-03-10 00:44:08 +00:00
|
|
|
"sort"
|
2020-07-22 21:19:59 +00:00
|
|
|
"strconv"
|
2019-07-09 22:58:51 +00:00
|
|
|
|
2020-06-02 18:12:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-06-05 22:59:01 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-11-23 01:26:32 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-07-22 22:07:02 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-03-10 00:26:10 +00:00
|
|
|
|
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,
|
2020-07-22 21:19:59 +00:00
|
|
|
mpoolReplaceCmd,
|
2020-07-22 22:07:02 +00:00
|
|
|
mpoolFindCmd,
|
2019-07-09 22:58:51 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var mpoolPending = &cli.Command{
|
|
|
|
Name: "pending",
|
|
|
|
Usage: "Get pending messages",
|
2020-04-29 19:52:04 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
2020-04-30 11:19:37 +00:00
|
|
|
Name: "local",
|
2020-04-29 19:52:04 +00:00
|
|
|
Usage: "print pending messages for addresses in local wallet only",
|
|
|
|
},
|
|
|
|
},
|
2019-07-09 22:58:51 +00:00
|
|
|
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
|
|
|
|
2020-04-29 19:52:04 +00:00
|
|
|
var filter map[address.Address]struct{}
|
|
|
|
if cctx.Bool("local") {
|
|
|
|
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{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
msgs, err := api.MpoolPending(ctx, types.EmptyTSK)
|
2019-07-09 22:58:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, msg := range msgs {
|
2020-04-30 11:19:37 +00:00
|
|
|
if filter != nil {
|
2020-04-29 19:52:04 +00:00
|
|
|
if _, has := filter[msg.Message.From]; !has {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-09 05:39:49 +00:00
|
|
|
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",
|
2020-07-21 01:30:21 +00:00
|
|
|
Usage: "Subscribe to mpool changes",
|
2019-11-17 07:44:06 +00:00
|
|
|
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
|
|
|
|
}
|
2020-03-10 00:44:08 +00:00
|
|
|
type mpStat struct {
|
|
|
|
addr string
|
|
|
|
past, cur, future uint64
|
|
|
|
}
|
2019-11-23 01:26:32 +00:00
|
|
|
|
|
|
|
var mpoolStat = &cli.Command{
|
|
|
|
Name: "stat",
|
|
|
|
Usage: "print mempool stats",
|
2020-04-29 19:52:04 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
2020-04-30 11:19:37 +00:00
|
|
|
Name: "local",
|
2020-04-29 19:52:04 +00:00
|
|
|
Usage: "print stats for addresses in local wallet only",
|
|
|
|
},
|
|
|
|
},
|
2019-11-23 01:26:32 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-04-29 19:52:04 +00:00
|
|
|
var filter map[address.Address]struct{}
|
|
|
|
if cctx.Bool("local") {
|
|
|
|
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{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:29:45 +00:00
|
|
|
msgs, err := api.MpoolPending(ctx, types.EmptyTSK)
|
2019-11-23 01:26:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
buckets := map[address.Address]*statBucket{}
|
|
|
|
|
|
|
|
for _, v := range msgs {
|
2020-04-30 11:19:37 +00:00
|
|
|
if filter != nil {
|
2020-04-29 19:52:04 +00:00
|
|
|
if _, has := filter[v.Message.From]; !has {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-23 01:26:32 +00:00
|
|
|
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
|
|
|
|
}
|
2020-03-10 00:44:08 +00:00
|
|
|
|
|
|
|
var out []mpStat
|
|
|
|
|
2019-11-23 01:26:32 +00:00
|
|
|
for a, bkt := range buckets {
|
2020-02-11 23:29:45 +00:00
|
|
|
act, err := api.StateGetActor(ctx, a, ts.Key())
|
2019-11-23 01:26:32 +00:00
|
|
|
if err != nil {
|
2020-01-20 15:18:02 +00:00
|
|
|
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++
|
|
|
|
}
|
|
|
|
|
2020-03-10 00:44:08 +00:00
|
|
|
past := uint64(0)
|
|
|
|
future := uint64(0)
|
2019-11-23 19:01:56 +00:00
|
|
|
for _, m := range bkt.msgs {
|
|
|
|
if m.Message.Nonce < act.Nonce {
|
|
|
|
past++
|
|
|
|
}
|
|
|
|
if m.Message.Nonce > cur {
|
|
|
|
future++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 00:44:08 +00:00
|
|
|
out = append(out, mpStat{
|
|
|
|
addr: a.String(),
|
|
|
|
past: past,
|
|
|
|
cur: cur - act.Nonce,
|
|
|
|
future: future,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(out, func(i, j int) bool {
|
|
|
|
return out[i].addr < out[j].addr
|
|
|
|
})
|
|
|
|
|
2020-07-27 16:23:01 +00:00
|
|
|
var total mpStat
|
|
|
|
|
2020-03-10 00:44:08 +00:00
|
|
|
for _, stat := range out {
|
2020-07-27 16:23:01 +00:00
|
|
|
total.past += stat.past
|
|
|
|
total.cur += stat.cur
|
|
|
|
total.future += stat.future
|
|
|
|
|
|
|
|
fmt.Printf("%s: past: %d, cur: %d, future: %d\n", stat.addr, stat.past, stat.cur, stat.future)
|
2019-11-23 01:26:32 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 16:23:01 +00:00
|
|
|
fmt.Println("-----")
|
|
|
|
fmt.Printf("total: past: %d, cur: %d, future: %d\n", total.past, total.cur, total.future)
|
|
|
|
|
2019-11-23 01:26:32 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2020-07-22 21:19:59 +00:00
|
|
|
|
|
|
|
var mpoolReplaceCmd = &cli.Command{
|
|
|
|
Name: "replace",
|
|
|
|
Usage: "replace a message in the mempool",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.Int64Flag{
|
|
|
|
Name: "gas-price",
|
|
|
|
Usage: "gas price for new message",
|
|
|
|
},
|
|
|
|
&cli.Int64Flag{
|
|
|
|
Name: "gas-limit",
|
|
|
|
Usage: "gas price for new message",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ArgsUsage: "[from] [nonce]",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.Args().Len() < 2 {
|
|
|
|
return cli.ShowCommandHelp(cctx, cctx.Command.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
from, err := address.NewFromString(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
nonce, err := strconv.ParseUint(cctx.Args().Get(1), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
pending, err := api.MpoolPending(ctx, ts.Key())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var found *types.SignedMessage
|
|
|
|
for _, p := range pending {
|
|
|
|
if p.Message.From == from && p.Message.Nonce == nonce {
|
|
|
|
found = p
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found == nil {
|
|
|
|
return fmt.Errorf("no pending message found from %s with nonce %d", from, nonce)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := found.Message
|
|
|
|
|
|
|
|
msg.GasLimit = cctx.Int64("gas-limit")
|
|
|
|
msg.GasPrice = types.NewInt(uint64(cctx.Int64("gas-price")))
|
|
|
|
|
|
|
|
smsg, err := api.WalletSignMessage(ctx, msg.From, &msg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to sign message: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cid, err := api.MpoolPush(ctx, smsg)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to push new message to mempool: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("new message cid: ", cid)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2020-07-22 22:07:02 +00:00
|
|
|
|
|
|
|
var mpoolFindCmd = &cli.Command{
|
|
|
|
Name: "find",
|
|
|
|
Usage: "find a message in the mempool",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "from",
|
|
|
|
Usage: "search for messages with given 'from' address",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "to",
|
|
|
|
Usage: "search for messages with given 'to' address",
|
|
|
|
},
|
|
|
|
&cli.Int64Flag{
|
|
|
|
Name: "method",
|
|
|
|
Usage: "search for messages with given method",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ctx := ReqContext(cctx)
|
|
|
|
|
|
|
|
pending, err := api.MpoolPending(ctx, types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var toFilter, fromFilter address.Address
|
|
|
|
if cctx.IsSet("to") {
|
|
|
|
a, err := address.NewFromString(cctx.String("to"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("'to' address was invalid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
toFilter = a
|
|
|
|
}
|
|
|
|
|
|
|
|
if cctx.IsSet("from") {
|
|
|
|
a, err := address.NewFromString(cctx.String("from"))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("'from' address was invalid: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fromFilter = a
|
|
|
|
}
|
|
|
|
|
|
|
|
var methodFilter *abi.MethodNum
|
|
|
|
if cctx.IsSet("method") {
|
|
|
|
m := abi.MethodNum(cctx.Int64("method"))
|
|
|
|
methodFilter = &m
|
|
|
|
}
|
|
|
|
|
|
|
|
var out []*types.SignedMessage
|
|
|
|
for _, m := range pending {
|
|
|
|
if toFilter != address.Undef && m.Message.To != toFilter {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if fromFilter != address.Undef && m.Message.From != fromFilter {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if methodFilter != nil && *methodFilter != m.Message.Method {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
out = append(out, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := json.MarshalIndent(out, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(b))
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|