[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-02-19 11:01:37 +03:00 committed by Alex Vanin
parent 4c8d29ce46
commit 9073e198b9
7 changed files with 107 additions and 13 deletions

View file

@ -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
}

2
go.mod
View file

@ -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

7
go.sum
View file

@ -21,6 +21,7 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/CityOfZion/neo-go v0.62.1-pre.0.20191114145240-e740fbe708f8/go.mod h1:MJCkWUBhi9pn/CrYO1Q3P687y2KeahrOPS9BD9LDGb0=
github.com/CityOfZion/neo-go v0.70.1-pre.0.20191209120015-fccb0085941e/go.mod h1:0enZl0az8xA6PVkwzEOwPWVJGqlt/GO4hA4kmQ5Xzig=
github.com/CityOfZion/neo-go v0.70.1-pre.0.20191212173117-32ac01130d4c/go.mod h1:JtlHfeqLywZLswKIKFnAp+yzezY4Dji9qlfQKB2OD/I=
github.com/CityOfZion/neo-go v0.71.1-pre.0.20200129171427-f773ec69fb84 h1:gcTXk9aO+PhHudJNPFJ9H4RmKjdzz40Tvv2NE1BwRZ0=
github.com/CityOfZion/neo-go v0.71.1-pre.0.20200129171427-f773ec69fb84/go.mod h1:FLI526IrRWHmcsO+mHsCbj64pJZhwQFTLJZu+A4PGOA=
github.com/DataDog/zstd v1.4.1 h1:3oxKN3wbHibqx897utPC2LTQU4J+IHWWJO+glkAkpFM=
github.com/DataDog/zstd v1.4.1/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
@ -108,6 +109,7 @@ github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymF
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/etcd-io/bbolt v1.3.3 h1:gSJmxrs37LgTqR/oyJBWok6k6SvXEUerFTbltIhXkBM=
github.com/etcd-io/bbolt v1.3.3/go.mod h1:ZF2nL25h33cCyBtcyWeZ2/I3HQOfTP+0PIEvHjkjCrw=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/flynn-archive/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:rZfgFAXFS/z/lEd6LJmf9HVZ1LkgYiHx5pHhV5DR16M=
@ -125,6 +127,7 @@ github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V
github.com/go-redis/redis v6.10.2+incompatible h1:SLbqrO/Ik1nhXA5/cbEs1P5MUBo1Qq4ihlNfGnnipPw=
github.com/go-redis/redis v6.10.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o=
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1 h1:/s5zKNz0uPFCZ5hddgPdo2TK2TVrUNMn0OOX8/aZMTE=
@ -291,8 +294,8 @@ github.com/nspcc-dev/neo-go v0.73.1-pre.0.20200303142215-f5a1b928ce09/go.mod h1:
github.com/nspcc-dev/neo-go v0.91.0/go.mod h1:G6HdOWvzQ6tlvFdvFSN/PgCzLPN/X/X4d5hTjFRUDcc=
github.com/nspcc-dev/neo-go v0.93.0 h1:B20D8z+/dByegXZoKpzLMkztsOtMozBGUnDjfVaI+zE=
github.com/nspcc-dev/neo-go v0.93.0/go.mod h1:6tixfAd+d8TIm05DA874j6t898G/fyqA2fHVJxkJCXQ=
github.com/nspcc-dev/neofs-api-go v1.23.0 h1:t4FB5uVY99UkYR0Hiyi1SHjZuqzf4qicw7tf7BBnkHk=
github.com/nspcc-dev/neofs-api-go v1.23.0/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8=
github.com/nspcc-dev/neofs-api-go v1.23.1-0.20210219132553-e2b0887be93d h1:Jq2IQ78H8dM+MyseJ+zbPc8ideoKuUsrLCc1r+o22qU=
github.com/nspcc-dev/neofs-api-go v1.23.1-0.20210219132553-e2b0887be93d/go.mod h1:G7dqincfdjBrAbL5nxVp82emF05fSVEqe59ICsoRDI8=
github.com/nspcc-dev/neofs-crypto v0.2.0/go.mod h1:F/96fUzPM3wR+UGsPi3faVNmFlA9KAEAUQR7dMxZmNA=
github.com/nspcc-dev/neofs-crypto v0.2.3/go.mod h1:8w16GEJbH6791ktVqHN9YRNH3s9BEEKYxGhlFnp0cDw=
github.com/nspcc-dev/neofs-crypto v0.3.0 h1:zlr3pgoxuzrmGCxc5W8dGVfA9Rro8diFvVnBg0L4ifM=

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}