mithril_cardano_node_chain/entities/
scanned_block.rs

1use pallas_traverse::MultiEraBlock;
2use std::fmt::{Debug, Formatter};
3
4use mithril_common::entities::{
5    BlockNumber, CardanoTransaction, ChainPoint, SlotNumber, TransactionHash,
6};
7
8/// A block scanned from a Cardano database
9#[derive(Clone, PartialEq)]
10pub struct ScannedBlock {
11    /// Block hash
12    pub block_hash: Vec<u8>,
13    /// Block number
14    pub block_number: BlockNumber,
15    /// Slot number of the block
16    pub slot_number: SlotNumber,
17    /// Hashes of the transactions in the block
18    pub transactions_hashes: Vec<TransactionHash>,
19}
20
21impl ScannedBlock {
22    /// Scanned block factory
23    pub fn new<B: Into<Vec<u8>>, T: Into<TransactionHash>>(
24        block_hash: B,
25        block_number: BlockNumber,
26        slot_number: SlotNumber,
27        transaction_hashes: Vec<T>,
28    ) -> Self {
29        Self {
30            block_hash: block_hash.into(),
31            block_number,
32            slot_number,
33            transactions_hashes: transaction_hashes.into_iter().map(|h| h.into()).collect(),
34        }
35    }
36
37    pub(crate) fn convert(multi_era_block: MultiEraBlock) -> Self {
38        let mut transactions = Vec::new();
39        for tx in &multi_era_block.txs() {
40            transactions.push(tx.hash().to_string());
41        }
42
43        Self::new(
44            *multi_era_block.hash(),
45            BlockNumber(multi_era_block.number()),
46            SlotNumber(multi_era_block.slot()),
47            transactions,
48        )
49    }
50
51    /// Number of transactions in the block
52    pub fn transactions_len(&self) -> usize {
53        self.transactions_hashes.len()
54    }
55
56    /// Convert the scanned block into a list of Cardano transactions.
57    ///
58    /// Consume the block.
59    pub fn into_transactions(self) -> Vec<CardanoTransaction> {
60        let block_hash = hex::encode(&self.block_hash);
61        self.transactions_hashes
62            .into_iter()
63            .map(|transaction_hash| {
64                CardanoTransaction::new(
65                    transaction_hash,
66                    self.block_number,
67                    self.slot_number,
68                    block_hash.clone(),
69                )
70            })
71            .collect::<Vec<_>>()
72    }
73}
74
75impl Debug for ScannedBlock {
76    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
77        let mut debug = f.debug_struct("ScannedBlock");
78        debug
79            .field("block_hash", &hex::encode(&self.block_hash))
80            .field("block_number", &self.block_number)
81            .field("slot_number", &self.slot_number)
82            .field("transactions_hashes", &self.transactions_hashes)
83            .finish()
84    }
85}
86
87impl From<&ScannedBlock> for ChainPoint {
88    fn from(scanned_block: &ScannedBlock) -> Self {
89        ChainPoint::new(
90            scanned_block.slot_number,
91            scanned_block.block_number,
92            hex::encode(&scanned_block.block_hash),
93        )
94    }
95}