2020-10-08 13:14:19 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
2021-09-20 16:13:17 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/refs"
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/version"
|
2020-10-08 13:14:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type executorSvc struct {
|
2021-11-10 07:08:33 +00:00
|
|
|
version *version.Version
|
2021-01-25 08:02:20 +00:00
|
|
|
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 {
|
2021-09-20 16:13:17 +00:00
|
|
|
// 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)
|
2021-02-19 08:01:37 +00:00
|
|
|
}
|
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
func NewExecutionService(s NodeState, v *version.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) {
|
2021-11-10 07:08:33 +00:00
|
|
|
ver := version.NewFromV2(req.GetMetaHeader().GetVersion())
|
2021-06-25 15:38:58 +00:00
|
|
|
|
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,
|
2021-09-20 16:13:17 +00:00
|
|
|
req *netmap.NetworkInfoRequest) (*netmap.NetworkInfoResponse, error) {
|
|
|
|
ni, err := s.netInfo.Dump(req.GetMetaHeader().GetVersion())
|
2021-02-19 08:01:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
body := new(netmap.NetworkInfoResponseBody)
|
|
|
|
body.SetNetworkInfo(ni)
|
|
|
|
|
|
|
|
resp := new(netmap.NetworkInfoResponse)
|
|
|
|
resp.SetBody(body)
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|