This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Rewrite define_env!
as a procedural macro
#11344
Labels
Z2-medium
Can be fixed by a coder with good Rust knowledge but little knowledge of the codebase.
Comments
athei
added
the
Z2-medium
Can be fixed by a coder with good Rust knowledge but little knowledge of the codebase.
label
May 3, 2022
So the new way we define the env (in #[define_env]
pub mod env {
#[host("seal0")]
fn gas(ctx: Runtime<E: Ext>, amount: u32) {
ctx.charge_gas(RuntimeCosts::MeteringBlock(amount))?;
Ok(())
}
#[host("seal1")]
fn seal_set_storage(ctx: Runtime<E: Ext>, key_ptr: u32, value_ptr: u32, value_len: u32) -> u32 {
ctx.set_storage(KeyType::Fix, key_ptr, value_ptr, value_len)
}
...
} @athei wdyt? |
Looks good in general. I have the following suggestions
#[define_env]
pub mod env {
// implicit version 0, no prefixing required as this function is only called "gas"
fn gas(ctx: Runtime<E: Ext>, amount: u32);
// implicit version 0, add alias "seal_set_storage"
#[prefixed_alias]
fn set_storage(ctx: Runtime<E: Ext>, key_ptr: u32, value_ptr: u32, value_len: u32);
// new version of function. also need alias
#[version(1)]
#[prefixed_alias]
fn set_storage(ctx: Runtime<E: Ext>, key_ptr: u32, value_ptr: u32, value_len: u32) -> u32 ;
// implicit version 0, no prefix required as newly added function
fn crazy_new_function();
} |
This one delayed for now for a follow-up PR. Applying all other suggestions, here is how environment definition will look like (in #[define_env(Env)]
pub mod env {
// will be expanded to the "seal0" module
fn gas(ctx: Runtime<E: Ext>, amount: u32) {
ctx.charge_gas(RuntimeCosts::MeteringBlock(amount))?;
Ok(())
}
// will be expanded to the "seal0" module
fn seal_set_storage(ctx: Runtime<E: Ext>, key_ptr: u32, value_ptr: u32, value_len: u32) {
ctx.set_storage(KeyType::Fix, key_ptr, value_ptr, value_len).map(|_| ())
}
// will be expanded to the "seal1" module
#[version(1)]
fn seal_set_storage(ctx: Runtime<E: Ext>, key_ptr: u32, value_ptr: u32, value_len: u32) -> u32 {
ctx.set_storage(KeyType::Fix, key_ptr, value_ptr, value_len)
}
// will be expanded to the "__unstable__" module
#[unstable]
fn seal_set_storage(
ctx: Runtime<E: Ext>,
key_ptr: u32,
key_len: u32,
value_ptr: u32,
value_len: u32,
) -> u32 {
ctx.set_storage(KeyType::Variable(key_len), key_ptr, value_ptr, value_len)
}
}
... |
Cool but let's not make one letter attributes :) : |
Ok. Also added attribute for the environment identifier, like |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Labels
Z2-medium
Can be fixed by a coder with good Rust knowledge but little knowledge of the codebase.
Currently, the contracts pallet uses the
define_env!
macro by example in order to define which host functions are available for contracts.This macro should be rewritten as a procedural attribute macro in order to achieve the following benefits:
There is already a
pallet-contracts-proc-macro
crate where this macro could live.The text was updated successfully, but these errors were encountered: