From 9c4a31c8ee2b271242e8b0583937530526f1bd5f Mon Sep 17 00:00:00 2001 From: Nimish Agrawal Date: Thu, 26 Oct 2023 23:49:02 +0530 Subject: [PATCH 1/2] refactor: move util_macros into its own repo - zink --- util_macros/Cargo.toml | 16 ------- util_macros/src/lib.rs | 80 -------------------------------- util_macros/src/ownable2step.rs | 51 -------------------- util_macros/src/parser.rs | 33 ------------- util_macros/src/upgradability.rs | 19 -------- 5 files changed, 199 deletions(-) delete mode 100644 util_macros/Cargo.toml delete mode 100644 util_macros/src/lib.rs delete mode 100644 util_macros/src/ownable2step.rs delete mode 100644 util_macros/src/parser.rs delete mode 100644 util_macros/src/upgradability.rs diff --git a/util_macros/Cargo.toml b/util_macros/Cargo.toml deleted file mode 100644 index a0b6b4d..0000000 --- a/util_macros/Cargo.toml +++ /dev/null @@ -1,16 +0,0 @@ -[package] -name = "util_macros" -version = "0.1.0" -authors = ["AZERO.ID "] -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -syn = { version = "2", features = ["full", "parsing", "extra-traits"] } -quote = "1" -proc-macro2 = "1" - -[lib] -path = "src/lib.rs" -proc-macro = true diff --git a/util_macros/src/lib.rs b/util_macros/src/lib.rs deleted file mode 100644 index ad5938d..0000000 --- a/util_macros/src/lib.rs +++ /dev/null @@ -1,80 +0,0 @@ -extern crate proc_macro; - -mod ownable2step; -mod parser; -mod upgradability; - -use crate::parser::AznsArgs; -use proc_macro::TokenStream; -use quote::ToTokens; -use syn::{parse_macro_input, Item, ItemMod, ItemStruct}; - -fn get_module_items(module: &mut ItemMod) -> &mut Vec { - &mut module - .content - .as_mut() - .expect("Expected contract module. Found mod declaration") - .1 -} - -fn get_storage_struct(items: &Vec) -> ItemStruct { - let is_storage_struct = |item: &ItemStruct| -> bool { - item.attrs - .iter() - .any(|attr| match attr.path().is_ident("ink") { - false => false, - true => attr - .parse_nested_meta(|meta| match meta.path.is_ident("storage") { - true => Ok(()), - false => Err(meta.error("not storage struct")), - }) - .is_ok(), - }) - }; - - items - .iter() - .find_map(|item| match item { - syn::Item::Struct(item_struct) if is_storage_struct(&item_struct) => Some(item_struct), - _ => None, - }) - .expect("Could not find struct marked with #[ink(storage)]") - .clone() -} - -fn support_upgrade(items: &mut Vec, storage_name: &syn::Ident) { - let code = upgradability::upgradability_impl(storage_name); - let item = syn::parse(code).unwrap(); - items.push(item); -} - -fn support_ownable2step( - items: &mut Vec, - storage_name: &syn::Ident, - err_name: &syn::Ident, - err_val: &syn::Expr, -) { - let code = ownable2step::ownable2step_impl(storage_name, err_name, err_val); - let item = syn::parse(code).unwrap(); - items.push(item); -} - -#[proc_macro_attribute] -pub fn azns_contract(args: TokenStream, input: TokenStream) -> TokenStream { - let args = parse_macro_input!(args as AznsArgs); - let mut module = parse_macro_input!(input as ItemMod); - - let mut module_items = get_module_items(&mut module); - let storage_name = get_storage_struct(&module_items).ident; - - if let Some(data) = args.ownable2step { - let (err_name, err_val) = data; - // verify admin, pending_admin fields ?? - support_ownable2step(&mut module_items, &storage_name, &err_name, &err_val); - } - if args.upgradable { - support_upgrade(&mut module_items, &storage_name); - } - - module.into_token_stream().into() -} diff --git a/util_macros/src/ownable2step.rs b/util_macros/src/ownable2step.rs deleted file mode 100644 index 0c1d3b5..0000000 --- a/util_macros/src/ownable2step.rs +++ /dev/null @@ -1,51 +0,0 @@ -pub fn ownable2step_impl( - struct_name: &syn::Ident, - err_name: &syn::Ident, - err_val: &syn::Expr, -) -> proc_macro::TokenStream { - quote::quote!( - impl #struct_name { - #[ink(message)] - pub fn get_admin(&self) -> AccountId { - self.admin - } - - #[ink(message)] - pub fn get_pending_admin(&self) -> Option { - self.pending_admin - } - - #[ink(message)] - pub fn transfer_ownership( - &mut self, - account: Option, - ) -> core::result::Result<(), #err_name> { - self.ensure_admin()?; - if account == Some([0u8; 32].into()) { - panic!("Zero Address not allowed"); - } - self.pending_admin = account; - Ok(()) - } - - #[ink(message)] - pub fn accept_ownership(&mut self) -> core::result::Result<(), #err_name> { - let caller = self.env().caller(); - if self.pending_admin != Some(caller) { - return Err(#err_val); - } - self.admin = self.pending_admin.expect("Infallible"); - self.pending_admin = None; - Ok(()) - } - - fn ensure_admin(&self) -> core::result::Result<(), #err_name> { - if self.env().caller() != self.admin { - return Err(#err_val); - } - Ok(()) - } - } - ) - .into() -} diff --git a/util_macros/src/parser.rs b/util_macros/src/parser.rs deleted file mode 100644 index 47fbd68..0000000 --- a/util_macros/src/parser.rs +++ /dev/null @@ -1,33 +0,0 @@ -pub struct AznsArgs { - pub ownable2step: Option<(syn::Ident, syn::Expr)>, - pub upgradable: bool, -} - -impl syn::parse::Parse for AznsArgs { - fn parse(input: syn::parse::ParseStream) -> syn::Result { - let mut ownable2step = None; - let mut upgradable = false; - - let name: syn::Ident = input.parse()?; - - if name == "Ownable2Step" { - let content; - syn::bracketed!(content in input); - - let err_name: syn::Ident = content.parse()?; - let _: syn::Token![=] = content.parse()?; - let err_val: syn::Expr = content.parse()?; - - ownable2step = Some((err_name, err_val)); - } else if name == "Upgradable" { - upgradable = true; - } else { - return Err(syn::Error::new_spanned(name, "unidentified keyword found")); - } - - Ok(AznsArgs { - ownable2step, - upgradable, - }) - } -} diff --git a/util_macros/src/upgradability.rs b/util_macros/src/upgradability.rs deleted file mode 100644 index ce507f6..0000000 --- a/util_macros/src/upgradability.rs +++ /dev/null @@ -1,19 +0,0 @@ -pub fn upgradability_impl(struct_name: &syn::Ident) -> proc_macro::TokenStream { - quote::quote!( - impl #struct_name { - #[ink(message)] - pub fn upgrade_contract(&mut self, code_hash: [u8; 32]) { - self.ensure_admin().expect("Not Authorised"); - - ink::env::set_code_hash(&code_hash).unwrap_or_else(|err| { - panic!( - "Failed to `set_code_hash` to {:?} due to {:?}", - code_hash, err - ) - }); - ink::env::debug_println!("Switched code hash to {:?}.", code_hash); - } - } - ) - .into() -} From 7adcead28a2c571e331def652753f78e513353f9 Mon Sep 17 00:00:00 2001 From: Nimish Agrawal Date: Thu, 26 Oct 2023 23:49:38 +0530 Subject: [PATCH 2/2] chore: use zink macros --- azns_fee_calculator/Cargo.toml | 2 +- azns_fee_calculator/lib.rs | 4 ++-- azns_merkle_verifier/Cargo.toml | 2 +- azns_merkle_verifier/lib.rs | 4 ++-- azns_name_checker/Cargo.toml | 2 +- azns_name_checker/lib.rs | 4 ++-- azns_registry/Cargo.toml | 2 +- azns_registry/lib.rs | 11 +++++++---- azns_router/Cargo.toml | 2 +- azns_router/lib.rs | 4 ++-- 10 files changed, 20 insertions(+), 17 deletions(-) diff --git a/azns_fee_calculator/Cargo.toml b/azns_fee_calculator/Cargo.toml index 3eeecb2..c54a7b6 100755 --- a/azns_fee_calculator/Cargo.toml +++ b/azns_fee_calculator/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [dependencies] ink = { version = "4.2.1", default-features = false } -util_macros = { path = "../util_macros" } +zink = { git = "https://github.com/scio-labs/zink" } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } scale-info = { version = "2.5", default-features = false, features = ["derive"], optional = true } diff --git a/azns_fee_calculator/lib.rs b/azns_fee_calculator/lib.rs index 507bc96..67aed4a 100755 --- a/azns_fee_calculator/lib.rs +++ b/azns_fee_calculator/lib.rs @@ -31,10 +31,10 @@ macro_rules! ensure { pub use self::azns_fee_calculator::{FeeCalculator, FeeCalculatorRef}; -#[util_macros::azns_contract(Ownable2Step[ +#[zink::coating(Ownable2Step[ Error = Error::NotAdmin ])] -#[util_macros::azns_contract(Upgradable)] +#[zink::coating(Upgradable)] #[ink::contract] mod azns_fee_calculator { use super::*; diff --git a/azns_merkle_verifier/Cargo.toml b/azns_merkle_verifier/Cargo.toml index bcb530d..c8e3997 100755 --- a/azns_merkle_verifier/Cargo.toml +++ b/azns_merkle_verifier/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [dependencies] ink = { version = "4.2.1", default-features = false } -util_macros = { path = "../util_macros" } +zink = { git = "https://github.com/scio-labs/zink" } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } scale-info = { version = "2.5", default-features = false, features = ["derive"], optional = true } diff --git a/azns_merkle_verifier/lib.rs b/azns_merkle_verifier/lib.rs index 97175f7..17f1610 100755 --- a/azns_merkle_verifier/lib.rs +++ b/azns_merkle_verifier/lib.rs @@ -2,10 +2,10 @@ pub use self::merkle_verifier::{MerkleVerifier, MerkleVerifierRef}; -#[util_macros::azns_contract(Ownable2Step[ +#[zink::coating(Ownable2Step[ Error = Error::NotAdmin ])] -#[util_macros::azns_contract(Upgradable)] +#[zink::coating(Upgradable)] #[ink::contract] mod merkle_verifier { diff --git a/azns_name_checker/Cargo.toml b/azns_name_checker/Cargo.toml index 20eccfe..8deebf7 100644 --- a/azns_name_checker/Cargo.toml +++ b/azns_name_checker/Cargo.toml @@ -8,7 +8,7 @@ publish = false [dependencies] ink = { version = "4.2.1", default-features = false } -util_macros = { path = "../util_macros" } +zink = { git = "https://github.com/scio-labs/zink" } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } scale-info = { version = "2.5", default-features = false, features = ["derive"], optional = true } diff --git a/azns_name_checker/lib.rs b/azns_name_checker/lib.rs index 0bb116f..25cd9d4 100644 --- a/azns_name_checker/lib.rs +++ b/azns_name_checker/lib.rs @@ -40,10 +40,10 @@ impl UnicodeRange { } } -#[util_macros::azns_contract(Ownable2Step[ +#[zink::coating(Ownable2Step[ Error = Error::NotAdmin ])] -#[util_macros::azns_contract(Upgradable)] +#[zink::coating(Upgradable)] #[ink::contract] mod azns_name_checker { use crate::UnicodeRange; diff --git a/azns_registry/Cargo.toml b/azns_registry/Cargo.toml index 762b44e..491b8c4 100755 --- a/azns_registry/Cargo.toml +++ b/azns_registry/Cargo.toml @@ -8,7 +8,7 @@ publish = false [dependencies] ink = { version = "4.2.1", default-features = false } -util_macros = { path = "../util_macros" } +zink = { git = "https://github.com/scio-labs/zink" } interfaces = { path = "../interfaces", default-features = false } azns_name_checker = { path = "../azns_name_checker", default-features = false, features = ["ink-as-dependency"] } diff --git a/azns_registry/lib.rs b/azns_registry/lib.rs index 82e9763..69becda 100644 --- a/azns_registry/lib.rs +++ b/azns_registry/lib.rs @@ -2,10 +2,10 @@ mod address_dict; -#[util_macros::azns_contract(Ownable2Step[ +#[zink::coating(Ownable2Step[ Error = Error::NotAdmin ])] -#[util_macros::azns_contract(Upgradable)] +#[zink::coating(Upgradable)] #[ink::contract] mod azns_registry { use crate::address_dict::AddressDict; @@ -1571,11 +1571,14 @@ mod azns_registry { return false; }; - let Ok((registration_time, expiration_time)) = self.get_registration_period_ref(&name) else { + let Ok((registration_time, expiration_time)) = + self.get_registration_period_ref(&name) + else { return false; }; - let Some(approval_time) = self.operator_approvals.get(&(owner, operator, id)) else { + let Some(approval_time) = self.operator_approvals.get(&(owner, operator, id)) + else { return false; }; diff --git a/azns_router/Cargo.toml b/azns_router/Cargo.toml index 265f262..b560272 100755 --- a/azns_router/Cargo.toml +++ b/azns_router/Cargo.toml @@ -7,7 +7,7 @@ edition = "2021" [dependencies] ink = { version = "4.2.1", default-features = false } -util_macros = { path = "../util_macros" } +zink = { git = "https://github.com/scio-labs/zink" } scale = { package = "parity-scale-codec", version = "3", default-features = false, features = ["derive"] } scale-info = { version = "2.5", default-features = false, features = ["derive"], optional = true } diff --git a/azns_router/lib.rs b/azns_router/lib.rs index 51d9f75..b6d98fe 100755 --- a/azns_router/lib.rs +++ b/azns_router/lib.rs @@ -1,9 +1,9 @@ #![cfg_attr(not(feature = "std"), no_std, no_main)] -#[util_macros::azns_contract(Ownable2Step[ +#[zink::coating(Ownable2Step[ Error = Error::NotAdmin ])] -#[util_macros::azns_contract(Upgradable)] +#[zink::coating(Upgradable)] #[ink::contract] mod azns_router { use ink::prelude::string::{String, ToString};