From 6a1d149b9a60bc8117e40b16e78d87efe18a948e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tadej=20Jane=C5=BE?= Date: Thu, 6 Feb 2020 11:20:14 +0100 Subject: [PATCH] go/oasis-node/cmd/registry/node: Add is-registered subcommand It checks whether the node is registered. --- .changelog/2508.feature.md | 3 + go/oasis-node/cmd/registry/node/node.go | 75 +++++++++++++++---- .../scenario/e2e/registry_cli.go | 25 +++++++ 3 files changed, 87 insertions(+), 16 deletions(-) create mode 100644 .changelog/2508.feature.md diff --git a/.changelog/2508.feature.md b/.changelog/2508.feature.md new file mode 100644 index 00000000000..ce95842044b --- /dev/null +++ b/.changelog/2508.feature.md @@ -0,0 +1,3 @@ +Add `oasis-node registry node is-registered` subcommand. + +It checks whether the node is registered. diff --git a/go/oasis-node/cmd/registry/node/node.go b/go/oasis-node/cmd/registry/node/node.go index 905c1474678..becc0f64807 100644 --- a/go/oasis-node/cmd/registry/node/node.go +++ b/go/oasis-node/cmd/registry/node/node.go @@ -69,6 +69,12 @@ var ( Run: doList, } + isRegisteredCmd = &cobra.Command{ + Use: "is-registered", + Short: "check whether the node is registered", + Run: doIsRegistered, + } + logger = logging.GetLogger("cmd/registry/node") ) @@ -325,31 +331,68 @@ func doList(cmd *cobra.Command, args []string) { } } +func doIsRegistered(cmd *cobra.Command, args []string) { + if err := cmdCommon.Init(); err != nil { + cmdCommon.EarlyLogAndExit(err) + } + + dataDir, err := cmdCommon.DataDirOrPwd() + if err != nil { + logger.Error("failed to query data directory", + "err", err, + ) + os.Exit(1) + } + + // Load node's identity. + nodeSignerFactory := fileSigner.NewFactory(dataDir, signature.SignerNode, signature.SignerP2P, signature.SignerConsensus) + nodeIdentity, err := identity.Load(dataDir, nodeSignerFactory) + if err != nil { + logger.Error("failed to load node identity", + "err", err, + ) + os.Exit(1) + } + + conn, client := doConnect(cmd) + defer conn.Close() + + nodes, err := client.GetNodes(context.Background(), consensus.HeightLatest) + if err != nil { + logger.Error("failed to query nodes", + "err", err, + ) + os.Exit(1) + } + + for _, node := range nodes { + if node.ID.Equal(nodeIdentity.NodeSigner.Public()) { + fmt.Println("node is registered") + os.Exit(0) + } + } + fmt.Println("node is not registered") + os.Exit(1) +} + // Register registers the node sub-command and all of it's children. func Register(parentCmd *cobra.Command) { - for _, v := range []*cobra.Command{ - initCmd, - listCmd, - } { - nodeCmd.AddCommand(v) - } + initCmd.Flags().AddFlagSet(flags) + initCmd.Flags().AddFlagSet(cmdFlags.DebugTestEntityFlags) + initCmd.Flags().AddFlagSet(cmdFlags.SignerFlags) + listCmd.Flags().AddFlagSet(cmdGrpc.ClientFlags) listCmd.Flags().AddFlagSet(cmdFlags.VerboseFlags) - for _, v := range []*cobra.Command{ - initCmd, - } { - v.Flags().AddFlagSet(cmdFlags.DebugTestEntityFlags) - v.Flags().AddFlagSet(cmdFlags.SignerFlags) - v.Flags().AddFlagSet(flags) - } + isRegisteredCmd.Flags().AddFlagSet(cmdGrpc.ClientFlags) - for _, v := range []*cobra.Command{ + for _, subCmd := range []*cobra.Command{ + initCmd, listCmd, + isRegisteredCmd, } { - v.Flags().AddFlagSet(cmdGrpc.ClientFlags) + nodeCmd.AddCommand(subCmd) } - parentCmd.AddCommand(nodeCmd) } diff --git a/go/oasis-test-runner/scenario/e2e/registry_cli.go b/go/oasis-test-runner/scenario/e2e/registry_cli.go index 22779580117..6f81b69c77c 100644 --- a/go/oasis-test-runner/scenario/e2e/registry_cli.go +++ b/go/oasis-test-runner/scenario/e2e/registry_cli.go @@ -111,6 +111,12 @@ func (r *registryCLIImpl) testEntityAndNode(childEnv *env.Env, cli *cli.Helpers) if len(nodes) != 3 { return fmt.Errorf("initial node list wrong number of nodes: %d, expected at least: %d. Nodes: %s", len(nodes), 3, nodes) } + // Check that is-registered subcommand detects all validators as registered. + for _, val := range r.basicImpl.net.Validators() { + if err = r.isRegistered(childEnv, val.Name, val.DataDir()); err != nil { + return err + } + } // Init new entity. entDir, err := childEnv.NewSubDir("entity") @@ -134,6 +140,10 @@ func (r *registryCLIImpl) testEntityAndNode(childEnv *env.Env, cli *cli.Helpers) if err != nil { return err } + err = r.isRegistered(childEnv, "node", nDir.String()) + if err == nil || !strings.Contains(err.Error(), "node is not registered") { + return errors.New("is-registered should detect the new node is not registered") + } // Update entity with a new node. var entUp *entity.Entity @@ -315,6 +325,21 @@ func (r *registryCLIImpl) listNodes(childEnv *env.Env) ([]signature.PublicKey, e return nodes, nil } +// isRegistered checks if the given node is registered. +func (r *registryCLIImpl) isRegistered(childEnv *env.Env, nodeName, nodeDataDir string) error { + r.logger.Info(fmt.Sprintf("checking if node %s is registered", nodeName)) + args := []string{ + "registry", "node", "is-registered", + "--" + grpc.CfgAddress, "unix:" + r.basicImpl.net.Validators()[0].SocketPath(), + "--" + cmdCommon.CfgDataDir, nodeDataDir, + } + out, err := cli.RunSubCommandWithOutput(childEnv, r.logger, "is-registered", r.basicImpl.net.Config().NodeBinary, args) + if err != nil { + return fmt.Errorf("failed to check if node %s is registered: error: %w output: %s", nodeName, err, out.String()) + } + return nil +} + // newTestNode returns a test node instance given the entityID. func (r *registryCLIImpl) newTestNode(entityID signature.PublicKey) (*node.Node, []string, []string, []string, error) { // Addresses.