2020-08-04 14:46:12 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-11-06 13:23:31 +00:00
|
|
|
"encoding/hex"
|
2021-10-14 08:27:08 +00:00
|
|
|
"time"
|
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"
|
2021-10-28 14:48:46 +00:00
|
|
|
internalclient "github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/client"
|
2022-05-18 11:03:40 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/common"
|
2022-05-18 09:22:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/cmd/neofs-cli/internal/commonflags"
|
2022-01-31 11:58:55 +00:00
|
|
|
nmClient "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
|
2021-01-14 18:32:14 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/control"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
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`,
|
2021-09-27 15:36:14 +00:00
|
|
|
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
|
|
|
// bind exactly that cmd's flags to
|
|
|
|
// the viper before execution
|
2022-05-18 09:22:02 +00:00
|
|
|
commonflags.Bind(cmd)
|
2021-10-07 10:37:57 +00:00
|
|
|
bindAPIFlags(cmd)
|
2021-09-27 15:36:14 +00:00
|
|
|
},
|
2020-08-04 14:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2021-09-27 15:36:14 +00:00
|
|
|
netmapChildCommands := []*cobra.Command{
|
2020-11-05 07:15:28 +00:00
|
|
|
getEpochCmd,
|
2020-11-06 10:36:32 +00:00
|
|
|
localNodeInfoCmd,
|
2021-02-19 08:03:34 +00:00
|
|
|
netInfoCmd,
|
2021-09-27 15:36:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rootCmd.AddCommand(netmapCmd)
|
|
|
|
netmapCmd.AddCommand(netmapChildCommands...)
|
2020-11-06 10:36:32 +00:00
|
|
|
|
2022-05-18 09:22:02 +00:00
|
|
|
commonflags.Init(getEpochCmd)
|
|
|
|
commonflags.Init(netInfoCmd)
|
2021-09-27 15:36:14 +00:00
|
|
|
|
2022-05-18 09:22:02 +00:00
|
|
|
commonflags.Init(localNodeInfoCmd)
|
2020-11-06 10:36:32 +00:00
|
|
|
localNodeInfoCmd.Flags().BoolVar(&nodeInfoJSON, "json", false, "print node info in JSON format")
|
2021-09-27 15:36:14 +00:00
|
|
|
|
|
|
|
for _, netmapCommand := range netmapChildCommands {
|
|
|
|
flags := netmapCommand.Flags()
|
|
|
|
|
|
|
|
flags.StringSliceVarP(&xHeaders, xHeadersKey, xHeadersShorthand, xHeadersDefault, xHeadersUsage)
|
|
|
|
flags.Uint32P(ttl, ttlShorthand, ttlDefault, ttlUsage)
|
|
|
|
}
|
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-10-28 14:48:46 +00:00
|
|
|
var prm internalclient.NetworkInfoPrm
|
2021-04-21 12:27:32 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
prepareAPIClient(cmd, &prm)
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
res, err := internalclient.NetworkInfo(prm)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "rpc error: %w", err)
|
2020-08-04 14:46:12 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
netInfo := res.NetworkInfo()
|
|
|
|
|
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-10-28 14:48:46 +00:00
|
|
|
var prm internalclient.NodeInfoPrm
|
2021-04-21 12:27:32 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
prepareAPIClient(cmd, &prm)
|
2020-11-06 10:36:32 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
res, err := internalclient.NodeInfo(prm)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "rpc error: %w", err)
|
2020-11-06 10:36:32 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
prettyPrintNodeInfo(cmd, res.NodeInfo(), nodeInfoJSON)
|
2020-11-06 10:36:32 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-09-20 16:15:58 +00:00
|
|
|
type netCfgWriter cobra.Command
|
|
|
|
|
|
|
|
func (x *netCfgWriter) print(name string, v interface{}, unknown bool) {
|
|
|
|
var sUnknown string
|
|
|
|
|
|
|
|
if unknown {
|
|
|
|
sUnknown = " (unknown)"
|
|
|
|
}
|
|
|
|
|
|
|
|
(*cobra.Command)(x).Printf(" %s%s: %v\n", name, sUnknown, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *netCfgWriter) UnknownParameter(k string, v []byte) {
|
|
|
|
x.print(k, hex.EncodeToString(v), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *netCfgWriter) MaxObjectSize(v uint64) {
|
|
|
|
x.print("Maximum object size", v, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *netCfgWriter) BasicIncomeRate(v uint64) {
|
|
|
|
x.print("Basic income rate", v, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *netCfgWriter) AuditFee(v uint64) {
|
|
|
|
x.print("Audit fee", v, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *netCfgWriter) EpochDuration(v uint64) {
|
|
|
|
x.print("Epoch duration", v, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *netCfgWriter) ContainerFee(v uint64) {
|
|
|
|
x.print("Container fee", v, false)
|
|
|
|
}
|
|
|
|
|
2021-10-19 15:14:23 +00:00
|
|
|
func (x *netCfgWriter) ContainerAliasFee(v uint64) {
|
|
|
|
x.print("Container alias fee", v, false)
|
|
|
|
}
|
|
|
|
|
2021-09-20 16:15:58 +00:00
|
|
|
func (x *netCfgWriter) EigenTrustIterations(v uint64) {
|
|
|
|
x.print("Number EigenTrust of iterations", v, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *netCfgWriter) EigenTrustAlpha(v float64) {
|
|
|
|
x.print("EigenTrust α", v, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *netCfgWriter) InnerRingCandidateFee(v uint64) {
|
|
|
|
x.print("Inner Ring candidate fee", v, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x *netCfgWriter) WithdrawFee(v uint64) {
|
|
|
|
x.print("Withdraw fee", v, false)
|
|
|
|
}
|
|
|
|
|
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-10-28 14:48:46 +00:00
|
|
|
var prm internalclient.NetworkInfoPrm
|
2021-04-21 12:27:32 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
prepareAPIClient(cmd, &prm)
|
2021-02-19 08:03:34 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
res, err := internalclient.NetworkInfo(prm)
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "rpc error: %w", err)
|
2021-02-19 08:03:34 +00:00
|
|
|
|
2021-10-28 14:48:46 +00:00
|
|
|
netInfo := res.NetworkInfo()
|
|
|
|
|
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-09-20 16:15:58 +00:00
|
|
|
|
2021-10-14 08:27:08 +00:00
|
|
|
cmd.Printf("Time per block: %s\n", time.Duration(netInfo.MsPerBlock())*time.Millisecond)
|
2021-09-20 16:15:58 +00:00
|
|
|
|
|
|
|
netCfg := netInfo.NetworkConfig()
|
|
|
|
|
|
|
|
cmd.Println("NeoFS network configuration")
|
|
|
|
|
2022-01-31 11:58:55 +00:00
|
|
|
err = nmClient.WriteConfig((*netCfgWriter)(cmd), func(f func(key []byte, val []byte) error) error {
|
2021-09-20 16:15:58 +00:00
|
|
|
var err error
|
|
|
|
|
|
|
|
netCfg.IterateParameters(func(prm *netmap.NetworkParameter) bool {
|
|
|
|
err = f(prm.Key(), prm.Value())
|
|
|
|
return err != nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
2022-05-18 11:03:40 +00:00
|
|
|
common.ExitOnErr(cmd, "read config: %w", err)
|
2021-02-19 08:03:34 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|