forked from TrueCloudLab/frostfs-node
[#625] client/reputation: remove intermediate wrapper
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
97d18bc515
commit
767ee5c0cd
13 changed files with 145 additions and 375 deletions
|
@ -5,68 +5,58 @@ import (
|
|||
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
|
||||
"github.com/nspcc-dev/neofs-sdk-go/reputation"
|
||||
)
|
||||
|
||||
// GetArgs groups the arguments of "get reputation value" test invocation.
|
||||
type GetArgs struct {
|
||||
epoch uint64
|
||||
peerID []byte // object of reputation evaluation
|
||||
}
|
||||
type (
|
||||
// GetPrm groups the arguments of "get reputation value" test invocation.
|
||||
GetPrm struct {
|
||||
epoch uint64
|
||||
peerID reputation.PeerID
|
||||
}
|
||||
|
||||
// GetByIDArgs groups the arguments of "get reputation value by reputation id"
|
||||
// test invocation.
|
||||
type GetByIDArgs struct {
|
||||
id []byte // id of reputation value in reputation contract
|
||||
}
|
||||
|
||||
// GetResult groups the stack parameters returned by
|
||||
// "get" and "get by id" test invocations.
|
||||
type GetResult struct {
|
||||
reputations [][]byte
|
||||
}
|
||||
// GetByIDPrm groups the arguments of "get reputation value by
|
||||
// reputation id" test invocation.
|
||||
GetByIDPrm struct {
|
||||
id ID
|
||||
}
|
||||
)
|
||||
|
||||
// SetEpoch sets epoch of expected reputation value.
|
||||
func (g *GetArgs) SetEpoch(v uint64) {
|
||||
func (g *GetPrm) SetEpoch(v uint64) {
|
||||
g.epoch = v
|
||||
}
|
||||
|
||||
// SetPeerID sets peer id of expected reputation value.
|
||||
func (g *GetArgs) SetPeerID(v []byte) {
|
||||
func (g *GetPrm) SetPeerID(v reputation.PeerID) {
|
||||
g.peerID = v
|
||||
}
|
||||
|
||||
// SetID sets id of expected reputation value in reputation contract.
|
||||
func (g *GetByIDArgs) SetID(v []byte) {
|
||||
func (g *GetByIDPrm) SetID(v ID) {
|
||||
g.id = v
|
||||
}
|
||||
|
||||
// Reputations returns slice of marshalled reputation values.
|
||||
func (g GetResult) Reputations() [][]byte {
|
||||
return g.reputations
|
||||
}
|
||||
|
||||
// Get invokes the call of "get reputation value" method of reputation contract.
|
||||
func (c *Client) Get(args GetArgs) (*GetResult, error) {
|
||||
func (c *Client) Get(p GetPrm) ([]reputation.GlobalTrust, error) {
|
||||
invokePrm := client.TestInvokePrm{}
|
||||
|
||||
invokePrm.SetMethod(getMethod)
|
||||
invokePrm.SetArgs(int64(args.epoch), args.peerID)
|
||||
invokePrm.SetArgs(int64(p.epoch), p.peerID.ToV2().GetPublicKey())
|
||||
|
||||
prms, err := c.client.TestInvoke(invokePrm)
|
||||
res, err := c.client.TestInvoke(invokePrm)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("could not perform test invocation (%s): %w", getMethod, err)
|
||||
}
|
||||
|
||||
return parseReputations(prms, getMethod)
|
||||
return parseReputations(res, getMethod)
|
||||
}
|
||||
|
||||
// GetByID invokes the call of "get reputation value by reputation id" method
|
||||
// of reputation contract.
|
||||
func (c *Client) GetByID(args GetByIDArgs) (*GetResult, error) {
|
||||
func (c *Client) GetByID(p GetByIDPrm) ([]reputation.GlobalTrust, error) {
|
||||
invokePrm := client.TestInvokePrm{}
|
||||
|
||||
invokePrm.SetMethod(getByIDMethod)
|
||||
invokePrm.SetArgs(args.id)
|
||||
invokePrm.SetArgs([]byte(p.id))
|
||||
|
||||
prms, err := c.client.TestInvoke(invokePrm)
|
||||
if err != nil {
|
||||
|
@ -76,7 +66,24 @@ func (c *Client) GetByID(args GetByIDArgs) (*GetResult, error) {
|
|||
return parseReputations(prms, getByIDMethod)
|
||||
}
|
||||
|
||||
func parseReputations(items []stackitem.Item, method string) (*GetResult, error) {
|
||||
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) {
|
||||
if ln := len(items); ln != 1 {
|
||||
return nil, fmt.Errorf("unexpected stack item count (%s): %d", method, ln)
|
||||
}
|
||||
|
@ -86,9 +93,7 @@ func parseReputations(items []stackitem.Item, method string) (*GetResult, error)
|
|||
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", method, err)
|
||||
}
|
||||
|
||||
res := &GetResult{
|
||||
reputations: make([][]byte, 0, len(items)),
|
||||
}
|
||||
res := make([][]byte, 0, len(items))
|
||||
|
||||
for i := range items {
|
||||
rawReputation, err := client.BytesFromStackItem(items[i])
|
||||
|
@ -96,8 +101,8 @@ func parseReputations(items []stackitem.Item, method string) (*GetResult, error)
|
|||
return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", method, err)
|
||||
}
|
||||
|
||||
res.reputations = append(res.reputations, rawReputation)
|
||||
res = append(res, rawReputation)
|
||||
}
|
||||
|
||||
return res, nil
|
||||
return parseGetResult(res, method)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue