use rusqlite::types::Value;
use crate::{
convert_named_params,
error::{WalletDbError, WalletDbResult},
Drk,
};
const WALLET_SCANNED_BLOCKS_TABLE: &str = "scanned_blocks";
const WALLET_SCANNED_BLOCKS_COL_HEIGH: &str = "height";
const WALLET_SCANNED_BLOCKS_COL_HASH: &str = "hash";
const WALLET_SCANNED_BLOCKS_COL_ROLLBACK_QUERY: &str = "rollback_query";
impl Drk {
pub fn put_scanned_block_record(
&self,
height: u32,
hash: &str,
rollback_query: &str,
) -> WalletDbResult<()> {
let query = format!(
"INSERT INTO {} ({}, {}, {}) VALUES (?1, ?2, ?3);",
WALLET_SCANNED_BLOCKS_TABLE,
WALLET_SCANNED_BLOCKS_COL_HEIGH,
WALLET_SCANNED_BLOCKS_COL_HASH,
WALLET_SCANNED_BLOCKS_COL_ROLLBACK_QUERY,
);
self.wallet.exec_sql(&query, rusqlite::params![height, hash, rollback_query])
}
fn parse_scanned_block_record(&self, row: &[Value]) -> WalletDbResult<(u32, String, String)> {
let Value::Integer(height) = row[0] else {
return Err(WalletDbError::ParseColumnValueError);
};
let Ok(height) = u32::try_from(height) else {
return Err(WalletDbError::ParseColumnValueError);
};
let Value::Text(ref hash) = row[1] else {
return Err(WalletDbError::ParseColumnValueError);
};
let Value::Text(ref rollback_query) = row[2] else {
return Err(WalletDbError::ParseColumnValueError);
};
Ok((height, hash.clone(), rollback_query.clone()))
}
pub fn get_scanned_block_record(&self, height: u32) -> WalletDbResult<(u32, String, String)> {
let row = self.wallet.query_single(
WALLET_SCANNED_BLOCKS_TABLE,
&[],
convert_named_params! {(WALLET_SCANNED_BLOCKS_COL_HEIGH, height)},
)?;
self.parse_scanned_block_record(&row)
}
pub fn get_scanned_block_records(&self) -> WalletDbResult<Vec<(u32, String, String)>> {
let rows = self.wallet.query_multiple(WALLET_SCANNED_BLOCKS_TABLE, &[], &[])?;
let mut ret = Vec::with_capacity(rows.len());
for row in rows {
ret.push(self.parse_scanned_block_record(&row)?);
}
Ok(ret)
}
pub fn get_last_scanned_block(&self) -> WalletDbResult<(u32, String)> {
let query = format!(
"SELECT * FROM {} ORDER BY {} DESC LIMIT 1;",
WALLET_SCANNED_BLOCKS_TABLE, WALLET_SCANNED_BLOCKS_COL_HEIGH,
);
let ret = self.wallet.query_custom(&query, &[])?;
if ret.is_empty() {
return Ok((0, String::from("-")))
}
let (height, hash, _) = self.parse_scanned_block_record(&ret[0])?;
Ok((height, hash))
}
pub fn reset_scanned_blocks(&self) -> WalletDbResult<()> {
println!("Resetting scanned blocks");
let query = format!("DELETE FROM {};", WALLET_SCANNED_BLOCKS_TABLE);
self.wallet.exec_sql(&query, &[])?;
println!("Successfully reset scanned blocks");
Ok(())
}
pub async fn reset_to_height(&self, height: u32) -> WalletDbResult<()> {
println!("Resetting wallet state to block: {height}");
if height == 0 {
return self.reset().await
}
let (last, _) = self.get_last_scanned_block()?;
if last <= height {
println!("Requested block height is greater or equal to last scanned block");
return Ok(())
}
for height in (height + 1..=last).rev() {
let (height, hash, query) = self.get_scanned_block_record(height)?;
println!("Reverting block: {height} - {hash}");
self.wallet.exec_batch_sql(&query)?;
let query = format!(
"DELETE FROM {} WHERE {} = {};",
WALLET_SCANNED_BLOCKS_TABLE, WALLET_SCANNED_BLOCKS_COL_HEIGH, height
);
self.wallet.exec_batch_sql(&query)?;
}
println!("Successfully reset wallet state");
Ok(())
}
}