Skip to content

Commit 3dfc782

Browse files
authored
Merge pull request #2688 from input-output-hk/jpraynaud/2687-fix-cardano-database-temp-folder
fix: flakiness in e2e tests in the CI
2 parents de2a341 + 42c8f9c commit 3dfc782

File tree

7 files changed

+124
-32
lines changed

7 files changed

+124
-32
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mithril-client/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mithril-client"
3-
version = "0.12.30"
3+
version = "0.12.31"
44
description = "Mithril client library"
55
authors = { workspace = true }
66
edition = { workspace = true }

mithril-client/src/cardano_database_client/api.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::feedback::FeedbackSender;
2222
#[cfg(feature = "fs")]
2323
use crate::file_downloader::FileDownloader;
2424
#[cfg(feature = "fs")]
25-
use crate::utils::AncillaryVerifier;
25+
use crate::utils::{AncillaryVerifier, TempDirectoryProvider};
2626
use crate::{CardanoDatabaseSnapshot, CardanoDatabaseSnapshotListItem, MithrilResult};
2727

2828
use super::fetch::InternalArtifactRetriever;
@@ -50,6 +50,7 @@ impl CardanoDatabaseClient {
5050
#[cfg(feature = "fs")] http_file_downloader: Arc<dyn FileDownloader>,
5151
#[cfg(feature = "fs")] ancillary_verifier: Option<Arc<AncillaryVerifier>>,
5252
#[cfg(feature = "fs")] feedback_sender: FeedbackSender,
53+
#[cfg(feature = "fs")] temp_directory_provider: Arc<dyn TempDirectoryProvider>,
5354
#[cfg(feature = "fs")] logger: Logger,
5455
) -> Self {
5556
#[cfg(feature = "fs")]
@@ -67,6 +68,7 @@ impl CardanoDatabaseClient {
6768
#[cfg(feature = "fs")]
6869
artifact_prover: InternalArtifactProver::new(
6970
http_file_downloader.clone(),
71+
temp_directory_provider.clone(),
7072
logger.clone(),
7173
),
7274
statistics_sender: InternalStatisticsSender::new(aggregator_client.clone()),
@@ -172,6 +174,8 @@ pub(crate) mod test_dependency_injector {
172174
#[cfg(feature = "fs")]
173175
use crate::file_downloader::{FileDownloader, MockFileDownloaderBuilder};
174176
#[cfg(feature = "fs")]
177+
use crate::utils::TimestampTempDirectoryProvider;
178+
#[cfg(feature = "fs")]
175179
use crate::{feedback::FeedbackReceiver, test_utils::TestLogger};
176180

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

@@ -204,6 +210,10 @@ pub(crate) mod test_dependency_injector {
204210
#[cfg(feature = "fs")]
205211
feedback_receivers: vec![],
206212
#[cfg(feature = "fs")]
213+
temp_directory_provider: Arc::new(TimestampTempDirectoryProvider::new(
214+
"cardano_database_client_test",
215+
)),
216+
#[cfg(feature = "fs")]
207217
logger: TestLogger::stdout(),
208218
}
209219
}
@@ -259,13 +269,25 @@ pub(crate) mod test_dependency_injector {
259269
}
260270
}
261271

272+
#[cfg(feature = "fs")]
273+
pub(crate) fn with_temp_directory_provider(
274+
self,
275+
temp_directory_provider: Arc<dyn TempDirectoryProvider>,
276+
) -> Self {
277+
Self {
278+
temp_directory_provider,
279+
..self
280+
}
281+
}
282+
262283
#[cfg(feature = "fs")]
263284
pub(crate) fn build_cardano_database_client(self) -> CardanoDatabaseClient {
264285
CardanoDatabaseClient::new(
265286
Arc::new(self.aggregator_client),
266287
self.http_file_downloader,
267288
self.ancillary_verifier,
268289
FeedbackSender::new(&self.feedback_receivers),
290+
self.temp_directory_provider,
269291
self.logger,
270292
)
271293
}

mithril-client/src/cardano_database_client/proving.rs

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ use crate::{
3232
cardano_database_client::ImmutableFileRange,
3333
feedback::MithrilEvent,
3434
file_downloader::{DownloadEvent, FileDownloader, FileDownloaderUri},
35-
utils::{create_directory_if_not_exists, delete_directory, read_files_in_directory},
35+
utils::{
36+
TempDirectoryProvider, create_directory_if_not_exists, delete_directory,
37+
read_files_in_directory,
38+
},
3639
};
3740

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

186189
pub struct InternalArtifactProver {
187190
http_file_downloader: Arc<dyn FileDownloader>,
191+
temp_directory_provider: Arc<dyn TempDirectoryProvider>,
188192
logger: slog::Logger,
189193
}
190194

191195
impl InternalArtifactProver {
192196
/// Constructs a new `InternalArtifactProver`.
193-
pub fn new(http_file_downloader: Arc<dyn FileDownloader>, logger: slog::Logger) -> Self {
197+
pub fn new(
198+
http_file_downloader: Arc<dyn FileDownloader>,
199+
temp_directory_provider: Arc<dyn TempDirectoryProvider>,
200+
logger: slog::Logger,
201+
) -> Self {
194202
Self {
195203
http_file_downloader,
204+
temp_directory_provider,
196205
logger,
197206
}
198207
}
@@ -223,7 +232,7 @@ impl InternalArtifactProver {
223232
certificate: &CertificateMessage,
224233
cardano_database_snapshot: &CardanoDatabaseSnapshotMessage,
225234
) -> MithrilResult<VerifiedDigests> {
226-
let digest_target_dir = Self::digest_target_dir();
235+
let digest_target_dir = self.digest_target_dir();
227236
delete_directory(&digest_target_dir)?;
228237
self.download_unpack_digest_file(&cardano_database_snapshot.digests, &digest_target_dir)
229238
.await?;
@@ -423,8 +432,8 @@ impl InternalArtifactProver {
423432
Ok(digest_map)
424433
}
425434

426-
fn digest_target_dir() -> PathBuf {
427-
std::env::temp_dir().join("mithril_digest")
435+
fn digest_target_dir(&self) -> PathBuf {
436+
self.temp_directory_provider.temp_dir()
428437
}
429438
}
430439

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

439448
use mithril_common::{
449+
current_function,
440450
entities::{CardanoDbBeacon, Epoch, HexEncodedDigest},
441451
messages::CardanoDatabaseDigestListItemMessage,
442452
test::{TempDir, double::Dummy},
@@ -445,6 +455,7 @@ mod tests {
445455
use crate::{
446456
cardano_database_client::CardanoDatabaseClientDependencyInjector,
447457
file_downloader::MockFileDownloaderBuilder, test_utils::TestLogger,
458+
utils::TimestampTempDirectoryProvider,
448459
};
449460

450461
use super::*;
@@ -561,11 +572,13 @@ mod tests {
561572

562573
mod download_and_verify_digests {
563574
use mithril_common::{
564-
StdResult,
575+
StdResult, current_function,
565576
entities::{ProtocolMessage, ProtocolMessagePartKey},
566577
messages::DigestsMessagePart,
567578
};
568579

580+
use crate::utils::TimestampTempDirectoryProvider;
581+
569582
use super::*;
570583

571584
fn write_digest_file(
@@ -610,7 +623,7 @@ mod tests {
610623
}
611624

612625
#[tokio::test]
613-
async fn download_and_verify_digest_should_return_digest_map_acording_to_beacon() {
626+
async fn download_and_verify_digest_should_return_digest_map_according_to_beacon() {
614627
let beacon = CardanoDbBeacon {
615628
epoch: Epoch(123),
616629
immutable_file_number: 42,
@@ -650,22 +663,28 @@ mod tests {
650663
},
651664
..CardanoDatabaseSnapshotMessage::dummy()
652665
};
666+
let temp_directory_provider =
667+
Arc::new(TimestampTempDirectoryProvider::new(current_function!()));
668+
let digest_target_dir = temp_directory_provider.temp_dir();
669+
let digest_target_dir_clone = digest_target_dir.clone();
670+
let http_file_downloader = Arc::new(
671+
MockFileDownloaderBuilder::default()
672+
.with_file_uri(digests_location)
673+
.with_target_dir(digest_target_dir.clone())
674+
.with_compression(None)
675+
.with_returning(Box::new(move |_, _, _, _, _| {
676+
write_digest_file(
677+
&digest_target_dir_clone,
678+
&build_digests_map(hightest_immutable_number_in_digest_file),
679+
)?;
680+
681+
Ok(())
682+
}))
683+
.build(),
684+
);
653685
let client = CardanoDatabaseClientDependencyInjector::new()
654-
.with_http_file_downloader(Arc::new(
655-
MockFileDownloaderBuilder::default()
656-
.with_file_uri(digests_location)
657-
.with_target_dir(InternalArtifactProver::digest_target_dir())
658-
.with_compression(None)
659-
.with_returning(Box::new(move |_, _, _, _, _| {
660-
write_digest_file(
661-
&InternalArtifactProver::digest_target_dir(),
662-
&build_digests_map(hightest_immutable_number_in_digest_file),
663-
)?;
664-
665-
Ok(())
666-
}))
667-
.build(),
668-
))
686+
.with_http_file_downloader(http_file_downloader)
687+
.with_temp_directory_provider(temp_directory_provider)
669688
.build_cardano_database_client();
670689

671690
let verified_digests = client
@@ -681,7 +700,7 @@ mod tests {
681700
.collect();
682701
assert_eq!(verified_digests.digests, expected_digests_in_certificate);
683702

684-
assert!(!InternalArtifactProver::digest_target_dir().exists());
703+
assert!(!digest_target_dir.exists());
685704
}
686705
}
687706

@@ -704,6 +723,7 @@ mod tests {
704723
.with_times(2)
705724
.build(),
706725
),
726+
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
707727
TestLogger::stdout(),
708728
);
709729

@@ -732,6 +752,7 @@ mod tests {
732752
let target_dir = Path::new(".");
733753
let artifact_prover = InternalArtifactProver::new(
734754
Arc::new(MockFileDownloader::new()),
755+
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
735756
TestLogger::stdout(),
736757
);
737758

@@ -760,6 +781,7 @@ mod tests {
760781
.with_success()
761782
.build(),
762783
),
784+
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
763785
TestLogger::stdout(),
764786
);
765787

@@ -794,6 +816,7 @@ mod tests {
794816
.with_success()
795817
.build(),
796818
),
819+
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
797820
TestLogger::stdout(),
798821
);
799822

@@ -828,6 +851,7 @@ mod tests {
828851
.with_success()
829852
.build(),
830853
),
854+
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
831855
TestLogger::stdout(),
832856
);
833857

@@ -884,6 +908,7 @@ mod tests {
884908
.with_success()
885909
.build(),
886910
),
911+
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
887912
TestLogger::stdout(),
888913
);
889914
artifact_prover
@@ -907,6 +932,7 @@ mod tests {
907932
.with_success()
908933
.build(),
909934
),
935+
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
910936
TestLogger::stdout(),
911937
);
912938
artifact_prover
@@ -929,6 +955,7 @@ mod tests {
929955
.with_success()
930956
.build(),
931957
),
958+
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
932959
TestLogger::stdout(),
933960
);
934961
artifact_prover
@@ -961,6 +988,7 @@ mod tests {
961988
.with_success()
962989
.build(),
963990
),
991+
Arc::new(TimestampTempDirectoryProvider::new(current_function!())),
964992
TestLogger::stdout(),
965993
);
966994

mithril-client/src/client.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use anyhow::{Context, anyhow};
2+
#[cfg(feature = "fs")]
3+
use chrono::Utc;
24
use reqwest::Url;
35
use serde::{Deserialize, Serialize};
46
use slog::{Logger, o};
@@ -28,6 +30,8 @@ use crate::mithril_stake_distribution_client::MithrilStakeDistributionClient;
2830
use crate::snapshot_client::SnapshotClient;
2931
#[cfg(feature = "fs")]
3032
use crate::utils::AncillaryVerifier;
33+
#[cfg(feature = "fs")]
34+
use crate::utils::TimestampTempDirectoryProvider;
3135

3236
const DEFAULT_CLIENT_TYPE: &str = "LIBRARY";
3337

@@ -312,6 +316,11 @@ impl ClientBuilder {
312316
#[cfg(feature = "fs")]
313317
feedback_sender,
314318
#[cfg(feature = "fs")]
319+
Arc::new(TimestampTempDirectoryProvider::new(&format!(
320+
"{}",
321+
Utc::now().timestamp_micros()
322+
))),
323+
#[cfg(feature = "fs")]
315324
logger,
316325
));
317326

mithril-client/src/utils/mod.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22
//! This module contains tools needed mostly for the snapshot download and unpack.
33
44
cfg_fs! {
5-
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.";
5+
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.";
66

77
mod ancillary_verifier;
8+
mod bootstrap_files;
89
mod fs;
910
mod stream_reader;
10-
mod bootstrap_files;
11+
mod temp_dir_provider;
1112
mod unexpected_downloaded_file_verifier;
1213
mod vec_deque_extensions;
1314

14-
pub use fs::*;
15-
pub use vec_deque_extensions::VecDequeExtensions;
1615
pub use ancillary_verifier::AncillaryVerifier;
17-
pub(crate) use unexpected_downloaded_file_verifier::*;
18-
pub use stream_reader::*;
1916
pub use bootstrap_files::*;
17+
pub use fs::*;
18+
pub use stream_reader::*;
19+
pub(crate) use temp_dir_provider::{TempDirectoryProvider, TimestampTempDirectoryProvider};
20+
pub(crate) use unexpected_downloaded_file_verifier::*;
21+
pub use vec_deque_extensions::VecDequeExtensions;
2022
}

0 commit comments

Comments
 (0)