-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreplication_test.go
376 lines (320 loc) · 12.9 KB
/
replication_test.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
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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package simplex_test
import (
"bytes"
"context"
"fmt"
"simplex"
"simplex/testutil"
"simplex/wal"
"time"
"testing"
"github.com/stretchr/testify/require"
)
func TestHandleFinalizationCertificateRequest(t *testing.T) {
l := testutil.MakeLogger(t, 1)
bb := &testBlockBuilder{out: make(chan *testBlock, 1)}
storage := newInMemStorage()
nodes := []simplex.NodeID{{1}, {2}, {3}, {4}}
signatureAggregator := &testSignatureAggregator{}
wal := wal.NewMemWAL(t)
conf := simplex.EpochConfig{
Logger: l,
ID: nodes[0],
Signer: &testSigner{},
WAL: wal,
Verifier: &testVerifier{},
Storage: storage,
Comm: noopComm(nodes),
BlockBuilder: bb,
SignatureAggregator: signatureAggregator,
BlockDeserializer: &blockDeserializer{},
QCDeserializer: &testQCDeserializer{t: t},
ReplicationEnabled: true,
}
seqs := createBlocks(t, nodes, bb, 10)
for _, data := range seqs {
conf.Storage.Index(data.Block, data.FCert)
}
e, err := simplex.NewEpoch(conf)
require.NoError(t, err)
require.NoError(t, e.Start())
sequences := []uint64{0, 1, 2, 3}
req := &simplex.ReplicationRequest{FinalizationCertificateRequest: &simplex.FinalizationCertificateRequest{
Sequences: sequences,
}}
resp := e.HandleReplicationRequest(req, nodes[1])
require.NotNil(t, resp.FinalizationCertificateResponse)
require.Equal(t, len(sequences), len(resp.FinalizationCertificateResponse.Data))
for i, data := range resp.FinalizationCertificateResponse.Data {
require.Equal(t, seqs[i].FCert, data.FCert)
require.Equal(t, seqs[i].Block, data.Block)
}
// request out of scope
req = &simplex.ReplicationRequest{FinalizationCertificateRequest: &simplex.FinalizationCertificateRequest{
Sequences: []uint64{11, 12, 13},
}}
resp = e.HandleReplicationRequest(req, nodes[1])
require.Zero(t, len(resp.FinalizationCertificateResponse.Data))
}
// TestReplication tests the replication process of a node that
// is behind the rest of the network by less than maxRoundWindow.
func TestReplication(t *testing.T) {
bb := newTestControlledBlockBuilder(t)
nodes := []simplex.NodeID{{1}, {2}, {3}, []byte("lagging")}
net := newInMemNetwork(t, nodes)
startSeq := uint64(8)
// initiate a network with 4 nodes. one node is behind by 8 blocks
storageData := createBlocks(t, nodes, &bb.testBlockBuilder, startSeq)
normalNode1 := newSimplexNodeWithStorage(t, nodes[0], net, bb, storageData)
normalNode2 := newSimplexNodeWithStorage(t, nodes[1], net, bb, storageData)
normalNode3 := newSimplexNodeWithStorage(t, nodes[2], net, bb, storageData)
laggingNode := newSimplexNode(t, nodes[3], net, bb, true)
require.Equal(t, startSeq, normalNode1.storage.Height())
require.Equal(t, startSeq, normalNode2.storage.Height())
require.Equal(t, startSeq, normalNode3.storage.Height())
require.Equal(t, uint64(0), laggingNode.storage.Height())
net.startInstances()
bb.triggerNewBlock()
// 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 i := 0; i <= int(startSeq); i++ {
for _, n := range net.instances {
n.storage.waitForBlockCommit(uint64(startSeq))
}
}
}
// TestReplicationExceedsMaxRoundWindow tests the replication process of a node that
// is behind the rest of the network by more than maxRoundWindow.
func TestReplicationExceedsMaxRoundWindow(t *testing.T) {
bb := newTestControlledBlockBuilder(t)
nodes := []simplex.NodeID{{1}, {2}, {3}, []byte("lagging")}
net := newInMemNetwork(t, nodes)
startSeq := uint64(simplex.DefaultMaxRoundWindow * 3)
storageData := createBlocks(t, nodes, &bb.testBlockBuilder, startSeq)
normalNode1 := newSimplexNodeWithStorage(t, nodes[0], net, bb, storageData)
normalNode2 := newSimplexNodeWithStorage(t, nodes[1], net, bb, storageData)
normalNode3 := newSimplexNodeWithStorage(t, nodes[2], net, bb, storageData)
laggingNode := newSimplexNode(t, nodes[3], net, bb, true)
require.Equal(t, startSeq, normalNode1.storage.Height())
require.Equal(t, startSeq, normalNode2.storage.Height())
require.Equal(t, startSeq, normalNode3.storage.Height())
require.Equal(t, uint64(0), laggingNode.storage.Height())
net.startInstances()
bb.triggerNewBlock()
for i := 0; i <= int(startSeq); i++ {
for _, n := range net.instances {
n.storage.waitForBlockCommit(uint64(startSeq))
}
}
}
// TestReplicationStartsBeforeCurrentRound tests the replication process of a node that
// starts replicating in the middle of the current round.
func TestReplicationStartsBeforeCurrentRound(t *testing.T) {
bb := newTestControlledBlockBuilder(t)
nodes := []simplex.NodeID{{1}, {2}, {3}, []byte("lagging")}
quorum := simplex.Quorum(len(nodes))
net := newInMemNetwork(t, nodes)
startSeq := uint64(simplex.DefaultMaxRoundWindow + 3)
storageData := createBlocks(t, nodes, &bb.testBlockBuilder, startSeq)
normalNode1 := newSimplexNodeWithStorage(t, nodes[0], net, bb, storageData)
normalNode2 := newSimplexNodeWithStorage(t, nodes[1], net, bb, storageData)
normalNode3 := newSimplexNodeWithStorage(t, nodes[2], net, bb, storageData)
laggingNode := newSimplexNode(t, nodes[3], net, bb, true)
firstBlock := storageData[0].Block
record := simplex.BlockRecord(firstBlock.BlockHeader(), firstBlock.Bytes())
laggingNode.wal.Append(record)
firstNotarizationRecord, err := newNotarizationRecord(laggingNode.e.Logger, laggingNode.e.SignatureAggregator, firstBlock, nodes[0:quorum])
require.NoError(t, err)
laggingNode.wal.Append(firstNotarizationRecord)
secondBlock := storageData[1].Block
record = simplex.BlockRecord(secondBlock.BlockHeader(), secondBlock.Bytes())
laggingNode.wal.Append(record)
secondNotarizationRecord, err := newNotarizationRecord(laggingNode.e.Logger, laggingNode.e.SignatureAggregator, secondBlock, nodes[0:quorum])
require.NoError(t, err)
laggingNode.wal.Append(secondNotarizationRecord)
require.Equal(t, startSeq, normalNode1.storage.Height())
require.Equal(t, startSeq, normalNode2.storage.Height())
require.Equal(t, startSeq, normalNode3.storage.Height())
require.Equal(t, uint64(0), laggingNode.storage.Height())
net.startInstances()
laggingNodeMd := laggingNode.e.Metadata()
require.Equal(t, uint64(2), laggingNodeMd.Round)
bb.triggerNewBlock()
for i := 0; i <= int(startSeq); i++ {
for _, n := range net.instances {
n.storage.waitForBlockCommit(uint64(startSeq))
}
}
}
func TestReplicationFutureFinalizationCertificate(t *testing.T) {
// send a block, then simultaneously send a finalization certificate for the block
l := testutil.MakeLogger(t, 1)
bb := &testBlockBuilder{out: make(chan *testBlock, 1)}
storage := newInMemStorage()
nodes := []simplex.NodeID{{1}, {2}, {3}, {4}}
quorum := simplex.Quorum(len(nodes))
signatureAggregator := &testSignatureAggregator{}
conf := simplex.EpochConfig{
MaxProposalWait: simplex.DefaultMaxProposalWaitTime,
Logger: l,
ID: nodes[1],
Signer: &testSigner{},
WAL: wal.NewMemWAL(t),
Verifier: &testVerifier{},
Storage: storage,
Comm: noopComm(nodes),
BlockBuilder: bb,
SignatureAggregator: signatureAggregator,
}
e, err := simplex.NewEpoch(conf)
require.NoError(t, err)
require.NoError(t, e.Start())
md := e.Metadata()
_, ok := bb.BuildBlock(context.Background(), md)
require.True(t, ok)
require.Equal(t, md.Round, md.Seq)
block := <-bb.out
block.verificationDelay = make(chan struct{}) // add a delay to the block verification
vote, err := newTestVote(block, nodes[0])
require.NoError(t, err)
err = e.HandleMessage(&simplex.Message{
BlockMessage: &simplex.BlockMessage{
Vote: *vote,
Block: block,
},
}, nodes[0])
require.NoError(t, err)
fCert, _ := newFinalizationRecord(t, l, signatureAggregator, block, nodes[0:quorum])
// send fcert
err = e.HandleMessage(&simplex.Message{
FinalizationCertificate: &fCert,
}, nodes[0])
require.NoError(t, err)
block.verificationDelay <- struct{}{} // unblock the block verification
storedBlock := storage.waitForBlockCommit(0)
require.Equal(t, uint64(1), storage.Height())
require.Equal(t, block, storedBlock)
}
// TestReplicationAfterNodeDisconnects tests the replication process of a node that
// disconnects from the network and reconnects after the rest of the network has made progress.
//
// All nodes make progress for `startDisconnect` blocks. The lagging node disconnects
// and the rest of the nodes continue to make progress for another `endDisconnect - startDisconnect` blocks.
// The lagging node reconnects and the after the next `fCert` is sent, the lagging node catches up to the latest height.
func TestReplicationAfterNodeDisconnects(t *testing.T) {
nodes := []simplex.NodeID{{1}, {2}, {3}, []byte("lagging")}
for startDisconnect := uint64(0); startDisconnect <= 5; startDisconnect++ {
for endDisconnect := uint64(10); endDisconnect <= 20; endDisconnect++ {
// lagging node cannot be the leader after node disconnects
isLaggingNodeLeader := bytes.Equal(simplex.LeaderForRound(nodes, endDisconnect), nodes[3])
if isLaggingNodeLeader {
continue
}
testName := fmt.Sprintf("Disconnect_%d_to_%d", startDisconnect, endDisconnect)
t.Run(testName, func(t *testing.T) {
t.Parallel()
testReplicationAfterNodeDisconnects(t, nodes, startDisconnect, endDisconnect)
})
}
}
}
func testReplicationAfterNodeDisconnects(t *testing.T, nodes []simplex.NodeID, startDisconnect, endDisconnect uint64) {
bb := newTestControlledBlockBuilder(t)
net := newInMemNetwork(t, nodes)
normalNode1 := newSimplexNode(t, nodes[0], net, bb, true)
normalNode2 := newSimplexNode(t, nodes[1], net, bb, true)
normalNode3 := newSimplexNode(t, nodes[2], net, bb, true)
laggingNode := newSimplexNode(t, nodes[3], net, bb, true)
require.Equal(t, uint64(0), normalNode1.storage.Height())
require.Equal(t, uint64(0), normalNode2.storage.Height())
require.Equal(t, uint64(0), normalNode3.storage.Height())
require.Equal(t, uint64(0), laggingNode.storage.Height())
epochTimes := make([]time.Time, 0, 4)
for _, n := range net.instances {
epochTimes = append(epochTimes, n.e.StartTime)
}
net.startInstances()
for i := uint64(0); i < startDisconnect; i++ {
bb.triggerNewBlock()
for _, n := range net.instances {
n.storage.waitForBlockCommit(i)
}
}
// all nodes have commited `startDisconnect` blocks
for _, n := range net.instances {
require.Equal(t, startDisconnect, n.storage.Height())
}
// lagging node disconnects
net.Disconnect(nodes[3])
isLaggingNodeLeader := bytes.Equal(simplex.LeaderForRound(nodes, startDisconnect), nodes[3])
if isLaggingNodeLeader {
bb.triggerNewBlock()
}
missedSeqs := uint64(0)
// normal nodes continue to make progress
for i := startDisconnect; i < endDisconnect; i++ {
emptyRound := bytes.Equal(simplex.LeaderForRound(nodes, i), nodes[3])
if emptyRound {
advanceWithoutLeader(t, net, bb, epochTimes)
missedSeqs++
} else {
bb.triggerNewBlock()
for _, n := range net.instances[:3] {
n.storage.waitForBlockCommit(i - missedSeqs)
}
}
}
// all nodes excpet for lagging node have progressed and commited [endDisconnect - missedSeqs] blocks
for _, n := range net.instances[:3] {
require.Equal(t, endDisconnect-missedSeqs, n.storage.Height())
}
require.Equal(t, startDisconnect, laggingNode.storage.Height())
require.Equal(t, startDisconnect, laggingNode.e.Metadata().Round)
// lagging node reconnects
net.Connect(nodes[3])
bb.triggerNewBlock()
for _, n := range net.instances {
n.storage.waitForBlockCommit(endDisconnect - missedSeqs)
}
for _, n := range net.instances {
require.Equal(t, endDisconnect-missedSeqs, n.storage.Height()-1)
require.Equal(t, endDisconnect+1, n.e.Metadata().Round)
}
bb.triggerNewBlock() // the lagging node should build a block when triggered
for _, n := range net.instances {
n.storage.waitForBlockCommit(endDisconnect - missedSeqs + 1)
}
}
func advanceWithoutLeader(t *testing.T, net *inMemNetwork, bb *testControlledBlockBuilder, epochTimes []time.Time) {
for range net.instances {
bb.blockShouldBeBuilt <- struct{}{}
}
for i, n := range net.instances[:3] {
waitForBlockProposerTimeout(t, n.e, epochTimes[i])
}
}
func createBlocks(t *testing.T, nodes []simplex.NodeID, bb simplex.BlockBuilder, seqCount uint64) []simplex.FinalizedBlock {
logger := testutil.MakeLogger(t, int(0))
ctx := context.Background()
data := make([]simplex.FinalizedBlock, 0, seqCount)
var prev simplex.Digest
for i := uint64(0); i < seqCount; i++ {
protocolMetadata := simplex.ProtocolMetadata{
Seq: i,
Round: i,
Prev: prev,
}
block, ok := bb.BuildBlock(ctx, protocolMetadata)
require.True(t, ok)
prev = block.BlockHeader().Digest
fCert, _ := newFinalizationRecord(t, logger, &testSignatureAggregator{}, block, nodes)
data = append(data, simplex.FinalizedBlock{
Block: block,
FCert: fCert,
})
}
return data
}