Add Inner Ring code

This commit is contained in:
Stanislav Bogatyrev 2020-07-24 16:54:03 +03:00
parent dadfd90dcd
commit b7b5079934
400 changed files with 11420 additions and 8690 deletions

View file

@ -0,0 +1,104 @@
package netmap
import (
"github.com/pkg/errors"
)
// PeerInfo groups the parameters of
// new NeoFS peer.
type PeerInfo struct {
address []byte // peer network address in a binary format
key []byte // peer public key
opts [][]byte // binary peer options
}
// AddPeerArgs groups the arguments
// of add peer invocation call.
type AddPeerArgs struct {
info PeerInfo // peer information
}
const addPeerFixedArgNumber = 2
// Address returns the peer network address
// in a binary format.
//
// Address format is dictated by application
// architecture.
func (a PeerInfo) Address() []byte {
return a.address
}
// SetAddress sets the peer network address
// in a binary format.
//
// Address format is dictated by application
// architecture.
func (a *PeerInfo) SetAddress(v []byte) {
a.address = v
}
// PublicKey returns the peer public key
// in a binary format.
//
// Key format is dictated by application
// architecture.
func (a PeerInfo) PublicKey() []byte {
return a.key
}
// SetPublicKey sets the peer public key
// in a binary format.
//
// Key format is dictated by application
// architecture.
func (a *PeerInfo) SetPublicKey(v []byte) {
a.key = v
}
// Options returns the peer options
// in a binary format.
//
// Option format is dictated by application
// architecture.
func (a PeerInfo) Options() [][]byte {
return a.opts
}
// SetOptions sets the peer options
// in a binary format.
//
// Option format is dictated by application
// architecture.
func (a *PeerInfo) SetOptions(v [][]byte) {
a.opts = v
}
// SetInfo sets the peer information.
func (a *AddPeerArgs) SetInfo(v PeerInfo) {
a.info = v
}
// AddPeer invokes the call of add peer method
// of NeoFS Netmap contract.
func (c *Client) AddPeer(args AddPeerArgs) error {
info := args.info
invokeArgs := make([]interface{}, 0, addPeerFixedArgNumber+len(info.opts))
invokeArgs = append(invokeArgs,
info.address,
info.key,
)
for i := range info.opts {
invokeArgs = append(invokeArgs, info.opts[i])
}
return errors.Wrapf(c.client.Invoke(
c.addPeerMethod,
invokeArgs...,
), "could not invoke method (%s)", c.addPeerMethod)
}

View file

@ -0,0 +1,156 @@
package netmap
import (
"errors"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
)
// Client is a wrapper over StaticClient
// which makes calls with the names and arguments
// of the NeoFS Netmap contract.
//
// Working client must be created via constructor New.
// Using the Client that has been created with new(Client)
// expression (or just declaring a Client variable) is unsafe
// 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
netMapMethod, // get network map method name
updateStateMethod, // update state method name for invocation
innerRingListMethod string // IR list method name for invocation
}
const (
defaultAddPeerMethod = "AddPeer" // default add peer method name
defaultNewEpochMethod = "NewEpoch" // default new epoch method name
defaultNetMapMethod = "Netmap" // default get network map method name
defaultUpdateStateMethod = "UpdateState" // default update state method name
defaultInnerRIngListMethod = "InnerRingList" // default IR list method name
)
func defaultConfig() *cfg {
return &cfg{
addPeerMethod: defaultAddPeerMethod,
newEpochMethod: defaultNewEpochMethod,
netMapMethod: defaultNetMapMethod,
updateStateMethod: defaultUpdateStateMethod,
innerRingListMethod: defaultInnerRIngListMethod,
}
}
// 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;
// * inner ring list method name: InnerRingList.
//
// 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) {
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
}
// 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
}
}
}
// WithInnerRingListMethod returns a client constructor option that
// specifies the method name of inner ring listing operation.
//
// Ignores empty value.
//
// If option not provided, "InnerRingList" is used.
func WithInnerRingListMethod(n string) Option {
return func(c *cfg) {
if n != "" {
c.innerRingListMethod = n
}
}
}

View file

@ -0,0 +1,61 @@
package netmap
import (
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/pkg/errors"
)
// InnerRingListArgs groups the arguments
// of inner ring list test invoke call.
type InnerRingListArgs struct {
}
// InnerRingListValues groups the stack parameters
// returned by inner ring list test invoke.
type InnerRingListValues struct {
keys [][]byte // list of keys of IR nodes in a binary format
}
// KeyList return the list of IR node keys
// in a binary format.
func (g InnerRingListValues) KeyList() [][]byte {
return g.keys
}
// InnerRingList performs the test invoke of inner ring list
// method of NeoFS Netmap contract.
func (c *Client) InnerRingList(args InnerRingListArgs) (*InnerRingListValues, error) {
prms, err := c.client.TestInvoke(
c.innerRingListMethod,
)
if err != nil {
return nil, errors.Wrapf(err, "could not perform test invocation (%s)", c.innerRingListMethod)
} else if ln := len(prms); ln != 1 {
return nil, errors.Errorf("unexpected stack item count (%s): %d", c.innerRingListMethod, ln)
}
prms, err = client.ArrayFromStackParameter(prms[0])
if err != nil {
return nil, errors.Wrapf(err, "could not get stack item array from stack item (%s)", c.innerRingListMethod)
}
res := &InnerRingListValues{
keys: make([][]byte, 0, len(prms)),
}
for i := range prms {
nodePrms, err := client.ArrayFromStackParameter(prms[i])
if err != nil {
return nil, errors.Wrap(err, "could not get stack item array (Node #%d)")
}
key, err := client.BytesFromStackParameter(nodePrms[0])
if err != nil {
return nil, errors.Wrapf(err, "could not parse stack item (Key #%d)", i)
}
res.keys = append(res.keys, key)
}
return res, nil
}

View file

@ -0,0 +1,99 @@
package netmap
import (
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/pkg/errors"
)
// GetNetMapArgs groups the arguments
// of get network map test invoke call.
type GetNetMapArgs struct {
}
// GetNetMapValues groups the stack parameters
// returned by get network map test invoke.
type GetNetMapValues struct {
peers []PeerInfo // peer list in a binary format
}
const nodeInfoFixedPrmNumber = 3
// Peers return the list of peers from
// network map in a binary format.
func (g GetNetMapValues) Peers() []PeerInfo {
return g.peers
}
// NetMap performs the test invoke of get network map
// method of NeoFS Netmap contract.
func (c *Client) NetMap(args GetNetMapArgs) (*GetNetMapValues, error) {
prms, err := c.client.TestInvoke(
c.netMapMethod,
)
if err != nil {
return nil, errors.Wrapf(err, "could not perform test invocation (%s)", c.netMapMethod)
} else if ln := len(prms); ln != 1 {
return nil, errors.Errorf("unexpected stack item count (%s): %d", c.netMapMethod, ln)
}
prms, err = client.ArrayFromStackParameter(prms[0])
if err != nil {
return nil, errors.Wrapf(err, "could not get stack item array from stack item (%s)", c.netMapMethod)
}
res := &GetNetMapValues{
peers: make([]PeerInfo, 0, len(prms)),
}
for i := range prms {
peer, err := peerInfoFromStackItem(prms[i])
if err != nil {
return nil, errors.Wrapf(err, "could not parse stack item (Peer #%d)", i)
}
res.peers = append(res.peers, *peer)
}
return res, nil
}
func peerInfoFromStackItem(prm smartcontract.Parameter) (*PeerInfo, error) {
prms, err := client.ArrayFromStackParameter(prm)
if err != nil {
return nil, errors.Wrapf(err, "could not get stack item array (PeerInfo)")
} else if ln := len(prms); ln != nodeInfoFixedPrmNumber {
return nil, errors.Errorf("unexpected stack item count (PeerInfo): expected %d, has %d", 3, ln)
}
res := new(PeerInfo)
// Address
res.address, err = client.BytesFromStackParameter(prms[0])
if err != nil {
return nil, errors.Wrap(err, "could not get byte array from stack item (Address)")
}
// Public key
if res.key, err = client.BytesFromStackParameter(prms[1]); err != nil {
return nil, errors.Wrap(err, "could not get byte array from stack item (Public key)")
}
// Options
if prms, err = client.ArrayFromStackParameter(prms[2]); err != nil {
return nil, errors.Wrapf(err, "could not get stack item array (Options)")
}
res.opts = make([][]byte, 0, len(prms))
for i := range prms {
opt, err := client.BytesFromStackParameter(prms[i])
if err != nil {
return nil, errors.Wrapf(err, "could not get byte array from stack item (Option #%d)", i)
}
res.opts = append(res.opts, opt)
}
return res, nil
}

View file

@ -0,0 +1,23 @@
package netmap
import "github.com/pkg/errors"
// NewEpochArgs groups the arguments
// of new epoch invocation call.
type NewEpochArgs struct {
number int64 // new epoch number
}
// SetEpochNumber sets the new epoch number.
func (a *NewEpochArgs) SetEpochNumber(v int64) {
a.number = v
}
// NewEpoch invokes the call of new epoch method
// of NeoFS Netmap contract.
func (c *Client) NewEpoch(args NewEpochArgs) error {
return errors.Wrapf(c.client.Invoke(
c.addPeerMethod,
args.number,
), "could not invoke method (%s)", c.newEpochMethod)
}

View file

@ -0,0 +1,34 @@
package netmap
import (
"github.com/pkg/errors"
)
// UpdateStateArgs groups the arguments
// of update state invocation call.
type UpdateStateArgs struct {
key []byte // peer public key
state int64 // new peer state
}
// SetPublicKey sets peer public key
// in a binary format.
func (u *UpdateStateArgs) SetPublicKey(v []byte) {
u.key = v
}
// SetState sets the new peer state.
func (u *UpdateStateArgs) SetState(v int64) {
u.state = v
}
// UpdateState invokes the call of update state method
// of NeoFS Netmap contract.
func (c *Client) UpdateState(args UpdateStateArgs) error {
return errors.Wrapf(c.client.Invoke(
c.addPeerMethod,
args.key,
args.state,
), "could not invoke method (%s)", c.updateStateMethod)
}

View file

@ -0,0 +1,37 @@
package wrapper
import (
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
contract "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/pkg/errors"
)
// AddPeer registers peer in NeoFS network through
// Netmap contract call.
func (w *Wrapper) AddPeer(nodeInfo netmap.Info) error {
// prepare invocation arguments
args := contract.AddPeerArgs{}
info := contract.PeerInfo{}
info.SetPublicKey(nodeInfo.PublicKey())
info.SetAddress([]byte(nodeInfo.Address()))
opts := nodeInfo.Options()
binOpts := make([][]byte, 0, len(opts))
for i := range opts {
binOpts = append(binOpts, []byte(opts[i]))
}
info.SetOptions(binOpts)
args.SetInfo(info)
// invoke smart contract call
//
// Note: errors.Wrap returns nil on nil error arg.
return errors.Wrap(
w.client.AddPeer(args),
"could not invoke smart contract",
)
}

View file

@ -0,0 +1,21 @@
package wrapper
import (
contract "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/pkg/errors"
)
// InnerRingKeys receives public key list of inner
// ring nodes through Netmap contract call and returns it.
func (w *Wrapper) InnerRingKeys() ([][]byte, error) {
// prepare invocation arguments
args := contract.InnerRingListArgs{}
// invoke smart contract call
values, err := w.client.InnerRingList(args)
if err != nil {
return nil, errors.Wrap(err, "could not invoke smart contract")
}
return values.KeyList(), nil
}

View file

@ -0,0 +1,60 @@
package wrapper
import (
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
contract "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/pkg/errors"
)
// NetMap represents the NeoFS network map.
//
// It is a type alias of
// github.com/nspcc-dev/neofs-node/pkg/core/NetMap.
type NetMap = netmap.NetMap
// Info represents node information.
//
// It is a type alias of
// github.com/nspcc-dev/neofs-node/pkg/core/netmap.Info.
type Info = netmap.Info
// GetNetMap receives information list about storage nodes
// through the Netmap contract call, composes network map
// from them and returns it.
func (w *Wrapper) GetNetMap() (*NetMap, error) {
// prepare invocation arguments
args := contract.GetNetMapArgs{}
// invoke smart contract call
values, err := w.client.NetMap(args)
if err != nil {
return nil, errors.Wrap(err, "could not invoke smart contract")
}
// parse response and fill the network map
nm := netmap.New()
peerList := values.Peers()
for i := range peerList {
info := Info{}
info.SetPublicKey(peerList[i].PublicKey())
info.SetAddress(string(peerList[i].Address()))
binOpts := peerList[i].Options()
opts := make([]string, 0, len(binOpts))
for j := range binOpts {
opts = append(opts, string(binOpts[j]))
}
info.SetOptions(opts)
if err := nm.AddNode(info); err != nil {
return nil, errors.Wrapf(err, "could not add node #%d to network map", i)
}
}
return nm, nil
}

View file

@ -0,0 +1,23 @@
package wrapper
import (
"github.com/nspcc-dev/neofs-node/pkg/core/netmap/epoch"
contract "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/pkg/errors"
)
// NewEpoch updates NeoFS epoch number through
// Netmap contract call.
func (w *Wrapper) NewEpoch(e epoch.Epoch) error {
// prepare invocation arguments
args := contract.NewEpochArgs{}
args.SetEpochNumber(int64(epoch.ToUint64(e)))
// invoke smart contract call
//
// Note: errors.Wrap returns nil on nil error arg.
return errors.Wrap(
w.client.NewEpoch(args),
"could not invoke smart contract",
)
}

View file

@ -0,0 +1,32 @@
package wrapper
import (
contract "github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
"github.com/pkg/errors"
)
// NodeState is a type of node states enumeration.
type NodeState int64
const (
_ NodeState = iota
// StateOffline is an offline node state value.
StateOffline
)
// UpdatePeerState changes peer status through Netmap contract
// call.
func (w *Wrapper) UpdatePeerState(key []byte, state NodeState) error {
args := contract.UpdateStateArgs{}
args.SetPublicKey(key)
args.SetState(int64(state))
// invoke smart contract call
//
// Note: errors.Wrap returns nil on nil error arg.
return errors.Wrap(
w.client.UpdateState(args),
"could not invoke smart contract",
)
}

View file

@ -0,0 +1,43 @@
package wrapper
import (
"errors"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap"
)
// Client represents the Netmap contract client.
//
// It is a type alias of
// github.com/nspcc-dev/neofs-node/pkg/morph/client/netmap.Client.
type Client = netmap.Client
// Wrapper is a wrapper over netmap contract
// client which implements:
// * network map storage;
// * tool for peer state updating.
//
// Working wrapper must be created via constructor New.
// Using the Wrapper that has been created with new(Wrapper)
// expression (or just declaring a Wrapper variable) is unsafe
// and can lead to panic.
type Wrapper struct {
client *Client
}
// ErrNilWrapper is returned by functions that expect
// a non-nil Wrapper pointer, but received nil.
var ErrNilWrapper = errors.New("netmap contract client wrapper is nil")
// New creates, initializes and returns the Wrapper instance.
//
// If Client is nil, netmap.ErrNilClient is returned.
func New(c *Client) (*Wrapper, error) {
if c == nil {
return nil, netmap.ErrNilClient
}
return &Wrapper{
client: c,
}, nil
}