-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_sync_manager.cpp
More file actions
659 lines (513 loc) · 27.3 KB
/
test_sync_manager.cpp
File metadata and controls
659 lines (513 loc) · 27.3 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
#define BOOST_TEST_MODULE sync_manager
#define FC_DISABLE_LOGGING
#include <eosio/net_plugin/sync_manager.hpp>
#include <eosio/net_plugin/mock_net_plugin_impl.hpp>
#include <boost/test/included/unit_test.hpp>
#include <boost/fakeit.hpp>
#include <tuple>
using namespace eosio::chain;
using namespace eosio::p2p;
using namespace fakeit;
template<typename _Fn>
using cache_t = sync_manager<mock_connection, mock_net_plugin_interface>::state_machine::cache<_Fn>;
template<typename _Fn>
using always_t = sync_manager<mock_connection, mock_net_plugin_interface>::state_machine::always<_Fn>;
Mock<mock_net_plugin_interface> create_mock_net_plugin() {
Mock<mock_net_plugin_interface> mock_net_plugin;
Fake(Method(mock_net_plugin, get_logger));
Fake(Method(mock_net_plugin, get_log_format));
Fake(Method(mock_net_plugin, shared_connections_lock));
Fake(Method(mock_net_plugin, get_chain_info));
Fake(Method(mock_net_plugin, for_each_connection));
Fake(Method(mock_net_plugin, for_each_block_connection));
Fake(Method(mock_net_plugin, get_connections));
return mock_net_plugin;
}
Mock<mock_connection> create_mock_connection() {
Mock<mock_connection> mock_conn;
Fake(Method(mock_conn, locked_connection_mutex));
Fake(Method(mock_conn, get_strand));
Fake(Method(mock_conn, request_sync_blocks));
Fake(Method(mock_conn, post));
Fake(Method(mock_conn, send_handshake));
Fake(Method(mock_conn, get_ci));
Fake(Method(mock_conn, get_fork_head_num));
Fake(Method(mock_conn, get_fork_head));
Fake(Method(mock_conn, get_id));
When(Method(mock_conn, get_last_handshake)).AlwaysReturn(handshake_message());
Fake(Method(mock_conn, current));
Fake(Method(mock_conn, is_transactions_only_connection));
Fake(Method(mock_conn, reset_fork_head));
return mock_conn;
}
std::shared_ptr<mock_net_plugin_interface> get_net_plugin_interface(Mock<mock_net_plugin_interface>& mock_net_plugin) {
return {&mock_net_plugin.get(), [](mock_net_plugin_interface*){}};
}
std::shared_ptr<mock_connection> get_connection_interface(Mock<mock_connection>& mock_conn) {
return {&mock_conn.get(), [](mock_connection*){}};
}
sync_manager<mock_connection, mock_net_plugin_interface> create_sync_manager(Mock<mock_net_plugin_interface>& mock_net_plugin) {
return sync_manager<mock_connection, mock_net_plugin_interface>(10, get_net_plugin_interface(mock_net_plugin));
}
handshake_message create_handshake_message(uint32_t last_lib) {
handshake_message m = handshake_message();
m.last_irreversible_block_num = last_lib;
return m;
}
BOOST_AUTO_TEST_SUITE( sync_manager_test )
BOOST_AUTO_TEST_CASE( is_sync_required_test ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
block_id_type null_id;
std::set<mock_connection_ptr> conn_set;
conn_set.insert( get_connection_interface(mock_conn) );
When(Method(mock_net_plugin, get_chain_info)).AlwaysReturn(std::make_tuple(2,0,3,null_id,null_id,null_id));
When(Method(mock_net_plugin, for_each_block_connection)).AlwaysDo([&mock_conn](auto lmd){ lmd(get_connection_interface(mock_conn)); });
When(Method(mock_net_plugin, get_connections)).AlwaysReturn(conn_set);
When(Method(mock_conn, get_last_handshake)).AlwaysReturn(create_handshake_message(11));
When(Method(mock_conn, current)).AlwaysReturn(true);
When(Method(mock_conn, is_transactions_only_connection)).AlwaysReturn(false);
BOOST_REQUIRE( sm.is_sync_required(3) );
BOOST_REQUIRE( !sm.is_sync_required(2) );
// set sync_known_lib_num to 11
sm.set_highest_lib();
// now target lib doesn't matter, we rely on sync_known_lib_num
BOOST_REQUIRE( sm.is_sync_required(2) );
//needed for calling continue_sync
sm.set_new_sync_source(get_connection_interface(mock_conn));
// after that call sync_last_requested_num should be 10
sm.continue_sync();
//now sync_last_requested_num is 1
When(Method(mock_conn, get_last_handshake)).AlwaysReturn(create_handshake_message(1));
sm.set_highest_lib();
// still sync required because of sync_last_requested_num is 10 (sync_last_requested_num - 1)
BOOST_REQUIRE( sm.is_sync_required(2) );
// setting sync_last_requested_num to 0 (sync_last_requested_num - 1)
sm.continue_sync();
// now sync is not required as we rely on controller lib again
BOOST_REQUIRE( !sm.is_sync_required(2) );
}
BOOST_AUTO_TEST_CASE( send_handshakes ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn1 = create_mock_connection();
auto mock_conn2 = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
When(Method(mock_conn1, current)).AlwaysReturn(true);
When(Method(mock_net_plugin, for_each_connection)).AlwaysDo(
[&mock_conn1, &mock_conn2](auto lmd){
lmd(get_connection_interface(mock_conn1));
lmd(get_connection_interface(mock_conn2));
});
sm.send_handshakes();
Verify(Method(mock_conn1, send_handshake)).Exactly(1);
Verify(Method(mock_conn1, current)).Exactly(1);
Verify(Method(mock_conn2, send_handshake)).Exactly(0);
Verify(Method(mock_conn2, current)).Exactly(1);
}
BOOST_AUTO_TEST_CASE( is_sync_source ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
When(Method(mock_conn, current)).AlwaysReturn(true);
BOOST_REQUIRE( !sm.is_sync_source( *get_connection_interface(mock_conn)) );
sm.set_new_sync_source(get_connection_interface(mock_conn));
BOOST_REQUIRE( sm.is_sync_source( *get_connection_interface(mock_conn)) );
}
BOOST_AUTO_TEST_CASE( sync_reset_lib_num ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
BOOST_REQUIRE( sm.get_known_lib() == 0 );
sm.sync_reset_lib_num(5);
BOOST_REQUIRE( sm.get_known_lib() == 5 );
sm.sync_reset_lib_num(4);
BOOST_REQUIRE( sm.get_known_lib() == 5 );
}
BOOST_AUTO_TEST_CASE( sync_update_expected ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
When(Method(mock_conn, get_last_handshake)).AlwaysReturn(create_handshake_message(11));
When(Method(mock_conn, current)).AlwaysReturn(true);
When(Method(mock_conn, is_transactions_only_connection)).AlwaysReturn(false);
When(Method(mock_net_plugin, for_each_block_connection)).AlwaysDo([&mock_conn](auto lmd){ lmd(get_connection_interface(mock_conn)); });
// set sync_known_lib_num to 11
sm.set_highest_lib();
//needed for calling continue_sync
sm.set_new_sync_source(get_connection_interface(mock_conn));
// after that call sync_last_requested_num should be 10
sm.continue_sync();
// after setting sync_known_lib_num and sync_last_requested_num in previous steps we can try to update next expected
sm.sync_update_expected({}, 2, true);
BOOST_REQUIRE( sm.get_sync_next_expected() == 3 );
// next expected should change because of it is equal to old value even though applied is false
sm.sync_update_expected({}, 3, false);
BOOST_REQUIRE( sm.get_sync_next_expected() == 4 );
// next expected is not changed because of applied is false
sm.sync_update_expected({}, 5, false);
BOOST_REQUIRE( sm.get_sync_next_expected() == 4 );
// next expected is not changed because of suggested value is greater than sync_last_requested_num
sm.sync_update_expected({}, 11, true);
BOOST_REQUIRE( sm.get_sync_next_expected() == 4 );
}
BOOST_AUTO_TEST_CASE( begin_sync ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
sm.begin_sync(get_connection_interface(mock_conn), 0);
Verify(Method(mock_net_plugin, for_each_connection)).Exactly(1);
When(Method(mock_conn, current)).AlwaysReturn(true);
sm.sync_reset_lib_num(10);
sm.set_new_sync_source(get_connection_interface(mock_conn));
sm.begin_sync(get_connection_interface(mock_conn), 0);
// verify for_each_connection was not called more since last check
Verify(Method(mock_net_plugin, for_each_connection)).Exactly(1);
// verify request was posted to connection
Verify(Method(mock_conn, post)).Exactly(1);
BOOST_REQUIRE( sm.get_sync_last_requested_num() == 10 );
// TODO: this case looks unrealistic but code logic permits this
// maybe that is indicator to check how this can be possible and remove this logic from sync_manager at all
When(Method(mock_conn, get_last_handshake)).AlwaysReturn(create_handshake_message(1));
When(Method(mock_net_plugin, for_each_block_connection)).AlwaysDo([&mock_conn](auto lmd){ lmd(get_connection_interface(mock_conn)); });
sm.set_highest_lib();
sm.sync_update_expected({}, 9, true);
// now we have sync_known_lib_num = 1 and sync_next_expected_num = 10
// start should be greater then end span
sm.begin_sync(get_connection_interface(mock_conn), 0);
//verify sync didn't begin and we sent handshakes to conections
Verify(Method(mock_net_plugin, for_each_connection)).Exactly(2);
Verify(Method(mock_conn, post)).Exactly(1);
}
BOOST_AUTO_TEST_CASE( continue_sync ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
sm.continue_sync();
Verify(Method(mock_net_plugin, for_each_connection)).Exactly(1);
When(Method(mock_conn, current)).AlwaysReturn(true);
sm.sync_reset_lib_num(10);
sm.set_new_sync_source(get_connection_interface(mock_conn));
sm.continue_sync();
// verify for_each_connection was not called more since last check
Verify(Method(mock_net_plugin, for_each_connection)).Exactly(1);
// verify request was posted to connection
Verify(Method(mock_conn, post)).Exactly(1);
BOOST_REQUIRE( sm.get_sync_last_requested_num() == 10 );
// TODO: this case looks unrealistic but code logic permits this
// maybe that is indicator to check how this can be possible and remove this logic from sync_manager at all
When(Method(mock_conn, get_last_handshake)).AlwaysReturn(create_handshake_message(1));
When(Method(mock_net_plugin, for_each_block_connection)).AlwaysDo([&mock_conn](auto lmd){ lmd(get_connection_interface(mock_conn)); });
sm.set_highest_lib();
sm.sync_update_expected({}, 9, true);
// now we have sync_known_lib_num = 1 and sync_next_expected_num = 10
// start should be greater then end span
sm.continue_sync();
//verify sync didn't begin and we sent handshakes to conections
Verify(Method(mock_net_plugin, for_each_connection)).Exactly(2);
Verify(Method(mock_conn, post)).Exactly(1);
}
BOOST_AUTO_TEST_CASE( fork_head_ge ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
//64 characters
block_id_type test_block_id("1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF");
BOOST_REQUIRE( !sm.fork_head_ge(0, {}) );
Verify(Method(mock_net_plugin, for_each_block_connection)).Exactly(1);
When(Method(mock_net_plugin, for_each_block_connection)).AlwaysDo([&mock_conn](auto lmd){ lmd(get_connection_interface(mock_conn)); });
When(Method(mock_conn, get_fork_head_num)).AlwaysReturn(10);
When(Method(mock_conn, get_fork_head)).AlwaysReturn(test_block_id);
BOOST_REQUIRE( sm.fork_head_ge(0, test_block_id) );
BOOST_REQUIRE( sm.fork_head_ge(9, {}) );
BOOST_REQUIRE( sm.fork_head_ge(10, test_block_id) );
// 11 > 10 but block_id match so it returns true here
BOOST_REQUIRE( sm.fork_head_ge(11, test_block_id) );
// same comparison with different blck id fails
BOOST_REQUIRE( !sm.fork_head_ge(11, {}) );
}
BOOST_AUTO_TEST_CASE( reset_last_requested_num ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
When(Method(mock_conn, get_last_handshake)).AlwaysReturn(create_handshake_message(11));
When(Method(mock_conn, current)).AlwaysReturn(true);
When(Method(mock_conn, is_transactions_only_connection)).AlwaysReturn(false);
When(Method(mock_net_plugin, for_each_block_connection)).AlwaysDo([&mock_conn](auto lmd){ lmd(get_connection_interface(mock_conn)); });
// set sync_known_lib_num to 11
sm.set_highest_lib();
//needed to set sync_source for calling continue_sync
sm.set_new_sync_source(get_connection_interface(mock_conn));
// after that call sync_last_requested_num should be 10
sm.continue_sync();
BOOST_REQUIRE( sm.get_sync_last_requested_num() == 10 );
sm.reset_last_requested_num();
BOOST_REQUIRE( sm.get_sync_last_requested_num() == 0 );
}
BOOST_AUTO_TEST_CASE( reset_sync_source ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
When(Method(mock_conn, current)).AlwaysReturn(true);
When(Method(mock_conn, is_transactions_only_connection)).AlwaysReturn(false);
sm.set_new_sync_source(get_connection_interface(mock_conn));
BOOST_REQUIRE( sm.is_sync_source(*get_connection_interface(mock_conn)) );
sm.reset_sync_source();
BOOST_REQUIRE( !sm.is_sync_source(*get_connection_interface(mock_conn)) );
}
BOOST_AUTO_TEST_CASE( closing_sync_source ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
block_id_type null_id;
When(Method(mock_conn, get_last_handshake)).AlwaysReturn(create_handshake_message(11));
When(Method(mock_conn, current)).AlwaysReturn(true);
When(Method(mock_conn, is_transactions_only_connection)).AlwaysReturn(false);
When(Method(mock_net_plugin, for_each_block_connection)).AlwaysDo([&mock_conn](auto lmd){ lmd(get_connection_interface(mock_conn)); });
When(Method(mock_net_plugin, get_chain_info)).AlwaysReturn(std::make_tuple(0, 11, 0, null_id, null_id, null_id));
// set sync_known_lib_num to 11
sm.set_highest_lib();
//needed to set sync_source for calling continue_sync
sm.set_new_sync_source(get_connection_interface(mock_conn));
// after that call sync_last_requested_num should be 10
sm.continue_sync();
BOOST_REQUIRE( sm.get_sync_last_requested_num() == 10 );
sm.closing_sync_source();
BOOST_REQUIRE( sm.get_sync_last_requested_num() == 0 );
BOOST_REQUIRE( sm.get_sync_next_expected() == 12 );
}
BOOST_AUTO_TEST_CASE( sync_in_progress ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
block_id_type null_id;
BOOST_REQUIRE( !sm.sync_in_progress() );
When(Method(mock_conn, get_last_handshake)).AlwaysReturn(create_handshake_message(11));
When(Method(mock_conn, current)).AlwaysReturn(true);
When(Method(mock_conn, is_transactions_only_connection)).AlwaysReturn(false);
When(Method(mock_net_plugin, for_each_block_connection)).AlwaysDo([&mock_conn](auto lmd){ lmd(get_connection_interface(mock_conn)); });
When(Method(mock_net_plugin, get_chain_info)).AlwaysReturn(std::make_tuple(0, 0, 9, null_id, null_id, null_id));
// set sync_known_lib_num to 11
sm.set_highest_lib();
//needed for calling continue_sync
sm.set_new_sync_source(get_connection_interface(mock_conn));
// after that call sync_last_requested_num should be 10
sm.continue_sync();
BOOST_REQUIRE( sm.get_sync_last_requested_num() == 10 );
// sync_last_requested_num > fork head and sync source current
BOOST_REQUIRE( sm.sync_in_progress() );
When(Method(mock_net_plugin, get_chain_info)).AlwaysReturn(std::make_tuple(0, 0, 11, null_id, null_id, null_id));
// sync_last_requested_num < fork head and sync source current
BOOST_REQUIRE( !sm.sync_in_progress() );
// restore to previous state
When(Method(mock_net_plugin, get_chain_info)).AlwaysReturn(std::make_tuple(0, 0, 9, null_id, null_id, null_id));
BOOST_REQUIRE( sm.sync_in_progress() );
// sync_last_requested_num > fork head but sync source is not current
When(Method(mock_conn, current)).AlwaysReturn(false);
BOOST_REQUIRE( !sm.sync_in_progress() );
// restore to previous state
When(Method(mock_conn, current)).AlwaysReturn(true);
BOOST_REQUIRE( sm.sync_in_progress() );
//fork head < sync_last_requested_num but sync_source is null
sm.reset_sync_source();
BOOST_REQUIRE( !sm.sync_in_progress() );
}
BOOST_AUTO_TEST_CASE( set_new_sync_source ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
block_id_type null_id;
std::set<mock_connection_ptr> conn_set;
conn_set.insert( get_connection_interface(mock_conn) );
When(Method(mock_conn, get_last_handshake)).AlwaysReturn(create_handshake_message(11));
When(Method(mock_conn, current)).AlwaysReturn(true);
When(Method(mock_net_plugin, get_connections)).AlwaysReturn({});
When(Method(mock_net_plugin, for_each_block_connection)).AlwaysDo([&mock_conn](auto lmd){ lmd(get_connection_interface(mock_conn)); });
When(Method(mock_net_plugin, get_chain_info)).AlwaysReturn(std::make_tuple(10, 0, 0, null_id, null_id, null_id));
// set sync_known_lib_num to 11
sm.set_highest_lib();
//needed for calling continue_sync
sm.set_new_sync_source(get_connection_interface(mock_conn));
BOOST_REQUIRE( sm.is_sync_source(*get_connection_interface(mock_conn)) );
// after that call sync_last_requested_num should be 10
sm.continue_sync();
// empty connections list and null sync_hint - can't set sync_source
BOOST_REQUIRE( sm.get_known_lib() == 11 );
BOOST_REQUIRE( sm.get_sync_last_requested_num() == 10 );
BOOST_REQUIRE( !sm.set_new_sync_source(nullptr) );
BOOST_REQUIRE( !sm.is_sync_source(*get_connection_interface(mock_conn)) );
// make sure lib was reset to known lib
BOOST_REQUIRE( sm.get_known_lib() == 10 );
BOOST_REQUIRE( sm.get_sync_last_requested_num() == 0 );
// empty connections list and non-current sync_hint - can't set sync_source
When(Method(mock_conn, current)).AlwaysReturn(false);
BOOST_REQUIRE( !sm.set_new_sync_source(get_connection_interface(mock_conn)) );
BOOST_REQUIRE( !sm.is_sync_source(*get_connection_interface(mock_conn)) );
// empty connections list and transaction-only sync_hint - can't set sync_source
When(Method(mock_conn, current)).AlwaysReturn(true);
When(Method(mock_conn, is_transactions_only_connection)).AlwaysReturn(true);
BOOST_REQUIRE( !sm.set_new_sync_source(get_connection_interface(mock_conn)) );
BOOST_REQUIRE( !sm.is_sync_source(*get_connection_interface(mock_conn)) );
When(Method(mock_net_plugin, get_chain_info)).AlwaysReturn(std::make_tuple(0, 0, 0, null_id, null_id, null_id));
// valid current sync hint - success
When(Method(mock_conn, is_transactions_only_connection)).AlwaysReturn(false);
BOOST_REQUIRE( sm.set_new_sync_source(get_connection_interface(mock_conn)) );
BOOST_REQUIRE( sm.is_sync_source(*get_connection_interface(mock_conn)) );
// null sync hint but connections has one valid - success
When(Method(mock_net_plugin, get_connections)).AlwaysReturn(conn_set);
BOOST_REQUIRE( sm.set_new_sync_source(nullptr) );
BOOST_REQUIRE( sm.is_sync_source(*get_connection_interface(mock_conn)) );
//no current connection in connections
When(Method(mock_conn, current)).AlwaysReturn(false);
BOOST_REQUIRE( !sm.set_new_sync_source(nullptr) );
BOOST_REQUIRE( !sm.is_sync_source(*get_connection_interface(mock_conn)) );
//no blocks connection in connections
When(Method(mock_conn, current)).AlwaysReturn(true);
When(Method(mock_conn, is_transactions_only_connection)).AlwaysReturn(true);
BOOST_REQUIRE( !sm.set_new_sync_source(nullptr) );
BOOST_REQUIRE( !sm.is_sync_source(*get_connection_interface(mock_conn)) );
// adding few more connections
auto mock_conn2 = create_mock_connection();
auto mock_conn3 = create_mock_connection();
When(Method(mock_conn3, current)).AlwaysReturn(true);
When(Method(mock_conn3, is_transactions_only_connection)).AlwaysReturn(false);
conn_set.insert( get_connection_interface(mock_conn2) );
conn_set.insert( get_connection_interface(mock_conn3) );
When(Method(mock_net_plugin, get_connections)).AlwaysReturn(conn_set);
// conn2 is not current and conn is transaction only
BOOST_REQUIRE( sm.set_new_sync_source(nullptr) );
BOOST_REQUIRE( sm.is_sync_source(*get_connection_interface(mock_conn3)) );
When(Method(mock_conn3, current)).AlwaysReturn(false);
When(Method(mock_conn2, current)).AlwaysReturn(true);
// now chosing next current blocks connection which is conn2
BOOST_REQUIRE( sm.set_new_sync_source(nullptr) );
BOOST_REQUIRE( sm.is_sync_source(*get_connection_interface(mock_conn2)) );
When(Method(mock_conn, current)).AlwaysReturn(true);
When(Method(mock_conn, is_transactions_only_connection)).AlwaysReturn(false);
When(Method(mock_conn2, current)).AlwaysReturn(false);
// suggesting mock_conn2 but mock_conn should be chosen
BOOST_REQUIRE( sm.set_new_sync_source(get_connection_interface(mock_conn2)) );
BOOST_REQUIRE( sm.is_sync_source(*get_connection_interface(mock_conn)) );
// set sync_known_lib_num to 11
sm.set_highest_lib();
When(Method(mock_conn2, get_last_handshake)).AlwaysReturn(create_handshake_message(9));
When(Method(mock_conn2, current)).AlwaysReturn(true);
When(Method(mock_conn3, get_last_handshake)).AlwaysReturn(create_handshake_message(12));
When(Method(mock_conn3, current)).AlwaysReturn(true);
// should skip mock_conn2 because of lower lib and choose mock_conn3
BOOST_REQUIRE( sm.set_new_sync_source(nullptr) );
BOOST_REQUIRE( sm.is_sync_source(*get_connection_interface(mock_conn3)) );
}
BOOST_AUTO_TEST_CASE( block_ge_lib ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
When(Method(mock_conn, get_last_handshake)).AlwaysReturn(create_handshake_message(11));
When(Method(mock_conn, current)).AlwaysReturn(true);
When(Method(mock_net_plugin, for_each_block_connection)).AlwaysDo([&mock_conn](auto lmd){ lmd(get_connection_interface(mock_conn)); });
// set sync_known_lib_num to 11
sm.set_highest_lib();
BOOST_REQUIRE( !sm.block_ge_lib(10) );
BOOST_REQUIRE( sm.block_ge_lib(11) );
BOOST_REQUIRE( sm.block_ge_lib(12) );
}
BOOST_AUTO_TEST_CASE( block_ge_last_requested ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
When(Method(mock_conn, get_last_handshake)).AlwaysReturn(create_handshake_message(11));
When(Method(mock_conn, current)).AlwaysReturn(true);
When(Method(mock_net_plugin, for_each_block_connection)).AlwaysDo([&mock_conn](auto lmd){ lmd(get_connection_interface(mock_conn)); });
// set sync_known_lib_num to 11
sm.set_highest_lib();
//needed for calling continue_sync
sm.set_new_sync_source(get_connection_interface(mock_conn));
// after that call sync_last_requested_num should be 10
sm.continue_sync();
BOOST_REQUIRE( !sm.block_ge_last_requested(9) );
BOOST_REQUIRE( sm.block_ge_last_requested(10) );
BOOST_REQUIRE( sm.block_ge_last_requested(11) );
}
BOOST_AUTO_TEST_CASE( continue_head_catchup ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
//64 characters
block_id_type test_block_id("1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF1234567890ABCDEF");
block_id_type null_id;
// no connections
BOOST_REQUIRE( !sm.continue_head_catchup({},{}) );
When(Method(mock_net_plugin, for_each_block_connection)).AlwaysDo([&mock_conn](auto lmd){ lmd(get_connection_interface(mock_conn)); });
When(Method(mock_conn, get_fork_head_num)).AlwaysReturn(10);
When(Method(mock_conn, get_fork_head)).AlwaysReturn(null_id);
// fork_head_id is null
BOOST_REQUIRE( !sm.continue_head_catchup(null_id,9) );
Verify(Method(mock_conn, reset_fork_head)).Never();
// fork_head < 11 but fork_head_id is null
BOOST_REQUIRE( !sm.continue_head_catchup(null_id,11) );
Verify(Method(mock_conn, reset_fork_head)).Never();
When(Method(mock_conn, get_fork_head)).AlwaysReturn(test_block_id);
// fork_head_id is not null
BOOST_REQUIRE( sm.continue_head_catchup(null_id,9) );
Verify(Method(mock_conn, reset_fork_head)).Never();
// fork_head_id is not null and less then 11
BOOST_REQUIRE( !sm.continue_head_catchup(null_id,11) );
Verify(Method(mock_conn, reset_fork_head)).Once();
}
BOOST_AUTO_TEST_CASE( set_highest_lib ) {
auto mock_net_plugin = create_mock_net_plugin();
auto mock_conn = create_mock_connection();
auto sm = create_sync_manager(mock_net_plugin);
When(Method(mock_net_plugin, for_each_block_connection)).AlwaysDo([&mock_conn](auto lmd){ lmd(get_connection_interface(mock_conn)); });
When(Method(mock_conn, get_last_handshake)).AlwaysReturn(create_handshake_message(11));
When(Method(mock_conn, current)).AlwaysReturn(true);
sm.set_highest_lib();
BOOST_REQUIRE( sm.get_known_lib() == 11 );
auto mock_conn2 = create_mock_connection();
When(Method(mock_conn2, get_last_handshake)).AlwaysReturn(create_handshake_message(13));
When(Method(mock_conn2, current)).AlwaysReturn(false);
auto mock_conn3 = create_mock_connection();
When(Method(mock_conn3, get_last_handshake)).AlwaysReturn(create_handshake_message(12));
When(Method(mock_conn3, current)).AlwaysReturn(true);
When(Method(mock_net_plugin, for_each_block_connection)).AlwaysDo(
[&](auto lmd){
lmd(get_connection_interface(mock_conn));
lmd(get_connection_interface(mock_conn2));
lmd(get_connection_interface(mock_conn3));
});
sm.set_highest_lib();
BOOST_REQUIRE( sm.get_known_lib() == 12 );
}
BOOST_AUTO_TEST_CASE( update_next_expected ) {
auto mock_net_plugin = create_mock_net_plugin();
auto sm = create_sync_manager(mock_net_plugin);
block_id_type null_id;
When(Method(mock_net_plugin, get_chain_info)).AlwaysReturn(std::make_tuple(10,0,0,null_id,null_id,null_id));
sm.update_next_expected();
BOOST_REQUIRE( sm.get_sync_next_expected() == 11 );
When(Method(mock_net_plugin, get_chain_info)).AlwaysReturn(std::make_tuple(9,0,0,null_id,null_id,null_id));
sm.update_next_expected();
BOOST_REQUIRE( sm.get_sync_next_expected() == 11 );
When(Method(mock_net_plugin, get_chain_info)).AlwaysReturn(std::make_tuple(11,0,0,null_id,null_id,null_id));
sm.update_next_expected();
BOOST_REQUIRE( sm.get_sync_next_expected() == 12 );
}
BOOST_AUTO_TEST_CASE( cache ) {
int counter = 0;
auto test_lmd = [&counter](){ return ++counter; };
auto c1 = cache_t<decltype(test_lmd)>(test_lmd);
auto c2 = cache_t<decltype(test_lmd)>(test_lmd, true);
BOOST_REQUIRE( c1() == c2() );
BOOST_REQUIRE( c1() != c1() );
BOOST_REQUIRE( c1() == c2() );
BOOST_REQUIRE( counter == 4 );
}
BOOST_AUTO_TEST_CASE( always ) {
auto test_lmd1 = [](){ return false; };
auto test_lmd2 = [](){};
auto test_lmd3 = [](){ return std::vector<std::string>(); };
BOOST_REQUIRE( always_t<decltype(test_lmd1)>(test_lmd1)() == true );
BOOST_REQUIRE( always_t<decltype(test_lmd2)>(test_lmd2)() == true );
BOOST_REQUIRE( always_t<decltype(test_lmd3)>(test_lmd3)() == true );
}
BOOST_AUTO_TEST_SUITE_END()