Skip to content

Commit c5b5134

Browse files
authored
Implement command to list power table and certificates form a peer (#735)
Implement command to get certificates from a specific peer using the cert exchange protocol and optionally list power table entries for the first certificate listed.
1 parent 3af226d commit c5b5134

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

cmd/f3/certs.go

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"fmt"
7+
"strconv"
8+
"time"
9+
10+
"github.com/filecoin-project/go-f3/certexchange"
11+
"github.com/filecoin-project/go-f3/certs"
12+
"github.com/filecoin-project/go-f3/gpbft"
13+
"github.com/ipfs/go-cid"
14+
"github.com/libp2p/go-libp2p"
15+
"github.com/libp2p/go-libp2p/core/peer"
16+
"github.com/urfave/cli/v2"
17+
)
18+
19+
var (
20+
certFrom *peer.AddrInfo
21+
22+
certsCmd = cli.Command{
23+
Name: "certs",
24+
Subcommands: []*cli.Command{
25+
{
26+
Name: "list-from",
27+
Flags: []cli.Flag{
28+
limitFlag,
29+
fromAddrFlag,
30+
networkNameFlag,
31+
includePowerTableFlag,
32+
},
33+
Action: func(cctx *cli.Context) error {
34+
instanceArg := cctx.Args().First()
35+
if instanceArg == "" {
36+
return errors.New("missing instance as first argument")
37+
}
38+
instance, err := strconv.ParseUint(instanceArg, 10, 64)
39+
if err != nil {
40+
return err
41+
}
42+
43+
if certFrom == nil {
44+
return errors.New("--from addrinfo is required")
45+
}
46+
47+
host, err := libp2p.New()
48+
if err != nil {
49+
return err
50+
}
51+
defer func() { _ = host.Close() }()
52+
53+
if err := host.Connect(cctx.Context, *certFrom); err != nil {
54+
return err
55+
}
56+
client := certexchange.Client{
57+
Host: host,
58+
NetworkName: gpbft.NetworkName(cctx.String(networkNameFlag.Name)),
59+
RequestTimeout: cctx.Duration(timeoutFlag.Name),
60+
}
61+
62+
rh, ch, err := client.Request(cctx.Context, certFrom.ID, &certexchange.Request{
63+
FirstInstance: instance,
64+
Limit: cctx.Uint64(limitFlag.Name),
65+
IncludePowerTable: true,
66+
})
67+
if err != nil {
68+
return err
69+
}
70+
var result = struct {
71+
*certexchange.ResponseHeader
72+
PowerTableCID cid.Cid
73+
Certificates []*certs.FinalityCertificate
74+
}{
75+
ResponseHeader: rh,
76+
}
77+
78+
result.PowerTableCID, err = certs.MakePowerTableCID(rh.PowerTable)
79+
if err != nil {
80+
return err
81+
}
82+
83+
for certificate := range ch {
84+
result.Certificates = append(result.Certificates, certificate)
85+
}
86+
output, err := json.MarshalIndent(result, "", " ")
87+
if err != nil {
88+
return err
89+
}
90+
_, _ = fmt.Fprintln(cctx.App.Writer, string(output))
91+
return nil
92+
},
93+
},
94+
},
95+
}
96+
97+
limitFlag = &cli.Uint64Flag{
98+
Name: "limit",
99+
Usage: "Maximum number of certificates to list from the peer",
100+
Value: 100,
101+
}
102+
fromAddrFlag = &cli.StringFlag{
103+
Name: "peer",
104+
Aliases: []string{"p"},
105+
Usage: "The addrinfo of the peer to get the certificate from",
106+
Action: func(cctx *cli.Context, v string) (err error) {
107+
certFrom, err = peer.AddrInfoFromString(v)
108+
return
109+
},
110+
Required: true,
111+
}
112+
networkNameFlag = &cli.StringFlag{
113+
Name: "networkName",
114+
Aliases: []string{"nn"},
115+
Usage: "The network name",
116+
Required: true,
117+
}
118+
119+
includePowerTableFlag = &cli.BoolFlag{
120+
Name: "includePowerTable",
121+
Aliases: []string{"pt"},
122+
Usage: "Whether to include the power table in the results",
123+
}
124+
timeoutFlag = &cli.DurationFlag{
125+
Name: "timeout",
126+
Usage: "Request timeout.",
127+
Value: 30 * time.Second,
128+
}
129+
)

cmd/f3/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ func main() {
2626
&manifestCmd,
2727
&observerCmd,
2828
&toolsCmd,
29+
&certsCmd,
2930
},
3031
}
3132

0 commit comments

Comments
 (0)