Skip to content

Commit 420e929

Browse files
committed
Merge #1335: fix(chain): tx_graph::ChangeSet::is_empty
13ab5a8 chore(chain): Improve TxGraph::ChangeSet docs (LLFourn) dbbd514 fix(chain)!: rm duplicate `is_empty` method in tx graph changeset (志宇) ae00e1e fix(chain): tx_graph::ChangeSet::is_empty (LLFourn) Pull request description: 🙈 ### Changelog notice - Fix bug in `tx_graph::ChangeSet::is_empty` where is returns true even when it wasn't empty ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### Bugfixes: * [ ] This pull request breaks the existing API * [x] I've added tests to reproduce the issue which are now passing ACKs for top commit: LLFourn: Self-ACK: 13ab5a8 evanlinjin: ACK 13ab5a8 Tree-SHA512: b9f1f17fd2ed0f8e2337a8033e1cbd3e9f15b1ad4b32da3f0eb73a30913d6798e7a08d6b297d93bd08c2e1c388226e97648650ac636846b2c7aa95c3bcefbcfd
2 parents 728e26f + 13ab5a8 commit 420e929

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

crates/chain/src/tx_graph.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,23 @@
4040
//! # use bdk_chain::example_utils::*;
4141
//! # use bitcoin::Transaction;
4242
//! # let tx_a = tx_from_hex(RAW_TX_1);
43-
//! let mut graph: TxGraph = TxGraph::default();
44-
//! let mut another_graph: TxGraph = TxGraph::default();
43+
//! let mut tx_graph: TxGraph = TxGraph::default();
4544
//!
4645
//! // insert a transaction
47-
//! let changeset = graph.insert_tx(tx_a);
46+
//! let changeset = tx_graph.insert_tx(tx_a);
47+
//!
48+
//! // We can restore the state of the `tx_graph` by applying all
49+
//! // the changesets obtained by mutating the original (the order doesn't matter).
50+
//! let mut restored_tx_graph: TxGraph = TxGraph::default();
51+
//! restored_tx_graph.apply_changeset(changeset);
4852
//!
49-
//! // the resulting changeset can be applied to another tx graph
50-
//! another_graph.apply_changeset(changeset);
53+
//! assert_eq!(tx_graph, restored_tx_graph);
5154
//! ```
5255
//!
53-
//! A [`TxGraph`] can also be updated with another [`TxGraph`].
56+
//! A [`TxGraph`] can also be updated with another [`TxGraph`] which merges them together.
5457
//!
5558
//! ```
56-
//! # use bdk_chain::BlockId;
59+
//! # use bdk_chain::{Append, BlockId};
5760
//! # use bdk_chain::tx_graph::TxGraph;
5861
//! # use bdk_chain::example_utils::*;
5962
//! # use bitcoin::Transaction;
@@ -1212,11 +1215,6 @@ impl<A> Default for ChangeSet<A> {
12121215
}
12131216

12141217
impl<A> ChangeSet<A> {
1215-
/// Returns true if the [`ChangeSet`] is empty (no transactions or txouts).
1216-
pub fn is_empty(&self) -> bool {
1217-
self.txs.is_empty() && self.txouts.is_empty()
1218-
}
1219-
12201218
/// Iterates over all outpoints contained within [`ChangeSet`].
12211219
pub fn txouts(&self) -> impl Iterator<Item = (OutPoint, &TxOut)> {
12221220
self.txs

crates/chain/tests/test_tx_graph.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ fn insert_tx_graph_doesnt_count_coinbase_as_spent() {
213213
};
214214

215215
let mut graph = TxGraph::<()>::default();
216-
let _ = graph.insert_tx(tx);
216+
let changeset = graph.insert_tx(tx);
217+
assert!(!changeset.is_empty());
217218
assert!(graph.outspends(OutPoint::null()).is_empty());
218219
assert!(graph.tx_spends(Txid::all_zeros()).next().is_none());
219220
}
@@ -289,7 +290,7 @@ fn insert_tx_displaces_txouts() {
289290
}],
290291
};
291292

292-
let _ = tx_graph.insert_txout(
293+
let changeset = tx_graph.insert_txout(
293294
OutPoint {
294295
txid: tx.txid(),
295296
vout: 0,
@@ -300,6 +301,8 @@ fn insert_tx_displaces_txouts() {
300301
},
301302
);
302303

304+
assert!(!changeset.is_empty());
305+
303306
let _ = tx_graph.insert_txout(
304307
OutPoint {
305308
txid: tx.txid(),
@@ -653,7 +656,8 @@ fn test_walk_ancestors() {
653656
]);
654657

655658
[&tx_a0, &tx_b1].iter().for_each(|&tx| {
656-
let _ = graph.insert_anchor(tx.txid(), tip.block_id());
659+
let changeset = graph.insert_anchor(tx.txid(), tip.block_id());
660+
assert!(!changeset.is_empty());
657661
});
658662

659663
let ancestors = [
@@ -1027,10 +1031,12 @@ fn test_changeset_last_seen_append() {
10271031
last_seen: original_ls.map(|ls| (txid, ls)).into_iter().collect(),
10281032
..Default::default()
10291033
};
1034+
assert!(!original.is_empty() || original_ls.is_none());
10301035
let update = ChangeSet::<()> {
10311036
last_seen: update_ls.map(|ls| (txid, ls)).into_iter().collect(),
10321037
..Default::default()
10331038
};
1039+
assert!(!update.is_empty() || update_ls.is_none());
10341040

10351041
original.append(update);
10361042
assert_eq!(

0 commit comments

Comments
 (0)