-
Notifications
You must be signed in to change notification settings - Fork 177
/
identity.go
439 lines (392 loc) · 14.2 KB
/
identity.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package flow
import (
"encoding/json"
"fmt"
"io"
"github.com/ethereum/go-ethereum/rlp"
"github.com/fxamacker/cbor/v2"
"github.com/onflow/crypto"
"github.com/vmihailenco/msgpack"
)
// DefaultInitialWeight is the default initial weight for a node identity.
// It is equal to the default initial weight in the FlowIDTableStaking smart contract.
const DefaultInitialWeight = 100
// IdentitySkeleton represents the static part of a network participant's (i.e. node's) public identity.
type IdentitySkeleton struct {
// NodeID uniquely identifies a particular node. A node's ID is fixed for
// the duration of that node's participation in the network.
NodeID Identifier
// Address is the network address where the node can be reached.
Address string
// Role is the node's role in the network and defines its abilities and
// responsibilities.
Role Role
// InitialWeight is a 'trust score' initially assigned by EpochSetup event after
// the staking phase. The initial weights define the supermajority thresholds for
// the cluster and security node consensus throughout the Epoch.
InitialWeight uint64
StakingPubKey crypto.PublicKey
NetworkPubKey crypto.PublicKey
}
// EpochParticipationStatus represents the status of a node's participation. Depending on what
// changes were applied to the protocol state, a node may be in one of four states:
// / - joining - the node is not active in the current epoch and will be active in the next epoch.
// / - active - the node was included in the EpochSetup event for the current epoch and is actively participating in the current epoch.
// / - leaving - the node was active in the previous epoch but is not active in the current epoch.
// / - ejected - the node has been permanently removed from the network.
//
// / EpochSetup
// / ┌────────────⬤ unregistered ◯◄───────────┐
// / ┌─────▼─────┐ ┌───────────┐ ┌─────┴─────┐
// / │ JOINING ├───────►│ ACTIVE ├───────►│ LEAVING │
// / └─────┬─────┘ └─────┬─────┘ └─────┬─────┘
// / │ ┌─────▼─────┐ │
// / └─────────────►│ EJECTED │◄─────────────┘
// / └───────────┘
//
// Only active nodes are allowed to perform certain tasks relative to other nodes.
// Nodes which are registered to join at the next epoch will appear in the
// identity table but aren't considered active until their first
// epoch begins. Likewise, nodes which were registered in the previous epoch
// but have left at the most recent epoch boundary will appear in the identity
// table with leaving participation status.
// A node may be ejected by either:
// - requesting self-ejection to protect its stake in case the node operator suspects
// the node's keys to be compromised
// - committing a serious protocol violation or multiple smaller misdemeanours.
type EpochParticipationStatus int
const (
EpochParticipationStatusJoining EpochParticipationStatus = iota
EpochParticipationStatusActive
EpochParticipationStatusLeaving
EpochParticipationStatusEjected
)
// String returns string representation of enum value.
func (s EpochParticipationStatus) String() string {
return [...]string{
"EpochParticipationStatusJoining",
"EpochParticipationStatusActive",
"EpochParticipationStatusLeaving",
"EpochParticipationStatusEjected",
}[s]
}
// ParseEpochParticipationStatus converts string representation of EpochParticipationStatus into a typed value.
// An exception will be returned if failed to convert.
func ParseEpochParticipationStatus(s string) (EpochParticipationStatus, error) {
switch s {
case EpochParticipationStatusJoining.String():
return EpochParticipationStatusJoining, nil
case EpochParticipationStatusActive.String():
return EpochParticipationStatusActive, nil
case EpochParticipationStatusLeaving.String():
return EpochParticipationStatusLeaving, nil
case EpochParticipationStatusEjected.String():
return EpochParticipationStatusEjected, nil
default:
return 0, fmt.Errorf("invalid epoch participation status")
}
}
// EncodeRLP performs RLP encoding of custom type, it's need to be able to hash structures that include EpochParticipationStatus.
// No errors are expected during normal operations.
func (s EpochParticipationStatus) EncodeRLP(w io.Writer) error {
encodable := s.String()
err := rlp.Encode(w, encodable)
if err != nil {
return fmt.Errorf("could not encode rlp: %w", err)
}
return nil
}
// DynamicIdentity represents the dynamic part of public identity of one network participant (node).
type DynamicIdentity struct {
EpochParticipationStatus
}
// Identity is combined from static and dynamic part and represents the full public identity of one network participant (node).
type Identity struct {
IdentitySkeleton
DynamicIdentity
}
// IsEjected returns true if the node is ejected from the network.
func (iy *DynamicIdentity) IsEjected() bool {
return iy.EpochParticipationStatus == EpochParticipationStatusEjected
}
// String returns a string representation of the identity.
func (iy Identity) String() string {
return fmt.Sprintf("%s-%s@%s=%s", iy.Role, iy.NodeID.String(), iy.Address, iy.EpochParticipationStatus.String())
}
// String returns a string representation of the identity.
func (iy IdentitySkeleton) String() string {
return fmt.Sprintf("%s-%s@%s", iy.Role, iy.NodeID.String(), iy.Address)
}
// ID returns a unique, persistent identifier for the identity.
// CAUTION: the ID may be chosen by a node operator, so long as it is unique.
func (iy Identity) ID() Identifier {
return iy.NodeID
}
// Checksum returns a checksum for the identity including mutable attributes.
func (iy Identity) Checksum() Identifier {
return MakeID(iy)
}
// GetNodeID returns node ID for the identity. It is needed to satisfy GenericIdentity constraint.
func (iy IdentitySkeleton) GetNodeID() Identifier {
return iy.NodeID
}
// GetRole returns a node role for the identity. It is needed to satisfy GenericIdentity constraint.
func (iy IdentitySkeleton) GetRole() Role {
return iy.Role
}
// GetStakingPubKey returns staking public key for the identity. It is needed to satisfy GenericIdentity constraint.
func (iy IdentitySkeleton) GetStakingPubKey() crypto.PublicKey {
return iy.StakingPubKey
}
// GetNetworkPubKey returns network public key for the identity. It is needed to satisfy GenericIdentity constraint.
func (iy IdentitySkeleton) GetNetworkPubKey() crypto.PublicKey {
return iy.NetworkPubKey
}
// GetInitialWeight returns initial weight for the identity. It is needed to satisfy GenericIdentity constraint.
func (iy IdentitySkeleton) GetInitialWeight() uint64 {
return iy.InitialWeight
}
// GetSkeleton returns the skeleton part for the identity. It is needed to satisfy GenericIdentity constraint.
func (iy IdentitySkeleton) GetSkeleton() IdentitySkeleton {
return iy
}
type encodableIdentitySkeleton struct {
NodeID Identifier
Address string `json:",omitempty"`
Role Role
InitialWeight uint64
StakingPubKey []byte
NetworkPubKey []byte
}
type encodableIdentity struct {
encodableIdentitySkeleton
ParticipationStatus string
}
func encodableSkeletonFromIdentity(iy IdentitySkeleton) encodableIdentitySkeleton {
ie := encodableIdentitySkeleton{
NodeID: iy.NodeID,
Address: iy.Address,
Role: iy.Role,
InitialWeight: iy.InitialWeight,
}
if iy.StakingPubKey != nil {
ie.StakingPubKey = iy.StakingPubKey.Encode()
}
if iy.NetworkPubKey != nil {
ie.NetworkPubKey = iy.NetworkPubKey.Encode()
}
return ie
}
func encodableFromIdentity(iy Identity) encodableIdentity {
return encodableIdentity{
encodableIdentitySkeleton: encodableSkeletonFromIdentity(iy.IdentitySkeleton),
ParticipationStatus: iy.EpochParticipationStatus.String(),
}
}
func (iy IdentitySkeleton) MarshalJSON() ([]byte, error) {
encodable := encodableSkeletonFromIdentity(iy)
data, err := json.Marshal(encodable)
if err != nil {
return nil, fmt.Errorf("could not encode json: %w", err)
}
return data, nil
}
func (iy IdentitySkeleton) MarshalCBOR() ([]byte, error) {
encodable := encodableSkeletonFromIdentity(iy)
data, err := cbor.Marshal(encodable)
if err != nil {
return nil, fmt.Errorf("could not encode cbor: %w", err)
}
return data, nil
}
func (iy IdentitySkeleton) MarshalMsgpack() ([]byte, error) {
encodable := encodableSkeletonFromIdentity(iy)
data, err := msgpack.Marshal(encodable)
if err != nil {
return nil, fmt.Errorf("could not encode msgpack: %w", err)
}
return data, nil
}
func (iy IdentitySkeleton) EncodeRLP(w io.Writer) error {
encodable := encodableSkeletonFromIdentity(iy)
err := rlp.Encode(w, encodable)
if err != nil {
return fmt.Errorf("could not encode rlp: %w", err)
}
return nil
}
func (iy Identity) MarshalJSON() ([]byte, error) {
encodable := encodableFromIdentity(iy)
data, err := json.Marshal(encodable)
if err != nil {
return nil, fmt.Errorf("could not encode json: %w", err)
}
return data, nil
}
func (iy Identity) MarshalCBOR() ([]byte, error) {
encodable := encodableFromIdentity(iy)
data, err := cbor.Marshal(encodable)
if err != nil {
return nil, fmt.Errorf("could not encode cbor: %w", err)
}
return data, nil
}
func (iy Identity) MarshalMsgpack() ([]byte, error) {
encodable := encodableFromIdentity(iy)
data, err := msgpack.Marshal(encodable)
if err != nil {
return nil, fmt.Errorf("could not encode msgpack: %w", err)
}
return data, nil
}
func (iy Identity) EncodeRLP(w io.Writer) error {
encodable := encodableFromIdentity(iy)
err := rlp.Encode(w, encodable)
if err != nil {
return fmt.Errorf("could not encode rlp: %w", err)
}
return nil
}
func identitySkeletonFromEncodable(ie encodableIdentitySkeleton, identity *IdentitySkeleton) error {
identity.NodeID = ie.NodeID
identity.Address = ie.Address
identity.Role = ie.Role
identity.InitialWeight = ie.InitialWeight
var err error
if ie.StakingPubKey != nil {
if identity.StakingPubKey, err = crypto.DecodePublicKey(crypto.BLSBLS12381, ie.StakingPubKey); err != nil {
return fmt.Errorf("could not decode staking key: %w", err)
}
}
if ie.NetworkPubKey != nil {
if identity.NetworkPubKey, err = crypto.DecodePublicKey(crypto.ECDSAP256, ie.NetworkPubKey); err != nil {
return fmt.Errorf("could not decode network key: %w", err)
}
}
return nil
}
func identityFromEncodable(ie encodableIdentity, identity *Identity) error {
err := identitySkeletonFromEncodable(ie.encodableIdentitySkeleton, &identity.IdentitySkeleton)
if err != nil {
return fmt.Errorf("could not decode identity skeleton: %w", err)
}
participationStatus, err := ParseEpochParticipationStatus(ie.ParticipationStatus)
if err != nil {
return fmt.Errorf("could not decode epoch participation status: %w", err)
}
identity.EpochParticipationStatus = participationStatus
return nil
}
func (iy *IdentitySkeleton) UnmarshalJSON(b []byte) error {
var decodable encodableIdentitySkeleton
err := json.Unmarshal(b, &decodable)
if err != nil {
return fmt.Errorf("could not decode json: %w", err)
}
err = identitySkeletonFromEncodable(decodable, iy)
if err != nil {
return fmt.Errorf("could not convert from encodable json: %w", err)
}
return nil
}
func (iy *IdentitySkeleton) UnmarshalCBOR(b []byte) error {
var encodable encodableIdentitySkeleton
err := cbor.Unmarshal(b, &encodable)
if err != nil {
return fmt.Errorf("could not decode json: %w", err)
}
err = identitySkeletonFromEncodable(encodable, iy)
if err != nil {
return fmt.Errorf("could not convert from encodable cbor: %w", err)
}
return nil
}
func (iy *IdentitySkeleton) UnmarshalMsgpack(b []byte) error {
var encodable encodableIdentitySkeleton
err := msgpack.Unmarshal(b, &encodable)
if err != nil {
return fmt.Errorf("could not decode json: %w", err)
}
err = identitySkeletonFromEncodable(encodable, iy)
if err != nil {
return fmt.Errorf("could not convert from encodable msgpack: %w", err)
}
return nil
}
func (iy *Identity) UnmarshalJSON(b []byte) error {
var decodable encodableIdentity
err := json.Unmarshal(b, &decodable)
if err != nil {
return fmt.Errorf("could not decode json: %w", err)
}
err = identityFromEncodable(decodable, iy)
if err != nil {
return fmt.Errorf("could not convert from encodable json: %w", err)
}
return nil
}
func (iy *Identity) UnmarshalCBOR(b []byte) error {
var encodable encodableIdentity
err := cbor.Unmarshal(b, &encodable)
if err != nil {
return fmt.Errorf("could not decode json: %w", err)
}
err = identityFromEncodable(encodable, iy)
if err != nil {
return fmt.Errorf("could not convert from encodable cbor: %w", err)
}
return nil
}
func (iy *Identity) UnmarshalMsgpack(b []byte) error {
var encodable encodableIdentity
err := msgpack.Unmarshal(b, &encodable)
if err != nil {
return fmt.Errorf("could not decode json: %w", err)
}
err = identityFromEncodable(encodable, iy)
if err != nil {
return fmt.Errorf("could not convert from encodable msgpack: %w", err)
}
return nil
}
func (iy *IdentitySkeleton) EqualTo(other *IdentitySkeleton) bool {
if iy.NodeID != other.NodeID {
return false
}
if iy.Address != other.Address {
return false
}
if iy.Role != other.Role {
return false
}
if iy.InitialWeight != other.InitialWeight {
return false
}
if (iy.StakingPubKey != nil && other.StakingPubKey == nil) ||
(iy.StakingPubKey == nil && other.StakingPubKey != nil) {
return false
}
if iy.StakingPubKey != nil && !iy.StakingPubKey.Equals(other.StakingPubKey) {
return false
}
if (iy.NetworkPubKey != nil && other.NetworkPubKey == nil) ||
(iy.NetworkPubKey == nil && other.NetworkPubKey != nil) {
return false
}
if iy.NetworkPubKey != nil && !iy.NetworkPubKey.Equals(other.NetworkPubKey) {
return false
}
return true
}
func (iy *DynamicIdentity) EqualTo(other *DynamicIdentity) bool {
return iy.EpochParticipationStatus == other.EpochParticipationStatus
}
func (iy *Identity) EqualTo(other *Identity) bool {
if !iy.IdentitySkeleton.EqualTo(&other.IdentitySkeleton) {
return false
}
if !iy.DynamicIdentity.EqualTo(&other.DynamicIdentity) {
return false
}
return true
}