multiversx_chain_vm/host/context/
tx_context_ref.rs1use std::{ops::Deref, sync::Arc};
2
3use crate::host::context::{TxContext, TxResult};
4
5use super::{BlockchainUpdate, TxPanic};
6
7#[derive(Debug)]
10pub struct TxContextRef(pub Arc<TxContext>);
11
12impl Deref for TxContextRef {
13    type Target = TxContext;
14    fn deref(&self) -> &Self::Target {
15        self.0.deref()
16    }
17}
18
19impl Clone for TxContextRef {
20    fn clone(&self) -> Self {
21        Self(self.0.clone())
22    }
23}
24
25impl TxContextRef {
26    pub fn new(tx_context_arc: Arc<TxContext>) -> Self {
27        Self(tx_context_arc)
28    }
29
30    pub fn dummy() -> Self {
31        Self::new(Arc::new(TxContext::dummy()))
32    }
33
34    pub fn into_blockchain_updates(self) -> BlockchainUpdate {
35        let tx_context = Arc::try_unwrap(self.0).unwrap();
36        let tx_cache = Arc::try_unwrap(tx_context.tx_cache).unwrap();
37        tx_cache.into_blockchain_updates()
38    }
39
40    pub fn into_tx_result(self) -> TxResult {
44        std::mem::take(&mut *self.tx_result_cell.lock().unwrap())
48    }
49
50    pub fn replace_tx_result_with_error(self, tx_panic: TxPanic) {
54        let _ = std::mem::replace(
55            &mut *self.tx_result_cell.lock().unwrap(),
56            TxResult::from_panic_obj(&tx_panic),
57        );
58    }
59
60    pub fn ptr_eq(this: &Self, other: &Self) -> bool {
62        Arc::ptr_eq(&this.0, &other.0)
63    }
64
65    pub fn into_ref(self) -> Arc<TxContext> {
66        self.0
67    }
68}