2020-07-24 13:54:03 +00:00
|
|
|
package wrapper
|
|
|
|
|
2020-09-22 08:38:23 +00:00
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-09-22 08:38:23 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
|
|
|
client "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
|
|
|
|
)
|
2020-07-24 13:54:03 +00:00
|
|
|
|
|
|
|
// GetNetMap receives information list about storage nodes
|
|
|
|
// through the Netmap contract call, composes network map
|
2020-09-22 08:38:23 +00:00
|
|
|
// from them and returns it. With diff == 0 returns current
|
|
|
|
// network map, else return snapshot of previous network map.
|
|
|
|
func (w Wrapper) GetNetMap(diff uint64) (*netmap.Netmap, error) {
|
|
|
|
args := client.GetSnapshotArgs{}
|
|
|
|
args.SetDiff(diff)
|
|
|
|
|
2021-01-12 11:26:58 +00:00
|
|
|
vals, err := w.client.Snapshot(args)
|
2020-09-22 08:38:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:26:58 +00:00
|
|
|
return unmarshalNetmap(vals.Peers())
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNetMapByEpoch receives information list about storage nodes
|
|
|
|
// through the Netmap contract call, composes network map
|
|
|
|
// from them and returns it. Returns snapshot of the specified epoch number.
|
|
|
|
func (w Wrapper) GetNetMapByEpoch(epoch uint64) (*netmap.Netmap, error) {
|
|
|
|
args := client.EpochSnapshotArgs{}
|
|
|
|
args.SetEpoch(epoch)
|
|
|
|
|
|
|
|
vals, err := w.client.EpochSnapshot(args)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return unmarshalNetmap(vals.Peers())
|
|
|
|
}
|
|
|
|
|
2021-06-25 15:58:05 +00:00
|
|
|
// 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())
|
|
|
|
}
|
|
|
|
|
2021-01-12 11:26:58 +00:00
|
|
|
func unmarshalNetmap(rawPeers [][]byte) (*netmap.Netmap, error) {
|
2020-11-05 15:51:43 +00:00
|
|
|
infos := make([]netmap.NodeInfo, 0, len(rawPeers))
|
2020-09-22 08:38:23 +00:00
|
|
|
|
|
|
|
for _, peer := range rawPeers {
|
2020-11-16 10:26:35 +00:00
|
|
|
nodeInfo := netmap.NewNodeInfo()
|
|
|
|
if err := nodeInfo.Unmarshal(peer); err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("can't unmarshal peer info: %w", err)
|
2020-09-22 08:38:23 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
infos = append(infos, *nodeInfo)
|
2020-09-22 08:38:23 +00:00
|
|
|
}
|
|
|
|
|
2020-11-05 15:51:43 +00:00
|
|
|
nodes := netmap.NodesFromInfo(infos)
|
2020-09-22 08:38:23 +00:00
|
|
|
|
|
|
|
return netmap.NewNetmap(nodes)
|
2020-07-24 13:54:03 +00:00
|
|
|
}
|
2021-06-25 15:58:05 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|