casper-contract-sdk 0.1.2

Casper contract sdk package
Documentation
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
use std::{
    cell::RefCell,
    collections::{BTreeMap, BTreeSet, VecDeque},
    convert::Infallible,
    fmt,
    panic::{self, UnwindSafe},
    ptr::{self, NonNull},
    slice,
    sync::{Arc, RwLock},
};

use crate::linkme::distributed_slice;
use bytes::Bytes;
use casper_executor_wasm_common::{
    env_info::EnvInfo,
    error::{
        CALLEE_REVERTED, CALLEE_SUCCEEDED, CALLEE_TRAPPED, HOST_ERROR_INTERNAL,
        HOST_ERROR_NOT_FOUND, HOST_ERROR_SUCCESS,
    },
    flags::ReturnFlags,
};
#[cfg(not(target_arch = "wasm32"))]
use rand::Rng;

use super::Entity;
use crate::types::Address;

/// The kind of export that is being registered.
///
/// This is used to identify the type of export and its name.
///
/// Depending on the location of given function it may be registered as a:
///
/// * `SmartContract` (if it's part of a `impl Contract` block),
/// * `TraitImpl` (if it's part of a `impl Trait for Contract` block),
/// * `Function` (if it's a standalone function).
///
/// This is used to dispatch exports under native code i.e. you want to write a test that calls
/// "foobar" regardless of location.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum EntryPointKind {
    /// Smart contract.
    ///
    /// This is used to identify the smart contract and its name.
    ///
    /// The `struct_name` is the name of the smart contract that is being registered.
    /// The `name` is the name of the function that is being registered.
    SmartContract {
        struct_name: &'static str,
        name: &'static str,
    },
    /// Trait implementation.
    ///
    /// This is used to identify the trait implementation and its name.
    ///
    /// The `trait_name` is the name of the trait that is being implemented.
    /// The `impl_name` is the name of the implementation.
    /// The `name` is the name of the function that is being implemented.
    TraitImpl {
        trait_name: &'static str,
        impl_name: &'static str,
        name: &'static str,
    },
    /// Function export.
    ///
    /// This is used to identify the function export and its name.
    ///
    /// The `name` is the name of the function that is being exported.
    Function { name: &'static str },
}

impl EntryPointKind {
    pub fn name(&self) -> &'static str {
        match self {
            EntryPointKind::SmartContract { name, .. }
            | EntryPointKind::TraitImpl { name, .. }
            | EntryPointKind::Function { name } => name,
        }
    }
}

/// Export is a structure that contains information about the exported function.
///
/// This is used to register the export and its name and physical location in the smart contract
/// source code.
pub struct EntryPoint {
    /// The kind of entry point that is being registered.
    pub kind: EntryPointKind,
    pub fptr: fn() -> (),
    pub module_path: &'static str,
    pub file: &'static str,
    pub line: u32,
}

#[distributed_slice]
#[linkme(crate = crate::linkme)]
pub static ENTRY_POINTS: [EntryPoint];

impl fmt::Debug for EntryPoint {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Self {
            kind,
            fptr: _,
            module_path,
            file,
            line,
        } = self;

        f.debug_struct("Export")
            .field("kind", kind)
            .field("fptr", &"<fptr>")
            .field("module_path", module_path)
            .field("file", file)
            .field("line", line)
            .finish()
    }
}

/// Invokes an export by its name.
///
/// This function is used to invoke an export by its name regardless of its location in the smart
/// contract.
pub fn invoke_export_by_name(name: &str) {
    let exports_by_name: Vec<_> = ENTRY_POINTS
        .iter()
        .filter(|export| export.kind.name() == name)
        .collect();

    assert_eq!(
        exports_by_name.len(),
        1,
        "Expected exactly one export {name} found, but got {exports_by_name:?}"
    );

    (exports_by_name[0].fptr)();
}

#[derive(Debug)]
pub enum NativeTrap {
    Return(ReturnFlags, Bytes),
    Panic(Box<dyn std::any::Any + Send + 'static>),
}

pub type Container = BTreeMap<u64, BTreeMap<Bytes, Bytes>>;

#[derive(Clone, Debug)]
#[allow(dead_code)]
pub struct NativeParam(pub(crate) String);

impl From<&casper_contract_sdk_sys::Param> for NativeParam {
    fn from(val: &casper_contract_sdk_sys::Param) -> Self {
        let name =
            String::from_utf8_lossy(unsafe { slice::from_raw_parts(val.name_ptr, val.name_len) })
                .into_owned();
        NativeParam(name)
    }
}

#[derive(Clone, Debug)]
pub struct Environment {
    pub db: Arc<RwLock<Container>>,
    contracts: Arc<RwLock<BTreeSet<Address>>>,
    // input_data: Arc<RwLock<Option<Bytes>>>,
    input_data: Option<Bytes>,
    caller: Entity,
    callee: Entity,
}

impl Default for Environment {
    fn default() -> Self {
        Self {
            db: Default::default(),
            contracts: Default::default(),
            input_data: Default::default(),
            caller: DEFAULT_ADDRESS,
            callee: DEFAULT_ADDRESS,
        }
    }
}

pub const DEFAULT_ADDRESS: Entity = Entity::Account([42; 32]);

impl Environment {
    #[must_use]
    pub fn new(db: Container, caller: Entity) -> Self {
        Self {
            db: Arc::new(RwLock::new(db)),
            contracts: Default::default(),
            input_data: Default::default(),
            caller,
            callee: caller,
        }
    }

    #[must_use]
    pub fn with_caller(&self, caller: Entity) -> Self {
        let mut env = self.clone();
        env.caller = caller;
        env
    }

    #[must_use]
    pub fn smart_contract(&self, callee: Entity) -> Self {
        let mut env = self.clone();
        env.caller = self.callee;
        env.callee = callee;
        env
    }

    #[must_use]
    pub fn session(&self, callee: Entity) -> Self {
        let mut env = self.clone();
        env.caller = callee;
        env.callee = callee;
        env
    }

    #[must_use]
    pub fn with_callee(&self, callee: Entity) -> Self {
        let mut env = self.clone();
        env.callee = callee;
        env
    }

    #[must_use]
    pub fn with_input_data(&self, input_data: Vec<u8>) -> Self {
        let mut env = self.clone();
        env.input_data = Some(Bytes::from(input_data));
        env
    }
}

impl Environment {
    fn key_prefix(&self, key: &[u8]) -> Vec<u8> {
        let entity = self.callee;

        let mut bytes = Vec::new();
        bytes.extend(entity.tag().to_le_bytes());
        bytes.extend(entity.address());
        bytes.extend(key);

        bytes
    }

    fn casper_read(
        &self,
        key_space: u64,
        key_ptr: *const u8,
        key_size: usize,
        info: *mut casper_contract_sdk_sys::ReadInfo,
        alloc: extern "C" fn(usize, *mut core::ffi::c_void) -> *mut u8,
        alloc_ctx: *const core::ffi::c_void,
    ) -> Result<u32, NativeTrap> {
        let key_bytes = unsafe { slice::from_raw_parts(key_ptr, key_size) };
        let key_bytes = self.key_prefix(key_bytes);

        let Ok(db) = self.db.read() else {
            return Ok(HOST_ERROR_INTERNAL);
        };

        let value = match db.get(&key_space) {
            Some(values) => values.get(key_bytes.as_slice()).cloned(),
            None => return Ok(HOST_ERROR_NOT_FOUND),
        };
        match value {
            Some(tagged_value) => {
                let ptr = NonNull::new(alloc(tagged_value.len(), alloc_ctx as _));

                if let Some(ptr) = ptr {
                    unsafe {
                        (*info).data = ptr.as_ptr();
                        (*info).size = tagged_value.len();
                    }

                    unsafe {
                        ptr::copy_nonoverlapping(
                            tagged_value.as_ptr(),
                            ptr.as_ptr(),
                            tagged_value.len(),
                        );
                    }
                }

                Ok(HOST_ERROR_SUCCESS)
            }
            None => Ok(HOST_ERROR_NOT_FOUND),
        }
    }

    fn casper_write(
        &self,
        key_space: u64,
        key_ptr: *const u8,
        key_size: usize,
        value_ptr: *const u8,
        value_size: usize,
    ) -> Result<u32, NativeTrap> {
        assert!(!key_ptr.is_null());
        assert!(!value_ptr.is_null());
        // let key_bytes = unsafe { slice::from_raw_parts(key_ptr, key_size) };
        let key_bytes = unsafe { slice::from_raw_parts(key_ptr, key_size) }.to_owned();
        let key_bytes = self.key_prefix(&key_bytes);

        let value_bytes = unsafe { slice::from_raw_parts(value_ptr, value_size) };

        let mut db = self.db.write().unwrap();
        db.entry(key_space).or_default().insert(
            Bytes::from(key_bytes.to_vec()),
            Bytes::from(value_bytes.to_vec()),
        );
        Ok(HOST_ERROR_SUCCESS)
    }

    fn casper_remove(
        &self,
        key_space: u64,
        key_ptr: *const u8,
        key_size: usize,
    ) -> Result<u32, NativeTrap> {
        assert!(!key_ptr.is_null());
        let key_bytes = unsafe { slice::from_raw_parts(key_ptr, key_size) };
        let key_bytes = self.key_prefix(key_bytes);

        let mut db = self.db.write().unwrap();
        if let Some(values) = db.get_mut(&key_space) {
            values.remove(key_bytes.as_slice());
            Ok(HOST_ERROR_SUCCESS)
        } else {
            Ok(HOST_ERROR_NOT_FOUND)
        }
    }

    fn casper_print(&self, msg_ptr: *const u8, msg_size: usize) -> Result<(), NativeTrap> {
        let msg_bytes = unsafe { slice::from_raw_parts(msg_ptr, msg_size) };
        let msg = std::str::from_utf8(msg_bytes).expect("Valid UTF-8 string");
        println!("💻 {msg}");
        Ok(())
    }

    fn casper_return(
        &self,
        flags: u32,
        data_ptr: *const u8,
        data_len: usize,
    ) -> Result<Infallible, NativeTrap> {
        let return_flags = ReturnFlags::from_bits_truncate(flags);
        let data = if data_ptr.is_null() {
            Bytes::new()
        } else {
            Bytes::copy_from_slice(unsafe { slice::from_raw_parts(data_ptr, data_len) })
        };
        Err(NativeTrap::Return(return_flags, data))
    }

    fn casper_copy_input(
        &self,
        alloc: extern "C" fn(usize, *mut core::ffi::c_void) -> *mut u8,
        alloc_ctx: *const core::ffi::c_void,
    ) -> Result<*mut u8, NativeTrap> {
        let input_data = self.input_data.clone();
        let input_data = input_data.as_ref().cloned().unwrap_or_default();
        let ptr = NonNull::new(alloc(input_data.len(), alloc_ctx as _));

        match ptr {
            Some(ptr) => {
                if !input_data.is_empty() {
                    unsafe {
                        ptr::copy_nonoverlapping(
                            input_data.as_ptr(),
                            ptr.as_ptr(),
                            input_data.len(),
                        );
                    }
                }
                Ok(unsafe { ptr.as_ptr().add(input_data.len()) })
            }
            None => Ok(ptr::null_mut()),
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn casper_create(
        &self,
        code_ptr: *const u8,
        code_size: usize,
        transferred_value: u64,
        constructor_ptr: *const u8,
        constructor_size: usize,
        input_ptr: *const u8,
        input_size: usize,
        seed_ptr: *const u8,
        seed_size: usize,
        result_ptr: *mut casper_contract_sdk_sys::CreateResult,
    ) -> Result<u32, NativeTrap> {
        // let manifest =
        //     NonNull::new(manifest_ptr as *mut casper_contract_sdk_sys::Manifest).expect("Manifest
        // instance");
        let code = if code_ptr.is_null() {
            None
        } else {
            Some(unsafe { slice::from_raw_parts(code_ptr, code_size) })
        };

        if code.is_some() {
            panic!("Supplying code is not supported yet in native mode");
        }

        let constructor = if constructor_ptr.is_null() {
            None
        } else {
            Some(unsafe { slice::from_raw_parts(constructor_ptr, constructor_size) })
        };

        let input_data = if input_ptr.is_null() {
            None
        } else {
            Some(unsafe { slice::from_raw_parts(input_ptr, input_size) })
        };

        let _seed = if seed_ptr.is_null() {
            None
        } else {
            Some(unsafe { slice::from_raw_parts(seed_ptr, seed_size) })
        };

        assert_eq!(
            transferred_value, 0,
            "Creating new contracts with transferred value is not supported in native mode"
        );

        let mut rng = rand::thread_rng();
        let contract_address = rng.gen();
        let package_address = rng.gen();

        let mut result = NonNull::new(result_ptr).expect("Valid pointer");
        unsafe {
            result.as_mut().contract_address = package_address;
        }

        let mut contracts = self.contracts.write().unwrap();
        contracts.insert(contract_address);

        if let Some(entry_point) = constructor {
            let entry_point = ENTRY_POINTS
                .iter()
                .find(|export| export.kind.name().as_bytes() == entry_point)
                .expect("Entry point exists");

            let mut stub = with_current_environment(|stub| stub);
            stub.input_data = input_data.map(Bytes::copy_from_slice);

            stub.caller = stub.callee;
            stub.callee = Entity::Contract(package_address);

            // stub.callee
            // Call constructor, expect a trap
            let result = dispatch_with(stub, || {
                // TODO: Handle panic inside constructor
                (entry_point.fptr)();
            });

            match result {
                Ok(()) => {}
                Err(NativeTrap::Return(flags, bytes)) => {
                    if flags.contains(ReturnFlags::REVERT) {
                        todo!("Constructor returned with a revert flag");
                    }
                    assert!(bytes.is_empty(), "When returning from the constructor it is expected that no bytes are passed in a return function");
                }
                Err(NativeTrap::Panic(_panic)) => {
                    todo!();
                }
            }
        }

        Ok(HOST_ERROR_SUCCESS)
    }

    #[allow(clippy::too_many_arguments)]
    fn casper_call(
        &self,
        address_ptr: *const u8,
        address_size: usize,
        transferred_value: u64,
        entry_point_ptr: *const u8,
        entry_point_size: usize,
        input_ptr: *const u8,
        input_size: usize,
        alloc: extern "C" fn(usize, *mut core::ffi::c_void) -> *mut u8, /* For capturing output
                                                                         * data */
        alloc_ctx: *const core::ffi::c_void,
    ) -> Result<u32, NativeTrap> {
        let address = unsafe { slice::from_raw_parts(address_ptr, address_size) };
        let input_data = unsafe { slice::from_raw_parts(input_ptr, input_size) };
        let entry_point = {
            let entry_point_ptr = NonNull::new(entry_point_ptr.cast_mut()).expect("Valid pointer");
            let entry_point =
                unsafe { slice::from_raw_parts(entry_point_ptr.as_ptr(), entry_point_size) };
            let entry_point = std::str::from_utf8(entry_point).expect("Valid UTF-8 string");
            entry_point.to_string()
        };

        assert_eq!(
            transferred_value, 0,
            "Transferred value is not supported in native mode"
        );

        let export = ENTRY_POINTS
            .iter()
            .find(|export|
                matches!(export.kind, EntryPointKind::SmartContract { name, .. } | EntryPointKind::TraitImpl { name, .. }
                    if name == entry_point)
            )
            .expect("Existing entry point");

        let mut new_stub = with_current_environment(|stub| stub.clone());
        new_stub.input_data = Some(Bytes::copy_from_slice(input_data));
        new_stub.caller = new_stub.callee;
        new_stub.callee = Entity::Contract(address.try_into().expect("Size to match"));

        let ret = dispatch_with(new_stub, || {
            // We need to convert any panic inside the entry point into a native trap. This probably
            // should be done in a more configurable way.
            dispatch_export_call(|| {
                (export.fptr)();
            })
        });

        let unfolded = match ret {
            Ok(Ok(())) => Ok(()),
            Ok(Err(error)) | Err(error) => Err(error),
        };

        match unfolded {
            Ok(()) => Ok(CALLEE_SUCCEEDED),
            Err(NativeTrap::Return(flags, bytes)) => {
                let ptr = NonNull::new(alloc(bytes.len(), alloc_ctx.cast_mut()));
                if let Some(output_ptr) = ptr {
                    unsafe {
                        ptr::copy_nonoverlapping(bytes.as_ptr(), output_ptr.as_ptr(), bytes.len());
                    }
                }

                if flags.contains(ReturnFlags::REVERT) {
                    Ok(CALLEE_REVERTED)
                } else {
                    Ok(CALLEE_SUCCEEDED)
                }
            }
            Err(NativeTrap::Panic(panic)) => {
                eprintln!("Panic {panic:?}");
                Ok(CALLEE_TRAPPED)
            }
        }
    }

    #[doc = r"Obtain data from the blockchain environemnt of current wasm invocation.

Example paths:

* `env_read([CASPER_CALLER], 1, nullptr, &caller_addr)` -> read caller's address into
  `caller_addr` memory.
* `env_read([CASPER_CHAIN, BLOCK_HASH, 0], 3, nullptr, &block_hash)` -> read hash of the
  current block into `block_hash` memory.
* `env_read([CASPER_CHAIN, BLOCK_HASH, 5], 3, nullptr, &block_hash)` -> read hash of the 5th
  block from the current one into `block_hash` memory.
* `env_read([CASPER_AUTHORIZED_KEYS], 1, nullptr, &authorized_keys)` -> read list of
  authorized keys into `authorized_keys` memory."]
    fn casper_env_read(
        &self,
        _env_path: *const u64,
        _env_path_size: usize,
        _alloc: Option<extern "C" fn(usize, *mut core::ffi::c_void) -> *mut u8>,
        _alloc_ctx: *const core::ffi::c_void,
    ) -> Result<*mut u8, NativeTrap> {
        todo!()
    }

    fn casper_env_info(&self, info_ptr: *const u8, info_size: u32) -> Result<u32, NativeTrap> {
        assert_eq!(info_size as usize, size_of::<EnvInfo>());
        let mut env_info = NonNull::new(info_ptr as *mut u8)
            .expect("Valid ptr")
            .cast::<EnvInfo>();
        let env_info = unsafe { env_info.as_mut() };
        *env_info = EnvInfo {
            block_time: 0,
            transferred_value: 0,
            caller_addr: *self.caller.address(),
            caller_kind: self.caller.tag(),
            callee_addr: *self.callee.address(),
            callee_kind: self.callee.tag(),
        };
        Ok(HOST_ERROR_SUCCESS)
    }
}

thread_local! {
    pub(crate) static LAST_TRAP: RefCell<Option<NativeTrap>> = const { RefCell::new(None) };
    static ENV_STACK: RefCell<VecDeque<Environment>> = RefCell::new(VecDeque::from_iter([
        // Stack of environments has a default element so unit tests do not require extra effort.
        // Environment::default()
    ]));
}

pub fn with_current_environment<T>(f: impl FnOnce(Environment) -> T) -> T {
    ENV_STACK.with(|stack| {
        let stub = {
            let borrowed = stack.borrow();
            let front = borrowed.front().expect("Stub exists").clone();
            front
        };
        f(stub)
    })
}

pub fn current_environment() -> Environment {
    with_current_environment(|env| env)
}

fn handle_ret_with<T>(value: Result<T, NativeTrap>, ret: impl FnOnce() -> T) -> T {
    match value {
        Ok(result) => {
            LAST_TRAP.with(|last_trap| last_trap.borrow_mut().take());
            result
        }
        Err(trap) => {
            let result = ret();
            LAST_TRAP.with(|last_trap| last_trap.borrow_mut().replace(trap));
            result
        }
    }
}

fn dispatch_export_call<F>(func: F) -> Result<(), NativeTrap>
where
    F: FnOnce() + Send + UnwindSafe,
{
    let call_result = panic::catch_unwind(|| {
        func();
    });
    match call_result {
        Ok(()) => {
            let last_trap = LAST_TRAP.with(|last_trap| last_trap.borrow_mut().take());
            match last_trap {
                Some(last_trap) => Err(last_trap),
                None => Ok(()),
            }
        }
        Err(error) => Err(NativeTrap::Panic(error)),
    }
}

fn handle_ret<T: Default>(value: Result<T, NativeTrap>) -> T {
    handle_ret_with(value, || T::default())
}

/// Dispatches a function with a default environment.
pub fn dispatch<T>(f: impl FnOnce() -> T) -> Result<T, NativeTrap> {
    dispatch_with(Environment::default(), f)
}

/// Dispatches a function with a given environment.
pub fn dispatch_with<T>(stub: Environment, f: impl FnOnce() -> T) -> Result<T, NativeTrap> {
    ENV_STACK.with(|stack| {
        let mut borrowed = stack.borrow_mut();
        borrowed.push_front(stub);
    });

    // Clear previous trap (if present)
    LAST_TRAP.with(|last_trap| last_trap.borrow_mut().take());

    // Call a function
    let result = f();

    // Check if a trap was set and return it if so (otherwise return the result).
    let last_trap = LAST_TRAP.with(|last_trap| last_trap.borrow_mut().take());

    let result = if let Some(trap) = last_trap {
        Err(trap)
    } else {
        Ok(result)
    };

    // Pop the stub from the stack
    ENV_STACK.with(|stack| {
        let mut borrowed = stack.borrow_mut();
        borrowed.pop_front();
    });

    result
}

mod symbols {
    // TODO: Figure out how to use for_each_host_function macro here and deal with never type in
    // casper_return
    #[no_mangle]
    /// Read value from a storage available for caller's entity address.
    pub extern "C" fn casper_read(
        key_space: u64,
        key_ptr: *const u8,
        key_size: usize,
        info: *mut ::casper_contract_sdk_sys::ReadInfo,
        alloc: extern "C" fn(usize, *mut core::ffi::c_void) -> *mut u8,
        alloc_ctx: *const core::ffi::c_void,
    ) -> u32 {
        let _name = "casper_read";
        let _args = (&key_space, &key_ptr, &key_size, &info, &alloc, &alloc_ctx);
        let _call_result = with_current_environment(|stub| {
            stub.casper_read(key_space, key_ptr, key_size, info, alloc, alloc_ctx)
        });
        crate::casper::native::handle_ret(_call_result)
    }

    #[no_mangle]
    pub extern "C" fn casper_write(
        key_space: u64,
        key_ptr: *const u8,
        key_size: usize,
        value_ptr: *const u8,
        value_size: usize,
    ) -> u32 {
        let _name = "casper_write";
        let _args = (&key_space, &key_ptr, &key_size, &value_ptr, &value_size);
        let _call_result = with_current_environment(|stub| {
            stub.casper_write(key_space, key_ptr, key_size, value_ptr, value_size)
        });
        crate::casper::native::handle_ret(_call_result)
    }

    #[no_mangle]
    pub extern "C" fn casper_remove(key_space: u64, key_ptr: *const u8, key_size: usize) -> u32 {
        let _name = "casper_remove";
        let _args = (&key_space, &key_ptr, &key_size);
        let _call_result =
            with_current_environment(|stub| stub.casper_remove(key_space, key_ptr, key_size));
        crate::casper::native::handle_ret(_call_result)
    }

    #[no_mangle]
    pub extern "C" fn casper_print(msg_ptr: *const u8, msg_size: usize) {
        let _name = "casper_print";
        let _args = (&msg_ptr, &msg_size);
        let _call_result = with_current_environment(|stub| stub.casper_print(msg_ptr, msg_size));
        crate::casper::native::handle_ret(_call_result);
    }

    use casper_executor_wasm_common::error::HOST_ERROR_SUCCESS;

    use crate::casper::native::LAST_TRAP;

    #[no_mangle]
    pub extern "C" fn casper_return(flags: u32, data_ptr: *const u8, data_len: usize) {
        let _name = "casper_return";
        let _args = (&flags, &data_ptr, &data_len);
        let _call_result =
            with_current_environment(|stub| stub.casper_return(flags, data_ptr, data_len));
        let err = _call_result.unwrap_err(); // SAFE
        LAST_TRAP.with(|last_trap| last_trap.borrow_mut().replace(err));
    }

    #[no_mangle]
    pub extern "C" fn casper_copy_input(
        alloc: extern "C" fn(usize, *mut core::ffi::c_void) -> *mut u8,
        alloc_ctx: *const core::ffi::c_void,
    ) -> *mut u8 {
        let _name = "casper_copy_input";
        let _args = (&alloc, &alloc_ctx);
        let _call_result =
            with_current_environment(|stub| stub.casper_copy_input(alloc, alloc_ctx));
        crate::casper::native::handle_ret_with(_call_result, ptr::null_mut)
    }

    #[no_mangle]
    pub extern "C" fn casper_create(
        code_ptr: *const u8,
        code_size: usize,
        transferred_value: u64,
        constructor_ptr: *const u8,
        constructor_size: usize,
        input_ptr: *const u8,
        input_size: usize,
        seed_ptr: *const u8,
        seed_size: usize,
        result_ptr: *mut casper_contract_sdk_sys::CreateResult,
    ) -> u32 {
        let _call_result = with_current_environment(|stub| {
            stub.casper_create(
                code_ptr,
                code_size,
                transferred_value,
                constructor_ptr,
                constructor_size,
                input_ptr,
                input_size,
                seed_ptr,
                seed_size,
                result_ptr,
            )
        });
        crate::casper::native::handle_ret(_call_result)
    }

    #[no_mangle]
    pub extern "C" fn casper_call(
        address_ptr: *const u8,
        address_size: usize,
        transferred_value: u64,
        entry_point_ptr: *const u8,
        entry_point_size: usize,
        input_ptr: *const u8,
        input_size: usize,
        alloc: extern "C" fn(usize, *mut core::ffi::c_void) -> *mut u8, /* For capturing output
                                                                         * data */
        alloc_ctx: *const core::ffi::c_void,
    ) -> u32 {
        let _call_result = with_current_environment(|stub| {
            stub.casper_call(
                address_ptr,
                address_size,
                transferred_value,
                entry_point_ptr,
                entry_point_size,
                input_ptr,
                input_size,
                alloc,
                alloc_ctx,
            )
        });
        crate::casper::native::handle_ret(_call_result)
    }

    #[no_mangle]
    pub extern "C" fn casper_upgrade(
        _code_ptr: *const u8,
        _code_size: usize,
        _entry_point_ptr: *const u8,
        _entry_point_size: usize,
        _input_ptr: *const u8,
        _input_size: usize,
    ) -> u32 {
        todo!()
    }

    use core::slice;
    use std::ptr;

    use super::with_current_environment;

    #[no_mangle]
    pub extern "C" fn casper_env_read(
        env_path: *const u64,
        env_path_size: usize,
        alloc: Option<extern "C" fn(usize, *mut core::ffi::c_void) -> *mut u8>,
        alloc_ctx: *const core::ffi::c_void,
    ) -> *mut u8 {
        let _name = "casper_env_read";
        let _args = (&env_path, &env_path_size, &alloc, &alloc_ctx);
        let _call_result = with_current_environment(|stub| {
            stub.casper_env_read(env_path, env_path_size, alloc, alloc_ctx)
        });
        crate::casper::native::handle_ret_with(_call_result, ptr::null_mut)
    }
    #[no_mangle]
    pub extern "C" fn casper_env_balance(
        _entity_kind: u32,
        _entity_addr_ptr: *const u8,
        _entity_addr_len: usize,
    ) -> u64 {
        todo!()
    }
    #[no_mangle]
    pub extern "C" fn casper_transfer(
        _entity_kind: u32,
        _entity_addr_ptr: *const u8,
        _entity_addr_len: usize,
        _amount: u64,
    ) -> u32 {
        todo!()
    }
    #[no_mangle]
    pub extern "C" fn casper_emit(
        topic_ptr: *const u8,
        topic_size: usize,
        data_ptr: *const u8,
        data_size: usize,
    ) -> u32 {
        let topic = unsafe { slice::from_raw_parts(topic_ptr, topic_size) };
        let data = unsafe { slice::from_raw_parts(data_ptr, data_size) };
        let topic = std::str::from_utf8(topic).expect("Valid UTF-8 string");
        println!("Emitting event with topic: {topic:?} and data: {data:?}");
        HOST_ERROR_SUCCESS
    }

    #[no_mangle]
    pub extern "C" fn casper_env_info(info_ptr: *const u8, info_size: u32) -> u32 {
        let ret = with_current_environment(|env| env.casper_env_info(info_ptr, info_size));
        crate::casper::native::handle_ret(ret)
    }
}

#[cfg(test)]
mod tests {
    use casper_executor_wasm_common::keyspace::Keyspace;

    use crate::casper;

    use super::*;

    #[test]
    fn foo() {
        dispatch(|| {
            casper::print("Hello");
            casper::write(Keyspace::Context(b"test"), b"value 1").unwrap();

            let change_context_1 =
                with_current_environment(|stub| stub.smart_contract(Entity::Contract([1; 32])));

            dispatch_with(change_context_1, || {
                casper::write(Keyspace::Context(b"test"), b"value 2").unwrap();
                casper::write(Keyspace::State, b"state").unwrap();
            })
            .unwrap();

            let change_context_1 =
                with_current_environment(|stub| stub.smart_contract(Entity::Contract([1; 32])));
            dispatch_with(change_context_1, || {
                assert_eq!(
                    casper::read_into_vec(Keyspace::Context(b"test")),
                    Ok(Some(b"value 2".to_vec()))
                );
                assert_eq!(
                    casper::read_into_vec(Keyspace::State),
                    Ok(Some(b"state".to_vec()))
                );
            })
            .unwrap();

            assert_eq!(casper::get_caller(), DEFAULT_ADDRESS);
            assert_eq!(
                casper::read_into_vec(Keyspace::Context(b"test")),
                Ok(Some(b"value 1".to_vec()))
            );
        })
        .unwrap();
    }
    #[test]
    fn test() {
        dispatch_with(Environment::default(), || {
            let msg = "Hello";
            let () = with_current_environment(|stub| stub.casper_print(msg.as_ptr(), msg.len()))
                .expect("Ok");
        })
        .unwrap();
    }

    #[test]
    fn test_returns() {
        dispatch_with(Environment::default(), || {
            let _ = with_current_environment(|stub| stub.casper_return(0, ptr::null(), 0));
        })
        .unwrap();
    }
}