2020-08-04 14:46:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-11-05 07:15:28 +00:00
|
|
|
"context"
|
2020-11-06 13:23:31 +00:00
|
|
|
"encoding/hex"
|
2020-08-04 14:46:12 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-01-14 18:32:14 +00:00
|
|
|
"github.com/mr-tron/base58"
|
2021-02-19 08:03:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/config/netmode"
|
2020-11-06 10:36:32 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2021-01-14 18:32:14 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/util/signature"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/control"
|
|
|
|
controlSvc "github.com/nspcc-dev/neofs-node/pkg/services/control/server"
|
2020-08-04 14:46:12 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2020-11-06 10:36:32 +00:00
|
|
|
var (
|
|
|
|
nodeInfoJSON bool
|
2021-01-14 18:32:14 +00:00
|
|
|
|
|
|
|
netmapSnapshotJSON bool
|
2020-11-06 10:36:32 +00:00
|
|
|
)
|
|
|
|
|
2020-08-04 14:46:12 +00:00
|
|
|
// netmapCmd represents the netmap command
|
|
|
|
var netmapCmd = &cobra.Command{
|
|
|
|
Use: "netmap",
|
|
|
|
Short: "Operations with Network Map",
|
|
|
|
Long: `Operations with Network Map`,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(netmapCmd)
|
|
|
|
|
2020-11-05 07:15:28 +00:00
|
|
|
netmapCmd.AddCommand(
|
|
|
|
getEpochCmd,
|
2020-11-06 10:36:32 +00:00
|
|
|
localNodeInfoCmd,
|
2021-01-14 18:32:14 +00:00
|
|
|
snapshotCmd,
|
2021-02-19 08:03:34 +00:00
|
|
|
netInfoCmd,
|
2020-11-05 07:15:28 +00:00
|
|
|
)
|
2020-11-06 10:36:32 +00:00
|
|
|
|
|
|
|
localNodeInfoCmd.Flags().BoolVar(&nodeInfoJSON, "json", false, "print node info in JSON format")
|
2021-01-14 18:32:14 +00:00
|
|
|
|
|
|
|
snapshotCmd.Flags().BoolVar(&netmapSnapshotJSON, "json", false,
|
|
|
|
"print netmap structure in JSON format")
|
2020-11-05 07:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var getEpochCmd = &cobra.Command{
|
|
|
|
Use: "epoch",
|
|
|
|
Short: "Get current epoch number",
|
|
|
|
Long: "Get current epoch number",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2021-04-21 12:27:32 +00:00
|
|
|
key, err := getKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cli, err := getSDKClient(key)
|
2020-11-05 07:15:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2021-03-15 11:23:04 +00:00
|
|
|
netInfo, err := cli.NetworkInfo(context.Background(), globalCallOptions()...)
|
2020-11-05 07:15:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("rpc error: %w", err)
|
|
|
|
}
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2021-03-15 11:23:04 +00:00
|
|
|
fmt.Println(netInfo.CurrentEpoch())
|
2020-11-05 07:15:28 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
2020-11-06 10:36:32 +00:00
|
|
|
|
|
|
|
var localNodeInfoCmd = &cobra.Command{
|
|
|
|
Use: "nodeinfo",
|
|
|
|
Short: "Get local node info",
|
|
|
|
Long: `Get local node info`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2021-04-21 12:27:32 +00:00
|
|
|
key, err := getKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cli, err := getSDKClient(key)
|
2020-11-06 10:36:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-12-24 11:25:36 +00:00
|
|
|
nodeInfo, err := cli.EndpointInfo(context.Background(), globalCallOptions()...)
|
2020-11-06 10:36:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("rpc error: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-03-15 11:23:04 +00:00
|
|
|
prettyPrintNodeInfo(nodeInfo.NodeInfo(), nodeInfoJSON)
|
2020-11-06 10:36:32 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-01-14 18:32:14 +00:00
|
|
|
var snapshotCmd = &cobra.Command{
|
|
|
|
Use: "snapshot",
|
|
|
|
Short: "Get network map snapshot",
|
|
|
|
Long: "Get network map snapshot",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
key, err := getKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := new(control.NetmapSnapshotRequest)
|
|
|
|
req.SetBody(new(control.NetmapSnapshotRequest_Body))
|
|
|
|
|
|
|
|
if err := controlSvc.SignMessage(key, req); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-21 12:27:32 +00:00
|
|
|
cli, err := getSDKClient(key)
|
2020-11-06 10:36:32 +00:00
|
|
|
if err != nil {
|
2021-01-14 18:32:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-15 11:23:04 +00:00
|
|
|
resp, err := control.NetmapSnapshot(cli.Raw(), req)
|
2021-01-14 18:32:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-11-06 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 18:32:14 +00:00
|
|
|
sign := resp.GetSignature()
|
|
|
|
|
|
|
|
if err := signature.VerifyDataWithSource(resp, func() ([]byte, []byte) {
|
|
|
|
return sign.GetKey(), sign.GetSign()
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
2020-11-06 10:36:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-14 18:32:14 +00:00
|
|
|
prettyPrintNetmap(resp.GetBody().GetNetmap(), netmapSnapshotJSON)
|
2020-11-06 10:36:32 +00:00
|
|
|
|
2021-01-14 18:32:14 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-02-19 08:03:34 +00:00
|
|
|
var netInfoCmd = &cobra.Command{
|
|
|
|
Use: "netinfo",
|
|
|
|
Short: "Get information about NeoFS network",
|
|
|
|
Long: "Get information about NeoFS network",
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2021-04-21 12:27:32 +00:00
|
|
|
key, err := getKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cli, err := getSDKClient(key)
|
2021-02-19 08:03:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
netInfo, err := cli.NetworkInfo(context.Background(), globalCallOptions()...)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("rpc error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmd.Printf("Epoch: %d\n", netInfo.CurrentEpoch())
|
|
|
|
|
|
|
|
magic := netInfo.MagicNumber()
|
|
|
|
cmd.Printf("Network magic: [%s] %d\n", netmode.Magic(magic), magic)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-01-14 18:32:14 +00:00
|
|
|
func prettyPrintNodeInfo(i *netmap.NodeInfo, jsonEncoding bool) {
|
|
|
|
if jsonEncoding {
|
|
|
|
printJSONMarshaler(i, "node info")
|
2020-11-06 10:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-11-06 13:23:31 +00:00
|
|
|
fmt.Println("key:", hex.EncodeToString(i.PublicKey()))
|
2020-11-06 10:36:32 +00:00
|
|
|
fmt.Println("address:", i.Address())
|
|
|
|
fmt.Println("state:", i.State())
|
|
|
|
|
|
|
|
for _, attribute := range i.Attributes() {
|
|
|
|
fmt.Printf("attribute: %s=%s\n", attribute.Key(), attribute.Value())
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 18:32:14 +00:00
|
|
|
|
|
|
|
func prettyPrintNetmap(nm *control.Netmap, jsonEncoding bool) {
|
|
|
|
if jsonEncoding {
|
2021-01-15 07:48:15 +00:00
|
|
|
printJSONMarshaler(nm, "netmap")
|
2021-01-14 18:32:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Epoch:", nm.GetEpoch())
|
|
|
|
|
|
|
|
for i, node := range nm.GetNodes() {
|
|
|
|
fmt.Printf("Node %d: %s %s %s\n", i+1,
|
|
|
|
base58.Encode(node.GetPublicKey()),
|
|
|
|
node.GetAddress(),
|
|
|
|
node.GetState(),
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, attr := range node.GetAttributes() {
|
|
|
|
fmt.Printf("\t%s: %s\n", attr.GetKey(), attr.GetValue())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|