Expose block number in seal_random (#8329)

* Allow contract callable functions to specify the module

* Add new version of `seal_random`

* Fix overlong lines

* Fix benchmarking code

* Update README.md

* Replace Module by Pallet
This commit is contained in:
Alexander Theißen
2021-03-23 14:48:36 +01:00
committed by GitHub
parent f9b6c869a3
commit 1f911ddb61
7 changed files with 213 additions and 95 deletions
@@ -43,21 +43,23 @@ macro_rules! gen_signature {
macro_rules! gen_signature_dispatch {
(
$needle_module:ident,
$needle_name:ident,
$needle_sig:ident ;
$module:ident,
$name:ident
( $ctx:ident $( , $names:ident : $params:ty )* ) $( -> $returns:ty )* , $($rest:tt)* ) => {
if stringify!($name).as_bytes() == $needle_name {
( $ctx:ident $( , $names:ident : $params:ty )* ) $( -> $returns:ty )* , $($rest:tt)*
) => {
if stringify!($module).as_bytes() == $needle_module && stringify!($name).as_bytes() == $needle_name {
let signature = gen_signature!( ( $( $params ),* ) $( -> $returns )* );
if $needle_sig == &signature {
return true;
}
} else {
gen_signature_dispatch!($needle_name, $needle_sig ; $($rest)*);
gen_signature_dispatch!($needle_module, $needle_name, $needle_sig ; $($rest)*);
}
};
( $needle_name:ident, $needle_sig:ident ; ) => {
};
( $needle_module:ident, $needle_name:ident, $needle_sig:ident ; ) => {};
}
/// Unmarshall arguments and then execute `body` expression and return its result.
@@ -151,10 +153,11 @@ macro_rules! register_func {
( $reg_cb:ident, < E: $seal_ty:tt > ; ) => {};
( $reg_cb:ident, < E: $seal_ty:tt > ;
$name:ident ( $ctx:ident $( , $names:ident : $params:ty )* )
$module:ident $name:ident ( $ctx:ident $( , $names:ident : $params:ty )* )
$( -> $returns:ty )* => $body:tt $($rest:tt)*
) => {
$reg_cb(
stringify!($module).as_bytes(),
stringify!($name).as_bytes(),
{
define_func!(
@@ -176,14 +179,17 @@ macro_rules! register_func {
/// and reject the code if any imported function has a mismatched signature.
macro_rules! define_env {
( $init_name:ident , < E: $seal_ty:tt > ,
$( $name:ident ( $ctx:ident $( , $names:ident : $params:ty )* )
$( [$module:ident] $name:ident ( $ctx:ident $( , $names:ident : $params:ty )* )
$( -> $returns:ty )* => $body:tt , )*
) => {
pub struct $init_name;
impl $crate::wasm::env_def::ImportSatisfyCheck for $init_name {
fn can_satisfy(name: &[u8], func_type: &parity_wasm::elements::FunctionType) -> bool {
gen_signature_dispatch!( name, func_type ; $( $name ( $ctx $(, $names : $params )* ) $( -> $returns )* , )* );
fn can_satisfy(module: &[u8], name: &[u8], func_type: &parity_wasm::elements::FunctionType) -> bool {
gen_signature_dispatch!(
module, name, func_type ;
$( $module, $name ( $ctx $(, $names : $params )* ) $( -> $returns )* , )*
);
return false;
}
@@ -195,8 +201,12 @@ macro_rules! define_env {
sp_core::crypto::UncheckedFrom<<E::T as frame_system::Config>::Hash> +
AsRef<[u8]>
{
fn impls<F: FnMut(&[u8], $crate::wasm::env_def::HostFunc<E>)>(f: &mut F) {
register_func!(f, < E: $seal_ty > ; $( $name ( $ctx $( , $names : $params )* ) $( -> $returns)* => $body )* );
fn impls<F: FnMut(&[u8], &[u8], $crate::wasm::env_def::HostFunc<E>)>(f: &mut F) {
register_func!(
f,
< E: $seal_ty > ;
$( $module $name ( $ctx $( , $names : $params )* ) $( -> $returns)* => $body )*
);
}
}
};
@@ -327,7 +337,7 @@ mod tests {
use crate::wasm::env_def::ImportSatisfyCheck;
define_env!(Env, <E: Ext>,
seal_gas( _ctx, amount: u32 ) => {
[seal0] seal_gas( _ctx, amount: u32 ) => {
let amount = Weight::from(amount);
if !amount.is_zero() {
Ok(())
@@ -337,7 +347,11 @@ mod tests {
},
);
assert!(Env::can_satisfy(b"seal_gas", &FunctionType::new(vec![ValueType::I32], None)));
assert!(!Env::can_satisfy(b"not_exists", &FunctionType::new(vec![], None)));
assert!(
Env::can_satisfy(b"seal0", b"seal_gas",&FunctionType::new(vec![ValueType::I32], None))
);
assert!(
!Env::can_satisfy(b"seal0", b"not_exists", &FunctionType::new(vec![], None))
);
}
}
@@ -74,7 +74,7 @@ pub type HostFunc<E> =
) -> Result<sp_sandbox::ReturnValue, sp_sandbox::HostError>;
pub trait FunctionImplProvider<E: Ext> {
fn impls<F: FnMut(&[u8], HostFunc<E>)>(f: &mut F);
fn impls<F: FnMut(&[u8], &[u8], HostFunc<E>)>(f: &mut F);
}
/// This trait can be used to check whether the host environment can satisfy
@@ -83,5 +83,5 @@ pub trait ImportSatisfyCheck {
/// Returns `true` if the host environment contains a function with
/// the specified name and its type matches to the given type, or `false`
/// otherwise.
fn can_satisfy(name: &[u8], func_type: &FunctionType) -> bool;
fn can_satisfy(module: &[u8], name: &[u8], func_type: &FunctionType) -> bool;
}