forked from near/nearcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
1479 lines (1363 loc) · 50.7 KB
/
lib.rs
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#![allow(clippy::all)]
use std::mem::size_of;
#[allow(unused)]
extern "C" {
// #############
// # Registers #
// #############
fn read_register(register_id: u64, ptr: u64);
fn register_len(register_id: u64) -> u64;
// ###############
// # Context API #
// ###############
fn current_account_id(register_id: u64);
fn signer_account_id(register_id: u64);
fn signer_account_pk(register_id: u64);
fn predecessor_account_id(register_id: u64);
fn input(register_id: u64);
fn block_index() -> u64;
fn block_timestamp() -> u64;
fn epoch_height() -> u64;
fn storage_usage() -> u64;
// #################
// # Economics API #
// #################
fn account_balance(balance_ptr: u64);
fn attached_deposit(balance_ptr: u64);
fn prepaid_gas() -> u64;
fn used_gas() -> u64;
// ############
// # Math API #
// ############
fn random_seed(register_id: u64);
fn sha256(value_len: u64, value_ptr: u64, register_id: u64);
// #####################
// # Miscellaneous API #
// #####################
fn value_return(value_len: u64, value_ptr: u64);
fn panic();
fn panic_utf8(len: u64, ptr: u64);
fn log_utf8(len: u64, ptr: u64);
fn log_utf16(len: u64, ptr: u64);
fn abort(msg_ptr: u32, filename_ptr: u32, line: u32, col: u32);
// ################
// # Promises API #
// ################
fn promise_create(
account_id_len: u64,
account_id_ptr: u64,
method_name_len: u64,
method_name_ptr: u64,
arguments_len: u64,
arguments_ptr: u64,
amount_ptr: u64,
gas: u64,
) -> u64;
fn promise_then(
promise_index: u64,
account_id_len: u64,
account_id_ptr: u64,
method_name_len: u64,
method_name_ptr: u64,
arguments_len: u64,
arguments_ptr: u64,
amount_ptr: u64,
gas: u64,
) -> u64;
fn promise_and(promise_idx_ptr: u64, promise_idx_count: u64) -> u64;
fn promise_batch_create(account_id_len: u64, account_id_ptr: u64) -> u64;
fn promise_batch_then(promise_index: u64, account_id_len: u64, account_id_ptr: u64) -> u64;
// #######################
// # Promise API actions #
// #######################
fn promise_batch_action_create_account(promise_index: u64);
fn promise_batch_action_deploy_contract(promise_index: u64, code_len: u64, code_ptr: u64);
fn promise_batch_action_function_call(
promise_index: u64,
method_name_len: u64,
method_name_ptr: u64,
arguments_len: u64,
arguments_ptr: u64,
amount_ptr: u64,
gas: u64,
);
#[cfg(feature = "latest_protocol")]
fn promise_batch_action_function_call_weight(
promise_index: u64,
method_name_len: u64,
method_name_ptr: u64,
arguments_len: u64,
arguments_ptr: u64,
amount_ptr: u64,
gas: u64,
gas_weight: u64,
);
fn promise_batch_action_transfer(promise_index: u64, amount_ptr: u64);
fn promise_batch_action_stake(
promise_index: u64,
amount_ptr: u64,
public_key_len: u64,
public_key_ptr: u64,
);
fn promise_batch_action_add_key_with_full_access(
promise_index: u64,
public_key_len: u64,
public_key_ptr: u64,
nonce: u64,
);
fn promise_batch_action_add_key_with_function_call(
promise_index: u64,
public_key_len: u64,
public_key_ptr: u64,
nonce: u64,
allowance_ptr: u64,
receiver_id_len: u64,
receiver_id_ptr: u64,
method_names_len: u64,
method_names_ptr: u64,
);
fn promise_batch_action_delete_key(
promise_index: u64,
public_key_len: u64,
public_key_ptr: u64,
);
fn promise_batch_action_delete_account(
promise_index: u64,
beneficiary_id_len: u64,
beneficiary_id_ptr: u64,
);
// #######################
// # Promise API results #
// #######################
fn promise_results_count() -> u64;
fn promise_result(result_idx: u64, register_id: u64) -> u64;
fn promise_return(promise_id: u64);
// ###############
// # Storage API #
// ###############
fn storage_write(
key_len: u64,
key_ptr: u64,
value_len: u64,
value_ptr: u64,
register_id: u64,
) -> u64;
fn storage_read(key_len: u64, key_ptr: u64, register_id: u64) -> u64;
fn storage_remove(key_len: u64, key_ptr: u64, register_id: u64) -> u64;
fn storage_has_key(key_len: u64, key_ptr: u64) -> u64;
fn storage_iter_prefix(prefix_len: u64, prefix_ptr: u64) -> u64;
fn storage_iter_range(start_len: u64, start_ptr: u64, end_len: u64, end_ptr: u64) -> u64;
fn storage_iter_next(iterator_id: u64, key_register_id: u64, value_register_id: u64) -> u64;
fn gas(ops: u32);
// #################
// # Validator API #
// #################
fn validator_stake(account_id_len: u64, account_id_ptr: u64, stake_ptr: u64);
fn validator_total_stake(stake_ptr: u64);
// ###################
// # Math Extensions #
// ###################
#[cfg(feature = "latest_protocol")]
fn ripemd160(value_len: u64, value_ptr: u64, register_id: u64);
// #################
// # alt_bn128 API #
// #################
#[cfg(feature = "latest_protocol")]
fn alt_bn128_g1_multiexp(value_len: u64, value_ptr: u64, register_id: u64);
#[cfg(feature = "latest_protocol")]
fn alt_bn128_g1_sum(value_len: u64, value_ptr: u64, register_id: u64);
#[cfg(feature = "latest_protocol")]
fn alt_bn128_pairing_check(value_len: u64, value_ptr: u64) -> u64;
// #################
// # Submodule API #
// #################
fn start_submodule(key_ptr: u64, key_len: u64, gas_limit: u64, output_register_id: u64) -> u64;
fn resume_submodule(
key_ptr: u64,
key_len: u64,
response_ptr: u64,
response_len: u64,
output_register_id: u64,
) -> u64;
}
const ONE_TERAGAS: u64 = 1_000_000_000_000;
macro_rules! ext_test {
($export_func:ident, $call_ext:expr) => {
#[no_mangle]
pub unsafe fn $export_func() {
$call_ext(0);
let result = vec![0; register_len(0) as usize];
read_register(0, result.as_ptr() as *const u64 as u64);
value_return(result.len() as u64, result.as_ptr() as *const u64 as u64);
}
};
}
macro_rules! ext_test_u64 {
($export_func:ident, $call_ext:expr) => {
#[no_mangle]
pub unsafe fn $export_func() {
let mut result = [0u8; size_of::<u64>()];
result.copy_from_slice(&$call_ext().to_le_bytes());
value_return(result.len() as u64, result.as_ptr() as *const u64 as u64);
}
};
}
macro_rules! ext_test_u128 {
($export_func:ident, $call_ext:expr) => {
#[no_mangle]
pub unsafe fn $export_func() {
let result = &[0u8; size_of::<u128>()];
$call_ext(result.as_ptr() as *const u64 as u64);
value_return(result.len() as u64, result.as_ptr() as *const u64 as u64);
}
};
}
ext_test_u64!(ext_storage_usage, storage_usage);
ext_test_u64!(ext_block_index, block_index);
ext_test_u64!(ext_block_timestamp, block_timestamp);
ext_test_u64!(ext_prepaid_gas, prepaid_gas);
ext_test!(ext_random_seed, random_seed);
ext_test!(ext_predecessor_account_id, predecessor_account_id);
ext_test!(ext_signer_pk, signer_account_pk);
ext_test!(ext_signer_id, signer_account_id);
ext_test!(ext_account_id, current_account_id);
ext_test_u128!(ext_account_balance, account_balance);
ext_test_u128!(ext_attached_deposit, attached_deposit);
ext_test_u128!(ext_validator_total_stake, validator_total_stake);
#[no_mangle]
pub unsafe fn ext_sha256() {
input(0);
let bytes = vec![0; register_len(0) as usize];
read_register(0, bytes.as_ptr() as *const u64 as u64);
sha256(bytes.len() as u64, bytes.as_ptr() as *const u64 as u64, 0);
let result = vec![0; register_len(0) as usize];
read_register(0, result.as_ptr() as *const u64 as u64);
value_return(result.len() as u64, result.as_ptr() as *const u64 as u64);
}
#[no_mangle]
pub unsafe fn ext_used_gas() {
let initial_used_gas = used_gas();
let mut a = 1;
let mut b = 1;
for _ in 0..30 {
let c = a + b;
a = b;
b = c;
}
assert_eq!(a, 1346269);
let gas = used_gas() - initial_used_gas;
let result = gas.to_le_bytes();
value_return(result.len() as u64, result.as_ptr() as *const u64 as u64);
}
#[no_mangle]
pub unsafe fn ext_validator_stake() {
input(0);
let account_id = vec![0; register_len(0) as usize];
read_register(0, account_id.as_ptr() as *const u64 as u64);
let result = [0u8; size_of::<u128>()];
validator_stake(
account_id.len() as u64,
account_id.as_ptr() as *const u64 as u64,
result.as_ptr() as *const u64 as u64,
);
value_return(result.len() as u64, result.as_ptr() as *const u64 as u64);
}
/// Write key-value pair into storage.
/// Input is the byte array where the value is `u64` represented by last 8 bytes and key is represented by the first
/// `register_len(0) - 8` bytes.
#[no_mangle]
pub unsafe fn write_key_value() {
input(0);
let data_len = register_len(0) as usize;
let value_len = size_of::<u64>();
let data = vec![0u8; data_len];
read_register(0, data.as_ptr() as u64);
let key = &data[0..data_len - value_len];
let value = &data[data_len - value_len..];
let result = storage_write(
key.len() as u64,
key.as_ptr() as u64,
value.len() as u64,
value.as_ptr() as u64,
1,
);
value_return(size_of::<u64>() as u64, &result as *const u64 as u64);
}
#[no_mangle]
pub unsafe fn write_block_height() {
let block_height = block_index();
let mut key = [0u8; size_of::<u64>()];
key.copy_from_slice(&block_height.to_le_bytes());
let value = b"hello";
storage_write(key.len() as _, key.as_ptr() as _, value.len() as _, value.as_ptr() as _, 0);
}
#[no_mangle]
pub unsafe fn write_random_value() {
random_seed(0);
let data = [0u8; 32];
read_register(0, data.as_ptr() as u64);
let value = b"hello";
storage_write(data.len() as _, data.as_ptr() as _, value.len() as _, value.as_ptr() as _, 1);
}
#[no_mangle]
pub unsafe fn read_value() {
input(0);
if register_len(0) != size_of::<u64>() as u64 {
panic()
}
let key = [0u8; size_of::<u64>()];
read_register(0, key.as_ptr() as u64);
let result = storage_read(key.len() as u64, key.as_ptr() as u64, 1);
if result == 1 {
let value = [0u8; size_of::<u64>()];
read_register(1, value.as_ptr() as u64);
value_return(value.len() as u64, &value as *const u8 as u64);
}
}
#[no_mangle]
pub unsafe fn log_something() {
let data = b"hello";
log_utf8(data.len() as u64, data.as_ptr() as _);
}
#[no_mangle]
pub unsafe fn loop_forever() {
loop {}
}
#[no_mangle]
pub unsafe fn abort_with_zero() {
// Tries to abort with 0 ptr to check underflow handling.
abort(0, 0, 0, 0);
}
#[no_mangle]
pub unsafe fn panic_with_message() {
let data = b"WAT?";
panic_utf8(data.len() as u64, data.as_ptr() as _);
}
#[no_mangle]
pub unsafe fn panic_after_logging() {
let data = b"hello";
log_utf8(data.len() as u64, data.as_ptr() as _);
let data = b"WAT?";
panic_utf8(data.len() as u64, data.as_ptr() as _);
}
#[no_mangle]
pub unsafe fn run_test() {
let value: [u8; 4] = 10i32.to_le_bytes();
value_return(value.len() as u64, value.as_ptr() as _);
}
#[no_mangle]
pub unsafe fn run_test_with_storage_change() {
let key = b"hello";
let value = b"world";
storage_write(key.len() as _, key.as_ptr() as _, value.len() as _, value.as_ptr() as _, 0);
}
#[no_mangle]
pub unsafe fn sum_with_input() {
input(0);
if register_len(0) != 2 * size_of::<u64>() as u64 {
panic()
}
let data = [0u8; 2 * size_of::<u64>()];
read_register(0, data.as_ptr() as u64);
let mut key = [0u8; size_of::<u64>()];
let mut value = [0u8; size_of::<u64>()];
key.copy_from_slice(&data[..size_of::<u64>()]);
value.copy_from_slice(&data[size_of::<u64>()..]);
let key = u64::from_le_bytes(key);
let value = u64::from_le_bytes(value);
let result = key + value;
value_return(size_of::<u64>() as u64, &result as *const u64 as u64);
}
/// Writes and reads some data into/from storage. Uses 8-bit key/values.
#[no_mangle]
pub unsafe fn benchmark_storage_8b() {
input(0);
if register_len(0) != size_of::<u64>() as u64 {
panic()
}
let data = [0u8; size_of::<u64>()];
read_register(0, data.as_ptr() as u64);
let n: u64 = u64::from_le_bytes(data);
let mut sum = 0u64;
for i in 0..n {
let el = i.to_le_bytes();
storage_write(el.len() as u64, el.as_ptr() as u64, el.len() as u64, el.as_ptr() as u64, 0);
let result = storage_read(el.len() as u64, el.as_ptr() as u64, 0);
if result == 1 {
let value = [0u8; size_of::<u64>()];
read_register(0, value.as_ptr() as u64);
sum += u64::from_le_bytes(value);
}
}
value_return(size_of::<u64>() as u64, &sum as *const u64 as u64);
}
#[inline]
fn generate_data(data: &mut [u8]) {
for i in 0..data.len() {
data[i] = (i % u8::MAX as usize) as u8;
}
}
/// Writes and reads some data into/from storage. Uses 10KiB key/values.
#[no_mangle]
pub unsafe fn benchmark_storage_10kib() {
input(0);
if register_len(0) != size_of::<u64>() as u64 {
panic()
}
let data = [0u8; size_of::<u64>()];
read_register(0, data.as_ptr() as u64);
let n: u64 = u64::from_le_bytes(data);
let mut el = [0u8; 10 << 10];
generate_data(&mut el);
let mut sum = 0u64;
for i in 0..n {
el[..size_of::<u64>()].copy_from_slice(&i.to_le_bytes());
storage_write(el.len() as u64, el.as_ptr() as u64, el.len() as u64, el.as_ptr() as u64, 0);
let result = storage_read(el.len() as u64, el.as_ptr() as u64, 0);
if result == 1 {
let el = [0u8; 10 << 10];
read_register(0, el.as_ptr() as u64);
let mut value = [0u8; size_of::<u64>()];
value.copy_from_slice(&el[0..size_of::<u64>()]);
sum += u64::from_le_bytes(value);
}
}
value_return(size_of::<u64>() as u64, &sum as *const u64 as u64);
}
/// Passes through input into output.
#[no_mangle]
pub unsafe fn pass_through() {
input(0);
if register_len(0) != size_of::<u64>() as u64 {
panic()
}
let data = [0u8; size_of::<u64>()];
read_register(0, data.as_ptr() as u64);
value_return(data.len() as u64, data.as_ptr() as u64);
}
/// Sums numbers.
#[no_mangle]
pub unsafe fn sum_n() {
input(0);
if register_len(0) != size_of::<u64>() as u64 {
panic()
}
let data = [0u8; size_of::<u64>()];
read_register(0, data.as_ptr() as u64);
let n = u64::from_le_bytes(data);
let mut sum = 0u64;
for i in 0..n {
// LLVM optimizes sum += i into O(1) computation, use volatile to thwart
// that.
let new_sum = std::ptr::read_volatile(&sum).wrapping_add(i);
std::ptr::write_volatile(&mut sum, new_sum);
}
let data = sum.to_le_bytes();
value_return(data.len() as u64, data.as_ptr() as u64);
}
/// Calculates Fibonacci numbers in inefficient way. Used to burn gas for the
/// sanity/max_gas_burnt_view.py test. The implementation has exponential
/// complexity (1.62^n to be exact) so even small increase in argument result in
/// large increase in gas use.
#[no_mangle]
pub unsafe fn fibonacci() {
input(0);
if register_len(0) != 1 {
panic()
}
let mut n: u8 = 0;
read_register(0, &mut n as *mut u8 as u64);
let data = fib(n).to_le_bytes();
value_return(data.len() as u64, data.as_ptr() as u64);
}
fn fib(n: u8) -> u64 {
if n < 2 {
n as u64
} else {
fib(n - 2) + fib(n - 1)
}
}
#[no_mangle]
pub unsafe fn insert_strings() {
input(0);
if register_len(0) != 2 * size_of::<u64>() as u64 {
panic()
}
let data = [0u8; 2 * size_of::<u64>()];
read_register(0, data.as_ptr() as u64);
let mut from = [0u8; size_of::<u64>()];
let mut to = [0u8; size_of::<u64>()];
from.copy_from_slice(&data[..size_of::<u64>()]);
to.copy_from_slice(&data[size_of::<u64>()..]);
let from = u64::from_le_bytes(from);
let to = u64::from_le_bytes(to);
let s = vec![b'a'; to as usize];
for i in from..to {
let mut key = s[(to - i) as usize..].to_vec();
key.push(b'b');
let value = b"x";
storage_write(
key.len() as u64,
key.as_ptr() as u64,
value.len() as u64,
value.as_ptr() as u64,
0,
);
}
}
#[no_mangle]
pub unsafe fn delete_strings() {
input(0);
if register_len(0) != 2 * size_of::<u64>() as u64 {
panic()
}
let data = [0u8; 2 * size_of::<u64>()];
read_register(0, data.as_ptr() as u64);
let mut from = [0u8; size_of::<u64>()];
let mut to = [0u8; size_of::<u64>()];
from.copy_from_slice(&data[..size_of::<u64>()]);
to.copy_from_slice(&data[size_of::<u64>()..]);
let from = u64::from_le_bytes(from);
let to = u64::from_le_bytes(to);
let s = vec![b'a'; to as usize];
for i in from..to {
let mut key = s[(to - i) as usize..].to_vec();
key.push(b'b');
storage_remove(key.len() as u64, key.as_ptr() as u64, 0);
}
}
#[no_mangle]
pub unsafe fn recurse() {
input(0);
if register_len(0) != size_of::<u64>() as u64 {
panic()
}
let data = [0u8; size_of::<u64>()];
read_register(0, data.as_ptr() as u64);
let n = u64::from_le_bytes(data);
let res = internal_recurse(n);
let data = res.to_le_bytes();
value_return(data.len() as u64, data.as_ptr() as u64);
}
/// Rust compiler is getting smarter and starts to optimize my deep recursion.
/// We're going to fight it with a more obscure implementations.
#[no_mangle]
fn internal_recurse(n: u64) -> u64 {
if n <= 1 {
n
} else {
let a = internal_recurse(n - 1) + 1;
if a % 2 == 1 {
(a + n) / 2
} else {
a
}
}
}
#[no_mangle]
pub fn out_of_memory() {
let mut vec = Vec::new();
loop {
vec.push(vec![0; 1024]);
}
}
// Can be used for debugging
#[no_mangle]
fn log_u64(msg: u64) {
unsafe {
log_utf8(8, &msg as *const u64 as u64);
}
}
pub fn from_base64(s: &str) -> Vec<u8> {
base64::decode(s).unwrap()
}
#[no_mangle]
fn call_promise() {
unsafe {
input(0);
let data = vec![0u8; register_len(0) as usize];
read_register(0, data.as_ptr() as u64);
let input_args: serde_json::Value = serde_json::from_slice(&data).unwrap();
for arg in input_args.as_array().unwrap() {
let actual_id = if let Some(create) = arg.get("create") {
let account_id = create["account_id"].as_str().unwrap().as_bytes();
let method_name = create["method_name"].as_str().unwrap().as_bytes();
let arguments = serde_json::to_vec(&create["arguments"]).unwrap();
let amount = create["amount"].as_str().unwrap().parse::<u128>().unwrap();
let gas = create["gas"].as_i64().unwrap() as u64;
promise_create(
account_id.len() as u64,
account_id.as_ptr() as u64,
method_name.len() as u64,
method_name.as_ptr() as u64,
arguments.len() as u64,
arguments.as_ptr() as u64,
&amount as *const u128 as *const u64 as u64,
gas,
)
} else if let Some(then) = arg.get("then") {
let promise_index = then["promise_index"].as_i64().unwrap() as u64;
let account_id = then["account_id"].as_str().unwrap().as_bytes();
let method_name = then["method_name"].as_str().unwrap().as_bytes();
let arguments = serde_json::to_vec(&then["arguments"]).unwrap();
let amount = then["amount"].as_str().unwrap().parse::<u128>().unwrap();
let gas = then["gas"].as_i64().unwrap() as u64;
promise_then(
promise_index,
account_id.len() as u64,
account_id.as_ptr() as u64,
method_name.len() as u64,
method_name.as_ptr() as u64,
arguments.len() as u64,
arguments.as_ptr() as u64,
&amount as *const u128 as *const u64 as u64,
gas,
)
} else if let Some(and) = arg.get("and") {
let and = and.as_array().unwrap();
let mut curr = and[0].as_i64().unwrap() as u64;
for other in &and[1..] {
curr = promise_and(curr, other.as_i64().unwrap() as u64);
}
curr
} else if let Some(batch_create) = arg.get("batch_create") {
let account_id = batch_create["account_id"].as_str().unwrap().as_bytes();
promise_batch_create(account_id.len() as u64, account_id.as_ptr() as u64)
} else if let Some(batch_then) = arg.get("batch_then") {
let promise_index = batch_then["promise_index"].as_i64().unwrap() as u64;
let account_id = batch_then["account_id"].as_str().unwrap().as_bytes();
promise_batch_then(
promise_index,
account_id.len() as u64,
account_id.as_ptr() as u64,
)
} else if let Some(action) = arg.get("action_create_account") {
let promise_index = action["promise_index"].as_i64().unwrap() as u64;
promise_batch_action_create_account(promise_index);
promise_index
} else if let Some(action) = arg.get("action_deploy_contract") {
let promise_index = action["promise_index"].as_i64().unwrap() as u64;
let code = from_base64(action["code"].as_str().unwrap());
promise_batch_action_deploy_contract(
promise_index,
code.len() as u64,
code.as_ptr() as u64,
);
promise_index
} else if let Some(action) = arg.get("action_function_call") {
let promise_index = action["promise_index"].as_i64().unwrap() as u64;
let method_name = action["method_name"].as_str().unwrap().as_bytes();
let arguments = serde_json::to_vec(&action["arguments"]).unwrap();
let amount = action["amount"].as_str().unwrap().parse::<u128>().unwrap();
let gas = action["gas"].as_i64().unwrap() as u64;
promise_batch_action_function_call(
promise_index,
method_name.len() as u64,
method_name.as_ptr() as u64,
arguments.len() as u64,
arguments.as_ptr() as u64,
&amount as *const u128 as *const u64 as u64,
gas,
);
promise_index
} else if let Some(action) = arg.get("action_transfer") {
let promise_index = action["promise_index"].as_i64().unwrap() as u64;
let amount = action["amount"].as_str().unwrap().parse::<u128>().unwrap();
promise_batch_action_transfer(
promise_index,
&amount as *const u128 as *const u64 as u64,
);
promise_index
} else if let Some(action) = arg.get("action_stake") {
let promise_index = action["promise_index"].as_i64().unwrap() as u64;
let amount = action["amount"].as_str().unwrap().parse::<u128>().unwrap();
let public_key = from_base64(action["public_key"].as_str().unwrap());
promise_batch_action_stake(
promise_index,
&amount as *const u128 as *const u64 as u64,
public_key.len() as u64,
public_key.as_ptr() as u64,
);
promise_index
} else if let Some(action) = arg.get("action_add_key_with_full_access") {
let promise_index = action["promise_index"].as_i64().unwrap() as u64;
let public_key = from_base64(action["public_key"].as_str().unwrap());
let nonce = action["nonce"].as_i64().unwrap() as u64;
promise_batch_action_add_key_with_full_access(
promise_index,
public_key.len() as u64,
public_key.as_ptr() as u64,
nonce,
);
promise_index
} else if let Some(action) = arg.get("action_add_key_with_function_call") {
let promise_index = action["promise_index"].as_i64().unwrap() as u64;
let public_key = from_base64(action["public_key"].as_str().unwrap());
let nonce = action["nonce"].as_i64().unwrap() as u64;
let allowance = action["allowance"].as_str().unwrap().parse::<u128>().unwrap();
let receiver_id = action["receiver_id"].as_str().unwrap().as_bytes();
let method_names = action["method_names"].as_str().unwrap().as_bytes();
promise_batch_action_add_key_with_function_call(
promise_index,
public_key.len() as u64,
public_key.as_ptr() as u64,
nonce,
&allowance as *const u128 as *const u64 as u64,
receiver_id.len() as u64,
receiver_id.as_ptr() as u64,
method_names.len() as u64,
method_names.as_ptr() as u64,
);
promise_index
} else if let Some(action) = arg.get("action_delete_key") {
let promise_index = action["promise_index"].as_i64().unwrap() as u64;
let public_key = from_base64(action["public_key"].as_str().unwrap());
promise_batch_action_delete_key(
promise_index,
public_key.len() as u64,
public_key.as_ptr() as u64,
);
promise_index
} else if let Some(action) = arg.get("action_delete_account") {
let promise_index = action["promise_index"].as_i64().unwrap() as u64;
let beneficiary_id = action["beneficiary_id"].as_str().unwrap().as_bytes();
promise_batch_action_delete_account(
promise_index,
beneficiary_id.len() as u64,
beneficiary_id.as_ptr() as u64,
);
promise_index
} else {
unimplemented!()
};
let expected_id = arg["id"].as_i64().unwrap() as u64;
assert_eq!(actual_id, expected_id);
if let Some(ret) = arg.get("return") {
if ret.as_bool().unwrap() == true {
promise_return(actual_id);
}
}
}
}
}
#[cfg(feature = "latest_protocol")]
#[no_mangle]
fn attach_unspent_gas_but_burn_all_gas() {
unsafe {
let account_id = "alice.near";
let promise_idx = promise_batch_create(account_id.len() as u64, account_id.as_ptr() as u64);
let method_name = "f";
let arguments_ptr = 0;
let arguments_len = 0;
let amount = 1u128;
let gas_fixed = 0;
let gas_weight = 1;
promise_batch_action_function_call_weight(
promise_idx,
method_name.len() as u64,
method_name.as_ptr() as u64,
arguments_ptr,
arguments_len,
&amount as *const u128 as u64,
gas_fixed,
gas_weight,
);
loop {
gas(10_000);
}
}
}
#[cfg(feature = "latest_protocol")]
#[no_mangle]
fn attach_unspent_gas_but_use_all_gas() {
unsafe {
let account_id = "alice.near";
let promise_idx = promise_batch_create(account_id.len() as u64, account_id.as_ptr() as u64);
let method_name = "f";
let arguments_ptr = 0;
let arguments_len = 0;
let amount = 1u128;
let gas_fixed = 0;
let gas_weight = 1;
promise_batch_action_function_call_weight(
promise_idx,
method_name.len() as u64,
method_name.as_ptr() as u64,
arguments_ptr,
arguments_len,
&amount as *const u128 as u64,
gas_fixed,
gas_weight,
);
let promise_idx = promise_batch_create(account_id.len() as u64, account_id.as_ptr() as u64);
let gas_fixed = 10u64.pow(14);
let gas_weight = 0;
promise_batch_action_function_call_weight(
promise_idx,
method_name.len() as u64,
method_name.as_ptr() as u64,
arguments_ptr,
arguments_len,
&amount as *const u128 as u64,
gas_fixed,
gas_weight,
);
}
}
#[cfg(feature = "latest_protocol")]
#[no_mangle]
fn do_ripemd() {
let data = b"tesdsst";
unsafe {
ripemd160(data.len() as _, data.as_ptr() as _, 0);
}
}
#[no_mangle]
pub unsafe fn noop() {}
fn insert_account_id_prefix(
prefix: &str,
account_id: Vec<u8>,
) -> Result<Vec<u8>, std::string::FromUtf8Error> {
let mut id = String::from_utf8(account_id)?;
id.insert_str(0, prefix);
Ok(id.as_bytes().to_vec())
}
/// Calls all host functions, either directly or via callback.
#[no_mangle]
pub unsafe fn sanity_check() {
// #############
// # Registers #
// #############
input(0);
let input_data = vec![0u8; register_len(0) as usize];
read_register(0, input_data.as_ptr() as u64);
let input_args: serde_json::Value = serde_json::from_slice(&input_data).unwrap();
// ###############
// # Context API #
// ###############
current_account_id(1);
let account_id = vec![0u8; register_len(1) as usize];
read_register(1, account_id.as_ptr() as u64);
signer_account_pk(1);
let account_public_key = vec![0u8; register_len(1) as usize];
read_register(1, account_public_key.as_ptr() as u64);
signer_account_id(1);
predecessor_account_id(1);
// input() already called when reading the input of the contract call
let _ = block_index();
let _ = block_timestamp();
let _ = epoch_height();
let _ = storage_usage();
// #################
// # Economics API #
// #################
let balance = [0u8; size_of::<u128>()];
account_balance(balance.as_ptr() as u64);
attached_deposit(balance.as_ptr() as u64);
let available_gas = prepaid_gas() - used_gas();
// ############
// # Math API #
// ############
random_seed(1);
let value = b"hello";
sha256(value.len() as u64, value.as_ptr() as u64, 1);
// #####################
// # Miscellaneous API #
// #####################
value_return(value.len() as u64, value.as_ptr() as u64);
// Calling host functions that terminate execution via promises.
let method_name_panic = b"sanity_check_panic";
let args_panic = b"";
let amount_zero = 0u128;
let gas_per_promise = available_gas / 50;
assert_eq!(
promise_create(
account_id.len() as u64,
account_id.as_ptr() as u64,
method_name_panic.len() as u64,
method_name_panic.as_ptr() as u64,
args_panic.len() as u64,
args_panic.as_ptr() as u64,
&amount_zero as *const u128 as *const u64 as u64,
gas_per_promise,
),
0,
);
let method_name_panic_utf8 = b"sanity_check_panic_utf8";
let args_panic_utf8 = b"";
assert_eq!(
promise_create(
account_id.len() as u64,
account_id.as_ptr() as u64,
method_name_panic_utf8.len() as u64,
method_name_panic_utf8.as_ptr() as u64,
args_panic_utf8.len() as u64,
args_panic_utf8.as_ptr() as u64,
&amount_zero as *const u128 as *const u64 as u64,
gas_per_promise,
),
1,
);
// Note: Skip `abort`, it's called only by code generated by AssemblyScript.
log_utf8(value.len() as u64, value.as_ptr() as u64);
let value_utf16 = b"h\x00e\x00l\x00l\x00o\x00";
log_utf16(value_utf16.len() as u64, value_utf16.as_ptr() as u64);
// ################
// # Promises API #
// ################
let method_name_noop = b"noop";
let args_noop = b"";
assert_eq!(
promise_create(
account_id.len() as u64,
account_id.as_ptr() as u64,
method_name_noop.len() as u64,
method_name_noop.as_ptr() as u64,
args_noop.len() as u64,
args_noop.as_ptr() as u64,
&amount_zero as *const u128 as *const u64 as u64,
gas_per_promise,
),
2
);
let promise_then_ids = [3, 4];
for expected_id in promise_then_ids {
assert_eq!(
promise_then(