mithril_persistence/database/query/block_range_root/
delete_block_range_root.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use anyhow::Context;
use sqlite::Value;

use mithril_common::entities::{BlockNumber, BlockRange};
use mithril_common::StdResult;

use crate::database::record::BlockRangeRootRecord;
use crate::sqlite::{Query, SourceAlias, SqLiteEntity, WhereCondition};

/// Query to delete old [BlockRangeRootRecord] from the sqlite database
pub struct DeleteBlockRangeRootQuery {
    condition: WhereCondition,
}

impl Query for DeleteBlockRangeRootQuery {
    type Entity = BlockRangeRootRecord;

    fn filters(&self) -> WhereCondition {
        self.condition.clone()
    }

    fn get_definition(&self, condition: &str) -> String {
        // it is important to alias the fields with the same name as the table
        // since the table cannot be aliased in a RETURNING statement in SQLite.
        let aliases = SourceAlias::new(&[("{:block_range_root:}", "block_range_root")]);
        let projection = Self::Entity::get_projection().expand(aliases);

        format!("delete from block_range_root where {condition} returning {projection}")
    }
}

impl DeleteBlockRangeRootQuery {
    pub fn contains_or_above_block_number_threshold(
        block_number_threshold: BlockNumber,
    ) -> StdResult<Self> {
        let block_range = BlockRange::from_block_number(block_number_threshold);
        let threshold = Value::Integer(block_range.start.try_into().with_context(|| {
            format!("Failed to convert threshold `{block_number_threshold}` to i64")
        })?);

        Ok(Self {
            condition: WhereCondition::new("start >= ?*", vec![threshold]),
        })
    }
}

#[cfg(test)]
mod tests {
    use mithril_common::crypto_helper::MKTreeNode;
    use mithril_common::entities::BlockRange;

    use crate::database::query::block_range_root::test_helper::insert_block_range_roots;
    use crate::database::query::GetBlockRangeRootQuery;
    use crate::database::test_helper::cardano_tx_db_connection;
    use crate::sqlite::ConnectionExtensions;

    use super::*;

    fn block_range_root_dataset() -> Vec<BlockRangeRootRecord> {
        [
            (
                BlockRange::from_block_number(BlockRange::LENGTH),
                MKTreeNode::from_hex("AAAA").unwrap(),
            ),
            (
                BlockRange::from_block_number(BlockRange::LENGTH * 2),
                MKTreeNode::from_hex("BBBB").unwrap(),
            ),
            (
                BlockRange::from_block_number(BlockRange::LENGTH * 3),
                MKTreeNode::from_hex("CCCC").unwrap(),
            ),
        ]
        .into_iter()
        .map(BlockRangeRootRecord::from)
        .collect()
    }

    #[test]
    fn test_prune_work_even_without_block_range_root_in_db() {
        let connection = cardano_tx_db_connection().unwrap();

        let cursor = connection
            .fetch(
                DeleteBlockRangeRootQuery::contains_or_above_block_number_threshold(BlockNumber(
                    100,
                ))
                .unwrap(),
            )
            .expect("pruning shouldn't crash without block range root stored");
        assert_eq!(0, cursor.count());
    }

    #[test]
    fn test_prune_all_data_if_given_block_number_is_lower_than_stored_number_of_block() {
        parameterized_test_prune_block_range(BlockNumber(0), block_range_root_dataset().len());
    }

    #[test]
    fn test_prune_keep_all_block_range_root_if_given_number_of_block_is_greater_than_the_highest_one(
    ) {
        parameterized_test_prune_block_range(BlockNumber(100_000), 0);
    }

    #[test]
    fn test_prune_block_range_when_block_number_is_block_range_start() {
        parameterized_test_prune_block_range(BlockRange::LENGTH * 2, 2);
    }

    #[test]
    fn test_prune_block_range_when_block_number_is_in_block_range() {
        parameterized_test_prune_block_range(BlockRange::LENGTH * 2 + 1, 2);
    }

    #[test]
    fn test_keep_block_range_when_block_number_is_just_before_range_start() {
        parameterized_test_prune_block_range(BlockRange::LENGTH * 2 - 1, 3);
    }

    fn parameterized_test_prune_block_range(
        block_threshold: BlockNumber,
        delete_record_number: usize,
    ) {
        let connection = cardano_tx_db_connection().unwrap();
        let dataset = block_range_root_dataset();
        insert_block_range_roots(&connection, dataset.clone());

        let query =
            DeleteBlockRangeRootQuery::contains_or_above_block_number_threshold(block_threshold)
                .unwrap();
        let cursor = connection.fetch(query).unwrap();
        assert_eq!(delete_record_number, cursor.count());

        let cursor = connection.fetch(GetBlockRangeRootQuery::all()).unwrap();
        assert_eq!(dataset.len() - delete_record_number, cursor.count());
    }
}