Rust区块链共识引擎库pallet-aura的使用,实现高效权威证明(Aura)共识机制的Substrate运行时模块
Rust区块链共识引擎库pallet-aura的使用,实现高效权威证明(Aura)共识机制的Substrate运行时模块
Aura模块概述
Aura模块通过管理离线报告扩展了Aura共识机制。
接口
公共函数
slot_duration
- 根据Timestamp模块配置确定Aura的slot-duration
相关模块
- Timestamp: Timestamp模块在Aura中用于跟踪共识轮次(通过
slots
)
实现参考
如果您有兴趣修改此模块,需要理解与substrate/primitives/inherents/src/lib.rs
的交互,特别是需要实现ProvideInherent
和ProvideInherentData
来创建和检查内在数据。
许可证: Apache-2.0
完整示例代码
以下是一个完整的Substrate运行时模块示例,展示如何使用pallet-aura实现Aura共识机制:
// runtime/src/lib.rs
use frame_support::{
construct_runtime, parameter_types,
traits::{KeyOwnerProofSystem, Randomness},
weights::Weight,
};
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use sp_core::OpaqueMetadata;
use sp_runtime::{
create_runtime_str, generic, impl_opaque_keys,
traits::{BlakeTwo256, Block as BlockT, IdentifyAccount, Verify},
MultiSignature, Perbill,
};
// 引入Aura pallet
pub use pallet_aura;
// 配置Aura pallet
impl pallet_aura::Config for Runtime {
type AuthorityId = AuraId;
type DisabledValidators = ();
type MaxAuthorities = MaxAuthorities;
}
parameter_types! {
pub const BlockHashCount: BlockNumber = 2400;
pub const Version: RuntimeVersion = VERSION;
pub RuntimeBlockLength: BlockLength =
BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO);
pub RuntimeBlockWeights: BlockWeights = BlockWeights::with_sensible_defaults(
2 * WEIGHT_PER_SECOND,
NORMAL_DISPATCH_RATIO,
);
pub const MaxAuthorities: u32 = 32;
}
construct_runtime!(
pub enum Runtime where
Block = Block,
NodeBlock = opaque::Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
// 其他pallet...
Aura: pallet_aura::{Pallet, Config<T>},
// 其他pallet...
}
);
// 实现Aura相关的运行时API
impl_runtime_apis! {
impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {
fn slot_duration() -> sp_consensus_aura::SlotDuration {
// 通常设置为6秒
sp_consensus_aura::SlotDuration::from_millis(6000)
}
fn authorities() -> Vec<AuraId> {
// 从Aura pallet获取当前验证人列表
Aura::authorities().into_inner()
}
}
}
Cargo.toml 配置
[dependencies]
pallet-aura = { version = "41.0.0", default-features = false }
[features]
default = ["std"]
std = [
"pallet-aura/std",
# 其他依赖的std特性
]
这个示例展示了如何在Substrate运行时中集成pallet-aura,配置Aura共识机制,并实现必要的运行时API。关键点包括:
- 为pallet-aura实现Config trait
- 在construct_runtime!宏中包含Aura pallet
- 实现AuraApi提供slot_duration和authorities函数
- 在Cargo.toml中添加pallet-aura依赖
Aura共识机制适合需要高效确定性共识的场景,通过轮流产生区块实现高吞吐量。
1 回复
Rust区块链共识引擎库pallet-aura的使用指南
以下是基于您提供的完整内容整理的pallet-aura使用指南,包含所有示例代码和说明:
概述
pallet-aura
是Substrate框架中实现权威证明(Aura)共识机制的运行时模块。Aura(Authority Round)是一种简单的基于轮次的区块链共识算法,它预先确定一组权威节点,这些节点按固定顺序轮流产生区块。
主要特点
- 确定性区块生产:权威节点按固定顺序轮流出块
- 高效性:相比工作量证明(PoW)更节能高效
- 低延迟:固定时间间隔出块
- 适合私有链或联盟链场景
完整示例代码
1. 添加依赖示例
# runtime/Cargo.toml
[dependencies.pallet-aura]
default-features = false
git = "https://github.com/paritytech/substrate.git"
branch = "polkadot-v0.9.28"
version = "4.0.0-dev"
2. 完整Runtime配置示例
// runtime/src/lib.rs
// 1. 引入所需类型
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
use frame_support::parameter_types;
// 2. 配置Aura
parameter_types! {
pub const MaxAuthorities: u32 = 32;
}
impl pallet_aura::Config for Runtime {
type AuthorityId = AuraId; // 使用sr25519加密的权威ID
type DisabledValidators = (); // 禁用验证器列表,空元组表示不跟踪
type MaxAuthorities = MaxAuthorities; // 最大权威节点数
}
// 3. 添加到运行时
construct_runtime!(
pub struct Runtime where
Block = Block,
NodeBlock = opaque::Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
System: frame_system,
// ...其他pallet
Aura: pallet_aura, // 添加Aura pallet
// ...其他pallet
}
);
3. 完整创世配置示例
// node/src/chain_spec.rs
use hex_literal::hex;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
/// 测试网创世配置
pub fn testnet_genesis() -> GenesisConfig {
GenesisConfig {
system: Default::default(),
aura: AuraConfig {
authorities: vec![
// Alice的Aura ID (预定义的开发账户)
hex!("d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d").into(),
// Bob的Aura ID
hex!("8eaf04151687736326c9fea17e25fc5287613693c912909cb226aa4794f26a48").into(),
],
},
// ...其他配置
}
}
4. 自定义Aura实现完整示例
// runtime/src/lib.rs
use sp_consensus_aura::{AuraApi, SlotDuration};
use sp_api::impl_runtime_apis;
#[impl_runtime_apis]
impl AuraApi<Block> for Runtime {
/// 获取每个slot的持续时间(毫秒)
fn slot_duration() -> SlotDuration {
// 设置为1秒一个区块
SlotDuration::from_millis(1000)
}
/// 获取当前权威节点列表
fn authorities() -> Vec<AuraId> {
// 从存储中获取权威节点并转换为Vec
Aura::authorities().to_vec()
}
}
5. 与BABE结合的完整示例
// runtime/src/lib.rs
parameter_types! {
pub const EpochDuration: u64 = 10; // 每个epoch的区块数
pub const ExpectedBlockTime: u64 = 1000; // 预期出块时间(毫秒)
}
impl pallet_babe::Config for Runtime {
type EpochDuration = EpochDuration;
type ExpectedBlockTime = ExpectedBlockTime;
type EpochChangeTrigger = pallet_babe::SameAuthoritiesForever;
// ...其他BABE配置
}
impl pallet_aura::Config for Runtime {
type AuthorityId = AuraId;
type DisabledValidators = ();
type MaxAuthorities = ConstU32<32>;
}
实际应用建议
- 权威节点管理:在生产环境中实现权威节点轮换机制
// 示例:权威节点变更回调
impl pallet_aura::Config for Runtime {
// ...其他配置
type OnNewAuthority = AuthorityChangeHandler;
}
struct AuthorityChangeHandler;
impl OnNewAuthority<AuraId> for AuthorityChangeHandler {
fn on_new_authorities(authorities: &[AuraId]) {
log::info!("Authority set changed: {:?}", authorities);
}
}
- 性能优化配置:根据网络规模调整参数
parameter_types! {
pub const MaxAuthorities: u32 = 100; // 适用于大型联盟链
}
impl pallet_aura::Config for Runtime {
type MaxAuthorities = MaxAuthorities;
// ...其他配置
}
- 安全建议:实现HSM集成
// 示例:使用安全签名机制
struct SecureSigner;
impl AuraAuthoritySignature<AuraId> for SecureSigner {
fn sign_aura_authority(data: &[u8], key: &AuraId) -> Option<Signature> {
// 调用HSM安全签名
hsm::sign_with_key(key, data)
}
}
注意事项
- Aura适合信任环境(如私有链或联盟链),因为权威节点是预先确定的
- 在生产环境中应考虑与GRANDPA等最终确定性工具结合使用
- 权威节点私钥需要妥善保管,建议使用HSM等安全设备
- 可以通过on_set_authorities钩子实现权威节点动态变更
pallet-aura
为Substrate区块链提供了简单高效的共识机制实现,特别适合需要确定性和高性能的区块链应用场景。