2020-10-22 14:19:07 +00:00
|
|
|
package netmap
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2020-10-22 14:19:07 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConfigArgs groups the arguments
|
|
|
|
// of get config value test invoke call.
|
|
|
|
type ConfigArgs struct {
|
|
|
|
key []byte
|
|
|
|
}
|
|
|
|
|
2021-11-10 10:44:19 +00:00
|
|
|
// SetKey sets binary key to configuration parameter.
|
|
|
|
func (c *ConfigArgs) SetKey(v []byte) {
|
|
|
|
c.key = v
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:29:46 +00:00
|
|
|
// ConfigValues groups the stack parameters
|
|
|
|
// returned by get config test invoke.
|
2020-10-22 14:19:07 +00:00
|
|
|
type ConfigValues struct {
|
|
|
|
val interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns configuration value.
|
|
|
|
func (c ConfigValues) Value() interface{} {
|
|
|
|
return c.val
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config performs the test invoke of get config value
|
|
|
|
// method of NeoFS Netmap contract.
|
|
|
|
func (c *Client) Config(args ConfigArgs, assert func(stackitem.Item) (interface{}, error)) (*ConfigValues, error) {
|
2021-11-09 20:52:29 +00:00
|
|
|
prm := client.TestInvokePrm{}
|
|
|
|
|
|
|
|
prm.SetMethod(c.configMethod)
|
|
|
|
prm.SetArgs(args.key)
|
|
|
|
|
|
|
|
items, err := c.client.TestInvoke(prm)
|
2020-10-22 14:19:07 +00:00
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
|
|
|
|
c.configMethod, err)
|
2020-10-22 14:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ln := len(items); ln != 1 {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("unexpected stack item count (%s): %d",
|
2020-10-22 14:19:07 +00:00
|
|
|
c.configMethod, ln)
|
|
|
|
}
|
|
|
|
|
|
|
|
val, err := assert(items[0])
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("value type assertion failed: %w", err)
|
2020-10-22 14:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &ConfigValues{
|
|
|
|
val: val,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-11-10 10:44:19 +00:00
|
|
|
// SetConfigPrm groups parameters of SetConfig operation.
|
|
|
|
type SetConfigPrm struct {
|
|
|
|
id []byte
|
|
|
|
key []byte
|
|
|
|
value interface{}
|
|
|
|
|
|
|
|
client.InvokePrmOptional
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetID sets ID of the config value.
|
|
|
|
func (s *SetConfigPrm) SetID(id []byte) {
|
|
|
|
s.id = id
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetKey sets key of the config value.
|
|
|
|
func (s *SetConfigPrm) SetKey(key []byte) {
|
|
|
|
s.key = key
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetValue sets value of the config value.
|
|
|
|
func (s *SetConfigPrm) SetValue(value interface{}) {
|
|
|
|
s.value = value
|
|
|
|
}
|
|
|
|
|
2021-05-31 11:50:11 +00:00
|
|
|
// SetConfig invokes `setConfig` method of NeoFS Netmap contract.
|
2021-11-10 10:44:19 +00:00
|
|
|
func (c *Client) SetConfig(args SetConfigPrm) error {
|
2021-11-09 20:52:29 +00:00
|
|
|
prm := client.InvokePrm{}
|
|
|
|
|
|
|
|
prm.SetMethod(c.setConfigMethod)
|
2021-11-10 10:44:19 +00:00
|
|
|
prm.SetArgs(args.id, args.key, args.value)
|
|
|
|
prm.InvokePrmOptional = args.InvokePrmOptional
|
2021-11-09 20:52:29 +00:00
|
|
|
|
|
|
|
return c.client.Invoke(prm)
|
2021-05-31 11:50:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-22 14:19:07 +00:00
|
|
|
func IntegerAssert(item stackitem.Item) (interface{}, error) {
|
|
|
|
return client.IntFromStackItem(item)
|
|
|
|
}
|
2021-05-05 20:54:08 +00:00
|
|
|
|
|
|
|
func StringAssert(item stackitem.Item) (interface{}, error) {
|
|
|
|
return client.StringFromStackItem(item)
|
|
|
|
}
|
2021-09-20 16:09:32 +00:00
|
|
|
|
|
|
|
// ListConfigArgs groups the arguments
|
|
|
|
// of config listing test invoke call.
|
|
|
|
type ListConfigArgs struct {
|
|
|
|
}
|
|
|
|
|
2021-11-09 20:52:29 +00:00
|
|
|
// ListConfigValues groups the stack parameters
|
2021-09-20 16:09:32 +00:00
|
|
|
// returned by config listing test invoke.
|
|
|
|
type ListConfigValues struct {
|
|
|
|
rs []stackitem.Item
|
|
|
|
}
|
|
|
|
|
|
|
|
// IterateRecords iterates over all config records and passes them to f.
|
|
|
|
//
|
|
|
|
// Returns f's errors directly.
|
|
|
|
func (x ListConfigValues) IterateRecords(f func(key, value []byte) error) error {
|
|
|
|
for i := range x.rs {
|
|
|
|
fields, err := client.ArrayFromStackItem(x.rs[i])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("record fields: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ln := len(fields); ln != 2 {
|
|
|
|
return fmt.Errorf("unexpected record fields number: %d", ln)
|
|
|
|
}
|
|
|
|
|
|
|
|
k, err := client.BytesFromStackItem(fields[0])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("record key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
v, err := client.BytesFromStackItem(fields[1])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("record value: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f(k, v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListConfig performs the test invoke of config listing method of NeoFS Netmap contract.
|
2021-11-10 10:44:19 +00:00
|
|
|
func (c *Client) ListConfig(_ ListConfigArgs) (*ListConfigValues, error) {
|
2021-11-09 20:52:29 +00:00
|
|
|
prm := client.TestInvokePrm{}
|
|
|
|
|
|
|
|
prm.SetMethod(c.configListMethod)
|
|
|
|
|
|
|
|
items, err := c.client.TestInvoke(prm)
|
2021-09-20 16:09:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
|
|
|
|
c.configListMethod, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ln := len(items); ln != 1 {
|
|
|
|
return nil, fmt.Errorf("unexpected stack item count (%s): %d",
|
|
|
|
c.configListMethod, ln)
|
|
|
|
}
|
|
|
|
|
|
|
|
arr, err := client.ArrayFromStackItem(items[0])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("record list (%s): %w",
|
|
|
|
c.configListMethod, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ListConfigValues{
|
|
|
|
rs: arr,
|
|
|
|
}, nil
|
|
|
|
}
|