sui_move_natives_latest/
transaction_context.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
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use better_any::{Tid, TidAble};
use move_binary_format::errors::{PartialVMError, PartialVMResult};
use move_core_types::{account_address::AccountAddress, vm_status::StatusCode};
use move_vm_runtime::native_extensions::NativeExtensionMarker;
use std::{cell::RefCell, rc::Rc};
use sui_types::{
    base_types::{ObjectID, SuiAddress, TxContext},
    committee::EpochId,
    digests::TransactionDigest,
};

// TransactionContext is a wrapper around TxContext that is exposed to NativeContextExtensions
// in order to provide transaction context information to Move native functions.
// Holds a Rc<RefCell<TxContext>> to allow for mutation of the TxContext.
#[derive(Tid)]
pub struct TransactionContext {
    pub(crate) tx_context: Rc<RefCell<TxContext>>,
    test_only: bool,
}

impl NativeExtensionMarker<'_> for TransactionContext {}

impl TransactionContext {
    pub fn new(tx_context: Rc<RefCell<TxContext>>) -> Self {
        Self {
            tx_context,
            test_only: false,
        }
    }

    pub fn new_for_testing(tx_context: Rc<RefCell<TxContext>>) -> Self {
        Self {
            tx_context,
            test_only: true,
        }
    }

    pub fn sender(&self) -> SuiAddress {
        self.tx_context.borrow().sender()
    }

    pub fn epoch(&self) -> EpochId {
        self.tx_context.borrow().epoch()
    }

    pub fn epoch_timestamp_ms(&self) -> u64 {
        self.tx_context.borrow().epoch_timestamp_ms()
    }

    pub fn digest(&self) -> TransactionDigest {
        self.tx_context.borrow().digest()
    }

    pub fn sponsor(&self) -> Option<SuiAddress> {
        self.tx_context.borrow().sponsor()
    }

    pub fn gas_price(&self) -> u64 {
        self.tx_context.borrow().gas_price()
    }

    pub fn gas_budget(&self) -> u64 {
        self.tx_context.borrow().gas_budget()
    }

    pub fn ids_created(&self) -> u64 {
        self.tx_context.borrow().ids_created()
    }

    pub fn fresh_id(&self) -> ObjectID {
        self.tx_context.borrow_mut().fresh_id()
    }

    //
    // Test only function
    //
    pub fn replace(
        &self,
        sender: AccountAddress,
        tx_hash: Vec<u8>,
        epoch: u64,
        epoch_timestamp_ms: u64,
        ids_created: u64,
        gas_price: u64,
        gas_budget: u64,
        sponsor: Option<AccountAddress>,
    ) -> PartialVMResult<()> {
        if !self.test_only {
            return Err(
                PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR)
                    .with_message("`replace` called on a non testing scenario".to_string()),
            );
        }
        self.tx_context.borrow_mut().replace(
            sender,
            tx_hash,
            epoch,
            epoch_timestamp_ms,
            ids_created,
            gas_price,
            gas_budget,
            sponsor,
        );
        Ok(())
    }
}