Skip to content

Commit dcceb41

Browse files
Alenarsfauveldlachaume
committed
Transfer immutable offsetting responsability to block scanner
Since the importer doesn't know anymore about immutable file numbers and the current block scanner role is precisely to fetch them for its `ImmutableBlockStreamer`. Co-authored-by: Sébastien Fauvel <[email protected]> Co-authored-by: Damien Lachaume <[email protected]>
1 parent 8d1e1b8 commit dcceb41

File tree

7 files changed

+57
-20
lines changed

7 files changed

+57
-20
lines changed

mithril-aggregator/src/dependency_injection/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,8 @@ impl DependenciesBuilder {
700700
.get_network()?
701701
.compute_allow_unparsable_block(self.configuration.allow_unparsable_block)?,
702702
self.get_transaction_repository().await?,
703+
// Rescan the last immutable when importing transactions, it may have been partially imported
704+
Some(1),
703705
);
704706

705707
Ok(Arc::new(block_scanner))
@@ -1039,8 +1041,6 @@ impl DependenciesBuilder {
10391041
self.get_block_scanner().await?,
10401042
self.get_transaction_repository().await?,
10411043
&self.configuration.db_directory,
1042-
// Rescan the last immutable when importing transactions, it may have been partially imported
1043-
Some(1),
10441044
self.get_logger().await?,
10451045
));
10461046
let block_range_root_retriever = self.get_transaction_repository().await?;

mithril-aggregator/src/services/cardano_transactions_importer.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ pub struct CardanoTransactionsImporter {
4646
block_scanner: Arc<dyn BlockScanner>,
4747
transaction_store: Arc<dyn TransactionStore>,
4848
logger: Logger,
49-
rescan_offset: Option<usize>,
5049
dirpath: PathBuf,
5150
}
5251

@@ -60,14 +59,12 @@ impl CardanoTransactionsImporter {
6059
block_scanner: Arc<dyn BlockScanner>,
6160
transaction_store: Arc<dyn TransactionStore>,
6261
dirpath: &Path,
63-
rescan_offset: Option<usize>,
6462
logger: Logger,
6563
) -> Self {
6664
Self {
6765
block_scanner,
6866
transaction_store,
6967
logger,
70-
rescan_offset,
7168
dirpath: dirpath.to_owned(),
7269
}
7370
}
@@ -213,7 +210,6 @@ mod tests {
213210
scanner,
214211
transaction_store,
215212
Path::new(""),
216-
None,
217213
crate::test_tools::logger_for_tests(),
218214
)
219215
}

mithril-common/src/cardano_block_scanner/block_scanner.rs

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ pub struct CardanoBlockScanner {
3232
/// This situation should only happen on the test networks and not on the mainnet.
3333
allow_unparsable_block: bool,
3434
lower_bound_finder: Arc<dyn ImmutableLowerBoundFinder>,
35+
rescan_offset: Option<usize>,
3536
}
3637

3738
impl CardanoBlockScanner {
@@ -40,6 +41,7 @@ impl CardanoBlockScanner {
4041
logger: Logger,
4142
allow_unparsable_block: bool,
4243
lower_bound_finder: Arc<dyn ImmutableLowerBoundFinder>,
44+
rescan_offset: Option<usize>,
4345
) -> Self {
4446
if allow_unparsable_block {
4547
warn!(
@@ -51,8 +53,16 @@ impl CardanoBlockScanner {
5153
logger,
5254
allow_unparsable_block,
5355
lower_bound_finder,
56+
rescan_offset,
5457
}
5558
}
59+
60+
async fn get_lower_bound(&self) -> StdResult<Option<ImmutableFileNumber>> {
61+
let highest = self.lower_bound_finder.find_lower_bound().await?;
62+
let rescan_offset = self.rescan_offset.unwrap_or(0);
63+
let highest = highest.map(|h| (h + 1).saturating_sub(rescan_offset as u64));
64+
Ok(highest)
65+
}
5666
}
5767

5868
#[async_trait]
@@ -63,7 +73,7 @@ impl BlockScanner for CardanoBlockScanner {
6373
_from: Option<ChainPoint>,
6474
_until: BlockNumber,
6575
) -> StdResult<Box<dyn BlockStreamer>> {
66-
let lower_bound = self.lower_bound_finder.find_lower_bound().await?;
76+
let lower_bound = self.get_lower_bound().await?;
6777
let is_in_bounds = |number: ImmutableFileNumber| match lower_bound {
6878
Some(from) => from <= number,
6979
None => true,
@@ -114,7 +124,7 @@ mod tests {
114124
mock.expect_find_lower_bound().returning(|| Ok(None));
115125
});
116126
let cardano_transaction_parser =
117-
CardanoBlockScanner::new(TestLogger::stdout(), false, lower_bound_finder);
127+
CardanoBlockScanner::new(TestLogger::stdout(), false, lower_bound_finder, None);
118128

119129
for until_block_number in [1, 10000] {
120130
let mut streamer = cardano_transaction_parser
@@ -145,7 +155,7 @@ mod tests {
145155
mock.expect_find_lower_bound().returning(|| Ok(Some(0)));
146156
});
147157
let cardano_transaction_parser =
148-
CardanoBlockScanner::new(TestLogger::stdout(), false, lower_bound_finder);
158+
CardanoBlockScanner::new(TestLogger::stdout(), false, lower_bound_finder, None);
149159

150160
for until_block_number in [1, 10000] {
151161
let mut streamer = cardano_transaction_parser
@@ -172,16 +182,20 @@ mod tests {
172182
let db_path = Path::new("../mithril-test-lab/test_data/immutable/");
173183
assert!(get_number_of_immutable_chunk_in_dir(db_path) >= 3);
174184

175-
let test_cases = [(0, None), (1, Some(1))];
176-
for (expected, lowest_found_immutable) in test_cases {
185+
let test_cases = [
186+
(None, 0),
187+
// When a lowest immutable file number is found we start from the next immutable (i + 1)
188+
(Some(1), 2),
189+
];
190+
for (lowest_found_immutable, expected) in test_cases {
177191
let lower_bound_finder = lower_bound_finder(|mock| {
178192
mock.expect_find_lower_bound()
179193
.return_once(move || Ok(lowest_found_immutable));
180194
});
181195

182196
let from = ChainPoint::dummy();
183197
let cardano_transaction_parser =
184-
CardanoBlockScanner::new(TestLogger::stdout(), false, lower_bound_finder);
198+
CardanoBlockScanner::new(TestLogger::stdout(), false, lower_bound_finder, None);
185199

186200
let mut streamer = cardano_transaction_parser
187201
.scan(db_path, Some(from), 10000000)
@@ -211,6 +225,7 @@ mod tests {
211225
TestLogger::file(&log_path),
212226
true,
213227
lower_bound_finder(|_| {}),
228+
None,
214229
);
215230
}
216231

@@ -232,10 +247,42 @@ mod tests {
232247
TestLogger::file(&log_path),
233248
false,
234249
lower_bound_finder(|_| {}),
250+
None,
235251
);
236252
}
237253

238254
let log_file = std::fs::read_to_string(&log_path).unwrap();
239255
assert!(!log_file.contains("The 'allow_unparsable_block' option is activated. This option should only be used on test networks."));
240256
}
257+
258+
#[tokio::test]
259+
async fn change_parsed_lower_bound_when_rescan_limit_is_set() {
260+
fn scanner_with_offset(
261+
highest_stored_immutable: ImmutableFileNumber,
262+
rescan_offset: ImmutableFileNumber,
263+
) -> CardanoBlockScanner {
264+
let mut store = MockImmutableLowerBoundFinder::new();
265+
store
266+
.expect_find_lower_bound()
267+
.returning(move || Ok(Some(highest_stored_immutable)));
268+
269+
CardanoBlockScanner::new(
270+
TestLogger::stdout(),
271+
false,
272+
Arc::new(store),
273+
Some(rescan_offset as usize),
274+
)
275+
}
276+
let scanner = scanner_with_offset(8, 3);
277+
278+
let from = scanner.get_lower_bound().await.unwrap();
279+
// Expected should be: highest_stored_beacon + 1 - rescan_offset
280+
assert_eq!(Some(6), from);
281+
282+
let scanner = scanner_with_offset(5, 10);
283+
284+
let from = scanner.get_lower_bound().await.unwrap();
285+
// If sub overflow it should be 0
286+
assert_eq!(Some(0), from);
287+
}
241288
}

mithril-signer/src/cardano_transactions_importer.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ pub struct CardanoTransactionsImporter {
4646
block_scanner: Arc<dyn BlockScanner>,
4747
transaction_store: Arc<dyn TransactionStore>,
4848
logger: Logger,
49-
rescan_offset: Option<usize>,
5049
dirpath: PathBuf,
5150
}
5251

@@ -60,14 +59,12 @@ impl CardanoTransactionsImporter {
6059
block_scanner: Arc<dyn BlockScanner>,
6160
transaction_store: Arc<dyn TransactionStore>,
6261
dirpath: &Path,
63-
rescan_offset: Option<usize>,
6462
logger: Logger,
6563
) -> Self {
6664
Self {
6765
block_scanner,
6866
transaction_store,
6967
logger,
70-
rescan_offset,
7168
dirpath: dirpath.to_owned(),
7269
}
7370
}
@@ -213,7 +210,6 @@ mod tests {
213210
scanner,
214211
transaction_store,
215212
Path::new(""),
216-
None,
217213
crate::test_tools::logger_for_tests(),
218214
)
219215
}

mithril-signer/src/runtime/runner.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,6 @@ mod tests {
547547
transaction_parser.clone(),
548548
transaction_store.clone(),
549549
Path::new(""),
550-
None,
551550
slog_scope::logger(),
552551
));
553552
let block_range_root_retriever = Arc::new(MockBlockRangeRootRetrieverImpl::new());

mithril-signer/src/runtime/signer_services.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,13 @@ impl<'a> ServiceBuilder for ProductionServiceBuilder<'a> {
265265
.get_network()?
266266
.compute_allow_unparsable_block(self.config.allow_unparsable_block)?,
267267
transaction_store.clone(),
268+
// Rescan the last immutable when importing transactions, it may have been partially imported
269+
Some(1),
268270
));
269271
let transactions_importer = Arc::new(CardanoTransactionsImporter::new(
270272
block_scanner,
271273
transaction_store.clone(),
272274
&self.config.db_directory,
273-
// Rescan the last immutable when importing transactions, it may have been partially imported
274-
Some(1),
275275
slog_scope::logger(),
276276
));
277277
// Wrap the transaction importer with decorator to prune the transactions after import

mithril-signer/tests/test_extensions/state_machine_tester.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ impl StateMachineTester {
164164
transaction_parser.clone(),
165165
transaction_store.clone(),
166166
Path::new(""),
167-
None,
168167
slog_scope::logger(),
169168
));
170169
let block_range_root_retriever = transaction_store.clone();

0 commit comments

Comments
 (0)