Skip to content

Commit 909f175

Browse files
committed
refactor(common): transform test only methods on entities into extension traits
This allow a clean split between the production and test code even when using thoses methods as to do so you will now need to import a trait located in the `mithril_common::test` module. one caveat: in order to transform `Blockrange::new` into a extension trait method, its `inner_range` property had to be made `pub(crate)`.
1 parent 6945748 commit 909f175

File tree

12 files changed

+166
-115
lines changed

12 files changed

+166
-115
lines changed

mithril-aggregator/src/database/record/buffered_single_signature_record.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use chrono::{DateTime, Utc};
33
use mithril_common::entities::{
44
HexEncodedSingleSignature, LotteryIndex, SignedEntityTypeDiscriminants, SingleSignature,
55
};
6+
#[cfg(test)]
7+
use mithril_common::test::entities_extensions::SingleSignatureTestExtension;
68
use mithril_common::{StdError, StdResult};
9+
710
use mithril_persistence::sqlite::{HydrationError, Projection, SqLiteEntity};
811

912
/// `BufferedSingleSignatureRecord` record is the representation of a buffered single_signature

mithril-aggregator/src/services/certifier/buffered_certifier.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ mod tests {
192192
};
193193
use mithril_common::entities::SingleSignatureAuthenticationStatus;
194194
use mithril_common::test::double::{Dummy, fake_data};
195+
use mithril_common::test::entities_extensions::SingleSignatureTestExtension;
195196

196197
use crate::database::repository::BufferedSingleSignatureRepository;
197198
use crate::database::test_helper::main_db_connection;

mithril-aggregator/src/tools/single_signature_authenticator.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ impl SingleSignatureAuthenticator {
112112
mod tests {
113113
use anyhow::anyhow;
114114

115+
use mithril_common::test::entities_extensions::SingleSignatureTestExtension;
116+
115117
use crate::multi_signer::MockMultiSigner;
116118
use crate::test::TestLogger;
117119

mithril-common/src/crypto_helper/merkle_map.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ mod tests {
468468
use crate::{
469469
crypto_helper::MKTreeStoreInMemory,
470470
entities::{BlockNumber, BlockRange},
471+
test::entities_extensions::BlockRangeTestExtension,
471472
};
472473

473474
use super::*;

mithril-common/src/entities/block_range.rs

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,41 +19,14 @@ pub type BlockRangeLength = BlockNumber;
1919
/// BlockRange for the Cardano chain
2020
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Debug, Hash)]
2121
pub struct BlockRange {
22-
inner_range: Range<BlockNumber>,
22+
pub(crate) inner_range: Range<BlockNumber>,
2323
}
2424

2525
impl BlockRange {
2626
/// The length of the block range
2727
/// Important: this value should be updated with extreme care (probably with an era change) in order to avoid signing disruptions.
2828
pub const LENGTH: BlockRangeLength = BlockNumber(15);
2929

30-
cfg_test_tools! {
31-
/// BlockRange factory
32-
pub fn new(start: u64, end: u64) -> Self {
33-
Self {
34-
inner_range: BlockNumber(start)..BlockNumber(end),
35-
}
36-
}
37-
38-
/// Try to add two BlockRanges
39-
pub fn try_add(&self, other: &BlockRange) -> StdResult<BlockRange> {
40-
if self.inner_range.end.max(other.inner_range.end)
41-
< self.inner_range.start.min(other.inner_range.start)
42-
{
43-
return Err(anyhow!(
44-
"BlockRange cannot be added as they don't strictly overlap"
45-
));
46-
}
47-
48-
Ok(Self {
49-
inner_range: Range {
50-
start: self.inner_range.start.min(other.inner_range.start),
51-
end: self.inner_range.end.max(other.inner_range.end),
52-
},
53-
})
54-
}
55-
}
56-
5730
/// Get the start of the block range that contains the given block number
5831
pub fn start(number: BlockNumber) -> BlockNumber {
5932
Self::start_with_length(number, Self::LENGTH)
@@ -232,6 +205,8 @@ impl ExactSizeIterator for BlockRangesSequence {
232205
mod tests {
233206
use std::ops::Not;
234207

208+
use crate::test::entities_extensions::BlockRangeTestExtension;
209+
235210
use super::*;
236211

237212
#[test]
@@ -259,22 +234,6 @@ mod tests {
259234
assert!(BlockRange::new(1, 11) < BlockRange::new(2, 10));
260235
}
261236

262-
#[test]
263-
fn test_block_range_try_add() {
264-
assert_eq!(
265-
BlockRange::new(1, 10).try_add(&BlockRange::new(1, 10)).unwrap(),
266-
BlockRange::new(1, 10)
267-
);
268-
assert_eq!(
269-
BlockRange::new(1, 10).try_add(&BlockRange::new(1, 11)).unwrap(),
270-
BlockRange::new(1, 11)
271-
);
272-
assert_eq!(
273-
BlockRange::new(1, 10).try_add(&BlockRange::new(2, 10)).unwrap(),
274-
BlockRange::new(1, 10)
275-
);
276-
}
277-
278237
#[test]
279238
fn test_block_range_start() {
280239
assert_eq!(BlockRange::start(BlockNumber(0)), 0);

mithril-common/src/entities/cardano_transactions_set_proof.rs

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ use crate::entities::TransactionHash;
44

55
use super::BlockRange;
66

7-
cfg_test_tools! {
8-
use crate::crypto_helper::{MKMap, MKTree, MKTreeNode, MKMapNode, MKTreeStorer, MKTreeStoreInMemory};
9-
use crate::entities::BlockNumber;
10-
use std::collections::HashMap;
11-
}
12-
137
/// A cryptographic proof of a set of Cardano transactions is included in the global Cardano transactions set
148
#[derive(Clone, Debug, PartialEq)]
159
pub struct CardanoTransactionsSetProof {
@@ -51,46 +45,14 @@ impl CardanoTransactionsSetProof {
5145

5246
Ok(())
5347
}
54-
55-
cfg_test_tools! {
56-
/// Helper to create a proof from a list of leaves
57-
pub fn from_leaves<S: MKTreeStorer>(leaves: &[(BlockNumber, TransactionHash)]) -> StdResult<Self> {
58-
let transactions_hashes: Vec<TransactionHash> =
59-
leaves.iter().map(|(_, t)| t.into()).collect();
60-
let mut transactions_by_block_ranges: HashMap<BlockRange, Vec<TransactionHash>> =
61-
HashMap::new();
62-
for (block_number, transaction_hash) in leaves {
63-
let block_range = BlockRange::from_block_number(*block_number);
64-
transactions_by_block_ranges
65-
.entry(block_range)
66-
.or_default()
67-
.push(transaction_hash.to_owned());
68-
}
69-
let mk_map = MKMap::<_, _, MKTreeStoreInMemory>::new(
70-
transactions_by_block_ranges
71-
.into_iter()
72-
.try_fold(
73-
vec![],
74-
|mut acc, (block_range, transactions)| -> StdResult<Vec<(_, MKMapNode<_,S>)>> {
75-
acc.push((block_range, MKTree::<S>::new(&transactions)?.into()));
76-
Ok(acc)
77-
},
78-
)?
79-
.as_slice(),
80-
)?;
81-
let mk_leaves: Vec<MKTreeNode> = transactions_hashes
82-
.iter()
83-
.map(|h| h.to_owned().into())
84-
.collect();
85-
let mk_proof = mk_map.compute_proof(&mk_leaves)?;
86-
Ok(Self::new(transactions_hashes, mk_proof))
87-
}
88-
89-
}
9048
}
9149

9250
#[cfg(test)]
9351
mod tests {
52+
use crate::crypto_helper::MKTreeStoreInMemory;
53+
use crate::entities::BlockNumber;
54+
use crate::test::entities_extensions::CardanoTransactionsSetProofTestExtension;
55+
9456
use super::*;
9557

9658
#[test]

mithril-common/src/entities/signed_entity_config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@ impl CardanoTransactionsSigningConfig {
120120
step,
121121
}
122122
}
123-
124123
}
125124

126125
/// Compute the block number to be signed based on the chain tip block number.

mithril-common/src/entities/single_signature.rs

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,34 +62,6 @@ impl SingleSignature {
6262
}
6363
}
6464

65-
cfg_test_tools! {
66-
impl SingleSignature {
67-
/// Create a fake [SingleSignature] with valid cryptographic data for testing purposes.
68-
pub fn fake<TPartyId: Into<String>, TMessage: Into<String>>(party_id: TPartyId, message: TMessage) -> Self {
69-
use crate::entities::{ProtocolParameters};
70-
use crate::test::builder::{MithrilFixtureBuilder, StakeDistributionGenerationMethod};
71-
72-
let party_id = party_id.into();
73-
let message = message.into();
74-
75-
let fixture = MithrilFixtureBuilder::default()
76-
.with_stake_distribution(StakeDistributionGenerationMethod::Custom(
77-
std::collections::BTreeMap::from([(party_id.to_string(), 100)]),
78-
))
79-
.with_protocol_parameters(ProtocolParameters::new(1, 1, 1.0))
80-
.build();
81-
let signature = fixture.signers_fixture()[0].sign(&message).unwrap();
82-
83-
Self {
84-
party_id,
85-
signature: signature.signature,
86-
won_indexes: vec![10, 15],
87-
authentication_status: SingleSignatureAuthenticationStatus::Unauthenticated,
88-
}
89-
}
90-
}
91-
}
92-
9365
impl Debug for SingleSignature {
9466
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
9567
let is_pretty_printing = f.alternate();

mithril-common/src/messages/cardano_transactions_proof.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ mod tests {
191191
};
192192
use crate::test::crypto_helper::MKProofTestExtension;
193193
use crate::test::double::Dummy;
194+
use crate::test::entities_extensions::CardanoTransactionsSetProofTestExtension;
194195

195196
use super::*;
196197

mithril-common/src/test/double/dummies.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::test::double::{Dummy, fake_data, fake_keys};
55
mod entities {
66
use crate::crypto_helper::MKTreeStoreInMemory;
77
use crate::entities::*;
8+
use crate::test::entities_extensions::CardanoTransactionsSetProofTestExtension;
89

910
use super::*;
1011

0 commit comments

Comments
 (0)