2021-01-14 16:06:49 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/control"
|
2021-11-10 07:08:33 +00:00
|
|
|
netmapAPI "github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2021-01-14 16:32:23 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2021-01-14 16:06:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NetmapSnapshot reads network map snapshot from Netmap storage.
|
|
|
|
func (s *Server) NetmapSnapshot(ctx context.Context, req *control.NetmapSnapshotRequest) (*control.NetmapSnapshotResponse, error) {
|
2021-01-14 16:32:23 +00:00
|
|
|
// verify request
|
|
|
|
if err := s.isValidRequest(req); err != nil {
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// get current epoch
|
|
|
|
epoch, err := s.netMapSrc.Epoch()
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.NotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
apiNetMap, err := s.netMapSrc.GetNetMapByEpoch(epoch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.NotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
nm := new(control.Netmap)
|
|
|
|
nm.SetEpoch(epoch)
|
2022-06-08 23:18:26 +00:00
|
|
|
nm.SetNodes(nodesFromAPI(apiNetMap.Nodes()))
|
2021-01-14 16:32:23 +00:00
|
|
|
|
|
|
|
// create and fill response
|
|
|
|
resp := new(control.NetmapSnapshotResponse)
|
|
|
|
|
|
|
|
body := new(control.NetmapSnapshotResponse_Body)
|
|
|
|
resp.SetBody(body)
|
|
|
|
|
|
|
|
body.SetNetmap(nm)
|
|
|
|
|
|
|
|
// sign the response
|
|
|
|
if err := SignMessage(s.key, resp); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func nodesFromAPI(apiNodes []netmapAPI.NodeInfo) []*control.NodeInfo {
|
2021-01-14 16:32:23 +00:00
|
|
|
nodes := make([]*control.NodeInfo, 0, len(apiNodes))
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
for i := range apiNodes {
|
2021-01-14 16:32:23 +00:00
|
|
|
node := new(control.NodeInfo)
|
2022-06-08 23:18:26 +00:00
|
|
|
node.SetPublicKey(apiNodes[i].PublicKey())
|
2021-06-22 19:48:33 +00:00
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
addrs := make([]string, 0, apiNodes[i].NumberOfNetworkEndpoints())
|
|
|
|
netmapAPI.IterateNetworkEndpoints(apiNodes[i], func(s string) {
|
2021-06-22 19:48:33 +00:00
|
|
|
addrs = append(addrs, s)
|
|
|
|
})
|
|
|
|
node.SetAddresses(addrs)
|
2022-06-08 23:18:26 +00:00
|
|
|
node.SetAttributes(attributesFromAPI(apiNodes[i]))
|
|
|
|
|
|
|
|
switch {
|
|
|
|
default:
|
|
|
|
node.SetState(control.NetmapStatus_STATUS_UNDEFINED)
|
|
|
|
case apiNodes[i].IsOnline():
|
|
|
|
node.SetState(control.NetmapStatus_ONLINE)
|
|
|
|
case apiNodes[i].IsOffline():
|
|
|
|
node.SetState(control.NetmapStatus_OFFLINE)
|
|
|
|
}
|
2021-01-14 16:32:23 +00:00
|
|
|
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func attributesFromAPI(apiNode netmapAPI.NodeInfo) []*control.NodeInfo_Attribute {
|
|
|
|
attrs := make([]*control.NodeInfo_Attribute, 0, apiNode.NumberOfAttributes())
|
2021-01-14 16:32:23 +00:00
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
apiNode.IterateAttributes(func(key, value string) {
|
2021-01-14 16:32:23 +00:00
|
|
|
a := new(control.NodeInfo_Attribute)
|
2022-06-08 23:18:26 +00:00
|
|
|
a.SetKey(key)
|
|
|
|
a.SetValue(value)
|
2021-01-15 13:06:02 +00:00
|
|
|
|
2021-01-14 16:32:23 +00:00
|
|
|
attrs = append(attrs, a)
|
2022-06-08 23:18:26 +00:00
|
|
|
})
|
2021-01-14 16:32:23 +00:00
|
|
|
|
|
|
|
return attrs
|
2021-01-14 16:06:49 +00:00
|
|
|
}
|