ext.rs 6.37 KB
Newer Older
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
// Copyright 2018-2019 Parity Technologies (UK) Ltd.
// This file is part of ink!.
//
// ink! is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ink! 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with ink!.  If not, see <http://www.gnu.org/licenses/>.

//! External C API to communicate with substrate contracts runtime module.
//!
//! Refer to substrate SRML contract module for more documentation.

use crate::{
    env2::srml::RetCode,
    storage::Key,
};

pub fn create(
    code_hash: &[u8],
    gas_limit: u64,
    value: &[u8],
    create_data: &[u8],
) -> RetCode {
    unsafe {
33
        sys::ext_instantiate(
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
            code_hash.as_ptr() as u32,
            code_hash.len() as u32,
            gas_limit,
            value.as_ptr() as u32,
            value.len() as u32,
            create_data.as_ptr() as u32,
            create_data.len() as u32,
        )
    }
    .into()
}

pub fn call(callee: &[u8], gas_limit: u64, value: &[u8], call_data: &[u8]) -> RetCode {
    unsafe {
        sys::ext_call(
            callee.as_ptr() as u32,
            callee.len() as u32,
            gas_limit,
            value.as_ptr() as u32,
            value.len() as u32,
            call_data.as_ptr() as u32,
            call_data.len() as u32,
        )
    }
    .into()
}

pub fn deposit_event(topics: &[u8], data: &[u8]) {
    unsafe {
        sys::ext_deposit_event(
            topics.as_ptr() as u32,
            topics.len() as u32,
            data.as_ptr() as u32,
            data.len() as u32,
        )
    }
}

pub fn set_storage(key: &[u8], value: Option<&[u8]>) {
    match value {
        Some(value) => unsafe {
            sys::ext_set_storage(
                key.as_ptr() as u32,
                1,
                value.as_ptr() as u32,
                value.len() as u32,
            )
        },
        None => unsafe { sys::ext_set_storage(key.as_ptr() as u32, 0, 0, 0) },
    }
}

pub fn get_storage(key: &[u8]) -> RetCode {
    unsafe { sys::ext_get_storage(key.as_ptr() as u32) }.into()
}

pub fn restore_to(
    dest: &[u8],
    code_hash: &[u8],
    rent_allowance: &[u8],
    filtered_keys: &[Key],
) {
    unsafe {
        sys::ext_restore_to(
            dest.as_ptr() as u32,
            dest.len() as u32,
            code_hash.as_ptr() as u32,
            code_hash.len() as u32,
            rent_allowance.as_ptr() as u32,
            rent_allowance.len() as u32,
            filtered_keys.as_ptr() as u32,
            filtered_keys.len() as u32,
        )
    }
}

pub fn dispatch_call(call: &[u8]) {
    unsafe { sys::ext_dispatch_call(call.as_ptr() as u32, call.len() as u32) }
}

pub fn scratch_size() -> usize {
    (unsafe { sys::ext_scratch_size() }) as usize
}

pub fn scratch_read(dest: &mut [u8], offset: u32) -> RetCode {
    unsafe { sys::ext_scratch_read(dest.as_mut_ptr() as u32, offset, dest.len() as u32) };
    RetCode::success()
}

pub fn scratch_write(src: &[u8]) -> RetCode {
    unsafe { sys::ext_scratch_write(src.as_ptr() as u32, src.len() as u32) };
    RetCode::success()
}

macro_rules! impl_ext_wrapper_for {
    ( $( ($name:ident => $ext_name:ident), )* ) => {
        $(
            pub fn $name() {
                unsafe {
                    sys::$ext_name()
                }
            }
        )*
    }
}
impl_ext_wrapper_for! {
    (caller => ext_caller),
    (block_number => ext_block_number),
    (address => ext_address),
    (balance => ext_balance),
    (gas_price => ext_gas_price),
    (gas_left => ext_gas_left),
    (value_transferred => ext_value_transferred),
    (now => ext_now),
    (rent_allowance => ext_rent_allowance),
    (minimum_balance => ext_minimum_balance),
}

pub fn set_rent_allowance(value: &[u8]) -> RetCode {
    unsafe { sys::ext_set_rent_allowance(value.as_ptr() as u32, value.len() as u32) };
    RetCode::success()
}

pub fn random_seed(subject: &[u8]) {
    unsafe { sys::ext_random_seed(subject.as_ptr() as u32, subject.len() as u32) }
}

pub fn println(content: &str) {
    let bytes = content.as_bytes();
    unsafe { sys::ext_println(bytes.as_ptr() as u32, bytes.len() as u32) }
}

mod sys {
    extern "C" {
168
        pub fn ext_instantiate(
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
            init_code_ptr: u32,
            init_code_len: u32,
            gas: u64,
            value_ptr: u32,
            value_len: u32,
            input_data_ptr: u32,
            input_data_len: u32,
        ) -> u32;

        pub fn ext_call(
            callee_ptr: u32,
            callee_len: u32,
            gas: u64,
            value_ptr: u32,
            value_len: u32,
            input_data_ptr: u32,
            input_data_len: u32,
        ) -> u32;

        pub fn ext_deposit_event(
            topics_ptr: u32,
            topics_len: u32,
            data_ptr: u32,
            data_len: u32,
        );

        pub fn ext_set_storage(
            key_ptr: u32,
            value_non_null: u32,
            value_ptr: u32,
            value_len: u32,
        );
        pub fn ext_get_storage(key_ptr: u32) -> u32;

        pub fn ext_restore_to(
            dest_ptr: u32,
            dest_len: u32,
            code_hash_ptr: u32,
            code_hash_len: u32,
            rent_allowance_ptr: u32,
            rent_allowance_len: u32,
            delta_ptr: u32,
            delta_count: u32,
        );

        pub fn ext_dispatch_call(call_ptr: u32, call_len: u32);

        pub fn ext_scratch_size() -> u32;
        pub fn ext_scratch_read(dst_ptr: u32, offset: u32, len: u32);
        pub fn ext_scratch_write(src_ptr: u32, len: u32);

        pub fn ext_caller();
        pub fn ext_block_number();
        pub fn ext_address();
        pub fn ext_balance();
        pub fn ext_gas_price();
        pub fn ext_gas_left();
        pub fn ext_value_transferred();
        pub fn ext_now();
        pub fn ext_rent_allowance();
        pub fn ext_minimum_balance();

        pub fn ext_set_rent_allowance(value_ptr: u32, value_len: u32);

        pub fn ext_random_seed(subject_ptr: u32, subject_len: u32);
        pub fn ext_println(str_ptr: u32, str_len: u32);
    }
}