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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# UNRELEASED

- Add json output of tipsets to `louts chain list`. ([filecoin-project/lotus#12691](https://github.com/filecoin-project/lotus/pull/12691))

# UNRELEASED v1.31.0

See https://github.com/filecoin-project/lotus/blob/release/v1.31.0/CHANGELOG.md
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
Loading