Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update solochain to use #[runtime] #5772

Merged
merged 16 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 2 additions & 2 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 @@ -16,7 +16,7 @@ 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)),
Template::cause_error(RuntimeOrigin::signed(1)),
Error::<Test>::NoneValue
);
});
Expand Down
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
)]
Comment on lines +13 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure the philosophy we want to follow, but the template does not need all these derives. i could see an argument to use it either way

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pro reducing this, but I cannot find the documentation explaining what each does. My personal preference would be to exclude them, and explain in the template why we are excluding them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the type isnt being used anywhere in the file, you can pretty safely delete it.

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
4 changes: 2 additions & 2 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 @@ -20,7 +20,7 @@ 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)),
Template::cause_error(RuntimeOrigin::signed(1)),
Error::<Test>::NoneValue
);
});
Expand Down
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;
}
Loading