|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "strings" |
| 8 | + "text/template" |
| 9 | + |
| 10 | + "github.com/urfave/cli/v2" |
| 11 | + |
| 12 | + "github.com/filecoin-project/go-f3/manifest" |
| 13 | + |
| 14 | + "github.com/filecoin-project/lotus/lib/tablewriter" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + F3Cmd = &cli.Command{ |
| 19 | + Name: "f3", |
| 20 | + Usage: "Manages Filecoin Fast Finality (F3) interactions", |
| 21 | + Subcommands: []*cli.Command{ |
| 22 | + { |
| 23 | + Name: "list-miners", |
| 24 | + Aliases: []string{"lp"}, |
| 25 | + Usage: "Lists the miners that currently participate in F3 via this node.", |
| 26 | + Action: func(cctx *cli.Context) error { |
| 27 | + api, closer, err := GetFullNodeAPIV1(cctx) |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + defer closer() |
| 32 | + |
| 33 | + miners, err := api.F3ListParticipants(cctx.Context) |
| 34 | + if err != nil { |
| 35 | + return fmt.Errorf("listing participants: %w", err) |
| 36 | + } |
| 37 | + if len(miners) == 0 { |
| 38 | + _, err = fmt.Fprintln(cctx.App.Writer, "No miners.") |
| 39 | + return err |
| 40 | + } |
| 41 | + const ( |
| 42 | + idColumn = "ID" |
| 43 | + fromColumn = "From" |
| 44 | + ToColumn = "To" |
| 45 | + ) |
| 46 | + tw := tablewriter.New( |
| 47 | + tablewriter.Col(idColumn), |
| 48 | + tablewriter.Col(fromColumn), |
| 49 | + tablewriter.Col(ToColumn), |
| 50 | + ) |
| 51 | + for _, participant := range miners { |
| 52 | + tw.Write(map[string]interface{}{ |
| 53 | + idColumn: participant.MinerID, |
| 54 | + fromColumn: participant.FromInstance, |
| 55 | + ToColumn: participant.FromInstance + participant.ValidityTerm, |
| 56 | + }) |
| 57 | + } |
| 58 | + return tw.Flush(cctx.App.Writer) |
| 59 | + }, |
| 60 | + }, |
| 61 | + { |
| 62 | + Name: "manifest", |
| 63 | + Usage: "Gets the current manifest used by F3.", |
| 64 | + Flags: []cli.Flag{f3FlagOutput}, |
| 65 | + Action: func(cctx *cli.Context) error { |
| 66 | + api, closer, err := GetFullNodeAPIV1(cctx) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + defer closer() |
| 71 | + |
| 72 | + manifest, err := api.F3GetManifest(cctx.Context) |
| 73 | + if err != nil { |
| 74 | + return fmt.Errorf("getting manifest: %w", err) |
| 75 | + } |
| 76 | + switch output := cctx.String("output"); strings.ToLower(output) { |
| 77 | + case "text": |
| 78 | + return prettyPrintManifest(cctx.App.Writer, manifest) |
| 79 | + case "json": |
| 80 | + encoder := json.NewEncoder(cctx.App.Writer) |
| 81 | + encoder.SetIndent("", " ") |
| 82 | + return encoder.Encode(manifest) |
| 83 | + default: |
| 84 | + return fmt.Errorf("unknown output format: %s", output) |
| 85 | + } |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + Name: "status", |
| 90 | + Usage: "Checks the F3 status.", |
| 91 | + Action: func(cctx *cli.Context) error { |
| 92 | + api, closer, err := GetFullNodeAPIV1(cctx) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + defer closer() |
| 97 | + |
| 98 | + running, err := api.F3IsRunning(cctx.Context) |
| 99 | + if err != nil { |
| 100 | + return fmt.Errorf("getting running state: %w", err) |
| 101 | + } |
| 102 | + _, _ = fmt.Fprintf(cctx.App.Writer, "Running: %t\n", running) |
| 103 | + if !running { |
| 104 | + return nil |
| 105 | + } |
| 106 | + |
| 107 | + progress, err := api.F3GetProgress(cctx.Context) |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("getting progress: %w", err) |
| 110 | + } |
| 111 | + |
| 112 | + _, _ = fmt.Fprintln(cctx.App.Writer, "Progress:") |
| 113 | + _, _ = fmt.Fprintf(cctx.App.Writer, " Instance: %d\n", progress.ID) |
| 114 | + _, _ = fmt.Fprintf(cctx.App.Writer, " Round: %d\n", progress.Round) |
| 115 | + _, _ = fmt.Fprintf(cctx.App.Writer, " Phase: %s\n", progress.Phase) |
| 116 | + |
| 117 | + manifest, err := api.F3GetManifest(cctx.Context) |
| 118 | + if err != nil { |
| 119 | + return fmt.Errorf("getting manifest: %w", err) |
| 120 | + } |
| 121 | + return prettyPrintManifest(cctx.App.Writer, manifest) |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + // TODO: we should standardise format as a top level flag. For now, here is an f3 |
| 128 | + // specific one. |
| 129 | + // See: https://github.com/filecoin-project/lotus/issues/12616 |
| 130 | + f3FlagOutput = &cli.StringFlag{ |
| 131 | + Name: "output", |
| 132 | + Usage: "The output format. Supported formats: text, json", |
| 133 | + Value: "text", |
| 134 | + Action: func(cctx *cli.Context, output string) error { |
| 135 | + switch output { |
| 136 | + case "text", "json": |
| 137 | + return nil |
| 138 | + default: |
| 139 | + return fmt.Errorf("unknown output format: %s", output) |
| 140 | + } |
| 141 | + }, |
| 142 | + } |
| 143 | +) |
| 144 | + |
| 145 | +func prettyPrintManifest(out io.Writer, manifest *manifest.Manifest) error { |
| 146 | + if manifest == nil { |
| 147 | + _, err := fmt.Fprintln(out, "Manifest: None") |
| 148 | + return err |
| 149 | + } |
| 150 | + |
| 151 | + const manifestTemplate = `Manifest: |
| 152 | + Protocol Version: {{.ProtocolVersion}} |
| 153 | + Paused: {{.Pause}} |
| 154 | + Initial Instance: {{.InitialInstance}} |
| 155 | + Bootstrap Epoch: {{.BootstrapEpoch}} |
| 156 | + Network Name: {{.NetworkName}} |
| 157 | + Ignore EC Power: {{.IgnoreECPower}} |
| 158 | + Committee Lookback: {{.CommitteeLookback}} |
| 159 | + Catch Up Alignment: {{.CatchUpAlignment}} |
| 160 | +
|
| 161 | + GPBFT Delta: {{.Gpbft.Delta}} |
| 162 | + GPBFT Delta BackOff Exponent: {{.Gpbft.DeltaBackOffExponent}} |
| 163 | + GPBFT Max Lookahead Rounds: {{.Gpbft.MaxLookaheadRounds}} |
| 164 | + GPBFT Rebroadcast Backoff Base: {{.Gpbft.RebroadcastBackoffBase}} |
| 165 | + GPBFT Rebroadcast Backoff Spread: {{.Gpbft.RebroadcastBackoffSpread}} |
| 166 | + GPBFT Rebroadcast Backoff Max: {{.Gpbft.RebroadcastBackoffMax}} |
| 167 | +
|
| 168 | + EC Period: {{.EC.Period}} |
| 169 | + EC Finality: {{.EC.Finality}} |
| 170 | + EC Delay Multiplier: {{.EC.DelayMultiplier}} |
| 171 | + EC Head Lookback: {{.EC.HeadLookback}} |
| 172 | + EC Finalize: {{.EC.Finalize}} |
| 173 | +
|
| 174 | + Certificate Exchange Client Timeout: {{.CertificateExchange.ClientRequestTimeout}} |
| 175 | + Certificate Exchange Server Timeout: {{.CertificateExchange.ServerRequestTimeout}} |
| 176 | + Certificate Exchange Min Poll Interval: {{.CertificateExchange.MinimumPollInterval}} |
| 177 | + Certificate Exchange Max Poll Interval: {{.CertificateExchange.MaximumPollInterval}} |
| 178 | +` |
| 179 | + return template.New("manifest").ExecuteTemplate(out, manifestTemplate, manifest) |
| 180 | +} |
0 commit comments