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