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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
use crate::cell::*;
use crate::num::*;
use crate::util::*;
use crate::models::account::StorageUsedShort;
use crate::models::currency::CurrencyCollection;
#[derive(Debug, Clone, Eq, PartialEq, Store, Load)]
pub struct StoragePhase {
pub storage_fees_collected: Tokens,
pub storage_fees_due: Option<Tokens>,
pub status_change: AccountStatusChange,
}
#[derive(CustomDebug, CustomClone, CustomEq, Store, Load)]
pub struct CreditPhase<C: CellFamily> {
pub due_fees_collected: Option<Tokens>,
pub credit: CurrencyCollection<C>,
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum ComputePhase {
Skipped(SkippedComputePhase),
Executed(ExecutedComputePhase),
}
impl<C: CellFamily> Store<C> for ComputePhase {
fn store_into(&self, builder: &mut CellBuilder<C>, finalizer: &mut dyn Finalizer<C>) -> bool {
match self {
Self::Skipped(phase) => {
builder.store_bit_zero() && phase.store_into(builder, finalizer)
}
Self::Executed(phase) => {
let cell = {
let mut builder = CellBuilder::<C>::new();
if phase.gas_used.store_into(&mut builder, finalizer)
&& phase.gas_credit.store_into(&mut builder, finalizer)
&& builder.store_u8(phase.mode as u8)
&& builder.store_u32(phase.exit_code as u32)
&& phase.exit_arg.store_into(&mut builder, finalizer)
&& builder.store_u32(phase.vm_steps)
&& builder.store_u256(&phase.vm_init_state_hash)
&& builder.store_u256(&phase.vm_final_state_hash)
{
match builder.build_ext(finalizer) {
Some(cell) => cell,
None => return false,
}
} else {
return false;
}
};
let flags = 0b1000u8
| ((phase.success as u8) << 2)
| ((phase.msg_state_used as u8) << 1)
| (phase.account_activated as u8);
builder.store_small_uint(flags, 4)
&& phase.gas_fees.store_into(builder, finalizer)
&& builder.store_reference(cell)
}
}
}
}
impl<'a, C: CellFamily> Load<'a, C> for ComputePhase {
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
if !slice.load_bit()? {
return Some(Self::Skipped(SkippedComputePhase::load_from(slice)?));
}
let flags = slice.load_small_uint(3)?;
let gas_fees = Tokens::load_from(slice)?;
let slice = &mut slice.load_reference()?.as_slice();
Some(Self::Executed(ExecutedComputePhase {
success: flags & 0b100 != 0,
msg_state_used: flags & 0b010 != 0,
account_activated: flags & 0b001 != 0,
gas_fees,
gas_used: VarUint56::load_from(slice)?,
gas_limit: VarUint56::load_from(slice)?,
gas_credit: Option::<VarUint24>::load_from(slice)?,
mode: slice.load_u8()? as i8,
exit_code: slice.load_u32()? as i32,
exit_arg: Option::<i32>::load_from(slice)?,
vm_steps: slice.load_u32()?,
vm_init_state_hash: slice.load_u256()?,
vm_final_state_hash: slice.load_u256()?,
}))
}
}
#[derive(CustomDebug, Clone, Eq, PartialEq)]
pub struct ExecutedComputePhase {
pub success: bool,
pub msg_state_used: bool,
pub account_activated: bool,
pub gas_fees: Tokens,
pub gas_used: VarUint56,
pub gas_limit: VarUint56,
pub gas_credit: Option<VarUint24>,
pub mode: i8,
pub exit_code: i32,
pub exit_arg: Option<i32>,
pub vm_steps: u32,
#[debug(with = "DisplayHash")]
pub vm_init_state_hash: CellHash,
#[debug(with = "DisplayHash")]
pub vm_final_state_hash: CellHash,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Store, Load)]
pub struct SkippedComputePhase {
pub reason: ComputePhaseSkipReason,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ComputePhaseSkipReason {
NoState = 0b00,
BadState = 0b01,
NoGas = 0b10,
}
impl<C: CellFamily> Store<C> for ComputePhaseSkipReason {
fn store_into(&self, builder: &mut CellBuilder<C>, _: &mut dyn Finalizer<C>) -> bool {
builder.store_small_uint(*self as u8, 2)
}
}
impl<'a, C: CellFamily> Load<'a, C> for ComputePhaseSkipReason {
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
let ty = slice.load_small_uint(2)?;
Some(match ty {
0b00 => Self::NoState,
0b01 => Self::BadState,
0b10 => Self::NoGas,
_ => return None,
})
}
}
#[derive(CustomDebug, Clone, Eq, PartialEq)]
pub struct ActionPhase {
pub success: bool,
pub valid: bool,
pub no_funds: bool,
pub status_change: AccountStatusChange,
pub total_fwd_fees: Option<Tokens>,
pub total_action_fees: Option<Tokens>,
pub result_code: i32,
pub result_arg: Option<i32>,
pub total_actions: u16,
pub special_actions: u16,
pub skipped_actions: u16,
pub messages_created: u16,
#[debug(with = "DisplayHash")]
pub action_list_hash: CellHash,
pub total_message_size: StorageUsedShort,
}
impl<C: CellFamily> Store<C> for ActionPhase {
fn store_into(&self, builder: &mut CellBuilder<C>, finalizer: &mut dyn Finalizer<C>) -> bool {
let flags = ((self.success as u8) << 2) | ((self.valid as u8) << 1) | self.no_funds as u8;
let counts = ((self.total_actions as u64) << 48)
| ((self.special_actions as u64) << 32)
| ((self.skipped_actions as u64) << 16)
| self.messages_created as u64;
builder.store_small_uint(flags, 3)
&& self.status_change.store_into(builder, finalizer)
&& self.total_fwd_fees.store_into(builder, finalizer)
&& self.total_action_fees.store_into(builder, finalizer)
&& builder.store_u32(self.result_code as u32)
&& self.result_arg.store_into(builder, finalizer)
&& builder.store_u64(counts)
&& builder.store_u256(&self.action_list_hash)
&& self.total_message_size.store_into(builder, finalizer)
}
}
impl<'a, C: CellFamily> Load<'a, C> for ActionPhase {
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
let flags = slice.load_small_uint(3)?;
let status_change = AccountStatusChange::load_from(slice)?;
let total_fwd_fees = Option::<Tokens>::load_from(slice)?;
let total_action_fees = Option::<Tokens>::load_from(slice)?;
let result_code = slice.load_u32()? as i32;
let result_arg = Option::<i32>::load_from(slice)?;
let counts = slice.load_u64()?;
Some(Self {
success: flags & 0b100 != 0,
valid: flags & 0b010 != 0,
no_funds: flags & 0b001 != 0,
status_change,
total_fwd_fees,
total_action_fees,
result_code,
result_arg,
total_actions: (counts >> 48) as u16,
special_actions: (counts >> 32) as u16,
skipped_actions: (counts >> 16) as u16,
messages_created: counts as u16,
action_list_hash: slice.load_u256()?,
total_message_size: StorageUsedShort::load_from(slice)?,
})
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum BouncePhase {
NegativeFunds,
NoFunds(NoFundsBouncePhase),
Executed(ExecutedBouncePhase),
}
impl<C: CellFamily> Store<C> for BouncePhase {
fn store_into(&self, builder: &mut CellBuilder<C>, finalizer: &mut dyn Finalizer<C>) -> bool {
match self {
Self::NegativeFunds => builder.store_small_uint(0b00, 2),
Self::NoFunds(phase) => {
builder.store_small_uint(0b01, 2) && phase.store_into(builder, finalizer)
}
Self::Executed(phase) => {
builder.store_bit_one() && phase.store_into(builder, finalizer)
}
}
}
}
impl<'a, C: CellFamily> Load<'a, C> for BouncePhase {
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
Some(if slice.load_bit()? {
Self::Executed(ExecutedBouncePhase::load_from(slice)?)
} else if slice.load_bit()? {
Self::NoFunds(NoFundsBouncePhase::load_from(slice)?)
} else {
Self::NegativeFunds
})
}
}
#[derive(Debug, Clone, Eq, PartialEq, Store, Load)]
pub struct NoFundsBouncePhase {
pub msg_size: StorageUsedShort,
pub req_fwd_fees: Tokens,
}
#[derive(Debug, Clone, Eq, PartialEq, Store, Load)]
pub struct ExecutedBouncePhase {
pub msg_size: StorageUsedShort,
pub msg_fees: Tokens,
pub fwd_fees: Tokens,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum AccountStatusChange {
Unchanged = 0b0,
Frozen = 0b10,
Deleted = 0b11,
}
impl<C: CellFamily> Store<C> for AccountStatusChange {
fn store_into(&self, builder: &mut CellBuilder<C>, _: &mut dyn Finalizer<C>) -> bool {
if *self == Self::Unchanged {
builder.store_bit_zero()
} else {
builder.store_small_uint(*self as u8, 2)
}
}
}
impl<'a, C: CellFamily> Load<'a, C> for AccountStatusChange {
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
Some(if !slice.load_bit()? {
Self::Unchanged
} else if slice.load_bit()? {
Self::Deleted
} else {
Self::Frozen
})
}
}