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
//! In-memory client implementation

use crate::{
    backtrace::Backtrace,
    checked_transaction::Checked,
    error::InterpreterError,
    interpreter::{
        EcalHandler,
        InterpreterParams,
        Memory,
        NotSupportedEcal,
    },
    state::StateTransitionRef,
    storage::MemoryStorage,
    transactor::Transactor,
};
use core::convert::Infallible;
use fuel_tx::{
    Create,
    FeeParameters,
    GasCosts,
    Receipt,
    Script,
    Upgrade,
    Upload,
};

#[cfg(any(test, feature = "test-helpers"))]
use crate::interpreter::MemoryInstance;

#[derive(Debug)]
/// Client implementation with in-memory storage backend.
pub struct MemoryClient<M, Ecal = NotSupportedEcal> {
    transactor: Transactor<M, MemoryStorage, Script, Ecal>,
}

#[cfg(any(test, feature = "test-helpers"))]
impl Default for MemoryClient<MemoryInstance> {
    fn default() -> Self {
        Self::from_txtor(Transactor::new(
            MemoryInstance::new(),
            MemoryStorage::default(),
            InterpreterParams::default(),
        ))
    }
}

impl<M, Ecal: EcalHandler> AsRef<MemoryStorage> for MemoryClient<M, Ecal> {
    fn as_ref(&self) -> &MemoryStorage {
        self.transactor.as_ref()
    }
}

impl<M, Ecal: EcalHandler> AsMut<MemoryStorage> for MemoryClient<M, Ecal> {
    fn as_mut(&mut self) -> &mut MemoryStorage {
        self.transactor.as_mut()
    }
}

impl<M, Ecal: EcalHandler + Default> MemoryClient<M, Ecal> {
    /// Create a new instance of the memory client out of a provided storage.
    pub fn new(
        memory: M,
        storage: MemoryStorage,
        interpreter_params: InterpreterParams,
    ) -> Self {
        Self {
            transactor: Transactor::new(memory, storage, interpreter_params),
        }
    }
}

impl<M, Ecal: EcalHandler> MemoryClient<M, Ecal> {
    /// Create a new instance of the memory client out of a provided storage.
    pub fn from_txtor(transactor: Transactor<M, MemoryStorage, Script, Ecal>) -> Self {
        Self { transactor }
    }
}

impl<M, Ecal: EcalHandler> MemoryClient<M, Ecal>
where
    M: Memory,
{
    /// If a transaction was executed and produced a VM panic, returns the
    /// backtrace; return `None` otherwise.
    pub fn backtrace(&self) -> Option<Backtrace> {
        self.transactor.backtrace()
    }

    /// If a transaction was successfully executed, returns the produced
    /// receipts; return `None` otherwise.
    pub fn receipts(&self) -> Option<&[Receipt]> {
        self.transactor.receipts()
    }

    /// State transition representation after the execution of a transaction.
    pub fn state_transition(&self) -> Option<StateTransitionRef<'_, Script>> {
        self.transactor.state_transition()
    }

    /// Deploys a `Create` transaction.
    pub fn deploy(
        &mut self,
        tx: Checked<Create>,
    ) -> Result<Create, InterpreterError<Infallible>> {
        self.transactor.deploy(tx)
    }

    /// Executes `Upgrade` transaction.
    pub fn upgrade(
        &mut self,
        tx: Checked<Upgrade>,
    ) -> Result<Upgrade, InterpreterError<Infallible>> {
        self.transactor.upgrade(tx)
    }

    /// Executes `Upload` transaction.
    pub fn upload(&mut self, tx: Checked<Upload>) -> Option<Upload> {
        self.transactor.upload(tx).ok()
    }

    /// Execute a transaction.
    ///
    /// Since the memory storage is `Infallible`, associatively, the memory
    /// client should also be.
    pub fn transact(&mut self, tx: Checked<Script>) -> &[Receipt] {
        self.transactor.transact(tx);

        // TODO `Transactor::result` should accept error as generic so compile-time
        // constraints can be applied.
        //
        // In this case, we should expect `Infallible` error.
        if let Ok(state) = self.transactor.result() {
            if state.should_revert() {
                self.transactor.as_mut().revert();
            } else {
                self.transactor.as_mut().commit();
            }
        } else {
            // if vm failed to execute, revert storage just in case
            self.transactor.as_mut().revert();
        }

        self.transactor.receipts().unwrap_or_default()
    }

    /// Persist the changes caused by [`Self::transact`].
    pub fn persist(&mut self) {
        self.as_mut().persist();
    }

    /// Tx memory offset
    pub fn tx_offset(&self) -> usize {
        self.transactor.tx_offset()
    }

    /// Gas costs for opcodes
    pub fn gas_costs(&self) -> &GasCosts {
        self.transactor.gas_costs()
    }

    /// Fee parameters
    pub fn fee_params(&self) -> &FeeParameters {
        self.transactor.fee_params()
    }

    #[cfg(feature = "test-helpers")]
    /// Sets the gas price of the `Interpreter`
    pub fn set_gas_price(&mut self, gas_price: u64) {
        self.transactor.set_gas_price(gas_price);
    }
}

impl<M, Ecal: EcalHandler> From<MemoryClient<M, Ecal>>
    for Transactor<M, MemoryStorage, Script, Ecal>
{
    fn from(client: MemoryClient<M, Ecal>) -> Self {
        client.transactor
    }
}