Rust跨链通信库cumulus-pallet-parachain-system-proc-macro的使用:实现Substrate平行链系统的过程宏与核心功能
Rust跨链通信库cumulus-pallet-parachain-system-proc-macro的使用:实现Substrate平行链系统的过程宏与核心功能
元数据
- 包名: cumulus-pallet-parachain-system-proc-macro@0.6.0
- 发布时间: 超过1年前
- 版本: 2021版
- 许可证: Apache-2.0
- 大小: 2.9 KiB
安装
在项目目录中运行以下Cargo命令:
cargo add cumulus-pallet-parachain-system-proc-macro
或者在Cargo.toml中添加以下行:
cumulus-pallet-parachain-system-proc-macro = "0.6.0"
示例代码
以下是使用cumulus-pallet-parachain-system-proc-macro
实现Substrate平行链系统的过程宏与核心功能的完整示例:
//! 平行链运行时配置示例
use cumulus_pallet_parachain_system_proc_macro::parachain_system;
use frame_support::construct_runtime;
use sp_runtime::traits::{BlakeTwo256, IdentifyAccount, Verify};
// 定义账户ID类型
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
pub type Signature = sp_runtime::MultiSignature;
pub type BlockNumber = u32;
pub type Hash = sp_core::H256;
// 定义运行时版本信息
pub const VERSION: sp_version::RuntimeVersion = sp_version::RuntimeVersion {
spec_name: sp_version::create_runtime_str!("parallel-chain"),
impl_name: sp_version::create_runtime_str!("parallel-chain"),
authoring_version: 1,
spec_version: 1,
impl_version: 1,
apis: sp_version::create_apis_vec!([]),
transaction_version: 1,
};
// 使用过程宏定义平行链系统
#[parachain_system]
mod parachain_system {
use super::*;
pub type Header = sp_runtime::generic::Header<BlockNumber, BlakeTwo256>;
pub type Block = sp_runtime::generic::Block<Header, sp_runtime::OpaqueExtrinsic>;
pub struct Runtime;
impl frame_system::Config for Runtime {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type Origin = Origin;
type Call = Call;
type Index = u64;
type BlockNumber = BlockNumber;
type Hash = Hash;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = sp_runtime::traits::BlakeTwo256;
type Header = Header;
type Event = Event;
type BlockHashCount = ();
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = ();
}
}
// 构造运行时
construct_runtime!(
pub enum Runtime where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
ParachainSystem: parachain_system::{Pallet, Call, Storage, Config, Event<T>, Inherent},
}
);
代码说明
-
类型定义:
AccountId
: 账户ID类型Signature
: 多重签名类型BlockNumber
: 区块号类型Hash
: 哈希类型
-
运行时版本:
- 定义了运行时的版本信息
-
过程宏:
- 使用
#[parachain_system]
宏定义平行链系统模块 - 配置了frame_system的基本参数
- 使用
-
运行时构造:
- 使用
construct_runtime!
宏构建运行时 - 包含系统模块和平行链系统模块
- 使用
这个示例展示了如何使用cumulus-pallet-parachain-system-proc-macro
来快速构建一个平行链运行时框架,包含了平行链系统的基本配置和结构。
1 回复
以下是基于提供内容整理的完整示例demo,首先展示内容中提供的示例,然后给出一个更完整的实现:
内容中提供的示例回顾
- 基本配置示例:
[dependencies]
cumulus-pallet-parachain-system = { version = "0.9.0", default-features = false }
cumulus-pallet-parachain-system-proc-macro = { version = "0.9.0", optional = true }
- 运行时集成示例:
use cumulus_pallet_parachain_system::register_validate_block;
#[register_validate_block]
fn validate_block(params: BlockData) -> ValidationResult {
ValidationResult {
head_data: params.head_data,
new_validation_code: None,
upward_messages: Vec::new(),
horizontal_messages: Vec::new(),
processed_downward_messages: 0,
hrmp_watermark: params.block_number,
}
}
完整示例Demo
//! 平行链运行时完整示例
use cumulus_pallet_parachain_system::{
register_validate_block,
BlockData,
ValidationResult,
Config
};
use frame_support::{parameter_types, traits::Everything};
use sp_runtime::{
traits::{BlakeTwo256, IdentifyAccount, Verify},
MultiSignature
};
use xcm::latest::prelude::*;
// 1. 定义基本类型
pub type BlockNumber = u32;
pub type Hash = sp_core::H256;
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
pub type Signature = MultiSignature;
// 2. 平行链运行时结构
pub struct Runtime;
// 3. 实现frame_system配置
impl frame_system::Config for Runtime {
type BaseCallFilter = Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Index = u64;
type BlockNumber = BlockNumber;
type Hash = Hash;
type Hashing = BlakeTwo256;
type AccountId = AccountId;
type Lookup = sp_runtime::traits::BlakeTwo256;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = frame_support::traits::ConstU32<250>;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = ();
type OnSetCode = cumulus_pallet_parachain_system::ParachainSetCode<Self>;
type MaxConsumers = frame_support::traits::ConstU32<16>;
}
// 4. 实现平行链系统配置
impl cumulus_pallet_parachain_system::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type SelfParaId = parachain_info::Pallet<Runtime>;
type OutboundXcmpMessageSource = XcmpQueue;
type DmpMessageHandler = DmpQueue;
type ReservedDmpWeight = ReservedDmpWeight;
type XcmpMessageHandler = XcmpQueue;
type ReservedXcmpWeight = ReservedXcmpWeight;
}
// 5. 定义平行链信息模块
impl parachain_info::Config for Runtime {}
// 6. 实现验证函数
#[register_validate_block]
fn validate_block(params: BlockData) -> ValidationResult {
// 示例验证逻辑:检查块头是否有效
if params.header.number == 0 {
panic!("Genesis block should have number 0");
}
ValidationResult {
head_data: params.head_data,
new_validation_code: None, // 没有验证码更新
upward_messages: Vec::new(), // 没有上行消息
horizontal_messages: Vec::new(), // 没有水平消息
processed_downward_messages: 0, // 处理的下行消息数
hrmp_watermark: params.block_number, // 水位标记
}
}
// 7. 跨链消息处理示例
pub struct XcmpQueue;
impl xcm_builder::ProcessXcmMessage for XcmpQueue {
type RuntimeCall = RuntimeCall;
fn process_message(
_message: &mut [u8],
_origin: xcm::latest::MultiLocation,
_max_weight: u64,
_weight_credit: &mut u64,
) -> Result<bool, xcm::latest::Error> {
// 实际应用中实现消息处理逻辑
Ok(true)
}
}
// 8. 权重参数
parameter_types! {
pub const ReservedDmpWeight: Weight = Weight::from_parts(1_000_000_000, 1_000_000);
pub const ReservedXcmpWeight: Weight = Weight::from_parts(1_000_000_000, 1_000_000);
}
// 9. 主函数(模拟运行时构建)
fn main() {
println!("平行链运行时构建完成!");
}
关键点说明
-
类型定义:定义了平行链运行所需的基本类型如BlockNumber、Hash等
-
系统配置:实现了frame_system::Config,这是Substrate运行时的基础配置
-
平行链配置:实现了cumulus_pallet_parachain_system::Config,包含:
- 事件类型
- 自身平行链ID
- 跨链消息处理器
- 权重预留等
-
验证函数:使用#[register_validate_block]宏标记的核心验证函数
-
跨链处理:实现了XCMP消息处理的基本框架
-
权重管理:通过parameter_types定义DMP和XCMP的预留权重
这个示例展示了如何构建一个完整的平行链运行时,包含了从基本配置到跨链通信的全部关键组件。开发者可以在此基础上添加业务特定的pallet和逻辑。