2020-10-08 12:06:36 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2020-11-05 12:49:31 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2020-10-08 12:06:36 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/client"
|
2020-11-05 12:49:31 +00:00
|
|
|
v2netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap"
|
2020-10-08 12:06:36 +00:00
|
|
|
v2signature "github.com/nspcc-dev/neofs-api-go/v2/signature"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EndpointInfo returns attributes, address and public key of the node, specified
|
|
|
|
// in client constructor via address or open connection. This can be used as a
|
|
|
|
// health check to see if node is alive and responses to requests.
|
|
|
|
func (c Client) EndpointInfo(ctx context.Context, opts ...CallOption) (*netmap.NodeInfo, error) {
|
|
|
|
switch c.remoteNode.Version.GetMajor() {
|
|
|
|
case 2:
|
2020-11-03 15:59:57 +00:00
|
|
|
resp, err := c.endpointInfoV2(ctx, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-05 12:49:31 +00:00
|
|
|
return netmap.NewNodeInfoFromV2(resp.GetBody().GetNodeInfo()), nil
|
2020-10-08 12:06:36 +00:00
|
|
|
default:
|
|
|
|
return nil, unsupportedProtocolErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 15:59:57 +00:00
|
|
|
// Epoch returns the epoch number from the local state of the remote host.
|
|
|
|
func (c Client) Epoch(ctx context.Context, opts ...CallOption) (uint64, error) {
|
|
|
|
switch c.remoteNode.Version.GetMajor() {
|
|
|
|
case 2:
|
|
|
|
resp, err := c.endpointInfoV2(ctx, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.GetMetaHeader().GetEpoch(), nil
|
|
|
|
default:
|
|
|
|
return 0, unsupportedProtocolErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 12:49:31 +00:00
|
|
|
func (c Client) endpointInfoV2(ctx context.Context, opts ...CallOption) (*v2netmap.LocalNodeInfoResponse, error) {
|
2020-10-08 12:06:36 +00:00
|
|
|
// apply all available options
|
2020-10-27 12:54:02 +00:00
|
|
|
callOptions := c.defaultCallOptions()
|
2020-10-08 12:06:36 +00:00
|
|
|
for i := range opts {
|
|
|
|
opts[i].apply(&callOptions)
|
|
|
|
}
|
|
|
|
|
2020-11-05 12:49:31 +00:00
|
|
|
reqBody := new(v2netmap.LocalNodeInfoRequestBody)
|
2020-10-08 12:06:36 +00:00
|
|
|
|
2020-11-05 12:49:31 +00:00
|
|
|
req := new(v2netmap.LocalNodeInfoRequest)
|
2020-10-08 12:06:36 +00:00
|
|
|
req.SetBody(reqBody)
|
|
|
|
req.SetMetaHeader(v2MetaHeaderFromOpts(callOptions))
|
|
|
|
|
|
|
|
err := v2signature.SignServiceMessage(c.key, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch c.remoteNode.Protocol {
|
|
|
|
case GRPC:
|
|
|
|
cli, err := v2NetmapClientFromOptions(c.opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't create grpc client")
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := cli.LocalNodeInfo(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "transport error")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = v2signature.VerifyServiceMessage(resp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "can't verify response message")
|
|
|
|
}
|
|
|
|
|
2020-11-03 15:59:57 +00:00
|
|
|
return resp, nil
|
2020-10-08 12:06:36 +00:00
|
|
|
default:
|
|
|
|
return nil, unsupportedProtocolErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-05 12:49:31 +00:00
|
|
|
func v2NetmapClientFromOptions(opts *clientOptions) (cli *v2netmap.Client, err error) {
|
2020-10-08 12:06:36 +00:00
|
|
|
switch {
|
|
|
|
case opts.grpcOpts.v2NetmapClient != nil:
|
|
|
|
// return value from client cache
|
|
|
|
return opts.grpcOpts.v2NetmapClient, nil
|
|
|
|
|
|
|
|
case opts.grpcOpts.conn != nil:
|
2020-11-05 12:49:31 +00:00
|
|
|
cli, err = v2netmap.NewClient(v2netmap.WithGlobalOpts(
|
2020-10-08 12:06:36 +00:00
|
|
|
client.WithGRPCConn(opts.grpcOpts.conn)),
|
|
|
|
)
|
|
|
|
|
|
|
|
case opts.addr != "":
|
2020-11-05 12:49:31 +00:00
|
|
|
cli, err = v2netmap.NewClient(v2netmap.WithGlobalOpts(
|
2020-10-08 12:06:36 +00:00
|
|
|
client.WithNetworkAddress(opts.addr)),
|
|
|
|
)
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, errors.New("lack of sdk client options to create accounting client")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if client correct and save in cache
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.grpcOpts.v2NetmapClient = cli
|
|
|
|
|
|
|
|
return cli, nil
|
|
|
|
}
|