From 34d0277e44fd054c8d463dfa756d8531ccda3ca9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=97=E5=AE=87?= Date: Tue, 28 Mar 2023 14:58:59 +0800 Subject: [PATCH] [bdk_chain_redesign] Rm anchor type param for structs that don't use it --- crates/bdk/src/wallet/mod.rs | 28 +++--- crates/bdk/src/wallet/tx_builder.rs | 3 +- crates/chain/src/chain_graph.rs | 87 ++++++++----------- crates/chain/src/keychain.rs | 40 ++++----- crates/chain/src/keychain/persist.rs | 24 ++--- crates/chain/src/keychain/tracker.rs | 41 +++++---- crates/chain/tests/test_chain_graph.rs | 32 ++++--- crates/chain/tests/test_keychain_tracker.rs | 6 +- crates/electrum/src/lib.rs | 9 +- crates/esplora/src/async_ext.rs | 6 +- crates/esplora/src/blocking_ext.rs | 6 +- crates/file_store/src/file_store.rs | 32 +++---- crates/file_store/src/lib.rs | 10 +-- .../keychain_tracker_electrum/src/main.rs | 2 +- .../keychain_tracker_esplora/src/main.rs | 2 +- .../keychain_tracker_example_cli/src/lib.rs | 47 +++++----- 16 files changed, 174 insertions(+), 201 deletions(-) diff --git a/crates/bdk/src/wallet/mod.rs b/crates/bdk/src/wallet/mod.rs index 194c6c901..6e4adf74b 100644 --- a/crates/bdk/src/wallet/mod.rs +++ b/crates/bdk/src/wallet/mod.rs @@ -85,19 +85,19 @@ const COINBASE_MATURITY: u32 = 100; pub struct Wallet { signers: Arc, change_signers: Arc, - keychain_tracker: KeychainTracker, - persist: persist::Persist, + keychain_tracker: KeychainTracker, + persist: persist::Persist, 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; +pub type Update = KeychainScan; /// Error indicating that something was wrong with an [`Update`]. pub type UpdateError = chain_graph::UpdateError; /// The changeset produced internally by applying an update -pub(crate) type ChangeSet = KeychainChangeSet; +pub(crate) type ChangeSet = KeychainChangeSet; /// 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`. @@ -197,7 +197,7 @@ impl Wallet { network: Network, ) -> Result> where - D: persist::PersistBackend, + D: persist::PersistBackend, { let secp = Secp256k1::new(); @@ -259,7 +259,7 @@ impl Wallet { /// (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, + D: persist::PersistBackend, { self._get_address(address_index, KeychainKind::External) } @@ -273,14 +273,14 @@ impl Wallet { /// be returned for any [`AddressIndex`]. pub fn get_internal_address(&mut self, address_index: AddressIndex) -> AddressInfo where - D: persist::PersistBackend, + D: persist::PersistBackend, { self._get_address(address_index, KeychainKind::Internal) } fn _get_address(&mut self, address_index: AddressIndex, keychain: KeychainKind) -> AddressInfo where - D: persist::PersistBackend, + D: persist::PersistBackend, { let keychain = self.map_keychain(keychain); let txout_index = &mut self.keychain_tracker.txout_index; @@ -620,7 +620,7 @@ impl Wallet { params: TxParams, ) -> Result<(psbt::PartiallySignedTransaction, TransactionDetails), Error> where - D: persist::PersistBackend, + D: persist::PersistBackend, { let external_descriptor = self .keychain_tracker @@ -1694,7 +1694,7 @@ impl Wallet { /// [`commit`]: Self::commit pub fn apply_update(&mut self, update: Update) -> Result<(), UpdateError> where - D: persist::PersistBackend, + D: persist::PersistBackend, { let changeset = self.keychain_tracker.apply_update(update)?; self.persist.stage(changeset); @@ -1706,7 +1706,7 @@ impl Wallet { /// [`staged`]: Self::staged pub fn commit(&mut self) -> Result<(), D::WriteError> where - D: persist::PersistBackend, + D: persist::PersistBackend, { self.persist.commit() } @@ -1724,7 +1724,7 @@ impl Wallet { } /// Get a reference to the inner [`ChainGraph`](bdk_chain::chain_graph::ChainGraph). - pub fn as_chain_graph(&self) -> &bdk_chain::chain_graph::ChainGraph { + pub fn as_chain_graph(&self) -> &bdk_chain::chain_graph::ChainGraph { self.keychain_tracker.chain_graph() } } @@ -1735,8 +1735,8 @@ impl AsRef for Wallet { } } -impl AsRef> for Wallet { - fn as_ref(&self) -> &bdk_chain::chain_graph::ChainGraph { +impl AsRef> for Wallet { + fn as_ref(&self) -> &bdk_chain::chain_graph::ChainGraph { self.keychain_tracker.chain_graph() } } diff --git a/crates/bdk/src/wallet/tx_builder.rs b/crates/bdk/src/wallet/tx_builder.rs index 150d33aa0..dbd4811c1 100644 --- a/crates/bdk/src/wallet/tx_builder.rs +++ b/crates/bdk/src/wallet/tx_builder.rs @@ -39,7 +39,6 @@ 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; @@ -527,7 +526,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, + D: persist::PersistBackend, { self.wallet .borrow_mut() diff --git a/crates/chain/src/chain_graph.rs b/crates/chain/src/chain_graph.rs index fcb980433..3c841965a 100644 --- a/crates/chain/src/chain_graph.rs +++ b/crates/chain/src/chain_graph.rs @@ -3,7 +3,7 @@ use crate::{ collections::HashSet, sparse_chain::{self, ChainPosition, SparseChain}, tx_graph::{self, TxGraph, TxInGraph}, - BlockAnchor, BlockId, ForEachTxOut, FullTxOut, TxHeight, + BlockId, ForEachTxOut, FullTxOut, TxHeight, }; use alloc::{string::ToString, vec::Vec}; use bitcoin::{OutPoint, Transaction, TxOut, Txid}; @@ -25,12 +25,12 @@ use core::fmt::Debug; /// `graph` but not the other way around. Transactions may fall out of the *chain* (via re-org or /// mempool eviction) but will remain in the *graph*. #[derive(Clone, Debug, PartialEq)] -pub struct ChainGraph { +pub struct ChainGraph

{ chain: SparseChain

, - graph: TxGraph, + graph: TxGraph, } -impl Default for ChainGraph { +impl

Default for ChainGraph

{ fn default() -> Self { Self { chain: Default::default(), @@ -39,39 +39,38 @@ impl Default for ChainGraph { } } -impl AsRef> for ChainGraph { +impl

AsRef> for ChainGraph

{ fn as_ref(&self) -> &SparseChain

{ &self.chain } } -impl AsRef> for ChainGraph { - fn as_ref(&self) -> &TxGraph { +impl

AsRef> for ChainGraph

{ + fn as_ref(&self) -> &TxGraph { &self.graph } } -impl AsRef> for ChainGraph { - fn as_ref(&self) -> &ChainGraph { +impl

AsRef> for ChainGraph

{ + fn as_ref(&self) -> &ChainGraph

{ self } } -impl ChainGraph { +impl

ChainGraph

{ /// Returns a reference to the internal [`SparseChain`]. pub fn chain(&self) -> &SparseChain

{ &self.chain } /// Returns a reference to the internal [`TxGraph`]. - pub fn graph(&self) -> &TxGraph { + pub fn graph(&self) -> &TxGraph { &self.graph } } -impl ChainGraph +impl

ChainGraph

where - A: BlockAnchor, P: ChainPosition, { /// Create a new chain graph from a `chain` and a `graph`. @@ -82,7 +81,7 @@ where /// transaction in `graph`. /// 2. The `chain` has two transactions that are allegedly in it, but they conflict in the `graph` /// (so could not possibly be in the same chain). - pub fn new(chain: SparseChain

, graph: TxGraph) -> Result> { + pub fn new(chain: SparseChain

, graph: TxGraph) -> Result> { let mut missing = HashSet::default(); for (pos, txid) in chain.txids() { if let Some(graphed_tx) = graph.get_tx(*txid) { @@ -129,7 +128,7 @@ where &self, update: SparseChain

, new_txs: impl IntoIterator, - ) -> Result, NewError

> { + ) -> Result, NewError

> { let mut inflated_chain = SparseChain::default(); let mut inflated_graph = TxGraph::default(); @@ -188,7 +187,7 @@ where /// Determines the changes required to invalidate checkpoints `from_height` (inclusive) and /// above. Displaced transactions will have their positions moved to [`TxHeight::Unconfirmed`]. - pub fn invalidate_checkpoints_preview(&self, from_height: u32) -> ChangeSet { + pub fn invalidate_checkpoints_preview(&self, from_height: u32) -> ChangeSet

{ ChangeSet { chain: self.chain.invalidate_checkpoints_preview(from_height), ..Default::default() @@ -200,9 +199,9 @@ where /// /// This is equivalent to calling [`Self::invalidate_checkpoints_preview`] and /// [`Self::apply_changeset`] in sequence. - pub fn invalidate_checkpoints(&mut self, from_height: u32) -> ChangeSet + pub fn invalidate_checkpoints(&mut self, from_height: u32) -> ChangeSet

where - ChangeSet: Clone, + ChangeSet

: Clone, { let changeset = self.invalidate_checkpoints_preview(from_height); self.apply_changeset(changeset.clone()); @@ -213,7 +212,7 @@ where /// /// This does not necessarily mean that it is *confirmed* in the blockchain; it might just be in /// the unconfirmed transaction list within the [`SparseChain`]. - pub fn get_tx_in_chain(&self, txid: Txid) -> Option<(&P, TxInGraph<'_, Transaction, A>)> { + pub fn get_tx_in_chain(&self, txid: Txid) -> Option<(&P, TxInGraph<'_, Transaction, BlockId>)> { let position = self.chain.tx_position(txid)?; let graphed_tx = self.graph.get_tx(txid).expect("must exist"); Some((position, graphed_tx)) @@ -228,7 +227,7 @@ where &self, tx: Transaction, pos: P, - ) -> Result, InsertTxError

> { + ) -> Result, InsertTxError

> { let mut changeset = ChangeSet { chain: self.chain.insert_tx_preview(tx.txid(), pos)?, graph: self.graph.insert_tx_preview(tx), @@ -241,18 +240,14 @@ where /// /// This is equivalent to calling [`Self::insert_tx_preview`] and [`Self::apply_changeset`] in /// sequence. - pub fn insert_tx( - &mut self, - tx: Transaction, - pos: P, - ) -> Result, InsertTxError

> { + pub fn insert_tx(&mut self, tx: Transaction, pos: P) -> Result, InsertTxError

> { let changeset = self.insert_tx_preview(tx, pos)?; self.apply_changeset(changeset.clone()); Ok(changeset) } /// Determines the changes required to insert a [`TxOut`] into the internal [`TxGraph`]. - pub fn insert_txout_preview(&self, outpoint: OutPoint, txout: TxOut) -> ChangeSet { + pub fn insert_txout_preview(&self, outpoint: OutPoint, txout: TxOut) -> ChangeSet

{ ChangeSet { chain: Default::default(), graph: self.graph.insert_txout_preview(outpoint, txout), @@ -263,7 +258,7 @@ where /// /// This is equivalent to calling [`Self::insert_txout_preview`] and [`Self::apply_changeset`] /// in sequence. - pub fn insert_txout(&mut self, outpoint: OutPoint, txout: TxOut) -> ChangeSet { + pub fn insert_txout(&mut self, outpoint: OutPoint, txout: TxOut) -> ChangeSet

{ let changeset = self.insert_txout_preview(outpoint, txout); self.apply_changeset(changeset.clone()); changeset @@ -276,7 +271,7 @@ where pub fn insert_checkpoint_preview( &self, block_id: BlockId, - ) -> Result, InsertCheckpointError> { + ) -> Result, InsertCheckpointError> { self.chain .insert_checkpoint_preview(block_id) .map(|chain_changeset| ChangeSet { @@ -292,7 +287,7 @@ where pub fn insert_checkpoint( &mut self, block_id: BlockId, - ) -> Result, InsertCheckpointError> { + ) -> Result, InsertCheckpointError> { let changeset = self.insert_checkpoint_preview(block_id)?; self.apply_changeset(changeset.clone()); Ok(changeset) @@ -301,8 +296,8 @@ where /// Calculates the difference between self and `update` in the form of a [`ChangeSet`]. pub fn determine_changeset( &self, - update: &ChainGraph, - ) -> Result, UpdateError

> { + update: &ChainGraph

, + ) -> Result, UpdateError

> { let chain_changeset = self .chain .determine_changeset(&update.chain) @@ -337,10 +332,7 @@ where /// /// **WARNING:** If there are any missing full txs, conflict resolution will not be complete. In /// debug mode, this will result in panic. - fn fix_conflicts( - &self, - changeset: &mut ChangeSet, - ) -> Result<(), UnresolvableConflict

> { + fn fix_conflicts(&self, changeset: &mut ChangeSet

) -> Result<(), UnresolvableConflict

> { let mut chain_conflicts = vec![]; for (&txid, pos_change) in &changeset.chain.txids { @@ -416,17 +408,14 @@ where /// /// **Warning** this method assumes that the changeset is correctly formed. If it is not, the /// chain graph may behave incorrectly in the future and panic unexpectedly. - pub fn apply_changeset(&mut self, changeset: ChangeSet) { + pub fn apply_changeset(&mut self, changeset: ChangeSet

) { self.chain.apply_changeset(changeset.chain); self.graph.apply_additions(changeset.graph); } /// Applies the `update` chain graph. Note this is shorthand for calling /// [`Self::determine_changeset()`] and [`Self::apply_changeset()`] in sequence. - pub fn apply_update( - &mut self, - update: ChainGraph, - ) -> Result, UpdateError

> { + pub fn apply_update(&mut self, update: ChainGraph

) -> Result, UpdateError

> { let changeset = self.determine_changeset(&update)?; self.apply_changeset(changeset.clone()); Ok(changeset) @@ -441,7 +430,7 @@ where /// in ascending order. pub fn transactions_in_chain( &self, - ) -> impl DoubleEndedIterator)> { + ) -> impl DoubleEndedIterator)> { self.chain .txids() .map(move |(pos, txid)| (pos, self.graph.get_tx(*txid).expect("must exist"))) @@ -472,18 +461,18 @@ where serde( crate = "serde_crate", bound( - deserialize = "A: Ord + serde::Deserialize<'de>, P: serde::Deserialize<'de>", - serialize = "A: Ord + serde::Serialize, P: serde::Serialize" + deserialize = "P: serde::Deserialize<'de>", + serialize = "P: serde::Serialize" ) ) )] #[must_use] -pub struct ChangeSet { +pub struct ChangeSet

{ pub chain: sparse_chain::ChangeSet

, - pub graph: tx_graph::Additions, + pub graph: tx_graph::Additions, } -impl ChangeSet { +impl

ChangeSet

{ /// Returns `true` if this [`ChangeSet`] records no changes. pub fn is_empty(&self) -> bool { self.chain.is_empty() && self.graph.is_empty() @@ -499,7 +488,7 @@ impl ChangeSet { /// Appends the changes in `other` into self such that applying `self` afterward has the same /// effect as sequentially applying the original `self` and `other`. - pub fn append(&mut self, other: ChangeSet) + pub fn append(&mut self, other: ChangeSet

) where P: ChainPosition, { @@ -508,7 +497,7 @@ impl ChangeSet { } } -impl Default for ChangeSet { +impl

Default for ChangeSet

{ fn default() -> Self { Self { chain: Default::default(), @@ -523,7 +512,7 @@ impl

ForEachTxOut for ChainGraph

{ } } -impl ForEachTxOut for ChangeSet { +impl

ForEachTxOut for ChangeSet

{ fn for_each_txout(&self, f: impl FnMut((OutPoint, &TxOut))) { self.graph.for_each_txout(f) } diff --git a/crates/chain/src/keychain.rs b/crates/chain/src/keychain.rs index dd419db56..da2af6f25 100644 --- a/crates/chain/src/keychain.rs +++ b/crates/chain/src/keychain.rs @@ -105,14 +105,14 @@ impl AsRef> for DerivationAdditions { #[derive(Clone, Debug, PartialEq)] /// An update that includes the last active indexes of each keychain. -pub struct KeychainScan { +pub struct KeychainScan { /// The update data in the form of a chain that could be applied - pub update: ChainGraph, + pub update: ChainGraph

, /// The last active indexes of each keychain pub last_active_indices: BTreeMap, } -impl Default for KeychainScan { +impl Default for KeychainScan { fn default() -> Self { Self { update: Default::default(), @@ -121,8 +121,8 @@ impl Default for KeychainScan { } } -impl From> for KeychainScan { - fn from(update: ChainGraph) -> Self { +impl From> for KeychainScan { + fn from(update: ChainGraph

) -> Self { KeychainScan { update, last_active_indices: Default::default(), @@ -140,20 +140,20 @@ impl From> for KeychainScan { serde( crate = "serde_crate", bound( - deserialize = "K: Ord + serde::Deserialize<'de>, A: Ord + serde::Deserialize<'de>, P: serde::Deserialize<'de>", - serialize = "K: Ord + serde::Serialize, A: Ord + serde::Serialize, P: serde::Serialize" + deserialize = "K: Ord + serde::Deserialize<'de>, P: serde::Deserialize<'de>", + serialize = "K: Ord + serde::Serialize, P: serde::Serialize" ) ) )] #[must_use] -pub struct KeychainChangeSet { +pub struct KeychainChangeSet { /// The changes in local keychain derivation indices pub derivation_indices: DerivationAdditions, /// The changes that have occurred in the blockchain - pub chain_graph: chain_graph::ChangeSet, + pub chain_graph: chain_graph::ChangeSet

, } -impl Default for KeychainChangeSet { +impl Default for KeychainChangeSet { fn default() -> Self { Self { chain_graph: Default::default(), @@ -162,7 +162,7 @@ impl Default for KeychainChangeSet { } } -impl KeychainChangeSet { +impl KeychainChangeSet { /// Returns whether the [`KeychainChangeSet`] is empty (no changes recorded). pub fn is_empty(&self) -> bool { self.chain_graph.is_empty() && self.derivation_indices.is_empty() @@ -173,7 +173,7 @@ impl KeychainChangeSet { /// /// Note the derivation indices cannot be decreased, so `other` will only change the derivation /// index for a keychain, if it's value is higher than the one in `self`. - pub fn append(&mut self, other: KeychainChangeSet) + pub fn append(&mut self, other: KeychainChangeSet) where K: Ord, P: ChainPosition, @@ -183,8 +183,8 @@ impl KeychainChangeSet { } } -impl From> for KeychainChangeSet { - fn from(changeset: chain_graph::ChangeSet) -> Self { +impl From> for KeychainChangeSet { + fn from(changeset: chain_graph::ChangeSet

) -> Self { Self { chain_graph: changeset, ..Default::default() @@ -192,7 +192,7 @@ impl From> for KeychainChangeSet } } -impl From> for KeychainChangeSet { +impl From> for KeychainChangeSet { fn from(additions: DerivationAdditions) -> Self { Self { derivation_indices: additions, @@ -201,13 +201,13 @@ impl From> for KeychainChangeSet { } } -impl AsRef> for KeychainScan { - fn as_ref(&self) -> &TxGraph { +impl AsRef for KeychainScan { + fn as_ref(&self) -> &TxGraph { self.update.graph() } } -impl ForEachTxOut for KeychainChangeSet { +impl ForEachTxOut for KeychainChangeSet { fn for_each_txout(&self, f: impl FnMut((bitcoin::OutPoint, &bitcoin::TxOut))) { self.chain_graph.for_each_txout(f) } @@ -293,12 +293,12 @@ mod test { rhs_di.insert(Keychain::Four, 4); let mut lhs = KeychainChangeSet { derivation_indices: DerivationAdditions(lhs_di), - chain_graph: chain_graph::ChangeSet::<(), TxHeight>::default(), + chain_graph: chain_graph::ChangeSet::::default(), }; let rhs = KeychainChangeSet { derivation_indices: DerivationAdditions(rhs_di), - chain_graph: chain_graph::ChangeSet::<(), TxHeight>::default(), + chain_graph: chain_graph::ChangeSet::::default(), }; lhs.append(rhs); diff --git a/crates/chain/src/keychain/persist.rs b/crates/chain/src/keychain/persist.rs index f0bc8d116..1a3ffab02 100644 --- a/crates/chain/src/keychain/persist.rs +++ b/crates/chain/src/keychain/persist.rs @@ -18,12 +18,12 @@ use crate::{keychain, sparse_chain::ChainPosition}; /// /// [`KeychainTracker`]: keychain::KeychainTracker #[derive(Debug)] -pub struct Persist { +pub struct Persist { backend: B, - stage: keychain::KeychainChangeSet, + stage: keychain::KeychainChangeSet, } -impl Persist { +impl Persist { /// Create a new `Persist` from a [`PersistBackend`]. pub fn new(backend: B) -> Self { Self { @@ -35,7 +35,7 @@ impl Persist { /// Stage a `changeset` to later persistence with [`commit`]. /// /// [`commit`]: Self::commit - pub fn stage(&mut self, changeset: keychain::KeychainChangeSet) + pub fn stage(&mut self, changeset: keychain::KeychainChangeSet) where K: Ord, P: ChainPosition, @@ -44,7 +44,7 @@ impl Persist { } /// Get the changes that haven't been committed yet - pub fn staged(&self) -> &keychain::KeychainChangeSet { + pub fn staged(&self) -> &keychain::KeychainChangeSet { &self.stage } @@ -53,7 +53,7 @@ impl Persist { /// Returns a backend-defined error if this fails. pub fn commit(&mut self) -> Result<(), B::WriteError> where - B: PersistBackend, + B: PersistBackend, { self.backend.append_changeset(&self.stage)?; self.stage = Default::default(); @@ -62,7 +62,7 @@ impl Persist { } /// A persistence backend for [`Persist`]. -pub trait PersistBackend { +pub trait PersistBackend { /// The error the backend returns when it fails to write. type WriteError: core::fmt::Debug; @@ -79,29 +79,29 @@ pub trait PersistBackend { /// [`load_into_keychain_tracker`]: Self::load_into_keychain_tracker fn append_changeset( &mut self, - changeset: &keychain::KeychainChangeSet, + changeset: &keychain::KeychainChangeSet, ) -> Result<(), Self::WriteError>; /// Applies all the changesets the backend has received to `tracker`. fn load_into_keychain_tracker( &mut self, - tracker: &mut keychain::KeychainTracker, + tracker: &mut keychain::KeychainTracker, ) -> Result<(), Self::LoadError>; } -impl PersistBackend for () { +impl PersistBackend for () { type WriteError = (); type LoadError = (); fn append_changeset( &mut self, - _changeset: &keychain::KeychainChangeSet, + _changeset: &keychain::KeychainChangeSet, ) -> Result<(), Self::WriteError> { Ok(()) } fn load_into_keychain_tracker( &mut self, - _tracker: &mut keychain::KeychainTracker, + _tracker: &mut keychain::KeychainTracker, ) -> Result<(), Self::LoadError> { Ok(()) } diff --git a/crates/chain/src/keychain/tracker.rs b/crates/chain/src/keychain/tracker.rs index db4e8d893..fff5ee2b4 100644 --- a/crates/chain/src/keychain/tracker.rs +++ b/crates/chain/src/keychain/tracker.rs @@ -17,16 +17,15 @@ use super::{Balance, DerivationAdditions}; /// The [`KeychainTracker`] atomically updates its [`KeychainTxOutIndex`] whenever new chain data is /// incorporated into its internal [`ChainGraph`]. #[derive(Clone, Debug)] -pub struct KeychainTracker { +pub struct KeychainTracker { /// Index between script pubkeys to transaction outputs pub txout_index: KeychainTxOutIndex, - chain_graph: ChainGraph, + chain_graph: ChainGraph

, } -impl KeychainTracker +impl KeychainTracker where P: sparse_chain::ChainPosition, - A: crate::BlockAnchor, K: Ord + Clone + core::fmt::Debug, { /// Add a keychain to the tracker's `txout_index` with a descriptor to derive addresses. @@ -65,8 +64,8 @@ where /// [`KeychainTxOutIndex`]. pub fn determine_changeset( &self, - scan: &KeychainScan, - ) -> Result, chain_graph::UpdateError

> { + scan: &KeychainScan, + ) -> Result, chain_graph::UpdateError

> { // TODO: `KeychainTxOutIndex::determine_additions` let mut derivation_indices = scan.last_active_indices.clone(); derivation_indices.retain(|keychain, index| { @@ -90,8 +89,8 @@ where /// [`apply_changeset`]: Self::apply_changeset pub fn apply_update( &mut self, - scan: KeychainScan, - ) -> Result, chain_graph::UpdateError

> { + scan: KeychainScan, + ) -> Result, chain_graph::UpdateError

> { let changeset = self.determine_changeset(&scan)?; self.apply_changeset(changeset.clone()); Ok(changeset) @@ -101,7 +100,7 @@ where /// /// Internally, this calls [`KeychainTxOutIndex::apply_additions`] and /// [`ChainGraph::apply_changeset`] in sequence. - pub fn apply_changeset(&mut self, changeset: KeychainChangeSet) { + pub fn apply_changeset(&mut self, changeset: KeychainChangeSet) { let KeychainChangeSet { derivation_indices, chain_graph, @@ -133,12 +132,12 @@ where } /// Returns a reference to the internal [`ChainGraph`]. - pub fn chain_graph(&self) -> &ChainGraph { + pub fn chain_graph(&self) -> &ChainGraph

{ &self.chain_graph } /// Returns a reference to the internal [`TxGraph`] (which is part of the [`ChainGraph`]). - pub fn graph(&self) -> &TxGraph { + pub fn graph(&self) -> &TxGraph { self.chain_graph().graph() } @@ -160,7 +159,7 @@ where pub fn insert_checkpoint_preview( &self, block_id: BlockId, - ) -> Result, chain_graph::InsertCheckpointError> { + ) -> Result, chain_graph::InsertCheckpointError> { Ok(KeychainChangeSet { chain_graph: self.chain_graph.insert_checkpoint_preview(block_id)?, ..Default::default() @@ -177,7 +176,7 @@ where pub fn insert_checkpoint( &mut self, block_id: BlockId, - ) -> Result, chain_graph::InsertCheckpointError> { + ) -> Result, chain_graph::InsertCheckpointError> { let changeset = self.insert_checkpoint_preview(block_id)?; self.apply_changeset(changeset.clone()); Ok(changeset) @@ -192,7 +191,7 @@ where &self, tx: Transaction, pos: P, - ) -> Result, chain_graph::InsertTxError

> { + ) -> Result, chain_graph::InsertTxError

> { Ok(KeychainChangeSet { chain_graph: self.chain_graph.insert_tx_preview(tx, pos)?, ..Default::default() @@ -210,7 +209,7 @@ where &mut self, tx: Transaction, pos: P, - ) -> Result, chain_graph::InsertTxError

> { + ) -> Result, chain_graph::InsertTxError

> { let changeset = self.insert_tx_preview(tx, pos)?; self.apply_changeset(changeset.clone()); Ok(changeset) @@ -281,7 +280,7 @@ where } } -impl Default for KeychainTracker { +impl Default for KeychainTracker { fn default() -> Self { Self { txout_index: Default::default(), @@ -290,20 +289,20 @@ impl Default for KeychainTracker { } } -impl AsRef> for KeychainTracker { +impl AsRef> for KeychainTracker { fn as_ref(&self) -> &SparseChain

{ self.chain_graph.chain() } } -impl AsRef> for KeychainTracker { - fn as_ref(&self) -> &TxGraph { +impl AsRef for KeychainTracker { + fn as_ref(&self) -> &TxGraph { self.chain_graph.graph() } } -impl AsRef> for KeychainTracker { - fn as_ref(&self) -> &ChainGraph { +impl AsRef> for KeychainTracker { + fn as_ref(&self) -> &ChainGraph

{ &self.chain_graph } } diff --git a/crates/chain/tests/test_chain_graph.rs b/crates/chain/tests/test_chain_graph.rs index f7b39d2b0..0514acc99 100644 --- a/crates/chain/tests/test_chain_graph.rs +++ b/crates/chain/tests/test_chain_graph.rs @@ -10,9 +10,7 @@ use bdk_chain::{ tx_graph::{self, TxGraph, TxInGraph}, BlockId, TxHeight, }; -use bitcoin::{ - BlockHash, OutPoint, PackedLockTime, Script, Sequence, Transaction, TxIn, TxOut, Witness, -}; +use bitcoin::{OutPoint, PackedLockTime, Script, Sequence, Transaction, TxIn, TxOut, Witness}; #[test] fn test_spent_by() { @@ -47,7 +45,7 @@ fn test_spent_by() { output: vec![], }; - let mut cg1 = ChainGraph::<(u32, BlockHash), _>::default(); + let mut cg1 = ChainGraph::default(); let _ = cg1 .insert_tx(tx1, TxHeight::Unconfirmed) .expect("should insert"); @@ -128,7 +126,7 @@ fn update_evicts_conflicting_tx() { cg }; - let changeset = ChangeSet::<(u32, BlockHash), TxHeight> { + let changeset = ChangeSet:: { chain: sparse_chain::ChangeSet { checkpoints: Default::default(), txids: [ @@ -137,7 +135,7 @@ fn update_evicts_conflicting_tx() { ] .into(), }, - graph: tx_graph::Additions::<(u32, BlockHash)> { + graph: tx_graph::Additions { tx: [tx_b2.clone()].into(), txout: [].into(), ..Default::default() @@ -154,7 +152,7 @@ fn update_evicts_conflicting_tx() { { let cg1 = { - let mut cg = ChainGraph::<(u32, BlockHash), _>::default(); + let mut cg = ChainGraph::default(); let _ = cg.insert_checkpoint(cp_a).expect("should insert cp"); let _ = cg.insert_checkpoint(cp_b).expect("should insert cp"); let _ = cg @@ -208,7 +206,7 @@ fn update_evicts_conflicting_tx() { cg }; - let changeset = ChangeSet::<(u32, BlockHash), TxHeight> { + let changeset = ChangeSet:: { chain: sparse_chain::ChangeSet { checkpoints: [(1, Some(h!("B'")))].into(), txids: [ @@ -217,7 +215,7 @@ fn update_evicts_conflicting_tx() { ] .into(), }, - graph: tx_graph::Additions::<(u32, BlockHash)> { + graph: tx_graph::Additions { tx: [tx_b2].into(), txout: [].into(), ..Default::default() @@ -256,7 +254,7 @@ fn chain_graph_new_missing() { (tx_b.txid(), TxHeight::Confirmed(0)) ] ); - let mut graph = TxGraph::<(u32, BlockHash)>::default(); + let mut graph = TxGraph::default(); let mut expected_missing = HashSet::new(); expected_missing.insert(tx_a.txid()); @@ -293,7 +291,7 @@ fn chain_graph_new_missing() { let new_graph = ChainGraph::new(update.clone(), graph.clone()).unwrap(); let expected_graph = { - let mut cg = ChainGraph::<(u32, BlockHash), TxHeight>::default(); + let mut cg = ChainGraph::::default(); let _ = cg .insert_checkpoint(update.latest_checkpoint().unwrap()) .unwrap(); @@ -348,7 +346,7 @@ fn chain_graph_new_conflicts() { ] ); - let graph = TxGraph::<(u32, BlockHash)>::new([tx_a, tx_b, tx_b2]); + let graph = TxGraph::new([tx_a, tx_b, tx_b2]); assert!(matches!( ChainGraph::new(chain, graph), @@ -358,7 +356,7 @@ fn chain_graph_new_conflicts() { #[test] fn test_get_tx_in_chain() { - let mut cg = ChainGraph::<(u32, BlockHash), _>::default(); + let mut cg = ChainGraph::default(); let tx = Transaction { version: 0x01, lock_time: PackedLockTime(0), @@ -383,7 +381,7 @@ fn test_get_tx_in_chain() { #[test] fn test_iterate_transactions() { - let mut cg = ChainGraph::::default(); + let mut cg = ChainGraph::default(); let txs = (0..3) .map(|i| Transaction { version: i, @@ -480,7 +478,7 @@ fn test_apply_changes_reintroduce_tx() { // block1, block2a, tx1, tx2a let mut cg = { - let mut cg = ChainGraph::<(u32, BlockHash), _>::default(); + let mut cg = ChainGraph::default(); let _ = cg.insert_checkpoint(block1).unwrap(); let _ = cg.insert_checkpoint(block2a).unwrap(); let _ = cg.insert_tx(tx1, TxHeight::Confirmed(1)).unwrap(); @@ -636,7 +634,7 @@ fn test_evict_descendants() { let txid_conflict = tx_conflict.txid(); let cg = { - let mut cg = ChainGraph::<(u32, BlockHash), TxHeight>::default(); + let mut cg = ChainGraph::::default(); let _ = cg.insert_checkpoint(block_1); let _ = cg.insert_checkpoint(block_2a); let _ = cg.insert_tx(tx_1, TxHeight::Confirmed(1)); @@ -648,7 +646,7 @@ fn test_evict_descendants() { }; let update = { - let mut cg = ChainGraph::<(u32, BlockHash), TxHeight>::default(); + let mut cg = ChainGraph::::default(); let _ = cg.insert_checkpoint(block_1); let _ = cg.insert_checkpoint(block_2b); let _ = cg.insert_tx(tx_conflict.clone(), TxHeight::Confirmed(2)); diff --git a/crates/chain/tests/test_keychain_tracker.rs b/crates/chain/tests/test_keychain_tracker.rs index b4e51d850..c3fee3475 100644 --- a/crates/chain/tests/test_keychain_tracker.rs +++ b/crates/chain/tests/test_keychain_tracker.rs @@ -12,11 +12,11 @@ use bdk_chain::{ tx_graph::TxInGraph, BlockId, ConfirmationTime, TxHeight, }; -use bitcoin::{BlockHash, TxIn}; +use bitcoin::TxIn; #[test] fn test_insert_tx() { - let mut tracker = KeychainTracker::<_, BlockId, _>::default(); + let mut tracker = KeychainTracker::default(); let secp = Secp256k1::new(); let (descriptor, _) = Descriptor::parse_descriptor(&secp, "tr([73c5da0a/86'/0'/0']xprv9xgqHN7yz9MwCkxsBPN5qetuNdQSUttZNKw1dcYTV4mkaAFiBVGQziHs3NRSWMkCzvgjEe3n9xV8oYywvM8at9yRqyaZVz6TYYhX98VjsUk/0/*)").unwrap(); tracker.add_keychain((), descriptor.clone()); @@ -72,7 +72,7 @@ fn test_balance() { One, Two, } - let mut tracker = KeychainTracker::::default(); + let mut tracker = KeychainTracker::default(); let one = Descriptor::from_str("tr([73c5da0a/86'/0'/0']xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/0/*)#rg247h69").unwrap(); let two = Descriptor::from_str("tr([73c5da0a/86'/0'/0']xpub6BgBgsespWvERF3LHQu6CnqdvfEvtMcQjYrcRzx53QJjSxarj2afYWcLteoGVky7D3UKDP9QyrLprQ3VCECoY49yfdDEHGCtMMj92pReUsQ/1/*)#ju05rz2a").unwrap(); tracker.add_keychain(Keychain::One, one); diff --git a/crates/electrum/src/lib.rs b/crates/electrum/src/lib.rs index d062cfdc3..bddbd8f25 100644 --- a/crates/electrum/src/lib.rs +++ b/crates/electrum/src/lib.rs @@ -32,7 +32,7 @@ use bdk_chain::{ keychain::KeychainScan, sparse_chain::{self, ChainPosition, SparseChain}, tx_graph::TxGraph, - BlockAnchor, BlockId, ConfirmationTime, TxHeight, + BlockId, ConfirmationTime, TxHeight, }; pub use electrum_client; use electrum_client::{Client, ElectrumApi, Error}; @@ -243,14 +243,13 @@ impl ElectrumUpdate { /// `tracker`. /// /// This will fail if there are missing full transactions not provided via `new_txs`. - pub fn into_keychain_scan( + pub fn into_keychain_scan( self, new_txs: Vec, chain_graph: &CG, - ) -> Result, chain_graph::NewError

> + ) -> Result, chain_graph::NewError

> where - CG: AsRef>, - A: BlockAnchor, + CG: AsRef>, { Ok(KeychainScan { update: chain_graph diff --git a/crates/esplora/src/async_ext.rs b/crates/esplora/src/async_ext.rs index 420f1197a..266fd30b6 100644 --- a/crates/esplora/src/async_ext.rs +++ b/crates/esplora/src/async_ext.rs @@ -48,7 +48,7 @@ pub trait EsploraAsyncExt { outpoints: impl IntoIterator + Send> + Send, stop_gap: usize, parallel_requests: usize, - ) -> Result, Error>; + ) -> Result, Error>; /// Convenience method to call [`scan`] without requiring a keychain. /// @@ -61,7 +61,7 @@ pub trait EsploraAsyncExt { txids: impl IntoIterator + Send> + Send, outpoints: impl IntoIterator + Send> + Send, parallel_requests: usize, - ) -> Result, Error> { + ) -> Result, Error> { let wallet_scan = self .scan( local_chain, @@ -100,7 +100,7 @@ impl EsploraAsyncExt for esplora_client::AsyncClient { outpoints: impl IntoIterator + Send> + Send, stop_gap: usize, parallel_requests: usize, - ) -> Result, Error> { + ) -> Result, Error> { let txids = txids.into_iter(); let outpoints = outpoints.into_iter(); let parallel_requests = parallel_requests.max(1); diff --git a/crates/esplora/src/blocking_ext.rs b/crates/esplora/src/blocking_ext.rs index d4a511ac7..c22668a53 100644 --- a/crates/esplora/src/blocking_ext.rs +++ b/crates/esplora/src/blocking_ext.rs @@ -38,7 +38,7 @@ pub trait EsploraExt { outpoints: impl IntoIterator, stop_gap: usize, parallel_requests: usize, - ) -> Result, Error>; + ) -> Result, Error>; /// Convenience method to call [`scan`] without requiring a keychain. /// @@ -51,7 +51,7 @@ pub trait EsploraExt { txids: impl IntoIterator, outpoints: impl IntoIterator, parallel_requests: usize, - ) -> Result, Error> { + ) -> Result, Error> { let wallet_scan = self.scan( local_chain, [( @@ -81,7 +81,7 @@ impl EsploraExt for esplora_client::BlockingClient { outpoints: impl IntoIterator, stop_gap: usize, parallel_requests: usize, - ) -> Result, Error> { + ) -> Result, Error> { let parallel_requests = parallel_requests.max(1); let mut scan = KeychainScan::default(); let update = &mut scan.update; diff --git a/crates/file_store/src/file_store.rs b/crates/file_store/src/file_store.rs index ba0dc21db..824e3ccc5 100644 --- a/crates/file_store/src/file_store.rs +++ b/crates/file_store/src/file_store.rs @@ -4,7 +4,7 @@ //! [`KeychainChangeSet`]s which can be used to restore a [`KeychainTracker`]. use bdk_chain::{ keychain::{KeychainChangeSet, KeychainTracker}, - sparse_chain, BlockAnchor, + sparse_chain, }; use bincode::{DefaultOptions, Options}; use core::marker::PhantomData; @@ -23,21 +23,20 @@ const MAGIC_BYTES: [u8; MAGIC_BYTES_LEN] = [98, 100, 107, 102, 115, 48, 48, 48, /// Persists an append only list of `KeychainChangeSet` to a single file. /// [`KeychainChangeSet`] record the changes made to a [`KeychainTracker`]. #[derive(Debug)] -pub struct KeychainStore { +pub struct KeychainStore { db_file: File, - changeset_type_params: core::marker::PhantomData<(K, A, P)>, + changeset_type_params: core::marker::PhantomData<(K, P)>, } fn bincode() -> impl bincode::Options { DefaultOptions::new().with_varint_encoding() } -impl KeychainStore +impl KeychainStore where K: Ord + Clone + core::fmt::Debug, - A: BlockAnchor, P: sparse_chain::ChainPosition, - KeychainChangeSet: serde::Serialize + serde::de::DeserializeOwned, + KeychainChangeSet: serde::Serialize + serde::de::DeserializeOwned, { /// Creates a new store from a [`File`]. /// @@ -86,9 +85,7 @@ where /// **WARNING**: This method changes the write position in the underlying file. You should /// always iterate over all entries until `None` is returned if you want your next write to go /// at the end; otherwise, you will write over existing entries. - pub fn iter_changesets( - &mut self, - ) -> Result>, io::Error> { + pub fn iter_changesets(&mut self) -> Result>, io::Error> { self.db_file .seek(io::SeekFrom::Start(MAGIC_BYTES_LEN as _))?; @@ -107,7 +104,7 @@ where /// /// **WARNING**: This method changes the write position of the underlying file. The next /// changeset will be written over the erroring entry (or the end of the file if none existed). - pub fn aggregate_changeset(&mut self) -> (KeychainChangeSet, Result<(), IterError>) { + pub fn aggregate_changeset(&mut self) -> (KeychainChangeSet, Result<(), IterError>) { let mut changeset = KeychainChangeSet::default(); let result = (|| { let iter_changeset = self.iter_changesets()?; @@ -127,7 +124,7 @@ where /// changeset will be written over the erroring entry (or the end of the file if none existed). pub fn load_into_keychain_tracker( &mut self, - tracker: &mut KeychainTracker, + tracker: &mut KeychainTracker, ) -> Result<(), IterError> { for changeset in self.iter_changesets()? { tracker.apply_changeset(changeset?) @@ -141,7 +138,7 @@ where /// directly after the appended changeset. pub fn append_changeset( &mut self, - changeset: &KeychainChangeSet, + changeset: &KeychainChangeSet, ) -> Result<(), io::Error> { if changeset.is_empty() { return Ok(()); @@ -291,7 +288,7 @@ mod test { use super::*; use bdk_chain::{ keychain::{DerivationAdditions, KeychainChangeSet}, - BlockId, TxHeight, + TxHeight, }; use std::{ io::{Read, Write}, @@ -335,7 +332,7 @@ mod test { file.write_all(&MAGIC_BYTES[..MAGIC_BYTES_LEN - 1]) .expect("should write"); - match KeychainStore::::new(file.reopen().unwrap()) { + match KeychainStore::::new(file.reopen().unwrap()) { Err(FileError::Io(e)) => assert_eq!(e.kind(), std::io::ErrorKind::UnexpectedEof), unexpected => panic!("unexpected result: {:?}", unexpected), }; @@ -349,7 +346,7 @@ mod test { file.write_all(invalid_magic_bytes.as_bytes()) .expect("should write"); - match KeychainStore::::new(file.reopen().unwrap()) { + match KeychainStore::::new(file.reopen().unwrap()) { Err(FileError::InvalidMagicBytes(b)) => { assert_eq!(b, invalid_magic_bytes.as_bytes()) } @@ -373,9 +370,8 @@ mod test { let mut file = NamedTempFile::new().unwrap(); file.write_all(&data).expect("should write"); - let mut store = - KeychainStore::::new(file.reopen().unwrap()) - .expect("should open"); + let mut store = KeychainStore::::new(file.reopen().unwrap()) + .expect("should open"); match store.iter_changesets().expect("seek should succeed").next() { Some(Err(IterError::Bincode(_))) => {} unexpected_res => panic!("unexpected result: {:?}", unexpected_res), diff --git a/crates/file_store/src/lib.rs b/crates/file_store/src/lib.rs index a9673be94..e33474194 100644 --- a/crates/file_store/src/lib.rs +++ b/crates/file_store/src/lib.rs @@ -3,16 +3,14 @@ mod file_store; use bdk_chain::{ keychain::{KeychainChangeSet, KeychainTracker, PersistBackend}, sparse_chain::ChainPosition, - BlockAnchor, }; pub use file_store::*; -impl PersistBackend for KeychainStore +impl PersistBackend for KeychainStore where K: Ord + Clone + core::fmt::Debug, - A: BlockAnchor, P: ChainPosition, - KeychainChangeSet: serde::Serialize + serde::de::DeserializeOwned, + KeychainChangeSet: serde::Serialize + serde::de::DeserializeOwned, { type WriteError = std::io::Error; @@ -20,14 +18,14 @@ where fn append_changeset( &mut self, - changeset: &KeychainChangeSet, + changeset: &KeychainChangeSet, ) -> Result<(), Self::WriteError> { KeychainStore::append_changeset(self, changeset) } fn load_into_keychain_tracker( &mut self, - tracker: &mut KeychainTracker, + tracker: &mut KeychainTracker, ) -> Result<(), Self::LoadError> { KeychainStore::load_into_keychain_tracker(self, tracker) } diff --git a/example-crates/keychain_tracker_electrum/src/main.rs b/example-crates/keychain_tracker_electrum/src/main.rs index 08f29ceb7..c8b9e0684 100644 --- a/example-crates/keychain_tracker_electrum/src/main.rs +++ b/example-crates/keychain_tracker_electrum/src/main.rs @@ -48,7 +48,7 @@ pub struct ScanOptions { } fn main() -> anyhow::Result<()> { - let (args, keymap, tracker, db) = cli::init::()?; + let (args, keymap, tracker, db) = cli::init::()?; let electrum_url = match args.network { Network::Bitcoin => "ssl://electrum.blockstream.info:50002", diff --git a/example-crates/keychain_tracker_esplora/src/main.rs b/example-crates/keychain_tracker_esplora/src/main.rs index 04d121d23..cae5e9601 100644 --- a/example-crates/keychain_tracker_esplora/src/main.rs +++ b/example-crates/keychain_tracker_esplora/src/main.rs @@ -49,7 +49,7 @@ pub struct ScanOptions { } fn main() -> anyhow::Result<()> { - let (args, keymap, keychain_tracker, db) = cli::init::()?; + let (args, keymap, keychain_tracker, db) = cli::init::()?; let esplora_url = match args.network { Network::Bitcoin => "https://mempool.space/api", Network::Testnet => "https://mempool.space/testnet/api", diff --git a/example-crates/keychain_tracker_example_cli/src/lib.rs b/example-crates/keychain_tracker_example_cli/src/lib.rs index e118cbf43..df42df1ac 100644 --- a/example-crates/keychain_tracker_example_cli/src/lib.rs +++ b/example-crates/keychain_tracker_example_cli/src/lib.rs @@ -13,7 +13,7 @@ use bdk_chain::{ Descriptor, DescriptorPublicKey, }, sparse_chain::{self, ChainPosition}, - BlockAnchor, DescriptorExt, FullTxOut, + DescriptorExt, FullTxOut, }; use bdk_coin_select::{coin_select_bnb, CoinSelector, CoinSelectorOpt, WeightedValue}; use bdk_file_store::KeychainStore; @@ -179,16 +179,15 @@ pub struct AddrsOutput { used: bool, } -pub fn run_address_cmd( - tracker: &Mutex>, - db: &Mutex>, +pub fn run_address_cmd

( + tracker: &Mutex>, + db: &Mutex>, addr_cmd: AddressCmd, network: Network, ) -> Result<()> where - A: bdk_chain::BlockAnchor, P: bdk_chain::sparse_chain::ChainPosition, - KeychainChangeSet: serde::Serialize + serde::de::DeserializeOwned, + KeychainChangeSet: serde::Serialize + serde::de::DeserializeOwned, { let mut tracker = tracker.lock().unwrap(); let txout_index = &mut tracker.txout_index; @@ -242,9 +241,7 @@ where } } -pub fn run_balance_cmd( - tracker: &Mutex>, -) { +pub fn run_balance_cmd(tracker: &Mutex>) { let tracker = tracker.lock().unwrap(); let (confirmed, unconfirmed) = tracker @@ -261,9 +258,9 @@ pub fn run_balance_cmd( println!("unconfirmed: {}", unconfirmed); } -pub fn run_txo_cmd( +pub fn run_txo_cmd( txout_cmd: TxOutCmd, - tracker: &Mutex>, + tracker: &Mutex>, network: Network, ) { match txout_cmd { @@ -316,11 +313,11 @@ pub fn run_txo_cmd( } #[allow(clippy::type_complexity)] // FIXME -pub fn create_tx( +pub fn create_tx( value: u64, address: Address, coin_select: CoinSelectionAlgo, - keychain_tracker: &mut KeychainTracker, + keychain_tracker: &mut KeychainTracker, keymap: &HashMap, ) -> Result<( Transaction, @@ -529,20 +526,19 @@ pub fn create_tx( Ok((transaction, change_info)) } -pub fn handle_commands( +pub fn handle_commands( command: Commands, broadcast: impl FnOnce(&Transaction) -> Result<()>, // we Mutex around these not because we need them for a simple CLI app but to demonstrate how // all the stuff we're doing can be made thread-safe and not keep locks up over an IO bound. - tracker: &Mutex>, - store: &Mutex>, + tracker: &Mutex>, + store: &Mutex>, network: Network, keymap: &HashMap, ) -> Result<()> where - A: BlockAnchor, P: ChainPosition, - KeychainChangeSet: serde::Serialize + serde::de::DeserializeOwned, + KeychainChangeSet: serde::Serialize + serde::de::DeserializeOwned, { match command { // TODO: Make these functions return stuffs @@ -623,18 +619,17 @@ where } #[allow(clippy::type_complexity)] // FIXME -pub fn init() -> anyhow::Result<( +pub fn init() -> anyhow::Result<( Args, KeyMap, // These don't need to have mutexes around them, but we want the cli example code to make it obvious how they // are thread-safe, forcing the example developers to show where they would lock and unlock things. - Mutex>, - Mutex>, + Mutex>, + Mutex>, )> where - A: BlockAnchor, P: sparse_chain::ChainPosition, - KeychainChangeSet: serde::Serialize + serde::de::DeserializeOwned, + KeychainChangeSet: serde::Serialize + serde::de::DeserializeOwned, { let args = Args::::parse(); let secp = Secp256k1::default(); @@ -660,7 +655,7 @@ where .add_keychain(Keychain::Internal, internal_descriptor); }; - let mut db = KeychainStore::::new_from_path(args.db_path.as_path())?; + let mut db = KeychainStore::::new_from_path(args.db_path.as_path())?; if let Err(e) = db.load_into_keychain_tracker(&mut tracker) { match tracker.chain().latest_checkpoint() { @@ -674,8 +669,8 @@ where Ok((args, keymap, Mutex::new(tracker), Mutex::new(db))) } -pub fn planned_utxos<'a, AK: bdk_tmp_plan::CanDerive + Clone, A: BlockAnchor, P: ChainPosition>( - tracker: &'a KeychainTracker, +pub fn planned_utxos<'a, AK: bdk_tmp_plan::CanDerive + Clone, P: ChainPosition>( + tracker: &'a KeychainTracker, assets: &'a bdk_tmp_plan::Assets, ) -> impl Iterator, FullTxOut

)> + 'a { tracker