Skip to content

Commit

Permalink
Merge #1065: refactor!(chain): Unify ChangeSet nomenclature
Browse files Browse the repository at this point in the history
62f2531 ci: Update tokio version (Daniela Brozzoni)
80f5ecf feat(chain): Add `initial_changeset` to graphs (Daniela Brozzoni)
ea50b6a refactor!(chain): Unify ChangeSet nomenclature (Daniela Brozzoni)

Pull request description:

  Solves #1022

  ### Changelog notice

  Rename `indexed_tx_graph::IndexedAdditions` to `indexed_tx_graph::ChangeSet`
  Rename `indexed_tx_graph::IndexedAdditions::graph_additions` to `indexed_tx_graph::ChangeSet::graph`
  Rename `indexed_tx_graph::IndexedAdditions::index_additions` to `indexed_tx_graph::ChangeSet::indexer`
  Rename `tx_graph::Additions` to `tx_graph::ChangeSet`
  Rename `keychain::DerivationAdditions` to `keychain::ChangeSet`
  Rename `CanonicalTx::node` to `CanonicalTx::tx_node`
  Rename `CanonicalTx::observed_as` to `CanonicalTx::chain_position`
  Rename `LocalChangeSet` to `WalletChangeSet`
  Rename `LocalChangeSet::chain_changeset` to `WalletChangeSet::chain`
  Rename `LocalChangeSet::indexed_additions` to `WalletChangeSet::indexed_tx_graph`
  Rename `LocalUpdate` to `WalletUpdate`
  Make `TxGraph::determine_changeset` `pub(crate)`
  Add `TxGraph::initial_changeset`
  Add `IndexedTxGraph::initial_changeset`
  Remove `TxGraph::insert_txout_preview`
  Remove `TxGraph::insert_tx_preview`
  Remove `insert_anchor_preview`
  Remove `insert_seen_at_preview`

  ### Notes to reviewers

  I'm not sure whether we want to keep `TxGraph::determine_changeset` public or not

  ### 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:
  evanlinjin:
    ACK 62f2531

Tree-SHA512: 401bdf49c5f2082971cfcd54725ce98abd08263de7571205a324a2adbb5359be4f48454def415bedadde0a9c81db437b91c13cd6e892ba54b215059f75006dd5
  • Loading branch information
evanlinjin committed Aug 18, 2023
2 parents 0cd2348 + 62f2531 commit d6bcd9b
Show file tree
Hide file tree
Showing 17 changed files with 374 additions and 384 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cont_integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
cargo update -p log --precise "0.4.18"
cargo update -p tempfile --precise "3.6.0"
cargo update -p rustls:0.21.6 --precise "0.21.1"
cargo update -p tokio:1.31.0 --precise "1.29.1"
cargo update -p tokio:1.32.0 --precise "1.29.1"
cargo update -p flate2:1.0.27 --precise "1.0.26"
- name: Build
run: cargo build ${{ matrix.features }}
Expand Down
9 changes: 4 additions & 5 deletions crates/bdk/src/wallet/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,12 @@ impl FullyNodedExport {
Self::is_compatible_with_core(&descriptor)?;

let blockheight = if include_blockheight {
wallet
.transactions()
.next()
.map_or(0, |canonical_tx| match canonical_tx.observed_as {
wallet.transactions().next().map_or(0, |canonical_tx| {
match canonical_tx.chain_position {
bdk_chain::ChainPosition::Confirmed(a) => a.confirmation_height,
bdk_chain::ChainPosition::Unconfirmed(_) => 0,
})
}
})
} else {
0
};
Expand Down
54 changes: 30 additions & 24 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use alloc::{
};
pub use bdk_chain::keychain::Balance;
use bdk_chain::{
indexed_tx_graph::IndexedAdditions,
keychain::{KeychainTxOutIndex, LocalChangeSet, LocalUpdate},
indexed_tx_graph,
keychain::{KeychainTxOutIndex, WalletChangeSet, WalletUpdate},
local_chain::{self, CannotConnectError, CheckPoint, CheckPointIter, LocalChain},
tx_graph::{CanonicalTx, TxGraph},
Append, BlockId, ChainPosition, ConfirmationTime, ConfirmationTimeAnchor, FullTxOut,
Expand Down Expand Up @@ -95,10 +95,10 @@ pub struct Wallet<D = ()> {
}

/// The update to a [`Wallet`] used in [`Wallet::apply_update`]. This is usually returned from blockchain data sources.
pub type Update = LocalUpdate<KeychainKind, ConfirmationTimeAnchor>;
pub type Update = WalletUpdate<KeychainKind, ConfirmationTimeAnchor>;

/// The changeset produced internally by [`Wallet`] when mutated.
pub type ChangeSet = LocalChangeSet<KeychainKind, ConfirmationTimeAnchor>;
pub type ChangeSet = WalletChangeSet<KeychainKind, ConfirmationTimeAnchor>;

/// The address index selection strategy to use to derived an address from the wallet's external
/// descriptor. See [`Wallet::get_address`]. If you're unsure which one to use use `WalletIndex::New`.
Expand Down Expand Up @@ -246,8 +246,8 @@ impl<D> Wallet<D> {
};

let changeset = db.load_from_persistence().map_err(NewError::Persist)?;
chain.apply_changeset(&changeset.chain_changeset);
indexed_graph.apply_additions(changeset.indexed_additions);
chain.apply_changeset(&changeset.chain);
indexed_graph.apply_changeset(changeset.index_tx_graph);

let persist = Persist::new(db);

Expand Down Expand Up @@ -320,14 +320,14 @@ impl<D> Wallet<D> {
{
let keychain = self.map_keychain(keychain);
let txout_index = &mut self.indexed_graph.index;
let (index, spk, additions) = match address_index {
let (index, spk, changeset) = match address_index {
AddressIndex::New => {
let ((index, spk), index_additions) = txout_index.reveal_next_spk(&keychain);
(index, spk.into(), Some(index_additions))
let ((index, spk), index_changeset) = txout_index.reveal_next_spk(&keychain);
(index, spk.into(), Some(index_changeset))
}
AddressIndex::LastUnused => {
let ((index, spk), index_additions) = txout_index.next_unused_spk(&keychain);
(index, spk.into(), Some(index_additions))
let ((index, spk), index_changeset) = txout_index.next_unused_spk(&keychain);
(index, spk.into(), Some(index_changeset))
}
AddressIndex::Peek(index) => {
let (index, spk) = txout_index
Expand All @@ -339,9 +339,11 @@ impl<D> Wallet<D> {
}
};

if let Some(additions) = additions {
if let Some(changeset) = changeset {
self.persist
.stage(ChangeSet::from(IndexedAdditions::from(additions)));
.stage(ChangeSet::from(indexed_tx_graph::ChangeSet::from(
changeset,
)));
self.persist.commit()?;
}

Expand Down Expand Up @@ -436,12 +438,12 @@ impl<D> Wallet<D> {
let graph = self.indexed_graph.graph();

let canonical_tx = CanonicalTx {
observed_as: graph.get_chain_position(
chain_position: graph.get_chain_position(
&self.chain,
self.chain.tip().map(|cp| cp.block_id()).unwrap_or_default(),
txid,
)?,
node: graph.get_tx_node(txid)?,
tx_node: graph.get_tx_node(txid)?,
};

Some(new_tx_details(
Expand Down Expand Up @@ -889,12 +891,14 @@ impl<D> Wallet<D> {
Some(ref drain_recipient) => drain_recipient.clone(),
None => {
let change_keychain = self.map_keychain(KeychainKind::Internal);
let ((index, spk), index_additions) =
let ((index, spk), index_changeset) =
self.indexed_graph.index.next_unused_spk(&change_keychain);
let spk = spk.into();
self.indexed_graph.index.mark_used(&change_keychain, index);
self.persist
.stage(ChangeSet::from(IndexedAdditions::from(index_additions)));
.stage(ChangeSet::from(indexed_tx_graph::ChangeSet::from(
index_changeset,
)));
self.persist.commit().expect("TODO");
spk
}
Expand Down Expand Up @@ -1286,7 +1290,7 @@ impl<D> Wallet<D> {
.indexed_graph
.graph()
.get_chain_position(&self.chain, chain_tip, input.previous_output.txid)
.map(|observed_as| match observed_as {
.map(|chain_position| match chain_position {
ChainPosition::Confirmed(a) => a.confirmation_height,
ChainPosition::Unconfirmed(_) => u32::MAX,
});
Expand Down Expand Up @@ -1469,7 +1473,7 @@ impl<D> Wallet<D> {
.graph()
.get_chain_position(&self.chain, chain_tip, txid)
{
Some(observed_as) => observed_as.cloned().into(),
Some(chain_position) => chain_position.cloned().into(),
None => return false,
};

Expand Down Expand Up @@ -1716,11 +1720,13 @@ impl<D> Wallet<D> {
D: PersistBackend<ChangeSet>,
{
let mut changeset = ChangeSet::from(self.chain.apply_update(update.chain)?);
let (_, index_additions) = self
let (_, index_changeset) = self
.indexed_graph
.index
.reveal_to_target_multi(&update.last_active_indices);
changeset.append(ChangeSet::from(IndexedAdditions::from(index_additions)));
changeset.append(ChangeSet::from(indexed_tx_graph::ChangeSet::from(
index_changeset,
)));
changeset.append(ChangeSet::from(
self.indexed_graph.apply_update(update.graph),
));
Expand Down Expand Up @@ -1827,7 +1833,7 @@ fn new_tx_details(
) -> TransactionDetails {
let graph = indexed_graph.graph();
let index = &indexed_graph.index;
let tx = canonical_tx.node.tx;
let tx = canonical_tx.tx_node.tx;

let received = tx
.output
Expand Down Expand Up @@ -1867,11 +1873,11 @@ fn new_tx_details(

TransactionDetails {
transaction: if include_raw { Some(tx.clone()) } else { None },
txid: canonical_tx.node.txid,
txid: canonical_tx.tx_node.txid,
received,
sent,
fee,
confirmation_time: canonical_tx.observed_as.cloned().into(),
confirmation_time: canonical_tx.chain_position.cloned().into(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/bdk/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1571,7 +1571,7 @@ fn test_bump_fee_remove_output_manually_selected_only() {
.transactions()
.last()
.unwrap()
.observed_as
.chain_position
.cloned()
.into(),
)
Expand Down Expand Up @@ -1621,7 +1621,7 @@ fn test_bump_fee_add_input() {
.transactions()
.last()
.unwrap()
.observed_as
.chain_position
.cloned()
.into();
wallet.insert_tx(init_tx, pos).unwrap();
Expand Down
Loading

0 comments on commit d6bcd9b

Please sign in to comment.