[#11] Use Neo:Morph ServiceExecutor of Accounting service in app

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-08-22 18:20:47 +03:00 committed by Alex Vanin
parent 5022362c1a
commit e6fedfbc69
4 changed files with 91 additions and 26 deletions

View file

@ -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,
),
),
),
)
}

View file

@ -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),
},
}
}

View file

@ -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()
}()
}

13
cmd/neofs-node/morph.go Normal file
View file

@ -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)
}