Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mithril-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-client"
version = "0.12.30"
version = "0.12.31"
description = "Mithril client library"
authors = { workspace = true }
edition = { workspace = true }
Expand Down
24 changes: 23 additions & 1 deletion mithril-client/src/cardano_database_client/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::feedback::FeedbackSender;
#[cfg(feature = "fs")]
use crate::file_downloader::FileDownloader;
#[cfg(feature = "fs")]
use crate::utils::AncillaryVerifier;
use crate::utils::{AncillaryVerifier, TempDirectoryProvider};
use crate::{CardanoDatabaseSnapshot, CardanoDatabaseSnapshotListItem, MithrilResult};

use super::fetch::InternalArtifactRetriever;
Expand Down Expand Up @@ -50,6 +50,7 @@ impl CardanoDatabaseClient {
#[cfg(feature = "fs")] http_file_downloader: Arc<dyn FileDownloader>,
#[cfg(feature = "fs")] ancillary_verifier: Option<Arc<AncillaryVerifier>>,
#[cfg(feature = "fs")] feedback_sender: FeedbackSender,
#[cfg(feature = "fs")] temp_directory_provider: Arc<dyn TempDirectoryProvider>,
#[cfg(feature = "fs")] logger: Logger,
) -> Self {
#[cfg(feature = "fs")]
Expand All @@ -67,6 +68,7 @@ impl CardanoDatabaseClient {
#[cfg(feature = "fs")]
artifact_prover: InternalArtifactProver::new(
http_file_downloader.clone(),
temp_directory_provider.clone(),
logger.clone(),
),
statistics_sender: InternalStatisticsSender::new(aggregator_client.clone()),
Expand Down Expand Up @@ -172,6 +174,8 @@ pub(crate) mod test_dependency_injector {
#[cfg(feature = "fs")]
use crate::file_downloader::{FileDownloader, MockFileDownloaderBuilder};
#[cfg(feature = "fs")]
use crate::utils::TimestampTempDirectoryProvider;
#[cfg(feature = "fs")]
use crate::{feedback::FeedbackReceiver, test_utils::TestLogger};

/// Dependency injector for `CardanoDatabaseClient` for testing purposes.
Expand All @@ -184,6 +188,8 @@ pub(crate) mod test_dependency_injector {
#[cfg(feature = "fs")]
feedback_receivers: Vec<Arc<dyn FeedbackReceiver>>,
#[cfg(feature = "fs")]
temp_directory_provider: Arc<dyn TempDirectoryProvider>,
#[cfg(feature = "fs")]
logger: Logger,
}

Expand All @@ -204,6 +210,10 @@ pub(crate) mod test_dependency_injector {
#[cfg(feature = "fs")]
feedback_receivers: vec![],
#[cfg(feature = "fs")]
temp_directory_provider: Arc::new(TimestampTempDirectoryProvider::new(
"cardano_database_client_test",
)),
#[cfg(feature = "fs")]
logger: TestLogger::stdout(),
}
}
Expand Down Expand Up @@ -259,13 +269,25 @@ pub(crate) mod test_dependency_injector {
}
}

#[cfg(feature = "fs")]
pub(crate) fn with_temp_directory_provider(
self,
temp_directory_provider: Arc<dyn TempDirectoryProvider>,
) -> Self {
Self {
temp_directory_provider,
..self
}
}

#[cfg(feature = "fs")]
pub(crate) fn build_cardano_database_client(self) -> CardanoDatabaseClient {
CardanoDatabaseClient::new(
Arc::new(self.aggregator_client),
self.http_file_downloader,
self.ancillary_verifier,
FeedbackSender::new(&self.feedback_receivers),
self.temp_directory_provider,
self.logger,
)
}
Expand Down
74 changes: 51 additions & 23 deletions mithril-client/src/cardano_database_client/proving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ use crate::{
cardano_database_client::ImmutableFileRange,
feedback::MithrilEvent,
file_downloader::{DownloadEvent, FileDownloader, FileDownloaderUri},
utils::{create_directory_if_not_exists, delete_directory, read_files_in_directory},
utils::{
TempDirectoryProvider, create_directory_if_not_exists, delete_directory,
read_files_in_directory,
},
};

/// Represents the verified digests and the Merkle tree built from them.
Expand Down Expand Up @@ -185,14 +188,20 @@ impl VerifiedDigests {

pub struct InternalArtifactProver {
http_file_downloader: Arc<dyn FileDownloader>,
temp_directory_provider: Arc<dyn TempDirectoryProvider>,
logger: slog::Logger,
}

impl InternalArtifactProver {
/// Constructs a new `InternalArtifactProver`.
pub fn new(http_file_downloader: Arc<dyn FileDownloader>, logger: slog::Logger) -> Self {
pub fn new(
http_file_downloader: Arc<dyn FileDownloader>,
temp_directory_provider: Arc<dyn TempDirectoryProvider>,
logger: slog::Logger,
) -> Self {
Self {
http_file_downloader,
temp_directory_provider,
logger,
}
}
Expand Down Expand Up @@ -223,7 +232,7 @@ impl InternalArtifactProver {
certificate: &CertificateMessage,
cardano_database_snapshot: &CardanoDatabaseSnapshotMessage,
) -> MithrilResult<VerifiedDigests> {
let digest_target_dir = Self::digest_target_dir();
let digest_target_dir = self.digest_target_dir();
delete_directory(&digest_target_dir)?;
self.download_unpack_digest_file(&cardano_database_snapshot.digests, &digest_target_dir)
.await?;
Expand Down Expand Up @@ -423,8 +432,8 @@ impl InternalArtifactProver {
Ok(digest_map)
}

fn digest_target_dir() -> PathBuf {
std::env::temp_dir().join("mithril_digest")
fn digest_target_dir(&self) -> PathBuf {
self.temp_directory_provider.temp_dir()
}
}

Expand All @@ -437,6 +446,7 @@ mod tests {
use std::sync::Arc;

use mithril_common::{
current_function,
entities::{CardanoDbBeacon, Epoch, HexEncodedDigest},
messages::CardanoDatabaseDigestListItemMessage,
test::{TempDir, double::Dummy},
Expand All @@ -445,6 +455,7 @@ mod tests {
use crate::{
cardano_database_client::CardanoDatabaseClientDependencyInjector,
file_downloader::MockFileDownloaderBuilder, test_utils::TestLogger,
utils::TimestampTempDirectoryProvider,
};

use super::*;
Expand Down Expand Up @@ -561,11 +572,13 @@ mod tests {

mod download_and_verify_digests {
use mithril_common::{
StdResult,
StdResult, current_function,
entities::{ProtocolMessage, ProtocolMessagePartKey},
messages::DigestsMessagePart,
};

use crate::utils::TimestampTempDirectoryProvider;

use super::*;

fn write_digest_file(
Expand Down Expand Up @@ -610,7 +623,7 @@ mod tests {
}

#[tokio::test]
async fn download_and_verify_digest_should_return_digest_map_acording_to_beacon() {
async fn download_and_verify_digest_should_return_digest_map_according_to_beacon() {
let beacon = CardanoDbBeacon {
epoch: Epoch(123),
immutable_file_number: 42,
Expand Down Expand Up @@ -650,22 +663,28 @@ mod tests {
},
..CardanoDatabaseSnapshotMessage::dummy()
};
let temp_directory_provider =
Arc::new(TimestampTempDirectoryProvider::new(current_function!()));
let digest_target_dir = temp_directory_provider.temp_dir();
let digest_target_dir_clone = digest_target_dir.clone();
let http_file_downloader = Arc::new(
MockFileDownloaderBuilder::default()
.with_file_uri(digests_location)
.with_target_dir(digest_target_dir.clone())
.with_compression(None)
.with_returning(Box::new(move |_, _, _, _, _| {
write_digest_file(
&digest_target_dir_clone,
&build_digests_map(hightest_immutable_number_in_digest_file),
)?;

Ok(())
}))
.build(),
);
let client = CardanoDatabaseClientDependencyInjector::new()
.with_http_file_downloader(Arc::new(
MockFileDownloaderBuilder::default()
.with_file_uri(digests_location)
.with_target_dir(InternalArtifactProver::digest_target_dir())
.with_compression(None)
.with_returning(Box::new(move |_, _, _, _, _| {
write_digest_file(
&InternalArtifactProver::digest_target_dir(),
&build_digests_map(hightest_immutable_number_in_digest_file),
)?;

Ok(())
}))
.build(),
))
.with_http_file_downloader(http_file_downloader)
.with_temp_directory_provider(temp_directory_provider)
.build_cardano_database_client();

let verified_digests = client
Expand All @@ -681,7 +700,7 @@ mod tests {
.collect();
assert_eq!(verified_digests.digests, expected_digests_in_certificate);

assert!(!InternalArtifactProver::digest_target_dir().exists());
assert!(!digest_target_dir.exists());
}
}

Expand All @@ -704,6 +723,7 @@ mod tests {
.with_times(2)
.build(),
),
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
TestLogger::stdout(),
);

Expand Down Expand Up @@ -732,6 +752,7 @@ mod tests {
let target_dir = Path::new(".");
let artifact_prover = InternalArtifactProver::new(
Arc::new(MockFileDownloader::new()),
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
TestLogger::stdout(),
);

Expand Down Expand Up @@ -760,6 +781,7 @@ mod tests {
.with_success()
.build(),
),
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
TestLogger::stdout(),
);

Expand Down Expand Up @@ -794,6 +816,7 @@ mod tests {
.with_success()
.build(),
),
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
TestLogger::stdout(),
);

Expand Down Expand Up @@ -828,6 +851,7 @@ mod tests {
.with_success()
.build(),
),
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
TestLogger::stdout(),
);

Expand Down Expand Up @@ -884,6 +908,7 @@ mod tests {
.with_success()
.build(),
),
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
TestLogger::stdout(),
);
artifact_prover
Expand All @@ -907,6 +932,7 @@ mod tests {
.with_success()
.build(),
),
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
TestLogger::stdout(),
);
artifact_prover
Expand All @@ -929,6 +955,7 @@ mod tests {
.with_success()
.build(),
),
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
TestLogger::stdout(),
);
artifact_prover
Expand Down Expand Up @@ -961,6 +988,7 @@ mod tests {
.with_success()
.build(),
),
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
TestLogger::stdout(),
);

Expand Down
9 changes: 9 additions & 0 deletions mithril-client/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use anyhow::{Context, anyhow};
#[cfg(feature = "fs")]
use chrono::Utc;
use reqwest::Url;
use serde::{Deserialize, Serialize};
use slog::{Logger, o};
Expand Down Expand Up @@ -28,6 +30,8 @@ use crate::mithril_stake_distribution_client::MithrilStakeDistributionClient;
use crate::snapshot_client::SnapshotClient;
#[cfg(feature = "fs")]
use crate::utils::AncillaryVerifier;
#[cfg(feature = "fs")]
use crate::utils::TimestampTempDirectoryProvider;

const DEFAULT_CLIENT_TYPE: &str = "LIBRARY";

Expand Down Expand Up @@ -312,6 +316,11 @@ impl ClientBuilder {
#[cfg(feature = "fs")]
feedback_sender,
#[cfg(feature = "fs")]
Arc::new(TimestampTempDirectoryProvider::new(&format!(
"{}",
Utc::now().timestamp_micros()
))),
#[cfg(feature = "fs")]
logger,
));

Expand Down
14 changes: 8 additions & 6 deletions mithril-client/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@
//! This module contains tools needed mostly for the snapshot download and unpack.

cfg_fs! {
pub const ANCILLARIES_NOT_SIGNED_BY_MITHRIL:&str = "Ancillary verification does not use the Mithril certification: as a mitigation, IOG owned keys are used to sign these files.";
pub const ANCILLARIES_NOT_SIGNED_BY_MITHRIL: &str = "Ancillary verification does not use the Mithril certification: as a mitigation, IOG owned keys are used to sign these files.";

mod ancillary_verifier;
mod bootstrap_files;
mod fs;
mod stream_reader;
mod bootstrap_files;
mod temp_dir_provider;
mod unexpected_downloaded_file_verifier;
mod vec_deque_extensions;

pub use fs::*;
pub use vec_deque_extensions::VecDequeExtensions;
pub use ancillary_verifier::AncillaryVerifier;
pub(crate) use unexpected_downloaded_file_verifier::*;
pub use stream_reader::*;
pub use bootstrap_files::*;
pub use fs::*;
pub use stream_reader::*;
pub(crate) use temp_dir_provider::{TempDirectoryProvider, TimestampTempDirectoryProvider};
pub(crate) use unexpected_downloaded_file_verifier::*;
pub use vec_deque_extensions::VecDequeExtensions;
}
Loading
Loading