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
// Copyright (C) Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::ReturnFlags;
#[cfg(target_arch = "riscv64")]
mod riscv64;
/// Implements [`HostFn`] when compiled on supported architectures (RISC-V).
pub enum HostFnImpl {}
/// Defines all the host apis available to contracts.
pub trait HostFn: private::Sealed {
/// Stores the address of the current contract into the supplied buffer.
///
/// # Parameters
///
/// - `output`: A reference to the output data buffer to write the address.
fn address(output: &mut [u8; 20]);
/// Stores the U256 value at given `offset` from the input passed by the caller
/// into the supplied buffer.
///
/// # Note
/// - If `offset` is out of bounds, a value of zero will be returned.
/// - If `offset` is in bounds but there is not enough call data, the available data
/// is right-padded in order to fill a whole U256 value.
/// - The data written to `output` is a little endian U256 integer value.
///
/// # Parameters
///
/// - `output`: A reference to the fixed output data buffer to write the value.
/// - `offset`: The offset (index) into the call data.
fn call_data_load(output: &mut [u8; 32], offset: u32);
/// Returns the call data size.
fn call_data_size() -> u64;
/// Stores the input data passed by the caller into the supplied `output` buffer,
/// starting from the given input data `offset`.
///
/// The `output` buffer is guaranteed to always be fully populated:
/// - If the call data (starting from the given `offset`) is larger than the `output` buffer,
/// only what fits into the `output` buffer is written.
/// - If the `output` buffer size exceeds the call data size (starting from `offset`), remaining
/// bytes in the `output` buffer are zeroed out.
/// - If the provided call data `offset` is out-of-bounds, the whole `output` buffer is zeroed
/// out.
///
/// # Note
///
/// This function traps if:
/// - the input was previously forwarded by a [`call()`][`Self::call()`].
/// - the `output` buffer is located in an PolkaVM invalid memory range.
///
/// # Parameters
///
/// - `output`: A reference to the output data buffer to write the call data.
/// - `offset`: The offset index into the call data from where to start copying.
fn call_data_copy(output: &mut [u8], offset: u32);
/// Stores the address of the caller into the supplied buffer.
///
/// If this is a top-level call (i.e. initiated by an extrinsic) the origin address of the
/// extrinsic will be returned. Otherwise, if this call is initiated by another contract then
/// the address of the contract will be returned.
///
/// If there is no address associated with the caller (e.g. because the caller is root) then
/// it traps with `BadOrigin`.
///
/// # Parameters
///
/// - `output`: A reference to the output data buffer to write the caller address.
fn caller(output: &mut [u8; 20]);
/// Stores the origin address (initator of the call stack) into the supplied buffer.
///
/// If there is no address associated with the origin (e.g. because the origin is root) then
/// it traps with `BadOrigin`. This can only happen through on-chain governance actions or
/// customized runtimes.
///
/// # Parameters
///
/// - `output`: A reference to the output data buffer to write the origin's address.
fn origin(output: &mut [u8; 20]);
/// Deposit a contract event with the data buffer and optional list of topics. There is a limit
/// on the maximum number of topics specified by `event_topics`.
///
/// There should not be any duplicates in `topics`.
///
/// # Parameters
///
/// - `topics`: The topics list. It can't contain duplicates.
fn deposit_event(topics: &[[u8; 32]], data: &[u8]);
/// Cease contract execution and save a data buffer as a result of the execution.
///
/// This function never returns as it stops execution of the caller.
/// This is the only way to return a data buffer to the caller. Returning from
/// execution without calling this function is equivalent to calling:
/// ```nocompile
/// return_value(ReturnFlags::empty(), &[])
/// ```
///
/// Using an unnamed non empty `ReturnFlags` triggers a trap.
///
/// # Parameters
///
/// - `flags`: Flag used to signal special return conditions to the supervisor. See
/// [`ReturnFlags`] for a documentation of the supported flags.
/// - `return_value`: The return value buffer.
fn return_value(flags: ReturnFlags, return_value: &[u8]) -> !;
}
mod private {
pub trait Sealed {}
impl Sealed for super::HostFnImpl {}
}