2023-03-24 09:23:36 +08:00
|
|
|
use bitcoin::{Block, BlockHash, OutPoint, Transaction, TxOut};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BlockAnchor for (u32, BlockHash) {
|
|
|
|
fn anchor_block(&self) -> BlockId {
|
|
|
|
(*self).into()
|
|
|
|
}
|
|
|
|
}
|