[contracts] define_env! re-write as a proc macro (#11888)

* define_env proc macro basics + can_satisfy part ready

* expand_impls part done

* fix of the &FunctionType bug

* pallet is compiled

* updated host fn definition syntax

* docs comments allowed to host fn definitions

* all 53 host funcs re-defined by the new macro

* unstable feat fix

* cleanup

* legacy mbe macros cleaned up

* Added Env ident to macro attribute; all tests pass!

* \#[v(..)] -> \#[version(..)]

* some tiny corrections

* save

* builds with non-magic rt; tests fail

* tests pass

* refactored errors + added docs

* merge err fixed

* fixes on @ascjones review, all except moving away from `pub mod env` syntax

* debug printing cleared

* clippy fix
This commit is contained in:
Sasha Gryaznov
2022-08-22 16:06:43 +03:00
committed by GitHub
parent c4e063ff02
commit c930cd0d79
5 changed files with 1545 additions and 1337 deletions
+19 -8
View File
@@ -517,6 +517,7 @@ mod tests {
schedule::Limits,
tests::{Test, ALICE},
};
use pallet_contracts_proc_macro::define_env;
use std::fmt;
impl fmt::Debug for PrefabWasmModule<Test> {
@@ -532,17 +533,27 @@ mod tests {
// Define test environment for tests. We need ImportSatisfyCheck
// implementation from it. So actual implementations doesn't matter.
define_env!(Test, <E: Ext>,
[seal0] panic(_ctx) => { unreachable!(); },
#[define_env]
pub mod test_env {
fn panic(_ctx: crate::wasm::Runtime<E>) -> Result<(), TrapReason> {
Ok(())
}
// gas is an implementation defined function and a contract can't import it.
[seal0] gas(_ctx, _amount: u32) => { unreachable!(); },
fn gas(_ctx: crate::wasm::Runtime<E>, _amount: u32) -> Result<(), TrapReason> {
Ok(())
}
[seal0] nop(_ctx, _unused: u64) => { unreachable!(); },
fn nop(_ctx: crate::wasm::Runtime<E>, _unused: u64) -> Result<(), TrapReason> {
Ok(())
}
// new version of nop with other data type for argument
[seal1] nop(_ctx, _unused: i32) => { unreachable!(); },
);
// new version of nop with other data type for argumebt
#[version(1)]
fn nop(_ctx: crate::wasm::Runtime<E>, _unused: i32) -> Result<(), TrapReason> {
Ok(())
}
}
}
macro_rules! prepare_test {
@@ -561,7 +572,7 @@ mod tests {
},
.. Default::default()
};
let r = do_preparation::<env::Test, Test>(wasm, &schedule, ALICE);
let r = do_preparation::<env::Env, Test>(wasm, &schedule, ALICE);
assert_matches::assert_matches!(r.map_err(|(_, msg)| msg), $($expected)*);
}
};