[bdk_chain_redesign] Docs for is_mature and is_confirmed_and_spendable

Docs are updated to explain why `is_mature` and
`is_confirmed_and_spendable` may return false-negatives.
This commit is contained in:
志宇 2023-04-22 23:39:49 +08:00
parent ac336aa32f
commit ecc74ce4cd
No known key found for this signature in database
GPG Key ID: F6345C9837C2BDE8

View File

@ -247,12 +247,14 @@ impl<A: Anchor> FullTxOut<ObservedAs<A>> {
/// This is the alternative version of [`is_mature`] which depends on `chain_position` being a /// This is the alternative version of [`is_mature`] which depends on `chain_position` being a
/// [`ObservedAs<A>`] where `A` implements [`Anchor`]. /// [`ObservedAs<A>`] where `A` implements [`Anchor`].
/// ///
/// Depending on the implementation of [`confirmation_height_upper_bound`] in [`Anchor`], this
/// method may return false-negatives. In other words, interpretted confirmation count may be
/// less than the actual value.
///
/// [`is_mature`]: Self::is_mature /// [`is_mature`]: Self::is_mature
/// [`confirmation_height_upper_bound`]: Anchor::confirmation_height_upper_bound
pub fn is_mature(&self, tip: u32) -> bool { pub fn is_mature(&self, tip: u32) -> bool {
if !self.is_on_coinbase { if self.is_on_coinbase {
return true;
}
let tx_height = match &self.chain_position { let tx_height = match &self.chain_position {
ObservedAs::Confirmed(anchor) => anchor.confirmation_height_upper_bound(), ObservedAs::Confirmed(anchor) => anchor.confirmation_height_upper_bound(),
ObservedAs::Unconfirmed(_) => { ObservedAs::Unconfirmed(_) => {
@ -260,11 +262,11 @@ impl<A: Anchor> FullTxOut<ObservedAs<A>> {
return false; return false;
} }
}; };
let age = tip.saturating_sub(tx_height); let age = tip.saturating_sub(tx_height);
if age + 1 < COINBASE_MATURITY { if age + 1 < COINBASE_MATURITY {
return false; return false;
} }
}
true true
} }
@ -276,7 +278,12 @@ impl<A: Anchor> FullTxOut<ObservedAs<A>> {
/// This is the alternative version of [`is_spendable_at`] which depends on `chain_position` /// This is the alternative version of [`is_spendable_at`] which depends on `chain_position`
/// being a [`ObservedAs<A>`] where `A` implements [`Anchor`]. /// being a [`ObservedAs<A>`] where `A` implements [`Anchor`].
/// ///
/// Depending on the implementation of [`confirmation_height_upper_bound`] in [`Anchor`], this
/// method may return false-negatives. In other words, interpretted confirmation count may be
/// less than the actual value.
///
/// [`is_spendable_at`]: Self::is_spendable_at /// [`is_spendable_at`]: Self::is_spendable_at
/// [`confirmation_height_upper_bound`]: Anchor::confirmation_height_upper_bound
pub fn is_confirmed_and_spendable(&self, tip: u32) -> bool { pub fn is_confirmed_and_spendable(&self, tip: u32) -> bool {
if !self.is_mature(tip) { if !self.is_mature(tip) {
return false; return false;
@ -300,5 +307,3 @@ impl<A: Anchor> FullTxOut<ObservedAs<A>> {
true true
} }
} }
// TODO: make test