Skip to content

Commit

Permalink
remove abstractions (#48)
Browse files Browse the repository at this point in the history
* remove abstractions

* fix fmt

* remove `abstraction` atribute from runtime api

* Update procedural/src/lib.rs

Co-authored-by: Amar Singh <asinghchrony@protonmail.com>

---------

Co-authored-by: Amar Singh <asinghchrony@protonmail.com>
  • Loading branch information
KitHat and 4meta5 authored Nov 26, 2024
1 parent d367e35 commit 777a5a0
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 78 deletions.
2 changes: 0 additions & 2 deletions procedural/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ We have made an abstraction over the `construct_runtime!`. The macro itself has
```rust
#[openzeppelin_construct_runtime]
mod runtime {
#[abstraction]
struct System; // Available names are System, Consensus, XCM, Assets, Governance, EVM.
#[pallet]
type Pallet = pallet_crate; // It mimics the second version of construct runtime macro, but without the pallet_index assignment
Expand Down Expand Up @@ -38,7 +37,6 @@ mod apis {
// block type
type Block = Block;

#[abstraction]
mod assets {
type TransactionPayment = TransactionPayment;
type RuntimeCall = RuntimeCall;
Expand Down
2 changes: 1 addition & 1 deletion procedural/src/apis/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl TryFrom<&[Item]> for XCMBenchmarkAPIFields {
fee_asset_id,
transaction_byte_fee,
address,
balances
balances,
})
}
}
Expand Down
8 changes: 2 additions & 6 deletions procedural/src/apis/tanssi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ impl TryFrom<&[Item]> for TanssiAPIFields {
}
}

pub fn tanssi_apis(
runtime: &Ident,
block: &Ident,
session_keys: &Ident,
) -> TokenStream {
pub fn tanssi_apis(runtime: &Ident, block: &Ident, session_keys: &Ident) -> TokenStream {
let mut res = quote! {};
res.extend(quote! {
impl sp_session::SessionKeys<#block> for #runtime {
Expand All @@ -51,4 +47,4 @@ pub fn tanssi_apis(
});

res
}
}
30 changes: 18 additions & 12 deletions procedural/src/construct_runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ fn parse_abstraction(
let abstraction_name = ConstructAbstractions::try_from(item).expect("Wrong Struct");

match abstraction_name {
ConstructAbstractions::System => (
construct_system(index),
None,
),
ConstructAbstractions::System => (construct_system(index), None),
ConstructAbstractions::Assets => (construct_assets(index), None),
ConstructAbstractions::Consensus => (construct_consensus(index), Some(quote! {
cumulus_pallet_parachain_system::register_validate_block! {
Runtime = Runtime,
BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
}
})),
ConstructAbstractions::Consensus => (
construct_consensus(index),
Some(quote! {
cumulus_pallet_parachain_system::register_validate_block! {
Runtime = Runtime,
BlockExecutor = cumulus_pallet_aura_ext::BlockExecutor::<Runtime, Executive>,
}
}),
),
ConstructAbstractions::Governance => (construct_governance(index), None),
ConstructAbstractions::Xcm => (construct_xcm(index), None),
ConstructAbstractions::Evm => (construct_evm(index), None),
Expand Down Expand Up @@ -142,11 +142,17 @@ fn construct_evm(index: &mut u32) -> proc_macro2::TokenStream {
}

fn construct_assets(index: &mut u32) -> proc_macro2::TokenStream {
construct_abstraction(index, &openzeppelin_pallet_abstractions::assets::PALLET_NAMES)
construct_abstraction(
index,
&openzeppelin_pallet_abstractions::assets::PALLET_NAMES,
)
}

fn construct_system(index: &mut u32) -> proc_macro2::TokenStream {
construct_abstraction(index, &openzeppelin_pallet_abstractions::system::PALLET_NAMES)
construct_abstraction(
index,
&openzeppelin_pallet_abstractions::system::PALLET_NAMES,
)
}

fn construct_abstraction(index: &mut u32, pallets: &[(&str, &str)]) -> proc_macro2::TokenStream {
Expand Down
28 changes: 13 additions & 15 deletions procedural/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ mod models;
mod runtime_apis;

/// Construct runtime macro abstraction
/// This macro allows to construct runtime operating with abstractions as long as with pallets.
/// It allows you omit the pallet indexes -- they will be assigned in ascending order.
/// This macro allows to construct runtime operating with abstractions as long as with pallets.
/// It allows you omit the pallet indices such that they are assigned in ascending order.
/// This API may change in future releases, it is not yet stabilised.
///
///
/// Example:
/// ```
/// #[openzeppelin_construct_runtime]
/// mod runtime {
/// #[abstraction]
/// struct System; // Available names are System, Consensus, XCM, Assets, Governance, EVM.
/// #[pallet]
/// type Pallet = pallet_crate; // It mimics the second version of construct runtime macro, but without the pallet_index assignment
/// }
/// ```
///
///
/// Supported abstractions and pallets inside them:
/// * `System`:
/// * `System`:
/// * `frame_system`
/// * `pallet_timestamp`
/// * `parachain_info`
Expand Down Expand Up @@ -51,15 +50,15 @@ mod runtime_apis;
/// * `pallet_whitelist`
/// * `pallet_custom_origins`
/// * `pallet_referenda`
/// * `XCM`
/// * `XCM`
/// * `pallet_message_queue`
/// * `cumulus_pallet_xcmp_queue`
/// * `pallet_xcm`
/// * `cumulus_pallet_xcm`
/// * `pallet_xcm_weight_trader`
/// * `orml_xtokens`
/// * `pallet_xcm_transactor`
/// * `EVM`
/// * `EVM`
/// * `pallet_ethereum`
/// * `pallet_evm`
/// * `pallet_base_fee`
Expand All @@ -73,28 +72,27 @@ pub fn openzeppelin_construct_runtime(_: TokenStream, tokens: TokenStream) -> To
/// Runtime API macro abstraction
/// This macro wraps the `impl_runtime_api` macro and provides our implementations of them.
/// It also works in the same groupings as our abstractions pallets and to get the runtime API implementations you only need to provide some types.
///
///
/// Example:
/// ```
/// #[openzeppelin_runtime_apis]
/// mod apis {
/// // these types should be present and required for all abstractions
/// // runtime generated by construct_runtime
/// type Runtime = Runtime;
/// // block type
/// // block type
/// type Block = Block;
///
/// #[abstraction]
///
/// mod assets {
/// type TransactionPayment = TransactionPayment;
/// type RuntimeCall = RuntimeCall;
/// type Balance = Balance;
/// }
///
///
/// // Any impl block can also go there, it will be ported to the `impl_runtime_api!`
/// }
/// ```
///
///
/// Supported abstractions:
/// * `EVM`
/// * Implemented APIs:
Expand All @@ -118,7 +116,7 @@ pub fn openzeppelin_construct_runtime(_: TokenStream, tokens: TokenStream) -> To
/// * `sp_session::SessionKeys`
/// * `cumulus_primitives_aura::AuraUnincludedSegmentApi` (if `async-backing` feature is enabled)
/// * Required Types:
/// * `SessionKeys` -- struct generated by `impl_opaque_keys` macro
/// * `SessionKeys` -- struct generated by `impl_opaque_keys` macro
/// * `Aura` -- `pallet_aura` struct pallet generated by `construct_runtime` macro (only if `async-backing` feature is not enabled)
/// * `SlotDuration` -- constant that is use for slot duration definition (only if `async-backing` feature is enabled)
/// * `ConsensusHook` -- type that is used in `cumulus_pallet_parachain_system::Config::ConsensusHook` (only if `async-backing` feature is enabled)
Expand Down
18 changes: 2 additions & 16 deletions procedural/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,17 @@ pub enum ConstructAbstractions {
System,
Governance,
Consensus,
Tanssi
Tanssi,
}

#[derive(Debug)]
pub enum ConversionError {
UnknownAbstraction,
NoAbstractionAttribute,
}

impl TryFrom<ItemStruct> for ConstructAbstractions {
type Error = ConversionError;
fn try_from(value: ItemStruct) -> Result<Self, Self::Error> {
let is_pallet = value.attrs.iter().any(|f| {
let Ok(path) = f.meta.require_path_only() else {
return false;
};
let Ok(ident) = path.require_ident() else {
return false;
};
ident == "abstraction"
});
if !is_pallet {
return Err(ConversionError::NoAbstractionAttribute);
}

ConstructAbstractions::try_from(value.ident)
}
}
Expand Down Expand Up @@ -66,7 +52,7 @@ pub enum APIAbstractions {
Evm,
Consensus,
Assets,
Tanssi
Tanssi,
}

impl TryFrom<Ident> for APIAbstractions {
Expand Down
18 changes: 3 additions & 15 deletions procedural/src/runtime_apis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,7 @@ pub fn impl_openzeppelin_runtime_apis(input: TokenStream) -> TokenStream {
block = Some(fetch_ident(&ty.ty));
}
}
Item::Mod(m) => {
let is_abstraction = m.attrs.iter().any(|f| {
let Ok(path) = f.meta.require_path_only() else {
return false;
};
let Ok(ident) = path.require_ident() else {
return false;
};
ident == "abstraction"
});
if is_abstraction {
abstractions.push(m)
}
}
Item::Mod(m) => abstractions.push(m),
Item::Impl(im) => {
inner.extend(im.to_token_stream());
}
Expand Down Expand Up @@ -181,7 +168,8 @@ fn construct_abstraction(
quote! {}
}
APIAbstractions::Tanssi => {
let TanssiAPIFields { session_keys } = TanssiAPIFields::try_from(content.as_slice()).expect("Error while parsing Tanssi config");
let TanssiAPIFields { session_keys } = TanssiAPIFields::try_from(content.as_slice())
.expect("Error while parsing Tanssi config");
apis::tanssi_apis(runtime, block, &session_keys)
}
}
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,3 @@ pub trait EvmConfig {
type Erc20XcmBridgeTransferGasLimit;
type LocationToH160;
}

17 changes: 7 additions & 10 deletions src/tanssi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#[macro_export]
macro_rules! impl_openzeppelin_tanssi {
() => {
Expand All @@ -7,23 +6,21 @@ macro_rules! impl_openzeppelin_tanssi {
type AccountLookup = dp_consensus::NimbusLookUp;
type CanAuthor = pallet_cc_authorities_noting::CanAuthor<Runtime>;
type SlotBeacon = dp_consensus::AuraDigestSlotBeacon<Runtime>;
type WeightInfo =
pallet_author_inherent::weights::SubstrateWeight<Runtime>;
type WeightInfo = pallet_author_inherent::weights::SubstrateWeight<Runtime>;
}

impl pallet_cc_authorities_noting::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type SelfParaId = parachain_info::Pallet<Runtime>;
type RelayChainStateProvider =
type RelayChainStateProvider =
cumulus_pallet_parachain_system::RelaychainDataProvider<Self>;
type AuthorityId = nimbus_primitives::NimbusId;
type WeightInfo =
pallet_cc_authorities_noting::weights::SubstrateWeight<Runtime>;
type WeightInfo = pallet_cc_authorities_noting::weights::SubstrateWeight<Runtime>;
}
};
}

pub const PALLET_NAMES: [(&str, &str); 2] = [
("AuthorInherent", "pallet_author_inherent"),
("AuthoritiesNoting", "pallet_cc_authorities_noting")
];
("AuthorInherent", "pallet_author_inherent"),
("AuthoritiesNoting", "pallet_cc_authorities_noting"),
];

0 comments on commit 777a5a0

Please sign in to comment.