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 {} ({}, {}, {}) VALUES (?1, ?2, ?3);",
44            WALLET_SCANNED_BLOCKS_TABLE,
45            WALLET_SCANNED_BLOCKS_COL_HEIGH,
46            WALLET_SCANNED_BLOCKS_COL_HASH,
47            WALLET_SCANNED_BLOCKS_COL_ROLLBACK_QUERY,
48        );
49        self.wallet.exec_sql(&query, rusqlite::params![height, hash, rollback_query])
50    }
51
52    /// Auxiliary function to parse a `WALLET_SCANNED_BLOCKS_TABLE` records.
53    fn parse_scanned_block_record(&self, row: &[Value]) -> WalletDbResult<(u32, String, String)> {
54        let Value::Integer(height) = row[0] else {
55            return Err(WalletDbError::ParseColumnValueError);
56        };
57        let Ok(height) = u32::try_from(height) else {
58            return Err(WalletDbError::ParseColumnValueError);
59        };
60
61        let Value::Text(ref hash) = row[1] else {
62            return Err(WalletDbError::ParseColumnValueError);
63        };
64
65        let Value::Text(ref rollback_query) = row[2] else {
66            return Err(WalletDbError::ParseColumnValueError);
67        };
68
69        Ok((height, hash.clone(), rollback_query.clone()))
70    }
71
72    /// Get a scanned block information record.
73    pub fn get_scanned_block_record(&self, height: u32) -> WalletDbResult<(u32, String, String)> {
74        let row = self.wallet.query_single(
75            WALLET_SCANNED_BLOCKS_TABLE,
76            &[],
77            convert_named_params! {(WALLET_SCANNED_BLOCKS_COL_HEIGH, height)},
78        )?;
79
80        self.parse_scanned_block_record(&row)
81    }
82
83    /// Fetch all scanned block information record.
84    pub fn get_scanned_block_records(&self) -> WalletDbResult<Vec<(u32, String, String)>> {
85        let rows = self.wallet.query_multiple(WALLET_SCANNED_BLOCKS_TABLE, &[], &[])?;
86
87        let mut ret = Vec::with_capacity(rows.len());
88        for row in rows {
89            ret.push(self.parse_scanned_block_record(&row)?);
90        }
91
92        Ok(ret)
93    }
94
95    /// Get the last scanned block height and hash from the wallet.
96    /// If database is empty default (0, '-') is returned.
97    pub fn get_last_scanned_block(&self) -> WalletDbResult<(u32, String)> {
98        let query = format!(
99            "SELECT * FROM {} ORDER BY {} DESC LIMIT 1;",
100            WALLET_SCANNED_BLOCKS_TABLE, WALLET_SCANNED_BLOCKS_COL_HEIGH,
101        );
102        let ret = self.wallet.query_custom(&query, &[])?;
103
104        if ret.is_empty() {
105            return Ok((0, String::from("-")))
106        }
107
108        let (height, hash, _) = self.parse_scanned_block_record(&ret[0])?;
109
110        Ok((height, hash))
111    }
112
113    /// Reset the scanned blocks information records in the wallet.
114    pub fn reset_scanned_blocks(&self) -> WalletDbResult<()> {
115        println!("Resetting scanned blocks");
116        let query = format!("DELETE FROM {};", WALLET_SCANNED_BLOCKS_TABLE);
117        self.wallet.exec_sql(&query, &[])?;
118        println!("Successfully reset scanned blocks");
119
120        Ok(())
121    }
122
123    /// Reset state to provided block height.
124    /// If genesis block height(0) was provided, perform a full reset.
125    pub async fn reset_to_height(&self, height: u32) -> WalletDbResult<()> {
126        println!("Resetting wallet state to block: {height}");
127
128        // If genesis block height(0) was provided,
129        // perform a full reset.
130        if height == 0 {
131            return self.reset().await
132        }
133
134        // Grab last scanned block height
135        let (last, _) = self.get_last_scanned_block()?;
136
137        // Check if requested height is after it
138        if last <= height {
139            println!("Requested block height is greater or equal to last scanned block");
140            return Ok(())
141        }
142
143        // Iterate the range (height, last] in reverse to grab the corresponding blocks
144        for height in (height + 1..=last).rev() {
145            let (height, hash, query) = self.get_scanned_block_record(height)?;
146            println!("Reverting block: {height} - {hash}");
147            self.wallet.exec_batch_sql(&query)?;
148            let query = format!(
149                "DELETE FROM {} WHERE {} = {};",
150                WALLET_SCANNED_BLOCKS_TABLE, WALLET_SCANNED_BLOCKS_COL_HEIGH, height
151            );
152            self.wallet.exec_batch_sql(&query)?;
153        }
154
155        println!("Successfully reset wallet state");
156        Ok(())
157    }
158}