Skip to content

Commit 88c92d5

Browse files
committed
Fix overflow when computing prune threshold
It would happen if the number of block to keep is greater than the highest stored block range root start.
1 parent 5f68ae3 commit 88c92d5

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

internal/mithril-persistence/src/database/provider/cardano_transaction/delete_cardano_transaction.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use anyhow::Context;
12
use sqlite::Value;
23

34
use mithril_common::entities::BlockNumber;
@@ -36,18 +37,22 @@ impl<'conn> DeleteCardanoTransactionProvider<'conn> {
3637
Self { connection }
3738
}
3839

39-
fn get_prune_condition(&self, threshold: BlockNumber) -> WhereCondition {
40-
let threshold = Value::Integer(threshold.try_into().unwrap());
40+
fn get_prune_condition(&self, threshold: BlockNumber) -> StdResult<WhereCondition> {
41+
let threshold = Value::Integer(
42+
threshold
43+
.try_into()
44+
.with_context(|| format!("Failed to convert threshold `{threshold}` to i64"))?,
45+
);
4146

42-
WhereCondition::new("block_number < ?*", vec![threshold])
47+
Ok(WhereCondition::new("block_number < ?*", vec![threshold]))
4348
}
4449

4550
/// Prune the cardano transaction data below the given threshold.
4651
pub fn prune(
4752
&self,
4853
threshold: BlockNumber,
4954
) -> StdResult<EntityCursor<CardanoTransactionRecord>> {
50-
let filters = self.get_prune_condition(threshold);
55+
let filters = self.get_prune_condition(threshold)?;
5156

5257
self.find(filters)
5358
}

internal/mithril-persistence/src/database/repository/cardano_transaction_repository.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ impl CardanoTransactionRepository {
297297
.await?
298298
{
299299
let provider = DeleteCardanoTransactionProvider::new(&self.connection);
300-
let threshold = highest_block_range_start - number_of_blocks_to_keep;
300+
let threshold = highest_block_range_start.saturating_sub(number_of_blocks_to_keep);
301301
provider.prune(threshold)?.next();
302302
}
303303

@@ -897,6 +897,12 @@ mod tests {
897897
let transaction_result = repository.get_all().await.unwrap();
898898
assert_eq!(cardano_transactions.len(), transaction_result.len());
899899

900+
// Pruning with a number of block to keep greater than the highest block range start should
901+
// do nothing.
902+
repository.prune_transaction(10_000_000).await.unwrap();
903+
let transaction_result = repository.get_all_transactions().await.unwrap();
904+
assert_eq!(cardano_transactions, transaction_result);
905+
900906
// Since the highest block range start is 45, pruning with 20 should remove transactions
901907
// with a block number strictly below 25.
902908
repository.prune_transaction(20).await.unwrap();

0 commit comments

Comments
 (0)