#[cfg(feature = "std")]
impl<P: core::fmt::Debug> std::error::Error for UpdateError<P> {}
-impl<P> SparseChain<P> {
+impl<P: ChainPosition> SparseChain<P> {
/// Creates a new chain from a list of block hashes and heights. The caller must guarantee they
/// are in the same chain.
pub fn from_checkpoints<C>(checkpoints: C) -> Self
.map(|&hash| BlockId { height, hash })
}
+ /// Return the [`ChainPosition`] of a `txid`.
+ ///
+ /// This returns [`None`] if the transaction does not exist.
+ pub fn tx_position(&self, txid: Txid) -> Option<&P> {
+ self.txid_to_pos.get(&txid)
+ }
+
/// Return a [`BTreeMap`] of all checkpoints (block hashes by height).
pub fn checkpoints(&self) -> &BTreeMap<u32, BlockHash> {
&self.checkpoints
.map(|(&height, &hash)| BlockId { height, hash })
}
- /// Returns the value set as the checkpoint limit.
- ///
- /// Refer to [`set_checkpoint_limit`].
- ///
- /// [`set_checkpoint_limit`]: Self::set_checkpoint_limit
- pub fn checkpoint_limit(&self) -> Option<usize> {
- self.checkpoint_limit
- }
-
- /// Set the checkpoint limit.
- ///
- /// The checkpoint limit restricts the number of checkpoints that can be stored in [`Self`].
- /// Oldest checkpoints are pruned first.
- pub fn set_checkpoint_limit(&mut self, limit: Option<usize>) {
- self.checkpoint_limit = limit;
- self.prune_checkpoints();
- }
-
- fn prune_checkpoints(&mut self) -> Option<BTreeMap<u32, BlockHash>> {
- let limit = self.checkpoint_limit?;
-
- // find the last height to be pruned
- let last_height = *self.checkpoints.keys().rev().nth(limit)?;
- // first height to be kept
- let keep_height = last_height + 1;
-
- let mut split = self.checkpoints.split_off(&keep_height);
- core::mem::swap(&mut self.checkpoints, &mut split);
-
- Some(split)
- }
-}
-
-impl<P: ChainPosition> SparseChain<P> {
- /// Return the [`ChainPosition`] of a `txid`.
- ///
- /// This returns [`None`] if the transaction does not exist.
- pub fn tx_position(&self, txid: Txid) -> Option<&P> {
- self.txid_to_pos.get(&txid)
- }
-
/// Preview changes of updating [`Self`] with another chain that connects to it.
///
/// If the `update` wishes to introduce confirmed transactions, it must contain a checkpoint
/// Attempt to retrieve a [`FullTxOut`] of the given `outpoint`.
///
/// This will return `Some` only if the output's transaction is in both `self` and `graph`.
- pub fn full_txout<A>(&self, graph: &TxGraph<A>, outpoint: OutPoint) -> Option<FullTxOut<P>> {
+ pub fn full_txout(&self, graph: &TxGraph, outpoint: OutPoint) -> Option<FullTxOut<P>> {
let chain_pos = self.tx_position(outpoint.txid)?;
let tx = graph.get_tx(outpoint.txid)?;
})
}
+ /// Returns the value set as the checkpoint limit.
+ ///
+ /// Refer to [`set_checkpoint_limit`].
+ ///
+ /// [`set_checkpoint_limit`]: Self::set_checkpoint_limit
+ pub fn checkpoint_limit(&self) -> Option<usize> {
+ self.checkpoint_limit
+ }
+
+ /// Set the checkpoint limit.
+ ///
+ /// The checkpoint limit restricts the number of checkpoints that can be stored in [`Self`].
+ /// Oldest checkpoints are pruned first.
+ pub fn set_checkpoint_limit(&mut self, limit: Option<usize>) {
+ self.checkpoint_limit = limit;
+ self.prune_checkpoints();
+ }
+
/// Return [`Txid`]s that would be added to the sparse chain if this `changeset` was applied.
pub fn changeset_additions<'a>(
&'a self,
.map(|(&txid, _)| txid)
}
+ fn prune_checkpoints(&mut self) -> Option<BTreeMap<u32, BlockHash>> {
+ let limit = self.checkpoint_limit?;
+
+ // find the last height to be pruned
+ let last_height = *self.checkpoints.keys().rev().nth(limit)?;
+ // first height to be kept
+ let keep_height = last_height + 1;
+
+ let mut split = self.checkpoints.split_off(&keep_height);
+ core::mem::swap(&mut self.checkpoints, &mut split);
+
+ Some(split)
+ }
+
/// Finds the transaction in the chain that spends `outpoint`.
///
/// [`TxGraph`] is used to provide the spend relationships.
///
/// Note that the transaction including `outpoint` does not need to be in the `graph` or the
/// `chain` for this to return `Some`.
- pub fn spent_by<A>(&self, graph: &TxGraph<A>, outpoint: OutPoint) -> Option<(&P, Txid)> {
+ pub fn spent_by(&self, graph: &TxGraph, outpoint: OutPoint) -> Option<(&P, Txid)> {
graph
- .output_spends(outpoint)
+ .outspends(outpoint)
.iter()
.find_map(|&txid| Some((self.tx_position(txid)?, txid)))
}