Skip to content

Commit

Permalink
feat(chain): add AnchorFromBlockPosition trait
Browse files Browse the repository at this point in the history
This is useful for block-by-block chain sources. We can determine the
tx's anchor based on the block, block height and tx position in the
block.
  • Loading branch information
evanlinjin committed Oct 9, 2023
1 parent f795a43 commit b3db5ca
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
28 changes: 27 additions & 1 deletion crates/chain/src/chain_data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bitcoin::{hashes::Hash, BlockHash, OutPoint, TxOut, Txid};

use crate::{Anchor, COINBASE_MATURITY};
use crate::{Anchor, AnchorFromBlockPosition, COINBASE_MATURITY};

/// Represents the observed position of some chain data.
///
Expand Down Expand Up @@ -109,6 +109,12 @@ impl Anchor for BlockId {
}
}

impl AnchorFromBlockPosition for BlockId {
fn from_block_position(_block: &bitcoin::Block, block_id: BlockId, _tx_pos: usize) -> Self {
block_id
}
}

impl Default for BlockId {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -168,6 +174,15 @@ impl Anchor for ConfirmationHeightAnchor {
}
}

impl AnchorFromBlockPosition for ConfirmationHeightAnchor {
fn from_block_position(_block: &bitcoin::Block, block_id: BlockId, _tx_pos: usize) -> Self {
Self {
anchor_block: block_id,
confirmation_height: block_id.height,
}
}
}

/// An [`Anchor`] implementation that also records the exact confirmation time and height of the
/// transaction.
///
Expand Down Expand Up @@ -196,6 +211,17 @@ impl Anchor for ConfirmationTimeAnchor {
self.confirmation_height
}
}

impl AnchorFromBlockPosition for ConfirmationTimeAnchor {
fn from_block_position(block: &bitcoin::Block, block_id: BlockId, _tx_pos: usize) -> Self {
Self {
anchor_block: block_id,
confirmation_height: block_id.height,
confirmation_time: block.header.time as _,
}
}
}

/// A `TxOut` with as much data as we can retrieve about it
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct FullTxOut<A> {
Expand Down
9 changes: 8 additions & 1 deletion crates/chain/src/tx_data_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,19 @@ pub trait Anchor: core::fmt::Debug + Clone + Eq + PartialOrd + Ord + core::hash:
}
}

impl<A: Anchor> Anchor for &'static A {
impl<'a, A: Anchor> Anchor for &'a A {
fn anchor_block(&self) -> BlockId {
<A as Anchor>::anchor_block(self)
}
}

/// An [`Anchor`] that can be constructed from a given block, block height and transaction position
/// within the block.
pub trait AnchorFromBlockPosition: Anchor {
/// Construct the anchor from a given `block`, block height and `tx_pos` within the block.
fn from_block_position(block: &bitcoin::Block, block_id: BlockId, tx_pos: usize) -> Self;
}

/// Trait that makes an object appendable.
pub trait Append {
/// Append another object of the same type onto `self`.
Expand Down

0 comments on commit b3db5ca

Please sign in to comment.