2020-10-08 13:14:19 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg"
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type executorSvc struct {
|
2021-01-25 08:02:20 +00:00
|
|
|
version *pkg.Version
|
|
|
|
state NodeState
|
2021-02-19 08:01:37 +00:00
|
|
|
|
|
|
|
netInfo NetworkInfo
|
2020-10-08 13:14:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 08:02:20 +00:00
|
|
|
// NodeState encapsulates information
|
|
|
|
// about current node state.
|
|
|
|
type NodeState interface {
|
|
|
|
// Must return current node state
|
|
|
|
// in NeoFS API v2 NodeInfo structure.
|
|
|
|
LocalNodeInfo() (*netmap.NodeInfo, error)
|
|
|
|
}
|
|
|
|
|
2021-02-19 08:01:37 +00:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2021-03-15 10:53:08 +00:00
|
|
|
func NewExecutionService(s NodeState, v *pkg.Version, netInfo NetworkInfo) Server {
|
2021-02-19 08:01:37 +00:00
|
|
|
if s == nil || v == nil || netInfo == nil {
|
2020-10-08 13:14:19 +00:00
|
|
|
// this should never happen, otherwise it programmers bug
|
|
|
|
panic("can't create netmap execution service")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &executorSvc{
|
2021-01-25 08:02:20 +00:00
|
|
|
version: v,
|
|
|
|
state: s,
|
2021-02-19 08:01:37 +00:00
|
|
|
netInfo: netInfo,
|
2020-10-08 13:14:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *executorSvc) LocalNodeInfo(
|
|
|
|
_ context.Context,
|
2021-06-25 15:38:58 +00:00
|
|
|
req *netmap.LocalNodeInfoRequest) (*netmap.LocalNodeInfoResponse, error) {
|
|
|
|
ver := pkg.NewVersionFromV2(req.GetMetaHeader().GetVersion())
|
|
|
|
if err := pkg.IsSupportedVersion(ver); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-25 08:02:20 +00:00
|
|
|
ni, err := s.state.LocalNodeInfo()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-25 15:38:58 +00:00
|
|
|
if addrNum := ni.NumberOfAddresses(); addrNum > 0 && ver.Minor() <= 7 {
|
|
|
|
ni2 := new(netmap.NodeInfo)
|
|
|
|
ni2.SetPublicKey(ni.GetPublicKey())
|
|
|
|
ni2.SetState(ni.GetState())
|
|
|
|
ni2.SetAttributes(ni.GetAttributes())
|
|
|
|
ni.IterateAddresses(func(s string) bool {
|
|
|
|
ni2.SetAddresses(s)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
ni = ni2
|
|
|
|
}
|
|
|
|
|
2020-10-08 13:14:19 +00:00
|
|
|
body := new(netmap.LocalNodeInfoResponseBody)
|
|
|
|
body.SetVersion(s.version.ToV2())
|
2021-01-25 08:02:20 +00:00
|
|
|
body.SetNodeInfo(ni)
|
2020-10-08 13:14:19 +00:00
|
|
|
|
|
|
|
resp := new(netmap.LocalNodeInfoResponse)
|
|
|
|
resp.SetBody(body)
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
2021-02-19 08:01:37 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|