From 2857f6c0edc9a6e31ebe3a646a6a3dc8063e2bd8 Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 29 Apr 2021 08:46:38 -0700 Subject: [PATCH] fix: use a consistent tipset in commands It's very easy to write an incorrect command that operates over different heads by using the "empty" tipset. This change makes the `LoadTipSet` command helper get the latest head from the lotus daemon if its unset. The cost is an extra call to get the head. That should be trivial in most cases. --- cli/chain.go | 6 ------ cli/state.go | 27 ++++----------------------- cmd/lotus-shed/frozen-miners.go | 6 ------ cmd/lotus-shed/postfind.go | 6 ------ cmd/lotus-shed/stateroot-stats.go | 14 -------------- 5 files changed, 4 insertions(+), 55 deletions(-) diff --git a/cli/chain.go b/cli/chain.go index 9954813de..cc2fe50ec 100644 --- a/cli/chain.go +++ b/cli/chain.go @@ -723,12 +723,6 @@ var ChainGetCmd = &cli.Command{ return err } - if ts == nil { - ts, err = api.ChainHead(ctx) - if err != nil { - return err - } - } p = "/ipfs/" + ts.ParentState().String() + p if cctx.Bool("verbose") { fmt.Println(p) diff --git a/cli/state.go b/cli/state.go index 60bb0b59f..ae1553dd8 100644 --- a/cli/state.go +++ b/cli/state.go @@ -180,10 +180,13 @@ func ParseTipSetString(ts string) ([]cid.Cid, error) { return cids, nil } +// LoadTipSet gets the tipset from the context, or the head from the API. +// +// It always gets the head from the API so commands use a consistent tipset even if time pases. func LoadTipSet(ctx context.Context, cctx *cli.Context, api v0api.FullNode) (*types.TipSet, error) { tss := cctx.String("tipset") if tss == "" { - return nil, nil + return api.ChainHead(ctx) } return ParseTipSetRef(ctx, api, tss) @@ -850,14 +853,6 @@ var StateListMessagesCmd = &cli.Command{ return err } - if ts == nil { - head, err := api.ChainHead(ctx) - if err != nil { - return err - } - ts = head - } - windowSize := abi.ChainEpoch(100) cur := ts @@ -957,13 +952,6 @@ var StateComputeStateCmd = &cli.Command{ } h := abi.ChainEpoch(cctx.Uint64("vm-height")) - if ts == nil { - head, err := api.ChainHead(ctx) - if err != nil { - return err - } - ts = head - } if h == 0 { h = ts.Height() } @@ -1765,13 +1753,6 @@ var StateSectorCmd = &cli.Command{ return err } - if ts == nil { - ts, err = api.ChainHead(ctx) - if err != nil { - return err - } - } - maddr, err := address.NewFromString(cctx.Args().Get(0)) if err != nil { return err diff --git a/cmd/lotus-shed/frozen-miners.go b/cmd/lotus-shed/frozen-miners.go index 6b843f0d6..ed09c00c5 100644 --- a/cmd/lotus-shed/frozen-miners.go +++ b/cmd/lotus-shed/frozen-miners.go @@ -35,12 +35,6 @@ var frozenMinersCmd = &cli.Command{ if err != nil { return err } - if ts == nil { - ts, err = api.ChainHead(ctx) - if err != nil { - return err - } - } queryEpoch := ts.Height() diff --git a/cmd/lotus-shed/postfind.go b/cmd/lotus-shed/postfind.go index 83006fd09..c8a4c9907 100644 --- a/cmd/lotus-shed/postfind.go +++ b/cmd/lotus-shed/postfind.go @@ -49,12 +49,6 @@ var postFindCmd = &cli.Command{ if err != nil { return err } - if startTs == nil { - startTs, err = api.ChainHead(ctx) - if err != nil { - return err - } - } stopEpoch := startTs.Height() - abi.ChainEpoch(c.Int("lookback")) if verbose { fmt.Printf("Collecting messages between %d and %d\n", startTs.Height(), stopEpoch) diff --git a/cmd/lotus-shed/stateroot-stats.go b/cmd/lotus-shed/stateroot-stats.go index 023f782bd..6d5d57708 100644 --- a/cmd/lotus-shed/stateroot-stats.go +++ b/cmd/lotus-shed/stateroot-stats.go @@ -56,13 +56,6 @@ var staterootDiffsCmd = &cli.Command{ return err } - if ts == nil { - ts, err = api.ChainHead(ctx) - if err != nil { - return err - } - } - fn := func(ts *types.TipSet) (cid.Cid, []cid.Cid) { blk := ts.Blocks()[0] strt := blk.ParentStateRoot @@ -134,13 +127,6 @@ var staterootStatCmd = &cli.Command{ return err } - if ts == nil { - ts, err = api.ChainHead(ctx) - if err != nil { - return err - } - } - var addrs []address.Address for _, inp := range cctx.Args().Slice() {