Skip to content

Commit b384487

Browse files
committed
Use BDK mempool wallet events
Use BDK's wallet event helper for mempool updates. This removes the local event diffing copy now that BDK exposes the needed event API. Co-Authored-By: HAL 9000
1 parent 678732d commit b384487

1 file changed

Lines changed: 7 additions & 125 deletions

File tree

src/wallet/mod.rs

Lines changed: 7 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -185,29 +185,13 @@ impl Wallet {
185185

186186
let mut locked_wallet = self.inner.lock().expect("lock");
187187

188-
let chain_tip1 = locked_wallet.latest_checkpoint().block_id();
189-
let wallet_txs1 = locked_wallet
190-
.transactions()
191-
.map(|wtx| (wtx.tx_node.txid, (wtx.tx_node.tx.clone(), wtx.chain_position)))
192-
.collect::<std::collections::BTreeMap<
193-
Txid,
194-
(Arc<Transaction>, bdk_chain::ChainPosition<bdk_chain::ConfirmationBlockTime>),
195-
>>();
196-
197-
locked_wallet.apply_unconfirmed_txs(unconfirmed_txs);
198-
locked_wallet.apply_evicted_txs(evicted_txids);
199-
200-
let chain_tip2 = locked_wallet.latest_checkpoint().block_id();
201-
let wallet_txs2 = locked_wallet
202-
.transactions()
203-
.map(|wtx| (wtx.tx_node.txid, (wtx.tx_node.tx.clone(), wtx.chain_position)))
204-
.collect::<std::collections::BTreeMap<
205-
Txid,
206-
(Arc<Transaction>, bdk_chain::ChainPosition<bdk_chain::ConfirmationBlockTime>),
207-
>>();
208-
209-
let events =
210-
wallet_events(&mut *locked_wallet, chain_tip1, chain_tip2, wallet_txs1, wallet_txs2);
188+
let events = locked_wallet
189+
.events_helper(|wallet| -> Result<(), std::convert::Infallible> {
190+
wallet.apply_unconfirmed_txs(unconfirmed_txs);
191+
wallet.apply_evicted_txs(evicted_txids);
192+
Ok(())
193+
})
194+
.expect("applying mempool updates cannot fail");
211195

212196
self.update_payment_store(&mut *locked_wallet, events).map_err(|e| {
213197
log_error!(self.logger, "Failed to update payment store: {}", e);
@@ -1765,105 +1749,3 @@ fn ldk_to_bdk_satisfaction_weight(ldk_satisfaction_weight: u64) -> Weight {
17651749
.saturating_sub(EMPTY_SCRIPT_SIG_WEIGHT + EMPTY_WITNESS_COUNT_WEIGHT),
17661750
)
17671751
}
1768-
1769-
// FIXME/TODO: This is copied-over from bdk_wallet and only used to generate `WalletEvent`s after
1770-
// applying mempool transactions. We should drop this when BDK offers to generate events for
1771-
// mempool transactions natively.
1772-
pub(crate) fn wallet_events(
1773-
wallet: &mut bdk_wallet::Wallet, chain_tip1: bdk_chain::BlockId,
1774-
chain_tip2: bdk_chain::BlockId,
1775-
wallet_txs1: std::collections::BTreeMap<
1776-
Txid,
1777-
(Arc<Transaction>, bdk_chain::ChainPosition<bdk_chain::ConfirmationBlockTime>),
1778-
>,
1779-
wallet_txs2: std::collections::BTreeMap<
1780-
Txid,
1781-
(Arc<Transaction>, bdk_chain::ChainPosition<bdk_chain::ConfirmationBlockTime>),
1782-
>,
1783-
) -> Vec<WalletEvent> {
1784-
let mut events: Vec<WalletEvent> = Vec::new();
1785-
1786-
if chain_tip1 != chain_tip2 {
1787-
events.push(WalletEvent::ChainTipChanged { old_tip: chain_tip1, new_tip: chain_tip2 });
1788-
}
1789-
1790-
wallet_txs2.iter().for_each(|(txid2, (tx2, cp2))| {
1791-
if let Some((tx1, cp1)) = wallet_txs1.get(txid2) {
1792-
assert_eq!(tx1.compute_txid(), *txid2);
1793-
match (cp1, cp2) {
1794-
(
1795-
bdk_chain::ChainPosition::Unconfirmed { .. },
1796-
bdk_chain::ChainPosition::Confirmed { anchor, .. },
1797-
) => {
1798-
events.push(WalletEvent::TxConfirmed {
1799-
txid: *txid2,
1800-
tx: tx2.clone(),
1801-
block_time: *anchor,
1802-
old_block_time: None,
1803-
});
1804-
},
1805-
(
1806-
bdk_chain::ChainPosition::Confirmed { anchor, .. },
1807-
bdk_chain::ChainPosition::Unconfirmed { .. },
1808-
) => {
1809-
events.push(WalletEvent::TxUnconfirmed {
1810-
txid: *txid2,
1811-
tx: tx2.clone(),
1812-
old_block_time: Some(*anchor),
1813-
});
1814-
},
1815-
(
1816-
bdk_chain::ChainPosition::Confirmed { anchor: anchor1, .. },
1817-
bdk_chain::ChainPosition::Confirmed { anchor: anchor2, .. },
1818-
) => {
1819-
if *anchor1 != *anchor2 {
1820-
events.push(WalletEvent::TxConfirmed {
1821-
txid: *txid2,
1822-
tx: tx2.clone(),
1823-
block_time: *anchor2,
1824-
old_block_time: Some(*anchor1),
1825-
});
1826-
}
1827-
},
1828-
(
1829-
bdk_chain::ChainPosition::Unconfirmed { .. },
1830-
bdk_chain::ChainPosition::Unconfirmed { .. },
1831-
) => {
1832-
// do nothing if still unconfirmed
1833-
},
1834-
}
1835-
} else {
1836-
match cp2 {
1837-
bdk_chain::ChainPosition::Confirmed { anchor, .. } => {
1838-
events.push(WalletEvent::TxConfirmed {
1839-
txid: *txid2,
1840-
tx: tx2.clone(),
1841-
block_time: *anchor,
1842-
old_block_time: None,
1843-
});
1844-
},
1845-
bdk_chain::ChainPosition::Unconfirmed { .. } => {
1846-
events.push(WalletEvent::TxUnconfirmed {
1847-
txid: *txid2,
1848-
tx: tx2.clone(),
1849-
old_block_time: None,
1850-
});
1851-
},
1852-
}
1853-
}
1854-
});
1855-
1856-
// find tx that are no longer canonical
1857-
wallet_txs1.iter().for_each(|(txid1, (tx1, _))| {
1858-
if !wallet_txs2.contains_key(txid1) {
1859-
let conflicts = wallet.tx_graph().direct_conflicts(tx1).collect::<Vec<_>>();
1860-
if !conflicts.is_empty() {
1861-
events.push(WalletEvent::TxReplaced { txid: *txid1, tx: tx1.clone(), conflicts });
1862-
} else {
1863-
events.push(WalletEvent::TxDropped { txid: *txid1, tx: tx1.clone() });
1864-
}
1865-
}
1866-
});
1867-
1868-
events
1869-
}

0 commit comments

Comments
 (0)