mithril_common/entities/
cardano_transaction.rs

1use crate::{
2    crypto_helper::MKTreeNode,
3    entities::{BlockHash, BlockNumber, SlotNumber},
4};
5
6/// TransactionHash is the unique identifier of a cardano transaction.
7pub type TransactionHash = String;
8
9#[derive(Debug, PartialEq, Clone)]
10/// Cardano transaction representation
11pub struct CardanoTransaction {
12    /// Unique hash of the transaction
13    pub transaction_hash: TransactionHash,
14
15    /// Block number of the transaction
16    pub block_number: BlockNumber,
17
18    /// Slot number of the transaction
19    pub slot_number: SlotNumber,
20
21    /// Block hash of the transaction
22    pub block_hash: BlockHash,
23}
24
25impl CardanoTransaction {
26    /// CardanoTransaction factory
27    pub fn new<T: Into<TransactionHash>, U: Into<BlockHash>>(
28        hash: T,
29        block_number: BlockNumber,
30        slot_number: SlotNumber,
31        block_hash: U,
32    ) -> Self {
33        Self {
34            transaction_hash: hash.into(),
35            block_number,
36            slot_number,
37            block_hash: block_hash.into(),
38        }
39    }
40}
41
42impl From<CardanoTransaction> for MKTreeNode {
43    fn from(other: CardanoTransaction) -> Self {
44        (&other).into()
45    }
46}
47
48impl From<&CardanoTransaction> for MKTreeNode {
49    fn from(other: &CardanoTransaction) -> Self {
50        MKTreeNode::new(other.transaction_hash.as_bytes().to_vec())
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn test_convert_cardano_transaction_to_merkle_tree_node() {
60        let transaction =
61            CardanoTransaction::new("tx-hash-123", BlockNumber(10), SlotNumber(4), "block_hash");
62
63        let computed_mktree_node: MKTreeNode = transaction.into();
64        let expected_mk_tree_node = MKTreeNode::new("tx-hash-123".as_bytes().to_vec());
65        let non_expected_mk_tree_node = MKTreeNode::new("tx-hash-456".as_bytes().to_vec());
66
67        assert_eq!(expected_mk_tree_node, computed_mktree_node);
68        assert_ne!(non_expected_mk_tree_node, computed_mktree_node);
69    }
70}