Skip to content

Commit 59b2026

Browse files
committed
Hide 'test only' tools behind the dedicated flag
1 parent aa362cf commit 59b2026

File tree

10 files changed

+154
-122
lines changed

10 files changed

+154
-122
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
//! Tools to request metadata, like the current epoch or the stake distribution, from the Cardano
22
33
mod cli_observer;
4+
#[cfg(any(test, feature = "test_only"))]
45
mod fake_observer;
56
mod interface;
67

78
pub use cli_observer::{CardanoCliChainObserver, CardanoCliRunner};
9+
#[cfg(any(test, feature = "test_only"))]
810
pub use fake_observer::FakeObserver;
911
pub use interface::{ChainObserver, ChainObserverError};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::{
2+
digesters::{ImmutableDigester, ImmutableDigesterError},
3+
entities::Beacon,
4+
};
5+
use async_trait::async_trait;
6+
use tokio::sync::RwLock;
7+
8+
/// A [ImmutableDigester] returning configurable result for testing purpose.
9+
pub struct DumbImmutableDigester {
10+
digest: RwLock<String>,
11+
is_success: bool,
12+
}
13+
14+
impl DumbImmutableDigester {
15+
/// DumbDigester factory
16+
pub fn new(digest: &str, is_success: bool) -> Self {
17+
let digest = RwLock::new(String::from(digest));
18+
19+
Self { digest, is_success }
20+
}
21+
22+
/// Update digest returned by [compute_digest][DumbImmutableDigester::compute_digest]
23+
pub async fn update_digest(&self, new_digest: String) {
24+
let mut digest = self.digest.write().await;
25+
*digest = new_digest;
26+
}
27+
}
28+
29+
impl Default for DumbImmutableDigester {
30+
fn default() -> Self {
31+
Self::new("1234", true)
32+
}
33+
}
34+
35+
#[async_trait]
36+
impl ImmutableDigester for DumbImmutableDigester {
37+
async fn compute_digest(&self, beacon: &Beacon) -> Result<String, ImmutableDigesterError> {
38+
if self.is_success {
39+
Ok(self.digest.read().await.clone())
40+
} else {
41+
Err(ImmutableDigesterError::NotEnoughImmutable {
42+
expected_number: beacon.immutable_file_number,
43+
found_number: None,
44+
})
45+
}
46+
}
47+
}

mithril-common/src/digesters/dummy_immutable_db_builder.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
#![cfg(test)]
21
use crate::{digesters::ImmutableFile, entities::ImmutableFileNumber};
32
use std::{
43
fs::File,
54
io::prelude::Write,
65
path::{Path, PathBuf},
76
};
87

8+
/// A [DummyImmutableDb] builder.
99
pub struct DummyImmutablesDbBuilder {
1010
dir: PathBuf,
1111
immutables_to_write: Vec<ImmutableFileNumber>,
@@ -14,9 +14,13 @@ pub struct DummyImmutablesDbBuilder {
1414
file_size: Option<u64>,
1515
}
1616

17+
/// A dummy cardano immutable db.
1718
pub struct DummyImmutableDb {
19+
/// The dummy cardano db directory path.
1820
pub dir: PathBuf,
21+
/// The [immutables files][ImmutableFile] in the dummy cardano db.
1922
pub immutables_files: Vec<ImmutableFile>,
23+
/// Files that doesn't follow the immutable file name scheme in the dummy cardano db.
2024
pub non_immutables_files: Vec<PathBuf>,
2125
}
2226

@@ -33,11 +37,14 @@ impl DummyImmutablesDbBuilder {
3337
}
3438
}
3539

40+
/// Set the immutables file number that will be used to generate the immutable files, for each
41+
/// number three files will be generated (a 'chunk', a 'primary' and a 'secondary' file).
3642
pub fn with_immutables(&mut self, immutables: &[ImmutableFileNumber]) -> &mut Self {
3743
self.immutables_to_write = immutables.to_vec();
3844
self
3945
}
4046

47+
/// Set filenames to write to the db that doesn't follow the immutable file name scheme.
4148
pub fn with_non_immutables(&mut self, non_immutables: &[&str]) -> &mut Self {
4249
self.non_immutables_to_write = non_immutables.iter().map(|f| f.to_string()).collect();
4350
self
@@ -59,6 +66,7 @@ impl DummyImmutablesDbBuilder {
5966
self
6067
}
6168

69+
/// Build a [DummyImmutableDb].
6270
pub fn build(&self) -> DummyImmutableDb {
6371
let mut non_immutables_files = vec![];
6472
let mut immutable_numbers = self.immutables_to_write.clone();

mithril-common/src/digesters/immutable_digester.rs

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::{
55
use async_trait::async_trait;
66
use std::io;
77
use thiserror::Error;
8-
use tokio::sync::RwLock;
98

109
/// A digester than can compute the digest used for mithril signatures
1110
///
@@ -68,44 +67,3 @@ pub enum ImmutableDigesterError {
6867
#[error("Digest computation failed: {0}")]
6968
DigestComputationError(#[from] io::Error),
7069
}
71-
72-
/// A [ImmutableDigester] returning configurable result for testing purpose.
73-
pub struct DumbImmutableDigester {
74-
digest: RwLock<String>,
75-
is_success: bool,
76-
}
77-
78-
impl DumbImmutableDigester {
79-
/// DumbDigester factory
80-
pub fn new(digest: &str, is_success: bool) -> Self {
81-
let digest = RwLock::new(String::from(digest));
82-
83-
Self { digest, is_success }
84-
}
85-
86-
/// Update digest returned by [compute_digest][DumbImmutableDigester::compute_digest]
87-
pub async fn update_digest(&self, new_digest: String) {
88-
let mut digest = self.digest.write().await;
89-
*digest = new_digest;
90-
}
91-
}
92-
93-
impl Default for DumbImmutableDigester {
94-
fn default() -> Self {
95-
Self::new("1234", true)
96-
}
97-
}
98-
99-
#[async_trait]
100-
impl ImmutableDigester for DumbImmutableDigester {
101-
async fn compute_digest(&self, beacon: &Beacon) -> Result<String, ImmutableDigesterError> {
102-
if self.is_success {
103-
Ok(self.digest.read().await.clone())
104-
} else {
105-
Err(ImmutableDigesterError::NotEnoughImmutable {
106-
expected_number: beacon.immutable_file_number,
107-
found_number: None,
108-
})
109-
}
110-
}
111-
}

mithril-common/src/digesters/mod.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22
33
pub mod cache;
44
mod cardano_immutable_digester;
5+
#[cfg(any(test, feature = "test_only"))]
6+
mod dumb_immutable_observer;
7+
#[cfg(any(test, feature = "test_only"))]
58
mod dummy_immutable_db_builder;
69
mod immutable_digester;
710
mod immutable_file;
811
mod immutable_file_observer;
912

1013
pub use cardano_immutable_digester::CardanoImmutableDigester;
11-
pub use immutable_digester::{DumbImmutableDigester, ImmutableDigester, ImmutableDigesterError};
14+
pub use immutable_digester::{ImmutableDigester, ImmutableDigesterError};
1215
pub use immutable_file::{ImmutableFile, ImmutableFileCreationError, ImmutableFileListingError};
1316
pub use immutable_file_observer::{
1417
DumbImmutableFileObserver, ImmutableFileObserver, ImmutableFileObserverError,
1518
ImmutableFileSystemObserver,
1619
};
1720

18-
#[cfg(test)]
19-
pub use dummy_immutable_db_builder::DummyImmutablesDbBuilder;
21+
#[cfg(any(test, feature = "test_only"))]
22+
pub use dumb_immutable_observer::DumbImmutableDigester;
23+
#[cfg(any(test, feature = "test_only"))]
24+
pub use dummy_immutable_db_builder::{DummyImmutableDb, DummyImmutablesDbBuilder};

mithril-common/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
//! lib with the [crypto_helper].
1010
//! - A [certificate chain] used to validate the Certificate Chain created by an aggregator
1111
//! - The [entities] used by, and exchanged between, the aggregator, signers and client.
12-
//! - useful test utilities including stubs, [fake data][fake_data] builders, a tool validate
13-
//! conformity to an open api specification ([apispec]).
1412
13+
#[cfg(any(test, feature = "test_only"))]
1514
pub mod apispec;
1615
mod beacon_provider;
1716
pub mod certificate_chain;
@@ -20,6 +19,7 @@ pub mod crypto_helper;
2019
pub mod database;
2120
pub mod digesters;
2221
pub mod entities;
22+
#[cfg(any(test, feature = "test_only"))]
2323
pub mod fake_data;
2424
pub mod sqlite;
2525
pub mod store;
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Define a generic way to store data with the [Store Adapter][store_adapter::StoreAdapter], with
2-
//! an adapter ([in memory][MemoryAdapter] and two more for testing ([a stub with one
3-
//! record][DumbStoreAdapter] and one which [always fails][FailStoreAdapter]).
2+
//! an adapter [in memory][MemoryAdapter] and another [sqlite][SQLiteAdapter].
43
54
mod memory_adapter;
65
mod sqlite_adapter;
@@ -10,7 +9,11 @@ pub use memory_adapter::MemoryAdapter;
109
pub use sqlite_adapter::{SQLiteAdapter, SQLiteResultIterator};
1110
pub use store_adapter::*;
1211

12+
#[cfg(any(test, feature = "test_only"))]
1313
mod dumb_adapter;
14+
#[cfg(any(test, feature = "test_only"))]
1415
pub use dumb_adapter::DumbStoreAdapter;
16+
#[cfg(any(test, feature = "test_only"))]
1517
mod fail_adapter;
18+
#[cfg(any(test, feature = "test_only"))]
1619
pub use fail_adapter::FailStoreAdapter;

0 commit comments

Comments
 (0)