From e6fedfbc6934a8d07d8091179a8f52c24120ce1f Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Sat, 22 Aug 2020 18:20:47 +0300 Subject: [PATCH] [#11] Use Neo:Morph ServiceExecutor of Accounting service in app Signed-off-by: Leonard Lyubich --- cmd/neofs-node/accounting.go | 51 ++++++++++++++++++++++++++++++++++++ cmd/neofs-node/config.go | 24 ++++++++++++++--- cmd/neofs-node/grpc.go | 29 +++++--------------- cmd/neofs-node/morph.go | 13 +++++++++ 4 files changed, 91 insertions(+), 26 deletions(-) create mode 100644 cmd/neofs-node/accounting.go create mode 100644 cmd/neofs-node/morph.go diff --git a/cmd/neofs-node/accounting.go b/cmd/neofs-node/accounting.go new file mode 100644 index 000000000..6e576f453 --- /dev/null +++ b/cmd/neofs-node/accounting.go @@ -0,0 +1,51 @@ +package main + +import ( + "github.com/nspcc-dev/neo-go/pkg/util" + accountingGRPC "github.com/nspcc-dev/neofs-api-go/v2/accounting/grpc" + "github.com/nspcc-dev/neofs-api-go/v2/session" + "github.com/nspcc-dev/neofs-node/pkg/morph/client" + "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance" + accountingTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/accounting/grpc" + accountingService "github.com/nspcc-dev/neofs-node/pkg/services/accounting" + accounting "github.com/nspcc-dev/neofs-node/pkg/services/accounting/morph" +) + +type cfgAccounting struct { + scriptHash string + + fee util.Fixed8 +} + +func initAccountingService(c *cfg) { + if c.morphClient == nil { + initMorphComponents(c) + } + + u160, err := util.Uint160DecodeStringLE(c.cfgAccounting.scriptHash) + fatalOnErr(err) + + staticClient, err := client.NewStatic(c.morphClient, u160, c.cfgAccounting.fee) + fatalOnErr(err) + + balanceClient, err := balance.New(staticClient) + fatalOnErr(err) + + metaHdr := new(session.ResponseMetaHeader) + xHdr := new(session.XHeader) + xHdr.SetKey("test X-Header key") + xHdr.SetValue("test X-Header value") + metaHdr.SetXHeaders([]*session.XHeader{xHdr}) + + accountingGRPC.RegisterAccountingServiceServer(c.grpcSrv, + accountingTransportGRPC.New( + accountingService.NewSignService( + c.key, + accountingService.NewExecutionService( + accounting.NewExecutor(balanceClient), + metaHdr, + ), + ), + ), + ) +} diff --git a/cmd/neofs-node/config.go b/cmd/neofs-node/config.go index cb9fda6dc..9eac49be3 100644 --- a/cmd/neofs-node/config.go +++ b/cmd/neofs-node/config.go @@ -5,7 +5,10 @@ import ( "crypto/ecdsa" "sync" + "github.com/nspcc-dev/neo-go/pkg/util" crypto "github.com/nspcc-dev/neofs-crypto" + "github.com/nspcc-dev/neofs-node/pkg/morph/client" + "google.golang.org/grpc" ) type cfg struct { @@ -16,6 +19,14 @@ type cfg struct { grpcAddr string key *ecdsa.PrivateKey + + grpcSrv *grpc.Server + + morphEndpoint string + + morphClient *client.Client + + cfgAccounting *cfgAccounting } func defaultCfg() *cfg { @@ -23,9 +34,14 @@ func defaultCfg() *cfg { fatalOnErr(err) return &cfg{ - ctx: context.Background(), - wg: new(sync.WaitGroup), - grpcAddr: "127.0.0.1:50501", - key: key, + ctx: context.Background(), + wg: new(sync.WaitGroup), + grpcAddr: "127.0.0.1:50501", + key: key, + morphEndpoint: "http://morph_chain.localtest.nspcc.ru:30333/", + cfgAccounting: &cfgAccounting{ + scriptHash: "1aeefe1d0dfade49740fff779c02cd4a0538ffb1", + fee: util.Fixed8(1), + }, } } diff --git a/cmd/neofs-node/grpc.go b/cmd/neofs-node/grpc.go index 65d15b01c..f8b68bc28 100644 --- a/cmd/neofs-node/grpc.go +++ b/cmd/neofs-node/grpc.go @@ -6,18 +6,15 @@ import ( "net" "github.com/nspcc-dev/neofs-api-go/v2/accounting" - accountingGRPC "github.com/nspcc-dev/neofs-api-go/v2/accounting/grpc" containerGRPC "github.com/nspcc-dev/neofs-api-go/v2/container" container "github.com/nspcc-dev/neofs-api-go/v2/container/grpc" objectGRPC "github.com/nspcc-dev/neofs-api-go/v2/object" object "github.com/nspcc-dev/neofs-api-go/v2/object/grpc" "github.com/nspcc-dev/neofs-api-go/v2/session" sessionGRPC "github.com/nspcc-dev/neofs-api-go/v2/session/grpc" - accountingTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/accounting/grpc" containerTransport "github.com/nspcc-dev/neofs-node/pkg/network/transport/container/grpc" objectTransport "github.com/nspcc-dev/neofs-node/pkg/network/transport/object/grpc" sessionTransport "github.com/nspcc-dev/neofs-node/pkg/network/transport/session/grpc" - accountingService "github.com/nspcc-dev/neofs-node/pkg/services/accounting" "github.com/pkg/errors" "google.golang.org/grpc" ) @@ -98,25 +95,13 @@ func serveGRPC(c *cfg) { lis, err := net.Listen("tcp", c.grpcAddr) fatalOnErr(err) - srv := grpc.NewServer() + c.grpcSrv = grpc.NewServer() - metaHdr := new(session.ResponseMetaHeader) - xHdr := new(session.XHeader) - xHdr.SetKey("test X-Header key") - xHdr.SetValue("test X-Header value") - metaHdr.SetXHeaders([]*session.XHeader{xHdr}) + initAccountingService(c) - accountingGRPC.RegisterAccountingServiceServer(srv, - accountingTransportGRPC.New( - accountingService.NewSignService( - c.key, - accountingService.NewExecutionService(new(accountingSvcExec), metaHdr), - ), - ), - ) - container.RegisterContainerServiceServer(srv, containerTransport.New(new(containerSvc))) - sessionGRPC.RegisterSessionServiceServer(srv, sessionTransport.New(new(sessionSvc))) - object.RegisterObjectServiceServer(srv, objectTransport.New(new(objectSvc))) + container.RegisterContainerServiceServer(c.grpcSrv, containerTransport.New(new(containerSvc))) + sessionGRPC.RegisterSessionServiceServer(c.grpcSrv, sessionTransport.New(new(sessionSvc))) + object.RegisterObjectServiceServer(c.grpcSrv, objectTransport.New(new(objectSvc))) go func() { c.wg.Add(1) @@ -124,7 +109,7 @@ func serveGRPC(c *cfg) { c.wg.Done() }() - if err := srv.Serve(lis); err != nil { + if err := c.grpcSrv.Serve(lis); err != nil { fmt.Println("gRPC server error", err) } }() @@ -139,6 +124,6 @@ func serveGRPC(c *cfg) { <-c.ctx.Done() - srv.GracefulStop() + c.grpcSrv.GracefulStop() }() } diff --git a/cmd/neofs-node/morph.go b/cmd/neofs-node/morph.go new file mode 100644 index 000000000..f713bd2aa --- /dev/null +++ b/cmd/neofs-node/morph.go @@ -0,0 +1,13 @@ +package main + +import ( + "github.com/nspcc-dev/neofs-node/pkg/morph/client" +) + +func initMorphComponents(c *cfg) { + var err error + + c.morphClient, err = client.New(c.key, c.morphEndpoint) + + fatalOnErr(err) +}