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"
|
2021-06-22 19:48:33 +00:00
|
|
|
|
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-node/pkg/services/control"
|
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-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")
|
2020-11-05 07:15:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var getEpochCmd = &cobra.Command{
|
|
|
|
Use: "epoch",
|
|
|
|
Short: "Get current epoch number",
|
|
|
|
Long: "Get current epoch number",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-04-21 12:27:32 +00:00
|
|
|
key, err := getKey()
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2021-04-21 12:27:32 +00:00
|
|
|
|
|
|
|
cli, err := getSDKClient(key)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2021-03-15 11:23:04 +00:00
|
|
|
netInfo, err := cli.NetworkInfo(context.Background(), globalCallOptions()...)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
cmd.Println(netInfo.CurrentEpoch())
|
2020-11-05 07:15:28 +00:00
|
|
|
},
|
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`,
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-04-21 12:27:32 +00:00
|
|
|
key, err := getKey()
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2021-04-21 12:27:32 +00:00
|
|
|
|
|
|
|
cli, err := getSDKClient(key)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2020-11-06 10:36:32 +00:00
|
|
|
|
2020-12-24 11:25:36 +00:00
|
|
|
nodeInfo, err := cli.EndpointInfo(context.Background(), globalCallOptions()...)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
2020-11-06 10:36:32 +00:00
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
prettyPrintNodeInfo(cmd, nodeInfo.NodeInfo(), nodeInfoJSON)
|
2020-11-06 10:36:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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",
|
2021-06-20 23:02:56 +00:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2021-04-21 12:27:32 +00:00
|
|
|
key, err := getKey()
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2021-04-21 12:27:32 +00:00
|
|
|
|
|
|
|
cli, err := getSDKClient(key)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, err)
|
2021-02-19 08:03:34 +00:00
|
|
|
|
|
|
|
netInfo, err := cli.NetworkInfo(context.Background(), globalCallOptions()...)
|
2021-07-06 12:27:54 +00:00
|
|
|
exitOnErr(cmd, errf("rpc error: %w", err))
|
2021-02-19 08:03:34 +00:00
|
|
|
|
|
|
|
cmd.Printf("Epoch: %d\n", netInfo.CurrentEpoch())
|
|
|
|
|
|
|
|
magic := netInfo.MagicNumber()
|
|
|
|
cmd.Printf("Network magic: [%s] %d\n", netmode.Magic(magic), magic)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func prettyPrintNodeInfo(cmd *cobra.Command, i *netmap.NodeInfo, jsonEncoding bool) {
|
2021-01-14 18:32:14 +00:00
|
|
|
if jsonEncoding {
|
2021-06-20 23:02:56 +00:00
|
|
|
printJSONMarshaler(cmd, i, "node info")
|
2020-11-06 10:36:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
cmd.Println("key:", hex.EncodeToString(i.PublicKey()))
|
|
|
|
cmd.Println("state:", i.State())
|
2021-06-23 12:27:00 +00:00
|
|
|
netmap.IterateAllAddresses(i, func(s string) {
|
|
|
|
cmd.Println("address:", s)
|
|
|
|
})
|
2020-11-06 10:36:32 +00:00
|
|
|
|
|
|
|
for _, attribute := range i.Attributes() {
|
2021-06-20 23:02:56 +00:00
|
|
|
cmd.Printf("attribute: %s=%s\n", attribute.Key(), attribute.Value())
|
2020-11-06 10:36:32 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-14 18:32:14 +00:00
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
func prettyPrintNetmap(cmd *cobra.Command, nm *control.Netmap, jsonEncoding bool) {
|
2021-01-14 18:32:14 +00:00
|
|
|
if jsonEncoding {
|
2021-06-20 23:02:56 +00:00
|
|
|
printJSONMarshaler(cmd, nm, "netmap")
|
2021-01-14 18:32:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-20 23:02:56 +00:00
|
|
|
cmd.Println("Epoch:", nm.GetEpoch())
|
2021-01-14 18:32:14 +00:00
|
|
|
|
|
|
|
for i, node := range nm.GetNodes() {
|
2021-06-22 19:48:33 +00:00
|
|
|
cmd.Printf("Node %d: %s %s %v\n", i+1,
|
2021-01-14 18:32:14 +00:00
|
|
|
base58.Encode(node.GetPublicKey()),
|
|
|
|
node.GetState(),
|
2021-06-22 19:48:33 +00:00
|
|
|
node.GetAddresses(),
|
2021-01-14 18:32:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, attr := range node.GetAttributes() {
|
2021-06-20 23:02:56 +00:00
|
|
|
cmd.Printf("\t%s: %s\n", attr.GetKey(), attr.GetValue())
|
2021-01-14 18:32:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|