|
| 1 | +mod extensions; |
| 2 | + |
| 3 | +use std::sync::Arc; |
| 4 | + |
| 5 | +use mithril_client::{ |
| 6 | + aggregator_client::AggregatorRequest, |
| 7 | + cardano_database_client::{DownloadUnpackOptions, ImmutableFileRange}, |
| 8 | + feedback::SlogFeedbackReceiver, |
| 9 | + ClientBuilder, MessageBuilder, |
| 10 | +}; |
| 11 | +use mithril_common::digesters::{CardanoImmutableDigester, DummyCardanoDbBuilder}; |
| 12 | + |
| 13 | +use crate::extensions::fake::{FakeAggregator, FakeCertificateVerifier}; |
| 14 | + |
| 15 | +#[tokio::test] |
| 16 | +async fn cardano_db_snapshot_list_get_download_verify() { |
| 17 | + let work_dir = extensions::get_test_dir("cardano_db_snapshot_list_get_download_verify"); |
| 18 | + let genesis_verification_key = |
| 19 | + mithril_common::test_utils::fake_keys::genesis_verification_key()[0]; |
| 20 | + let cardano_db_snapshot_hash = "cardano_db_snapshot_hash"; |
| 21 | + let certificate_hash = "certificate_hash"; |
| 22 | + let cardano_db = DummyCardanoDbBuilder::new("cardano_db_snapshot_list_get_download_verify_db") |
| 23 | + .with_immutables(&[1, 2, 3, 4]) |
| 24 | + .append_immutable_trio() |
| 25 | + .with_ledger_files(&["blocks-0.dat", "blocks-1.dat"]) |
| 26 | + .with_volatile_files(&["437", "537", "637"]) |
| 27 | + .build(); |
| 28 | + let digester = |
| 29 | + CardanoImmutableDigester::new("whatever".to_string(), None, extensions::test_logger()); |
| 30 | + let range = 1..=4; |
| 31 | + let fake_aggregator = FakeAggregator::new(); |
| 32 | + let test_http_server = fake_aggregator |
| 33 | + .spawn_with_cardano_db_snapshot( |
| 34 | + cardano_db_snapshot_hash, |
| 35 | + certificate_hash, |
| 36 | + &cardano_db, |
| 37 | + &work_dir, |
| 38 | + digester, |
| 39 | + range, |
| 40 | + ) |
| 41 | + .await; |
| 42 | + let client = ClientBuilder::aggregator(&test_http_server.url(), genesis_verification_key) |
| 43 | + .with_certificate_verifier(FakeCertificateVerifier::build_that_validate_any_certificate()) |
| 44 | + .add_feedback_receiver(Arc::new(SlogFeedbackReceiver::new( |
| 45 | + extensions::test_logger(), |
| 46 | + ))) |
| 47 | + .build() |
| 48 | + .expect("Should be able to create a Client"); |
| 49 | + |
| 50 | + let cardano_db_snapshots = client |
| 51 | + .cardano_database() |
| 52 | + .list() |
| 53 | + .await |
| 54 | + .expect("List CardanoDatabaseSnapshot should not fail"); |
| 55 | + |
| 56 | + let last_hash = cardano_db_snapshots.first().unwrap().hash.as_ref(); |
| 57 | + |
| 58 | + let cardano_db_snapshot = client |
| 59 | + .cardano_database() |
| 60 | + .get(last_hash) |
| 61 | + .await |
| 62 | + .expect("Get CardanoDatabaseSnapshot should not fail ") |
| 63 | + .unwrap_or_else(|| panic!("A CardanoDatabaseSnapshot should exist for hash '{last_hash}'")); |
| 64 | + |
| 65 | + let certificate = client |
| 66 | + .certificate() |
| 67 | + .verify_chain(&cardano_db_snapshot.certificate_hash) |
| 68 | + .await |
| 69 | + .expect("Validating the chain should not fail"); |
| 70 | + |
| 71 | + let unpacked_dir = work_dir.join("unpack"); |
| 72 | + std::fs::create_dir(&unpacked_dir).unwrap(); |
| 73 | + let immutable_file_range = ImmutableFileRange::From(2); |
| 74 | + let download_unpack_options = DownloadUnpackOptions { |
| 75 | + include_ancillary: true, |
| 76 | + ..DownloadUnpackOptions::default() |
| 77 | + }; |
| 78 | + |
| 79 | + client |
| 80 | + .cardano_database() |
| 81 | + .download_unpack( |
| 82 | + &cardano_db_snapshot, |
| 83 | + &immutable_file_range, |
| 84 | + &unpacked_dir, |
| 85 | + download_unpack_options, |
| 86 | + ) |
| 87 | + .await |
| 88 | + .expect("download/unpack cardano db snapshot should not fail"); |
| 89 | + |
| 90 | + let full_restoration = immutable_file_range == ImmutableFileRange::Full; |
| 91 | + let include_ancillary = download_unpack_options.include_ancillary; |
| 92 | + let number_of_immutable_files_restored = |
| 93 | + immutable_file_range.length(cardano_db_snapshot.beacon.immutable_file_number); |
| 94 | + client |
| 95 | + .cardano_database() |
| 96 | + .add_statistics( |
| 97 | + full_restoration, |
| 98 | + include_ancillary, |
| 99 | + number_of_immutable_files_restored, |
| 100 | + ) |
| 101 | + .await |
| 102 | + .expect("add_statistics should not fail"); |
| 103 | + let last_api_calls = fake_aggregator.get_latest_calls(3).await; |
| 104 | + assert!(last_api_calls.contains(&format!( |
| 105 | + "/{}", |
| 106 | + AggregatorRequest::IncrementCardanoDatabaseAncillaryStatistic.route() |
| 107 | + ))); |
| 108 | + assert!(last_api_calls.contains(&format!( |
| 109 | + "/{}", |
| 110 | + AggregatorRequest::IncrementCardanoDatabasePartialRestorationStatistic.route() |
| 111 | + ))); |
| 112 | + assert!(last_api_calls.contains(&format!( |
| 113 | + "/{}", |
| 114 | + AggregatorRequest::IncrementCardanoDatabaseImmutablesRestoredStatistic { |
| 115 | + number_of_immutables: number_of_immutable_files_restored.to_string() |
| 116 | + } |
| 117 | + .route() |
| 118 | + ))); |
| 119 | + |
| 120 | + let merkle_proof = client |
| 121 | + .cardano_database() |
| 122 | + .compute_merkle_proof( |
| 123 | + &certificate, |
| 124 | + &cardano_db_snapshot, |
| 125 | + &immutable_file_range, |
| 126 | + &unpacked_dir, |
| 127 | + ) |
| 128 | + .await |
| 129 | + .expect("Computing merkle proof should not fail"); |
| 130 | + merkle_proof.verify().expect("Merkle proof should be valid"); |
| 131 | + |
| 132 | + let message = MessageBuilder::new() |
| 133 | + .compute_cardano_database_message(&certificate, &merkle_proof) |
| 134 | + .await |
| 135 | + .expect("Computing cardano database snapshot message should not fail"); |
| 136 | + |
| 137 | + assert!( |
| 138 | + certificate.match_message(&message), |
| 139 | + "Certificate and message did not match:\ncertificate_message: '{}'\n computed_message: '{}'", |
| 140 | + certificate.signed_message, |
| 141 | + message.compute_hash() |
| 142 | + ); |
| 143 | +} |
0 commit comments