From 2d5cb378a748c3772c8a95a80084a7c615967906 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 8 Oct 2020 16:14:19 +0300 Subject: [PATCH] [#84] Add netmap service executor and signer Signed-off-by: Alex Vanin --- pkg/services/netmap/executor.go | 41 +++++++++++++++++++++++++++++++++ pkg/services/netmap/sign.go | 38 ++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 pkg/services/netmap/executor.go create mode 100644 pkg/services/netmap/sign.go diff --git a/pkg/services/netmap/executor.go b/pkg/services/netmap/executor.go new file mode 100644 index 000000000..e8f9df8ca --- /dev/null +++ b/pkg/services/netmap/executor.go @@ -0,0 +1,41 @@ +package netmap + +import ( + "context" + + "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/session" +) + +type executorSvc struct { + version *pkg.Version + localNodeInfo *netmap.NodeInfo +} + +func NewExecutionService(ni *netmap.NodeInfo, v *pkg.Version) netmap.Service { + if ni == nil || v == nil { + // this should never happen, otherwise it programmers bug + panic("can't create netmap execution service") + } + + return &executorSvc{ + version: v, + localNodeInfo: ni, + } +} + +func (s *executorSvc) LocalNodeInfo( + _ context.Context, + _ *netmap.LocalNodeInfoRequest) (*netmap.LocalNodeInfoResponse, error) { + + body := new(netmap.LocalNodeInfoResponseBody) + body.SetVersion(s.version.ToV2()) + body.SetNodeInfo(s.localNodeInfo) + + resp := new(netmap.LocalNodeInfoResponse) + resp.SetBody(body) + resp.SetMetaHeader(new(session.ResponseMetaHeader)) + + return resp, nil +} diff --git a/pkg/services/netmap/sign.go b/pkg/services/netmap/sign.go new file mode 100644 index 000000000..57018c41f --- /dev/null +++ b/pkg/services/netmap/sign.go @@ -0,0 +1,38 @@ +package netmap + +import ( + "context" + "crypto/ecdsa" + + "github.com/nspcc-dev/neofs-api-go/v2/netmap" + "github.com/nspcc-dev/neofs-node/pkg/services/util" +) + +type signService struct { + sigSvc *util.SignService + + svc netmap.Service +} + +func NewSignService(key *ecdsa.PrivateKey, svc netmap.Service) netmap.Service { + return &signService{ + sigSvc: util.NewUnarySignService(key), + svc: svc, + } +} + +func (s *signService) LocalNodeInfo( + ctx context.Context, + req *netmap.LocalNodeInfoRequest) (*netmap.LocalNodeInfoResponse, error) { + + resp, err := s.sigSvc.HandleUnaryRequest(ctx, req, + func(ctx context.Context, req interface{}) (interface{}, error) { + return s.svc.LocalNodeInfo(ctx, req.(*netmap.LocalNodeInfoRequest)) + }, + ) + if err != nil { + return nil, err + } + + return resp.(*netmap.LocalNodeInfoResponse), nil +}