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
use crate::cell::*;
use crate::dict::*;
use crate::error::*;
use crate::num::*;
use crate::util::*;
use crate::models::currency::CurrencyCollection;
use crate::models::message::IntAddr;
use crate::models::Lazy;
#[derive(Debug, Default, Clone, Eq, PartialEq, Store, Load)]
pub struct StorageUsed {
pub cells: VarUint56,
pub bits: VarUint56,
pub public_cells: VarUint56,
}
impl StorageUsed {
pub const ZERO: Self = Self {
cells: VarUint56::ZERO,
bits: VarUint56::ZERO,
public_cells: VarUint56::ZERO,
};
}
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Store, Load)]
pub struct StorageUsedShort {
pub cells: VarUint56,
pub bits: VarUint56,
}
impl StorageUsedShort {
pub const ZERO: Self = Self {
cells: VarUint56::ZERO,
bits: VarUint56::ZERO,
};
}
#[derive(Debug, Default, Clone, Eq, PartialEq, Store, Load)]
pub struct StorageInfo {
pub used: StorageUsed,
pub last_paid: u32,
pub due_payment: Option<Tokens>,
}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum AccountStatus {
Uninit = 0b00,
Frozen = 0b01,
Active = 0b10,
NotExists = 0b11,
}
impl AccountStatus {
pub const BITS: u16 = 2;
}
impl<C: CellFamily> Store<C> for AccountStatus {
#[inline]
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 AccountStatus {
#[inline]
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
let ty = slice.load_small_uint(2)?;
Some(match ty {
0b00 => Self::Uninit,
0b01 => Self::Frozen,
0b10 => Self::Active,
0b11 => Self::NotExists,
_ => return None,
})
}
}
#[derive(CustomDebug, CustomClone, CustomEq, Store, Load)]
pub struct ShardAccount<C: CellFamily> {
pub account: Lazy<C, OptionalAccount<C>>,
#[debug(with = "DisplayHash")]
pub last_trans_hash: CellHash,
pub last_trans_lt: u64,
}
impl<C: CellFamily> ShardAccount<C> {
pub fn load_account(&self) -> Result<Option<Account<C>>, Error> {
match self.account.load() {
Some(OptionalAccount(account)) => Ok(account),
None => Err(Error::CellUnderflow),
}
}
}
#[derive(CustomDebug, CustomClone, CustomEq)]
pub struct OptionalAccount<C: CellFamily>(pub Option<Account<C>>);
impl<C: CellFamily> Default for OptionalAccount<C> {
#[inline]
fn default() -> Self {
Self(None)
}
}
impl<C: CellFamily> OptionalAccount<C> {
pub const EMPTY: Self = Self(None);
}
impl<C: CellFamily> Store<C> for OptionalAccount<C> {
fn store_into(&self, builder: &mut CellBuilder<C>, finalizer: &mut dyn Finalizer<C>) -> bool {
match &self.0 {
None => builder.store_bit_zero(),
Some(account) => {
let with_init_code_hash = account.init_code_hash.is_some();
let prefix_stored = if with_init_code_hash {
builder.store_small_uint(0b0001, 4)
} else {
builder.store_bit_one()
};
prefix_stored
&& account.address.store_into(builder, finalizer)
&& account.storage_stat.store_into(builder, finalizer)
&& builder.store_u64(account.last_trans_lt)
&& account.balance.store_into(builder, finalizer)
&& account.state.store_into(builder, finalizer)
&& if let Some(init_code_hash) = &account.init_code_hash {
builder.store_bit_one() && builder.store_u256(init_code_hash)
} else {
true
}
}
}
}
}
impl<'a, C: CellFamily> Load<'a, C> for OptionalAccount<C> {
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
let with_init_code_hash = if slice.load_bit()? {
false } else if slice.is_data_empty() {
return Some(Self::EMPTY);
} else {
let tag = slice.load_small_uint(3)?;
match tag {
0 => false, 1 => true, _ => return None,
}
};
Some(Self(Some(Account {
address: IntAddr::load_from(slice)?,
storage_stat: StorageInfo::load_from(slice)?,
last_trans_lt: slice.load_u64()?,
balance: CurrencyCollection::load_from(slice)?,
state: AccountState::load_from(slice)?,
init_code_hash: if with_init_code_hash {
Option::<CellHash>::load_from(slice)?
} else {
None
},
})))
}
}
#[derive(CustomDebug, CustomClone, CustomEq)]
pub struct Account<C: CellFamily> {
pub address: IntAddr,
pub storage_stat: StorageInfo,
pub last_trans_lt: u64,
pub balance: CurrencyCollection<C>,
pub state: AccountState<C>,
#[debug(with = "DisplayOptionalHash")]
pub init_code_hash: Option<CellHash>,
}
#[derive(CustomDebug, CustomClone, CustomEq)]
pub enum AccountState<C: CellFamily> {
Uninit,
Active(StateInit<C>),
Frozen(CellHash),
}
impl<C: CellFamily> Store<C> for AccountState<C> {
fn store_into(&self, builder: &mut CellBuilder<C>, finalizer: &mut dyn Finalizer<C>) -> bool {
match self {
Self::Uninit => builder.store_small_uint(0b00, 2),
Self::Active(state) => builder.store_bit_one() && state.store_into(builder, finalizer),
Self::Frozen(hash) => builder.store_small_uint(0b01, 2) && builder.store_u256(hash),
}
}
}
impl<'a, C: CellFamily> Load<'a, C> for AccountState<C> {
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
Some(if slice.load_bit()? {
Self::Active(StateInit::<C>::load_from(slice)?)
} else if slice.load_bit()? {
Self::Frozen(slice.load_u256()?)
} else {
Self::Uninit
})
}
}
#[derive(CustomDebug, CustomClone, CustomEq, Store, Load)]
pub struct StateInit<C: CellFamily> {
pub split_depth: Option<SplitDepth>,
pub special: Option<SpecialFlags>,
pub code: Option<CellContainer<C>>,
pub data: Option<CellContainer<C>>,
pub libraries: Dict<C, CellHash, SimpleLib<C>>,
}
impl<C: CellFamily> Default for StateInit<C> {
fn default() -> Self {
Self {
split_depth: None,
special: None,
code: None,
data: None,
libraries: Dict::new(),
}
}
}
impl<C: CellFamily> StateInit<C> {
pub const fn bit_len(&self) -> u16 {
(1 + self.split_depth.is_some() as u16 * SplitDepth::BITS)
+ (1 + self.special.is_some() as u16 * SpecialFlags::BITS)
+ 3
}
pub const fn reference_count(&self) -> u8 {
self.code.is_some() as u8 + self.data.is_some() as u8 + !self.libraries.is_empty() as u8
}
}
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq)]
pub struct SpecialFlags {
pub tick: bool,
pub tock: bool,
}
impl SpecialFlags {
pub const BITS: u16 = 2;
}
impl<C: CellFamily> Store<C> for SpecialFlags {
fn store_into(&self, builder: &mut CellBuilder<C>, _: &mut dyn Finalizer<C>) -> bool {
builder.store_small_uint(((self.tick as u8) << 1) | self.tock as u8, 2)
}
}
impl<'a, C: CellFamily> Load<'a, C> for SpecialFlags {
fn load_from(slice: &mut CellSlice<'a, C>) -> Option<Self> {
let data = slice.load_small_uint(2)?;
Some(Self {
tick: data & 0b10 != 0,
tock: data & 0b01 != 0,
})
}
}
#[derive(CustomDebug, CustomClone, CustomEq, Store, Load)]
pub struct SimpleLib<C: CellFamily> {
pub public: bool,
pub root: CellContainer<C>,
}