database: add wallet() method and use in getinfo command

This allows us to get several values relating to the wallet
in one call.

The field names of the `Wallet` struct have been chosen to
match the corresponding DB interface methods.

Some sqlite methods will call `wallet()` instead of `db_wallet()`
to ensure consistency in output.
This commit is contained in:
Michael Mallan 2024-10-24 10:25:28 +01:00
parent 4694eaaef9
commit dee9554c51
No known key found for this signature in database
GPG Key ID: 5177CDCEDB0EABEB
3 changed files with 58 additions and 12 deletions

View File

@ -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,
}
}

View File

@ -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<u32>,
/// Timestamp at which the last poll of the blockchain completed, if any,
pub last_poll_timestamp: Option<u32>,
}
pub trait DatabaseInterface: Send {
fn connection(&self) -> Box<dyn DatabaseConnection>;
}
@ -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<u32> {
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<u32> {
self.db_wallet().last_poll_timestamp
self.wallet().last_poll_timestamp
}
fn set_last_poll(&mut self, timestamp: u32) {

View File

@ -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<bitcoin::Txid, bitcoin::Transaction>,
spend_txs: HashMap<bitcoin::Txid, (Psbt, Option<u32>)>,
timestamp: u32,
rescan_timestamp: Option<u32>,
last_poll_timestamp: Option<u32>,
}
@ -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<u32> {
None
self.db.read().unwrap().rescan_timestamp
}
fn set_rescan(&mut self, _: u32) {