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
macro_rules! ok {
($e:expr $(,)?) => {
match $e {
core::result::Result::Ok(val) => val,
core::result::Result::Err(err) => return core::result::Result::Err(err),
}
};
}
macro_rules! offset_of {
($ty: path, $field: tt) => {{
let $ty { $field: _, .. };
let uninit = ::std::mem::MaybeUninit::<$ty>::uninit();
let base_ptr = uninit.as_ptr() as *const $ty;
unsafe {
let field_ptr = std::ptr::addr_of!((*base_ptr).$field);
(field_ptr as *const u8).offset_from(base_ptr as *const u8) as usize
}
}};
}
pub use self::boc::Boc;
pub use self::cell::builder::CellBuilder;
pub use self::cell::rc::{RcCell, RcCellFamily};
pub use self::cell::slice::CellSlice;
pub use self::cell::sync::{ArcCell, ArcCellFamily};
pub use self::cell::{Cell, CellDescriptor, CellHash, LevelMask};
pub type ArcBoc = Boc<ArcCellFamily>;
pub type RcBoc = Boc<RcCellFamily>;
pub type ArcCellBuilder = CellBuilder<ArcCellFamily>;
pub type RcCellBuilder = CellBuilder<RcCellFamily>;
pub type ArcCellSlice<'a> = CellSlice<'a, ArcCellFamily>;
pub type RcCellSlice<'a> = CellSlice<'a, RcCellFamily>;
pub mod boc;
pub mod cell;
pub mod util;
pub fn emit(cell: &dyn Cell<RcCellFamily>) -> Vec<u8> {
Boc::<RcCellFamily>::encode(cell)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn correct_deserialization() {
let data = base64::decode("te6ccgEBBAEAzwACg4AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAIBAEAAAAAAAAAAAAAAAAAAAAAAAAAAm2c6ClpzoTVSAHvzVQGDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHKq1w7OAAkYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACRwAwBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEljGP8=").unwrap();
let arc_cell = ArcBoc::decode(&data).unwrap();
let rc_cell = RcBoc::decode(&data).unwrap();
assert_eq!(arc_cell.as_ref(), rc_cell.as_ref());
assert_eq!(
arc_cell.repr_hash(),
&[
0x63, 0xd4, 0x75, 0x13, 0x9a, 0xc1, 0x4f, 0x3e, 0xfe, 0x69, 0x0e, 0xd7, 0xfd, 0x4f,
0xf0, 0x02, 0x1c, 0xf2, 0x6b, 0xc4, 0xab, 0xd0, 0xaf, 0x01, 0x40, 0xa3, 0xb4, 0xc8,
0x95, 0xf0, 0x73, 0x76
]
);
let serialized = RcBoc::encode(rc_cell.as_ref());
assert_eq!(serialized, data);
}
#[test]
fn cell_slices() {
let data = base64::decode(
"te6ccgEBAQEALQAAVb23gAA3/WsCOdnvw2dedGrVhjTaZxn/TYcWb7TR8Im/MkK13n6c883gt8A=",
)
.unwrap();
let cell = RcBoc::decode(data).unwrap();
let mut slice = cell.as_slice();
assert!(!slice.is_data_empty());
assert_eq!(slice.remaining_bits(), 337);
assert!(slice.is_refs_empty());
assert_eq!(slice.remaining_refs(), 0);
assert!(slice.reference(0).is_none());
assert!(slice.reference_cloned(0).is_none());
assert!(slice.get_next_reference().is_none());
assert!(slice.get_next_reference_cloned().is_none());
assert_eq!(slice.get_bit(0), Some(true));
assert_eq!(slice.get_next_bit(), Some(true));
assert_eq!(slice.get_bits(0, 8), Some(123));
assert_eq!(slice.get_bits(8, 8), Some(111));
assert_eq!(slice.get_next_u16(), Some(0x7b6f));
assert_eq!(slice.get_u32(0), Some(0x00006ffa));
assert_eq!(slice.get_u32(32), Some(0xd60473b3));
assert_eq!(slice.get_next_u64(), Some(0x6ffad60473b3));
assert_eq!(
slice.get_next_u256(),
Some([
0xdf, 0x86, 0xce, 0xbc, 0xe8, 0xd5, 0xab, 0x0c, 0x69, 0xb4, 0xce, 0x33, 0xfe, 0x9b,
0x0e, 0x2c, 0xdf, 0x69, 0xa3, 0xe1, 0x13, 0x7e, 0x64, 0x85, 0x6b, 0xbc, 0xfd, 0x39,
0xe7, 0x9b, 0xc1, 0x6f,
])
);
assert_eq!(slice.get_bits(0, 1), None);
}
#[test]
fn test_builder() {
let data = base64::decode("te6ccgEBAQEAAwAAAbE=").unwrap();
let parsed_cell = Boc::<RcCellFamily>::decode(data).unwrap();
let mut builder = CellBuilder::<RcCellFamily>::new();
assert!(builder.store_bit_true());
assert!(builder.store_bit_zero());
assert!(builder.store_bit_true());
assert!(builder.store_bit_true());
assert!(builder.store_bit_zero());
assert!(builder.store_bit_zero());
assert!(builder.store_bit_zero());
let built_cell = builder.build().unwrap();
assert_eq!(parsed_cell.repr_hash(), built_cell.repr_hash());
let data = base64::decode("te6ccgEBAQEAggAA////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////").unwrap();
let parsed_cell = RcBoc::decode(data).unwrap();
let mut builder = RcCellBuilder::new();
for _ in 0..cell::MAX_BIT_LEN {
assert!(builder.store_bit_true());
}
assert!(!builder.store_bit_true());
let built_cell = builder.build().unwrap();
assert_eq!(parsed_cell.repr_hash(), built_cell.repr_hash());
let mut builder = RcCellBuilder::new();
assert!(builder.store_bit_true());
assert!(builder.store_u128(0xaaffaaffaaffaaffaaffaaffaaffaaff));
let cell = builder.build().unwrap();
let mut builder = RcCellBuilder::new();
assert!(builder.store_bit_true());
assert!(builder.store_u64(0xaaffaaffaaffaaff));
assert!(builder.store_u64(0xaaffaaffaaffaaff));
assert_eq!(cell.as_ref(), builder.build().unwrap().as_ref());
let mut builder = RcCellBuilder::new();
assert!(builder.store_zeros(1020));
assert!(builder.store_small_uint(0x5, 3));
builder.build().unwrap();
let mut builder = RcCellBuilder::new();
assert!(builder.store_small_uint(5, 3));
assert!(builder.store_u256(&[
0xdf, 0x86, 0xce, 0xbc, 0xe8, 0xd5, 0xab, 0x0c, 0x69, 0xb4, 0xce, 0x33, 0xfe, 0x9b,
0x0e, 0x2c, 0xdf, 0x69, 0xa3, 0xe1, 0x13, 0x7e, 0x64, 0x85, 0x6b, 0xbc, 0xfd, 0x39,
0xe7, 0x9b, 0xc1, 0x6f,
]));
let cell = builder.build().unwrap();
let target_cell = RcBoc::decode(
base64::decode("te6ccgEBAQEAIwAAQbvw2dedGrVhjTaZxn/TYcWb7TR8Im/MkK13n6c883gt8A==")
.unwrap(),
)
.unwrap();
assert_eq!(cell.as_ref(), target_cell.as_ref());
let mut builder = RcCellBuilder::new();
assert!(builder.store_zeros(3));
assert!(builder.store_raw(&[0xdd, 0x55], 10));
assert!(builder.store_reference(target_cell));
assert!(builder.store_reference(cell));
let cell = builder.build().unwrap();
let mut builder = RcCellBuilder::new();
assert!(builder.store_slice(cell.as_slice()));
let cell = builder.build().unwrap();
println!("{}", cell.display_tree());
}
}