Skip to content

Commit

Permalink
Improvements in minimal template (#4119)
Browse files Browse the repository at this point in the history
This PR makes a few improvements in the docs for the minimal template.

---------

Co-authored-by: Kian Paimani <[email protected]>
  • Loading branch information
2 people authored and Morganamilo committed May 2, 2024
1 parent 63bd05e commit 8145769
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 21 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ members = [
"substrate/utils/substrate-bip39",
"substrate/utils/wasm-builder",

"templates/minimal",
"templates/minimal/node",
"templates/minimal/pallets/template",
"templates/minimal/runtime",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub fn expand_outer_dispatch(
quote! {
#( #query_call_part_macros )*

/// The aggregated runtime call type.
#[derive(
Clone, PartialEq, Eq,
#scrate::__private::codec::Encode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ pub(crate) fn decl_all_pallets<'a>(
for pallet_declaration in pallet_declarations {
let type_name = &pallet_declaration.name;
let pallet = &pallet_declaration.path;
let docs = &pallet_declaration.docs;
let mut generics = vec![quote!(#runtime)];
generics.extend(pallet_declaration.instance.iter().map(|name| quote!(#pallet::#name)));
let mut attrs = Vec::new();
Expand All @@ -541,6 +542,7 @@ pub(crate) fn decl_all_pallets<'a>(
attrs.extend(TokenStream2::from_str(&feat).expect("was parsed successfully; qed"));
}
let type_decl = quote!(
#( #[doc = #docs] )*
#(#attrs)*
pub type #type_name = #pallet::Pallet <#(#generics),*>;
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,8 @@ pub struct Pallet {
pub pallet_parts: Vec<PalletPart>,
/// Expressions specified inside of a #[cfg] attribute.
pub cfg_pattern: Vec<cfg_expr::Expression>,
/// The doc literals
pub docs: Vec<syn::Expr>,
}

impl Pallet {
Expand Down Expand Up @@ -774,6 +776,7 @@ fn convert_pallets(pallets: Vec<PalletDeclaration>) -> syn::Result<PalletsConver
instance: pallet.instance,
cfg_pattern,
pallet_parts,
docs: vec![],
})
})
.collect::<Result<Vec<_>>>()?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,9 @@ pub fn expand_tt_default_parts(def: &mut Def) -> proc_macro2::TokenStream {
macro_rules! #default_parts_unique_id_v2 {
{
$caller:tt
frame_support = [{ $($frame_support:ident)::* }]
your_tt_return = [{ $my_tt_return:path }]
} => {
$($frame_support)*::__private::tt_return! {
$my_tt_return! {
$caller
tokens = [{
+ Pallet #call_part_v2 #storage_part_v2 #event_part_v2 #error_part_v2 #origin_part_v2 #config_part_v2
Expand Down
4 changes: 2 additions & 2 deletions substrate/frame/support/procedural/src/runtime/expand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn construct_runtime_implicit_to_explicit(
let frame_support = generate_access_from_frame_or_crate("frame-support")?;
let attr = if legacy_ordering { quote!((legacy_ordering)) } else { quote!() };
let mut expansion = quote::quote!(
#[frame_support::runtime #attr]
#[#frame_support::runtime #attr]
#input
);
for pallet in definition.pallet_decls.iter() {
Expand All @@ -103,7 +103,7 @@ fn construct_runtime_implicit_to_explicit(
expansion = quote::quote!(
#frame_support::__private::tt_call! {
macro = [{ #pallet_path::tt_default_parts_v2 }]
frame_support = [{ #frame_support }]
your_tt_return = [{ #frame_support::__private::tt_return }]
~~> #frame_support::match_and_insert! {
target = [{ #expansion }]
pattern = [{ #pallet_name = #pallet_path #pallet_instance }]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// limitations under the License.

use crate::construct_runtime::parse::{Pallet, PalletPart, PalletPartKeyword, PalletPath};
use frame_support_procedural_tools::get_doc_literals;
use quote::ToTokens;
use syn::{punctuated::Punctuated, spanned::Spanned, token, Error, Ident, PathArguments};

Expand Down Expand Up @@ -86,6 +87,8 @@ impl Pallet {

let cfg_pattern = vec![];

let docs = get_doc_literals(&item.attrs);

Ok(Pallet {
is_expanded: true,
name,
Expand All @@ -94,6 +97,7 @@ impl Pallet {
instance,
cfg_pattern,
pallet_parts,
docs,
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ use syn::{spanned::Spanned, Attribute, Ident, PathArguments};
/// The declaration of a pallet.
#[derive(Debug, Clone)]
pub struct PalletDeclaration {
/// The name of the pallet, e.g.`System` in `System: frame_system`.
/// The name of the pallet, e.g.`System` in `pub type System = frame_system`.
pub name: Ident,
/// Optional attributes tagged right above a pallet declaration.
pub attrs: Vec<Attribute>,
/// The path of the pallet, e.g. `frame_system` in `System: frame_system`.
/// The path of the pallet, e.g. `frame_system` in `pub type System = frame_system`.
pub path: syn::Path,
/// The instance of the pallet, e.g. `Instance1` in `Council: pallet_collective::<Instance1>`.
/// The instance of the pallet, e.g. `Instance1` in `pub type Council =
/// pallet_collective<Instance1>`.
pub instance: Option<Ident>,
}

Expand Down
25 changes: 25 additions & 0 deletions templates/minimal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "minimal-template"
description = "A minimal template built with Substrate, part of Polkadot Sdk."
version = "0.0.0"
license = "MIT-0"
authors.workspace = true
homepage.workspace = true
repository.workspace = true
edition.workspace = true
publish = false

[lints]
workspace = true

[dependencies]
minimal-template-node = { path = "./node" }
minimal-template-runtime = { path = "./runtime" }
pallet-minimal-template = { path = "./pallets/template" }
polkadot-sdk-docs = { path = "../../docs/sdk" }

frame = { package = "polkadot-sdk-frame", path = "../../substrate/frame" }

# How we build docs in rust-docs
simple-mermaid = "0.1.1"
docify = "0.2.7"
13 changes: 13 additions & 0 deletions templates/minimal/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Minimal Template

This is a minimal template for creating a blockchain using the Polkadot SDK.

# Docs

You can generate and view the [Rust
Docs](https://doc.rust-lang.org/cargo/commands/cargo-doc.html) for this template
with this command:

```sh
cargo doc -p minimal-template --open
```
79 changes: 65 additions & 14 deletions templates/minimal/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! A minimal runtime that includes the template [`pallet`](`pallet_minimal_template`).
#![cfg_attr(not(feature = "std"), no_std)]

// Make the WASM binary available.
Expand All @@ -24,6 +26,7 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs"));
use frame::{
deps::frame_support::{
genesis_builder_helper::{build_state, get_preset},
runtime,
weights::{FixedFee, NoFee},
},
prelude::*,
Expand All @@ -36,6 +39,7 @@ use frame::{
},
};

/// The runtime version.
#[runtime_version]
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("minimal-template-runtime"),
Expand All @@ -54,61 +58,108 @@ pub fn native_version() -> NativeVersion {
NativeVersion { runtime_version: VERSION, can_author_with: Default::default() }
}

/// The signed extensions that are added to the runtime.
type SignedExtra = (
// Checks that the sender is not the zero address.
frame_system::CheckNonZeroSender<Runtime>,
// Checks that the runtime version is correct.
frame_system::CheckSpecVersion<Runtime>,
// Checks that the transaction version is correct.
frame_system::CheckTxVersion<Runtime>,
// Checks that the genesis hash is correct.
frame_system::CheckGenesis<Runtime>,
// Checks that the era is valid.
frame_system::CheckEra<Runtime>,
// Checks that the nonce is valid.
frame_system::CheckNonce<Runtime>,
// Checks that the weight is valid.
frame_system::CheckWeight<Runtime>,
// Ensures that the sender has enough funds to pay for the transaction
// and deducts the fee from the sender's account.
pallet_transaction_payment::ChargeTransactionPayment<Runtime>,
);

construct_runtime!(
pub enum Runtime {
System: frame_system,
Timestamp: pallet_timestamp,

Balances: pallet_balances,
Sudo: pallet_sudo,
TransactionPayment: pallet_transaction_payment,

// our local pallet
Template: pallet_minimal_template,
}
);
// Composes the runtime by adding all the used pallets and deriving necessary types.
#[runtime]
mod runtime {
/// The main runtime type.
#[runtime::runtime]
#[runtime::derive(
RuntimeCall,
RuntimeEvent,
RuntimeError,
RuntimeOrigin,
RuntimeFreezeReason,
RuntimeHoldReason,
RuntimeSlashReason,
RuntimeLockId,
RuntimeTask
)]
pub struct Runtime;

/// Mandatory system pallet that should always be included in a FRAME runtime.
#[runtime::pallet_index(0)]
pub type System = frame_system;

/// Provides a way for consensus systems to set and check the onchain time.
#[runtime::pallet_index(1)]
pub type Timestamp = pallet_timestamp;

/// Provides the ability to keep track of balances.
#[runtime::pallet_index(2)]
pub type Balances = pallet_balances;

/// Provides a way to execute privileged functions.
#[runtime::pallet_index(3)]
pub type Sudo = pallet_sudo;

/// Provides the ability to charge for extrinsic execution.
#[runtime::pallet_index(4)]
pub type TransactionPayment = pallet_transaction_payment;

/// A minimal pallet template.
#[runtime::pallet_index(5)]
pub type Template = pallet_minimal_template;
}

parameter_types! {
pub const Version: RuntimeVersion = VERSION;
}

/// Implements the types required for the system pallet.
#[derive_impl(frame_system::config_preludes::SolochainDefaultConfig)]
impl frame_system::Config for Runtime {
type Block = Block;
type Version = Version;
type BlockHashCount = ConstU32<1024>;
// Use the account data from the balances pallet
type AccountData = pallet_balances::AccountData<<Runtime as pallet_balances::Config>::Balance>;
}

// Implements the types required for the balances pallet.
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for Runtime {
type AccountStore = System;
}

// Implements the types required for the sudo pallet.
#[derive_impl(pallet_sudo::config_preludes::TestDefaultConfig)]
impl pallet_sudo::Config for Runtime {}

// Implements the types required for the sudo pallet.
#[derive_impl(pallet_timestamp::config_preludes::TestDefaultConfig)]
impl pallet_timestamp::Config for Runtime {}

// Implements the types required for the transaction payment pallet.
#[derive_impl(pallet_transaction_payment::config_preludes::TestDefaultConfig)]
impl pallet_transaction_payment::Config for Runtime {
type OnChargeTransaction = pallet_transaction_payment::FungibleAdapter<Balances, ()>;
// Setting fee as independent of the weight of the extrinsic for demo purposes
type WeightToFee = NoFee<<Self as pallet_balances::Config>::Balance>;
// Setting fee as fixed for any length of the call data for demo purposes
type LengthToFee = FixedFee<1, <Self as pallet_balances::Config>::Balance>;
}

// Implements the types required for the template pallet.
impl pallet_minimal_template::Config for Runtime {}

type Block = frame::runtime::types_common::BlockOf<Runtime, SignedExtra>;
Expand Down
Loading

0 comments on commit 8145769

Please sign in to comment.