[#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 <carpawell@nspcc.ru>
This commit is contained in:
Pavel Karpy 2021-11-10 13:41:27 +03:00 committed by Alex Vanin
parent 1db6d316c2
commit 3114be39d0
4 changed files with 163 additions and 12 deletions

View file

@ -22,6 +22,13 @@ type commonBindArgs struct {
scriptHash []byte // script hash of account identifier scriptHash []byte // script hash of account identifier
keys [][]byte // list of serialized public keys 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. // 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.SetMethod(x.bindKeysMethod)
prm.SetArgs(args.scriptHash, args.keys) prm.SetArgs(args.scriptHash, args.keys)
prm.InvokePrmOptional = args.InvokePrmOptional
err := x.client.Invoke(prm) err := x.client.Invoke(prm)
if err != nil { if err != nil {
@ -57,6 +65,7 @@ func (x *Client) UnbindKeys(args UnbindKeysArgs) error {
prm.SetMethod(x.unbindKeysMethod) prm.SetMethod(x.unbindKeysMethod)
prm.SetArgs(args.scriptHash, args.keys) prm.SetArgs(args.scriptHash, args.keys)
prm.InvokePrmOptional = args.InvokePrmOptional
err := x.client.Invoke(prm) err := x.client.Invoke(prm)
if err != nil { if err != nil {

View file

@ -6,22 +6,73 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/morph/client" "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. // 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 := client.InvokePrm{}
prm.SetMethod(x.chequeMethod) 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) 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. // 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 := client.InvokePrm{}
prm.SetMethod(x.alphabetUpdateMethod) prm.SetMethod(x.alphabetUpdateMethod)
prm.SetArgs(id, pubs) prm.SetArgs(args.id, args.pubs)
prm.InvokePrmOptional = args.InvokePrmOptional
return x.client.Invoke(prm) return x.client.Invoke(prm)
} }

View file

@ -1,14 +1,40 @@
package neofscontract package neofscontract
import ( import (
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
neofscontract "github.com/nspcc-dev/neofs-node/pkg/morph/client/neofs" 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. // 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 { type args interface {
SetScriptHash([]byte) SetScriptHash([]byte)
SetKeys([][]byte) SetKeys([][]byte)
SetOptionalPrm(optional client.InvokePrmOptional)
} }
var ( var (
@ -16,7 +42,7 @@ func (x *ClientWrapper) ManageKeys(scriptHash []byte, ks [][]byte, bind bool) er
call func(args) error call func(args) error
) )
if bind { if prm.bind {
a = new(neofscontract.BindKeysArgs) a = new(neofscontract.BindKeysArgs)
call = func(a args) error { call = func(a args) error {
return x.client.BindKeys(*a.(*neofscontract.BindKeysArgs)) 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.SetScriptHash(prm.scriptHash)
a.SetKeys(ks) a.SetKeys(prm.ks)
a.SetOptionalPrm(prm.InvokePrmOptional)
return call(a) return call(a)
} }

View file

@ -3,14 +3,78 @@ package neofscontract
import ( import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neo-go/pkg/util" "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. // Cheque invokes `cheque` method of NeoFS contract.
func (x *ClientWrapper) Cheque(id []byte, user util.Uint160, amount int64, lock util.Uint160) error { func (x *ClientWrapper) Cheque(prm ChequePrm) error {
return x.client.Cheque(id, user, amount, lock) 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. // AlphabetUpdate update list of alphabet nodes.
func (x *ClientWrapper) AlphabetUpdate(id []byte, pubs keys.PublicKeys) error { func (x *ClientWrapper) AlphabetUpdate(prm AlphabetUpdatePrm) error {
return x.client.AlphabetUpdate(id, pubs) args := neofscontract.AlphabetUpdatePrm{}
args.SetID(prm.id)
args.SetPubs(prm.pubs)
args.InvokePrmOptional = prm.InvokePrmOptional
return x.client.AlphabetUpdate(args)
} }