-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathf3.go
711 lines (655 loc) · 21 KB
/
f3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
package cli
import (
"context"
"embed"
"encoding/json"
"errors"
"fmt"
"io"
"strconv"
"strings"
"text/template"
"github.com/ipfs/go-cid"
"github.com/urfave/cli/v2"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-f3/certs"
"github.com/filecoin-project/go-f3/gpbft"
"github.com/filecoin-project/go-f3/manifest"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/api/v1api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/lib/tablewriter"
)
var F3Cmd = &cli.Command{
Name: "f3",
Usage: "Manages Filecoin Fast Finality (F3) interactions",
Subcommands: []*cli.Command{
f3SbCmdListMiners,
f3SubCmdPowerTable,
f3SubCmdCerts,
f3SubCmdManifest,
f3SubCmdStatus,
},
}
var f3SbCmdListMiners = &cli.Command{
Name: "list-miners",
Aliases: []string{"lm"},
Usage: `Lists the miners that currently participate in F3 via this node.`,
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPIV1(cctx)
if err != nil {
return err
}
defer closer()
miners, err := api.F3ListParticipants(cctx.Context)
if err != nil {
return fmt.Errorf("listing participants: %w", err)
}
if len(miners) == 0 {
_, err = fmt.Fprintln(cctx.App.Writer, "No miners.")
return err
}
const (
miner = "Miner"
from = "From"
to = "To"
)
tw := tablewriter.New(
tablewriter.Col(miner),
tablewriter.Col(from),
tablewriter.Col(to),
)
for _, participant := range miners {
addr, err := address.NewIDAddress(participant.MinerID)
if err != nil {
return fmt.Errorf("converting miner ID to address: %w", err)
}
tw.Write(map[string]interface{}{
miner: addr,
from: participant.FromInstance,
to: participant.FromInstance + participant.ValidityTerm,
})
}
return tw.Flush(cctx.App.Writer)
},
}
var f3SubCmdPowerTable = &cli.Command{
Name: "powertable",
Aliases: []string{"pt"},
Subcommands: []*cli.Command{
{
Name: "get",
Aliases: []string{"g"},
Usage: `Get F3 power table at a specific instance ID or latest instance if none is specified.`,
ArgsUsage: "[instance]",
Flags: []cli.Flag{f3FlagPowerTableFromEC},
Before: func(cctx *cli.Context) error {
if cctx.Args().Len() > 1 {
return fmt.Errorf("too many arguments")
}
return nil
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPIV1(cctx)
if err != nil {
return err
}
defer closer()
progress, err := api.F3GetProgress(cctx.Context)
if err != nil {
return fmt.Errorf("getting progress: %w", err)
}
var instance uint64
if cctx.Args().Present() {
instance, err = strconv.ParseUint(cctx.Args().First(), 10, 64)
if err != nil {
return fmt.Errorf("parsing instance: %w", err)
}
if instance > progress.ID {
// TODO: Technically we can return power table for instances ahead as long as
// instance is within lookback. Implement it.
return fmt.Errorf("instance is ahead the current instance in progress: %d > %d", instance, progress.ID)
}
} else {
instance = progress.ID
}
ltsk, expectedPowerTableCID, err := f3GetPowerTableTSKByInstance(cctx.Context, api, instance)
if err != nil {
return fmt.Errorf("getting power table tsk for instance %d: %w", instance, err)
}
var result = struct {
Instance uint64
FromEC bool
PowerTable struct {
CID string
Entries gpbft.PowerEntries
Total gpbft.StoragePower
ScaledTotal int64
}
}{
Instance: instance,
FromEC: cctx.Bool(f3FlagPowerTableFromEC.Name),
}
if result.FromEC {
result.PowerTable.Entries, err = api.F3GetECPowerTable(cctx.Context, ltsk)
} else {
result.PowerTable.Entries, err = api.F3GetF3PowerTable(cctx.Context, ltsk)
}
if err != nil {
return fmt.Errorf("getting f3 power table at instance %d: %w", instance, err)
}
pt := gpbft.NewPowerTable()
if err := pt.Add(result.PowerTable.Entries...); err != nil {
// Sanity check the entries returned by the API.
return fmt.Errorf("retrieved power table is not valid for instance %d: %w", instance, err)
}
result.PowerTable.Total = pt.Total
result.PowerTable.ScaledTotal = pt.ScaledTotal
actualPowerTableCID, err := certs.MakePowerTableCID(result.PowerTable.Entries)
if err != nil {
return fmt.Errorf("gettingh power table CID at instance %d: %w", instance, err)
}
if !cid.Undef.Equals(expectedPowerTableCID) && !expectedPowerTableCID.Equals(actualPowerTableCID) {
return fmt.Errorf("expected power table CID %s at instance %d, got: %s", expectedPowerTableCID, instance, actualPowerTableCID)
}
result.PowerTable.CID = actualPowerTableCID.String()
output, err := json.MarshalIndent(result, "", " ")
if err != nil {
return fmt.Errorf("marshalling f3 power table at instance %d: %w", instance, err)
}
_, _ = fmt.Fprint(cctx.App.Writer, string(output))
return nil
},
},
{
Name: "get-proportion",
Aliases: []string{"gp"},
Usage: `Gets the total proportion of power for a list of actors at a given instance.`,
ArgsUsage: "<actor-id> [actor-id] ...",
Flags: []cli.Flag{
f3FlagPowerTableFromEC,
f3FlagInstanceID,
},
Before: func(cctx *cli.Context) error {
if cctx.Args().Len() < 1 {
return fmt.Errorf("at least one actor ID must be specified")
}
return nil
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPIV1(cctx)
if err != nil {
return err
}
defer closer()
progress, err := api.F3GetProgress(cctx.Context)
if err != nil {
return fmt.Errorf("getting progress: %w", err)
}
var instance uint64
if cctx.IsSet(f3FlagInstanceID.Name) {
instance = cctx.Uint64(f3FlagInstanceID.Name)
if instance > progress.ID {
// TODO: Technically we can return power table for instances ahead as long as
// instance is within lookback. Implement it.
return fmt.Errorf("instance is ahead the current instance in progress: %d > %d", instance, progress.ID)
}
} else {
instance = progress.ID
}
ltsk, expectedPowerTableCID, err := f3GetPowerTableTSKByInstance(cctx.Context, api, instance)
if err != nil {
return fmt.Errorf("getting power table tsk for instance %d: %w", instance, err)
}
var result = struct {
Instance uint64
FromEC bool
PowerTable struct {
CID string
ScaledTotal int64
}
ScaledSum int64
Proportion float64
NotFound []gpbft.ActorID
}{
Instance: instance,
FromEC: cctx.Bool(f3FlagPowerTableFromEC.Name),
}
var powerEntries gpbft.PowerEntries
if result.FromEC {
powerEntries, err = api.F3GetECPowerTable(cctx.Context, ltsk)
} else {
powerEntries, err = api.F3GetF3PowerTable(cctx.Context, ltsk)
}
if err != nil {
return fmt.Errorf("getting f3 power table at instance %d: %w", instance, err)
}
actualPowerTableCID, err := certs.MakePowerTableCID(powerEntries)
if err != nil {
return fmt.Errorf("gettingh power table CID at instance %d: %w", instance, err)
}
if !cid.Undef.Equals(expectedPowerTableCID) && !expectedPowerTableCID.Equals(actualPowerTableCID) {
return fmt.Errorf("expected power table CID %s at instance %d, got: %s", expectedPowerTableCID, instance, actualPowerTableCID)
}
result.PowerTable.CID = actualPowerTableCID.String()
pt := gpbft.NewPowerTable()
if err := pt.Add(powerEntries...); err != nil {
return fmt.Errorf("constructing power table from entries: %w", err)
}
result.PowerTable.ScaledTotal = pt.ScaledTotal
inputActorIDs := cctx.Args().Slice()
seenIDs := map[gpbft.ActorID]struct{}{}
for _, stringID := range inputActorIDs {
var actorID gpbft.ActorID
switch addr, err := address.NewFromString(stringID); {
case err == nil:
idAddr, err := address.IDFromAddress(addr)
if err != nil {
return fmt.Errorf("parsing ID from address %q: %w", stringID, err)
}
actorID = gpbft.ActorID(idAddr)
case errors.Is(err, address.ErrUnknownNetwork),
errors.Is(err, address.ErrUnknownProtocol):
// Try parsing as uint64 straight up.
id, err := strconv.ParseUint(stringID, 10, 64)
if err != nil {
return fmt.Errorf("parsing as uint64 %q: %w", stringID, err)
}
actorID = gpbft.ActorID(id)
default:
return fmt.Errorf("parsing address %q: %w", stringID, err)
}
// Prune duplicate IDs.
if _, ok := seenIDs[actorID]; ok {
continue
}
seenIDs[actorID] = struct{}{}
scaled, key := pt.Get(actorID)
if key == nil {
result.NotFound = append(result.NotFound, actorID)
continue
}
result.ScaledSum += scaled
}
result.Proportion = float64(result.ScaledSum) / float64(result.PowerTable.ScaledTotal)
output, err := json.MarshalIndent(result, "", " ")
if err != nil {
return fmt.Errorf("marshalling f3 power table at instance %d: %w", instance, err)
}
_, _ = fmt.Fprint(cctx.App.Writer, string(output))
return nil
},
},
},
}
var f3SubCmdCerts = &cli.Command{
Name: "certs",
Aliases: []string{"c"},
Usage: "Manages interactions with F3 finality certificates.",
Subcommands: []*cli.Command{
{
Name: "get",
Usage: "Gets an F3 finality certificate to a given instance ID, " +
"or the latest certificate if no instance is specified.",
ArgsUsage: "[instance]",
Flags: []cli.Flag{
f3FlagOutput,
},
Before: func(cctx *cli.Context) error {
if count := cctx.NArg(); count > 1 {
return fmt.Errorf("too many arguments: expected at most 1 but got %d", count)
}
return nil
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPIV1(cctx)
if err != nil {
return err
}
defer closer()
// Get the certificate, either for the given instance or the latest if no
// instance is specified.
var cert *certs.FinalityCertificate
if cctx.Args().Present() {
var instance uint64
instance, err = strconv.ParseUint(cctx.Args().First(), 10, 64)
if err != nil {
return fmt.Errorf("parsing instance: %w", err)
}
cert, err = api.F3GetCertificate(cctx.Context, instance)
} else {
cert, err = api.F3GetLatestCertificate(cctx.Context)
}
if err != nil {
return fmt.Errorf("getting finality certificate: %w", err)
}
if cert == nil {
_, _ = fmt.Fprintln(cctx.App.ErrWriter, "No certificate.")
return nil
}
return outputFinalityCertificate(cctx, cert)
},
},
{
Name: "list",
Usage: `Lists a range of F3 finality certificates.
By default the certificates are listed in newest to oldest order,
i.e. descending instance IDs. The order may be reversed using the
'--reverse' flag.
A range may optionally be specified as the first argument to indicate
inclusive range of 'from' and 'to' instances in following notation:
'<from>..<to>'. Either <from> or <to> may be omitted, but not both.
An omitted <from> value is always interpreted as 0, and an omitted
<to> value indicates the latest instance. If both are specified, <from>
must never exceed <to>.
If no range is specified, the latest 10 certificates are listed, i.e.
the range of '0..' with limit of 10. Otherwise, all certificates in
the specified range are listed unless limit is explicitly specified.
Examples:
* All certificates from newest to oldest:
$ lotus f3 certs list 0..
* Three newest certificates:
$ lotus f3 certs list --limit 3 0..
* Three oldest certificates:
$ lotus f3 certs list --limit 3 --reverse 0..
* Up to three certificates starting from instance 1413 to the oldest:
$ lotus f3 certs list --limit 3 ..1413
* Up to 3 certificates starting from instance 1413 to the newest:
$ lotus f3 certs list --limit 3 --reverse 1413..
* All certificates from instance 3 to 1413 in order of newest to oldest:
$ lotus f3 certs list 3..1413
`,
ArgsUsage: "[range]",
Flags: []cli.Flag{
f3FlagOutput,
f3FlagInstanceLimit,
f3FlagReverseOrder,
},
Before: func(cctx *cli.Context) error {
if count := cctx.NArg(); count > 1 {
return fmt.Errorf("too many arguments: expected at most 1 but got %d", count)
}
return nil
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPIV1(cctx)
if err != nil {
return err
}
defer closer()
limit := cctx.Int(f3FlagInstanceLimit.Name)
reverse := cctx.Bool(f3FlagReverseOrder.Name)
fromTo := cctx.Args().First()
if fromTo == "" {
fromTo = "0.."
if !cctx.IsSet(f3FlagInstanceLimit.Name) {
// Default to limit of 10 if no explicit range and limit is given.
limit = 10
}
}
r, err := newRanger(fromTo, limit, reverse, func() (uint64, error) {
latest, err := api.F3GetLatestCertificate(cctx.Context)
if err != nil {
return 0, fmt.Errorf("getting latest finality certificate: %w", err)
}
if latest == nil {
return 0, errors.New("no latest finality certificate")
}
return latest.GPBFTInstance, nil
})
if err != nil {
return err
}
var cert *certs.FinalityCertificate
for cctx.Context.Err() == nil {
next, proceed := r.next()
if !proceed {
return nil
}
cert, err = api.F3GetCertificate(cctx.Context, next)
if err != nil {
return fmt.Errorf("getting finality certificate for instance %d: %w", next, err)
}
if cert == nil {
// This is unexpected, because the range of iteration was determined earlier and
// certstore should to have all the certs. Error out.
return fmt.Errorf("nil finality certificate for instance %d", next)
}
if err := outputFinalityCertificate(cctx, cert); err != nil {
return err
}
_, _ = fmt.Fprintln(cctx.App.Writer)
}
return nil
},
},
},
}
var f3SubCmdManifest = &cli.Command{
Name: "manifest",
Usage: "Gets the current manifest used by F3.",
Flags: []cli.Flag{f3FlagOutput},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPIV1(cctx)
if err != nil {
return err
}
defer closer()
manifest, err := api.F3GetManifest(cctx.Context)
if err != nil {
return fmt.Errorf("getting manifest: %w", err)
}
switch output := cctx.String(f3FlagOutput.Name); strings.ToLower(output) {
case "text":
return prettyPrintManifest(cctx.App.Writer, manifest)
case "json":
encoder := json.NewEncoder(cctx.App.Writer)
encoder.SetIndent("", " ")
return encoder.Encode(manifest)
default:
return fmt.Errorf("unknown output format: %s", output)
}
},
}
var f3SubCmdStatus = &cli.Command{
Name: "status",
Usage: "Checks the F3 status.",
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPIV1(cctx)
if err != nil {
return err
}
defer closer()
running, err := api.F3IsRunning(cctx.Context)
if err != nil {
return fmt.Errorf("getting running state: %w", err)
}
_, _ = fmt.Fprintf(cctx.App.Writer, "Running: %t\n", running)
if !running {
return nil
}
progress, err := api.F3GetProgress(cctx.Context)
if err != nil {
return fmt.Errorf("getting progress: %w", err)
}
_, _ = fmt.Fprintln(cctx.App.Writer, "Progress:")
_, _ = fmt.Fprintf(cctx.App.Writer, " Instance: %d\n", progress.ID)
_, _ = fmt.Fprintf(cctx.App.Writer, " Round: %d\n", progress.Round)
_, _ = fmt.Fprintf(cctx.App.Writer, " Phase: %s\n", progress.Phase)
manifest, err := api.F3GetManifest(cctx.Context)
if err != nil {
return fmt.Errorf("getting manifest: %w", err)
}
return prettyPrintManifest(cctx.App.Writer, manifest)
},
}
// TODO: we should standardise format as a top level flag. For now, here is an f3
//
// specific one.
// See: https://github.com/filecoin-project/lotus/issues/12616
var f3FlagOutput = &cli.StringFlag{
Name: "output",
Usage: "The output format. Supported formats: text, json",
Value: "text",
Action: func(cctx *cli.Context, output string) error {
switch output {
case "text", "json":
return nil
default:
return fmt.Errorf("unknown output format: %s", output)
}
},
}
var f3FlagInstanceLimit = &cli.IntFlag{
Name: "limit",
Usage: "The maximum number of instances. A value less than 0 indicates no limit.",
DefaultText: "10 when no range is specified. Otherwise, unlimited.",
Value: -1,
}
var f3FlagReverseOrder = &cli.BoolFlag{
Name: "reverse",
Usage: "Reverses the default order of output. ",
}
var f3FlagPowerTableFromEC = &cli.BoolFlag{
Name: "ec",
Usage: "Whether to get the power table from EC.",
}
var f3FlagInstanceID = &cli.Uint64Flag{
Name: "instance",
Aliases: []string{"i"},
Usage: "The F3 instance ID.",
DefaultText: "Latest Instance",
}
//go:embed templates/f3_*.go.tmpl
var f3TemplatesFS embed.FS
var f3Templates = template.Must(
template.New("").
Funcs(template.FuncMap{
"ptDiffToString": f3PowerTableDiffsToString,
"tipSetKeyToLotusTipSetKey": types.TipSetKeyFromBytes,
"add": func(a, b int) int { return a + b },
"sub": func(a, b int) int { return a - b },
}).
ParseFS(f3TemplatesFS, "templates/f3_*.go.tmpl"),
)
func f3GetPowerTableTSKByInstance(ctx context.Context, api v1api.FullNode, instance uint64) (types.TipSetKey, cid.Cid, error) {
mfst, err := api.F3GetManifest(ctx)
if err != nil {
return types.EmptyTSK, cid.Undef, fmt.Errorf("getting manifest: %w", err)
}
if instance < mfst.InitialInstance+mfst.CommitteeLookback {
ts, err := api.ChainGetTipSetByHeight(ctx, abi.ChainEpoch(mfst.BootstrapEpoch-mfst.EC.Finality), types.EmptyTSK)
if err != nil {
return types.EmptyTSK, cid.Undef, fmt.Errorf("getting bootstrap epoch tipset: %w", err)
}
return ts.Key(), mfst.InitialPowerTable, nil
}
previous, err := api.F3GetCertificate(ctx, instance-1)
if err != nil {
return types.EmptyTSK, cid.Undef, fmt.Errorf("getting certificate for previous instance: %w", err)
}
lookback, err := api.F3GetCertificate(ctx, instance-mfst.CommitteeLookback)
if err != nil {
return types.EmptyTSK, cid.Undef, fmt.Errorf("getting certificate for lookback instance: %w", err)
}
ltsk, err := types.TipSetKeyFromBytes(lookback.ECChain.Head().Key)
if err != nil {
return types.EmptyTSK, cid.Undef, fmt.Errorf("getting lotus tipset key from head of lookback certificate: %w", err)
}
return ltsk, previous.SupplementalData.PowerTable, nil
}
func outputFinalityCertificate(cctx *cli.Context, cert *certs.FinalityCertificate) error {
switch output := cctx.String(f3FlagOutput.Name); strings.ToLower(output) {
case "text":
return prettyPrintFinalityCertificate(cctx.App.Writer, cert)
case "json":
encoder := json.NewEncoder(cctx.App.Writer)
encoder.SetIndent("", " ")
return encoder.Encode(cert)
default:
return fmt.Errorf("unknown output format: %s", output)
}
}
func prettyPrintManifest(out io.Writer, manifest *manifest.Manifest) error {
if manifest == nil {
_, err := fmt.Fprintln(out, "Manifest: None")
return err
}
return f3Templates.ExecuteTemplate(out, "f3_manifest.go.tmpl", manifest)
}
func prettyPrintFinalityCertificate(out io.Writer, cert *certs.FinalityCertificate) error {
if cert == nil {
_, _ = fmt.Fprintln(out, "Certificate: None")
return nil
}
return f3Templates.ExecuteTemplate(out, "f3_finality_cert.go.tmpl", cert)
}
func f3PowerTableDiffsToString(diff certs.PowerTableDiff) (string, error) {
if len(diff) == 0 {
return "None", nil
}
totalDiff := gpbft.NewStoragePower(0).Int
for _, delta := range diff {
if !delta.IsZero() {
totalDiff = totalDiff.Add(totalDiff, delta.PowerDelta.Int)
}
}
if totalDiff.Cmp(gpbft.NewStoragePower(0).Int) == 0 {
return "None", nil
}
return fmt.Sprintf("Total of %s storage power across %d miner(s).", totalDiff, len(diff)), nil
}
type ranger struct {
from, to uint64
limit int
ascend bool
}
func newRanger(fromTo string, limit int, ascend bool, latest func() (uint64, error)) (*ranger, error) {
parts := strings.Split(strings.TrimSpace(fromTo), "..")
if len(parts) != 2 || (parts[0] == "" && parts[1] == "") {
return nil, fmt.Errorf("invalid range format: expected '<from>..<to>', got: %s", fromTo)
}
r := ranger{
limit: limit,
ascend: ascend,
}
var err error
if parts[0] != "" {
r.from, err = strconv.ParseUint(parts[0], 10, 64)
if err != nil {
return nil, fmt.Errorf("determining 'from' instance: %v", err)
}
}
if parts[1] != "" {
r.to, err = strconv.ParseUint(parts[1], 10, 64)
} else {
r.to, err = latest()
}
if err != nil {
return nil, fmt.Errorf("determining 'to' instance: %v", err)
}
if r.from > r.to {
return nil, fmt.Errorf("invalid range: 'from' cannot exceed 'to': %d > %d", r.from, r.to)
}
if !ascend {
r.from, r.to = r.to, r.from
}
return &r, nil
}
func (r *ranger) next() (uint64, bool) {
if r.limit == 0 {
return 0, false
}
next := r.from
if r.from == r.to {
r.limit = 0
return next, true
}
if r.ascend {
r.from++
} else if r.from > 0 {
r.from--
}
r.limit--
return next, true
}