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 */
1819use rusqlite::types::Value;
2021use crate::{
22 convert_named_params,
23 error::{WalletDbError, WalletDbResult},
24 Drk,
25};
2627// 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";
3334impl Drk {
35/// Insert a scanned block information record into the wallet.
36pub fn put_scanned_block_record(
37&self,
38 height: u32,
39 hash: &str,
40 rollback_query: &str,
41 ) -> WalletDbResult<()> {
42let 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 );
49self.wallet.exec_sql(&query, rusqlite::params![height, hash, rollback_query])
50 }
5152/// Auxiliary function to parse a `WALLET_SCANNED_BLOCKS_TABLE` records.
53fn parse_scanned_block_record(&self, row: &[Value]) -> WalletDbResult<(u32, String, String)> {
54let Value::Integer(height) = row[0] else {
55return Err(WalletDbError::ParseColumnValueError);
56 };
57let Ok(height) = u32::try_from(height) else {
58return Err(WalletDbError::ParseColumnValueError);
59 };
6061let Value::Text(ref hash) = row[1] else {
62return Err(WalletDbError::ParseColumnValueError);
63 };
6465let Value::Text(ref rollback_query) = row[2] else {
66return Err(WalletDbError::ParseColumnValueError);
67 };
6869Ok((height, hash.clone(), rollback_query.clone()))
70 }
7172/// Get a scanned block information record.
73pub fn get_scanned_block_record(&self, height: u32) -> WalletDbResult<(u32, String, String)> {
74let row = self.wallet.query_single(
75 WALLET_SCANNED_BLOCKS_TABLE,
76&[],
77convert_named_params! {(WALLET_SCANNED_BLOCKS_COL_HEIGH, height)},
78 )?;
7980self.parse_scanned_block_record(&row)
81 }
8283/// Fetch all scanned block information record.
84pub fn get_scanned_block_records(&self) -> WalletDbResult<Vec<(u32, String, String)>> {
85let rows = self.wallet.query_multiple(WALLET_SCANNED_BLOCKS_TABLE, &[], &[])?;
8687let mut ret = Vec::with_capacity(rows.len());
88for row in rows {
89 ret.push(self.parse_scanned_block_record(&row)?);
90 }
9192Ok(ret)
93 }
9495/// Get the last scanned block height and hash from the wallet.
96 /// If database is empty default (0, '-') is returned.
97pub fn get_last_scanned_block(&self) -> WalletDbResult<(u32, String)> {
98let query = format!(
99"SELECT * FROM {} ORDER BY {} DESC LIMIT 1;",
100 WALLET_SCANNED_BLOCKS_TABLE, WALLET_SCANNED_BLOCKS_COL_HEIGH,
101 );
102let ret = self.wallet.query_custom(&query, &[])?;
103104if ret.is_empty() {
105return Ok((0, String::from("-")))
106 }
107108let (height, hash, _) = self.parse_scanned_block_record(&ret[0])?;
109110Ok((height, hash))
111 }
112113/// Reset the scanned blocks information records in the wallet.
114pub fn reset_scanned_blocks(&self) -> WalletDbResult<()> {
115println!("Resetting scanned blocks");
116let query = format!("DELETE FROM {};", WALLET_SCANNED_BLOCKS_TABLE);
117self.wallet.exec_sql(&query, &[])?;
118println!("Successfully reset scanned blocks");
119120Ok(())
121 }
122123/// Reset state to provided block height.
124 /// If genesis block height(0) was provided, perform a full reset.
125pub async fn reset_to_height(&self, height: u32) -> WalletDbResult<()> {
126println!("Resetting wallet state to block: {height}");
127128// If genesis block height(0) was provided,
129 // perform a full reset.
130if height == 0 {
131return self.reset().await
132}
133134// Grab last scanned block height
135let (last, _) = self.get_last_scanned_block()?;
136137// Check if requested height is after it
138if last <= height {
139println!("Requested block height is greater or equal to last scanned block");
140return Ok(())
141 }
142143// Iterate the range (height, last] in reverse to grab the corresponding blocks
144for height in (height + 1..=last).rev() {
145let (height, hash, query) = self.get_scanned_block_record(height)?;
146println!("Reverting block: {height} - {hash}");
147self.wallet.exec_batch_sql(&query)?;
148let query = format!(
149"DELETE FROM {} WHERE {} = {};",
150 WALLET_SCANNED_BLOCKS_TABLE, WALLET_SCANNED_BLOCKS_COL_HEIGH, height
151 );
152self.wallet.exec_batch_sql(&query)?;
153 }
154155println!("Successfully reset wallet state");
156Ok(())
157 }
158}