-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.fc
527 lines (425 loc) · 14.6 KB
/
3.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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
{-
TASK 3 - Message validation.
Write the method that checks that cell contains valid message
in accordance to https://github.com/ton-blockchain/ton/blob/master/crypto/block/block.tlb#L155
Note, tests will only cover valid and invalid MessageAny objects,
valid MessageRelaxed (which are not simultaneously MessageAny) will not be presented
If message is invalid for any reason - method should return (0, null), otherwise
it should return (-1, [slice src, slice dest, int amount] ), where src, dest and
amount represents source of the message, destination of the message and attached
amount of TONs. If any of those values are not presented (or presented as
addr_none slice) in the message cell - they should be substituted with null.
It is guaranteed that for all tests any HashmapE datatype in message structure
is empty hashmaps (has hme_empty constructor).
Documentation on TL-B schemes can be found in https://ton.org/docs/#/overviews/TL-B
-}
() recv_internal() {
}
(slice, int, slice, slice, int) validate_common_msg_info(slice cs);
(slice, int, slice, slice, int) validate_internal_info(slice cs);
(slice, int, slice, slice, int) validate_external_in_info(slice cs);
(slice, int, slice, slice, int) validate_external_out_info(slice cs);
(slice, int) validate_init(slice cs);
(slice, int) validate_body(slice cs);
(slice, int, slice) read_msg_addr_int(slice cs);
(slice, int, slice) read_msg_addr_ext(slice cs);
(slice, int, slice) read_extarnal_msg_addr(slice cs);
(slice, int, slice) read_std_internal_msg_addr(slice cs);
(slice, int, slice) read_var_internal_msg_addr(slice cs);
(slice, int, int, int) read_any_cast_info(slice cs);
(slice, int, int) read_grams(slice cs);
(slice, int, int) read_bits(slice cs, int len);
(slice, int) ignore_bits(slice cs, int len);
;; testable
(int, tuple) validate_message(cell message) method_id {
slice cs = message.begin_parse();
(cs, int valid, slice src, slice dst, int amount) = cs.validate_common_msg_info();
if (valid == 0) {
return (0, null());
}
(cs, valid) = cs.validate_init();
if (valid == 0) {
return (0, null());
}
(cs, valid) = cs.validate_body();
if (valid == 0) {
return (0, null());
}
return (-1, unsafe_tuple([src, dst, amount]));
}
(slice, int, slice, slice, int) validate_common_msg_info(slice cs) {
;; int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool
;; src:MsgAddressInt dest:MsgAddressInt
;; value:CurrencyCollection ihr_fee:Grams fwd_fee:Grams
;; created_lt:uint64 created_at:uint32 = CommonMsgInfo;
;; ext_in_msg_info$10 src:MsgAddressExt dest:MsgAddressInt
;; import_fee:Grams = CommonMsgInfo;
;; ext_out_msg_info$11 src:MsgAddressInt dest:MsgAddressExt
;; created_lt:uint64 created_at:uint32 = CommonMsgInfo;
if (cs.slice_bits() < 1) {
return (cs, 0, null(), null(), 0);
}
var int_ext = cs~load_uint(1);
if (int_ext == 0) {
;; Internal message
(cs, int valid, slice src, slice dst, int amount) = cs.validate_internal_info();
return (cs, valid, src, dst, amount);
} else {
;; External message
if (cs.slice_bits() < 1) {
return (cs, 0, null(), null(), 0);
}
var in_out = cs~load_uint(1);
if (in_out == 0) {
(cs, int valid, slice src, slice dst, int amount) = cs.validate_external_in_info();
return (cs, valid, src, dst, amount);
} else {
(cs, int valid, slice src, slice dst, int amount) = cs.validate_external_out_info();
return (cs, valid, src, dst, amount);
}
}
}
(slice, int, slice, slice, int) validate_internal_info(slice cs) {
;; Internal message
;;
;; int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool
;; src:MsgAddressInt dest:MsgAddressInt
;; value:CurrencyCollection ihr_fee:Grams fwd_fee:Grams
;; created_lt:uint64 created_at:uint32 = CommonMsgInfo;
if (cs.slice_bits() < 3) {
return (cs, 0, null(), null(), 0);
}
var ihr_disabled = cs~load_uint(1);
var bounce = cs~load_uint(1);
var bounced = cs~load_uint(1);
var (cs, valid, src) = cs.read_msg_addr_int();
if (valid == 0) {
return (cs, 0, null(), null(), 0);
}
var (cs, valid, dst) = cs.read_msg_addr_int();
if (valid == 0) {
return (cs, 0, null(), null(), 0);
}
var (cs, valid, value) = cs.read_grams();
if (valid == 0) {
return (cs, 0, null(), null(), 0);
}
var extra = cs~load_dict(); ;; The tests assumes that theree is no extra currency.
var (cs, valid, ihr_fee) = cs.read_grams();
if (valid == 0) {
return (cs, 0, null(), null(), 0);
}
var (cs, valid, fwd_fee) = cs.read_grams();
if (valid == 0) {
return (cs, 0, null(), null(), 0);
}
if (cs.slice_bits() < (32 + 64)) {
return (cs, 0, null(), null(), 0);
}
var logical_time = cs~load_uint(64);
var created_time = cs~load_uint(32);
if (valid) {
return (cs, -1, src, dst, value);
} else {
return (cs, 0, null(), null(), 0);
}
}
(slice, int, slice, slice, int) validate_external_in_info(slice cs) {
;; external in bound message.
;;
;; ext_in_msg_info$10 src:MsgAddressExt dest:MsgAddressInt
;; import_fee:Grams = CommonMsgInfo;
var (cs, valid, src) = cs.read_msg_addr_ext();
if (valid == 0) {
return (cs, 0, null(), null(), 0);
}
var (cs, alid, dst) = cs.read_msg_addr_int();
if (valid == 0) {
return (cs, 0, null(), null(), 0);
}
var (cs, valid, amount) = cs.read_grams();
if (valid == 0) {
return (cs, 0, null(), null(), 0);
}
return (cs, -1, src, dst, amount);
}
(slice, int, slice, slice, int) validate_external_out_info(slice cs) {
;; external out bound message
;;
;; ext_out_msg_info$11 src:MsgAddressInt dest:MsgAddressExt
;; created_lt:uint64 created_at:uint32 = CommonMsgInfo;
var (cs, valid, src) = cs.read_msg_addr_int();
if (valid == 0) {
return (cs, 0, null(), null(), 0);
}
var (cs, valiid, dst) = cs.read_msg_addr_ext();
if (valid == 0) {
return (cs, 0, null(), null(), 0);
}
var logical_time = cs~load_uint(64);
var created_time = cs~load_uint(32);
return (cs, -1, src, dst, null());
}
(slice, int, slice) read_msg_addr_int(slice cs) {
;; anycast_info$_ depth:(#<= 30) { depth >= 1 }
;; rewrite_pfx:(bits depth) = Anycast;
;; addr_std$10 anycast:(Maybe Anycast)
;; workchain_id:int8 address:bits256 = MsgAddressInt;
;; addr_var$11 anycast:(Maybe Anycast) addr_len:(## 9)
;; workchain_id:int32 address:(bits addr_len) = MsgAddressInt;
;; _ _:MsgAddressInt = MsgAddress;
if (cs.slice_bits() < 2) {
return (cs, 0, null());
}
int addr_type = cs~load_uint(2);
if (addr_type == 2) {
(cs, int valid, slice addr) = cs.read_std_internal_msg_addr();
return (cs, valid, addr);
}
if (addr_type == 3) {
(cs, int valid, slice addr) = cs.read_var_internal_msg_addr();
return (cs, valid, addr);
}
return (cs, 0, null());
}
(slice, int, slice) read_msg_addr_ext(slice cs) {
;; addr_none$00 = MsgAddressExt;
;; addr_extern$01 len:(## 9) external_address:(bits len)
;; = MsgAddressExt;
;; _ _:MsgAddressExt = MsgAddress;
if (cs.slice_bits() < 2) {
return (cs, 0, null());
}
int addr_type = cs~load_uint(2);
if (addr_type == 0) {
return (cs, -1, null()); ;; Valid but empty address
}
if (addr_type == 1) {
(cs, var valid, slice addr) = cs.read_extarnal_msg_addr();
return (cs, valid, addr);
}
return (cs, 0, null());
}
(slice, int, slice) read_extarnal_msg_addr(slice cs) {
;; addr_extern$01 len:(## 9) external_address:(bits len)
;; = MsgAddressExt;
if (cs.slice_bits() < 9) {
return (cs, 0, null());
}
var len = cs~load_uint(9);
if (cs.slice_bits() < len) {
return (cs, 0, null());
}
var ext_addr = cs~load_uint(len);
var addr = begin_cell()
.store_uint(1, 2) ;; 01 = external
.store_uint(len, 9) ;; len
.store_uint(ext_addr, len) ;; external address
.end_cell();
return (cs, -1, addr.begin_parse());
}
(slice, int, slice) read_std_internal_msg_addr(slice cs) {
;; anycast_info$_ depth:(#<= 30) { depth >= 1 }
;; rewrite_pfx:(bits depth) = Anycast;
;; addr_std$10 anycast:(Maybe Anycast)
;; workchain_id:int8 address:bits256 = MsgAddressInt;
int valid = -1;
int info_len = 0;
int info_bits = 0;
if (cs.slice_bits() < 1) {
return (cs, 0, null());
}
int maybe = cs~load_uint(1);
if (maybe == 1) {
(cs, valid, info_len, info_bits) = cs.read_any_cast_info();
}
if (valid == 0) {
return (cs, 0, null());
}
if (cs.slice_bits() < (8 + 256)) {
return (cs, 0, null());
}
var workchain = cs~load_int(8);
var addr_bits = cs~load_uint(256);
var bldr = begin_cell()
.store_uint(2, 2) ;; 02 = standard internal
.store_uint(maybe, 1);
if (maybe == 1) {
bldr~store_uint(info_len, 5);
if (info_len >= 1) {
bldr~store_uint(info_bits, info_len);
}
}
bldr.store_int(workchain, 8) ;; workchain
.store_uint(addr_bits, 256); ;; address bits
cell addr = bldr.end_cell();
return (cs, -1, addr.begin_parse());
}
(slice, int, slice) read_var_internal_msg_addr(slice cs) {
;; anycast_info$_ depth:(#<= 30) { depth >= 1 }
;; rewrite_pfx:(bits depth) = Anycast;
;; addr_var$11 anycast:(Maybe Anycast) addr_len:(## 9)
;; workchain_id:int32 address:(bits addr_len) = MsgAddressInt;
int valid = -1;
int info_len = 0;
int info_bits = 0;
if (cs.slice_bits() < 1) {
return (cs, 0, null());
}
int maybe = cs~load_uint(1);
if (maybe == 1) {
(cs, valid, info_len, info_bits) = cs.read_any_cast_info();
}
if (valid == 0) {
return (cs, 0, null());
}
if (cs.slice_bits() < 9) {
return (cs, 0, null());
}
var addr_len = cs~load_int(9);
if (cs.slice_bits() < (32 + addr_len)) {
return (cs, 0, null());
}
var workchain = cs~load_int(32);
var addr_bits = cs~load_uint(addr_len);
var bldr = begin_cell()
.store_uint(3, 2) ;; 03 = variable internal
.store_uint(maybe, 1);
if (maybe == 1) {
bldr~store_uint(info_len, 5);
if (info_len >= 1) {
bldr~store_uint(info_bits, info_len);
}
}
bldr.store_int(addr_len, 9) ;; addreess length
.store_int(workchain, 32) ;; workchain
.store_uint(addr_bits, addr_len); ;; address bits
cell addr = bldr.end_cell();
return (cs, -1, addr.begin_parse());
}
(slice, int, int, int) read_any_cast_info(slice cs) {
;; anycast_info$_ depth:(#<= 30) { depth >= 1 }
;; rewrite_pfx:(bits depth) = Anycast;
if (cs.slice_bits() < 5) {
return (cs, 0, 0, 0);
}
int dept = cs~load_uint(5);
if (dept > 30) {
return (cs, 0, 0, 0);
}
if (cs.slice_bits() < dept) {
return (cs, 0, 0, 0);
}
int pfx = null();
if (dept >= 1) {
pfx = cs~load_uint(dept);
}
return (cs, -1, dept, pfx);
}
(slice, int, int) read_grams(slice cs) {
;; var_uint$_ {n:#} len:(#< n) value:(uint (len * 8))
;; = VarUInteger n;
;; nanograms$_ amount:(VarUInteger 16) = Grams;
if (cs.slice_bits() < 4) {
return (cs, 0, 0);
}
int len = cs~load_uint(4);
if (cs.slice_bits() < (len * 8)) {
return (cs, 0, 0);
}
int value = cs~load_uint(len * 8);
return (cs, -1, value);
}
(slice, int) validate_init(slice cs) {
;; tick_tock$_ tick:Bool tock:Bool = TickTock;
;;
;; _ split_depth:(Maybe (## 5)) special:(Maybe TickTock)
;; code:(Maybe ^Cell) data:(Maybe ^Cell)
;; library:(HashmapE 256 SimpleLib) = StateInit;
;; message$_ {X:Type} info:CommonMsgInfo
;; init:(Maybe (Either StateInit ^StateInit))
;; body:(Either X ^X) = Message X;
(cs, int valid, int bits) = cs.read_bits(1); ;; init:(MAYBE
if (valid == 0) {
return (cs, 0);
}
if (bits == 1) {
(cs, valid, bits) = cs.read_bits(1); ;; init:(Maybe EITHER
if (valid == 0) {
return (cs, 0);
}
if (bits == 0) {
(cs, valid, bits) = cs.read_bits(1); ;; split_depth:(MAYBE
if (valid == 0) {
return (cs, 0);
}
if (bits == 1) {
(cs, valid) = cs.ignore_bits(5); ;; split_dept: ## 5
}
if (valid == 0) {
return (cs, 0);
}
(cs, valid, bits) = cs.read_bits(1); ;; special:(Maybe
if (valid == 0) {
return (cs, 0);
}
if (bits == 1) {
(cs, valid) = cs.ignore_bits(2); ;; special: tick-tock
}
if (valid == 0) {
return (cs, 0);
}
(cs, valid, bits) = cs.read_bits(1); ;; code:(Maybe
if (valid == 0) {
return (cs, 0);
}
if (bits == 1) {
_ = cs~load_ref();
}
(cs, valid, bits) = cs.read_bits(1); ;; data:(Maybe
if (valid == 0) {
return (cs, 0);
}
if (bits == 1) {
_ = cs~load_ref();
}
(cs, valid, bits) = cs.read_bits(1); ;; Empty library
if (valid == 0) {
return (cs, 0);
}
if (bits != 0) { ;; The hashmap is empty in tests
return (cs, 0);
}
return (cs, -1);
} else {
_ = cs~load_ref();
return (cs, -1);
}
}
return (cs, -1);
}
(slice, int) validate_body(slice cs) {
(cs, int valid, int bits) = cs.read_bits(1);
if (valid == 0) {
return (cs, 0);
}
if (bits == 0) {
;; do nothing
} else {
cs~load_ref();
}
return (cs, -1);
}
(slice, int, int) read_bits(slice cs, int len) {
if (cs.slice_bits() < len) {
return (cs, 0, 0);
}
return (cs, -1, cs~load_uint(len));
}
(slice, int) ignore_bits(slice cs, int len) {
if (cs.slice_bits() < len) {
return (cs, 0);
}
cs~skip_bits(len);
return (cs, -1);
}