forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ops.go
408 lines (393 loc) · 9.54 KB
/
ops.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package message
import (
"errors"
"fmt"
"github.com/MetalBlockchain/metalgo/proto/pb/p2p"
"github.com/MetalBlockchain/metalgo/utils/set"
)
// Op is an opcode
type Op byte
// Types of messages that may be sent between nodes
// Note: If you add a new parseable Op below, you must also add it to ops
// (declared below)
const (
// Handshake:
PingOp Op = iota
PongOp
VersionOp
PeerListOp
PeerListAckOp
// State sync:
GetStateSummaryFrontierOp
GetStateSummaryFrontierFailedOp
StateSummaryFrontierOp
GetAcceptedStateSummaryOp
GetAcceptedStateSummaryFailedOp
AcceptedStateSummaryOp
// Bootstrapping:
GetAcceptedFrontierOp
GetAcceptedFrontierFailedOp
AcceptedFrontierOp
GetAcceptedOp
GetAcceptedFailedOp
AcceptedOp
GetAncestorsOp
GetAncestorsFailedOp
AncestorsOp
// Consensus:
GetOp
GetFailedOp
PutOp
PushQueryOp
PullQueryOp
QueryFailedOp
ChitsOp
// Application:
AppRequestOp
AppRequestFailedOp
AppResponseOp
AppGossipOp
// Cross chain:
CrossChainAppRequestOp
CrossChainAppRequestFailedOp
CrossChainAppResponseOp
// Internal:
ConnectedOp
ConnectedSubnetOp
DisconnectedOp
NotifyOp
GossipRequestOp
TimeoutOp
)
var (
HandshakeOps = []Op{
PingOp,
PongOp,
VersionOp,
PeerListOp,
PeerListAckOp,
}
// List of all consensus request message types
ConsensusRequestOps = []Op{
GetStateSummaryFrontierOp,
GetAcceptedStateSummaryOp,
GetAcceptedFrontierOp,
GetAcceptedOp,
GetAncestorsOp,
GetOp,
PushQueryOp,
PullQueryOp,
AppRequestOp,
}
ConsensusResponseOps = []Op{
StateSummaryFrontierOp,
AcceptedStateSummaryOp,
AcceptedFrontierOp,
AcceptedOp,
AncestorsOp,
PutOp,
ChitsOp,
AppResponseOp,
}
// AppGossip is the only message that is sent unrequested without the
// expectation of a response
ConsensusExternalOps = append(
ConsensusRequestOps,
append(
ConsensusResponseOps,
AppGossipOp,
)...,
)
ConsensusInternalOps = []Op{
GetStateSummaryFrontierFailedOp,
GetAcceptedStateSummaryFailedOp,
GetAcceptedFrontierFailedOp,
GetAcceptedFailedOp,
GetAncestorsFailedOp,
GetFailedOp,
QueryFailedOp,
AppRequestFailedOp,
CrossChainAppRequestOp,
CrossChainAppRequestFailedOp,
CrossChainAppResponseOp,
ConnectedOp,
ConnectedSubnetOp,
DisconnectedOp,
NotifyOp,
GossipRequestOp,
TimeoutOp,
}
ConsensusOps = append(ConsensusExternalOps, ConsensusInternalOps...)
ExternalOps = append(ConsensusExternalOps, HandshakeOps...)
SynchronousOps = []Op{
// State sync
GetStateSummaryFrontierOp,
GetStateSummaryFrontierFailedOp,
StateSummaryFrontierOp,
GetAcceptedStateSummaryOp,
GetAcceptedStateSummaryFailedOp,
AcceptedStateSummaryOp,
// Bootstrapping
GetAcceptedFrontierOp,
GetAcceptedFrontierFailedOp,
AcceptedFrontierOp,
GetAcceptedOp,
GetAcceptedFailedOp,
AcceptedOp,
GetAncestorsOp,
GetAncestorsFailedOp,
AncestorsOp,
// Consensus
GetOp,
GetFailedOp,
PutOp,
PushQueryOp,
PullQueryOp,
QueryFailedOp,
ChitsOp,
// Internal
ConnectedOp,
ConnectedSubnetOp,
DisconnectedOp,
}
AsynchronousOps = []Op{
// Application
AppRequestOp,
AppRequestFailedOp,
AppGossipOp,
AppResponseOp,
// Cross chain
CrossChainAppRequestOp,
CrossChainAppRequestFailedOp,
CrossChainAppResponseOp,
}
FailedToResponseOps = map[Op]Op{
GetStateSummaryFrontierFailedOp: StateSummaryFrontierOp,
GetAcceptedStateSummaryFailedOp: AcceptedStateSummaryOp,
GetAcceptedFrontierFailedOp: AcceptedFrontierOp,
GetAcceptedFailedOp: AcceptedOp,
GetAncestorsFailedOp: AncestorsOp,
GetFailedOp: PutOp,
QueryFailedOp: ChitsOp,
AppRequestFailedOp: AppResponseOp,
CrossChainAppRequestFailedOp: CrossChainAppResponseOp,
}
UnrequestedOps = set.Set[Op]{
GetAcceptedFrontierOp: {},
GetAcceptedOp: {},
GetAncestorsOp: {},
GetOp: {},
PushQueryOp: {},
PullQueryOp: {},
AppRequestOp: {},
AppGossipOp: {},
CrossChainAppRequestOp: {},
GetStateSummaryFrontierOp: {},
GetAcceptedStateSummaryOp: {},
}
errUnknownMessageType = errors.New("unknown message type")
)
func (op Op) String() string {
switch op {
// Handshake
case PingOp:
return "ping"
case PongOp:
return "pong"
case VersionOp:
return "version"
case PeerListOp:
return "peerlist"
case PeerListAckOp:
return "peerlist_ack"
// State sync
case GetStateSummaryFrontierOp:
return "get_state_summary_frontier"
case GetStateSummaryFrontierFailedOp:
return "get_state_summary_frontier_failed"
case StateSummaryFrontierOp:
return "state_summary_frontier"
case GetAcceptedStateSummaryOp:
return "get_accepted_state_summary"
case GetAcceptedStateSummaryFailedOp:
return "get_accepted_state_summary_failed"
case AcceptedStateSummaryOp:
return "accepted_state_summary"
// Bootstrapping
case GetAcceptedFrontierOp:
return "get_accepted_frontier"
case GetAcceptedFrontierFailedOp:
return "get_accepted_frontier_failed"
case AcceptedFrontierOp:
return "accepted_frontier"
case GetAcceptedOp:
return "get_accepted"
case GetAcceptedFailedOp:
return "get_accepted_failed"
case AcceptedOp:
return "accepted"
case GetAncestorsOp:
return "get_ancestors"
case GetAncestorsFailedOp:
return "get_ancestors_failed"
case AncestorsOp:
return "ancestors"
// Consensus
case GetOp:
return "get"
case GetFailedOp:
return "get_failed"
case PutOp:
return "put"
case PushQueryOp:
return "push_query"
case PullQueryOp:
return "pull_query"
case QueryFailedOp:
return "query_failed"
case ChitsOp:
return "chits"
// Application
case AppRequestOp:
return "app_request"
case AppRequestFailedOp:
return "app_request_failed"
case AppResponseOp:
return "app_response"
case AppGossipOp:
return "app_gossip"
// Cross chain
case CrossChainAppRequestOp:
return "cross_chain_app_request"
case CrossChainAppRequestFailedOp:
return "cross_chain_app_request_failed"
case CrossChainAppResponseOp:
return "cross_chain_app_response"
// Internal
case ConnectedOp:
return "connected"
case ConnectedSubnetOp:
return "connected_subnet"
case DisconnectedOp:
return "disconnected"
case NotifyOp:
return "notify"
case GossipRequestOp:
return "gossip_request"
case TimeoutOp:
return "timeout"
default:
return "unknown"
}
}
func Unwrap(m *p2p.Message) (fmt.Stringer, error) {
switch msg := m.GetMessage().(type) {
// Handshake:
case *p2p.Message_Ping:
return msg.Ping, nil
case *p2p.Message_Pong:
return msg.Pong, nil
case *p2p.Message_Version:
return msg.Version, nil
case *p2p.Message_PeerList:
return msg.PeerList, nil
case *p2p.Message_PeerListAck:
return msg.PeerListAck, nil
// State sync:
case *p2p.Message_GetStateSummaryFrontier:
return msg.GetStateSummaryFrontier, nil
case *p2p.Message_StateSummaryFrontier_:
return msg.StateSummaryFrontier_, nil
case *p2p.Message_GetAcceptedStateSummary:
return msg.GetAcceptedStateSummary, nil
case *p2p.Message_AcceptedStateSummary_:
return msg.AcceptedStateSummary_, nil
// Bootstrapping:
case *p2p.Message_GetAcceptedFrontier:
return msg.GetAcceptedFrontier, nil
case *p2p.Message_AcceptedFrontier_:
return msg.AcceptedFrontier_, nil
case *p2p.Message_GetAccepted:
return msg.GetAccepted, nil
case *p2p.Message_Accepted_:
return msg.Accepted_, nil
case *p2p.Message_GetAncestors:
return msg.GetAncestors, nil
case *p2p.Message_Ancestors_:
return msg.Ancestors_, nil
// Consensus:
case *p2p.Message_Get:
return msg.Get, nil
case *p2p.Message_Put:
return msg.Put, nil
case *p2p.Message_PushQuery:
return msg.PushQuery, nil
case *p2p.Message_PullQuery:
return msg.PullQuery, nil
case *p2p.Message_Chits:
return msg.Chits, nil
// Application:
case *p2p.Message_AppRequest:
return msg.AppRequest, nil
case *p2p.Message_AppResponse:
return msg.AppResponse, nil
case *p2p.Message_AppGossip:
return msg.AppGossip, nil
default:
return nil, fmt.Errorf("%w: %T", errUnknownMessageType, msg)
}
}
func ToOp(m *p2p.Message) (Op, error) {
switch msg := m.GetMessage().(type) {
case *p2p.Message_Ping:
return PingOp, nil
case *p2p.Message_Pong:
return PongOp, nil
case *p2p.Message_Version:
return VersionOp, nil
case *p2p.Message_PeerList:
return PeerListOp, nil
case *p2p.Message_PeerListAck:
return PeerListAckOp, nil
case *p2p.Message_GetStateSummaryFrontier:
return GetStateSummaryFrontierOp, nil
case *p2p.Message_StateSummaryFrontier_:
return StateSummaryFrontierOp, nil
case *p2p.Message_GetAcceptedStateSummary:
return GetAcceptedStateSummaryOp, nil
case *p2p.Message_AcceptedStateSummary_:
return AcceptedStateSummaryOp, nil
case *p2p.Message_GetAcceptedFrontier:
return GetAcceptedFrontierOp, nil
case *p2p.Message_AcceptedFrontier_:
return AcceptedFrontierOp, nil
case *p2p.Message_GetAccepted:
return GetAcceptedOp, nil
case *p2p.Message_Accepted_:
return AcceptedOp, nil
case *p2p.Message_GetAncestors:
return GetAncestorsOp, nil
case *p2p.Message_Ancestors_:
return AncestorsOp, nil
case *p2p.Message_Get:
return GetOp, nil
case *p2p.Message_Put:
return PutOp, nil
case *p2p.Message_PushQuery:
return PushQueryOp, nil
case *p2p.Message_PullQuery:
return PullQueryOp, nil
case *p2p.Message_Chits:
return ChitsOp, nil
case *p2p.Message_AppRequest:
return AppRequestOp, nil
case *p2p.Message_AppResponse:
return AppResponseOp, nil
case *p2p.Message_AppGossip:
return AppGossipOp, nil
default:
return 0, fmt.Errorf("%w: %T", errUnknownMessageType, msg)
}
}