Skip to content

Commit 815c7f8

Browse files
brunopgalvaoTeluceroeshaben
authored
Add pallet testing (#1168)
* add pallet unit testing * add pallet testing * Adding a pallet is included in the create a pallet page * Resolve PR review comments - Add periods to bullet points for consistency - Change Events and Block Number admonition from warning to info - Add title to lib.rs code block * Resolve PR review comments and add pallet code reference - Add periods to all bullet points for consistency - Change Events and Block Number admonition from warning to info - Add title to lib.rs code block - Add collapsible complete pallet code reference using snippet include - Add test descriptions as sentences under section headers * llms * resolve llms * Formatting * terminal element * fix spacing * check links and update .nav.yml to remove add-pallet-to-runtime page * Merge staging/product-ia --------- Co-authored-by: Taylor Lucero <Telucero@users.noreply.github.com> Co-authored-by: Taylor Lucero <67010424+Telucero@users.noreply.github.com> Co-authored-by: Erin Shaben <eshaben@icloud.com>
1 parent 36a9dfa commit 815c7f8

35 files changed

+1997
-5404
lines changed

.ai/categories/basics.md

Lines changed: 0 additions & 357 deletions
Original file line numberDiff line numberDiff line change
@@ -117,362 +117,6 @@ The address mapping system maintains security through several design choices evi
117117
All source code references are from the [`address.rs`](https://github.com/paritytech/polkadot-sdk/blob/stable2412/substrate/frame/revive/src/address.rs){target=\_blank} file in the Revive pallet of the Polkadot SDK repository.
118118

119119

120-
---
121-
122-
Page Title: Add Pallets to the Runtime
123-
124-
- Source (raw): https://raw.githubusercontent.com/polkadot-developers/polkadot-docs/master/.ai/pages/parachains-customize-runtime-pallet-development-add-pallet-to-runtime.md
125-
- Canonical (HTML): https://docs.polkadot.com/parachains/customize-runtime/pallet-development/add-pallet-to-runtime/
126-
- Summary: Add pallets to your runtime for custom functionality. Learn to configure and integrate pallets in Polkadot SDK-based blockchains.
127-
128-
# Add Pallets to the Runtime
129-
130-
## Introduction
131-
132-
In previous tutorials, you learned how to [create a custom pallet](/tutorials/polkadot-sdk/parachains/zero-to-hero/build-custom-pallet/){target=\_blank} and [test it](/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-unit-testing/){target=\_blank}. The next step is to include this pallet in your runtime, integrating it into the core logic of your blockchain.
133-
134-
This tutorial will guide you through adding two pallets to your runtime: the custom pallet you previously developed and the [utility pallet](https://paritytech.github.io/polkadot-sdk/master/pallet_utility/index.html){target=\_blank}. This standard Polkadot SDK pallet provides powerful dispatch functionality. The utility pallet offers, for example, batch dispatch, a stateless operation that enables executing multiple calls in a single transaction.
135-
136-
## Add the Pallets as Dependencies
137-
138-
First, you'll update the runtime's `Cargo.toml` file to include the Utility pallet and your custom pallets as dependencies for the runtime. Follow these steps:
139-
140-
1. Open the `runtime/Cargo.toml` file and locate the `[dependencies]` section. Add pallet-utility as one of the features for the `polkadot-sdk` dependency with the following line:
141-
142-
```toml hl_lines="4" title="runtime/Cargo.toml"
143-
[dependencies]
144-
...
145-
polkadot-sdk = { workspace = true, features = [
146-
"pallet-utility",
147-
...
148-
], default-features = false }
149-
```
150-
151-
2. In the same `[dependencies]` section, add the custom pallet that you built from scratch with the following line:
152-
153-
```toml hl_lines="3" title="Cargo.toml"
154-
[dependencies]
155-
...
156-
custom-pallet = { path = "../pallets/custom-pallet", default-features = false }
157-
```
158-
159-
3. In the `[features]` section, add the custom pallet to the `std` feature list:
160-
161-
```toml hl_lines="5" title="Cargo.toml"
162-
[features]
163-
default = ["std"]
164-
std = [
165-
...
166-
"custom-pallet/std",
167-
...
168-
]
169-
```
170-
171-
3. Save the changes and close the `Cargo.toml` file.
172-
173-
Once you have saved your file, it should look like the following:
174-
175-
???- code "runtime/Cargo.toml"
176-
177-
```rust title="runtime/Cargo.toml"
178-
[package]
179-
name = "parachain-template-runtime"
180-
description = "A parachain runtime template built with Substrate and Cumulus, part of Polkadot Sdk."
181-
version = "0.1.0"
182-
license = "Unlicense"
183-
authors.workspace = true
184-
homepage.workspace = true
185-
repository.workspace = true
186-
edition.workspace = true
187-
publish = false
188-
189-
[package.metadata.docs.rs]
190-
targets = ["x86_64-unknown-linux-gnu"]
191-
192-
[build-dependencies]
193-
docify = { workspace = true }
194-
substrate-wasm-builder = { optional = true, workspace = true, default-features = true }
195-
196-
[dependencies]
197-
codec = { features = ["derive"], workspace = true }
198-
cumulus-pallet-parachain-system.workspace = true
199-
docify = { workspace = true }
200-
hex-literal = { optional = true, workspace = true, default-features = true }
201-
log = { workspace = true }
202-
pallet-parachain-template = { path = "../pallets/template", default-features = false }
203-
polkadot-sdk = { workspace = true, features = [
204-
"pallet-utility",
205-
"cumulus-pallet-aura-ext",
206-
"cumulus-pallet-session-benchmarking",
207-
"cumulus-pallet-weight-reclaim",
208-
"cumulus-pallet-xcm",
209-
"cumulus-pallet-xcmp-queue",
210-
"cumulus-primitives-aura",
211-
"cumulus-primitives-core",
212-
"cumulus-primitives-utility",
213-
"pallet-aura",
214-
"pallet-authorship",
215-
"pallet-balances",
216-
"pallet-collator-selection",
217-
"pallet-message-queue",
218-
"pallet-session",
219-
"pallet-sudo",
220-
"pallet-timestamp",
221-
"pallet-transaction-payment",
222-
"pallet-transaction-payment-rpc-runtime-api",
223-
"pallet-xcm",
224-
"parachains-common",
225-
"polkadot-parachain-primitives",
226-
"polkadot-runtime-common",
227-
"runtime",
228-
"staging-parachain-info",
229-
"staging-xcm",
230-
"staging-xcm-builder",
231-
"staging-xcm-executor",
232-
], default-features = false }
233-
scale-info = { features = ["derive"], workspace = true }
234-
serde_json = { workspace = true, default-features = false, features = [
235-
"alloc",
236-
] }
237-
smallvec = { workspace = true, default-features = true }
238-
239-
custom-pallet = { path = "../pallets/custom-pallet", default-features = false }
240-
241-
[features]
242-
default = ["std"]
243-
std = [
244-
"codec/std",
245-
"cumulus-pallet-parachain-system/std",
246-
"log/std",
247-
"pallet-parachain-template/std",
248-
"polkadot-sdk/std",
249-
"scale-info/std",
250-
"serde_json/std",
251-
"substrate-wasm-builder",
252-
"custom-pallet/std",
253-
]
254-
255-
runtime-benchmarks = [
256-
"cumulus-pallet-parachain-system/runtime-benchmarks",
257-
"hex-literal",
258-
"pallet-parachain-template/runtime-benchmarks",
259-
"polkadot-sdk/runtime-benchmarks",
260-
]
261-
262-
try-runtime = [
263-
"cumulus-pallet-parachain-system/try-runtime",
264-
"pallet-parachain-template/try-runtime",
265-
"polkadot-sdk/try-runtime",
266-
]
267-
268-
# Enable the metadata hash generation.
269-
#
270-
# This is hidden behind a feature because it increases the compile time.
271-
# The wasm binary needs to be compiled twice, once to fetch the metadata,
272-
# generate the metadata hash and then a second time with the
273-
# `RUNTIME_METADATA_HASH` environment variable set for the `CheckMetadataHash`
274-
# extension.
275-
metadata-hash = ["substrate-wasm-builder/metadata-hash"]
276-
277-
# A convenience feature for enabling things when doing a build
278-
# for an on-chain release.
279-
on-chain-release-build = ["metadata-hash"]
280-
281-
```
282-
283-
Update your root parachain template's `Cargo.toml` file to include your custom pallet as a dependency. Follow these steps:
284-
285-
1. Open the `./Cargo.toml` file and locate the `[workspace]` section.
286-
287-
Make sure the `custom-pallet` is a member of the workspace:
288-
289-
```toml hl_lines="4" title="Cargo.toml"
290-
[workspace]
291-
default-members = ["pallets/template", "runtime"]
292-
members = [
293-
"node", "pallets/custom-pallet",
294-
"pallets/template",
295-
"runtime",
296-
]
297-
```
298-
299-
???- code "./Cargo.toml"
300-
301-
```rust title="./Cargo.toml"
302-
[workspace.package]
303-
license = "MIT-0"
304-
authors = ["Parity Technologies <admin@parity.io>"]
305-
homepage = "https://paritytech.github.io/polkadot-sdk/"
306-
repository = "https://github.com/paritytech/polkadot-sdk-parachain-template.git"
307-
edition = "2021"
308-
309-
[workspace]
310-
default-members = ["pallets/template", "runtime"]
311-
members = [
312-
"node", "pallets/custom-pallet",
313-
"pallets/template",
314-
"runtime",
315-
]
316-
resolver = "2"
317-
318-
[workspace.dependencies]
319-
parachain-template-runtime = { path = "./runtime", default-features = false }
320-
pallet-parachain-template = { path = "./pallets/template", default-features = false }
321-
clap = { version = "4.5.13" }
322-
color-print = { version = "0.3.4" }
323-
docify = { version = "0.2.9" }
324-
futures = { version = "0.3.31" }
325-
jsonrpsee = { version = "0.24.3" }
326-
log = { version = "0.4.22", default-features = false }
327-
polkadot-sdk = { version = "2503.0.1", default-features = false }
328-
prometheus-endpoint = { version = "0.17.2", default-features = false, package = "substrate-prometheus-endpoint" }
329-
serde = { version = "1.0.214", default-features = false }
330-
codec = { version = "3.7.4", default-features = false, package = "parity-scale-codec" }
331-
cumulus-pallet-parachain-system = { version = "0.20.0", default-features = false }
332-
hex-literal = { version = "0.4.1", default-features = false }
333-
scale-info = { version = "2.11.6", default-features = false }
334-
serde_json = { version = "1.0.132", default-features = false }
335-
smallvec = { version = "1.11.0", default-features = false }
336-
substrate-wasm-builder = { version = "26.0.1", default-features = false }
337-
frame = { version = "0.9.1", default-features = false, package = "polkadot-sdk-frame" }
338-
339-
[profile.release]
340-
opt-level = 3
341-
panic = "unwind"
342-
343-
[profile.production]
344-
codegen-units = 1
345-
inherits = "release"
346-
lto = true
347-
```
348-
349-
350-
### Update the Runtime Configuration
351-
352-
Configure the pallets by implementing their `Config` trait and update the runtime macro to include the new pallets:
353-
354-
1. Add the `OriginCaller` import:
355-
356-
```rust title="mod.rs" hl_lines="8"
357-
// Local module imports
358-
use super::OriginCaller;
359-
...
360-
```
361-
362-
2. Implement the [`Config`](https://paritytech.github.io/polkadot-sdk/master/pallet_utility/pallet/trait.Config.html){target=\_blank} trait for both pallets at the end of the `runtime/src/config/mod.rs` file:
363-
364-
```rust title="mod.rs" hl_lines="8-25"
365-
...
366-
/// Configure the pallet template in pallets/template.
367-
impl pallet_parachain_template::Config for Runtime {
368-
type RuntimeEvent = RuntimeEvent;
369-
type WeightInfo = pallet_parachain_template::weights::SubstrateWeight<Runtime>;
370-
}
371-
372-
// Configure utility pallet.
373-
impl pallet_utility::Config for Runtime {
374-
type RuntimeEvent = RuntimeEvent;
375-
type RuntimeCall = RuntimeCall;
376-
type PalletsOrigin = OriginCaller;
377-
type WeightInfo = pallet_utility::weights::SubstrateWeight<Runtime>;
378-
}
379-
// Define counter max value runtime constant.
380-
parameter_types! {
381-
pub const CounterMaxValue: u32 = 500;
382-
}
383-
384-
// Configure custom pallet.
385-
impl custom_pallet::Config for Runtime {
386-
type RuntimeEvent = RuntimeEvent;
387-
type CounterMaxValue = CounterMaxValue;
388-
}
389-
```
390-
391-
3. Locate the `#[frame_support::runtime]` macro in the `runtime/src/lib.rs` file and add the pallets:
392-
393-
```rust hl_lines="9-14" title="lib.rs"
394-
#[frame_support::runtime]
395-
mod runtime {
396-
#[runtime::runtime]
397-
#[runtime::derive(
398-
...
399-
)]
400-
pub struct Runtime;
401-
#[runtime::pallet_index(51)]
402-
pub type Utility = pallet_utility;
403-
404-
#[runtime::pallet_index(52)]
405-
pub type CustomPallet = custom_pallet;
406-
}
407-
```
408-
409-
## Recompile the Runtime
410-
411-
After adding and configuring your pallets in the runtime, the next step is to ensure everything is set up correctly. To do this, recompile the runtime with the following command (make sure you're in the project's root directory):
412-
413-
```bash
414-
cargo build --release
415-
```
416-
417-
This command ensures the runtime compiles without errors, validates the pallet configurations, and prepares the build for subsequent testing or deployment.
418-
419-
## Run Your Chain Locally
420-
421-
Launch your parachain locally and start producing blocks:
422-
423-
!!!tip
424-
Generated chain TestNet specifications include development accounts "Alice" and "Bob." These accounts are pre-funded with native parachain currency, allowing you to sign and send TestNet transactions. Take a look at the [Polkadot.js Accounts section](https://polkadot.js.org/apps/#/accounts){target=\_blank} to view the development accounts for your chain.
425-
426-
1. Create a new chain specification file with the updated runtime:
427-
428-
```bash
429-
chain-spec-builder create -t development \
430-
--relay-chain paseo \
431-
--para-id 1000 \
432-
--runtime ./target/release/wbuild/parachain-template-runtime/parachain_template_runtime.compact.compressed.wasm \
433-
named-preset development
434-
```
435-
436-
2. Start the omni node with the generated chain specification:
437-
438-
```bash
439-
polkadot-omni-node --chain ./chain_spec.json --dev
440-
```
441-
442-
3. Verify you can interact with the new pallets using the [Polkadot.js Apps](https://polkadot.js.org/apps/?rpc=ws%3A%2F%2F127.0.0.1%3A9944#/extrinsics){target=\_blank} interface. Navigate to the **Extrinsics** tab and check that you can see both pallets:
443-
444-
- Utility pallet
445-
446-
![](/images/parachains/customize-runtime/pallet-development/add-pallet-to-runtime/add-pallets-to-runtime-01.webp)
447-
448-
449-
- Custom pallet
450-
451-
![](/images/parachains/customize-runtime/pallet-development/add-pallet-to-runtime/add-pallets-to-runtime-02.webp)
452-
453-
## Where to Go Next
454-
455-
<div class="grid cards" markdown>
456-
457-
- <span class="badge tutorial">Tutorial</span> __Deploy on Paseo TestNet__
458-
459-
---
460-
461-
Deploy your Polkadot SDK blockchain on Paseo! Follow this step-by-step guide for a seamless journey to a successful TestNet deployment.
462-
463-
[:octicons-arrow-right-24: Get Started](/tutorials/polkadot-sdk/parachains/zero-to-hero/deploy-to-testnet/)
464-
465-
- <span class="badge tutorial">Tutorial</span> __Pallet Benchmarking (Optional)__
466-
467-
---
468-
469-
Discover how to measure extrinsic costs and assign precise weights to optimize your pallet for accurate fees and runtime performance.
470-
471-
[:octicons-arrow-right-24: Get Started](/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/)
472-
473-
</div>
474-
475-
476120
---
477121

478122
Page Title: Contract Deployment
@@ -1514,7 +1158,6 @@ Deep dive into creating and managing custom pallets for your parachain.
15141158
| [Create a Custom Pallet](/parachains/customize-runtime/pallet-development/create-a-pallet/) | Build a pallet from scratch with custom logic |
15151159
| [Mock Your Runtime](/parachains/customize-runtime/pallet-development/mock-runtime/) | Set up a mock runtime environment for testing |
15161160
| [Pallet Unit Testing](/parachains/customize-runtime/pallet-development/pallet-testing/) | Write comprehensive tests for your pallet logic |
1517-
| [Add Your Custom Pallet to the Runtime](/parachains/customize-runtime/pallet-development/add-pallet-to-runtime/) | Integrate your custom pallet into your parachain runtime |
15181161
| [Benchmark the Custom Pallet](/parachains/customize-runtime/pallet-development/benchmark-pallet/) | Measure and optimize pallet performance with benchmarking |
15191162
15201163
## Testing

0 commit comments

Comments
 (0)