2023-04-05 18:17:08 +08:00
|
|
|
use bitcoin::{Block, BlockHash, OutPoint, Transaction, TxOut};
|
2023-03-24 09:23:36 +08:00
|
|
|
|
|
|
|
use crate::BlockId;
|
2023-03-01 11:09:08 +01:00
|
|
|
|
|
|
|
/// Trait to do something with every txout contained in a structure.
|
|
|
|
///
|
2023-03-10 23:23:29 +05:30
|
|
|
/// We would prefer to just work with things that can give us an `Iterator<Item=(OutPoint, &TxOut)>`
|
|
|
|
/// here, but rust's type system makes it extremely hard to do this (without trait objects).
|
2023-03-01 11:09:08 +01:00
|
|
|
pub trait ForEachTxOut {
|
2023-03-10 23:23:29 +05:30
|
|
|
/// The provided closure `f` will be called with each `outpoint/txout` pair.
|
2023-03-01 11:09:08 +01:00
|
|
|
fn for_each_txout(&self, f: impl FnMut((OutPoint, &TxOut)));
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ForEachTxOut for Block {
|
|
|
|
fn for_each_txout(&self, mut f: impl FnMut((OutPoint, &TxOut))) {
|
|
|
|
for tx in self.txdata.iter() {
|
|
|
|
tx.for_each_txout(&mut f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 11:39:25 +13:00
|
|
|
impl ForEachTxOut for Transaction {
|
2023-03-01 11:09:08 +01:00
|
|
|
fn for_each_txout(&self, mut f: impl FnMut((OutPoint, &TxOut))) {
|
2023-03-08 11:39:25 +13:00
|
|
|
let txid = self.txid();
|
|
|
|
for (i, txout) in self.output.iter().enumerate() {
|
2023-03-01 11:09:08 +01:00
|
|
|
f((
|
|
|
|
OutPoint {
|
|
|
|
txid,
|
|
|
|
vout: i as u32,
|
|
|
|
},
|
|
|
|
txout,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 09:23:36 +08:00
|
|
|
|
|
|
|
/// Trait that "anchors" blockchain data in a specific block of height and hash.
|
|
|
|
///
|
|
|
|
/// This trait is typically associated with blockchain data such as transactions.
|
|
|
|
pub trait BlockAnchor:
|
|
|
|
core::fmt::Debug + Clone + Eq + PartialOrd + Ord + core::hash::Hash + Send + Sync + 'static
|
|
|
|
{
|
|
|
|
/// Returns the [`BlockId`] that the associated blockchain data is "anchored" in.
|
|
|
|
fn anchor_block(&self) -> BlockId;
|
|
|
|
}
|
|
|
|
|
2023-03-24 15:47:39 +08:00
|
|
|
impl<A: BlockAnchor> BlockAnchor for &'static A {
|
|
|
|
fn anchor_block(&self) -> BlockId {
|
|
|
|
<A as BlockAnchor>::anchor_block(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-24 09:23:36 +08:00
|
|
|
impl BlockAnchor for (u32, BlockHash) {
|
|
|
|
fn anchor_block(&self) -> BlockId {
|
|
|
|
(*self).into()
|
|
|
|
}
|
|
|
|
}
|
2023-03-24 15:47:39 +08:00
|
|
|
|
2023-04-05 17:29:20 +08:00
|
|
|
/// Trait that makes an object appendable.
|
|
|
|
pub trait Append {
|
|
|
|
/// Append another object of the same type onto `self`.
|
|
|
|
fn append(&mut self, other: Self);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Append for () {
|
|
|
|
fn append(&mut self, _other: Self) {}
|
|
|
|
}
|
|
|
|
|
2023-03-24 15:47:39 +08:00
|
|
|
/// Represents an index of transaction data.
|
|
|
|
pub trait TxIndex {
|
|
|
|
/// The resultant "additions" when new transaction data is indexed.
|
2023-03-31 12:39:00 +08:00
|
|
|
type Additions;
|
2023-03-26 11:24:30 +08:00
|
|
|
|
2023-03-24 15:47:39 +08:00
|
|
|
/// Scan and index the given `outpoint` and `txout`.
|
|
|
|
fn index_txout(&mut self, outpoint: OutPoint, txout: &TxOut) -> Self::Additions;
|
|
|
|
|
|
|
|
/// Scan and index the given transaction.
|
2023-03-31 12:39:00 +08:00
|
|
|
fn index_tx(&mut self, tx: &Transaction) -> Self::Additions;
|
2023-03-24 15:47:39 +08:00
|
|
|
|
2023-03-27 15:36:37 +08:00
|
|
|
/// Apply additions to itself.
|
|
|
|
fn apply_additions(&mut self, additions: Self::Additions);
|
|
|
|
|
2023-04-05 18:17:08 +08:00
|
|
|
/// Returns whether the txout is marked as relevant in the index.
|
|
|
|
fn is_txout_relevant(&self, outpoint: OutPoint, txout: &TxOut) -> bool;
|
2023-03-26 11:24:30 +08:00
|
|
|
|
2023-04-05 18:17:08 +08:00
|
|
|
/// Returns whether the transaction is marked as relevant in the index.
|
|
|
|
fn is_tx_relevant(&self, tx: &Transaction) -> bool;
|
2023-03-24 15:47:39 +08:00
|
|
|
}
|