-
Notifications
You must be signed in to change notification settings - Fork 38
/
get.go
100 lines (81 loc) · 2.55 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package reputation
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
)
// GetArgs groups the arguments of "get reputation value" test invocation.
type GetArgs struct {
epoch uint64
peerID []byte // object of reputation evaluation
}
// 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
}
// SetEpoch sets epoch of expected reputation value.
func (g *GetArgs) SetEpoch(v uint64) {
g.epoch = v
}
// SetPeerID sets peer id of expected reputation value.
func (g *GetArgs) SetPeerID(v []byte) {
g.peerID = v
}
// SetID sets id of expected reputation value in reputation contract.
func (g *GetByIDArgs) SetID(v []byte) {
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) {
prms, err := c.client.TestInvoke(
c.getMethod,
int64(args.epoch),
args.peerID,
)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.getMethod, err)
}
return parseReputations(prms, c.getMethod)
}
// GetByID invokes the call of "get reputation value by reputation id" method
// of reputation contract.
func (c *Client) GetByID(args GetByIDArgs) (*GetResult, error) {
prms, err := c.client.TestInvoke(
c.getByIDMethod,
args.id,
)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.getByIDMethod, err)
}
return parseReputations(prms, c.getByIDMethod)
}
func parseReputations(items []stackitem.Item, method string) (*GetResult, error) {
if ln := len(items); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", method, ln)
}
items, err := client.ArrayFromStackItem(items[0])
if err != nil {
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)),
}
for i := range items {
rawReputation, err := client.BytesFromStackItem(items[i])
if err != nil {
return nil, fmt.Errorf("could not get byte array from stack item (%s): %w", method, err)
}
res.reputations = append(res.reputations, rawReputation)
}
return res, nil
}