-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathreplication_timeout_test.go
More file actions
677 lines (560 loc) · 23.2 KB
/
replication_timeout_test.go
File metadata and controls
677 lines (560 loc) · 23.2 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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package simplex_test
import (
"bytes"
"context"
"errors"
"reflect"
"sync"
"testing"
"time"
"github.com/ava-labs/simplex"
"github.com/ava-labs/simplex/testutil"
"github.com/stretchr/testify/require"
)
func rejectReplicationRequests(msg *simplex.Message, _, _ simplex.NodeID) bool {
return msg.ReplicationRequest == nil && msg.ReplicationResponse == nil && msg.VerifiedReplicationResponse == nil
}
// A node attempts to request blocks to replicate, but fails to receive them
func TestReplicationRequestTimeout(t *testing.T) {
nodes := []simplex.NodeID{{1}, {2}, {3}, []byte("lagging")}
numInitialSeqs := uint64(8)
// node begins replication
net := testutil.NewControlledNetwork(t, nodes)
storageData := createBlocks(t, nodes, numInitialSeqs)
newNodeConfig := func(from simplex.NodeID) *testutil.TestNodeConfig {
comm := testutil.NewTestComm(from, net.BasicInMemoryNetwork, rejectReplicationRequests)
return &testutil.TestNodeConfig{
InitialStorage: storageData,
Comm: comm,
ReplicationEnabled: true,
}
}
testutil.NewControlledSimplexNode(t, nodes[0], net, newNodeConfig(nodes[0]))
testutil.NewControlledSimplexNode(t, nodes[1], net, newNodeConfig(nodes[1]))
testutil.NewControlledSimplexNode(t, nodes[2], net, newNodeConfig(nodes[2]))
laggingNode := testutil.NewControlledSimplexNode(t, nodes[3], net, &testutil.TestNodeConfig{
ReplicationEnabled: true,
})
net.StartInstances()
defer net.StopInstances()
net.TriggerLeaderBlockBuilder(0)
// typically the lagging node would catch up here, but since we block
// replication requests, the lagging node will be forced to resend requests after a timeout
for i := uint64(0); i <= numInitialSeqs; i++ {
for _, n := range net.Instances {
if n.E.ID.Equals(laggingNode.E.ID) {
continue
}
n.Storage.WaitForBlockCommit(i)
}
}
// assert the lagging node has not received any replication requests
require.Equal(t, uint64(0), laggingNode.Storage.NumBlocks())
// allow the replication state to cancel the request before setting filter
time.Sleep(100 * time.Millisecond)
// after the timeout, the nodes should respond and the lagging node will replicate
net.SetAllNodesMessageFilter(testutil.AllowAllMessages)
laggingNode.E.AdvanceTime(laggingNode.E.StartTime.Add(simplex.DefaultReplicationRequestTimeout / 2))
require.Equal(t, uint64(0), laggingNode.Storage.NumBlocks())
laggingNode.E.AdvanceTime(laggingNode.E.StartTime.Add(simplex.DefaultReplicationRequestTimeout * 2))
laggingNode.Storage.WaitForBlockCommit(uint64(numInitialSeqs))
}
type testTimeoutMessageFilter struct {
t *testing.T
replicationResponses chan struct{}
}
func (m *testTimeoutMessageFilter) failOnReplicationRequest(msg *simplex.Message, _, _ simplex.NodeID) bool {
require.Nil(m.t, msg.ReplicationRequest)
return true
}
// receiveReplicationRequest is used to filter out sending replication responses, and notify a channel
// when a replication request is received.
func (m *testTimeoutMessageFilter) receivedReplicationRequest(msg *simplex.Message, _, _ simplex.NodeID) bool {
if msg.VerifiedReplicationResponse != nil || msg.ReplicationResponse != nil {
m.replicationResponses <- struct{}{}
return false
}
return true
}
func TestReplicationRequestTimeoutCancels(t *testing.T) {
nodes := []simplex.NodeID{{1}, {2}, {3}, []byte("lagging")}
startSeq := uint64(8)
net := testutil.NewControlledNetwork(t, nodes)
// initiate a network with 4 nodes. one node is behind by startSeq blocks
storageData := createBlocks(t, nodes, startSeq)
testEpochConfig := &testutil.TestNodeConfig{
InitialStorage: storageData,
ReplicationEnabled: true,
}
testutil.NewControlledSimplexNode(t, nodes[0], net, testEpochConfig)
testutil.NewControlledSimplexNode(t, nodes[1], net, testEpochConfig)
testutil.NewControlledSimplexNode(t, nodes[2], net, testEpochConfig)
laggingNode := testutil.NewControlledSimplexNode(t, nodes[3], net, &testutil.TestNodeConfig{
ReplicationEnabled: true,
})
net.StartInstances()
defer net.StopInstances()
net.TriggerLeaderBlockBuilder(startSeq)
// all blocks except the lagging node start at round 8, seq 8.
// lagging node starts at round 0, seq 0.
// this asserts that the lagging node catches up to the latest round
for _, n := range net.Instances {
n.Storage.WaitForBlockCommit(startSeq)
}
// ensure lagging node doesn't resend requests
mf := &testTimeoutMessageFilter{
t: t,
}
// allow the replication state to cancel the request before setting filter
time.Sleep(100 * time.Millisecond)
laggingNode.E.Comm.(*testutil.TestComm).SetFilter(mf.failOnReplicationRequest)
laggingNode.E.AdvanceTime(laggingNode.E.StartTime.Add(simplex.DefaultReplicationRequestTimeout * 2))
// ensure enough time passes after advanceTime is called
net.TriggerLeaderBlockBuilder(startSeq + 1)
for _, n := range net.Instances {
n.Storage.WaitForBlockCommit(startSeq + 1)
}
}
// A node attempts to request blocks to replicate, but fails to
// receive them multiple times
func TestReplicationRequestTimeoutMultiple(t *testing.T) {
nodes := []simplex.NodeID{{1}, {2}, {3}, []byte("lagging")}
startSeq := uint64(8)
// node begins replication
net := testutil.NewControlledNetwork(t, nodes)
storageData := createBlocks(t, nodes, startSeq)
newNodeConfig := func(from simplex.NodeID) *testutil.TestNodeConfig {
comm := testutil.NewTestComm(from, net.BasicInMemoryNetwork, rejectReplicationRequests)
return &testutil.TestNodeConfig{
InitialStorage: storageData,
Comm: comm,
ReplicationEnabled: true,
}
}
mf := &testTimeoutMessageFilter{
t: t,
replicationResponses: make(chan struct{}, 1),
}
testutil.NewControlledSimplexNode(t, nodes[0], net, newNodeConfig(nodes[0]))
normalNode2 := testutil.NewControlledSimplexNode(t, nodes[1], net, newNodeConfig(nodes[1]))
normalNode2.E.Comm.(*testutil.TestComm).SetFilter(mf.receivedReplicationRequest)
testutil.NewControlledSimplexNode(t, nodes[2], net, newNodeConfig(nodes[2]))
laggingNode := testutil.NewControlledSimplexNode(t, nodes[3], net, &testutil.TestNodeConfig{
ReplicationEnabled: true,
})
net.StartInstances()
defer net.StopInstances()
net.TriggerLeaderBlockBuilder(0)
// typically the lagging node would catch up here, but since we block
// replication requests, the lagging node will be forced to resend requests after a timeout
for i := 0; i <= int(startSeq); i++ {
for _, n := range net.Instances {
if n.E.ID.Equals(laggingNode.E.ID) {
continue
}
n.Storage.WaitForBlockCommit(uint64(startSeq))
}
}
// this is done from normalNode2 since the lagging node will request
// seqs [0-startSeq/3] after the timeout
<-mf.replicationResponses
// assert the lagging node has not received any replication responses
require.Equal(t, uint64(0), laggingNode.Storage.NumBlocks())
normalNode2.E.Comm.(*testutil.TestComm).SetFilter(testutil.AllowAllMessages)
// after the timeout, only normalNode2 should respond
laggingNode.E.AdvanceTime(laggingNode.E.StartTime.Add(simplex.DefaultReplicationRequestTimeout / 2))
require.Equal(t, uint64(0), laggingNode.Storage.NumBlocks())
laggingNode.E.AdvanceTime(laggingNode.E.StartTime.Add(simplex.DefaultReplicationRequestTimeout))
laggingNode.Storage.WaitForBlockCommit(startSeq / 3)
net.SetAllNodesMessageFilter(testutil.AllowAllMessages)
// timeout again, now all nodes will respond
laggingNode.E.AdvanceTime(laggingNode.E.StartTime.Add(simplex.DefaultReplicationRequestTimeout * 2))
laggingNode.Storage.WaitForBlockCommit(startSeq)
}
// modifies the replication response to only send every even quorum round
func incompleteReplicationResponseFilter(msg *simplex.Message, _, _ simplex.NodeID) bool {
if msg.VerifiedReplicationResponse != nil || msg.ReplicationResponse != nil {
newLen := len(msg.VerifiedReplicationResponse.Data) / 2
newData := make([]simplex.VerifiedQuorumRound, 0, newLen)
for _, qr := range msg.VerifiedReplicationResponse.Data {
if qr.GetRound()%2 == 0 {
newData = append(newData, qr)
}
}
msg.VerifiedReplicationResponse.Data = newData
}
return true
}
// A node attempts to request blocks to replicate, but receives incomplete
// responses from nodes.
func TestReplicationRequestIncompleteResponses(t *testing.T) {
nodes := []simplex.NodeID{{1}, {2}, {3}, []byte("lagging")}
startSeq := uint64(8)
// node begins replication
net := testutil.NewControlledNetwork(t, nodes)
storageData := createBlocks(t, nodes, startSeq)
newNodeConfig := func(from simplex.NodeID) *testutil.TestNodeConfig {
comm := testutil.NewTestComm(from, net.BasicInMemoryNetwork, rejectReplicationRequests)
return &testutil.TestNodeConfig{
InitialStorage: storageData,
Comm: comm,
ReplicationEnabled: true,
}
}
mf := &testTimeoutMessageFilter{
t: t,
replicationResponses: make(chan struct{}, 1),
}
testutil.NewControlledSimplexNode(t, nodes[0], net, newNodeConfig(nodes[0]))
normalNode2 := testutil.NewControlledSimplexNode(t, nodes[1], net, newNodeConfig(nodes[1]))
normalNode2.E.Comm.(*testutil.TestComm).SetFilter(mf.receivedReplicationRequest)
testutil.NewControlledSimplexNode(t, nodes[2], net, newNodeConfig(nodes[2]))
recordedMessages := make(chan *simplex.Message, 1000)
comm := testutil.NewTestComm(nodes[3], net.BasicInMemoryNetwork, testutil.AllowAllMessages)
laggingNode := testutil.NewControlledSimplexNode(t, nodes[3], net, &testutil.TestNodeConfig{
ReplicationEnabled: true,
Comm: &recordingComm{Communication: comm, SentMessages: recordedMessages},
})
for _, node := range net.Instances[:3] {
node.Silence()
}
net.StartInstances()
defer net.StopInstances()
net.TriggerLeaderBlockBuilder(startSeq)
// typically the lagging node would catch up here, but since we block
// replication requests, the lagging node will be forced to resend requests after a timeout
for i := 0; i <= int(startSeq); i++ {
for _, n := range net.Instances {
if n.E.ID.Equals(laggingNode.E.ID) {
continue
}
n.Storage.WaitForBlockCommit(uint64(startSeq))
}
}
// this is done from normalNode2 since the lagging node will request
// seqs [0-startSeq/3] after the timeout
<-mf.replicationResponses
// assert the lagging node has not received any replication responses
require.Equal(t, uint64(0), laggingNode.Storage.NumBlocks())
net.SetAllNodesMessageFilter(incompleteReplicationResponseFilter)
// after the timeout, only normalNode2 should respond(but with incomplete data)
laggingNode.E.AdvanceTime(laggingNode.E.StartTime.Add(simplex.DefaultReplicationRequestTimeout / 2))
require.Equal(t, uint64(0), laggingNode.Storage.NumBlocks())
laggingNode.E.AdvanceTime(laggingNode.E.StartTime.Add(simplex.DefaultReplicationRequestTimeout))
laggingNode.Storage.WaitForBlockCommit(0)
require.Eventually(t, func() bool {
msg, ok := <-recordedMessages
if !ok {
return false
}
if msg.ReplicationRequest == nil {
return false
}
return reflect.DeepEqual(msg.ReplicationRequest.Seqs, []uint64{3, 4, 5})
}, 30*time.Second, 10*time.Millisecond)
net.SetAllNodesMessageFilter(testutil.AllowAllMessages)
// timeout again, now all nodes will respond
laggingNode.E.AdvanceTime(laggingNode.E.StartTime.Add(simplex.DefaultReplicationRequestTimeout * 2))
laggingNode.Storage.WaitForBlockCommit(startSeq)
}
type collectNotarizationComm struct {
lock *sync.Mutex
notarizations map[uint64]*simplex.Notarization
testutil.TestNetworkCommunication
replicationResponses chan struct{}
}
func newCollectNotarizationComm(nodeID simplex.NodeID, net *testutil.ControlledInMemoryNetwork, notarizations map[uint64]*simplex.Notarization, lock *sync.Mutex) *collectNotarizationComm {
return &collectNotarizationComm{
notarizations: notarizations,
TestNetworkCommunication: testutil.NewTestComm(nodeID, net.BasicInMemoryNetwork, testutil.AllowAllMessages),
replicationResponses: make(chan struct{}, 3),
lock: lock,
}
}
func (c *collectNotarizationComm) Send(msg *simplex.Message, to simplex.NodeID) {
if msg.Notarization != nil {
c.lock.Lock()
c.notarizations[msg.Notarization.Vote.Round] = msg.Notarization
c.lock.Unlock()
}
c.TestNetworkCommunication.Send(msg, to)
}
func (c *collectNotarizationComm) Broadcast(msg *simplex.Message) {
if msg.Notarization != nil {
c.lock.Lock()
c.notarizations[msg.Notarization.Vote.Round] = msg.Notarization
c.lock.Unlock()
}
c.TestNetworkCommunication.Broadcast(msg)
}
func (c *collectNotarizationComm) removeFinalizationsFromReplicationResponses(msg *simplex.Message, from, to simplex.NodeID) bool {
c.lock.Lock()
defer c.lock.Unlock()
if msg.VerifiedReplicationResponse != nil || msg.ReplicationResponse != nil {
newData := make([]simplex.VerifiedQuorumRound, 0, len(msg.VerifiedReplicationResponse.Data))
for i := 0; i < len(msg.VerifiedReplicationResponse.Data); i++ {
qr := msg.VerifiedReplicationResponse.Data[i]
if qr.Finalization != nil && c.notarizations[qr.GetRound()] != nil {
qr.Finalization = nil
qr.Notarization = c.notarizations[qr.GetRound()]
}
newData = append(newData, qr)
}
msg.VerifiedReplicationResponse.Data = newData
select {
case c.replicationResponses <- struct{}{}:
default:
}
}
if msg.Finalization != nil && msg.Finalization.Finalization.Round == 0 {
// we drop a finalization here because the lagging node could timeout on round 0
// therefore it would send an empty vote message to a node.
// When nodes receive empty votes, they send back a finalization/notarization to a lagging node.
// Since we are testing replication, lets block this finalization to ensure the lagging node
// has to rely on replication to get notarizations/finalizations.
return false
}
return true
}
// TestReplicationRequestWithoutFinalization tests that a replication request is not marked as completed
// if we are expecting a finalization but it is not present in the response.
func TestReplicationRequestWithoutFinalization(t *testing.T) {
nodes := []simplex.NodeID{{1}, {2}, {3}, []byte("lagging")}
endDisconnect := uint64(10)
net := testutil.NewControlledNetwork(t, nodes)
notarizations := make(map[uint64]*simplex.Notarization)
mapLock := &sync.Mutex{}
testConfig := func(nodeID simplex.NodeID) *testutil.TestNodeConfig {
return &testutil.TestNodeConfig{
ReplicationEnabled: true,
Comm: newCollectNotarizationComm(nodeID, net, notarizations, mapLock),
}
}
notarizationComm := newCollectNotarizationComm(nodes[0], net, notarizations, mapLock)
testutil.NewControlledSimplexNode(t, nodes[0], net, &testutil.TestNodeConfig{
ReplicationEnabled: true,
Comm: notarizationComm,
})
testutil.NewControlledSimplexNode(t, nodes[1], net, testConfig(nodes[1]))
testutil.NewControlledSimplexNode(t, nodes[2], net, testConfig(nodes[2]))
laggingNode := testutil.NewControlledSimplexNode(t, nodes[3], net, testConfig(nodes[3]))
epochTimes := make([]time.Time, 0, 4)
for _, n := range net.Instances {
epochTimes = append(epochTimes, n.E.StartTime)
}
// lagging node disconnects
net.Disconnect(nodes[3])
net.StartInstances()
defer net.StopInstances()
missedSeqs := uint64(0)
// normal nodes continue to make progress
for i := uint64(0); i < endDisconnect; i++ {
emptyRound := bytes.Equal(simplex.LeaderForRound(nodes, i), nodes[3])
if emptyRound {
net.AdvanceWithoutLeader(i, laggingNode.E.ID)
missedSeqs++
} else {
net.TriggerLeaderBlockBuilder(i)
for _, n := range net.Instances[:3] {
n.Storage.WaitForBlockCommit(i - missedSeqs)
}
}
}
// all nodes except for lagging node have progressed and committed [endDisconnect - missedSeqs] blocks
for _, n := range net.Instances[:3] {
require.Equal(t, endDisconnect-missedSeqs, n.Storage.NumBlocks())
}
require.Equal(t, uint64(0), laggingNode.Storage.NumBlocks())
require.Equal(t, uint64(0), laggingNode.E.Metadata().Round)
// lagging node reconnects
net.SetAllNodesMessageFilter(notarizationComm.removeFinalizationsFromReplicationResponses)
net.Connect(nodes[3])
net.TriggerLeaderBlockBuilder(endDisconnect)
for _, n := range net.Instances {
if n.E.ID.Equals(laggingNode.E.ID) {
continue
}
n.Storage.WaitForBlockCommit(endDisconnect - missedSeqs)
<-notarizationComm.replicationResponses
}
// wait until the lagging nodes sends replication requests
// due to the removeFinalizationsFromReplicationResponses message filter
// the lagging node should not have processed any replication requests
require.Equal(t, uint64(0), laggingNode.Storage.NumBlocks())
// we should still have these replication requests in the timeout handler
laggingNode.E.AdvanceTime(laggingNode.E.StartTime.Add(simplex.DefaultReplicationRequestTimeout * 2))
for range net.Instances[:3] {
// the lagging node should have sent replication requests
// and the normal nodes should have responded
<-notarizationComm.replicationResponses
}
require.Equal(t, uint64(0), laggingNode.Storage.NumBlocks())
// We should still have these replication requests in the timeout handler
// but now we allow the lagging node to process them
net.SetAllNodesMessageFilter(testutil.AllowAllMessages)
timeout := time.After(1 * time.Minute)
// we may be in the process of creating timeout requests
for {
laggingNode.E.AdvanceTime(laggingNode.E.StartTime.Add(simplex.DefaultReplicationRequestTimeout * 4))
if laggingNode.Storage.NumBlocks() > endDisconnect-missedSeqs {
break
}
select {
case <-time.After(100 * time.Millisecond):
continue
case <-timeout:
t.Fatalf("Lagging node did not catch up after timeout")
}
}
}
// TestReplicationMalformedQuorumRound tests that a node re-sends a replication request when it receives a malformed quorum round message.
func TestReplicationMalformedQuorumRound(t *testing.T) {
nodes := []simplex.NodeID{{1}, {2}, {3}, []byte("lagging")}
startSeq := uint64(8)
// node begins replication
net := testutil.NewControlledNetwork(t, nodes)
storageData := createBlocks(t, nodes, startSeq)
newNodeConfig := func(from simplex.NodeID) *testutil.TestNodeConfig {
comm := testutil.NewTestComm(from, net.BasicInMemoryNetwork, rejectReplicationRequests)
return &testutil.TestNodeConfig{
InitialStorage: storageData,
Comm: comm,
ReplicationEnabled: true,
}
}
mf := &testTimeoutMessageFilter{
t: t,
replicationResponses: make(chan struct{}, 1),
}
testutil.NewControlledSimplexNode(t, nodes[0], net, newNodeConfig(nodes[0]))
normalNode2 := testutil.NewControlledSimplexNode(t, nodes[1], net, newNodeConfig(nodes[1]))
normalNode2.E.Comm.(*testutil.TestComm).SetFilter(mf.receivedReplicationRequest)
testutil.NewControlledSimplexNode(t, nodes[2], net, newNodeConfig(nodes[2]))
recordedMessages := make(chan *simplex.Message, 1000)
comm := testutil.NewTestComm(nodes[3], net.BasicInMemoryNetwork, testutil.AllowAllMessages)
laggingNode := testutil.NewControlledSimplexNode(t, nodes[3], net, &testutil.TestNodeConfig{
ReplicationEnabled: true,
Comm: &recordingComm{Communication: comm, SentMessages: recordedMessages},
})
net.StartInstances()
defer net.StopInstances()
net.TriggerLeaderBlockBuilder(0)
// typically the lagging node would catch up here, but since we block
// replication requests, the lagging node will be forced to resend requests after a timeout
for i := 0; i <= int(startSeq); i++ {
for _, n := range net.Instances {
if n.E.ID.Equals(laggingNode.E.ID) {
continue
}
n.Storage.WaitForBlockCommit(uint64(startSeq))
}
}
<-mf.replicationResponses
// assert the lagging node has not received any replication responses
require.Equal(t, uint64(0), laggingNode.Storage.NumBlocks())
net.SetAllNodesMessageFilter(
func(msg *simplex.Message, _, _ simplex.NodeID) bool {
if msg.VerifiedReplicationResponse != nil || msg.ReplicationResponse != nil {
newData := make([]simplex.VerifiedQuorumRound, 0, len(msg.VerifiedReplicationResponse.Data))
for _, qr := range msg.VerifiedReplicationResponse.Data {
qr.Notarization = nil // remove notarization
qr.Finalization = nil // remove finalization
newData = append(newData, qr)
}
msg.VerifiedReplicationResponse.Data = newData
}
return true
},
)
// after the timeout, only normalNode2 should respond, but with malformed data
laggingNode.E.AdvanceTime(laggingNode.E.StartTime.Add(simplex.DefaultReplicationRequestTimeout / 2))
require.Equal(t, uint64(0), laggingNode.Storage.NumBlocks())
laggingNode.E.AdvanceTime(laggingNode.E.StartTime.Add(simplex.DefaultReplicationRequestTimeout))
require.Equal(t, uint64(0), laggingNode.Storage.NumBlocks())
require.Eventually(t, func() bool {
msg, ok := <-recordedMessages
if !ok {
return false
}
if msg.ReplicationRequest == nil {
return false
}
return reflect.DeepEqual(msg.ReplicationRequest.Seqs, []uint64{3, 4, 5})
}, 30*time.Second, 10*time.Millisecond)
net.SetAllNodesMessageFilter(testutil.AllowAllMessages)
// timeout again, now all nodes will respond
laggingNode.E.AdvanceTime(laggingNode.E.StartTime.Add(simplex.DefaultReplicationRequestTimeout * 2))
laggingNode.Storage.WaitForBlockCommit(startSeq)
}
func TestReplicationResendsFinalizedBlocksThatFailedVerification(t *testing.T) {
// send a block, then simultaneously send a finalization for the block
l := testutil.MakeLogger(t, 1)
bb := testutil.NewTestBlockBuilder()
nodes := []simplex.NodeID{{1}, {2}, {3}, {4}}
quorum := simplex.Quorum(len(nodes))
sentMessages := make(chan *simplex.Message, 100)
conf, _, storage := testutil.DefaultTestNodeEpochConfig(t, nodes[1], &recordingComm{
Communication: testutil.NewNoopComm(nodes),
SentMessages: sentMessages,
}, bb)
conf.ReplicationEnabled = true
e, err := simplex.NewEpoch(conf)
require.NoError(t, err)
require.NoError(t, e.Start())
md := e.Metadata()
_, ok := bb.BuildBlock(context.Background(), md, emptyBlacklist)
require.True(t, ok)
require.Equal(t, md.Round, md.Seq)
block := bb.GetBuiltBlock()
block.VerificationError = errors.New("block verification failed")
finalization, _ := testutil.NewFinalizationRecord(t, l, e.SignatureAggregator, block, nodes[0:quorum])
// send the finalization to start the replication process
e.HandleMessage(&simplex.Message{
Finalization: &finalization,
}, nodes[0])
// wait for the replication request to be sent
for {
msg := <-sentMessages
if msg.ReplicationRequest != nil {
break
}
}
replicationResponse := &simplex.ReplicationResponse{
Data: []simplex.QuorumRound{
{
Block: block,
Finalization: &finalization,
},
},
}
e.HandleMessage(&simplex.Message{
ReplicationResponse: replicationResponse,
}, nodes[0])
// wait for the replication request to be sent again
for {
msg := <-sentMessages
if msg.ReplicationRequest != nil {
break
}
}
block = testutil.NewTestBlock(md, emptyBlacklist)
block.Data = append(block.Data, 0)
block.ComputeDigest()
finalization, _ = testutil.NewFinalizationRecord(t, l, e.SignatureAggregator, block, nodes[0:quorum])
replicationResponse = &simplex.ReplicationResponse{
Data: []simplex.QuorumRound{
{
Block: block,
Finalization: &finalization,
},
},
}
e.HandleMessage(&simplex.Message{
ReplicationResponse: replicationResponse,
}, nodes[0])
storedBlock := storage.WaitForBlockCommit(0)
require.Equal(t, uint64(1), storage.NumBlocks())
require.Equal(t, block, storedBlock)
}