-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathcontext.rs
More file actions
1239 lines (1085 loc) · 43 KB
/
context.rs
File metadata and controls
1239 lines (1085 loc) · 43 KB
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
use graph::data::value::Word;
use graph::runtime::gas;
use graph::util::lfu_cache::LfuCache;
use std::collections::HashMap;
use wasmtime::AsContext;
use wasmtime::AsContextMut;
use wasmtime::StoreContextMut;
use std::sync::Arc;
use std::time::Instant;
use anyhow::Error;
use graph::components::store::GetScope;
use never::Never;
use crate::asc_abi::class::*;
use crate::HostExports;
use graph::data::store;
use crate::asc_abi::class::AscEntity;
use crate::asc_abi::class::AscString;
use crate::mapping::MappingContext;
use crate::mapping::ValidModule;
use crate::ExperimentalFeatures;
use graph::prelude::*;
use graph::runtime::AscPtr;
use graph::runtime::{asc_new, gas::GasCounter, DeterministicHostError, HostExportError};
use super::asc_get;
use super::AscHeapCtx;
pub(crate) struct WasmInstanceContext<'a> {
inner: StoreContextMut<'a, WasmInstanceData>,
}
impl WasmInstanceContext<'_> {
pub fn new(ctx: &mut impl AsContextMut<Data = WasmInstanceData>) -> WasmInstanceContext<'_> {
WasmInstanceContext {
inner: ctx.as_context_mut(),
}
}
pub fn as_ref(&self) -> &WasmInstanceData {
self.inner.data()
}
pub fn as_mut(&mut self) -> &mut WasmInstanceData {
self.inner.data_mut()
}
pub fn asc_heap(&self) -> &Arc<AscHeapCtx> {
self.as_ref().asc_heap()
}
pub fn suspend_timeout(&mut self) {
// See also: runtime-timeouts
self.inner.set_epoch_deadline(u64::MAX);
}
pub fn start_timeout(&mut self) {
// See also: runtime-timeouts
self.inner.set_epoch_deadline(2);
}
}
impl AsContext for WasmInstanceContext<'_> {
type Data = WasmInstanceData;
fn as_context(&self) -> wasmtime::StoreContext<'_, Self::Data> {
self.inner.as_context()
}
}
impl AsContextMut for WasmInstanceContext<'_> {
fn as_context_mut(&mut self) -> wasmtime::StoreContextMut<'_, Self::Data> {
self.inner.as_context_mut()
}
}
pub struct WasmInstanceData {
pub ctx: MappingContext,
pub valid_module: Arc<ValidModule>,
pub host_metrics: Arc<HostMetrics>,
// A trap ocurred due to a possible reorg detection.
pub possible_reorg: bool,
// A host export trap ocurred for a deterministic reason.
pub deterministic_host_trap: bool,
pub(crate) experimental_features: ExperimentalFeatures,
// This option is needed to break the cyclic dependency between, instance, store, and context.
// during execution it should always be populated.
asc_heap: Option<Arc<AscHeapCtx>>,
}
impl WasmInstanceData {
pub fn from_instance(
ctx: MappingContext,
valid_module: Arc<ValidModule>,
host_metrics: Arc<HostMetrics>,
experimental_features: ExperimentalFeatures,
) -> Self {
WasmInstanceData {
asc_heap: None,
ctx,
valid_module,
host_metrics,
possible_reorg: false,
deterministic_host_trap: false,
experimental_features,
}
}
pub fn set_asc_heap(&mut self, asc_heap: Arc<AscHeapCtx>) {
self.asc_heap = Some(asc_heap);
}
pub fn asc_heap(&self) -> &Arc<AscHeapCtx> {
self.asc_heap.as_ref().expect("asc_heap not set")
}
pub fn take_state(mut self) -> BlockState {
let state = &mut self.ctx.state;
std::mem::replace(
state,
BlockState::new(state.entity_cache.store.cheap_clone(), LfuCache::default()),
)
}
}
impl WasmInstanceContext<'_> {
fn store_get_scoped(
&mut self,
gas: &GasCounter,
entity_ptr: AscPtr<AscString>,
id_ptr: AscPtr<AscString>,
scope: GetScope,
) -> Result<AscPtr<AscEntity>, HostExportError> {
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let _timer = self
.as_ref()
.host_metrics
.cheap_clone()
.time_host_fn_execution_region("store_get");
let entity_type: String = asc_get(self, entity_ptr, gas)?;
let id: String = asc_get(self, id_ptr, gas)?;
let entity_option = host_exports.store_get(
&mut self.as_mut().ctx.state,
entity_type.clone(),
id.clone(),
gas,
scope,
)?;
if self.as_ref().ctx.instrument {
debug!(self.as_ref().ctx.logger, "store_get";
"type" => &entity_type,
"id" => &id,
"found" => entity_option.is_some());
}
let host_metrics = self.as_ref().host_metrics.cheap_clone();
let debug_fork = self.as_ref().ctx.debug_fork.cheap_clone();
let ret = match entity_option {
Some(entity) => {
let _section = host_metrics.stopwatch.start_section("store_get_asc_new");
asc_new(self, &entity.sorted_ref(), gas)?
}
None => match &debug_fork {
Some(fork) => {
let entity_option = fork.fetch(entity_type, id).map_err(|e| {
HostExportError::Unknown(anyhow!(
"store_get: failed to fetch entity from the debug fork: {}",
e
))
})?;
match entity_option {
Some(entity) => {
let _section =
host_metrics.stopwatch.start_section("store_get_asc_new");
let entity = asc_new(self, &entity.sorted(), gas)?;
self.store_set(gas, entity_ptr, id_ptr, entity)?;
entity
}
None => AscPtr::null(),
}
}
None => AscPtr::null(),
},
};
Ok(ret)
}
}
// Implementation of externals.
impl WasmInstanceContext<'_> {
/// function abort(message?: string | null, fileName?: string | null, lineNumber?: u32, columnNumber?: u32): void
/// Always returns a trap.
pub fn abort(
&mut self,
gas: &GasCounter,
message_ptr: AscPtr<AscString>,
file_name_ptr: AscPtr<AscString>,
line_number: u32,
column_number: u32,
) -> Result<Never, DeterministicHostError> {
let message = match message_ptr.is_null() {
false => Some(asc_get(self, message_ptr, gas)?),
true => None,
};
let file_name = match file_name_ptr.is_null() {
false => Some(asc_get(self, file_name_ptr, gas)?),
true => None,
};
let line_number = match line_number {
0 => None,
_ => Some(line_number),
};
let column_number = match column_number {
0 => None,
_ => Some(column_number),
};
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
host_exports.abort(
message,
file_name,
line_number,
column_number,
gas,
&mut ctx.state,
)
}
/// function store.set(entity: string, id: string, data: Entity): void
pub fn store_set(
&mut self,
gas: &GasCounter,
entity_ptr: AscPtr<AscString>,
id_ptr: AscPtr<AscString>,
data_ptr: AscPtr<AscEntity>,
) -> Result<(), HostExportError> {
let stopwatch = self.as_ref().host_metrics.stopwatch.cheap_clone();
let logger = self.as_ref().ctx.logger.cheap_clone();
let block_number = self.as_ref().ctx.block_ptr.block_number();
stopwatch.start_section("host_export_store_set__wasm_instance_context_store_set");
let entity: String = asc_get(self, entity_ptr, gas)?;
let id: String = asc_get(self, id_ptr, gas)?;
let data = asc_get(self, data_ptr, gas)?;
if self.as_ref().ctx.instrument {
debug!(self.as_ref().ctx.logger, "store_set";
"type" => &entity,
"id" => &id);
}
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
host_exports.store_set(
&logger,
block_number,
&mut ctx.state,
&ctx.proof_of_indexing,
ctx.timestamp,
entity,
id,
data,
&stopwatch,
gas,
)?;
Ok(())
}
/// function store.remove(entity: string, id: string): void
pub fn store_remove(
&mut self,
gas: &GasCounter,
entity_ptr: AscPtr<AscString>,
id_ptr: AscPtr<AscString>,
) -> Result<(), HostExportError> {
let logger = self.as_ref().ctx.logger.cheap_clone();
let entity: String = asc_get(self, entity_ptr, gas)?;
let id: String = asc_get(self, id_ptr, gas)?;
if self.as_ref().ctx.instrument {
debug!(self.as_ref().ctx.logger, "store_remove";
"type" => &entity,
"id" => &id);
}
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
host_exports.store_remove(
&logger,
&mut ctx.state,
&ctx.proof_of_indexing,
entity,
id,
gas,
)
}
/// function store.get(entity: string, id: string): Entity | null
pub fn store_get(
&mut self,
gas: &GasCounter,
entity_ptr: AscPtr<AscString>,
id_ptr: AscPtr<AscString>,
) -> Result<AscPtr<AscEntity>, HostExportError> {
self.store_get_scoped(gas, entity_ptr, id_ptr, GetScope::Store)
}
/// function store.get_in_block(entity: string, id: string): Entity | null
pub fn store_get_in_block(
&mut self,
gas: &GasCounter,
entity_ptr: AscPtr<AscString>,
id_ptr: AscPtr<AscString>,
) -> Result<AscPtr<AscEntity>, HostExportError> {
self.store_get_scoped(gas, entity_ptr, id_ptr, GetScope::InBlock)
}
/// function store.loadRelated(entity_type: string, id: string, field: string): Array<Entity>
pub fn store_load_related(
&mut self,
gas: &GasCounter,
entity_type_ptr: AscPtr<AscString>,
id_ptr: AscPtr<AscString>,
field_ptr: AscPtr<AscString>,
) -> Result<AscPtr<Array<AscPtr<AscEntity>>>, HostExportError> {
let entity_type: String = asc_get(self, entity_type_ptr, gas)?;
let id: String = asc_get(self, id_ptr, gas)?;
let field: String = asc_get(self, field_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let entities = host_exports.store_load_related(
&mut self.as_mut().ctx.state,
entity_type.clone(),
id.clone(),
field.clone(),
gas,
)?;
let entities: Vec<Vec<(Word, Value)>> =
entities.into_iter().map(|entity| entity.sorted()).collect();
let ret = asc_new(self, &entities, gas)?;
Ok(ret)
}
/// function typeConversion.bytesToString(bytes: Bytes): string
pub fn bytes_to_string(
&mut self,
gas: &GasCounter,
bytes_ptr: AscPtr<Uint8Array>,
) -> Result<AscPtr<AscString>, HostExportError> {
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let bytes = asc_get(self, bytes_ptr, gas)?;
let ctx = &mut self.as_mut().ctx;
let string = host_exports.bytes_to_string(&ctx.logger, bytes, gas, &mut ctx.state)?;
asc_new(self, &string, gas)
}
/// Converts bytes to a hex string.
/// function typeConversion.bytesToHex(bytes: Bytes): string
/// References:
/// https://godoc.org/github.com/ethereum/go-ethereum/common/hexutil#hdr-Encoding_Rules
/// https://github.com/ethereum/web3.js/blob/f98fe1462625a6c865125fecc9cb6b414f0a5e83/packages/web3-utils/src/utils.js#L283
pub fn bytes_to_hex(
&mut self,
gas: &GasCounter,
bytes_ptr: AscPtr<Uint8Array>,
) -> Result<AscPtr<AscString>, HostExportError> {
let bytes: Vec<u8> = asc_get(self, bytes_ptr, gas)?;
let ctx = &mut self.as_mut().ctx;
HostExports::track_gas_and_ops(
gas,
&mut ctx.state,
gas::DEFAULT_GAS_OP.with_args(gas::complexity::Size, &bytes),
"bytes_to_hex",
)?;
// Even an empty string must be prefixed with `0x`.
// Encodes each byte as a two hex digits.
let hex = format!("0x{}", hex::encode(bytes));
asc_new(self, &hex, gas)
}
/// function typeConversion.bigIntToString(n: Uint8Array): string
pub fn big_int_to_string(
&mut self,
gas: &GasCounter,
big_int_ptr: AscPtr<AscBigInt>,
) -> Result<AscPtr<AscString>, HostExportError> {
let n: BigInt = asc_get(self, big_int_ptr, gas)?;
let ctx = &mut self.as_mut().ctx;
HostExports::track_gas_and_ops(
gas,
&mut ctx.state,
gas::DEFAULT_GAS_OP.with_args(gas::complexity::Mul, (&n, &n)),
"big_int_to_string",
)?;
asc_new(self, &n.to_string(), gas)
}
/// function bigInt.fromString(x: string): BigInt
pub fn big_int_from_string(
&mut self,
gas: &GasCounter,
string_ptr: AscPtr<AscString>,
) -> Result<AscPtr<AscBigInt>, HostExportError> {
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let s = asc_get(self, string_ptr, gas)?;
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_int_from_string(s, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function typeConversion.bigIntToHex(n: Uint8Array): string
pub fn big_int_to_hex(
&mut self,
gas: &GasCounter,
big_int_ptr: AscPtr<AscBigInt>,
) -> Result<AscPtr<AscString>, HostExportError> {
let n: BigInt = asc_get(self, big_int_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let hex = host_exports.big_int_to_hex(n, gas, &mut ctx.state)?;
asc_new(self, &hex, gas)
}
/// function typeConversion.stringToH160(s: String): H160
pub fn string_to_h160(
&mut self,
gas: &GasCounter,
str_ptr: AscPtr<AscString>,
) -> Result<AscPtr<AscH160>, HostExportError> {
let s: String = asc_get(self, str_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let h160 = host_exports.string_to_h160(&s, gas, &mut ctx.state)?;
asc_new(self, &h160, gas)
}
/// function json.fromBytes(bytes: Bytes): JSONValue
pub fn json_from_bytes(
&mut self,
gas: &GasCounter,
bytes_ptr: AscPtr<Uint8Array>,
) -> Result<AscPtr<AscEnum<JsonValueKind>>, HostExportError> {
let bytes: Vec<u8> = asc_get(self, bytes_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports
.json_from_bytes(&bytes, gas, &mut ctx.state)
.with_context(|| {
format!(
"Failed to parse JSON from byte array. Bytes (truncated to 1024 chars): `{:?}`",
&bytes[..bytes.len().min(1024)],
)
})
.map_err(DeterministicHostError::from)?;
asc_new(self, &result, gas)
}
/// function json.try_fromBytes(bytes: Bytes): Result<JSONValue, boolean>
pub fn json_try_from_bytes(
&mut self,
gas: &GasCounter,
bytes_ptr: AscPtr<Uint8Array>,
) -> Result<AscPtr<AscResult<AscPtr<AscEnum<JsonValueKind>>, bool>>, HostExportError> {
let bytes: Vec<u8> = asc_get(self, bytes_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports
.json_from_bytes(&bytes, gas, &mut ctx.state)
.map_err(|e| {
warn!(
&self.as_ref().ctx.logger,
"Failed to parse JSON from byte array";
"bytes" => format!("{:?}", bytes),
"error" => format!("{}", e)
);
// Map JSON errors to boolean to match the `Result<JSONValue, boolean>`
// result type expected by mappings
true
});
asc_new(self, &result, gas)
}
/// function ipfs.cat(link: String): Bytes
pub fn ipfs_cat(
&mut self,
gas: &GasCounter,
link_ptr: AscPtr<AscString>,
) -> Result<AscPtr<Uint8Array>, HostExportError> {
// Note on gas: There is no gas costing for the ipfs call itself,
// since it's not enabled on the network.
if !self
.as_ref()
.experimental_features
.allow_non_deterministic_ipfs
{
return Err(HostExportError::Deterministic(anyhow!(
"`ipfs.cat` is deprecated. Improved support for IPFS will be added in the future"
)));
}
let link = asc_get(self, link_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let logger = self.as_ref().ctx.logger.cheap_clone();
let ipfs_res = host_exports.ipfs_cat(&logger, link);
let logger = self.as_ref().ctx.logger.cheap_clone();
match ipfs_res {
Ok(bytes) => asc_new(self, &*bytes, gas).map_err(Into::into),
// Return null in case of error.
Err(e) => {
info!(&logger, "Failed ipfs.cat, returning `null`";
"link" => asc_get::<String, _, _>( self, link_ptr, gas)?,
"error" => e.to_string());
Ok(AscPtr::null())
}
}
}
/// function ipfs.getBlock(link: String): Bytes
pub fn ipfs_get_block(
&mut self,
gas: &GasCounter,
link_ptr: AscPtr<AscString>,
) -> Result<AscPtr<Uint8Array>, HostExportError> {
// Note on gas: There is no gas costing for the ipfs call itself,
// since it's not enabled on the network.
if !self
.as_ref()
.experimental_features
.allow_non_deterministic_ipfs
{
return Err(HostExportError::Deterministic(anyhow!(
"`ipfs.getBlock` is deprecated. Improved support for IPFS will be added in the future"
)));
}
let link = asc_get(self, link_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ipfs_res = host_exports.ipfs_get_block(&self.as_ref().ctx.logger, link);
match ipfs_res {
Ok(bytes) => asc_new(self, &*bytes, gas).map_err(Into::into),
// Return null in case of error.
Err(e) => {
info!(&self.as_ref().ctx.logger, "Failed ipfs.getBlock, returning `null`";
"link" => asc_get::<String, _, _>( self, link_ptr, gas)?,
"error" => e.to_string());
Ok(AscPtr::null())
}
}
}
/// function ipfs.map(link: String, callback: String, flags: String[]): void
pub fn ipfs_map(
&mut self,
gas: &GasCounter,
link_ptr: AscPtr<AscString>,
callback: AscPtr<AscString>,
user_data: AscPtr<AscEnum<StoreValueKind>>,
flags: AscPtr<Array<AscPtr<AscString>>>,
) -> Result<(), HostExportError> {
// Note on gas:
// Ideally we would consume gas the same as ipfs_cat and then share
// gas across the spawned modules for callbacks.
if !self
.as_ref()
.experimental_features
.allow_non_deterministic_ipfs
{
return Err(HostExportError::Deterministic(anyhow!(
"`ipfs.map` is deprecated. Improved support for IPFS will be added in the future"
)));
}
let link: String = asc_get(self, link_ptr, gas)?;
let callback: String = asc_get(self, callback, gas)?;
let user_data: store::Value = asc_get(self, user_data, gas)?;
let flags = asc_get(self, flags, gas)?;
// Pause the timeout while running ipfs_map, and resume it when done.
self.suspend_timeout();
let start_time = Instant::now();
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let output_states =
host_exports.ipfs_map(self.as_ref(), link.clone(), &callback, user_data, flags)?;
self.start_timeout();
debug!(
&self.as_ref().ctx.logger,
"Successfully processed file with ipfs.map";
"link" => &link,
"callback" => &*callback,
"n_calls" => output_states.len(),
"time" => format!("{}ms", start_time.elapsed().as_millis())
);
for output_state in output_states {
self.as_mut().ctx.state.extend(output_state);
}
Ok(())
}
/// Expects a decimal string.
/// function json.toI64(json: String): i64
pub fn json_to_i64(
&mut self,
gas: &GasCounter,
json_ptr: AscPtr<AscString>,
) -> Result<i64, DeterministicHostError> {
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let json = asc_get(self, json_ptr, gas)?;
let ctx = &mut self.as_mut().ctx;
host_exports.json_to_i64(json, gas, &mut ctx.state)
}
/// Expects a decimal string.
/// function json.toU64(json: String): u64
pub fn json_to_u64(
&mut self,
gas: &GasCounter,
json_ptr: AscPtr<AscString>,
) -> Result<u64, DeterministicHostError> {
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let json: String = asc_get(self, json_ptr, gas)?;
let ctx = &mut self.as_mut().ctx;
host_exports.json_to_u64(json, gas, &mut ctx.state)
}
/// Expects a decimal string.
/// function json.toF64(json: String): f64
pub fn json_to_f64(
&mut self,
gas: &GasCounter,
json_ptr: AscPtr<AscString>,
) -> Result<f64, DeterministicHostError> {
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let json = asc_get(self, json_ptr, gas)?;
let ctx = &mut self.as_mut().ctx;
host_exports.json_to_f64(json, gas, &mut ctx.state)
}
/// Expects a decimal string.
/// function json.toBigInt(json: String): BigInt
pub fn json_to_big_int(
&mut self,
gas: &GasCounter,
json_ptr: AscPtr<AscString>,
) -> Result<AscPtr<AscBigInt>, HostExportError> {
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let json = asc_get(self, json_ptr, gas)?;
let ctx = &mut self.as_mut().ctx;
let big_int = host_exports.json_to_big_int(json, gas, &mut ctx.state)?;
asc_new(self, &*big_int, gas)
}
/// function crypto.keccak256(input: Bytes): Bytes
pub fn crypto_keccak_256(
&mut self,
gas: &GasCounter,
input_ptr: AscPtr<Uint8Array>,
) -> Result<AscPtr<Uint8Array>, HostExportError> {
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let input = asc_get(self, input_ptr, gas)?;
let ctx = &mut self.as_mut().ctx;
let input = host_exports.crypto_keccak_256(input, gas, &mut ctx.state)?;
asc_new(self, input.as_ref(), gas)
}
/// function bigInt.plus(x: BigInt, y: BigInt): BigInt
pub fn big_int_plus(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigInt>,
y_ptr: AscPtr<AscBigInt>,
) -> Result<AscPtr<AscBigInt>, HostExportError> {
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let x = asc_get(self, x_ptr, gas)?;
let y = asc_get(self, y_ptr, gas)?;
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_int_plus(x, y, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigInt.minus(x: BigInt, y: BigInt): BigInt
pub fn big_int_minus(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigInt>,
y_ptr: AscPtr<AscBigInt>,
) -> Result<AscPtr<AscBigInt>, HostExportError> {
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let x = asc_get(self, x_ptr, gas)?;
let y = asc_get(self, y_ptr, gas)?;
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_int_minus(x, y, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigInt.times(x: BigInt, y: BigInt): BigInt
pub fn big_int_times(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigInt>,
y_ptr: AscPtr<AscBigInt>,
) -> Result<AscPtr<AscBigInt>, HostExportError> {
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let x = asc_get(self, x_ptr, gas)?;
let y = asc_get(self, y_ptr, gas)?;
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_int_times(x, y, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigInt.dividedBy(x: BigInt, y: BigInt): BigInt
pub fn big_int_divided_by(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigInt>,
y_ptr: AscPtr<AscBigInt>,
) -> Result<AscPtr<AscBigInt>, HostExportError> {
let x = asc_get(self, x_ptr, gas)?;
let y = asc_get(self, y_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_int_divided_by(x, y, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigInt.dividedByDecimal(x: BigInt, y: BigDecimal): BigDecimal
pub fn big_int_divided_by_decimal(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigInt>,
y_ptr: AscPtr<AscBigDecimal>,
) -> Result<AscPtr<AscBigDecimal>, HostExportError> {
let x = BigDecimal::new(asc_get(self, x_ptr, gas)?, 0);
let y = asc_get(self, y_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_decimal_divided_by(x, y, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigInt.mod(x: BigInt, y: BigInt): BigInt
pub fn big_int_mod(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigInt>,
y_ptr: AscPtr<AscBigInt>,
) -> Result<AscPtr<AscBigInt>, HostExportError> {
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let x = asc_get(self, x_ptr, gas)?;
let y = asc_get(self, y_ptr, gas)?;
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_int_mod(x, y, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigInt.pow(x: BigInt, exp: u8): BigInt
pub fn big_int_pow(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigInt>,
exp: u32,
) -> Result<AscPtr<AscBigInt>, HostExportError> {
let exp = u8::try_from(exp).map_err(|e| DeterministicHostError::from(Error::from(e)))?;
let x = asc_get(self, x_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_int_pow(x, exp, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigInt.bitOr(x: BigInt, y: BigInt): BigInt
pub fn big_int_bit_or(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigInt>,
y_ptr: AscPtr<AscBigInt>,
) -> Result<AscPtr<AscBigInt>, HostExportError> {
let x = asc_get(self, x_ptr, gas)?;
let y = asc_get(self, y_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_int_bit_or(x, y, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigInt.bitAnd(x: BigInt, y: BigInt): BigInt
pub fn big_int_bit_and(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigInt>,
y_ptr: AscPtr<AscBigInt>,
) -> Result<AscPtr<AscBigInt>, HostExportError> {
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let x = asc_get(self, x_ptr, gas)?;
let y = asc_get(self, y_ptr, gas)?;
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_int_bit_and(x, y, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigInt.leftShift(x: BigInt, bits: u8): BigInt
pub fn big_int_left_shift(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigInt>,
bits: u32,
) -> Result<AscPtr<AscBigInt>, HostExportError> {
let bits = u8::try_from(bits).map_err(|e| DeterministicHostError::from(Error::from(e)))?;
let x = asc_get(self, x_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_int_left_shift(x, bits, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigInt.rightShift(x: BigInt, bits: u8): BigInt
pub fn big_int_right_shift(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigInt>,
bits: u32,
) -> Result<AscPtr<AscBigInt>, HostExportError> {
let bits = u8::try_from(bits).map_err(|e| DeterministicHostError::from(Error::from(e)))?;
let x = asc_get(self, x_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_int_right_shift(x, bits, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function typeConversion.bytesToBase58(bytes: Bytes): string
pub fn bytes_to_base58(
&mut self,
gas: &GasCounter,
bytes_ptr: AscPtr<Uint8Array>,
) -> Result<AscPtr<AscString>, HostExportError> {
let bytes = asc_get(self, bytes_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports.bytes_to_base58(bytes, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigDecimal.toString(x: BigDecimal): string
pub fn big_decimal_to_string(
&mut self,
gas: &GasCounter,
big_decimal_ptr: AscPtr<AscBigDecimal>,
) -> Result<AscPtr<AscString>, HostExportError> {
let x = asc_get(self, big_decimal_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_decimal_to_string(x, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigDecimal.fromString(x: string): BigDecimal
pub fn big_decimal_from_string(
&mut self,
gas: &GasCounter,
string_ptr: AscPtr<AscString>,
) -> Result<AscPtr<AscBigDecimal>, HostExportError> {
let s = asc_get(self, string_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_decimal_from_string(s, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigDecimal.plus(x: BigDecimal, y: BigDecimal): BigDecimal
pub fn big_decimal_plus(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigDecimal>,
y_ptr: AscPtr<AscBigDecimal>,
) -> Result<AscPtr<AscBigDecimal>, HostExportError> {
let x = asc_get(self, x_ptr, gas)?;
let y = asc_get(self, y_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_decimal_plus(x, y, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigDecimal.minus(x: BigDecimal, y: BigDecimal): BigDecimal
pub fn big_decimal_minus(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigDecimal>,
y_ptr: AscPtr<AscBigDecimal>,
) -> Result<AscPtr<AscBigDecimal>, HostExportError> {
let x = asc_get(self, x_ptr, gas)?;
let y = asc_get(self, y_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_decimal_minus(x, y, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigDecimal.times(x: BigDecimal, y: BigDecimal): BigDecimal
pub fn big_decimal_times(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigDecimal>,
y_ptr: AscPtr<AscBigDecimal>,
) -> Result<AscPtr<AscBigDecimal>, HostExportError> {
let x = asc_get(self, x_ptr, gas)?;
let y = asc_get(self, y_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_decimal_times(x, y, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigDecimal.dividedBy(x: BigDecimal, y: BigDecimal): BigDecimal
pub fn big_decimal_divided_by(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigDecimal>,
y_ptr: AscPtr<AscBigDecimal>,
) -> Result<AscPtr<AscBigDecimal>, HostExportError> {
let x = asc_get(self, x_ptr, gas)?;
let y = asc_get(self, y_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
let result = host_exports.big_decimal_divided_by(x, y, gas, &mut ctx.state)?;
asc_new(self, &result, gas)
}
/// function bigDecimal.equals(x: BigDecimal, y: BigDecimal): bool
pub fn big_decimal_equals(
&mut self,
gas: &GasCounter,
x_ptr: AscPtr<AscBigDecimal>,
y_ptr: AscPtr<AscBigDecimal>,
) -> Result<bool, HostExportError> {
let x = asc_get(self, x_ptr, gas)?;
let y = asc_get(self, y_ptr, gas)?;
let host_exports = self.as_ref().ctx.host_exports.cheap_clone();
let ctx = &mut self.as_mut().ctx;
host_exports.big_decimal_equals(x, y, gas, &mut ctx.state)