Skip to content

Commit e0cf19e

Browse files
committed
Change signature of BlockScanner::scan to return an Iterator
1 parent c0a7b67 commit e0cf19e

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

mithril-aggregator/src/services/cardano_transactions_importer.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,8 @@ impl CardanoTransactionsImporter {
105105
// todo: temp algorithm, should be optimized to avoid loading all blocks & transactions
106106
// at once in memory (probably using iterators)
107107
let scanned_blocks = self.block_scanner.scan(&self.dirpath, from, until).await?;
108-
let parsed_transactions: Vec<CardanoTransaction> = scanned_blocks
109-
.into_iter()
110-
.flat_map(|b| b.into_transactions())
111-
.collect();
108+
let parsed_transactions: Vec<CardanoTransaction> =
109+
scanned_blocks.flat_map(|b| b.into_transactions()).collect();
112110
debug!(
113111
self.logger,
114112
"TransactionsImporter retrieved '{}' Cardano transactions between immutables '{}' and '{until}'",
@@ -199,7 +197,7 @@ mod tests {
199197
dirpath: &Path,
200198
from_immutable: Option<ImmutableFileNumber>,
201199
until_immutable: ImmutableFileNumber,
202-
) -> StdResult<Vec<ScannedBlock>>;
200+
) -> StdResult<Box<dyn Iterator<Item = ScannedBlock>>>;
203201
}
204202
}
205203

@@ -267,7 +265,7 @@ mod tests {
267265
scanner_mock
268266
.expect_scan()
269267
.withf(move |_, from, until| from.is_none() && until == &up_to_beacon)
270-
.return_once(move |_, _, _| Ok(blocks));
268+
.return_once(move |_, _, _| Ok(Box::new(blocks.into_iter())));
271269
CardanoTransactionsImporter::new_for_test(Arc::new(scanner_mock), repository.clone())
272270
};
273271

@@ -429,7 +427,7 @@ mod tests {
429427
scanner_mock
430428
.expect_scan()
431429
.withf(move |_, from, until| from == &Some(12) && until == &up_to_beacon)
432-
.return_once(move |_, _, _| Ok(scanned_blocks))
430+
.return_once(move |_, _, _| Ok(Box::new(scanned_blocks.into_iter())))
433431
.once();
434432
CardanoTransactionsImporter::new_for_test(Arc::new(scanner_mock), repository.clone())
435433
};
@@ -608,7 +606,9 @@ mod tests {
608606
let importer = {
609607
let connection = cardano_tx_db_connection().unwrap();
610608
let mut scanner = MockBlockScannerImpl::new();
611-
scanner.expect_scan().return_once(move |_, _, _| Ok(blocks));
609+
scanner
610+
.expect_scan()
611+
.return_once(move |_, _, _| Ok(Box::new(blocks.into_iter())));
612612

613613
CardanoTransactionsImporter::new_for_test(
614614
Arc::new(scanner),

mithril-common/src/cardano_block_scanner.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use tokio::sync::RwLock;
3838
/// dirpath: &Path,
3939
/// from_immutable: Option<ImmutableFileNumber>,
4040
/// until_immutable: ImmutableFileNumber,
41-
/// ) -> StdResult<Vec<ScannedBlock>>;
41+
/// ) -> StdResult<Box<dyn Iterator<Item = ScannedBlock>>>;
4242
/// }
4343
/// }
4444
///
@@ -59,7 +59,7 @@ pub trait BlockScanner: Sync + Send {
5959
dirpath: &Path,
6060
from_immutable: Option<ImmutableFileNumber>,
6161
until_immutable: ImmutableFileNumber,
62-
) -> StdResult<Vec<ScannedBlock>>;
62+
) -> StdResult<Box<dyn Iterator<Item = ScannedBlock>>>;
6363
}
6464

6565
/// Dumb Block Scanner
@@ -89,8 +89,9 @@ impl BlockScanner for DumbBlockScanner {
8989
_dirpath: &Path,
9090
_from_immutable: Option<ImmutableFileNumber>,
9191
_until_immutable: ImmutableFileNumber,
92-
) -> StdResult<Vec<ScannedBlock>> {
93-
Ok(self.blocks.read().await.clone())
92+
) -> StdResult<Box<dyn Iterator<Item = ScannedBlock>>> {
93+
let iter = self.blocks.read().await.clone().into_iter();
94+
Ok(Box::new(iter))
9495
}
9596
}
9697

@@ -256,7 +257,7 @@ impl BlockScanner for CardanoBlockScanner {
256257
dirpath: &Path,
257258
from_immutable: Option<ImmutableFileNumber>,
258259
until_immutable: ImmutableFileNumber,
259-
) -> StdResult<Vec<ScannedBlock>> {
260+
) -> StdResult<Box<dyn Iterator<Item = ScannedBlock>>> {
260261
let is_in_bounds = |number: ImmutableFileNumber| match from_immutable {
261262
Some(from) => (from..=until_immutable).contains(&number),
262263
None => number <= until_immutable,
@@ -279,7 +280,7 @@ impl BlockScanner for CardanoBlockScanner {
279280
scanned_blocks.append(&mut blocks);
280281
}
281282

282-
Ok(scanned_blocks)
283+
Ok(Box::new(scanned_blocks.into_iter()))
283284
}
284285
}
285286

@@ -325,7 +326,7 @@ mod tests {
325326
.scan(db_path, None, until_immutable_file)
326327
.await
327328
.unwrap();
328-
let tx_count: usize = blocks.iter().map(|b| b.transactions.len()).sum();
329+
let tx_count: usize = blocks.map(|b| b.transactions.len()).sum();
329330

330331
assert_eq!(tx_count, expected_tx_count);
331332
}
@@ -346,7 +347,7 @@ mod tests {
346347
.scan(db_path, Some(2), until_immutable_file)
347348
.await
348349
.unwrap();
349-
let tx_count: usize = blocks.iter().map(|b| b.transactions.len()).sum();
350+
let tx_count: usize = blocks.map(|b| b.transactions.len()).sum();
350351

351352
assert_eq!(tx_count, expected_tx_count);
352353
}
@@ -379,10 +380,10 @@ mod tests {
379380
let cardano_transaction_parser =
380381
CardanoBlockScanner::new(create_file_logger(&filepath), true);
381382

382-
cardano_transaction_parser
383+
let res = cardano_transaction_parser
383384
.scan(db_path, None, until_immutable_file)
384-
.await
385-
.expect_err("parse should have failed");
385+
.await;
386+
assert!(res.is_err(), "parse should have failed");
386387
}
387388

388389
let log_file = std::fs::read_to_string(&filepath).unwrap();
@@ -405,7 +406,7 @@ mod tests {
405406
.scan(db_path, None, until_immutable_file)
406407
.await
407408
.unwrap();
408-
let tx_count: usize = blocks.iter().map(|b| b.transactions.len()).sum();
409+
let tx_count: usize = blocks.map(|b| b.transactions.len()).sum();
409410

410411
assert_eq!(tx_count, expected_tx_count);
411412
}

mithril-signer/src/cardano_transactions_importer.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,8 @@ impl CardanoTransactionsImporter {
105105
// todo: temp algorithm, should be optimized to avoid loading all blocks & transactions
106106
// at once in memory (probably using iterators)
107107
let scanned_blocks = self.block_scanner.scan(&self.dirpath, from, until).await?;
108-
let parsed_transactions: Vec<CardanoTransaction> = scanned_blocks
109-
.into_iter()
110-
.flat_map(|b| b.into_transactions())
111-
.collect();
108+
let parsed_transactions: Vec<CardanoTransaction> =
109+
scanned_blocks.flat_map(|b| b.into_transactions()).collect();
112110
debug!(
113111
self.logger,
114112
"TransactionsImporter retrieved '{}' Cardano transactions between immutables '{}' and '{until}'",
@@ -199,7 +197,7 @@ mod tests {
199197
dirpath: &Path,
200198
from_immutable: Option<ImmutableFileNumber>,
201199
until_immutable: ImmutableFileNumber,
202-
) -> StdResult<Vec<ScannedBlock>>;
200+
) -> StdResult<Box<dyn Iterator<Item = ScannedBlock>>>;
203201
}
204202
}
205203

@@ -267,7 +265,7 @@ mod tests {
267265
scanner_mock
268266
.expect_scan()
269267
.withf(move |_, from, until| from.is_none() && until == &up_to_beacon)
270-
.return_once(move |_, _, _| Ok(blocks));
268+
.return_once(move |_, _, _| Ok(Box::new(blocks.into_iter())));
271269
CardanoTransactionsImporter::new_for_test(Arc::new(scanner_mock), repository.clone())
272270
};
273271

@@ -429,7 +427,7 @@ mod tests {
429427
scanner_mock
430428
.expect_scan()
431429
.withf(move |_, from, until| from == &Some(12) && until == &up_to_beacon)
432-
.return_once(move |_, _, _| Ok(scanned_blocks))
430+
.return_once(move |_, _, _| Ok(Box::new(scanned_blocks.into_iter())))
433431
.once();
434432
CardanoTransactionsImporter::new_for_test(Arc::new(scanner_mock), repository.clone())
435433
};
@@ -608,7 +606,9 @@ mod tests {
608606
let importer = {
609607
let connection = cardano_tx_db_connection().unwrap();
610608
let mut scanner = MockBlockScannerImpl::new();
611-
scanner.expect_scan().return_once(move |_, _, _| Ok(blocks));
609+
scanner
610+
.expect_scan()
611+
.return_once(move |_, _, _| Ok(Box::new(blocks.into_iter())));
612612

613613
CardanoTransactionsImporter::new_for_test(
614614
Arc::new(scanner),

0 commit comments

Comments
 (0)