L6
Hyperchain 超链
架设与技术解析 (基于 Ceres 协议 v6.x)
概述
Hyperchains 是 Aeternity 的二层/侧链扩容方案,允许开发者创建拥有独立共识但通过锚定(Pinning)机制共享父链安全性的区块链。
继承安全性
通过定期锚定到父链(AE/BTC),获得父链级别的安全保障
自定义参数
自定义出块时间、Gas 限制、验证者集合等
高性能
独立运行的子链,不受主链拥堵影响
技术架构
核心角色
Parent Chain (PC)
父链 - 提供安全性的 Aeternity 网络(Mainnet/Testnet)
Child Chain (CC)
子链 - 你的 Hyperchain,运行独立的共识
Staker/Pinner
负责在子链出块并将哈希提交到父链
核心模块
| 模块 | 功能 | 说明 |
|---|---|---|
aec_consensus_hc |
共识核心 | Epoch 机制、Leader 选举、熵源获取、最终性判断 |
aec_pinning_agent |
锚定代理 | GenServer 后台进程,自动化锚定流程 |
aec_parent_connector |
父链连接器 | 适配器模式,支持 AE/BTC 等多链 |
aec_parent_chain_cache |
父链缓存 | 缓存父链区块状态 |
系统合约
MainStaking.aes
管理验证者质押
HCElection.aes
基于父链熵的选举
DelegatedStaking.aes
委托质押 (可选)
Pinning 锚定流程
Epoch 结束
Leader 出块
Pin 到父链
父链确认
Proof 提交
流程详解
- Epoch 结束:子链达到 Epoch 预定高度
- Leader 动作:EOE Leader 生成 Epoch 结束区块
- Pin 提交:Agent 检测到新 Epoch,提交锚定数据到父链
- 父链确认:等待父链出块确认交易
- Proof 发布:在子链发布
ChildChainPinTx,包含父链 Merkle Proof
Pinning Payload 结构
{
CC_ID, // 子链标识
Epoch, // Epoch 编号
Height, // 区块高度
BlockHash // 区块哈希
}
编码函数: aeser_hc:encode_parent_pin_payload/1
搭建指南 (AE2AE 模式)
前置条件:
Erlang/OTP 24+ | Docker (推荐) | 可访问的父链节点 | 父链账户 (用于 Pinning Gas)
准备系统合约
将以下合约编译为字节码,用于创世块部署:
MainStaking.aes- 验证者质押管理HCElection.aes- 选举逻辑DelegatedStaking.aes- 委托质押 (可选)
生成创世文件
accounts.json
预定义账户余额
contracts.json
初始合约状态与字节码
// contracts.json 结构示例
[
{
"contract_pubkey": "ct_Staking...",
"code": "cb_...",
"call_data": "cb_..." // MainStaking.init(...)
},
{
"contract_pubkey": "ct_Election...",
"code": "cb_...",
"call_data": "cb_..." // HCElection.init(...)
}
]
节点配置 (aeternity.yaml)
chain:
persist: true
hard_forks:
'6': # Hyperchain 协议版本
height: 0
accounts_file: "data/accounts.json"
contracts_file: "data/contracts.json"
consensus:
'0':
type: hyperchain
config:
contract_owner: "ak_11111111111111111111111111111115rHyByZ"
staking_contract: "ct_StakingContractAddress..."
election_contract: "ct_ElectionContractAddress..."
rewards_contract: "ct_StakingContractAddress..."
child_block_time: 3000 # 子链出块时间 (ms)
child_epoch_length: 100 # Epoch 长度 (区块数)
# 父链配置 (AE2AE)
parent_chain:
consensus:
type: AE2AE
network_id: ae_uat # 父链网络ID
polling:
fetch_interval: 1000
nodes:
- "https://testnet.aeternity.io"
start_height: 1000000 # 起始锚定高度
parent_epoch_length: 10
# Pinner 配置
default_pinning_behavior: true
pinners:
- parent_chain_account:
owner: "ak_ChildChainStakerAddress..."
pub: "ak_ParentChainPinnerAddress..."
priv: "ParentChainPinnerPrivateKey..."
mining:
autostart: true
beneficiary: "ak_BeneficiaryAddress..."
fork_management:
network_id: "hc_my_custom_net"
启动与验证
# 编译并启动
./rebar3 as prod release
./_build/prod/rel/aeternity/bin/aeternity console
# 验证状态
curl http://localhost:3013/v3/status
成功日志:
Pinning ... 或 Parent chain height ...
失败日志:
Failed to post to parent chain
关键参数说明
| 参数 | 说明 | 典型值 |
|---|---|---|
child_block_time | 子链出块时间 | 3000 ms |
child_epoch_length | Epoch 长度(区块数) | 100 |
parent_epoch_length | 父链 Epoch 长度 | 10 |
start_height | 父链起始锚定高度 | 取决于父链 |
fetch_interval | 父链轮询间隔 | 1000 ms |
常见问题
检查
contracts.json 是否正确注册了验证者。如果当前 Epoch 没有合法的 Leader,链将暂停。
- 确认 Pinner 账户在父链上有足够的 Gas 费
- 确认
parent_chain.start_height设置正确,不要设置得太久远 - 检查父链节点 URL 是否可访问
在父链浏览器上查询 Pinner 地址,查看是否有发出的交易。交易的 Payload 应包含子链的哈希。
优化建议
可观测性
引入 Prometheus Metrics:
hc_pinning_success_totalhc_pinning_failure_totalhc_pinning_latency_seconds
重组处理
增加专门的 ReorgManager 模块,模拟父链重组事件并测试子链响应
模块化适配器
定义 aec_parent_chain_behaviour,允许动态加载第三方父链适配器
CLI 工具
开发 ae-hc-cli 用于手动触发 Pinning、查询状态、解码 Payload