-
Notifications
You must be signed in to change notification settings - Fork 178
/
epoch.go
251 lines (211 loc) · 7.44 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
package inmem
import (
"fmt"
"github.com/onflow/flow-go/model/encodable"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/flow/factory"
"github.com/onflow/flow-go/model/flow/filter"
"github.com/onflow/flow-go/module/signature"
"github.com/onflow/flow-go/state/cluster"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/state/protocol/invalid"
)
// Epoch is a memory-backed implementation of protocol.Epoch.
type Epoch struct {
enc EncodableEpoch
}
func (e Epoch) Encodable() EncodableEpoch {
return e.enc
}
func (e Epoch) Counter() (uint64, error) { return e.enc.Counter, nil }
func (e Epoch) FirstView() (uint64, error) { return e.enc.FirstView, nil }
func (e Epoch) DKGPhase1FinalView() (uint64, error) { return e.enc.DKGPhase1FinalView, nil }
func (e Epoch) DKGPhase2FinalView() (uint64, error) { return e.enc.DKGPhase2FinalView, nil }
func (e Epoch) DKGPhase3FinalView() (uint64, error) { return e.enc.DKGPhase3FinalView, nil }
func (e Epoch) FinalView() (uint64, error) { return e.enc.FinalView, nil }
func (e Epoch) InitialIdentities() (flow.IdentityList, error) {
return e.enc.InitialIdentities, nil
}
func (e Epoch) RandomSource() ([]byte, error) {
return e.enc.RandomSource, nil
}
func (e Epoch) Clustering() (flow.ClusterList, error) {
return e.enc.Clustering, nil
}
func (e Epoch) DKG() (protocol.DKG, error) {
if e.enc.DKG != nil {
return DKG{*e.enc.DKG}, nil
}
return nil, protocol.ErrEpochNotCommitted
}
func (e Epoch) Cluster(i uint) (protocol.Cluster, error) {
if e.enc.Clusters == nil {
return nil, protocol.ErrEpochNotCommitted
}
if i >= uint(len(e.enc.Clusters)) {
return nil, fmt.Errorf("no cluster with index %d: %w", i, protocol.ErrClusterNotFound)
}
return Cluster{e.enc.Clusters[i]}, nil
}
func (e Epoch) ClusterByChainID(chainID flow.ChainID) (protocol.Cluster, error) {
if e.enc.Clusters == nil {
return nil, protocol.ErrEpochNotCommitted
}
for _, cluster := range e.enc.Clusters {
if cluster.RootBlock.Header.ChainID == chainID {
return Cluster{cluster}, nil
}
}
chainIDs := make([]string, 0, len(e.enc.Clusters))
for _, cluster := range e.enc.Clusters {
chainIDs = append(chainIDs, string(cluster.RootBlock.Header.ChainID))
}
return nil, fmt.Errorf("no cluster with the given chain ID %v, available chainIDs %v: %w", chainID, chainIDs, protocol.ErrClusterNotFound)
}
type Epochs struct {
enc EncodableEpochs
}
func (eq Epochs) Previous() protocol.Epoch {
if eq.enc.Previous != nil {
return Epoch{*eq.enc.Previous}
}
return invalid.NewEpoch(protocol.ErrNoPreviousEpoch)
}
func (eq Epochs) Current() protocol.Epoch {
return Epoch{eq.enc.Current}
}
func (eq Epochs) Next() protocol.Epoch {
if eq.enc.Next != nil {
return Epoch{*eq.enc.Next}
}
return invalid.NewEpoch(protocol.ErrNextEpochNotSetup)
}
// setupEpoch is an implementation of protocol.Epoch backed by an EpochSetup
// service event. This is used for converting service events to inmem.Epoch.
type setupEpoch struct {
// EpochSetup service event
setupEvent *flow.EpochSetup
}
func (es *setupEpoch) Counter() (uint64, error) {
return es.setupEvent.Counter, nil
}
func (es *setupEpoch) FirstView() (uint64, error) {
return es.setupEvent.FirstView, nil
}
func (es *setupEpoch) DKGPhase1FinalView() (uint64, error) {
return es.setupEvent.DKGPhase1FinalView, nil
}
func (es *setupEpoch) DKGPhase2FinalView() (uint64, error) {
return es.setupEvent.DKGPhase2FinalView, nil
}
func (es *setupEpoch) DKGPhase3FinalView() (uint64, error) {
return es.setupEvent.DKGPhase3FinalView, nil
}
func (es *setupEpoch) FinalView() (uint64, error) {
return es.setupEvent.FinalView, nil
}
func (es *setupEpoch) InitialIdentities() (flow.IdentityList, error) {
identities := es.setupEvent.Participants.Filter(filter.Any)
return identities, nil
}
func (es *setupEpoch) Clustering() (flow.ClusterList, error) {
return ClusteringFromSetupEvent(es.setupEvent)
}
func ClusteringFromSetupEvent(setupEvent *flow.EpochSetup) (flow.ClusterList, error) {
collectorFilter := filter.HasRole(flow.RoleCollection)
clustering, err := factory.NewClusterList(setupEvent.Assignments, setupEvent.Participants.Filter(collectorFilter))
if err != nil {
return nil, fmt.Errorf("failed to generate ClusterList from collector identities: %w", err)
}
return clustering, nil
}
func (es *setupEpoch) Cluster(_ uint) (protocol.Cluster, error) {
return nil, protocol.ErrEpochNotCommitted
}
func (es *setupEpoch) ClusterByChainID(_ flow.ChainID) (protocol.Cluster, error) {
return nil, protocol.ErrEpochNotCommitted
}
func (es *setupEpoch) DKG() (protocol.DKG, error) {
return nil, protocol.ErrEpochNotCommitted
}
func (es *setupEpoch) RandomSource() ([]byte, error) {
return es.setupEvent.RandomSource, nil
}
// committedEpoch is an implementation of protocol.Epoch backed by an EpochSetup
// and EpochCommit service event. This is used for converting service events to
// inmem.Epoch.
type committedEpoch struct {
setupEpoch
commitEvent *flow.EpochCommit
}
func (es *committedEpoch) Cluster(index uint) (protocol.Cluster, error) {
epochCounter := es.setupEvent.Counter
clustering, err := es.Clustering()
if err != nil {
return nil, fmt.Errorf("failed to generate clustering: %w", err)
}
// TODO: double check ByIndex returns canonical order
members, ok := clustering.ByIndex(index)
if !ok {
return nil, fmt.Errorf("failed to get members of cluster %d: %w", index, err)
}
qcs := es.commitEvent.ClusterQCs
if uint(len(qcs)) <= index {
return nil, fmt.Errorf("no cluster with index %d", index)
}
rootQCVoteData := qcs[index]
signerIndices, err := signature.EncodeSignersToIndices(members.NodeIDs(), rootQCVoteData.VoterIDs)
if err != nil {
return nil, fmt.Errorf("could not encode signer indices for rootQCVoteData.VoterIDs: %w", err)
}
rootBlock := cluster.CanonicalRootBlock(epochCounter, members)
rootQC := &flow.QuorumCertificate{
View: rootBlock.Header.View,
BlockID: rootBlock.ID(),
SignerIndices: signerIndices,
SigData: rootQCVoteData.SigData,
}
cluster, err := ClusterFromEncodable(EncodableCluster{
Index: index,
Counter: epochCounter,
Members: members,
RootBlock: rootBlock,
RootQC: rootQC,
})
return cluster, err
}
func (es *committedEpoch) DKG() (protocol.DKG, error) {
// filter initial participants to valid DKG participants
participants := es.setupEvent.Participants.Filter(filter.IsValidDKGParticipant)
lookup, err := flow.ToDKGParticipantLookup(participants, es.commitEvent.DKGParticipantKeys)
if err != nil {
return nil, fmt.Errorf("could not construct dkg lookup: %w", err)
}
dkg, err := DKGFromEncodable(EncodableDKG{
GroupKey: encodable.RandomBeaconPubKey{
PublicKey: es.commitEvent.DKGGroupKey,
},
Participants: lookup,
})
return dkg, err
}
// NewSetupEpoch returns a memory-backed epoch implementation based on an
// EpochSetup event. Epoch information available after the setup phase will
// not be accessible in the resulting epoch instance.
func NewSetupEpoch(setupEvent *flow.EpochSetup) (*Epoch, error) {
convertible := &setupEpoch{
setupEvent: setupEvent,
}
return FromEpoch(convertible)
}
// NewCommittedEpoch returns a memory-backed epoch implementation based on an
// EpochSetup and EpochCommit event.
func NewCommittedEpoch(setupEvent *flow.EpochSetup, commitEvent *flow.EpochCommit) (*Epoch, error) {
convertible := &committedEpoch{
setupEpoch: setupEpoch{
setupEvent: setupEvent,
},
commitEvent: commitEvent,
}
return FromEpoch(convertible)
}