-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
372 lines (328 loc) · 10.5 KB
/
config_test.go
File metadata and controls
372 lines (328 loc) · 10.5 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package hotstuff2
import (
"errors"
"strings"
"testing"
"github.com/edgedlt/hotstuff2/internal/crypto"
"github.com/edgedlt/hotstuff2/timer"
"go.uber.org/zap"
)
// TestConfigCreation tests basic configuration creation.
func TestConfigCreation(t *testing.T) {
validators := NewTestValidatorSet(4)
key, _ := crypto.GenerateEd25519Key()
storage := NewMockStorage[TestHash]()
network := NewMockNetwork[TestHash]()
executor := NewMockExecutor[TestHash]()
mockTimer := timer.NewMockTimer()
cfg, err := NewConfig(
WithMyIndex[TestHash](0),
WithValidators[TestHash](validators),
WithPrivateKey[TestHash](key),
WithStorage[TestHash](storage),
WithNetwork[TestHash](network),
WithExecutor[TestHash](executor),
WithTimer[TestHash](mockTimer),
)
if err != nil {
t.Fatalf("Failed to create config: %v", err)
}
if cfg.MyIndex != 0 {
t.Errorf("MyIndex should be 0, got %d", cfg.MyIndex)
}
if cfg.CryptoScheme != CryptoSchemeEd25519 {
t.Errorf("Default CryptoScheme should be Ed25519, got %s", cfg.CryptoScheme)
}
if cfg.EnableVerification {
t.Error("EnableVerification should be false by default")
}
}
// TestConfigValidationMissingFields tests that required fields are validated.
func TestConfigValidationMissingFields(t *testing.T) {
tests := []struct {
name string
opts []ConfigOption[TestHash]
wantErr string
}{
{
name: "MissingValidators",
opts: []ConfigOption[TestHash]{},
wantErr: "validators is required",
},
{
name: "MissingPrivateKey",
opts: []ConfigOption[TestHash]{
WithValidators[TestHash](NewTestValidatorSet(4)),
},
wantErr: "private key is required",
},
{
name: "MissingStorage",
opts: []ConfigOption[TestHash]{
WithValidators[TestHash](NewTestValidatorSet(4)),
WithPrivateKey[TestHash](mustGenerateKey()),
},
wantErr: "storage is required",
},
{
name: "MissingNetwork",
opts: []ConfigOption[TestHash]{
WithValidators[TestHash](NewTestValidatorSet(4)),
WithPrivateKey[TestHash](mustGenerateKey()),
WithStorage[TestHash](NewMockStorage[TestHash]()),
},
wantErr: "network is required",
},
{
name: "MissingExecutor",
opts: []ConfigOption[TestHash]{
WithValidators[TestHash](NewTestValidatorSet(4)),
WithPrivateKey[TestHash](mustGenerateKey()),
WithStorage[TestHash](NewMockStorage[TestHash]()),
WithNetwork[TestHash](NewMockNetwork[TestHash]()),
},
wantErr: "executor is required",
},
{
name: "MissingTimer",
opts: []ConfigOption[TestHash]{
WithValidators[TestHash](NewTestValidatorSet(4)),
WithPrivateKey[TestHash](mustGenerateKey()),
WithStorage[TestHash](NewMockStorage[TestHash]()),
WithNetwork[TestHash](NewMockNetwork[TestHash]()),
WithExecutor[TestHash](NewMockExecutor[TestHash]()),
},
wantErr: "timer is required",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := NewConfig(tt.opts...)
if err == nil {
t.Fatal("Expected error, got nil")
}
// Check that it's a config error
if !errors.Is(err, ErrConfig) {
t.Errorf("Expected ErrConfig, got: %v", err)
}
// Check that error message contains the expected text
if !strings.Contains(err.Error(), tt.wantErr) {
t.Errorf("Expected error to contain '%s', got '%s'", tt.wantErr, err.Error())
}
})
}
}
// TestConfigInvalidIndex tests validation of my index.
func TestConfigInvalidIndex(t *testing.T) {
validators := NewTestValidatorSet(4) // indices 0-3
_, err := NewConfig(
WithMyIndex[TestHash](5), // Invalid: out of range
WithValidators[TestHash](validators),
WithPrivateKey[TestHash](mustGenerateKey()),
WithStorage[TestHash](NewMockStorage[TestHash]()),
WithNetwork[TestHash](NewMockNetwork[TestHash]()),
WithExecutor[TestHash](NewMockExecutor[TestHash]()),
WithTimer[TestHash](timer.NewMockTimer()),
)
if err == nil {
t.Fatal("Expected error for invalid index")
}
if !errors.Is(err, ErrConfig) {
t.Errorf("Expected ErrConfig, got: %v", err)
}
}
// TestConfigInvalidCryptoScheme tests validation of crypto scheme.
func TestConfigInvalidCryptoScheme(t *testing.T) {
_, err := NewConfig(
WithMyIndex[TestHash](0),
WithValidators[TestHash](NewTestValidatorSet(4)),
WithPrivateKey[TestHash](mustGenerateKey()),
WithStorage[TestHash](NewMockStorage[TestHash]()),
WithNetwork[TestHash](NewMockNetwork[TestHash]()),
WithExecutor[TestHash](NewMockExecutor[TestHash]()),
WithTimer[TestHash](timer.NewMockTimer()),
WithCryptoScheme[TestHash]("invalid"),
)
if err == nil {
t.Fatal("Expected error for invalid crypto scheme")
}
}
// TestConfigWithBLS tests BLS configuration.
func TestConfigWithBLS(t *testing.T) {
cfg, err := NewConfig(
WithMyIndex[TestHash](0),
WithValidators[TestHash](NewTestValidatorSet(4)),
WithPrivateKey[TestHash](mustGenerateKey()),
WithStorage[TestHash](NewMockStorage[TestHash]()),
WithNetwork[TestHash](NewMockNetwork[TestHash]()),
WithExecutor[TestHash](NewMockExecutor[TestHash]()),
WithTimer[TestHash](timer.NewMockTimer()),
WithCryptoScheme[TestHash](CryptoSchemeBLS),
)
if err != nil {
t.Fatalf("Failed to create config with BLS: %v", err)
}
if cfg.CryptoScheme != CryptoSchemeBLS {
t.Errorf("CryptoScheme should be BLS, got %s", cfg.CryptoScheme)
}
}
// TestConfigWithLogger tests custom logger configuration.
func TestConfigWithLogger(t *testing.T) {
logger := zap.NewExample()
cfg, err := NewConfig(
WithMyIndex[TestHash](0),
WithValidators[TestHash](NewTestValidatorSet(4)),
WithPrivateKey[TestHash](mustGenerateKey()),
WithStorage[TestHash](NewMockStorage[TestHash]()),
WithNetwork[TestHash](NewMockNetwork[TestHash]()),
WithExecutor[TestHash](NewMockExecutor[TestHash]()),
WithTimer[TestHash](timer.NewMockTimer()),
WithLogger[TestHash](logger),
)
if err != nil {
t.Fatalf("Failed to create config with logger: %v", err)
}
if cfg.Logger != logger {
t.Error("Logger should be set correctly")
}
}
// TestConfigWithNilLogger tests that nil logger is rejected.
func TestConfigWithNilLogger(t *testing.T) {
_, err := NewConfig(
WithMyIndex[TestHash](0),
WithValidators[TestHash](NewTestValidatorSet(4)),
WithPrivateKey[TestHash](mustGenerateKey()),
WithStorage[TestHash](NewMockStorage[TestHash]()),
WithNetwork[TestHash](NewMockNetwork[TestHash]()),
WithExecutor[TestHash](NewMockExecutor[TestHash]()),
WithTimer[TestHash](timer.NewMockTimer()),
WithLogger[TestHash](nil),
)
if err == nil {
t.Fatal("Expected error for nil logger")
}
}
// TestConfigWithVerification tests verification flag.
func TestConfigWithVerification(t *testing.T) {
cfg, err := NewConfig(
WithMyIndex[TestHash](0),
WithValidators[TestHash](NewTestValidatorSet(4)),
WithPrivateKey[TestHash](mustGenerateKey()),
WithStorage[TestHash](NewMockStorage[TestHash]()),
WithNetwork[TestHash](NewMockNetwork[TestHash]()),
WithExecutor[TestHash](NewMockExecutor[TestHash]()),
WithTimer[TestHash](timer.NewMockTimer()),
WithVerification[TestHash](true),
)
if err != nil {
t.Fatalf("Failed to create config: %v", err)
}
if !cfg.EnableVerification {
t.Error("EnableVerification should be true")
}
}
// TestConfigQuorum tests quorum calculation.
func TestConfigQuorum(t *testing.T) {
tests := []struct {
name string
validatorCount int
expectedQuorum int
}{
{"n=4", 4, 3}, // f=1, quorum=3
{"n=7", 7, 5}, // f=2, quorum=5
{"n=10", 10, 7}, // f=3, quorum=7
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg, err := NewConfig(
WithMyIndex[TestHash](0),
WithValidators[TestHash](NewTestValidatorSet(tt.validatorCount)),
WithPrivateKey[TestHash](mustGenerateKey()),
WithStorage[TestHash](NewMockStorage[TestHash]()),
WithNetwork[TestHash](NewMockNetwork[TestHash]()),
WithExecutor[TestHash](NewMockExecutor[TestHash]()),
WithTimer[TestHash](timer.NewMockTimer()),
)
if err != nil {
t.Fatalf("Failed to create config: %v", err)
}
if cfg.Quorum() != tt.expectedQuorum {
t.Errorf("Expected quorum %d, got %d", tt.expectedQuorum, cfg.Quorum())
}
})
}
}
// TestConfigIsLeader tests leader determination.
func TestConfigIsLeader(t *testing.T) {
cfg, err := NewConfig(
WithMyIndex[TestHash](0),
WithValidators[TestHash](NewTestValidatorSet(4)),
WithPrivateKey[TestHash](mustGenerateKey()),
WithStorage[TestHash](NewMockStorage[TestHash]()),
WithNetwork[TestHash](NewMockNetwork[TestHash]()),
WithExecutor[TestHash](NewMockExecutor[TestHash]()),
WithTimer[TestHash](timer.NewMockTimer()),
)
if err != nil {
t.Fatalf("Failed to create config: %v", err)
}
// View 0: leader should be validator 0
if !cfg.IsLeader(0) {
t.Error("Validator 0 should be leader for view 0")
}
// View 1: leader should be validator 1
if cfg.IsLeader(1) {
t.Error("Validator 0 should NOT be leader for view 1")
}
// View 4: round-robin back to validator 0
if !cfg.IsLeader(4) {
t.Error("Validator 0 should be leader for view 4")
}
}
// TestConfigByzantineFaultTolerance tests n >= 3f+1 validation.
func TestConfigByzantineFaultTolerance(t *testing.T) {
// n=3 gives f=(3-1)/3=0, which satisfies 3>=3*0+1=1, so it should succeed
// We need n=2 to fail: f=(2-1)/3=0, but 2 < 3*0+1=1 is false, so it succeeds
// Actually, with integer division: n=2, f=0, 2>=1 passes
// We need n=0 or n=1 to actually fail
t.Run("ValidMinimal", func(t *testing.T) {
// n=3, f=0: 3 >= 3*0+1=1 ✓
validators := NewTestValidatorSet(3)
_, err := NewConfig(
WithMyIndex[TestHash](0),
WithValidators[TestHash](validators),
WithPrivateKey[TestHash](mustGenerateKey()),
WithStorage[TestHash](NewMockStorage[TestHash]()),
WithNetwork[TestHash](NewMockNetwork[TestHash]()),
WithExecutor[TestHash](NewMockExecutor[TestHash]()),
WithTimer[TestHash](timer.NewMockTimer()),
)
if err != nil {
t.Errorf("n=3 should be valid (f=0, 3>=1), got error: %v", err)
}
})
t.Run("ValidStandard", func(t *testing.T) {
// n=4, f=1: 4 >= 3*1+1=4 ✓
validators := NewTestValidatorSet(4)
_, err := NewConfig(
WithMyIndex[TestHash](0),
WithValidators[TestHash](validators),
WithPrivateKey[TestHash](mustGenerateKey()),
WithStorage[TestHash](NewMockStorage[TestHash]()),
WithNetwork[TestHash](NewMockNetwork[TestHash]()),
WithExecutor[TestHash](NewMockExecutor[TestHash]()),
WithTimer[TestHash](timer.NewMockTimer()),
)
if err != nil {
t.Errorf("n=4 should be valid (f=1, 4>=4), got error: %v", err)
}
})
}
// Helper functions
func mustGenerateKey() PrivateKey {
key, err := crypto.GenerateEd25519Key()
if err != nil {
panic(err)
}
return key
}