-
Notifications
You must be signed in to change notification settings - Fork 107
/
keymanager_churp.go
281 lines (233 loc) · 6.14 KB
/
keymanager_churp.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
package runtime
import (
"context"
"fmt"
"time"
beacon "github.com/oasisprotocol/oasis-core/go/beacon/api"
"github.com/oasisprotocol/oasis-core/go/keymanager/churp"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/env"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/oasis"
"github.com/oasisprotocol/oasis-core/go/oasis-test-runner/scenario"
)
// KeymanagerChurp is the key manager CHURP scenario.
var KeymanagerChurp scenario.Scenario = newKmChurpImpl()
type kmChurpImpl struct {
Scenario
}
func newKmChurpImpl() scenario.Scenario {
return &kmChurpImpl{
Scenario: *NewScenario(
"keymanager-churp",
NewTestClient(),
),
}
}
func (sc *kmChurpImpl) Clone() scenario.Scenario {
return &kmChurpImpl{
Scenario: *sc.Scenario.Clone().(*Scenario),
}
}
func (sc *kmChurpImpl) Fixture() (*oasis.NetworkFixture, error) {
f, err := sc.Scenario.Fixture()
if err != nil {
return nil, err
}
// We don't need compute workers.
f.ComputeWorkers = []oasis.ComputeWorkerFixture{}
// We need 4 key managers.
f.Keymanagers[0].ChurpIDs = []uint8{0}
for i := 0; i < 3; i++ {
f.Keymanagers = append(f.Keymanagers, f.Keymanagers[0])
}
for i := 2; i < 4; i++ {
f.Keymanagers[i].NoAutoStart = true
}
// Enable CHURP extension.
f.Network.EnableKeyManagerCHURP = true
// Speed up the test.
f.Network.Beacon.VRFParameters = &beacon.VRFParameters{
Interval: 10,
ProofSubmissionDelay: 2,
}
return f, nil
}
func (sc *kmChurpImpl) Run(ctx context.Context, _ *env.Env) error { //nolint: gocyclo
var nonce uint64
if err := sc.Net.Start(); err != nil {
return err
}
if err := sc.Net.ClientController().WaitReady(ctx); err != nil {
return err
}
stCh, stSub, err := sc.Net.ClientController().Keymanager.Churp().WatchStatuses(ctx)
if err != nil {
return err
}
defer stSub.Close()
// Create scheme.
id := uint8(0)
threshold := uint8(1)
handoffInterval := beacon.EpochTime(2)
if err = sc.createChurp(ctx, id, threshold, handoffInterval, nonce); err != nil {
return err
}
nonce++
// The dealing round requires threshold + 2 key manager nodes.
// Since only 2 are running, all handoffs should fail.
sc.Logger.Info("testing dealing phase (not enough nodes)")
var (
status *churp.Status
lastStatus *churp.Status
failed int
)
for failed < 2 {
status, err = sc.nextChurpStatus(ctx, stCh)
if err != nil {
return err
}
if status.Handoff != 0 || status.Committee != nil {
return fmt.Errorf("dealing phase should fail")
}
if lastStatus != nil && lastStatus.NextHandoff != 0 {
if status.NextHandoff-lastStatus.NextHandoff > 1 {
return fmt.Errorf("failed handoffs should be 1 epoch apart")
}
if status.NextHandoff != lastStatus.NextHandoff {
failed++
}
}
lastStatus = status
}
// The dealing phase.
sc.Logger.Info("testing dealing phase")
if err = sc.Net.Keymanagers()[2].Start(); err != nil {
return err
}
for status.Handoff == 0 {
status, err = sc.nextChurpStatus(ctx, stCh)
if err != nil {
return err
}
}
if len(status.Committee) != 3 {
return fmt.Errorf("committee should have 3 members")
}
if status.Checksum == nil {
return fmt.Errorf("checksum should be set")
}
if status.NextHandoff-status.Handoff != handoffInterval {
return fmt.Errorf("invalid handoff interval")
}
// Committee unchanged.
for i := 0; i < 2; i++ {
sc.Logger.Info("testing committee unchanged",
"round", i,
)
lastStatus = status
status, err = sc.waitNextHandoff(ctx, status.Handoff, stCh)
if err != nil {
return err
}
if len(status.Committee) != 3 {
return fmt.Errorf("committee should have 3 members")
}
if status.Checksum == lastStatus.Checksum {
return fmt.Errorf("checksum should change")
}
}
// Committee changed.
for i := 0; i < 2; i++ {
// Add node.
sc.Logger.Info("testing committee changed (node added)",
"round", i,
)
if err = sc.Net.Keymanagers()[3].Start(); err != nil {
return err
}
// Ignore the first handoff as the node needs time to start.
for j := 0; j < 2; j++ {
lastStatus = status
status, err = sc.waitNextHandoff(ctx, status.Handoff, stCh)
if err != nil {
return err
}
}
if len(status.Committee) != 4 {
return fmt.Errorf("committee should have 4 members")
}
if status.Checksum == lastStatus.Checksum {
return fmt.Errorf("checksum should change")
}
// Remove node.
sc.Logger.Info("testing committee changed (node removed)",
"round", i,
)
if err = sc.Net.Keymanagers()[3].Stop(); err != nil {
return err
}
lastStatus = status
status, err = sc.waitNextHandoff(ctx, status.Handoff, stCh)
if err != nil {
return err
}
if len(status.Committee) != 3 {
return fmt.Errorf("committee should have 3 members")
}
if status.Checksum == lastStatus.Checksum {
return fmt.Errorf("checksum should change")
}
}
// Handoffs disabled.
sc.Logger.Info("testing handoffs disabled")
handoffInterval = 0
if err = sc.updateChurp(ctx, id, handoffInterval, nonce); err != nil {
return err
}
nonce++
// After the update, there should be no status updates.
for !status.HandoffsDisabled() {
status, err = sc.nextChurpStatus(ctx, stCh)
if err != nil {
return err
}
}
select {
case status = <-stCh:
sc.Logger.Info("status updated",
"status", fmt.Sprintf("%+v", status),
)
return fmt.Errorf("handoffs should be disabled")
case <-ctx.Done():
return ctx.Err()
case <-time.After(20 * time.Second):
}
// Handoffs enabled.
sc.Logger.Info("testing handoffs enabled")
handoffInterval = beacon.EpochTime(1)
if err = sc.updateChurp(ctx, id, handoffInterval, nonce); err != nil {
return err
}
for i := 0; i < 2; i++ {
lastStatus = status
status, err = sc.waitNextHandoff(ctx, status.Handoff, stCh)
if err != nil {
return err
}
}
if status.Handoff-lastStatus.Handoff != handoffInterval {
return fmt.Errorf("invalid handoff interval")
}
return nil
}
func (sc *kmChurpImpl) waitNextHandoff(ctx context.Context, epoch beacon.EpochTime, stCh <-chan *churp.Status) (*churp.Status, error) {
for {
status, err := sc.nextChurpStatus(ctx, stCh)
if err != nil {
return nil, err
}
if status.Handoff <= epoch {
continue
}
return status, nil
}
}