Rust跨链通信插件库cumulus-pallet-parachain-system的使用:实现Polkadot平行链系统核心功能

Rust跨链通信插件库cumulus-pallet-parachain-system的使用:实现Polkadot平行链系统核心功能

安装

在项目目录中运行以下Cargo命令:

cargo add cumulus-pallet-parachain-system

或者在Cargo.toml中添加以下行:

cumulus-pallet-parachain-system = "0.22.0"

示例代码

下面是一个使用cumulus-pallet-parachain-system实现Polkadot平行链系统核心功能的完整示例:

use frame_support::{parameter_types, traits::Everything};
use sp_core::H256;
use sp_runtime::{
    traits::{BlakeTwo256, IdentityLookup},
    BuildStorage,
};

// 平行链系统配置
parameter_types! {
    pub const RelayChainId: cumulus_primitives_core::ParaId = 2000.into();
    pub const ReservedXcmpWeight: u64 = 1_000_000_000;
    pub const ReservedDmpWeight: u64 = 1_000_000_000;
}

// 运行时配置
pub struct Runtime;

impl cumulus_pallet_parachain_system::Config for Runtime {
    type RuntimeEvent = ();
    type OnSystemEvent = ();
    type SelfParaId = RelayChainId;
    type OutboundXcmpMessageSource = ();
    type XcmpMessageHandler = ();
    type ReservedXcmpWeight = ReservedXcmpWeight;
    type ReservedDmpWeight = ReservedDmpWeight;
    type CheckAssociatedRelayNumber = cumulus_pallet_parachain_system::RelayNumberStrictlyIncreases;
    type ConsensusHook = cumulus_pallet_parachain_system::ParachainBlockImportMarker;
    type WeightInfo = ();
}

// 创建平行链运行时
pub type Block = frame_system::mocking::MockBlock<Runtime>;

frame_support::construct_runtime!(
    pub struct Runtime {
        System: frame_system,
        ParachainSystem: cumulus_pallet_parachain_system,
    }
);

// 系统配置
impl frame_system::Config for Runtime {
    type BaseCallFilter = Everything;
    type BlockWeights = ();
    type BlockLength = ();
    type DbWeight = ();
    type RuntimeOrigin = ();
    type RuntimeCall = ();
    type Index = u64;
    type BlockNumber = u32;
    type Hash = H256;
    type Hashing = BlakeTwo256;
    type AccountId = u64;
    type Lookup = IdentityLookup<Self::AccountId>;
    type Header = sp_runtime::generic::Header<Self::BlockNumber, Self::Hashing>;
    type RuntimeEvent = ();
    type BlockHashCount = ();
    type Version = ();
    type PalletInfo = PalletInfo;
    type AccountData = ();
    type OnNewAccount = ();
    type OnKilledAccount = ();
    type SystemWeightInfo = ();
    type SS58Prefix = ();
    type OnSetCode = cumulus_pallet_parachain_system::ParachainSetCode<Self>;
    type MaxConsumers = frame_support::traits::ConstU32<16>;
}

// 测试平行链系统功能
#[test]
fn test_parachain_system() {
    use cumulus_pallet_parachain_system::Pallet as ParachainSystem;
    use frame_support::traits::OnInitialize;
    
    let mut t = frame_system::GenesisConfig::<Runtime>::default()
        .build_storage()
        .unwrap();
    
    // 初始化平行链系统
    let block = 1;
    ParachainSystem::<Runtime>::on_initialize(block);
    
    // 验证系统状态
    assert_eq!(
        ParachainSystem::<Runtime>::active_config(),
        cumulus_primitives_core::AbridgedHostConfiguration::default()
    );
}

功能说明

  1. 跨链消息传递(XCMP):通过XcmpMessageHandler处理来自其他平行链的消息
  2. 下行消息传递(DMP):通过ReservedDmpWeight配置中继链到平行链的消息权重限制
  3. 平行链身份管理:通过SelfParaId定义平行链的唯一标识符
  4. 区块验证:通过ConsensusHook确保平行链区块被中继链验证
  5. 代码升级:通过OnSetCode实现平行链运行时代码升级功能

1 回复

Rust跨链通信插件库cumulus-pallet-parachain-system使用指南

概述

cumulus-pallet-parachain-system是Substrate框架中用于实现Polkadot平行链核心功能的pallet。它为平行链提供了与中继链通信的基础设施,是构建Polkadot生态平行链的关键组件。

主要功能

  1. 处理与中继链的跨链消息传递(XCMP)
  2. 管理平行链的验证过程
  3. 提供中继链状态访问接口
  4. 处理升级和治理相关功能

使用方法

1. 添加依赖

Cargo.toml中添加依赖:

[dependencies]
cumulus-pallet-parachain-system = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-vX.Y.Z" }

2. 在runtime中集成

impl pallet_parachain_system::Config for Runtime {
    type Event = Event;
    type OnSystemEvent = ();
    type SelfParaId = parachain_info::Module<Runtime>;
    type OutboundXcmpMessageSource = XcmpQueue;
    type DmpMessageHandler = DmpQueue;
    type ReservedDmpWeight = ReservedDmpWeight;
    type XcmpMessageHandler = XcmpQueue;
    type ReservedXcmpWeight = ReservedXcmpWeight;
}

3. 基本使用示例

获取平行链ID

use cumulus_pallet_parachain_system::Pallet as ParachainSystem;

let para_id = ParachainSystem::parachain_id();

处理跨链消息

use cumulus_pallet_parachain_system::Pallet as ParachainSystem;

// 发送跨链消息
ParachainSystem::send_upward_message(dest, message).unwrap();

// 接收消息通常由系统自动处理

访问中继链状态

use cumulus_pallet_parachain_system::Pallet as ParachainSystem;

let relay_parent_number = ParachainSystem::relay_parent_number();

高级配置

配置消息队列

parameter_types! {
    pub const ReservedXcmpWeight: Weight = WEIGHT_PER_SECOND / 4;
    pub const ReservedDmpWeight: Weight = WEIGHT_PER_SECOND / 4;
}

impl pallet_parachain_system::Config for Runtime {
    // ... 其他配置
    type ReservedDmpWeight = ReservedDmpWeight;
    type ReservedXcmpWeight = ReservedXcmpWeight;
}

处理升级

impl pallet_parachain_system::Config for Runtime {
    // ... 其他配置
    type OnUpgrade = ();
}

注意事项

  1. 该pallet通常需要与parachain-info pallet一起使用
  2. 跨链消息处理需要正确配置权重
  3. 平行链的runtime必须实现特定的trait才能与中继链正确交互

完整runtime集成示例

frame_support::construct_runtime!(
    pub enum Runtime where
        Block = Block,
        NodeBlock = opaque::Block,
        UncheckedExtrinsic = UncheckedExtrinsic,
    {
        // ... 其他pallet
        ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Storage, Inherent, Event<T>},
        ParachainInfo: parachain_info::{Pallet, Storage, Config},
        // ... 其他pallet
    }
);

完整示例demo

以下是一个完整的平行链runtime集成示例:

// 引入必要的依赖
use frame_support::{construct_runtime, parameter_types};
use sp_runtime::traits::{BlakeTwo256, IdentifyAccount, Verify};
use sp_core::sr25519;

// 定义runtime的基本类型
pub type BlockNumber = u32;
pub type AccountId = <<Signature as Verify>::Signer as IdentifyAccount>::AccountId;
pub type Signature = sr25519::Signature;
pub type Index = u32;

// 定义消息队列权重参数
parameter_types! {
    pub const BlockHashCount: BlockNumber = 250;
    pub const Version: RuntimeVersion = RuntimeVersion {
        spec_name: create_runtime_str!("demo-parachain"),
        impl_name: create_runtime_str!("demo-parachain"),
        authoring_version: 1,
        spec_version: 1,
        impl_version: 0,
        apis: RUNTIME_API_VERSIONS,
        transaction_version: 1,
    };
    pub const ReservedXcmpWeight: Weight = WEIGHT_PER_SECOND / 4;
    pub const ReservedDmpWeight: Weight = WEIGHT_PER_SECOND / 4;
}

// 实现parachain-system配置
impl pallet_parachain_system::Config for Runtime {
    type Event = Event;
    type OnSystemEvent = ();
    type SelfParaId = parachain_info::Module<Runtime>;
    type OutboundXcmpMessageSource = XcmpQueue;
    type DmpMessageHandler = DmpQueue;
    type ReservedDmpWeight = ReservedDmpWeight;
    type XcmpMessageHandler = XcmpQueue;
    type ReservedXcmpWeight = ReservedXcmpWeight;
}

// 实现parachain-info配置
impl parachain_info::Config for Runtime {}

// 构造runtime
construct_runtime!(
    pub enum Runtime where
        Block = Block,
        NodeBlock = opaque::Block,
        UncheckedExtrinsic = UncheckedExtrinsic,
    {
        System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
        Timestamp: pallet_timestamp::{Pallet, Call, Storage, Inherent},
        Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
        TransactionPayment: pallet_transaction_payment::{Pallet, Storage},
        ParachainSystem: cumulus_pallet_parachain_system::{Pallet, Call, Storage, Inherent, Event<T>},
        ParachainInfo: parachain_info::{Pallet, Storage, Config},
    }
);

通过正确配置和使用cumulus-pallet-parachain-system,开发者可以构建符合Polkadot标准的平行链,实现与中继链及其他平行链的安全通信。

回到顶部