impls.rs 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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/>.

use crate::{
    env2::{
Robin Freyler's avatar
Robin Freyler committed
19 20 21 22 23
        call::{
            CallParams,
            CreateParams,
            ReturnType,
        },
24 25 26 27 28 29 30 31 32 33 34 35
        property,
        srml::ext,
        utils::{
            EnlargeTo,
            Reset,
        },
        Env,
        EnvTypes,
        Error,
        GetProperty,
        Result,
        SetProperty,
36
        Topics,
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
    },
    storage::Key,
};
use core::marker::PhantomData;
use scale::{
    Decode,
    Encode,
};

/// The SRML contract environment.
///
/// # Dev Notes
///
/// The implementation of environmental access routines have to be as efficient
/// as possible to not waste gas by any means. This has the effect that implementation
/// of some environmental access routines defined here are suboptimal in respect
/// to readability.
///
/// Any avoidable inefficiency is regarded by us as a bug.
/// If you spot such an inefficiency please report an issue at:
/// https://github.com/paritytech/ink/issues.
pub struct SrmlEnv<T> {
    marker: PhantomData<fn() -> T>,
}

62
#[cfg(feature = "ink-generate-abi")]
63
impl<E> type_metadata::HasTypeId for SrmlEnv<E>
64 65 66 67 68
where
    E: type_metadata::Metadata,
{
    fn type_id() -> type_metadata::TypeId {
        type_metadata::TypeIdCustom::new(
69
            "SrmlEnv",
70 71
            type_metadata::Namespace::from_module_path(module_path!())
                .expect("namespace from module path cannot fail"),
72
            vec![E::meta_type()],
73 74 75 76 77 78
        )
        .into()
    }
}

#[cfg(feature = "ink-generate-abi")]
79
impl<E> type_metadata::HasTypeDef for SrmlEnv<E> {
80 81 82 83 84
    fn type_def() -> type_metadata::TypeDef {
        type_metadata::TypeDefStruct::new(vec![]).into()
    }
}

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
impl<T> EnvTypes for SrmlEnv<T>
where
    T: EnvTypes,
{
    /// The type of an address.
    type AccountId = T::AccountId;
    /// The type of balances.
    type Balance = T::Balance;
    /// The type of hash.
    type Hash = T::Hash;
    /// The type of timestamps.
    type Moment = T::Moment;
    /// The type of block number.
    type BlockNumber = T::BlockNumber;
    /// The type of a call into the runtime
    type Call = T::Call;
}

impl<T> SrmlEnv<T>
where
    T: EnvTypes,
{
    fn get_property_impl<P, I>(buffer: &mut I, ext_fn: fn()) -> P::In
    where
        P: property::ReadProperty,
        I: AsMut<[u8]> + EnlargeTo,
    {
        ext_fn();
        let req_len = ext::scratch_size();
        buffer.enlarge_to(req_len);
        ext::scratch_read(&mut buffer.as_mut()[0..req_len], 0);
        Decode::decode(&mut &buffer.as_mut()[0..req_len])
            .expect("failed at decoding the property")
    }
}

macro_rules! impl_get_property_for {
    ( $( ($name:ident use $sys_fn:expr), )* ) => {
        $(
            impl<T> GetProperty<property::$name<Self>> for SrmlEnv<T>
            where
                T: EnvTypes,
            {
                fn get_property<I>(
                    buffer: &mut I,
                ) -> <property::$name<Self> as property::ReadProperty>::In
                where
                    I: AsMut<[u8]> + EnlargeTo,
                {
                    Self::get_property_impl::<property::$name::<T>, _>(buffer, $sys_fn)
                }
            }
        )*
    }
}

impl_get_property_for! {
    (Caller use ext::caller),
    (TransferredBalance use ext::value_transferred),
    (GasPrice use ext::gas_price),
    (GasLeft use ext::gas_left),
    (NowInMs use ext::now),
    (Address use ext::address),
    (Balance use ext::balance),
    (RentAllowance use ext::rent_allowance),
    (BlockNumber use ext::block_number),
    (MinimumBalance use ext::minimum_balance),
}

impl<T> GetProperty<property::Input<Self>> for SrmlEnv<T>
where
    T: EnvTypes,
{
    fn get_property<I>(
        buffer: &mut I,
    ) -> <property::Input<Self> as property::ReadProperty>::In
    where
        I: AsMut<[u8]> + EnlargeTo,
    {
        Self::get_property_impl::<property::Input<T>, _>(buffer, || ())
    }
}

impl<T> SetProperty<property::RentAllowance<Self>> for SrmlEnv<T>
where
    T: EnvTypes,
{
    fn set_property<O>(
        buffer: &mut O,
        value: &<property::RentAllowance<Self> as property::WriteProperty>::Out,
    ) where
        O: scale::Output + AsRef<[u8]> + Reset,
    {
        buffer.reset();
        value.encode_to(buffer);
        ext::set_rent_allowance(buffer.as_ref());
    }
}

184 185 186 187 188 189 190
impl<T> SrmlEnv<T>
where
    T: EnvTypes,
{
    /// Invokes the cross-contract call from the given parameters.
    ///
    /// Uses the given buffer as cache for intermediate data.
Robin Freyler's avatar
Robin Freyler committed
191 192 193 194
    fn invoke_contract_impl<O, Any>(
        buffer: &mut O,
        call_data: &CallParams<Self, Any>,
    ) -> Result<()>
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
    where
        O: scale::Output + AsRef<[u8]> + Reset,
    {
        // First we reset the buffer to start from a clean slate.
        buffer.reset();
        // Now we encode `call_data`, `endowment` and `input_data`
        // each after one another into our buffer and remember their
        // boundaries using the guards.
        call_data.callee().encode_to(buffer);
        let callee_guard = buffer.as_ref().len();
        call_data.endowment().encode_to(buffer);
        let endowment_guard = buffer.as_ref().len();
        call_data.input_data().encode_to(buffer);
        // We now use the guards in order to split the buffer into
        // some read-only slices that each store their respective
        // encoded value and call the actual routine.
        let callee = &buffer.as_ref()[0..callee_guard];
        let endowment = &buffer.as_ref()[callee_guard..endowment_guard];
        let gas_limit = call_data.gas_limit();
        let call_data = &buffer.as_ref()[endowment_guard..];
        // Do the actual contract call.
        let ret = ext::call(callee, gas_limit, endowment, call_data);
        if !ret.is_success() {
            // Maybe the called contract trapped.
            return Err(Error::InvalidContractCall)
        }
        Ok(())
    }
}

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
impl<T> Env for SrmlEnv<T>
where
    T: EnvTypes,
{
    fn get_contract_storage<I, R>(buffer: &mut I, key: Key) -> Result<R>
    where
        I: AsMut<[u8]> + EnlargeTo,
        R: scale::Decode,
    {
        let ret = ext::get_storage(key.as_bytes());
        if !ret.is_success() {
            return Err(Error::InvalidStorageKey)
        }
        let req_len = ext::scratch_size();
        buffer.enlarge_to(req_len);
        let ret = ext::scratch_read(&mut buffer.as_mut()[0..req_len], 0);
        if !ret.is_success() {
            return Err(Error::InvalidStorageRead)
        }
        Decode::decode(&mut &buffer.as_mut()[0..req_len]).map_err(Into::into)
    }

    fn set_contract_storage<O, V>(buffer: &mut O, key: Key, val: &V)
    where
        O: scale::Output + AsRef<[u8]> + Reset,
        V: scale::Encode,
    {
        buffer.reset();
        val.encode_to(buffer);
        ext::set_storage(key.as_bytes(), Some(buffer.as_ref()));
    }

    fn clear_contract_storage(key: Key) {
        ext::set_storage(key.as_bytes(), None);
    }

261
    fn invoke_contract<O>(buffer: &mut O, call_data: &CallParams<Self, ()>) -> Result<()>
262 263 264
    where
        O: scale::Output + AsRef<[u8]> + Reset,
    {
265
        Self::invoke_contract_impl(buffer, call_data)
266 267
    }

Robin Freyler's avatar
Robin Freyler committed
268 269 270 271
    fn eval_contract<IO, R>(
        buffer: &mut IO,
        call_data: &CallParams<Self, ReturnType<R>>,
    ) -> Result<R>
272 273 274 275
    where
        IO: scale::Output + AsRef<[u8]> + AsMut<[u8]> + EnlargeTo + Reset,
        R: scale::Decode,
    {
276
        Self::invoke_contract_impl(buffer, call_data)?;
277 278 279 280 281 282 283 284 285 286 287
        // At this point our call was successful and we can now fetch
        // the returned data and decode it for the result value.
        let req_len = ext::scratch_size();
        buffer.enlarge_to(req_len);
        let ret = ext::scratch_read(&mut buffer.as_mut()[0..req_len], 0);
        if !ret.is_success() {
            return Err(Error::InvalidContractCallReturn)
        }
        Decode::decode(&mut &buffer.as_ref()[0..req_len]).map_err(Into::into)
    }

Robin Freyler's avatar
Robin Freyler committed
288 289 290 291
    fn create_contract<IO, C>(
        buffer: &mut IO,
        create_data: &CreateParams<Self, C>,
    ) -> Result<Self::AccountId>
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
    where
        IO: scale::Output + AsRef<[u8]> + AsMut<[u8]> + EnlargeTo + Reset,
    {
        // First we reset the buffer to start from a clean slate.
        buffer.reset();
        // Now we encode `code_hash`, `endowment` and `input_data`
        // each after one another into our buffer and remember their
        // boundaries using the guards.
        create_data.code_hash().encode_to(buffer);
        let code_hash_guard = buffer.as_ref().len();
        create_data.endowment().encode_to(buffer);
        let endowment_guard = buffer.as_ref().len();
        create_data.input_data().encode_to(buffer);
        // We now use the guards in order to split the buffer into
        // some read-only slices that each store their respective
        // encoded value and call the actual routine.
        let code_hash = &buffer.as_ref()[0..code_hash_guard];
        let endowment = &buffer.as_ref()[code_hash_guard..endowment_guard];
        let gas_limit = create_data.gas_limit();
        let call_data = &buffer.as_ref()[endowment_guard..];
        // Do the actual contract instantiation.
        let ret = ext::create(code_hash, gas_limit, endowment, call_data);
        if !ret.is_success() {
            // Maybe the called contract trapped.
            return Err(Error::InvalidContractInstantiation)
        }
        // At this point our contract instantiation was successful
        // and we can now fetch the returned data and decode it for
        // the result value.
        let req_len = ext::scratch_size();
        buffer.enlarge_to(req_len);
323
        let ret = ext::scratch_read(&mut buffer.as_mut()[0..req_len], 0);
324 325 326 327 328 329
        if !ret.is_success() {
            return Err(Error::InvalidContractInstantiationReturn)
        }
        Decode::decode(&mut &buffer.as_ref()[0..req_len]).map_err(Into::into)
    }

330
    fn emit_event<O, Event>(buffer: &mut O, event: Event)
331
    where
332 333
        O: scale::Output + AsRef<[u8]> + Reset,
        Event: Topics<Self> + scale::Encode,
334 335 336 337 338 339
    {
        // First we reset the buffer to start from a clean slate.
        buffer.reset();
        // Now we encode `topics` and the raw encoded `data`
        // each after one another into our buffer and remember their
        // boundaries using guards respectively.
340
        event.topics().encode_to(buffer);
341
        let topics_guard = buffer.as_ref().len();
342
        event.encode_to(buffer);
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
        // We now use the guards in order to split the buffer into
        // some read-only slices that each store their respective
        // encoded value and call the actual routine.
        let topics = &buffer.as_ref()[0..topics_guard];
        let data = &buffer.as_ref()[topics_guard..];
        // Do the actual depositing of the event.
        ext::deposit_event(topics, data);
    }

    fn invoke_runtime<O, V>(buffer: &mut O, call_data: &V)
    where
        O: scale::Output + AsRef<[u8]> + Reset,
        V: scale::Encode,
    {
        buffer.reset();
        call_data.encode_to(buffer);
        ext::dispatch_call(buffer.as_ref());
    }

    fn restore_to<O>(
        buffer: &mut O,
        dest: Self::AccountId,
        code_hash: Self::Hash,
        rent_allowance: Self::Balance,
        filtered_keys: &[Key],
    ) where
        O: scale::Output + AsRef<[u8]> + Reset,
    {
        // First we reset the buffer to start from a clean slate.
        buffer.reset();
        // Now we encode `dest`, `code_hash` and `rent_allowance`
        // each after one another into our buffer and remember their
        // boundaries using guards respectively.
        dest.encode_to(buffer);
        let dest_guard = buffer.as_ref().len();
        code_hash.encode_to(buffer);
        let code_hash_guard = buffer.as_ref().len();
        rent_allowance.encode_to(buffer);
        // We now use the guards in order to split the buffer into
        // some read-only slices that each store their respective
        // encoded value and call the actual routine.
        let dest = &buffer.as_ref()[0..dest_guard];
        let code_hash = &buffer.as_ref()[dest_guard..code_hash_guard];
        let rent_allowance = &buffer.as_ref()[code_hash_guard..];
        // Perform the actual restoration process.
        ext::restore_to(dest, code_hash, rent_allowance, filtered_keys);
    }

    fn output<O, R>(buffer: &mut O, return_value: &R)
    where
        O: scale::Output + AsRef<[u8]> + Reset,
        R: scale::Encode,
    {
        buffer.reset();
        return_value.encode_to(buffer);
        ext::scratch_write(buffer.as_ref());
    }

    fn random<I>(buffer: &mut I, subject: &[u8]) -> Self::Hash
    where
        I: AsMut<[u8]> + EnlargeTo,
    {
        ext::random_seed(subject);
        let req_len = ext::scratch_size();
        buffer.enlarge_to(req_len);
        ext::scratch_read(&mut buffer.as_mut()[0..req_len], 0);
        Decode::decode(&mut &buffer.as_mut()[0..req_len])
            .expect("failed at decoding value returned by `ext_random_seed`")
    }

    fn println(content: &str) {
        ext::println(content)
    }
}