From 7204f2235803eea9430fa13ae2b4cbb113dcbf1c Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Wed, 22 Jan 2025 22:57:46 +0300 Subject: [PATCH] adm: provide a way to look into v2 candidates Signed-off-by: Roman Khimov --- .../modules/fschain/netmap_candidates.go | 42 ++++++++++++++----- .../internal/modules/fschain/root.go | 3 +- pkg/morph/client/netmap/netmap.go | 7 ++-- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/cmd/neofs-adm/internal/modules/fschain/netmap_candidates.go b/cmd/neofs-adm/internal/modules/fschain/netmap_candidates.go index b45e7050de..68d2f1d2cd 100644 --- a/cmd/neofs-adm/internal/modules/fschain/netmap_candidates.go +++ b/cmd/neofs-adm/internal/modules/fschain/netmap_candidates.go @@ -4,9 +4,11 @@ import ( "fmt" "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" + netmaprpc "github.com/nspcc-dev/neofs-contract/rpc/netmap" "github.com/nspcc-dev/neofs-contract/rpc/nns" "github.com/nspcc-dev/neofs-node/cmd/internal/cmdprinter" "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap" + netmapSDK "github.com/nspcc-dev/neofs-sdk-go/netmap" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -29,20 +31,38 @@ func listNetmapCandidatesNodes(cmd *cobra.Command, _ []string) error { return fmt.Errorf("can't get netmap contract hash: %w", err) } - res, err := inv.Call(nmHash, "netmapCandidates") - if err != nil { - return fmt.Errorf("can't fetch list of network config keys from the netmap contract: %w", err) - } - if res.State != "HALT" { - return fmt.Errorf("netmap contract returned unexpected exception: %s", res.FaultException) - } + useV2, _ := cmd.Flags().GetBool(nodeV2Flag) - nm, err := netmap.DecodeNetMap(res.Stack) + var nodes []netmapSDK.NodeInfo + if !useV2 { + res, err := inv.Call(nmHash, "netmapCandidates") + if err != nil { + return fmt.Errorf("can't fetch list of network config keys from the netmap contract: %w", err) + } + if res.State != "HALT" { + return fmt.Errorf("netmap contract returned unexpected exception: %s", res.FaultException) + } - if err != nil { - return fmt.Errorf("unable to decode netmap: %w", err) + nm, err := netmap.DecodeNetMap(res.Stack) + + if err != nil { + return fmt.Errorf("unable to decode netmap: %w", err) + } + nodes = nm.Nodes() + } else { + var ( + reader = netmaprpc.NewReader(inv, nmHash) + sess, iter, err = reader.ListCandidates() + ) + if err != nil { + return fmt.Errorf("can't list candidates: %w", err) + } + // Conversion can be avoided in future. + nodes, err = netmap.CollectNodes(inv, sess, &iter) + if err != nil { + return fmt.Errorf("can't collect nodes: %w", err) + } } - nodes := nm.Nodes() for i := range nodes { cmdprinter.PrettyPrintNodeInfo(cmd, nodes[i], i, "", false) } diff --git a/cmd/neofs-adm/internal/modules/fschain/root.go b/cmd/neofs-adm/internal/modules/fschain/root.go index 132e157b89..9ec1e1bc76 100644 --- a/cmd/neofs-adm/internal/modules/fschain/root.go +++ b/cmd/neofs-adm/internal/modules/fschain/root.go @@ -58,6 +58,7 @@ const ( estimationsContainerFlag = "cid" mintNeofsAmountFlag = "amount" mintTxHashFlag = "deposit-tx" + nodeV2Flag = "nodev2" ) var ( @@ -326,7 +327,6 @@ Values for unknown keys are added exactly the way they're provided, no conversio netmapCandidatesCmd = &cobra.Command{ Use: "netmap-candidates", Short: "List netmap candidates nodes", - Args: cobra.NoArgs, PreRun: func(cmd *cobra.Command, _ []string) { _ = viper.BindPFlag(endpointFlag, cmd.Flags().Lookup(endpointFlag)) }, @@ -481,6 +481,7 @@ func init() { RootCmd.AddCommand(netmapCandidatesCmd) netmapCandidatesCmd.Flags().StringP(endpointFlag, "r", "", "N3 RPC node endpoint") + netmapCandidatesCmd.Flags().BoolP(nodeV2Flag, "2", false, "Use node v2 data") RootCmd.AddCommand(estimationsCmd) ff := estimationsCmd.Flags() diff --git a/pkg/morph/client/netmap/netmap.go b/pkg/morph/client/netmap/netmap.go index 74dfe804aa..3d38401b89 100644 --- a/pkg/morph/client/netmap/netmap.go +++ b/pkg/morph/client/netmap/netmap.go @@ -81,7 +81,7 @@ func (c *Client) GetCandidates() ([]netmap.NodeInfo, error) { if err != nil { return nil, err } - return collectNodes(inv, sess, &iter) + return CollectNodes(inv, sess, &iter) } // NetMap calls "netmap" method (or listNodes for v2 nodes) and decodes @@ -111,7 +111,7 @@ func (c *Client) NetMap() (*netmap.NetMap, error) { } func collectNetmap(inv *invoker.Invoker, sess uuid.UUID, iter *result.Iterator) (*netmap.NetMap, error) { - nodes, err := collectNodes(inv, sess, iter) + nodes, err := CollectNodes(inv, sess, iter) if err != nil { return nil, err } @@ -122,7 +122,8 @@ func collectNetmap(inv *invoker.Invoker, sess uuid.UUID, iter *result.Iterator) return nm, nil } -func collectNodes(inv *invoker.Invoker, sess uuid.UUID, iter *result.Iterator) ([]netmap.NodeInfo, error) { +// CollectNodes gathers all node data from the provided iterator and closes it. +func CollectNodes(inv *invoker.Invoker, sess uuid.UUID, iter *result.Iterator) ([]netmap.NodeInfo, error) { var nodes []netmap.NodeInfo defer func() {