Skip to content

Commit 5375b91

Browse files
committed
Add get_by_hashes to aggregator TransactionsRetriever trait
The goal is to use it to retrieves the transactions to prove instead of walking throught the entire transactions list.
1 parent eaa82f1 commit 5375b91

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ impl<'client> GetCardanoTransactionProvider<'client> {
2929
)
3030
}
3131

32+
pub fn get_transaction_hashes_condition(
33+
&self,
34+
transactions_hashes: Vec<TransactionHash>,
35+
) -> WhereCondition {
36+
let hashes_values = transactions_hashes.into_iter().map(Value::String).collect();
37+
38+
WhereCondition::where_in("transaction_hash", hashes_values)
39+
}
40+
3241
pub fn get_transaction_up_to_beacon_condition(
3342
&self,
3443
beacon: ImmutableFileNumber,

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,20 @@ impl TransactionsRetriever for CardanoTransactionRepository {
159159
.collect::<Vec<CardanoTransaction>>()
160160
})
161161
}
162+
163+
async fn get_by_hashes(
164+
&self,
165+
hashes: Vec<TransactionHash>,
166+
) -> StdResult<Vec<CardanoTransaction>> {
167+
let provider = GetCardanoTransactionProvider::new(&self.connection);
168+
let filters = provider.get_transaction_hashes_condition(hashes);
169+
let transactions = provider.find(filters)?;
170+
171+
Ok(transactions
172+
.into_iter()
173+
.map(|record| record.into())
174+
.collect())
175+
}
162176
}
163177

164178
#[cfg(test)]
@@ -193,6 +207,32 @@ mod tests {
193207
);
194208
}
195209

210+
#[tokio::test]
211+
async fn repository_get_transaction_by_hashes() {
212+
let connection = Arc::new(cardano_tx_db_connection().unwrap());
213+
let repository = CardanoTransactionRepository::new(connection);
214+
repository
215+
.create_transaction("tx-hash-123", 10, 50, "block_hash-123", 99)
216+
.await
217+
.unwrap();
218+
repository
219+
.create_transaction("tx-hash-456", 11, 51, "block_hash-456", 100)
220+
.await
221+
.unwrap();
222+
let transactions_result = repository
223+
.get_by_hashes(vec!["tx-hash-123".to_string(), "tx-hash-456".to_string()])
224+
.await
225+
.unwrap();
226+
227+
assert_eq!(
228+
vec![
229+
CardanoTransaction::new("tx-hash-123", 10, 50, "block_hash-123", 99),
230+
CardanoTransaction::new("tx-hash-456", 11, 51, "block_hash-456", 100),
231+
],
232+
transactions_result
233+
);
234+
}
235+
196236
#[tokio::test]
197237
async fn repository_create_ignore_further_transactions_when_exists() {
198238
let connection = Arc::new(cardano_tx_db_connection().unwrap());

mithril-aggregator/src/services/prover.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ use mithril_common::{
1212
StdResult,
1313
};
1414

15-
#[cfg(test)]
16-
use mockall::automock;
17-
1815
/// Prover service is the cryptographic engine in charge of producing cryptographic proofs for transactions
19-
#[cfg_attr(test, automock)]
16+
#[cfg_attr(test, mockall::automock)]
2017
#[async_trait]
2118
pub trait ProverService: Sync + Send {
2219
/// Compute the cryptographic proofs for the given transactions
@@ -28,11 +25,17 @@ pub trait ProverService: Sync + Send {
2825
}
2926

3027
/// Transactions retriever
31-
#[cfg_attr(test, automock)]
28+
#[cfg_attr(test, mockall::automock)]
3229
#[async_trait]
3330
pub trait TransactionsRetriever: Sync + Send {
34-
/// Get transactions up to given beacon using chronological order
31+
/// Get all transactions up to given beacon using chronological order
3532
async fn get_up_to(&self, beacon: &CardanoDbBeacon) -> StdResult<Vec<CardanoTransaction>>;
33+
34+
/// Get a list of transactions by hashes using chronological order
35+
async fn get_by_hashes(
36+
&self,
37+
hashes: Vec<TransactionHash>,
38+
) -> StdResult<Vec<CardanoTransaction>>;
3639
}
3740

3841
/// Mithril prover

0 commit comments

Comments
 (0)