[#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" "crypto/ecdsa"
"sync" "sync"
"github.com/nspcc-dev/neo-go/pkg/util"
crypto "github.com/nspcc-dev/neofs-crypto" crypto "github.com/nspcc-dev/neofs-crypto"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"google.golang.org/grpc"
) )
type cfg struct { type cfg struct {
@ -16,6 +19,14 @@ type cfg struct {
grpcAddr string grpcAddr string
key *ecdsa.PrivateKey key *ecdsa.PrivateKey
grpcSrv *grpc.Server
morphEndpoint string
morphClient *client.Client
cfgAccounting *cfgAccounting
} }
func defaultCfg() *cfg { func defaultCfg() *cfg {
@ -23,9 +34,14 @@ func defaultCfg() *cfg {
fatalOnErr(err) fatalOnErr(err)
return &cfg{ return &cfg{
ctx: context.Background(), ctx: context.Background(),
wg: new(sync.WaitGroup), wg: new(sync.WaitGroup),
grpcAddr: "127.0.0.1:50501", grpcAddr: "127.0.0.1:50501",
key: key, 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" "net"
"github.com/nspcc-dev/neofs-api-go/v2/accounting" "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" containerGRPC "github.com/nspcc-dev/neofs-api-go/v2/container"
container "github.com/nspcc-dev/neofs-api-go/v2/container/grpc" container "github.com/nspcc-dev/neofs-api-go/v2/container/grpc"
objectGRPC "github.com/nspcc-dev/neofs-api-go/v2/object" objectGRPC "github.com/nspcc-dev/neofs-api-go/v2/object"
object "github.com/nspcc-dev/neofs-api-go/v2/object/grpc" object "github.com/nspcc-dev/neofs-api-go/v2/object/grpc"
"github.com/nspcc-dev/neofs-api-go/v2/session" "github.com/nspcc-dev/neofs-api-go/v2/session"
sessionGRPC "github.com/nspcc-dev/neofs-api-go/v2/session/grpc" 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" containerTransport "github.com/nspcc-dev/neofs-node/pkg/network/transport/container/grpc"
objectTransport "github.com/nspcc-dev/neofs-node/pkg/network/transport/object/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" 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" "github.com/pkg/errors"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
@ -98,25 +95,13 @@ func serveGRPC(c *cfg) {
lis, err := net.Listen("tcp", c.grpcAddr) lis, err := net.Listen("tcp", c.grpcAddr)
fatalOnErr(err) fatalOnErr(err)
srv := grpc.NewServer() c.grpcSrv = grpc.NewServer()
metaHdr := new(session.ResponseMetaHeader) initAccountingService(c)
xHdr := new(session.XHeader)
xHdr.SetKey("test X-Header key")
xHdr.SetValue("test X-Header value")
metaHdr.SetXHeaders([]*session.XHeader{xHdr})
accountingGRPC.RegisterAccountingServiceServer(srv, container.RegisterContainerServiceServer(c.grpcSrv, containerTransport.New(new(containerSvc)))
accountingTransportGRPC.New( sessionGRPC.RegisterSessionServiceServer(c.grpcSrv, sessionTransport.New(new(sessionSvc)))
accountingService.NewSignService( object.RegisterObjectServiceServer(c.grpcSrv, objectTransport.New(new(objectSvc)))
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)))
go func() { go func() {
c.wg.Add(1) c.wg.Add(1)
@ -124,7 +109,7 @@ func serveGRPC(c *cfg) {
c.wg.Done() c.wg.Done()
}() }()
if err := srv.Serve(lis); err != nil { if err := c.grpcSrv.Serve(lis); err != nil {
fmt.Println("gRPC server error", err) fmt.Println("gRPC server error", err)
} }
}() }()
@ -139,6 +124,6 @@ func serveGRPC(c *cfg) {
<-c.ctx.Done() <-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)
}