-
Notifications
You must be signed in to change notification settings - Fork 175
/
convert.go
299 lines (263 loc) · 9 KB
/
convert.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
package inmem
import (
"errors"
"fmt"
"github.com/onflow/flow-go/model/encodable"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/flow/filter"
"github.com/onflow/flow-go/module/signature"
"github.com/onflow/flow-go/state/protocol"
)
// FromSnapshot generates a memory-backed snapshot from the input snapshot.
// Typically, this would be used to convert a database-backed snapshot to
// one that can easily be serialized to disk or to network.
//
func FromSnapshot(from protocol.Snapshot) (*Snapshot, error) {
var (
snap EncodableSnapshot
err error
)
// convert top-level fields
snap.Head, err = from.Head()
if err != nil {
return nil, fmt.Errorf("could not get head: %w", err)
}
snap.Identities, err = from.Identities(filter.Any)
if err != nil {
return nil, fmt.Errorf("could not get identities: %w", err)
}
snap.LatestResult, snap.LatestSeal, err = from.SealedResult()
if err != nil {
return nil, fmt.Errorf("could not get seal: %w", err)
}
snap.SealingSegment, err = from.SealingSegment()
if err != nil {
return nil, fmt.Errorf("could not get sealing segment: %w", err)
}
snap.QuorumCertificate, err = from.QuorumCertificate()
if err != nil {
return nil, fmt.Errorf("could not get qc: %w", err)
}
snap.Phase, err = from.Phase()
if err != nil {
return nil, fmt.Errorf("could not get phase: %w", err)
}
// convert epochs
previous, err := FromEpoch(from.Epochs().Previous())
// it is possible for valid snapshots to have no previous epoch
if errors.Is(err, protocol.ErrNoPreviousEpoch) {
snap.Epochs.Previous = nil
} else if err != nil {
return nil, fmt.Errorf("could not get previous epoch: %w", err)
} else {
snap.Epochs.Previous = &previous.enc
}
current, err := FromEpoch(from.Epochs().Current())
if err != nil {
return nil, fmt.Errorf("could not get current epoch: %w", err)
}
snap.Epochs.Current = current.enc
next, err := FromEpoch(from.Epochs().Next())
// it is possible for valid snapshots to have no next epoch
if errors.Is(err, protocol.ErrNextEpochNotSetup) {
snap.Epochs.Next = nil
} else if err != nil {
return nil, fmt.Errorf("could not get next epoch: %w", err)
} else {
snap.Epochs.Next = &next.enc
}
// convert global state parameters
params, err := FromParams(from.Params())
if err != nil {
return nil, fmt.Errorf("could not get params: %w", err)
}
snap.Params = params.enc
return &Snapshot{snap}, nil
}
// FromParams converts any protocol.GlobalParams to a memory-backed Params.
func FromParams(from protocol.GlobalParams) (*Params, error) {
var (
params EncodableParams
err error
)
params.ChainID, err = from.ChainID()
if err != nil {
return nil, fmt.Errorf("could not get chain id: %w", err)
}
params.SporkID, err = from.SporkID()
if err != nil {
return nil, fmt.Errorf("could not get spork id: %w", err)
}
params.ProtocolVersion, err = from.ProtocolVersion()
if err != nil {
return nil, fmt.Errorf("could not get protocol version: %w", err)
}
return &Params{params}, nil
}
// FromEpoch converts any protocol.Epoch to a memory-backed Epoch.
func FromEpoch(from protocol.Epoch) (*Epoch, error) {
var (
epoch EncodableEpoch
err error
)
// convert top-level fields
epoch.Counter, err = from.Counter()
if err != nil {
return nil, fmt.Errorf("could not get counter: %w", err)
}
epoch.InitialIdentities, err = from.InitialIdentities()
if err != nil {
return nil, fmt.Errorf("could not get initial identities: %w", err)
}
epoch.FirstView, err = from.FirstView()
if err != nil {
return nil, fmt.Errorf("could not get first view: %w", err)
}
epoch.FinalView, err = from.FinalView()
if err != nil {
return nil, fmt.Errorf("could not get final view: %w", err)
}
epoch.RandomSource, err = from.RandomSource()
if err != nil {
return nil, fmt.Errorf("could not get random source: %w", err)
}
epoch.DKGPhase1FinalView, epoch.DKGPhase2FinalView, epoch.DKGPhase3FinalView, err = protocol.DKGPhaseViews(from)
if err != nil {
return nil, fmt.Errorf("could not get dkg final views")
}
clustering, err := from.Clustering()
if err != nil {
return nil, fmt.Errorf("could not get clustering: %w", err)
}
epoch.Clustering = clustering
// convert dkg
dkg, err := from.DKG()
// if this epoch hasn't been committed yet, return the epoch as-is
if errors.Is(err, protocol.ErrEpochNotCommitted) {
return &Epoch{epoch}, nil
}
if err != nil {
return nil, fmt.Errorf("could not get dkg: %w", err)
}
convertedDKG, err := FromDKG(dkg, epoch.InitialIdentities.Filter(filter.HasRole(flow.RoleConsensus)))
if err != nil {
return nil, err
}
epoch.DKG = &convertedDKG.enc
// convert clusters
for index := range clustering {
cluster, err := from.Cluster(uint(index))
if err != nil {
return nil, fmt.Errorf("could not get cluster %d: %w", index, err)
}
convertedCluster, err := FromCluster(cluster)
if err != nil {
return nil, fmt.Errorf("could not convert cluster %d: %w", index, err)
}
epoch.Clusters = append(epoch.Clusters, convertedCluster.enc)
}
return &Epoch{epoch}, nil
}
// FromCluster converts any protocol.Cluster to a memory-backed Cluster
func FromCluster(from protocol.Cluster) (*Cluster, error) {
cluster := EncodableCluster{
Counter: from.EpochCounter(),
Index: from.Index(),
Members: from.Members(),
RootBlock: from.RootBlock(),
RootQC: from.RootQC(),
}
return &Cluster{cluster}, nil
}
// FromDKG converts any protocol.DKG to a memory-backed DKG.
//
// The given participant list must exactly match the DKG members.
func FromDKG(from protocol.DKG, participants flow.IdentityList) (*DKG, error) {
var dkg EncodableDKG
dkg.GroupKey = encodable.RandomBeaconPubKey{PublicKey: from.GroupKey()}
lookup, err := protocol.ToDKGParticipantLookup(from, participants)
if err != nil {
return nil, fmt.Errorf("could not generate dkg participant lookup: %w", err)
}
dkg.Participants = lookup
return &DKG{dkg}, nil
}
// DKGFromEncodable returns a DKG backed by the given encodable representation.
func DKGFromEncodable(enc EncodableDKG) (*DKG, error) {
return &DKG{enc}, nil
}
// ClusterFromEncodable returns a Cluster backed by the given encodable representation.
func ClusterFromEncodable(enc EncodableCluster) (*Cluster, error) {
return &Cluster{enc}, nil
}
// SnapshotFromBootstrapState generates a protocol.Snapshot representing a
// root bootstrap state. This is used to bootstrap the protocol state for
// genesis or post-spork states.
func SnapshotFromBootstrapState(root *flow.Block, result *flow.ExecutionResult, seal *flow.Seal, qc *flow.QuorumCertificate) (*Snapshot, error) {
return SnapshotFromBootstrapStateWithProtocolVersion(root, result, seal, qc, flow.DefaultProtocolVersion)
}
// SnapshotFromBootstrapStateWithProtocolVersion is SnapshotFromBootstrapState
// with a caller-specified protocol version.
func SnapshotFromBootstrapStateWithProtocolVersion(
root *flow.Block,
result *flow.ExecutionResult,
seal *flow.Seal,
qc *flow.QuorumCertificate,
version uint,
) (*Snapshot, error) {
setup, ok := result.ServiceEvents[0].Event.(*flow.EpochSetup)
if !ok {
return nil, fmt.Errorf("invalid setup event type (%T)", result.ServiceEvents[0].Event)
}
commit, ok := result.ServiceEvents[1].Event.(*flow.EpochCommit)
if !ok {
return nil, fmt.Errorf("invalid commit event type (%T)", result.ServiceEvents[1].Event)
}
clustering, err := ClusteringFromSetupEvent(setup)
if err != nil {
return nil, fmt.Errorf("setup event has invalid clustering: %w", err)
}
// sanity check the commit event has the same number of cluster QC as the number clusters
if len(clustering) != len(commit.ClusterQCs) {
return nil, fmt.Errorf("mismatching number of ClusterQCs, expect %v but got %v",
len(clustering), len(commit.ClusterQCs))
}
// sanity check the QC in the commit event, which should be found in the identities in
// the setup event
for i, cluster := range clustering {
rootQCVoteData := commit.ClusterQCs[i]
_, err = signature.EncodeSignersToIndices(cluster.NodeIDs(), rootQCVoteData.VoterIDs)
if err != nil {
return nil, fmt.Errorf("mismatching cluster and qc: %w", err)
}
}
current, err := NewCommittedEpoch(setup, commit)
if err != nil {
return nil, fmt.Errorf("could not convert epoch: %w", err)
}
epochs := EncodableEpochs{
Current: current.enc,
}
params := EncodableParams{
ChainID: root.Header.ChainID, // chain ID must match the root block
SporkID: root.ID(), // use root block ID as the unique spork identifier
ProtocolVersion: version, // major software version for this spork
}
snap := SnapshotFromEncodable(EncodableSnapshot{
Head: root.Header,
Identities: setup.Participants,
LatestSeal: seal,
LatestResult: result,
SealingSegment: &flow.SealingSegment{
Blocks: []*flow.Block{root},
ExecutionResults: flow.ExecutionResultList{result},
LatestSeals: map[flow.Identifier]flow.Identifier{root.ID(): seal.ID()},
FirstSeal: seal,
},
QuorumCertificate: qc,
Phase: flow.EpochPhaseStaking,
Epochs: epochs,
Params: params,
})
return snap, nil
}