From 16e9e726ff087efd571c2f22e6a37aad412f9c0b Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 1 Jun 2021 14:35:53 +0300 Subject: [PATCH] [#496] morph/client: add wrapper for neofs contract Signed-off-by: Evgenii Stratonikov --- pkg/innerring/innerring.go | 14 +++++++----- .../processors/balance/process_assets.go | 4 +--- pkg/innerring/processors/balance/processor.go | 19 ++++------------ .../processors/governance/process_update.go | 5 +---- .../processors/governance/processor.go | 22 +++++++------------ pkg/morph/client/neofs/cheque.go | 16 ++++++++++++++ pkg/morph/client/neofs/client.go | 14 ++++++++---- pkg/morph/client/neofs/wrapper/cheque.go | 17 ++++++++++++++ 8 files changed, 66 insertions(+), 45 deletions(-) create mode 100644 pkg/morph/client/neofs/cheque.go create mode 100644 pkg/morph/client/neofs/wrapper/cheque.go diff --git a/pkg/innerring/innerring.go b/pkg/innerring/innerring.go index f8f6ced88..f8e6076ac 100644 --- a/pkg/innerring/innerring.go +++ b/pkg/innerring/innerring.go @@ -27,6 +27,7 @@ import ( 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" + neofsWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs/wrapper" neofsid "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofsid/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" @@ -417,6 +418,12 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error return nil, err } + neofsClient, err := neofsWrapper.NewFromMorph(server.mainnetClient, server.contracts.neofs, + server.feeConfig.MainChainFee()) + if err != nil { + return nil, err + } + // create global runtime config reader globalConfig := config.NewGlobalConfigReader(cfg, server.netmapClient) @@ -517,7 +524,7 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error // create governance processor governanceProcessor, err := governance.New(&governance.Params{ Log: log, - NeoFSContract: server.contracts.neofs, + NeoFSClient: neofsClient, NetmapClient: server.netmapClient, AlphabetState: server, EpochState: server, @@ -525,7 +532,6 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error MorphClient: server.morphClient, MainnetClient: server.mainnetClient, NotaryDisabled: server.sideNotaryConfig.disabled, - FeeProvider: server.feeConfig, }) if err != nil { return nil, err @@ -598,12 +604,10 @@ func New(ctx context.Context, log *zap.Logger, cfg *viper.Viper) (*Server, error balanceProcessor, err := balance.New(&balance.Params{ Log: log, PoolSize: cfg.GetInt("workers.balance"), - NeoFSContract: server.contracts.neofs, + NeoFSClient: neofsClient, BalanceContract: server.contracts.balance, - MainnetClient: server.mainnetClient, AlphabetState: server, Converter: &server.precision, - FeeProvider: server.feeConfig, }) if err != nil { return nil, err diff --git a/pkg/innerring/processors/balance/process_assets.go b/pkg/innerring/processors/balance/process_assets.go index fd940f79e..5ee16cdf9 100644 --- a/pkg/innerring/processors/balance/process_assets.go +++ b/pkg/innerring/processors/balance/process_assets.go @@ -5,8 +5,6 @@ import ( "go.uber.org/zap" ) -const chequeMethod = "cheque" - // Process lock event by invoking Cheque method in main net to send assets // back to the withdraw issuer. func (bp *Processor) processLock(lock *balanceEvent.Lock) { @@ -15,7 +13,7 @@ func (bp *Processor) processLock(lock *balanceEvent.Lock) { return } - err := bp.mainnetClient.NotaryInvoke(bp.neofsContract, bp.feeProvider.MainChainFee(), chequeMethod, + err := bp.neofsClient.Cheque( lock.ID(), lock.User(), bp.converter.ToFixed8(lock.Amount()), diff --git a/pkg/innerring/processors/balance/processor.go b/pkg/innerring/processors/balance/processor.go index ca6b6d74d..49712d00f 100644 --- a/pkg/innerring/processors/balance/processor.go +++ b/pkg/innerring/processors/balance/processor.go @@ -5,8 +5,7 @@ import ( "fmt" "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neofs-node/pkg/innerring/config" - "github.com/nspcc-dev/neofs-node/pkg/morph/client" + neofscontract "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs/wrapper" "github.com/nspcc-dev/neofs-node/pkg/morph/event" balanceEvent "github.com/nspcc-dev/neofs-node/pkg/morph/event/balance" "github.com/panjf2000/ants/v2" @@ -28,24 +27,20 @@ type ( Processor struct { log *zap.Logger pool *ants.Pool - neofsContract util.Uint160 + neofsClient *neofscontract.ClientWrapper balanceContract util.Uint160 - mainnetClient *client.Client alphabetState AlphabetState converter PrecisionConverter - feeProvider *config.FeeConfig } // Params of the processor constructor. Params struct { Log *zap.Logger PoolSize int - NeoFSContract util.Uint160 + NeoFSClient *neofscontract.ClientWrapper BalanceContract util.Uint160 - MainnetClient *client.Client AlphabetState AlphabetState Converter PrecisionConverter - FeeProvider *config.FeeConfig } ) @@ -58,14 +53,10 @@ func New(p *Params) (*Processor, error) { switch { case p.Log == nil: return nil, errors.New("ir/balance: logger is not set") - case p.MainnetClient == nil: - return nil, errors.New("ir/balance: neo:mainnet client is not set") case p.AlphabetState == nil: return nil, errors.New("ir/balance: global state is not set") case p.Converter == nil: return nil, errors.New("ir/balance: balance precision converter is not set") - case p.FeeProvider == nil: - return nil, errors.New("ir/balance: fee provider is not set") } p.Log.Debug("balance worker pool", zap.Int("size", p.PoolSize)) @@ -78,12 +69,10 @@ func New(p *Params) (*Processor, error) { return &Processor{ log: p.Log, pool: pool, - neofsContract: p.NeoFSContract, + neofsClient: p.NeoFSClient, balanceContract: p.BalanceContract, - mainnetClient: p.MainnetClient, alphabetState: p.AlphabetState, converter: p.Converter, - feeProvider: p.FeeProvider, }, nil } diff --git a/pkg/innerring/processors/governance/process_update.go b/pkg/innerring/processors/governance/process_update.go index 11cfa344f..9aa25ab3d 100644 --- a/pkg/innerring/processors/governance/process_update.go +++ b/pkg/innerring/processors/governance/process_update.go @@ -9,8 +9,6 @@ import ( const ( alphabetUpdateIDPrefix = "AlphabetUpdate" - - alphabetUpdateMethod = "alphabetUpdate" ) func (gp *Processor) processAlphabetSync() { @@ -98,8 +96,7 @@ func (gp *Processor) processAlphabetSync() { id := append([]byte(alphabetUpdateIDPrefix), buf...) - err = gp.mainnetClient.NotaryInvoke(gp.neofsContract, gp.feeProvider.MainChainFee(), alphabetUpdateMethod, - id, newAlphabet) + err = gp.neofsClient.AlphabetUpdate(id, newAlphabet) if err != nil { gp.log.Error("can't update list of alphabet nodes in neofs contract", zap.String("error", err.Error())) diff --git a/pkg/innerring/processors/governance/processor.go b/pkg/innerring/processors/governance/processor.go index d8fa00d47..5b36bc7e8 100644 --- a/pkg/innerring/processors/governance/processor.go +++ b/pkg/innerring/processors/governance/processor.go @@ -6,9 +6,8 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/native" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" - "github.com/nspcc-dev/neo-go/pkg/util" - "github.com/nspcc-dev/neofs-node/pkg/innerring/config" "github.com/nspcc-dev/neofs-node/pkg/morph/client" + neofscontract "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs/wrapper" nmWrapper "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap/wrapper" "github.com/nspcc-dev/neofs-node/pkg/morph/event" "github.com/nspcc-dev/neofs-node/pkg/morph/event/rolemanagement" @@ -39,10 +38,10 @@ type ( // Processor of events related to governance in the network. Processor struct { - log *zap.Logger - pool *ants.Pool - neofsContract util.Uint160 - netmapClient *nmWrapper.Wrapper + log *zap.Logger + pool *ants.Pool + neofsClient *neofscontract.ClientWrapper + netmapClient *nmWrapper.Wrapper alphabetState AlphabetState epochState EpochState @@ -52,13 +51,11 @@ type ( morphClient *client.Client notaryDisabled bool - feeProvider *config.FeeConfig } // Params of the processor constructor. Params struct { - Log *zap.Logger - NeoFSContract util.Uint160 + Log *zap.Logger AlphabetState AlphabetState EpochState EpochState @@ -66,10 +63,10 @@ type ( MorphClient *client.Client MainnetClient *client.Client + NeoFSClient *neofscontract.ClientWrapper NetmapClient *nmWrapper.Wrapper NotaryDisabled bool - FeeProvider *config.FeeConfig } ) @@ -88,8 +85,6 @@ func New(p *Params) (*Processor, error) { return nil, errors.New("ir/governance: global state is not set") case p.Voter == nil: return nil, errors.New("ir/governance: global state is not set") - case p.FeeProvider == nil: - return nil, errors.New("ir/governance: fee provider is not set") } pool, err := ants.NewPool(ProcessorPoolSize, ants.WithNonblocking(true)) @@ -100,7 +95,7 @@ func New(p *Params) (*Processor, error) { return &Processor{ log: p.Log, pool: pool, - neofsContract: p.NeoFSContract, + neofsClient: p.NeoFSClient, netmapClient: p.NetmapClient, alphabetState: p.AlphabetState, epochState: p.EpochState, @@ -108,7 +103,6 @@ func New(p *Params) (*Processor, error) { mainnetClient: p.MainnetClient, morphClient: p.MorphClient, notaryDisabled: p.NotaryDisabled, - feeProvider: p.FeeProvider, }, nil } diff --git a/pkg/morph/client/neofs/cheque.go b/pkg/morph/client/neofs/cheque.go new file mode 100644 index 000000000..6d844cb53 --- /dev/null +++ b/pkg/morph/client/neofs/cheque.go @@ -0,0 +1,16 @@ +package neofscontract + +import ( + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + "github.com/nspcc-dev/neo-go/pkg/util" +) + +// Cheque invokes `cheque` method of NeoFS contract. +func (x *Client) Cheque(id []byte, user util.Uint160, amount int64, lock util.Uint160) error { + return x.client.Invoke(x.chequeMethod, id, user.BytesBE(), amount, lock.BytesBE()) +} + +// AlphabetUpdate update list of alphabet nodes. +func (x *Client) AlphabetUpdate(id []byte, pubs keys.PublicKeys) error { + return x.client.Invoke(x.alphabetUpdateMethod, id, pubs) +} diff --git a/pkg/morph/client/neofs/client.go b/pkg/morph/client/neofs/client.go index 60b7aea8f..2fef77c28 100644 --- a/pkg/morph/client/neofs/client.go +++ b/pkg/morph/client/neofs/client.go @@ -22,19 +22,25 @@ type Client struct { type Option func(*cfg) type cfg struct { + alphabetUpdateMethod, + chequeMethod, bindKeysMethod, unbindKeysMethod string } func defaultConfig() *cfg { const ( - defaultBindKeysMethod = "bind" - defaultUnbindKeysMethod = "unbind" + defaultBindKeysMethod = "bind" + defaultUnbindKeysMethod = "unbind" + defaultAlphabetUpdateMethod = "alphabetUpdate" + defaultChequeMethod = "cheque" ) return &cfg{ - bindKeysMethod: defaultBindKeysMethod, - unbindKeysMethod: defaultUnbindKeysMethod, + alphabetUpdateMethod: defaultAlphabetUpdateMethod, + chequeMethod: defaultChequeMethod, + bindKeysMethod: defaultBindKeysMethod, + unbindKeysMethod: defaultUnbindKeysMethod, } } diff --git a/pkg/morph/client/neofs/wrapper/cheque.go b/pkg/morph/client/neofs/wrapper/cheque.go new file mode 100644 index 000000000..c03b8f7da --- /dev/null +++ b/pkg/morph/client/neofs/wrapper/cheque.go @@ -0,0 +1,17 @@ +package neofscontract + +import ( + "github.com/nspcc-dev/neo-go/pkg/crypto/keys" + "github.com/nspcc-dev/neo-go/pkg/util" + neofscontract "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs" +) + +// Cheque invokes `cheque` method of NeoFS contract. +func (x *ClientWrapper) Cheque(id []byte, user util.Uint160, amount int64, lock util.Uint160) error { + return (*neofscontract.Client)(x).Cheque(id, user, amount, lock) +} + +// AlphabetUpdate update list of alphabet nodes. +func (x *ClientWrapper) AlphabetUpdate(id []byte, pubs keys.PublicKeys) error { + return (*neofscontract.Client)(x).AlphabetUpdate(id, pubs) +}