-
Notifications
You must be signed in to change notification settings - Fork 180
/
validity_test.go
238 lines (209 loc) · 9.91 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
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)
// add an extra DKG participant key
commit.DKGParticipantKeys = append(commit.DKGParticipantKeys, unittest.KeyFixture(crypto.BLSBLS12381).PublicKey())
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)
})
}