-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreconfiguration_test.go
More file actions
566 lines (450 loc) · 17.5 KB
/
Copy pathreconfiguration_test.go
File metadata and controls
566 lines (450 loc) · 17.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
package narwhal
import (
"sync/atomic"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)
// mockValidatorSetForReconfig implements ValidatorSet for testing
type mockValidatorSetForReconfig struct {
count int
}
func (v mockValidatorSetForReconfig) Count() int { return v.count }
func (v mockValidatorSetForReconfig) GetByIndex(idx uint16) (PublicKey, error) { return nil, nil }
func (v mockValidatorSetForReconfig) Contains(idx uint16) bool { return int(idx) < v.count }
func (v mockValidatorSetForReconfig) F() int { return (v.count - 1) / 3 }
func TestReconfigurationState_String(t *testing.T) {
tests := []struct {
state ReconfigurationState
expected string
}{
{ReconfigurationStateIdle, "IDLE"},
{ReconfigurationStatePending, "PENDING"},
{ReconfigurationStateCommitting, "COMMITTING"},
{ReconfigurationStateComplete, "COMPLETE"},
{ReconfigurationState(99), "UNKNOWN"},
}
for _, tt := range tests {
t.Run(tt.expected, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.state.String())
})
}
}
func TestReconfigurer_NewReconfigurer(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
t.Run("with logger", func(t *testing.T) {
r := NewReconfigurer(0, validators, zap.NewNop())
require.NotNil(t, r)
assert.Equal(t, uint64(0), r.CurrentEpoch())
assert.Equal(t, ReconfigurationStateIdle, r.State())
})
t.Run("with nil logger", func(t *testing.T) {
r := NewReconfigurer(5, validators, nil)
require.NotNil(t, r)
assert.Equal(t, uint64(5), r.CurrentEpoch())
})
}
func TestReconfigurer_CurrentEpoch(t *testing.T) {
r := NewReconfigurer(10, mockValidatorSetForReconfig{count: 4}, nil)
assert.Equal(t, uint64(10), r.CurrentEpoch())
}
func TestReconfigurer_CurrentValidators(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 7}
r := NewReconfigurer(0, validators, nil)
assert.Equal(t, 7, r.CurrentValidators().Count())
}
func TestReconfigurer_ProposeEpochChange(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
newValidators := mockValidatorSetForReconfig{count: 5}
t.Run("successful proposal", func(t *testing.T) {
r := NewReconfigurer(0, validators, nil)
err := r.ProposeEpochChange(1, 100, newValidators)
require.NoError(t, err)
assert.Equal(t, ReconfigurationStatePending, r.State())
change := r.PendingChange()
require.NotNil(t, change)
assert.Equal(t, uint64(0), change.FromEpoch)
assert.Equal(t, uint64(1), change.ToEpoch)
assert.Equal(t, uint64(100), change.EffectiveRound)
})
t.Run("error when reconfiguration in progress", func(t *testing.T) {
r := NewReconfigurer(0, validators, nil)
require.NoError(t, r.ProposeEpochChange(1, 100, nil))
err := r.ProposeEpochChange(2, 200, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "already in progress")
})
t.Run("error for invalid epoch transition", func(t *testing.T) {
r := NewReconfigurer(0, validators, nil)
err := r.ProposeEpochChange(5, 100, nil) // Should be 1
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid epoch transition")
})
}
func TestReconfigurer_BeginCommit(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
t.Run("successful begin commit", func(t *testing.T) {
r := NewReconfigurer(0, validators, nil)
require.NoError(t, r.ProposeEpochChange(1, 100, nil))
err := r.BeginCommit()
require.NoError(t, err)
assert.Equal(t, ReconfigurationStateCommitting, r.State())
})
t.Run("error when not pending", func(t *testing.T) {
r := NewReconfigurer(0, validators, nil)
err := r.BeginCommit()
assert.Error(t, err)
assert.Contains(t, err.Error(), "not in pending state")
})
}
func TestReconfigurer_CompleteCommit(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
newValidators := mockValidatorSetForReconfig{count: 5}
t.Run("successful complete commit", func(t *testing.T) {
r := NewReconfigurer(0, validators, nil)
require.NoError(t, r.ProposeEpochChange(1, 100, newValidators))
require.NoError(t, r.BeginCommit())
err := r.CompleteCommit()
require.NoError(t, err)
assert.Equal(t, ReconfigurationStateIdle, r.State())
assert.Equal(t, uint64(1), r.CurrentEpoch())
assert.Equal(t, 5, r.CurrentValidators().Count())
assert.Nil(t, r.PendingChange())
})
t.Run("complete without new validators", func(t *testing.T) {
r := NewReconfigurer(0, validators, nil)
require.NoError(t, r.ProposeEpochChange(1, 100, nil))
require.NoError(t, r.BeginCommit())
err := r.CompleteCommit()
require.NoError(t, err)
// Validators unchanged
assert.Equal(t, 4, r.CurrentValidators().Count())
})
t.Run("error when not committing", func(t *testing.T) {
r := NewReconfigurer(0, validators, nil)
err := r.CompleteCommit()
assert.Error(t, err)
assert.Contains(t, err.Error(), "not in committing state")
})
}
func TestReconfigurer_CancelPendingChange(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
t.Run("successful cancel", func(t *testing.T) {
r := NewReconfigurer(0, validators, nil)
require.NoError(t, r.ProposeEpochChange(1, 100, nil))
err := r.CancelPendingChange()
require.NoError(t, err)
assert.Equal(t, ReconfigurationStateIdle, r.State())
assert.Nil(t, r.PendingChange())
})
t.Run("error when not pending", func(t *testing.T) {
r := NewReconfigurer(0, validators, nil)
err := r.CancelPendingChange()
assert.Error(t, err)
assert.Contains(t, err.Error(), "not in pending state")
})
}
func TestReconfigurer_OnEpochChange(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
var callbackCalled atomic.Int32
var receivedChange EpochChange
r := NewReconfigurer(0, validators, nil)
r.OnEpochChange(func(change EpochChange) {
callbackCalled.Add(1)
receivedChange = change
})
require.NoError(t, r.ProposeEpochChange(1, 100, nil))
require.NoError(t, r.BeginCommit())
require.NoError(t, r.CompleteCommit())
assert.Equal(t, int32(1), callbackCalled.Load())
assert.Equal(t, uint64(0), receivedChange.FromEpoch)
assert.Equal(t, uint64(1), receivedChange.ToEpoch)
assert.False(t, receivedChange.CommittedAt.IsZero())
}
func TestReconfigurer_MultipleCallbacks(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
var callback1Called, callback2Called atomic.Int32
r := NewReconfigurer(0, validators, nil)
r.OnEpochChange(func(change EpochChange) {
callback1Called.Add(1)
})
r.OnEpochChange(func(change EpochChange) {
callback2Called.Add(1)
})
require.NoError(t, r.ProposeEpochChange(1, 100, nil))
require.NoError(t, r.BeginCommit())
require.NoError(t, r.CompleteCommit())
assert.Equal(t, int32(1), callback1Called.Load())
assert.Equal(t, int32(1), callback2Called.Load())
}
func TestReconfigurer_ShouldAcceptForEpoch(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
r := NewReconfigurer(5, validators, nil)
// Current epoch should be accepted
assert.True(t, r.ShouldAcceptForEpoch(5))
// Old epoch should not be accepted
assert.False(t, r.ShouldAcceptForEpoch(4))
// Future epoch should not be accepted
assert.False(t, r.ShouldAcceptForEpoch(6))
// During transition, new epoch should be accepted when committing
require.NoError(t, r.ProposeEpochChange(6, 100, nil))
assert.False(t, r.ShouldAcceptForEpoch(6)) // Not yet committing
require.NoError(t, r.BeginCommit())
assert.True(t, r.ShouldAcceptForEpoch(6)) // Now committing
}
func TestReconfigurer_IsEpochBoundaryRound(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
r := NewReconfigurer(0, validators, nil)
// No pending change
assert.False(t, r.IsEpochBoundaryRound(100))
// With pending change
require.NoError(t, r.ProposeEpochChange(1, 100, nil))
assert.True(t, r.IsEpochBoundaryRound(100))
assert.False(t, r.IsEpochBoundaryRound(99))
assert.False(t, r.IsEpochBoundaryRound(101))
}
func TestReconfigurer_History(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
r := NewReconfigurer(0, validators, nil)
// Initially empty
assert.Len(t, r.History(), 0)
// Complete a few reconfigurations
for i := uint64(1); i <= 3; i++ {
require.NoError(t, r.ProposeEpochChange(i, i*100, nil))
require.NoError(t, r.BeginCommit())
require.NoError(t, r.CompleteCommit())
}
history := r.History()
assert.Len(t, history, 3)
assert.Equal(t, uint64(0), history[0].FromEpoch)
assert.Equal(t, uint64(1), history[0].ToEpoch)
assert.Equal(t, uint64(2), history[1].ToEpoch)
assert.Equal(t, uint64(3), history[2].ToEpoch)
}
func TestReconfigurer_HistoryLimit(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
r := NewReconfigurer(0, validators, nil)
r.maxHistory = 5
// Complete more reconfigurations than the limit
for i := uint64(1); i <= 10; i++ {
require.NoError(t, r.ProposeEpochChange(i, i*100, nil))
require.NoError(t, r.BeginCommit())
require.NoError(t, r.CompleteCommit())
}
history := r.History()
assert.Len(t, history, 5)
// Should have the most recent 5
assert.Equal(t, uint64(6), history[0].ToEpoch)
assert.Equal(t, uint64(10), history[4].ToEpoch)
}
func TestReconfigurer_Stats(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 7}
r := NewReconfigurer(5, validators, nil)
r.OnEpochChange(func(change EpochChange) {})
stats := r.Stats()
assert.Equal(t, uint64(5), stats.CurrentEpoch)
assert.Equal(t, ReconfigurationStateIdle, stats.State)
assert.False(t, stats.HasPendingChange)
assert.Equal(t, 7, stats.ValidatorCount)
assert.Equal(t, 1, stats.CallbackCount)
// With pending change
require.NoError(t, r.ProposeEpochChange(6, 100, nil))
stats = r.Stats()
assert.True(t, stats.HasPendingChange)
assert.Equal(t, uint64(6), stats.PendingToEpoch)
}
func TestValidatorSetChangeType_String(t *testing.T) {
tests := []struct {
changeType ValidatorSetChangeType
expected string
}{
{ValidatorSetChangeAdd, "ADD"},
{ValidatorSetChangeRemove, "REMOVE"},
{ValidatorSetChangeUpdate, "UPDATE"},
{ValidatorSetChangeType(99), "UNKNOWN"},
}
for _, tt := range tests {
t.Run(tt.expected, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.changeType.String())
})
}
}
func TestEpochAwareValidatorSet(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
epochAware := NewEpochAwareValidatorSet(validators, 10)
assert.Equal(t, uint64(10), epochAware.Epoch())
assert.Equal(t, 4, epochAware.Count())
assert.Equal(t, 1, epochAware.F())
}
// testReconfigHash is a simple hash type for coordinator testing
type testReconfigHash [32]byte
func (h testReconfigHash) Bytes() []byte { return h[:] }
func (h testReconfigHash) Equals(other Hash) bool {
if o, ok := other.(testReconfigHash); ok {
return h == o
}
return false
}
func (h testReconfigHash) String() string { return string(h[:8]) }
func TestReconfigurationCoordinator_NewReconfigurationCoordinator(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
r := NewReconfigurer(0, validators, nil)
hashFunc := func(b []byte) testReconfigHash { var h testReconfigHash; copy(h[:], b); return h }
coord := NewReconfigurationCoordinator[testReconfigHash](r, hashFunc, nil)
require.NotNil(t, coord)
}
func TestReconfigurationCoordinator_ProposeReconfiguration(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
r := NewReconfigurer(0, validators, nil)
hashFunc := func(b []byte) testReconfigHash { var h testReconfigHash; copy(h[:], b); return h }
coord := NewReconfigurationCoordinator[testReconfigHash](r, hashFunc, nil)
changes := []ValidatorSetChange{
{Type: ValidatorSetChangeAdd, ValidatorIndex: 4},
}
err := coord.ProposeReconfiguration(100, changes, 0, []byte("sig"))
require.NoError(t, err)
proposal := coord.CurrentProposal()
require.NotNil(t, proposal)
assert.Equal(t, uint64(1), proposal.Epoch)
assert.Equal(t, uint64(100), proposal.EffectiveRound)
assert.Len(t, proposal.Changes, 1)
}
func TestReconfigurationCoordinator_ProposeReconfiguration_AlreadyInProgress(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
r := NewReconfigurer(0, validators, nil)
hashFunc := func(b []byte) testReconfigHash { var h testReconfigHash; copy(h[:], b); return h }
coord := NewReconfigurationCoordinator[testReconfigHash](r, hashFunc, nil)
require.NoError(t, coord.ProposeReconfiguration(100, nil, 0, nil))
err := coord.ProposeReconfiguration(200, nil, 1, nil)
assert.Error(t, err)
assert.Contains(t, err.Error(), "already in progress")
}
func TestReconfigurationCoordinator_AddVote(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
r := NewReconfigurer(0, validators, nil)
hashFunc := func(b []byte) testReconfigHash { var h testReconfigHash; copy(h[:], b); return h }
coord := NewReconfigurationCoordinator[testReconfigHash](r, hashFunc, nil)
require.NoError(t, coord.ProposeReconfiguration(100, nil, 0, nil))
vote := &ReconfigurationVote{
ProposalHash: []byte("hash"),
Voter: 1,
Approve: true,
Signature: []byte("sig"),
}
err := coord.AddVote(vote)
require.NoError(t, err)
approvals, rejections, total := coord.VoteStats()
assert.Equal(t, 1, approvals)
assert.Equal(t, 0, rejections)
assert.Equal(t, 1, total)
}
func TestReconfigurationCoordinator_AddVote_NoProposal(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
r := NewReconfigurer(0, validators, nil)
hashFunc := func(b []byte) testReconfigHash { var h testReconfigHash; copy(h[:], b); return h }
coord := NewReconfigurationCoordinator[testReconfigHash](r, hashFunc, nil)
vote := &ReconfigurationVote{Voter: 1, Approve: true}
err := coord.AddVote(vote)
assert.Error(t, err)
assert.Contains(t, err.Error(), "no proposal in progress")
}
func TestReconfigurationCoordinator_AddVote_Duplicate(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
r := NewReconfigurer(0, validators, nil)
hashFunc := func(b []byte) testReconfigHash { var h testReconfigHash; copy(h[:], b); return h }
coord := NewReconfigurationCoordinator[testReconfigHash](r, hashFunc, nil)
require.NoError(t, coord.ProposeReconfiguration(100, nil, 0, nil))
vote := &ReconfigurationVote{Voter: 1, Approve: true}
require.NoError(t, coord.AddVote(vote))
err := coord.AddVote(vote)
assert.Error(t, err)
assert.Contains(t, err.Error(), "duplicate vote")
}
func TestReconfigurationCoordinator_CheckQuorum(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4} // f=1, quorum=3
r := NewReconfigurer(0, validators, nil)
hashFunc := func(b []byte) testReconfigHash { var h testReconfigHash; copy(h[:], b); return h }
coord := NewReconfigurationCoordinator[testReconfigHash](r, hashFunc, nil)
// No proposal
assert.False(t, coord.CheckQuorum())
require.NoError(t, coord.ProposeReconfiguration(100, nil, 0, nil))
// Not enough votes
_ = coord.AddVote(&ReconfigurationVote{Voter: 0, Approve: true})
_ = coord.AddVote(&ReconfigurationVote{Voter: 1, Approve: true})
assert.False(t, coord.CheckQuorum())
// Quorum reached
_ = coord.AddVote(&ReconfigurationVote{Voter: 2, Approve: true})
assert.True(t, coord.CheckQuorum())
}
func TestReconfigurationCoordinator_IsRejected(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4} // f=1
r := NewReconfigurer(0, validators, nil)
hashFunc := func(b []byte) testReconfigHash { var h testReconfigHash; copy(h[:], b); return h }
coord := NewReconfigurationCoordinator[testReconfigHash](r, hashFunc, nil)
// No proposal
assert.False(t, coord.IsRejected())
require.NoError(t, coord.ProposeReconfiguration(100, nil, 0, nil))
// Not enough rejections
_ = coord.AddVote(&ReconfigurationVote{Voter: 0, Approve: false})
assert.False(t, coord.IsRejected())
// f+1 = 2 rejections makes quorum impossible
_ = coord.AddVote(&ReconfigurationVote{Voter: 1, Approve: false})
assert.True(t, coord.IsRejected())
}
func TestReconfigurationCoordinator_Clear(t *testing.T) {
validators := mockValidatorSetForReconfig{count: 4}
r := NewReconfigurer(0, validators, nil)
hashFunc := func(b []byte) testReconfigHash { var h testReconfigHash; copy(h[:], b); return h }
coord := NewReconfigurationCoordinator[testReconfigHash](r, hashFunc, nil)
require.NoError(t, coord.ProposeReconfiguration(100, nil, 0, nil))
_ = coord.AddVote(&ReconfigurationVote{Voter: 0, Approve: true})
coord.Clear()
assert.Nil(t, coord.CurrentProposal())
approvals, rejections, total := coord.VoteStats()
assert.Equal(t, 0, approvals)
assert.Equal(t, 0, rejections)
assert.Equal(t, 0, total)
}
func TestEpochChange_Fields(t *testing.T) {
now := time.Now()
change := EpochChange{
FromEpoch: 1,
ToEpoch: 2,
EffectiveRound: 100,
NewValidators: mockValidatorSetForReconfig{count: 5},
ProposedAt: now,
CommittedAt: now.Add(time.Second),
}
assert.Equal(t, uint64(1), change.FromEpoch)
assert.Equal(t, uint64(2), change.ToEpoch)
assert.Equal(t, uint64(100), change.EffectiveRound)
assert.Equal(t, 5, change.NewValidators.Count())
}
func TestReconfigurationProposal_Fields(t *testing.T) {
now := time.Now()
proposal := ReconfigurationProposal{
Epoch: 5,
EffectiveRound: 500,
Changes: []ValidatorSetChange{
{Type: ValidatorSetChangeAdd, ValidatorIndex: 10},
},
Proposer: 0,
Signature: []byte("sig"),
ProposedAt: now,
}
assert.Equal(t, uint64(5), proposal.Epoch)
assert.Equal(t, uint64(500), proposal.EffectiveRound)
assert.Len(t, proposal.Changes, 1)
assert.Equal(t, uint16(0), proposal.Proposer)
}
func TestValidatorSetChange_Fields(t *testing.T) {
change := ValidatorSetChange{
Type: ValidatorSetChangeUpdate,
ValidatorIndex: 3,
PublicKey: nil,
}
assert.Equal(t, ValidatorSetChangeUpdate, change.Type)
assert.Equal(t, uint16(3), change.ValidatorIndex)
}