impl<A: Ord> From<TxGraph<A>> for TxUpdate<A> {
fn from(graph: TxGraph<A>) -> Self {
- Self {
- txs: graph.full_txs().map(|tx_node| tx_node.tx).collect(),
- txouts: graph
- .floating_txouts()
- .map(|(op, txo)| (op, txo.clone()))
- .collect(),
- anchors: graph
- .anchors
- .into_iter()
- .flat_map(|(txid, anchors)| anchors.into_iter().map(move |a| (a, txid)))
- .collect(),
- seen_ats: graph.last_seen.into_iter().collect(),
- }
+ let mut tx_update = TxUpdate::default();
+ tx_update.txs = graph.full_txs().map(|tx_node| tx_node.tx).collect();
+ tx_update.txouts = graph
+ .floating_txouts()
+ .map(|(op, txo)| (op, txo.clone()))
+ .collect();
+ tx_update.anchors = graph
+ .anchors
+ .into_iter()
+ .flat_map(|(txid, anchors)| anchors.into_iter().map(move |a| (a, txid)))
+ .collect();
+ tx_update.seen_ats = graph.last_seen.into_iter().collect();
+ tx_update
}
}
let test_cases: &[TestCase] = &[
("empty_update", TxUpdate::default()),
- (
- "single_tx",
- TxUpdate {
- txs: vec![make_tx(0).into()],
- ..Default::default()
- },
- ),
- (
- "two_txs",
- TxUpdate {
- txs: vec![make_tx(0).into(), make_tx(1).into()],
- ..Default::default()
- },
- ),
- (
- "with_floating_txouts",
- TxUpdate {
- txs: vec![make_tx(0).into(), make_tx(1).into()],
- txouts: [
- (OutPoint::new(hash!("a"), 0), make_txout(0)),
- (OutPoint::new(hash!("a"), 1), make_txout(1)),
- (OutPoint::new(hash!("b"), 0), make_txout(2)),
- ]
- .into(),
- ..Default::default()
- },
- ),
- (
- "with_anchors",
- TxUpdate {
- txs: vec![make_tx(0).into(), make_tx(1).into()],
- txouts: [
- (OutPoint::new(hash!("a"), 0), make_txout(0)),
- (OutPoint::new(hash!("a"), 1), make_txout(1)),
- (OutPoint::new(hash!("b"), 0), make_txout(2)),
- ]
- .into(),
- anchors: [
- (ConfirmationBlockTime::default(), hash!("a")),
- (ConfirmationBlockTime::default(), hash!("b")),
- ]
- .into(),
- ..Default::default()
- },
- ),
- (
- "with_seen_ats",
- TxUpdate {
- txs: vec![make_tx(0).into(), make_tx(1).into()],
- txouts: [
- (OutPoint::new(hash!("a"), 0), make_txout(0)),
- (OutPoint::new(hash!("a"), 1), make_txout(1)),
- (OutPoint::new(hash!("d"), 0), make_txout(2)),
- ]
- .into(),
- anchors: [
- (ConfirmationBlockTime::default(), hash!("a")),
- (ConfirmationBlockTime::default(), hash!("b")),
- ]
- .into(),
- seen_ats: [(hash!("c"), 12346)].into_iter().collect(),
- },
- ),
+ ("single_tx", {
+ let mut tx_update = TxUpdate::default();
+ tx_update.txs = vec![make_tx(0).into()];
+ tx_update
+ }),
+ ("two_txs", {
+ let mut tx_update = TxUpdate::default();
+ tx_update.txs = vec![make_tx(0).into(), make_tx(1).into()];
+ tx_update
+ }),
+ ("with_floating_txouts", {
+ let mut tx_update = TxUpdate::default();
+ tx_update.txs = vec![make_tx(0).into(), make_tx(1).into()];
+ tx_update.txouts = [
+ (OutPoint::new(hash!("a"), 0), make_txout(0)),
+ (OutPoint::new(hash!("a"), 1), make_txout(1)),
+ (OutPoint::new(hash!("b"), 0), make_txout(2)),
+ ]
+ .into();
+ tx_update
+ }),
+ ("with_anchors", {
+ let mut tx_update = TxUpdate::default();
+ tx_update.txs = vec![make_tx(0).into(), make_tx(1).into()];
+ tx_update.txouts = [
+ (OutPoint::new(hash!("a"), 0), make_txout(0)),
+ (OutPoint::new(hash!("a"), 1), make_txout(1)),
+ (OutPoint::new(hash!("b"), 0), make_txout(2)),
+ ]
+ .into();
+ tx_update.anchors = [
+ (ConfirmationBlockTime::default(), hash!("a")),
+ (ConfirmationBlockTime::default(), hash!("b")),
+ ]
+ .into();
+ tx_update
+ }),
+ ("with_seen_ats", {
+ let mut tx_update = TxUpdate::default();
+ tx_update.txs = vec![make_tx(0).into(), make_tx(1).into()];
+ tx_update.txouts = [
+ (OutPoint::new(hash!("a"), 0), make_txout(0)),
+ (OutPoint::new(hash!("a"), 1), make_txout(1)),
+ (OutPoint::new(hash!("d"), 0), make_txout(2)),
+ ]
+ .into();
+ tx_update.anchors = [
+ (ConfirmationBlockTime::default(), hash!("a")),
+ (ConfirmationBlockTime::default(), hash!("b")),
+ ]
+ .into();
+ tx_update.seen_ats = [(hash!("c"), 12346)].into_iter().collect();
+ tx_update
+ }),
];
for (test_name, update) in test_cases {
/// Data object used to communicate updates about relevant transactions from some chain data source
/// to the core model (usually a `bdk_chain::TxGraph`).
+///
+/// ```rust
+/// use bdk_core::TxUpdate;
+/// # use std::sync::Arc;
+/// # use bitcoin::{Transaction, transaction::Version, absolute::LockTime};
+/// # let version = Version::ONE;
+/// # let lock_time = LockTime::ZERO;
+/// # let tx = Arc::new(Transaction { input: vec![], output: vec![], version, lock_time });
+/// # let txid = tx.compute_txid();
+/// # let anchor = ();
+/// let mut tx_update = TxUpdate::default();
+/// tx_update.txs.push(tx);
+/// tx_update.anchors.insert((anchor, txid));
+/// ```
#[derive(Debug, Clone)]
+#[non_exhaustive]
pub struct TxUpdate<A = ()> {
/// Full transactions. These are transactions that were determined to be relevant to the wallet
/// given the request.