-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.fc
244 lines (234 loc) · 8.97 KB
/
1.fc
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
{-
TASK 1 - Find branch of the cell tree
Write the method that will find a branch of the tree by comparing its
hash with the hash received in the first parameter. When the algorithm finds
the subtree (branch) whose hash equals the received hash, the root cell of
this branch should be returned. Return empty cell if the branch is not found.
-}
() recv_internal() {
}
;; GasValue: 1232360
;; Points: 5.488395362775238
forall X -> int is_null (X x) asm "ISNULL";
forall X -> (tuple, ()) push_back (tuple tail, X head) asm "CONS";
forall X -> (tuple, (X)) pop_back (tuple t) asm "UNCONS";
cell empty_cell() asm "c4 PUSH"; ;; some trick to create empty cell with gas-efficient way
;; testable
(cell) find_branch_by_hash(int hash, cell tree) method_id {
if (tree.cell_hash() == hash) {
return tree;
}
tuple stack = null();
;; true iteration is not the most gas optimial, it is better to have a ladder. Yes, it looks weird...
;; And yes, code size and compilation time here is not optimal)
slice s0 = tree.begin_parse();
repeat (s0.slice_refs()) {
cell child1 = s0~load_ref();
if (child1.cell_hash() == hash) {
return child1;
}
slice s1 = child1.begin_parse();
repeat (s1.slice_refs()) {
cell child2 = s1~load_ref();
if (child2.cell_hash() == hash) {
return child2;
}
slice s2 = child2.begin_parse();
repeat (s2.slice_refs()) {
cell child3 = s2~load_ref();
if (child3.cell_hash() == hash) {
return child3;
}
slice s3 = child3.begin_parse();
repeat (s3.slice_refs()) {
cell child4 = s3~load_ref();
if (child4.cell_hash() == hash) {
return child4;
}
slice s4 = child4.begin_parse();
repeat (s4.slice_refs()) {
cell child5 = s4~load_ref();
if (child5.cell_hash() == hash) {
return child5;
}
slice s5 = child5.begin_parse();
repeat (s5.slice_refs()) {
cell child6 = s5~load_ref();
if (child6.cell_hash() == hash) {
return child6;
}
slice s6 = child6.begin_parse();
repeat (s6.slice_refs()) {
cell child7 = s6~load_ref();
if (child7.cell_hash() == hash) {
return child7;
}
slice s7 = child7.begin_parse();
repeat (s7.slice_refs()) {
cell child8 = s7~load_ref();
if (child8.cell_hash() == hash) {
return child8;
}
slice s8 = child8.begin_parse();
repeat (s8.slice_refs()) {
cell child9 = s8~load_ref();
if (child9.cell_hash() == hash) {
return child9;
}
slice s9 = child9.begin_parse();
repeat (s9.slice_refs()) {
cell child10 = s9~load_ref();
if (child10.cell_hash() == hash) {
return child10;
}
slice s10 = child10.begin_parse();
repeat (s10.slice_refs()) {
cell child11 = s10~load_ref();
if (child11.cell_hash() == hash) {
return child11;
}
slice s11 = child11.begin_parse();
repeat (s11.slice_refs()) {
cell child12 = s11~load_ref();
if (child12.cell_hash() == hash) {
return child12;
}
slice s12 = child12.begin_parse();
repeat (s12.slice_refs()) {
cell child13 = s12~load_ref();
if (child13.cell_hash() == hash) {
return child13;
}
slice s13 = child13.begin_parse();
repeat (s13.slice_refs()) {
cell child14 = s13~load_ref();
if (child14.cell_hash() == hash) {
return child14;
}
slice s14 = child14.begin_parse();
repeat (s14.slice_refs()) {
cell child15 = s14~load_ref();
if (child15.cell_hash() == hash) {
return child15;
}
stack~push_back(child15);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
;; first ladder goes without actual stack usage. It is still weird but it allows to save gas in case root cell has depth < 15
while (~ stack.is_null()) {
cell child0 = stack~pop_back();
slice s0 = child0.begin_parse();
repeat (s0.slice_refs()) {
cell child1 = s0~load_ref();
if (child1.cell_hash() == hash) {
return child1;
}
slice s1 = child1.begin_parse();
repeat (s1.slice_refs()) {
cell child2 = s1~load_ref();
if (child2.cell_hash() == hash) {
return child2;
}
slice s2 = child2.begin_parse();
repeat (s2.slice_refs()) {
cell child3 = s2~load_ref();
if (child3.cell_hash() == hash) {
return child3;
}
slice s3 = child3.begin_parse();
repeat (s3.slice_refs()) {
cell child4 = s3~load_ref();
if (child4.cell_hash() == hash) {
return child4;
}
slice s4 = child4.begin_parse();
repeat (s4.slice_refs()) {
cell child5 = s4~load_ref();
if (child5.cell_hash() == hash) {
return child5;
}
slice s5 = child5.begin_parse();
repeat (s5.slice_refs()) {
cell child6 = s5~load_ref();
if (child6.cell_hash() == hash) {
return child6;
}
slice s6 = child6.begin_parse();
repeat (s6.slice_refs()) {
cell child7 = s6~load_ref();
if (child7.cell_hash() == hash) {
return child7;
}
slice s7 = child7.begin_parse();
repeat (s7.slice_refs()) {
cell child8 = s7~load_ref();
if (child8.cell_hash() == hash) {
return child8;
}
slice s8 = child8.begin_parse();
repeat (s8.slice_refs()) {
cell child9 = s8~load_ref();
if (child9.cell_hash() == hash) {
return child9;
}
slice s9 = child9.begin_parse();
repeat (s9.slice_refs()) {
cell child10 = s9~load_ref();
if (child10.cell_hash() == hash) {
return child10;
}
slice s10 = child10.begin_parse();
repeat (s10.slice_refs()) {
cell child11 = s10~load_ref();
if (child11.cell_hash() == hash) {
return child11;
}
slice s11 = child11.begin_parse();
repeat (s11.slice_refs()) {
cell child12 = s11~load_ref();
if (child12.cell_hash() == hash) {
return child12;
}
slice s12 = child12.begin_parse();
repeat (s12.slice_refs()) {
cell child13 = s12~load_ref();
if (child13.cell_hash() == hash) {
return child13;
}
slice s13 = child13.begin_parse();
repeat (s13.slice_refs()) {
cell child14 = s13~load_ref();
if (child14.cell_hash() == hash) {
return child14;
}
stack~push_back(child14);
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return empty_cell();
}