[chain_redesign] Remove old structures
Other changes: * The `async-https` feature of `bdk_esplora` is no longer default. * Rename `ObservedAs` to `ChainPosition`. * Set temporary MSRV to 1.60.0 to compile all workspace members will all features.
This commit is contained in:
@@ -1,102 +1,43 @@
|
||||
use bitcoin::{hashes::Hash, BlockHash, OutPoint, TxOut, Txid};
|
||||
|
||||
use crate::{
|
||||
sparse_chain::{self, ChainPosition},
|
||||
Anchor, COINBASE_MATURITY,
|
||||
};
|
||||
use crate::{Anchor, COINBASE_MATURITY};
|
||||
|
||||
/// Represents an observation of some chain data.
|
||||
/// Represents the observed position of some chain data.
|
||||
///
|
||||
/// The generic `A` should be a [`Anchor`] implementation.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, core::hash::Hash)]
|
||||
pub enum ObservedAs<A> {
|
||||
pub enum ChainPosition<A> {
|
||||
/// The chain data is seen as confirmed, and in anchored by `A`.
|
||||
Confirmed(A),
|
||||
/// The chain data is seen in mempool at this given timestamp.
|
||||
Unconfirmed(u64),
|
||||
}
|
||||
|
||||
impl<A> ObservedAs<A> {
|
||||
/// Returns whether [`ObservedAs`] is confirmed or not.
|
||||
impl<A> ChainPosition<A> {
|
||||
/// Returns whether [`ChainPosition`] is confirmed or not.
|
||||
pub fn is_confirmed(&self) -> bool {
|
||||
matches!(self, Self::Confirmed(_))
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Clone> ObservedAs<&A> {
|
||||
pub fn cloned(self) -> ObservedAs<A> {
|
||||
impl<A: Clone> ChainPosition<&A> {
|
||||
pub fn cloned(self) -> ChainPosition<A> {
|
||||
match self {
|
||||
ObservedAs::Confirmed(a) => ObservedAs::Confirmed(a.clone()),
|
||||
ObservedAs::Unconfirmed(last_seen) => ObservedAs::Unconfirmed(last_seen),
|
||||
ChainPosition::Confirmed(a) => ChainPosition::Confirmed(a.clone()),
|
||||
ChainPosition::Unconfirmed(last_seen) => ChainPosition::Unconfirmed(last_seen),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents the height at which a transaction is confirmed.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
#[cfg_attr(
|
||||
feature = "serde",
|
||||
derive(serde::Deserialize, serde::Serialize),
|
||||
serde(crate = "serde_crate")
|
||||
)]
|
||||
pub enum TxHeight {
|
||||
Confirmed(u32),
|
||||
Unconfirmed,
|
||||
}
|
||||
|
||||
impl Default for TxHeight {
|
||||
fn default() -> Self {
|
||||
Self::Unconfirmed
|
||||
}
|
||||
}
|
||||
|
||||
impl core::fmt::Display for TxHeight {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
impl<A: Anchor> ChainPosition<A> {
|
||||
pub fn confirmation_height_upper_bound(&self) -> Option<u32> {
|
||||
match self {
|
||||
Self::Confirmed(h) => core::write!(f, "confirmed_at({})", h),
|
||||
Self::Unconfirmed => core::write!(f, "unconfirmed"),
|
||||
ChainPosition::Confirmed(a) => Some(a.confirmation_height_upper_bound()),
|
||||
ChainPosition::Unconfirmed(_) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Option<u32>> for TxHeight {
|
||||
fn from(opt: Option<u32>) -> Self {
|
||||
match opt {
|
||||
Some(h) => Self::Confirmed(h),
|
||||
None => Self::Unconfirmed,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<TxHeight> for Option<u32> {
|
||||
fn from(height: TxHeight) -> Self {
|
||||
match height {
|
||||
TxHeight::Confirmed(h) => Some(h),
|
||||
TxHeight::Unconfirmed => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl crate::sparse_chain::ChainPosition for TxHeight {
|
||||
fn height(&self) -> TxHeight {
|
||||
*self
|
||||
}
|
||||
|
||||
fn max_ord_of_height(height: TxHeight) -> Self {
|
||||
height
|
||||
}
|
||||
|
||||
fn min_ord_of_height(height: TxHeight) -> Self {
|
||||
height
|
||||
}
|
||||
}
|
||||
|
||||
impl TxHeight {
|
||||
pub fn is_confirmed(&self) -> bool {
|
||||
matches!(self, Self::Confirmed(_))
|
||||
}
|
||||
}
|
||||
|
||||
/// Block height and timestamp at which a transaction is confirmed.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Copy, PartialOrd, Ord, core::hash::Hash)]
|
||||
#[cfg_attr(
|
||||
@@ -109,49 +50,24 @@ pub enum ConfirmationTime {
|
||||
Unconfirmed { last_seen: u64 },
|
||||
}
|
||||
|
||||
impl sparse_chain::ChainPosition for ConfirmationTime {
|
||||
fn height(&self) -> TxHeight {
|
||||
match self {
|
||||
ConfirmationTime::Confirmed { height, .. } => TxHeight::Confirmed(*height),
|
||||
ConfirmationTime::Unconfirmed { .. } => TxHeight::Unconfirmed,
|
||||
}
|
||||
}
|
||||
|
||||
fn max_ord_of_height(height: TxHeight) -> Self {
|
||||
match height {
|
||||
TxHeight::Confirmed(height) => Self::Confirmed {
|
||||
height,
|
||||
time: u64::MAX,
|
||||
},
|
||||
TxHeight::Unconfirmed => Self::Unconfirmed { last_seen: 0 },
|
||||
}
|
||||
}
|
||||
|
||||
fn min_ord_of_height(height: TxHeight) -> Self {
|
||||
match height {
|
||||
TxHeight::Confirmed(height) => Self::Confirmed {
|
||||
height,
|
||||
time: u64::MIN,
|
||||
},
|
||||
TxHeight::Unconfirmed => Self::Unconfirmed { last_seen: 0 },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ConfirmationTime {
|
||||
pub fn unconfirmed(last_seen: u64) -> Self {
|
||||
Self::Unconfirmed { last_seen }
|
||||
}
|
||||
|
||||
pub fn is_confirmed(&self) -> bool {
|
||||
matches!(self, Self::Confirmed { .. })
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ObservedAs<ConfirmationTimeAnchor>> for ConfirmationTime {
|
||||
fn from(observed_as: ObservedAs<ConfirmationTimeAnchor>) -> Self {
|
||||
impl From<ChainPosition<ConfirmationTimeAnchor>> for ConfirmationTime {
|
||||
fn from(observed_as: ChainPosition<ConfirmationTimeAnchor>) -> Self {
|
||||
match observed_as {
|
||||
ObservedAs::Confirmed(a) => Self::Confirmed {
|
||||
ChainPosition::Confirmed(a) => Self::Confirmed {
|
||||
height: a.confirmation_height,
|
||||
time: a.confirmation_time,
|
||||
},
|
||||
ObservedAs::Unconfirmed(_) => Self::Unconfirmed { last_seen: 0 },
|
||||
ChainPosition::Unconfirmed(_) => Self::Unconfirmed { last_seen: 0 },
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -254,75 +170,32 @@ impl Anchor for ConfirmationTimeAnchor {
|
||||
}
|
||||
/// A `TxOut` with as much data as we can retrieve about it
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct FullTxOut<P> {
|
||||
pub struct FullTxOut<A> {
|
||||
/// The location of the `TxOut`.
|
||||
pub outpoint: OutPoint,
|
||||
/// The `TxOut`.
|
||||
pub txout: TxOut,
|
||||
/// The position of the transaction in `outpoint` in the overall chain.
|
||||
pub chain_position: P,
|
||||
pub chain_position: ChainPosition<A>,
|
||||
/// The txid and chain position of the transaction (if any) that has spent this output.
|
||||
pub spent_by: Option<(P, Txid)>,
|
||||
pub spent_by: Option<(ChainPosition<A>, Txid)>,
|
||||
/// Whether this output is on a coinbase transaction.
|
||||
pub is_on_coinbase: bool,
|
||||
}
|
||||
|
||||
impl<P: ChainPosition> FullTxOut<P> {
|
||||
/// Whether the utxo is/was/will be spendable at `height`.
|
||||
///
|
||||
/// It is spendable if it is not an immature coinbase output and no spending tx has been
|
||||
/// confirmed by that height.
|
||||
pub fn is_spendable_at(&self, height: u32) -> bool {
|
||||
if !self.is_mature(height) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if self.chain_position.height() > TxHeight::Confirmed(height) {
|
||||
return false;
|
||||
}
|
||||
|
||||
match &self.spent_by {
|
||||
Some((spending_height, _)) => spending_height.height() > TxHeight::Confirmed(height),
|
||||
None => true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_mature(&self, height: u32) -> bool {
|
||||
if self.is_on_coinbase {
|
||||
let tx_height = match self.chain_position.height() {
|
||||
TxHeight::Confirmed(tx_height) => tx_height,
|
||||
TxHeight::Unconfirmed => {
|
||||
debug_assert!(false, "coinbase tx can never be unconfirmed");
|
||||
return false;
|
||||
}
|
||||
};
|
||||
let age = height.saturating_sub(tx_height);
|
||||
if age + 1 < COINBASE_MATURITY {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Anchor> FullTxOut<ObservedAs<A>> {
|
||||
impl<A: Anchor> FullTxOut<A> {
|
||||
/// Whether the `txout` is considered mature.
|
||||
///
|
||||
/// This is the alternative version of [`is_mature`] which depends on `chain_position` 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_mature`]: Self::is_mature
|
||||
/// [`confirmation_height_upper_bound`]: Anchor::confirmation_height_upper_bound
|
||||
pub fn is_mature(&self, tip: u32) -> bool {
|
||||
if self.is_on_coinbase {
|
||||
let tx_height = match &self.chain_position {
|
||||
ObservedAs::Confirmed(anchor) => anchor.confirmation_height_upper_bound(),
|
||||
ObservedAs::Unconfirmed(_) => {
|
||||
ChainPosition::Confirmed(anchor) => anchor.confirmation_height_upper_bound(),
|
||||
ChainPosition::Unconfirmed(_) => {
|
||||
debug_assert!(false, "coinbase tx can never be unconfirmed");
|
||||
return false;
|
||||
}
|
||||
@@ -340,14 +213,10 @@ impl<A: Anchor> FullTxOut<ObservedAs<A>> {
|
||||
///
|
||||
/// This method does not take into account the locktime.
|
||||
///
|
||||
/// This is the alternative version of [`is_spendable_at`] which depends on `chain_position`
|
||||
/// 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
|
||||
/// [`confirmation_height_upper_bound`]: Anchor::confirmation_height_upper_bound
|
||||
pub fn is_confirmed_and_spendable(&self, tip: u32) -> bool {
|
||||
if !self.is_mature(tip) {
|
||||
@@ -355,15 +224,15 @@ impl<A: Anchor> FullTxOut<ObservedAs<A>> {
|
||||
}
|
||||
|
||||
let confirmation_height = match &self.chain_position {
|
||||
ObservedAs::Confirmed(anchor) => anchor.confirmation_height_upper_bound(),
|
||||
ObservedAs::Unconfirmed(_) => return false,
|
||||
ChainPosition::Confirmed(anchor) => anchor.confirmation_height_upper_bound(),
|
||||
ChainPosition::Unconfirmed(_) => return false,
|
||||
};
|
||||
if confirmation_height > tip {
|
||||
return false;
|
||||
}
|
||||
|
||||
// if the spending tx is confirmed within tip height, the txout is no longer spendable
|
||||
if let Some((ObservedAs::Confirmed(spending_anchor), _)) = &self.spent_by {
|
||||
if let Some((ChainPosition::Confirmed(spending_anchor), _)) = &self.spent_by {
|
||||
if spending_anchor.anchor_block().height <= tip {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,639 +0,0 @@
|
||||
//! Module for structures that combine the features of [`sparse_chain`] and [`tx_graph`].
|
||||
use crate::{
|
||||
collections::HashSet,
|
||||
sparse_chain::{self, ChainPosition, SparseChain},
|
||||
tx_graph::{self, TxGraph},
|
||||
Append, BlockId, ForEachTxOut, FullTxOut, TxHeight,
|
||||
};
|
||||
use alloc::{string::ToString, vec::Vec};
|
||||
use bitcoin::{OutPoint, Transaction, TxOut, Txid};
|
||||
use core::fmt::Debug;
|
||||
|
||||
/// A consistent combination of a [`SparseChain<P>`] and a [`TxGraph<T>`].
|
||||
///
|
||||
/// `SparseChain` only keeps track of transaction ids and their position in the chain, but you often
|
||||
/// want to store the full transactions as well. Additionally, you want to make sure that everything
|
||||
/// in the chain is consistent with the full transaction data. `ChainGraph` enforces these two
|
||||
/// invariants:
|
||||
///
|
||||
/// 1. Every transaction that is in the chain is also in the graph (you always have the full
|
||||
/// transaction).
|
||||
/// 2. No transactions in the chain conflict with each other, i.e., they don't double spend each
|
||||
/// other or have ancestors that double spend each other.
|
||||
///
|
||||
/// Note that the `ChainGraph` guarantees a 1:1 mapping between transactions in the `chain` and
|
||||
/// `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<P = TxHeight> {
|
||||
chain: SparseChain<P>,
|
||||
graph: TxGraph,
|
||||
}
|
||||
|
||||
impl<P> Default for ChainGraph<P> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
chain: Default::default(),
|
||||
graph: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> AsRef<SparseChain<P>> for ChainGraph<P> {
|
||||
fn as_ref(&self) -> &SparseChain<P> {
|
||||
&self.chain
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> AsRef<TxGraph> for ChainGraph<P> {
|
||||
fn as_ref(&self) -> &TxGraph {
|
||||
&self.graph
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> AsRef<ChainGraph<P>> for ChainGraph<P> {
|
||||
fn as_ref(&self) -> &ChainGraph<P> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> ChainGraph<P> {
|
||||
/// Returns a reference to the internal [`SparseChain`].
|
||||
pub fn chain(&self) -> &SparseChain<P> {
|
||||
&self.chain
|
||||
}
|
||||
|
||||
/// Returns a reference to the internal [`TxGraph`].
|
||||
pub fn graph(&self) -> &TxGraph {
|
||||
&self.graph
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> ChainGraph<P>
|
||||
where
|
||||
P: ChainPosition,
|
||||
{
|
||||
/// Create a new chain graph from a `chain` and a `graph`.
|
||||
///
|
||||
/// There are two reasons this can return an `Err`:
|
||||
///
|
||||
/// 1. There is a transaction in the `chain` that does not have its corresponding full
|
||||
/// 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<P>, graph: TxGraph) -> Result<Self, NewError<P>> {
|
||||
let mut missing = HashSet::default();
|
||||
for (pos, txid) in chain.txids() {
|
||||
if let Some(tx) = graph.get_tx(*txid) {
|
||||
let conflict = graph
|
||||
.walk_conflicts(tx, |_, txid| Some((chain.tx_position(txid)?.clone(), txid)))
|
||||
.next();
|
||||
if let Some((conflict_pos, conflict)) = conflict {
|
||||
return Err(NewError::Conflict {
|
||||
a: (pos.clone(), *txid),
|
||||
b: (conflict_pos, conflict),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
missing.insert(*txid);
|
||||
}
|
||||
}
|
||||
|
||||
if !missing.is_empty() {
|
||||
return Err(NewError::Missing(missing));
|
||||
}
|
||||
|
||||
Ok(Self { chain, graph })
|
||||
}
|
||||
|
||||
/// Take an update in the form of a [`SparseChain<P>`][`SparseChain`] and attempt to turn it
|
||||
/// into a chain graph by filling in full transactions from `self` and from `new_txs`. This
|
||||
/// returns a `ChainGraph<P, Cow<T>>` where the [`Cow<'a, T>`] will borrow the transaction if it
|
||||
/// got it from `self`.
|
||||
///
|
||||
/// This is useful when interacting with services like an electrum server which returns a list
|
||||
/// of txids and heights when calling [`script_get_history`], which can easily be inserted into a
|
||||
/// [`SparseChain<TxHeight>`][`SparseChain`]. From there, you need to figure out which full
|
||||
/// transactions you are missing in your chain graph and form `new_txs`. You then use
|
||||
/// `inflate_update` to turn this into an update `ChainGraph<P, Cow<Transaction>>` and finally
|
||||
/// use [`determine_changeset`] to generate the changeset from it.
|
||||
///
|
||||
/// [`SparseChain`]: crate::sparse_chain::SparseChain
|
||||
/// [`Cow<'a, T>`]: std::borrow::Cow
|
||||
/// [`script_get_history`]: https://docs.rs/electrum-client/latest/electrum_client/trait.ElectrumApi.html#tymethod.script_get_history
|
||||
/// [`determine_changeset`]: Self::determine_changeset
|
||||
pub fn inflate_update(
|
||||
&self,
|
||||
update: SparseChain<P>,
|
||||
new_txs: impl IntoIterator<Item = Transaction>,
|
||||
) -> Result<ChainGraph<P>, NewError<P>> {
|
||||
let mut inflated_chain = SparseChain::default();
|
||||
let mut inflated_graph = TxGraph::default();
|
||||
|
||||
for (height, hash) in update.checkpoints().clone().into_iter() {
|
||||
let _ = inflated_chain
|
||||
.insert_checkpoint(BlockId { height, hash })
|
||||
.expect("must insert");
|
||||
}
|
||||
|
||||
// [TODO] @evanlinjin: These need better comments
|
||||
// - copy transactions that have changed positions into the graph
|
||||
// - add new transactions to an inflated chain
|
||||
for (pos, txid) in update.txids() {
|
||||
match self.chain.tx_position(*txid) {
|
||||
Some(original_pos) => {
|
||||
if original_pos != pos {
|
||||
let tx = self
|
||||
.graph
|
||||
.get_tx(*txid)
|
||||
.expect("tx must exist as it is referenced in sparsechain")
|
||||
.clone();
|
||||
let _ = inflated_chain
|
||||
.insert_tx(*txid, pos.clone())
|
||||
.expect("must insert since this was already in update");
|
||||
let _ = inflated_graph.insert_tx(tx);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
let _ = inflated_chain
|
||||
.insert_tx(*txid, pos.clone())
|
||||
.expect("must insert since this was already in update");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for tx in new_txs {
|
||||
let _ = inflated_graph.insert_tx(tx);
|
||||
}
|
||||
|
||||
ChainGraph::new(inflated_chain, inflated_graph)
|
||||
}
|
||||
|
||||
/// Gets the checkpoint limit.
|
||||
///
|
||||
/// Refer to [`SparseChain::checkpoint_limit`] for more.
|
||||
pub fn checkpoint_limit(&self) -> Option<usize> {
|
||||
self.chain.checkpoint_limit()
|
||||
}
|
||||
|
||||
/// Sets the checkpoint limit.
|
||||
///
|
||||
/// Refer to [`SparseChain::set_checkpoint_limit`] for more.
|
||||
pub fn set_checkpoint_limit(&mut self, limit: Option<usize>) {
|
||||
self.chain.set_checkpoint_limit(limit)
|
||||
}
|
||||
|
||||
/// 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<P> {
|
||||
ChangeSet {
|
||||
chain: self.chain.invalidate_checkpoints_preview(from_height),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
/// Invalidate checkpoints `from_height` (inclusive) and above. Displaced transactions will be
|
||||
/// re-positioned to [`TxHeight::Unconfirmed`].
|
||||
///
|
||||
/// 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<P>
|
||||
where
|
||||
ChangeSet<P>: Clone,
|
||||
{
|
||||
let changeset = self.invalidate_checkpoints_preview(from_height);
|
||||
self.apply_changeset(changeset.clone());
|
||||
changeset
|
||||
}
|
||||
|
||||
/// Get a transaction currently in the underlying [`SparseChain`].
|
||||
///
|
||||
/// 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, &Transaction)> {
|
||||
let position = self.chain.tx_position(txid)?;
|
||||
let full_tx = self.graph.get_tx(txid).expect("must exist");
|
||||
Some((position, full_tx))
|
||||
}
|
||||
|
||||
/// Determines the changes required to insert a transaction into the inner [`ChainGraph`] and
|
||||
/// [`SparseChain`] at the given `position`.
|
||||
///
|
||||
/// If inserting it into the chain `position` will result in conflicts, the returned
|
||||
/// [`ChangeSet`] should evict conflicting transactions.
|
||||
pub fn insert_tx_preview(
|
||||
&self,
|
||||
tx: Transaction,
|
||||
pos: P,
|
||||
) -> Result<ChangeSet<P>, InsertTxError<P>> {
|
||||
let mut changeset = ChangeSet {
|
||||
chain: self.chain.insert_tx_preview(tx.txid(), pos)?,
|
||||
graph: self.graph.insert_tx_preview(tx),
|
||||
};
|
||||
self.fix_conflicts(&mut changeset)?;
|
||||
Ok(changeset)
|
||||
}
|
||||
|
||||
/// Inserts [`Transaction`] at the given chain position.
|
||||
///
|
||||
/// 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<ChangeSet<P>, InsertTxError<P>> {
|
||||
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<P> {
|
||||
ChangeSet {
|
||||
chain: Default::default(),
|
||||
graph: self.graph.insert_txout_preview(outpoint, txout),
|
||||
}
|
||||
}
|
||||
|
||||
/// Inserts a [`TxOut`] into the internal [`TxGraph`].
|
||||
///
|
||||
/// 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<P> {
|
||||
let changeset = self.insert_txout_preview(outpoint, txout);
|
||||
self.apply_changeset(changeset.clone());
|
||||
changeset
|
||||
}
|
||||
|
||||
/// Determines the changes required to insert a `block_id` (a height and block hash) into the
|
||||
/// chain.
|
||||
///
|
||||
/// If a checkpoint with a different hash already exists at that height, this will return an error.
|
||||
pub fn insert_checkpoint_preview(
|
||||
&self,
|
||||
block_id: BlockId,
|
||||
) -> Result<ChangeSet<P>, InsertCheckpointError> {
|
||||
self.chain
|
||||
.insert_checkpoint_preview(block_id)
|
||||
.map(|chain_changeset| ChangeSet {
|
||||
chain: chain_changeset,
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
/// Inserts checkpoint into [`Self`].
|
||||
///
|
||||
/// This is equivalent to calling [`Self::insert_checkpoint_preview`] and
|
||||
/// [`Self::apply_changeset`] in sequence.
|
||||
pub fn insert_checkpoint(
|
||||
&mut self,
|
||||
block_id: BlockId,
|
||||
) -> Result<ChangeSet<P>, InsertCheckpointError> {
|
||||
let changeset = self.insert_checkpoint_preview(block_id)?;
|
||||
self.apply_changeset(changeset.clone());
|
||||
Ok(changeset)
|
||||
}
|
||||
|
||||
/// Calculates the difference between self and `update` in the form of a [`ChangeSet`].
|
||||
pub fn determine_changeset(
|
||||
&self,
|
||||
update: &ChainGraph<P>,
|
||||
) -> Result<ChangeSet<P>, UpdateError<P>> {
|
||||
let chain_changeset = self
|
||||
.chain
|
||||
.determine_changeset(&update.chain)
|
||||
.map_err(UpdateError::Chain)?;
|
||||
|
||||
let mut changeset = ChangeSet {
|
||||
chain: chain_changeset,
|
||||
graph: self.graph.determine_additions(&update.graph),
|
||||
};
|
||||
|
||||
self.fix_conflicts(&mut changeset)?;
|
||||
Ok(changeset)
|
||||
}
|
||||
|
||||
/// Given a transaction, return an iterator of `txid`s that conflict with it (spends at least
|
||||
/// one of the same inputs). This iterator includes all descendants of conflicting transactions.
|
||||
///
|
||||
/// This method only returns conflicts that exist in the [`SparseChain`] as transactions that
|
||||
/// are not included in [`SparseChain`] are already considered as evicted.
|
||||
pub fn tx_conflicts_in_chain<'a>(
|
||||
&'a self,
|
||||
tx: &'a Transaction,
|
||||
) -> impl Iterator<Item = (&'a P, Txid)> + 'a {
|
||||
self.graph.walk_conflicts(tx, move |_, conflict_txid| {
|
||||
self.chain
|
||||
.tx_position(conflict_txid)
|
||||
.map(|conflict_pos| (conflict_pos, conflict_txid))
|
||||
})
|
||||
}
|
||||
|
||||
/// Fix changeset conflicts.
|
||||
///
|
||||
/// **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<P>) -> Result<(), UnresolvableConflict<P>> {
|
||||
let mut chain_conflicts = vec![];
|
||||
|
||||
for (&txid, pos_change) in &changeset.chain.txids {
|
||||
let pos = match pos_change {
|
||||
Some(pos) => {
|
||||
// Ignore txs that are still in the chain -- we only care about new ones
|
||||
if self.chain.tx_position(txid).is_some() {
|
||||
continue;
|
||||
}
|
||||
pos
|
||||
}
|
||||
// Ignore txids that are being deleted by the change (they can't conflict)
|
||||
None => continue,
|
||||
};
|
||||
|
||||
let mut full_tx = self.graph.get_tx(txid);
|
||||
|
||||
if full_tx.is_none() {
|
||||
full_tx = changeset.graph.tx.iter().find(|tx| tx.txid() == txid)
|
||||
}
|
||||
|
||||
debug_assert!(full_tx.is_some(), "should have full tx at this point");
|
||||
|
||||
let full_tx = match full_tx {
|
||||
Some(full_tx) => full_tx,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
for (conflict_pos, conflict_txid) in self.tx_conflicts_in_chain(full_tx) {
|
||||
chain_conflicts.push((pos.clone(), txid, conflict_pos, conflict_txid))
|
||||
}
|
||||
}
|
||||
|
||||
for (update_pos, update_txid, conflicting_pos, conflicting_txid) in chain_conflicts {
|
||||
// We have found a tx that conflicts with our update txid. Only allow this when the
|
||||
// conflicting tx will be positioned as "unconfirmed" after the update is applied.
|
||||
// If so, we will modify the changeset to evict the conflicting txid.
|
||||
|
||||
// determine the position of the conflicting txid after the current changeset is applied
|
||||
let conflicting_new_pos = changeset
|
||||
.chain
|
||||
.txids
|
||||
.get(&conflicting_txid)
|
||||
.map(Option::as_ref)
|
||||
.unwrap_or(Some(conflicting_pos));
|
||||
|
||||
match conflicting_new_pos {
|
||||
None => {
|
||||
// conflicting txid will be deleted, can ignore
|
||||
}
|
||||
Some(existing_new_pos) => match existing_new_pos.height() {
|
||||
TxHeight::Confirmed(_) => {
|
||||
// the new position of the conflicting tx is "confirmed", therefore cannot be
|
||||
// evicted, return error
|
||||
return Err(UnresolvableConflict {
|
||||
already_confirmed_tx: (conflicting_pos.clone(), conflicting_txid),
|
||||
update_tx: (update_pos, update_txid),
|
||||
});
|
||||
}
|
||||
TxHeight::Unconfirmed => {
|
||||
// the new position of the conflicting tx is "unconfirmed", therefore it can
|
||||
// be evicted
|
||||
changeset.chain.txids.insert(conflicting_txid, None);
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Applies `changeset` to `self`.
|
||||
///
|
||||
/// **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<P>) {
|
||||
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<P>) -> Result<ChangeSet<P>, UpdateError<P>> {
|
||||
let changeset = self.determine_changeset(&update)?;
|
||||
self.apply_changeset(changeset.clone());
|
||||
Ok(changeset)
|
||||
}
|
||||
|
||||
/// Get the full transaction output at an outpoint if it exists in the chain and the graph.
|
||||
pub fn full_txout(&self, outpoint: OutPoint) -> Option<FullTxOut<P>> {
|
||||
self.chain.full_txout(&self.graph, outpoint)
|
||||
}
|
||||
|
||||
/// Iterate over the full transactions and their position in the chain ordered by their position
|
||||
/// in ascending order.
|
||||
pub fn transactions_in_chain(&self) -> impl DoubleEndedIterator<Item = (&P, &Transaction)> {
|
||||
self.chain
|
||||
.txids()
|
||||
.map(move |(pos, txid)| (pos, self.graph.get_tx(*txid).expect("must exist")))
|
||||
}
|
||||
|
||||
/// Find the transaction in the chain that spends `outpoint`.
|
||||
///
|
||||
/// This uses the input/output relationships in the internal `graph`. Note that the transaction
|
||||
/// which includes `outpoint` does not need to be in the `graph` or the `chain` for this to
|
||||
/// return `Some(_)`.
|
||||
pub fn spent_by(&self, outpoint: OutPoint) -> Option<(&P, Txid)> {
|
||||
self.chain.spent_by(&self.graph, outpoint)
|
||||
}
|
||||
|
||||
/// Whether the chain graph contains any data whatsoever.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.chain.is_empty() && self.graph.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents changes to [`ChainGraph`].
|
||||
///
|
||||
/// This is essentially a combination of [`sparse_chain::ChangeSet`] and [`tx_graph::Additions`].
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
#[cfg_attr(
|
||||
feature = "serde",
|
||||
derive(serde::Deserialize, serde::Serialize),
|
||||
serde(
|
||||
crate = "serde_crate",
|
||||
bound(
|
||||
deserialize = "P: serde::Deserialize<'de>",
|
||||
serialize = "P: serde::Serialize"
|
||||
)
|
||||
)
|
||||
)]
|
||||
#[must_use]
|
||||
pub struct ChangeSet<P> {
|
||||
pub chain: sparse_chain::ChangeSet<P>,
|
||||
pub graph: tx_graph::Additions,
|
||||
}
|
||||
|
||||
impl<P> ChangeSet<P> {
|
||||
/// Returns `true` if this [`ChangeSet`] records no changes.
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.chain.is_empty() && self.graph.is_empty()
|
||||
}
|
||||
|
||||
/// Returns `true` if this [`ChangeSet`] contains transaction evictions.
|
||||
pub fn contains_eviction(&self) -> bool {
|
||||
self.chain
|
||||
.txids
|
||||
.iter()
|
||||
.any(|(_, new_pos)| new_pos.is_none())
|
||||
}
|
||||
|
||||
/// 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<P>)
|
||||
where
|
||||
P: ChainPosition,
|
||||
{
|
||||
self.chain.append(other.chain);
|
||||
self.graph.append(other.graph);
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> Default for ChangeSet<P> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
chain: Default::default(),
|
||||
graph: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> ForEachTxOut for ChainGraph<P> {
|
||||
fn for_each_txout(&self, f: impl FnMut((OutPoint, &TxOut))) {
|
||||
self.graph.for_each_txout(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> ForEachTxOut for ChangeSet<P> {
|
||||
fn for_each_txout(&self, f: impl FnMut((OutPoint, &TxOut))) {
|
||||
self.graph.for_each_txout(f)
|
||||
}
|
||||
}
|
||||
|
||||
/// Error that may occur when calling [`ChainGraph::new`].
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum NewError<P> {
|
||||
/// Two transactions within the sparse chain conflicted with each other
|
||||
Conflict { a: (P, Txid), b: (P, Txid) },
|
||||
/// One or more transactions in the chain were not in the graph
|
||||
Missing(HashSet<Txid>),
|
||||
}
|
||||
|
||||
impl<P: core::fmt::Debug> core::fmt::Display for NewError<P> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
match self {
|
||||
NewError::Conflict { a, b } => write!(
|
||||
f,
|
||||
"Unable to inflate sparse chain to chain graph since transactions {:?} and {:?}",
|
||||
a, b
|
||||
),
|
||||
NewError::Missing(missing) => write!(
|
||||
f,
|
||||
"missing full transactions for {}",
|
||||
missing
|
||||
.iter()
|
||||
.map(|txid| txid.to_string())
|
||||
.collect::<Vec<_>>()
|
||||
.join(", ")
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<P: core::fmt::Debug> std::error::Error for NewError<P> {}
|
||||
|
||||
/// Error that may occur when inserting a transaction.
|
||||
///
|
||||
/// Refer to [`ChainGraph::insert_tx_preview`] and [`ChainGraph::insert_tx`].
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum InsertTxError<P> {
|
||||
Chain(sparse_chain::InsertTxError<P>),
|
||||
UnresolvableConflict(UnresolvableConflict<P>),
|
||||
}
|
||||
|
||||
impl<P: core::fmt::Debug> core::fmt::Display for InsertTxError<P> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
match self {
|
||||
InsertTxError::Chain(inner) => core::fmt::Display::fmt(inner, f),
|
||||
InsertTxError::UnresolvableConflict(inner) => core::fmt::Display::fmt(inner, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> From<sparse_chain::InsertTxError<P>> for InsertTxError<P> {
|
||||
fn from(inner: sparse_chain::InsertTxError<P>) -> Self {
|
||||
Self::Chain(inner)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<P: core::fmt::Debug> std::error::Error for InsertTxError<P> {}
|
||||
|
||||
/// A nice alias of [`sparse_chain::InsertCheckpointError`].
|
||||
pub type InsertCheckpointError = sparse_chain::InsertCheckpointError;
|
||||
|
||||
/// Represents an update failure.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum UpdateError<P> {
|
||||
/// The update chain was inconsistent with the existing chain
|
||||
Chain(sparse_chain::UpdateError<P>),
|
||||
/// A transaction in the update spent the same input as an already confirmed transaction
|
||||
UnresolvableConflict(UnresolvableConflict<P>),
|
||||
}
|
||||
|
||||
impl<P: core::fmt::Debug> core::fmt::Display for UpdateError<P> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
match self {
|
||||
UpdateError::Chain(inner) => core::fmt::Display::fmt(inner, f),
|
||||
UpdateError::UnresolvableConflict(inner) => core::fmt::Display::fmt(inner, f),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> From<sparse_chain::UpdateError<P>> for UpdateError<P> {
|
||||
fn from(inner: sparse_chain::UpdateError<P>) -> Self {
|
||||
Self::Chain(inner)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<P: core::fmt::Debug> std::error::Error for UpdateError<P> {}
|
||||
|
||||
/// Represents an unresolvable conflict between an update's transaction and an
|
||||
/// already-confirmed transaction.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct UnresolvableConflict<P> {
|
||||
pub already_confirmed_tx: (P, Txid),
|
||||
pub update_tx: (P, Txid),
|
||||
}
|
||||
|
||||
impl<P: core::fmt::Debug> core::fmt::Display for UnresolvableConflict<P> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
let Self {
|
||||
already_confirmed_tx,
|
||||
update_tx,
|
||||
} = self;
|
||||
write!(f, "update transaction {} at height {:?} conflicts with an already confirmed transaction {} at height {:?}",
|
||||
update_tx.1, update_tx.0, already_confirmed_tx.1, already_confirmed_tx.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> From<UnresolvableConflict<P>> for UpdateError<P> {
|
||||
fn from(inner: UnresolvableConflict<P>) -> Self {
|
||||
Self::UnresolvableConflict(inner)
|
||||
}
|
||||
}
|
||||
|
||||
impl<P> From<UnresolvableConflict<P>> for InsertTxError<P> {
|
||||
fn from(inner: UnresolvableConflict<P>) -> Self {
|
||||
Self::UnresolvableConflict(inner)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<P: core::fmt::Debug> std::error::Error for UnresolvableConflict<P> {}
|
||||
@@ -8,31 +8,16 @@
|
||||
//! has a `txout` containing an indexed script pubkey). Internally, this uses [`SpkTxOutIndex`], but
|
||||
//! also maintains "revealed" and "lookahead" index counts per keychain.
|
||||
//!
|
||||
//! [`KeychainTracker`] combines [`ChainGraph`] and [`KeychainTxOutIndex`] and enforces atomic
|
||||
//! changes between both these structures. [`KeychainScan`] is a structure used to update to
|
||||
//! [`KeychainTracker`] and changes made on a [`KeychainTracker`] are reported by
|
||||
//! [`KeychainChangeSet`]s.
|
||||
//!
|
||||
//! [`SpkTxOutIndex`]: crate::SpkTxOutIndex
|
||||
|
||||
use crate::{
|
||||
chain_graph::{self, ChainGraph},
|
||||
collections::BTreeMap,
|
||||
indexed_tx_graph::IndexedAdditions,
|
||||
local_chain::{self, LocalChain},
|
||||
sparse_chain::ChainPosition,
|
||||
tx_graph::TxGraph,
|
||||
Anchor, Append, ForEachTxOut,
|
||||
Anchor, Append,
|
||||
};
|
||||
|
||||
#[cfg(feature = "miniscript")]
|
||||
pub mod persist;
|
||||
#[cfg(feature = "miniscript")]
|
||||
pub use persist::*;
|
||||
#[cfg(feature = "miniscript")]
|
||||
mod tracker;
|
||||
#[cfg(feature = "miniscript")]
|
||||
pub use tracker::*;
|
||||
#[cfg(feature = "miniscript")]
|
||||
mod txout_index;
|
||||
#[cfg(feature = "miniscript")]
|
||||
@@ -187,116 +172,6 @@ impl<K, A> From<IndexedAdditions<A, DerivationAdditions<K>>> for LocalChangeSet<
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
/// An update that includes the last active indexes of each keychain.
|
||||
pub struct KeychainScan<K, P> {
|
||||
/// The update data in the form of a chain that could be applied
|
||||
pub update: ChainGraph<P>,
|
||||
/// The last active indexes of each keychain
|
||||
pub last_active_indices: BTreeMap<K, u32>,
|
||||
}
|
||||
|
||||
impl<K, P> Default for KeychainScan<K, P> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
update: Default::default(),
|
||||
last_active_indices: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, P> From<ChainGraph<P>> for KeychainScan<K, P> {
|
||||
fn from(update: ChainGraph<P>) -> Self {
|
||||
KeychainScan {
|
||||
update,
|
||||
last_active_indices: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents changes to a [`KeychainTracker`].
|
||||
///
|
||||
/// This is essentially a combination of [`DerivationAdditions`] and [`chain_graph::ChangeSet`].
|
||||
#[derive(Clone, Debug)]
|
||||
#[cfg_attr(
|
||||
feature = "serde",
|
||||
derive(serde::Deserialize, serde::Serialize),
|
||||
serde(
|
||||
crate = "serde_crate",
|
||||
bound(
|
||||
deserialize = "K: Ord + serde::Deserialize<'de>, P: serde::Deserialize<'de>",
|
||||
serialize = "K: Ord + serde::Serialize, P: serde::Serialize"
|
||||
)
|
||||
)
|
||||
)]
|
||||
#[must_use]
|
||||
pub struct KeychainChangeSet<K, P> {
|
||||
/// The changes in local keychain derivation indices
|
||||
pub derivation_indices: DerivationAdditions<K>,
|
||||
/// The changes that have occurred in the blockchain
|
||||
pub chain_graph: chain_graph::ChangeSet<P>,
|
||||
}
|
||||
|
||||
impl<K, P> Default for KeychainChangeSet<K, P> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
chain_graph: Default::default(),
|
||||
derivation_indices: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, P> KeychainChangeSet<K, P> {
|
||||
/// 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()
|
||||
}
|
||||
|
||||
/// Appends the changes in `other` into `self` such that applying `self` afterward has the same
|
||||
/// effect as sequentially applying the original `self` and `other`.
|
||||
///
|
||||
/// 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<K, P>)
|
||||
where
|
||||
K: Ord,
|
||||
P: ChainPosition,
|
||||
{
|
||||
self.derivation_indices.append(other.derivation_indices);
|
||||
self.chain_graph.append(other.chain_graph);
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, P> From<chain_graph::ChangeSet<P>> for KeychainChangeSet<K, P> {
|
||||
fn from(changeset: chain_graph::ChangeSet<P>) -> Self {
|
||||
Self {
|
||||
chain_graph: changeset,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, P> From<DerivationAdditions<K>> for KeychainChangeSet<K, P> {
|
||||
fn from(additions: DerivationAdditions<K>) -> Self {
|
||||
Self {
|
||||
derivation_indices: additions,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, P> AsRef<TxGraph> for KeychainScan<K, P> {
|
||||
fn as_ref(&self) -> &TxGraph {
|
||||
self.update.graph()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, P> ForEachTxOut for KeychainChangeSet<K, P> {
|
||||
fn for_each_txout(&self, f: impl FnMut((bitcoin::OutPoint, &bitcoin::TxOut))) {
|
||||
self.chain_graph.for_each_txout(f)
|
||||
}
|
||||
}
|
||||
|
||||
/// Balance, differentiated into various categories.
|
||||
#[derive(Debug, PartialEq, Eq, Clone, Default)]
|
||||
#[cfg_attr(
|
||||
@@ -355,9 +230,8 @@ impl core::ops::Add for Balance {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::TxHeight;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn append_keychain_derivation_indices() {
|
||||
#[derive(Ord, PartialOrd, Eq, PartialEq, Clone, Debug)]
|
||||
@@ -375,25 +249,18 @@ mod test {
|
||||
rhs_di.insert(Keychain::Two, 5);
|
||||
lhs_di.insert(Keychain::Three, 3);
|
||||
rhs_di.insert(Keychain::Four, 4);
|
||||
let mut lhs = KeychainChangeSet {
|
||||
derivation_indices: DerivationAdditions(lhs_di),
|
||||
chain_graph: chain_graph::ChangeSet::<TxHeight>::default(),
|
||||
};
|
||||
|
||||
let rhs = KeychainChangeSet {
|
||||
derivation_indices: DerivationAdditions(rhs_di),
|
||||
chain_graph: chain_graph::ChangeSet::<TxHeight>::default(),
|
||||
};
|
||||
|
||||
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.derivation_indices.0.get(&Keychain::One), Some(&7));
|
||||
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.derivation_indices.0.get(&Keychain::Two), Some(&5));
|
||||
assert_eq!(lhs.0.get(&Keychain::Two), Some(&5));
|
||||
// Existing index is unchanged if keychain doesn't exist in `other`.
|
||||
assert_eq!(lhs.derivation_indices.0.get(&Keychain::Three), Some(&3));
|
||||
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.derivation_indices.0.get(&Keychain::Four), Some(&4));
|
||||
assert_eq!(lhs.0.get(&Keychain::Four), Some(&4));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
//! Persistence for changes made to a [`KeychainTracker`].
|
||||
//!
|
||||
//! BDK's [`KeychainTracker`] needs somewhere to persist changes it makes during operation.
|
||||
//! Operations like giving out a new address are crucial to persist so that next time the
|
||||
//! application is loaded, it can find transactions related to that address.
|
||||
//!
|
||||
//! Note that the [`KeychainTracker`] does not read this persisted data during operation since it
|
||||
//! always has a copy in memory.
|
||||
//!
|
||||
//! [`KeychainTracker`]: crate::keychain::KeychainTracker
|
||||
|
||||
use crate::{keychain, sparse_chain::ChainPosition};
|
||||
|
||||
/// `Persist` wraps a [`PersistBackend`] to create a convenient staging area for changes before they
|
||||
/// are persisted. Not all changes made to the [`KeychainTracker`] need to be written to disk right
|
||||
/// away so you can use [`Persist::stage`] to *stage* it first and then [`Persist::commit`] to
|
||||
/// finally, write it to disk.
|
||||
///
|
||||
/// [`KeychainTracker`]: keychain::KeychainTracker
|
||||
#[derive(Debug)]
|
||||
pub struct Persist<K, P, B> {
|
||||
backend: B,
|
||||
stage: keychain::KeychainChangeSet<K, P>,
|
||||
}
|
||||
|
||||
impl<K, P, B> Persist<K, P, B> {
|
||||
/// Create a new `Persist` from a [`PersistBackend`].
|
||||
pub fn new(backend: B) -> Self {
|
||||
Self {
|
||||
backend,
|
||||
stage: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Stage a `changeset` to later persistence with [`commit`].
|
||||
///
|
||||
/// [`commit`]: Self::commit
|
||||
pub fn stage(&mut self, changeset: keychain::KeychainChangeSet<K, P>)
|
||||
where
|
||||
K: Ord,
|
||||
P: ChainPosition,
|
||||
{
|
||||
self.stage.append(changeset)
|
||||
}
|
||||
|
||||
/// Get the changes that haven't been committed yet
|
||||
pub fn staged(&self) -> &keychain::KeychainChangeSet<K, P> {
|
||||
&self.stage
|
||||
}
|
||||
|
||||
/// Commit the staged changes to the underlying persistence backend.
|
||||
///
|
||||
/// Returns a backend-defined error if this fails.
|
||||
pub fn commit(&mut self) -> Result<(), B::WriteError>
|
||||
where
|
||||
B: PersistBackend<K, P>,
|
||||
{
|
||||
self.backend.append_changeset(&self.stage)?;
|
||||
self.stage = Default::default();
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// A persistence backend for [`Persist`].
|
||||
pub trait PersistBackend<K, P> {
|
||||
/// The error the backend returns when it fails to write.
|
||||
type WriteError: core::fmt::Debug;
|
||||
|
||||
/// The error the backend returns when it fails to load.
|
||||
type LoadError: core::fmt::Debug;
|
||||
|
||||
/// Appends a new changeset to the persistent backend.
|
||||
///
|
||||
/// It is up to the backend what it does with this. It could store every changeset in a list or
|
||||
/// it inserts the actual changes into a more structured database. All it needs to guarantee is
|
||||
/// that [`load_into_keychain_tracker`] restores a keychain tracker to what it should be if all
|
||||
/// changesets had been applied sequentially.
|
||||
///
|
||||
/// [`load_into_keychain_tracker`]: Self::load_into_keychain_tracker
|
||||
fn append_changeset(
|
||||
&mut self,
|
||||
changeset: &keychain::KeychainChangeSet<K, P>,
|
||||
) -> Result<(), Self::WriteError>;
|
||||
|
||||
/// Applies all the changesets the backend has received to `tracker`.
|
||||
fn load_into_keychain_tracker(
|
||||
&mut self,
|
||||
tracker: &mut keychain::KeychainTracker<K, P>,
|
||||
) -> Result<(), Self::LoadError>;
|
||||
}
|
||||
|
||||
impl<K, P> PersistBackend<K, P> for () {
|
||||
type WriteError = ();
|
||||
type LoadError = ();
|
||||
|
||||
fn append_changeset(
|
||||
&mut self,
|
||||
_changeset: &keychain::KeychainChangeSet<K, P>,
|
||||
) -> Result<(), Self::WriteError> {
|
||||
Ok(())
|
||||
}
|
||||
fn load_into_keychain_tracker(
|
||||
&mut self,
|
||||
_tracker: &mut keychain::KeychainTracker<K, P>,
|
||||
) -> Result<(), Self::LoadError> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,308 +0,0 @@
|
||||
use bitcoin::Transaction;
|
||||
use miniscript::{Descriptor, DescriptorPublicKey};
|
||||
|
||||
use crate::{
|
||||
chain_graph::{self, ChainGraph},
|
||||
collections::*,
|
||||
keychain::{KeychainChangeSet, KeychainScan, KeychainTxOutIndex},
|
||||
sparse_chain::{self, SparseChain},
|
||||
tx_graph::TxGraph,
|
||||
BlockId, FullTxOut, TxHeight,
|
||||
};
|
||||
|
||||
use super::{Balance, DerivationAdditions};
|
||||
|
||||
/// A convenient combination of a [`KeychainTxOutIndex`] and a [`ChainGraph`].
|
||||
///
|
||||
/// The [`KeychainTracker`] atomically updates its [`KeychainTxOutIndex`] whenever new chain data is
|
||||
/// incorporated into its internal [`ChainGraph`].
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct KeychainTracker<K, P> {
|
||||
/// Index between script pubkeys to transaction outputs
|
||||
pub txout_index: KeychainTxOutIndex<K>,
|
||||
chain_graph: ChainGraph<P>,
|
||||
}
|
||||
|
||||
impl<K, P> KeychainTracker<K, P>
|
||||
where
|
||||
P: sparse_chain::ChainPosition,
|
||||
K: Ord + Clone + core::fmt::Debug,
|
||||
{
|
||||
/// Add a keychain to the tracker's `txout_index` with a descriptor to derive addresses.
|
||||
/// This is just shorthand for calling [`KeychainTxOutIndex::add_keychain`] on the internal
|
||||
/// `txout_index`.
|
||||
///
|
||||
/// Adding a keychain means you will be able to derive new script pubkeys under that keychain
|
||||
/// and the tracker will discover transaction outputs with those script pubkeys.
|
||||
pub fn add_keychain(&mut self, keychain: K, descriptor: Descriptor<DescriptorPublicKey>) {
|
||||
self.txout_index.add_keychain(keychain, descriptor)
|
||||
}
|
||||
|
||||
/// Get the internal map of keychains to their descriptors. This is just shorthand for calling
|
||||
/// [`KeychainTxOutIndex::keychains`] on the internal `txout_index`.
|
||||
pub fn keychains(&mut self) -> &BTreeMap<K, Descriptor<DescriptorPublicKey>> {
|
||||
self.txout_index.keychains()
|
||||
}
|
||||
|
||||
/// Get the checkpoint limit of the internal [`SparseChain`].
|
||||
///
|
||||
/// Refer to [`SparseChain::checkpoint_limit`] for more.
|
||||
pub fn checkpoint_limit(&self) -> Option<usize> {
|
||||
self.chain_graph.checkpoint_limit()
|
||||
}
|
||||
|
||||
/// Set the checkpoint limit of the internal [`SparseChain`].
|
||||
///
|
||||
/// Refer to [`SparseChain::set_checkpoint_limit`] for more.
|
||||
pub fn set_checkpoint_limit(&mut self, limit: Option<usize>) {
|
||||
self.chain_graph.set_checkpoint_limit(limit)
|
||||
}
|
||||
|
||||
/// Determines the resultant [`KeychainChangeSet`] if the given [`KeychainScan`] is applied.
|
||||
///
|
||||
/// Internally, we call [`ChainGraph::determine_changeset`] and also determine the additions of
|
||||
/// [`KeychainTxOutIndex`].
|
||||
pub fn determine_changeset(
|
||||
&self,
|
||||
scan: &KeychainScan<K, P>,
|
||||
) -> Result<KeychainChangeSet<K, P>, chain_graph::UpdateError<P>> {
|
||||
// TODO: `KeychainTxOutIndex::determine_additions`
|
||||
let mut derivation_indices = scan.last_active_indices.clone();
|
||||
derivation_indices.retain(|keychain, index| {
|
||||
match self.txout_index.last_revealed_index(keychain) {
|
||||
Some(existing) => *index > existing,
|
||||
None => true,
|
||||
}
|
||||
});
|
||||
|
||||
Ok(KeychainChangeSet {
|
||||
derivation_indices: DerivationAdditions(derivation_indices),
|
||||
chain_graph: self.chain_graph.determine_changeset(&scan.update)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Directly applies a [`KeychainScan`] on [`KeychainTracker`].
|
||||
///
|
||||
/// This is equivalent to calling [`determine_changeset`] and [`apply_changeset`] in sequence.
|
||||
///
|
||||
/// [`determine_changeset`]: Self::determine_changeset
|
||||
/// [`apply_changeset`]: Self::apply_changeset
|
||||
pub fn apply_update(
|
||||
&mut self,
|
||||
scan: KeychainScan<K, P>,
|
||||
) -> Result<KeychainChangeSet<K, P>, chain_graph::UpdateError<P>> {
|
||||
let changeset = self.determine_changeset(&scan)?;
|
||||
self.apply_changeset(changeset.clone());
|
||||
Ok(changeset)
|
||||
}
|
||||
|
||||
/// Applies the changes in `changeset` to [`KeychainTracker`].
|
||||
///
|
||||
/// Internally, this calls [`KeychainTxOutIndex::apply_additions`] and
|
||||
/// [`ChainGraph::apply_changeset`] in sequence.
|
||||
pub fn apply_changeset(&mut self, changeset: KeychainChangeSet<K, P>) {
|
||||
let KeychainChangeSet {
|
||||
derivation_indices,
|
||||
chain_graph,
|
||||
} = changeset;
|
||||
self.txout_index.apply_additions(derivation_indices);
|
||||
let _ = self.txout_index.scan(&chain_graph);
|
||||
self.chain_graph.apply_changeset(chain_graph)
|
||||
}
|
||||
|
||||
/// Iterates through [`FullTxOut`]s that are considered to exist in our representation of the
|
||||
/// blockchain/mempool.
|
||||
///
|
||||
/// In other words, these are `txout`s of confirmed and in-mempool transactions, based on our
|
||||
/// view of the blockchain/mempool.
|
||||
pub fn full_txouts(&self) -> impl Iterator<Item = (&(K, u32), FullTxOut<P>)> + '_ {
|
||||
self.txout_index
|
||||
.txouts()
|
||||
.filter_map(move |(spk_i, op, _)| Some((spk_i, self.chain_graph.full_txout(op)?)))
|
||||
}
|
||||
|
||||
/// Iterates through [`FullTxOut`]s that are unspent outputs.
|
||||
///
|
||||
/// Refer to [`full_txouts`] for more.
|
||||
///
|
||||
/// [`full_txouts`]: Self::full_txouts
|
||||
pub fn full_utxos(&self) -> impl Iterator<Item = (&(K, u32), FullTxOut<P>)> + '_ {
|
||||
self.full_txouts()
|
||||
.filter(|(_, txout)| txout.spent_by.is_none())
|
||||
}
|
||||
|
||||
/// Returns a reference to the internal [`ChainGraph`].
|
||||
pub fn chain_graph(&self) -> &ChainGraph<P> {
|
||||
&self.chain_graph
|
||||
}
|
||||
|
||||
/// Returns a reference to the internal [`TxGraph`] (which is part of the [`ChainGraph`]).
|
||||
pub fn graph(&self) -> &TxGraph {
|
||||
self.chain_graph().graph()
|
||||
}
|
||||
|
||||
/// Returns a reference to the internal [`SparseChain`] (which is part of the [`ChainGraph`]).
|
||||
pub fn chain(&self) -> &SparseChain<P> {
|
||||
self.chain_graph().chain()
|
||||
}
|
||||
|
||||
/// Determines the changes as a result of inserting `block_id` (a height and block hash) into the
|
||||
/// tracker.
|
||||
///
|
||||
/// The caller is responsible for guaranteeing that a block exists at that height. If a
|
||||
/// checkpoint already exists at that height with a different hash; this will return an error.
|
||||
/// Otherwise it will return `Ok(true)` if the checkpoint didn't already exist or `Ok(false)`
|
||||
/// if it did.
|
||||
///
|
||||
/// **Warning**: This function modifies the internal state of the tracker. You are responsible
|
||||
/// for persisting these changes to disk if you need to restore them.
|
||||
pub fn insert_checkpoint_preview(
|
||||
&self,
|
||||
block_id: BlockId,
|
||||
) -> Result<KeychainChangeSet<K, P>, chain_graph::InsertCheckpointError> {
|
||||
Ok(KeychainChangeSet {
|
||||
chain_graph: self.chain_graph.insert_checkpoint_preview(block_id)?,
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
/// Directly insert a `block_id` into the tracker.
|
||||
///
|
||||
/// This is equivalent of calling [`insert_checkpoint_preview`] and [`apply_changeset`] in
|
||||
/// sequence.
|
||||
///
|
||||
/// [`insert_checkpoint_preview`]: Self::insert_checkpoint_preview
|
||||
/// [`apply_changeset`]: Self::apply_changeset
|
||||
pub fn insert_checkpoint(
|
||||
&mut self,
|
||||
block_id: BlockId,
|
||||
) -> Result<KeychainChangeSet<K, P>, chain_graph::InsertCheckpointError> {
|
||||
let changeset = self.insert_checkpoint_preview(block_id)?;
|
||||
self.apply_changeset(changeset.clone());
|
||||
Ok(changeset)
|
||||
}
|
||||
|
||||
/// Determines the changes as a result of inserting a transaction into the inner [`ChainGraph`]
|
||||
/// and optionally into the inner chain at `position`.
|
||||
///
|
||||
/// **Warning**: This function modifies the internal state of the chain graph. You are
|
||||
/// responsible for persisting these changes to disk if you need to restore them.
|
||||
pub fn insert_tx_preview(
|
||||
&self,
|
||||
tx: Transaction,
|
||||
pos: P,
|
||||
) -> Result<KeychainChangeSet<K, P>, chain_graph::InsertTxError<P>> {
|
||||
Ok(KeychainChangeSet {
|
||||
chain_graph: self.chain_graph.insert_tx_preview(tx, pos)?,
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
|
||||
/// Directly insert a transaction into the inner [`ChainGraph`] and optionally into the inner
|
||||
/// chain at `position`.
|
||||
///
|
||||
/// This is equivalent of calling [`insert_tx_preview`] and [`apply_changeset`] in sequence.
|
||||
///
|
||||
/// [`insert_tx_preview`]: Self::insert_tx_preview
|
||||
/// [`apply_changeset`]: Self::apply_changeset
|
||||
pub fn insert_tx(
|
||||
&mut self,
|
||||
tx: Transaction,
|
||||
pos: P,
|
||||
) -> Result<KeychainChangeSet<K, P>, chain_graph::InsertTxError<P>> {
|
||||
let changeset = self.insert_tx_preview(tx, pos)?;
|
||||
self.apply_changeset(changeset.clone());
|
||||
Ok(changeset)
|
||||
}
|
||||
|
||||
/// Returns the *balance* of the keychain, i.e., the value of unspent transaction outputs tracked.
|
||||
///
|
||||
/// The caller provides a `should_trust` predicate which must decide whether the value of
|
||||
/// unconfirmed outputs on this keychain are guaranteed to be realized or not. For example:
|
||||
///
|
||||
/// - For an *internal* (change) keychain, `should_trust` should generally be `true` since even if
|
||||
/// you lose an internal output due to eviction, you will always gain back the value from whatever output the
|
||||
/// unconfirmed transaction was spending (since that output is presumably from your wallet).
|
||||
/// - For an *external* keychain, you might want `should_trust` to return `false` since someone may cancel (by double spending)
|
||||
/// a payment made to addresses on that keychain.
|
||||
///
|
||||
/// When in doubt set `should_trust` to return false. This doesn't do anything other than change
|
||||
/// where the unconfirmed output's value is accounted for in `Balance`.
|
||||
pub fn balance(&self, mut should_trust: impl FnMut(&K) -> bool) -> Balance {
|
||||
let mut immature = 0;
|
||||
let mut trusted_pending = 0;
|
||||
let mut untrusted_pending = 0;
|
||||
let mut confirmed = 0;
|
||||
let last_sync_height = self.chain().latest_checkpoint().map(|latest| latest.height);
|
||||
for ((keychain, _), utxo) in self.full_utxos() {
|
||||
let chain_position = &utxo.chain_position;
|
||||
|
||||
match chain_position.height() {
|
||||
TxHeight::Confirmed(_) => {
|
||||
if utxo.is_on_coinbase {
|
||||
if utxo.is_mature(
|
||||
last_sync_height
|
||||
.expect("since it's confirmed we must have a checkpoint"),
|
||||
) {
|
||||
confirmed += utxo.txout.value;
|
||||
} else {
|
||||
immature += utxo.txout.value;
|
||||
}
|
||||
} else {
|
||||
confirmed += utxo.txout.value;
|
||||
}
|
||||
}
|
||||
TxHeight::Unconfirmed => {
|
||||
if should_trust(keychain) {
|
||||
trusted_pending += utxo.txout.value;
|
||||
} else {
|
||||
untrusted_pending += utxo.txout.value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Balance {
|
||||
immature,
|
||||
trusted_pending,
|
||||
untrusted_pending,
|
||||
confirmed,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the balance of all spendable confirmed unspent outputs of this tracker at a
|
||||
/// particular height.
|
||||
pub fn balance_at(&self, height: u32) -> u64 {
|
||||
self.full_txouts()
|
||||
.filter(|(_, full_txout)| full_txout.is_spendable_at(height))
|
||||
.map(|(_, full_txout)| full_txout.txout.value)
|
||||
.sum()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, P> Default for KeychainTracker<K, P> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
txout_index: Default::default(),
|
||||
chain_graph: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, P> AsRef<SparseChain<P>> for KeychainTracker<K, P> {
|
||||
fn as_ref(&self) -> &SparseChain<P> {
|
||||
self.chain_graph.chain()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, P> AsRef<TxGraph> for KeychainTracker<K, P> {
|
||||
fn as_ref(&self) -> &TxGraph {
|
||||
self.chain_graph.graph()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, P> AsRef<ChainGraph<P>> for KeychainTracker<K, P> {
|
||||
fn as_ref(&self) -> &ChainGraph<P> {
|
||||
&self.chain_graph
|
||||
}
|
||||
}
|
||||
@@ -166,7 +166,10 @@ impl<K: Clone + Ord + Debug> KeychainTxOutIndex<K> {
|
||||
///
|
||||
/// This will panic if a different `descriptor` is introduced to the same `keychain`.
|
||||
pub fn add_keychain(&mut self, keychain: K, descriptor: Descriptor<DescriptorPublicKey>) {
|
||||
let old_descriptor = &*self.keychains.entry(keychain).or_insert(descriptor.clone());
|
||||
let old_descriptor = &*self
|
||||
.keychains
|
||||
.entry(keychain)
|
||||
.or_insert_with(|| descriptor.clone());
|
||||
assert_eq!(
|
||||
&descriptor, old_descriptor,
|
||||
"keychain already contains a different descriptor"
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
//! [Bitcoin Dev Kit]: https://bitcoindevkit.org/
|
||||
#![no_std]
|
||||
pub use bitcoin;
|
||||
pub mod chain_graph;
|
||||
mod spk_txout_index;
|
||||
pub use spk_txout_index::*;
|
||||
mod chain_data;
|
||||
@@ -27,7 +26,6 @@ pub use chain_data::*;
|
||||
pub mod indexed_tx_graph;
|
||||
pub mod keychain;
|
||||
pub mod local_chain;
|
||||
pub mod sparse_chain;
|
||||
mod tx_data_traits;
|
||||
pub mod tx_graph;
|
||||
pub use tx_data_traits::*;
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -20,13 +20,13 @@ use bitcoin::{self, OutPoint, Script, Transaction, TxOut, Txid};
|
||||
/// Note there is no harm in scanning transactions that disappear from the blockchain or were never
|
||||
/// in there in the first place. `SpkTxOutIndex` is intentionally *monotone* -- you cannot delete or
|
||||
/// modify txouts that have been indexed. To find out which txouts from the index are actually in the
|
||||
/// chain or unspent, you must use other sources of information like a [`SparseChain`].
|
||||
/// chain or unspent, you must use other sources of information like a [`TxGraph`].
|
||||
///
|
||||
/// [`TxOut`]: bitcoin::TxOut
|
||||
/// [`insert_spk`]: Self::insert_spk
|
||||
/// [`Ord`]: core::cmp::Ord
|
||||
/// [`scan`]: Self::scan
|
||||
/// [`SparseChain`]: crate::sparse_chain::SparseChain
|
||||
/// [`TxGraph`]: crate::tx_graph::TxGraph
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct SpkTxOutIndex<I> {
|
||||
/// script pubkeys ordered by index
|
||||
|
||||
@@ -56,8 +56,8 @@
|
||||
//! ```
|
||||
|
||||
use crate::{
|
||||
collections::*, keychain::Balance, Anchor, Append, BlockId, ChainOracle, ForEachTxOut,
|
||||
FullTxOut, ObservedAs,
|
||||
collections::*, keychain::Balance, Anchor, Append, BlockId, ChainOracle, ChainPosition,
|
||||
ForEachTxOut, FullTxOut,
|
||||
};
|
||||
use alloc::vec::Vec;
|
||||
use bitcoin::{OutPoint, Script, Transaction, TxOut, Txid};
|
||||
@@ -135,7 +135,7 @@ impl Default for TxNodeInternal {
|
||||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct CanonicalTx<'a, T, A> {
|
||||
/// How the transaction is observed as (confirmed or unconfirmed).
|
||||
pub observed_as: ObservedAs<&'a A>,
|
||||
pub observed_as: ChainPosition<&'a A>,
|
||||
/// The transaction node (as part of the graph).
|
||||
pub node: TxNode<'a, T, A>,
|
||||
}
|
||||
@@ -614,7 +614,7 @@ impl<A: Anchor> TxGraph<A> {
|
||||
chain: &C,
|
||||
chain_tip: BlockId,
|
||||
txid: Txid,
|
||||
) -> Result<Option<ObservedAs<&A>>, C::Error> {
|
||||
) -> Result<Option<ChainPosition<&A>>, C::Error> {
|
||||
let (tx_node, anchors, last_seen) = match self.txs.get(&txid) {
|
||||
Some(v) => v,
|
||||
None => return Ok(None),
|
||||
@@ -622,7 +622,7 @@ impl<A: Anchor> TxGraph<A> {
|
||||
|
||||
for anchor in anchors {
|
||||
match chain.is_block_in_chain(anchor.anchor_block(), chain_tip)? {
|
||||
Some(true) => return Ok(Some(ObservedAs::Confirmed(anchor))),
|
||||
Some(true) => return Ok(Some(ChainPosition::Confirmed(anchor))),
|
||||
_ => continue,
|
||||
}
|
||||
}
|
||||
@@ -651,7 +651,7 @@ impl<A: Anchor> TxGraph<A> {
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Some(ObservedAs::Unconfirmed(*last_seen)))
|
||||
Ok(Some(ChainPosition::Unconfirmed(*last_seen)))
|
||||
}
|
||||
|
||||
/// Get the position of the transaction in `chain` with tip `chain_tip`.
|
||||
@@ -664,7 +664,7 @@ impl<A: Anchor> TxGraph<A> {
|
||||
chain: &C,
|
||||
chain_tip: BlockId,
|
||||
txid: Txid,
|
||||
) -> Option<ObservedAs<&A>> {
|
||||
) -> Option<ChainPosition<&A>> {
|
||||
self.try_get_chain_position(chain, chain_tip, txid)
|
||||
.expect("error is infallible")
|
||||
}
|
||||
@@ -686,7 +686,7 @@ impl<A: Anchor> TxGraph<A> {
|
||||
chain: &C,
|
||||
chain_tip: BlockId,
|
||||
outpoint: OutPoint,
|
||||
) -> Result<Option<(ObservedAs<&A>, Txid)>, C::Error> {
|
||||
) -> Result<Option<(ChainPosition<&A>, Txid)>, C::Error> {
|
||||
if self
|
||||
.try_get_chain_position(chain, chain_tip, outpoint.txid)?
|
||||
.is_none()
|
||||
@@ -714,7 +714,7 @@ impl<A: Anchor> TxGraph<A> {
|
||||
chain: &C,
|
||||
static_block: BlockId,
|
||||
outpoint: OutPoint,
|
||||
) -> Option<(ObservedAs<&A>, Txid)> {
|
||||
) -> Option<(ChainPosition<&A>, Txid)> {
|
||||
self.try_get_chain_spend(chain, static_block, outpoint)
|
||||
.expect("error is infallible")
|
||||
}
|
||||
@@ -786,7 +786,7 @@ impl<A: Anchor> TxGraph<A> {
|
||||
chain: &'a C,
|
||||
chain_tip: BlockId,
|
||||
outpoints: impl IntoIterator<Item = (OI, OutPoint)> + 'a,
|
||||
) -> impl Iterator<Item = Result<(OI, FullTxOut<ObservedAs<A>>), C::Error>> + 'a {
|
||||
) -> impl Iterator<Item = Result<(OI, FullTxOut<A>), C::Error>> + 'a {
|
||||
outpoints
|
||||
.into_iter()
|
||||
.map(
|
||||
@@ -837,7 +837,7 @@ impl<A: Anchor> TxGraph<A> {
|
||||
chain: &'a C,
|
||||
chain_tip: BlockId,
|
||||
outpoints: impl IntoIterator<Item = (OI, OutPoint)> + 'a,
|
||||
) -> impl Iterator<Item = (OI, FullTxOut<ObservedAs<A>>)> + 'a {
|
||||
) -> impl Iterator<Item = (OI, FullTxOut<A>)> + 'a {
|
||||
self.try_filter_chain_txouts(chain, chain_tip, outpoints)
|
||||
.map(|r| r.expect("oracle is infallible"))
|
||||
}
|
||||
@@ -865,7 +865,7 @@ impl<A: Anchor> TxGraph<A> {
|
||||
chain: &'a C,
|
||||
chain_tip: BlockId,
|
||||
outpoints: impl IntoIterator<Item = (OI, OutPoint)> + 'a,
|
||||
) -> impl Iterator<Item = Result<(OI, FullTxOut<ObservedAs<A>>), C::Error>> + 'a {
|
||||
) -> impl Iterator<Item = Result<(OI, FullTxOut<A>), C::Error>> + 'a {
|
||||
self.try_filter_chain_txouts(chain, chain_tip, outpoints)
|
||||
.filter(|r| match r {
|
||||
// keep unspents, drop spents
|
||||
@@ -886,7 +886,7 @@ impl<A: Anchor> TxGraph<A> {
|
||||
chain: &'a C,
|
||||
chain_tip: BlockId,
|
||||
txouts: impl IntoIterator<Item = (OI, OutPoint)> + 'a,
|
||||
) -> impl Iterator<Item = (OI, FullTxOut<ObservedAs<A>>)> + 'a {
|
||||
) -> impl Iterator<Item = (OI, FullTxOut<A>)> + 'a {
|
||||
self.try_filter_chain_unspents(chain, chain_tip, txouts)
|
||||
.map(|r| r.expect("oracle is infallible"))
|
||||
}
|
||||
@@ -919,14 +919,14 @@ impl<A: Anchor> TxGraph<A> {
|
||||
let (spk_i, txout) = res?;
|
||||
|
||||
match &txout.chain_position {
|
||||
ObservedAs::Confirmed(_) => {
|
||||
ChainPosition::Confirmed(_) => {
|
||||
if txout.is_confirmed_and_spendable(chain_tip.height) {
|
||||
confirmed += txout.txout.value;
|
||||
} else if !txout.is_mature(chain_tip.height) {
|
||||
immature += txout.txout.value;
|
||||
}
|
||||
}
|
||||
ObservedAs::Unconfirmed(_) => {
|
||||
ChainPosition::Unconfirmed(_) => {
|
||||
if trust_predicate(&spk_i, &txout.txout.script_pubkey) {
|
||||
trusted_pending += txout.txout.value;
|
||||
} else {
|
||||
|
||||
@@ -1,655 +0,0 @@
|
||||
#[macro_use]
|
||||
mod common;
|
||||
|
||||
use bdk_chain::{
|
||||
chain_graph::*,
|
||||
collections::HashSet,
|
||||
sparse_chain,
|
||||
tx_graph::{self, TxGraph},
|
||||
BlockId, TxHeight,
|
||||
};
|
||||
use bitcoin::{OutPoint, PackedLockTime, Script, Sequence, Transaction, TxIn, TxOut, Witness};
|
||||
|
||||
#[test]
|
||||
fn test_spent_by() {
|
||||
let tx1 = Transaction {
|
||||
version: 0x01,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![],
|
||||
output: vec![TxOut::default()],
|
||||
};
|
||||
|
||||
let op = OutPoint {
|
||||
txid: tx1.txid(),
|
||||
vout: 0,
|
||||
};
|
||||
|
||||
let tx2 = Transaction {
|
||||
version: 0x01,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![TxIn {
|
||||
previous_output: op,
|
||||
..Default::default()
|
||||
}],
|
||||
output: vec![],
|
||||
};
|
||||
let tx3 = Transaction {
|
||||
version: 0x01,
|
||||
lock_time: PackedLockTime(42),
|
||||
input: vec![TxIn {
|
||||
previous_output: op,
|
||||
..Default::default()
|
||||
}],
|
||||
output: vec![],
|
||||
};
|
||||
|
||||
let mut cg1 = ChainGraph::default();
|
||||
let _ = cg1
|
||||
.insert_tx(tx1, TxHeight::Unconfirmed)
|
||||
.expect("should insert");
|
||||
let mut cg2 = cg1.clone();
|
||||
let _ = cg1
|
||||
.insert_tx(tx2.clone(), TxHeight::Unconfirmed)
|
||||
.expect("should insert");
|
||||
let _ = cg2
|
||||
.insert_tx(tx3.clone(), TxHeight::Unconfirmed)
|
||||
.expect("should insert");
|
||||
|
||||
assert_eq!(cg1.spent_by(op), Some((&TxHeight::Unconfirmed, tx2.txid())));
|
||||
assert_eq!(cg2.spent_by(op), Some((&TxHeight::Unconfirmed, tx3.txid())));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn update_evicts_conflicting_tx() {
|
||||
let cp_a = BlockId {
|
||||
height: 0,
|
||||
hash: h!("A"),
|
||||
};
|
||||
let cp_b = BlockId {
|
||||
height: 1,
|
||||
hash: h!("B"),
|
||||
};
|
||||
let cp_b2 = BlockId {
|
||||
height: 1,
|
||||
hash: h!("B'"),
|
||||
};
|
||||
|
||||
let tx_a = Transaction {
|
||||
version: 0x01,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![],
|
||||
output: vec![TxOut::default()],
|
||||
};
|
||||
|
||||
let tx_b = Transaction {
|
||||
version: 0x01,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![TxIn {
|
||||
previous_output: OutPoint::new(tx_a.txid(), 0),
|
||||
script_sig: Script::new(),
|
||||
sequence: Sequence::default(),
|
||||
witness: Witness::new(),
|
||||
}],
|
||||
output: vec![TxOut::default()],
|
||||
};
|
||||
|
||||
let tx_b2 = Transaction {
|
||||
version: 0x02,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![TxIn {
|
||||
previous_output: OutPoint::new(tx_a.txid(), 0),
|
||||
script_sig: Script::new(),
|
||||
sequence: Sequence::default(),
|
||||
witness: Witness::new(),
|
||||
}],
|
||||
output: vec![TxOut::default(), TxOut::default()],
|
||||
};
|
||||
{
|
||||
let mut cg1 = {
|
||||
let mut cg = ChainGraph::default();
|
||||
let _ = cg.insert_checkpoint(cp_a).expect("should insert cp");
|
||||
let _ = cg
|
||||
.insert_tx(tx_a.clone(), TxHeight::Confirmed(0))
|
||||
.expect("should insert tx");
|
||||
let _ = cg
|
||||
.insert_tx(tx_b.clone(), TxHeight::Unconfirmed)
|
||||
.expect("should insert tx");
|
||||
cg
|
||||
};
|
||||
let cg2 = {
|
||||
let mut cg = ChainGraph::default();
|
||||
let _ = cg
|
||||
.insert_tx(tx_b2.clone(), TxHeight::Unconfirmed)
|
||||
.expect("should insert tx");
|
||||
cg
|
||||
};
|
||||
|
||||
let changeset = ChangeSet::<TxHeight> {
|
||||
chain: sparse_chain::ChangeSet {
|
||||
checkpoints: Default::default(),
|
||||
txids: [
|
||||
(tx_b.txid(), None),
|
||||
(tx_b2.txid(), Some(TxHeight::Unconfirmed)),
|
||||
]
|
||||
.into(),
|
||||
},
|
||||
graph: tx_graph::Additions {
|
||||
tx: [tx_b2.clone()].into(),
|
||||
txout: [].into(),
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
assert_eq!(
|
||||
cg1.determine_changeset(&cg2),
|
||||
Ok(changeset.clone()),
|
||||
"tx should be evicted from mempool"
|
||||
);
|
||||
|
||||
cg1.apply_changeset(changeset);
|
||||
}
|
||||
|
||||
{
|
||||
let cg1 = {
|
||||
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
|
||||
.insert_tx(tx_a.clone(), TxHeight::Confirmed(0))
|
||||
.expect("should insert tx");
|
||||
let _ = cg
|
||||
.insert_tx(tx_b.clone(), TxHeight::Confirmed(1))
|
||||
.expect("should insert tx");
|
||||
cg
|
||||
};
|
||||
let cg2 = {
|
||||
let mut cg = ChainGraph::default();
|
||||
let _ = cg
|
||||
.insert_tx(tx_b2.clone(), TxHeight::Unconfirmed)
|
||||
.expect("should insert tx");
|
||||
cg
|
||||
};
|
||||
assert_eq!(
|
||||
cg1.determine_changeset(&cg2),
|
||||
Err(UpdateError::UnresolvableConflict(UnresolvableConflict {
|
||||
already_confirmed_tx: (TxHeight::Confirmed(1), tx_b.txid()),
|
||||
update_tx: (TxHeight::Unconfirmed, tx_b2.txid()),
|
||||
})),
|
||||
"fail if tx is evicted from valid block"
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
// Given 2 blocks `{A, B}`, and an update that invalidates block B with
|
||||
// `{A, B'}`, we expect txs that exist in `B` that conflicts with txs
|
||||
// introduced in the update to be successfully evicted.
|
||||
let mut cg1 = {
|
||||
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
|
||||
.insert_tx(tx_a, TxHeight::Confirmed(0))
|
||||
.expect("should insert tx");
|
||||
let _ = cg
|
||||
.insert_tx(tx_b.clone(), TxHeight::Confirmed(1))
|
||||
.expect("should insert tx");
|
||||
cg
|
||||
};
|
||||
let cg2 = {
|
||||
let mut cg = ChainGraph::default();
|
||||
let _ = cg.insert_checkpoint(cp_a).expect("should insert cp");
|
||||
let _ = cg.insert_checkpoint(cp_b2).expect("should insert cp");
|
||||
let _ = cg
|
||||
.insert_tx(tx_b2.clone(), TxHeight::Unconfirmed)
|
||||
.expect("should insert tx");
|
||||
cg
|
||||
};
|
||||
|
||||
let changeset = ChangeSet::<TxHeight> {
|
||||
chain: sparse_chain::ChangeSet {
|
||||
checkpoints: [(1, Some(h!("B'")))].into(),
|
||||
txids: [
|
||||
(tx_b.txid(), None),
|
||||
(tx_b2.txid(), Some(TxHeight::Unconfirmed)),
|
||||
]
|
||||
.into(),
|
||||
},
|
||||
graph: tx_graph::Additions {
|
||||
tx: [tx_b2].into(),
|
||||
txout: [].into(),
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
assert_eq!(
|
||||
cg1.determine_changeset(&cg2),
|
||||
Ok(changeset.clone()),
|
||||
"tx should be evicted from B",
|
||||
);
|
||||
|
||||
cg1.apply_changeset(changeset);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chain_graph_new_missing() {
|
||||
let tx_a = Transaction {
|
||||
version: 0x01,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![],
|
||||
output: vec![TxOut::default()],
|
||||
};
|
||||
let tx_b = Transaction {
|
||||
version: 0x02,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![],
|
||||
output: vec![TxOut::default()],
|
||||
};
|
||||
|
||||
let update = chain!(
|
||||
index: TxHeight,
|
||||
checkpoints: [[0, h!("A")]],
|
||||
txids: [
|
||||
(tx_a.txid(), TxHeight::Confirmed(0)),
|
||||
(tx_b.txid(), TxHeight::Confirmed(0))
|
||||
]
|
||||
);
|
||||
let mut graph = TxGraph::default();
|
||||
|
||||
let mut expected_missing = HashSet::new();
|
||||
expected_missing.insert(tx_a.txid());
|
||||
expected_missing.insert(tx_b.txid());
|
||||
|
||||
assert_eq!(
|
||||
ChainGraph::new(update.clone(), graph.clone()),
|
||||
Err(NewError::Missing(expected_missing.clone()))
|
||||
);
|
||||
|
||||
let _ = graph.insert_tx(tx_b.clone());
|
||||
expected_missing.remove(&tx_b.txid());
|
||||
|
||||
assert_eq!(
|
||||
ChainGraph::new(update.clone(), graph.clone()),
|
||||
Err(NewError::Missing(expected_missing.clone()))
|
||||
);
|
||||
|
||||
let _ = graph.insert_txout(
|
||||
OutPoint {
|
||||
txid: tx_a.txid(),
|
||||
vout: 0,
|
||||
},
|
||||
tx_a.output[0].clone(),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
ChainGraph::new(update.clone(), graph.clone()),
|
||||
Err(NewError::Missing(expected_missing)),
|
||||
"inserting an output instead of full tx doesn't satisfy constraint"
|
||||
);
|
||||
|
||||
let _ = graph.insert_tx(tx_a.clone());
|
||||
|
||||
let new_graph = ChainGraph::new(update.clone(), graph.clone()).unwrap();
|
||||
let expected_graph = {
|
||||
let mut cg = ChainGraph::<TxHeight>::default();
|
||||
let _ = cg
|
||||
.insert_checkpoint(update.latest_checkpoint().unwrap())
|
||||
.unwrap();
|
||||
let _ = cg.insert_tx(tx_a, TxHeight::Confirmed(0)).unwrap();
|
||||
let _ = cg.insert_tx(tx_b, TxHeight::Confirmed(0)).unwrap();
|
||||
cg
|
||||
};
|
||||
|
||||
assert_eq!(new_graph, expected_graph);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn chain_graph_new_conflicts() {
|
||||
let tx_a = Transaction {
|
||||
version: 0x01,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![],
|
||||
output: vec![TxOut::default()],
|
||||
};
|
||||
|
||||
let tx_b = Transaction {
|
||||
version: 0x01,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![TxIn {
|
||||
previous_output: OutPoint::new(tx_a.txid(), 0),
|
||||
script_sig: Script::new(),
|
||||
sequence: Sequence::default(),
|
||||
witness: Witness::new(),
|
||||
}],
|
||||
output: vec![TxOut::default()],
|
||||
};
|
||||
|
||||
let tx_b2 = Transaction {
|
||||
version: 0x02,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![TxIn {
|
||||
previous_output: OutPoint::new(tx_a.txid(), 0),
|
||||
script_sig: Script::new(),
|
||||
sequence: Sequence::default(),
|
||||
witness: Witness::new(),
|
||||
}],
|
||||
output: vec![TxOut::default(), TxOut::default()],
|
||||
};
|
||||
|
||||
let chain = chain!(
|
||||
index: TxHeight,
|
||||
checkpoints: [[5, h!("A")]],
|
||||
txids: [
|
||||
(tx_a.txid(), TxHeight::Confirmed(1)),
|
||||
(tx_b.txid(), TxHeight::Confirmed(2)),
|
||||
(tx_b2.txid(), TxHeight::Confirmed(3))
|
||||
]
|
||||
);
|
||||
|
||||
let graph = TxGraph::new([tx_a, tx_b, tx_b2]);
|
||||
|
||||
assert!(matches!(
|
||||
ChainGraph::new(chain, graph),
|
||||
Err(NewError::Conflict { .. })
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_get_tx_in_chain() {
|
||||
let mut cg = ChainGraph::default();
|
||||
let tx = Transaction {
|
||||
version: 0x01,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![],
|
||||
output: vec![TxOut::default()],
|
||||
};
|
||||
|
||||
let _ = cg.insert_tx(tx.clone(), TxHeight::Unconfirmed).unwrap();
|
||||
assert_eq!(
|
||||
cg.get_tx_in_chain(tx.txid()),
|
||||
Some((&TxHeight::Unconfirmed, &tx,))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_iterate_transactions() {
|
||||
let mut cg = ChainGraph::default();
|
||||
let txs = (0..3)
|
||||
.map(|i| Transaction {
|
||||
version: i,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![],
|
||||
output: vec![TxOut::default()],
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let _ = cg
|
||||
.insert_checkpoint(BlockId {
|
||||
height: 1,
|
||||
hash: h!("A"),
|
||||
})
|
||||
.unwrap();
|
||||
let _ = cg
|
||||
.insert_tx(txs[0].clone(), TxHeight::Confirmed(1))
|
||||
.unwrap();
|
||||
let _ = cg.insert_tx(txs[1].clone(), TxHeight::Unconfirmed).unwrap();
|
||||
let _ = cg
|
||||
.insert_tx(txs[2].clone(), TxHeight::Confirmed(0))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
cg.transactions_in_chain().collect::<Vec<_>>(),
|
||||
vec![
|
||||
(&TxHeight::Confirmed(0), &txs[2],),
|
||||
(&TxHeight::Confirmed(1), &txs[0],),
|
||||
(&TxHeight::Unconfirmed, &txs[1],),
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/// Start with: block1, block2a, tx1, tx2a
|
||||
/// Update 1: block2a -> block2b , tx2a -> tx2b
|
||||
/// Update 2: block2b -> block2c , tx2b -> tx2a
|
||||
#[test]
|
||||
fn test_apply_changes_reintroduce_tx() {
|
||||
let block1 = BlockId {
|
||||
height: 1,
|
||||
hash: h!("block 1"),
|
||||
};
|
||||
let block2a = BlockId {
|
||||
height: 2,
|
||||
hash: h!("block 2a"),
|
||||
};
|
||||
let block2b = BlockId {
|
||||
height: 2,
|
||||
hash: h!("block 2b"),
|
||||
};
|
||||
let block2c = BlockId {
|
||||
height: 2,
|
||||
hash: h!("block 2c"),
|
||||
};
|
||||
|
||||
let tx1 = Transaction {
|
||||
version: 0,
|
||||
lock_time: PackedLockTime(1),
|
||||
input: Vec::new(),
|
||||
output: [TxOut {
|
||||
value: 1,
|
||||
script_pubkey: Script::new(),
|
||||
}]
|
||||
.into(),
|
||||
};
|
||||
|
||||
let tx2a = Transaction {
|
||||
version: 0,
|
||||
lock_time: PackedLockTime('a'.into()),
|
||||
input: [TxIn {
|
||||
previous_output: OutPoint::new(tx1.txid(), 0),
|
||||
..Default::default()
|
||||
}]
|
||||
.into(),
|
||||
output: [TxOut {
|
||||
value: 0,
|
||||
..Default::default()
|
||||
}]
|
||||
.into(),
|
||||
};
|
||||
|
||||
let tx2b = Transaction {
|
||||
lock_time: PackedLockTime('b'.into()),
|
||||
..tx2a.clone()
|
||||
};
|
||||
|
||||
// block1, block2a, tx1, tx2a
|
||||
let mut cg = {
|
||||
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();
|
||||
let _ = cg.insert_tx(tx2a.clone(), TxHeight::Confirmed(2)).unwrap();
|
||||
cg
|
||||
};
|
||||
|
||||
// block2a -> block2b , tx2a -> tx2b
|
||||
let update = {
|
||||
let mut update = ChainGraph::default();
|
||||
let _ = update.insert_checkpoint(block1).unwrap();
|
||||
let _ = update.insert_checkpoint(block2b).unwrap();
|
||||
let _ = update
|
||||
.insert_tx(tx2b.clone(), TxHeight::Confirmed(2))
|
||||
.unwrap();
|
||||
update
|
||||
};
|
||||
assert_eq!(
|
||||
cg.apply_update(update).expect("should update"),
|
||||
ChangeSet {
|
||||
chain: changeset! {
|
||||
checkpoints: [(2, Some(block2b.hash))],
|
||||
txids: [(tx2a.txid(), None), (tx2b.txid(), Some(TxHeight::Confirmed(2)))]
|
||||
},
|
||||
graph: tx_graph::Additions {
|
||||
tx: [tx2b.clone()].into(),
|
||||
..Default::default()
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
// block2b -> block2c , tx2b -> tx2a
|
||||
let update = {
|
||||
let mut update = ChainGraph::default();
|
||||
let _ = update.insert_checkpoint(block1).unwrap();
|
||||
let _ = update.insert_checkpoint(block2c).unwrap();
|
||||
let _ = update
|
||||
.insert_tx(tx2a.clone(), TxHeight::Confirmed(2))
|
||||
.unwrap();
|
||||
update
|
||||
};
|
||||
assert_eq!(
|
||||
cg.apply_update(update).expect("should update"),
|
||||
ChangeSet {
|
||||
chain: changeset! {
|
||||
checkpoints: [(2, Some(block2c.hash))],
|
||||
txids: [(tx2b.txid(), None), (tx2a.txid(), Some(TxHeight::Confirmed(2)))]
|
||||
},
|
||||
..Default::default()
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_evict_descendants() {
|
||||
let block_1 = BlockId {
|
||||
height: 1,
|
||||
hash: h!("block 1"),
|
||||
};
|
||||
|
||||
let block_2a = BlockId {
|
||||
height: 2,
|
||||
hash: h!("block 2 a"),
|
||||
};
|
||||
|
||||
let block_2b = BlockId {
|
||||
height: 2,
|
||||
hash: h!("block 2 b"),
|
||||
};
|
||||
|
||||
let tx_1 = Transaction {
|
||||
input: vec![TxIn {
|
||||
previous_output: OutPoint::new(h!("fake tx"), 0),
|
||||
..Default::default()
|
||||
}],
|
||||
output: vec![TxOut {
|
||||
value: 10_000,
|
||||
script_pubkey: Script::new(),
|
||||
}],
|
||||
..common::new_tx(1)
|
||||
};
|
||||
let tx_2 = Transaction {
|
||||
input: vec![TxIn {
|
||||
previous_output: OutPoint::new(tx_1.txid(), 0),
|
||||
..Default::default()
|
||||
}],
|
||||
output: vec![
|
||||
TxOut {
|
||||
value: 20_000,
|
||||
script_pubkey: Script::new(),
|
||||
},
|
||||
TxOut {
|
||||
value: 30_000,
|
||||
script_pubkey: Script::new(),
|
||||
},
|
||||
],
|
||||
..common::new_tx(2)
|
||||
};
|
||||
let tx_3 = Transaction {
|
||||
input: vec![TxIn {
|
||||
previous_output: OutPoint::new(tx_2.txid(), 0),
|
||||
..Default::default()
|
||||
}],
|
||||
output: vec![TxOut {
|
||||
value: 40_000,
|
||||
script_pubkey: Script::new(),
|
||||
}],
|
||||
..common::new_tx(3)
|
||||
};
|
||||
let tx_4 = Transaction {
|
||||
input: vec![TxIn {
|
||||
previous_output: OutPoint::new(tx_2.txid(), 1),
|
||||
..Default::default()
|
||||
}],
|
||||
output: vec![TxOut {
|
||||
value: 40_000,
|
||||
script_pubkey: Script::new(),
|
||||
}],
|
||||
..common::new_tx(4)
|
||||
};
|
||||
let tx_5 = Transaction {
|
||||
input: vec![TxIn {
|
||||
previous_output: OutPoint::new(tx_4.txid(), 0),
|
||||
..Default::default()
|
||||
}],
|
||||
output: vec![TxOut {
|
||||
value: 40_000,
|
||||
script_pubkey: Script::new(),
|
||||
}],
|
||||
..common::new_tx(5)
|
||||
};
|
||||
|
||||
let tx_conflict = Transaction {
|
||||
input: vec![TxIn {
|
||||
previous_output: OutPoint::new(tx_1.txid(), 0),
|
||||
..Default::default()
|
||||
}],
|
||||
output: vec![TxOut {
|
||||
value: 12345,
|
||||
script_pubkey: Script::new(),
|
||||
}],
|
||||
..common::new_tx(6)
|
||||
};
|
||||
|
||||
// 1 is spent by 2, 2 is spent by 3 and 4, 4 is spent by 5
|
||||
let _txid_1 = tx_1.txid();
|
||||
let txid_2 = tx_2.txid();
|
||||
let txid_3 = tx_3.txid();
|
||||
let txid_4 = tx_4.txid();
|
||||
let txid_5 = tx_5.txid();
|
||||
|
||||
// this tx conflicts with 2
|
||||
let txid_conflict = tx_conflict.txid();
|
||||
|
||||
let cg = {
|
||||
let mut cg = ChainGraph::<TxHeight>::default();
|
||||
let _ = cg.insert_checkpoint(block_1);
|
||||
let _ = cg.insert_checkpoint(block_2a);
|
||||
let _ = cg.insert_tx(tx_1, TxHeight::Confirmed(1));
|
||||
let _ = cg.insert_tx(tx_2, TxHeight::Confirmed(2));
|
||||
let _ = cg.insert_tx(tx_3, TxHeight::Confirmed(2));
|
||||
let _ = cg.insert_tx(tx_4, TxHeight::Confirmed(2));
|
||||
let _ = cg.insert_tx(tx_5, TxHeight::Confirmed(2));
|
||||
cg
|
||||
};
|
||||
|
||||
let update = {
|
||||
let mut cg = ChainGraph::<TxHeight>::default();
|
||||
let _ = cg.insert_checkpoint(block_1);
|
||||
let _ = cg.insert_checkpoint(block_2b);
|
||||
let _ = cg.insert_tx(tx_conflict.clone(), TxHeight::Confirmed(2));
|
||||
cg
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
cg.determine_changeset(&update),
|
||||
Ok(ChangeSet {
|
||||
chain: changeset! {
|
||||
checkpoints: [(2, Some(block_2b.hash))],
|
||||
txids: [(txid_2, None), (txid_3, None), (txid_4, None), (txid_5, None), (txid_conflict, Some(TxHeight::Confirmed(2)))]
|
||||
},
|
||||
graph: tx_graph::Additions {
|
||||
tx: [tx_conflict.clone()].into(),
|
||||
..Default::default()
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
let err = cg
|
||||
.insert_tx_preview(tx_conflict, TxHeight::Unconfirmed)
|
||||
.expect_err("must fail due to conflicts");
|
||||
assert!(matches!(err, InsertTxError::UnresolvableConflict(_)));
|
||||
}
|
||||
@@ -8,7 +8,7 @@ use bdk_chain::{
|
||||
keychain::{Balance, DerivationAdditions, KeychainTxOutIndex},
|
||||
local_chain::LocalChain,
|
||||
tx_graph::Additions,
|
||||
BlockId, ConfirmationHeightAnchor, ObservedAs,
|
||||
BlockId, ChainPosition, ConfirmationHeightAnchor,
|
||||
};
|
||||
use bitcoin::{secp256k1::Secp256k1, BlockHash, OutPoint, Script, Transaction, TxIn, TxOut};
|
||||
use miniscript::Descriptor;
|
||||
@@ -266,7 +266,7 @@ fn test_list_owned_txouts() {
|
||||
let confirmed_txouts_txid = txouts
|
||||
.iter()
|
||||
.filter_map(|(_, full_txout)| {
|
||||
if matches!(full_txout.chain_position, ObservedAs::Confirmed(_)) {
|
||||
if matches!(full_txout.chain_position, ChainPosition::Confirmed(_)) {
|
||||
Some(full_txout.outpoint.txid)
|
||||
} else {
|
||||
None
|
||||
@@ -277,7 +277,7 @@ fn test_list_owned_txouts() {
|
||||
let unconfirmed_txouts_txid = txouts
|
||||
.iter()
|
||||
.filter_map(|(_, full_txout)| {
|
||||
if matches!(full_txout.chain_position, ObservedAs::Unconfirmed(_)) {
|
||||
if matches!(full_txout.chain_position, ChainPosition::Unconfirmed(_)) {
|
||||
Some(full_txout.outpoint.txid)
|
||||
} else {
|
||||
None
|
||||
@@ -288,7 +288,7 @@ fn test_list_owned_txouts() {
|
||||
let confirmed_utxos_txid = utxos
|
||||
.iter()
|
||||
.filter_map(|(_, full_txout)| {
|
||||
if matches!(full_txout.chain_position, ObservedAs::Confirmed(_)) {
|
||||
if matches!(full_txout.chain_position, ChainPosition::Confirmed(_)) {
|
||||
Some(full_txout.outpoint.txid)
|
||||
} else {
|
||||
None
|
||||
@@ -299,7 +299,7 @@ fn test_list_owned_txouts() {
|
||||
let unconfirmed_utxos_txid = utxos
|
||||
.iter()
|
||||
.filter_map(|(_, full_txout)| {
|
||||
if matches!(full_txout.chain_position, ObservedAs::Unconfirmed(_)) {
|
||||
if matches!(full_txout.chain_position, ChainPosition::Unconfirmed(_)) {
|
||||
Some(full_txout.outpoint.txid)
|
||||
} else {
|
||||
None
|
||||
|
||||
@@ -1,240 +0,0 @@
|
||||
#![cfg(feature = "miniscript")]
|
||||
#[macro_use]
|
||||
mod common;
|
||||
|
||||
use bdk_chain::{
|
||||
keychain::{Balance, KeychainTracker},
|
||||
miniscript::{
|
||||
bitcoin::{secp256k1::Secp256k1, OutPoint, PackedLockTime, Transaction, TxOut},
|
||||
Descriptor,
|
||||
},
|
||||
BlockId, ConfirmationTime, TxHeight,
|
||||
};
|
||||
use bitcoin::TxIn;
|
||||
|
||||
#[test]
|
||||
fn test_insert_tx() {
|
||||
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());
|
||||
let txout = TxOut {
|
||||
value: 100_000,
|
||||
script_pubkey: descriptor.at_derivation_index(5).script_pubkey(),
|
||||
};
|
||||
|
||||
let tx = Transaction {
|
||||
version: 0x01,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![],
|
||||
output: vec![txout],
|
||||
};
|
||||
|
||||
let _ = tracker.txout_index.reveal_to_target(&(), 5);
|
||||
|
||||
let changeset = tracker
|
||||
.insert_tx_preview(tx.clone(), ConfirmationTime::Unconfirmed { last_seen: 0 })
|
||||
.unwrap();
|
||||
tracker.apply_changeset(changeset);
|
||||
assert_eq!(
|
||||
tracker
|
||||
.chain_graph()
|
||||
.transactions_in_chain()
|
||||
.collect::<Vec<_>>(),
|
||||
vec![(&ConfirmationTime::Unconfirmed { last_seen: 0 }, &tx,)]
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
tracker
|
||||
.txout_index
|
||||
.txouts_of_keychain(&())
|
||||
.collect::<Vec<_>>(),
|
||||
vec![(
|
||||
5,
|
||||
OutPoint {
|
||||
txid: tx.txid(),
|
||||
vout: 0
|
||||
}
|
||||
)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_balance() {
|
||||
use core::str::FromStr;
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd)]
|
||||
enum Keychain {
|
||||
One,
|
||||
Two,
|
||||
}
|
||||
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);
|
||||
tracker.add_keychain(Keychain::Two, two);
|
||||
|
||||
let tx1 = Transaction {
|
||||
version: 0x01,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![],
|
||||
output: vec![TxOut {
|
||||
value: 13_000,
|
||||
script_pubkey: tracker
|
||||
.txout_index
|
||||
.reveal_next_spk(&Keychain::One)
|
||||
.0
|
||||
.1
|
||||
.clone(),
|
||||
}],
|
||||
};
|
||||
|
||||
let tx2 = Transaction {
|
||||
version: 0x01,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![],
|
||||
output: vec![TxOut {
|
||||
value: 7_000,
|
||||
script_pubkey: tracker
|
||||
.txout_index
|
||||
.reveal_next_spk(&Keychain::Two)
|
||||
.0
|
||||
.1
|
||||
.clone(),
|
||||
}],
|
||||
};
|
||||
|
||||
let tx_coinbase = Transaction {
|
||||
version: 0x01,
|
||||
lock_time: PackedLockTime(0),
|
||||
input: vec![TxIn::default()],
|
||||
output: vec![TxOut {
|
||||
value: 11_000,
|
||||
script_pubkey: tracker
|
||||
.txout_index
|
||||
.reveal_next_spk(&Keychain::Two)
|
||||
.0
|
||||
.1
|
||||
.clone(),
|
||||
}],
|
||||
};
|
||||
|
||||
assert!(tx_coinbase.is_coin_base());
|
||||
|
||||
let _ = tracker
|
||||
.insert_checkpoint(BlockId {
|
||||
height: 5,
|
||||
hash: h!("1"),
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
let should_trust = |keychain: &Keychain| match *keychain {
|
||||
Keychain::One => false,
|
||||
Keychain::Two => true,
|
||||
};
|
||||
|
||||
assert_eq!(tracker.balance(should_trust), Balance::default());
|
||||
|
||||
let _ = tracker
|
||||
.insert_tx(tx1.clone(), TxHeight::Unconfirmed)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
tracker.balance(should_trust),
|
||||
Balance {
|
||||
untrusted_pending: 13_000,
|
||||
..Default::default()
|
||||
}
|
||||
);
|
||||
|
||||
let _ = tracker
|
||||
.insert_tx(tx2.clone(), TxHeight::Unconfirmed)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
tracker.balance(should_trust),
|
||||
Balance {
|
||||
trusted_pending: 7_000,
|
||||
untrusted_pending: 13_000,
|
||||
..Default::default()
|
||||
}
|
||||
);
|
||||
|
||||
let _ = tracker
|
||||
.insert_tx(tx_coinbase, TxHeight::Confirmed(0))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
tracker.balance(should_trust),
|
||||
Balance {
|
||||
trusted_pending: 7_000,
|
||||
untrusted_pending: 13_000,
|
||||
immature: 11_000,
|
||||
..Default::default()
|
||||
}
|
||||
);
|
||||
|
||||
let _ = tracker.insert_tx(tx1, TxHeight::Confirmed(1)).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
tracker.balance(should_trust),
|
||||
Balance {
|
||||
trusted_pending: 7_000,
|
||||
untrusted_pending: 0,
|
||||
immature: 11_000,
|
||||
confirmed: 13_000,
|
||||
}
|
||||
);
|
||||
|
||||
let _ = tracker.insert_tx(tx2, TxHeight::Confirmed(2)).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
tracker.balance(should_trust),
|
||||
Balance {
|
||||
trusted_pending: 0,
|
||||
untrusted_pending: 0,
|
||||
immature: 11_000,
|
||||
confirmed: 20_000,
|
||||
}
|
||||
);
|
||||
|
||||
let _ = tracker
|
||||
.insert_checkpoint(BlockId {
|
||||
height: 98,
|
||||
hash: h!("98"),
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
tracker.balance(should_trust),
|
||||
Balance {
|
||||
trusted_pending: 0,
|
||||
untrusted_pending: 0,
|
||||
immature: 11_000,
|
||||
confirmed: 20_000,
|
||||
}
|
||||
);
|
||||
|
||||
let _ = tracker
|
||||
.insert_checkpoint(BlockId {
|
||||
height: 99,
|
||||
hash: h!("99"),
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
tracker.balance(should_trust),
|
||||
Balance {
|
||||
trusted_pending: 0,
|
||||
untrusted_pending: 0,
|
||||
immature: 0,
|
||||
confirmed: 31_000,
|
||||
}
|
||||
);
|
||||
|
||||
assert_eq!(tracker.balance_at(0), 0);
|
||||
assert_eq!(tracker.balance_at(1), 13_000);
|
||||
assert_eq!(tracker.balance_at(2), 20_000);
|
||||
assert_eq!(tracker.balance_at(98), 20_000);
|
||||
assert_eq!(tracker.balance_at(99), 31_000);
|
||||
assert_eq!(tracker.balance_at(100), 31_000);
|
||||
}
|
||||
@@ -1,773 +0,0 @@
|
||||
#[macro_use]
|
||||
mod common;
|
||||
|
||||
use bdk_chain::{collections::BTreeSet, sparse_chain::*, BlockId, TxHeight};
|
||||
use bitcoin::{hashes::Hash, Txid};
|
||||
use core::ops::Bound;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
|
||||
pub struct TestIndex(TxHeight, u32);
|
||||
|
||||
impl ChainPosition for TestIndex {
|
||||
fn height(&self) -> TxHeight {
|
||||
self.0
|
||||
}
|
||||
|
||||
fn max_ord_of_height(height: TxHeight) -> Self {
|
||||
Self(height, u32::MAX)
|
||||
}
|
||||
|
||||
fn min_ord_of_height(height: TxHeight) -> Self {
|
||||
Self(height, u32::MIN)
|
||||
}
|
||||
}
|
||||
|
||||
impl TestIndex {
|
||||
pub fn new<H>(height: H, ext: u32) -> Self
|
||||
where
|
||||
H: Into<TxHeight>,
|
||||
{
|
||||
Self(height.into(), ext)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_first_checkpoint() {
|
||||
let chain = SparseChain::default();
|
||||
assert_eq!(
|
||||
chain.determine_changeset(&chain!([0, h!("A")])),
|
||||
Ok(changeset! {
|
||||
checkpoints: [(0, Some(h!("A")))],
|
||||
txids: []
|
||||
},),
|
||||
"add first tip"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn add_second_tip() {
|
||||
let chain = chain!([0, h!("A")]);
|
||||
assert_eq!(
|
||||
chain.determine_changeset(&chain!([0, h!("A")], [1, h!("B")])),
|
||||
Ok(changeset! {
|
||||
checkpoints: [(1, Some(h!("B")))],
|
||||
txids: []
|
||||
},),
|
||||
"extend tip by one"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn two_disjoint_chains_cannot_merge() {
|
||||
let chain1 = chain!([0, h!("A")]);
|
||||
let chain2 = chain!([1, h!("B")]);
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Err(UpdateError::NotConnected(0))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_chains_should_merge() {
|
||||
let chain1 = chain!([0, h!("A")]);
|
||||
let chain2 = chain!([0, h!("A")]);
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(ChangeSet::default())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_chains_with_txs_should_merge() {
|
||||
let chain1 = chain!(checkpoints: [[0,h!("A")]], txids: [(h!("tx0"), TxHeight::Confirmed(0))]);
|
||||
let chain2 = chain!(checkpoints: [[0,h!("A")]], txids: [(h!("tx0"), TxHeight::Confirmed(0))]);
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(ChangeSet::default())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn duplicate_chains_with_different_txs_should_merge() {
|
||||
let chain1 = chain!(checkpoints: [[0,h!("A")]], txids: [(h!("tx0"), TxHeight::Confirmed(0))]);
|
||||
let chain2 = chain!(checkpoints: [[0,h!("A")]], txids: [(h!("tx1"), TxHeight::Confirmed(0))]);
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [],
|
||||
txids: [(h!("tx1"), Some(TxHeight::Confirmed(0)))]
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalidate_first_and_only_checkpoint_without_tx_changes() {
|
||||
let chain1 = chain!(checkpoints: [[0,h!("A")]], txids: [(h!("tx0"), TxHeight::Confirmed(0))]);
|
||||
let chain2 = chain!(checkpoints: [[0,h!("A'")]], txids: [(h!("tx0"), TxHeight::Confirmed(0))]);
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [(0, Some(h!("A'")))],
|
||||
txids: []
|
||||
},)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalidate_first_and_only_checkpoint_with_tx_move_forward() {
|
||||
let chain1 = chain!(checkpoints: [[0,h!("A")]], txids: [(h!("tx0"), TxHeight::Confirmed(0))]);
|
||||
let chain2 = chain!(checkpoints: [[0,h!("A'")],[1, h!("B")]], txids: [(h!("tx0"), TxHeight::Confirmed(1))]);
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [(0, Some(h!("A'"))), (1, Some(h!("B")))],
|
||||
txids: [(h!("tx0"), Some(TxHeight::Confirmed(1)))]
|
||||
},)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalidate_first_and_only_checkpoint_with_tx_move_backward() {
|
||||
let chain1 = chain!(checkpoints: [[1,h!("B")]], txids: [(h!("tx0"), TxHeight::Confirmed(1))]);
|
||||
let chain2 = chain!(checkpoints: [[0,h!("A")],[1, h!("B'")]], txids: [(h!("tx0"), TxHeight::Confirmed(0))]);
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [(0, Some(h!("A"))), (1, Some(h!("B'")))],
|
||||
txids: [(h!("tx0"), Some(TxHeight::Confirmed(0)))]
|
||||
},)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalidate_a_checkpoint_and_try_and_move_tx_when_it_wasnt_within_invalidation() {
|
||||
let chain1 = chain!(checkpoints: [[0, h!("A")], [1, h!("B")]], txids: [(h!("tx0"), TxHeight::Confirmed(0))]);
|
||||
let chain2 = chain!(checkpoints: [[0, h!("A")], [1, h!("B'")]], txids: [(h!("tx0"), TxHeight::Confirmed(1))]);
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Err(UpdateError::TxInconsistent {
|
||||
txid: h!("tx0"),
|
||||
original_pos: TxHeight::Confirmed(0),
|
||||
update_pos: TxHeight::Confirmed(1),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
/// This test doesn't make much sense. We're invalidating a block at height 1 and moving it to
|
||||
/// height 0. It should be impossible for it to be at height 1 at any point if it was at height 0
|
||||
/// all along.
|
||||
#[test]
|
||||
fn move_invalidated_tx_into_earlier_checkpoint() {
|
||||
let chain1 = chain!(checkpoints: [[0, h!("A")], [1, h!("B")]], txids: [(h!("tx0"), TxHeight::Confirmed(1))]);
|
||||
let chain2 = chain!(checkpoints: [[0, h!("A")], [1, h!("B'")]], txids: [(h!("tx0"), TxHeight::Confirmed(0))]);
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [(1, Some(h!("B'")))],
|
||||
txids: [(h!("tx0"), Some(TxHeight::Confirmed(0)))]
|
||||
},)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalidate_first_and_only_checkpoint_with_tx_move_to_mempool() {
|
||||
let chain1 = chain!(checkpoints: [[0,h!("A")]], txids: [(h!("tx0"), TxHeight::Confirmed(0))]);
|
||||
let chain2 = chain!(checkpoints: [[0,h!("A'")]], txids: [(h!("tx0"), TxHeight::Unconfirmed)]);
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [(0, Some(h!("A'")))],
|
||||
txids: [(h!("tx0"), Some(TxHeight::Unconfirmed))]
|
||||
},)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirm_tx_without_extending_chain() {
|
||||
let chain1 = chain!(checkpoints: [[0,h!("A")]], txids: [(h!("tx0"), TxHeight::Unconfirmed)]);
|
||||
let chain2 = chain!(checkpoints: [[0,h!("A")]], txids: [(h!("tx0"), TxHeight::Confirmed(0))]);
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [],
|
||||
txids: [(h!("tx0"), Some(TxHeight::Confirmed(0)))]
|
||||
},)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirm_tx_backwards_while_extending_chain() {
|
||||
let chain1 = chain!(checkpoints: [[0,h!("A")]], txids: [(h!("tx0"), TxHeight::Unconfirmed)]);
|
||||
let chain2 = chain!(checkpoints: [[0,h!("A")],[1,h!("B")]], txids: [(h!("tx0"), TxHeight::Confirmed(0))]);
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [(1, Some(h!("B")))],
|
||||
txids: [(h!("tx0"), Some(TxHeight::Confirmed(0)))]
|
||||
},)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn confirm_tx_in_new_block() {
|
||||
let chain1 = chain!(checkpoints: [[0,h!("A")]], txids: [(h!("tx0"), TxHeight::Unconfirmed)]);
|
||||
let chain2 = chain! {
|
||||
checkpoints: [[0,h!("A")], [1,h!("B")]],
|
||||
txids: [(h!("tx0"), TxHeight::Confirmed(1))]
|
||||
};
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [(1, Some(h!("B")))],
|
||||
txids: [(h!("tx0"), Some(TxHeight::Confirmed(1)))]
|
||||
},)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn merging_mempool_of_empty_chains_doesnt_fail() {
|
||||
let chain1 = chain!(checkpoints: [], txids: [(h!("tx0"), TxHeight::Unconfirmed)]);
|
||||
let chain2 = chain!(checkpoints: [], txids: [(h!("tx1"), TxHeight::Unconfirmed)]);
|
||||
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [],
|
||||
txids: [(h!("tx1"), Some(TxHeight::Unconfirmed))]
|
||||
},)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_insert_confirmed_tx_without_checkpoints() {
|
||||
let chain = SparseChain::default();
|
||||
assert_eq!(
|
||||
chain.insert_tx_preview(h!("A"), TxHeight::Confirmed(0)),
|
||||
Err(InsertTxError::TxTooHigh {
|
||||
txid: h!("A"),
|
||||
tx_height: 0,
|
||||
tip_height: None
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_chain_can_add_unconfirmed_transactions() {
|
||||
let chain1 = chain!(checkpoints: [[0, h!("A")]], txids: []);
|
||||
let chain2 = chain!(checkpoints: [], txids: [(h!("tx0"), TxHeight::Unconfirmed)]);
|
||||
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [],
|
||||
txids: [ (h!("tx0"), Some(TxHeight::Unconfirmed)) ]
|
||||
},)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_update_with_shorter_chain() {
|
||||
let chain1 = chain!(checkpoints: [[1, h!("B")],[2, h!("C")]], txids: []);
|
||||
let chain2 = chain!(checkpoints: [[1, h!("B")]], txids: [(h!("tx0"), TxHeight::Confirmed(1))]);
|
||||
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [],
|
||||
txids: [(h!("tx0"), Some(TxHeight::Confirmed(1)))]
|
||||
},)
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_introduce_older_checkpoints() {
|
||||
let chain1 = chain!(checkpoints: [[2, h!("C")], [3, h!("D")]], txids: []);
|
||||
let chain2 = chain!(checkpoints: [[1, h!("B")], [2, h!("C")]], txids: []);
|
||||
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [(1, Some(h!("B")))],
|
||||
txids: []
|
||||
},)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fix_blockhash_before_agreement_point() {
|
||||
let chain1 = chain!([0, h!("im-wrong")], [1, h!("we-agree")]);
|
||||
let chain2 = chain!([0, h!("fix")], [1, h!("we-agree")]);
|
||||
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [(0, Some(h!("fix")))],
|
||||
txids: []
|
||||
},)
|
||||
)
|
||||
}
|
||||
|
||||
// TODO: Use macro
|
||||
#[test]
|
||||
fn cannot_change_ext_index_of_confirmed_tx() {
|
||||
let chain1 = chain!(
|
||||
index: TestIndex,
|
||||
checkpoints: [[1, h!("A")]],
|
||||
txids: [(h!("tx0"), TestIndex(TxHeight::Confirmed(1), 10))]
|
||||
);
|
||||
let chain2 = chain!(
|
||||
index: TestIndex,
|
||||
checkpoints: [[1, h!("A")]],
|
||||
txids: [(h!("tx0"), TestIndex(TxHeight::Confirmed(1), 20))]
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Err(UpdateError::TxInconsistent {
|
||||
txid: h!("tx0"),
|
||||
original_pos: TestIndex(TxHeight::Confirmed(1), 10),
|
||||
update_pos: TestIndex(TxHeight::Confirmed(1), 20),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn can_change_index_of_unconfirmed_tx() {
|
||||
let chain1 = chain!(
|
||||
index: TestIndex,
|
||||
checkpoints: [[1, h!("A")]],
|
||||
txids: [(h!("tx1"), TestIndex(TxHeight::Unconfirmed, 10))]
|
||||
);
|
||||
let chain2 = chain!(
|
||||
index: TestIndex,
|
||||
checkpoints: [[1, h!("A")]],
|
||||
txids: [(h!("tx1"), TestIndex(TxHeight::Unconfirmed, 20))]
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(ChangeSet {
|
||||
checkpoints: [].into(),
|
||||
txids: [(h!("tx1"), Some(TestIndex(TxHeight::Unconfirmed, 20)),)].into()
|
||||
},),
|
||||
)
|
||||
}
|
||||
|
||||
/// B and C are in both chain and update
|
||||
/// ```
|
||||
/// | 0 | 1 | 2 | 3 | 4
|
||||
/// chain | B C
|
||||
/// update | A B C D
|
||||
/// ```
|
||||
/// This should succeed with the point of agreement being C and A should be added in addition.
|
||||
#[test]
|
||||
fn two_points_of_agreement() {
|
||||
let chain1 = chain!([1, h!("B")], [2, h!("C")]);
|
||||
let chain2 = chain!([0, h!("A")], [1, h!("B")], [2, h!("C")], [3, h!("D")]);
|
||||
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [(0, Some(h!("A"))), (3, Some(h!("D")))]
|
||||
},),
|
||||
);
|
||||
}
|
||||
|
||||
/// Update and chain does not connect:
|
||||
/// ```
|
||||
/// | 0 | 1 | 2 | 3 | 4
|
||||
/// chain | B C
|
||||
/// update | A B D
|
||||
/// ```
|
||||
/// This should fail as we cannot figure out whether C & D are on the same chain
|
||||
#[test]
|
||||
fn update_and_chain_does_not_connect() {
|
||||
let chain1 = chain!([1, h!("B")], [2, h!("C")]);
|
||||
let chain2 = chain!([0, h!("A")], [1, h!("B")], [3, h!("D")]);
|
||||
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Err(UpdateError::NotConnected(2)),
|
||||
);
|
||||
}
|
||||
|
||||
/// Transient invalidation:
|
||||
/// ```
|
||||
/// | 0 | 1 | 2 | 3 | 4 | 5
|
||||
/// chain | A B C E
|
||||
/// update | A B' C' D
|
||||
/// ```
|
||||
/// This should succeed and invalidate B,C and E with point of agreement being A.
|
||||
/// It should also invalidate transactions at height 1.
|
||||
#[test]
|
||||
fn transitive_invalidation_applies_to_checkpoints_higher_than_invalidation() {
|
||||
let chain1 = chain! {
|
||||
checkpoints: [[0, h!("A")], [2, h!("B")], [3, h!("C")], [5, h!("E")]],
|
||||
txids: [
|
||||
(h!("a"), TxHeight::Confirmed(0)),
|
||||
(h!("b1"), TxHeight::Confirmed(1)),
|
||||
(h!("b2"), TxHeight::Confirmed(2)),
|
||||
(h!("d"), TxHeight::Confirmed(3)),
|
||||
(h!("e"), TxHeight::Confirmed(5))
|
||||
]
|
||||
};
|
||||
let chain2 = chain! {
|
||||
checkpoints: [[0, h!("A")], [2, h!("B'")], [3, h!("C'")], [4, h!("D")]],
|
||||
txids: [(h!("b1"), TxHeight::Confirmed(4)), (h!("b2"), TxHeight::Confirmed(3))]
|
||||
};
|
||||
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [
|
||||
(2, Some(h!("B'"))),
|
||||
(3, Some(h!("C'"))),
|
||||
(4, Some(h!("D"))),
|
||||
(5, None)
|
||||
],
|
||||
txids: [
|
||||
(h!("b1"), Some(TxHeight::Confirmed(4))),
|
||||
(h!("b2"), Some(TxHeight::Confirmed(3))),
|
||||
(h!("d"), Some(TxHeight::Unconfirmed)),
|
||||
(h!("e"), Some(TxHeight::Unconfirmed))
|
||||
]
|
||||
},)
|
||||
);
|
||||
}
|
||||
|
||||
/// Transient invalidation:
|
||||
/// ```
|
||||
/// | 0 | 1 | 2 | 3 | 4
|
||||
/// chain | B C E
|
||||
/// update | B' C' D
|
||||
/// ```
|
||||
///
|
||||
/// This should succeed and invalidate B, C and E with no point of agreement
|
||||
#[test]
|
||||
fn transitive_invalidation_applies_to_checkpoints_higher_than_invalidation_no_point_of_agreement() {
|
||||
let chain1 = chain!([1, h!("B")], [2, h!("C")], [4, h!("E")]);
|
||||
let chain2 = chain!([1, h!("B'")], [2, h!("C'")], [3, h!("D")]);
|
||||
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [
|
||||
(1, Some(h!("B'"))),
|
||||
(2, Some(h!("C'"))),
|
||||
(3, Some(h!("D"))),
|
||||
(4, None)
|
||||
]
|
||||
},)
|
||||
)
|
||||
}
|
||||
|
||||
/// Transient invalidation:
|
||||
/// ```
|
||||
/// | 0 | 1 | 2 | 3 | 4
|
||||
/// chain | A B C E
|
||||
/// update | B' C' D
|
||||
/// ```
|
||||
///
|
||||
/// This should fail since although it tells us that B and C are invalid it doesn't tell us whether
|
||||
/// A was invalid.
|
||||
#[test]
|
||||
fn invalidation_but_no_connection() {
|
||||
let chain1 = chain!([0, h!("A")], [1, h!("B")], [2, h!("C")], [4, h!("E")]);
|
||||
let chain2 = chain!([1, h!("B'")], [2, h!("C'")], [3, h!("D")]);
|
||||
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Err(UpdateError::NotConnected(0))
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn checkpoint_limit_is_respected() {
|
||||
let mut chain1 = SparseChain::default();
|
||||
let _ = chain1
|
||||
.apply_update(chain!(
|
||||
[1, h!("A")],
|
||||
[2, h!("B")],
|
||||
[3, h!("C")],
|
||||
[4, h!("D")],
|
||||
[5, h!("E")]
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(chain1.checkpoints().len(), 5);
|
||||
chain1.set_checkpoint_limit(Some(4));
|
||||
assert_eq!(chain1.checkpoints().len(), 4);
|
||||
|
||||
let _ = chain1
|
||||
.insert_checkpoint(BlockId {
|
||||
height: 6,
|
||||
hash: h!("F"),
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(chain1.checkpoints().len(), 4);
|
||||
|
||||
let changeset = chain1.determine_changeset(&chain!([6, h!("F")], [7, h!("G")]));
|
||||
assert_eq!(changeset, Ok(changeset!(checkpoints: [(7, Some(h!("G")))])));
|
||||
|
||||
chain1.apply_changeset(changeset.unwrap());
|
||||
|
||||
assert_eq!(chain1.checkpoints().len(), 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn range_txids_by_height() {
|
||||
let mut chain = chain!(index: TestIndex, checkpoints: [[1, h!("block 1")], [2, h!("block 2")]]);
|
||||
|
||||
let txids: [(TestIndex, Txid); 4] = [
|
||||
(
|
||||
TestIndex(TxHeight::Confirmed(1), u32::MIN),
|
||||
Txid::from_inner([0x00; 32]),
|
||||
),
|
||||
(
|
||||
TestIndex(TxHeight::Confirmed(1), u32::MAX),
|
||||
Txid::from_inner([0xfe; 32]),
|
||||
),
|
||||
(
|
||||
TestIndex(TxHeight::Confirmed(2), u32::MIN),
|
||||
Txid::from_inner([0x01; 32]),
|
||||
),
|
||||
(
|
||||
TestIndex(TxHeight::Confirmed(2), u32::MAX),
|
||||
Txid::from_inner([0xff; 32]),
|
||||
),
|
||||
];
|
||||
|
||||
// populate chain with txids
|
||||
for (index, txid) in txids {
|
||||
let _ = chain.insert_tx(txid, index).expect("should succeed");
|
||||
}
|
||||
|
||||
// inclusive start
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids_by_height(TxHeight::Confirmed(1)..)
|
||||
.collect::<Vec<_>>(),
|
||||
txids.iter().collect::<Vec<_>>(),
|
||||
);
|
||||
|
||||
// exclusive start
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids_by_height((Bound::Excluded(TxHeight::Confirmed(1)), Bound::Unbounded,))
|
||||
.collect::<Vec<_>>(),
|
||||
txids[2..].iter().collect::<Vec<_>>(),
|
||||
);
|
||||
|
||||
// inclusive end
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids_by_height((Bound::Unbounded, Bound::Included(TxHeight::Confirmed(2))))
|
||||
.collect::<Vec<_>>(),
|
||||
txids[..4].iter().collect::<Vec<_>>(),
|
||||
);
|
||||
|
||||
// exclusive end
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids_by_height(..TxHeight::Confirmed(2))
|
||||
.collect::<Vec<_>>(),
|
||||
txids[..2].iter().collect::<Vec<_>>(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn range_txids_by_index() {
|
||||
let mut chain = chain!(index: TestIndex, checkpoints: [[1, h!("block 1")],[2, h!("block 2")]]);
|
||||
|
||||
let txids: [(TestIndex, Txid); 4] = [
|
||||
(TestIndex(TxHeight::Confirmed(1), u32::MIN), h!("tx 1 min")),
|
||||
(TestIndex(TxHeight::Confirmed(1), u32::MAX), h!("tx 1 max")),
|
||||
(TestIndex(TxHeight::Confirmed(2), u32::MIN), h!("tx 2 min")),
|
||||
(TestIndex(TxHeight::Confirmed(2), u32::MAX), h!("tx 2 max")),
|
||||
];
|
||||
|
||||
// populate chain with txids
|
||||
for (index, txid) in txids {
|
||||
let _ = chain.insert_tx(txid, index).expect("should succeed");
|
||||
}
|
||||
|
||||
// inclusive start
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids_by_position(TestIndex(TxHeight::Confirmed(1), u32::MIN)..)
|
||||
.collect::<Vec<_>>(),
|
||||
txids.iter().collect::<Vec<_>>(),
|
||||
);
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids_by_position(TestIndex(TxHeight::Confirmed(1), u32::MAX)..)
|
||||
.collect::<Vec<_>>(),
|
||||
txids[1..].iter().collect::<Vec<_>>(),
|
||||
);
|
||||
|
||||
// exclusive start
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids_by_position((
|
||||
Bound::Excluded(TestIndex(TxHeight::Confirmed(1), u32::MIN)),
|
||||
Bound::Unbounded
|
||||
))
|
||||
.collect::<Vec<_>>(),
|
||||
txids[1..].iter().collect::<Vec<_>>(),
|
||||
);
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids_by_position((
|
||||
Bound::Excluded(TestIndex(TxHeight::Confirmed(1), u32::MAX)),
|
||||
Bound::Unbounded
|
||||
))
|
||||
.collect::<Vec<_>>(),
|
||||
txids[2..].iter().collect::<Vec<_>>(),
|
||||
);
|
||||
|
||||
// inclusive end
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids_by_position((
|
||||
Bound::Unbounded,
|
||||
Bound::Included(TestIndex(TxHeight::Confirmed(2), u32::MIN))
|
||||
))
|
||||
.collect::<Vec<_>>(),
|
||||
txids[..3].iter().collect::<Vec<_>>(),
|
||||
);
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids_by_position((
|
||||
Bound::Unbounded,
|
||||
Bound::Included(TestIndex(TxHeight::Confirmed(2), u32::MAX))
|
||||
))
|
||||
.collect::<Vec<_>>(),
|
||||
txids[..4].iter().collect::<Vec<_>>(),
|
||||
);
|
||||
|
||||
// exclusive end
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids_by_position(..TestIndex(TxHeight::Confirmed(2), u32::MIN))
|
||||
.collect::<Vec<_>>(),
|
||||
txids[..2].iter().collect::<Vec<_>>(),
|
||||
);
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids_by_position(..TestIndex(TxHeight::Confirmed(2), u32::MAX))
|
||||
.collect::<Vec<_>>(),
|
||||
txids[..3].iter().collect::<Vec<_>>(),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn range_txids() {
|
||||
let mut chain = SparseChain::default();
|
||||
|
||||
let txids = (0..100)
|
||||
.map(|v| Txid::hash(v.to_string().as_bytes()))
|
||||
.collect::<BTreeSet<Txid>>();
|
||||
|
||||
// populate chain
|
||||
for txid in &txids {
|
||||
let _ = chain
|
||||
.insert_tx(*txid, TxHeight::Unconfirmed)
|
||||
.expect("should succeed");
|
||||
}
|
||||
|
||||
for txid in &txids {
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids((TxHeight::Unconfirmed, *txid)..)
|
||||
.map(|(_, txid)| txid)
|
||||
.collect::<Vec<_>>(),
|
||||
txids.range(*txid..).collect::<Vec<_>>(),
|
||||
"range with inclusive start should succeed"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids((
|
||||
Bound::Excluded((TxHeight::Unconfirmed, *txid)),
|
||||
Bound::Unbounded,
|
||||
))
|
||||
.map(|(_, txid)| txid)
|
||||
.collect::<Vec<_>>(),
|
||||
txids
|
||||
.range((Bound::Excluded(*txid), Bound::Unbounded,))
|
||||
.collect::<Vec<_>>(),
|
||||
"range with exclusive start should succeed"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids(..(TxHeight::Unconfirmed, *txid))
|
||||
.map(|(_, txid)| txid)
|
||||
.collect::<Vec<_>>(),
|
||||
txids.range(..*txid).collect::<Vec<_>>(),
|
||||
"range with exclusive end should succeed"
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
chain
|
||||
.range_txids((
|
||||
Bound::Included((TxHeight::Unconfirmed, *txid)),
|
||||
Bound::Unbounded,
|
||||
))
|
||||
.map(|(_, txid)| txid)
|
||||
.collect::<Vec<_>>(),
|
||||
txids
|
||||
.range((Bound::Included(*txid), Bound::Unbounded,))
|
||||
.collect::<Vec<_>>(),
|
||||
"range with inclusive end should succeed"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalidated_txs_move_to_unconfirmed() {
|
||||
let chain1 = chain! {
|
||||
checkpoints: [[0, h!("A")], [1, h!("B")], [2, h!("C")]],
|
||||
txids: [
|
||||
(h!("a"), TxHeight::Confirmed(0)),
|
||||
(h!("b"), TxHeight::Confirmed(1)),
|
||||
(h!("c"), TxHeight::Confirmed(2)),
|
||||
(h!("d"), TxHeight::Unconfirmed)
|
||||
]
|
||||
};
|
||||
|
||||
let chain2 = chain!([0, h!("A")], [1, h!("B'")]);
|
||||
|
||||
assert_eq!(
|
||||
chain1.determine_changeset(&chain2),
|
||||
Ok(changeset! {
|
||||
checkpoints: [
|
||||
(1, Some(h!("B'"))),
|
||||
(2, None)
|
||||
],
|
||||
txids: [
|
||||
(h!("b"), Some(TxHeight::Unconfirmed)),
|
||||
(h!("c"), Some(TxHeight::Unconfirmed))
|
||||
]
|
||||
},)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn change_tx_position_from_unconfirmed_to_confirmed() {
|
||||
let mut chain = SparseChain::<TxHeight>::default();
|
||||
let txid = h!("txid");
|
||||
|
||||
let _ = chain.insert_tx(txid, TxHeight::Unconfirmed).unwrap();
|
||||
|
||||
assert_eq!(chain.tx_position(txid), Some(&TxHeight::Unconfirmed));
|
||||
let _ = chain
|
||||
.insert_checkpoint(BlockId {
|
||||
height: 0,
|
||||
hash: h!("0"),
|
||||
})
|
||||
.unwrap();
|
||||
let _ = chain.insert_tx(txid, TxHeight::Confirmed(0)).unwrap();
|
||||
|
||||
assert_eq!(chain.tx_position(txid), Some(&TxHeight::Confirmed(0)));
|
||||
}
|
||||
@@ -4,7 +4,7 @@ use bdk_chain::{
|
||||
collections::*,
|
||||
local_chain::LocalChain,
|
||||
tx_graph::{Additions, TxGraph},
|
||||
Append, BlockId, ConfirmationHeightAnchor, ObservedAs,
|
||||
Append, BlockId, ChainPosition, ConfirmationHeightAnchor,
|
||||
};
|
||||
use bitcoin::{
|
||||
hashes::Hash, BlockHash, OutPoint, PackedLockTime, Script, Transaction, TxIn, TxOut, Txid,
|
||||
@@ -56,17 +56,17 @@ fn insert_txouts() {
|
||||
};
|
||||
|
||||
// Conf anchor used to mark the full transaction as confirmed.
|
||||
let conf_anchor = ObservedAs::Confirmed(BlockId {
|
||||
let conf_anchor = ChainPosition::Confirmed(BlockId {
|
||||
height: 100,
|
||||
hash: h!("random blockhash"),
|
||||
});
|
||||
|
||||
// Unconfirmed anchor to mark the partial transactions as unconfirmed
|
||||
let unconf_anchor = ObservedAs::<BlockId>::Unconfirmed(1000000);
|
||||
let unconf_anchor = ChainPosition::<BlockId>::Unconfirmed(1000000);
|
||||
|
||||
// Make the original graph
|
||||
let mut graph = {
|
||||
let mut graph = TxGraph::<ObservedAs<BlockId>>::default();
|
||||
let mut graph = TxGraph::<ChainPosition<BlockId>>::default();
|
||||
for (outpoint, txout) in &original_ops {
|
||||
assert_eq!(
|
||||
graph.insert_txout(*outpoint, txout.clone()),
|
||||
@@ -707,7 +707,7 @@ fn test_chain_spends() {
|
||||
assert_eq!(
|
||||
graph.get_chain_spend(&local_chain, tip, OutPoint::new(tx_0.txid(), 0)),
|
||||
Some((
|
||||
ObservedAs::Confirmed(&ConfirmationHeightAnchor {
|
||||
ChainPosition::Confirmed(&ConfirmationHeightAnchor {
|
||||
anchor_block: tip,
|
||||
confirmation_height: 98
|
||||
}),
|
||||
@@ -719,7 +719,7 @@ fn test_chain_spends() {
|
||||
assert_eq!(
|
||||
graph.get_chain_position(&local_chain, tip, tx_0.txid()),
|
||||
// Some(ObservedAs::Confirmed(&local_chain.get_block(95).expect("block expected"))),
|
||||
Some(ObservedAs::Confirmed(&ConfirmationHeightAnchor {
|
||||
Some(ChainPosition::Confirmed(&ConfirmationHeightAnchor {
|
||||
anchor_block: tip,
|
||||
confirmation_height: 95
|
||||
}))
|
||||
@@ -728,7 +728,7 @@ fn test_chain_spends() {
|
||||
// Even if unconfirmed tx has a last_seen of 0, it can still be part of a chain spend.
|
||||
assert_eq!(
|
||||
graph.get_chain_spend(&local_chain, tip, OutPoint::new(tx_0.txid(), 1)),
|
||||
Some((ObservedAs::Unconfirmed(0), tx_2.txid())),
|
||||
Some((ChainPosition::Unconfirmed(0), tx_2.txid())),
|
||||
);
|
||||
|
||||
// Mark the unconfirmed as seen and check correct ObservedAs status is returned.
|
||||
@@ -739,7 +739,7 @@ fn test_chain_spends() {
|
||||
graph
|
||||
.get_chain_spend(&local_chain, tip, OutPoint::new(tx_0.txid(), 1))
|
||||
.unwrap(),
|
||||
(ObservedAs::Unconfirmed(1234567), tx_2.txid())
|
||||
(ChainPosition::Unconfirmed(1234567), tx_2.txid())
|
||||
);
|
||||
|
||||
// A conflicting transaction that conflicts with tx_1.
|
||||
@@ -775,7 +775,7 @@ fn test_chain_spends() {
|
||||
graph
|
||||
.get_chain_position(&local_chain, tip, tx_2_conflict.txid())
|
||||
.expect("position expected"),
|
||||
ObservedAs::Unconfirmed(1234568)
|
||||
ChainPosition::Unconfirmed(1234568)
|
||||
);
|
||||
|
||||
// Chain_spend now catches the new transaction as the spend.
|
||||
@@ -783,7 +783,7 @@ fn test_chain_spends() {
|
||||
graph
|
||||
.get_chain_spend(&local_chain, tip, OutPoint::new(tx_0.txid(), 1))
|
||||
.expect("expect observation"),
|
||||
(ObservedAs::Unconfirmed(1234568), tx_2_conflict.txid())
|
||||
(ChainPosition::Unconfirmed(1234568), tx_2_conflict.txid())
|
||||
);
|
||||
|
||||
// Chain position of the `tx_2` is now none, as it is older than `tx_2_conflict`
|
||||
|
||||
Reference in New Issue
Block a user