state-sync-driver 0.2.7

The driver for state sync
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
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
// Copyright (c) Aptos
// SPDX-License-Identifier: Apache-2.0

use crate::bootstrapper::GENESIS_TRANSACTION_VERSION;
use crate::{
    bootstrapper::Bootstrapper,
    driver::DriverConfiguration,
    error::Error,
    tests::{
        mocks::{
            create_mock_db_reader, create_mock_streaming_client, create_ready_storage_synchronizer,
            MockMetadataStorage, MockStorageSynchronizer, MockStreamingClient,
        },
        utils::{
            create_data_stream_listener, create_empty_epoch_state, create_epoch_ending_ledger_info,
            create_full_node_driver_configuration, create_global_summary,
            create_output_list_with_proof, create_random_epoch_ending_ledger_info,
            create_transaction_info, create_transaction_list_with_proof,
        },
    },
};
use aptos_config::config::BootstrappingMode;
use aptos_data_client::GlobalDataSummary;
use aptos_types::{
    transaction::{TransactionOutputListWithProof, Version},
    waypoint::Waypoint,
};
use claim::{assert_matches, assert_none, assert_ok};
use data_streaming_service::{
    data_notification::{DataNotification, DataPayload},
    streaming_client::NotificationFeedback,
};
use futures::{channel::oneshot, FutureExt};
use mockall::{predicate::eq, Sequence};
use std::sync::Arc;

#[tokio::test]
async fn test_bootstrap_genesis_waypoint() {
    // Create a driver configuration with a genesis waypoint
    let driver_configuration = create_full_node_driver_configuration();

    // Create the mock streaming client
    let mock_streaming_client = create_mock_streaming_client();

    // Create the bootstrapper and verify it's not yet bootstrapped
    let mut bootstrapper = create_bootstrapper(driver_configuration, mock_streaming_client, true);
    assert!(!bootstrapper.is_bootstrapped());

    // Subscribe to a bootstrapped notification
    let (bootstrap_notification_sender, bootstrap_notification_receiver) = oneshot::channel();
    bootstrapper
        .subscribe_to_bootstrap_notifications(bootstrap_notification_sender)
        .unwrap();

    // Create a global data summary where only epoch 0 has ended
    let global_data_summary = create_global_summary(0);

    // Drive progress and verify we're now bootstrapped
    drive_progress(&mut bootstrapper, &global_data_summary, true)
        .await
        .unwrap();
    assert!(bootstrapper.is_bootstrapped());
    verify_bootstrap_notification(bootstrap_notification_receiver);

    // Drive progress again and verify we get an error (we're already bootstrapped!)
    let error = drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap_err();
    assert_matches!(error, Error::AlreadyBootstrapped(_));
}

#[tokio::test]
async fn test_bootstrap_immediate_notification() {
    // Create a driver configuration with a genesis waypoint
    let driver_configuration = create_full_node_driver_configuration();

    // Create the mock streaming client
    let mock_streaming_client = create_mock_streaming_client();

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper(driver_configuration, mock_streaming_client, true);

    // Create a global data summary where only epoch 0 has ended
    let global_data_summary = create_global_summary(0);

    // Drive progress and verify we're now bootstrapped
    drive_progress(&mut bootstrapper, &global_data_summary, true)
        .await
        .unwrap();
    assert!(bootstrapper.is_bootstrapped());

    // Subscribe to a bootstrapped notification and verify immediate notification
    let (bootstrap_notification_sender, bootstrap_notification_receiver) = oneshot::channel();
    bootstrapper
        .subscribe_to_bootstrap_notifications(bootstrap_notification_sender)
        .unwrap();
    verify_bootstrap_notification(bootstrap_notification_receiver);
}

#[tokio::test]
async fn test_bootstrap_no_notification() {
    // Create a driver configuration with a genesis waypoint
    let driver_configuration = create_full_node_driver_configuration();

    // Create the mock streaming client
    let mut mock_streaming_client = create_mock_streaming_client();
    let (_notification_sender, data_stream_listener) = create_data_stream_listener();
    mock_streaming_client
        .expect_get_all_epoch_ending_ledger_infos()
        .with(eq(1))
        .return_once(move |_| Ok(data_stream_listener));

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper(driver_configuration, mock_streaming_client, true);

    // Create a global data summary where epoch 0 and 1 have ended
    let global_data_summary = create_global_summary(1);

    // Subscribe to a bootstrapped notification
    let (bootstrap_notification_sender, bootstrap_notification_receiver) = oneshot::channel();
    bootstrapper
        .subscribe_to_bootstrap_notifications(bootstrap_notification_sender)
        .unwrap();

    // Drive progress
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();

    // Verify no notification
    assert_none!(bootstrap_notification_receiver.now_or_never());
}

#[tokio::test]
async fn test_critical_timeout() {
    // Create a driver configuration with a genesis waypoint and a stream timeout of 1 second
    let mut driver_configuration = create_full_node_driver_configuration();
    driver_configuration.config.max_stream_wait_time_ms = 1000;

    // Create the mock streaming client
    let mut mock_streaming_client = create_mock_streaming_client();
    let mut expectation_sequence = Sequence::new();
    let (_notification_sender_1, data_stream_listener_1) = create_data_stream_listener();
    let (_notification_sender_2, data_stream_listener_2) = create_data_stream_listener();
    for data_stream_listener in [data_stream_listener_1, data_stream_listener_2] {
        mock_streaming_client
            .expect_get_all_epoch_ending_ledger_infos()
            .times(1)
            .with(eq(1))
            .return_once(move |_| Ok(data_stream_listener))
            .in_sequence(&mut expectation_sequence);
    }

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper(driver_configuration, mock_streaming_client, true);

    // Create a global data summary where epoch 0 and 1 have ended
    let global_data_summary = create_global_summary(1);

    // Drive progress to initialize the epoch ending data stream
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();

    // Drive progress twice and verify we get non-critical timeouts
    for _ in 0..2 {
        let error = drive_progress(&mut bootstrapper, &global_data_summary, false)
            .await
            .unwrap_err();
        assert_matches!(error, Error::DataStreamNotificationTimeout(_));
    }

    // Drive progress again and verify we get a critical timeout
    let error = drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap_err();
    assert_matches!(error, Error::CriticalDataStreamTimeout(_));

    // Drive progress to initialize the epoch ending data stream again
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();

    // Drive progress again and verify we get a non-critical timeout
    let error = drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap_err();
    assert_matches!(error, Error::DataStreamNotificationTimeout(_));
}

#[tokio::test]
async fn test_data_stream_state_values() {
    // Create test data
    let notification_id = 50043;
    let highest_version = 10000;
    let highest_ledger_info = create_random_epoch_ending_ledger_info(highest_version, 1);

    // Create a driver configuration with a genesis waypoint and state syncing
    let mut driver_configuration = create_full_node_driver_configuration();
    driver_configuration.config.bootstrapping_mode = BootstrappingMode::DownloadLatestStates;

    // Create the mock streaming client
    let mut mock_streaming_client = create_mock_streaming_client();
    let mut expectation_sequence = Sequence::new();
    let (notification_sender_1, data_stream_listener_1) = create_data_stream_listener();
    let (_notification_sender_2, data_stream_listener_2) = create_data_stream_listener();
    for data_stream_listener in [data_stream_listener_1, data_stream_listener_2] {
        mock_streaming_client
            .expect_get_all_transaction_outputs()
            .times(1)
            .with(
                eq(highest_version),
                eq(highest_version),
                eq(highest_version),
            )
            .return_once(move |_, _, _| Ok(data_stream_listener))
            .in_sequence(&mut expectation_sequence);
    }
    mock_streaming_client
        .expect_terminate_stream_with_feedback()
        .with(
            eq(notification_id),
            eq(NotificationFeedback::InvalidPayloadData),
        )
        .return_const(Ok(()));

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper(driver_configuration, mock_streaming_client, true);

    // Insert an epoch ending ledger info into the verified states of the bootstrapper
    manipulate_verified_epoch_states(&mut bootstrapper, true, true, Some(highest_version));

    // Create a global data summary
    let mut global_data_summary = create_global_summary(1);
    global_data_summary.advertised_data.synced_ledger_infos = vec![highest_ledger_info.clone()];

    // Drive progress to initialize the state values stream
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();

    // Send an invalid output along the stream
    let data_notification = DataNotification {
        notification_id,
        data_payload: DataPayload::TransactionOutputsWithProof(create_output_list_with_proof()),
    };
    notification_sender_1.push((), data_notification).unwrap();

    // Drive progress again and ensure we get a verification error
    let error = drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap_err();
    assert_matches!(error, Error::VerificationError(_));

    // Drive progress to initialize the state value stream
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();
}

#[tokio::test]
async fn test_data_stream_transactions() {
    // Create test data
    let notification_id = 0;
    let highest_version = 9998765;
    let highest_ledger_info = create_random_epoch_ending_ledger_info(highest_version, 1);

    // Create a driver configuration with a genesis waypoint and transaction syncing
    let mut driver_configuration = create_full_node_driver_configuration();
    driver_configuration.config.bootstrapping_mode =
        BootstrappingMode::ExecuteTransactionsFromGenesis;

    // Create the mock streaming client
    let mut mock_streaming_client = create_mock_streaming_client();
    let mut expectation_sequence = Sequence::new();
    let (notification_sender_1, data_stream_listener_1) = create_data_stream_listener();
    let (_notification_sender_2, data_stream_listener_2) = create_data_stream_listener();
    for data_stream_listener in [data_stream_listener_1, data_stream_listener_2] {
        mock_streaming_client
            .expect_get_all_transactions()
            .times(1)
            .with(eq(1), eq(highest_version), eq(highest_version), eq(false))
            .return_once(move |_, _, _, _| Ok(data_stream_listener))
            .in_sequence(&mut expectation_sequence);
    }
    mock_streaming_client
        .expect_terminate_stream_with_feedback()
        .with(
            eq(notification_id),
            eq(NotificationFeedback::InvalidPayloadData),
        )
        .return_const(Ok(()));

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper(driver_configuration, mock_streaming_client, true);

    // Insert an epoch ending ledger info into the verified states of the bootstrapper
    manipulate_verified_epoch_states(&mut bootstrapper, true, true, Some(highest_version));

    // Create a global data summary
    let mut global_data_summary = create_global_summary(1);
    global_data_summary.advertised_data.synced_ledger_infos = vec![highest_ledger_info.clone()];

    // Drive progress to initialize the transaction output stream
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();

    // Send an invalid output along the stream
    let data_notification = DataNotification {
        notification_id,
        data_payload: DataPayload::TransactionsWithProof(create_transaction_list_with_proof()),
    };
    notification_sender_1.push((), data_notification).unwrap();

    // Drive progress again and ensure we get a verification error
    let error = drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap_err();
    assert_matches!(error, Error::VerificationError(_));

    // Drive progress to initialize the transaction output stream
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();
}

#[tokio::test]
async fn test_data_stream_transaction_outputs() {
    // Create test data
    let notification_id = 1235;
    let highest_version = 45;
    let highest_ledger_info = create_random_epoch_ending_ledger_info(highest_version, 1);

    // Create a driver configuration with a genesis waypoint and output syncing
    let mut driver_configuration = create_full_node_driver_configuration();
    driver_configuration.config.bootstrapping_mode =
        BootstrappingMode::ApplyTransactionOutputsFromGenesis;

    // Create the mock streaming client
    let mut mock_streaming_client = create_mock_streaming_client();
    let mut expectation_sequence = Sequence::new();
    let (notification_sender_1, data_stream_listener_1) = create_data_stream_listener();
    let (_notification_sender_2, data_stream_listener_2) = create_data_stream_listener();
    for data_stream_listener in [data_stream_listener_1, data_stream_listener_2] {
        mock_streaming_client
            .expect_get_all_transaction_outputs()
            .times(1)
            .with(eq(1), eq(highest_version), eq(highest_version))
            .return_once(move |_, _, _| Ok(data_stream_listener))
            .in_sequence(&mut expectation_sequence);
    }
    mock_streaming_client
        .expect_terminate_stream_with_feedback()
        .with(
            eq(notification_id),
            eq(NotificationFeedback::EmptyPayloadData),
        )
        .return_const(Ok(()));

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper(driver_configuration, mock_streaming_client, true);

    // Insert an epoch ending ledger info into the verified states of the bootstrapper
    manipulate_verified_epoch_states(&mut bootstrapper, true, true, Some(highest_version));

    // Create a global data summary
    let mut global_data_summary = create_global_summary(1);
    global_data_summary.advertised_data.synced_ledger_infos = vec![highest_ledger_info.clone()];

    // Drive progress to initialize the transaction output stream
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();

    // Send an invalid output along the stream
    let data_notification = DataNotification {
        notification_id,
        data_payload: DataPayload::TransactionOutputsWithProof(
            TransactionOutputListWithProof::new_empty(),
        ),
    };
    notification_sender_1.push((), data_notification).unwrap();

    // Drive progress again and ensure we get a verification error
    let error = drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap_err();
    assert_matches!(error, Error::VerificationError(_));

    // Drive progress to initialize the transaction output stream
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();
}

#[tokio::test]
async fn test_fetch_epoch_ending_ledger_infos() {
    // Create a driver configuration with a genesis waypoint and a stream timeout of 1 second
    let mut driver_configuration = create_full_node_driver_configuration();
    driver_configuration.config.max_stream_wait_time_ms = 1000;

    // Create the mock streaming client
    let mut mock_streaming_client = create_mock_streaming_client();
    let (_notification_sender, data_stream_listener) = create_data_stream_listener();
    mock_streaming_client
        .expect_get_all_epoch_ending_ledger_infos()
        .with(eq(1))
        .return_once(move |_| Ok(data_stream_listener));

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper(driver_configuration, mock_streaming_client, true);

    // Set the waypoint as already having been verified (but no fetched ledger infos)
    manipulate_verified_epoch_states(&mut bootstrapper, false, true, None);

    // Create a global data summary where epoch 0 and 1 have ended
    let global_data_summary = create_global_summary(1);

    // Drive progress to initialize the epoch ending data stream
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();

    // Drive progress and verify we get a timeout error as we're still waiting
    // for epoch ending ledger infos to epoch skip.
    let error = drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap_err();
    assert_matches!(error, Error::DataStreamNotificationTimeout(_));
}

#[tokio::test]
async fn test_snapshot_sync_epoch_change() {
    // Create test data
    let synced_version = GENESIS_TRANSACTION_VERSION; // Genesis is the highest synced
    let target_version = 1000;
    let highest_version = 5000;
    let last_persisted_index = 1030405;
    let target_ledger_info = create_random_epoch_ending_ledger_info(target_version, 1);
    let highest_ledger_info = create_random_epoch_ending_ledger_info(highest_version, 2);

    // Create a driver configuration with a genesis waypoint and state syncing
    let mut driver_configuration = create_full_node_driver_configuration();
    driver_configuration.config.bootstrapping_mode = BootstrappingMode::DownloadLatestStates;

    // Create the mock streaming client
    let mut mock_streaming_client = create_mock_streaming_client();
    let (_notification_sender_1, data_stream_listener_1) = create_data_stream_listener();
    mock_streaming_client
        .expect_get_all_state_values()
        .times(1)
        .with(eq(target_version), eq(Some(last_persisted_index)))
        .return_once(move |_, _| Ok(data_stream_listener_1));

    // Create the mock metadata storage
    let mut metadata_storage = MockMetadataStorage::new();
    let target_ledger_info_clone = target_ledger_info.clone();
    let last_persisted_index_clone = last_persisted_index;
    metadata_storage
        .expect_previous_snapshot_sync_target()
        .returning(move || Ok(Some(target_ledger_info_clone.clone())));
    metadata_storage
        .expect_is_snapshot_sync_complete()
        .returning(|_| Ok(false));
    metadata_storage
        .expect_get_last_persisted_state_value_index()
        .returning(move |_| Ok(last_persisted_index_clone));

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper_with_storage(
        driver_configuration,
        mock_streaming_client,
        metadata_storage,
        synced_version,
        true,
    );

    // Insert an epoch ending ledger info into the verified states of the bootstrapper
    manipulate_verified_epoch_states(&mut bootstrapper, true, true, Some(highest_version));

    // Manually insert a transaction output to sync
    bootstrapper
        .get_state_value_syncer()
        .set_transaction_output_to_sync(create_output_list_with_proof());

    // Create a global data summary
    let mut global_data_summary = create_global_summary(1);
    global_data_summary.advertised_data.synced_ledger_infos = vec![highest_ledger_info.clone()];

    // Drive progress to start the state value stream
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();
}

#[tokio::test]
async fn test_snapshot_sync_existing_state() {
    // Create test data
    let synced_version = GENESIS_TRANSACTION_VERSION; // Genesis is the highest synced
    let highest_version = 1000000;
    let highest_ledger_info = create_random_epoch_ending_ledger_info(highest_version, 1);
    let last_persisted_index = 4567;

    // Create a driver configuration with a genesis waypoint and state syncing
    let mut driver_configuration = create_full_node_driver_configuration();
    driver_configuration.config.bootstrapping_mode = BootstrappingMode::DownloadLatestStates;

    // Create the mock streaming client
    let mut mock_streaming_client = create_mock_streaming_client();
    let mut expectation_sequence = Sequence::new();
    let (notification_sender_1, data_stream_listener_1) = create_data_stream_listener();
    let (_notification_sender_2, data_stream_listener_2) = create_data_stream_listener();
    mock_streaming_client
        .expect_get_all_state_values()
        .times(1)
        .with(eq(highest_version), eq(Some(last_persisted_index)))
        .return_once(move |_, _| Ok(data_stream_listener_1))
        .in_sequence(&mut expectation_sequence);
    let notification_id = 100;
    mock_streaming_client
        .expect_terminate_stream_with_feedback()
        .times(1)
        .with(
            eq(notification_id),
            eq(NotificationFeedback::InvalidPayloadData),
        )
        .return_const(Ok(()))
        .in_sequence(&mut expectation_sequence);
    mock_streaming_client
        .expect_get_all_state_values()
        .times(1)
        .with(eq(highest_version), eq(Some(last_persisted_index)))
        .return_once(move |_, _| Ok(data_stream_listener_2))
        .in_sequence(&mut expectation_sequence);

    // Create the mock metadata storage
    let mut metadata_storage = MockMetadataStorage::new();
    let highest_ledger_info_clone = highest_ledger_info.clone();
    let last_persisted_index_clone = last_persisted_index;
    metadata_storage
        .expect_previous_snapshot_sync_target()
        .returning(move || Ok(Some(highest_ledger_info_clone.clone())));
    metadata_storage
        .expect_is_snapshot_sync_complete()
        .returning(|_| Ok(false));
    metadata_storage
        .expect_get_last_persisted_state_value_index()
        .returning(move |_| Ok(last_persisted_index_clone));

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper_with_storage(
        driver_configuration,
        mock_streaming_client,
        metadata_storage,
        synced_version,
        true,
    );

    // Insert an epoch ending ledger info into the verified states of the bootstrapper
    manipulate_verified_epoch_states(&mut bootstrapper, true, true, Some(highest_version));

    // Manually insert a transaction output to sync
    bootstrapper
        .get_state_value_syncer()
        .set_transaction_output_to_sync(create_output_list_with_proof());

    // Create a global data summary
    let mut global_data_summary = create_global_summary(1);
    global_data_summary.advertised_data.synced_ledger_infos = vec![highest_ledger_info.clone()];

    // Drive progress to start the state value stream
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();

    // Send an invalid notification (incorrect data type)
    let data_notification = DataNotification {
        notification_id,
        data_payload: DataPayload::TransactionOutputsWithProof(create_output_list_with_proof()),
    };
    notification_sender_1.push((), data_notification).unwrap();

    // Drive progress again and ensure we get an invalid payload error
    let error = drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap_err();
    assert_matches!(error, Error::InvalidPayload(_));

    // Drive progress to start the state value stream again
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();
}

#[tokio::test]
async fn test_snapshot_sync_fresh_state() {
    // Create test data
    let synced_version = GENESIS_TRANSACTION_VERSION; // Genesis is the highest synced
    let highest_version = 1000;
    let highest_ledger_info = create_random_epoch_ending_ledger_info(highest_version, 1);

    // Create a driver configuration with a genesis waypoint and state syncing
    let mut driver_configuration = create_full_node_driver_configuration();
    driver_configuration.config.bootstrapping_mode = BootstrappingMode::DownloadLatestStates;

    // Create the mock streaming client
    let mut mock_streaming_client = create_mock_streaming_client();
    let (_notification_sender_1, data_stream_listener_1) = create_data_stream_listener();
    mock_streaming_client
        .expect_get_all_state_values()
        .times(1)
        .with(eq(highest_version), eq(Some(0)))
        .return_once(move |_, _| Ok(data_stream_listener_1));

    // Create the mock metadata storage
    let mut metadata_storage = MockMetadataStorage::new();
    metadata_storage
        .expect_previous_snapshot_sync_target()
        .returning(move || Ok(None));

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper_with_storage(
        driver_configuration,
        mock_streaming_client,
        metadata_storage,
        synced_version,
        true,
    );

    // Insert an epoch ending ledger info into the verified states of the bootstrapper
    manipulate_verified_epoch_states(&mut bootstrapper, true, true, Some(highest_version));

    // Manually insert a transaction output to sync
    bootstrapper
        .get_state_value_syncer()
        .set_transaction_output_to_sync(create_output_list_with_proof());

    // Create a global data summary
    let mut global_data_summary = create_global_summary(1);
    global_data_summary.advertised_data.synced_ledger_infos = vec![highest_ledger_info.clone()];

    // Drive progress to start the state value stream
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();
}

#[tokio::test]
#[should_panic(
    expected = "The snapshot sync for the target was marked as complete but the highest synced version is genesis!"
)]
async fn test_snapshot_sync_invalid_state() {
    // Create test data
    let synced_version = GENESIS_TRANSACTION_VERSION; // Genesis is the highest synced
    let highest_version = 1000000;
    let highest_ledger_info = create_random_epoch_ending_ledger_info(highest_version, 1);

    // Create a driver configuration with a genesis waypoint and state syncing
    let mut driver_configuration = create_full_node_driver_configuration();
    driver_configuration.config.bootstrapping_mode = BootstrappingMode::DownloadLatestStates;

    // Create the mock streaming client
    let mock_streaming_client = create_mock_streaming_client();

    // Create the mock metadata storage
    let mut metadata_storage = MockMetadataStorage::new();
    let highest_ledger_info_clone = highest_ledger_info.clone();
    metadata_storage
        .expect_previous_snapshot_sync_target()
        .return_once(move || Ok(Some(highest_ledger_info_clone)));
    metadata_storage
        .expect_is_snapshot_sync_complete()
        .returning(|_| Ok(true));

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper_with_storage(
        driver_configuration,
        mock_streaming_client,
        metadata_storage,
        synced_version,
        true,
    );

    // Insert an epoch ending ledger info into the verified states of the bootstrapper
    manipulate_verified_epoch_states(&mut bootstrapper, true, true, Some(highest_version));

    // Create a global data summary
    let mut global_data_summary = create_global_summary(1);
    global_data_summary.advertised_data.synced_ledger_infos = vec![highest_ledger_info.clone()];

    // Drive progress and verify that the bootstrapper panics (due to invalid state)
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();
}

#[tokio::test]
async fn test_snapshot_sync_lag() {
    // Create test data
    let num_versions_behind = 1000;
    let highest_version = 1000000;
    let synced_version = highest_version - num_versions_behind;
    let highest_ledger_info = create_random_epoch_ending_ledger_info(highest_version, 1);

    // Create a driver configuration with a genesis waypoint and state syncing
    let mut driver_configuration = create_full_node_driver_configuration();
    driver_configuration.config.bootstrapping_mode = BootstrappingMode::DownloadLatestStates;
    driver_configuration
        .config
        .num_versions_to_skip_snapshot_sync = num_versions_behind + 1;

    // Create the mock streaming client
    let mock_streaming_client = create_mock_streaming_client();

    // Create the mock metadata storage
    let mut metadata_storage = MockMetadataStorage::new();
    metadata_storage
        .expect_previous_snapshot_sync_target()
        .returning(|| Ok(None));

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper_with_storage(
        driver_configuration,
        mock_streaming_client,
        metadata_storage,
        synced_version,
        true,
    );

    // Insert an epoch ending ledger info into the verified states of the bootstrapper
    manipulate_verified_epoch_states(&mut bootstrapper, true, true, Some(highest_version));

    // Create a global data summary
    let mut global_data_summary = create_global_summary(1);
    global_data_summary.advertised_data.synced_ledger_infos = vec![highest_ledger_info.clone()];

    // Drive progress to mark bootstrapping complete (we're within the snapshot sync lag)
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();

    // Verify the bootstrapper has completed
    assert!(bootstrapper.is_bootstrapped());
}

#[tokio::test]
#[should_panic(
    expected = "Snapshot syncing is currently unsupported for nodes with existing state!"
)]
async fn test_snapshot_sync_lag_panic() {
    // Create test data
    let num_versions_behind = 10000;
    let highest_version = 1000000;
    let synced_version = highest_version - num_versions_behind;
    let highest_ledger_info = create_random_epoch_ending_ledger_info(highest_version, 1);

    // Create a driver configuration with a genesis waypoint and state syncing
    let mut driver_configuration = create_full_node_driver_configuration();
    driver_configuration.config.bootstrapping_mode = BootstrappingMode::DownloadLatestStates;
    driver_configuration
        .config
        .num_versions_to_skip_snapshot_sync = num_versions_behind;

    // Create the mock streaming client
    let mock_streaming_client = create_mock_streaming_client();

    // Create the mock metadata storage
    let mut metadata_storage = MockMetadataStorage::new();
    metadata_storage
        .expect_previous_snapshot_sync_target()
        .returning(|| Ok(None));

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper_with_storage(
        driver_configuration,
        mock_streaming_client,
        metadata_storage,
        synced_version,
        true,
    );

    // Insert an epoch ending ledger info into the verified states of the bootstrapper
    manipulate_verified_epoch_states(&mut bootstrapper, true, true, Some(highest_version));

    // Create a global data summary
    let mut global_data_summary = create_global_summary(1);
    global_data_summary.advertised_data.synced_ledger_infos = vec![highest_ledger_info.clone()];

    // Drive progress to panic the node (we're too many versions behind)
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();
}

#[tokio::test]
async fn test_waypoint_mismatch() {
    // Create a waypoint
    let waypoint_version = 1;
    let waypoint_epoch = 1;
    let waypoint = create_random_epoch_ending_ledger_info(waypoint_version, waypoint_epoch);

    // Create a driver configuration with the specified waypoint
    let mut driver_configuration = create_full_node_driver_configuration();
    driver_configuration.waypoint = Waypoint::new_any(waypoint.ledger_info());

    // Create the mock streaming client
    let mut mock_streaming_client = create_mock_streaming_client();
    let (notification_sender, data_stream_listener) = create_data_stream_listener();
    mock_streaming_client
        .expect_get_all_epoch_ending_ledger_infos()
        .with(eq(1))
        .return_once(move |_| Ok(data_stream_listener));
    let notification_id = 100;
    mock_streaming_client
        .expect_terminate_stream_with_feedback()
        .with(
            eq(notification_id),
            eq(NotificationFeedback::PayloadProofFailed),
        )
        .return_const(Ok(()));

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper(driver_configuration, mock_streaming_client, true);

    // Create a global data summary up to the waypoint
    let mut global_data_summary = create_global_summary(waypoint_epoch);
    global_data_summary.advertised_data.synced_ledger_infos = vec![waypoint.clone()];

    // Drive progress to initialize the epoch ending data stream
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();

    // Send an invalid epoch ending payload along the stream (invalid waypoint hash)
    let invalid_ledger_info = vec![create_random_epoch_ending_ledger_info(
        waypoint_version,
        waypoint_epoch,
    )];
    let data_notification = DataNotification {
        notification_id,
        data_payload: DataPayload::EpochEndingLedgerInfos(invalid_ledger_info),
    };
    notification_sender.push((), data_notification).unwrap();

    // Drive progress again and ensure we get a verification error
    let error = drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap_err();
    assert_matches!(error, Error::VerificationError(_));
}

#[tokio::test]
async fn test_waypoint_must_be_verified() {
    // Create a driver configuration with a genesis waypoint and a stream timeout of 1 second
    let mut driver_configuration = create_full_node_driver_configuration();
    driver_configuration.config.max_stream_wait_time_ms = 1000;

    // Create the mock streaming client
    let mut mock_streaming_client = create_mock_streaming_client();
    let (_notification_sender, data_stream_listener) = create_data_stream_listener();
    mock_streaming_client
        .expect_get_all_epoch_ending_ledger_infos()
        .with(eq(1))
        .return_once(move |_| Ok(data_stream_listener));

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper(driver_configuration, mock_streaming_client, true);

    // Set fetched ledger infos to true but the waypoint is still not verified
    manipulate_verified_epoch_states(&mut bootstrapper, true, false, None);

    // Create a global data summary where epoch 0 and 1 have ended
    let global_data_summary = create_global_summary(1);

    // Drive progress to initialize the epoch ending data stream
    drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap();

    // Drive progress again and verify we get a timeout error as we're still waiting
    // for epoch ending ledger infos to verify the waypoint.
    let error = drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap_err();
    assert_matches!(error, Error::DataStreamNotificationTimeout(_));
}

#[tokio::test]
async fn test_waypoint_satisfiable() {
    // Create a driver configuration with a non-genesis waypoint
    let mut driver_configuration = create_full_node_driver_configuration();
    let waypoint = create_random_epoch_ending_ledger_info(10, 1);
    driver_configuration.waypoint = Waypoint::new_any(waypoint.ledger_info());

    // Create the mock streaming client
    let mock_streaming_client = create_mock_streaming_client();

    // Create the bootstrapper
    let mut bootstrapper = create_bootstrapper(driver_configuration, mock_streaming_client, true);

    // Create an empty global data summary
    let mut global_data_summary = GlobalDataSummary::empty();

    // Drive progress and verify that no advertised data is found
    let error = drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap_err();
    assert_matches!(error, Error::AdvertisedDataError(_));

    // Update the global data summary with advertised data lower than the waypoint
    global_data_summary.advertised_data.synced_ledger_infos =
        vec![create_random_epoch_ending_ledger_info(9, 5)];

    // Verify the waypoint is not satisfiable
    let error = drive_progress(&mut bootstrapper, &global_data_summary, false)
        .await
        .unwrap_err();
    assert_matches!(error, Error::AdvertisedDataError(_));
}

/// Creates a bootstrapper for testing
fn create_bootstrapper(
    driver_configuration: DriverConfiguration,
    mock_streaming_client: MockStreamingClient,
    expect_reset_executor: bool,
) -> Bootstrapper<MockMetadataStorage, MockStorageSynchronizer, MockStreamingClient> {
    // Initialize the logger for tests
    aptos_logger::Logger::init_for_testing();

    // Create the mock storage synchronizer
    let mock_storage_synchronizer = create_ready_storage_synchronizer(expect_reset_executor);

    // Create the mock metadata storage
    let mut metadata_storage = MockMetadataStorage::new();
    metadata_storage
        .expect_previous_snapshot_sync_target()
        .returning(|| Ok(None));

    // Create the mock db reader with only genesis loaded
    let mut mock_database_reader = create_mock_db_reader();
    mock_database_reader
        .expect_get_latest_epoch_state()
        .returning(|| Ok(create_empty_epoch_state()));
    mock_database_reader
        .expect_get_latest_ledger_info()
        .returning(|| Ok(create_epoch_ending_ledger_info()));
    mock_database_reader
        .expect_get_latest_transaction_info_option()
        .returning(|| Ok(Some((0, create_transaction_info()))));

    Bootstrapper::new(
        driver_configuration,
        metadata_storage,
        mock_streaming_client,
        Arc::new(mock_database_reader),
        mock_storage_synchronizer,
    )
}

/// Creates a bootstrapper for testing with a mock metadata storage
fn create_bootstrapper_with_storage(
    driver_configuration: DriverConfiguration,
    mock_streaming_client: MockStreamingClient,
    mock_metadata_storage: MockMetadataStorage,
    latest_synced_version: Version,
    expect_reset_executor: bool,
) -> Bootstrapper<MockMetadataStorage, MockStorageSynchronizer, MockStreamingClient> {
    // Initialize the logger for tests
    aptos_logger::Logger::init_for_testing();

    // Create the mock storage synchronizer
    let mock_storage_synchronizer = create_ready_storage_synchronizer(expect_reset_executor);

    // Create the mock db reader with only genesis loaded
    let mut mock_database_reader = create_mock_db_reader();
    mock_database_reader
        .expect_get_latest_epoch_state()
        .returning(|| Ok(create_empty_epoch_state()));
    mock_database_reader
        .expect_get_latest_ledger_info()
        .returning(|| Ok(create_epoch_ending_ledger_info()));
    mock_database_reader
        .expect_get_latest_transaction_info_option()
        .returning(move || Ok(Some((latest_synced_version, create_transaction_info()))));

    Bootstrapper::new(
        driver_configuration,
        mock_metadata_storage,
        mock_streaming_client,
        Arc::new(mock_database_reader),
        mock_storage_synchronizer,
    )
}

/// Drives progress for the given bootstrapper. If `until_bootstrapped`
/// is true this method will continue to drive the bootstrapper until
/// bootstrapping is complete.
async fn drive_progress(
    bootstrapper: &mut Bootstrapper<
        MockMetadataStorage,
        MockStorageSynchronizer,
        MockStreamingClient,
    >,
    global_data_summary: &GlobalDataSummary,
    until_bootstrapped: bool,
) -> Result<(), Error> {
    loop {
        // Attempt to drive progress
        bootstrapper.drive_progress(global_data_summary).await?;

        // Return early if we should only drive progress once or if we've already bootstrapped
        if !until_bootstrapped || bootstrapper.is_bootstrapped() {
            return Ok(());
        }
    }
}

/// Manipulates the internal state of the verified epoch states used by
/// the given bootstrapper and inserts a verified epoch ending ledger
/// info at the specified `highest_version_to_insert` (if provided).
fn manipulate_verified_epoch_states(
    bootstrapper: &mut Bootstrapper<
        MockMetadataStorage,
        MockStorageSynchronizer,
        MockStreamingClient,
    >,
    fetched_epochs: bool,
    verified_waypoint: bool,
    highest_version_to_insert: Option<Version>,
) {
    let verified_epoch_states = bootstrapper.get_verified_epoch_states();
    if fetched_epochs {
        verified_epoch_states.set_fetched_epoch_ending_ledger_infos();
    }
    if verified_waypoint {
        verified_epoch_states.set_verified_waypoint();
    }
    if let Some(highest_version) = highest_version_to_insert {
        let epoch_ending_ledger_info = create_random_epoch_ending_ledger_info(highest_version, 0);
        let waypoint_ledger_info = create_random_epoch_ending_ledger_info(0, 1);
        verified_epoch_states
            .verify_epoch_ending_ledger_info(
                &epoch_ending_ledger_info,
                &Waypoint::new_any(waypoint_ledger_info.ledger_info()),
            )
            .unwrap();
    }
}

/// Verifies that the receiver gets a successful notification
fn verify_bootstrap_notification(notification_receiver: oneshot::Receiver<Result<(), Error>>) {
    assert_ok!(notification_receiver.now_or_never().unwrap().unwrap());
}