substrate学习笔记3:添加一个Pallet到Runtime
说明
substrate node template提供了一个最小的可工作的运行时,但是为了保持精炼,它并不包括Frame中的大多数的Pallet。本节我们将学习如何将Pallet添加到runtime中。
安装Node Template
本节的学习需要使用到Node Template,其安装过程我们在之前Substrate中已经讲过,此处不再累述。
导入Pallet
打开runtime/Catgo.toml,分别添加依赖和feature。
- 添加依赖:
[dependencies]
allet-nicks = { default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', tag = 'monthly-2021-08' }
- 添加feature:
[features]
default = ["std"]
std = [
#--snip--
'pallet-nicks/std',
#--snip--
]
配置Pallet
在此节,我们主要要为runtime实现对应的nick pallet的config接口,因此我们在runtime/src/lib.rs中添加如下代码:
parameter_types! {
// Choose a fee that incentivizes desireable behavior.
pub const NickReservationFee: u128 = 100;
pub const MinNickLength: u32 = 8;
// Maximum bounds on storage are important to secure your chain.
pub const MaxNickLength: u32 = 32;
}
impl pallet_nicks::Config for Runtime {
// The Balances pallet implements the ReservableCurrency trait.
// `Balances` is defined in `construct_runtimes!` macro. See below.
// https://substrate.dev/rustdocs/latest/pallet_balances/index.html#implementations-2
type Currency = Balances;
// Use the NickReservationFee from the parameter_types block.
type ReservationFee = NickReservationFee;
// No action is taken when deposits are forfeited.
type Slashed = ();
// Configure the FRAME System Root origin as the Nick pallet admin.
// https://substrate.dev/rustdocs/latest/frame_system/enum.RawOrigin.html#variant.Root
type ForceOrigin = frame_system::EnsureRoot<AccountId>;
// Use the MinNickLength from the parameter_types block.
type MinLength = MinNickLength;
// Use the MaxNickLength from the parameter_types block.
type MaxLength = MaxNickLength;
// The ubiquitous event type.
type Event = Event;
}
将Nicks添加到construct_runtime!中
construct_runtime!宏的意思是用给定的模块来定义我们的运行时,因此我们需要将我们的nick pallet添加到其中。在runtime/src/lib.rs的construct_runtime!中添加代码:
construct_runtime!(
pub enum Runtime where
Block = Block,
NodeBlock = opaque::Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
/* --snip-- */
/*** Add This Line ***/
Nicks: pallet_nicks::{Pallet, Call, Storage, Event<T>},
}
);
编译
运行如下命令编译:
cargo build --release
运行
用如下命令运行:
./target/release/node-template --dev --tmp
启动前端:
yarn start
本作品采用《CC 协议》,转载必须注明作者和本文链接