Skip to content

Commit f1f13a4

Browse files
committed
f BDK: Account for Balance changes
.. and drop the balance cache.
1 parent 03be78d commit f1f13a4

File tree

1 file changed

+7
-35
lines changed

1 file changed

+7
-35
lines changed

src/wallet/mod.rs

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use lightning_invoice::RawBolt11Invoice;
2626

2727
use bdk::blockchain::EsploraBlockchain;
2828
use bdk::wallet::AddressIndex;
29-
use bdk::{Balance, SignOptions, SyncOptions};
29+
use bdk::{SignOptions, SyncOptions};
3030
use bdk_chain::ChainPosition;
3131
use bdk_wallet::Wallet as BdkWallet;
3232

@@ -41,7 +41,7 @@ use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey, Signing};
4141
use bitcoin::{ScriptBuf, Transaction, TxOut, Txid, WPubkeyHash, WitnessProgram, WitnessVersion};
4242

4343
use std::ops::{Deref, DerefMut};
44-
use std::sync::{Arc, Mutex, RwLock};
44+
use std::sync::{Arc, Mutex};
4545
use std::time::Duration;
4646

4747
enum WalletSyncStatus {
@@ -64,8 +64,6 @@ where
6464
fee_estimator: E,
6565
// A Mutex holding the current sync status.
6666
sync_status: Mutex<WalletSyncStatus>,
67-
// TODO: Drop this workaround after BDK 1.0 upgrade.
68-
balance_cache: RwLock<Balance>,
6967
logger: L,
7068
}
7169

@@ -79,17 +77,9 @@ where
7977
blockchain: EsploraBlockchain, wallet: BdkWallet, broadcaster: B, fee_estimator: E,
8078
logger: L,
8179
) -> Self {
82-
let start_balance = wallet.get_balance().unwrap_or(Balance {
83-
immature: 0,
84-
trusted_pending: 0,
85-
untrusted_pending: 0,
86-
confirmed: 0,
87-
});
88-
8980
let inner = Mutex::new(wallet);
9081
let sync_status = Mutex::new(WalletSyncStatus::Completed);
91-
let balance_cache = RwLock::new(start_balance);
92-
Self { blockchain, inner, broadcaster, fee_estimator, sync_status, balance_cache, logger }
82+
Self { blockchain, inner, broadcaster, fee_estimator, sync_status, logger }
9383
}
9484

9585
pub(crate) async fn sync(&self) -> Result<(), Error> {
@@ -112,14 +102,7 @@ where
112102

113103
match wallet_sync_timeout_fut.await {
114104
Ok(res) => match res {
115-
Ok(()) => {
116-
// TODO: Drop this workaround after BDK 1.0 upgrade.
117-
// Update balance cache after syncing.
118-
if let Ok(balance) = wallet_lock.get_balance() {
119-
*self.balance_cache.write().unwrap() = balance;
120-
}
121-
Ok(())
122-
},
105+
Ok(()) => Ok(()),
123106
Err(e) => match e {
124107
bdk::Error::Esplora(ref be) => match **be {
125108
bdk::blockchain::esplora::EsploraError::Reqwest(_) => {
@@ -208,22 +191,11 @@ where
208191
pub(crate) fn get_balances(
209192
&self, total_anchor_channels_reserve_sats: u64,
210193
) -> Result<(u64, u64), Error> {
211-
// TODO: Drop this workaround after BDK 1.0 upgrade.
212-
// We get the balance and update our cache if we can do so without blocking on the wallet
213-
// Mutex. Otherwise, we return a cached value.
214-
let balance = match self.inner.try_lock() {
215-
Ok(wallet_lock) => {
216-
// Update balance cache if we can.
217-
let balance = wallet_lock.get_balance()?;
218-
*self.balance_cache.write().unwrap() = balance.clone();
219-
balance
220-
},
221-
Err(_) => self.balance_cache.read().unwrap().clone(),
222-
};
194+
let balance = self.inner.lock().unwrap().balance();
223195

224196
let (total, spendable) = (
225-
balance.get_total(),
226-
balance.get_spendable().saturating_sub(total_anchor_channels_reserve_sats),
197+
balance.total().to_sat(),
198+
balance.trusted_spendable().to_sat().saturating_sub(total_anchor_channels_reserve_sats),
227199
);
228200

229201
Ok((total, spendable))

0 commit comments

Comments
 (0)