Skip to content

Commit 82feeed

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 52905ac commit 82feeed

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

0 commit comments

Comments
 (0)