From 0decb95591ac9bb61bd9243641cf9ffd964cade0 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Sat, 29 Jan 2022 16:21:49 +0300 Subject: [PATCH] [#625] morph/client: make all constructors return no error In some places we panic, in some we return error, in some (audit) just return a client. However in all of the places static client is created immediately before the sugared-client creation. This commit makes all constructors to just return a client for the sake of code simplification and unification. Signed-off-by: Evgenii Stratonikov --- pkg/morph/client/balance/client.go | 10 ++-------- pkg/morph/client/balance/wrapper/wrapper.go | 7 +------ pkg/morph/client/container/client.go | 10 ++-------- pkg/morph/client/container/wrapper/wrapper.go | 7 +------ pkg/morph/client/neofs/client.go | 6 ------ pkg/morph/client/neofsid/client.go | 6 ------ pkg/morph/client/netmap/client.go | 10 ++-------- pkg/morph/client/netmap/wrapper/wrapper.go | 7 +------ pkg/morph/client/reputation/client.go | 10 ++-------- pkg/morph/client/reputation/wrapper/wrapper.go | 7 +------ pkg/morph/client/static.go | 5 ----- 11 files changed, 12 insertions(+), 73 deletions(-) diff --git a/pkg/morph/client/balance/client.go b/pkg/morph/client/balance/client.go index 1ad2f3ea86..2cede64692 100644 --- a/pkg/morph/client/balance/client.go +++ b/pkg/morph/client/balance/client.go @@ -26,12 +26,6 @@ const ( ) // New creates, initializes and returns the Client instance. -// -// If StaticClient is nil, client.ErrNilStaticClient is returned. -func New(c *client.StaticClient) (*Client, error) { - if c == nil { - return nil, client.ErrNilStaticClient - } - - return &Client{client: c}, nil +func New(c *client.StaticClient) *Client { + return &Client{client: c} } diff --git a/pkg/morph/client/balance/wrapper/wrapper.go b/pkg/morph/client/balance/wrapper/wrapper.go index 695387030f..1fc8990f9f 100644 --- a/pkg/morph/client/balance/wrapper/wrapper.go +++ b/pkg/morph/client/balance/wrapper/wrapper.go @@ -67,13 +67,8 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8, return nil, fmt.Errorf("could not create static client of Balance contract: %w", err) } - enhancedBalanceClient, err := balance.New(staticClient) - if err != nil { - return nil, fmt.Errorf("could not create Balance contract client: %w", err) - } - return &Wrapper{ StaticClient: staticClient, - client: enhancedBalanceClient, + client: balance.New(staticClient), }, nil } diff --git a/pkg/morph/client/container/client.go b/pkg/morph/client/container/client.go index a6641dbcb2..f628c4713d 100644 --- a/pkg/morph/client/container/client.go +++ b/pkg/morph/client/container/client.go @@ -36,14 +36,8 @@ const ( ) // New creates, initializes and returns the Client instance. -// -// If StaticClient is nil, client.ErrNilStaticClient is returned. -func New(c *client.StaticClient) (*Client, error) { - if c == nil { - return nil, client.ErrNilStaticClient - } - - return &Client{client: c}, nil +func New(c *client.StaticClient) *Client { + return &Client{client: c} } // Morph returns raw morph client. diff --git a/pkg/morph/client/container/wrapper/wrapper.go b/pkg/morph/client/container/wrapper/wrapper.go index 37a241bd8e..2ac69d1bf1 100644 --- a/pkg/morph/client/container/wrapper/wrapper.go +++ b/pkg/morph/client/container/wrapper/wrapper.go @@ -93,13 +93,8 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8, return nil, fmt.Errorf("can't create container static client: %w", err) } - enhancedContainerClient, err := container.New(staticClient) - if err != nil { - return nil, fmt.Errorf("can't create container morph client: %w", err) - } - return &Wrapper{ StaticClient: staticClient, - client: enhancedContainerClient, + client: container.New(staticClient), }, nil } diff --git a/pkg/morph/client/neofs/client.go b/pkg/morph/client/neofs/client.go index 3ff274fe40..01aec0c45a 100644 --- a/pkg/morph/client/neofs/client.go +++ b/pkg/morph/client/neofs/client.go @@ -24,12 +24,6 @@ const ( ) // New creates, initializes and returns the Client instance. -// -// If StaticClient is nil, panic occurs. func New(c *client.StaticClient) *Client { - if c == nil { - panic("static client is nil") - } - return &Client{client: c} } diff --git a/pkg/morph/client/neofsid/client.go b/pkg/morph/client/neofsid/client.go index 645961d822..82d42d94bc 100644 --- a/pkg/morph/client/neofsid/client.go +++ b/pkg/morph/client/neofsid/client.go @@ -23,12 +23,6 @@ const ( ) // New creates, initializes and returns the Client instance. -// -// If StaticClient is nil, panic occurs. func New(c *client.StaticClient) *Client { - if c == nil { - panic("static client is nil") - } - return &Client{client: c} } diff --git a/pkg/morph/client/netmap/client.go b/pkg/morph/client/netmap/client.go index e525acd934..569f38cf40 100644 --- a/pkg/morph/client/netmap/client.go +++ b/pkg/morph/client/netmap/client.go @@ -39,14 +39,8 @@ const ( ) // New creates, initializes and returns the Client instance. -// -// If StaticClient is nil, client.ErrNilStaticClient is returned. -func New(c *client.StaticClient) (*Client, error) { - if c == nil { - return nil, client.ErrNilStaticClient - } - - return &Client{client: c}, nil +func New(c *client.StaticClient) *Client { + return &Client{client: c} } // Morph returns raw morph client. diff --git a/pkg/morph/client/netmap/wrapper/wrapper.go b/pkg/morph/client/netmap/wrapper/wrapper.go index 05ad7f92da..9ce777cb37 100644 --- a/pkg/morph/client/netmap/wrapper/wrapper.go +++ b/pkg/morph/client/netmap/wrapper/wrapper.go @@ -48,14 +48,9 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8, return nil, fmt.Errorf("can't create netmap static client: %w", err) } - enhancedNetmapClient, err := netmap.New(staticClient) - if err != nil { - return nil, fmt.Errorf("can't create netmap morph client: %w", err) - } - return &Wrapper{ StaticClient: staticClient, - client: enhancedNetmapClient, + client: netmap.New(staticClient), }, nil } diff --git a/pkg/morph/client/reputation/client.go b/pkg/morph/client/reputation/client.go index f8758a3e78..a244b11c82 100644 --- a/pkg/morph/client/reputation/client.go +++ b/pkg/morph/client/reputation/client.go @@ -24,14 +24,8 @@ const ( ) // New creates, initializes and returns the Client instance. -// -// If StaticClient is nil, client.ErrNilStaticClient is returned. -func New(c *client.StaticClient) (*Client, error) { - if c == nil { - return nil, client.ErrNilStaticClient - } - - return &Client{client: c}, nil +func New(c *client.StaticClient) *Client { + return &Client{client: c} } // Morph returns raw morph client. diff --git a/pkg/morph/client/reputation/wrapper/wrapper.go b/pkg/morph/client/reputation/wrapper/wrapper.go index 6f5015bb48..c0d5bb67b8 100644 --- a/pkg/morph/client/reputation/wrapper/wrapper.go +++ b/pkg/morph/client/reputation/wrapper/wrapper.go @@ -65,13 +65,8 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8, return nil, fmt.Errorf("could not create static client of reputation contract: %w", err) } - enhancedRepurationClient, err := reputation.New(staticClient) - if err != nil { - return nil, fmt.Errorf("could not create reputation contract client: %w", err) - } - return &ClientWrapper{ StaticClient: staticClient, - client: enhancedRepurationClient, + client: reputation.New(staticClient), }, nil } diff --git a/pkg/morph/client/static.go b/pkg/morph/client/static.go index 3e13794b87..1df6c65126 100644 --- a/pkg/morph/client/static.go +++ b/pkg/morph/client/static.go @@ -1,7 +1,6 @@ package client import ( - "errors" "fmt" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" @@ -35,10 +34,6 @@ type staticOpts struct { // parameter of StaticClient. type StaticClientOption func(*staticOpts) -// ErrNilStaticClient is returned by functions that expect -// a non-nil StaticClient pointer, but received nil. -var ErrNilStaticClient = errors.New("static client is nil") - // NewStatic creates, initializes and returns the StaticClient instance. // // If provided Client instance is nil, ErrNilClient is returned.