Skip to content

Commit

Permalink
Add beacon id to some cli commands (#887)
Browse files Browse the repository at this point in the history
* add beacon id to some cli commands

Co-authored-by: Ezequiel Raynaudo <raynaudo.ee@gmail.com>
  • Loading branch information
emmanuelm41 and raynaudoe committed Dec 23, 2021
1 parent 2c63d68 commit 110d212
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 77 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ relay-s3:
drand-relay-s3: relay-s3

build_all: drand drand-client drand-relay-http drand-relay-gossip drand-relay-s3

build_docker_all: build_docker build_docker_dev
build_docker:
docker build --build-arg major=$(MAJOR) --build-arg minor=$(MINOR) --build-arg patch=$(PATCH) --build-arg gitCommit=`git rev-parse HEAD` -t drandorg/go-drand:latest .

Expand Down
29 changes: 17 additions & 12 deletions cmd/drand-cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,17 @@ var hashOnly = &cli.BoolFlag{
Usage: "Only print the hash of the group file",
}

var hashInfoFlag = &cli.StringFlag{
var hashInfoReq = &cli.StringFlag{
Name: "chain-hash",
Usage: "The hash of the chain info",
Required: true,
}

var hashInfoNoReq = &cli.StringFlag{
Name: "chain-hash",
Usage: "The hash of the chain info",
}

// using a simple string flag because the StringSliceFlag is not intuitive
// see https://github.com/urfave/cli/issues/62
var syncNodeFlag = &cli.StringFlag{
Expand Down Expand Up @@ -295,7 +300,7 @@ var appCommands = []*cli.Command{
{
Name: "stop",
Usage: "Stop the drand daemon.\n",
Flags: toArray(controlFlag),
Flags: toArray(controlFlag, beaconIDFlag),
Action: func(c *cli.Context) error {
banner()
return stopDaemon(c)
Expand All @@ -317,7 +322,7 @@ var appCommands = []*cli.Command{
{
Name: "follow",
Usage: "follow and store a randomness chain",
Flags: toArray(folderFlag, controlFlag, hashInfoFlag, syncNodeFlag,
Flags: toArray(folderFlag, controlFlag, hashInfoReq, syncNodeFlag,
tlsCertFlag, insecureFlag, upToFlag, beaconIDFlag),
Action: followCmd,
},
Expand Down Expand Up @@ -369,7 +374,7 @@ var appCommands = []*cli.Command{
Name: "chain-info",
Usage: "Get the binding chain information that this nodes participates to",
ArgsUsage: "`ADDRESS1` `ADDRESS2` ... provides the addresses of the node to try to contact to.",
Flags: toArray(tlsCertFlag, insecureFlag, hashOnly),
Flags: toArray(tlsCertFlag, insecureFlag, hashOnly, hashInfoNoReq),
Action: getChainInfo,
},
},
Expand All @@ -392,7 +397,7 @@ var appCommands = []*cli.Command{
Usage: "Ask for the statuses of remote nodes indicated by " +
"`ADDRESS1 ADDRESS2 ADDRESS3...`, including the network " +
"visibility over the rest of the addresses given.",
Flags: toArray(controlFlag, jsonFlag),
Flags: toArray(controlFlag, jsonFlag, beaconIDFlag),
Action: remoteStatusCmd,
},
{
Expand All @@ -410,7 +415,7 @@ var appCommands = []*cli.Command{
{
Name: "status",
Usage: "Get the status of many modules of running the daemon\n",
Flags: toArray(controlFlag, jsonFlag),
Flags: toArray(controlFlag, jsonFlag, beaconIDFlag),
Action: statusCmd,
},
{
Expand Down Expand Up @@ -444,7 +449,7 @@ var appCommands = []*cli.Command{
{
Name: "backup",
Usage: "backs up the primary drand database to a secondary location.",
Flags: toArray(outFlag, controlFlag),
Flags: toArray(outFlag, controlFlag, beaconIDFlag),
Action: backupDBCmd,
},
},
Expand All @@ -462,33 +467,33 @@ var appCommands = []*cli.Command{
{
Name: "share",
Usage: "shows the private share\n",
Flags: toArray(controlFlag),
Flags: toArray(controlFlag, beaconIDFlag),
Action: showShareCmd,
},
{
Name: "group",
Usage: "shows the current group.toml used. The group.toml " +
"may contain the distributed public key if the DKG has been " +
"ran already.\n",
Flags: toArray(outFlag, controlFlag, hashOnly),
Flags: toArray(outFlag, controlFlag, hashOnly, beaconIDFlag),
Action: showGroupCmd,
},
{
Name: "chain-info",
Usage: "shows the chain information this node is participating to",
Flags: toArray(controlFlag, hashOnly),
Flags: toArray(controlFlag, hashOnly, beaconIDFlag),
Action: showChainInfo,
},
{
Name: "private",
Usage: "shows the long-term private key of a node.\n",
Flags: toArray(controlFlag),
Flags: toArray(controlFlag, beaconIDFlag),
Action: showPrivateCmd,
},
{
Name: "public",
Usage: "shows the long-term public key of a node.\n",
Flags: toArray(controlFlag),
Flags: toArray(controlFlag, beaconIDFlag),
Action: showPublicCmd,
},
},
Expand Down
45 changes: 36 additions & 9 deletions cmd/drand-cli/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,16 +317,20 @@ func remoteStatusCmd(c *cli.Context) error {
if err != nil {
return err
}

ips := c.Args().Slice()
isTLS := !c.IsSet(insecureFlag.Name)
beaconID := getBeaconID(c)

addresses := make([]*drand.Address, len(ips))
for i := 0; i < len(ips); i++ {
addresses[i] = &drand.Address{
Address: ips[i],
Tls: isTLS,
}
}
resp, err := client.RemoteStatus(c.Context, addresses)

resp, err := client.RemoteStatus(c.Context, addresses, beaconID)
if err != nil {
return err
}
Expand Down Expand Up @@ -377,7 +381,9 @@ func statusCmd(c *cli.Context) error {
if err != nil {
return err
}
resp, err := client.Status()

beaconID := getBeaconID(c)
resp, err := client.Status(beaconID)
if err != nil {
return fmt.Errorf("drand: can't get the status of the daemon ... %s", err)
}
Expand Down Expand Up @@ -411,6 +417,7 @@ func schemesCmd(c *cli.Context) error {
if err != nil {
return err
}

resp, err := client.ListSchemes()
if err != nil {
return fmt.Errorf("drand: can't get the list of scheme ids availables ... %s", err)
Expand All @@ -431,14 +438,18 @@ func showGroupCmd(c *cli.Context) error {
if err != nil {
return err
}
r, err := client.GroupFile()

beaconID := getBeaconID(c)
r, err := client.GroupFile(beaconID)
if err != nil {
return fmt.Errorf("fetching group file error: %s", err)
}

group, err := key.GroupFromProto(r)
if err != nil {
return err
}

return groupOut(c, group)
}

Expand All @@ -447,14 +458,18 @@ func showChainInfo(c *cli.Context) error {
if err != nil {
return err
}
resp, err := client.ChainInfo()

beaconID := getBeaconID(c)
resp, err := client.ChainInfo(beaconID)
if err != nil {
return fmt.Errorf("could not request chain info: %s", err)
}

ci, err := chain.InfoFromProto(resp)
if err != nil {
return fmt.Errorf("could not get correct chain info: %s", err)
}

return printChainInfo(c, ci)
}

Expand All @@ -463,10 +478,13 @@ func showPrivateCmd(c *cli.Context) error {
if err != nil {
return err
}
resp, err := client.PrivateKey()

beaconID := getBeaconID(c)
resp, err := client.PrivateKey(beaconID)
if err != nil {
return fmt.Errorf("could not request drand.private: %s", err)
}

return printJSON(resp)
}

Expand All @@ -475,10 +493,13 @@ func showPublicCmd(c *cli.Context) error {
if err != nil {
return err
}
resp, err := client.PublicKey()

beaconID := getBeaconID(c)
resp, err := client.PublicKey(beaconID)
if err != nil {
return fmt.Errorf("drand: could not request drand.public: %s", err)
}

return printJSON(resp)
}

Expand All @@ -487,7 +508,9 @@ func showShareCmd(c *cli.Context) error {
if err != nil {
return err
}
resp, err := client.Share()

beaconID := getBeaconID(c)
resp, err := client.Share(beaconID)
if err != nil {
return fmt.Errorf("could not request drand.share: %s", err)
}
Expand All @@ -499,10 +522,14 @@ func backupDBCmd(c *cli.Context) error {
if err != nil {
return err
}
err = client.BackupDB(c.String(outFlag.Name))

outDir := c.String(outFlag.Name)
beaconID := getBeaconID(c)
err = client.BackupDB(outDir, beaconID)
if err != nil {
return fmt.Errorf("could not back up: %s", err)
}

return nil
}

Expand Down Expand Up @@ -584,7 +611,7 @@ func followCmd(c *cli.Context) error {
addrs := strings.Split(c.String(syncNodeFlag.Name), ",")
channel, errCh, err := ctrlClient.StartFollowChain(
c.Context,
c.String(hashInfoFlag.Name),
c.String(hashInfoReq.Name),
addrs,
!c.Bool(insecureFlag.Name),
uint64(c.Int(upToFlag.Name)),
Expand Down
18 changes: 15 additions & 3 deletions cmd/drand-cli/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,21 @@ func stopDaemon(c *cli.Context) error {
if err != nil {
return err
}
if _, err := ctrlClient.Shutdown(); err != nil {
return fmt.Errorf("error stopping drand daemon: %w", err)

beaconID := c.String(beaconIDFlag.Name)
_, err = ctrlClient.Shutdown(beaconID)

if beaconID != "" {
if err != nil {
return fmt.Errorf("error stopping beacon process [%s]: %w", beaconID, err)
}
fmt.Fprintf(output, "beacon process [%s] stopped correctly. Bye.\n", beaconID)
} else {
if err != nil {
return fmt.Errorf("error stopping drand daemon: %w", err)
}
fmt.Fprintf(output, "drand daemon stopped correctly. Bye.\n")
}
fmt.Println("drand daemon stopped correctly. Bye.")

return nil
}
16 changes: 13 additions & 3 deletions cmd/drand-cli/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func getPrivateCmd(c *cli.Context) error {
if err != nil {
return err
}
grpcClient := core.NewGrpcClientFromCert(defaultManager)
grpcClient := core.NewGrpcClientFromCert(nil, defaultManager)
var resp []byte
for _, public := range ids {
resp, err = grpcClient.Private(public.Identity)
Expand All @@ -55,6 +55,7 @@ func getPublicRandomness(c *cli.Context) error {
if !c.Args().Present() {
return errors.New("get public command takes a group file as argument")
}

certPath := ""
if c.IsSet(tlsCertFlag.Name) {
certPath = c.String(tlsCertFlag.Name)
Expand All @@ -64,6 +65,7 @@ func getPublicRandomness(c *cli.Context) error {
if err != nil {
return err
}

group, err := getGroup(c)
if err != nil {
return err
Expand Down Expand Up @@ -100,14 +102,22 @@ func getPublicRandomness(c *cli.Context) error {
}

func getChainInfo(c *cli.Context) error {
var grpcClient = core.NewGrpcClient()
var err error
chainHash := make([]byte, 0)
if c.IsSet(hashInfoNoReq.Name) {
if chainHash, err = hex.DecodeString(c.String(hashInfoNoReq.Name)); err != nil {
return fmt.Errorf("invalid chain hash given: %s", err)
}
}

grpcClient := core.NewGrpcClient(chainHash)
if c.IsSet(tlsCertFlag.Name) {
defaultManager := net.NewCertManager()
certPath := c.String(tlsCertFlag.Name)
if err := defaultManager.Add(certPath); err != nil {
return err
}
grpcClient = core.NewGrpcClientFromCert(defaultManager)
grpcClient = core.NewGrpcClientFromCert(chainHash, defaultManager)
}
var ci *chain.Info
for _, addr := range c.Args().Slice() {
Expand Down
3 changes: 2 additions & 1 deletion cmd/relay/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ func Relay(c *cli.Context) error {
handler.RegisterNewBeaconHandler(c, fmt.Sprintf("%x", hash))
}
} else {
return fmt.Errorf("must specify flag %s or %s", lib.HashFlag.Name, lib.HashListFlag.Name)
fmt.Println("hash flags were not set. Registering beacon handler for default chain hash")
handler.RegisterNewBeaconHandler(client, common.DefaultChainHash)
}
}

Expand Down
Loading

0 comments on commit 110d212

Please sign in to comment.