Skip to content

Commit

Permalink
chore: add Anchor trait restriction for ChainPosition
Browse files Browse the repository at this point in the history
  • Loading branch information
yanganto committed Feb 6, 2024
1 parent 7aca884 commit a223c0c
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
10 changes: 4 additions & 6 deletions crates/chain/src/chain_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ use bitcoin::{hashes::Hash, BlockHash, OutPoint, TxOut, Txid};
use crate::{Anchor, AnchorFromBlockPosition, COINBASE_MATURITY};

/// 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 ChainPosition<A> {
pub enum ChainPosition<A: Anchor> {
/// 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> ChainPosition<A> {
impl<A: Anchor> ChainPosition<A> {
/// Returns whether [`ChainPosition`] is confirmed or not.
pub fn is_confirmed(&self) -> bool {
matches!(self, Self::Confirmed(_))
}
}

impl<A: Clone> ChainPosition<&A> {
impl<A: Clone + Anchor> ChainPosition<&A> {
/// Maps a [`ChainPosition<&A>`] into a [`ChainPosition<A>`] by cloning the contents.
pub fn cloned(self) -> ChainPosition<A> {
match self {
Expand Down Expand Up @@ -228,7 +226,7 @@ impl AnchorFromBlockPosition for ConfirmationTimeHeightAnchor {

/// A `TxOut` with as much data as we can retrieve about it
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct FullTxOut<A> {
pub struct FullTxOut<A: Anchor> {
/// The location of the `TxOut`.
pub outpoint: OutPoint,
/// The `TxOut`.
Expand Down
2 changes: 1 addition & 1 deletion crates/chain/src/tx_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ impl Default for TxNodeInternal {

/// A transaction that is included in the chain, or is still in mempool.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct CanonicalTx<'a, T, A> {
pub struct CanonicalTx<'a, T, A: Anchor> {
/// How the transaction is observed as (confirmed or unconfirmed).
pub chain_position: ChainPosition<&'a A>,
/// The transaction node (as part of the graph).
Expand Down

0 comments on commit a223c0c

Please sign in to comment.