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
/* This file is part of DarkFi (https://dark.fi)
 *
 * Copyright (C) 2020-2024 Dyne.org foundation
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use core::{mem::size_of, slice::from_raw_parts};

use crate::crypto::ContractId;

/// Success exit code for a contract
pub const SUCCESS: i64 = 0;

#[macro_export]
macro_rules! define_contract {
    (
        init: $init_func:ident,
        exec: $exec_func:ident,
        apply: $apply_func:ident,
        metadata: $metadata_func:ident
    ) => {
        /// # Safety
        #[no_mangle]
        pub unsafe extern "C" fn __initialize(input: *mut u8) -> i64 {
            let (contract_id, instruction_data) = $crate::wasm::entrypoint::deserialize(input);

            match $init_func(contract_id, &instruction_data) {
                Ok(()) => $crate::wasm::entrypoint::SUCCESS,
                Err(e) => e.into(),
            }
        }
        #[no_mangle]
        pub unsafe extern "C" fn __entrypoint(input: *mut u8) -> i64 {
            let (contract_id, instruction_data) = $crate::wasm::entrypoint::deserialize(input);

            match $exec_func(contract_id, &instruction_data) {
                Ok(()) => $crate::wasm::entrypoint::SUCCESS,
                Err(e) => e.into(),
            }
        }
        #[no_mangle]
        pub unsafe extern "C" fn __update(input: *mut u8) -> i64 {
            let (contract_id, update_data) = $crate::wasm::entrypoint::deserialize(input);

            match $apply_func(contract_id, &update_data) {
                Ok(()) => $crate::wasm::entrypoint::SUCCESS,
                Err(e) => e.into(),
            }
        }
        #[no_mangle]
        pub unsafe extern "C" fn __metadata(input: *mut u8) -> i64 {
            let (contract_id, instruction_data) = $crate::wasm::entrypoint::deserialize(input);

            match $metadata_func(contract_id, &instruction_data) {
                Ok(()) => $crate::wasm::entrypoint::SUCCESS,
                Err(e) => e.into(),
            }
        }
    };
}

/// Deserialize a given payload in `entrypoint`
/// The return values from this are the input values for the above defined functions.
/// # Safety
pub unsafe fn deserialize<'a>(input: *mut u8) -> (ContractId, &'a [u8]) {
    let mut offset: usize = 0;

    let contract_id_len = 32;
    let contract_id_slice = { from_raw_parts(input.add(offset), contract_id_len) };
    offset += contract_id_len;

    let instruction_data_len = *(input.add(offset) as *const u64) as usize;
    offset += size_of::<u64>();
    let instruction_data = { from_raw_parts(input.add(offset), instruction_data_len) };

    let contract_id = ContractId::from_bytes(contract_id_slice.try_into().unwrap());
    // We unwrap here because if this panics, something's wrong in the runtime:
    let contract_id = contract_id.unwrap();

    (contract_id, instruction_data)
}