From b52751e9929fc75640a979caa6cc97c943188ef5 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 21 May 2021 13:39:01 +0300 Subject: [PATCH] [#496] innerring/invoke: move wrapper structs to separate packages Signed-off-by: Evgenii Stratonikov --- pkg/innerring/innerring.go | 17 ++-- pkg/innerring/invoke/chain.go | 19 ---- pkg/innerring/invoke/enhanced.go | 88 ------------------- pkg/innerring/processors/audit/processor.go | 5 +- pkg/morph/client/audit/wrapper/wrapper.go | 13 +++ pkg/morph/client/balance/wrapper/wrapper.go | 19 ++++ pkg/morph/client/container/wrapper/wrapper.go | 20 +++++ pkg/morph/client/netmap/wrapper/wrapper.go | 19 ++++ .../client/reputation/wrapper/wrapper.go | 20 +++++ 9 files changed, 104 insertions(+), 116 deletions(-) delete mode 100644 pkg/innerring/invoke/chain.go delete mode 100644 pkg/innerring/invoke/enhanced.go diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index cf176f894..6f884044d 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -13,7 +13,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" crypto "github.com/nspcc-dev/neofs-crypto" "github.com/nspcc-dev/neofs-node/pkg/innerring/config" - "github.com/nspcc-dev/neofs-node/pkg/innerring/invoke" "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/alphabet" "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/audit" "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/balance" @@ -26,6 +25,10 @@ import ( auditSettlement "github.com/nspcc-dev/neofs-node/pkg/innerring/processors/settlement/audit" "github.com/nspcc-dev/neofs-node/pkg/morph/client" auditWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit/wrapper" + balanceWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance/wrapper" + cntWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" + nmWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper" + repWrapper "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/subscriber" "github.com/nspcc-dev/neofs-node/pkg/morph/timer" @@ -345,27 +348,29 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error return nil, err } - server.auditClient, err = invoke.NewAuditClient(server.morphClient, server.contracts.audit, server.feeConfig) + fee := server.feeConfig.SideChainFee() + + server.auditClient, err = auditWrapper.NewFromMorph(server.morphClient, server.contracts.audit, fee) if err != nil { return nil, err } - cnrClient, err := invoke.NewContainerClient(server.morphClient, server.contracts.container, server.feeConfig) + cnrClient, err := cntWrapper.NewFromMorph(server.morphClient, server.contracts.container, fee) if err != nil { return nil, err } - nmClient, err := invoke.NewNetmapClient(server.morphClient, server.contracts.netmap, server.feeConfig) + nmClient, err := nmWrapper.NewFromMorph(server.morphClient, server.contracts.netmap, fee) if err != nil { return nil, err } - balClient, err := invoke.NewBalanceClient(server.morphClient, server.contracts.balance, server.feeConfig) + balClient, err := balanceWrapper.NewFromMorph(server.morphClient, server.contracts.balance, fee) if err != nil { return nil, err } - repClient, err := invoke.NewReputationClient(server.morphClient, server.contracts.reputation, server.feeConfig) + repClient, err := repWrapper.NewFromMorph(server.morphClient, server.contracts.reputation, fee) if err != nil { return nil, err } diff --git a/pkg/innerring/invoke/chain.go b/pkg/innerring/invoke/chain.go deleted file mode 100644 index d774fff2c..000000000 --- a/pkg/innerring/invoke/chain.go +++ /dev/null @@ -1,19 +0,0 @@ -package invoke - -import ( - "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" -) - -type ( - // SideFeeProvider is an interface that used by invoker package method to - // get extra fee for all non notary smart contract invocations in side chain. - SideFeeProvider interface { - SideChainFee() fixedn.Fixed8 - } - - // MainFeeProvider is an interface that used by invoker package method to - // get extra fee for all non notary smart contract invocations in main chain. - MainFeeProvider interface { - MainChainFee() fixedn.Fixed8 - } -) diff --git a/pkg/innerring/invoke/enhanced.go b/pkg/innerring/invoke/enhanced.go deleted file mode 100644 index 29d631cf8..000000000 --- a/pkg/innerring/invoke/enhanced.go +++ /dev/null @@ -1,88 +0,0 @@ -package invoke - -import ( - "fmt" - - "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/audit" - auditWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/audit/wrapper" - "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance" - balanceWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/balance/wrapper" - morphContainer "github.com/nspcc-dev/neofs-node/pkg/morph/client/container" - wrapContainer "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" - morphNetmap "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap" - wrapNetmap "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper" - "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation" - reputationWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/reputation/wrapper" -) - -// NewContainerClient creates wrapper to access data from container contract. -func NewContainerClient(cli *client.Client, contract util.Uint160, fee SideFeeProvider) (*wrapContainer.Wrapper, error) { - staticClient, err := client.NewStatic(cli, contract, fee.SideChainFee()) - if err != nil { - return nil, fmt.Errorf("can't create container static client: %w", err) - } - - enhancedContainerClient, err := morphContainer.New(staticClient) - if err != nil { - return nil, fmt.Errorf("can't create container morph client: %w", err) - } - - return wrapContainer.New(enhancedContainerClient) -} - -// NewNetmapClient creates wrapper to access data from netmap contract. -func NewNetmapClient(cli *client.Client, contract util.Uint160, fee SideFeeProvider) (*wrapNetmap.Wrapper, error) { - staticClient, err := client.NewStatic(cli, contract, fee.SideChainFee()) - if err != nil { - return nil, fmt.Errorf("can't create netmap static client: %w", err) - } - - enhancedNetmapClient, err := morphNetmap.New(staticClient) - if err != nil { - return nil, fmt.Errorf("can't create netmap morph client: %w", err) - } - - return wrapNetmap.New(enhancedNetmapClient) -} - -// NewAuditClient creates wrapper to work with Audit contract. -func NewAuditClient(cli *client.Client, contract util.Uint160, fee SideFeeProvider) (*auditWrapper.ClientWrapper, error) { - staticClient, err := client.NewStatic(cli, contract, fee.SideChainFee()) - if err != nil { - return nil, err - } - - return auditWrapper.WrapClient(audit.New(staticClient)), nil -} - -// NewBalanceClient creates wrapper to work with Balance contract. -func NewBalanceClient(cli *client.Client, contract util.Uint160, fee SideFeeProvider) (*balanceWrapper.Wrapper, error) { - staticClient, err := client.NewStatic(cli, contract, fee.SideChainFee()) - if err != nil { - 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 balanceWrapper.New(enhancedBalanceClient) -} - -// NewReputationClient creates wrapper to work with reputation contract. -func NewReputationClient(cli *client.Client, contract util.Uint160, fee SideFeeProvider) (*reputationWrapper.ClientWrapper, error) { - staticClient, err := client.NewStatic(cli, contract, fee.SideChainFee()) - if err != nil { - 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 reputationWrapper.WrapClient(enhancedRepurationClient), nil -} diff --git a/pkg/innerring/processors/audit/processor.go b/pkg/innerring/processors/audit/processor.go index 1bdde8d96..06b92e239 100644 --- a/pkg/innerring/processors/audit/processor.go +++ b/pkg/innerring/processors/audit/processor.go @@ -10,7 +10,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/util" SDKClient "github.com/nspcc-dev/neofs-api-go/pkg/client" "github.com/nspcc-dev/neofs-node/pkg/innerring/config" - "github.com/nspcc-dev/neofs-node/pkg/innerring/invoke" "github.com/nspcc-dev/neofs-node/pkg/morph/client" wrapContainer "github.com/nspcc-dev/neofs-node/pkg/morph/client/container/wrapper" wrapNetmap "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper" @@ -115,13 +114,13 @@ func New(p *Params) (*Processor, error) { } // creating enhanced client for getting network map - netmapClient, err := invoke.NewNetmapClient(p.MorphClient, p.NetmapContract, p.FeeProvider) + netmapClient, err := wrapNetmap.NewFromMorph(p.MorphClient, p.NetmapContract, p.FeeProvider.SideChainFee()) if err != nil { return nil, err } // creating enhanced client for getting containers - containerClient, err := invoke.NewContainerClient(p.MorphClient, p.ContainerContract, p.FeeProvider) + containerClient, err := wrapContainer.NewFromMorph(p.MorphClient, p.ContainerContract, p.FeeProvider.SideChainFee()) if err != nil { return nil, err } diff --git a/pkg/morph/client/audit/wrapper/wrapper.go b/pkg/morph/client/audit/wrapper/wrapper.go index 3236a1f51..7480baf5a 100644 --- a/pkg/morph/client/audit/wrapper/wrapper.go +++ b/pkg/morph/client/audit/wrapper/wrapper.go @@ -1,6 +1,9 @@ package audit import ( + "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" + "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/audit" ) @@ -12,3 +15,13 @@ type ClientWrapper audit.Client 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) + if err != nil { + return nil, err + } + + return WrapClient(audit.New(staticClient)), nil +} diff --git a/pkg/morph/client/balance/wrapper/wrapper.go b/pkg/morph/client/balance/wrapper/wrapper.go index 317b1deb1..675620288 100644 --- a/pkg/morph/client/balance/wrapper/wrapper.go +++ b/pkg/morph/client/balance/wrapper/wrapper.go @@ -2,7 +2,11 @@ package wrapper import ( "errors" + "fmt" + "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" + "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/balance" ) @@ -41,3 +45,18 @@ func New(c *Client) (*Wrapper, error) { 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) + if err != nil { + 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 New(enhancedBalanceClient) +} diff --git a/pkg/morph/client/container/wrapper/wrapper.go b/pkg/morph/client/container/wrapper/wrapper.go index e7c469b12..883a38a3f 100644 --- a/pkg/morph/client/container/wrapper/wrapper.go +++ b/pkg/morph/client/container/wrapper/wrapper.go @@ -1,6 +1,11 @@ package wrapper import ( + "fmt" + + "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" + "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/container" ) @@ -34,3 +39,18 @@ func New(c *Client) (*Wrapper, error) { 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) + if err != nil { + 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 New(enhancedContainerClient) +} diff --git a/pkg/morph/client/netmap/wrapper/wrapper.go b/pkg/morph/client/netmap/wrapper/wrapper.go index 5a8eabf75..0a9e9e3bf 100644 --- a/pkg/morph/client/netmap/wrapper/wrapper.go +++ b/pkg/morph/client/netmap/wrapper/wrapper.go @@ -2,7 +2,11 @@ package wrapper import ( "errors" + "fmt" + "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" + "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" ) @@ -41,3 +45,18 @@ func New(c *Client) (*Wrapper, error) { 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) + if err != nil { + 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 New(enhancedNetmapClient) +} diff --git a/pkg/morph/client/reputation/wrapper/wrapper.go b/pkg/morph/client/reputation/wrapper/wrapper.go index 2a790ce84..6836a7844 100644 --- a/pkg/morph/client/reputation/wrapper/wrapper.go +++ b/pkg/morph/client/reputation/wrapper/wrapper.go @@ -1,6 +1,11 @@ package wrapper import ( + "fmt" + + "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" + "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/reputation" ) @@ -12,3 +17,18 @@ type ClientWrapper reputation.Client 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) + if err != nil { + 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 WrapClient(enhancedRepurationClient), nil +}