mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 09:57:56 +00:00
Contracts: Translate .wat fixtures to rust (#2654)
- Translate all pallet-contracts fixtures from `wat` to Rust files. - Fix read_sandbox_memory_as to not use MaxEncodedLen as this could break if used with types with a non-fixed encoded len. --------- 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> Co-authored-by: command-bot <>
This commit is contained in:
@@ -38,6 +38,7 @@ macro_rules! hash_fn {
|
||||
|
||||
// TODO remove cfg once used by all targets
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[inline(always)]
|
||||
fn extract_from_slice(output: &mut &mut [u8], new_len: usize) {
|
||||
debug_assert!(new_len <= output.len());
|
||||
let tmp = core::mem::take(output);
|
||||
@@ -45,7 +46,8 @@ fn extract_from_slice(output: &mut &mut [u8], new_len: usize) {
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
fn ptr_len_or_sentinel(data: &mut Option<&mut [u8]>) -> (*mut u8, u32) {
|
||||
#[inline(always)]
|
||||
fn ptr_len_or_sentinel(data: &mut Option<&mut &mut [u8]>) -> (*mut u8, u32) {
|
||||
match data {
|
||||
Some(ref mut data) => (data.as_mut_ptr(), data.len() as _),
|
||||
None => (crate::SENTINEL as _, 0),
|
||||
@@ -53,6 +55,7 @@ fn ptr_len_or_sentinel(data: &mut Option<&mut [u8]>) -> (*mut u8, u32) {
|
||||
}
|
||||
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[inline(always)]
|
||||
fn ptr_or_sentinel(data: &Option<&[u8]>) -> *const u8 {
|
||||
match data {
|
||||
Some(ref data) => data.as_ptr(),
|
||||
@@ -132,7 +135,7 @@ pub trait HostFn {
|
||||
gas: u64,
|
||||
value: &[u8],
|
||||
input_data: &[u8],
|
||||
output: Option<&mut [u8]>,
|
||||
output: Option<&mut &mut [u8]>,
|
||||
) -> Result;
|
||||
|
||||
/// Make a call to another contract.
|
||||
@@ -145,7 +148,7 @@ pub trait HostFn {
|
||||
gas: u64,
|
||||
value: &[u8],
|
||||
input_data: &[u8],
|
||||
output: Option<&mut [u8]>,
|
||||
output: Option<&mut &mut [u8]>,
|
||||
) -> Result;
|
||||
|
||||
/// Call (possibly transferring some amount of funds) into the specified account.
|
||||
@@ -186,7 +189,7 @@ pub trait HostFn {
|
||||
deposit: Option<&[u8]>,
|
||||
value: &[u8],
|
||||
input_data: &[u8],
|
||||
output: Option<&mut [u8]>,
|
||||
output: Option<&mut &mut [u8]>,
|
||||
) -> Result;
|
||||
|
||||
/// Call into the chain extension provided by the chain if any.
|
||||
@@ -211,7 +214,7 @@ pub trait HostFn {
|
||||
/// # Return
|
||||
///
|
||||
/// The chain extension returned value, if executed successfully.
|
||||
fn call_chain_extension(func_id: u32, input: &[u8], output: Option<&mut [u8]>) -> u32;
|
||||
fn call_chain_extension(func_id: u32, input: &[u8], output: Option<&mut &mut [u8]>) -> u32;
|
||||
|
||||
/// Call some dispatchable of the runtime.
|
||||
///
|
||||
@@ -374,7 +377,7 @@ pub trait HostFn {
|
||||
flags: CallFlags,
|
||||
code_hash: &[u8],
|
||||
input_data: &[u8],
|
||||
output: Option<&mut [u8]>,
|
||||
output: Option<&mut &mut [u8]>,
|
||||
) -> Result;
|
||||
|
||||
/// Deposit a contract event with the data buffer and optional list of topics. There is a limit
|
||||
@@ -485,8 +488,8 @@ pub trait HostFn {
|
||||
gas: u64,
|
||||
value: &[u8],
|
||||
input: &[u8],
|
||||
address: Option<&mut [u8]>,
|
||||
output: Option<&mut [u8]>,
|
||||
address: Option<&mut &mut [u8]>,
|
||||
output: Option<&mut &mut [u8]>,
|
||||
salt: &[u8],
|
||||
) -> Result;
|
||||
|
||||
@@ -534,8 +537,8 @@ pub trait HostFn {
|
||||
deposit: Option<&[u8]>,
|
||||
value: &[u8],
|
||||
input: &[u8],
|
||||
address: Option<&mut [u8]>,
|
||||
output: Option<&mut [u8]>,
|
||||
address: Option<&mut &mut [u8]>,
|
||||
output: Option<&mut &mut [u8]>,
|
||||
salt: &[u8],
|
||||
) -> Result;
|
||||
|
||||
|
||||
@@ -84,8 +84,8 @@ impl HostFn for HostFnImpl {
|
||||
gas: u64,
|
||||
value: &[u8],
|
||||
input: &[u8],
|
||||
mut address: Option<&mut [u8]>,
|
||||
mut output: Option<&mut [u8]>,
|
||||
mut address: Option<&mut &mut [u8]>,
|
||||
mut output: Option<&mut &mut [u8]>,
|
||||
salt: &[u8],
|
||||
) -> Result {
|
||||
todo!()
|
||||
@@ -98,8 +98,8 @@ impl HostFn for HostFnImpl {
|
||||
deposit: Option<&[u8]>,
|
||||
value: &[u8],
|
||||
input: &[u8],
|
||||
mut address: Option<&mut [u8]>,
|
||||
mut output: Option<&mut [u8]>,
|
||||
mut address: Option<&mut &mut [u8]>,
|
||||
mut output: Option<&mut &mut [u8]>,
|
||||
salt: &[u8],
|
||||
) -> Result {
|
||||
todo!()
|
||||
@@ -110,7 +110,7 @@ impl HostFn for HostFnImpl {
|
||||
gas: u64,
|
||||
value: &[u8],
|
||||
input_data: &[u8],
|
||||
mut output: Option<&mut [u8]>,
|
||||
mut output: Option<&mut &mut [u8]>,
|
||||
) -> Result {
|
||||
todo!()
|
||||
}
|
||||
@@ -121,7 +121,7 @@ impl HostFn for HostFnImpl {
|
||||
gas: u64,
|
||||
value: &[u8],
|
||||
input_data: &[u8],
|
||||
mut output: Option<&mut [u8]>,
|
||||
mut output: Option<&mut &mut [u8]>,
|
||||
) -> Result {
|
||||
todo!()
|
||||
}
|
||||
@@ -134,7 +134,7 @@ impl HostFn for HostFnImpl {
|
||||
deposit: Option<&[u8]>,
|
||||
value: &[u8],
|
||||
input_data: &[u8],
|
||||
mut output: Option<&mut [u8]>,
|
||||
mut output: Option<&mut &mut [u8]>,
|
||||
) -> Result {
|
||||
todo!()
|
||||
}
|
||||
@@ -147,7 +147,7 @@ impl HostFn for HostFnImpl {
|
||||
flags: CallFlags,
|
||||
code_hash: &[u8],
|
||||
input: &[u8],
|
||||
mut output: Option<&mut [u8]>,
|
||||
mut output: Option<&mut &mut [u8]>,
|
||||
) -> Result {
|
||||
todo!()
|
||||
}
|
||||
@@ -203,7 +203,7 @@ impl HostFn for HostFnImpl {
|
||||
todo!()
|
||||
}
|
||||
|
||||
fn call_chain_extension(func_id: u32, input: &[u8], output: Option<&mut [u8]>) -> u32 {
|
||||
fn call_chain_extension(func_id: u32, input: &[u8], output: Option<&mut &mut [u8]>) -> u32 {
|
||||
todo!()
|
||||
}
|
||||
|
||||
|
||||
@@ -336,13 +336,14 @@ macro_rules! impl_hash_fn {
|
||||
}
|
||||
|
||||
impl HostFn for HostFnImpl {
|
||||
#[inline(always)]
|
||||
fn instantiate_v1(
|
||||
code_hash: &[u8],
|
||||
gas: u64,
|
||||
value: &[u8],
|
||||
input: &[u8],
|
||||
mut address: Option<&mut [u8]>,
|
||||
mut output: Option<&mut [u8]>,
|
||||
mut address: Option<&mut &mut [u8]>,
|
||||
mut output: Option<&mut &mut [u8]>,
|
||||
salt: &[u8],
|
||||
) -> Result {
|
||||
let (address_ptr, mut address_len) = ptr_len_or_sentinel(&mut address);
|
||||
@@ -379,8 +380,8 @@ impl HostFn for HostFnImpl {
|
||||
deposit: Option<&[u8]>,
|
||||
value: &[u8],
|
||||
input: &[u8],
|
||||
mut address: Option<&mut [u8]>,
|
||||
mut output: Option<&mut [u8]>,
|
||||
mut address: Option<&mut &mut [u8]>,
|
||||
mut output: Option<&mut &mut [u8]>,
|
||||
salt: &[u8],
|
||||
) -> Result {
|
||||
let (address_ptr, mut address_len) = ptr_len_or_sentinel(&mut address);
|
||||
@@ -418,12 +419,13 @@ impl HostFn for HostFnImpl {
|
||||
ret_code.into()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn call(
|
||||
callee: &[u8],
|
||||
gas: u64,
|
||||
value: &[u8],
|
||||
input_data: &[u8],
|
||||
mut output: Option<&mut [u8]>,
|
||||
mut output: Option<&mut &mut [u8]>,
|
||||
) -> Result {
|
||||
let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output);
|
||||
let ret_code = {
|
||||
@@ -449,13 +451,14 @@ impl HostFn for HostFnImpl {
|
||||
ret_code.into()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn call_v1(
|
||||
flags: CallFlags,
|
||||
callee: &[u8],
|
||||
gas: u64,
|
||||
value: &[u8],
|
||||
input_data: &[u8],
|
||||
mut output: Option<&mut [u8]>,
|
||||
mut output: Option<&mut &mut [u8]>,
|
||||
) -> Result {
|
||||
let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output);
|
||||
let ret_code = {
|
||||
@@ -488,7 +491,7 @@ impl HostFn for HostFnImpl {
|
||||
deposit: Option<&[u8]>,
|
||||
value: &[u8],
|
||||
input_data: &[u8],
|
||||
mut output: Option<&mut [u8]>,
|
||||
mut output: Option<&mut &mut [u8]>,
|
||||
) -> Result {
|
||||
let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output);
|
||||
let deposit_ptr = ptr_or_sentinel(&deposit);
|
||||
@@ -520,11 +523,12 @@ impl HostFn for HostFnImpl {
|
||||
unsafe { sys::caller_is_root() }.into_u32()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn delegate_call(
|
||||
flags: CallFlags,
|
||||
code_hash: &[u8],
|
||||
input: &[u8],
|
||||
mut output: Option<&mut [u8]>,
|
||||
mut output: Option<&mut &mut [u8]>,
|
||||
) -> Result {
|
||||
let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output);
|
||||
let ret_code = {
|
||||
@@ -602,6 +606,7 @@ impl HostFn for HostFnImpl {
|
||||
ret_code.into()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn get_storage(key: &[u8], output: &mut &mut [u8]) -> Result {
|
||||
let mut output_len = output.len() as u32;
|
||||
let ret_code =
|
||||
@@ -610,6 +615,7 @@ impl HostFn for HostFnImpl {
|
||||
ret_code.into()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn get_storage_v1(key: &[u8], output: &mut &mut [u8]) -> Result {
|
||||
let mut output_len = output.len() as u32;
|
||||
let ret_code = {
|
||||
@@ -626,6 +632,7 @@ impl HostFn for HostFnImpl {
|
||||
ret_code.into()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn take_storage(key: &[u8], output: &mut &mut [u8]) -> Result {
|
||||
let mut output_len = output.len() as u32;
|
||||
let ret_code = {
|
||||
@@ -665,7 +672,7 @@ impl HostFn for HostFnImpl {
|
||||
unsafe { sys::v1::terminate(beneficiary.as_ptr()) }
|
||||
}
|
||||
|
||||
fn call_chain_extension(func_id: u32, input: &[u8], mut output: Option<&mut [u8]>) -> u32 {
|
||||
fn call_chain_extension(func_id: u32, input: &[u8], mut output: Option<&mut &mut [u8]>) -> u32 {
|
||||
let (output_ptr, mut output_len) = ptr_len_or_sentinel(&mut output);
|
||||
let ret_code = {
|
||||
unsafe {
|
||||
@@ -685,6 +692,7 @@ impl HostFn for HostFnImpl {
|
||||
ret_code.into_u32()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn input(output: &mut &mut [u8]) {
|
||||
let mut output_len = output.len() as u32;
|
||||
{
|
||||
@@ -707,6 +715,7 @@ impl HostFn for HostFnImpl {
|
||||
(v1) => [gas_left],
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn weight_to_fee(gas: u64, output: &mut &mut [u8]) {
|
||||
let mut output_len = output.len() as u32;
|
||||
{
|
||||
|
||||
@@ -36,7 +36,7 @@ macro_rules! define_error_codes {
|
||||
) => {
|
||||
/// Every error that can be returned to a contract when it calls any of the host functions.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
#[repr(u32)]
|
||||
#[repr(u8)]
|
||||
pub enum ReturnErrorCode {
|
||||
/// API call successful.
|
||||
Success = 0,
|
||||
|
||||
Reference in New Issue
Block a user