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