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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
use crate::error::InterpreterError;
use crate::interpreter::{CheckedMetadata, ExecutableTransaction, Interpreter};
use crate::state::{StateTransition, StateTransitionRef};
use crate::storage::InterpreterStorage;
use crate::{backtrace::Backtrace, state::ProgramState};
use fuel_tx::{Checked, ConsensusParameters, Create, IntoChecked, Receipt, Script};
#[derive(Debug)]
pub struct Transactor<S, Tx> {
interpreter: Interpreter<S, Tx>,
program_state: Option<ProgramState>,
error: Option<InterpreterError>,
}
impl<'a, S, Tx> Transactor<S, Tx>
where
Tx: ExecutableTransaction,
{
pub fn new(storage: S, params: ConsensusParameters) -> Self {
Interpreter::with_storage(storage, params).into()
}
pub fn state_transition(&'a self) -> Option<StateTransitionRef<'a, Tx>> {
self.program_state
.map(|state| StateTransitionRef::new(state, self.interpreter.transaction(), self.interpreter.receipts()))
}
pub fn to_owned_state_transition(&self) -> Option<StateTransition<Tx>> {
self.program_state.map(|state| {
StateTransition::new(
state,
self.interpreter.transaction().clone(),
self.interpreter.receipts().to_vec(),
)
})
}
pub const fn error(&self) -> Option<&InterpreterError> {
self.error.as_ref()
}
pub const fn is_success(&self) -> bool {
self.program_state.is_some()
}
pub const fn is_error(&self) -> bool {
self.error.is_some()
}
pub fn result(&'a self) -> Result<StateTransitionRef<'a, Tx>, &InterpreterError> {
let state = self.state_transition();
let error = self.error.as_ref();
match (state, error) {
(Some(s), None) => Ok(s),
(None, Some(e)) => Err(e),
_ => Err(&InterpreterError::NoTransactionInitialized),
}
}
pub fn interpreter(self) -> Interpreter<S, Tx> {
self.into()
}
pub const fn params(&self) -> &ConsensusParameters {
self.interpreter.params()
}
pub const fn tx_offset(&self) -> usize {
self.interpreter.tx_offset()
}
}
impl<S> Transactor<S, Script> {
pub fn receipts(&self) -> Option<&[Receipt]> {
self.program_state.is_some().then(|| self.interpreter.receipts())
}
pub fn backtrace(&self) -> Option<Backtrace> {
self.receipts()
.and_then(|r| r.iter().find_map(Receipt::result))
.copied()
.map(|result| Backtrace::from_vm_error(&self.interpreter, result))
}
}
impl<S, Tx> Transactor<S, Tx>
where
S: InterpreterStorage,
{
pub fn deploy(&mut self, checked: Checked<Create>) -> Result<Create, InterpreterError> {
self.interpreter.deploy(checked)
}
}
impl<S, Tx> Transactor<S, Tx>
where
S: InterpreterStorage,
Tx: ExecutableTransaction,
<Tx as IntoChecked>::Metadata: CheckedMetadata,
{
pub fn transact(&mut self, tx: Checked<Tx>) -> &mut Self {
match self.interpreter.transact(tx) {
Ok(s) => {
self.program_state.replace(s.into());
self.error.take();
}
Err(e) => {
self.program_state.take();
self.error.replace(e);
}
}
self
}
}
impl<S, Tx> From<Interpreter<S, Tx>> for Transactor<S, Tx>
where
Tx: ExecutableTransaction,
{
fn from(interpreter: Interpreter<S, Tx>) -> Self {
let program_state = None;
let error = None;
Self {
interpreter,
program_state,
error,
}
}
}
impl<S, Tx> From<Transactor<S, Tx>> for Interpreter<S, Tx>
where
Tx: ExecutableTransaction,
{
fn from(transactor: Transactor<S, Tx>) -> Self {
transactor.interpreter
}
}
impl<S, Tx> AsRef<Interpreter<S, Tx>> for Transactor<S, Tx>
where
Tx: ExecutableTransaction,
{
fn as_ref(&self) -> &Interpreter<S, Tx> {
&self.interpreter
}
}
impl<S, Tx> AsRef<S> for Transactor<S, Tx>
where
Tx: ExecutableTransaction,
{
fn as_ref(&self) -> &S {
self.interpreter.as_ref()
}
}
impl<S, Tx> AsMut<S> for Transactor<S, Tx>
where
Tx: ExecutableTransaction,
{
fn as_mut(&mut self) -> &mut S {
self.interpreter.as_mut()
}
}
impl<S, Tx> Default for Transactor<S, Tx>
where
S: Default,
Tx: ExecutableTransaction,
{
fn default() -> Self {
Self::new(Default::default(), Default::default())
}
}