Skip to content

Commit 52ed633

Browse files
committed
Refactor transaction pruning to use two request instead of one
A first request obtains the highest `block_range_root.start` and a second use it, and a given number of block to keep, as the prune threshold. We do that this instead of using a join to make it simple to test (else the tests would need data on the two tables, our test framework doesn't allow yet to do that without a lot of boilerplate).
1 parent e9dd7aa commit 52ed633

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

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

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,18 @@ impl<'conn> DeleteCardanoTransactionProvider<'conn> {
3636
Self { connection }
3737
}
3838

39-
fn get_prune_condition(&self, number_of_block_to_keep: BlockNumber) -> WhereCondition {
40-
let number_of_block_to_keep = Value::Integer(number_of_block_to_keep.try_into().unwrap());
39+
fn get_prune_condition(&self, threshold: BlockNumber) -> WhereCondition {
40+
let threshold = Value::Integer(threshold.try_into().unwrap());
4141

42-
WhereCondition::new(
43-
"block_number < ((select max(block_number) from cardano_tx) - ?*)",
44-
vec![number_of_block_to_keep],
45-
)
42+
WhereCondition::new("block_number < ?*", vec![threshold])
4643
}
4744

48-
/// Prune the cardano transaction data after the given number of block.
45+
/// Prune the cardano transaction data below the given threshold.
4946
pub fn prune(
5047
&self,
51-
number_of_block_to_keep: BlockNumber,
48+
threshold: BlockNumber,
5249
) -> StdResult<EntityCursor<CardanoTransactionRecord>> {
53-
let filters = self.get_prune_condition(number_of_block_to_keep);
50+
let filters = self.get_prune_condition(threshold);
5451

5552
self.find(filters)
5653
}
@@ -96,44 +93,44 @@ mod tests {
9693
}
9794

9895
#[test]
99-
fn test_prune_keep_all_data_if_given_block_number_is_larger_than_stored_number_of_block() {
96+
fn test_prune_all_data_if_given_block_number_is_larger_than_stored_number_of_block() {
10097
let connection = cardano_tx_db_connection().unwrap();
10198
insert_transactions(&connection, test_transaction_set());
10299

103100
let prune_provider = DeleteCardanoTransactionProvider::new(&connection);
104101
let cursor = prune_provider.prune(100_000).unwrap();
105-
assert_eq!(0, cursor.count());
102+
assert_eq!(test_transaction_set().len(), cursor.count());
106103

107104
let get_provider = GetCardanoTransactionProvider::new(&connection);
108105
let cursor = get_provider.get_all().unwrap();
109-
assert_eq!(test_transaction_set().len(), cursor.count());
106+
assert_eq!(0, cursor.count());
110107
}
111108

112109
#[test]
113-
fn test_prune_keep_only_tx_of_last_block_if_given_number_of_block_is_zero() {
110+
fn test_prune_keep_all_tx_of_last_block_if_given_number_of_block_is_zero() {
114111
let connection = cardano_tx_db_connection().unwrap();
115112
insert_transactions(&connection, test_transaction_set());
116113

117114
let prune_provider = DeleteCardanoTransactionProvider::new(&connection);
118115
let cursor = prune_provider.prune(0).unwrap();
119-
assert_eq!(4, cursor.count());
116+
assert_eq!(0, cursor.count());
120117

121118
let get_provider = GetCardanoTransactionProvider::new(&connection);
122119
let cursor = get_provider.get_all().unwrap();
123-
assert_eq!(2, cursor.count());
120+
assert_eq!(test_transaction_set().len(), cursor.count());
124121
}
125122

126123
#[test]
127-
fn test_prune_data_of_older_than_n_blocks() {
124+
fn test_prune_data_of_below_given_blocks() {
128125
let connection = cardano_tx_db_connection().unwrap();
129126
insert_transactions(&connection, test_transaction_set());
130127

131128
let prune_provider = DeleteCardanoTransactionProvider::new(&connection);
132-
let cursor = prune_provider.prune(1).unwrap();
133-
assert_eq!(2, cursor.count());
129+
let cursor = prune_provider.prune(12).unwrap();
130+
assert_eq!(4, cursor.count());
134131

135132
let get_provider = GetCardanoTransactionProvider::new(&connection);
136133
let cursor = get_provider.get_all().unwrap();
137-
assert_eq!(4, cursor.count());
134+
assert_eq!(2, cursor.count());
138135
}
139136
}

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

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,10 +289,18 @@ impl CardanoTransactionRepository {
289289
Ok(transactions.collect())
290290
}
291291

292-
/// Prune the transaction strictly after the given number of block.
293-
pub async fn prune_transaction(&self, number_of_block_to_keep: BlockNumber) -> StdResult<()> {
294-
let provider = DeleteCardanoTransactionProvider::new(&self.connection);
295-
provider.prune(number_of_block_to_keep)?.next();
292+
/// Prune the transactions older than the given number of blocks (based on the block range root
293+
/// stored).
294+
pub async fn prune_transaction(&self, number_of_blocks_to_keep: BlockNumber) -> StdResult<()> {
295+
if let Some(highest_block_range_start) = self
296+
.get_highest_start_block_number_for_block_range_roots()
297+
.await?
298+
{
299+
let provider = DeleteCardanoTransactionProvider::new(&self.connection);
300+
let threshold = highest_block_range_start - number_of_blocks_to_keep;
301+
provider.prune(threshold)?.next();
302+
}
303+
296304
Ok(())
297305
}
298306
}
@@ -878,13 +886,22 @@ mod tests {
878886
.create_transactions(cardano_transactions.clone())
879887
.await
880888
.unwrap();
889+
repository
890+
.create_block_range_roots(vec![(
891+
BlockRange::from_block_number(45),
892+
MKTreeNode::from_hex("BBBB").unwrap(),
893+
)])
894+
.await
895+
.unwrap();
881896

882897
let transaction_result = repository.get_all().await.unwrap();
883898
assert_eq!(cardano_transactions.len(), transaction_result.len());
884899

885-
repository.prune_transaction(24).await.unwrap();
900+
// Since the highest block range start is 45, pruning with 20 should remove transactions
901+
// with a block number strictly below 25.
902+
repository.prune_transaction(20).await.unwrap();
886903
let transaction_result = repository
887-
.get_transactions_in_range_blocks(0..26)
904+
.get_transactions_in_range_blocks(0..25)
888905
.await
889906
.unwrap();
890907
assert_eq!(Vec::<CardanoTransactionRecord>::new(), transaction_result);

0 commit comments

Comments
 (0)