Skip to content

Commit

Permalink
sqlite: store if coin is from self
Browse files Browse the repository at this point in the history
This adds a new `is_from_self` column to the coins table that
indicates if the coin is an output of a transaction for which
all inputs are ours. In the case of unconfirmed coins, being
from self requires that all unconfirmed ancestors, if any, are
also from self.

The DB migration sets the value of this column for all existing
coins and the poller keeps the column up to date.
  • Loading branch information
jp1ac4 committed Nov 22, 2024
1 parent bde3299 commit 07b2651
Show file tree
Hide file tree
Showing 6 changed files with 790 additions and 15 deletions.
3 changes: 3 additions & 0 deletions lianad/src/bitcoin/poller/looper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ fn updates(
db_conn.unspend_coins(&updated_coins.expired_spending);
db_conn.spend_coins(&updated_coins.spending);
db_conn.confirm_spend(&updated_coins.spent);
// Update info about which coins are from self only after
// coins have been inserted & updated above.
db_conn.update_coins_from_self(current_tip.height);
if latest_tip != current_tip {
db_conn.update_tip(&latest_tip);
log::debug!("New tip: '{}'", latest_tip);
Expand Down
9 changes: 9 additions & 0 deletions lianad/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ pub trait DatabaseConnection {
/// Store transactions in database, ignoring any that already exist.
fn new_txs(&mut self, txs: &[bitcoin::Transaction]);

/// For all unconfirmed coins and those confirmed after `current_tip_height`,
/// update whether the coin is from self or not.
fn update_coins_from_self(&mut self, current_tip_height: i32);

/// Retrieve a list of transactions and their corresponding block heights and times.
fn list_wallet_transactions(
&mut self,
Expand Down Expand Up @@ -379,6 +383,11 @@ impl DatabaseConnection for SqliteConn {
self.new_txs(txs)
}

fn update_coins_from_self(&mut self, current_tip_height: i32) {
self.update_coins_from_self(current_tip_height)
.expect("must not fail")
}

fn list_wallet_transactions(
&mut self,
txids: &[bitcoin::Txid],
Expand Down
Loading

0 comments on commit 07b2651

Please sign in to comment.