Skip to content

Commit

Permalink
Merge bitcoindevkit#1502: chore(chain)!: Rename Append to Merge
Browse files Browse the repository at this point in the history
962305f chore(chain)!: Rename `Append` to `Merge` (Wei Chen)

Pull request description:

  <!-- You can erase any parts of this template not applicable to your Pull Request. -->
  Fixes bitcoindevkit#1467.

  ### Description

  <!-- Describe the purpose of this PR, what's being adding and/or fixed -->
  Renames the `Append` trait to `Merge`.

  ### Notes to the reviewers

  <!-- In this section you can include notes directed to the reviewers, like explaining why some parts
  of the PR were done in a specific way -->

  ### Changelog notice

  <!-- Notice the release manager should include in the release tag message changelog -->
  <!-- See https://keepachangelog.com/en/1.0.0/ for examples -->
  * Rename `bdk_chain::Append` to `bdk_chain::Merge`.

  ### 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

ACKs for top commit:
  notmandatory:
    ACK 962305f

Tree-SHA512: 2019d9ed631776850cda39c9a53bdd3660c7f4715d8c418824d8ad13718f2b2fd160159e03fd63743dbf0b9e91cfdfed7e4cd4a591636f67d2aaec419486d136
  • Loading branch information
notmandatory committed Jul 8, 2024
2 parents db8fbd7 + 962305f commit 43f093d
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 117 deletions.
2 changes: 1 addition & 1 deletion crates/bitcoind_rpc/tests/test_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bdk_bitcoind_rpc::Emitter;
use bdk_chain::{
bitcoin::{Address, Amount, Txid},
local_chain::{CheckPoint, LocalChain},
Append, Balance, BlockId, IndexedTxGraph, SpkTxOutIndex,
Balance, BlockId, IndexedTxGraph, Merge, SpkTxOutIndex,
};
use bdk_testenv::{anyhow, TestEnv};
use bitcoin::{hashes::Hash, Block, OutPoint, ScriptBuf, WScriptHash};
Expand Down
8 changes: 4 additions & 4 deletions crates/chain/src/changeset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ impl<K, A> core::default::Default for CombinedChangeSet<K, A> {
}

#[cfg(feature = "miniscript")]
impl<K: Ord, A: crate::Anchor> crate::Append for CombinedChangeSet<K, A> {
fn append(&mut self, other: Self) {
crate::Append::append(&mut self.chain, other.chain);
crate::Append::append(&mut self.indexed_tx_graph, other.indexed_tx_graph);
impl<K: Ord, A: crate::Anchor> crate::Merge for CombinedChangeSet<K, A> {
fn merge(&mut self, other: Self) {
crate::Merge::merge(&mut self.chain, other.chain);
crate::Merge::merge(&mut self.indexed_tx_graph, other.indexed_tx_graph);
if other.network.is_some() {
debug_assert!(
self.network.is_none() || self.network == other.network,
Expand Down
36 changes: 18 additions & 18 deletions crates/chain/src/indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use bitcoin::{Block, OutPoint, Transaction, TxOut, Txid};

use crate::{
tx_graph::{self, TxGraph},
Anchor, AnchorFromBlockPosition, Append, BlockId, Indexer,
Anchor, AnchorFromBlockPosition, BlockId, Indexer, Merge,
};

/// The [`IndexedTxGraph`] combines a [`TxGraph`] and an [`Indexer`] implementation.
Expand Down Expand Up @@ -67,18 +67,18 @@ impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I> {

impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I>
where
I::ChangeSet: Default + Append,
I::ChangeSet: Default + Merge,
{
fn index_tx_graph_changeset(
&mut self,
tx_graph_changeset: &tx_graph::ChangeSet<A>,
) -> I::ChangeSet {
let mut changeset = I::ChangeSet::default();
for added_tx in &tx_graph_changeset.txs {
changeset.append(self.index.index_tx(added_tx));
changeset.merge(self.index.index_tx(added_tx));
}
for (&added_outpoint, added_txout) in &tx_graph_changeset.txouts {
changeset.append(self.index.index_txout(added_outpoint, added_txout));
changeset.merge(self.index.index_txout(added_outpoint, added_txout));
}
changeset
}
Expand Down Expand Up @@ -137,16 +137,16 @@ where

let mut indexer = I::ChangeSet::default();
for (tx, _) in &txs {
indexer.append(self.index.index_tx(tx));
indexer.merge(self.index.index_tx(tx));
}

let mut graph = tx_graph::ChangeSet::default();
for (tx, anchors) in txs {
if self.index.is_tx_relevant(tx) {
let txid = tx.compute_txid();
graph.append(self.graph.insert_tx(tx.clone()));
graph.merge(self.graph.insert_tx(tx.clone()));
for anchor in anchors {
graph.append(self.graph.insert_anchor(txid, anchor));
graph.merge(self.graph.insert_anchor(txid, anchor));
}
}
}
Expand Down Expand Up @@ -176,7 +176,7 @@ where

let mut indexer = I::ChangeSet::default();
for (tx, _) in &txs {
indexer.append(self.index.index_tx(tx));
indexer.merge(self.index.index_tx(tx));
}

let graph = self.graph.batch_insert_unconfirmed(
Expand Down Expand Up @@ -210,7 +210,7 @@ where
/// Methods are available if the anchor (`A`) implements [`AnchorFromBlockPosition`].
impl<A: Anchor, I: Indexer> IndexedTxGraph<A, I>
where
I::ChangeSet: Default + Append,
I::ChangeSet: Default + Merge,
A: AnchorFromBlockPosition,
{
/// Batch insert all transactions of the given `block` of `height`, filtering out those that are
Expand All @@ -232,14 +232,14 @@ where
};
let mut changeset = ChangeSet::<A, I::ChangeSet>::default();
for (tx_pos, tx) in block.txdata.iter().enumerate() {
changeset.indexer.append(self.index.index_tx(tx));
changeset.indexer.merge(self.index.index_tx(tx));
if self.index.is_tx_relevant(tx) {
let txid = tx.compute_txid();
let anchor = A::from_block_position(block, block_id, tx_pos);
changeset.graph.append(self.graph.insert_tx(tx.clone()));
changeset.graph.merge(self.graph.insert_tx(tx.clone()));
changeset
.graph
.append(self.graph.insert_anchor(txid, anchor));
.merge(self.graph.insert_anchor(txid, anchor));
}
}
changeset
Expand All @@ -261,8 +261,8 @@ where
let mut graph = tx_graph::ChangeSet::default();
for (tx_pos, tx) in block.txdata.iter().enumerate() {
let anchor = A::from_block_position(&block, block_id, tx_pos);
graph.append(self.graph.insert_anchor(tx.compute_txid(), anchor));
graph.append(self.graph.insert_tx(tx.clone()));
graph.merge(self.graph.insert_anchor(tx.compute_txid(), anchor));
graph.merge(self.graph.insert_tx(tx.clone()));
}
let indexer = self.index_tx_graph_changeset(&graph);
ChangeSet { graph, indexer }
Expand Down Expand Up @@ -299,10 +299,10 @@ impl<A, IA: Default> Default for ChangeSet<A, IA> {
}
}

impl<A: Anchor, IA: Append> Append for ChangeSet<A, IA> {
fn append(&mut self, other: Self) {
self.graph.append(other.graph);
self.indexer.append(other.indexer);
impl<A: Anchor, IA: Merge> Merge for ChangeSet<A, IA> {
fn merge(&mut self, other: Self) {
self.graph.merge(other.graph);
self.indexer.merge(other.indexer);
}

fn is_empty(&self) -> bool {
Expand Down
16 changes: 8 additions & 8 deletions crates/chain/src/indexer/keychain_txout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use core::{
ops::{Bound, RangeBounds},
};

use crate::Append;
use crate::Merge;

/// The default lookahead for a [`KeychainTxOutIndex`]
pub const DEFAULT_LOOKAHEAD: u32 = 25;
Expand Down Expand Up @@ -157,7 +157,7 @@ impl<K: Clone + Ord + Debug> Indexer for KeychainTxOutIndex<K> {
let mut changeset = ChangeSet::<K>::default();
let txid = tx.compute_txid();
for (op, txout) in tx.output.iter().enumerate() {
changeset.append(self.index_txout(OutPoint::new(txid, op as u32), txout));
changeset.merge(self.index_txout(OutPoint::new(txid, op as u32), txout));
}
changeset
}
Expand Down Expand Up @@ -632,7 +632,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {

for (keychain, &index) in keychains {
if let Some((_, new_changeset)) = self.reveal_to_target(keychain, index) {
changeset.append(new_changeset);
changeset.merge(new_changeset);
}
}

Expand Down Expand Up @@ -666,7 +666,7 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
match self.reveal_next_spk(keychain) {
Some(((i, spk), change)) => {
spks.push((i, spk));
changeset.append(change);
changeset.merge(change);
}
None => break,
}
Expand Down Expand Up @@ -856,12 +856,12 @@ impl<K: core::fmt::Debug> std::error::Error for InsertDescriptorError<K> {}
///
/// It can be applied to [`KeychainTxOutIndex`] with [`apply_changeset`].
///
/// The `last_revealed` field is monotone in that [`append`] will never decrease it.
/// The `last_revealed` field is monotone in that [`merge`] will never decrease it.
/// `keychains_added` is *not* monotone, once it is set any attempt to change it is subject to the
/// same *one-to-one* keychain <-> descriptor mapping invariant as [`KeychainTxOutIndex`] itself.
///
/// [`apply_changeset`]: KeychainTxOutIndex::apply_changeset
/// [`append`]: Self::append
/// [`Merge`]: Self::merge
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(
feature = "serde",
Expand All @@ -882,14 +882,14 @@ pub struct ChangeSet<K> {
pub last_revealed: BTreeMap<DescriptorId, u32>,
}

impl<K: Ord> Append for ChangeSet<K> {
impl<K: Ord> Merge for ChangeSet<K> {
/// Merge another [`ChangeSet<K>`] into self.
///
/// For the `keychains_added` field this method respects the invariants of
/// [`insert_descriptor`]. `last_revealed` always becomes the larger of the two.
///
/// [`insert_descriptor`]: KeychainTxOutIndex::insert_descriptor
fn append(&mut self, other: Self) {
fn merge(&mut self, other: Self) {
for (new_keychain, new_descriptor) in other.keychains_added {
// enforce 1-to-1 invariance
if !self.keychains_added.contains_key(&new_keychain)
Expand Down
54 changes: 27 additions & 27 deletions crates/chain/src/tx_data_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ pub trait AnchorFromBlockPosition: Anchor {
fn from_block_position(block: &bitcoin::Block, block_id: BlockId, tx_pos: usize) -> Self;
}

/// Trait that makes an object appendable.
pub trait Append: Default {
/// Append another object of the same type onto `self`.
fn append(&mut self, other: Self);
/// Trait that makes an object mergeable.
pub trait Merge: Default {
/// Merge another object of the same type onto `self`.
fn merge(&mut self, other: Self);

/// Returns whether the structure is considered empty.
fn is_empty(&self) -> bool;
Expand All @@ -131,8 +131,8 @@ pub trait Append: Default {
}
}

impl<K: Ord, V> Append for BTreeMap<K, V> {
fn append(&mut self, other: Self) {
impl<K: Ord, V> Merge for BTreeMap<K, V> {
fn merge(&mut self, other: Self) {
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
BTreeMap::extend(self, other)
Expand All @@ -143,8 +143,8 @@ impl<K: Ord, V> Append for BTreeMap<K, V> {
}
}

impl<T: Ord> Append for BTreeSet<T> {
fn append(&mut self, other: Self) {
impl<T: Ord> Merge for BTreeSet<T> {
fn merge(&mut self, other: Self) {
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
BTreeSet::extend(self, other)
Expand All @@ -155,8 +155,8 @@ impl<T: Ord> Append for BTreeSet<T> {
}
}

impl<T> Append for Vec<T> {
fn append(&mut self, mut other: Self) {
impl<T> Merge for Vec<T> {
fn merge(&mut self, mut other: Self) {
Vec::append(self, &mut other)
}

Expand All @@ -165,30 +165,30 @@ impl<T> Append for Vec<T> {
}
}

macro_rules! impl_append_for_tuple {
macro_rules! impl_merge_for_tuple {
($($a:ident $b:tt)*) => {
impl<$($a),*> Append for ($($a,)*) where $($a: Append),* {
impl<$($a),*> Merge for ($($a,)*) where $($a: Merge),* {

fn append(&mut self, _other: Self) {
$(Append::append(&mut self.$b, _other.$b) );*
fn merge(&mut self, _other: Self) {
$(Merge::merge(&mut self.$b, _other.$b) );*
}

fn is_empty(&self) -> bool {
$(Append::is_empty(&self.$b) && )* true
$(Merge::is_empty(&self.$b) && )* true
}
}
}
}

impl_append_for_tuple!();
impl_append_for_tuple!(T0 0);
impl_append_for_tuple!(T0 0 T1 1);
impl_append_for_tuple!(T0 0 T1 1 T2 2);
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3);
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4);
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5);
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6);
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7);
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8);
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9);
impl_append_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10);
impl_merge_for_tuple!();
impl_merge_for_tuple!(T0 0);
impl_merge_for_tuple!(T0 0 T1 1);
impl_merge_for_tuple!(T0 0 T1 1 T2 2);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9);
impl_merge_for_tuple!(T0 0 T1 1 T2 2 T3 3 T4 4 T5 5 T6 6 T7 7 T8 8 T9 9 T10 10);
14 changes: 7 additions & 7 deletions crates/chain/src/tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
//! A [`TxGraph`] can also be updated with another [`TxGraph`] which merges them together.
//!
//! ```
//! # use bdk_chain::{Append, BlockId};
//! # use bdk_chain::{Merge, BlockId};
//! # use bdk_chain::tx_graph::TxGraph;
//! # use bdk_chain::example_utils::*;
//! # use bitcoin::Transaction;
Expand All @@ -89,7 +89,7 @@
//! [`insert_txout`]: TxGraph::insert_txout
use crate::{
collections::*, Anchor, Append, Balance, BlockId, ChainOracle, ChainPosition, FullTxOut,
collections::*, Anchor, Balance, BlockId, ChainOracle, ChainPosition, FullTxOut, Merge,
};
use alloc::collections::vec_deque::VecDeque;
use alloc::sync::Arc;
Expand Down Expand Up @@ -547,8 +547,8 @@ impl<A: Clone + Ord> TxGraph<A> {
) -> ChangeSet<A> {
let mut changeset = ChangeSet::<A>::default();
for (tx, seen_at) in txs {
changeset.append(self.insert_seen_at(tx.compute_txid(), seen_at));
changeset.append(self.insert_tx(tx));
changeset.merge(self.insert_seen_at(tx.compute_txid(), seen_at));
changeset.merge(self.insert_tx(tx));
}
changeset
}
Expand Down Expand Up @@ -630,7 +630,7 @@ impl<A: Clone + Ord> TxGraph<A> {
.collect();

for txid in unanchored_txs {
changeset.append(self.insert_seen_at(txid, seen_at));
changeset.merge(self.insert_seen_at(txid, seen_at));
}
changeset
}
Expand Down Expand Up @@ -1293,8 +1293,8 @@ impl<A> ChangeSet<A> {
}
}

impl<A: Ord> Append for ChangeSet<A> {
fn append(&mut self, other: Self) {
impl<A: Ord> Merge for ChangeSet<A> {
fn merge(&mut self, other: Self) {
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
self.txs.extend(other.txs);
Expand Down
2 changes: 1 addition & 1 deletion crates/chain/tests/test_indexed_tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bdk_chain::{
indexed_tx_graph::{self, IndexedTxGraph},
indexer::keychain_txout::KeychainTxOutIndex,
local_chain::LocalChain,
tx_graph, Append, Balance, ChainPosition, ConfirmationHeightAnchor, DescriptorExt,
tx_graph, Balance, ChainPosition, ConfirmationHeightAnchor, DescriptorExt, Merge,
};
use bitcoin::{
secp256k1::Secp256k1, Amount, OutPoint, Script, ScriptBuf, Transaction, TxIn, TxOut,
Expand Down
Loading

0 comments on commit 43f093d

Please sign in to comment.