mithril_common/entities/
cardano_block.rs

1use crate::entities::{BlockHash, BlockNumber, CardanoTransaction, SlotNumber, TransactionHash};
2
3/// Cardano block representation
4#[derive(Debug, Clone, PartialEq)]
5pub struct CardanoBlock {
6    /// Block hash
7    pub block_hash: BlockHash,
8    /// Block number
9    pub block_number: BlockNumber,
10    /// Slot number of the block
11    pub slot_number: SlotNumber,
12}
13
14impl CardanoBlock {
15    /// CardanoBlock factory
16    pub fn new<U: Into<BlockHash>>(
17        block_hash: U,
18        block_number: BlockNumber,
19        slot_number: SlotNumber,
20    ) -> Self {
21        Self {
22            block_hash: block_hash.into(),
23            block_number,
24            slot_number,
25        }
26    }
27}
28
29/// Cardano block representation, including the hashes of the transactions in the block
30#[derive(Debug, Clone, PartialEq)]
31pub struct CardanoBlockWithTransactions {
32    /// Block hash
33    pub block_hash: BlockHash,
34    /// Block number
35    pub block_number: BlockNumber,
36    /// Slot number of the block
37    pub slot_number: SlotNumber,
38    /// Hashes of the transactions in the block
39    pub transactions_hashes: Vec<TransactionHash>,
40}
41
42impl CardanoBlockWithTransactions {
43    /// CardanoBlockWithTransactions factory
44    pub fn new<U: Into<BlockHash>, T: Into<TransactionHash>>(
45        block_hash: U,
46        block_number: BlockNumber,
47        slot_number: SlotNumber,
48        tx_hashes: Vec<T>,
49    ) -> Self {
50        Self {
51            block_hash: block_hash.into(),
52            block_number,
53            slot_number,
54            transactions_hashes: tx_hashes.into_iter().map(Into::into).collect(),
55        }
56    }
57
58    /// Converts the block into a vector of transactions.
59    pub fn into_transactions(self) -> Vec<CardanoTransaction> {
60        self.transactions_hashes
61            .into_iter()
62            .map(|tx_hash| {
63                CardanoTransaction::new(
64                    tx_hash,
65                    self.block_number,
66                    self.slot_number,
67                    self.block_hash.clone(),
68                )
69            })
70            .collect()
71    }
72
73    /// Returns the number of transactions in the block.
74    pub fn transactions_count(&self) -> usize {
75        self.transactions_hashes.len()
76    }
77}