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,174 @@
package container
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 Container 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 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
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 string // get eACL method name for invocation
}
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
)
func defaultConfig() *cfg {
return &cfg{
putMethod: defaultPutMethod,
deleteMethod: defaultDeleteMethod,
getMethod: defaultGetMethod,
listMethod: defaultListMethod,
setEACLMethod: defaultSetEACLMethod,
eaclMethod: defaultEACLMethod,
}
}
// 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.
//
// 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
}
// 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
}
}
}

View file

@ -0,0 +1,42 @@
package container
import "github.com/pkg/errors"
// DeleteArgs groups the arguments
// of delete container invocation call.
type DeleteArgs struct {
cid []byte // container identifier
ownerID []byte // container owner identifier
sig []byte // container identifier signature
}
// SetOwnerID sets the container owner identifier
// in a binary format.
func (p *DeleteArgs) SetOwnerID(v []byte) {
p.ownerID = v
}
// SetCID sets the container identifier
// in a binary format.
func (p *DeleteArgs) SetCID(v []byte) {
p.cid = v
}
// SetSignature sets the container identifier
// owner's signature.
func (p *DeleteArgs) SetSignature(v []byte) {
p.sig = v
}
// Delete invokes the call of delete container
// method of NeoFS Container contract.
func (c *Client) Delete(args DeleteArgs) error {
return errors.Wrapf(c.client.Invoke(
c.deleteMethod,
args.cid,
args.ownerID,
args.sig,
), "could not invoke method (%s)", c.deleteMethod)
}

View file

@ -0,0 +1,53 @@
package container
import (
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/pkg/errors"
)
// EACLArgs groups the arguments
// of get eACL test invoke call.
type EACLArgs struct {
cid []byte // container identifier
}
// EACLValues groups the stack parameters
// returned by get eACL test invoke.
type EACLValues struct {
eacl []byte // extended ACL table
}
// SetCID sets the container identifier
// in a binary format.
func (g *EACLArgs) SetCID(v []byte) {
g.cid = v
}
// EACL returns the eACL table
// in a binary format.
func (g *EACLValues) EACL() []byte {
return g.eacl
}
// EACL performs the test invoke of get eACL
// method of NeoFS Container contract.
func (c *Client) EACL(args EACLArgs) (*EACLValues, error) {
prms, err := c.client.TestInvoke(
c.eaclMethod,
args.cid,
)
if err != nil {
return nil, errors.Wrapf(err, "could not perform test invocation (%s)", c.eaclMethod)
} else if ln := len(prms); ln != 1 {
return nil, errors.Errorf("unexpected stack item count (%s): %d", c.eaclMethod, ln)
}
eacl, err := client.BytesFromStackParameter(prms[0])
if err != nil {
return nil, errors.Wrapf(err, "could not get byte array from stack item (%s)", c.eaclMethod)
}
return &EACLValues{
eacl: eacl,
}, nil
}

View file

@ -0,0 +1,42 @@
package container
import "github.com/pkg/errors"
// SetEACLArgs groups the arguments
// of set eACL invocation call.
type SetEACLArgs struct {
cid []byte // container identifier in a binary format
eacl []byte // extended ACL table
sig []byte // eACL table signature
}
// SetCID sets the container identifier
// in a binary format.
func (p *SetEACLArgs) SetCID(v []byte) {
p.cid = v
}
// SetEACL sets the extended ACL table
// in a binary format.
func (p *SetEACLArgs) SetEACL(v []byte) {
p.eacl = v
}
// SetSignature sets the eACL table structure
// owner's signature.
func (p *SetEACLArgs) SetSignature(v []byte) {
p.sig = v
}
// SetEACL invokes the call of set eACL method
// of NeoFS Container contract.
func (c *Client) SetEACL(args SetEACLArgs) error {
return errors.Wrapf(c.client.Invoke(
c.setEACLMethod,
args.cid,
args.eacl,
args.sig,
), "could not invoke method (%s)", c.setEACLMethod)
}

View file

@ -0,0 +1,53 @@
package container
import (
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/pkg/errors"
)
// GetArgs groups the arguments
// of get container test invoke call.
type GetArgs struct {
cid []byte // container identifier
}
// GetValues groups the stack parameters
// returned by get container test invoke.
type GetValues struct {
cnr []byte // container in a binary form
}
// SetCID sets the container identifier
// in a binary format.
func (g *GetArgs) SetCID(v []byte) {
g.cid = v
}
// Container returns the container
// in a binary format.
func (g *GetValues) Container() []byte {
return g.cnr
}
// Get performs the test invoke of get container
// method of NeoFS Container contract.
func (c *Client) Get(args GetArgs) (*GetValues, error) {
prms, err := c.client.TestInvoke(
c.getMethod,
args.cid,
)
if err != nil {
return nil, errors.Wrapf(err, "could not perform test invocation (%s)", c.getMethod)
} else if ln := len(prms); ln != 1 {
return nil, errors.Errorf("unexpected stack item count (%s): %d", c.getMethod, ln)
}
cnrBytes, err := client.BytesFromStackParameter(prms[0])
if err != nil {
return nil, errors.Wrapf(err, "could not get byte array from stack item (%s)", c.getMethod)
}
return &GetValues{
cnr: cnrBytes,
}, nil
}

View file

@ -0,0 +1,70 @@
package container
import (
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
"github.com/pkg/errors"
)
// ListArgs groups the arguments
// of list containers test invoke call.
type ListArgs struct {
ownerID []byte // container owner identifier
}
// ListValues groups the stack parameters
// returned by list containers test invoke.
type ListValues struct {
cidList [][]byte // list of container identifiers
}
// SetOwnerID sets the container owner identifier
// in a binary format.
func (l *ListArgs) SetOwnerID(v []byte) {
l.ownerID = v
}
// CIDList returns the list of container
// identifiers in a binary format.
func (l *ListValues) CIDList() [][]byte {
return l.cidList
}
// List performs the test invoke of list container
// method of NeoFS Container contract.
func (c *Client) List(args ListArgs) (*ListValues, error) {
invokeArgs := make([]interface{}, 0, 1)
if len(args.ownerID) > 0 {
invokeArgs = append(invokeArgs, args.ownerID)
}
prms, err := c.client.TestInvoke(
c.listMethod,
invokeArgs...,
)
if err != nil {
return nil, errors.Wrapf(err, "could not perform test invocation (%s)", c.listMethod)
} else if ln := len(prms); ln != 1 {
return nil, errors.Errorf("unexpected stack item count (%s): %d", c.listMethod, 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.listMethod)
}
res := &ListValues{
cidList: make([][]byte, 0, len(prms)),
}
for i := range prms {
cid, err := client.BytesFromStackParameter(prms[i])
if err != nil {
return nil, errors.Wrapf(err, "could not get byte array from stack item (%s)", c.listMethod)
}
res.cidList = append(res.cidList, cid)
}
return res, nil
}

View file

@ -0,0 +1,44 @@
package container
import (
"github.com/pkg/errors"
)
// PutArgs groups the arguments
// of put container invocation call.
type PutArgs struct {
ownerID []byte // container owner identifier
cnr []byte // container in a binary format
sig []byte // binary container signature
}
// SetOwnerID sets the container owner identifier
// in a binary format.
func (p *PutArgs) SetOwnerID(v []byte) {
p.ownerID = v
}
// SetContainer sets the container structure
// in a binary format.
func (p *PutArgs) SetContainer(v []byte) {
p.cnr = v
}
// SetSignature sets the container structure
// owner's signature.
func (p *PutArgs) SetSignature(v []byte) {
p.sig = v
}
// Put invokes the call of put container method
// of NeoFS Container contract.
func (c *Client) Put(args PutArgs) error {
return errors.Wrapf(c.client.Invoke(
c.putMethod,
args.ownerID,
args.cnr,
args.sig,
), "could not invoke method (%s)", c.putMethod)
}

View file

@ -0,0 +1,148 @@
package wrapper
import (
"github.com/nspcc-dev/neofs-api-go/refs"
"github.com/nspcc-dev/neofs-node/pkg/core/container"
"github.com/nspcc-dev/neofs-node/pkg/core/container/storage"
contract "github.com/nspcc-dev/neofs-node/pkg/morph/client/container"
"github.com/pkg/errors"
)
// OwnerID represents the container owner identifier.
//
// It is a type alias of
// github.com/nspcc-dev/neofs-node/pkg/core/container/storage.OwnerID.
type OwnerID = storage.OwnerID
// Container represents the NeoFS Container structure.
//
// It is a type alias of
// github.com/nspcc-dev/neofs-node/pkg/core/container/storage.Container.
type Container = storage.Container
// Put saves passed container structure in NeoFS system
// through Container contract call.
//
// Returns calculated container identifier and any error
// encountered that caused the saving to interrupt.
func (w *Wrapper) Put(cnr *Container) (*CID, error) {
// calculate container identifier
//
// Note: cid is used as return value only, but the calculation is performed
// primarily in order to catch potential error before contract client call.
cid, err := container.CalculateID(cnr)
if err != nil {
return nil, errors.Wrap(err, "could not calculate container identifier")
}
// marshal the container
cnrBytes, err := cnr.MarshalBinary()
if err != nil {
return nil, errors.Wrap(err, "could not marshal the container")
}
// prepare invocation arguments
args := contract.PutArgs{}
args.SetOwnerID(cnr.OwnerID().Bytes())
args.SetContainer(cnrBytes)
args.SetSignature(nil) // TODO: set signature from request when will appear.
// invoke smart contract call
if err := w.client.Put(args); err != nil {
return nil, errors.Wrap(err, "could not invoke smart contract")
}
return cid, nil
}
// Get reads the container from NeoFS system by identifier
// through Container contract call.
//
// If an empty slice is returned for the requested identifier,
// storage.ErrNotFound error is returned.
func (w *Wrapper) Get(cid CID) (*Container, error) {
// prepare invocation arguments
args := contract.GetArgs{}
args.SetCID(cid.Bytes())
// invoke smart contract call
values, err := w.client.Get(args)
if err != nil {
return nil, errors.Wrap(err, "could not invoke smart contract")
}
cnrBytes := values.Container()
if len(cnrBytes) == 0 {
return nil, storage.ErrNotFound
}
cnr := new(Container)
// unmarshal the container
if err := cnr.UnmarshalBinary(cnrBytes); err != nil {
return nil, errors.Wrap(err, "could not unmarshal container")
}
return cnr, nil
}
// Delete removes the container from NeoFS system
// through Container contract call.
//
// Returns any error encountered that caused
// the removal to interrupt.
func (w *Wrapper) Delete(cid CID) error {
// prepare invocation arguments
args := contract.DeleteArgs{}
args.SetCID(cid.Bytes())
args.SetOwnerID(nil) // TODO: add owner ID when will appear.
args.SetSignature(nil) // TODO: add CID signature when will appear.
// invoke smart contract call
//
// Note: errors.Wrap return nil on nil error arg.
return errors.Wrap(
w.client.Delete(args),
"could not invoke smart contract",
)
}
// List returns a list of container identifiers belonging
// to the specified owner of NeoFS system. The list is composed
// through Container contract call.
//
// Returns the identifiers of all NeoFS containers if pointer
// to owner identifier is nil.
func (w *Wrapper) List(ownerID *OwnerID) ([]CID, error) {
// prepare invocation arguments
args := contract.ListArgs{}
// Note: by default owner identifier slice is nil,
// so client won't attach invocation arguments.
// This behavior matches the nil argument of current method.
// If argument is not nil, we must specify owner identifier.
if ownerID != nil {
args.SetOwnerID(ownerID.Bytes())
}
// invoke smart contract call
values, err := w.client.List(args)
if err != nil {
return nil, errors.Wrap(err, "could not invoke smart contract")
}
binCIDList := values.CIDList()
cidList := make([]CID, 0, len(binCIDList))
// unmarshal all container identifiers
for i := range binCIDList {
cid, err := refs.CIDFromBytes(binCIDList[i])
if err != nil {
return nil, errors.Wrapf(err, "could not decode container ID #%d", i)
}
cidList = append(cidList, cid)
}
return cidList, nil
}

View file

@ -0,0 +1,51 @@
package wrapper
import (
eacl "github.com/nspcc-dev/neofs-api-go/acl/extended"
"github.com/nspcc-dev/neofs-node/pkg/core/container/acl/extended/storage"
contract "github.com/nspcc-dev/neofs-node/pkg/morph/client/container"
"github.com/pkg/errors"
)
// Table represents extended ACL rule table.
//
// It is a type alias of
// github.com/nspcc-dev/neofs-node/pkg/core/container/acl/extended/storage.Table.
type Table = storage.Table
// GetEACL reads the extended ACL table from NeoFS system
// through Container contract call.
func (w *Wrapper) GetEACL(cid CID) (Table, error) {
// prepare invocation arguments
args := contract.EACLArgs{}
args.SetCID(cid.Bytes())
// invoke smart contract call
values, err := w.client.EACL(args)
if err != nil {
return nil, errors.Wrap(err, "could not invoke smart contract")
}
// unmarshal and return eACL table
return eacl.UnmarshalTable(values.EACL())
}
// PutEACL saves the extended ACL table in NeoFS system
// through Container contract call.
//
// Returns any error encountered that caused the saving to interrupt.
func (w *Wrapper) PutEACL(cid CID, table Table, sig []byte) error {
// prepare invocation arguments
args := contract.SetEACLArgs{}
args.SetEACL(eacl.MarshalTable(table))
args.SetCID(cid.Bytes())
args.SetSignature(sig)
// invoke smart contract call
//
// Note: errors.Wrap return nil on nil error arg.
return errors.Wrap(
w.client.SetEACL(args),
"could not invoke smart contract",
)
}

View file

@ -0,0 +1,43 @@
package wrapper
import (
"github.com/nspcc-dev/neofs-node/pkg/core/container/storage"
"github.com/nspcc-dev/neofs-node/pkg/morph/client/container"
)
// Client represents the Container contract client.
//
// It is a type alias of
// github.com/nspcc-dev/neofs-node/pkg/morph/client/container.Client.
type Client = container.Client
// CID represents the container identifier.
//
// CID is a type alias of
// github.com/nspcc-dev/neofs-node/pkg/core/container/storage.CID.
type CID = storage.CID
// Wrapper is a wrapper over container contract
// client which implements container storage and
// eACL storage methods.
//
// 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
}
// New creates, initializes and returns the Wrapper instance.
//
// If Client is nil, container.ErrNilClient is returned.
func New(c *Client) (*Wrapper, error) {
if c == nil {
return nil, container.ErrNilClient
}
return &Wrapper{
client: c,
}, nil
}