[#971] morph/netmap: 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:44:19 +03:00 committed by Alex Vanin
parent 3114be39d0
commit c25f5a86ae
11 changed files with 224 additions and 90 deletions

View file

@ -10,6 +10,8 @@ import (
// of add peer invocation call. // of add peer invocation call.
type AddPeerArgs struct { type AddPeerArgs struct {
info []byte info []byte
client.InvokePrmOptional
} }
// SetInfo sets the peer information. // SetInfo sets the peer information.
@ -24,6 +26,7 @@ func (c *Client) AddPeer(args AddPeerArgs) error {
prm.SetMethod(c.addPeerMethod) prm.SetMethod(c.addPeerMethod)
prm.SetArgs(args.info) prm.SetArgs(args.info)
prm.InvokePrmOptional = args.InvokePrmOptional
if err := c.client.Invoke(prm); err != nil { 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", c.addPeerMethod, err)

View file

@ -13,17 +13,17 @@ type ConfigArgs struct {
key []byte key []byte
} }
// SetKey sets binary key to configuration parameter.
func (c *ConfigArgs) SetKey(v []byte) {
c.key = v
}
// ConfigValues groups the stack parameters // ConfigValues groups the stack parameters
// returned by get config test invoke. // returned by get config test invoke.
type ConfigValues struct { type ConfigValues struct {
val interface{} val interface{}
} }
// SetKey sets binary key to configuration parameter.
func (c *ConfigArgs) SetKey(v []byte) {
c.key = v
}
// Value returns configuration value. // Value returns configuration value.
func (c ConfigValues) Value() interface{} { func (c ConfigValues) Value() interface{} {
return c.val return c.val
@ -58,12 +58,37 @@ func (c *Client) Config(args ConfigArgs, assert func(stackitem.Item) (interface{
}, nil }, nil
} }
// 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
}
// SetConfig invokes `setConfig` method of NeoFS Netmap contract. // SetConfig invokes `setConfig` method of NeoFS Netmap contract.
func (c *Client) SetConfig(id, key []byte, value interface{}) error { func (c *Client) SetConfig(args SetConfigPrm) error {
prm := client.InvokePrm{} prm := client.InvokePrm{}
prm.SetMethod(c.setConfigMethod) prm.SetMethod(c.setConfigMethod)
prm.SetArgs(id, key, value) prm.SetArgs(args.id, args.key, args.value)
prm.InvokePrmOptional = args.InvokePrmOptional
return c.client.Invoke(prm) return c.client.Invoke(prm)
} }
@ -120,7 +145,7 @@ func (x ListConfigValues) IterateRecords(f func(key, value []byte) error) error
} }
// ListConfig performs the test invoke of config listing method of NeoFS Netmap contract. // ListConfig performs the test invoke of config listing method of NeoFS Netmap contract.
func (c *Client) ListConfig(args ListConfigArgs) (*ListConfigValues, error) { func (c *Client) ListConfig(_ ListConfigArgs) (*ListConfigValues, error) {
prm := client.TestInvokePrm{} prm := client.TestInvokePrm{}
prm.SetMethod(c.configListMethod) prm.SetMethod(c.configListMethod)

View file

@ -8,8 +8,7 @@ import (
// EpochArgs groups the arguments // EpochArgs groups the arguments
// of get epoch number test invoke call. // of get epoch number test invoke call.
type EpochArgs struct { type EpochArgs struct{}
}
// EpochValues groups the stack parameters // EpochValues groups the stack parameters
// returned by get epoch number test invoke. // returned by get epoch number test invoke.
@ -22,22 +21,6 @@ func (e EpochValues) Number() int64 {
return e.num return e.num
} }
// EpochBlockArgs groups the arguments of
// get epoch block number test invoke call.
type EpochBlockArgs struct {
}
// EpochBlockValues groups the stack parameters
// returned by get epoch block number test invoke.
type EpochBlockValues struct {
block int64
}
// Block return the block number of NeoFS epoch.
func (e EpochBlockValues) Block() int64 {
return e.block
}
// Epoch performs the test invoke of get epoch number // Epoch performs the test invoke of get epoch number
// method of NeoFS Netmap contract. // method of NeoFS Netmap contract.
func (c *Client) Epoch(_ EpochArgs) (*EpochValues, error) { func (c *Client) Epoch(_ EpochArgs) (*EpochValues, error) {
@ -65,6 +48,21 @@ func (c *Client) Epoch(_ EpochArgs) (*EpochValues, error) {
}, nil }, nil
} }
// EpochBlockArgs groups the arguments of
// get epoch block number test invoke call.
type EpochBlockArgs struct{}
// EpochBlockValues groups the stack parameters
// returned by get epoch block number test invoke.
type EpochBlockValues struct {
block int64
}
// Block return the block number of NeoFS epoch.
func (e EpochBlockValues) Block() int64 {
return e.block
}
// LastEpochBlock performs the test invoke of get epoch block number // LastEpochBlock performs the test invoke of get epoch block number
// method of NeoFS Netmap contract. // method of NeoFS Netmap contract.
func (c *Client) LastEpochBlock(_ EpochBlockArgs) (*EpochBlockValues, error) { func (c *Client) LastEpochBlock(_ EpochBlockArgs) (*EpochBlockValues, error) {

View file

@ -9,17 +9,32 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client"
) )
// UpdateIRPrm groups parameters of UpdateInnerRing
// invocation.
type UpdateIRPrm struct {
keys keys.PublicKeys
client.InvokePrmOptional
}
// SetKeys sets new inner ring keys.
func (u *UpdateIRPrm) SetKeys(keys keys.PublicKeys) {
u.keys = keys
}
// UpdateInnerRing updates inner ring members in netmap contract. // UpdateInnerRing updates inner ring members in netmap contract.
func (c *Client) UpdateInnerRing(keys keys.PublicKeys) error { func (c *Client) UpdateInnerRing(p UpdateIRPrm) error {
args := make([][]byte, len(keys)) args := make([][]byte, len(p.keys))
for i := range args { for i := range args {
args[i] = keys[i].Bytes() args[i] = p.keys[i].Bytes()
} }
prm := client.InvokePrm{} prm := client.InvokePrm{}
prm.SetMethod(c.updateInnerRing) prm.SetMethod(c.updateInnerRing)
prm.SetArgs(args) prm.SetArgs(args)
prm.InvokePrmOptional = p.InvokePrmOptional
return c.client.Invoke(prm) return c.client.Invoke(prm)
} }

View file

@ -7,50 +7,12 @@ import (
"github.com/nspcc-dev/neofs-node/pkg/morph/client" "github.com/nspcc-dev/neofs-node/pkg/morph/client"
) )
// GetNetMapArgs groups the arguments
// of get network map test invoke call.
type GetNetMapArgs struct {
}
// GetSnapshotArgs groups the arguments
// of get netmap snapshot test invoke call.
type GetSnapshotArgs struct {
diff uint64
}
// GetNetMapValues groups the stack parameters // GetNetMapValues groups the stack parameters
// returned by get network map test invoke. // returned by get network map test invoke.
type GetNetMapValues struct { type GetNetMapValues struct {
peers [][]byte peers [][]byte
} }
// EpochSnapshotArgs groups the arguments
// of snapshot by epoch test invoke call.
type EpochSnapshotArgs struct {
epoch uint64
}
// EpochSnapshotValues groups the stack parameters
// returned by snapshot by epoch test invoke.
type EpochSnapshotValues struct {
*GetNetMapValues
}
// GetNetMapCandidatesArgs groups the arguments
// of get network map candidates test invoke call.
type GetNetMapCandidatesArgs struct {
}
// GetNetMapCandidatesValues groups the stack parameters
// returned by get network map candidates test invoke.
type GetNetMapCandidatesValues struct {
netmapNodes []*PeerWithState
}
func (g GetNetMapCandidatesValues) NetmapNodes() []*PeerWithState {
return g.netmapNodes
}
// State is an enumeration of various states of the NeoFS node. // State is an enumeration of various states of the NeoFS node.
type State int64 type State int64
@ -86,23 +48,16 @@ const (
peerWithStateFixedPrmNumber = 2 peerWithStateFixedPrmNumber = 2
) )
// SetDiff sets argument for snapshot method of
// netmap contract.
func (g *GetSnapshotArgs) SetDiff(d uint64) {
g.diff = d
}
// SetEpoch sets epoch number to get snapshot.
func (a *EpochSnapshotArgs) SetEpoch(d uint64) {
a.epoch = d
}
// Peers return the list of peers from // Peers return the list of peers from
// network map in a binary format. // network map in a binary format.
func (g GetNetMapValues) Peers() [][]byte { func (g GetNetMapValues) Peers() [][]byte {
return g.peers return g.peers
} }
// GetNetMapArgs groups the arguments
// of get network map test invoke call.
type GetNetMapArgs struct{}
// NetMap performs the test invoke of get network map // NetMap performs the test invoke of get network map
// method of NeoFS Netmap contract. // method of NeoFS Netmap contract.
func (c *Client) NetMap(_ GetNetMapArgs) (*GetNetMapValues, error) { func (c *Client) NetMap(_ GetNetMapArgs) (*GetNetMapValues, error) {
@ -118,6 +73,18 @@ func (c *Client) NetMap(_ GetNetMapArgs) (*GetNetMapValues, error) {
return peersFromStackItems(prms, c.netMapMethod) return peersFromStackItems(prms, c.netMapMethod)
} }
// GetSnapshotArgs groups the arguments
// of get netmap snapshot test invoke call.
type GetSnapshotArgs struct {
diff uint64
}
// SetDiff sets argument for snapshot method of
// netmap contract.
func (g *GetSnapshotArgs) SetDiff(d uint64) {
g.diff = d
}
// Snapshot performs the test invoke of get snapshot of network map // Snapshot performs the test invoke of get snapshot of network map
// from NeoFS Netmap contract. Contract saves only one previous epoch, // from NeoFS Netmap contract. Contract saves only one previous epoch,
// so all invokes with diff > 1 return error. // so all invokes with diff > 1 return error.
@ -136,6 +103,23 @@ func (c *Client) Snapshot(a GetSnapshotArgs) (*GetNetMapValues, error) {
return peersFromStackItems(prms, c.snapshotMethod) return peersFromStackItems(prms, c.snapshotMethod)
} }
// EpochSnapshotArgs groups the arguments
// of snapshot by epoch test invoke call.
type EpochSnapshotArgs struct {
epoch uint64
}
// SetEpoch sets epoch number to get snapshot.
func (a *EpochSnapshotArgs) SetEpoch(d uint64) {
a.epoch = d
}
// EpochSnapshotValues groups the stack parameters
// returned by snapshot by epoch test invoke.
type EpochSnapshotValues struct {
*GetNetMapValues
}
// EpochSnapshot performs the test invoke of get snapshot of network map by epoch // EpochSnapshot performs the test invoke of get snapshot of network map by epoch
// from NeoFS Netmap contract. // from NeoFS Netmap contract.
func (c *Client) EpochSnapshot(args EpochSnapshotArgs) (*EpochSnapshotValues, error) { func (c *Client) EpochSnapshot(args EpochSnapshotArgs) (*EpochSnapshotValues, error) {
@ -160,6 +144,20 @@ func (c *Client) EpochSnapshot(args EpochSnapshotArgs) (*EpochSnapshotValues, er
}, nil }, nil
} }
// GetNetMapCandidatesArgs groups the arguments
// of get network map candidates test invoke call.
type GetNetMapCandidatesArgs struct{}
// GetNetMapCandidatesValues groups the stack parameters
// returned by get network map candidates test invoke.
type GetNetMapCandidatesValues struct {
netmapNodes []*PeerWithState
}
func (g GetNetMapCandidatesValues) NetmapNodes() []*PeerWithState {
return g.netmapNodes
}
func (c *Client) Candidates(_ GetNetMapCandidatesArgs) (*GetNetMapCandidatesValues, error) { func (c *Client) Candidates(_ GetNetMapCandidatesArgs) (*GetNetMapCandidatesValues, error) {
invokePrm := client.TestInvokePrm{} invokePrm := client.TestInvokePrm{}
invokePrm.SetMethod(c.netMapCandidatesMethod) invokePrm.SetMethod(c.netMapCandidatesMethod)

View file

@ -10,6 +10,8 @@ import (
// of new epoch invocation call. // of new epoch invocation call.
type NewEpochArgs struct { type NewEpochArgs struct {
number int64 // new epoch number number int64 // new epoch number
client.InvokePrmOptional
} }
// SetEpochNumber sets the new epoch number. // SetEpochNumber sets the new epoch number.
@ -24,6 +26,7 @@ func (c *Client) NewEpoch(args NewEpochArgs) error {
prm.SetMethod(c.newEpochMethod) prm.SetMethod(c.newEpochMethod)
prm.SetArgs(args.number) prm.SetArgs(args.number)
prm.InvokePrmOptional = args.InvokePrmOptional
if err := c.client.Invoke(prm); err != nil { 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", c.newEpochMethod, err)

View file

@ -12,6 +12,8 @@ type UpdateStateArgs struct {
key []byte // peer public key key []byte // peer public key
state int64 // new peer state state int64 // new peer state
client.InvokePrmOptional
} }
// SetPublicKey sets peer public key // SetPublicKey sets peer public key
@ -32,9 +34,9 @@ func (c *Client) UpdateState(args UpdateStateArgs) error {
prm.SetMethod(c.updateStateMethod) prm.SetMethod(c.updateStateMethod)
prm.SetArgs(args.state, args.key) prm.SetArgs(args.state, args.key)
prm.InvokePrmOptional = args.InvokePrmOptional
err := c.client.Invoke(prm) err := c.client.Invoke(prm)
if err != nil { if err != nil {
return fmt.Errorf("could not invoke method (%s): %w", c.updateStateMethod, err) return fmt.Errorf("could not invoke method (%s): %w", c.updateStateMethod, err)
} }

View file

@ -4,23 +4,37 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap" "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
) )
// AddPeerPrm groups parameters of AddPeer operation.
type AddPeerPrm struct {
nodeInfo *netmap.NodeInfo
client.InvokePrmOptional
}
// SetNodeInfo sets new peer NodeInfo.
func (a *AddPeerPrm) SetNodeInfo(nodeInfo *netmap.NodeInfo) {
a.nodeInfo = nodeInfo
}
// AddPeer registers peer in NeoFS network through // AddPeer registers peer in NeoFS network through
// Netmap contract call. // Netmap contract call.
func (w *Wrapper) AddPeer(nodeInfo *netmap.NodeInfo) error { func (w *Wrapper) AddPeer(prm AddPeerPrm) error {
if nodeInfo == nil { if prm.nodeInfo == nil {
return errors.New("nil node info") return errors.New("nil node info")
} }
rawNodeInfo, err := nodeInfo.Marshal() rawNodeInfo, err := prm.nodeInfo.Marshal()
if err != nil { if err != nil {
return err return err
} }
args := netmap.AddPeerArgs{} args := netmap.AddPeerArgs{}
args.SetInfo(rawNodeInfo) args.SetInfo(rawNodeInfo)
args.InvokePrmOptional = prm.InvokePrmOptional
if err := w.client.AddPeer(args); err != nil { if err := w.client.AddPeer(args); err != nil {
return fmt.Errorf("could not invoke smart contract: %w", err) return fmt.Errorf("could not invoke smart contract: %w", err)

View file

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"strconv" "strconv"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/nspcc-dev/neo-go/pkg/encoding/bigint" "github.com/nspcc-dev/neo-go/pkg/encoding/bigint"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap" "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
) )
@ -168,9 +170,40 @@ func (w *Wrapper) readStringConfig(key string) (string, error) {
return str, nil return str, nil
} }
// 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
}
// SetConfig sets config field. // SetConfig sets config field.
func (w *Wrapper) SetConfig(id, key []byte, value interface{}) error { func (w *Wrapper) SetConfig(prm SetConfigPrm) error {
return w.client.SetConfig(id, key, value) args := netmap.SetConfigPrm{}
args.SetID(prm.id)
args.SetKey(prm.key)
args.SetValue(prm.value)
args.InvokePrmOptional = prm.InvokePrmOptional
return w.client.SetConfig(args)
} }
// IterateConfigParameters iterates over configuration parameters stored in Netmap contract and passes them to f. // IterateConfigParameters iterates over configuration parameters stored in Netmap contract and passes them to f.

View file

@ -1,10 +1,32 @@
package wrapper package wrapper
import "github.com/nspcc-dev/neo-go/pkg/crypto/keys" import (
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
)
// UpdateIRPrm groups parameters of UpdateInnerRing
// invocation.
type UpdateIRPrm struct {
keys keys.PublicKeys
client.InvokePrmOptional
}
// SetKeys sets new inner ring keys.
func (u *UpdateIRPrm) SetKeys(keys keys.PublicKeys) {
u.keys = keys
}
// UpdateInnerRing updates inner ring keys. // UpdateInnerRing updates inner ring keys.
func (w *Wrapper) UpdateInnerRing(keys keys.PublicKeys) error { func (w *Wrapper) UpdateInnerRing(prm UpdateIRPrm) error {
return w.client.UpdateInnerRing(keys) args := netmap.UpdateIRPrm{}
args.SetKeys(prm.keys)
args.InvokePrmOptional = prm.InvokePrmOptional
return w.client.UpdateInnerRing(args)
} }
// GetInnerRingList return current IR list. // GetInnerRingList return current IR list.

View file

@ -3,16 +3,37 @@ package wrapper
import ( import (
"fmt" "fmt"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
contract "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap" contract "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/nspcc-dev/neofs-sdk-go/netmap" "github.com/nspcc-dev/neofs-sdk-go/netmap"
) )
// UpdatePeerPrm groups parameters of UpdatePeerState operation.
type UpdatePeerPrm struct {
key []byte
state netmap.NodeState
client.InvokePrmOptional
}
// SetKey sets public key.
func (u *UpdatePeerPrm) SetKey(key []byte) {
u.key = key
}
// SetState sets node state.
func (u *UpdatePeerPrm) SetState(state netmap.NodeState) {
u.state = state
}
// UpdatePeerState changes peer status through Netmap contract // UpdatePeerState changes peer status through Netmap contract
// call. // call.
func (w *Wrapper) UpdatePeerState(key []byte, state netmap.NodeState) error { func (w *Wrapper) UpdatePeerState(prm UpdatePeerPrm) error {
args := contract.UpdateStateArgs{} args := contract.UpdateStateArgs{}
args.SetPublicKey(key)
args.SetState(int64(state.ToV2())) args.SetPublicKey(prm.key)
args.SetState(int64(prm.state.ToV2()))
args.InvokePrmOptional = prm.InvokePrmOptional
// invoke smart contract call // invoke smart contract call
if err := w.client.UpdateState(args); err != nil { if err := w.client.UpdateState(args); err != nil {