-
Notifications
You must be signed in to change notification settings - Fork 189
/
Copy pathvalidity_test.go
350 lines (304 loc) · 15.5 KB
/
validity_test.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
package protocol_test
import (
"testing"
"github.com/onflow/crypto"
"github.com/stretchr/testify/require"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/model/flow/filter"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/utils/unittest"
)
var participants = unittest.IdentityListFixture(20, unittest.WithAllRoles())
func TestEpochSetupValidity(t *testing.T) {
t.Run("invalid first/final view", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
// set an invalid final view for the first epoch
setup.FinalView = setup.FirstView
err := protocol.IsValidEpochSetup(setup, true)
require.Error(t, err)
})
t.Run("non-canonically ordered identities", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
// randomly shuffle the identities so they are not canonically ordered
var err error
setup.Participants, err = setup.Participants.Shuffle()
require.NoError(t, err)
err = protocol.IsValidEpochSetup(setup, true)
require.Error(t, err)
})
t.Run("invalid cluster assignments", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
// create an invalid cluster assignment (node appears in multiple clusters)
collector := participants.Filter(filter.HasRole[flow.Identity](flow.RoleCollection))[0]
setup.Assignments = append(setup.Assignments, []flow.Identifier{collector.NodeID})
err := protocol.IsValidEpochSetup(setup, true)
require.Error(t, err)
})
t.Run("short seed", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
setup.RandomSource = unittest.SeedFixture(crypto.KeyGenSeedMinLen - 1)
err := protocol.IsValidEpochSetup(setup, true)
require.Error(t, err)
})
t.Run("node role missing", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
allWithoutExecutionNodes := setup.Participants.Filter(func(identitySkeleton *flow.IdentitySkeleton) bool {
return identitySkeleton.Role != flow.RoleExecution
})
setup.Participants = allWithoutExecutionNodes
err := protocol.IsValidEpochSetup(setup, true)
require.Error(t, err)
})
t.Run("network addresses are not unique", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
setup.Participants[0].Address = setup.Participants[1].Address
err := protocol.IsValidEpochSetup(setup, true)
require.Error(t, err)
})
t.Run("no cluster assignment", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
setup.Assignments = flow.AssignmentList{}
err := protocol.IsValidEpochSetup(setup, true)
require.Error(t, err)
})
}
func TestBootstrapInvalidEpochCommit(t *testing.T) {
t.Run("inconsistent counter", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
commit := result.ServiceEvents[1].Event.(*flow.EpochCommit)
// use a different counter for the commit
commit.Counter = setup.Counter + 1
err := protocol.IsValidEpochCommit(commit, setup)
require.Error(t, err)
})
t.Run("inconsistent cluster QCs", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
commit := result.ServiceEvents[1].Event.(*flow.EpochCommit)
// add an extra QC to commit
extraQC := unittest.QuorumCertificateWithSignerIDsFixture()
commit.ClusterQCs = append(commit.ClusterQCs, flow.ClusterQCVoteDataFromQC(extraQC))
err := protocol.IsValidEpochCommit(commit, setup)
require.Error(t, err)
})
t.Run("missing dkg group key", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
commit := result.ServiceEvents[1].Event.(*flow.EpochCommit)
commit.DKGGroupKey = nil
err := protocol.IsValidEpochCommit(commit, setup)
require.Error(t, err)
})
t.Run("inconsistent DKG participants", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
commit := result.ServiceEvents[1].Event.(*flow.EpochCommit)
// remove a DKG participant key, this will lead to a case where we have more DKG participants than resulting keys.
commit.DKGParticipantKeys = commit.DKGParticipantKeys[1:]
for nodeID, index := range commit.DKGIndexMap {
if index == 0 {
delete(commit.DKGIndexMap, nodeID)
break
}
}
err := protocol.IsValidEpochCommit(commit, setup)
require.Error(t, err)
})
t.Run("inconsistent DKG index map", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
commit := result.ServiceEvents[1].Event.(*flow.EpochCommit)
// add an extra DKG participant key, this will lead to a case where size of index map is different from the number of keys.
commit.DKGParticipantKeys = append(commit.DKGParticipantKeys, unittest.KeyFixture(crypto.BLSBLS12381).PublicKey())
err := protocol.IsValidEpochCommit(commit, setup)
require.Error(t, err)
})
t.Run("DKG index map contains negative index", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
commit := result.ServiceEvents[1].Event.(*flow.EpochCommit)
// replace entity in the index map so the size matches but with negative index.
nodeID := setup.Participants.Filter(filter.IsConsensusCommitteeMember)[0].NodeID
commit.DKGIndexMap[nodeID] = -1
err := protocol.IsValidEpochCommit(commit, setup)
require.Error(t, err)
})
t.Run("DKG indexes are not consecutive", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
commit := result.ServiceEvents[1].Event.(*flow.EpochCommit)
nodeID := setup.Participants.Filter(filter.IsConsensusCommitteeMember)[0].NodeID
commit.DKGIndexMap[nodeID] = len(commit.DKGParticipantKeys) // change index so it's out of bound and not consecutive
err := protocol.IsValidEpochCommit(commit, setup)
require.Error(t, err)
})
t.Run("DKG indexes are duplicated", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
commit := result.ServiceEvents[1].Event.(*flow.EpochCommit)
// replace entity in the index map so the size matches but with negative index.
nodeID := setup.Participants.Filter(filter.IsConsensusCommitteeMember)[0].NodeID
otherNodeID := setup.Participants.Filter(filter.IsConsensusCommitteeMember)[1].NodeID
commit.DKGIndexMap[nodeID] = commit.DKGIndexMap[otherNodeID] // change index so it's out of bound and not consecutive
err := protocol.IsValidEpochCommit(commit, setup)
require.Error(t, err)
})
t.Run("random beacon safety threshold not met", func(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
commit := result.ServiceEvents[1].Event.(*flow.EpochCommit)
requiredThreshold := protocol.RandomBeaconSafetyThreshold(uint(len(commit.DKGIndexMap)))
require.Greater(t, requiredThreshold, uint(0), "threshold has to be at least 1, otherwise the test is invalid")
// sample one less than the required threshold, so the threshold is not met
sampled, err := setup.Participants.Filter(filter.IsConsensusCommitteeMember).Sample(requiredThreshold - 1)
require.NoError(t, err)
setup.Participants = sampled
err = protocol.IsValidEpochCommit(commit, setup)
require.Error(t, err)
})
}
// TestIsValidEpochCommitBackwardCompatible tests that the implementation is backward compatible with the previous version of EpochCommit.
// The main difference in validation logic is that the old version requires that number of consensus participants is
// equal to the number of keys(random beacon participants) for new version of the [flow.EpochCommit] this is not required rather
// we rely on a threshold for random beacon participants.
// TODO(EFM, #6794): Remove this once we complete the network upgrade
func TestIsValidEpochCommitBackwardCompatible(t *testing.T) {
_, result, _ := unittest.BootstrapFixture(participants)
setup := result.ServiceEvents[0].Event.(*flow.EpochSetup)
commit := result.ServiceEvents[1].Event.(*flow.EpochCommit)
requiredThreshold := protocol.RandomBeaconSafetyThreshold(uint(len(commit.DKGIndexMap)))
require.Less(t, int(requiredThreshold), len(commit.DKGParticipantKeys),
"threshold has to be at lower than the number of keys, otherwise the test is invalid")
// preserve the DKGIndexMap since we will be removing it later
dkgIndexMap := commit.DKGIndexMap
// since we are passing the new version validation result should be successful
err := protocol.IsValidEpochCommit(commit, setup)
require.NoError(t, err)
commit.DKGIndexMap = nil
// since we are passing the v0 version(because we have removed DKGIndexMap) validation result should be successful
// because number of participant keys is equal to the number of consensus participants.
err = protocol.IsValidEpochCommit(commit, setup)
require.NoError(t, err)
// now we are going to sample participants so the number of keys is not equal to the number of consensus participants
// but the threshold for random beacon participants is still met. This is valid for the new version of the [flow.EpochCommit]
// since it requires the [flow.DKGIndexMap] to be present, but it's invalid for the v0 version.
sampled, err := setup.Participants.Filter(filter.IsConsensusCommitteeMember).Sample(requiredThreshold)
require.NoError(t, err)
setup.Participants = sampled
commit.DKGIndexMap = dkgIndexMap
err = protocol.IsValidEpochCommit(commit, setup)
require.NoError(t, err)
commit.DKGIndexMap = nil
// since we are passing the v0 version(because we have removed DKGIndexMap) validation result should be an error
// because number of participant keys is not equal to the number of consensus participants and this is not valid for the v0 version.
err = protocol.IsValidEpochCommit(commit, setup)
require.Error(t, err)
}
// TestIsValidExtendingEpochSetup tests that implementation enforces the following protocol rules in case they are violated:
// (a) We should only have a single epoch setup event per epoch.
// (b) The setup event should have the counter increased by one
// (c) The first view needs to be exactly one greater than the current epoch final view
// additionally we require other conditions, but they are tested by separate test `TestEpochSetupValidity`.
func TestIsValidExtendingEpochSetup(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
protocolState := unittest.EpochStateFixture().EpochStateEntry
currentEpochSetup := protocolState.CurrentEpochSetup
extendingSetup := unittest.EpochSetupFixture(
unittest.WithFirstView(currentEpochSetup.FinalView+1),
unittest.WithFinalView(currentEpochSetup.FinalView+1000),
unittest.SetupWithCounter(currentEpochSetup.Counter+1),
unittest.WithParticipants(participants.ToSkeleton()),
)
err := protocol.IsValidExtendingEpochSetup(extendingSetup, protocolState)
require.NoError(t, err)
})
t.Run("(a) We should only have a single epoch setup event per epoch.", func(t *testing.T) {
protocolState := unittest.EpochStateFixture(unittest.WithNextEpochProtocolState()).EpochStateEntry
currentEpochSetup := protocolState.CurrentEpochSetup
extendingSetup := unittest.EpochSetupFixture(
unittest.WithFirstView(currentEpochSetup.FinalView+1),
unittest.WithFinalView(currentEpochSetup.FinalView+1000),
unittest.SetupWithCounter(currentEpochSetup.Counter+1),
unittest.WithParticipants(participants.ToSkeleton()),
)
err := protocol.IsValidExtendingEpochSetup(extendingSetup, protocolState)
require.Error(t, err)
})
t.Run("(b) The setup event should have the counter increased by one", func(t *testing.T) {
protocolState := unittest.EpochStateFixture().EpochStateEntry
currentEpochSetup := protocolState.CurrentEpochSetup
extendingSetup := unittest.EpochSetupFixture(
unittest.WithFirstView(currentEpochSetup.FinalView+1),
unittest.WithFinalView(currentEpochSetup.FinalView+1000),
unittest.SetupWithCounter(currentEpochSetup.Counter+2),
unittest.WithParticipants(participants.ToSkeleton()),
)
err := protocol.IsValidExtendingEpochSetup(extendingSetup, protocolState)
require.Error(t, err)
})
t.Run("(c) The first view needs to be exactly one greater than the current epoch final view", func(t *testing.T) {
protocolState := unittest.EpochStateFixture().EpochStateEntry
currentEpochSetup := protocolState.CurrentEpochSetup
extendingSetup := unittest.EpochSetupFixture(
unittest.WithFirstView(currentEpochSetup.FinalView+2),
unittest.WithFinalView(currentEpochSetup.FinalView+1000),
unittest.SetupWithCounter(currentEpochSetup.Counter+1),
unittest.WithParticipants(participants.ToSkeleton()),
)
err := protocol.IsValidExtendingEpochSetup(extendingSetup, protocolState)
require.Error(t, err)
})
}
// TestIsValidExtendingEpochCommit tests that implementation enforces the following protocol rules in case they are violated:
// (a) The epoch setup event needs to happen before the commit.
// (b) We should only have a single epoch commit event per epoch.
// additionally we require other conditions, but they are tested by separate test `TestEpochCommitValidity`.
func TestIsValidExtendingEpochCommit(t *testing.T) {
t.Run("happy path", func(t *testing.T) {
protocolState := unittest.EpochStateFixture(unittest.WithNextEpochProtocolState(), func(entry *flow.RichEpochStateEntry) {
entry.NextEpochCommit = nil
entry.NextEpoch.CommitID = flow.ZeroID
})
nextEpochSetup := protocolState.NextEpochSetup
extendingSetup := unittest.EpochCommitFixture(
unittest.CommitWithCounter(nextEpochSetup.Counter),
unittest.WithDKGFromParticipants(nextEpochSetup.Participants),
)
err := protocol.IsValidExtendingEpochCommit(extendingSetup, protocolState.MinEpochStateEntry, nextEpochSetup)
require.NoError(t, err)
})
t.Run("(a) The epoch setup event needs to happen before the commit", func(t *testing.T) {
protocolState := unittest.EpochStateFixture()
currentEpochSetup := protocolState.CurrentEpochSetup
nextEpochSetup := unittest.EpochSetupFixture(
unittest.WithFirstView(currentEpochSetup.FinalView+1),
unittest.WithFinalView(currentEpochSetup.FinalView+1000),
unittest.SetupWithCounter(currentEpochSetup.Counter+1),
unittest.WithParticipants(participants.ToSkeleton()),
)
extendingSetup := unittest.EpochCommitFixture(
unittest.CommitWithCounter(nextEpochSetup.Counter),
unittest.WithDKGFromParticipants(nextEpochSetup.Participants),
)
err := protocol.IsValidExtendingEpochCommit(extendingSetup, protocolState.MinEpochStateEntry, nextEpochSetup)
require.Error(t, err)
})
t.Run("We should only have a single epoch commit event per epoch", func(t *testing.T) {
protocolState := unittest.EpochStateFixture(unittest.WithNextEpochProtocolState())
nextEpochSetup := protocolState.NextEpochSetup
extendingSetup := unittest.EpochCommitFixture(
unittest.CommitWithCounter(nextEpochSetup.Counter),
unittest.WithDKGFromParticipants(nextEpochSetup.Participants),
)
err := protocol.IsValidExtendingEpochCommit(extendingSetup, protocolState.MinEpochStateEntry, nextEpochSetup)
require.Error(t, err)
})
}