Skip to content

Commit

Permalink
runtime-sdk: Implement automatic Module derivation
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Sep 13, 2023
1 parent 2eff201 commit 8ae5a57
Show file tree
Hide file tree
Showing 12 changed files with 104 additions and 67 deletions.
17 changes: 16 additions & 1 deletion runtime-sdk-macros/src/module_derive/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod method_handler;
mod migration_handler;
mod module;

use proc_macro2::TokenStream;
use quote::quote;
Expand All @@ -17,6 +18,7 @@ pub fn derive_module(impl_block: syn::ItemImpl) -> TokenStream {
let mut derivations: Vec<TokenStream> = Vec::new();

let mut derivers: Vec<Box<dyn Deriver>> = vec![
module::DeriveModule::new(),
migration_handler::DeriveMigrationHandler::new(),
method_handler::DeriveMethodHandler::new(),
];
Expand Down Expand Up @@ -376,9 +378,14 @@ mod tests {
}

#[test]
fn generate_migration_handler_impl() {
fn generate_module_impl() {
let input = syn::parse_quote!(
impl<C: Cfg> MyModule<C> {
const NAME: &'static str = MODULE_NAME;
const VERSION: u32 = 2;
type Error = Error;
type Event = ();
type Parameters = Parameters;
type Genesis = Genesis;

#[migration(init)]
Expand All @@ -404,6 +411,14 @@ mod tests {
const _: () = {
#uses
#[automatically_derived]
impl<C: Cfg> sdk::module::Module for MyModule<C> {
const NAME: &'static str = MODULE_NAME;
const VERSION: u32 = 2;
type Error = Error;
type Event = ();
type Parameters = Parameters;
}
#[automatically_derived]
impl<C: Cfg> sdk::module::MigrationHandler for MyModule<C> {
type Genesis = Genesis;
fn init_or_migrate<C: Context>(
Expand Down
54 changes: 54 additions & 0 deletions runtime-sdk-macros/src/module_derive/module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use proc_macro2::TokenStream;
use quote::quote;

/// Deriver for the `Module` trait.
pub struct DeriveModule {
/// Items specifying the module configuration.
module_cfg: Vec<syn::ImplItem>,
}

impl DeriveModule {
pub fn new() -> Box<Self> {
Box::new(Self { module_cfg: vec![] })
}
}

impl super::Deriver for DeriveModule {
fn preprocess(&mut self, item: syn::ImplItem) -> Option<syn::ImplItem> {
match item {
syn::ImplItem::Type(ref ty) => {
match ty.ident.to_string().as_str() {
"Error" | "Event" | "Parameters" => {
self.module_cfg.push(item);
None // Take the item.
}
_ => Some(item), // Return the item.
}
}
syn::ImplItem::Const(ref cnst) => {
match cnst.ident.to_string().as_str() {
"NAME" | "VERSION" => {
self.module_cfg.push(item);
None // Take the item.
}
_ => Some(item), // Return the item.
}
}
_ => Some(item), // Return the item.
}
}

fn derive(&mut self, generics: &syn::Generics, ty: &Box<syn::Type>) -> TokenStream {
if self.module_cfg.is_empty() {
return quote! {};
}
let module_cfg = &self.module_cfg;

quote! {
#[automatically_derived]
impl #generics sdk::module::Module for #ty {
#(#module_cfg)*
}
}
}
}
11 changes: 4 additions & 7 deletions runtime-sdk/modules/contracts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,10 @@ impl<Cfg: Config> Module<Cfg> {

#[sdk_derive(Module)]
impl<Cfg: Config> Module<Cfg> {
const NAME: &'static str = MODULE_NAME;
type Error = Error;
type Event = Event;
type Parameters = Parameters;
type Genesis = Genesis;

#[migration(init)]
Expand Down Expand Up @@ -864,13 +868,6 @@ impl<Cfg: Config> Module<Cfg> {
}
}

impl<Cfg: Config> module::Module for Module<Cfg> {
const NAME: &'static str = MODULE_NAME;
type Error = Error;
type Event = Event;
type Parameters = Parameters;
}

impl<Cfg: Config> module::TransactionHandler for Module<Cfg> {}
impl<Cfg: Config> module::BlockHandler for Module<Cfg> {}
impl<Cfg: Config> module::InvariantHandler for Module<Cfg> {}
11 changes: 4 additions & 7 deletions runtime-sdk/modules/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,6 @@ pub enum Event {
},
}

impl<Cfg: Config> module::Module for Module<Cfg> {
const NAME: &'static str = MODULE_NAME;
type Error = Error;
type Event = Event;
type Parameters = Parameters;
}

/// Interface that can be called from other modules.
pub trait API {
/// Perform an Ethereum CREATE transaction.
Expand Down Expand Up @@ -660,6 +653,10 @@ impl<Cfg: Config> Module<Cfg> {

#[sdk_derive(Module)]
impl<Cfg: Config> Module<Cfg> {
const NAME: &'static str = MODULE_NAME;
type Error = Error;
type Event = Event;
type Parameters = Parameters;
type Genesis = Genesis;

#[migration(init)]
Expand Down
7 changes: 2 additions & 5 deletions runtime-sdk/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -959,16 +959,13 @@ mod test {
/// A module with multiple no-op methods; intended for testing routing.
struct AlphabetModule;

impl module::Module for AlphabetModule {
#[sdk_derive(Module)]
impl AlphabetModule {
const NAME: &'static str = "alphabet";
const VERSION: u32 = 42;
type Error = AlphabetError;
type Event = ();
type Parameters = ();
}

#[sdk_derive(Module)]
impl AlphabetModule {
type Genesis = ();

#[handler(call = "alphabet.ReadOnly")]
Expand Down
11 changes: 4 additions & 7 deletions runtime-sdk/src/modules/accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,10 @@ impl API for Module {

#[sdk_derive(Module)]
impl Module {
const NAME: &'static str = MODULE_NAME;
type Error = Error;
type Event = Event;
type Parameters = Parameters;
type Genesis = Genesis;

#[migration(init)]
Expand Down Expand Up @@ -866,13 +870,6 @@ impl Module {
}
}

impl module::Module for Module {
const NAME: &'static str = MODULE_NAME;
type Error = Error;
type Event = Event;
type Parameters = Parameters;
}

impl module::TransactionHandler for Module {
fn authenticate_tx<C: Context>(
ctx: &mut C,
Expand Down
7 changes: 2 additions & 5 deletions runtime-sdk/src/modules/accounts/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,12 @@ impl Runtime for TestRuntime {
/// A module with multiple no-op methods; intended for testing routing.
struct TestModule;

impl module::Module for TestModule {
#[sdk_derive(Module)]
impl TestModule {
const NAME: &'static str = "test";
type Error = CoreError;
type Event = ();
type Parameters = ();
}

#[sdk_derive(Module)]
impl TestModule {
type Genesis = ();

#[handler(call = "test.RefundFee")]
Expand Down
13 changes: 5 additions & 8 deletions runtime-sdk/src/modules/consensus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,11 @@ impl Module {

#[sdk_derive(Module)]
impl Module {
const NAME: &'static str = MODULE_NAME;
const VERSION: u32 = 1;
type Error = Error;
type Event = Event;
type Parameters = Parameters;
type Genesis = Genesis;

#[migration(init)]
Expand Down Expand Up @@ -400,14 +405,6 @@ impl API for Module {
}
}

impl module::Module for Module {
const NAME: &'static str = MODULE_NAME;
const VERSION: u32 = 1;
type Error = Error;
type Event = Event;
type Parameters = Parameters;
}

impl module::TransactionHandler for Module {}

impl module::BlockHandler for Module {}
Expand Down
15 changes: 5 additions & 10 deletions runtime-sdk/src/modules/consensus_accounts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,11 @@ impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API> API
impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API>
Module<Accounts, Consensus>
{
const NAME: &'static str = MODULE_NAME;
const VERSION: u32 = 1;
type Error = Error;
type Event = Event;
type Parameters = Parameters;
type Genesis = Genesis;

#[migration(init)]
Expand Down Expand Up @@ -694,16 +699,6 @@ impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API>
}
}

impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API> module::Module
for Module<Accounts, Consensus>
{
const NAME: &'static str = MODULE_NAME;
const VERSION: u32 = 1;
type Error = Error;
type Event = Event;
type Parameters = Parameters;
}

impl<Accounts: modules::accounts::API, Consensus: modules::consensus::API>
module::TransactionHandler for Module<Accounts, Consensus>
{
Expand Down
11 changes: 4 additions & 7 deletions runtime-sdk/src/modules/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,10 @@ impl<Cfg: Config> API for Module<Cfg> {

#[sdk_derive(Module)]
impl<Cfg: Config> Module<Cfg> {
const NAME: &'static str = MODULE_NAME;
type Error = Error;
type Event = Event;
type Parameters = Parameters;
type Genesis = Genesis;

#[migration(init)]
Expand Down Expand Up @@ -910,13 +914,6 @@ impl<Cfg: Config> Module<Cfg> {
}
}

impl<Cfg: Config> module::Module for Module<Cfg> {
const NAME: &'static str = MODULE_NAME;
type Error = Error;
type Event = Event;
type Parameters = Parameters;
}

impl<Cfg: Config> module::TransactionHandler for Module<Cfg> {
fn approve_raw_tx<C: Context>(_ctx: &mut C, tx: &[u8]) -> Result<(), Error> {
let params = Self::params();
Expand Down
7 changes: 2 additions & 5 deletions runtime-sdk/src/modules/core/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,13 @@ impl GasWasterModule {
const METHOD_SPECIFIC_GAS_REQUIRED_HUGE: &'static str = "test.SpecificGasRequiredHuge";
}

impl module::Module for GasWasterModule {
#[sdk_derive(Module)]
impl GasWasterModule {
const NAME: &'static str = "gaswaster";
const VERSION: u32 = 42;
type Error = crate::modules::core::Error;
type Event = ();
type Parameters = ();
}

#[sdk_derive(Module)]
impl GasWasterModule {
type Genesis = ();

#[handler(call = Self::METHOD_WASTE_GAS)]
Expand Down
7 changes: 2 additions & 5 deletions runtime-sdk/src/modules/rewards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,13 @@ pub struct Module<Accounts: modules::accounts::API> {
pub static ADDRESS_REWARD_POOL: Lazy<Address> =
Lazy::new(|| Address::from_module(MODULE_NAME, "reward-pool"));

impl<Accounts: modules::accounts::API> module::Module for Module<Accounts> {
#[sdk_derive(Module)]
impl<Accounts: modules::accounts::API> Module<Accounts> {
const NAME: &'static str = MODULE_NAME;
const VERSION: u32 = 2;
type Error = Error;
type Event = ();
type Parameters = Parameters;
}

#[sdk_derive(Module)]
impl<Accounts: modules::accounts::API> Module<Accounts> {
type Genesis = Genesis;

#[migration(init)]
Expand Down

0 comments on commit 8ae5a57

Please sign in to comment.