-
Notifications
You must be signed in to change notification settings - Fork 930
/
rpc_beacon_blocks_by_range_test.go
372 lines (328 loc) · 11.6 KB
/
rpc_beacon_blocks_by_range_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
package sync
import (
"context"
"sync"
"testing"
"time"
"github.com/kevinms/leakybucket-go"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/protocol"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
db "github.com/prysmaticlabs/prysm/beacon-chain/db/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/flags"
p2ptest "github.com/prysmaticlabs/prysm/beacon-chain/p2p/testing"
"github.com/prysmaticlabs/prysm/beacon-chain/state/stateutil"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/testutil"
logTest "github.com/sirupsen/logrus/hooks/test"
)
func TestRPCBeaconBlocksByRange_RPCHandlerReturnsBlocks(t *testing.T) {
p1 := p2ptest.NewTestP2P(t)
p2 := p2ptest.NewTestP2P(t)
p1.Connect(p2)
if len(p1.Host.Network().Peers()) != 1 {
t.Error("Expected peers to be connected")
}
d := db.SetupDB(t)
req := &pb.BeaconBlocksByRangeRequest{
StartSlot: 100,
Step: 64,
Count: 16,
}
// Populate the database with blocks that would match the request.
for i := req.StartSlot; i < req.StartSlot+(req.Step*req.Count); i += req.Step {
if err := d.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: i}}); err != nil {
t.Fatal(err)
}
}
// Start service with 160 as allowed blocks capacity (and almost zero capacity recovery).
r := &Service{p2p: p1, db: d, blocksRateLimiter: leakybucket.NewCollector(0.000001, int64(req.Count*10), false)}
pcl := protocol.ID("/testing")
var wg sync.WaitGroup
wg.Add(1)
p2.Host.SetStreamHandler(pcl, func(stream network.Stream) {
defer wg.Done()
for i := req.StartSlot; i < req.StartSlot+req.Count*req.Step; i += req.Step {
expectSuccess(t, r, stream)
res := ðpb.SignedBeaconBlock{}
if err := r.p2p.Encoding().DecodeWithLength(stream, res); err != nil {
t.Error(err)
}
if (res.Block.Slot-req.StartSlot)%req.Step != 0 {
t.Errorf("Received unexpected block slot %d", res.Block.Slot)
}
}
})
stream1, err := p1.Host.NewStream(context.Background(), p2.Host.ID(), pcl)
if err != nil {
t.Fatal(err)
}
err = r.beaconBlocksByRangeRPCHandler(context.Background(), req, stream1)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
// Make sure that rate limiter doesn't limit capacity exceedingly.
remainingCapacity := r.blocksRateLimiter.Remaining(p2.PeerID().String())
expectedCapacity := int64(req.Count*10 - req.Count)
if remainingCapacity != expectedCapacity {
t.Fatalf("Unexpected rate limiting capacity, expected: %v, got: %v", expectedCapacity, remainingCapacity)
}
if testutil.WaitTimeout(&wg, 1*time.Second) {
t.Fatal("Did not receive stream within 1 sec")
}
}
func TestRPCBeaconBlocksByRange_RPCHandlerReturnsSortedBlocks(t *testing.T) {
p1 := p2ptest.NewTestP2P(t)
p2 := p2ptest.NewTestP2P(t)
p1.Connect(p2)
if len(p1.Host.Network().Peers()) != 1 {
t.Error("Expected peers to be connected")
}
d := db.SetupDB(t)
req := &pb.BeaconBlocksByRangeRequest{
StartSlot: 200,
Step: 21,
Count: 33,
}
endSlot := req.StartSlot + (req.Step * (req.Count - 1))
// Populate the database with blocks that would match the request.
for i := endSlot; i >= req.StartSlot; i -= req.Step {
if err := d.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: i}}); err != nil {
t.Fatal(err)
}
}
// Start service with 160 as allowed blocks capacity (and almost zero capacity recovery).
r := &Service{p2p: p1, db: d, blocksRateLimiter: leakybucket.NewCollector(0.000001, int64(req.Count*10), false)}
pcl := protocol.ID("/testing")
var wg sync.WaitGroup
wg.Add(1)
p2.Host.SetStreamHandler(pcl, func(stream network.Stream) {
defer wg.Done()
prevSlot := uint64(0)
for i := req.StartSlot; i < req.StartSlot+req.Count*req.Step; i += req.Step {
expectSuccess(t, r, stream)
res := ðpb.SignedBeaconBlock{}
if err := r.p2p.Encoding().DecodeWithLength(stream, res); err != nil {
t.Error(err)
}
if res.Block.Slot < prevSlot {
t.Errorf("Received block is unsorted with slot %d lower than previous slot %d", res.Block.Slot, prevSlot)
}
prevSlot = res.Block.Slot
}
})
stream1, err := p1.Host.NewStream(context.Background(), p2.Host.ID(), pcl)
if err != nil {
t.Fatal(err)
}
err = r.beaconBlocksByRangeRPCHandler(context.Background(), req, stream1)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if testutil.WaitTimeout(&wg, 1*time.Second) {
t.Fatal("Did not receive stream within 1 sec")
}
}
func TestRPCBeaconBlocksByRange_ReturnsGenesisBlock(t *testing.T) {
p1 := p2ptest.NewTestP2P(t)
p2 := p2ptest.NewTestP2P(t)
p1.Connect(p2)
if len(p1.Host.Network().Peers()) != 1 {
t.Error("Expected peers to be connected")
}
d := db.SetupDB(t)
req := &pb.BeaconBlocksByRangeRequest{
StartSlot: 0,
Step: 1,
Count: 4,
}
// Populate the database with blocks that would match the request.
for i := req.StartSlot; i < req.StartSlot+(req.Step*req.Count); i++ {
// Save genesis block
if i == 0 {
rt, err := stateutil.BlockRoot(ðpb.BeaconBlock{Slot: i})
if err != nil {
t.Fatal(err)
}
if err := d.SaveGenesisBlockRoot(context.Background(), rt); err != nil {
t.Fatal(err)
}
}
if err := d.SaveBlock(context.Background(), ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: i}}); err != nil {
t.Fatal(err)
}
}
r := &Service{p2p: p1, db: d, blocksRateLimiter: leakybucket.NewCollector(10000, 10000, false)}
pcl := protocol.ID("/testing")
var wg sync.WaitGroup
wg.Add(1)
p2.Host.SetStreamHandler(pcl, func(stream network.Stream) {
defer wg.Done()
// check for genesis block
expectSuccess(t, r, stream)
res := ðpb.SignedBeaconBlock{}
if err := r.p2p.Encoding().DecodeWithLength(stream, res); err != nil {
t.Error(err)
}
if res.Block.Slot != 0 {
t.Fatal("genesis block was not returned")
}
for i := req.StartSlot + req.Step; i < req.Count*req.Step; i += req.Step {
expectSuccess(t, r, stream)
res := ðpb.SignedBeaconBlock{}
if err := r.p2p.Encoding().DecodeWithLength(stream, res); err != nil {
t.Error(err)
}
}
})
stream1, err := p1.Host.NewStream(context.Background(), p2.Host.ID(), pcl)
if err != nil {
t.Fatal(err)
}
err = r.beaconBlocksByRangeRPCHandler(context.Background(), req, stream1)
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if testutil.WaitTimeout(&wg, 1*time.Second) {
t.Fatal("Did not receive stream within 1 sec")
}
}
func TestRPCBeaconBlocksByRange_RPCHandlerRateLimitOverflow(t *testing.T) {
d := db.SetupDB(t)
hook := logTest.NewGlobal()
saveBlocks := func(req *pb.BeaconBlocksByRangeRequest) {
// Populate the database with blocks that would match the request.
for i := req.StartSlot; i < req.StartSlot+(req.Step*req.Count); i += req.Step {
block := ðpb.SignedBeaconBlock{Block: ðpb.BeaconBlock{Slot: i}}
if err := d.SaveBlock(context.Background(), block); err != nil {
t.Fatal(err)
}
}
}
sendRequest := func(p1, p2 *p2ptest.TestP2P, r *Service,
req *pb.BeaconBlocksByRangeRequest, validateBlocks bool) error {
var wg sync.WaitGroup
wg.Add(1)
pcl := protocol.ID("/testing")
p2.Host.SetStreamHandler(pcl, func(stream network.Stream) {
defer wg.Done()
if !validateBlocks {
return
}
for i := req.StartSlot; i < req.StartSlot+req.Count*req.Step; i += req.Step {
expectSuccess(t, r, stream)
res := ðpb.SignedBeaconBlock{}
if err := r.p2p.Encoding().DecodeWithLength(stream, res); err != nil {
t.Error(err)
}
if (res.Block.Slot-req.StartSlot)%req.Step != 0 {
t.Errorf("Received unexpected block slot %d", res.Block.Slot)
}
}
})
stream, err := p1.Host.NewStream(context.Background(), p2.Host.ID(), pcl)
if err != nil {
t.Fatal(err)
}
if err = r.beaconBlocksByRangeRPCHandler(context.Background(), req, stream); err != nil {
return err
}
if testutil.WaitTimeout(&wg, 1*time.Second) {
t.Fatal("Did not receive stream within 1 sec")
}
return nil
}
t.Run("high request count param and no overflow", func(t *testing.T) {
p1 := p2ptest.NewTestP2P(t)
p2 := p2ptest.NewTestP2P(t)
p1.Connect(p2)
if len(p1.Host.Network().Peers()) != 1 {
t.Error("Expected peers to be connected")
}
capacity := int64(flags.Get().BlockBatchLimit * 3)
r := &Service{p2p: p1, db: d, blocksRateLimiter: leakybucket.NewCollector(0.000001, capacity, false)}
req := &pb.BeaconBlocksByRangeRequest{
StartSlot: 100,
Step: 5,
Count: uint64(capacity),
}
saveBlocks(req)
hook.Reset()
if err := sendRequest(p1, p2, r, req, true); err != nil {
t.Errorf("Unexpected error: %v", err)
}
testutil.AssertLogsDoNotContain(t, hook, "Disconnecting bad peer")
remainingCapacity := r.blocksRateLimiter.Remaining(p2.PeerID().String())
expectedCapacity := int64(0) // Whole capacity is used, but no overflow.
if remainingCapacity != expectedCapacity {
t.Fatalf("Unexpected rate limiting capacity, expected: %v, got: %v", expectedCapacity, remainingCapacity)
}
})
t.Run("high request count param and overflow", func(t *testing.T) {
p1 := p2ptest.NewTestP2P(t)
p2 := p2ptest.NewTestP2P(t)
p1.Connect(p2)
if len(p1.Host.Network().Peers()) != 1 {
t.Error("Expected peers to be connected")
}
capacity := int64(flags.Get().BlockBatchLimit * 3)
r := &Service{p2p: p1, db: d, blocksRateLimiter: leakybucket.NewCollector(0.000001, capacity, false)}
req := &pb.BeaconBlocksByRangeRequest{
StartSlot: 100,
Step: 5,
Count: uint64(capacity + 1),
}
saveBlocks(req)
hook.Reset()
for i := 0; i < p2.Peers().MaxBadResponses(); i++ {
err := sendRequest(p1, p2, r, req, false)
if err == nil || err.Error() != rateLimitedError {
t.Errorf("Expected error not thrown, want: %v, got: %v", rateLimitedError, err)
}
}
// Make sure that we were blocked indeed.
testutil.AssertLogsContain(t, hook, "Disconnecting bad peer")
remainingCapacity := r.blocksRateLimiter.Remaining(p2.PeerID().String())
expectedCapacity := int64(0) // Whole capacity is used.
if remainingCapacity != expectedCapacity {
t.Fatalf("Unexpected rate limiting capacity, expected: %v, got: %v", expectedCapacity, remainingCapacity)
}
})
t.Run("many requests with count set to max blocks per second", func(t *testing.T) {
p1 := p2ptest.NewTestP2P(t)
p2 := p2ptest.NewTestP2P(t)
p1.Connect(p2)
if len(p1.Host.Network().Peers()) != 1 {
t.Error("Expected peers to be connected")
}
capacity := int64(flags.Get().BlockBatchLimit * flags.Get().BlockBatchLimitBurstFactor)
r := &Service{p2p: p1, db: d, blocksRateLimiter: leakybucket.NewCollector(0.000001, capacity, false)}
req := &pb.BeaconBlocksByRangeRequest{
StartSlot: 100,
Step: 1,
Count: uint64(flags.Get().BlockBatchLimit),
}
saveBlocks(req)
hook.Reset()
for i := 0; i < flags.Get().BlockBatchLimitBurstFactor; i++ {
if err := sendRequest(p1, p2, r, req, true); err != nil {
t.Errorf("Unexpected error: %v", err)
}
}
testutil.AssertLogsDoNotContain(t, hook, "Disconnecting bad peer")
// One more request should result in overflow.
hook.Reset()
for i := 0; i < p2.Peers().MaxBadResponses(); i++ {
err := sendRequest(p1, p2, r, req, false)
if err == nil || err.Error() != rateLimitedError {
t.Errorf("Expected error not thrown, want: %v, got: %v", rateLimitedError, err)
}
}
testutil.AssertLogsContain(t, hook, "Disconnecting bad peer")
remainingCapacity := r.blocksRateLimiter.Remaining(p2.PeerID().String())
expectedCapacity := int64(0) // Whole capacity is used.
if remainingCapacity != expectedCapacity {
t.Fatalf("Unexpected rate limiting capacity, expected: %v, got: %v", expectedCapacity, remainingCapacity)
}
})
}