From 9073e198b92dd727a166139086d0503fb634f394 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Fri, 19 Feb 2021 11:01:37 +0300 Subject: [PATCH] [#398] cmd/node: Serve NetmapService.NetworkInfo RPC Implement `NetworkInfo` calls on full stack of Netmap services. Current epoch is read from node local state, magic number is read via `MagicNumber` call of morph client. Signed-off-by: Leonard Lyubich --- cmd/neofs-node/netmap.go | 42 +++++++++++++++---- go.mod | 2 +- go.sum | Bin 61254 -> 61605 bytes pkg/network/transport/netmap/grpc/service.go | 11 +++++ pkg/services/netmap/executor.go | 32 +++++++++++++- pkg/services/netmap/response.go | 13 ++++++ pkg/services/netmap/sign.go | 13 ++++++ 7 files changed, 102 insertions(+), 11 deletions(-) diff --git a/cmd/neofs-node/netmap.go b/cmd/neofs-node/netmap.go index 4fe56f892..4caf940fd 100644 --- a/cmd/neofs-node/netmap.go +++ b/cmd/neofs-node/netmap.go @@ -3,9 +3,11 @@ package main import ( "bytes" - "github.com/nspcc-dev/neofs-api-go/pkg/netmap" + netmapSDK "github.com/nspcc-dev/neofs-api-go/pkg/netmap" + netmapV2 "github.com/nspcc-dev/neofs-api-go/v2/netmap" netmapGRPC "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc" crypto "github.com/nspcc-dev/neofs-crypto" + "github.com/nspcc-dev/neofs-node/pkg/core/netmap" "github.com/nspcc-dev/neofs-node/pkg/morph/event" netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap" netmapTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/netmap/grpc" @@ -36,14 +38,18 @@ func (s *networkState) setCurrentEpoch(v uint64) { } func initNetmapService(c *cfg) { - peerInfo := new(netmap.NodeInfo) + peerInfo := new(netmapSDK.NodeInfo) peerInfo.SetAddress(c.localAddr.String()) peerInfo.SetPublicKey(crypto.MarshalPublicKey(&c.key.PublicKey)) peerInfo.SetAttributes(c.cfgNodeInfo.attributes...) - peerInfo.SetState(netmap.NodeStateOffline) + peerInfo.SetState(netmapSDK.NodeStateOffline) c.handleLocalNodeInfo(peerInfo) + if c.cfgMorph.client == nil { + initMorphComponents(c) + } + netmapGRPC.RegisterNetmapServiceServer(c.cfgGRPC.server, netmapTransportGRPC.New( netmapService.NewSignService( @@ -52,6 +58,10 @@ func initNetmapService(c *cfg) { netmapService.NewExecutionService( c, c.apiVersion, + &netInfo{ + netState: c.cfgNetmap.state, + magic: c.cfgMorph.client, + }, ), c.respSvc, ), @@ -137,7 +147,7 @@ func initState(c *cfg) { c.cfgNetmap.state.setCurrentEpoch(epoch) } -func (c *cfg) netmapLocalNodeState(epoch uint64) (*netmap.NodeInfo, error) { +func (c *cfg) netmapLocalNodeState(epoch uint64) (*netmapSDK.NodeInfo, error) { // calculate current network state nm, err := c.cfgNetmap.wrapper.GetNetMapByEpoch(epoch) if err != nil { @@ -147,7 +157,7 @@ func (c *cfg) netmapLocalNodeState(epoch uint64) (*netmap.NodeInfo, error) { return c.localNodeInfoFromNetmap(nm), nil } -func (c *cfg) localNodeInfoFromNetmap(nm *netmap.Netmap) *netmap.NodeInfo { +func (c *cfg) localNodeInfoFromNetmap(nm *netmapSDK.Netmap) *netmapSDK.NodeInfo { for _, n := range nm.Nodes { if bytes.Equal(n.PublicKey(), crypto.MarshalPublicKey(&c.key.PublicKey)) { return n.NodeInfo @@ -164,7 +174,7 @@ func addNewEpochNotificationHandler(c *cfg, h event.Handler) { func goOffline(c *cfg) { err := c.cfgNetmap.wrapper.UpdatePeerState( crypto.MarshalPublicKey(&c.key.PublicKey), - netmap.NodeStateOffline, + netmapSDK.NodeStateOffline, ) if err != nil { @@ -181,10 +191,10 @@ func (c *cfg) SetNetmapStatus(st control.NetmapStatus) error { return c.cfgNetmap.wrapper.AddPeer(c.toOnlineLocalNodeInfo()) } - var apiState netmap.NodeState + var apiState netmapSDK.NodeState if st == control.NetmapStatus_OFFLINE { - apiState = netmap.NodeStateOffline + apiState = netmapSDK.NodeStateOffline } return c.cfgNetmap.wrapper.UpdatePeerState( @@ -192,3 +202,19 @@ func (c *cfg) SetNetmapStatus(st control.NetmapStatus) error { apiState, ) } + +type netInfo struct { + netState netmap.State + + magic interface { + MagicNumber() uint64 + } +} + +func (n *netInfo) Dump() (*netmapV2.NetworkInfo, error) { + ni := new(netmapV2.NetworkInfo) + ni.SetCurrentEpoch(n.netState.CurrentEpoch()) + ni.SetMagicNumber(n.magic.MagicNumber()) + + return ni, nil +} diff --git a/go.mod b/go.mod index be854daa1..e2cdc6b47 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/multiformats/go-multihash v0.0.13 // indirect github.com/nspcc-dev/hrw v1.0.9 github.com/nspcc-dev/neo-go v0.93.0 - github.com/nspcc-dev/neofs-api-go v1.23.0 + github.com/nspcc-dev/neofs-api-go v1.23.1-0.20210219132553-e2b0887be93d github.com/nspcc-dev/neofs-crypto v0.3.0 github.com/nspcc-dev/tzhash v1.4.0 github.com/panjf2000/ants/v2 v2.3.0 diff --git a/go.sum b/go.sum index 86b7f7c3a8b853db4d176171d69f3c71bde4f942..c232986fe81a343c65dc1f708d8f98c021b70634 100644 GIT binary patch delta 292 zcmX?hk9p}s<_#}7^c6A;tD ztLh($ diff --git a/pkg/network/transport/netmap/grpc/service.go b/pkg/network/transport/netmap/grpc/service.go index a54dc9259..53cdb7e7a 100644 --- a/pkg/network/transport/netmap/grpc/service.go +++ b/pkg/network/transport/netmap/grpc/service.go @@ -32,3 +32,14 @@ func (s *Server) LocalNodeInfo( return netmap.LocalNodeInfoResponseToGRPCMessage(resp), nil } + +// NetworkInfo converts gRPC request message and passes it to internal netmap service. +func (s *Server) NetworkInfo(ctx context.Context, req *netmapGRPC.NetworkInfoRequest) (*netmapGRPC.NetworkInfoResponse, error) { + resp, err := s.srv.NetworkInfo(ctx, netmap.NetworkInfoRequestFromGRPCMessage(req)) + if err != nil { + // TODO: think about how we transport errors through gRPC + return nil, err + } + + return netmap.NetworkInfoResponseToGRPCMessage(resp), nil +} diff --git a/pkg/services/netmap/executor.go b/pkg/services/netmap/executor.go index e80dfce7e..4600681fc 100644 --- a/pkg/services/netmap/executor.go +++ b/pkg/services/netmap/executor.go @@ -10,6 +10,8 @@ import ( type executorSvc struct { version *pkg.Version state NodeState + + netInfo NetworkInfo } // NodeState encapsulates information @@ -20,8 +22,16 @@ type NodeState interface { LocalNodeInfo() (*netmap.NodeInfo, error) } -func NewExecutionService(s NodeState, v *pkg.Version) netmap.Service { - if s == nil || v == nil { +// NetworkInfo encapsulates source of the +// recent information about the NeoFS network. +type NetworkInfo interface { + // Must return recent network information. + // in NeoFS API v2 NetworkInfo structure. + Dump() (*netmap.NetworkInfo, error) +} + +func NewExecutionService(s NodeState, v *pkg.Version, netInfo NetworkInfo) netmap.Service { + if s == nil || v == nil || netInfo == nil { // this should never happen, otherwise it programmers bug panic("can't create netmap execution service") } @@ -29,6 +39,7 @@ func NewExecutionService(s NodeState, v *pkg.Version) netmap.Service { return &executorSvc{ version: v, state: s, + netInfo: netInfo, } } @@ -49,3 +60,20 @@ func (s *executorSvc) LocalNodeInfo( return resp, nil } + +func (s *executorSvc) NetworkInfo( + _ context.Context, + _ *netmap.NetworkInfoRequest) (*netmap.NetworkInfoResponse, error) { + ni, err := s.netInfo.Dump() + if err != nil { + return nil, err + } + + body := new(netmap.NetworkInfoResponseBody) + body.SetNetworkInfo(ni) + + resp := new(netmap.NetworkInfoResponse) + resp.SetBody(body) + + return resp, nil +} diff --git a/pkg/services/netmap/response.go b/pkg/services/netmap/response.go index ed224d00d..9fc0e9ef1 100644 --- a/pkg/services/netmap/response.go +++ b/pkg/services/netmap/response.go @@ -35,3 +35,16 @@ func (s *responseService) LocalNodeInfo(ctx context.Context, req *netmap.LocalNo return resp.(*netmap.LocalNodeInfoResponse), nil } + +func (s *responseService) NetworkInfo(ctx context.Context, req *netmap.NetworkInfoRequest) (*netmap.NetworkInfoResponse, error) { + resp, err := s.respSvc.HandleUnaryRequest(ctx, req, + func(ctx context.Context, req interface{}) (util.ResponseMessage, error) { + return s.svc.NetworkInfo(ctx, req.(*netmap.NetworkInfoRequest)) + }, + ) + if err != nil { + return nil, err + } + + return resp.(*netmap.NetworkInfoResponse), nil +} diff --git a/pkg/services/netmap/sign.go b/pkg/services/netmap/sign.go index 6c3f77f77..4c7a60212 100644 --- a/pkg/services/netmap/sign.go +++ b/pkg/services/netmap/sign.go @@ -35,3 +35,16 @@ func (s *signService) LocalNodeInfo( return resp.(*netmap.LocalNodeInfoResponse), nil } + +func (s *signService) NetworkInfo(ctx context.Context, req *netmap.NetworkInfoRequest) (*netmap.NetworkInfoResponse, error) { + resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, + func(ctx context.Context, req interface{}) (util.ResponseMessage, error) { + return s.svc.NetworkInfo(ctx, req.(*netmap.NetworkInfoRequest)) + }, + ) + if err != nil { + return nil, err + } + + return resp.(*netmap.NetworkInfoResponse), nil +}