lotus/cmd/lotus-sim/info.go

111 lines
3.1 KiB
Go
Raw Normal View History

2021-06-07 22:05:52 +00:00
package main
import (
2021-06-08 18:23:51 +00:00
"context"
2021-06-07 22:05:52 +00:00
"fmt"
2021-06-10 04:27:48 +00:00
"io"
2021-06-07 22:05:52 +00:00
"text/tabwriter"
2021-06-08 18:23:51 +00:00
"time"
2021-06-07 22:05:52 +00:00
"github.com/urfave/cli/v2"
2021-06-08 18:23:51 +00:00
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/lotus/chain/actors/builtin"
2021-06-08 18:23:51 +00:00
"github.com/filecoin-project/lotus/chain/actors/builtin/power"
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/filecoin-project/lotus/chain/types"
2021-06-10 04:27:48 +00:00
"github.com/filecoin-project/lotus/cmd/lotus-sim/simulation"
2021-06-07 22:05:52 +00:00
)
2021-06-08 18:23:51 +00:00
func getTotalPower(ctx context.Context, sm *stmgr.StateManager, ts *types.TipSet) (power.Claim, error) {
actor, err := sm.LoadActor(ctx, power.Address, ts)
if err != nil {
return power.Claim{}, err
}
state, err := power.Load(sm.ChainStore().ActorStore(ctx), actor)
if err != nil {
return power.Claim{}, err
}
return state.TotalPower()
}
2021-06-10 04:27:48 +00:00
func printInfo(ctx context.Context, sim *simulation.Simulation, out io.Writer) error {
head := sim.GetHead()
start := sim.GetStart()
powerNow, err := getTotalPower(ctx, sim.StateManager, head)
if err != nil {
return err
}
powerLookbackEpoch := head.Height() - builtin.EpochsInDay*2
if powerLookbackEpoch < start.Height() {
powerLookbackEpoch = start.Height()
}
lookbackTs, err := sim.Node.Chainstore.GetTipsetByHeight(ctx, powerLookbackEpoch, head, false)
2021-06-10 04:27:48 +00:00
if err != nil {
return err
}
powerLookback, err := getTotalPower(ctx, sim.StateManager, lookbackTs)
2021-06-10 04:27:48 +00:00
if err != nil {
return err
}
// growth rate in size/day
growthRate := big.Div(
big.Mul(big.Sub(powerNow.RawBytePower, powerLookback.RawBytePower),
big.NewInt(builtin.EpochsInDay)),
big.NewInt(int64(head.Height()-lookbackTs.Height())),
)
2021-06-10 04:27:48 +00:00
tw := tabwriter.NewWriter(out, 8, 8, 1, ' ', 0)
headEpoch := head.Height()
firstEpoch := start.Height() + 1
headTime := time.Unix(int64(head.MinTimestamp()), 0)
startTime := time.Unix(int64(start.MinTimestamp()), 0)
duration := headTime.Sub(startTime)
fmt.Fprintf(tw, "Name:\t%s\n", sim.Name())
fmt.Fprintf(tw, "Head:\t%s\n", head)
fmt.Fprintf(tw, "Start Epoch:\t%d\n", firstEpoch)
fmt.Fprintf(tw, "End Epoch:\t%d\n", headEpoch)
2021-06-10 04:27:48 +00:00
fmt.Fprintf(tw, "Length:\t%d\n", headEpoch-firstEpoch)
fmt.Fprintf(tw, "Start Date:\t%s\n", startTime)
fmt.Fprintf(tw, "End Date:\t%s\n", headTime)
fmt.Fprintf(tw, "Duration:\t%.2f day(s)\n", duration.Hours()/24)
fmt.Fprintf(tw, "Capacity:\t%s\n", types.SizeStr(powerNow.RawBytePower))
fmt.Fprintf(tw, "Daily Capacity Growth:\t%s/day\n", types.SizeStr(growthRate))
2021-06-10 04:27:48 +00:00
fmt.Fprintf(tw, "Network Version:\t%d\n", sim.GetNetworkVersion())
return tw.Flush()
}
2021-06-07 22:05:52 +00:00
var infoSimCommand = &cli.Command{
Name: "info",
Description: "Output information about the simulation.",
Subcommands: []*cli.Command{
infoCommitGasSimCommand,
infoMessageSizeSimCommand,
infoWindowPostBandwidthSimCommand,
2021-06-12 18:44:38 +00:00
infoCapacityGrowthSimCommand,
2021-06-15 00:38:55 +00:00
infoStateGrowthSimCommand,
},
2021-06-18 18:17:35 +00:00
Action: func(cctx *cli.Context) (err error) {
2021-06-07 22:05:52 +00:00
node, err := open(cctx)
if err != nil {
return err
}
2021-06-18 18:17:35 +00:00
defer func() {
if cerr := node.Close(); err == nil {
err = cerr
}
}()
2021-06-07 22:05:52 +00:00
sim, err := node.LoadSim(cctx.Context, cctx.String("simulation"))
if err != nil {
return err
}
2021-06-10 04:27:48 +00:00
return printInfo(cctx.Context, sim, cctx.App.Writer)
2021-06-07 22:05:52 +00:00
},
}