Contracts build risc-v fixtures (#2554)

Follow up from #2347 this time to verify that fixtures build to RISC-V

---------

Co-authored-by: alvicsam <alvicsam@gmail.com>
Co-authored-by: Alexander Samusev <41779041+alvicsam@users.noreply.github.com>
Co-authored-by: Alexander Theißen <alex.theissen@me.com>
This commit is contained in:
PG Herveou
2024-01-04 15:49:42 +01:00
committed by GitHub
parent 6f9b1f61ec
commit f82c297728
14 changed files with 647 additions and 150 deletions
@@ -1,3 +1,4 @@
#![allow(unused_variables, unused_mut)]
// Copyright (C) Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
@@ -12,3 +13,295 @@
// See the License for the specific language governing permissions and
// limitations under the License.
// TODO: bring up to date with wasm32.rs
use super::{CallFlags, HostFn, HostFnImpl, Result};
use crate::ReturnFlags;
/// A macro to implement all Host functions with a signature of `fn(&mut &mut [u8])`.
///
/// Example:
/// ```nocompile
// impl_wrapper_for! {
// () => [gas_left],
// (v1) => [gas_left],
// }
// ```
//
// Expands to:
// ```nocompile
// fn gas_left(output: &mut &mut [u8]) {
// unsafe { sys::gas_left(...); }
// }
// fn gas_left_v1(output: &mut &mut [u8]) {
// unsafe { sys::v1::gas_left(...); }
// }
// ```
macro_rules! impl_wrapper_for {
(@impl_fn $( $mod:ident )::*, $suffix_sep: literal, $suffix:tt, $name:ident) => {
paste::paste! {
fn [<$name $suffix_sep $suffix>](output: &mut &mut [u8]) {
todo!()
}
}
};
() => {};
(($mod:ident) => [$( $name:ident),*], $($tail:tt)*) => {
$(impl_wrapper_for!(@impl_fn sys::$mod, "_", $mod, $name);)*
impl_wrapper_for!($($tail)*);
};
(() => [$( $name:ident),*], $($tail:tt)*) => {
$(impl_wrapper_for!(@impl_fn sys, "", "", $name);)*
impl_wrapper_for!($($tail)*);
};
}
/// A macro to implement all the hash functions Apis.
macro_rules! impl_hash_fn {
( $name:ident, $bytes_result:literal ) => {
paste::item! {
fn [<hash_ $name>](input: &[u8], output: &mut [u8; $bytes_result]) {
todo!()
}
}
};
}
/// A macro to implement the get_storage functions.
macro_rules! impl_get_storage {
($fn_name:ident, $sys_get_storage:path) => {
fn $fn_name(key: &[u8], output: &mut &mut [u8]) -> Result {
todo!()
}
};
}
impl HostFn for HostFnImpl {
fn instantiate_v1(
code_hash: &[u8],
gas: u64,
value: &[u8],
input: &[u8],
mut address: Option<&mut [u8]>,
mut output: Option<&mut [u8]>,
salt: &[u8],
) -> Result {
todo!()
}
fn instantiate_v2(
code_hash: &[u8],
ref_time_limit: u64,
proof_size_limit: u64,
deposit: Option<&[u8]>,
value: &[u8],
input: &[u8],
mut address: Option<&mut [u8]>,
mut output: Option<&mut [u8]>,
salt: &[u8],
) -> Result {
todo!()
}
fn call(
callee: &[u8],
gas: u64,
value: &[u8],
input_data: &[u8],
mut output: Option<&mut [u8]>,
) -> Result {
todo!()
}
fn call_v1(
flags: CallFlags,
callee: &[u8],
gas: u64,
value: &[u8],
input_data: &[u8],
mut output: Option<&mut [u8]>,
) -> Result {
todo!()
}
fn call_v2(
flags: CallFlags,
callee: &[u8],
ref_time_limit: u64,
proof_time_limit: u64,
deposit: Option<&[u8]>,
value: &[u8],
input_data: &[u8],
mut output: Option<&mut [u8]>,
) -> Result {
todo!()
}
fn caller_is_root() -> u32 {
todo!()
}
fn delegate_call(
flags: CallFlags,
code_hash: &[u8],
input: &[u8],
mut output: Option<&mut [u8]>,
) -> Result {
todo!()
}
fn transfer(account_id: &[u8], value: &[u8]) -> Result {
todo!()
}
fn deposit_event(topics: &[u8], data: &[u8]) {
todo!()
}
fn set_storage(key: &[u8], value: &[u8]) {
todo!()
}
fn set_storage_v1(key: &[u8], encoded_value: &[u8]) -> Option<u32> {
todo!()
}
fn set_storage_v2(key: &[u8], encoded_value: &[u8]) -> Option<u32> {
todo!()
}
fn clear_storage(key: &[u8]) {
todo!()
}
fn clear_storage_v1(key: &[u8]) -> Option<u32> {
todo!()
}
impl_get_storage!(get_storage, sys::get_storage);
impl_get_storage!(get_storage_v1, sys::v1::get_storage);
fn take_storage(key: &[u8], output: &mut &mut [u8]) -> Result {
todo!()
}
fn contains_storage(key: &[u8]) -> Option<u32> {
todo!()
}
fn contains_storage_v1(key: &[u8]) -> Option<u32> {
todo!()
}
fn terminate(beneficiary: &[u8]) -> ! {
todo!()
}
fn terminate_v1(beneficiary: &[u8]) -> ! {
todo!()
}
fn call_chain_extension(func_id: u32, input: &[u8], output: Option<&mut [u8]>) -> u32 {
todo!()
}
fn input(output: &mut &mut [u8]) {
todo!()
}
fn return_value(flags: ReturnFlags, return_value: &[u8]) -> ! {
todo!()
}
fn call_runtime(call: &[u8]) -> Result {
todo!()
}
fn debug_message(str: &[u8]) -> Result {
todo!()
}
impl_wrapper_for! {
() => [caller, block_number, address, balance, gas_left, value_transferred, now, minimum_balance],
(v1) => [gas_left],
}
fn weight_to_fee(gas: u64, output: &mut &mut [u8]) {
todo!()
}
fn weight_to_fee_v1(ref_time_limit: u64, proof_size_limit: u64, output: &mut &mut [u8]) {
todo!()
}
impl_hash_fn!(sha2_256, 32);
impl_hash_fn!(keccak_256, 32);
impl_hash_fn!(blake2_256, 32);
impl_hash_fn!(blake2_128, 16);
fn ecdsa_recover(
signature: &[u8; 65],
message_hash: &[u8; 32],
output: &mut [u8; 33],
) -> Result {
todo!()
}
fn ecdsa_to_eth_address(pubkey: &[u8; 33], output: &mut [u8; 20]) -> Result {
todo!()
}
fn sr25519_verify(signature: &[u8; 64], message: &[u8], pub_key: &[u8; 32]) -> Result {
todo!()
}
fn is_contract(account_id: &[u8]) -> bool {
todo!()
}
fn caller_is_origin() -> bool {
todo!()
}
fn set_code_hash(code_hash: &[u8]) -> Result {
todo!()
}
fn code_hash(account_id: &[u8], output: &mut [u8]) -> Result {
todo!()
}
fn own_code_hash(output: &mut [u8]) {
todo!()
}
fn account_reentrance_count(account: &[u8]) -> u32 {
todo!()
}
fn add_delegate_dependency(code_hash: &[u8]) {
todo!()
}
fn remove_delegate_dependency(code_hash: &[u8]) {
todo!()
}
fn instantiation_nonce() -> u64 {
todo!()
}
fn reentrance_count() -> u32 {
todo!()
}
fn xcm_execute(msg: &[u8], output: &mut &mut [u8]) -> Result {
todo!()
}
fn xcm_send(dest: &[u8], msg: &[u8], output: &mut [u8; 32]) -> Result {
todo!()
}
}
@@ -69,6 +69,8 @@ mod sys {
pub fn contains_storage(key_ptr: *const u8, key_len: u32) -> ReturnCode;
pub fn debug_message(str_ptr: *const u8, str_len: u32) -> ReturnCode;
pub fn delegate_call(
flags: u32,
code_hash_ptr: *const u8,
@@ -97,7 +99,6 @@ mod sys {
pub fn get_storage(
key_ptr: *const u8,
key_len: u32,
out_ptr: *mut u8,
out_len_ptr: *mut u32,
) -> ReturnCode;
@@ -130,12 +131,7 @@ mod sys {
pub fn set_code_hash(code_hash_ptr: *const u8) -> ReturnCode;
pub fn set_storage(
key_ptr: *const u8,
key_len: u32,
value_ptr: *const u8,
value_len: u32,
) -> ReturnCode;
pub fn set_storage(key_ptr: *const u8, value_ptr: *const u8, value_len: u32);
pub fn sr25519_verify(
signature_ptr: *const u8,
@@ -219,7 +215,6 @@ mod sys {
pub fn set_storage(
key_ptr: *const u8,
key_len: u32,
value_ptr: *const u8,
value_len: u32,
) -> ReturnCode;
@@ -280,29 +275,47 @@ mod sys {
}
/// A macro to implement all Host functions with a signature of `fn(&mut &mut [u8])`.
///
/// Example:
/// ```nocompile
// impl_wrapper_for! {
// () => [gas_left],
// (v1) => [gas_left],
// }
// ```
//
// Expands to:
// ```nocompile
// fn gas_left(output: &mut &mut [u8]) {
// unsafe { sys::gas_left(...); }
// }
// fn gas_left_v1(output: &mut &mut [u8]) {
// unsafe { sys::v1::gas_left(...); }
// }
// ```
macro_rules! impl_wrapper_for {
(@impl_fn $( $mod:ident )::*, $suffix:literal, $name:ident) => {
paste::paste! {
fn [<$name $suffix>](output: &mut &mut [u8]) {
let mut output_len = output.len() as u32;
unsafe {
$( $mod )::*::$name(output.as_mut_ptr(), &mut output_len);
}
}
}
};
(@impl_fn $( $mod:ident )::*, $suffix_sep: literal, $suffix:tt, $name:ident) => {
paste::paste! {
fn [<$name $suffix_sep $suffix>](output: &mut &mut [u8]) {
let mut output_len = output.len() as u32;
unsafe {
$( $mod )::*::$name(output.as_mut_ptr(), &mut output_len);
}
}
}
};
() => {};
() => {};
(($mod:ident, $suffix:literal) => [$( $name:ident),*], $($tail:tt)*) => {
$(impl_wrapper_for!(@impl_fn sys::$mod, $suffix, $name);)*
impl_wrapper_for!($($tail)*);
};
(($mod:ident) => [$( $name:ident),*], $($tail:tt)*) => {
$(impl_wrapper_for!(@impl_fn sys::$mod, "_", $mod, $name);)*
impl_wrapper_for!($($tail)*);
};
(() => [$( $name:ident),*], $($tail:tt)*) => {
$(impl_wrapper_for!(@impl_fn sys, "", $name);)*
impl_wrapper_for!($($tail)*);
};
(() => [$( $name:ident),*], $($tail:tt)*) => {
$(impl_wrapper_for!(@impl_fn sys, "", "", $name);)*
impl_wrapper_for!($($tail)*);
};
}
/// A macro to implement all the hash functions Apis.
@@ -322,27 +335,6 @@ macro_rules! impl_hash_fn {
};
}
/// A macro to implement the get_storage functions.
macro_rules! impl_get_storage {
($fn_name:ident, $sys_get_storage:path) => {
fn $fn_name(key: &[u8], output: &mut &mut [u8]) -> Result {
let mut output_len = output.len() as u32;
let ret_code = {
unsafe {
$sys_get_storage(
key.as_ptr(),
key.len() as u32,
output.as_mut_ptr(),
&mut output_len,
)
}
};
extract_from_slice(output, output_len as usize);
ret_code.into()
}
};
}
impl HostFn for HostFnImpl {
fn instantiate_v1(
code_hash: &[u8],
@@ -579,19 +571,12 @@ impl HostFn for HostFnImpl {
}
fn set_storage(key: &[u8], value: &[u8]) {
unsafe {
sys::set_storage(key.as_ptr(), key.len() as u32, value.as_ptr(), value.len() as u32)
};
unsafe { sys::set_storage(key.as_ptr(), value.as_ptr(), value.len() as u32) };
}
fn set_storage_v1(key: &[u8], encoded_value: &[u8]) -> Option<u32> {
let ret_code = unsafe {
sys::v1::set_storage(
key.as_ptr(),
key.len() as u32,
encoded_value.as_ptr(),
encoded_value.len() as u32,
)
sys::v1::set_storage(key.as_ptr(), encoded_value.as_ptr(), encoded_value.len() as u32)
};
ret_code.into()
}
@@ -617,8 +602,29 @@ impl HostFn for HostFnImpl {
ret_code.into()
}
impl_get_storage!(get_storage, sys::get_storage);
impl_get_storage!(get_storage_v1, sys::v1::get_storage);
fn get_storage(key: &[u8], output: &mut &mut [u8]) -> Result {
let mut output_len = output.len() as u32;
let ret_code =
{ unsafe { sys::get_storage(key.as_ptr(), output.as_mut_ptr(), &mut output_len) } };
extract_from_slice(output, output_len as usize);
ret_code.into()
}
fn get_storage_v1(key: &[u8], output: &mut &mut [u8]) -> Result {
let mut output_len = output.len() as u32;
let ret_code = {
unsafe {
sys::v1::get_storage(
key.as_ptr(),
key.len() as u32,
output.as_mut_ptr(),
&mut output_len,
)
}
};
extract_from_slice(output, output_len as usize);
ret_code.into()
}
fn take_storage(key: &[u8], output: &mut &mut [u8]) -> Result {
let mut output_len = output.len() as u32;
@@ -636,6 +642,11 @@ impl HostFn for HostFnImpl {
ret_code.into()
}
fn debug_message(str: &[u8]) -> Result {
let ret_code = unsafe { sys::debug_message(str.as_ptr(), str.len() as u32) };
ret_code.into()
}
fn contains_storage(key: &[u8]) -> Option<u32> {
let ret_code = unsafe { sys::contains_storage(key.as_ptr(), key.len() as u32) };
ret_code.into()
@@ -654,20 +665,23 @@ impl HostFn for HostFnImpl {
unsafe { sys::v1::terminate(beneficiary.as_ptr()) }
}
fn call_chain_extension(func_id: u32, input: &[u8], output: &mut &mut [u8]) -> u32 {
let mut output_len = output.len() as u32;
fn call_chain_extension(func_id: u32, input: &[u8], mut output: Option<&mut [u8]>) -> u32 {
let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output);
let ret_code = {
unsafe {
sys::call_chain_extension(
func_id,
input.as_ptr(),
input.len() as u32,
output.as_mut_ptr(),
output_ptr,
&mut output_len,
)
}
};
extract_from_slice(output, output_len as usize);
if let Some(ref mut output) = output {
extract_from_slice(output, output_len as usize);
}
ret_code.into_u32()
}
@@ -690,7 +704,7 @@ impl HostFn for HostFnImpl {
impl_wrapper_for! {
() => [caller, block_number, address, balance, gas_left, value_transferred, now, minimum_balance],
(v1, "_v1") => [gas_left],
(v1) => [gas_left],
}
fn weight_to_fee(gas: u64, output: &mut &mut [u8]) {
@@ -802,7 +816,7 @@ impl HostFn for HostFnImpl {
ret_code.into()
}
fn xcm_send(dest: &[u8], msg: &[u8], output: &mut &mut [u8]) -> Result {
fn xcm_send(dest: &[u8], msg: &[u8], output: &mut [u8; 32]) -> Result {
let ret_code = unsafe {
sys::xcm_send(dest.as_ptr(), msg.as_ptr(), msg.len() as _, output.as_mut_ptr())
};