Skip to content

Commit 8695cc0

Browse files
authored
Merge pull request #2344 from input-output-hk/dlachaume/2328/add-integration-test-incremental-db-client
Test: add integration test for incremental Cardano database in `mithril-client`
2 parents a0e1d89 + c9b5787 commit 8695cc0

File tree

9 files changed

+522
-6
lines changed

9 files changed

+522
-6
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-client/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-client"
3-
version = "0.11.8"
3+
version = "0.11.9"
44
description = "Mithril client library"
55
authors = { workspace = true }
66
edition = { workspace = true }
@@ -23,6 +23,11 @@ name = "cardano_transaction_proof"
2323
path = "tests/cardano_transaction_proof.rs"
2424
required-features = ["unstable"]
2525

26+
[[test]]
27+
name = "cardano_db_snapshot_list_get_download_verify"
28+
path = "tests/cardano_db_snapshot_list_get_download_verify.rs"
29+
required-features = ["unstable", "fs"]
30+
2631
[dependencies]
2732
anyhow = "1.0.95"
2833
async-recursion = "1.1.1"
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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

Comments
 (0)