b206a985cffaa9b614841219371faa53ba23dfc3 fix: Even more refactoring to code and documentation (志宇) bea8e5aff4f1e4d61db3970a6efaec86e686dbc3 fix: `TxGraph::missing_blocks` logic (志宇) db15e03bdce78c6321f906f390b10b3d9e7501b2 fix: improve more docs and more refactoring (志宇) 95312d4d05618b4c464acc0fdff49fb17405ec88 fix: docs and some minor refactoring (志宇) 8bf7a997f70fdffd072fd37e12c385e731728c5a Refactor `debug_assertions` checks for `LocalChain` (志宇) 315e7e0b4b373d7175f21a48ff6480b6e919a2c6 fix: rm duplicate `bdk_tmp_plan` module (志宇) af705da1a846214f104df8886201a23cfa4b6b74 Add exclusion of example cli `*.db` files in `.gitignore` (志宇) eabeb6ccb169b32f7b7541c9dc6481693bdeeb8a Implement linked-list `LocalChain` and update chain-src crates/examples (志宇) Pull request description: Fixes #997 Replaces #1002 ### Description This PR changes the `LocalChain` implementation to have blocks stored as a linked-list. This allows the data-src thread to hold a shared ref to a single checkpoint and have access to the whole history of checkpoints without cloning or keeping a lock on `LocalChain`. The APIs of `bdk::Wallet`, `esplora` and `electrum` are also updated to reflect these changes. Note that the `esplora` crate is rewritten to anchor txs in the confirmation block (using the esplora API's tx status block_hash). This guarantees 100% consistency between anchor blocks and their transactions (instead of anchoring txs to the latest tip). `ExploraExt` now has separate methods for updating the `TxGraph` and `LocalChain`. A new method `TxGraph::missing_blocks` is introduced for finding "floating anchors" of a `TxGraph` update (given a chain). Additional changes: * `test_local_chain.rs` is refactored to make test cases easier to write. Additional tests are also added. * Examples are updated. * Exclude example-cli `*.db` files in `.gitignore`. * Rm duplicate `bdk_tmp_plan` module. ### Notes to the reviewers This is the smallest possible division of #1002 without resulting in PRs that do not compile. Since we have changed the API of `LocalChain`, we also need to change `esplora`, `electrum` crates and examples alongside `bdk::Wallet`. ### Changelog notice * Implement linked-list `LocalChain`. This allows the data-src thread to hold a shared ref to a single checkpoint and have access to the whole history of checkpoints without cloning or keeping a lock on `LocalChain`. * Rewrote `esplora` chain-src crate to anchor txs to their confirmation blocks (using esplora API's tx-status `block_hash`). ### 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 #### New Features: * [x] I've added tests for the new feature * [x] I've added docs for the new feature ACKs for top commit: LLFourn: ACK b206a985cffaa9b614841219371faa53ba23dfc3 Tree-SHA512: a513eecb4f1aae6a5c06a69854e4492961424312a75a42d74377d363b364e3d52415bc81b4aa3fbc3f369ded19bddd07ab895130ebba288e8a43e9d6186e9fcc
271 lines
8.5 KiB
Rust
271 lines
8.5 KiB
Rust
//! Module for keychain related structures.
|
|
//!
|
|
//! A keychain here is a set of application-defined indexes for a miniscript descriptor where we can
|
|
//! derive script pubkeys at a particular derivation index. The application's index is simply
|
|
//! anything that implements `Ord`.
|
|
//!
|
|
//! [`KeychainTxOutIndex`] indexes script pubkeys of keychains and scans in relevant outpoints (that
|
|
//! has a `txout` containing an indexed script pubkey). Internally, this uses [`SpkTxOutIndex`], but
|
|
//! also maintains "revealed" and "lookahead" index counts per keychain.
|
|
//!
|
|
//! [`SpkTxOutIndex`]: crate::SpkTxOutIndex
|
|
|
|
use crate::{
|
|
collections::BTreeMap, indexed_tx_graph::IndexedAdditions, local_chain, tx_graph::TxGraph,
|
|
Anchor, Append,
|
|
};
|
|
|
|
#[cfg(feature = "miniscript")]
|
|
mod txout_index;
|
|
#[cfg(feature = "miniscript")]
|
|
pub use txout_index::*;
|
|
|
|
/// Represents updates to the derivation index of a [`KeychainTxOutIndex`].
|
|
///
|
|
/// It can be applied to [`KeychainTxOutIndex`] with [`apply_additions`]. [`DerivationAdditions] are
|
|
/// monotone in that they will never decrease the revealed derivation index.
|
|
///
|
|
/// [`KeychainTxOutIndex`]: crate::keychain::KeychainTxOutIndex
|
|
/// [`apply_additions`]: crate::keychain::KeychainTxOutIndex::apply_additions
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
#[cfg_attr(
|
|
feature = "serde",
|
|
derive(serde::Deserialize, serde::Serialize),
|
|
serde(
|
|
crate = "serde_crate",
|
|
bound(
|
|
deserialize = "K: Ord + serde::Deserialize<'de>",
|
|
serialize = "K: Ord + serde::Serialize"
|
|
)
|
|
)
|
|
)]
|
|
#[must_use]
|
|
pub struct DerivationAdditions<K>(pub BTreeMap<K, u32>);
|
|
|
|
impl<K> DerivationAdditions<K> {
|
|
/// Get the inner map of the keychain to its new derivation index.
|
|
pub fn as_inner(&self) -> &BTreeMap<K, u32> {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
impl<K: Ord> Append for DerivationAdditions<K> {
|
|
/// Append another [`DerivationAdditions`] into self.
|
|
///
|
|
/// If the keychain already exists, increase the index when the other's index > self's index.
|
|
/// If the keychain did not exist, append the new keychain.
|
|
fn append(&mut self, mut other: Self) {
|
|
self.0.iter_mut().for_each(|(key, index)| {
|
|
if let Some(other_index) = other.0.remove(key) {
|
|
*index = other_index.max(*index);
|
|
}
|
|
});
|
|
|
|
self.0.append(&mut other.0);
|
|
}
|
|
|
|
/// Returns whether the additions are empty.
|
|
fn is_empty(&self) -> bool {
|
|
self.0.is_empty()
|
|
}
|
|
}
|
|
|
|
impl<K> Default for DerivationAdditions<K> {
|
|
fn default() -> Self {
|
|
Self(Default::default())
|
|
}
|
|
}
|
|
|
|
impl<K> AsRef<BTreeMap<K, u32>> for DerivationAdditions<K> {
|
|
fn as_ref(&self) -> &BTreeMap<K, u32> {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
/// A structure to update [`KeychainTxOutIndex`], [`TxGraph`] and [`LocalChain`] atomically.
|
|
///
|
|
/// [`LocalChain`]: local_chain::LocalChain
|
|
#[derive(Debug, Clone)]
|
|
pub struct LocalUpdate<K, A> {
|
|
/// Contains the last active derivation indices per keychain (`K`), which is used to update the
|
|
/// [`KeychainTxOutIndex`].
|
|
pub last_active_indices: BTreeMap<K, u32>,
|
|
|
|
/// Update for the [`TxGraph`].
|
|
pub graph: TxGraph<A>,
|
|
|
|
/// Update for the [`LocalChain`].
|
|
///
|
|
/// [`LocalChain`]: local_chain::LocalChain
|
|
pub chain: local_chain::Update,
|
|
}
|
|
|
|
impl<K, A> LocalUpdate<K, A> {
|
|
/// Construct a [`LocalUpdate`] with a given [`local_chain::Update`].
|
|
///
|
|
/// [`CheckPoint`]: local_chain::CheckPoint
|
|
pub fn new(chain_update: local_chain::Update) -> Self {
|
|
Self {
|
|
last_active_indices: BTreeMap::new(),
|
|
graph: TxGraph::default(),
|
|
chain: chain_update,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A structure that records the corresponding changes as result of applying an [`LocalUpdate`].
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
#[cfg_attr(
|
|
feature = "serde",
|
|
derive(serde::Deserialize, serde::Serialize),
|
|
serde(
|
|
crate = "serde_crate",
|
|
bound(
|
|
deserialize = "K: Ord + serde::Deserialize<'de>, A: Ord + serde::Deserialize<'de>",
|
|
serialize = "K: Ord + serde::Serialize, A: Ord + serde::Serialize",
|
|
)
|
|
)
|
|
)]
|
|
pub struct LocalChangeSet<K, A> {
|
|
/// Changes to the [`LocalChain`].
|
|
///
|
|
/// [`LocalChain`]: local_chain::LocalChain
|
|
pub chain_changeset: local_chain::ChangeSet,
|
|
|
|
/// Additions to [`IndexedTxGraph`].
|
|
///
|
|
/// [`IndexedTxGraph`]: crate::indexed_tx_graph::IndexedTxGraph
|
|
pub indexed_additions: IndexedAdditions<A, DerivationAdditions<K>>,
|
|
}
|
|
|
|
impl<K, A> Default for LocalChangeSet<K, A> {
|
|
fn default() -> Self {
|
|
Self {
|
|
chain_changeset: Default::default(),
|
|
indexed_additions: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<K: Ord, A: Anchor> Append for LocalChangeSet<K, A> {
|
|
fn append(&mut self, other: Self) {
|
|
Append::append(&mut self.chain_changeset, other.chain_changeset);
|
|
Append::append(&mut self.indexed_additions, other.indexed_additions);
|
|
}
|
|
|
|
fn is_empty(&self) -> bool {
|
|
self.chain_changeset.is_empty() && self.indexed_additions.is_empty()
|
|
}
|
|
}
|
|
|
|
impl<K, A> From<local_chain::ChangeSet> for LocalChangeSet<K, A> {
|
|
fn from(chain_changeset: local_chain::ChangeSet) -> Self {
|
|
Self {
|
|
chain_changeset,
|
|
..Default::default()
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<K, A> From<IndexedAdditions<A, DerivationAdditions<K>>> for LocalChangeSet<K, A> {
|
|
fn from(indexed_additions: IndexedAdditions<A, DerivationAdditions<K>>) -> Self {
|
|
Self {
|
|
indexed_additions,
|
|
..Default::default()
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Balance, differentiated into various categories.
|
|
#[derive(Debug, PartialEq, Eq, Clone, Default)]
|
|
#[cfg_attr(
|
|
feature = "serde",
|
|
derive(serde::Deserialize, serde::Serialize),
|
|
serde(crate = "serde_crate",)
|
|
)]
|
|
pub struct Balance {
|
|
/// All coinbase outputs not yet matured
|
|
pub immature: u64,
|
|
/// Unconfirmed UTXOs generated by a wallet tx
|
|
pub trusted_pending: u64,
|
|
/// Unconfirmed UTXOs received from an external wallet
|
|
pub untrusted_pending: u64,
|
|
/// Confirmed and immediately spendable balance
|
|
pub confirmed: u64,
|
|
}
|
|
|
|
impl Balance {
|
|
/// Get sum of trusted_pending and confirmed coins.
|
|
///
|
|
/// This is the balance you can spend right now that shouldn't get cancelled via another party
|
|
/// double spending it.
|
|
pub fn trusted_spendable(&self) -> u64 {
|
|
self.confirmed + self.trusted_pending
|
|
}
|
|
|
|
/// Get the whole balance visible to the wallet.
|
|
pub fn total(&self) -> u64 {
|
|
self.confirmed + self.trusted_pending + self.untrusted_pending + self.immature
|
|
}
|
|
}
|
|
|
|
impl core::fmt::Display for Balance {
|
|
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
|
write!(
|
|
f,
|
|
"{{ immature: {}, trusted_pending: {}, untrusted_pending: {}, confirmed: {} }}",
|
|
self.immature, self.trusted_pending, self.untrusted_pending, self.confirmed
|
|
)
|
|
}
|
|
}
|
|
|
|
impl core::ops::Add for Balance {
|
|
type Output = Self;
|
|
|
|
fn add(self, other: Self) -> Self {
|
|
Self {
|
|
immature: self.immature + other.immature,
|
|
trusted_pending: self.trusted_pending + other.trusted_pending,
|
|
untrusted_pending: self.untrusted_pending + other.untrusted_pending,
|
|
confirmed: self.confirmed + other.confirmed,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn append_keychain_derivation_indices() {
|
|
#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug)]
|
|
enum Keychain {
|
|
One,
|
|
Two,
|
|
Three,
|
|
Four,
|
|
}
|
|
let mut lhs_di = BTreeMap::<Keychain, u32>::default();
|
|
let mut rhs_di = BTreeMap::<Keychain, u32>::default();
|
|
lhs_di.insert(Keychain::One, 7);
|
|
lhs_di.insert(Keychain::Two, 0);
|
|
rhs_di.insert(Keychain::One, 3);
|
|
rhs_di.insert(Keychain::Two, 5);
|
|
lhs_di.insert(Keychain::Three, 3);
|
|
rhs_di.insert(Keychain::Four, 4);
|
|
|
|
let mut lhs = DerivationAdditions(lhs_di);
|
|
let rhs = DerivationAdditions(rhs_di);
|
|
lhs.append(rhs);
|
|
|
|
// Exiting index doesn't update if the new index in `other` is lower than `self`.
|
|
assert_eq!(lhs.0.get(&Keychain::One), Some(&7));
|
|
// Existing index updates if the new index in `other` is higher than `self`.
|
|
assert_eq!(lhs.0.get(&Keychain::Two), Some(&5));
|
|
// Existing index is unchanged if keychain doesn't exist in `other`.
|
|
assert_eq!(lhs.0.get(&Keychain::Three), Some(&3));
|
|
// New keychain gets added if the keychain is in `other` but not in `self`.
|
|
assert_eq!(lhs.0.get(&Keychain::Four), Some(&4));
|
|
}
|
|
}
|