Skip to content

Commit

Permalink
update solochain to use #[runtime] (paritytech#5772)
Browse files Browse the repository at this point in the history
# Description

* This is part of [issue
5242](paritytech#5242),
specifically getting solochain to use `#[frame::runtime]`
* Furthermore, reinforced the convention of `Template` instead of
`TemplateModule`


## Integration

* Should be integrated into the `solochain` template and documentation

## Review Notes

* Refactored `solochain` template from
[construct_runtime!](https://paritytech.github.io/polkadot-sdk/master/frame_support/macro.construct_runtime.html)
to
[#[runtime]](https://paritytech.github.io/polkadot-sdk/master/frame_support/attr.runtime.html).
* AFAIU `Template` is our new convention, and preferred over
`TemplateModule`.

# Out of scope

* The
[#[runtime]](https://paritytech.github.io/polkadot-sdk/master/frame_support/attr.runtime.html)
documentation is still very rudimentary, and should ideally be expanded
to explain the macro, both what it does and the input options.
* Furthermore, suggest update
[#[runtime]](https://paritytech.github.io/polkadot-sdk/master/frame_support/attr.runtime.html)
documentation to replace `#[crate::runtime]` with
`#[frame_support::runtime]`

---------

Co-authored-by: Jan-Jan <[email protected]>
Co-authored-by: Oliver Tale-Yazdi <[email protected]>
Co-authored-by: Shawn Tabrizi <[email protected]>
Co-authored-by: command-bot <>
  • Loading branch information
4 people authored Sep 23, 2024
1 parent fbcda7e commit 08498f5
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 39 deletions.
2 changes: 1 addition & 1 deletion prdoc/1.9.0/pr_1378.prdoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ doc:
3. `#[runtime::pallet_index]` must be attached to a pallet to define its index
4. `#[runtime::disable_call]` can be optionally attached to a pallet to disable its calls
5. `#[runtime::disable_unsigned]` can be optionally attached to a pallet to disable unsigned calls
6. A pallet instance can be defined as `TemplateModule: pallet_template<Instance>`
6. A pallet instance can be defined as `Template: pallet_template<Instance>`
An optional attribute can be defined as `#[frame_support::runtime(legacy_ordering)]` to ensure that
the order of hooks is same as the order of pallets (and not based on the pallet_index). This is to support
legacy runtimes and should be avoided for new ones.
Expand Down
2 changes: 1 addition & 1 deletion templates/parachain/pallets/template/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod test_runtime {
#[runtime::pallet_index(0)]
pub type System = frame_system;
#[runtime::pallet_index(1)]
pub type TemplateModule = crate;
pub type Template = crate;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
Expand Down
7 changes: 2 additions & 5 deletions templates/parachain/pallets/template/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use frame_support::{assert_noop, assert_ok};
fn it_works_for_default_value() {
new_test_ext().execute_with(|| {
// Dispatch a signed extrinsic.
assert_ok!(TemplateModule::do_something(RuntimeOrigin::signed(1), 42));
assert_ok!(Template::do_something(RuntimeOrigin::signed(1), 42));
// Read pallet storage and assert an expected result.
assert_eq!(Something::<Test>::get().map(|v| v.block_number), Some(42));
});
Expand All @@ -15,9 +15,6 @@ fn it_works_for_default_value() {
fn correct_error_for_none_value() {
new_test_ext().execute_with(|| {
// Ensure the expected error is thrown when no value is present.
assert_noop!(
TemplateModule::cause_error(RuntimeOrigin::signed(1)),
Error::<Test>::NoneValue
);
assert_noop!(Template::cause_error(RuntimeOrigin::signed(1)), Error::<Test>::NoneValue);
});
}
16 changes: 8 additions & 8 deletions templates/parachain/pallets/template/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub trait WeightInfo {
/// Weights for pallet_template using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
/// Storage: TemplateModule Something (r:0 w:1)
/// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
/// Storage: Template Something (r:0 w:1)
/// Proof: Template Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
fn do_something() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
Expand All @@ -51,8 +51,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
Weight::from_parts(9_000_000, 0)
.saturating_add(T::DbWeight::get().writes(1_u64))
}
/// Storage: TemplateModule Something (r:1 w:1)
/// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
/// Storage: Template Something (r:1 w:1)
/// Proof: Template Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
fn cause_error() -> Weight {
// Proof Size summary in bytes:
// Measured: `32`
Expand All @@ -66,8 +66,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {

// For backwards compatibility and tests
impl WeightInfo for () {
/// Storage: TemplateModule Something (r:0 w:1)
/// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
/// Storage: Template Something (r:0 w:1)
/// Proof: Template Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
fn do_something() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
Expand All @@ -76,8 +76,8 @@ impl WeightInfo for () {
Weight::from_parts(9_000_000, 0)
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: TemplateModule Something (r:1 w:1)
/// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
/// Storage: Template Something (r:1 w:1)
/// Proof: Template Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
fn cause_error() -> Weight {
// Proof Size summary in bytes:
// Measured: `32`
Expand Down
2 changes: 1 addition & 1 deletion templates/solochain/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ template and note the following:
configuration is defined by a code block that begins with `impl
$PALLET_NAME::Config for Runtime`.
- The pallets are composed into a single runtime by way of the
[`construct_runtime!`](https://paritytech.github.io/substrate/master/frame_support/macro.construct_runtime.html)
[#[runtime]](https://paritytech.github.io/polkadot-sdk/master/frame_support/attr.runtime.html)
macro, which is part of the [core FRAME pallet
library](https://docs.substrate.io/reference/frame-pallets/#system-pallets).

Expand Down
32 changes: 24 additions & 8 deletions templates/solochain/pallets/template/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,30 @@ use sp_runtime::BuildStorage;

type Block = frame_system::mocking::MockBlock<Test>;

// Configure a mock runtime to test the pallet.
frame_support::construct_runtime!(
pub enum Test
{
System: frame_system,
TemplateModule: pallet_template,
}
);
#[frame_support::runtime]
mod runtime {
// The main runtime
#[runtime::runtime]
// Runtime Types to be generated
#[runtime::derive(
RuntimeCall,
RuntimeEvent,
RuntimeError,
RuntimeOrigin,
RuntimeFreezeReason,
RuntimeHoldReason,
RuntimeSlashReason,
RuntimeLockId,
RuntimeTask
)]
pub struct Test;

#[runtime::pallet_index(0)]
pub type System = frame_system::Pallet<Test>;

#[runtime::pallet_index(1)]
pub type Template = pallet_template::Pallet<Test>;
}

#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for Test {
Expand Down
7 changes: 2 additions & 5 deletions templates/solochain/pallets/template/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn it_works_for_default_value() {
// Go past genesis block so events get deposited
System::set_block_number(1);
// Dispatch a signed extrinsic.
assert_ok!(TemplateModule::do_something(RuntimeOrigin::signed(1), 42));
assert_ok!(Template::do_something(RuntimeOrigin::signed(1), 42));
// Read pallet storage and assert an expected result.
assert_eq!(Something::<Test>::get(), Some(42));
// Assert that the correct event was deposited
Expand All @@ -19,9 +19,6 @@ fn it_works_for_default_value() {
fn correct_error_for_none_value() {
new_test_ext().execute_with(|| {
// Ensure the expected error is thrown when no value is present.
assert_noop!(
TemplateModule::cause_error(RuntimeOrigin::signed(1)),
Error::<Test>::NoneValue
);
assert_noop!(Template::cause_error(RuntimeOrigin::signed(1)), Error::<Test>::NoneValue);
});
}
16 changes: 8 additions & 8 deletions templates/solochain/pallets/template/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub trait WeightInfo {
/// Weights for pallet_template using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
/// Storage: TemplateModule Something (r:0 w:1)
/// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
/// Storage: Template Something (r:0 w:1)
/// Proof: Template Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
fn do_something() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
Expand All @@ -51,8 +51,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
Weight::from_parts(9_000_000, 0)
.saturating_add(T::DbWeight::get().writes(1_u64))
}
/// Storage: TemplateModule Something (r:1 w:1)
/// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
/// Storage: Template Something (r:1 w:1)
/// Proof: Template Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
fn cause_error() -> Weight {
// Proof Size summary in bytes:
// Measured: `32`
Expand All @@ -66,8 +66,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {

// For backwards compatibility and tests
impl WeightInfo for () {
/// Storage: TemplateModule Something (r:0 w:1)
/// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
/// Storage: Template Something (r:0 w:1)
/// Proof: Template Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
fn do_something() -> Weight {
// Proof Size summary in bytes:
// Measured: `0`
Expand All @@ -76,8 +76,8 @@ impl WeightInfo for () {
Weight::from_parts(9_000_000, 0)
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
/// Storage: TemplateModule Something (r:1 w:1)
/// Proof: TemplateModule Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
/// Storage: Template Something (r:1 w:1)
/// Proof: Template Something (max_values: Some(1), max_size: Some(4), added: 499, mode: MaxEncodedLen)
fn cause_error() -> Weight {
// Proof Size summary in bytes:
// Measured: `32`
Expand Down
2 changes: 1 addition & 1 deletion templates/solochain/runtime/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ frame_benchmarking::define_benchmarks!(
[pallet_balances, Balances]
[pallet_timestamp, Timestamp]
[pallet_sudo, Sudo]
[pallet_template, TemplateModule]
[pallet_template, Template]
);
2 changes: 1 addition & 1 deletion templates/solochain/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,5 @@ mod runtime {

// Include the custom logic from the pallet-template in the runtime.
#[runtime::pallet_index(7)]
pub type TemplateModule = pallet_template;
pub type Template = pallet_template;
}

0 comments on commit 08498f5

Please sign in to comment.