2021-03-31 14:02:02 +00:00
|
|
|
package reputation
|
|
|
|
|
|
|
|
import (
|
2021-05-18 08:12:51 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/client"
|
|
|
|
"github.com/TrueCloudLab/frostfs-sdk-go/reputation"
|
2021-03-31 14:02:02 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
|
|
|
)
|
|
|
|
|
2022-01-31 12:19:10 +00:00
|
|
|
type (
|
|
|
|
// GetPrm groups the arguments of "get reputation value" test invocation.
|
|
|
|
GetPrm struct {
|
|
|
|
epoch uint64
|
|
|
|
peerID reputation.PeerID
|
|
|
|
}
|
2021-03-31 14:02:02 +00:00
|
|
|
|
2022-01-31 12:19:10 +00:00
|
|
|
// GetByIDPrm groups the arguments of "get reputation value by
|
|
|
|
// reputation id" test invocation.
|
|
|
|
GetByIDPrm struct {
|
|
|
|
id ID
|
|
|
|
}
|
|
|
|
)
|
2021-03-31 14:02:02 +00:00
|
|
|
|
|
|
|
// SetEpoch sets epoch of expected reputation value.
|
2022-01-31 12:19:10 +00:00
|
|
|
func (g *GetPrm) SetEpoch(v uint64) {
|
2021-03-31 14:02:02 +00:00
|
|
|
g.epoch = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetPeerID sets peer id of expected reputation value.
|
2022-01-31 12:19:10 +00:00
|
|
|
func (g *GetPrm) SetPeerID(v reputation.PeerID) {
|
2021-03-31 14:02:02 +00:00
|
|
|
g.peerID = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetID sets id of expected reputation value in reputation contract.
|
2022-01-31 12:19:10 +00:00
|
|
|
func (g *GetByIDPrm) SetID(v ID) {
|
2021-03-31 14:02:02 +00:00
|
|
|
g.id = v
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get invokes the call of "get reputation value" method of reputation contract.
|
2022-01-31 12:19:10 +00:00
|
|
|
func (c *Client) Get(p GetPrm) ([]reputation.GlobalTrust, error) {
|
2021-11-09 20:52:29 +00:00
|
|
|
invokePrm := client.TestInvokePrm{}
|
2022-01-29 13:06:36 +00:00
|
|
|
invokePrm.SetMethod(getMethod)
|
2022-07-04 13:24:51 +00:00
|
|
|
invokePrm.SetArgs(p.epoch, p.peerID.PublicKey())
|
2021-11-09 20:52:29 +00:00
|
|
|
|
2022-01-31 12:19:10 +00:00
|
|
|
res, err := c.client.TestInvoke(invokePrm)
|
2021-03-31 14:02:02 +00:00
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("could not perform test invocation (%s): %w", getMethod, err)
|
2021-03-31 14:02:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 12:19:10 +00:00
|
|
|
return parseReputations(res, getMethod)
|
2021-03-31 14:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetByID invokes the call of "get reputation value by reputation id" method
|
|
|
|
// of reputation contract.
|
2022-01-31 12:19:10 +00:00
|
|
|
func (c *Client) GetByID(p GetByIDPrm) ([]reputation.GlobalTrust, error) {
|
2021-11-09 20:52:29 +00:00
|
|
|
invokePrm := client.TestInvokePrm{}
|
2022-01-29 13:06:36 +00:00
|
|
|
invokePrm.SetMethod(getByIDMethod)
|
2022-01-31 12:19:10 +00:00
|
|
|
invokePrm.SetArgs([]byte(p.id))
|
2021-11-09 20:52:29 +00:00
|
|
|
|
|
|
|
prms, err := c.client.TestInvoke(invokePrm)
|
2021-03-31 14:02:02 +00:00
|
|
|
if err != nil {
|
2022-01-29 13:06:36 +00:00
|
|
|
return nil, fmt.Errorf("could not perform test invocation (%s): %w", getByIDMethod, err)
|
2021-03-31 14:02:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-29 13:06:36 +00:00
|
|
|
return parseReputations(prms, getByIDMethod)
|
2021-03-31 14:02:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 12:19:10 +00:00
|
|
|
func parseGetResult(rawReputations [][]byte, method string) ([]reputation.GlobalTrust, error) {
|
|
|
|
reputations := make([]reputation.GlobalTrust, 0, len(rawReputations))
|
|
|
|
|
|
|
|
for i := range rawReputations {
|
|
|
|
r := reputation.GlobalTrust{}
|
|
|
|
|
|
|
|
err := r.Unmarshal(rawReputations[i])
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't unmarshal global trust value (%s): %w", method, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
reputations = append(reputations, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
return reputations, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseReputations(items []stackitem.Item, method string) ([]reputation.GlobalTrust, error) {
|
2021-03-31 14:02:02 +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", method, ln)
|
2021-03-31 14:02:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
items, err := client.ArrayFromStackItem(items[0])
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", method, err)
|
2021-03-31 14:02:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 12:19:10 +00:00
|
|
|
res := make([][]byte, 0, len(items))
|
2021-03-31 14:02:02 +00:00
|
|
|
|
|
|
|
for i := range items {
|
|
|
|
rawReputation, err := client.BytesFromStackItem(items[i])
|
|
|
|
if err != nil {
|
2021-05-18 08:12:51 +00:00
|
|
|
return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", method, err)
|
2021-03-31 14:02:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 12:19:10 +00:00
|
|
|
res = append(res, rawReputation)
|
2021-03-31 14:02:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-31 12:19:10 +00:00
|
|
|
return parseGetResult(res, method)
|
2021-03-31 14:02:02 +00:00
|
|
|
}
|