From 3114be39d03d7a51f4336ba90cba5b4d2d350e4b Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Wed, 10 Nov 2021 13:41:27 +0300 Subject: [PATCH] [#971] morph/neofs: Add optional parameters Add optional parameters to the client call signature. Group parameters of a client call into struct to improve future codebase support. Signed-off-by: Pavel Karpy --- pkg/morph/client/neofs/bind.go | 9 +++ pkg/morph/client/neofs/cheque.go | 59 +++++++++++++++++-- pkg/morph/client/neofs/wrapper/bind.go | 35 ++++++++++-- pkg/morph/client/neofs/wrapper/cheque.go | 72 ++++++++++++++++++++++-- 4 files changed, 163 insertions(+), 12 deletions(-) diff --git a/pkg/morph/client/neofs/bind.go b/pkg/morph/client/neofs/bind.go index 8f3757189..4879a732c 100644 --- a/pkg/morph/client/neofs/bind.go +++ b/pkg/morph/client/neofs/bind.go @@ -22,6 +22,13 @@ type commonBindArgs struct { scriptHash []byte // script hash of account identifier keys [][]byte // list of serialized public keys + + client.InvokePrmOptional +} + +// SetOptionalPrm sets optional client parameters. +func (x *commonBindArgs) SetOptionalPrm(op client.InvokePrmOptional) { + x.InvokePrmOptional = op } // SetScriptHash sets script hash of the NeoFS account identifier. @@ -41,6 +48,7 @@ func (x *Client) BindKeys(args BindKeysArgs) error { prm.SetMethod(x.bindKeysMethod) prm.SetArgs(args.scriptHash, args.keys) + prm.InvokePrmOptional = args.InvokePrmOptional err := x.client.Invoke(prm) if err != nil { @@ -57,6 +65,7 @@ func (x *Client) UnbindKeys(args UnbindKeysArgs) error { prm.SetMethod(x.unbindKeysMethod) prm.SetArgs(args.scriptHash, args.keys) + prm.InvokePrmOptional = args.InvokePrmOptional err := x.client.Invoke(prm) if err != nil { diff --git a/pkg/morph/client/neofs/cheque.go b/pkg/morph/client/neofs/cheque.go index a2fa03697..1c2f3445c 100644 --- a/pkg/morph/client/neofs/cheque.go +++ b/pkg/morph/client/neofs/cheque.go @@ -6,22 +6,73 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/morph/client" ) +// ChequePrm groups the arguments of Cheque operation. +type ChequePrm struct { + id []byte + user util.Uint160 + amount int64 + lock util.Uint160 + + client.InvokePrmOptional +} + +// SetID sets ID of the cheque. +func (c *ChequePrm) SetID(id []byte) { + c.id = id +} + +// SetUser sets user. +func (c *ChequePrm) SetUser(user util.Uint160) { + c.user = user +} + +// SetAmount sets amount. +func (c *ChequePrm) SetAmount(amount int64) { + c.amount = amount +} + +// SetLock sets lock. +func (c *ChequePrm) SetLock(lock util.Uint160) { + c.lock = lock +} + // Cheque invokes `cheque` method of NeoFS contract. -func (x *Client) Cheque(id []byte, user util.Uint160, amount int64, lock util.Uint160) error { +func (x *Client) Cheque(args ChequePrm) error { prm := client.InvokePrm{} prm.SetMethod(x.chequeMethod) - prm.SetArgs(id, user.BytesBE(), amount, lock.BytesBE()) + prm.SetArgs(args.id, args.user.BytesBE(), args.amount, args.lock.BytesBE()) + prm.InvokePrmOptional = args.InvokePrmOptional return x.client.Invoke(prm) } +// AlphabetUpdatePrm groups the arguments +// of alphabet nodes update invocation call. +type AlphabetUpdatePrm struct { + id []byte + pubs keys.PublicKeys + + client.InvokePrmOptional +} + +// SetID sets update ID. +func (a *AlphabetUpdatePrm) SetID(id []byte) { + a.id = id +} + +// SetPubs sets new alphabet public keys. +func (a *AlphabetUpdatePrm) SetPubs(pubs keys.PublicKeys) { + a.pubs = pubs +} + // AlphabetUpdate update list of alphabet nodes. -func (x *Client) AlphabetUpdate(id []byte, pubs keys.PublicKeys) error { +func (x *Client) AlphabetUpdate(args AlphabetUpdatePrm) error { prm := client.InvokePrm{} prm.SetMethod(x.alphabetUpdateMethod) - prm.SetArgs(id, pubs) + prm.SetArgs(args.id, args.pubs) + prm.InvokePrmOptional = args.InvokePrmOptional return x.client.Invoke(prm) } diff --git a/pkg/morph/client/neofs/wrapper/bind.go b/pkg/morph/client/neofs/wrapper/bind.go index df793cad5..8410d65c9 100644 --- a/pkg/morph/client/neofs/wrapper/bind.go +++ b/pkg/morph/client/neofs/wrapper/bind.go @@ -1,14 +1,40 @@ package neofscontract import ( + "github.com/nspcc-dev/neofs-node/pkg/morph/client" neofscontract "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs" ) +// ManageKeysPrm groups parameters of ManageKeys operation. +type ManageKeysPrm struct { + scriptHash []byte + ks [][]byte + bind bool + + client.InvokePrmOptional +} + +// SetScriptHash sets script hash. +func (m *ManageKeysPrm) SetScriptHash(scriptHash []byte) { + m.scriptHash = scriptHash +} + +// SetKeys sets keys. +func (m *ManageKeysPrm) SetKeys(ks [][]byte) { + m.ks = ks +} + +// SetBind sets operation type: bind/unbind. +func (m *ManageKeysPrm) SetBind(bind bool) { + m.bind = bind +} + // ManageKeys binds/unbinds list of public keys from NeoFS account by script hash. -func (x *ClientWrapper) ManageKeys(scriptHash []byte, ks [][]byte, bind bool) error { +func (x *ClientWrapper) ManageKeys(prm ManageKeysPrm) error { type args interface { SetScriptHash([]byte) SetKeys([][]byte) + SetOptionalPrm(optional client.InvokePrmOptional) } var ( @@ -16,7 +42,7 @@ func (x *ClientWrapper) ManageKeys(scriptHash []byte, ks [][]byte, bind bool) er call func(args) error ) - if bind { + if prm.bind { a = new(neofscontract.BindKeysArgs) call = func(a args) error { return x.client.BindKeys(*a.(*neofscontract.BindKeysArgs)) @@ -28,8 +54,9 @@ func (x *ClientWrapper) ManageKeys(scriptHash []byte, ks [][]byte, bind bool) er } } - a.SetScriptHash(scriptHash) - a.SetKeys(ks) + a.SetScriptHash(prm.scriptHash) + a.SetKeys(prm.ks) + a.SetOptionalPrm(prm.InvokePrmOptional) return call(a) } diff --git a/pkg/morph/client/neofs/wrapper/cheque.go b/pkg/morph/client/neofs/wrapper/cheque.go index 955449d64..7b499de8c 100644 --- a/pkg/morph/client/neofs/wrapper/cheque.go +++ b/pkg/morph/client/neofs/wrapper/cheque.go @@ -3,14 +3,78 @@ package neofscontract import ( "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/nspcc-dev/neofs-node/pkg/morph/client" + neofscontract "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs" ) +// ChequePrm groups parameters of AlphabetUpdate operation. +type ChequePrm struct { + id []byte + user util.Uint160 + amount int64 + lock util.Uint160 + + client.InvokePrmOptional +} + +// SetID sets ID of the cheque. +func (c *ChequePrm) SetID(id []byte) { + c.id = id +} + +// SetUser sets user. +func (c *ChequePrm) SetUser(user util.Uint160) { + c.user = user +} + +// SetAmount sets amount. +func (c *ChequePrm) SetAmount(amount int64) { + c.amount = amount +} + +// SetLock sets lock. +func (c *ChequePrm) SetLock(lock util.Uint160) { + c.lock = lock +} + // Cheque invokes `cheque` method of NeoFS contract. -func (x *ClientWrapper) Cheque(id []byte, user util.Uint160, amount int64, lock util.Uint160) error { - return x.client.Cheque(id, user, amount, lock) +func (x *ClientWrapper) Cheque(prm ChequePrm) error { + args := neofscontract.ChequePrm{} + + args.SetID(prm.id) + args.SetUser(prm.user) + args.SetAmount(prm.amount) + args.SetLock(prm.lock) + args.InvokePrmOptional = prm.InvokePrmOptional + + return x.client.Cheque(args) +} + +// AlphabetUpdatePrm groups parameters of AlphabetUpdate operation. +type AlphabetUpdatePrm struct { + id []byte + pubs keys.PublicKeys + + client.InvokePrmOptional +} + +// SetID sets update ID. +func (a *AlphabetUpdatePrm) SetID(id []byte) { + a.id = id +} + +// SetPubs sets new alphabet public keys. +func (a *AlphabetUpdatePrm) SetPubs(pubs keys.PublicKeys) { + a.pubs = pubs } // AlphabetUpdate update list of alphabet nodes. -func (x *ClientWrapper) AlphabetUpdate(id []byte, pubs keys.PublicKeys) error { - return x.client.AlphabetUpdate(id, pubs) +func (x *ClientWrapper) AlphabetUpdate(prm AlphabetUpdatePrm) error { + args := neofscontract.AlphabetUpdatePrm{} + + args.SetID(prm.id) + args.SetPubs(prm.pubs) + args.InvokePrmOptional = prm.InvokePrmOptional + + return x.client.AlphabetUpdate(args) }