2021-01-28 15:58:57 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-01-28 15:58:57 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PutSizeArgs groups the arguments
|
|
|
|
// of "put container size" invocation call.
|
|
|
|
type PutSizeArgs struct {
|
|
|
|
epoch int64
|
|
|
|
|
|
|
|
size int64
|
|
|
|
|
|
|
|
cid []byte
|
|
|
|
|
|
|
|
reporterKey []byte
|
2021-11-12 15:14:55 +00:00
|
|
|
|
|
|
|
client.InvokePrmOptional
|
2021-01-28 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetEpoch sets the number of the epoch when
|
|
|
|
// size was estimated.
|
|
|
|
func (p *PutSizeArgs) SetEpoch(v uint64) {
|
|
|
|
p.epoch = int64(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSize sets estimation of the container size.
|
|
|
|
func (p *PutSizeArgs) SetSize(v uint64) {
|
|
|
|
p.size = int64(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetContainerID sets identifier of the container
|
|
|
|
// being evaluated.
|
|
|
|
func (p *PutSizeArgs) SetContainerID(v []byte) {
|
|
|
|
p.cid = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetReporterKey ыуеы public key of the storage node
|
|
|
|
// that collected size estimation.
|
|
|
|
func (p *PutSizeArgs) SetReporterKey(v []byte) {
|
|
|
|
p.reporterKey = v
|
|
|
|
}
|
|
|
|
|
2021-06-23 13:29:46 +00:00
|
|
|
// PutSize invokes the call of put container size method
|
2021-01-28 15:58:57 +00:00
|
|
|
// of NeoFS Container contract.
|
|
|
|
func (c *Client) PutSize(args PutSizeArgs) error {
|
2021-11-09 20:52:29 +00:00
|
|
|
prm := client.InvokePrm{}
|
|
|
|
|
2022-01-29 13:06:36 +00:00
|
|
|
prm.SetMethod(putSizeMethod)
|
2021-11-09 20:52:29 +00:00
|
|
|
prm.SetArgs(args.epoch, args.cid, args.size, args.reporterKey)
|
2021-11-12 15:14:55 +00:00
|
|
|
prm.InvokePrmOptional = args.InvokePrmOptional
|
2021-11-09 20:52:29 +00:00
|
|
|
|
|
|
|
err := c.client.Invoke(prm)
|
2021-05-18 08:12:51 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return fmt.Errorf("could not invoke method (%s): %w", putSizeMethod, err)
|
2021-05-18 08:12:51 +00:00
|
|
|
}
|
|
|
|
return nil
|
2021-01-28 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListSizesArgs groups the arguments
|
|
|
|
// of "list container sizes" test invoke call..
|
|
|
|
type ListSizesArgs struct {
|
|
|
|
epoch int64
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetEpoch sets the number of the epoch to select
|
|
|
|
// the estimations.
|
|
|
|
func (p *ListSizesArgs) SetEpoch(v uint64) {
|
|
|
|
p.epoch = int64(v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListSizesValues groups the stack items
|
|
|
|
// returned by "list container sizes" test invoke.
|
|
|
|
type ListSizesValues struct {
|
|
|
|
ids [][]byte
|
|
|
|
}
|
|
|
|
|
2021-11-09 20:52:29 +00:00
|
|
|
// IDList returns list of identifiers of the
|
2021-01-28 15:58:57 +00:00
|
|
|
// container load estimations.
|
|
|
|
func (v *ListSizesValues) IDList() [][]byte {
|
|
|
|
return v.ids
|
|
|
|
}
|
|
|
|
|
2021-11-09 20:52:29 +00:00
|
|
|
// ListSizes performs the test invoke of "list container sizes"
|
2021-01-28 15:58:57 +00:00
|
|
|
// method of NeoFS Container contract.
|
|
|
|
func (c *Client) ListSizes(args ListSizesArgs) (*ListSizesValues, error) {
|
2021-11-09 20:52:29 +00:00
|
|
|
invokePrm := client.TestInvokePrm{}
|
|
|
|
|
2022-01-29 13:06:36 +00:00
|
|
|
invokePrm.SetMethod(listSizesMethod)
|
2021-11-09 20:52:29 +00:00
|
|
|
invokePrm.SetArgs(args.epoch)
|
|
|
|
|
|
|
|
prms, err := c.client.TestInvoke(invokePrm)
|
2021-01-28 15:58:57 +00:00
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("could not perform test invocation (%s): %w", listSizesMethod, err)
|
2021-01-28 15:58:57 +00:00
|
|
|
} else if ln := len(prms); ln != 1 {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("unexpected stack item count (%s): %d", listSizesMethod, ln)
|
2021-01-28 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prms, err = client.ArrayFromStackItem(prms[0])
|
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", listSizesMethod, err)
|
2021-01-28 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res := &ListSizesValues{
|
|
|
|
ids: make([][]byte, 0, len(prms)),
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range prms {
|
|
|
|
id, err := client.BytesFromStackItem(prms[i])
|
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("could not get ID byte array from stack item (%s): %w", listSizesMethod, err)
|
2021-01-28 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res.ids = append(res.ids, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSizeArgs groups the arguments
|
|
|
|
// of "get container size" test invoke call..
|
|
|
|
type GetSizeArgs struct {
|
|
|
|
id []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetID sets identifier of the container estimation.
|
|
|
|
func (p *GetSizeArgs) SetID(v []byte) {
|
|
|
|
p.id = v
|
|
|
|
}
|
|
|
|
|
|
|
|
type Estimation struct {
|
|
|
|
Size int64
|
|
|
|
|
|
|
|
Reporter []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type Estimations struct {
|
|
|
|
ContainerID []byte
|
|
|
|
|
|
|
|
Estimations []Estimation
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSizeValues groups the stack items
|
|
|
|
// returned by "get container size" test invoke.
|
|
|
|
type GetSizeValues struct {
|
|
|
|
est Estimations
|
|
|
|
}
|
|
|
|
|
|
|
|
// Estimations returns list of the container load estimations.
|
|
|
|
func (v *GetSizeValues) Estimations() Estimations {
|
|
|
|
return v.est
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetContainerSize performs the test invoke of "get container size"
|
|
|
|
// method of NeoFS Container contract.
|
|
|
|
func (c *Client) GetContainerSize(args GetSizeArgs) (*GetSizeValues, error) {
|
2021-11-09 20:52:29 +00:00
|
|
|
invokePrm := client.TestInvokePrm{}
|
|
|
|
|
2022-01-29 13:06:36 +00:00
|
|
|
invokePrm.SetMethod(getSizeMethod)
|
2021-11-09 20:52:29 +00:00
|
|
|
invokePrm.SetArgs(args.id)
|
|
|
|
|
|
|
|
prms, err := c.client.TestInvoke(invokePrm)
|
2021-01-28 15:58:57 +00:00
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("could not perform test invocation (%s): %w", getSizeMethod, err)
|
2021-01-28 15:58:57 +00:00
|
|
|
} else if ln := len(prms); ln != 1 {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("unexpected stack item count (%s): %d", getSizeMethod, ln)
|
2021-01-28 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prms, err = client.ArrayFromStackItem(prms[0])
|
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("could not get stack items of estimation fields from stack item (%s): %w", getSizeMethod, err)
|
2021-01-28 15:58:57 +00:00
|
|
|
} else if ln := len(prms); ln != 2 {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("unexpected stack item count of estimations fields (%s)", getSizeMethod)
|
2021-01-28 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
es := Estimations{}
|
|
|
|
|
|
|
|
es.ContainerID, err = client.BytesFromStackItem(prms[0])
|
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("could not get container ID byte array from stack item (%s): %w", getSizeMethod, err)
|
2021-01-28 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prms, err = client.ArrayFromStackItem(prms[1])
|
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("could not get estimation list array from stack item (%s): %w", getSizeMethod, err)
|
2021-01-28 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
es.Estimations = make([]Estimation, 0, len(prms))
|
|
|
|
|
|
|
|
for i := range prms {
|
|
|
|
arr, err := client.ArrayFromStackItem(prms[i])
|
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("could not get estimation struct from stack item (%s): %w", getSizeMethod, err)
|
2021-01-28 15:58:57 +00:00
|
|
|
} else if ln := len(arr); ln != 2 {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("unexpected stack item count of estimation fields (%s)", getSizeMethod)
|
2021-01-28 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
e := Estimation{}
|
|
|
|
|
|
|
|
e.Reporter, err = client.BytesFromStackItem(arr[0])
|
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("could not get reporter byte array from stack item (%s): %w", getSizeMethod, err)
|
2021-01-28 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
e.Size, err = client.IntFromStackItem(arr[1])
|
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("could not get estimation size from stack item (%s): %w", getSizeMethod, err)
|
2021-01-28 15:58:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
es.Estimations = append(es.Estimations, e)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &GetSizeValues{
|
|
|
|
est: es,
|
|
|
|
}, nil
|
|
|
|
}
|