Skip to content

Commit aac32d1

Browse files
replaced teh copied code with new bdk_wallet api
1 parent 403cbb6 commit aac32d1

1 file changed

Lines changed: 25 additions & 133 deletions

File tree

src/wallet/mod.rs

Lines changed: 25 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ use std::sync::{Arc, Mutex};
1313
use bdk_chain::spk_client::{FullScanRequest, SyncRequest};
1414
use bdk_wallet::descriptor::ExtendedDescriptor;
1515
use bdk_wallet::error::{BuildFeeBumpError, CreateTxError};
16-
use bdk_wallet::event::WalletEvent;
1716
#[allow(deprecated)]
1817
use bdk_wallet::SignOptions;
19-
use bdk_wallet::{Balance, KeychainKind, PersistedWallet, Update};
18+
use bdk_wallet::{Balance, KeychainKind, PersistedWallet, Update, WalletEvent};
2019
use bitcoin::address::NetworkUnchecked;
2120
use bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
2221
use bitcoin::blockdata::locktime::absolute::LockTime;
@@ -181,30 +180,13 @@ impl Wallet {
181180
}
182181

183182
let mut locked_wallet = self.inner.lock().expect("lock");
184-
185-
let chain_tip1 = locked_wallet.latest_checkpoint().block_id();
186-
let wallet_txs1 = locked_wallet
187-
.transactions()
188-
.map(|wtx| (wtx.tx_node.txid, (wtx.tx_node.tx.clone(), wtx.chain_position)))
189-
.collect::<std::collections::BTreeMap<
190-
Txid,
191-
(Arc<Transaction>, bdk_chain::ChainPosition<bdk_chain::ConfirmationBlockTime>),
192-
>>();
193-
194-
locked_wallet.apply_unconfirmed_txs(unconfirmed_txs);
195-
locked_wallet.apply_evicted_txs(evicted_txids);
196-
197-
let chain_tip2 = locked_wallet.latest_checkpoint().block_id();
198-
let wallet_txs2 = locked_wallet
199-
.transactions()
200-
.map(|wtx| (wtx.tx_node.txid, (wtx.tx_node.tx.clone(), wtx.chain_position)))
201-
.collect::<std::collections::BTreeMap<
202-
Txid,
203-
(Arc<Transaction>, bdk_chain::ChainPosition<bdk_chain::ConfirmationBlockTime>),
204-
>>();
205-
206-
let events =
207-
wallet_events(&mut *locked_wallet, chain_tip1, chain_tip2, wallet_txs1, wallet_txs2);
183+
let events = locked_wallet
184+
.events_helper(|wallet| {
185+
wallet.apply_unconfirmed_txs(unconfirmed_txs);
186+
wallet.apply_evicted_txs(evicted_txids);
187+
Ok::<(), std::convert::Infallible>(())
188+
})
189+
.expect("infallible");
208190

209191
self.update_payment_store(&mut *locked_wallet, events).map_err(|e| {
210192
log_error!(self.logger, "Failed to update payment store: {}", e);
@@ -504,7 +486,7 @@ impl Wallet {
504486
let mut locked_wallet = self.inner.lock().expect("lock");
505487
let mut locked_persister = self.persister.lock().expect("lock");
506488

507-
locked_wallet.cancel_tx(tx);
489+
reclaim_tx_outputs(&mut locked_wallet, tx);
508490
locked_wallet.persist(&mut locked_persister).map_err(|e| {
509491
log_error!(self.logger, "Failed to persist wallet: {}", e);
510492
Error::PersistenceFailed
@@ -593,7 +575,7 @@ impl Wallet {
593575
/// Builds a temporary drain transaction and returns the maximum amount that would be sent to
594576
/// the drain output, along with the PSBT for further inspection.
595577
///
596-
/// The caller is responsible for cancelling the PSBT via `locked_wallet.cancel_tx()`.
578+
/// The caller is responsible for reclaiming any used addresses from the PSBT.
597579
fn get_max_drain_amount(
598580
&self, locked_wallet: &mut PersistedWallet<KVStoreWalletPersister>,
599581
drain_script: ScriptBuf, cur_anchor_reserve_sats: u64, fee_rate: FeeRate,
@@ -665,7 +647,7 @@ impl Wallet {
665647
None,
666648
)?;
667649

668-
locked_wallet.cancel_tx(&tmp_psbt.unsigned_tx);
650+
reclaim_tx_outputs(&mut locked_wallet, &tmp_psbt.unsigned_tx);
669651

670652
Ok(max_amount)
671653
}
@@ -695,7 +677,7 @@ impl Wallet {
695677
Some(&shared_input),
696678
)?;
697679

698-
locked_wallet.cancel_tx(&tmp_psbt.unsigned_tx);
680+
reclaim_tx_outputs(&mut locked_wallet, &tmp_psbt.unsigned_tx);
699681

700682
Ok(splice_amount)
701683
}
@@ -751,7 +733,7 @@ impl Wallet {
751733
e
752734
})?;
753735

754-
locked_wallet.cancel_tx(&tmp_psbt.unsigned_tx);
736+
reclaim_tx_outputs(&mut locked_wallet, &tmp_psbt.unsigned_tx);
755737

756738
let mut tx_builder = locked_wallet.build_tx();
757739
tx_builder
@@ -1427,6 +1409,18 @@ impl Wallet {
14271409
}
14281410
}
14291411

1412+
fn reclaim_tx_outputs(
1413+
locked_wallet: &mut PersistedWallet<KVStoreWalletPersister>, tx: &Transaction,
1414+
) {
1415+
for output in &tx.output {
1416+
if let Some((keychain, index)) =
1417+
locked_wallet.derivation_of_spk(output.script_pubkey.clone())
1418+
{
1419+
locked_wallet.unmark_used(keychain, index);
1420+
}
1421+
}
1422+
}
1423+
14301424
impl Listen for Wallet {
14311425
fn filtered_block_connected(
14321426
&self, _header: &bitcoin::block::Header,
@@ -1716,105 +1710,3 @@ impl ChangeDestinationSource for WalletKeysManager {
17161710
}
17171711
}
17181712
}
1719-
1720-
// FIXME/TODO: This is copied-over from bdk_wallet and only used to generate `WalletEvent`s after
1721-
// applying mempool transactions. We should drop this when BDK offers to generate events for
1722-
// mempool transactions natively.
1723-
pub(crate) fn wallet_events(
1724-
wallet: &mut bdk_wallet::Wallet, chain_tip1: bdk_chain::BlockId,
1725-
chain_tip2: bdk_chain::BlockId,
1726-
wallet_txs1: std::collections::BTreeMap<
1727-
Txid,
1728-
(Arc<Transaction>, bdk_chain::ChainPosition<bdk_chain::ConfirmationBlockTime>),
1729-
>,
1730-
wallet_txs2: std::collections::BTreeMap<
1731-
Txid,
1732-
(Arc<Transaction>, bdk_chain::ChainPosition<bdk_chain::ConfirmationBlockTime>),
1733-
>,
1734-
) -> Vec<WalletEvent> {
1735-
let mut events: Vec<WalletEvent> = Vec::new();
1736-
1737-
if chain_tip1 != chain_tip2 {
1738-
events.push(WalletEvent::ChainTipChanged { old_tip: chain_tip1, new_tip: chain_tip2 });
1739-
}
1740-
1741-
wallet_txs2.iter().for_each(|(txid2, (tx2, cp2))| {
1742-
if let Some((tx1, cp1)) = wallet_txs1.get(txid2) {
1743-
assert_eq!(tx1.compute_txid(), *txid2);
1744-
match (cp1, cp2) {
1745-
(
1746-
bdk_chain::ChainPosition::Unconfirmed { .. },
1747-
bdk_chain::ChainPosition::Confirmed { anchor, .. },
1748-
) => {
1749-
events.push(WalletEvent::TxConfirmed {
1750-
txid: *txid2,
1751-
tx: tx2.clone(),
1752-
block_time: *anchor,
1753-
old_block_time: None,
1754-
});
1755-
},
1756-
(
1757-
bdk_chain::ChainPosition::Confirmed { anchor, .. },
1758-
bdk_chain::ChainPosition::Unconfirmed { .. },
1759-
) => {
1760-
events.push(WalletEvent::TxUnconfirmed {
1761-
txid: *txid2,
1762-
tx: tx2.clone(),
1763-
old_block_time: Some(*anchor),
1764-
});
1765-
},
1766-
(
1767-
bdk_chain::ChainPosition::Confirmed { anchor: anchor1, .. },
1768-
bdk_chain::ChainPosition::Confirmed { anchor: anchor2, .. },
1769-
) => {
1770-
if *anchor1 != *anchor2 {
1771-
events.push(WalletEvent::TxConfirmed {
1772-
txid: *txid2,
1773-
tx: tx2.clone(),
1774-
block_time: *anchor2,
1775-
old_block_time: Some(*anchor1),
1776-
});
1777-
}
1778-
},
1779-
(
1780-
bdk_chain::ChainPosition::Unconfirmed { .. },
1781-
bdk_chain::ChainPosition::Unconfirmed { .. },
1782-
) => {
1783-
// do nothing if still unconfirmed
1784-
},
1785-
}
1786-
} else {
1787-
match cp2 {
1788-
bdk_chain::ChainPosition::Confirmed { anchor, .. } => {
1789-
events.push(WalletEvent::TxConfirmed {
1790-
txid: *txid2,
1791-
tx: tx2.clone(),
1792-
block_time: *anchor,
1793-
old_block_time: None,
1794-
});
1795-
},
1796-
bdk_chain::ChainPosition::Unconfirmed { .. } => {
1797-
events.push(WalletEvent::TxUnconfirmed {
1798-
txid: *txid2,
1799-
tx: tx2.clone(),
1800-
old_block_time: None,
1801-
});
1802-
},
1803-
}
1804-
}
1805-
});
1806-
1807-
// find tx that are no longer canonical
1808-
wallet_txs1.iter().for_each(|(txid1, (tx1, _))| {
1809-
if !wallet_txs2.contains_key(txid1) {
1810-
let conflicts = wallet.tx_graph().direct_conflicts(tx1).collect::<Vec<_>>();
1811-
if !conflicts.is_empty() {
1812-
events.push(WalletEvent::TxReplaced { txid: *txid1, tx: tx1.clone(), conflicts });
1813-
} else {
1814-
events.push(WalletEvent::TxDropped { txid: *txid1, tx: tx1.clone() });
1815-
}
1816-
}
1817-
});
1818-
1819-
events
1820-
}

0 commit comments

Comments
 (0)