-
Notifications
You must be signed in to change notification settings - Fork 465
/
mpool.go
750 lines (633 loc) · 18.3 KB
/
mpool.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
package cmd
import (
"context"
"encoding/json"
"errors"
"fmt"
"sort"
"strconv"
stdbig "math/big"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/ipfs/go-cid"
cmds "github.com/ipfs/go-ipfs-cmds"
"github.com/filecoin-project/venus/app/node"
"github.com/filecoin-project/venus/pkg/config"
"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/pkg/messagepool"
"github.com/filecoin-project/venus/venus-shared/types"
)
var mpoolCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Manage message pool",
},
Subcommands: map[string]*cmds.Command{
"pending": mpoolPending,
"clear": mpoolClear,
"sub": mpoolSub,
"stat": mpoolStat,
"replace": mpoolReplaceCmd,
"find": mpoolFindCmd,
"config": mpoolConfig,
"gas-perf": mpoolGasPerfCmd,
"publish": mpoolPublish,
"delete": mpoolDeleteAddress,
"select": mpoolSelect,
},
}
var mpoolDeleteAddress = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "delete",
ShortDescription: "delete message by address",
},
Options: []cmds.Option{
cmds.StringOption("from", "optionally specify the wallet for publish message"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
ctx := context.TODO()
from, _ := req.Options["from"].(string)
if from == "" {
return fmt.Errorf("address can`t be null")
}
addr, err := address.NewFromString(from)
if err != nil {
return err
}
err = env.(*node.Env).MessagePoolAPI.MpoolDeleteByAdress(ctx, addr)
if err != nil {
return err
}
return nil
},
}
var mpoolSelect = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "select",
ShortDescription: "select message from mpool",
},
Options: []cmds.Option{
cmds.FloatOption("quality", "optionally specify the wallet for publish message").WithDefault(float64(0.5)),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
ctx := context.TODO()
quality, _ := req.Options["quality"].(float64)
head, err := env.(*node.Env).ChainAPI.ChainHead(ctx)
if err != nil {
return err
}
msgs, err := env.(*node.Env).MessagePoolAPI.MpoolSelect(ctx, head.Key(), quality)
if err != nil {
return err
}
selectMsg, err := json.MarshalIndent(msgs, " ", "\t")
if err != nil {
return err
}
return printOneString(re, string(selectMsg))
},
}
var mpoolPublish = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "publish",
ShortDescription: "publish pending messages",
},
Options: []cmds.Option{
cmds.StringOption("from", "optionally specify the wallet for publish message"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
from, _ := req.Options["from"].(string)
ctx := context.TODO()
var fromAddr address.Address
if from == "" {
defaddr, err := env.(*node.Env).WalletAPI.WalletDefaultAddress(req.Context)
if err != nil {
return err
}
fromAddr = defaddr
} else {
addr, err := address.NewFromString(from)
if err != nil {
return err
}
fromAddr = addr
}
err := env.(*node.Env).MessagePoolAPI.MpoolPublishByAddr(ctx, fromAddr)
if err != nil {
return err
}
return nil
},
}
var mpoolFindCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "find",
ShortDescription: "find a message in the mempool",
},
Options: []cmds.Option{
cmds.StringOption("from", "search for messages with given 'from' address"),
cmds.StringOption("to", "search for messages with given 'to' address"),
cmds.Int64Option("method", "search for messages with given method"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
from, _ := req.Options["from"].(string)
to, _ := req.Options["to"].(string)
method, _ := req.Options["method"].(int64)
ctx := context.TODO()
pending, err := env.(*node.Env).MessagePoolAPI.MpoolPending(ctx, types.TipSetKey{})
if err != nil {
return err
}
var toFilter, fromFilter address.Address
if len(to) > 0 {
a, err := address.NewFromString(to)
if err != nil {
return fmt.Errorf("'to' address was invalid: %w", err)
}
toFilter = a
}
if len(from) > 0 {
a, err := address.NewFromString(from)
if err != nil {
return fmt.Errorf("'from' address was invalid: %w", err)
}
fromFilter = a
}
var methodFilter *abi.MethodNum
if method > 0 {
m := abi.MethodNum(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)
}
_ = re.Emit(out)
return nil
},
}
var mpoolReplaceCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "replace",
ShortDescription: "replace a message in the mempool",
},
Arguments: []cmds.Argument{
cmds.StringArg("from", false, true, "from"),
cmds.StringArg("nonce", false, true, "nonce"),
cmds.StringArg("message-cid", false, true, "message-cid"),
},
Options: []cmds.Option{
feecapOption,
premiumOption,
limitOption,
cmds.BoolOption("auto", "automatically reprice the specified message"),
cmds.StringOption("max-fee", "Spend up to X FIL for this message (applicable for auto mode)"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
ctx := requestContext(req)
feecap, premium, gasLimit, err := parseGasOptions(req)
if err != nil {
return err
}
auto, _ := req.Options["auto"].(bool)
maxFee, _ := req.Options["max-fee"].(string)
var from address.Address
var nonce uint64
switch len(req.Arguments) {
case 1:
mcid, err := cid.Decode(req.Arguments[0])
if err != nil {
return err
}
msg, err := env.(*node.Env).ChainAPI.ChainGetMessage(ctx, mcid)
if err != nil {
return fmt.Errorf("could not find referenced message: %w", err)
}
from = msg.From
nonce = msg.Nonce
case 2:
f, err := address.NewFromString(req.Arguments[0])
if err != nil {
return err
}
n, err := strconv.ParseUint(req.Arguments[1], 10, 64)
if err != nil {
return err
}
from = f
nonce = n
default:
return errors.New("command syntax error")
}
ts, err := env.(*node.Env).ChainAPI.ChainHead(ctx)
if err != nil {
return fmt.Errorf("getting chain head: %w", err)
}
pending, err := env.(*node.Env).MessagePoolAPI.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
if auto {
cfg, err := getEnv(env).MessagePoolAPI.MpoolGetConfig(ctx)
if err != nil {
return fmt.Errorf("failed to lookup the message pool config: %w", err)
}
defaultRBF := messagepool.ComputeRBF(msg.GasPremium, cfg.ReplaceByFeeRatio)
var mss *types.MessageSendSpec
if len(maxFee) > 0 {
maxFee, err := big.FromString(maxFee)
if err != nil {
return fmt.Errorf("parsing max-spend: %w", err)
}
mss = &types.MessageSendSpec{
MaxFee: maxFee,
}
}
// msg.GasLimit = 0 // TODO: need to fix the way we estimate gas limits to account for the messages already being in the mempool
msg.GasFeeCap = abi.NewTokenAmount(0)
msg.GasPremium = abi.NewTokenAmount(0)
retm, err := env.(*node.Env).MessagePoolAPI.GasEstimateMessageGas(req.Context, &msg, mss, types.TipSetKey{})
if err != nil {
return fmt.Errorf("failed to estimate gas values: %w", err)
}
msg.GasPremium = big.Max(retm.GasPremium, defaultRBF)
msg.GasFeeCap = big.Max(retm.GasFeeCap, msg.GasPremium)
mff := func() (abi.TokenAmount, error) {
return abi.TokenAmount{Int: config.DefaultDefaultMaxFee.Int}, nil
}
messagepool.CapGasFee(mff, &msg, mss)
} else {
msg.GasFeeCap = abi.NewTokenAmount(0)
msg.GasPremium = abi.NewTokenAmount(0)
newMsg, err := env.(*node.Env).MessagePoolAPI.GasEstimateMessageGas(req.Context, &msg, nil, types.TipSetKey{})
if err != nil {
return fmt.Errorf("failed to estimate gas values: %w", err)
}
msg = *newMsg
if gasLimit > 0 {
msg.GasLimit = gasLimit
}
if err == nil && premium.Int64() != 0 {
msg.GasPremium = premium
}
// TODO: estimate fee cap here
msg.GasFeeCap = feecap
}
smsg, err := env.(*node.Env).WalletAPI.WalletSignMessage(req.Context, msg.From, &msg)
if err != nil {
return fmt.Errorf("failed to sign message: %w", err)
}
cid, err := env.(*node.Env).MessagePoolAPI.MpoolPush(req.Context, smsg)
if err != nil {
return fmt.Errorf("failed to push new message to mempool: %w", err)
}
_ = re.Emit(fmt.Sprintf("new message cid: %s", cid))
return nil
},
}
var mpoolStat = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "print mpool state messages",
ShortDescription: `
Get pending messages.
`,
},
Options: []cmds.Option{
cmds.BoolOption("local", "print stats for addresses in local wallet only"),
cmds.Int64Option("basefee-lookback", "number of blocks to look back for minimum basefee"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
local, _ := req.Options["local"].(bool)
basefee, _ := req.Options["basefee-lookback"].(int)
ctx := context.TODO()
ts, err := env.(*node.Env).ChainAPI.ChainHead(ctx)
if err != nil {
return fmt.Errorf("getting chain head: %w", err)
}
currBF := ts.Blocks()[0].ParentBaseFee
minBF := currBF
{
currTS := ts
for i := 0; i < basefee; i++ {
key := currTS.Parents()
currTS, err = env.(*node.Env).ChainAPI.ChainGetTipSet(req.Context, key)
if err != nil {
return fmt.Errorf("walking chain: %w", err)
}
if newBF := currTS.Blocks()[0].ParentBaseFee; newBF.LessThan(minBF) {
minBF = newBF
}
}
}
var filter map[address.Address]struct{}
if local {
filter = map[address.Address]struct{}{}
addrss := env.(*node.Env).WalletAPI.WalletAddresses(req.Context)
for _, a := range addrss {
filter[a] = struct{}{}
}
}
msgs, err := env.(*node.Env).MessagePoolAPI.MpoolPending(ctx, types.TipSetKey{})
if err != nil {
return err
}
type statBucket struct {
msgs map[uint64]*types.SignedMessage
}
type mpStat struct {
addr string
past, cur, future uint64
belowCurr, belowPast uint64
gasLimit big.Int
}
buckets := map[address.Address]*statBucket{}
for _, v := range msgs {
if filter != nil {
if _, has := filter[v.Message.From]; !has {
continue
}
}
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
}
var out []mpStat
for a, bkt := range buckets {
act, err := env.(*node.Env).ChainAPI.StateGetActor(ctx, a, ts.Key())
if err != nil {
fmt.Printf("%s, err: %s\n", a, err)
continue
}
cur := act.Nonce
for {
_, ok := bkt.msgs[cur]
if !ok {
break
}
cur++
}
var s mpStat
s.addr = a.String()
s.gasLimit = big.Zero()
for _, m := range bkt.msgs {
if m.Message.Nonce < act.Nonce {
s.past++
} else if m.Message.Nonce > cur {
s.future++
} else {
s.cur++
}
if m.Message.GasFeeCap.LessThan(currBF) {
s.belowCurr++
}
if m.Message.GasFeeCap.LessThan(minBF) {
s.belowPast++
}
s.gasLimit = big.Add(s.gasLimit, big.NewInt(m.Message.GasLimit))
}
out = append(out, s)
}
sort.Slice(out, func(i, j int) bool {
return out[i].addr < out[j].addr
})
var total mpStat
total.gasLimit = big.Zero()
for _, stat := range out {
total.past += stat.past
total.cur += stat.cur
total.future += stat.future
total.belowCurr += stat.belowCurr
total.belowPast += stat.belowPast
total.gasLimit = big.Add(total.gasLimit, stat.gasLimit)
_ = re.Emit(fmt.Sprintf("%s: Nonce past: %d, cur: %d, future: %d; FeeCap cur: %d, min-%d: %d, gasLimit: %s", stat.addr, stat.past, stat.cur, stat.future, stat.belowCurr, basefee, stat.belowPast, stat.gasLimit))
}
_ = re.Emit("-----")
_ = re.Emit(fmt.Sprintf("total: Nonce past: %d, cur: %d, future: %d; FeeCap cur: %d, min-%d: %d, gasLimit: %s", total.past, total.cur, total.future, total.belowCurr, basefee, total.belowPast, total.gasLimit))
return nil
},
}
var mpoolPending = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "Get pending messages",
ShortDescription: `
Get pending messages.
`,
},
Options: []cmds.Option{
cmds.BoolOption("local", "print pending messages for addresses in local wallet only"),
cmds.BoolOption("cids", "only print cids of messages in output"),
cmds.StringOption("to", "return messages to a given address"),
cmds.StringOption("from", "return messages from a given address"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
local, _ := req.Options["local"].(bool)
cids, _ := req.Options["cids"].(bool)
to, _ := req.Options["to"].(string)
from, _ := req.Options["from"].(string)
var toa, froma address.Address
if to != "" {
a, err := address.NewFromString(to)
if err != nil {
return fmt.Errorf("given 'to' address %q was invalid: %w", to, err)
}
toa = a
}
if from != "" {
a, err := address.NewFromString(from)
if err != nil {
return fmt.Errorf("given 'to' address %q was invalid: %w", from, err)
}
froma = a
}
var filter map[address.Address]struct{}
if local {
filter = map[address.Address]struct{}{}
addrss := env.(*node.Env).WalletAPI.WalletAddresses(req.Context)
for _, a := range addrss {
filter[a] = struct{}{}
}
}
msgs, err := env.(*node.Env).MessagePoolAPI.MpoolPending(req.Context, types.TipSetKey{})
if err != nil {
return err
}
for _, msg := range msgs {
if filter != nil {
if _, has := filter[msg.Message.From]; !has {
continue
}
}
if toa != address.Undef && msg.Message.To != toa {
continue
}
if froma != address.Undef && msg.Message.From != froma {
continue
}
if cids {
_ = re.Emit(msg.Cid())
_ = re.Emit(err)
} else {
_ = re.Emit(msg)
}
}
return nil
},
}
var mpoolClear = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "clear",
ShortDescription: `
Clear all pending messages from the mpool (USE WITH CARE)
`,
},
Options: []cmds.Option{
cmds.BoolOption("local", "also clear local messages"),
cmds.BoolOption("really-do-it", "must be specified for the action to take effect"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
local, _ := req.Options["local"].(bool)
really, _ := req.Options["really-do-it"].(bool)
if !really {
//nolint:golint
return fmt.Errorf("--really-do-it must be specified for this action to have an effect; you have been warned")
}
return env.(*node.Env).MessagePoolAPI.MpoolClear(context.TODO(), local)
},
}
var mpoolSub = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "sub",
ShortDescription: `
Subscribe to mpool changes
`,
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
ctx := context.TODO()
sub, err := env.(*node.Env).MessagePoolAPI.MpoolSub(ctx)
if err != nil {
return err
}
for {
select {
case update := <-sub:
_ = re.Emit(update)
case <-ctx.Done():
return nil
}
}
},
}
var mpoolConfig = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "config",
ShortDescription: "get or set current mpool configuration",
},
Arguments: []cmds.Argument{
cmds.StringArg("cfg", false, false, "config"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
ctx := context.TODO()
if len(req.Arguments) > 0 {
cfg := new(types.MpoolConfig)
paras := req.Arguments[0]
err := json.Unmarshal([]byte(paras), cfg)
if err != nil {
return err
}
return env.(*node.Env).MessagePoolAPI.MpoolSetConfig(ctx, cfg)
}
cfg, err := env.(*node.Env).MessagePoolAPI.MpoolGetConfig(ctx)
if err != nil {
return err
}
_ = re.Emit(cfg)
return nil
},
}
var mpoolGasPerfCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "gas-perf",
ShortDescription: `
Check gas performance of messages in mempool
`,
},
Options: []cmds.Option{
cmds.BoolOption("all", "print gas performance for all mempool messages (default only prints for local)"),
},
Run: func(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
all, _ := req.Options["all"].(bool)
ctx := context.TODO()
msgs, err := env.(*node.Env).MessagePoolAPI.MpoolPending(ctx, types.TipSetKey{})
if err != nil {
return err
}
var filter map[address.Address]struct{}
if !all {
filter = map[address.Address]struct{}{}
addrss := env.(*node.Env).WalletAPI.WalletAddresses(req.Context)
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 := env.(*node.Env).ChainAPI.ChainHead(ctx)
if err != nil {
return fmt.Errorf("failed to get chain head: %w", err)
}
baseFee := ts.Blocks()[0].ParentBaseFee
bigBlockGasLimit := big.NewInt(constants.BlockGasLimit)
getGasReward := func(msg *types.SignedMessage) big.Int {
maxPremium := big.Sub(msg.Message.GasFeeCap, baseFee)
if big.Cmp(maxPremium, msg.Message.GasPremium) < 0 {
maxPremium = msg.Message.GasPremium
}
return big.Mul(maxPremium, big.NewInt(msg.Message.GasLimit))
}
getGasPerf := func(gasReward big.Int, gasLimit int64) float64 {
// gasPerf = gasReward * constants.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)
_ = re.Emit(fmt.Sprintf("%s %d %s %f", m.Message.From, m.Message.Nonce, gasReward, gasPerf))
}
return nil
},
}