[#625] morph/client: make method names constant

We don't use custom names and the only place where custom method option
is used it provides the default name and can be omitted.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-01-29 16:06:36 +03:00 committed by LeL
parent 35dec2f494
commit 3c5b62d839
44 changed files with 249 additions and 1097 deletions

View file

@ -14,133 +14,18 @@ import (
// and can lead to panic.
type Client struct {
client *client.StaticClient // static Audit contract client
*cfg // contract method names
}
// Option is a client configuration change function.
type Option func(*cfg)
type cfg struct {
putResultMethod, // put audit result method name for invocation
getResultMethod, // get audit result method name for invocation
listResultsMethod string // list all audit result IDs method name for invocation
listByEpochResultsMethod string // list audit result IDs by epoch method name for invocation
listByCIDResultsMethod string // list audit result IDs by epoch and CID method name for invocation
listByNodeResultsMethod string // list audit result IDs by epoch, CID, and node key method name for invocation
}
const (
defaultPutResultMethod = "put" // default "put audit result" method name
defaultGetResultMethod = "get" // default "get audit result" method name
defaultListResultsMethod = "list" // default "list all audit result IDs" method name
defaultListByEpochResultsMethod = "listByEpoch" // default "list audit result IDs by epoch" method name
defaultListByCIDResultsMethod = "listByCID" // default "list audit result IDs by epoch and CID" method name
defaultListByNodeResultsMethod = "listByNode" // default "list audit result IDs by epoch, CID and node key" method name
putResultMethod = "put"
getResultMethod = "get"
listResultsMethod = "list"
listByEpochResultsMethod = "listByEpoch"
listByCIDResultsMethod = "listByCID"
listByNodeResultsMethod = "listByNode"
)
func defaultConfig() *cfg {
return &cfg{
putResultMethod: defaultPutResultMethod,
getResultMethod: defaultGetResultMethod,
listResultsMethod: defaultListResultsMethod,
listByEpochResultsMethod: defaultListByEpochResultsMethod,
listByCIDResultsMethod: defaultListByCIDResultsMethod,
listByNodeResultsMethod: defaultListByNodeResultsMethod,
}
}
// New creates, initializes and returns the Client instance.
//
// Other values are set according to provided options, or by default:
// * "put audit result" method name: put;
// * "list audit results" method name: list.
//
// If desired option satisfies the default value, it can be omitted.
// If multiple options of the same config value are supplied,
// the option with the highest index in the arguments will be used.
func New(c *client.StaticClient, opts ...Option) *Client {
res := &Client{
client: c,
cfg: defaultConfig(), // build default configuration
}
// apply options
for _, opt := range opts {
opt(res.cfg)
}
return res
}
// WithPutAuditResultMethod returns a client constructor option that
// specifies the "put audit result" method name.
//
// Ignores empty value.
func WithPutAuditResultMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.putResultMethod = n
}
}
}
// WithGetAuditResultMethod returns a client constructor option that
// specifies the "get audit result" method name.
//
// Ignores empty value.
func WithGetAuditResultMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.getResultMethod = n
}
}
}
// WithListResultsMethod returns a client constructor option that
// specifies the "list all audit result IDs" method name.
//
// Ignores empty value.
func WithListResultsMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.listResultsMethod = n
}
}
}
// WithListByEpochResultsMethod returns a client constructor option that
// specifies the "list audit result IDs by epoch" method name.
//
// Ignores empty value.
func WithListByEpochResultsMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.listByEpochResultsMethod = n
}
}
}
// WithListByCIDResultsMethod returns a client constructor option that
// specifies the "list audit result IDs by epoch and CID" method name.
//
// Ignores empty value.
func WithListByCIDResultsMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.listByCIDResultsMethod = n
}
}
}
// WithListByNodeResultsMethod returns a client constructor option that
// specifies the "list audit result IDs by epoch, CID, and node key" method name.
//
// Ignores empty value.
func WithListByNodeResultsMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.listByNodeResultsMethod = n
}
}
func New(c *client.StaticClient) *Client {
return &Client{client: c}
}

View file

@ -33,19 +33,19 @@ func (v *GetAuditResultValue) Result() []byte {
func (c *Client) GetAuditResult(args GetAuditResultArgs) (*GetAuditResultValue, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.getResultMethod)
invokePrm.SetMethod(getResultMethod)
invokePrm.SetArgs(args.id)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.getResultMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", getResultMethod, err)
} else if ln := len(prms); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", c.getResultMethod, ln)
return nil, fmt.Errorf("unexpected stack item count (%s): %d", getResultMethod, ln)
}
resultBytes, err := client.BytesFromStackItem(prms[0])
if err != nil {
return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", c.getResultMethod, err)
return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", getResultMethod, err)
}
return &GetAuditResultValue{

View file

@ -65,14 +65,14 @@ func (v *ListResultsByNodeArgs) SetNodeKey(key []byte) {
func (c *Client) ListAuditResults(args ListResultsArgs) (*ListResultsValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.listResultsMethod)
invokePrm.SetMethod(listResultsMethod)
items, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.listResultsMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", listResultsMethod, err)
}
return parseAuditResults(items, c.listResultsMethod)
return parseAuditResults(items, listResultsMethod)
}
// ListAuditResultsByEpoch performs the test invoke of "list audit result IDs
@ -80,15 +80,15 @@ func (c *Client) ListAuditResults(args ListResultsArgs) (*ListResultsValues, err
func (c *Client) ListAuditResultsByEpoch(args ListResultsByEpochArgs) (*ListResultsValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.listByEpochResultsMethod)
invokePrm.SetMethod(listByEpochResultsMethod)
invokePrm.SetArgs(args.epoch)
items, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.listByEpochResultsMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", listByEpochResultsMethod, err)
}
return parseAuditResults(items, c.listByEpochResultsMethod)
return parseAuditResults(items, listByEpochResultsMethod)
}
// ListAuditResultsByCID performs the test invoke of "list audit result IDs
@ -96,15 +96,15 @@ func (c *Client) ListAuditResultsByEpoch(args ListResultsByEpochArgs) (*ListResu
func (c *Client) ListAuditResultsByCID(args ListResultsByCIDArgs) (*ListResultsValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.listByCIDResultsMethod)
invokePrm.SetMethod(listByCIDResultsMethod)
invokePrm.SetArgs(args.epoch, args.cid)
items, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.listByCIDResultsMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", listByCIDResultsMethod, err)
}
return parseAuditResults(items, c.listByCIDResultsMethod)
return parseAuditResults(items, listByCIDResultsMethod)
}
// ListAuditResultsByNode performs the test invoke of "list audit result IDs
@ -112,15 +112,15 @@ func (c *Client) ListAuditResultsByCID(args ListResultsByCIDArgs) (*ListResultsV
func (c *Client) ListAuditResultsByNode(args ListResultsByNodeArgs) (*ListResultsValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.listByNodeResultsMethod)
invokePrm.SetMethod(listByNodeResultsMethod)
invokePrm.SetArgs(args.epoch, args.cid, args.nodeKey)
items, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.listByNodeResultsMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", listByNodeResultsMethod, err)
}
return parseAuditResults(items, c.listByNodeResultsMethod)
return parseAuditResults(items, listByNodeResultsMethod)
}
func parseAuditResults(items []stackitem.Item, method string) (*ListResultsValues, error) {

View file

@ -25,14 +25,14 @@ func (g *PutAuditResultArgs) SetRawResult(v []byte) {
func (c *Client) PutAuditResult(args PutAuditResultArgs) error {
prm := client.InvokePrm{}
prm.SetMethod(c.putResultMethod)
prm.SetMethod(putResultMethod)
prm.SetArgs(args.rawResult)
prm.InvokePrmOptional = args.InvokePrmOptional
err := c.client.Invoke(prm)
if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.putResultMethod, err)
return fmt.Errorf("could not invoke method (%s): %w", putResultMethod, err)
}
return nil
}

View file

@ -35,19 +35,19 @@ func (g *GetBalanceOfValues) Amount() *big.Int {
func (c *Client) BalanceOf(args GetBalanceOfArgs) (*GetBalanceOfValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.balanceOfMethod)
invokePrm.SetMethod(balanceOfMethod)
invokePrm.SetArgs(args.wallet)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.balanceOfMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", balanceOfMethod, err)
} else if ln := len(prms); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", c.balanceOfMethod, ln)
return nil, fmt.Errorf("unexpected stack item count (%s): %d", balanceOfMethod, ln)
}
amount, err := client.BigIntFromStackItem(prms[0])
if err != nil {
return nil, fmt.Errorf("could not get integer stack item from stack item (%s): %w", c.balanceOfMethod, err)
return nil, fmt.Errorf("could not get integer stack item from stack item (%s): %w", balanceOfMethod, err)
}
return &GetBalanceOfValues{

View file

@ -1,8 +1,6 @@
package balance
import (
"errors"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
)
@ -16,113 +14,24 @@ import (
// and can lead to panic.
type Client struct {
client *client.StaticClient // static Balance contract client
*cfg // contract method names
}
// ErrNilClient is returned by functions that expect
// a non-nil Client pointer, but received nil.
var ErrNilClient = errors.New("balance contract client is nil")
// Option is a client configuration change function.
type Option func(*cfg)
type cfg struct {
transferXMethod, // transferX method name for invocation
mintMethod,
burnMethod,
lockMethod,
balanceOfMethod, // balanceOf method name for invocation
decimalsMethod string // decimals method name for invocation
}
const (
defaultTransferXMethod = "transferX" // default "transferX" method name
defaultMintMethod = "mint" // default "mint" method name
defaultBurnMethod = "burn" // default "burn" method name
defaultLockMethod = "lock" // default "lock" method name
defaultBalanceOfMethod = "balanceOf" // default "balance of" method name
defaultDecimalsMethod = "decimals" // default decimals method name
transferXMethod = "transferX"
mintMethod = "mint"
burnMethod = "burn"
lockMethod = "lock"
balanceOfMethod = "balanceOf"
decimalsMethod = "decimals"
)
func defaultConfig() *cfg {
return &cfg{
transferXMethod: defaultTransferXMethod,
mintMethod: defaultMintMethod,
burnMethod: defaultBurnMethod,
lockMethod: defaultLockMethod,
balanceOfMethod: defaultBalanceOfMethod,
decimalsMethod: defaultDecimalsMethod,
}
}
// New creates, initializes and returns the Client instance.
//
// If StaticClient is nil, client.ErrNilStaticClient is returned.
//
// Other values are set according to provided options, or by default:
// * "balance of" method name: balanceOf;
// * decimals method name: decimals.
//
// If desired option satisfies the default value, it can be omitted.
// If multiple options of the same config value are supplied,
// the option with the highest index in the arguments will be used.
func New(c *client.StaticClient, opts ...Option) (*Client, error) {
func New(c *client.StaticClient) (*Client, error) {
if c == nil {
return nil, client.ErrNilStaticClient
}
res := &Client{
client: c,
cfg: defaultConfig(), // build default configuration
}
// apply options
for _, opt := range opts {
opt(res.cfg)
}
return res, nil
}
// WithBalanceOfMethod returns a client constructor option that
// specifies the "balance of" method name.
//
// Ignores empty value.
//
// If option not provided, "balanceOf" is used.
func WithBalanceOfMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.balanceOfMethod = n
}
}
}
// WithDecimalsMethod returns a client constructor option that
// specifies the method name of decimals receiving operation.
//
// Ignores empty value.
//
// If option not provided, "decimals" is used.
func WithDecimalsMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.decimalsMethod = n
}
}
}
// WithTransferXMethod returns a client constructor option that
// specifies the "transferX" method name.
//
// Ignores empty value.
//
// If option not provided, "transferX" is used.
func WithTransferXMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.transferXMethod = n
}
}
return &Client{client: c}, nil
}

View file

@ -27,18 +27,18 @@ func (d *DecimalsValues) Decimals() int64 {
func (c *Client) Decimals(args DecimalsArgs) (*DecimalsValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.decimalsMethod)
invokePrm.SetMethod(decimalsMethod)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.decimalsMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", decimalsMethod, err)
} else if ln := len(prms); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", c.decimalsMethod, ln)
return nil, fmt.Errorf("unexpected stack item count (%s): %d", decimalsMethod, ln)
}
decimals, err := client.IntFromStackItem(prms[0])
if err != nil {
return nil, fmt.Errorf("could not get integer stack item from stack item (%s): %w", c.decimalsMethod, err)
return nil, fmt.Errorf("could not get integer stack item from stack item (%s): %w", decimalsMethod, err)
}
return &DecimalsValues{

View file

@ -49,13 +49,13 @@ func (t *TransferXArgs) SetDetails(v []byte) {
func (c *Client) TransferX(args TransferXArgs) error {
prm := client.InvokePrm{}
prm.SetMethod(c.transferXMethod)
prm.SetMethod(transferXMethod)
prm.SetArgs(args.sender, args.recipient, args.amount, args.details)
prm.InvokePrmOptional = args.InvokePrmOptional
err := c.client.Invoke(prm)
if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.transferXMethod, err)
return fmt.Errorf("could not invoke method (%s): %w", transferXMethod, err)
}
return nil
@ -89,7 +89,7 @@ func (m *MintPrm) SetID(id []byte) {
func (c *Client) Mint(args MintPrm) error {
prm := client.InvokePrm{}
prm.SetMethod(c.mintMethod)
prm.SetMethod(mintMethod)
prm.SetArgs(args.to, args.amount, args.id)
prm.InvokePrmOptional = args.InvokePrmOptional
@ -124,7 +124,7 @@ func (b *BurnPrm) SetID(id []byte) {
func (c *Client) Burn(args BurnPrm) error {
prm := client.InvokePrm{}
prm.SetMethod(c.burnMethod)
prm.SetMethod(burnMethod)
prm.SetArgs(args.to, args.amount, args.id)
prm.InvokePrmOptional = args.InvokePrmOptional
@ -171,7 +171,7 @@ func (l *LockPrm) SetDueEpoch(dueEpoch int64) {
func (c *Client) Lock(args LockPrm) error {
prm := client.InvokePrm{}
prm.SetMethod(c.lockMethod)
prm.SetMethod(lockMethod)
prm.SetArgs(args.id, args.user, args.lock, args.amount, args.dueEpoch)
prm.InvokePrmOptional = args.InvokePrmOptional

View file

@ -1,8 +1,6 @@
package container
import (
"errors"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
)
@ -16,276 +14,39 @@ import (
// and can lead to panic.
type Client struct {
client *client.StaticClient // static Container contract client
*cfg // contract method names
}
// ErrNilClient is returned by functions that expect
// a non-nil Client pointer, but received nil.
var ErrNilClient = errors.New("container contract client is nil")
// Option is a client configuration change function.
type Option func(*cfg)
type cfg struct {
putMethod, // put container method name for invocation
putNamedMethod, // put named container method name for invocation
putSizeMethod, // put container size method name for invocation
listSizesMethod, // list container sizes method name for invocation
getSizeMethod, // get container size method name for invocation
deleteMethod, // delete container method name for invocation
getMethod, // get container method name for invocation
listMethod, // list container method name for invocation
setEACLMethod, // set eACL method name for invocation
eaclMethod, // get eACL method name for invocation
startEstimation,
stopEstimation string
}
const (
defaultPutMethod = "put" // default put container method name
defaultDeleteMethod = "delete" // default delete container method name
defaultGetMethod = "get" // default get container method name
defaultListMethod = "list" // default list containers method name
defaultEACLMethod = "eACL" // default get eACL method name
defaultSetEACLMethod = "setEACL" // default set eACL method name
putMethod = "put"
deleteMethod = "delete"
getMethod = "get"
listMethod = "list"
eaclMethod = "eACL"
setEACLMethod = "setEACL"
defaultStartEstimation = "startContainerEstimation"
defaultStopEstimation = "stopContainerEstimation"
startEstimationMethod = "startContainerEstimation"
stopEstimationMethod = "stopContainerEstimation"
defaultPutSizeMethod = "putContainerSize" // default "put container size" method name
defaultListSizesMethod = "listContainerSizes" // default "list container sizes" method name
defaultGetSizeMethod = "getContainerSize" // default "get container size" method name
putSizeMethod = "putContainerSize"
listSizesMethod = "listContainerSizes"
getSizeMethod = "getContainerSize"
defaultPutNamedMethod = "putNamed" // default put named container method name
// PutNamedMethod is method name for container put with an alias. It is exported to provide custom fee.
PutNamedMethod = "putNamed"
)
func defaultConfig() *cfg {
return &cfg{
putMethod: defaultPutMethod,
deleteMethod: defaultDeleteMethod,
getMethod: defaultGetMethod,
listMethod: defaultListMethod,
setEACLMethod: defaultSetEACLMethod,
eaclMethod: defaultEACLMethod,
startEstimation: defaultStartEstimation,
stopEstimation: defaultStopEstimation,
putSizeMethod: defaultPutSizeMethod,
listSizesMethod: defaultListSizesMethod,
getSizeMethod: defaultGetSizeMethod,
putNamedMethod: defaultPutNamedMethod,
}
}
// New creates, initializes and returns the Client instance.
//
// If StaticClient is nil, client.ErrNilStaticClient is returned.
//
// Other values are set according to provided options, or by default:
// * put container method name: Put;
// * delete container method name: Delete;
// * get container method name: Get;
// * list containers method name: List;
// * set eACL method name: SetEACL;
// * get eACL method name: EACL.
// * start estimation method name: startContainerEstimation
// * stop estimation method name: stopContainerEstimation
// * put container size method name: putContainerSize
// * get container size method name: getContainerSize
// * list container sizes method name: listContainerSizes
//
// If desired option satisfies the default value, it can be omitted.
// If multiple options of the same config value are supplied,
// the option with the highest index in the arguments will be used.
func New(c *client.StaticClient, opts ...Option) (*Client, error) {
func New(c *client.StaticClient) (*Client, error) {
if c == nil {
return nil, client.ErrNilStaticClient
}
res := &Client{
client: c,
cfg: defaultConfig(), // build default configuration
}
// apply options
for _, opt := range opts {
opt(res.cfg)
}
return res, nil
return &Client{client: c}, nil
}
// Morph returns raw morph client.
func (c Client) Morph() *client.Client {
return c.client.Morph()
}
// WithPutMethod returns a client constructor option that
// specifies the method name of container storing operation.
//
// Ignores empty value.
//
// If option not provided, "Put" is used.
func WithPutMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.putMethod = n
}
}
}
// WithDeleteMethod returns a client constructor option that
// specifies the method name of container removal operation.
//
// Ignores empty value.
//
// If option not provided, "Delete" is used.
func WithDeleteMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.deleteMethod = n
}
}
}
// WithGetMethod returns a client constructor option that
// specifies the method name of container receiving operation.
//
// Ignores empty value.
//
// If option not provided, "Get" is used.
func WithGetMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.getMethod = n
}
}
}
// WithListMethod returns a client constructor option that
// specifies the method name of container listing operation.
//
// Ignores empty value.
//
// If option not provided, "List" is used.
func WithListMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.listMethod = n
}
}
}
// WithSetEACLMethod returns a client constructor option that
// specifies the method name of eACL storing operation.
//
// Ignores empty value.
//
// If option not provided, "SetEACL" is used.
func WithSetEACLMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.setEACLMethod = n
}
}
}
// WithEACLMethod returns a client constructor option that
// specifies the method name of eACL receiving operation.
//
// Ignores empty value.
//
// If option not provided, "EACL" is used.
func WithEACLMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.eaclMethod = n
}
}
}
// WithStartEstimationMethod returns a client constructor option that
// specifies the method name of vote to start container size estimation.
//
// Ignores empty value.
//
// If option not provided, "startContainerEstimation" is used.
func WithStartEstimationMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.startEstimation = n
}
}
}
// WithStopEstimationMethod returns a client constructor option that
// specifies the method name of vote to stop container size estimation.
//
// Ignores empty value.
//
// If option not provided, "stopContainerEstimation" is used.
func WithStopEstimationMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.stopEstimation = n
}
}
}
// WithPutSizeMethod returns a client constructor option that
// specifies the method name of "put container size" operation.
//
// Ignores empty value.
//
// If option not provided, "putContainerSize" is used.
func WithPutSizeMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.putSizeMethod = n
}
}
}
// WithListSizesMethod returns a client constructor option that
// specifies the method name of "list container sizes" operation.
//
// Ignores empty value.
//
// If option not provided, "listContainerSizes" is used.
func WithListSizesMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.listSizesMethod = n
}
}
}
// WithGetSizeMethod returns a client constructor option that
// specifies the method name of "get container size" operation.
//
// Ignores empty value.
//
// If option not provided, "getContainerSize" is used.
func WithGetSizeMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.getSizeMethod = n
}
}
}
// WithPutNamedMethod returns a client constructor option that
// specifies the method name of "put named container" operation.
//
// Ignores empty value.
//
// If option not provided, "putNamed" is used.
func WithPutNamedMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.putNamedMethod = n
}
}
}

View file

@ -42,14 +42,14 @@ func (p *DeleteArgs) SetSessionToken(v []byte) {
func (c *Client) Delete(args DeleteArgs) error {
prm := client.InvokePrm{}
prm.SetMethod(c.deleteMethod)
prm.SetMethod(deleteMethod)
prm.SetArgs(args.cid, args.sig, args.token)
prm.InvokePrmOptional = args.InvokePrmOptional
err := c.client.Invoke(prm)
if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.deleteMethod, err)
return fmt.Errorf("could not invoke method (%s): %w", deleteMethod, err)
}
return nil
}

View file

@ -57,43 +57,43 @@ func (g *EACLValues) SessionToken() []byte {
func (c *Client) EACL(args EACLArgs) (*EACLValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.eaclMethod)
invokePrm.SetMethod(eaclMethod)
invokePrm.SetArgs(args.cid)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.eaclMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", eaclMethod, err)
} else if ln := len(prms); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", c.eaclMethod, ln)
return nil, fmt.Errorf("unexpected stack item count (%s): %d", eaclMethod, ln)
}
arr, err := client.ArrayFromStackItem(prms[0])
if err != nil {
return nil, fmt.Errorf("could not get item array of eACL (%s): %w", c.eaclMethod, err)
return nil, fmt.Errorf("could not get item array of eACL (%s): %w", eaclMethod, err)
}
if len(arr) != 4 {
return nil, fmt.Errorf("unexpected eacl stack item count (%s): %d", c.eaclMethod, len(arr))
return nil, fmt.Errorf("unexpected eacl stack item count (%s): %d", eaclMethod, len(arr))
}
eacl, err := client.BytesFromStackItem(arr[0])
if err != nil {
return nil, fmt.Errorf("could not get byte array of eACL (%s): %w", c.eaclMethod, err)
return nil, fmt.Errorf("could not get byte array of eACL (%s): %w", eaclMethod, err)
}
sig, err := client.BytesFromStackItem(arr[1])
if err != nil {
return nil, fmt.Errorf("could not get byte array of eACL signature (%s): %w", c.eaclMethod, err)
return nil, fmt.Errorf("could not get byte array of eACL signature (%s): %w", eaclMethod, err)
}
pub, err := client.BytesFromStackItem(arr[2])
if err != nil {
return nil, fmt.Errorf("could not get byte array of eACL public key (%s): %w", c.eaclMethod, err)
return nil, fmt.Errorf("could not get byte array of eACL public key (%s): %w", eaclMethod, err)
}
tok, err := client.BytesFromStackItem(arr[3])
if err != nil {
return nil, fmt.Errorf("could not get byte array of eACL session token (%s): %w", c.eaclMethod, err)
return nil, fmt.Errorf("could not get byte array of eACL session token (%s): %w", eaclMethod, err)
}
return &EACLValues{

View file

@ -50,14 +50,14 @@ func (p *SetEACLArgs) SetSessionToken(v []byte) {
func (c *Client) SetEACL(args SetEACLArgs) error {
prm := client.InvokePrm{}
prm.SetMethod(c.setEACLMethod)
prm.SetMethod(setEACLMethod)
prm.SetArgs(args.eacl, args.sig, args.pubkey, args.token)
prm.InvokePrmOptional = args.InvokePrmOptional
err := c.client.Invoke(prm)
if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.setEACLMethod, err)
return fmt.Errorf("could not invoke method (%s): %w", setEACLMethod, err)
}
return nil
}

View file

@ -30,12 +30,12 @@ func (e *StopEstimation) SetEpoch(v int64) {
func (c *Client) StartEstimation(args StartEstimation) error {
prm := client.InvokePrm{}
prm.SetMethod(c.startEstimation)
prm.SetMethod(startEstimationMethod)
prm.SetArgs(args.epoch)
prm.InvokePrmOptional = args.InvokePrmOptional
if err := c.client.Invoke(prm); err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.startEstimation, err)
return fmt.Errorf("could not invoke method (%s): %w", startEstimationMethod, err)
}
return nil
}
@ -43,12 +43,12 @@ func (c *Client) StartEstimation(args StartEstimation) error {
func (c *Client) StopEstimation(args StopEstimation) error {
prm := client.InvokePrm{}
prm.SetMethod(c.stopEstimation)
prm.SetMethod(stopEstimationMethod)
prm.SetArgs(args.epoch)
prm.InvokePrmOptional = args.InvokePrmOptional
if err := c.client.Invoke(prm); err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.stopEstimation, err)
return fmt.Errorf("could not invoke method (%s): %w", stopEstimationMethod, err)
}
return nil
}

View file

@ -57,43 +57,43 @@ func (g *GetValues) SessionToken() []byte {
func (c *Client) Get(args GetArgs) (*GetValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.getMethod)
invokePrm.SetMethod(getMethod)
invokePrm.SetArgs(args.cid)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.getMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", getMethod, err)
} else if ln := len(prms); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", c.getMethod, ln)
return nil, fmt.Errorf("unexpected stack item count (%s): %d", getMethod, ln)
}
arr, err := client.ArrayFromStackItem(prms[0])
if err != nil {
return nil, fmt.Errorf("could not get item array of container (%s): %w", c.getMethod, err)
return nil, fmt.Errorf("could not get item array of container (%s): %w", getMethod, err)
}
if len(arr) != 4 {
return nil, fmt.Errorf("unexpected container stack item count (%s): %d", c.getMethod, len(arr))
return nil, fmt.Errorf("unexpected container stack item count (%s): %d", getMethod, len(arr))
}
cnrBytes, err := client.BytesFromStackItem(arr[0])
if err != nil {
return nil, fmt.Errorf("could not get byte array of container (%s): %w", c.getMethod, err)
return nil, fmt.Errorf("could not get byte array of container (%s): %w", getMethod, err)
}
sig, err := client.BytesFromStackItem(arr[1])
if err != nil {
return nil, fmt.Errorf("could not get byte array of container signature (%s): %w", c.getMethod, err)
return nil, fmt.Errorf("could not get byte array of container signature (%s): %w", getMethod, err)
}
pub, err := client.BytesFromStackItem(arr[2])
if err != nil {
return nil, fmt.Errorf("could not get byte array of public key (%s): %w", c.getMethod, err)
return nil, fmt.Errorf("could not get byte array of public key (%s): %w", getMethod, err)
}
tok, err := client.BytesFromStackItem(arr[3])
if err != nil {
return nil, fmt.Errorf("could not get byte array of session token (%s): %w", c.getMethod, err)
return nil, fmt.Errorf("could not get byte array of session token (%s): %w", getMethod, err)
}
return &GetValues{

View file

@ -35,19 +35,19 @@ func (l *ListValues) CIDList() [][]byte {
func (c *Client) List(args ListArgs) (*ListValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.listMethod)
invokePrm.SetMethod(listMethod)
invokePrm.SetArgs(args.ownerID)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.listMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", listMethod, err)
} else if ln := len(prms); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", c.listMethod, ln)
return nil, fmt.Errorf("unexpected stack item count (%s): %d", listMethod, ln)
}
prms, err = client.ArrayFromStackItem(prms[0])
if err != nil {
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", c.listMethod, err)
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", listMethod, err)
}
res := &ListValues{
@ -57,7 +57,7 @@ func (c *Client) List(args ListArgs) (*ListValues, error) {
for i := range prms {
cid, err := client.BytesFromStackItem(prms[i])
if err != nil {
return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", c.listMethod, err)
return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", listMethod, err)
}
res.cidList = append(res.cidList, cid)

View file

@ -48,14 +48,14 @@ func (p *PutSizeArgs) SetReporterKey(v []byte) {
func (c *Client) PutSize(args PutSizeArgs) error {
prm := client.InvokePrm{}
prm.SetMethod(c.putSizeMethod)
prm.SetMethod(putSizeMethod)
prm.SetArgs(args.epoch, args.cid, args.size, args.reporterKey)
prm.InvokePrmOptional = args.InvokePrmOptional
err := c.client.Invoke(prm)
if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.putSizeMethod, err)
return fmt.Errorf("could not invoke method (%s): %w", putSizeMethod, err)
}
return nil
}
@ -89,19 +89,19 @@ func (v *ListSizesValues) IDList() [][]byte {
func (c *Client) ListSizes(args ListSizesArgs) (*ListSizesValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.listSizesMethod)
invokePrm.SetMethod(listSizesMethod)
invokePrm.SetArgs(args.epoch)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.listSizesMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", listSizesMethod, err)
} else if ln := len(prms); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", c.listSizesMethod, ln)
return nil, fmt.Errorf("unexpected stack item count (%s): %d", listSizesMethod, ln)
}
prms, err = client.ArrayFromStackItem(prms[0])
if err != nil {
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", c.listSizesMethod, err)
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", listSizesMethod, err)
}
res := &ListSizesValues{
@ -111,7 +111,7 @@ func (c *Client) ListSizes(args ListSizesArgs) (*ListSizesValues, error) {
for i := range prms {
id, err := client.BytesFromStackItem(prms[i])
if err != nil {
return nil, fmt.Errorf("could not get ID byte array from stack item (%s): %w", c.listSizesMethod, err)
return nil, fmt.Errorf("could not get ID byte array from stack item (%s): %w", listSizesMethod, err)
}
res.ids = append(res.ids, id)
@ -159,33 +159,33 @@ func (v *GetSizeValues) Estimations() Estimations {
func (c *Client) GetContainerSize(args GetSizeArgs) (*GetSizeValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.getSizeMethod)
invokePrm.SetMethod(getSizeMethod)
invokePrm.SetArgs(args.id)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.getSizeMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", getSizeMethod, err)
} else if ln := len(prms); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", c.getSizeMethod, ln)
return nil, fmt.Errorf("unexpected stack item count (%s): %d", getSizeMethod, ln)
}
prms, err = client.ArrayFromStackItem(prms[0])
if err != nil {
return nil, fmt.Errorf("could not get stack items of estimation fields from stack item (%s): %w", c.getSizeMethod, err)
return nil, fmt.Errorf("could not get stack items of estimation fields from stack item (%s): %w", getSizeMethod, err)
} else if ln := len(prms); ln != 2 {
return nil, fmt.Errorf("unexpected stack item count of estimations fields (%s)", c.getSizeMethod)
return nil, fmt.Errorf("unexpected stack item count of estimations fields (%s)", getSizeMethod)
}
es := Estimations{}
es.ContainerID, err = client.BytesFromStackItem(prms[0])
if err != nil {
return nil, fmt.Errorf("could not get container ID byte array from stack item (%s): %w", c.getSizeMethod, err)
return nil, fmt.Errorf("could not get container ID byte array from stack item (%s): %w", getSizeMethod, err)
}
prms, err = client.ArrayFromStackItem(prms[1])
if err != nil {
return nil, fmt.Errorf("could not get estimation list array from stack item (%s): %w", c.getSizeMethod, err)
return nil, fmt.Errorf("could not get estimation list array from stack item (%s): %w", getSizeMethod, err)
}
es.Estimations = make([]Estimation, 0, len(prms))
@ -193,21 +193,21 @@ func (c *Client) GetContainerSize(args GetSizeArgs) (*GetSizeValues, error) {
for i := range prms {
arr, err := client.ArrayFromStackItem(prms[i])
if err != nil {
return nil, fmt.Errorf("could not get estimation struct from stack item (%s): %w", c.getSizeMethod, err)
return nil, fmt.Errorf("could not get estimation struct from stack item (%s): %w", getSizeMethod, err)
} else if ln := len(arr); ln != 2 {
return nil, fmt.Errorf("unexpected stack item count of estimation fields (%s)", c.getSizeMethod)
return nil, fmt.Errorf("unexpected stack item count of estimation fields (%s)", getSizeMethod)
}
e := Estimation{}
e.Reporter, err = client.BytesFromStackItem(arr[0])
if err != nil {
return nil, fmt.Errorf("could not get reporter byte array from stack item (%s): %w", c.getSizeMethod, err)
return nil, fmt.Errorf("could not get reporter byte array from stack item (%s): %w", getSizeMethod, err)
}
e.Size, err = client.IntFromStackItem(arr[1])
if err != nil {
return nil, fmt.Errorf("could not get estimation size from stack item (%s): %w", c.getSizeMethod, err)
return nil, fmt.Errorf("could not get estimation size from stack item (%s): %w", getSizeMethod, err)
}
es.Estimations = append(es.Estimations, e)

View file

@ -61,11 +61,11 @@ func (c *Client) Put(args PutArgs) error {
)
if args.name != "" {
method = c.putNamedMethod
method = PutNamedMethod
prm.SetArgs(args.cnr, args.sig, args.publicKey, args.token, args.name, args.zone)
} else {
method = c.putMethod
method = putMethod
prm.SetArgs(args.cnr, args.sig, args.publicKey, args.token)
}

View file

@ -82,19 +82,10 @@ func NewFromMorph(cli *client.Client, contract util.Uint160, fee fixedn.Fixed8,
opts[i](o)
}
// below is working but not the best solution to customize fee for PutNamed operation
// It is done like that because container package doesn't provide option to specify the fee.
// In the future, we will possibly get rid of the container package at all.
const methodNamePutNamed = "putNamed"
var (
staticOpts = o.staticOpts
cnrOpts = make([]container.Option, 0, 1)
)
staticOpts := o.staticOpts
if o.feePutNamedSet {
staticOpts = append(staticOpts, client.WithCustomFee(methodNamePutNamed, o.feePutNamed))
cnrOpts = append(cnrOpts, container.WithPutNamedMethod(methodNamePutNamed))
staticOpts = append(staticOpts, client.WithCustomFee(container.PutNamedMethod, o.feePutNamed))
}
staticClient, err := client.NewStatic(cli, contract, fee, staticOpts...)
@ -102,7 +93,7 @@ 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, cnrOpts...)
enhancedContainerClient, err := container.New(staticClient)
if err != nil {
return nil, fmt.Errorf("can't create container morph client: %w", err)
}

View file

@ -46,13 +46,13 @@ func (x *commonBindArgs) SetKeys(v [][]byte) {
func (x *Client) BindKeys(args BindKeysArgs) error {
prm := client.InvokePrm{}
prm.SetMethod(x.bindKeysMethod)
prm.SetMethod(bindKeysMethod)
prm.SetArgs(args.scriptHash, args.keys)
prm.InvokePrmOptional = args.InvokePrmOptional
err := x.client.Invoke(prm)
if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", x.bindKeysMethod, err)
return fmt.Errorf("could not invoke method (%s): %w", bindKeysMethod, err)
}
return nil
@ -63,13 +63,13 @@ func (x *Client) BindKeys(args BindKeysArgs) error {
func (x *Client) UnbindKeys(args UnbindKeysArgs) error {
prm := client.InvokePrm{}
prm.SetMethod(x.unbindKeysMethod)
prm.SetMethod(unbindKeysMethod)
prm.SetArgs(args.scriptHash, args.keys)
prm.InvokePrmOptional = args.InvokePrmOptional
err := x.client.Invoke(prm)
if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", x.unbindKeysMethod, err)
return fmt.Errorf("could not invoke method (%s): %w", unbindKeysMethod, err)
}
return nil

View file

@ -40,7 +40,7 @@ func (c *ChequePrm) SetLock(lock util.Uint160) {
func (x *Client) Cheque(args ChequePrm) error {
prm := client.InvokePrm{}
prm.SetMethod(x.chequeMethod)
prm.SetMethod(chequeMethod)
prm.SetArgs(args.id, args.user.BytesBE(), args.amount, args.lock.BytesBE())
prm.InvokePrmOptional = args.InvokePrmOptional
@ -70,7 +70,7 @@ func (a *AlphabetUpdatePrm) SetPubs(pubs keys.PublicKeys) {
func (x *Client) AlphabetUpdate(args AlphabetUpdatePrm) error {
prm := client.InvokePrm{}
prm.SetMethod(x.alphabetUpdateMethod)
prm.SetMethod(alphabetUpdateMethod)
prm.SetArgs(args.id, args.pubs)
prm.InvokePrmOptional = args.InvokePrmOptional

View file

@ -14,85 +14,22 @@ import (
// and can lead to panic.
type Client struct {
client *client.StaticClient // static NeoFS contract client
*cfg // contract method names
}
// Option is a client configuration change function.
type Option func(*cfg)
type cfg struct {
alphabetUpdateMethod,
chequeMethod,
bindKeysMethod,
unbindKeysMethod string
}
func defaultConfig() *cfg {
const (
defaultBindKeysMethod = "bind"
defaultUnbindKeysMethod = "unbind"
defaultAlphabetUpdateMethod = "alphabetUpdate"
defaultChequeMethod = "cheque"
)
return &cfg{
alphabetUpdateMethod: defaultAlphabetUpdateMethod,
chequeMethod: defaultChequeMethod,
bindKeysMethod: defaultBindKeysMethod,
unbindKeysMethod: defaultUnbindKeysMethod,
}
}
const (
bindKeysMethod = "bind"
unbindKeysMethod = "unbind"
alphabetUpdateMethod = "alphabetUpdate"
chequeMethod = "cheque"
)
// New creates, initializes and returns the Client instance.
//
// If StaticClient is nil, panic occurs.
//
// Other values are set according to provided options, or by default:
// * key binding method name: bind;
// * key unbinding method name: unbind;
//
// If desired option satisfies the default value, it can be omitted.
// If multiple options of the same config value are supplied,
// the option with the highest index in the arguments will be used.
func New(c *client.StaticClient, opts ...Option) *Client {
func New(c *client.StaticClient) *Client {
if c == nil {
panic("static client is nil")
}
res := &Client{
client: c,
cfg: defaultConfig(), // build default configuration
}
// apply options
for _, opt := range opts {
opt(res.cfg)
}
return res
}
// WithBindKeysMethod returns a client constructor option that
// specifies the method name of key binding operation.
//
// Ignores empty value.
func WithBindKeysMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.bindKeysMethod = n
}
}
}
// WithUnbindKeysMethod returns a client constructor option that
// specifies the method name of key unbinding operation.
//
// Ignores empty value.
func WithUnbindKeysMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.unbindKeysMethod = n
}
}
return &Client{client: c}
}

View file

@ -45,13 +45,13 @@ func (x *commonBindArgs) SetKeys(v [][]byte) {
func (x *Client) AddKeys(args AddKeysArgs) error {
prm := client.InvokePrm{}
prm.SetMethod(x.addKeysMethod)
prm.SetMethod(addKeysMethod)
prm.SetArgs(args.ownerID, args.keys)
prm.InvokePrmOptional = args.InvokePrmOptional
err := x.client.Invoke(prm)
if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", x.addKeysMethod, err)
return fmt.Errorf("could not invoke method (%s): %w", addKeysMethod, err)
}
return nil
@ -62,13 +62,13 @@ func (x *Client) AddKeys(args AddKeysArgs) error {
func (x *Client) RemoveKeys(args RemoveKeysArgs) error {
prm := client.InvokePrm{}
prm.SetMethod(x.removeKeysMethod)
prm.SetMethod(removeKeysMethod)
prm.SetArgs(args.ownerID, args.keys)
prm.InvokePrmOptional = args.InvokePrmOptional
err := x.client.Invoke(prm)
if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", x.removeKeysMethod, err)
return fmt.Errorf("could not invoke method (%s): %w", removeKeysMethod, err)
}
return nil

View file

@ -14,99 +14,21 @@ import (
// and can lead to panic.
type Client struct {
client *client.StaticClient // static NeoFS ID contract client
*cfg // contract method names
}
// Option is a client configuration change function.
type Option func(*cfg)
type cfg struct {
addKeysMethod,
removeKeysMethod,
keyListingMethod string
}
const (
defaultKeyListingMethod = "key" // default key listing method name
defaultAddKeysMethod = "addKey"
defaultRemoveKeysMethod = "removeKey"
keyListingMethod = "key"
addKeysMethod = "addKey"
removeKeysMethod = "removeKey"
)
func defaultConfig() *cfg {
return &cfg{
addKeysMethod: defaultAddKeysMethod,
removeKeysMethod: defaultRemoveKeysMethod,
keyListingMethod: defaultKeyListingMethod,
}
}
// New creates, initializes and returns the Client instance.
//
// If StaticClient is nil, panic occurs.
//
// Other values are set according to provided options, or by default:
// * key listing method name: key.
//
// If desired option satisfies the default value, it can be omitted.
// If multiple options of the same config value are supplied,
// the option with the highest index in the arguments will be used.
func New(c *client.StaticClient, opts ...Option) *Client {
func New(c *client.StaticClient) *Client {
if c == nil {
panic("static client is nil")
}
res := &Client{
client: c,
cfg: defaultConfig(), // build default configuration
}
// apply options
for _, opt := range opts {
opt(res.cfg)
}
return res
}
// WithKeyListingMethod returns a client constructor option that
// specifies the method name of key listing operation.
//
// Ignores empty value.
//
// If option not provided, "key" is used.
func WithKeyListingMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.keyListingMethod = n
}
}
}
// WithAddKeysMethod returns a client constructor option that
// specifies the method name of adding key operation.
//
// Ignores empty value.
//
// If option not provided, "addKey" is used.
func WithAddKeysMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.addKeysMethod = n
}
}
}
// WithRemoveKeysMethod returns a client constructor option that
// specifies the method name of removing key operation.
//
// Ignores empty value.
//
// If option not provided, "removeKey" is used.
func WithRemoveKeysMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.removeKeysMethod = n
}
}
return &Client{client: c}
}

View file

@ -35,19 +35,19 @@ func (l *KeyListingValues) Keys() [][]byte {
func (x *Client) AccountKeys(args KeyListingArgs) (*KeyListingValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(x.keyListingMethod)
invokePrm.SetMethod(keyListingMethod)
invokePrm.SetArgs(args.ownerID)
items, err := x.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", x.keyListingMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", keyListingMethod, err)
} else if ln := len(items); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", x.keyListingMethod, ln)
return nil, fmt.Errorf("unexpected stack item count (%s): %d", keyListingMethod, ln)
}
items, err = client.ArrayFromStackItem(items[0])
if err != nil {
return nil, fmt.Errorf("1st stack item must be an array (%s)", x.keyListingMethod)
return nil, fmt.Errorf("1st stack item must be an array (%s)", keyListingMethod)
}
keys := make([][]byte, 0, len(items))
@ -55,7 +55,7 @@ func (x *Client) AccountKeys(args KeyListingArgs) (*KeyListingValues, error) {
for i := range items {
key, err := client.BytesFromStackItem(items[i])
if err != nil {
return nil, fmt.Errorf("invalid stack item, expected byte array (%s)", x.keyListingMethod)
return nil, fmt.Errorf("invalid stack item, expected byte array (%s)", keyListingMethod)
}
keys = append(keys, key)

View file

@ -24,12 +24,12 @@ func (a *AddPeerArgs) SetInfo(v []byte) {
func (c *Client) AddPeer(args AddPeerArgs) error {
prm := client.InvokePrm{}
prm.SetMethod(c.addPeerMethod)
prm.SetMethod(addPeerMethod)
prm.SetArgs(args.info)
prm.InvokePrmOptional = args.InvokePrmOptional
if err := c.client.Invoke(prm); err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.addPeerMethod, err)
return fmt.Errorf("could not invoke method (%s): %w", addPeerMethod, err)
}
return nil
}

View file

@ -1,8 +1,6 @@
package netmap
import (
"errors"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/nspcc-dev/neofs-sdk-go/netmap"
)
@ -19,216 +17,39 @@ type NodeInfo = netmap.NodeInfo
// and can lead to panic.
type Client struct {
client *client.StaticClient // static Netmap contract client
*cfg // contract method names
}
// ErrNilClient is returned by functions that expect
// a non-nil Client pointer, but received nil.
var ErrNilClient = errors.New("netmap contract client is nil")
// Option is a client configuration change function.
type Option func(*cfg)
type cfg struct {
addPeerMethod, // add peer method name for invocation
newEpochMethod, // new epoch method name for invocation
innerRingList, // get innerring list method name for invocation
netMapMethod, // get network map method name
netMapCandidatesMethod, // get network candidates method name
snapshotMethod, // get network map snapshot method name
epochSnapshotMethod, // get network map snapshot by epoch method name
updateStateMethod, // update state method name for invocation
epochMethod, // get epoch number method name
lastEpochBlockMethod, // get last epoch number method name
updateInnerRing, // update innerring method name
setConfigMethod, // set config method name
configMethod, // get config value method name
configListMethod string // list config method name
}
const (
defaultAddPeerMethod = "addPeer" // default add peer method name
defaultConfigMethod = "config" // default get config value method name
defaultEpochMethod = "epoch" // default get epoch number method name
defaultLastEpochBlockMethod = "lastEpochBlock" // default get last epoch block number method name
defaultInnerRingListMethod = "innerRingList" // default get innerring list method name
defaultNetMapCandidateMethod = "netmapCandidates" // default get network candidates method name
defaultNetMapMethod = "netmap" // default get network map method name
defaultNewEpochMethod = "newEpoch" // default new epoch method name
defaultSetConfigMethod = "setConfig" // default get config value method name
defaultUpdateInnerRingMethod = "updateInnerRing" // default update innerring method name
defaultSnapshotMethod = "snapshot" // default get network map snapshot method name
defaultUpdateStateMethod = "updateState" // default update state method name
addPeerMethod = "addPeer"
configMethod = "config"
epochMethod = "epoch"
lastEpochBlockMethod = "lastEpochBlock"
innerRingListMethod = "innerRingList"
netMapCandidatesMethod = "netmapCandidates"
netMapMethod = "netmap"
newEpochMethod = "newEpoch"
setConfigMethod = "setConfig"
updateInnerRingMethod = "updateInnerRing"
snapshotMethod = "snapshot"
updateStateMethod = "updateState"
defaultEpochSnapshotMethod = "snapshotByEpoch" // default get network map snapshot by epoch method name
epochSnapshotMethod = "snapshotByEpoch"
defaultConfigListMethod = "listConfig" // default config listing method name
configListMethod = "listConfig"
)
func defaultConfig() *cfg {
return &cfg{
addPeerMethod: defaultAddPeerMethod,
configMethod: defaultConfigMethod,
epochMethod: defaultEpochMethod,
lastEpochBlockMethod: defaultLastEpochBlockMethod,
innerRingList: defaultInnerRingListMethod,
netMapCandidatesMethod: defaultNetMapCandidateMethod,
netMapMethod: defaultNetMapMethod,
newEpochMethod: defaultNewEpochMethod,
setConfigMethod: defaultSetConfigMethod,
snapshotMethod: defaultSnapshotMethod,
updateStateMethod: defaultUpdateStateMethod,
updateInnerRing: defaultUpdateInnerRingMethod,
epochSnapshotMethod: defaultEpochSnapshotMethod,
configListMethod: defaultConfigListMethod,
}
}
// New creates, initializes and returns the Client instance.
//
// If StaticClient is nil, client.ErrNilStaticClient is returned.
//
// Other values are set according to provided options, or by default:
// * add peer method name: AddPeer;
// * new epoch method name: NewEpoch;
// * get network map method name: Netmap;
// * update state method name: UpdateState;
//
// If desired option satisfies the default value, it can be omitted.
// If multiple options of the same config value are supplied,
// the option with the highest index in the arguments will be used.
func New(c *client.StaticClient, opts ...Option) (*Client, error) {
func New(c *client.StaticClient) (*Client, error) {
if c == nil {
return nil, client.ErrNilStaticClient
}
res := &Client{
client: c,
cfg: defaultConfig(), // build default configuration
}
// apply options
for _, opt := range opts {
opt(res.cfg)
}
return res, nil
return &Client{client: c}, nil
}
// Morph returns raw morph client.
func (c Client) Morph() *client.Client {
return c.client.Morph()
}
// WithAddPeerMethod returns a client constructor option that
// specifies the method name of adding peer operation.
//
// Ignores empty value.
//
// If option not provided, "AddPeer" is used.
func WithAddPeerMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.addPeerMethod = n
}
}
}
// WithNewEpochMethod returns a client constructor option that
// specifies the method name of new epoch operation.
//
// Ignores empty value.
//
// If option not provided, "NewEpoch" is used.
func WithNewEpochMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.newEpochMethod = n
}
}
}
// WithNetMapMethod returns a client constructor option that
// specifies the method name of network map receiving operation.
//
// Ignores empty value.
//
// If option not provided, "Netmap" is used.
func WithNetMapMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.netMapMethod = n
}
}
}
// WithUpdateStateMethod returns a client constructor option that
// specifies the method name of peer state updating operation.
//
// Ignores empty value.
//
// If option not provided, "UpdateState" is used.
func WithUpdateStateMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.updateStateMethod = n
}
}
}
// WithEpochMethod returns a client constructor option that
// specifies the method name of epoch number receiving operation.
//
// Ignores empty value.
//
// If option not provided, "epoch" is used.
func WithEpochMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.epochMethod = n
}
}
}
// WithConfigMethod returns a client constructor option that
// specifies the method name of config value receiving operation.
//
// Ignores empty value.
//
// If option not provided, "config" is used.
func WithConfigMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.configMethod = n
}
}
}
// WithEpochSnapshotMethod returns a client constructor option that
// specifies the method name of snapshot by value receiving operation.
//
// Ignores empty value.
//
// If option not provided, "snapshotByValue" is used.
func WithEpochSnapshotMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.epochSnapshotMethod = n
}
}
}
// WithConfigListMethod returns a client constructor option that
// specifies the config listing method name.
//
// Ignores empty value.
//
// If option not provided, "listConfig" is used.
func WithConfigListMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.configListMethod = n
}
}
}

View file

@ -34,18 +34,18 @@ func (c ConfigValues) Value() interface{} {
func (c *Client) Config(args ConfigArgs, assert func(stackitem.Item) (interface{}, error)) (*ConfigValues, error) {
prm := client.TestInvokePrm{}
prm.SetMethod(c.configMethod)
prm.SetMethod(configMethod)
prm.SetArgs(args.key)
items, err := c.client.TestInvoke(prm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.configMethod, err)
configMethod, err)
}
if ln := len(items); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d",
c.configMethod, ln)
configMethod, ln)
}
val, err := assert(items[0])
@ -86,7 +86,7 @@ func (s *SetConfigPrm) SetValue(value interface{}) {
func (c *Client) SetConfig(args SetConfigPrm) error {
prm := client.InvokePrm{}
prm.SetMethod(c.setConfigMethod)
prm.SetMethod(setConfigMethod)
prm.SetArgs(args.id, args.key, args.value)
prm.InvokePrmOptional = args.InvokePrmOptional
@ -148,23 +148,23 @@ func (x ListConfigValues) IterateRecords(f func(key, value []byte) error) error
func (c *Client) ListConfig(_ ListConfigArgs) (*ListConfigValues, error) {
prm := client.TestInvokePrm{}
prm.SetMethod(c.configListMethod)
prm.SetMethod(configListMethod)
items, err := c.client.TestInvoke(prm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.configListMethod, err)
configListMethod, err)
}
if ln := len(items); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d",
c.configListMethod, ln)
configListMethod, ln)
}
arr, err := client.ArrayFromStackItem(items[0])
if err != nil {
return nil, fmt.Errorf("record list (%s): %w",
c.configListMethod, err)
configListMethod, err)
}
return &ListConfigValues{

View file

@ -25,22 +25,22 @@ func (e EpochValues) Number() int64 {
// method of NeoFS Netmap contract.
func (c *Client) Epoch(_ EpochArgs) (*EpochValues, error) {
prm := client.TestInvokePrm{}
prm.SetMethod(c.epochMethod)
prm.SetMethod(epochMethod)
items, err := c.client.TestInvoke(prm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.epochMethod, err)
epochMethod, err)
}
if ln := len(items); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d",
c.epochMethod, ln)
epochMethod, ln)
}
num, err := client.IntFromStackItem(items[0])
if err != nil {
return nil, fmt.Errorf("could not get number from stack item (%s): %w", c.epochMethod, err)
return nil, fmt.Errorf("could not get number from stack item (%s): %w", epochMethod, err)
}
return &EpochValues{
@ -67,23 +67,23 @@ func (e EpochBlockValues) Block() int64 {
// method of NeoFS Netmap contract.
func (c *Client) LastEpochBlock(_ EpochBlockArgs) (*EpochBlockValues, error) {
prm := client.TestInvokePrm{}
prm.SetMethod(c.lastEpochBlockMethod)
prm.SetMethod(lastEpochBlockMethod)
items, err := c.client.TestInvoke(prm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.lastEpochBlockMethod, err)
lastEpochBlockMethod, err)
}
if ln := len(items); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d",
c.lastEpochBlockMethod, ln)
lastEpochBlockMethod, ln)
}
block, err := client.IntFromStackItem(items[0])
if err != nil {
return nil, fmt.Errorf("could not get number from stack item (%s): %w",
c.lastEpochBlockMethod, err)
lastEpochBlockMethod, err)
}
return &EpochBlockValues{

View file

@ -32,7 +32,7 @@ func (c *Client) UpdateInnerRing(p UpdateIRPrm) error {
prm := client.InvokePrm{}
prm.SetMethod(c.updateInnerRing)
prm.SetMethod(updateInnerRingMethod)
prm.SetArgs(args)
prm.InvokePrmOptional = p.InvokePrmOptional
@ -43,14 +43,14 @@ func (c *Client) UpdateInnerRing(p UpdateIRPrm) error {
// netmap contract.
func (c *Client) InnerRingList() (keys.PublicKeys, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.innerRingList)
invokePrm.SetMethod(innerRingListMethod)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.innerRingList, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", innerRingListMethod, err)
}
return irKeysFromStackItem(prms, c.innerRingList)
return irKeysFromStackItem(prms, innerRingListMethod)
}
func irKeysFromStackItem(stack []stackitem.Item, method string) (keys.PublicKeys, error) {

View file

@ -62,15 +62,15 @@ type GetNetMapArgs struct{}
// method of NeoFS Netmap contract.
func (c *Client) NetMap(_ GetNetMapArgs) (*GetNetMapValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.netMapMethod)
invokePrm.SetMethod(netMapMethod)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.netMapMethod, err)
netMapMethod, err)
}
return peersFromStackItems(prms, c.netMapMethod)
return peersFromStackItems(prms, netMapMethod)
}
// GetSnapshotArgs groups the arguments
@ -91,16 +91,16 @@ func (g *GetSnapshotArgs) SetDiff(d uint64) {
func (c *Client) Snapshot(a GetSnapshotArgs) (*GetNetMapValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.snapshotMethod)
invokePrm.SetMethod(snapshotMethod)
invokePrm.SetArgs(int64(a.diff))
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.netMapMethod, err)
netMapMethod, err)
}
return peersFromStackItems(prms, c.snapshotMethod)
return peersFromStackItems(prms, snapshotMethod)
}
// EpochSnapshotArgs groups the arguments
@ -125,16 +125,16 @@ type EpochSnapshotValues struct {
func (c *Client) EpochSnapshot(args EpochSnapshotArgs) (*EpochSnapshotValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.epochSnapshotMethod)
invokePrm.SetMethod(epochSnapshotMethod)
invokePrm.SetArgs(int64(args.epoch))
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.epochSnapshotMethod, err)
epochSnapshotMethod, err)
}
nmVals, err := peersFromStackItems(prms, c.epochSnapshotMethod)
nmVals, err := peersFromStackItems(prms, epochSnapshotMethod)
if err != nil {
return nil, err
}
@ -160,14 +160,14 @@ func (g GetNetMapCandidatesValues) NetmapNodes() []*PeerWithState {
func (c *Client) Candidates(_ GetNetMapCandidatesArgs) (*GetNetMapCandidatesValues, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.netMapCandidatesMethod)
invokePrm.SetMethod(netMapCandidatesMethod)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.netMapCandidatesMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", netMapCandidatesMethod, err)
}
candVals, err := peersWithStateFromStackItems(prms, c.netMapCandidatesMethod)
candVals, err := peersWithStateFromStackItems(prms, netMapCandidatesMethod)
if err != nil {
return nil, fmt.Errorf("could not parse contract response: %w", err)
}

View file

@ -24,12 +24,12 @@ func (a *NewEpochArgs) SetEpochNumber(v int64) {
func (c *Client) NewEpoch(args NewEpochArgs) error {
prm := client.InvokePrm{}
prm.SetMethod(c.newEpochMethod)
prm.SetMethod(newEpochMethod)
prm.SetArgs(args.number)
prm.InvokePrmOptional = args.InvokePrmOptional
if err := c.client.Invoke(prm); err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.newEpochMethod, err)
return fmt.Errorf("could not invoke method (%s): %w", newEpochMethod, err)
}
return nil
}

View file

@ -32,13 +32,13 @@ func (u *UpdateStateArgs) SetState(v int64) {
func (c *Client) UpdateState(args UpdateStateArgs) error {
prm := client.InvokePrm{}
prm.SetMethod(c.updateStateMethod)
prm.SetMethod(updateStateMethod)
prm.SetArgs(args.state, args.key)
prm.InvokePrmOptional = args.InvokePrmOptional
err := c.client.Invoke(prm)
if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.updateStateMethod, err)
return fmt.Errorf("could not invoke method (%s): %w", updateStateMethod, err)
}
return nil

View file

@ -14,120 +14,27 @@ import (
// and can lead to panic.
type Client struct {
client *client.StaticClient // static reputation contract client
*cfg // contract method names
}
// Option is a client configuration change function.
type Option func(*cfg)
type cfg struct {
putMethod,
getMethod,
getByIDMethod,
listByEpochMethod string
}
const (
defaultPutMethod = "put"
defaultGetMethod = "get"
defaultGetByIDMethod = "getByID"
defaultListByEpochMethod = "listByEpoch"
putMethod = "put"
getMethod = "get"
getByIDMethod = "getByID"
listByEpochMethod = "listByEpoch"
)
func defaultConfig() *cfg {
return &cfg{
putMethod: defaultPutMethod,
getMethod: defaultGetMethod,
getByIDMethod: defaultGetByIDMethod,
listByEpochMethod: defaultListByEpochMethod,
}
}
// New creates, initializes and returns the Client instance.
//
// If StaticClient is nil, client.ErrNilStaticClient is returned.
//
// Other values are set according to provided options, or by default.
//
// If desired option satisfies the default value, it can be omitted.
// If multiple options of the same config value are supplied,
// the option with the highest index in the arguments will be used.
func New(c *client.StaticClient, opts ...Option) (*Client, error) {
func New(c *client.StaticClient) (*Client, error) {
if c == nil {
return nil, client.ErrNilStaticClient
}
res := &Client{
client: c,
cfg: defaultConfig(), // build default configuration
}
// apply options
for _, opt := range opts {
opt(res.cfg)
}
return res, nil
return &Client{client: c}, nil
}
// Morph returns raw morph client.
func (c Client) Morph() *client.Client {
return c.client.Morph()
}
// WithPutMethod returns a client constructor option that
// specifies the method name to put reputation value.
//
// Ignores empty value.
//
// If option not provided, "put" is used.
func WithPutMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.putMethod = n
}
}
}
// WithGetMethod returns a client constructor option that
// specifies the method name to get reputation value.
//
// Ignores empty value.
//
// If option not provided, "get" is used.
func WithGetMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.getMethod = n
}
}
}
// WithGetByIDMethod returns a client constructor option that
// specifies the method name to get reputation value by it's ID in the contract.
//
// Ignores empty value.
//
// If option not provided, "getByID" is used.
func WithGetByIDMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.getByIDMethod = n
}
}
}
// WithListByEpochMethod returns a client constructor option that
// specifies the method name to list reputation value IDs for certain epoch.
//
// Ignores empty value.
//
// If option not provided, "listByEpoch" is used.
func WithListByEpochDMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.listByEpochMethod = n
}
}
}

View file

@ -49,15 +49,15 @@ func (g GetResult) Reputations() [][]byte {
func (c *Client) Get(args GetArgs) (*GetResult, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.getMethod)
invokePrm.SetMethod(getMethod)
invokePrm.SetArgs(int64(args.epoch), args.peerID)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.getMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", getMethod, err)
}
return parseReputations(prms, c.getMethod)
return parseReputations(prms, getMethod)
}
// GetByID invokes the call of "get reputation value by reputation id" method
@ -65,15 +65,15 @@ func (c *Client) Get(args GetArgs) (*GetResult, error) {
func (c *Client) GetByID(args GetByIDArgs) (*GetResult, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.getByIDMethod)
invokePrm.SetMethod(getByIDMethod)
invokePrm.SetArgs(args.id)
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.getByIDMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", getByIDMethod, err)
}
return parseReputations(prms, c.getByIDMethod)
return parseReputations(prms, getByIDMethod)
}
func parseReputations(items []stackitem.Item, method string) (*GetResult, error) {

View file

@ -33,19 +33,19 @@ func (l ListByEpochResult) IDs() [][]byte {
func (c *Client) ListByEpoch(args ListByEpochArgs) (*ListByEpochResult, error) {
invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.listByEpochMethod)
invokePrm.SetMethod(listByEpochMethod)
invokePrm.SetArgs(int64(args.epoch))
prms, err := c.client.TestInvoke(invokePrm)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.listByEpochMethod, err)
return nil, fmt.Errorf("could not perform test invocation (%s): %w", listByEpochMethod, err)
} else if ln := len(prms); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", c.listByEpochMethod, ln)
return nil, fmt.Errorf("unexpected stack item count (%s): %d", listByEpochMethod, ln)
}
items, err := client.ArrayFromStackItem(prms[0])
if err != nil {
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", c.listByEpochMethod, err)
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", listByEpochMethod, err)
}
res := &ListByEpochResult{
@ -55,7 +55,7 @@ func (c *Client) ListByEpoch(args ListByEpochArgs) (*ListByEpochResult, error) {
for i := range items {
rawReputation, err := client.BytesFromStackItem(items[i])
if err != nil {
return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", c.listByEpochMethod, err)
return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", listByEpochMethod, err)
}
res.ids = append(res.ids, rawReputation)

View file

@ -32,13 +32,13 @@ func (p *PutArgs) SetValue(v []byte) {
func (c *Client) Put(args PutArgs) error {
prm := client.InvokePrm{}
prm.SetMethod(c.putMethod)
prm.SetMethod(putMethod)
prm.SetArgs(int64(args.epoch), args.peerID, args.value)
err := c.client.Invoke(prm)
if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.putMethod, err)
return fmt.Errorf("could not invoke method (%s): %w", putMethod, err)
}
return nil
}

View file

@ -59,17 +59,17 @@ func (x Client) ManageAdmins(prm ManageAdminsPrm) (*ManageAdminsPrm, error) {
args = append(args, prm.group, prm.admin)
if prm.rm {
method = "removeClientAdmin"
method = removeClientAdminMethod
} else {
method = "addClientAdmin"
method = addClientAdminMethod
}
} else {
args = append(args, prm.admin)
if prm.rm {
method = "removeNodeAdmin"
method = removeNodeAdminMethod
} else {
method = "addNodeAdmin"
method = addNodeAdminMethod
}
}

View file

@ -24,6 +24,25 @@ type InitPrm struct {
mode Mode
}
const (
deleteMethod = "delete"
getMethod = "get"
putMethod = "put"
removeClientAdminMethod = "removeClientAdmin"
addClientAdminMethod = "addClientAdmin"
userAllowedMethod = "userAllowed"
removeUserMethod = "removeUser"
addUserMethod = "addUser"
removeNodeAdminMethod = "removeNodeAdmin"
addNodeAdminMethod = "addNodeAdmin"
nodeAllowedMethod = "nodeAllowed"
removeNodeMethod = "removeNode"
addNodeMethod = "addNode"
)
// SetBaseClient sets basic morph client.
func (x *InitPrm) SetBaseClient(base *client.Client) {
x.base = base

View file

@ -35,7 +35,7 @@ func (x UserAllowedRes) Allowed() bool {
func (x *Client) UserAllowed(prm UserAllowedPrm) (*UserAllowedRes, error) {
args := client.TestInvokePrm{}
args.SetMethod("userAllowed")
args.SetMethod(userAllowedMethod)
args.SetArgs(prm.args[:]...)
res, err := x.client.TestInvoke(args)
@ -95,9 +95,9 @@ func (x Client) ManageClients(prm ManageClientsPrm) (*ManageClientsRes, error) {
var method string
if prm.rm {
method = "removeUser"
method = removeUserMethod
} else {
method = "addUser"
method = addUserMethod
}
var prmInvoke client.InvokePrm

View file

@ -28,7 +28,7 @@ type DeleteRes struct{}
// Delete removes subnet though the call of the corresponding method of the Subnet contract.
func (x Client) Delete(prm DeletePrm) (*DeleteRes, error) {
prm.cliPrm.SetMethod("delete")
prm.cliPrm.SetMethod(deleteMethod)
prm.cliPrm.SetArgs(prm.args[:]...)
err := x.client.Invoke(prm.cliPrm)

View file

@ -32,7 +32,7 @@ var errEmptyResponse = errors.New("empty response")
func (x *Client) Get(prm GetPrm) (*GetRes, error) {
var prmGet client.TestInvokePrm
prmGet.SetMethod("get")
prmGet.SetMethod(getMethod)
prmGet.SetArgs(prm.args[:]...)
res, err := x.client.TestInvoke(prmGet)

View file

@ -35,7 +35,7 @@ func (x NodeAllowedRes) Allowed() bool {
// NodeAllowed checks if the node is included in the subnetwork.
func (x *Client) NodeAllowed(prm NodeAllowedPrm) (*NodeAllowedRes, error) {
prm.cliPrm.SetMethod("nodeAllowed")
prm.cliPrm.SetMethod(nodeAllowedMethod)
prm.cliPrm.SetArgs(prm.args[:]...)
res, err := x.client.TestInvoke(prm.cliPrm)

View file

@ -35,9 +35,9 @@ func (x Client) ManageNodes(prm ManageNodesPrm) (*ManageNodesRes, error) {
var method string
if prm.rm {
method = "removeNode"
method = removeNodeMethod
} else {
method = "addNode"
method = addNodeMethod
}
var prmInvoke client.InvokePrm

View file

@ -38,7 +38,7 @@ type PutRes struct{}
// Put creates subnet though the call of the corresponding method of the Subnet contract.
func (x Client) Put(prm PutPrm) (*PutRes, error) {
prm.cliPrm.SetMethod("put")
prm.cliPrm.SetMethod(putMethod)
prm.cliPrm.SetArgs(prm.args[:]...)
err := x.client.Invoke(prm.cliPrm)