From a2b622d62ecc85e405ed1942b70c3f56b69ede42 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Fri, 25 Jun 2021 18:58:05 +0300 Subject: [PATCH] [#647] pkg/morph/netmap/wrapper: `GetCandidates` method Signed-off-by: Pavel Karpy --- pkg/morph/client/netmap/wrapper/netmap.go | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/pkg/morph/client/netmap/wrapper/netmap.go b/pkg/morph/client/netmap/wrapper/netmap.go index a8e99ab1..4f3b51ec 100644 --- a/pkg/morph/client/netmap/wrapper/netmap.go +++ b/pkg/morph/client/netmap/wrapper/netmap.go @@ -38,6 +38,20 @@ func (w Wrapper) GetNetMapByEpoch(epoch uint64) (*netmap.Netmap, error) { return unmarshalNetmap(vals.Peers()) } +// GetCandidates receives information list about candidates +// for the next epoch network map through the Netmap contract +// call, composes network map from them and returns it. +func (w Wrapper) GetCandidates() (*netmap.Netmap, error) { + args := client.GetNetMapCandidatesArgs{} + + vals, err := w.client.Candidates(args) + if err != nil { + return nil, err + } + + return unmarshalCandidates(vals.NetmapNodes()) +} + func unmarshalNetmap(rawPeers [][]byte) (*netmap.Netmap, error) { infos := make([]netmap.NodeInfo, 0, len(rawPeers)) @@ -54,3 +68,29 @@ func unmarshalNetmap(rawPeers [][]byte) (*netmap.Netmap, error) { return netmap.NewNetmap(nodes) } + +func unmarshalCandidates(rawCandidate []*client.PeerWithState) (*netmap.Netmap, error) { + candidates := make([]netmap.NodeInfo, 0, len(rawCandidate)) + + for _, candidate := range rawCandidate { + nodeInfo := netmap.NewNodeInfo() + if err := nodeInfo.Unmarshal(candidate.Peer()); err != nil { + return nil, fmt.Errorf("can't unmarshal peer info: %w", err) + } + + switch candidate.State() { + case client.Online: + nodeInfo.SetState(netmap.NodeStateOnline) + case client.Offline: + nodeInfo.SetState(netmap.NodeStateOffline) + default: + nodeInfo.SetState(0) + } + + candidates = append(candidates, *nodeInfo) + } + + nodes := netmap.NodesFromInfo(candidates) + + return netmap.NewNetmap(nodes) +}