2023-03-10 23:23:29 +05:30
|
|
|
//! Module for keychain related structures.
|
2023-03-01 11:09:08 +01:00
|
|
|
//!
|
2023-03-10 23:23:29 +05:30
|
|
|
//! A keychain here is a set of application-defined indexes for a miniscript descriptor where we can
|
2023-03-01 11:09:08 +01:00
|
|
|
//! 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
|
2023-03-10 23:23:29 +05:30
|
|
|
//! also maintains "revealed" and "lookahead" index counts per keychain.
|
2023-03-01 11:09:08 +01:00
|
|
|
//!
|
|
|
|
//! [`SpkTxOutIndex`]: crate::SpkTxOutIndex
|
2023-03-31 12:39:00 +08:00
|
|
|
|
2023-08-21 15:18:16 +03:00
|
|
|
use crate::{collections::BTreeMap, Append};
|
2023-03-01 11:09:08 +01:00
|
|
|
|
|
|
|
#[cfg(feature = "miniscript")]
|
|
|
|
mod txout_index;
|
|
|
|
#[cfg(feature = "miniscript")]
|
|
|
|
pub use txout_index::*;
|
|
|
|
|
|
|
|
/// Represents updates to the derivation index of a [`KeychainTxOutIndex`].
|
2023-08-16 17:39:35 +02:00
|
|
|
/// It maps each keychain `K` to its last revealed index.
|
2023-03-01 11:09:08 +01:00
|
|
|
///
|
2024-01-17 14:07:14 -05:00
|
|
|
/// It can be applied to [`KeychainTxOutIndex`] with [`apply_changeset`]. [`ChangeSet`]s are
|
2023-03-01 11:09:08 +01:00
|
|
|
/// monotone in that they will never decrease the revealed derivation index.
|
|
|
|
///
|
|
|
|
/// [`KeychainTxOutIndex`]: crate::keychain::KeychainTxOutIndex
|
2023-08-07 17:43:17 +02:00
|
|
|
/// [`apply_changeset`]: crate::keychain::KeychainTxOutIndex::apply_changeset
|
2023-03-01 11:09:08 +01:00
|
|
|
#[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]
|
2023-08-07 17:43:17 +02:00
|
|
|
pub struct ChangeSet<K>(pub BTreeMap<K, u32>);
|
2023-03-01 11:09:08 +01:00
|
|
|
|
2023-08-07 17:43:17 +02:00
|
|
|
impl<K> ChangeSet<K> {
|
2023-03-10 23:23:29 +05:30
|
|
|
/// Get the inner map of the keychain to its new derivation index.
|
2023-03-01 11:09:08 +01:00
|
|
|
pub fn as_inner(&self) -> &BTreeMap<K, u32> {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-07 17:43:17 +02:00
|
|
|
impl<K: Ord> Append for ChangeSet<K> {
|
|
|
|
/// Append another [`ChangeSet`] into self.
|
2023-03-01 11:09:08 +01:00
|
|
|
///
|
2023-03-10 23:23:29 +05:30
|
|
|
/// 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.
|
2023-04-05 17:29:20 +08:00
|
|
|
fn append(&mut self, mut other: Self) {
|
2023-03-01 11:09:08 +01:00
|
|
|
self.0.iter_mut().for_each(|(key, index)| {
|
|
|
|
if let Some(other_index) = other.0.remove(key) {
|
|
|
|
*index = other_index.max(*index);
|
|
|
|
}
|
|
|
|
});
|
2024-01-07 14:39:31 +08:00
|
|
|
// We use `extend` instead of `BTreeMap::append` due to performance issues with `append`.
|
|
|
|
// Refer to https://github.com/rust-lang/rust/issues/34666#issuecomment-675658420
|
|
|
|
self.0.extend(other.0);
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
2023-05-09 09:59:42 +08:00
|
|
|
|
2023-08-07 17:43:17 +02:00
|
|
|
/// Returns whether the changeset are empty.
|
2023-05-09 09:59:42 +08:00
|
|
|
fn is_empty(&self) -> bool {
|
|
|
|
self.0.is_empty()
|
|
|
|
}
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
|
|
|
|
2023-08-07 17:43:17 +02:00
|
|
|
impl<K> Default for ChangeSet<K> {
|
2023-03-01 11:09:08 +01:00
|
|
|
fn default() -> Self {
|
|
|
|
Self(Default::default())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-07 17:43:17 +02:00
|
|
|
impl<K> AsRef<BTreeMap<K, u32>> for ChangeSet<K> {
|
2023-03-01 11:09:08 +01:00
|
|
|
fn as_ref(&self) -> &BTreeMap<K, u32> {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-10 23:23:29 +05:30
|
|
|
/// Balance, differentiated into various categories.
|
2023-03-01 11:09:08 +01:00
|
|
|
#[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::*;
|
2023-05-24 11:37:26 +08:00
|
|
|
|
2023-03-01 11:09:08 +01:00
|
|
|
#[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);
|
|
|
|
|
2023-08-07 17:43:17 +02:00
|
|
|
let mut lhs = ChangeSet(lhs_di);
|
|
|
|
let rhs = ChangeSet(rhs_di);
|
2023-03-01 11:09:08 +01:00
|
|
|
lhs.append(rhs);
|
|
|
|
|
2023-03-10 23:23:29 +05:30
|
|
|
// Exiting index doesn't update if the new index in `other` is lower than `self`.
|
2023-05-24 11:37:26 +08:00
|
|
|
assert_eq!(lhs.0.get(&Keychain::One), Some(&7));
|
2023-03-10 23:23:29 +05:30
|
|
|
// Existing index updates if the new index in `other` is higher than `self`.
|
2023-05-24 11:37:26 +08:00
|
|
|
assert_eq!(lhs.0.get(&Keychain::Two), Some(&5));
|
2023-03-10 23:23:29 +05:30
|
|
|
// Existing index is unchanged if keychain doesn't exist in `other`.
|
2023-05-24 11:37:26 +08:00
|
|
|
assert_eq!(lhs.0.get(&Keychain::Three), Some(&3));
|
2023-03-10 23:23:29 +05:30
|
|
|
// New keychain gets added if the keychain is in `other` but not in `self`.
|
2023-05-24 11:37:26 +08:00
|
|
|
assert_eq!(lhs.0.get(&Keychain::Four), Some(&4));
|
2023-03-01 11:09:08 +01:00
|
|
|
}
|
|
|
|
}
|