solana_svm/
transaction_processing_result.rs

1use {
2    crate::{
3        account_loader::FeesOnlyTransaction,
4        transaction_execution_result::{ExecutedTransaction, TransactionExecutionDetails},
5    },
6    solana_sdk::{
7        fee::FeeDetails,
8        transaction::{Result as TransactionResult, TransactionError},
9    },
10};
11
12pub type TransactionProcessingResult = TransactionResult<ProcessedTransaction>;
13
14pub trait TransactionProcessingResultExtensions {
15    fn was_processed(&self) -> bool;
16    fn was_processed_with_successful_result(&self) -> bool;
17    fn processed_transaction(&self) -> Option<&ProcessedTransaction>;
18    fn flattened_result(&self) -> TransactionResult<()>;
19}
20
21#[derive(Debug)]
22pub enum ProcessedTransaction {
23    /// Transaction was executed, but if execution failed, all account state changes
24    /// will be rolled back except deducted fees and any advanced nonces
25    Executed(Box<ExecutedTransaction>),
26    /// Transaction was not able to be executed but fees are able to be
27    /// collected and any nonces are advanceable
28    FeesOnly(Box<FeesOnlyTransaction>),
29}
30
31impl TransactionProcessingResultExtensions for TransactionProcessingResult {
32    fn was_processed(&self) -> bool {
33        self.is_ok()
34    }
35
36    fn was_processed_with_successful_result(&self) -> bool {
37        match self {
38            Ok(processed_tx) => processed_tx.was_processed_with_successful_result(),
39            Err(_) => false,
40        }
41    }
42
43    fn processed_transaction(&self) -> Option<&ProcessedTransaction> {
44        match self {
45            Ok(processed_tx) => Some(processed_tx),
46            Err(_) => None,
47        }
48    }
49
50    fn flattened_result(&self) -> TransactionResult<()> {
51        self.as_ref()
52            .map_err(|err| err.clone())
53            .and_then(|processed_tx| processed_tx.status())
54    }
55}
56
57impl ProcessedTransaction {
58    fn was_processed_with_successful_result(&self) -> bool {
59        match self {
60            Self::Executed(executed_tx) => executed_tx.execution_details.status.is_ok(),
61            Self::FeesOnly(_) => false,
62        }
63    }
64
65    pub fn status(&self) -> TransactionResult<()> {
66        match self {
67            Self::Executed(executed_tx) => executed_tx.execution_details.status.clone(),
68            Self::FeesOnly(details) => Err(TransactionError::clone(&details.load_error)),
69        }
70    }
71
72    pub fn fee_details(&self) -> FeeDetails {
73        match self {
74            Self::Executed(executed_tx) => executed_tx.loaded_transaction.fee_details,
75            Self::FeesOnly(details) => details.fee_details,
76        }
77    }
78
79    pub fn executed_transaction(&self) -> Option<&ExecutedTransaction> {
80        match self {
81            Self::Executed(context) => Some(context),
82            Self::FeesOnly { .. } => None,
83        }
84    }
85
86    pub fn execution_details(&self) -> Option<&TransactionExecutionDetails> {
87        match self {
88            Self::Executed(context) => Some(&context.execution_details),
89            Self::FeesOnly { .. } => None,
90        }
91    }
92}