Skip to content

Commit

Permalink
Merge bitcoindevkit#1335: fix(chain): tx_graph::ChangeSet::is_empty
Browse files Browse the repository at this point in the history
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
  • Loading branch information
evanlinjin committed Feb 11, 2024
2 parents 728e26f + 13ab5a8 commit 420e929
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
22 changes: 10 additions & 12 deletions crates/chain/src/tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1212,11 +1215,6 @@ impl<A> Default for ChangeSet<A> {
}

impl<A> ChangeSet<A> {
/// 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()
}

/// Iterates over all outpoints contained within [`ChangeSet`].
pub fn txouts(&self) -> impl Iterator<Item = (OutPoint, &TxOut)> {
self.txs
Expand Down
12 changes: 9 additions & 3 deletions crates/chain/tests/test_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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,
Expand All @@ -300,6 +301,8 @@ fn insert_tx_displaces_txouts() {
},
);

assert!(!changeset.is_empty());

let _ = tx_graph.insert_txout(
OutPoint {
txid: tx.txid(),
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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!(
Expand Down

0 comments on commit 420e929

Please sign in to comment.