-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathreplication_request_test.go
219 lines (186 loc) · 6.28 KB
/
replication_request_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
package simplex_test
import (
"bytes"
"simplex"
"testing"
"github.com/stretchr/testify/require"
)
// TestReplicationRequestIndexedBlocks tests replication requests for indexed blocks.
func TestReplicationeRequestIndexedBlocks(t *testing.T) {
bb := &testBlockBuilder{out: make(chan *testBlock, 1)}
nodes := []simplex.NodeID{{1}, {2}, {3}, {4}}
comm := NewListenerComm(nodes)
conf := defaultTestNodeEpochConfig(t, nodes[0], comm, bb)
conf.ReplicationEnabled = true
numBlocks := uint64(10)
seqs := createBlocks(t, nodes, bb, numBlocks)
for _, data := range seqs {
conf.Storage.Index(data.VerifiedBlock, data.FCert)
}
e, err := simplex.NewEpoch(conf)
require.NoError(t, err)
require.NoError(t, e.Start())
sequences := []uint64{0, 1, 2, 3}
req := &simplex.Message{
ReplicationRequest: &simplex.ReplicationRequest{
Seqs: sequences,
LatestRound: numBlocks,
},
}
err = e.HandleMessage(req, nodes[1])
require.NoError(t, err)
msg := <-comm.in
resp := msg.VerifiedReplicationResponse
require.Nil(t, resp.LatestRound)
require.Equal(t, len(sequences), len(resp.Data))
for i, data := range resp.Data {
require.Equal(t, seqs[i].FCert, *data.FCert)
require.Equal(t, seqs[i].VerifiedBlock, data.VerifiedBlock)
}
// request out of scope
req = &simplex.Message{
ReplicationRequest: &simplex.ReplicationRequest{
Seqs: []uint64{11, 12, 13},
},
}
err = e.HandleMessage(req, nodes[1])
require.NoError(t, err)
msg = <-comm.in
resp = msg.VerifiedReplicationResponse
require.Zero(t, len(resp.Data))
}
// TestReplicationRequestNotarizations tests replication requests for notarized blocks.
func TestReplicationRequestNotarizations(t *testing.T) {
// generate 5 blocks & notarizations
bb := &testBlockBuilder{out: make(chan *testBlock, 1)}
nodes := []simplex.NodeID{{1}, {2}, {3}, {4}}
comm := NewListenerComm(nodes)
conf := defaultTestNodeEpochConfig(t, nodes[0], comm, bb)
conf.ReplicationEnabled = true
e, err := simplex.NewEpoch(conf)
require.NoError(t, err)
require.NoError(t, e.Start())
numBlocks := uint64(5)
rounds := make(map[uint64]simplex.VerifiedQuorumRound)
for i := uint64(0); i < numBlocks; i++ {
block, notarization := advanceRoundFromNotarization(t, e, bb)
rounds[i] = simplex.VerifiedQuorumRound{
VerifiedBlock: block,
Notarization: notarization,
}
}
require.Equal(t, uint64(numBlocks), e.Metadata().Round)
seqs := make([]uint64, 0, len(rounds))
for k := range rounds {
seqs = append(seqs, k)
}
req := &simplex.Message{
ReplicationRequest: &simplex.ReplicationRequest{
Seqs: seqs,
LatestRound: 0,
},
}
err = e.HandleMessage(req, nodes[1])
require.NoError(t, err)
msg := <-comm.in
resp := msg.VerifiedReplicationResponse
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, *resp.LatestRound, rounds[numBlocks-1])
for _, round := range resp.Data {
require.Nil(t, round.EmptyNotarization)
notarizedBlock, ok := rounds[round.VerifiedBlock.BlockHeader().Round]
require.True(t, ok)
require.Equal(t, notarizedBlock.VerifiedBlock, round.VerifiedBlock)
require.Equal(t, notarizedBlock.Notarization, round.Notarization)
}
}
// TestReplicationRequestMixed ensures the replication response also includes empty notarizations
func TestReplicationRequestMixed(t *testing.T) {
// generate 5 blocks & notarizations
bb := &testBlockBuilder{out: make(chan *testBlock, 1)}
nodes := []simplex.NodeID{{1}, {2}, {3}, {4}}
comm := NewListenerComm(nodes)
conf := defaultTestNodeEpochConfig(t, nodes[0], comm, bb)
conf.ReplicationEnabled = true
e, err := simplex.NewEpoch(conf)
require.NoError(t, err)
require.NoError(t, e.Start())
numBlocks := uint64(8)
rounds := make(map[uint64]simplex.VerifiedQuorumRound)
// only produce a notarization for blocks we are the leader, otherwise produce an empty notarization
for i := range numBlocks {
leaderForRound := bytes.Equal(simplex.LeaderForRound(nodes, uint64(i)), e.ID)
emptyBlock := !leaderForRound
if emptyBlock {
emptyNotarization := newEmptyNotarization(nodes, uint64(i), uint64(i))
e.HandleMessage(&simplex.Message{
EmptyNotarization: emptyNotarization,
}, nodes[1])
e.WAL.(*testWAL).assertNotarization(uint64(i))
rounds[i] = simplex.VerifiedQuorumRound{
EmptyNotarization: emptyNotarization,
}
continue
}
block, notarization := advanceRoundFromNotarization(t, e, bb)
rounds[i] = simplex.VerifiedQuorumRound{
VerifiedBlock: block,
Notarization: notarization,
}
}
require.Equal(t, uint64(numBlocks), e.Metadata().Round)
seqs := make([]uint64, 0, len(rounds))
for k := range rounds {
seqs = append(seqs, k)
}
req := &simplex.Message{
ReplicationRequest: &simplex.ReplicationRequest{
Seqs: seqs,
LatestRound: 0,
},
}
err = e.HandleMessage(req, nodes[1])
require.NoError(t, err)
msg := <-comm.in
resp := msg.VerifiedReplicationResponse
require.Equal(t, *resp.LatestRound, rounds[numBlocks-1])
for _, round := range resp.Data {
notarizedBlock, ok := rounds[round.GetRound()]
require.True(t, ok)
require.Equal(t, notarizedBlock.VerifiedBlock, round.VerifiedBlock)
require.Equal(t, notarizedBlock.Notarization, round.Notarization)
require.Equal(t, notarizedBlock.EmptyNotarization, round.EmptyNotarization)
}
}
func TestNilReplicationResponse(t *testing.T) {
bb := newTestControlledBlockBuilder(t)
nodes := []simplex.NodeID{{1}, {2}, {3}, {4}}
net := newInMemNetwork(t, nodes)
normalNode0 := newSimplexNode(t, nodes[0], net, bb, nil)
normalNode0.start()
err := normalNode0.HandleMessage(&simplex.Message{
ReplicationResponse: &simplex.ReplicationResponse{
Data: []simplex.QuorumRound{{}},
},
}, nodes[1])
require.NoError(t, err)
}
// TestMalformedReplicationResponse tests that a malformed replication response is handled correctly.
// This replication response is malformeds since it must also include a notarization or
// finalization certificate.
func TestMalformedReplicationResponse(t *testing.T) {
bb := newTestControlledBlockBuilder(t)
nodes := []simplex.NodeID{{1}, {2}, {3}, {4}}
net := newInMemNetwork(t, nodes)
normalNode0 := newSimplexNode(t, nodes[0], net, bb, nil)
normalNode0.start()
err := normalNode0.HandleMessage(&simplex.Message{
ReplicationResponse: &simplex.ReplicationResponse{
Data: []simplex.QuorumRound{{
Block: &testBlock{},
}},
},
}, nodes[1])
require.NoError(t, err)
}