[#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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-09-20 19:13:17 +03:00 committed by Alex Vanin
parent 7582689de4
commit 283ccc04b4
2 changed files with 48 additions and 8 deletions

View file

@ -3,10 +3,12 @@ package main
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt"
netmapSDK "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" netmapV2 "github.com/nspcc-dev/neofs-api-go/v2/netmap"
netmapGRPC "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc" 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/core/netmap"
"github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/nspcc-dev/neofs-node/pkg/morph/event"
netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap" netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
@ -96,8 +98,10 @@ func initNetmapService(c *cfg) {
c, c,
c.apiVersion, c.apiVersion,
&netInfo{ &netInfo{
netState: c.cfgNetmap.state, netState: c.cfgNetmap.state,
magic: c.cfgMorph.client, magic: c.cfgMorph.client,
netCfg: c.cfgNetmap.wrapper.IterateConfigParameters,
msPerBlockRdr: c.cfgMorph.client.MsPerBlock,
}, },
), ),
c.respSvc, c.respSvc,
@ -274,9 +278,13 @@ type netInfo struct {
magic interface { magic interface {
MagicNumber() (uint64, error) 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() magic, err := n.magic.MagicNumber()
if err != nil { if err != nil {
return nil, err return nil, err
@ -286,5 +294,35 @@ func (n *netInfo) Dump() (*netmapV2.NetworkInfo, error) {
ni.SetCurrentEpoch(n.netState.CurrentEpoch()) ni.SetCurrentEpoch(n.netState.CurrentEpoch())
ni.SetMagicNumber(magic) 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 return ni, nil
} }

View file

@ -5,6 +5,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg" "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/netmap"
"github.com/nspcc-dev/neofs-api-go/v2/refs"
) )
type executorSvc struct { type executorSvc struct {
@ -25,9 +26,10 @@ type NodeState interface {
// NetworkInfo encapsulates source of the // NetworkInfo encapsulates source of the
// recent information about the NeoFS network. // recent information about the NeoFS network.
type NetworkInfo interface { type NetworkInfo interface {
// Must return recent network information. // Must return recent network information in NeoFS API v2 NetworkInfo structure.
// in NeoFS API v2 NetworkInfo structure. //
Dump() (*netmap.NetworkInfo, error) // 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 { func NewExecutionService(s NodeState, v *pkg.Version, netInfo NetworkInfo) Server {
@ -78,8 +80,8 @@ func (s *executorSvc) LocalNodeInfo(
func (s *executorSvc) NetworkInfo( func (s *executorSvc) NetworkInfo(
_ context.Context, _ context.Context,
_ *netmap.NetworkInfoRequest) (*netmap.NetworkInfoResponse, error) { req *netmap.NetworkInfoRequest) (*netmap.NetworkInfoResponse, error) {
ni, err := s.netInfo.Dump() ni, err := s.netInfo.Dump(req.GetMetaHeader().GetVersion())
if err != nil { if err != nil {
return nil, err return nil, err
} }