diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 7c9e002c..3eca39ac 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -309,10 +309,10 @@ impl DaemonControl { /// Get information about the current state of the daemon pub fn get_info(&self) -> GetInfoResult { let mut db_conn = self.db.connection(); - let block_height = db_conn.chain_tip().map(|tip| tip.height).unwrap_or(0); - let rescan_progress = db_conn - .rescan_timestamp() + let wallet = db_conn.wallet(); + let rescan_progress = wallet + .rescan_timestamp .map(|_| self.bitcoin.rescan_progress().unwrap_or(1.0)); GetInfoResult { version: VERSION.to_string(), @@ -323,8 +323,8 @@ impl DaemonControl { main: self.config.main_descriptor.clone(), }, rescan_progress, - timestamp: db_conn.timestamp(), - last_poll_timestamp: db_conn.last_poll_timestamp(), + timestamp: wallet.timestamp, + last_poll_timestamp: wallet.last_poll_timestamp, } } diff --git a/src/database/mod.rs b/src/database/mod.rs index 3657b7e3..6ffce1e3 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -22,6 +22,23 @@ use std::{ use miniscript::bitcoin::{self, bip32, psbt::Psbt, secp256k1}; +/// Information about the wallet. +/// +/// All timestamps are the number of seconds since the UNIX epoch. +#[derive(Clone, Debug)] +pub struct Wallet { + /// Timestamp at wallet creation time. + pub timestamp: u32, + /// Derivation index for the next receiving address. + pub receive_index: bip32::ChildNumber, + /// Derivation index for the next change address. + pub change_index: bip32::ChildNumber, + /// Timestamp to start rescanning from, if any. + pub rescan_timestamp: Option, + /// Timestamp at which the last poll of the blockchain completed, if any, + pub last_poll_timestamp: Option, +} + pub trait DatabaseInterface: Send { fn connection(&self) -> Box; } @@ -46,6 +63,9 @@ pub trait DatabaseConnection { /// The network we are operating on. fn network(&mut self) -> bitcoin::Network; + /// Get the `Wallet`. + fn wallet(&mut self) -> Wallet; + /// The timestamp at wallet creation time fn timestamp(&mut self) -> u32; @@ -184,8 +204,19 @@ impl DatabaseConnection for SqliteConn { self.db_tip().network } + fn wallet(&mut self) -> Wallet { + let db_wallet = self.db_wallet(); + Wallet { + timestamp: db_wallet.timestamp, + receive_index: db_wallet.deposit_derivation_index, + change_index: db_wallet.change_derivation_index, + rescan_timestamp: db_wallet.rescan_timestamp, + last_poll_timestamp: db_wallet.last_poll_timestamp, + } + } + fn timestamp(&mut self) -> u32 { - self.db_wallet().timestamp + self.wallet().timestamp } fn update_tip(&mut self, tip: &BlockChainTip) { @@ -193,7 +224,7 @@ impl DatabaseConnection for SqliteConn { } fn receive_index(&mut self) -> bip32::ChildNumber { - self.db_wallet().deposit_derivation_index + self.wallet().receive_index } fn set_receive_index( @@ -205,7 +236,7 @@ impl DatabaseConnection for SqliteConn { } fn change_index(&mut self) -> bip32::ChildNumber { - self.db_wallet().change_derivation_index + self.wallet().change_index } fn set_change_index( @@ -217,7 +248,7 @@ impl DatabaseConnection for SqliteConn { } fn rescan_timestamp(&mut self) -> Option { - self.db_wallet().rescan_timestamp + self.wallet().rescan_timestamp } fn set_rescan(&mut self, timestamp: u32) { @@ -229,7 +260,7 @@ impl DatabaseConnection for SqliteConn { } fn last_poll_timestamp(&mut self) -> Option { - self.db_wallet().last_poll_timestamp + self.wallet().last_poll_timestamp } fn set_last_poll(&mut self, timestamp: u32) { diff --git a/src/testutils.rs b/src/testutils.rs index 44dea5ee..3be99169 100644 --- a/src/testutils.rs +++ b/src/testutils.rs @@ -1,7 +1,9 @@ use crate::{ bitcoin::{BitcoinInterface, Block, BlockChainTip, MempoolEntry, SyncProgress, UTxO}, config::{BitcoinConfig, Config}, - database::{BlockInfo, Coin, CoinStatus, DatabaseConnection, DatabaseInterface, LabelItem}, + database::{ + BlockInfo, Coin, CoinStatus, DatabaseConnection, DatabaseInterface, LabelItem, Wallet, + }, descriptors, DaemonControl, DaemonHandle, }; @@ -149,6 +151,7 @@ struct DummyDbState { txs: HashMap, spend_txs: HashMap)>, timestamp: u32, + rescan_timestamp: Option, last_poll_timestamp: Option, } @@ -182,6 +185,7 @@ impl DummyDatabase { txs: HashMap::new(), spend_txs: HashMap::new(), timestamp: now, + rescan_timestamp: None, last_poll_timestamp: None, })), } @@ -203,6 +207,17 @@ impl DatabaseConnection for DummyDatabase { self.db.read().unwrap().curr_tip } + fn wallet(&mut self) -> Wallet { + let db_wallet = self.db.read().unwrap(); + Wallet { + timestamp: db_wallet.timestamp, + receive_index: db_wallet.deposit_index, + change_index: db_wallet.change_index, + rescan_timestamp: db_wallet.rescan_timestamp, + last_poll_timestamp: db_wallet.last_poll_timestamp, + } + } + fn timestamp(&mut self) -> u32 { self.db.read().unwrap().timestamp } @@ -402,7 +417,7 @@ impl DatabaseConnection for DummyDatabase { } fn rescan_timestamp(&mut self) -> Option { - None + self.db.read().unwrap().rescan_timestamp } fn set_rescan(&mut self, _: u32) {