-
Notifications
You must be signed in to change notification settings - Fork 38
/
netmap.go
278 lines (226 loc) · 6.79 KB
/
netmap.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package netmap
import (
"fmt"
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
"github.com/nspcc-dev/neofs-node/pkg/morph/client"
)
// GetNetMapArgs groups the arguments
// of get network map test invoke call.
type GetNetMapArgs struct {
}
// GetSnapshotArgs groups the arguments
// of get netmap snapshot test invoke call.
type GetSnapshotArgs struct {
diff uint64
}
// GetNetMapValues groups the stack parameters
// returned by get network map test invoke.
type GetNetMapValues struct {
peers [][]byte
}
// EpochSnapshotArgs groups the arguments
// of snapshot by epoch test invoke call.
type EpochSnapshotArgs struct {
epoch uint64
}
// EpochSnapshotValues groups the stack parameters
// returned by snapshot by epoch test invoke.
type EpochSnapshotValues struct {
*GetNetMapValues
}
// GetNetMapCandidatesArgs groups the arguments
// of get network map candidates test invoke call.
type GetNetMapCandidatesArgs struct {
}
// GetNetMapCandidatesValues groups the stack parameters
// returned by get network map candidates test invoke.
type GetNetMapCandidatesValues struct {
netmapNodes []*PeerWithState
}
func (g GetNetMapCandidatesValues) NetmapNodes() []*PeerWithState {
return g.netmapNodes
}
// State is an enumeration of various states of the NeoFS node.
type State int64
const (
// Undefined is unknown state.
Undefined State = iota
// Online is network unavailable state.
Online
// Offline is an active state in the network.
Offline
)
// PeerWithState groups information about peer
// and its state in network map.
type PeerWithState struct {
peer []byte
state State
}
func (ps PeerWithState) State() State {
return ps.state
}
func (ps PeerWithState) Peer() []byte {
return ps.peer
}
const (
nodeInfoFixedPrmNumber = 1
peerWithStateFixedPrmNumber = 2
)
// SetDiff sets argument for snapshot method of
// netmap contract.
func (g *GetSnapshotArgs) SetDiff(d uint64) {
g.diff = d
}
// SetEpoch sets epoch number to get snapshot.
func (a *EpochSnapshotArgs) SetEpoch(d uint64) {
a.epoch = d
}
// Peers return the list of peers from
// network map in a binary format.
func (g GetNetMapValues) Peers() [][]byte {
return g.peers
}
// NetMap performs the test invoke of get network map
// method of NeoFS Netmap contract.
func (c *Client) NetMap(_ GetNetMapArgs) (*GetNetMapValues, error) {
prms, err := c.client.TestInvoke(
c.netMapMethod,
)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.netMapMethod, err)
}
return peersFromStackItems(prms, c.netMapMethod)
}
// Snapshot performs the test invoke of get snapshot of network map
// from NeoFS Netmap contract. Contract saves only one previous epoch,
// so all invokes with diff > 1 return error.
func (c *Client) Snapshot(a GetSnapshotArgs) (*GetNetMapValues, error) {
prms, err := c.client.TestInvoke(
c.snapshotMethod,
int64(a.diff),
)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.netMapMethod, err)
}
return peersFromStackItems(prms, c.snapshotMethod)
}
// EpochSnapshot performs the test invoke of get snapshot of network map by epoch
// from NeoFS Netmap contract.
func (c *Client) EpochSnapshot(args EpochSnapshotArgs) (*EpochSnapshotValues, error) {
prms, err := c.client.TestInvoke(
c.epochSnapshotMethod,
int64(args.epoch),
)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w",
c.epochSnapshotMethod, err)
}
nmVals, err := peersFromStackItems(prms, c.epochSnapshotMethod)
if err != nil {
return nil, err
}
return &EpochSnapshotValues{
GetNetMapValues: nmVals,
}, nil
}
func (c *Client) Candidates(_ GetNetMapCandidatesArgs) (*GetNetMapCandidatesValues, error) {
prms, err := c.client.TestInvoke(
c.netMapCandidatesMethod,
)
if err != nil {
return nil, fmt.Errorf("could not perform test invocation (%s): %w", c.netMapCandidatesMethod, err)
}
candVals, err := peersWithStateFromStackItems(prms, c.netMapCandidatesMethod)
if err != nil {
return nil, fmt.Errorf("could not parse contract response: %w", err)
}
return candVals, nil
}
func peersWithStateFromStackItems(stack []stackitem.Item, method string) (*GetNetMapCandidatesValues, error) {
if ln := len(stack); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d", method, ln)
}
netmapNodes, err := client.ArrayFromStackItem(stack[0])
if err != nil {
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w", method, err)
}
res := &GetNetMapCandidatesValues{
netmapNodes: make([]*PeerWithState, 0, len(netmapNodes)),
}
for i := range netmapNodes {
node, err := peerWithStateFromStackItem(netmapNodes[i])
if err != nil {
return nil, fmt.Errorf("could not parse stack item (Peer #%d): %w", i, err)
}
res.netmapNodes = append(res.netmapNodes, node)
}
return res, nil
}
func peerWithStateFromStackItem(prm stackitem.Item) (*PeerWithState, error) {
prms, err := client.ArrayFromStackItem(prm)
if err != nil {
return nil, fmt.Errorf("could not get stack item array (PeerWithState): %w", err)
} else if ln := len(prms); ln != peerWithStateFixedPrmNumber {
return nil, fmt.Errorf(
"unexpected stack item count (PeerWithState): expected %d, has %d",
peerWithStateFixedPrmNumber,
ln,
)
}
var res PeerWithState
// peer
if res.peer, err = peerInfoFromStackItem(prms[0]); err != nil {
return nil, fmt.Errorf("could not get bytes from 'node' field of PeerWithState: %w", err)
}
// state
state, err := client.IntFromStackItem(prms[1])
if err != nil {
return nil, fmt.Errorf("could not get int from 'state' field of PeerWithState: %w", err)
}
switch state {
case 1:
res.state = Online
case 2:
res.state = Offline
default:
res.state = Undefined
}
return &res, nil
}
func peersFromStackItems(stack []stackitem.Item, method string) (*GetNetMapValues, error) {
if ln := len(stack); ln != 1 {
return nil, fmt.Errorf("unexpected stack item count (%s): %d",
method, ln)
}
peers, err := client.ArrayFromStackItem(stack[0])
if err != nil {
return nil, fmt.Errorf("could not get stack item array from stack item (%s): %w",
method, err)
}
res := &GetNetMapValues{
peers: make([][]byte, 0, len(peers)),
}
for i := range peers {
peer, err := peerInfoFromStackItem(peers[i])
if err != nil {
return nil, fmt.Errorf("could not parse stack item (Peer #%d): %w", i, err)
}
res.peers = append(res.peers, peer)
}
return res, nil
}
func peerInfoFromStackItem(prm stackitem.Item) ([]byte, error) {
prms, err := client.ArrayFromStackItem(prm)
if err != nil {
return nil, fmt.Errorf("could not get stack item array (PeerInfo): %w", err)
} else if ln := len(prms); ln != nodeInfoFixedPrmNumber {
return nil, fmt.Errorf(
"unexpected stack item count (PeerInfo): expected %d, has %d",
nodeInfoFixedPrmNumber,
ln,
)
}
return client.BytesFromStackItem(prms[0])
}