From 283ccc04b426d55092dcbfcd3a6b214408f2248e Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 20 Sep 2021 19:13:17 +0300 Subject: [PATCH] [#833] services/netmap: Support new fields of netmap.NetworkInfo Make the implementation of network info source (Netmap V2 service dependency) to read MillisecondsPerBlock sidechain parameter and NeoFS network parameters depending on the client version. Signed-off-by: Leonard Lyubich --- cmd/neofs-node/netmap.go | 44 ++++++++++++++++++++++++++++++--- pkg/services/netmap/executor.go | 12 +++++---- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/cmd/neofs-node/netmap.go b/cmd/neofs-node/netmap.go index 73ad33a73..628162fe3 100644 --- a/cmd/neofs-node/netmap.go +++ b/cmd/neofs-node/netmap.go @@ -3,10 +3,12 @@ package main import ( "bytes" "errors" + "fmt" 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" + "github.com/nspcc-dev/neofs-api-go/v2/refs" "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" @@ -96,8 +98,10 @@ func initNetmapService(c *cfg) { c, c.apiVersion, &netInfo{ - netState: c.cfgNetmap.state, - magic: c.cfgMorph.client, + netState: c.cfgNetmap.state, + magic: c.cfgMorph.client, + netCfg: c.cfgNetmap.wrapper.IterateConfigParameters, + msPerBlockRdr: c.cfgMorph.client.MsPerBlock, }, ), c.respSvc, @@ -274,9 +278,13 @@ type netInfo struct { magic interface { MagicNumber() (uint64, error) } + + netCfg func(func(key, value []byte) error) error + + msPerBlockRdr func() (int64, error) } -func (n *netInfo) Dump() (*netmapV2.NetworkInfo, error) { +func (n *netInfo) Dump(ver *refs.Version) (*netmapV2.NetworkInfo, error) { magic, err := n.magic.MagicNumber() if err != nil { return nil, err @@ -286,5 +294,35 @@ func (n *netInfo) Dump() (*netmapV2.NetworkInfo, error) { ni.SetCurrentEpoch(n.netState.CurrentEpoch()) ni.SetMagicNumber(magic) + if mjr := ver.GetMajor(); mjr > 2 || mjr == 2 && ver.GetMinor() > 9 { + msPerBlock, err := n.msPerBlockRdr() + if err != nil { + return nil, fmt.Errorf("ms per block: %w", err) + } + + var ( + ps []*netmapV2.NetworkParameter + netCfg netmapV2.NetworkConfig + ) + + if err := n.netCfg(func(key, value []byte) error { + var p netmapV2.NetworkParameter + + p.SetKey(key) + p.SetValue(value) + + ps = append(ps, &p) + + return nil + }); err != nil { + return nil, fmt.Errorf("network config: %w", err) + } + + netCfg.SetParameters(ps...) + + ni.SetNetworkConfig(&netCfg) + ni.SetMsPerBlock(msPerBlock) + } + return ni, nil } diff --git a/pkg/services/netmap/executor.go b/pkg/services/netmap/executor.go index 5d748aa40..9a33dfe74 100644 --- a/pkg/services/netmap/executor.go +++ b/pkg/services/netmap/executor.go @@ -5,6 +5,7 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg" "github.com/nspcc-dev/neofs-api-go/v2/netmap" + "github.com/nspcc-dev/neofs-api-go/v2/refs" ) type executorSvc struct { @@ -25,9 +26,10 @@ type NodeState interface { // 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) + // Must return recent network information in NeoFS API v2 NetworkInfo structure. + // + // If protocol version is <=2.9, MillisecondsPerBlock and network config should be unset. + Dump(*refs.Version) (*netmap.NetworkInfo, error) } func NewExecutionService(s NodeState, v *pkg.Version, netInfo NetworkInfo) Server { @@ -78,8 +80,8 @@ func (s *executorSvc) LocalNodeInfo( func (s *executorSvc) NetworkInfo( _ context.Context, - _ *netmap.NetworkInfoRequest) (*netmap.NetworkInfoResponse, error) { - ni, err := s.netInfo.Dump() + req *netmap.NetworkInfoRequest) (*netmap.NetworkInfoResponse, error) { + ni, err := s.netInfo.Dump(req.GetMetaHeader().GetVersion()) if err != nil { return nil, err }