cuprate_consensus/
transactions.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! # Transaction Verifier Service.
//!
//! This module contains the transaction validation interface, which can be accessed with [`start_tx_verification`].
//!
//! Transaction verification states will be cached to prevent doing the expensive checks multiple times.
//!
//! ## Example Semantic Verification
//!
//! ```rust
//! # use cuprate_test_utils::data::TX_E2D393;
//! # use monero_serai::transaction::Transaction;
//! use cuprate_consensus::{transactions::start_tx_verification, HardFork, batch_verifier::MultiThreadedBatchVerifier};
//!
//! # fn main() -> Result<(), tower::BoxError> {
//! # let tx = Transaction::read(&mut TX_E2D393).unwrap();
//! let batch_verifier = MultiThreadedBatchVerifier::new(rayon::current_num_threads());
//!
//! let tx = start_tx_verification()
//!              .append_txs(vec![tx])
//!              .prepare()?
//!              .only_semantic(HardFork::V9)
//!              .queue(&batch_verifier)?;
//!
//! assert!(batch_verifier.verify());
//! Ok(())
//! # }
//! ```
use std::collections::HashSet;

use monero_serai::transaction::{Input, Timelock, Transaction};
use rayon::prelude::*;
use tower::ServiceExt;

use cuprate_consensus_rules::{
    transactions::{
        check_decoy_info, check_transaction_contextual, check_transaction_semantic,
        output_unlocked, TransactionError,
    },
    ConsensusError, HardFork,
};
use cuprate_helper::asynch::rayon_spawn_async;
use cuprate_types::{
    blockchain::{BlockchainReadRequest, BlockchainResponse},
    CachedVerificationState, TransactionVerificationData, TxVersion,
};

use crate::{
    batch_verifier::MultiThreadedBatchVerifier,
    transactions::contextual_data::{batch_get_decoy_info, batch_get_ring_member_info},
    Database, ExtendedConsensusError,
};

pub mod contextual_data;
mod free;

pub use free::new_tx_verification_data;

/// An enum representing the type of validation that needs to be completed for this transaction.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum VerificationNeeded {
    /// Decoy check on a v1 transaction.
    V1DecoyCheck,
    /// Both semantic validation and contextual validation are needed.
    SemanticAndContextual,
    /// Only contextual validation is needed.
    Contextual,
    /// No verification needed.
    None,
}

/// Start the transaction verification process.
pub const fn start_tx_verification() -> PrepTransactions {
    PrepTransactions {
        txs: vec![],
        prepped_txs: vec![],
    }
}

/// The preparation phase of transaction verification.
///
/// The order of transactions will be kept throughout the verification process, transactions
/// inserted with [`PrepTransactions::append_prepped_txs`] will be put before transactions given
/// in [`PrepTransactions::append_txs`]
pub struct PrepTransactions {
    prepped_txs: Vec<TransactionVerificationData>,
    txs: Vec<Transaction>,
}

impl PrepTransactions {
    /// Append some new transactions to prepare.
    #[must_use]
    pub fn append_txs(mut self, mut txs: Vec<Transaction>) -> Self {
        self.txs.append(&mut txs);

        self
    }

    /// Append some already prepped transactions.
    #[must_use]
    pub fn append_prepped_txs(mut self, mut txs: Vec<TransactionVerificationData>) -> Self {
        self.prepped_txs.append(&mut txs);

        self
    }

    /// Prepare the transactions and advance to the next step: [`VerificationWanted`].
    ///
    /// # [`rayon`]
    ///
    /// This function will use [`rayon`] to parallelize the preparation process, so should not be called
    /// in an async function, unless all the transactions given were already prepared, i.e. [`Self::append_prepped_txs`].
    pub fn prepare(mut self) -> Result<VerificationWanted, ConsensusError> {
        if !self.txs.is_empty() {
            self.prepped_txs.append(
                &mut self
                    .txs
                    .into_par_iter()
                    .map(new_tx_verification_data)
                    .collect::<Result<_, _>>()?,
            );
        }

        Ok(VerificationWanted {
            prepped_txs: self.prepped_txs,
        })
    }
}

/// The step where the type of verification is decided.
pub struct VerificationWanted {
    prepped_txs: Vec<TransactionVerificationData>,
}

impl VerificationWanted {
    /// Only semantic verification.
    ///
    /// Semantic verification is verification that can done without other blockchain data. The [`HardFork`]
    /// is technically other blockchain data but excluding it reduces the amount of things that can be checked
    /// significantly, and it is easy to get compared to other blockchain data needed for contextual validation.
    pub fn only_semantic(self, hf: HardFork) -> SemanticVerification {
        SemanticVerification {
            prepped_txs: self.prepped_txs,
            hf,
        }
    }

    /// Full verification.
    ///
    /// Fully verify the transactions, all checks will be performed, if they were already performed then they
    /// won't be done again unless necessary.
    pub fn full<D: Database>(
        self,
        current_chain_height: usize,
        top_hash: [u8; 32],
        time_for_time_lock: u64,
        hf: HardFork,
        database: D,
    ) -> FullVerification<D> {
        FullVerification {
            prepped_txs: self.prepped_txs,
            current_chain_height,
            top_hash,
            time_for_time_lock,
            hf,
            database,
        }
    }
}

/// Semantic transaction verification.
///
/// [`VerificationWanted::only_semantic`]
pub struct SemanticVerification {
    prepped_txs: Vec<TransactionVerificationData>,
    hf: HardFork,
}

impl SemanticVerification {
    /// Perform the semantic checks and queue any checks that can be batched into the batch verifier.
    ///
    /// If this function returns [`Ok`] the transaction(s) could still be semantically invalid,
    /// [`MultiThreadedBatchVerifier::verify`] must be called on the `batch_verifier` after.
    pub fn queue(
        mut self,
        batch_verifier: &MultiThreadedBatchVerifier,
    ) -> Result<Vec<TransactionVerificationData>, ConsensusError> {
        self.prepped_txs.par_iter_mut().try_for_each(|tx| {
            let fee = check_transaction_semantic(
                &tx.tx,
                tx.tx_blob.len(),
                tx.tx_weight,
                &tx.tx_hash,
                self.hf,
                batch_verifier,
            )?;
            // make sure we calculated the right fee.
            assert_eq!(fee, tx.fee);

            tx.cached_verification_state = CachedVerificationState::OnlySemantic(self.hf);

            Ok::<_, ConsensusError>(())
        })?;

        Ok(self.prepped_txs)
    }
}

/// Full transaction verification.
///
/// [`VerificationWanted::full`]
pub struct FullVerification<D> {
    prepped_txs: Vec<TransactionVerificationData>,

    current_chain_height: usize,
    top_hash: [u8; 32],
    time_for_time_lock: u64,
    hf: HardFork,
    database: D,
}

impl<D: Database + Clone> FullVerification<D> {
    /// Fully verify each transaction.
    pub async fn verify(
        mut self,
    ) -> Result<Vec<TransactionVerificationData>, ExtendedConsensusError> {
        check_kis_unique(&self.prepped_txs, &mut self.database).await?;

        let hashes_in_main_chain =
            hashes_referenced_in_main_chain(&self.prepped_txs, &mut self.database).await?;

        let (verification_needed, any_v1_decoy_check_needed) = verification_needed(
            &self.prepped_txs,
            &hashes_in_main_chain,
            self.hf,
            self.current_chain_height,
            self.time_for_time_lock,
        )?;

        if any_v1_decoy_check_needed {
            verify_transactions_decoy_info(
                self.prepped_txs
                    .iter()
                    .zip(verification_needed.iter())
                    .filter_map(|(tx, needed)| {
                        if *needed == VerificationNeeded::V1DecoyCheck {
                            Some(tx)
                        } else {
                            None
                        }
                    }),
                self.hf,
                self.database.clone(),
            )
            .await?;
        }

        verify_transactions(
            self.prepped_txs,
            verification_needed,
            self.current_chain_height,
            self.top_hash,
            self.time_for_time_lock,
            self.hf,
            self.database,
        )
        .await
    }
}

/// Check that each key image used in each transaction is unique in the whole chain.
async fn check_kis_unique<D: Database>(
    txs: &[TransactionVerificationData],
    database: &mut D,
) -> Result<(), ExtendedConsensusError> {
    let mut spent_kis = HashSet::with_capacity(txs.len());

    txs.iter().try_for_each(|tx| {
        tx.tx.prefix().inputs.iter().try_for_each(|input| {
            if let Input::ToKey { key_image, .. } = input {
                if !spent_kis.insert(key_image.compress().0) {
                    tracing::debug!("Duplicate key image found in batch.");
                    return Err(ConsensusError::Transaction(TransactionError::KeyImageSpent));
                }
            }

            Ok(())
        })
    })?;

    let BlockchainResponse::KeyImagesSpent(kis_spent) = database
        .ready()
        .await?
        .call(BlockchainReadRequest::KeyImagesSpent(spent_kis))
        .await?
    else {
        panic!("Database sent incorrect response!");
    };

    if kis_spent {
        tracing::debug!("One or more key images in batch already spent.");
        return Err(ConsensusError::Transaction(TransactionError::KeyImageSpent).into());
    }

    Ok(())
}

/// Returns a [`HashSet`] of all the hashes referenced in each transaction's [`CachedVerificationState`], that
/// are also in the main chain.
async fn hashes_referenced_in_main_chain<D: Database>(
    txs: &[TransactionVerificationData],
    database: &mut D,
) -> Result<HashSet<[u8; 32]>, ExtendedConsensusError> {
    let mut verified_at_block_hashes = txs
        .iter()
        .filter_map(|txs| txs.cached_verification_state.verified_at_block_hash())
        .collect::<HashSet<_>>();

    tracing::trace!(
        "Verified at hashes len: {}.",
        verified_at_block_hashes.len()
    );

    if !verified_at_block_hashes.is_empty() {
        tracing::trace!("Filtering block hashes not in the main chain.");

        let BlockchainResponse::FilterUnknownHashes(known_hashes) = database
            .ready()
            .await?
            .call(BlockchainReadRequest::FilterUnknownHashes(
                verified_at_block_hashes,
            ))
            .await?
        else {
            panic!("Database returned wrong response!");
        };
        verified_at_block_hashes = known_hashes;
    }

    Ok(verified_at_block_hashes)
}

/// Returns a list of [`VerificationNeeded`] for each transaction passed in. The returned
/// [`Vec`] will be the same length as the inputted transactions.
///
/// A [`bool`] is also returned, which will be true if any transactions need [`VerificationNeeded::V1DecoyCheck`].
fn verification_needed(
    txs: &[TransactionVerificationData],
    hashes_in_main_chain: &HashSet<[u8; 32]>,
    current_hf: HardFork,
    current_chain_height: usize,
    time_for_time_lock: u64,
) -> Result<(Vec<VerificationNeeded>, bool), ConsensusError> {
    // txs needing full validation: semantic and/or contextual
    let mut verification_needed = Vec::with_capacity(txs.len());

    let mut any_v1_decoy_checks = false;

    for tx in txs {
        match &tx.cached_verification_state {
            CachedVerificationState::NotVerified => {
                // Tx not verified at all need all checks.
                verification_needed.push(VerificationNeeded::SemanticAndContextual);
                continue;
            }
            CachedVerificationState::OnlySemantic(hf) => {
                if current_hf != *hf {
                    // HF changed must do semantic checks again.
                    verification_needed.push(VerificationNeeded::SemanticAndContextual);
                    continue;
                }
                // Tx already semantically valid for this HF only contextual checks needed.
                verification_needed.push(VerificationNeeded::Contextual);
                continue;
            }
            CachedVerificationState::ValidAtHashAndHF { block_hash, hf } => {
                if current_hf != *hf {
                    // HF changed must do all checks again.
                    verification_needed.push(VerificationNeeded::SemanticAndContextual);
                    continue;
                }

                if !hashes_in_main_chain.contains(block_hash) {
                    // The block we know this transaction was valid at is no longer in the chain do
                    // contextual checks again.
                    verification_needed.push(VerificationNeeded::Contextual);
                    continue;
                }
            }
            CachedVerificationState::ValidAtHashAndHFWithTimeBasedLock {
                block_hash,
                hf,
                time_lock,
            } => {
                if current_hf != *hf {
                    // HF changed must do all checks again.
                    verification_needed.push(VerificationNeeded::SemanticAndContextual);
                    continue;
                }

                if !hashes_in_main_chain.contains(block_hash) {
                    // The block we know this transaction was valid at is no longer in the chain do
                    // contextual checks again.
                    verification_needed.push(VerificationNeeded::Contextual);
                    continue;
                }

                // If the time lock is still locked then the transaction is invalid.
                // Time is not monotonic in Monero so these can become invalid with new blocks.
                if !output_unlocked(time_lock, current_chain_height, time_for_time_lock, *hf) {
                    return Err(ConsensusError::Transaction(
                        TransactionError::OneOrMoreRingMembersLocked,
                    ));
                }
            }
        }

        if tx.version == TxVersion::RingSignatures {
            // v1 txs always need at least decoy checks as they can become invalid with new blocks.
            verification_needed.push(VerificationNeeded::V1DecoyCheck);
            any_v1_decoy_checks = true;
            continue;
        }

        verification_needed.push(VerificationNeeded::None);
    }

    Ok((verification_needed, any_v1_decoy_checks))
}

/// Do [`VerificationNeeded::V1DecoyCheck`] on each tx passed in.
async fn verify_transactions_decoy_info<D: Database>(
    txs: impl Iterator<Item = &TransactionVerificationData> + Clone,
    hf: HardFork,
    database: D,
) -> Result<(), ExtendedConsensusError> {
    // Decoy info is not validated for V1 txs.
    if hf == HardFork::V1 {
        return Ok(());
    }

    batch_get_decoy_info(txs, hf, database)
        .await?
        .try_for_each(|decoy_info| decoy_info.and_then(|di| Ok(check_decoy_info(&di, hf)?)))?;

    Ok(())
}

/// Do [`VerificationNeeded::Contextual`] or [`VerificationNeeded::SemanticAndContextual`].
///
/// The inputs to this function are the txs wanted to be verified and a list of [`VerificationNeeded`],
/// if any other [`VerificationNeeded`] is specified other than [`VerificationNeeded::Contextual`] or
/// [`VerificationNeeded::SemanticAndContextual`], nothing will be verified for that tx.
async fn verify_transactions<D>(
    mut txs: Vec<TransactionVerificationData>,
    verification_needed: Vec<VerificationNeeded>,
    current_chain_height: usize,
    top_hash: [u8; 32],
    current_time_lock_timestamp: u64,
    hf: HardFork,
    database: D,
) -> Result<Vec<TransactionVerificationData>, ExtendedConsensusError>
where
    D: Database,
{
    /// A filter each tx not [`VerificationNeeded::Contextual`] or
    /// [`VerificationNeeded::SemanticAndContextual`]
    const fn tx_filter<T>((_, needed): &(T, &VerificationNeeded)) -> bool {
        matches!(
            needed,
            VerificationNeeded::Contextual | VerificationNeeded::SemanticAndContextual
        )
    }

    let txs_ring_member_info = batch_get_ring_member_info(
        txs.iter()
            .zip(verification_needed.iter())
            .filter(tx_filter)
            .map(|(tx, _)| tx),
        hf,
        database,
    )
    .await?;

    rayon_spawn_async(move || {
        let batch_verifier = MultiThreadedBatchVerifier::new(rayon::current_num_threads());

        txs.iter()
            .zip(verification_needed.iter())
            .filter(tx_filter)
            .zip(txs_ring_member_info.iter())
            .par_bridge()
            .try_for_each(|((tx, verification_needed), ring)| {
                // do semantic validation if needed.
                if *verification_needed == VerificationNeeded::SemanticAndContextual {
                    let fee = check_transaction_semantic(
                        &tx.tx,
                        tx.tx_blob.len(),
                        tx.tx_weight,
                        &tx.tx_hash,
                        hf,
                        &batch_verifier,
                    )?;
                    // make sure we calculated the right fee.
                    assert_eq!(fee, tx.fee);
                }

                // Both variants of `VerificationNeeded` require contextual validation.
                check_transaction_contextual(
                    &tx.tx,
                    ring,
                    current_chain_height,
                    current_time_lock_timestamp,
                    hf,
                )?;

                Ok::<_, ConsensusError>(())
            })?;

        if !batch_verifier.verify() {
            return Err(ExtendedConsensusError::OneOrMoreBatchVerificationStatementsInvalid);
        }

        txs.iter_mut()
            .zip(verification_needed.iter())
            .filter(tx_filter)
            .zip(txs_ring_member_info)
            .for_each(|((tx, _), ring)| {
                tx.cached_verification_state = if ring.time_locked_outs.is_empty() {
                    // no outputs with time-locks used.
                    CachedVerificationState::ValidAtHashAndHF {
                        block_hash: top_hash,
                        hf,
                    }
                } else {
                    // an output with a time-lock was used, check if it was time-based.
                    let youngest_timebased_lock = ring
                        .time_locked_outs
                        .iter()
                        .filter_map(|lock| match lock {
                            Timelock::Time(time) => Some(time),
                            _ => None,
                        })
                        .min();

                    if let Some(time) = youngest_timebased_lock {
                        // time-based lock used.
                        CachedVerificationState::ValidAtHashAndHFWithTimeBasedLock {
                            block_hash: top_hash,
                            hf,
                            time_lock: Timelock::Time(*time),
                        }
                    } else {
                        // no time-based locked output was used.
                        CachedVerificationState::ValidAtHashAndHF {
                            block_hash: top_hash,
                            hf,
                        }
                    }
                }
            });

        Ok(txs)
    })
    .await
}