Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!(chain): Unify ChangeSet nomenclature #1065

Merged
merged 3 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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