From de18bb33767387bc3107f64101f3cbb3aa4b7ee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Hussein?= <80422357+nhussein11@users.noreply.github.com> Date: Fri, 13 Dec 2024 16:57:15 -0300 Subject: [PATCH] [FIX] - Refactoring testing section (#248) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: testing refactor * Apply suggestions from code review Co-authored-by: Erin Shaben * fix: references and snippets * fix: adding cards * Apply suggestions from code review Co-authored-by: Dawn Kelly <83190195+dawnkelly09@users.noreply.github.com> * Update develop/parachains/testing/mock-runtime.md Co-authored-by: Dawn Kelly <83190195+dawnkelly09@users.noreply.github.com> * Tutorials > Interoperability > XCM Transfers (#221) * fix: add wip * fix: adding last sections * fix: wording * fix: structure * Apply suggestions from code review Co-authored-by: 0xLucca <95830307+0xLucca@users.noreply.github.com> * fix: adding links * fix: images structure * fix: paraphrasing * fix: paraphrasing * Update tutorials/interoperability/xcm-transfers/from-relaychain-to-parachain.md Co-authored-by: Erin Shaben * Update tutorials/interoperability/xcm-transfers/from-relaychain-to-parachain.md Co-authored-by: Erin Shaben * fix: table * fix: adding papi tutorial * fix: paraphrasing * fix: moving code to snippet * fix: snippet * Update tutorials/interoperability/xcm-transfers/from-relaychain-to-parachain.md Co-authored-by: 0xLucca <95830307+0xLucca@users.noreply.github.com> * Update tutorials/interoperability/xcm-transfers/from-relaychain-to-parachain.md Co-authored-by: 0xLucca <95830307+0xLucca@users.noreply.github.com> * Update tutorials/interoperability/xcm-transfers/from-relaychain-to-parachain.md Co-authored-by: 0xLucca <95830307+0xLucca@users.noreply.github.com> * fix: feedback * fix: snippet * fix: link --------- Co-authored-by: 0xLucca <95830307+0xLucca@users.noreply.github.com> Co-authored-by: Erin Shaben * [FIX] - Adding references to tutorials (#230) * fix: adding reference to tutorials on asset hub page * Add xcm channels * Add runtime upgrades * Add chain spec * Add custom parachains * fix: adding chopsticks * fix: zombienet * Update develop/parachains/get-started/build-custom-parachains.md Co-authored-by: Nicolás Hussein <80422357+nhussein11@users.noreply.github.com> * fix: xcm channels references * fix: generate chain specs references * fix: generate chain specs 'where to go next' * fix: build custom parachains references * fix: runtime upgrades references * fix: chopsticks get started references * fix: zombienet get started references * fix: asset hub references * fix: add existing pallets references * fix: add smart contract funcionality references * fix: make custom pallet references * fix: pallet testing references * fix: generate chain specs references * fix: adding comment in build custom parachains * fix: deploy parachain to polkadot references * fix: setup references * fix: consensus get started references removed * fix: architecture parachains references --------- Co-authored-by: 0xLucca <0xlucca.dev@gmail.com> Co-authored-by: 0xLucca <95830307+0xLucca@users.noreply.github.com> * Tutorials > Polkadot SDK > Parachains > Build Custom Pallet > Build the Pallet (#232) * wip: build-pallet * Page ready * Improvements * Extract code snippets * Description and grammarly * fix: typo * Add suggestions * Update tutorials/polkadot-sdk/parachains/build-custom-pallet/build-pallet.md Co-authored-by: Nicolás Hussein <80422357+nhussein11@users.noreply.github.com> * Add root origin reference * Apply suggestions from code review Co-authored-by: Erin Shaben * Apply fmt * Apply suggestions from code review Co-authored-by: Erin Shaben * Apply suggestions from code review Co-authored-by: Nicolás Hussein <80422357+nhussein11@users.noreply.github.com> * Apply fixes --------- Co-authored-by: nhussein11 Co-authored-by: Nicolás Hussein <80422357+nhussein11@users.noreply.github.com> Co-authored-by: Erin Shaben * fix: hidding polkaVM instances and adding informational banner (#251) * Restructure Get Started section (#246) * Remove get-started section * Remove build and deploy pages * Apply suggestions from code review Co-authored-by: Erin Shaben --------- Co-authored-by: Erin Shaben * Tutorials > Polkadot SDK > Parachains > Build Custom Pallet > Pallet Unit Testing (#233) * fix: wip * fix: adding snippets * fix: typo * Apply suggestions from code review Co-authored-by: 0xLucca <95830307+0xLucca@users.noreply.github.com> * fix: updating mock runtime\ * fix: snippets comments * fix: typo * fix: closing expandable elements * fix: overflow * fix: overflow * Update tutorials/polkadot-sdk/parachains/build-custom-pallet/pallet-unit-testing.md Co-authored-by: Erin Shaben * fix: feedback * fix: clean up the add tutorial branch * Update tutorials/polkadot-sdk/parachains/build-custom-pallet/pallet-unit-testing.md Co-authored-by: Erin Shaben --------- Co-authored-by: 0xLucca <95830307+0xLucca@users.noreply.github.com> Co-authored-by: Erin Shaben * fix: feedback applied on mock runtime desc * fix: adding further resources * Update develop/parachains/testing/mock-runtime.md Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * fix: glossary runtime reference * fix: paraphrasing according to feedback * fix: paraphrasing * corrects "benchmark testing" to "benchmarking" throughout the page * Apply suggestions from code review Co-authored-by: Dawn Kelly <83190195+dawnkelly09@users.noreply.github.com> * Update develop/parachains/testing/benchmarking.md Co-authored-by: Dawn Kelly <83190195+dawnkelly09@users.noreply.github.com> * Update develop/parachains/testing/pallet-testing.md Co-authored-by: Dawn Kelly <83190195+dawnkelly09@users.noreply.github.com> * Update develop/parachains/testing/pallet-testing.md Co-authored-by: Dawn Kelly <83190195+dawnkelly09@users.noreply.github.com> * Update develop/parachains/testing/pallet-testing.md Co-authored-by: Dawn Kelly <83190195+dawnkelly09@users.noreply.github.com> * fix: adding reference to polkadot sdk guide * fix: curly aphostrophes * Update develop/parachains/testing/mock-runtime.md Co-authored-by: Dawn Kelly <83190195+dawnkelly09@users.noreply.github.com> * Update develop/parachains/testing/mock-runtime.md Co-authored-by: Dawn Kelly <83190195+dawnkelly09@users.noreply.github.com> * fix: feedback * fix: feedback --------- Co-authored-by: Erin Shaben Co-authored-by: Dawn Kelly <83190195+dawnkelly09@users.noreply.github.com> Co-authored-by: 0xLucca <95830307+0xLucca@users.noreply.github.com> Co-authored-by: 0xLucca <0xlucca.dev@gmail.com> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: DAWN KELLY --- .../pallet-testing/runtime-composition.rs | 6 - .../benchmarking/benchmark-output.html | 0 .../benchmarking/cargo.toml | 0 .../dispatchable-pallet-weight.rs | 0 .../benchmarking/frame-benchmark-macro.rs | 0 .../benchmarking/weight-config.rs | 0 .../genesis-config-custom.rs | 4 +- .../testing/mock-runtime/genesis-config.rs | 33 +++++ .../integration-testing-module.rs | 2 +- .../testing/mock-runtime/mock-runtime.rs | 25 ++++ .../mock-runtime}/pallets-configurations.rs | 7 ++ .../pallet-testing/event-testing.rs | 0 .../pallet-testing/function-call-testing.rs | 0 .../genesis-config-initialization.rs | 0 .../pallet-testing/runtime-composition.rs | 24 ++++ .../pallet-testing/storage-testing.rs | 0 .../pallet-testing/test-initialization.rs | 0 .../testing/setup/genesis-config.rs | 11 -- develop/parachains/customize-parachain/.pages | 4 +- .../customize-parachain/pallet-testing.md | 119 ------------------ develop/parachains/testing/.pages | 5 +- .../benchmarking.md | 48 +++---- develop/parachains/testing/mock-runtime.md | 89 +++++++++++++ develop/parachains/testing/pallet-testing.md | 85 +++++++++++++ develop/parachains/testing/runtime.md | 61 --------- .../basics/blocks-transactions-fees/fees.md | 2 +- polkadot-protocol/glossary.md | 2 +- 27 files changed, 296 insertions(+), 231 deletions(-) delete mode 100644 .snippets/code/develop/parachains/customize-parachain/pallet-testing/runtime-composition.rs rename .snippets/code/develop/parachains/{customize-parachain => testing}/benchmarking/benchmark-output.html (100%) rename .snippets/code/develop/parachains/{customize-parachain => testing}/benchmarking/cargo.toml (100%) rename .snippets/code/develop/parachains/{customize-parachain => testing}/benchmarking/dispatchable-pallet-weight.rs (100%) rename .snippets/code/develop/parachains/{customize-parachain => testing}/benchmarking/frame-benchmark-macro.rs (100%) rename .snippets/code/develop/parachains/{customize-parachain => testing}/benchmarking/weight-config.rs (100%) rename .snippets/code/develop/parachains/testing/{setup => mock-runtime}/genesis-config-custom.rs (83%) create mode 100644 .snippets/code/develop/parachains/testing/mock-runtime/genesis-config.rs rename .snippets/code/develop/parachains/testing/{setup => mock-runtime}/integration-testing-module.rs (50%) create mode 100644 .snippets/code/develop/parachains/testing/mock-runtime/mock-runtime.rs rename .snippets/code/develop/parachains/{customize-parachain/pallet-testing => testing/mock-runtime}/pallets-configurations.rs (51%) rename .snippets/code/develop/parachains/{customize-parachain => testing}/pallet-testing/event-testing.rs (100%) rename .snippets/code/develop/parachains/{customize-parachain => testing}/pallet-testing/function-call-testing.rs (100%) rename .snippets/code/develop/parachains/{customize-parachain => testing}/pallet-testing/genesis-config-initialization.rs (100%) create mode 100644 .snippets/code/develop/parachains/testing/pallet-testing/runtime-composition.rs rename .snippets/code/develop/parachains/{customize-parachain => testing}/pallet-testing/storage-testing.rs (100%) rename .snippets/code/develop/parachains/{customize-parachain => testing}/pallet-testing/test-initialization.rs (100%) delete mode 100644 .snippets/code/develop/parachains/testing/setup/genesis-config.rs delete mode 100644 develop/parachains/customize-parachain/pallet-testing.md rename develop/parachains/{customize-parachain => testing}/benchmarking.md (67%) create mode 100644 develop/parachains/testing/mock-runtime.md create mode 100644 develop/parachains/testing/pallet-testing.md delete mode 100644 develop/parachains/testing/runtime.md diff --git a/.snippets/code/develop/parachains/customize-parachain/pallet-testing/runtime-composition.rs b/.snippets/code/develop/parachains/customize-parachain/pallet-testing/runtime-composition.rs deleted file mode 100644 index 0bd001a21..000000000 --- a/.snippets/code/develop/parachains/customize-parachain/pallet-testing/runtime-composition.rs +++ /dev/null @@ -1,6 +0,0 @@ -frame_support::construct_runtime!( - pub enum Test { - System: frame_system, - TemplateModule: pallet_template, - } -); \ No newline at end of file diff --git a/.snippets/code/develop/parachains/customize-parachain/benchmarking/benchmark-output.html b/.snippets/code/develop/parachains/testing/benchmarking/benchmark-output.html similarity index 100% rename from .snippets/code/develop/parachains/customize-parachain/benchmarking/benchmark-output.html rename to .snippets/code/develop/parachains/testing/benchmarking/benchmark-output.html diff --git a/.snippets/code/develop/parachains/customize-parachain/benchmarking/cargo.toml b/.snippets/code/develop/parachains/testing/benchmarking/cargo.toml similarity index 100% rename from .snippets/code/develop/parachains/customize-parachain/benchmarking/cargo.toml rename to .snippets/code/develop/parachains/testing/benchmarking/cargo.toml diff --git a/.snippets/code/develop/parachains/customize-parachain/benchmarking/dispatchable-pallet-weight.rs b/.snippets/code/develop/parachains/testing/benchmarking/dispatchable-pallet-weight.rs similarity index 100% rename from .snippets/code/develop/parachains/customize-parachain/benchmarking/dispatchable-pallet-weight.rs rename to .snippets/code/develop/parachains/testing/benchmarking/dispatchable-pallet-weight.rs diff --git a/.snippets/code/develop/parachains/customize-parachain/benchmarking/frame-benchmark-macro.rs b/.snippets/code/develop/parachains/testing/benchmarking/frame-benchmark-macro.rs similarity index 100% rename from .snippets/code/develop/parachains/customize-parachain/benchmarking/frame-benchmark-macro.rs rename to .snippets/code/develop/parachains/testing/benchmarking/frame-benchmark-macro.rs diff --git a/.snippets/code/develop/parachains/customize-parachain/benchmarking/weight-config.rs b/.snippets/code/develop/parachains/testing/benchmarking/weight-config.rs similarity index 100% rename from .snippets/code/develop/parachains/customize-parachain/benchmarking/weight-config.rs rename to .snippets/code/develop/parachains/testing/benchmarking/weight-config.rs diff --git a/.snippets/code/develop/parachains/testing/setup/genesis-config-custom.rs b/.snippets/code/develop/parachains/testing/mock-runtime/genesis-config-custom.rs similarity index 83% rename from .snippets/code/develop/parachains/testing/setup/genesis-config-custom.rs rename to .snippets/code/develop/parachains/testing/mock-runtime/genesis-config-custom.rs index b31ce2e40..bc946d706 100644 --- a/.snippets/code/develop/parachains/testing/setup/genesis-config-custom.rs +++ b/.snippets/code/develop/parachains/testing/mock-runtime/genesis-config-custom.rs @@ -6,12 +6,12 @@ pub fn new_test_ext() -> sp_io::TestExternalities { (AccountId32::from([1u8; 32]), 2_000_000_000_000), ]; - let mut t = frame_system::GenesisConfig::::default() + let mut t = frame_system::GenesisConfig::::default() .build_storage() .unwrap(); // Adding balances configuration to the genesis config - pallet_balances::GenesisConfig:: { + pallet_balances::GenesisConfig:: { balances: initial_balances, } .assimilate_storage(&mut t) diff --git a/.snippets/code/develop/parachains/testing/mock-runtime/genesis-config.rs b/.snippets/code/develop/parachains/testing/mock-runtime/genesis-config.rs new file mode 100644 index 000000000..a3e8fcb36 --- /dev/null +++ b/.snippets/code/develop/parachains/testing/mock-runtime/genesis-config.rs @@ -0,0 +1,33 @@ +pub mod tests { + use crate::*; + use sp_runtime::BuildStorage; + + #[frame_support::runtime] + mod runtime { + #[runtime::runtime] + #[runtime::derive( + RuntimeCall, + RuntimeEvent, + RuntimeError, + RuntimeOrigin, + RuntimeFreezeReason, + RuntimeHoldReason, + RuntimeSlashReason, + RuntimeLockId, + RuntimeTask + )] + pub struct Test; + + #[runtime::pallet_index(0)] + pub type System = frame_system::Pallet; + + // Other pallets... + } + + pub fn new_test_ext() -> sp_io::TestExternalities { + frame_system::GenesisConfig::::default() + .build_storage() + .unwrap() + .into() + } +} \ No newline at end of file diff --git a/.snippets/code/develop/parachains/testing/setup/integration-testing-module.rs b/.snippets/code/develop/parachains/testing/mock-runtime/integration-testing-module.rs similarity index 50% rename from .snippets/code/develop/parachains/testing/setup/integration-testing-module.rs rename to .snippets/code/develop/parachains/testing/mock-runtime/integration-testing-module.rs index 63350c74f..629fff8a0 100644 --- a/.snippets/code/develop/parachains/testing/setup/integration-testing-module.rs +++ b/.snippets/code/develop/parachains/testing/mock-runtime/integration-testing-module.rs @@ -1,4 +1,4 @@ -pub mod integration_testing { +pub mod tests { use crate::*; // ... } \ No newline at end of file diff --git a/.snippets/code/develop/parachains/testing/mock-runtime/mock-runtime.rs b/.snippets/code/develop/parachains/testing/mock-runtime/mock-runtime.rs new file mode 100644 index 000000000..efaeb3f62 --- /dev/null +++ b/.snippets/code/develop/parachains/testing/mock-runtime/mock-runtime.rs @@ -0,0 +1,25 @@ +pub mod tests { + use crate::*; + + #[frame_support::runtime] + mod runtime { + #[runtime::runtime] + #[runtime::derive( + RuntimeCall, + RuntimeEvent, + RuntimeError, + RuntimeOrigin, + RuntimeFreezeReason, + RuntimeHoldReason, + RuntimeSlashReason, + RuntimeLockId, + RuntimeTask + )] + pub struct Test; + + #[runtime::pallet_index(0)] + pub type System = frame_system::Pallet; + + // Other pallets... + } +} \ No newline at end of file diff --git a/.snippets/code/develop/parachains/customize-parachain/pallet-testing/pallets-configurations.rs b/.snippets/code/develop/parachains/testing/mock-runtime/pallets-configurations.rs similarity index 51% rename from .snippets/code/develop/parachains/customize-parachain/pallet-testing/pallets-configurations.rs rename to .snippets/code/develop/parachains/testing/mock-runtime/pallets-configurations.rs index b9349dd65..76a71d8b6 100644 --- a/.snippets/code/develop/parachains/customize-parachain/pallet-testing/pallets-configurations.rs +++ b/.snippets/code/develop/parachains/testing/mock-runtime/pallets-configurations.rs @@ -1,3 +1,4 @@ +#[derive_impl(frame_system::config_preludes::TestDefaultConfig)] impl frame_system::Config for Test { ... type Index = u64; @@ -6,4 +7,10 @@ impl frame_system::Config for Test { type Hashing = BlakeTwo256; type AccountId = u64; ... +} + +impl pallet_template::Config for Test { + type RuntimeEvent = RuntimeEvent; + type WeightInfo = (); + ... } \ No newline at end of file diff --git a/.snippets/code/develop/parachains/customize-parachain/pallet-testing/event-testing.rs b/.snippets/code/develop/parachains/testing/pallet-testing/event-testing.rs similarity index 100% rename from .snippets/code/develop/parachains/customize-parachain/pallet-testing/event-testing.rs rename to .snippets/code/develop/parachains/testing/pallet-testing/event-testing.rs diff --git a/.snippets/code/develop/parachains/customize-parachain/pallet-testing/function-call-testing.rs b/.snippets/code/develop/parachains/testing/pallet-testing/function-call-testing.rs similarity index 100% rename from .snippets/code/develop/parachains/customize-parachain/pallet-testing/function-call-testing.rs rename to .snippets/code/develop/parachains/testing/pallet-testing/function-call-testing.rs diff --git a/.snippets/code/develop/parachains/customize-parachain/pallet-testing/genesis-config-initialization.rs b/.snippets/code/develop/parachains/testing/pallet-testing/genesis-config-initialization.rs similarity index 100% rename from .snippets/code/develop/parachains/customize-parachain/pallet-testing/genesis-config-initialization.rs rename to .snippets/code/develop/parachains/testing/pallet-testing/genesis-config-initialization.rs diff --git a/.snippets/code/develop/parachains/testing/pallet-testing/runtime-composition.rs b/.snippets/code/develop/parachains/testing/pallet-testing/runtime-composition.rs new file mode 100644 index 000000000..199883eb4 --- /dev/null +++ b/.snippets/code/develop/parachains/testing/pallet-testing/runtime-composition.rs @@ -0,0 +1,24 @@ +#[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; + + #[runtime::pallet_index(1)] + pub type Template = pallet_template::Pallet; +} \ No newline at end of file diff --git a/.snippets/code/develop/parachains/customize-parachain/pallet-testing/storage-testing.rs b/.snippets/code/develop/parachains/testing/pallet-testing/storage-testing.rs similarity index 100% rename from .snippets/code/develop/parachains/customize-parachain/pallet-testing/storage-testing.rs rename to .snippets/code/develop/parachains/testing/pallet-testing/storage-testing.rs diff --git a/.snippets/code/develop/parachains/customize-parachain/pallet-testing/test-initialization.rs b/.snippets/code/develop/parachains/testing/pallet-testing/test-initialization.rs similarity index 100% rename from .snippets/code/develop/parachains/customize-parachain/pallet-testing/test-initialization.rs rename to .snippets/code/develop/parachains/testing/pallet-testing/test-initialization.rs diff --git a/.snippets/code/develop/parachains/testing/setup/genesis-config.rs b/.snippets/code/develop/parachains/testing/setup/genesis-config.rs deleted file mode 100644 index 857cd9e7d..000000000 --- a/.snippets/code/develop/parachains/testing/setup/genesis-config.rs +++ /dev/null @@ -1,11 +0,0 @@ -pub mod integration_testing { - use crate::*; - use sp_runtime::BuildStorage; - - pub fn new_test_ext() -> sp_io::TestExternalities { - frame_system::GenesisConfig::::default() - .build_storage() - .unwrap() - .into() - } -} \ No newline at end of file diff --git a/develop/parachains/customize-parachain/.pages b/develop/parachains/customize-parachain/.pages index d9ec54ccc..c00495400 100644 --- a/develop/parachains/customize-parachain/.pages +++ b/develop/parachains/customize-parachain/.pages @@ -4,6 +4,4 @@ nav: - 'Overview (FRAME)': overview.md - 'Add Existing Pallets': add-existing-pallets.md - 'Add Smart Contract Functionality': add-smart-contract-functionality.md - - 'Make a Custom Pallet': make-custom-pallet.md - - 'Pallet Testing': pallet-testing.md - - 'Benchmark Testing': benchmarking.md \ No newline at end of file + - 'Make a Custom Pallet': make-custom-pallet.md \ No newline at end of file diff --git a/develop/parachains/customize-parachain/pallet-testing.md b/develop/parachains/customize-parachain/pallet-testing.md deleted file mode 100644 index b72fe53cc..000000000 --- a/develop/parachains/customize-parachain/pallet-testing.md +++ /dev/null @@ -1,119 +0,0 @@ ---- -title: Pallet Testing -description: Learn how to efficiently test pallets in the Polkadot SDK, ensuring the reliability and security of your pallets operations. ---- - -# Pallet Testing - -## Introduction - -Unit testing in the Polkadot SDK helps ensure that the functions provided by a pallet behave as expected. It also confirms that data and events associated with a pallet are processed correctly during interactions. The Polkadot SDK offers a set of APIs to create a test environment that can simulate runtime and mock transaction execution for both extrinsic and queries. - -This guide will explore how to mock a runtime and test a pallet. For that, the Polkadot SDK pallets use the `mock.rs` and `test.rs` files as a basis for testing pallet processes. The `mock.rs` file allows the mock runtime to be tested, and the `test.rs` file allows writing unit test functions to check the functionality of isolated pieces of code within the pallet. - -## Mocking the Runtime - -To test a pallet, a mock runtime is created to simulate the behavior of the blockchain environment where the pallet will be included. This involves defining a minimal runtime configuration that only provides for the required dependencies for the tested pallet. - -For a complete example of a mocked runtime, check out the `mock.rs` file in the [Solochain Template](https://github.com/paritytech/polkadot-sdk/blob/master/templates/solochain/pallets/template/src/mock.rs){target=\_blank}. - -A `mock.rs` file defines the mock runtime in a typical Polkadot SDK project. It includes the elements described below. - - -### Runtime Composition - -This section describes the pallets included for the mocked runtime. For example, the following code snippet shows how to build a mocked runtime called `Test` that consists of the `frame_system` pallet and the `pallet_template`: - -```rust ---8<-- 'code/develop/parachains/customize-parachain/pallet-testing/runtime-composition.rs' -``` - -### Pallets Configurations - -This section outlines the types linked to each pallet in the mocked runtime. For testing, many of these types are simple or primitive, replacing more complex, abstract types to streamline the process. - -```rust ---8<-- 'code/develop/parachains/customize-parachain/pallet-testing/pallets-configurations.rs' -``` - -The configuration should be set for each pallet existing in the mocked runtime. - -!!! note - The simplification of types is for simplifying the testing process. For example, `AccountId` is `u64`, meaning a valid account address can be an unsigned integer: - - ```rust - let alice_account: u64 = 1; - ``` - -### Genesis Config Initialization - -To initialize the genesis storage according to the mocked runtime, the following function can be used: - -```rust ---8<-- 'code/develop/parachains/customize-parachain/pallet-testing/genesis-config-initialization.rs' -``` - -## Pallet Unit Testing - -Once the mock runtime is in place, the next step is to write unit tests that evaluate the functionality of your pallet. Unit tests allow you to test specific pallet features in isolation, ensuring that each function behaves correctly under various conditions. These tests typically reside in your pallet’s module’s `test.rs` file. - -### Writing Unit Tests - -Unit tests in the Polkadot SDK use the Rust testing framework, and the mock runtime you’ve defined earlier will serve as the test environment. Below are the typical steps involved in writing unit tests for a pallet. - -#### Test Initialization - -Each test starts by initializing the runtime environment, typically using the `new_test_ext()` function, which sets up the mock storage and environment. - -```rust ---8<-- 'code/develop/parachains/customize-parachain/pallet-testing/test-initialization.rs' -``` - -#### Function Call Testing - -Call the pallet’s extrinsics or functions to simulate user interaction or internal logic. Use the `assert_ok!` macro to check for successful execution and `assert_err!` to verify that errors are handled properly. - -```rust ---8<-- 'code/develop/parachains/customize-parachain/pallet-testing/function-call-testing.rs' -``` - -#### Storage Testing - -After calling a function or extrinsic in your pallet, it's important to verify that the state changes in the pallet's storage match the expected behavior. This ensures that data is updated correctly based on the actions taken. - -The following example shows how to test the storage behavior before and after the function call: - -```rust ---8<-- 'code/develop/parachains/customize-parachain/pallet-testing/storage-testing.rs' -``` - -#### Event Testing - -It’s also crucial to test the events that your pallet emits during execution. By default, events generated in a pallet using the [`#generate_deposit`](https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/attr.generate_deposit.html){target=\_blank} macro are stored under the system's event storage key (system/events) as [`EventRecord`](https://paritytech.github.io/polkadot-sdk/master/frame_system/struct.EventRecord.html){target=\_blank} entries. These can be accessed using [`System::events()`](https://paritytech.github.io/polkadot-sdk/master/frame_system/pallet/struct.Pallet.html#method.events){target=\_blank} or verified with specific helper methods provided by the system pallet, such as [`assert_has_event`](https://paritytech.github.io/polkadot-sdk/master/frame_system/pallet/struct.Pallet.html#method.assert_has_event){target=\_blank} and [`assert_last_event`](https://paritytech.github.io/polkadot-sdk/master/frame_system/pallet/struct.Pallet.html#method.assert_last_event){target=\_blank}. - -Here’s an example of testing events in a mock runtime: - -```rust ---8<-- 'code/develop/parachains/customize-parachain/pallet-testing/event-testing.rs' -``` - -Some key considerations are: - -- **Block number** - events are not emitted on the genesis block, so you need to set the block number using [`System::set_block_number()`](https://paritytech.github.io/polkadot-sdk/master/frame_system/pallet/struct.Pallet.html#method.set_block_number){target=\_blank} to ensure events are triggered -- **Converting events** - use `.into()` when instantiating your pallet’s event to convert it into a generic event type, as required by the system’s event storage - -## Where to Go Next - -- Dive into the full implementation of the [`mock.rs`](https://github.com/paritytech/polkadot-sdk/blob/master/templates/solochain/pallets/template/src/mock.rs){target=\_blank} and [`test.rs`](https://github.com/paritytech/polkadot-sdk/blob/master/templates/solochain/pallets/template/src/tests.rs){target=\_blank} files in the [Solochain Template](https://github.com/paritytech/polkadot-sdk/tree/master/templates/solochain){target=_blank} - -
- -- Guide __Benchmarking__ - - --- - - Explore methods to measure the performance and execution cost of your pallet. - - [:octicons-arrow-right-24: Reference](/develop/parachains/testing/benchmarking) - -
\ No newline at end of file diff --git a/develop/parachains/testing/.pages b/develop/parachains/testing/.pages index 810f967dd..62ab73922 100644 --- a/develop/parachains/testing/.pages +++ b/develop/parachains/testing/.pages @@ -1,5 +1,6 @@ title: Testing nav: - index.md - - 'Setup': setup.md - - 'Runtime': runtime.md \ No newline at end of file + - 'Mock Runtime': mock-runtime.md + - 'Pallet Testing': pallet-testing.md + - 'Benchmarking': benchmarking.md \ No newline at end of file diff --git a/develop/parachains/customize-parachain/benchmarking.md b/develop/parachains/testing/benchmarking.md similarity index 67% rename from develop/parachains/customize-parachain/benchmarking.md rename to develop/parachains/testing/benchmarking.md index 1c0847eec..3c03e792a 100644 --- a/develop/parachains/customize-parachain/benchmarking.md +++ b/develop/parachains/testing/benchmarking.md @@ -1,23 +1,23 @@ --- -title: Benchmark Testing +title: Benchmarking FRAME Pallets description: Learn how to use FRAME's benchmarking framework to benchmark your custom pallets and provide correct weights for on-chain computation and execution of extrinsics. --- -# Benchmark Testing +# Benchmarking ## Introduction -Benchmark testing is a critical component of developing efficient and secure blockchain runtimes. In the Polkadot ecosystem, accurately benchmark testing your custom pallets ensures that each extrinsic has a precise [weight](/polkadot-protocol/glossary/#weight){target=\_blank}, representing its computational and storage demands. This process is vital for maintaining the blockchain's performance and preventing potential vulnerabilities, such as Denial of Service (DoS) attacks. +Benchmarking is a critical component of developing efficient and secure blockchain runtimes. In the Polkadot ecosystem, accurately benchmarking your custom pallets ensures that each extrinsic has a precise [weight](/polkadot-protocol/glossary/#weight){target=\_blank}, representing its computational and storage demands. This process is vital for maintaining the blockchain's performance and preventing potential vulnerabilities, such as Denial of Service (DoS) attacks. The Polkadot SDK leverages the [FRAME](/polkadot-protocol/glossary/#frame-framework-for-runtime-aggregation-of-modularized-entities){target=\_blank} benchmarking framework, offering tools to measure and assign weights to extrinsics. These weights help determine the maximum number of transactions or system-level calls processed within a block. This guide covers how to use FRAME's [benchmarking framework](https://paritytech.github.io/polkadot-sdk/master/frame_benchmarking/v2/index.html){target=\_blank}, from setting up your environment to writing and running benchmarks for your custom pallets. You'll understand how to generate accurate weights by the end, ensuring your runtime remains performant and secure. -## The Case for Benchmark Testing +## The Case for Benchmarking -Benchmark testing helps validate that the required execution time for different functions is within reasonable boundaries to ensure your blockchain runtime can handle transactions efficiently and securely. By accurately measuring the weight of each extrinsic, you can prevent service interruptions caused by computationally intensive calls that exceed block time limits. Without benchmark testing, runtime performance could be vulnerable to DoS attacks, where malicious users exploit functions with unoptimized weights. +Benchmarking helps validate that the required execution time for different functions is within reasonable boundaries to ensure your blockchain runtime can handle transactions efficiently and securely. By accurately measuring the weight of each extrinsic, you can prevent service interruptions caused by computationally intensive calls that exceed block time limits. Without benchmarking, runtime performance could be vulnerable to DoS attacks, where malicious users exploit functions with unoptimized weights. -Benchmark testing also ensures predictable transaction fees. Weights derived from benchmark tests accurately reflect the resource usage of function calls, allowing fair fee calculation. This approach discourages abuse while maintaining network reliability. +Benchmarking also ensures predictable transaction fees. Weights derived from benchmark tests accurately reflect the resource usage of function calls, allowing fair fee calculation. This approach discourages abuse while maintaining network reliability. -### Benchmark Testing and Weight +### Benchmarking and Weight In Polkadot SDK-based chains, weight quantifies the computational effort needed to process transactions. This weight includes factors such as: @@ -26,46 +26,46 @@ In Polkadot SDK-based chains, weight quantifies the computational effort needed - Database reads and writes - Hardware specifications -Benchmark testing uses real-world testing to simulate worst-case scenarios for extrinsics. The framework generates a linear model for weight calculation by running multiple iterations with varied parameters. These worst-case weights ensure blocks remain within execution limits, enabling the runtime to maintain throughput under varying loads. Excess fees can be refunded if a call uses fewer resources than expected, offering users a fair cost model. +Benchmarking uses real-world testing to simulate worst-case scenarios for extrinsics. The framework generates a linear model for weight calculation by running multiple iterations with varied parameters. These worst-case weights ensure blocks remain within execution limits, enabling the runtime to maintain throughput under varying loads. Excess fees can be refunded if a call uses fewer resources than expected, offering users a fair cost model. -Because weight is a generic unit of measurement based on computation time for a specific physical machine, the weight of any function can change based on the specifications of hardware used for benchmark testing. By modeling the expected weight of each runtime function, the blockchain can calculate the number of transactions or system-level calls it can execute within a certain period of time. +Because weight is a generic unit of measurement based on computation time for a specific physical machine, the weight of any function can change based on the specifications of hardware used for benchmarking. By modeling the expected weight of each runtime function, the blockchain can calculate the number of transactions or system-level calls it can execute within a certain period. Within FRAME, each function call that is dispatched must have a `#[pallet::weight]` annotation that can return the expected weight for the worst-case scenario execution of that function given its inputs: ```rust hl_lines="2" ---8<-- 'code/develop/parachains/customize-parachain/benchmarking/dispatchable-pallet-weight.rs' +--8<-- 'code/develop/parachains/testing/benchmarking/dispatchable-pallet-weight.rs' ``` -The `WeightInfo` file is automatically generated during benchmark testing. Based on these tests, this file provides accurate weights for each extrinsic. +The `WeightInfo` file is automatically generated during benchmarking. Based on these tests, this file provides accurate weights for each extrinsic. -## Benchmark Process +## Benchmarking Process -Benchmark testing a pallet involves the following steps: +Benchmarking a pallet involves the following steps: 1. Creating a `benchmarking.rs` file within your pallet's structure 2. Writing a benchmarking test for each extrinsic 3. Executing the benchmarking tool to calculate weights based on performance metrics -The benchmarking tool runs multiple iterations to model worst-case execution times and determine the appropriate weight. By default, the benchmark testing pipeline is deactivated. To activate it, compile your runtime with the `runtime-benchmarks` feature flag. +The benchmarking tool runs multiple iterations to model worst-case execution times and determine the appropriate weight. By default, the benchmarking pipeline is deactivated. To activate it, compile your runtime with the `runtime-benchmarks` feature flag. ### Prepare Your Environment Before writing benchmark tests, you need to ensure the `frame-benchmarking` crate is included in your pallet's `Cargo.toml` similar to the following: ```toml title="Cargo.toml" ---8<-- 'code/develop/parachains/customize-parachain/benchmarking/cargo.toml::1' +--8<-- 'code/develop/parachains/testing/benchmarking/cargo.toml::1' ``` You must also ensure that you add the `runtime-benchmarks` feature flag as follows under the `[features]` section of your pallet's `Cargo.toml`: ```toml title="Cargo.toml" ---8<-- 'code/develop/parachains/customize-parachain/benchmarking/cargo.toml:2:7' +--8<-- 'code/develop/parachains/testing/benchmarking/cargo.toml:2:7' ``` Lastly, ensure that `frame-benchmarking` is included in `std = []`: ```toml title="Cargo.toml" ---8<-- 'code/develop/parachains/customize-parachain/benchmarking/cargo.toml:8:12' +--8<-- 'code/develop/parachains/testing/benchmarking/cargo.toml:8:12' ``` Once complete, you have the required dependencies for writing benchmark tests for your pallet. @@ -91,7 +91,7 @@ With the directory structure set, you can use the [`polkadot-sdk-parachain-templ In your benchmarking tests, employ these best practices: - **Write custom testing functions** - the function `do_something` in the preceding example is a placeholder. Similar to writing unit tests, you must write custom functions to benchmark test your extrinsics. Access the mock runtime and use functions such as `whitelisted_caller()` to sign transactions and facilitate testing -- **Use the `#[extrinsic_call]` macro** - this macro is used when calling the extrinsic itself and is a required part of a benchmark testing function. See the [`extrinsic_call Rust docs](https://paritytech.github.io/polkadot-sdk/master/frame_benchmarking/v2/index.html#extrinsic_call-and-block){target=_blank} for more details +- **Use the `#[extrinsic_call]` macro** - this macro is used when calling the extrinsic itself and is a required part of a benchmarking function. See the [`extrinsic_call Rust docs](https://paritytech.github.io/polkadot-sdk/master/frame_benchmarking/v2/index.html#extrinsic_call-and-block){target=_blank} for more details - **Validate extrinsic behavior** - the `assert_eq` expression ensures that the extrinsic is working properly within the benchmark context ### Add Benchmarks to Runtime @@ -100,11 +100,11 @@ Before running the benchmarking tool, you must integrate benchmarks with your ru 1. Create a `benchmarks.rs` file. This file should contain the following macro, which registers all pallets for benchmarking, as well as their respective configurations: ```rust title="benchmarks.rs" - --8<-- 'code/develop/parachains/customize-parachain/benchmarking/frame-benchmark-macro.rs' + --8<-- 'code/develop/parachains/testing/benchmarking/frame-benchmark-macro.rs' ``` - For example, to register a pallet named `pallet_parachain_template` for benchmark testing, add it as follows: + For example, to register a pallet named `pallet_parachain_template` for benchmarking, add it as follows: ```rust title="benchmarks.rs" hl_lines="3" - --8<-- 'code/develop/parachains/customize-parachain/benchmarking/frame-benchmark-macro.rs::3' + --8<-- 'code/develop/parachains/testing/benchmarking/frame-benchmark-macro.rs::3' ); ``` @@ -150,20 +150,20 @@ You can now compile your runtime with the `runtime-benchmarks` feature flag. Thi The generated `weights.rs` file contains weight annotations for your extrinsics, ready to be added to your pallet. The output should be similar to the following. Some output is omitted for brevity: ---8<-- 'code/develop/parachains/customize-parachain/benchmarking/benchmark-output.html' +--8<-- 'code/develop/parachains/testing/benchmarking/benchmark-output.html' #### Add Benchmark Weights to Pallet Once the `weights.rs` is generated, you may add the generated weights to your pallet. It is common that `weights.rs` become part of your pallet's root in `src/`: ```rust ---8<-- 'code/develop/parachains/customize-parachain/benchmarking/weight-config.rs' +--8<-- 'code/develop/parachains/testing/benchmarking/weight-config.rs' ``` After which, you may add this to the `#[pallet::weight]` annotation in the extrinsic via the `Config`: ```rust hl_lines="2" ---8<-- 'code/develop/parachains/customize-parachain/benchmarking/dispatchable-pallet-weight.rs' +--8<-- 'code/develop/parachains/testing/benchmarking/dispatchable-pallet-weight.rs' ``` ## Where to Go Next diff --git a/develop/parachains/testing/mock-runtime.md b/develop/parachains/testing/mock-runtime.md new file mode 100644 index 000000000..b1ffc429e --- /dev/null +++ b/develop/parachains/testing/mock-runtime.md @@ -0,0 +1,89 @@ +--- +title: Mock Runtime for Pallet Testing +description: Learn to create a mock environment in the Polkadot SDK for testing intra-pallet functionality and inter-pallet interactions seamlessly. +--- + +# Mock Runtime + +## Introduction + +Testing is essential in Polkadot SDK development to ensure your blockchain operates as intended and effectively handles various potential scenarios. This guide walks you through setting up an environment to test pallets within the [runtime](/polkadot-protocol/glossary#runtime){target=_blank}, allowing you to evaluate how different pallets, their configurations, and system components interact to ensure reliable blockchain functionality. + +## Configuring a Mock Runtime + +### Testing Module + +The mock runtime includes all the necessary pallets and configurations needed for testing. To ensure proper testing, you must create a module that integrates all components, enabling assessment of interactions between pallets and system elements. + + +Here's a simple example of how to create a testing module that simulates these interactions: + +```rust +--8<-- 'code/develop/parachains/testing/mock-runtime/integration-testing-module.rs' +``` + +!!! note + The `crate::*;` snippet imports all the components from your crate (including runtime configurations, pallet modules, and utility functions) into the `tests` module. This allows you to write tests without manually importing each piece, making the code more concise and readable. + +Alternatively, you can create a separate `mock.rs` file to define the configuration for your mock runtime and a companion `tests.rs` file to house the specific logic for each test. + +Once the testing module is configured, you can craft your mock runtime using the [`frame_support::runtime`](https://paritytech.github.io/polkadot-sdk/master/frame_support/attr.runtime.html){target=\_blank} macro. This macro allows you to define a runtime environment that will be created for testing purposes: + +```rust +--8<-- 'code/develop/parachains/testing/mock-runtime/mock-runtime.rs' +``` +### Genesis Storage + +The next step is configuring the genesis storage—the initial state of your runtime. Genesis storage sets the starting conditions for the runtime, defining how pallets are configured before any blocks are produced. You can only customize the initial state only of those items that implement the [`[pallet::genesis_config]`](https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/attr.genesis_config.html){target=\_blank} and [`[pallet::genesis_build]`](https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/attr.genesis_build.html){target=\_blank} macros within their respective pallets. + +In Polkadot SDK, you can create this storage using the [`BuildStorage`](https://paritytech.github.io/polkadot-sdk/master/sp_runtime/trait.BuildStorage.html){target=\_blank} trait from the [`sp_runtime`](https://paritytech.github.io/polkadot-sdk/master/sp_runtime){target=\_blank} crate. This trait is essential for building the configuration that initializes the blockchain's state. + +The function `new_test_ext()` demonstrates setting up this environment. It uses `frame_system::GenesisConfig::::default()` to generate a default genesis configuration for the runtime, followed by `.build_storage()` to create the initial storage state. This storage is then converted into a format usable by the testing framework, [`sp_io::TestExternalities`](https://paritytech.github.io/polkadot-sdk/master/sp_io/type.TestExternalities.html){target=\_blank}, allowing tests to be executed in a simulated blockchain environment. + +Here's the code that sets the genesis storage configuration: + +```rust +--8<-- 'code/develop/parachains/testing/mock-runtime/genesis-config.rs' +``` + +You can also customize the genesis storage to set initial values for your runtime pallets. For example, you can set the initial balance for accounts like this: + +```rust +--8<-- 'code/develop/parachains/testing/mock-runtime/genesis-config-custom.rs' +``` + +!!! note + For a more idiomatic approach, consult the [`Your first pallet`](https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#better-test-setup){target=\_blank} guide from the Polkadot SDK rust documentation. + +### Pallet Configuration + +Each pallet in the mocked runtime requires an associated configuration, specifying the types and values it depends on to function. These configurations often use basic or primitive types (e.g., u32, bool) instead of more complex types like structs or traits, ensuring the setup remains straightforward and manageable. + +```rust +--8<-- 'code/develop/parachains/testing/mock-runtime/pallets-configurations.rs' +``` + +The configuration should be set for each pallet existing in the mocked runtime. + +!!! note + The simplification of types is for simplifying the testing process. For example, `AccountId` is `u64`, meaning a valid account address can be an unsigned integer: + + ```rust + let alice_account: u64 = 1; + ``` + +## Where to Go Next + +With the mock environment in place, developers can now test and explore how pallets interact and ensure they work seamlessly together. For further details about mocking runtimes, see the following [Polkadot SDK docs guide](https://paritytech.github.io/polkadot-sdk/master/polkadot_sdk_docs/guides/your_first_pallet/index.html#your-first-test-runtime){target=\_blank}. + +
+ +- Guide __Pallet Testing__ + + --- + + Learn how to efficiently test pallets in the Polkadot SDK, ensuring your pallet operations are reliable and secure. + + [:octicons-arrow-right-24: Reference](/develop/parachains/testing/pallet-testing/) + +
\ No newline at end of file diff --git a/develop/parachains/testing/pallet-testing.md b/develop/parachains/testing/pallet-testing.md new file mode 100644 index 000000000..528c0acc4 --- /dev/null +++ b/develop/parachains/testing/pallet-testing.md @@ -0,0 +1,85 @@ +--- +title: Pallet Testing +description: Learn how to efficiently test pallets in the Polkadot SDK, ensuring the reliability and security of your pallets operations. +--- + +# Pallet Testing + +## Introduction + +Unit testing in the Polkadot SDK helps ensure that the functions provided by a pallet behave as expected. It also confirms that data and events associated with a pallet are processed correctly during interactions. The Polkadot SDK offers a set of APIs to create a test environment to simulate runtime and mock transaction execution for extrinsics and queries. + +To begin unit testing, you must first set up a mock runtime that simulates blockchain behavior, incorporating the necessary pallets. For a deeper understanding, consult the [Mock Runtime](/develop/parachains/testing/mock-runtime/){target=\_blank} guide. + +## Writing Unit Tests + +Once the mock runtime is in place, the next step is to write unit tests that evaluate the functionality of your pallet. Unit tests allow you to test specific pallet features in isolation, ensuring that each function behaves correctly under various conditions. These tests typically reside in your pallet module's `test.rs` file. + +Unit tests in the Polkadot SDK use the Rust testing framework, and the mock runtime you've defined earlier will serve as the test environment. Below are the typical steps involved in writing unit tests for a pallet. + +The tests confirm that: + +- **Pallets initialize correctly** - at the start of each test, the system should initialize with block number 0, and the pallets should be in their default states +- **Pallets modify each other's state** - the second test shows how one pallet can trigger changes in another pallet's internal state, confirming proper cross-pallet interactions +- **State transitions between blocks are seamless** - by simulating block transitions, the tests validate that the runtime responds correctly to changes in the block number + +Testing pallet interactions within the runtime is critical for ensuring the blockchain behaves as expected under real-world conditions. Writing integration tests allows validation of how pallets function together, preventing issues that might arise when the system is fully assembled. + +This approach provides a comprehensive view of the runtime's functionality, ensuring the blockchain is stable and reliable. + +### Test Initialization + +Each test starts by initializing the runtime environment, typically using the `new_test_ext()` function, which sets up the mock storage and environment. + +```rust +--8<-- 'code/develop/parachains/testing/pallet-testing/test-initialization.rs' +``` + +### Function Call Testing + +Call the pallet's extrinsics or functions to simulate user interaction or internal logic. Use the `assert_ok!` macro to check for successful execution and `assert_err!` to verify that errors are correctly handled. + +```rust +--8<-- 'code/develop/parachains/testing/pallet-testing/function-call-testing.rs' +``` + +### Storage Testing + +After calling a function or extrinsic in your pallet, it's essential to verify that the state changes in the pallet's storage match the expected behavior to ensure data is updated correctly based on the actions taken. + +The following example shows how to test the storage behavior before and after the function call: + +```rust +--8<-- 'code/develop/parachains/testing/pallet-testing/storage-testing.rs' +``` + +### Event Testing + +It's also crucial to test the events that your pallet emits during execution. By default, events generated in a pallet using the [`#generate_deposit`](https://paritytech.github.io/polkadot-sdk/master/frame_support/pallet_macros/attr.generate_deposit.html){target=\_blank} macro are stored under the system's event storage key (system/events) as [`EventRecord`](https://paritytech.github.io/polkadot-sdk/master/frame_system/struct.EventRecord.html){target=\_blank} entries. These can be accessed using [`System::events()`](https://paritytech.github.io/polkadot-sdk/master/frame_system/pallet/struct.Pallet.html#method.events){target=\_blank} or verified with specific helper methods provided by the system pallet, such as [`assert_has_event`](https://paritytech.github.io/polkadot-sdk/master/frame_system/pallet/struct.Pallet.html#method.assert_has_event){target=\_blank} and [`assert_last_event`](https://paritytech.github.io/polkadot-sdk/master/frame_system/pallet/struct.Pallet.html#method.assert_last_event){target=\_blank}. + +Here's an example of testing events in a mock runtime: + +```rust +--8<-- 'code/develop/parachains/testing/pallet-testing/event-testing.rs' +``` + +Some key considerations are: + +- **Block number** - events are not emitted on the genesis block, so you need to set the block number using [`System::set_block_number()`](https://paritytech.github.io/polkadot-sdk/master/frame_system/pallet/struct.Pallet.html#method.set_block_number){target=\_blank} to ensure events are triggered +- **Converting events** - use `.into()` when instantiating your pallet's event to convert it into a generic event type, as required by the system's event storage + +## Where to Go Next + +- Dive into the full implementation of the [`mock.rs`](https://github.com/paritytech/polkadot-sdk/blob/master/templates/solochain/pallets/template/src/mock.rs){target=\_blank} and [`test.rs`](https://github.com/paritytech/polkadot-sdk/blob/master/templates/solochain/pallets/template/src/tests.rs){target=\_blank} files in the [Solochain Template](https://github.com/paritytech/polkadot-sdk/tree/master/templates/solochain){target=_blank} + +
+ +- Guide __Benchmarking__ + + --- + + Explore methods to measure the performance and execution cost of your pallet. + + [:octicons-arrow-right-24: Reference](/develop/parachains/testing/benchmarking) + +
\ No newline at end of file diff --git a/develop/parachains/testing/runtime.md b/develop/parachains/testing/runtime.md deleted file mode 100644 index a4290160e..000000000 --- a/develop/parachains/testing/runtime.md +++ /dev/null @@ -1,61 +0,0 @@ ---- -title: Testing Your Polkadot SDK-Based Runtime -description: Explore runtime testing in Polkadot SDK to verify how multiple pallets interact, ensuring smooth functionality within the blockchain environment. ---- - -# Runtime Testing - -## Introduction - -In the Polkadot SDK, it's important to test individual pallets in isolation and how they interact within the runtime. Once unit tests for specific pallets are complete, the next step is integration testing to verify that multiple pallets work together correctly within the blockchain system. This testing ensures that the entire runtime functions as expected under real-world conditions. - -This article extends the [Testing Setup](/develop/parachains/testing/setup){target=\_blank} guide by illustrating how to test interactions between different pallets within the same runtime. - -## Testing Pallets Interactions - -Once the test environment is ready, you can write tests to simulate interactions between multiple pallets in the runtime. Below is an example of how to test the interaction between two generic pallets, referred to here as `pallet_a` and `pallet_b`. In this scenario, assume that `pallet_b` depends on `pallet_a`. The configuration of `pallet_b` is the following: - -```rust ---8<-- "code/develop/parachains/testing/runtime/pallet-coupling.rs" -``` - -And also, `pallet_b` exposes a call that interacts with `pallet_a`: - -```rust ---8<-- "code/develop/parachains/testing/runtime/pallet-b-call.rs" -``` - -In this first test, a call to `pallet_a` is simulated, and the internal state is checked to ensure it updates correctly. The block number is also checked to ensure it advances as expected: - -```rust ---8<-- "code/develop/parachains/testing/runtime/pallet-a-integration-test.rs" -``` - -Next, a test can be written to verify the interaction between `pallet_a` and `pallet_b`: - -```rust ---8<-- "code/develop/parachains/testing/runtime/pallet-b-integration-test.rs" -``` - -This test demonstrates how `pallet_b` can trigger a change in `pallet_a`'s state, verifying that the pallets interact properly during runtime. - -For more information about testing more specific elements like storage, errors, and events, see the [Pallet Testing](/develop/parachains/customize-parachain/pallet-testing/){target=\_blank} article. - -??? "Integration Test - Complete Code" - The complete code for the integration test is shown below: - - ```rust - --8<-- "code/develop/parachains/testing/runtime/full-integration-test.rs" - ``` - -## Verifying Pallet Interactions - -The tests confirm that: - -- **Pallets initialize correctly** - at the start of each test, the system should initialize with block number 0, and the pallets should be in their default states -- **Pallets modify each other's state** - the second test shows how one pallet can trigger changes in another pallet's internal state, confirming proper cross-pallet interactions -- **State transitions between blocks are seamless** - by simulating block transitions, the tests validate that the runtime responds correctly to changes in the block number - -Testing pallet interactions within the runtime is critical for ensuring the blockchain behaves as expected under real-world conditions. Writing integration tests allows validation of how pallets function together, preventing issues that might arise when the system is fully assembled. - -This approach provides a comprehensive view of the runtime's functionality, ensuring the blockchain is stable and reliable. \ No newline at end of file diff --git a/polkadot-protocol/basics/blocks-transactions-fees/fees.md b/polkadot-protocol/basics/blocks-transactions-fees/fees.md index 6c4cabbca..79ef884d4 100644 --- a/polkadot-protocol/basics/blocks-transactions-fees/fees.md +++ b/polkadot-protocol/basics/blocks-transactions-fees/fees.md @@ -218,7 +218,7 @@ The following example illustrates how to customize your inclusion fee. You must You now know the weight system, how it affects transaction fee computation, and how to specify weights for your dispatchable calls. The next step is determining the correct weight for your dispatchable operations. You can use Substrate benchmarking functions and frame-benchmarking calls to test your functions with different parameters and empirically determine the proper weight in their worst-case scenarios. -- [Benchmark](/develop/parachains/customize-parachain/benchmarking/) +- [Benchmark](/develop/parachains/testing/benchmarking/) - [`SignedExtension`](https://paritytech.github.io/polkadot-sdk/master/sp_runtime/traits/trait.SignedExtension.html){target=\_blank} - [Custom weights for the Example pallet](https://github.com/paritytech/polkadot-sdk/blob/master/substrate/frame/examples/basic/src/weights.rs){target=\_blank} - [Web3 Foundation Research](https://research.web3.foundation/Polkadot/overview/token-economics#relay-chain-transaction-fees-and-per-block-transaction-limits){target=\_blank} \ No newline at end of file diff --git a/polkadot-protocol/glossary.md b/polkadot-protocol/glossary.md index dba3045ae..b8c32f194 100644 --- a/polkadot-protocol/glossary.md +++ b/polkadot-protocol/glossary.md @@ -276,7 +276,7 @@ A [parachain](#parachain) test network for the Polkadot network. The [Rococo](#r ## Runtime -The runtime provides the [state transition function](#state-transition-function-stf) for a node. In Polkadot SDK, the runtime is stored as a [Wasm](#webassembly-wasm) binary in the chain state. +The runtime represents the [state transition function](#state-transition-function-stf) for a blockchain. In Polkadot SDK, the runtime is stored as a [Wasm](#webassembly-wasm) binary in the chain state. The Runtime is stored under a unique state key and can be modified during the execution of the state transition function. ## Slot