-
Notifications
You must be signed in to change notification settings - Fork 107
/
keymanager_churp_many.go
190 lines (157 loc) · 4.25 KB
/
keymanager_churp_many.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
package runtime
import (
"context"
"fmt"
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"
)
// KeymanagerChurpMany is the key manager CHURP scenario with multiple schemes.
var KeymanagerChurpMany scenario.Scenario = newKmChurpManyImpl()
type kmChurpManyImpl struct {
Scenario
}
func newKmChurpManyImpl() scenario.Scenario {
return &kmChurpManyImpl{
Scenario: *NewScenario(
"keymanager-churp-many",
NewTestClient(),
),
}
}
func (sc *kmChurpManyImpl) Clone() scenario.Scenario {
return &kmChurpManyImpl{
Scenario: *sc.Scenario.Clone().(*Scenario),
}
}
func (sc *kmChurpManyImpl) 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 6 key managers for 3 schemes (012+5, 234+5, 01234+5).
for i := 0; i < 5; i++ {
f.Keymanagers = append(f.Keymanagers, f.Keymanagers[0])
}
f.Keymanagers[0].ChurpIDs = []uint8{0, 2}
f.Keymanagers[1].ChurpIDs = []uint8{0, 2}
f.Keymanagers[2].ChurpIDs = []uint8{0, 1, 2}
f.Keymanagers[3].ChurpIDs = []uint8{1, 2}
f.Keymanagers[4].ChurpIDs = []uint8{1, 2}
f.Keymanagers[5].ChurpIDs = []uint8{0, 1, 2}
f.Keymanagers[5].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 *kmChurpManyImpl) Run(ctx context.Context, _ *env.Env) error {
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 schemes.
schemes := []struct {
id uint8
threshold uint8
handoffInterval beacon.EpochTime
numNodes int
}{
{0, 0, 1, 4},
{1, 1, 2, 4},
{2, 2, 3, 6},
}
for _, s := range schemes {
if err = sc.createChurp(ctx, s.id, s.threshold, s.handoffInterval, nonce); err != nil {
return err
}
nonce++
}
// Dealing phase + committee unchanged.
sc.Logger.Info("testing dealing phase and committee unchanged")
var status *churp.Status
lastStatuses := make(map[uint8]*churp.Status)
handoffs := make(map[uint8]int)
wait := true
for wait {
status, err = sc.nextChurpStatus(ctx, stCh)
if err != nil {
return err
}
lastStatus, ok := lastStatuses[status.ID]
lastStatuses[status.ID] = status
if !ok || lastStatus.Handoff == status.Handoff {
continue
}
if lastStatus.Handoff != 0 && status.Handoff-lastStatus.Handoff != status.HandoffInterval {
return fmt.Errorf("invalid handoff interval")
}
if lastStatus.Checksum == status.Checksum {
return fmt.Errorf("checksum should change")
}
handoffs[status.ID]++
// Wait until all schemes do the dealing phase and at least 2 handoffs
// where committee doesn't change.
wait = false
for _, n := range handoffs {
if n < 3 {
wait = true
break
}
}
}
// Committee changed.
waitCommitteeChange := func(diff int) error {
wait = true
for wait {
status, err = sc.nextChurpStatus(ctx, stCh)
if err != nil {
return err
}
lastStatuses[status.ID] = status
// Wait until all schemes add or remove the new node.
wait = false
for _, scheme := range schemes {
lastStatus := lastStatuses[scheme.id]
if len(lastStatus.Committee) != scheme.numNodes-diff {
wait = true
break
}
}
}
return nil
}
// Add node.
sc.Logger.Info("testing committee changed (node added)")
if err = sc.Net.Keymanagers()[5].Start(); err != nil {
return err
}
if err = waitCommitteeChange(0); err != nil {
return err
}
// Remove node.
sc.Logger.Info("testing committee changed (node removed)")
if err = sc.Net.Keymanagers()[2].Stop(); err != nil {
return err
}
if err = waitCommitteeChange(1); err != nil {
return err
}
return nil
}