solana_svm/
transaction_processing_result.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
use {
    crate::{
        account_loader::FeesOnlyTransaction,
        transaction_execution_result::{ExecutedTransaction, TransactionExecutionDetails},
    },
    solana_sdk::{
        fee::FeeDetails,
        transaction::{Result as TransactionResult, TransactionError},
    },
};

pub type TransactionProcessingResult = TransactionResult<ProcessedTransaction>;

pub trait TransactionProcessingResultExtensions {
    fn was_processed(&self) -> bool;
    fn was_processed_with_successful_result(&self) -> bool;
    fn processed_transaction(&self) -> Option<&ProcessedTransaction>;
    fn flattened_result(&self) -> TransactionResult<()>;
}

#[derive(Debug)]
pub enum ProcessedTransaction {
    /// Transaction was executed, but if execution failed, all account state changes
    /// will be rolled back except deducted fees and any advanced nonces
    Executed(Box<ExecutedTransaction>),
    /// Transaction was not able to be executed but fees are able to be
    /// collected and any nonces are advanceable
    FeesOnly(Box<FeesOnlyTransaction>),
}

impl TransactionProcessingResultExtensions for TransactionProcessingResult {
    fn was_processed(&self) -> bool {
        self.is_ok()
    }

    fn was_processed_with_successful_result(&self) -> bool {
        match self {
            Ok(processed_tx) => processed_tx.was_processed_with_successful_result(),
            Err(_) => false,
        }
    }

    fn processed_transaction(&self) -> Option<&ProcessedTransaction> {
        match self {
            Ok(processed_tx) => Some(processed_tx),
            Err(_) => None,
        }
    }

    fn flattened_result(&self) -> TransactionResult<()> {
        self.as_ref()
            .map_err(|err| err.clone())
            .and_then(|processed_tx| processed_tx.status())
    }
}

impl ProcessedTransaction {
    fn was_processed_with_successful_result(&self) -> bool {
        match self {
            Self::Executed(executed_tx) => executed_tx.execution_details.status.is_ok(),
            Self::FeesOnly(_) => false,
        }
    }

    pub fn status(&self) -> TransactionResult<()> {
        match self {
            Self::Executed(executed_tx) => executed_tx.execution_details.status.clone(),
            Self::FeesOnly(details) => Err(TransactionError::clone(&details.load_error)),
        }
    }

    pub fn fee_details(&self) -> FeeDetails {
        match self {
            Self::Executed(executed_tx) => executed_tx.loaded_transaction.fee_details,
            Self::FeesOnly(details) => details.fee_details,
        }
    }

    pub fn executed_transaction(&self) -> Option<&ExecutedTransaction> {
        match self {
            Self::Executed(context) => Some(context),
            Self::FeesOnly { .. } => None,
        }
    }

    pub fn execution_details(&self) -> Option<&TransactionExecutionDetails> {
        match self {
            Self::Executed(context) => Some(&context.execution_details),
            Self::FeesOnly { .. } => None,
        }
    }
}