Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): enhance ChainListCmd with JSON output and <epoch> placeholder support #12691

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Return a `"data"` field on the `"error"` returned from RPC when `eth_call` and `eth_estimateGas` APIs encounter `execution reverted` errors. ([filecoin-project/lotus#12553](https://github.com/filecoin-project/lotus/pull/12553))
- Implement `EthGetTransactionByBlockNumberAndIndex` (`eth_getTransactionByBlockNumberAndIndex`) and `EthGetTransactionByBlockHashAndIndex` (`eth_getTransactionByBlockHashAndIndex`) methods. ([filecoin-project/lotus#12618](https://github.com/filecoin-project/lotus/pull/12618))
- Add a set of `lotus-shed datastore` commands for importing, exporting, and clearing parts of the datastore ([filecoin-project/lotus#12685](https://github.com/filecoin-project/lotus/pull/12685)):
- Add json output of tipsets to `louts chain list`. ([filecoin-project/lotus#12691](https://github.com/filecoin-project/lotus/pull/12691))

## Bug Fixes
- Fix a bug in the `lotus-shed indexes backfill-events` command that may result in either duplicate events being backfilled where there are existing events (such an operation *should* be idempotent) or events erroneously having duplicate `logIndex` values when queried via ETH APIs. ([filecoin-project/lotus#12567](https://github.com/filecoin-project/lotus/pull/12567))
Expand Down
20 changes: 16 additions & 4 deletions cli/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,12 +581,12 @@ var ChainListCmd = &cli.Command{
Aliases: []string{"love"},
Usage: "View a segment of the chain",
Flags: []cli.Flag{
&cli.Uint64Flag{Name: "height", DefaultText: "current head"},
&cli.Uint64Flag{Name: "epoch", Aliases: []string{"height"}, DefaultText: "current head"},
&cli.IntFlag{Name: "count", Value: 30},
&cli.StringFlag{
Name: "format",
Usage: "specify the format to print out tipsets",
Value: "<height>: (<time>) <blocks>",
Usage: "specify the format to print out tipsets using placeholders: <epoch>, <time>, <blocks>, <weight>, <tipset>, <json_tipset>\n",
Value: "<epoch>: (<time>) <blocks>",
},
&cli.BoolFlag{
Name: "gas-stats",
Expand Down Expand Up @@ -932,7 +932,9 @@ func handleHamtAddress(ctx context.Context, api v0api.FullNode, r cid.Cid) error
}

func printTipSet(format string, ts *types.TipSet, afmt *AppFmt) {
format = strings.ReplaceAll(format, "<height>", fmt.Sprint(ts.Height()))
format = strings.ReplaceAll(format, "<epoch>", fmt.Sprint(ts.Height()))
format = strings.ReplaceAll(format, "<height>", fmt.Sprint(ts.Height())) // backwards compatibility

format = strings.ReplaceAll(format, "<time>", time.Unix(int64(ts.MinTimestamp()), 0).Format(time.Stamp))
blks := "[ "
for _, b := range ts.Blocks() {
Expand All @@ -948,6 +950,16 @@ func printTipSet(format string, ts *types.TipSet, afmt *AppFmt) {

format = strings.ReplaceAll(format, "<tipset>", strings.Join(sCids, ","))
format = strings.ReplaceAll(format, "<blocks>", blks)
if strings.Contains(format, "<json_tipset>") {
jsonTipset, err := json.Marshal(ts)
if err != nil {
// should not happen
afmt.Println("Error encoding tipset to JSON:", err)
return
}
format = strings.ReplaceAll(format, "<json_tipset>", string(jsonTipset))
}

format = strings.ReplaceAll(format, "<weight>", fmt.Sprint(ts.Blocks()[0].ParentWeight))

afmt.Println(format)
Expand Down
11 changes: 6 additions & 5 deletions documentation/en/cli-lotus.md
Original file line number Diff line number Diff line change
Expand Up @@ -1718,11 +1718,12 @@ USAGE:
lotus chain list [command options] [arguments...]

OPTIONS:
--height value (default: current head)
--count value (default: 30)
--format value specify the format to print out tipsets (default: "<height>: (<time>) <blocks>")
--gas-stats view gas statistics for the chain (default: false)
--help, -h show help
--epoch value, --height value (default: current head)
--count value (default: 30)
--format value specify the format to print out tipsets using placeholders: <epoch>, <time>, <blocks>, <weight>, <tipset>, <json_tipset>
(default: "<epoch>: (<time>) <blocks>")
--gas-stats view gas statistics for the chain (default: false)
--help, -h show help
```

### lotus chain get
Expand Down