Skip to content

Commit

Permalink
adm: provide a way to look into v2 candidates
Browse files Browse the repository at this point in the history
Signed-off-by: Roman Khimov <[email protected]>
  • Loading branch information
roman-khimov committed Jan 22, 2025
1 parent 729718a commit 7204f22
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
42 changes: 31 additions & 11 deletions cmd/neofs-adm/internal/modules/fschain/netmap_candidates.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/neofs-adm/internal/modules/fschain/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const (
estimationsContainerFlag = "cid"
mintNeofsAmountFlag = "amount"
mintTxHashFlag = "deposit-tx"
nodeV2Flag = "nodev2"
)

var (
Expand Down Expand Up @@ -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))
},
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 4 additions & 3 deletions pkg/morph/client/netmap/netmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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() {
Expand Down

0 comments on commit 7204f22

Please sign in to comment.