drk/
scanned_blocks.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use rusqlite::types::Value;
20
21use crate::{
22    convert_named_params,
23    error::{WalletDbError, WalletDbResult},
24    Drk,
25};
26
27// Wallet SQL table constant names. These have to represent the `wallet.sql`
28// SQL schema.
29const WALLET_SCANNED_BLOCKS_TABLE: &str = "scanned_blocks";
30const WALLET_SCANNED_BLOCKS_COL_HEIGH: &str = "height";
31const WALLET_SCANNED_BLOCKS_COL_HASH: &str = "hash";
32const WALLET_SCANNED_BLOCKS_COL_ROLLBACK_QUERY: &str = "rollback_query";
33
34impl Drk {
35    /// Insert a scanned block information record into the wallet.
36    pub fn put_scanned_block_record(
37        &self,
38        height: u32,
39        hash: &str,
40        rollback_query: &str,
41    ) -> WalletDbResult<()> {
42        let query = format!(
43            "INSERT INTO {WALLET_SCANNED_BLOCKS_TABLE} ({WALLET_SCANNED_BLOCKS_COL_HEIGH}, {WALLET_SCANNED_BLOCKS_COL_HASH}, {WALLET_SCANNED_BLOCKS_COL_ROLLBACK_QUERY}) VALUES (?1, ?2, ?3);"
44        );
45        self.wallet.exec_sql(&query, rusqlite::params![height, hash, rollback_query])
46    }
47
48    /// Auxiliary function to parse a `WALLET_SCANNED_BLOCKS_TABLE` records.
49    fn parse_scanned_block_record(&self, row: &[Value]) -> WalletDbResult<(u32, String, String)> {
50        let Value::Integer(height) = row[0] else {
51            return Err(WalletDbError::ParseColumnValueError);
52        };
53        let Ok(height) = u32::try_from(height) else {
54            return Err(WalletDbError::ParseColumnValueError);
55        };
56
57        let Value::Text(ref hash) = row[1] else {
58            return Err(WalletDbError::ParseColumnValueError);
59        };
60
61        let Value::Text(ref rollback_query) = row[2] else {
62            return Err(WalletDbError::ParseColumnValueError);
63        };
64
65        Ok((height, hash.clone(), rollback_query.clone()))
66    }
67
68    /// Get a scanned block information record.
69    pub fn get_scanned_block_record(&self, height: u32) -> WalletDbResult<(u32, String, String)> {
70        let row = self.wallet.query_single(
71            WALLET_SCANNED_BLOCKS_TABLE,
72            &[],
73            convert_named_params! {(WALLET_SCANNED_BLOCKS_COL_HEIGH, height)},
74        )?;
75
76        self.parse_scanned_block_record(&row)
77    }
78
79    /// Fetch all scanned block information record.
80    pub fn get_scanned_block_records(&self) -> WalletDbResult<Vec<(u32, String, String)>> {
81        let rows = self.wallet.query_multiple(WALLET_SCANNED_BLOCKS_TABLE, &[], &[])?;
82
83        let mut ret = Vec::with_capacity(rows.len());
84        for row in rows {
85            ret.push(self.parse_scanned_block_record(&row)?);
86        }
87
88        Ok(ret)
89    }
90
91    /// Get the last scanned block height and hash from the wallet.
92    /// If database is empty default (0, '-') is returned.
93    pub fn get_last_scanned_block(&self) -> WalletDbResult<(u32, String)> {
94        let query = format!(
95            "SELECT * FROM {WALLET_SCANNED_BLOCKS_TABLE} ORDER BY {WALLET_SCANNED_BLOCKS_COL_HEIGH} DESC LIMIT 1;"
96        );
97        let ret = self.wallet.query_custom(&query, &[])?;
98
99        if ret.is_empty() {
100            return Ok((0, String::from("-")))
101        }
102
103        let (height, hash, _) = self.parse_scanned_block_record(&ret[0])?;
104
105        Ok((height, hash))
106    }
107
108    /// Reset the scanned blocks information records in the wallet.
109    pub fn reset_scanned_blocks(&self) -> WalletDbResult<()> {
110        println!("Resetting scanned blocks");
111        let query = format!("DELETE FROM {WALLET_SCANNED_BLOCKS_TABLE};");
112        self.wallet.exec_sql(&query, &[])?;
113        println!("Successfully reset scanned blocks");
114
115        Ok(())
116    }
117
118    /// Reset state to provided block height.
119    /// If genesis block height(0) was provided, perform a full reset.
120    pub async fn reset_to_height(&self, height: u32) -> WalletDbResult<()> {
121        println!("Resetting wallet state to block: {height}");
122
123        // If genesis block height(0) was provided,
124        // perform a full reset.
125        if height == 0 {
126            return self.reset().await
127        }
128
129        // Grab last scanned block height
130        let (last, _) = self.get_last_scanned_block()?;
131
132        // Check if requested height is after it
133        if last <= height {
134            println!("Requested block height is greater or equal to last scanned block");
135            return Ok(())
136        }
137
138        // Iterate the range (height, last] in reverse to grab the corresponding blocks
139        for height in (height + 1..=last).rev() {
140            let (height, hash, query) = self.get_scanned_block_record(height)?;
141            println!("Reverting block: {height} - {hash}");
142            self.wallet.exec_batch_sql(&query)?;
143            let query = format!("DELETE FROM {WALLET_SCANNED_BLOCKS_TABLE} WHERE {WALLET_SCANNED_BLOCKS_COL_HEIGH} = {height};");
144            self.wallet.exec_batch_sql(&query)?;
145        }
146
147        println!("Successfully reset wallet state");
148        Ok(())
149    }
150}