From 2b2b2c2c458204850f3a7f8f919454198924fab3 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 21 May 2021 14:02:46 +0300 Subject: [PATCH] [#496] Use single contract wrapper constructor There is no need in a separate `New()` or `WrapClient()` Signed-off-by: Evgenii Stratonikov --- cmd/neofs-node/accounting.go | 14 +------------- cmd/neofs-node/container.go | 16 ++-------------- cmd/neofs-node/morph.go | 13 +------------ cmd/neofs-node/reputation.go | 15 +-------------- pkg/morph/client/audit/wrapper/result_test.go | 7 +------ pkg/morph/client/audit/wrapper/wrapper.go | 7 +------ pkg/morph/client/balance/wrapper/wrapper.go | 15 +-------------- pkg/morph/client/container/wrapper/wrapper.go | 15 +-------------- pkg/morph/client/netmap/wrapper/wrapper.go | 15 +-------------- pkg/morph/client/reputation/wrapper/wrapper.go | 7 +------ pkg/services/container/morph/executor.go | 9 +-------- 11 files changed, 12 insertions(+), 121 deletions(-) diff --git a/cmd/neofs-node/accounting.go b/cmd/neofs-node/accounting.go index d5d3c33e9..e58426fcc 100644 --- a/cmd/neofs-node/accounting.go +++ b/cmd/neofs-node/accounting.go @@ -2,8 +2,6 @@ package main import ( accountingGRPC "github.com/nspcc-dev/neofs-api-go/v2/accounting/grpc" - "github.com/nspcc-dev/neofs-node/pkg/morph/client" - "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance" "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance/wrapper" accountingTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/accounting/grpc" accountingService "github.com/nspcc-dev/neofs-node/pkg/services/accounting" @@ -15,17 +13,7 @@ func initAccountingService(c *cfg) { initMorphComponents(c) } - staticClient, err := client.NewStatic( - c.cfgMorph.client, - c.cfgAccounting.scriptHash, - 0, - ) - fatalOnErr(err) - - balanceClient, err := balance.New(staticClient) - fatalOnErr(err) - - balanceMorphWrapper, err := wrapper.New(balanceClient) + balanceMorphWrapper, err := wrapper.NewFromMorph(c.cfgMorph.client, c.cfgAccounting.scriptHash, 0) fatalOnErr(err) accountingGRPC.RegisterAccountingServiceServer(c.cfgGRPC.server, diff --git a/cmd/neofs-node/container.go b/cmd/neofs-node/container.go index 564ff9452..7f7299596 100644 --- a/cmd/neofs-node/container.go +++ b/cmd/neofs-node/container.go @@ -17,8 +17,6 @@ import ( containerCore "github.com/nspcc-dev/neofs-node/pkg/core/container" netmapCore "github.com/nspcc-dev/neofs-node/pkg/core/netmap" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine" - "github.com/nspcc-dev/neofs-node/pkg/morph/client" - "github.com/nspcc-dev/neofs-node/pkg/morph/client/container" "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" "github.com/nspcc-dev/neofs-node/pkg/morph/event" containerEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/container" @@ -41,17 +39,7 @@ const ( ) func initContainerService(c *cfg) { - staticClient, err := client.NewStatic( - c.cfgMorph.client, - c.cfgContainer.scriptHash, - 0, - ) - fatalOnErr(err) - - cnrClient, err := container.New(staticClient) - fatalOnErr(err) - - wrap, err := wrapper.New(cnrClient) + wrap, err := wrapper.NewFromMorph(c.cfgMorph.client, c.cfgContainer.scriptHash, 0) fatalOnErr(err) c.cfgObject.cnrStorage = newCachedContainerStorage(wrap) // use RPC node as source of containers (with caching) @@ -126,7 +114,7 @@ func initContainerService(c *cfg) { c.key, containerService.NewResponseService( &usedSpaceService{ - Server: containerService.NewExecutionService(containerMorph.NewExecutor(cnrClient)), + Server: containerService.NewExecutionService(containerMorph.NewExecutor(wrap)), loadWriterProvider: loadRouter, loadPlacementBuilder: loadPlacementBuilder, routeBuilder: routeBuilder, diff --git a/cmd/neofs-node/morph.go b/cmd/neofs-node/morph.go index 32e2a20fe..7135dc5c6 100644 --- a/cmd/neofs-node/morph.go +++ b/cmd/neofs-node/morph.go @@ -9,7 +9,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/block" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neofs-node/pkg/morph/client" - "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap" "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper" "github.com/nspcc-dev/neofs-node/pkg/morph/event" netmapEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap" @@ -74,17 +73,7 @@ func initMorphComponents(c *cfg) { c.cfgMorph.client = cli }, true) - staticClient, err := client.NewStatic( - c.cfgMorph.client, - c.cfgNetmap.scriptHash, - 0, - ) - fatalOnErr(err) - - cli, err := netmap.New(staticClient) - fatalOnErr(err) - - wrap, err := wrapper.New(cli) + wrap, err := wrapper.NewFromMorph(c.cfgMorph.client, c.cfgNetmap.scriptHash, 0) fatalOnErr(err) c.cfgObject.netMapStorage = newCachedNetmapStorage(c.cfgNetmap.state, wrap) diff --git a/cmd/neofs-node/reputation.go b/cmd/neofs-node/reputation.go index a8f80bc3e..0011ed926 100644 --- a/cmd/neofs-node/reputation.go +++ b/cmd/neofs-node/reputation.go @@ -4,7 +4,6 @@ import ( "context" "fmt" - "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" v2reputation "github.com/nspcc-dev/neofs-api-go/v2/reputation" v2reputationgrpc "github.com/nspcc-dev/neofs-api-go/v2/reputation/grpc" "github.com/nspcc-dev/neofs-api-go/v2/session" @@ -13,8 +12,6 @@ import ( "github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/intermediate" intermediatereputation "github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/intermediate" localreputation "github.com/nspcc-dev/neofs-node/cmd/neofs-node/reputation/local" - "github.com/nspcc-dev/neofs-node/pkg/morph/client" - rptclient "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation" rtpwrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation/wrapper" "github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap" @@ -38,17 +35,7 @@ import ( ) func initReputationService(c *cfg) { - staticClient, err := client.NewStatic( - c.cfgMorph.client, - c.cfgReputation.scriptHash, - fixedn.Fixed8(0), - ) - fatalOnErr(err) - - rptClient, err := rptclient.New(staticClient) - fatalOnErr(err) - - wrap := rtpwrapper.WrapClient(rptClient) + wrap, err := rtpwrapper.NewFromMorph(c.cfgMorph.client, c.cfgReputation.scriptHash, 0) fatalOnErr(err) localKey := crypto.MarshalPublicKey(&c.key.PublicKey) diff --git a/pkg/morph/client/audit/wrapper/result_test.go b/pkg/morph/client/audit/wrapper/result_test.go index 91556eed0..1aa647878 100644 --- a/pkg/morph/client/audit/wrapper/result_test.go +++ b/pkg/morph/client/audit/wrapper/result_test.go @@ -10,7 +10,6 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg/container" crypto "github.com/nspcc-dev/neofs-crypto" "github.com/nspcc-dev/neofs-node/pkg/morph/client" - "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit" auditWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit/wrapper" "github.com/stretchr/testify/require" ) @@ -34,13 +33,9 @@ func TestAuditResults(t *testing.T) { morphClient, err := client.New(key, endpoint) require.NoError(t, err) - auditContractClient, err := client.NewStatic(morphClient, auditHash, 0) + auditClientWrapper, err := auditWrapper.NewFromMorph(morphClient, auditHash, 0) require.NoError(t, err) - auditClient := audit.New(auditContractClient) - - auditClientWrapper := auditWrapper.WrapClient(auditClient) - cid := container.NewID() cid.SetSHA256([sha256.Size]byte{1, 2, 3}) diff --git a/pkg/morph/client/audit/wrapper/wrapper.go b/pkg/morph/client/audit/wrapper/wrapper.go index 7480baf5a..18ed37325 100644 --- a/pkg/morph/client/audit/wrapper/wrapper.go +++ b/pkg/morph/client/audit/wrapper/wrapper.go @@ -11,11 +11,6 @@ import ( // client which implements storage of audit results. type ClientWrapper audit.Client -// WrapClient wraps Audit contract client and returns ClientWrapper instance. -func WrapClient(c *audit.Client) *ClientWrapper { - return (*ClientWrapper)(c) -} - // NewFromMorph returns the wrapper instance from the raw morph client. func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8) (*ClientWrapper, error) { staticClient, err := client.NewStatic(cli, contract, fee) @@ -23,5 +18,5 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8) return nil, err } - return WrapClient(audit.New(staticClient)), nil + return (*ClientWrapper)(audit.New(staticClient)), nil } diff --git a/pkg/morph/client/balance/wrapper/wrapper.go b/pkg/morph/client/balance/wrapper/wrapper.go index 675620288..41dccf2e1 100644 --- a/pkg/morph/client/balance/wrapper/wrapper.go +++ b/pkg/morph/client/balance/wrapper/wrapper.go @@ -33,19 +33,6 @@ type Wrapper struct { // a non-nil Wrapper pointer, but received nil. var ErrNilWrapper = errors.New("balance contract client wrapper is nil") -// New creates, initializes and returns the Wrapper instance. -// -// If Client is nil, balance.ErrNilClient is returned. -func New(c *Client) (*Wrapper, error) { - if c == nil { - return nil, balance.ErrNilClient - } - - return &Wrapper{ - client: c, - }, nil -} - // NewFromMorph returns the wrapper instance from the raw morph client. func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8) (*Wrapper, error) { staticClient, err := client.NewStatic(cli, contract, fee) @@ -58,5 +45,5 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8) return nil, fmt.Errorf("could not create Balance contract client: %w", err) } - return New(enhancedBalanceClient) + return &Wrapper{client: enhancedBalanceClient}, nil } diff --git a/pkg/morph/client/container/wrapper/wrapper.go b/pkg/morph/client/container/wrapper/wrapper.go index 883a38a3f..7635cea5c 100644 --- a/pkg/morph/client/container/wrapper/wrapper.go +++ b/pkg/morph/client/container/wrapper/wrapper.go @@ -27,19 +27,6 @@ type Wrapper struct { client *Client } -// New creates, initializes and returns the Wrapper instance. -// -// If Client is nil, container.ErrNilClient is returned. -func New(c *Client) (*Wrapper, error) { - if c == nil { - return nil, container.ErrNilClient - } - - return &Wrapper{ - client: c, - }, nil -} - // NewFromMorph returns the wrapper instance from the raw morph client. func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8) (*Wrapper, error) { staticClient, err := client.NewStatic(cli, contract, fee) @@ -52,5 +39,5 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8) return nil, fmt.Errorf("can't create container morph client: %w", err) } - return New(enhancedContainerClient) + return &Wrapper{client: enhancedContainerClient}, nil } diff --git a/pkg/morph/client/netmap/wrapper/wrapper.go b/pkg/morph/client/netmap/wrapper/wrapper.go index 0a9e9e3bf..0bfc47c72 100644 --- a/pkg/morph/client/netmap/wrapper/wrapper.go +++ b/pkg/morph/client/netmap/wrapper/wrapper.go @@ -33,19 +33,6 @@ type Wrapper struct { // a non-nil Wrapper pointer, but received nil. var ErrNilWrapper = errors.New("netmap contract client wrapper is nil") -// New creates, initializes and returns the Wrapper instance. -// -// If Client is nil, netmap.ErrNilClient is returned. -func New(c *Client) (*Wrapper, error) { - if c == nil { - return nil, netmap.ErrNilClient - } - - return &Wrapper{ - client: c, - }, nil -} - // NewFromMorph returns the wrapper instance from the raw morph client. func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8) (*Wrapper, error) { staticClient, err := client.NewStatic(cli, contract, fee) @@ -58,5 +45,5 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8) return nil, fmt.Errorf("can't create netmap morph client: %w", err) } - return New(enhancedNetmapClient) + return &Wrapper{client: enhancedNetmapClient}, nil } diff --git a/pkg/morph/client/reputation/wrapper/wrapper.go b/pkg/morph/client/reputation/wrapper/wrapper.go index 6836a7844..4fe8043f3 100644 --- a/pkg/morph/client/reputation/wrapper/wrapper.go +++ b/pkg/morph/client/reputation/wrapper/wrapper.go @@ -13,11 +13,6 @@ import ( // client which implements storage of reputation values. type ClientWrapper reputation.Client -// WrapClient wraps reputation contract client and returns ClientWrapper instance. -func WrapClient(c *reputation.Client) *ClientWrapper { - return (*ClientWrapper)(c) -} - // NewFromMorph returns the wrapper instance from the raw morph client. func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8) (*ClientWrapper, error) { staticClient, err := client.NewStatic(cli, contract, fee) @@ -30,5 +25,5 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8) return nil, fmt.Errorf("could not create reputation contract client: %w", err) } - return WrapClient(enhancedRepurationClient), nil + return (*ClientWrapper)(enhancedRepurationClient), nil } diff --git a/pkg/services/container/morph/executor.go b/pkg/services/container/morph/executor.go index d681e5d04..9020df7a0 100644 --- a/pkg/services/container/morph/executor.go +++ b/pkg/services/container/morph/executor.go @@ -9,7 +9,6 @@ import ( "github.com/nspcc-dev/neofs-api-go/pkg/owner" "github.com/nspcc-dev/neofs-api-go/v2/container" "github.com/nspcc-dev/neofs-api-go/v2/refs" - containerMorph "github.com/nspcc-dev/neofs-node/pkg/morph/client/container" "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" containerSvc "github.com/nspcc-dev/neofs-node/pkg/services/container" ) @@ -18,13 +17,7 @@ type morphExecutor struct { wrapper *wrapper.Wrapper } -func NewExecutor(client *containerMorph.Client) containerSvc.ServiceExecutor { - w, err := wrapper.New(client) - if err != nil { - // log there, maybe panic? - return nil - } - +func NewExecutor(w *wrapper.Wrapper) containerSvc.ServiceExecutor { return &morphExecutor{ wrapper: w, }