Skip to content

Commit

Permalink
Merge remote-tracking branch 'tnull/2023-07-add-anchor-support' into …
Browse files Browse the repository at this point in the history
…chore/update-anchor-support
  • Loading branch information
rolznz committed Mar 18, 2024
2 parents d614f26 + 32bf929 commit ff70ba2
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use lightning::util::message_signing;
use bdk::blockchain::EsploraBlockchain;
use bdk::database::BatchDatabase;
use bdk::wallet::AddressIndex;
use bdk::FeeRate;
use bdk::{Balance, FeeRate};
use bdk::{SignOptions, SyncOptions};

use bitcoin::address::{Payload, WitnessVersion};
Expand All @@ -34,7 +34,7 @@ use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey, Signing};
use bitcoin::{ScriptBuf, Transaction, TxOut, Txid};

use std::ops::Deref;
use std::sync::{Arc, Condvar, Mutex};
use std::sync::{Arc, Condvar, Mutex, RwLock};
use std::time::Duration;

pub struct Wallet<D, B: Deref, E: Deref, L: Deref>
Expand All @@ -52,6 +52,8 @@ where
broadcaster: B,
fee_estimator: E,
sync_lock: (Mutex<()>, Condvar),
// TODO: Drop this workaround after BDK 1.0 upgrade.
balance_cache: RwLock<Balance>,
logger: L,
}

Expand All @@ -68,7 +70,13 @@ where
) -> Self {
let inner = Mutex::new(wallet);
let sync_lock = (Mutex::new(()), Condvar::new());
Self { blockchain, inner, broadcaster, fee_estimator, sync_lock, logger }
let balance_cache = RwLock::new(Balance {
immature: 0,
trusted_pending: 0,
untrusted_pending: 0,
confirmed: 0,
});
Self { blockchain, inner, broadcaster, fee_estimator, sync_lock, balance_cache, logger }
}

pub(crate) async fn sync(&self) -> Result<(), Error> {
Expand All @@ -88,7 +96,14 @@ where
let sync_options = SyncOptions { progress: None };
let wallet_lock = self.inner.lock().unwrap();
let res = match wallet_lock.sync(&self.blockchain, sync_options).await {
Ok(()) => Ok(()),
Ok(()) => {
// TODO: Drop this workaround after BDK 1.0 upgrade.
// Update balance cache after syncing.
if let Ok(balance) = wallet_lock.get_balance() {
*self.balance_cache.write().unwrap() = balance;
}
Ok(())
},
Err(e) => match e {
bdk::Error::Esplora(ref be) => match **be {
bdk::blockchain::esplora::EsploraError::Reqwest(_) => {
Expand Down Expand Up @@ -172,13 +187,24 @@ where
pub(crate) fn get_balances(
&self, total_anchor_channels_reserve_sats: u64,
) -> Result<(u64, u64), Error> {
let wallet_lock = self.inner.lock().unwrap();
let (total, spendable) = wallet_lock.get_balance().map(|bal| {
(
bal.get_total(),
bal.get_spendable().saturating_sub(total_anchor_channels_reserve_sats),
)
})?;
// TODO: Drop this workaround after BDK 1.0 upgrade.
// We get the balance and update our cache if we can do so without blocking on the wallet
// Mutex. Otherwise, we return a cached value.
let balance = match self.inner.try_lock() {
Ok(wallet_lock) => {
// Update balance cache if we can.
let balance = wallet_lock.get_balance()?;
*self.balance_cache.write().unwrap() = balance.clone();
balance
},
Err(_) => self.balance_cache.read().unwrap().clone(),
};

let (total, spendable) = (
balance.get_total(),
balance.get_spendable().saturating_sub(total_anchor_channels_reserve_sats),
);

Ok((total, spendable))
}

Expand Down

0 comments on commit ff70ba2

Please sign in to comment.