-
Notifications
You must be signed in to change notification settings - Fork 177
/
epoch.go
297 lines (255 loc) · 8.14 KB
/
epoch.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
package flow
import (
"encoding/json"
"io"
"sort"
"github.com/ethereum/go-ethereum/rlp"
"github.com/vmihailenco/msgpack"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/model/encodable"
)
// EpochPhase represents a phase of the Epoch Preparation Protocol. The phase
// of an epoch is resolved based on a block reference and is fork-dependent.
// An epoch begins in the staking phase, then transitions to the setup phase in
// the block containing the EpochSetup service event, then to the committed
// phase in the block containing the EpochCommit service event.
// |<-- EpochPhaseStaking -->|<-- EpochPhaseSetup -->|<-- EpochPhaseCommitted -->|<-- EpochPhaseStaking -->...
// |<------------------------------- Epoch N ------------------------------------>|<-- Epoch N + 1 --...
type EpochPhase int
const (
EpochPhaseUndefined EpochPhase = iota
EpochPhaseStaking
EpochPhaseSetup
EpochPhaseCommitted
)
func (p EpochPhase) String() string {
return [...]string{
"EpochPhaseUndefined",
"EpochPhaseStaking",
"EpochPhaseSetup",
"EpochPhaseCommitted",
}[p]
}
// EpochSetup is a service event emitted when the network is ready to set up
// for the upcoming epoch. It contains the participants in the epoch, the
// length, the cluster assignment, and the seed for leader selection.
type EpochSetup struct {
Counter uint64 // the number of the epoch
FinalView uint64 // the final view of the epoch
Participants IdentityList // all participants of the epoch
Assignments AssignmentList // cluster assignment for the epoch
RandomSource []byte // source of randomness for epoch-specific setup tasks
}
func (setup *EpochSetup) ServiceEvent() ServiceEvent {
return ServiceEvent{
Type: ServiceEventSetup,
Event: setup,
}
}
// ID returns the hash of the event contents.
func (setup *EpochSetup) ID() Identifier {
return MakeID(setup)
}
// EpochCommit is a service event emitted when epoch setup has been completed.
// When an EpochCommit event is emitted, the network is ready to transition to
// the epoch.
type EpochCommit struct {
Counter uint64 // the number of the epoch
ClusterQCs []*QuorumCertificate // quorum certificates for each cluster
DKGGroupKey crypto.PublicKey // group key from DKG
DKGParticipants map[Identifier]DKGParticipant // public keys for DKG participants
}
func (commit *EpochCommit) ServiceEvent() ServiceEvent {
return ServiceEvent{
Type: ServiceEventCommit,
Event: commit,
}
}
type encodableCommit struct {
Counter uint64
ClusterQCs []*QuorumCertificate
DKGGroupKey encodable.RandomBeaconPubKey
DKGParticipants map[Identifier]DKGParticipant
}
func encodableFromCommit(commit *EpochCommit) encodableCommit {
return encodableCommit{
Counter: commit.Counter,
ClusterQCs: commit.ClusterQCs,
DKGGroupKey: encodable.RandomBeaconPubKey{PublicKey: commit.DKGGroupKey},
DKGParticipants: commit.DKGParticipants,
}
}
func commitFromEncodable(enc encodableCommit) EpochCommit {
return EpochCommit{
Counter: enc.Counter,
ClusterQCs: enc.ClusterQCs,
DKGGroupKey: enc.DKGGroupKey.PublicKey,
DKGParticipants: enc.DKGParticipants,
}
}
func (commit *EpochCommit) MarshalJSON() ([]byte, error) {
return json.Marshal(encodableFromCommit(commit))
}
func (commit *EpochCommit) UnmarshalJSON(b []byte) error {
var enc encodableCommit
err := json.Unmarshal(b, &enc)
if err != nil {
return err
}
*commit = commitFromEncodable(enc)
return nil
}
func (commit *EpochCommit) MarshalMsgpack() ([]byte, error) {
return msgpack.Marshal(encodableFromCommit(commit))
}
func (commit *EpochCommit) UnmarshalMsgpack(b []byte) error {
var enc encodableCommit
err := msgpack.Unmarshal(b, &enc)
if err != nil {
return err
}
*commit = commitFromEncodable(enc)
return nil
}
// EncodeRLP encodes the commit as RLP. The RLP encoding needs to be handled
// differently from JSON/msgpack, because it does not handle custom encoders
// within map types.
func (commit *EpochCommit) EncodeRLP(w io.Writer) error {
rlpEncodable := struct {
Counter uint64
ClusterQCs []*QuorumCertificate
DKGGroupKey []byte
DKGParticipants []struct {
NodeID []byte
Part encodableDKGParticipant
}
}{
Counter: commit.Counter,
ClusterQCs: commit.ClusterQCs,
DKGGroupKey: commit.DKGGroupKey.Encode(),
}
for nodeID, part := range commit.DKGParticipants {
// must copy the node ID, since the loop variable references the same
// backing memory for each iteration
nodeIDRaw := make([]byte, len(nodeID))
copy(nodeIDRaw, nodeID[:])
rlpEncodable.DKGParticipants = append(rlpEncodable.DKGParticipants, struct {
NodeID []byte
Part encodableDKGParticipant
}{
NodeID: nodeIDRaw,
Part: encodableFromDKGParticipant(part),
})
}
// sort to ensure consistent ordering prior to encoding
sort.Slice(rlpEncodable.DKGParticipants, func(i, j int) bool {
return rlpEncodable.DKGParticipants[i].Part.Index < rlpEncodable.DKGParticipants[j].Part.Index
})
return rlp.Encode(w, rlpEncodable)
}
// ID returns the hash of the event contents.
func (commit *EpochCommit) ID() Identifier {
return MakeID(commit)
}
type DKGParticipant struct {
Index uint
KeyShare crypto.PublicKey
}
type encodableDKGParticipant struct {
Index uint
KeyShare encodable.RandomBeaconPubKey
}
func encodableFromDKGParticipant(part DKGParticipant) encodableDKGParticipant {
return encodableDKGParticipant{
Index: part.Index,
KeyShare: encodable.RandomBeaconPubKey{PublicKey: part.KeyShare},
}
}
func dkgParticipantFromEncodable(enc encodableDKGParticipant) DKGParticipant {
return DKGParticipant{
Index: enc.Index,
KeyShare: enc.KeyShare.PublicKey,
}
}
func (part DKGParticipant) MarshalJSON() ([]byte, error) {
enc := encodableFromDKGParticipant(part)
return json.Marshal(enc)
}
func (part *DKGParticipant) UnmarshalJSON(b []byte) error {
var enc encodableDKGParticipant
err := json.Unmarshal(b, &enc)
if err != nil {
return err
}
*part = dkgParticipantFromEncodable(enc)
return nil
}
func (part DKGParticipant) MarshalMsgpack() ([]byte, error) {
return msgpack.Marshal(encodableFromDKGParticipant(part))
}
func (part *DKGParticipant) UnmarshalMsgpack(b []byte) error {
var enc encodableDKGParticipant
err := msgpack.Unmarshal(b, &enc)
if err != nil {
return err
}
*part = dkgParticipantFromEncodable(enc)
return nil
}
func (part DKGParticipant) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, encodableFromDKGParticipant(part))
}
// EpochStatus represents the status of the current and next epoch with respect
// to a reference block. Concretely, it contains the IDs for all relevant
// service events emitted as of the reference block. Events not yet emitted are
// represented by ZeroID.
type EpochStatus struct {
CurrentEpoch EventIDs // Epoch Preparation Events for the current Epoch
NextEpoch EventIDs // Epoch Preparation Events for the next Epoch
}
type EventIDs struct {
// SetupID is the ID of the EpochSetup event for the respective Epoch
SetupID Identifier
// CommitID is the ID of the EpochCommit event for the respective Epoch
CommitID Identifier
}
func NewEpochStatus(currentSetup, currentCommit, nextSetup, nextCommit Identifier) *EpochStatus {
return &EpochStatus{
CurrentEpoch: EventIDs{
SetupID: currentSetup,
CommitID: currentCommit,
},
NextEpoch: EventIDs{
SetupID: nextSetup,
CommitID: nextCommit,
},
}
}
// Valid returns true if the status is well-formed.
func (es *EpochStatus) Valid() bool {
if es == nil {
return false
}
// must reference event IDs for current epoch
if es.CurrentEpoch.SetupID == ZeroID || es.CurrentEpoch.CommitID == ZeroID {
return false
}
// must not reference a commit without a setup
if es.NextEpoch.SetupID == ZeroID && es.NextEpoch.CommitID != ZeroID {
return false
}
return true
}
// Phase returns the phase for the CURRENT epoch, given this epoch status.
func (es *EpochStatus) Phase() EpochPhase {
if !es.Valid() {
return EpochPhaseUndefined
}
if es.NextEpoch.SetupID == ZeroID {
return EpochPhaseStaking
}
if es.NextEpoch.CommitID == ZeroID {
return EpochPhaseSetup
}
return EpochPhaseCommitted
}