2020-07-22 17:14:45 +00:00
package main
import (
"fmt"
2023-09-07 09:19:10 +00:00
"time"
2020-07-22 17:14:45 +00:00
2022-06-14 15:00:51 +00:00
"github.com/urfave/cli/v2"
2020-07-27 17:59:30 +00:00
"github.com/filecoin-project/lotus/build"
2020-07-30 17:32:10 +00:00
"github.com/filecoin-project/lotus/chain/types"
2020-07-22 17:14:45 +00:00
lcli "github.com/filecoin-project/lotus/cli"
)
var mpoolCmd = & cli . Command {
Name : "mpool" ,
Usage : "Tools for diagnosing mempool issues" ,
Flags : [ ] cli . Flag { } ,
Subcommands : [ ] * cli . Command {
minerSelectMsgsCmd ,
2021-03-29 16:54:08 +00:00
mpoolClear ,
2020-07-22 17:14:45 +00:00
} ,
}
var minerSelectMsgsCmd = & cli . Command {
2022-05-03 18:35:06 +00:00
Name : "miner-select-messages" ,
Aliases : [ ] string { "miner-select-msgs" } ,
2020-08-11 11:05:04 +00:00
Flags : [ ] cli . Flag {
& cli . Float64Flag {
Name : "ticket-quality" ,
Value : 1 ,
} ,
} ,
2020-07-22 17:14:45 +00:00
Action : func ( cctx * cli . Context ) error {
api , closer , err := lcli . GetFullNodeAPI ( cctx )
if err != nil {
return err
}
defer closer ( )
ctx := lcli . ReqContext ( cctx )
head , err := api . ChainHead ( ctx )
if err != nil {
return err
}
2023-09-07 09:19:10 +00:00
// Get the size of the mempool
pendingMsgs , err := api . MpoolPending ( ctx , types . EmptyTSK )
if err != nil {
return err
}
mpoolSize := len ( pendingMsgs )
// Measure the time taken by MpoolSelect
startTime := time . Now ( )
2020-08-11 11:05:04 +00:00
msgs , err := api . MpoolSelect ( ctx , head . Key ( ) , cctx . Float64 ( "ticket-quality" ) )
2020-07-22 17:14:45 +00:00
if err != nil {
return err
}
2023-09-07 09:19:10 +00:00
duration := time . Since ( startTime )
2020-07-22 17:14:45 +00:00
2020-07-27 17:59:30 +00:00
var totalGas int64
2020-08-06 07:39:11 +00:00
for i , f := range msgs {
2020-07-30 17:32:10 +00:00
from := f . Message . From . String ( )
if len ( from ) > 8 {
from = "..." + from [ len ( from ) - 8 : ]
}
to := f . Message . To . String ( )
if len ( to ) > 8 {
to = "..." + to [ len ( to ) - 8 : ]
}
2020-08-06 21:08:42 +00:00
fmt . Printf ( "%d: %s -> %s, method %d, gasFeecap %s, gasPremium %s, gasLimit %d, val %s\n" , i , from , to , f . Message . Method , f . Message . GasFeeCap , f . Message . GasPremium , f . Message . GasLimit , types . FIL ( f . Message . Value ) )
2020-07-27 17:59:30 +00:00
totalGas += f . Message . GasLimit
}
2023-09-07 09:27:28 +00:00
// Log the duration, size of the mempool, selected messages and total gas limit of selected messages
2023-09-07 09:19:10 +00:00
fmt . Printf ( "Message selection took %s\n" , duration )
fmt . Printf ( "Size of the mempool: %d\n" , mpoolSize )
2020-08-06 07:39:11 +00:00
fmt . Println ( "selected messages: " , len ( msgs ) )
2020-07-27 17:59:30 +00:00
fmt . Printf ( "total gas limit of selected messages: %d / %d (%0.2f%%)\n" , totalGas , build . BlockGasLimit , 100 * float64 ( totalGas ) / float64 ( build . BlockGasLimit ) )
2020-07-22 17:14:45 +00:00
return nil
} ,
}
2021-03-29 16:54:08 +00:00
var mpoolClear = & cli . Command {
Name : "clear" ,
Usage : "Clear all pending messages from the mpool (USE WITH CARE)" ,
Flags : [ ] cli . Flag {
& cli . BoolFlag {
Name : "local" ,
Usage : "also clear local messages" ,
} ,
& cli . BoolFlag {
Name : "really-do-it" ,
Usage : "must be specified for the action to take effect" ,
} ,
} ,
Action : func ( cctx * cli . Context ) error {
api , closer , err := lcli . GetFullNodeAPI ( cctx )
if err != nil {
return err
}
defer closer ( )
really := cctx . Bool ( "really-do-it" )
if ! really {
//nolint:golint
return fmt . Errorf ( "--really-do-it must be specified for this action to have an effect; you have been warned" )
}
local := cctx . Bool ( "local" )
ctx := lcli . ReqContext ( cctx )
return api . MpoolClear ( ctx , local )
} ,
}