Skip to main content

signet_types/
confirmation.rs

1//! Block confirmation metadata for transactions and receipts.
2
3use crate::TxLocation;
4use alloy::primitives::{BlockNumber, B256};
5
6/// Block confirmation metadata for a transaction or receipt.
7///
8/// Contains the block context in which a transaction was included,
9/// enabling downstream consumers (e.g., RPC layers) to populate
10/// block-related fields without additional lookups.
11///
12/// # Example
13///
14/// ```
15/// # use alloy::primitives::B256;
16/// # use signet_types::ConfirmationMeta;
17/// let meta = ConfirmationMeta::new(42, B256::ZERO, 0);
18/// assert_eq!(meta.block_number(), 42);
19/// assert_eq!(meta.transaction_index(), 0);
20/// ```
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22pub struct ConfirmationMeta {
23    /// The block number containing the transaction.
24    block_number: BlockNumber,
25    /// The hash of the block containing the transaction.
26    block_hash: B256,
27    /// The index of the transaction within the block.
28    transaction_index: u64,
29}
30
31impl ConfirmationMeta {
32    /// Create new confirmation metadata.
33    pub const fn new(block_number: BlockNumber, block_hash: B256, transaction_index: u64) -> Self {
34        Self { block_number, block_hash, transaction_index }
35    }
36
37    /// Returns the block number.
38    pub const fn block_number(&self) -> BlockNumber {
39        self.block_number
40    }
41
42    /// Returns the block hash.
43    pub const fn block_hash(&self) -> B256 {
44        self.block_hash
45    }
46
47    /// Returns the transaction index within the block.
48    pub const fn transaction_index(&self) -> u64 {
49        self.transaction_index
50    }
51}
52
53impl From<(TxLocation, B256)> for ConfirmationMeta {
54    fn from((loc, block_hash): (TxLocation, B256)) -> Self {
55        Self::new(loc.block, block_hash, loc.index)
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_confirmation_meta_new() {
65        let meta = ConfirmationMeta::new(100, B256::ZERO, 5);
66        assert_eq!(meta.block_number(), 100);
67        assert_eq!(meta.block_hash(), B256::ZERO);
68        assert_eq!(meta.transaction_index(), 5);
69    }
70
71    #[test]
72    fn test_from_tx_location() {
73        let loc = TxLocation { block: 42, index: 3 };
74        let hash = B256::repeat_byte(0xAB);
75        let meta = ConfirmationMeta::from((loc, hash));
76        assert_eq!(meta.block_number(), 42);
77        assert_eq!(meta.block_hash(), hash);
78        assert_eq!(meta.transaction_index(), 3);
79    }
80}