-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathstorage_synchronizer.rs
988 lines (870 loc) · 35.2 KB
/
storage_synchronizer.rs
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
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
// Copyright © Aptos Foundation
// SPDX-License-Identifier: Apache-2.0
use crate::{
error::Error,
metadata_storage::PersistentMetadataStorage,
notification_handlers::{
CommitNotification, CommitNotificationListener, CommittedTransactions,
ErrorNotificationListener, MempoolNotificationHandler, StorageServiceNotificationHandler,
},
storage_synchronizer::{
NotificationMetadata, StorageSynchronizer, StorageSynchronizerHandles,
StorageSynchronizerInterface,
},
tests::{
mocks::{
create_mock_db_writer, create_mock_executor, create_mock_reader_writer,
create_mock_reader_writer_with_version, create_mock_receiver, MockChunkExecutor,
},
utils::{
create_epoch_ending_ledger_info, create_event, create_output_list_with_proof,
create_state_value_chunk_with_proof, create_transaction,
create_transaction_list_with_proof, verify_commit_notification,
},
},
};
use anyhow::format_err;
use aptos_config::config::StateSyncDriverConfig;
use aptos_data_streaming_service::data_notification::NotificationId;
use aptos_event_notifications::EventSubscriptionService;
use aptos_executor_types::ChunkCommitNotification;
use aptos_infallible::{Mutex, RwLock};
use aptos_mempool_notifications::MempoolNotificationListener;
use aptos_storage_interface::{AptosDbError, DbReaderWriter};
use aptos_storage_service_notifications::StorageServiceNotificationListener;
use aptos_types::{
ledger_info::LedgerInfoWithSignatures,
transaction::{TransactionOutputListWithProof, Version},
};
use claims::assert_matches;
use futures::StreamExt;
use mockall::predicate::always;
use std::{sync::Arc, time::Duration};
use tokio::time::timeout;
// Useful test constants
const TEST_TIMEOUT_SECS: u64 = 30;
#[tokio::test(flavor = "multi_thread")]
async fn test_apply_outputs() {
// Create test data
let transaction_to_commit = create_transaction();
let event_to_commit = create_event(None);
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor
.expect_enqueue_chunk_by_transaction_outputs()
.with(always(), always(), always())
.returning(|_, _, _| Ok(()));
chunk_executor.expect_update_ledger().returning(|| Ok(()));
let expected_commit_return = Ok(ChunkCommitNotification {
subscribable_events: vec![event_to_commit.clone()],
committed_transactions: vec![transaction_to_commit.clone()],
reconfiguration_occurred: false,
});
chunk_executor
.expect_commit_chunk()
.return_once(move || expected_commit_return);
// Create the mock DB reader/writer
let highest_synced_version = 1090;
let mock_reader_writer =
create_mock_reader_writer_with_version(None, None, highest_synced_version);
// Create the storage synchronizer
let (
_,
_,
event_subscription_service,
mut mempool_listener,
mut storage_service_listener,
mut storage_synchronizer,
_,
) = create_storage_synchronizer(chunk_executor, mock_reader_writer);
// Subscribe to the expected event
let mut event_listener = event_subscription_service
.lock()
.subscribe_to_events(vec![*event_to_commit.v1().unwrap().key()], vec![])
.unwrap();
// Attempt to apply a chunk of outputs
storage_synchronizer
.apply_transaction_outputs(
NotificationMetadata::new_for_test(0),
create_output_list_with_proof(),
create_epoch_ending_ledger_info(),
None,
)
.await
.unwrap();
// Verify that all components are notified
verify_commit_notification(
Some(&mut event_listener),
&mut mempool_listener,
&mut storage_service_listener,
vec![transaction_to_commit],
vec![event_to_commit],
highest_synced_version,
)
.await;
// Verify there's no pending data
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_apply_outputs_error() {
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor
.expect_enqueue_chunk_by_transaction_outputs()
.with(always(), always(), always())
.returning(|_, _, _| Err(format_err!("Failed to apply chunk!")));
// Create the storage synchronizer
let (_, mut error_listener, _, _, _, mut storage_synchronizer, _) =
create_storage_synchronizer(chunk_executor, create_mock_reader_writer(None, None));
// Attempt to apply a chunk of outputs
let notification_id = 100;
storage_synchronizer
.apply_transaction_outputs(
NotificationMetadata::new_for_test(notification_id),
create_output_list_with_proof(),
create_epoch_ending_ledger_info(),
None,
)
.await
.unwrap();
// Verify we get an error notification and that there's no pending data
verify_error_notification(&mut error_listener, notification_id).await;
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_apply_outputs_send_error() {
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor
.expect_enqueue_chunk_by_transaction_outputs()
.with(always(), always(), always())
.returning(|_, _, _| Ok(()));
// Create the storage synchronizer
let (_, mut error_listener, _, _, _, mut storage_synchronizer, storage_synchronizer_handles) =
create_storage_synchronizer(chunk_executor, create_mock_reader_writer(None, None));
// Explicitly drop the ledger updater to cause a send error for the executor
let ledger_updater = storage_synchronizer_handles.ledger_updater;
ledger_updater.abort();
// Attempt to apply a chunk of outputs
let notification_id = 101;
storage_synchronizer
.apply_transaction_outputs(
NotificationMetadata::new_for_test(notification_id),
create_output_list_with_proof(),
create_epoch_ending_ledger_info(),
None,
)
.await
.unwrap();
// Verify we get an error notification and that there's no pending data
verify_error_notification(&mut error_listener, notification_id).await;
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_apply_outputs_update_error() {
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor
.expect_enqueue_chunk_by_transaction_outputs()
.with(always(), always(), always())
.returning(|_, _, _| Ok(()));
chunk_executor
.expect_update_ledger()
.returning(|| Err(format_err!("Failed to update the ledger!")));
// Create the storage synchronizer
let (_, mut error_listener, _, _, _, mut storage_synchronizer, _) =
create_storage_synchronizer(chunk_executor, create_mock_reader_writer(None, None));
// Attempt to apply a chunk of outputs
let notification_id = 101;
storage_synchronizer
.apply_transaction_outputs(
NotificationMetadata::new_for_test(notification_id),
create_output_list_with_proof(),
create_epoch_ending_ledger_info(),
None,
)
.await
.unwrap();
// Verify we get an error notification and that there's no pending data
verify_error_notification(&mut error_listener, notification_id).await;
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_apply_outputs_update_send_error() {
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor
.expect_enqueue_chunk_by_transaction_outputs()
.with(always(), always(), always())
.returning(|_, _, _| Ok(()));
chunk_executor.expect_update_ledger().returning(|| Ok(()));
// Create the storage synchronizer
let (_, mut error_listener, _, _, _, mut storage_synchronizer, storage_synchronizer_handles) =
create_storage_synchronizer(chunk_executor, create_mock_reader_writer(None, None));
// Explicitly drop the committer to cause a send error for the ledger updater
let committer = storage_synchronizer_handles.committer;
committer.abort();
// Attempt to apply a chunk of outputs
let notification_id = 101;
storage_synchronizer
.apply_transaction_outputs(
NotificationMetadata::new_for_test(notification_id),
create_output_list_with_proof(),
create_epoch_ending_ledger_info(),
None,
)
.await
.unwrap();
// Verify we get an error notification and that there's no pending data
verify_error_notification(&mut error_listener, notification_id).await;
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_apply_outputs_commit_error() {
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor
.expect_enqueue_chunk_by_transaction_outputs()
.with(always(), always(), always())
.returning(|_, _, _| Ok(()));
chunk_executor.expect_update_ledger().returning(|| Ok(()));
chunk_executor
.expect_commit_chunk()
.return_once(|| Err(format_err!("Failed to commit chunk!")));
// Create the storage synchronizer
let (_, mut error_listener, _, _, _, mut storage_synchronizer, _) =
create_storage_synchronizer(chunk_executor, create_mock_reader_writer(None, None));
// Attempt to apply a chunk of outputs
let notification_id = 101;
storage_synchronizer
.apply_transaction_outputs(
NotificationMetadata::new_for_test(notification_id),
create_output_list_with_proof(),
create_epoch_ending_ledger_info(),
None,
)
.await
.unwrap();
// Verify we get an error notification and that there's no pending data
verify_error_notification(&mut error_listener, notification_id).await;
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_apply_outputs_commit_send_error() {
// Create test data
let transaction_to_commit = create_transaction();
let event_to_commit = create_event(None);
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor
.expect_enqueue_chunk_by_transaction_outputs()
.with(always(), always(), always())
.returning(|_, _, _| Ok(()));
chunk_executor.expect_update_ledger().returning(|| Ok(()));
let expected_commit_return = Ok(ChunkCommitNotification {
subscribable_events: vec![event_to_commit.clone()],
committed_transactions: vec![transaction_to_commit.clone()],
reconfiguration_occurred: false,
});
chunk_executor
.expect_commit_chunk()
.return_once(move || expected_commit_return);
// Create the mock DB reader/writer
let highest_synced_version = 1090;
let mock_reader_writer =
create_mock_reader_writer_with_version(None, None, highest_synced_version);
// Create the storage synchronizer
let (_, mut error_listener, _, _, _, mut storage_synchronizer, storage_synchronizer_handles) =
create_storage_synchronizer(chunk_executor, mock_reader_writer);
// Explicitly drop the commit post processor to cause a send error for the ledger updater
let commit_post_processor = storage_synchronizer_handles.commit_post_processor;
commit_post_processor.abort();
// Attempt to apply a chunk of outputs
let notification_id = 555;
storage_synchronizer
.apply_transaction_outputs(
NotificationMetadata::new_for_test(notification_id),
create_output_list_with_proof(),
create_epoch_ending_ledger_info(),
None,
)
.await
.unwrap();
// Verify we get an error notification and that there's no pending data
verify_error_notification(&mut error_listener, notification_id).await;
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_execute_transactions() {
// Create test data
let transaction_to_commit = create_transaction();
let event_to_commit = create_event(None);
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor
.expect_enqueue_chunk_by_execution()
.with(always(), always(), always())
.returning(|_, _, _| Ok(()));
let expected_commit_return = Ok(ChunkCommitNotification {
subscribable_events: vec![event_to_commit.clone()],
committed_transactions: vec![transaction_to_commit.clone()],
reconfiguration_occurred: false,
});
chunk_executor.expect_update_ledger().returning(|| Ok(()));
chunk_executor
.expect_commit_chunk()
.return_once(move || expected_commit_return);
// Create the mock DB reader/writer
let highest_synced_version = 10101;
let mock_reader_writer =
create_mock_reader_writer_with_version(None, None, highest_synced_version);
// Create the storage synchronizer
let (
_,
_,
event_subscription_service,
mut mempool_listener,
mut storage_service_listener,
mut storage_synchronizer,
_,
) = create_storage_synchronizer(chunk_executor, mock_reader_writer);
// Subscribe to the expected event
let mut event_listener = event_subscription_service
.lock()
.subscribe_to_events(vec![*event_to_commit.v1().unwrap().key()], vec![])
.unwrap();
// Attempt to execute a chunk of transactions
storage_synchronizer
.execute_transactions(
NotificationMetadata::new_for_test(0),
create_transaction_list_with_proof(),
create_epoch_ending_ledger_info(),
None,
)
.await
.unwrap();
// Verify that all components are notified
verify_commit_notification(
Some(&mut event_listener),
&mut mempool_listener,
&mut storage_service_listener,
vec![transaction_to_commit],
vec![event_to_commit],
highest_synced_version,
)
.await;
// Verify there's no pending data
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_execute_transactions_error() {
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor
.expect_enqueue_chunk_by_execution()
.with(always(), always(), always())
.returning(|_, _, _| Err(format_err!("Failed to execute chunk!")));
// Create the storage synchronizer
let (_, mut error_listener, _, _, _, mut storage_synchronizer, _) =
create_storage_synchronizer(chunk_executor, create_mock_reader_writer(None, None));
// Attempt to execute a chunk of transactions
let notification_id = 100;
storage_synchronizer
.execute_transactions(
NotificationMetadata::new_for_test(notification_id),
create_transaction_list_with_proof(),
create_epoch_ending_ledger_info(),
None,
)
.await
.unwrap();
// Verify we get an error notification and that there's no pending data
verify_error_notification(&mut error_listener, notification_id).await;
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_execute_transactions_send_error() {
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor
.expect_enqueue_chunk_by_execution()
.with(always(), always(), always())
.returning(|_, _, _| Ok(()));
// Create the storage synchronizer
let (_, mut error_listener, _, _, _, mut storage_synchronizer, storage_synchronizer_handles) =
create_storage_synchronizer(chunk_executor, create_mock_reader_writer(None, None));
// Explicitly drop the ledger updater to cause a send error for the executor
let ledger_updater = storage_synchronizer_handles.ledger_updater;
ledger_updater.abort();
// Attempt to execute a chunk of transactions
let notification_id = 101;
storage_synchronizer
.execute_transactions(
NotificationMetadata::new_for_test(notification_id),
create_transaction_list_with_proof(),
create_epoch_ending_ledger_info(),
None,
)
.await
.unwrap();
// Verify we get an error notification and that there's no pending data
verify_error_notification(&mut error_listener, notification_id).await;
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_execute_transactions_update_error() {
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor
.expect_enqueue_chunk_by_execution()
.with(always(), always(), always())
.returning(|_, _, _| Ok(()));
chunk_executor
.expect_update_ledger()
.returning(|| Err(format_err!("Failed to update the ledger!")));
// Create the storage synchronizer
let (_, mut error_listener, _, _, _, mut storage_synchronizer, _) =
create_storage_synchronizer(chunk_executor, create_mock_reader_writer(None, None));
// Attempt to execute a chunk of transactions
let notification_id = 100;
storage_synchronizer
.execute_transactions(
NotificationMetadata::new_for_test(notification_id),
create_transaction_list_with_proof(),
create_epoch_ending_ledger_info(),
None,
)
.await
.unwrap();
// Verify we get an error notification and that there's no pending data
verify_error_notification(&mut error_listener, notification_id).await;
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_execute_transactions_update_send_error() {
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor
.expect_enqueue_chunk_by_execution()
.with(always(), always(), always())
.returning(|_, _, _| Ok(()));
chunk_executor.expect_update_ledger().returning(|| Ok(()));
// Create the storage synchronizer
let (_, mut error_listener, _, _, _, mut storage_synchronizer, storage_synchronizer_handles) =
create_storage_synchronizer(chunk_executor, create_mock_reader_writer(None, None));
// Explicitly drop the committer to cause a send error for the ledger updater
let committer = storage_synchronizer_handles.committer;
committer.abort();
// Attempt to execute a chunk of transactions
let notification_id = 100;
storage_synchronizer
.execute_transactions(
NotificationMetadata::new_for_test(notification_id),
create_transaction_list_with_proof(),
create_epoch_ending_ledger_info(),
None,
)
.await
.unwrap();
// Verify we get an error notification and that there's no pending data
verify_error_notification(&mut error_listener, notification_id).await;
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_execute_transactions_commit_error() {
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor
.expect_enqueue_chunk_by_execution()
.with(always(), always(), always())
.returning(|_, _, _| Ok(()));
chunk_executor.expect_update_ledger().returning(|| Ok(()));
chunk_executor
.expect_commit_chunk()
.return_once(|| Err(format_err!("Failed to commit chunk!")));
// Create the storage synchronizer
let (_, mut error_listener, _, _, _, mut storage_synchronizer, _) =
create_storage_synchronizer(chunk_executor, create_mock_reader_writer(None, None));
// Attempt to execute a chunk of transactions
let notification_id = 100;
storage_synchronizer
.execute_transactions(
NotificationMetadata::new_for_test(notification_id),
create_transaction_list_with_proof(),
create_epoch_ending_ledger_info(),
None,
)
.await
.unwrap();
// Verify we get an error notification and that there's no pending data
verify_error_notification(&mut error_listener, notification_id).await;
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_execute_transactions_commit_send_error() {
// Create test data
let transaction_to_commit = create_transaction();
let event_to_commit = create_event(None);
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor
.expect_enqueue_chunk_by_execution()
.with(always(), always(), always())
.returning(|_, _, _| Ok(()));
let expected_commit_return = Ok(ChunkCommitNotification {
subscribable_events: vec![event_to_commit.clone()],
committed_transactions: vec![transaction_to_commit.clone()],
reconfiguration_occurred: false,
});
chunk_executor.expect_update_ledger().returning(|| Ok(()));
chunk_executor
.expect_commit_chunk()
.return_once(move || expected_commit_return);
// Create the mock DB reader/writer
let highest_synced_version = 10101;
let mock_reader_writer =
create_mock_reader_writer_with_version(None, None, highest_synced_version);
// Create the storage synchronizer
let (_, mut error_listener, _, _, _, mut storage_synchronizer, storage_synchronizer_handles) =
create_storage_synchronizer(chunk_executor, mock_reader_writer);
// Explicitly drop the commit post processor to cause a send error for the ledger updater
let commit_post_processor = storage_synchronizer_handles.commit_post_processor;
commit_post_processor.abort();
// Attempt to execute a chunk of transactions
let notification_id = 700;
storage_synchronizer
.execute_transactions(
NotificationMetadata::new_for_test(notification_id),
create_transaction_list_with_proof(),
create_epoch_ending_ledger_info(),
None,
)
.await
.unwrap();
// Verify we get an error notification and that there's no pending data
verify_error_notification(&mut error_listener, notification_id).await;
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
#[should_panic]
async fn test_initialize_state_synchronizer_missing_info() {
// Create test data that is missing transaction infos
let mut output_list_with_proof = create_output_list_with_proof();
output_list_with_proof.proof.transaction_infos = vec![]; // This is invalid!
// Create the storage synchronizer
let (_, _, _, _, _, mut storage_synchronizer, _) = create_storage_synchronizer(
create_mock_executor(),
create_mock_reader_writer(None, None),
);
// Initialize the state synchronizer
let state_synchronizer_handle = storage_synchronizer
.initialize_state_synchronizer(
vec![create_epoch_ending_ledger_info()],
create_epoch_ending_ledger_info(),
output_list_with_proof,
)
.unwrap();
// The handler should panic as it was given invalid data
state_synchronizer_handle.await.unwrap();
}
#[tokio::test(flavor = "multi_thread")]
#[should_panic]
async fn test_initialize_state_synchronizer_receiver_error() {
// Setup the mock db writer. The db writer should always fail.
let mut db_writer = create_mock_db_writer();
db_writer
.expect_get_state_snapshot_receiver()
.returning(|_, _| {
Err(AptosDbError::Other(
"Failed to get snapshot receiver!".to_string(),
))
});
// Create the storage synchronizer
let (_, _, _, _, _, mut storage_synchronizer, _) = create_storage_synchronizer(
create_mock_executor(),
create_mock_reader_writer(None, Some(db_writer)),
);
// Initialize the state synchronizer
let state_synchronizer_handle = storage_synchronizer
.initialize_state_synchronizer(
vec![create_epoch_ending_ledger_info()],
create_epoch_ending_ledger_info(),
create_output_list_with_proof(),
)
.unwrap();
// The handler should panic as storage failed to return a snapshot receiver
state_synchronizer_handle.await.unwrap();
}
#[tokio::test(flavor = "multi_thread")]
async fn test_save_states_completion() {
// Create test data
let target_ledger_info = create_epoch_ending_ledger_info();
let epoch_change_proofs = [
create_epoch_ending_ledger_info(),
create_epoch_ending_ledger_info(),
target_ledger_info.clone(),
];
let output_list_with_proof = create_output_list_with_proof();
// Setup the mock snapshot receiver
let mut snapshot_receiver = create_mock_receiver();
snapshot_receiver
.expect_add_chunk()
.with(always(), always())
.returning(|_, _| Ok(()));
snapshot_receiver.expect_finish_box().returning(|| Ok(()));
// Setup the mock executor
let mut chunk_executor = create_mock_executor();
chunk_executor.expect_reset().returning(|| Ok(()));
// Setup the mock db writer
let mut db_writer = create_mock_db_writer();
db_writer
.expect_get_state_snapshot_receiver()
.with(always(), always())
.return_once(move |_, _| Ok(Box::new(snapshot_receiver)));
let target_ledger_info_clone = target_ledger_info.clone();
let output_list_with_proof_clone = output_list_with_proof.clone();
let epoch_change_proofs_clone = epoch_change_proofs.clone();
db_writer
.expect_finalize_state_snapshot()
.withf(
move |version: &Version,
output_with_proof: &TransactionOutputListWithProof,
ledger_infos: &[LedgerInfoWithSignatures]| {
version == &target_ledger_info_clone.ledger_info().version()
&& output_with_proof == &output_list_with_proof_clone
&& ledger_infos == epoch_change_proofs_clone
},
)
.returning(|_, _, _| Ok(()));
// Create the storage synchronizer
let (mut commit_listener, _, _, _, _, mut storage_synchronizer, _) =
create_storage_synchronizer(
chunk_executor,
create_mock_reader_writer(None, Some(db_writer)),
);
// Subscribe to the expected event
let expected_event = output_list_with_proof.transactions_and_outputs[0]
.1
.events()[0]
.clone();
// Initialize the state synchronizer
let state_synchronizer_handle = storage_synchronizer
.initialize_state_synchronizer(
epoch_change_proofs.to_vec(),
target_ledger_info,
output_list_with_proof.clone(),
)
.unwrap();
// Save multiple state chunks (including the last chunk)
storage_synchronizer
.save_state_values(0, create_state_value_chunk_with_proof(false))
.await
.unwrap();
storage_synchronizer
.save_state_values(1, create_state_value_chunk_with_proof(true))
.await
.unwrap();
// Verify we get a commit notification
let expected_transaction = output_list_with_proof.transactions_and_outputs[0].0.clone();
let expected_committed_transactions = CommittedTransactions {
events: vec![expected_event.clone()],
transactions: vec![expected_transaction.clone()],
};
verify_snapshot_commit_notification(
&mut commit_listener,
expected_committed_transactions.clone(),
)
.await;
// The handler should return as we've finished writing all states
state_synchronizer_handle.await.unwrap();
verify_no_pending_data(&storage_synchronizer);
}
#[tokio::test(flavor = "multi_thread")]
#[should_panic]
async fn test_save_states_dropped_error_listener() {
// Setup the mock snapshot receiver
let mut snapshot_receiver = create_mock_receiver();
snapshot_receiver
.expect_add_chunk()
.with(always(), always())
.returning(|_, _| Ok(()));
// Setup the mock db writer
let mut db_writer = create_mock_db_writer();
db_writer
.expect_get_state_snapshot_receiver()
.with(always(), always())
.return_once(move |_, _| Ok(Box::new(snapshot_receiver)));
// Create the storage synchronizer (drop all listeners)
let (_, _, _, _, _, mut storage_synchronizer, _) = create_storage_synchronizer(
create_mock_executor(),
create_mock_reader_writer(None, Some(db_writer)),
);
// Initialize the state synchronizer
let state_synchronizer_handle = storage_synchronizer
.initialize_state_synchronizer(
vec![create_epoch_ending_ledger_info()],
create_epoch_ending_ledger_info(),
create_output_list_with_proof(),
)
.unwrap();
// Save the last state chunk
let notification_id = 0;
storage_synchronizer
.save_state_values(notification_id, create_state_value_chunk_with_proof(true))
.await
.unwrap();
// The handler should panic as the commit listener was dropped
state_synchronizer_handle.await.unwrap();
}
#[tokio::test(flavor = "multi_thread")]
async fn test_save_states_invalid_chunk() {
// Setup the mock snapshot receiver to always return errors
let mut snapshot_receiver = create_mock_receiver();
snapshot_receiver
.expect_add_chunk()
.with(always(), always())
.returning(|_, _| Err(AptosDbError::Other("Invalid chunk!".to_string())));
// Setup the mock db writer
let mut db_writer = create_mock_db_writer();
db_writer
.expect_get_state_snapshot_receiver()
.with(always(), always())
.return_once(move |_, _| Ok(Box::new(snapshot_receiver)));
// Create the storage synchronizer
let (_, mut error_listener, _, _, _, mut storage_synchronizer, _) = create_storage_synchronizer(
create_mock_executor(),
create_mock_reader_writer(None, Some(db_writer)),
);
// Initialize the state synchronizer
let _join_handle = storage_synchronizer
.initialize_state_synchronizer(
vec![create_epoch_ending_ledger_info()],
create_epoch_ending_ledger_info(),
create_output_list_with_proof(),
)
.unwrap();
// Save a state chunk and verify we get an error notification
let notification_id = 0;
storage_synchronizer
.save_state_values(notification_id, create_state_value_chunk_with_proof(false))
.await
.unwrap();
verify_error_notification(&mut error_listener, notification_id).await;
}
#[tokio::test]
#[should_panic]
async fn test_save_states_without_initialize() {
// Create the storage synchronizer
let (_, _, _, _, _, mut storage_synchronizer, _) = create_storage_synchronizer(
create_mock_executor(),
create_mock_reader_writer(None, None),
);
// Attempting to save the states should panic as the state
// synchronizer was not initialized!
storage_synchronizer
.save_state_values(0, create_state_value_chunk_with_proof(false))
.await
.unwrap();
}
/// Creates a storage synchronizer for testing
fn create_storage_synchronizer(
mock_chunk_executor: MockChunkExecutor,
mock_reader_writer: DbReaderWriter,
) -> (
CommitNotificationListener,
ErrorNotificationListener,
Arc<Mutex<EventSubscriptionService>>,
MempoolNotificationListener,
StorageServiceNotificationListener,
StorageSynchronizer<MockChunkExecutor, PersistentMetadataStorage>,
StorageSynchronizerHandles,
) {
aptos_logger::Logger::init_for_testing();
// Create the notification channels
let (commit_notification_sender, commit_notification_listener) =
CommitNotificationListener::new();
let (error_notification_sender, error_notification_listener) = ErrorNotificationListener::new();
// Create the event subscription service
let event_subscription_service = Arc::new(Mutex::new(EventSubscriptionService::new(Arc::new(
RwLock::new(mock_reader_writer.clone()),
))));
// Create the mempool notification handler
let (mempool_notification_sender, mempool_notification_listener) =
aptos_mempool_notifications::new_mempool_notifier_listener_pair(100);
let mempool_notification_handler = MempoolNotificationHandler::new(mempool_notification_sender);
// Create the storage service handler
let (storage_service_notifier, storage_service_listener) =
aptos_storage_service_notifications::new_storage_service_notifier_listener_pair();
let storage_service_notification_handler =
StorageServiceNotificationHandler::new(storage_service_notifier);
// Create the metadata storage
let db_path = aptos_temppath::TempPath::new();
let metadata_storage = PersistentMetadataStorage::new(db_path.path());
// Create the storage synchronizer
let (storage_synchronizer, storage_synchronizer_handles) = StorageSynchronizer::new(
StateSyncDriverConfig::default(),
Arc::new(mock_chunk_executor),
commit_notification_sender,
error_notification_sender,
event_subscription_service.clone(),
mempool_notification_handler,
storage_service_notification_handler,
metadata_storage,
mock_reader_writer,
None,
);
(
commit_notification_listener,
error_notification_listener,
event_subscription_service,
mempool_notification_listener,
storage_service_listener,
storage_synchronizer,
storage_synchronizer_handles,
)
}
/// Verifies that the expected snapshot commit notification is received by the listener
async fn verify_snapshot_commit_notification(
commit_listener: &mut CommitNotificationListener,
expected_committed_transactions: CommittedTransactions,
) {
let CommitNotification::CommittedStateSnapshot(committed_snapshot) = timeout(
Duration::from_secs(TEST_TIMEOUT_SECS),
commit_listener.select_next_some(),
)
.await
.unwrap();
assert_eq!(
committed_snapshot.committed_transaction,
expected_committed_transactions
);
}
/// Verifies that the expected error notification is received by the listener
async fn verify_error_notification(
error_listener: &mut ErrorNotificationListener,
expected_notification_id: NotificationId,
) {
let error_notification = timeout(
Duration::from_secs(TEST_TIMEOUT_SECS),
error_listener.select_next_some(),
)
.await
.unwrap();
assert_eq!(error_notification.notification_id, expected_notification_id);
assert_matches!(error_notification.error, Error::UnexpectedError(_));
}
/// Verifies that no pending data remains in the storage synchronizer.
/// Note: due to asynchronous execution, we might need to wait some
/// time for the pipelines to drain.
fn verify_no_pending_data(
storage_synchronizer: &StorageSynchronizer<MockChunkExecutor, PersistentMetadataStorage>,
) {
let max_drain_time_secs = 10;
for _ in 0..max_drain_time_secs {
if !storage_synchronizer.pending_storage_data() {
return;
}
std::thread::sleep(Duration::from_secs(1));
}
panic!("Timed-out waiting for the storage synchronizer to drain!");
}