Skip to main content

cuprate_types/
transaction_verification_data.rs

1//! Contains [`TransactionVerificationData`] and the related types.
2
3use bytes::Bytes;
4use monero_oxide::transaction::{Timelock, Transaction};
5
6use crate::{HardFork, VerifiedTransactionInformation};
7
8/// An enum representing all valid Monero transaction versions.
9#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
10pub enum TxVersion {
11    /// Legacy ring signatures.
12    RingSignatures,
13    /// Ring-CT
14    RingCT,
15}
16
17impl TxVersion {
18    /// Converts a `raw` version value to a [`TxVersion`].
19    ///
20    /// This will return `None` on invalid values.
21    ///
22    /// ref: <https://monero-book.cuprate.org/consensus_rules/transactions.html#version>
23    ///  &&  <https://monero-book.cuprate.org/consensus_rules/blocks/miner_tx.html#version>
24    pub const fn from_raw(version: u8) -> Option<Self> {
25        Some(match version {
26            1 => Self::RingSignatures,
27            2 => Self::RingCT,
28            _ => return None,
29        })
30    }
31}
32
33/// Represents if a transaction has been fully validated and under what conditions
34/// the transaction is valid in the future.
35#[derive(Copy, Clone, Debug, PartialEq, Eq)]
36pub enum CachedVerificationState {
37    /// The transaction has not been validated.
38    NotVerified,
39    /// The transaction was only validated semantically.
40    OnlySemantic(HardFork),
41    /// The transaction is valid* if the block represented by this hash is in the blockchain and the [`HardFork`]
42    /// is the same.
43    ///
44    /// *V1 transactions require checks on their ring-length even if this hash is in the blockchain.
45    ValidAtHashAndHF {
46        /// The block hash that was in the chain when this transaction was validated.
47        block_hash: [u8; 32],
48        /// The hf this transaction was validated against.
49        hf: HardFork,
50    },
51    /// The transaction is valid* if the block represented by this hash is in the blockchain _and_ this
52    /// given time lock is unlocked. The time lock here will represent the youngest used time based lock
53    /// (If the transaction uses any time based time locks). This is because time locks are not monotonic
54    /// so unlocked outputs could become re-locked.
55    ///
56    /// *V1 transactions require checks on their ring-length even if this hash is in the blockchain.
57    ValidAtHashAndHFWithTimeBasedLock {
58        /// The block hash that was in the chain when this transaction was validated.
59        block_hash: [u8; 32],
60        /// The hf this transaction was validated against.
61        hf: HardFork,
62        /// The youngest used time based lock.
63        time_lock: Timelock,
64    },
65}
66
67impl CachedVerificationState {
68    /// Returns the block hash this is valid for if in state [`CachedVerificationState::ValidAtHashAndHF`] or [`CachedVerificationState::ValidAtHashAndHFWithTimeBasedLock`].
69    pub const fn verified_at_block_hash(&self) -> Option<[u8; 32]> {
70        match self {
71            Self::NotVerified | Self::OnlySemantic(_) => None,
72            Self::ValidAtHashAndHF { block_hash, .. }
73            | Self::ValidAtHashAndHFWithTimeBasedLock { block_hash, .. } => Some(*block_hash),
74        }
75    }
76}
77
78/// Data needed to verify a transaction.
79#[derive(Debug, Clone)]
80pub struct TransactionVerificationData {
81    /// The transaction we are verifying
82    pub tx: Transaction,
83    /// The [`TxVersion`] of this tx.
84    pub version: TxVersion,
85    /// The serialised transaction.
86    pub tx_blob: Bytes,
87    /// The weight of the transaction.
88    pub tx_weight: usize,
89    /// The fee this transaction has paid.
90    pub fee: u64,
91    /// The hash of this transaction.
92    pub tx_hash: [u8; 32],
93    /// The verification state of this transaction.
94    pub cached_verification_state: CachedVerificationState,
95}
96
97#[derive(Debug, Copy, Clone, thiserror::Error)]
98#[error("Error converting a verified tx to a cached verification data tx.")]
99pub struct TxConversionError;
100
101impl TryFrom<VerifiedTransactionInformation> for TransactionVerificationData {
102    type Error = TxConversionError;
103
104    fn try_from(value: VerifiedTransactionInformation) -> Result<Self, Self::Error> {
105        let tx_blob = Bytes::from([value.tx_pruned, value.tx_prunable_blob].concat());
106        let tx = Transaction::read(&mut tx_blob.as_ref()).map_err(|_| TxConversionError)?;
107
108        Ok(Self {
109            version: TxVersion::from_raw(value.tx.version()).ok_or(TxConversionError)?,
110            tx,
111            tx_blob,
112            tx_weight: value.tx_weight,
113            fee: value.fee,
114            tx_hash: value.tx_hash,
115            cached_verification_state: CachedVerificationState::NotVerified,
116        })
117    }
118}