Skip to content

Commit

Permalink
go/oasis-node/cmd/registry/node: Add is-registered subcommand
Browse files Browse the repository at this point in the history
It checks whether the node is registered.
  • Loading branch information
tjanez committed Feb 6, 2020
1 parent 9b9df5e commit 6a1d149
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .changelog/2508.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add `oasis-node registry node is-registered` subcommand.

It checks whether the node is registered.
75 changes: 59 additions & 16 deletions go/oasis-node/cmd/registry/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

Expand Down Expand Up @@ -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)
}

Expand Down
25 changes: 25 additions & 0 deletions go/oasis-test-runner/scenario/e2e/registry_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 6a1d149

Please sign in to comment.