Skip to content

Commit

Permalink
[bdk_chain_redesign] Introduce TxAnchor trait
Browse files Browse the repository at this point in the history
  • Loading branch information
evanlinjin committed Mar 24, 2023
1 parent 82f9cad commit 7685ca2
Show file tree
Hide file tree
Showing 21 changed files with 433 additions and 257 deletions.
28 changes: 14 additions & 14 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@ const COINBASE_MATURITY: u32 = 100;
pub struct Wallet<D = ()> {
signers: Arc<SignersContainer>,
change_signers: Arc<SignersContainer>,
keychain_tracker: KeychainTracker<KeychainKind, ConfirmationTime>,
persist: persist::Persist<KeychainKind, ConfirmationTime, D>,
keychain_tracker: KeychainTracker<KeychainKind, BlockId, ConfirmationTime>,
persist: persist::Persist<KeychainKind, BlockId, ConfirmationTime, D>,
network: Network,
secp: SecpCtx,
}

/// The update to a [`Wallet`] used in [`Wallet::apply_update`]. This is usually returned from blockchain data sources.
/// The type parameter `T` indicates the kind of transaction contained in the update. It's usually a [`bitcoin::Transaction`].
pub type Update = KeychainScan<KeychainKind, ConfirmationTime>;
pub type Update = KeychainScan<KeychainKind, BlockId, ConfirmationTime>;
/// Error indicating that something was wrong with an [`Update<T>`].
pub type UpdateError = chain_graph::UpdateError<ConfirmationTime>;
/// The changeset produced internally by applying an update
pub(crate) type ChangeSet = KeychainChangeSet<KeychainKind, ConfirmationTime>;
pub(crate) type ChangeSet = KeychainChangeSet<KeychainKind, BlockId, ConfirmationTime>;

/// 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 @@ -195,7 +195,7 @@ impl<D> Wallet<D> {
network: Network,
) -> Result<Self, NewError<D::LoadError>>
where
D: persist::PersistBackend<KeychainKind, ConfirmationTime>,
D: persist::PersistBackend<KeychainKind, BlockId, ConfirmationTime>,
{
let secp = Secp256k1::new();

Expand Down Expand Up @@ -257,7 +257,7 @@ impl<D> Wallet<D> {
/// (i.e. does not end with /*) then the same address will always be returned for any [`AddressIndex`].
pub fn get_address(&mut self, address_index: AddressIndex) -> AddressInfo
where
D: persist::PersistBackend<KeychainKind, ConfirmationTime>,
D: persist::PersistBackend<KeychainKind, BlockId, ConfirmationTime>,
{
self._get_address(address_index, KeychainKind::External)
}
Expand All @@ -271,14 +271,14 @@ impl<D> Wallet<D> {
/// be returned for any [`AddressIndex`].
pub fn get_internal_address(&mut self, address_index: AddressIndex) -> AddressInfo
where
D: persist::PersistBackend<KeychainKind, ConfirmationTime>,
D: persist::PersistBackend<KeychainKind, BlockId, ConfirmationTime>,
{
self._get_address(address_index, KeychainKind::Internal)
}

fn _get_address(&mut self, address_index: AddressIndex, keychain: KeychainKind) -> AddressInfo
where
D: persist::PersistBackend<KeychainKind, ConfirmationTime>,
D: persist::PersistBackend<KeychainKind, BlockId, ConfirmationTime>,
{
let keychain = self.map_keychain(keychain);
let txout_index = &mut self.keychain_tracker.txout_index;
Expand Down Expand Up @@ -613,7 +613,7 @@ impl<D> Wallet<D> {
params: TxParams,
) -> Result<(psbt::PartiallySignedTransaction, TransactionDetails), Error>
where
D: persist::PersistBackend<KeychainKind, ConfirmationTime>,
D: persist::PersistBackend<KeychainKind, BlockId, ConfirmationTime>,
{
let external_descriptor = self
.keychain_tracker
Expand Down Expand Up @@ -1687,7 +1687,7 @@ impl<D> Wallet<D> {
/// [`commit`]: Self::commit
pub fn apply_update(&mut self, update: Update) -> Result<(), UpdateError>
where
D: persist::PersistBackend<KeychainKind, ConfirmationTime>,
D: persist::PersistBackend<KeychainKind, BlockId, ConfirmationTime>,
{
let changeset = self.keychain_tracker.apply_update(update)?;
self.persist.stage(changeset);
Expand All @@ -1699,7 +1699,7 @@ impl<D> Wallet<D> {
/// [`staged`]: Self::staged
pub fn commit(&mut self) -> Result<(), D::WriteError>
where
D: persist::PersistBackend<KeychainKind, ConfirmationTime>,
D: persist::PersistBackend<KeychainKind, BlockId, ConfirmationTime>,
{
self.persist.commit()
}
Expand All @@ -1717,7 +1717,7 @@ impl<D> Wallet<D> {
}

/// Get a reference to the inner [`ChainGraph`](bdk_chain::chain_graph::ChainGraph).
pub fn as_chain_graph(&self) -> &bdk_chain::chain_graph::ChainGraph<ConfirmationTime> {
pub fn as_chain_graph(&self) -> &bdk_chain::chain_graph::ChainGraph<BlockId, ConfirmationTime> {
self.keychain_tracker.chain_graph()
}
}
Expand All @@ -1728,8 +1728,8 @@ impl<D> AsRef<bdk_chain::tx_graph::TxGraph> for Wallet<D> {
}
}

impl<D> AsRef<bdk_chain::chain_graph::ChainGraph<ConfirmationTime>> for Wallet<D> {
fn as_ref(&self) -> &bdk_chain::chain_graph::ChainGraph<ConfirmationTime> {
impl<D> AsRef<bdk_chain::chain_graph::ChainGraph<BlockId, ConfirmationTime>> for Wallet<D> {
fn as_ref(&self) -> &bdk_chain::chain_graph::ChainGraph<BlockId, ConfirmationTime> {
self.keychain_tracker.chain_graph()
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/bdk/src/wallet/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use crate::collections::BTreeMap;
use crate::collections::HashSet;
use alloc::{boxed::Box, rc::Rc, string::String, vec::Vec};
use bdk_chain::BlockId;
use bdk_chain::ConfirmationTime;
use core::cell::RefCell;
use core::marker::PhantomData;
Expand Down Expand Up @@ -526,7 +527,7 @@ impl<'a, D, Cs: CoinSelectionAlgorithm, Ctx: TxBuilderContext> TxBuilder<'a, D,
/// [`BIP174`]: https://github.com/bitcoin/bips/blob/master/bip-0174.mediawiki
pub fn finish(self) -> Result<(Psbt, TransactionDetails), Error>
where
D: persist::PersistBackend<KeychainKind, ConfirmationTime>,
D: persist::PersistBackend<KeychainKind, BlockId, ConfirmationTime>,
{
self.wallet
.borrow_mut()
Expand Down
10 changes: 8 additions & 2 deletions crates/chain/src/chain_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use bitcoin::{hashes::Hash, BlockHash, OutPoint, TxOut, Txid};

use crate::{
sparse_chain::{self, ChainPosition},
COINBASE_MATURITY,
TxAnchor, COINBASE_MATURITY,
};

/// Represents the height at which a transaction is confirmed.
Expand Down Expand Up @@ -118,7 +118,7 @@ impl ConfirmationTime {
}

/// A reference to a block in the canonical chain.
#[derive(Debug, Clone, PartialEq, Eq, Copy, PartialOrd, Ord)]
#[derive(Debug, Clone, PartialEq, Eq, Copy, PartialOrd, Ord, core::hash::Hash)]
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
Expand All @@ -140,6 +140,12 @@ impl Default for BlockId {
}
}

impl TxAnchor for BlockId {
fn tx_anchor(&self) -> BlockId {
*self
}
}

impl From<(u32, BlockHash)> for BlockId {
fn from((height, hash): (u32, BlockHash)) -> Self {
Self { height, hash }
Expand Down
Loading

0 comments on commit 7685ca2

Please sign in to comment.