Skip to content

Commit bc0dd60

Browse files
committed
fix: apply review comments
1 parent ba3901a commit bc0dd60

File tree

6 files changed

+43
-19
lines changed

6 files changed

+43
-19
lines changed

mithril-aggregator/src/database/provider/cardano_transaction/get_cardano_transaction.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ use mithril_persistence::sqlite::{
77
Provider, SourceAlias, SqLiteEntity, SqliteConnection, WhereCondition,
88
};
99

10+
#[cfg(test)]
11+
use mithril_persistence::sqlite::GetAllCondition;
12+
1013
use crate::database::record::CardanoTransactionRecord;
1114

1215
/// Simple queries to retrieve [CardanoTransaction] from the sqlite database.
@@ -69,3 +72,6 @@ impl<'client> Provider<'client> for GetCardanoTransactionProvider<'client> {
6972
format!("select {projection} from cardano_tx where {condition} order by rowid")
7073
}
7174
}
75+
76+
#[cfg(test)]
77+
impl GetAllCondition for GetCardanoTransactionProvider<'_> {}

mithril-aggregator/src/database/repository/cardano_transaction_repository.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ use crate::database::provider::{
2222
use crate::database::record::{BlockRangeRootRecord, CardanoTransactionRecord};
2323
use crate::services::{TransactionStore, TransactionsRetriever};
2424

25+
#[cfg(test)]
26+
use mithril_persistence::sqlite::GetAllProvider;
27+
2528
/// ## Cardano transaction repository
2629
///
2730
/// This is a business oriented layer to perform actions on the database through
@@ -58,7 +61,8 @@ impl CardanoTransactionRepository {
5861
}
5962

6063
/// Return all the [CardanoTransactionRecord]s in the database up to the given beacon using
61-
/// chronological order.
64+
/// order of insertion.
65+
/// Note: until we rely on block number based beacons, this function needs to compute the highest block number for the given immutable file number.
6266
pub async fn get_transactions_up_to(
6367
&self,
6468
beacon: ImmutableFileNumber,
@@ -70,7 +74,7 @@ impl CardanoTransactionRepository {
7074
.await?
7175
.unwrap_or(0);
7276
let provider = GetCardanoTransactionProvider::new(&self.connection);
73-
let filters = provider.get_transaction_between_blocks_condition(0..block_number);
77+
let filters = provider.get_transaction_between_blocks_condition(0..block_number + 1);
7478
let transactions = provider.find(filters)?;
7579

7680
Ok(transactions.collect())
@@ -176,10 +180,9 @@ impl CardanoTransactionRepository {
176180
#[cfg(test)]
177181
pub(crate) async fn get_all(&self) -> StdResult<Vec<CardanoTransaction>> {
178182
let provider = GetCardanoTransactionProvider::new(&self.connection);
179-
let filters = WhereCondition::default();
180-
let transactions = provider.find(filters)?;
183+
let records = provider.get_all()?;
181184

182-
Ok(transactions.map(|record| record.into()).collect::<Vec<_>>())
185+
Ok(records.map(|record| record.into()).collect())
183186
}
184187
}
185188

@@ -486,27 +489,34 @@ mod tests {
486489
let connection = Arc::new(cardano_tx_db_connection().unwrap());
487490
let repository = CardanoTransactionRepository::new(connection);
488491

492+
// Build transactions with block numbers from 10 to 40 and immutable file numbers from 12 to 14
489493
let cardano_transactions: Vec<CardanoTransactionRecord> = (20..=40)
490494
.map(|i| CardanoTransactionRecord {
491495
transaction_hash: format!("tx-hash-{i}"),
492-
block_number: i / 10,
496+
block_number: i,
493497
slot_number: i * 100,
494498
block_hash: format!("block-hash-{i}"),
495-
immutable_file_number: i,
499+
immutable_file_number: i / 10 + 10,
496500
})
497501
.collect();
502+
498503
repository
499504
.create_transactions(cardano_transactions.clone())
500505
.await
501506
.unwrap();
502507

503-
let transaction_result = repository.get_transactions_up_to(34).await.unwrap();
504-
assert_eq!(cardano_transactions[0..10].to_vec(), transaction_result);
508+
let transaction_result = repository.get_transactions_up_to(12).await.unwrap();
509+
let transaction_up_to_immutable_file_number_12 = cardano_transactions[0..10].to_vec();
510+
assert_eq!(
511+
transaction_up_to_immutable_file_number_12,
512+
transaction_result
513+
);
505514

506515
let transaction_result = repository.get_transactions_up_to(300).await.unwrap();
507-
assert_eq!(cardano_transactions[0..20].to_vec(), transaction_result);
516+
let transaction_all = cardano_transactions[..].to_vec();
517+
assert_eq!(transaction_all, transaction_result);
508518

509-
let transaction_result = repository.get_transactions_up_to(19).await.unwrap();
519+
let transaction_result = repository.get_transactions_up_to(9).await.unwrap();
510520
assert_eq!(Vec::<CardanoTransactionRecord>::new(), transaction_result);
511521
}
512522

mithril-aggregator/src/services/prover.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,9 @@ impl MithrilProverService {
7272
) -> StdResult<MKMap<BlockRange, MKMapNode<BlockRange>>> {
7373
let mut transactions_by_block_ranges: BTreeMap<BlockRange, Vec<TransactionHash>> =
7474
BTreeMap::new();
75-
let mut last_transaction: Option<CardanoTransaction> = None;
75+
let last_transaction = transactions.last().cloned();
7676
for transaction in transactions {
7777
let block_range = BlockRange::from_block_number(transaction.block_number);
78-
last_transaction = Some(transaction.clone());
7978
transactions_by_block_ranges
8079
.entry(block_range)
8180
.or_default()
@@ -174,7 +173,7 @@ mod tests {
174173
let hash = format!("tx-{i}");
175174
transactions.push(CardanoTransaction::new(
176175
&hash,
177-
max(0, 10 * i - 1) as u64,
176+
max(0, 10 * i - 1) as u64, // will produce the following sequence: 0, 9, 19, 29, 39, 49, ...
178177
100 * i as u64,
179178
format!("block_hash-{i}"),
180179
i as u64,

mithril-common/src/signable_builder/cardano_transactions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub trait BlockRangeRootRetriever: Send + Sync {
4343
.await?
4444
.map(|(block_range, root)| (block_range, root.into()));
4545
let mk_hash_map = MKMap::new_from_iter(block_range_roots_iterator)
46-
.with_context(|| "ProverService failed to compute the merkelized structure that proves ownership of the transaction")?;
46+
.with_context(|| "BlockRangeRootRetriever failed to compute the merkelized structure that proves ownership of the transaction")?;
4747

4848
Ok(mk_hash_map)
4949
}

mithril-signer/src/database/provider/cardano_transaction/get_cardano_transaction.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ use mithril_persistence::sqlite::{
88

99
use crate::database::record::CardanoTransactionRecord;
1010

11+
#[cfg(test)]
12+
use mithril_persistence::sqlite::GetAllCondition;
13+
1114
/// Simple queries to retrieve [CardanoTransaction] from the sqlite database.
1215
pub struct GetCardanoTransactionProvider<'client> {
1316
connection: &'client SqliteConnection,
@@ -69,3 +72,6 @@ impl<'client> Provider<'client> for GetCardanoTransactionProvider<'client> {
6972
format!("select {projection} from cardano_tx where {condition} order by rowid")
7073
}
7174
}
75+
76+
#[cfg(test)]
77+
impl GetAllCondition for GetCardanoTransactionProvider<'_> {}

mithril-signer/src/database/repository/cardano_transaction_repository.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ use crate::database::provider::{
2222
use crate::database::record::{BlockRangeRootRecord, CardanoTransactionRecord};
2323
use crate::TransactionStore;
2424

25+
#[cfg(test)]
26+
use mithril_persistence::sqlite::GetAllProvider;
27+
2528
/// ## Cardano transaction repository
2629
///
2730
/// This is a business oriented layer to perform actions on the database through
@@ -58,7 +61,8 @@ impl CardanoTransactionRepository {
5861
}
5962

6063
/// Return all the [CardanoTransactionRecord]s in the database up to the given beacon using
61-
/// chronological order.
64+
/// order of insertion.
65+
/// Note: until we rely on block number based beacons, this function needs to compute the highest block number for the given immutable file number.
6266
pub async fn get_transactions_up_to(
6367
&self,
6468
beacon: ImmutableFileNumber,
@@ -170,10 +174,9 @@ impl CardanoTransactionRepository {
170174
#[cfg(test)]
171175
pub(crate) async fn get_all(&self) -> StdResult<Vec<CardanoTransaction>> {
172176
let provider = GetCardanoTransactionProvider::new(&self.connection);
173-
let filters = WhereCondition::default();
174-
let transactions = provider.find(filters)?;
177+
let records = provider.get_all()?;
175178

176-
Ok(transactions.map(|record| record.into()).collect::<Vec<_>>())
179+
Ok(records.map(|record| record.into()).collect())
177180
}
178181
}
179182

0 commit comments

Comments
 (0)