From ae00e1ee7b95c622fc3992c0804dd4b4517efc7b Mon Sep 17 00:00:00 2001 From: LLFourn Date: Fri, 9 Feb 2024 19:43:26 +1100 Subject: [PATCH 1/3] fix(chain): tx_graph::ChangeSet::is_empty --- crates/chain/src/tx_graph.rs | 5 ++++- crates/chain/tests/test_tx_graph.rs | 12 +++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index 66d1e9502..cfa9d5f5c 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -1214,7 +1214,10 @@ impl Default for ChangeSet { impl ChangeSet { /// Returns true if the [`ChangeSet`] is empty (no transactions or txouts). pub fn is_empty(&self) -> bool { - self.txs.is_empty() && self.txouts.is_empty() + self.txs.is_empty() + && self.txouts.is_empty() + && self.anchors.is_empty() + && self.last_seen.is_empty() } /// Iterates over all outpoints contained within [`ChangeSet`]. diff --git a/crates/chain/tests/test_tx_graph.rs b/crates/chain/tests/test_tx_graph.rs index a71e24f99..4afdd66e6 100644 --- a/crates/chain/tests/test_tx_graph.rs +++ b/crates/chain/tests/test_tx_graph.rs @@ -213,7 +213,8 @@ fn insert_tx_graph_doesnt_count_coinbase_as_spent() { }; let mut graph = TxGraph::<()>::default(); - let _ = graph.insert_tx(tx); + let changeset = graph.insert_tx(tx); + assert!(!changeset.is_empty()); assert!(graph.outspends(OutPoint::null()).is_empty()); assert!(graph.tx_spends(Txid::all_zeros()).next().is_none()); } @@ -289,7 +290,7 @@ fn insert_tx_displaces_txouts() { }], }; - let _ = tx_graph.insert_txout( + let changeset = tx_graph.insert_txout( OutPoint { txid: tx.txid(), vout: 0, @@ -300,6 +301,8 @@ fn insert_tx_displaces_txouts() { }, ); + assert!(!changeset.is_empty()); + let _ = tx_graph.insert_txout( OutPoint { txid: tx.txid(), @@ -653,7 +656,8 @@ fn test_walk_ancestors() { ]); [&tx_a0, &tx_b1].iter().for_each(|&tx| { - let _ = graph.insert_anchor(tx.txid(), tip.block_id()); + let changeset = graph.insert_anchor(tx.txid(), tip.block_id()); + assert!(!changeset.is_empty()); }); let ancestors = [ @@ -1027,10 +1031,12 @@ fn test_changeset_last_seen_append() { last_seen: original_ls.map(|ls| (txid, ls)).into_iter().collect(), ..Default::default() }; + assert!(!original.is_empty() || original_ls.is_none()); let update = ChangeSet::<()> { last_seen: update_ls.map(|ls| (txid, ls)).into_iter().collect(), ..Default::default() }; + assert!(!update.is_empty() || update_ls.is_none()); original.append(update); assert_eq!( From dbbd51424284a960a95c3801b02dc1973322725f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Sat, 10 Feb 2024 03:35:48 +0800 Subject: [PATCH 2/3] fix(chain)!: rm duplicate `is_empty` method in tx graph changeset --- crates/chain/src/tx_graph.rs | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index cfa9d5f5c..75a81d4ac 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -67,7 +67,10 @@ //! //! // if we apply it again, the resulting changeset will be empty //! let changeset = graph.apply_update(update); -//! assert!(changeset.is_empty()); +//! assert!({ +//! use bdk_chain::Append; +//! changeset.is_empty() +//! }); //! ``` //! [`try_get_chain_position`]: TxGraph::try_get_chain_position //! [`insert_txout`]: TxGraph::insert_txout @@ -1212,14 +1215,6 @@ impl Default for ChangeSet { } impl ChangeSet { - /// Returns true if the [`ChangeSet`] is empty (no transactions or txouts). - pub fn is_empty(&self) -> bool { - self.txs.is_empty() - && self.txouts.is_empty() - && self.anchors.is_empty() - && self.last_seen.is_empty() - } - /// Iterates over all outpoints contained within [`ChangeSet`]. pub fn txouts(&self) -> impl Iterator { self.txs From 13ab5a835d59341e387fbbefec12fe4f48e5f3c8 Mon Sep 17 00:00:00 2001 From: LLFourn Date: Sat, 10 Feb 2024 09:13:08 +1100 Subject: [PATCH 3/3] chore(chain): Improve TxGraph::ChangeSet docs --- crates/chain/src/tx_graph.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/crates/chain/src/tx_graph.rs b/crates/chain/src/tx_graph.rs index 75a81d4ac..4dd7f0a25 100644 --- a/crates/chain/src/tx_graph.rs +++ b/crates/chain/src/tx_graph.rs @@ -40,20 +40,23 @@ //! # use bdk_chain::example_utils::*; //! # use bitcoin::Transaction; //! # let tx_a = tx_from_hex(RAW_TX_1); -//! let mut graph: TxGraph = TxGraph::default(); -//! let mut another_graph: TxGraph = TxGraph::default(); +//! let mut tx_graph: TxGraph = TxGraph::default(); //! //! // insert a transaction -//! let changeset = graph.insert_tx(tx_a); +//! let changeset = tx_graph.insert_tx(tx_a); +//! +//! // We can restore the state of the `tx_graph` by applying all +//! // the changesets obtained by mutating the original (the order doesn't matter). +//! let mut restored_tx_graph: TxGraph = TxGraph::default(); +//! restored_tx_graph.apply_changeset(changeset); //! -//! // the resulting changeset can be applied to another tx graph -//! another_graph.apply_changeset(changeset); +//! assert_eq!(tx_graph, restored_tx_graph); //! ``` //! -//! A [`TxGraph`] can also be updated with another [`TxGraph`]. +//! A [`TxGraph`] can also be updated with another [`TxGraph`] which merges them together. //! //! ``` -//! # use bdk_chain::BlockId; +//! # use bdk_chain::{Append, BlockId}; //! # use bdk_chain::tx_graph::TxGraph; //! # use bdk_chain::example_utils::*; //! # use bitcoin::Transaction; @@ -67,10 +70,7 @@ //! //! // if we apply it again, the resulting changeset will be empty //! let changeset = graph.apply_update(update); -//! assert!({ -//! use bdk_chain::Append; -//! changeset.is_empty() -//! }); +//! assert!(changeset.is_empty()); //! ``` //! [`try_get_chain_position`]: TxGraph::try_get_chain_position //! [`insert_txout`]: TxGraph::insert_txout