Day 1 环境搭建与 Aeternity 基础
学习 Aeternity 核心特性,完成开发环境配置。
学习目标
- 理解 Aeternity 区块链的核心特性和应用场景
- 完成 Python 开发环境配置和 SDK 安装
- 掌握节点连接和基本查询操作
- 熟悉 SDK 的模块结构和文档使用
1. Aeternity 区块链简介
1.1 核心特性
| 特性 | 说明 |
|---|---|
| 混合共识 | Bitcoin-NG + Proof-of-Work,高吞吐量 |
| 状态通道 | 链下交易,即时确认,低手续费 |
| FATE 虚拟机 | Fast Aeternity Transaction Engine,高效执行 |
| Sophia 语言 | 函数式、强类型智能合约语言 |
| 原生预言机 | 链上预言机,获取链外数据 |
| AENS | Aeternity Naming System,人类可读域名 |
1.2 网络类型
┌─────────────────────────────────────────────────────────────┐
│ Aeternity 网络 │
├─────────────────┬─────────────────┬─────────────────────────┤
│ 主网 │ 测试网 │ 本地节点 │
│ (Mainnet) │ (Testnet) │ (Local) │
├─────────────────┼─────────────────┼─────────────────────────┤
│ ae_mainnet │ ae_uat │ ae_devnet │
│ 生产环境 │ 开发测试 │ 离线开发 │
│ 真实 AE 代币 │ 测试 AE(免费) │ 模拟环境 │
└─────────────────┴─────────────────┴─────────────────────────┘
1.3 区块结构
Aeternity 采用双层区块结构:
- 关键块 (Key Block): 包含 PoW 证明,约 3 分钟产生一个
- 微块 (Micro Block): 包含实际交易,快速生成
2. Python SDK 架构
2.1 模块分类
| 模块类别 | 模块名 | 功能说明 |
|---|---|---|
| 账户管理 | signing, wallet, hdwallet | 密钥生成、签名、助记词钱包 |
| 节点交互 | node, openapi | 节点客户端、API 封装 |
| 合约开发 | compiler, contract, contract_native | 编译、部署、调用 |
| 密码学 | crypto, hashing, message | 加密、哈希、消息签名 |
| 链上服务 | aens, oracles, channel | 域名、预言机、状态通道 |
| 交易构建 | transactions | 底层交易构建器 |
3. 实践任务
任务 1.1: 安装 SDK
# 检查 Python 版本
import sys
print(f"Python 版本: {sys.version}")
assert sys.version_info >= (3, 7), "需要 Python 3.7+"
# 安装 SDK(在命令行执行)
# pip install aeternity
# 验证安装
import aeternity
print(f"✅ Aeternity SDK 安装成功")
# 查看可用模块
modules = [m for m in dir(aeternity) if not m.startswith('_')]
print(f"可用模块: {modules}")
任务 1.2: 连接测试网节点
from aeternity.node import NodeClient, Config
# 创建测试网配置
config = Config(
external_url='https://testnet.aeternity.io',
network_id='ae_uat'
)
# 创建节点客户端
client = NodeClient(config)
# 获取节点状态
status = client.get_status()
print(f"节点版本: {status.node_version}")
print(f"网络 ID: {status.network_id}")
print(f"对等节点数: {status.peer_count}")
# 获取协议信息
for protocol in status.protocols:
print(f"协议版本 {protocol.version}, 生效高度: {protocol.effective_at_height}")
任务 1.3: 查询区块链状态
from aeternity.node import NodeClient, Config
client = NodeClient(Config(external_url='https://testnet.aeternity.io'))
# 1. 获取当前区块高度
height = client.get_current_key_block_height()
print(f"当前区块高度: {height}")
# 2. 获取顶部区块
top_block = client.get_top_block()
print(f"\n顶部区块:")
print(f" 哈希: {top_block.hash}")
print(f" 高度: {top_block.height}")
print(f" 时间: {top_block.time}")
# 3. 获取指定高度的关键块
if height > 0:
key_block = client.get_key_block_by_height(height=height)
print(f"\n关键块 {height}:")
print(f" 哈希: {key_block.hash}")
print(f" 前一块: {key_block.prev_key_hash}")
print(f" 矿工: {key_block.miner}")
任务 1.4: 验证编译器服务
from aeternity.compiler import CompilerClient
# 创建编译器客户端
compiler = CompilerClient(compiler_url='https://v8.compiler.aepps.com')
# 获取编译器版本
version = compiler.api_version()
print(f"编译器版本: {version}")
# 测试简单合约编译
simple_contract = '''
@compiler >= 6
contract HelloWorld =
entrypoint hello() : string = "Hello, Aeternity!"
entrypoint add(a : int, b : int) : int = a + b
'''
try:
# 编译合约
bytecode = compiler.compile(simple_contract)
print(f"\n✅ 编译成功!")
print(f"字节码长度: {len(bytecode)} 字符")
# 生成 ACI
aci = compiler.generate_aci(simple_contract)
print(f"\n合约接口 (ACI):")
for func in aci.get('contract', {}).get('functions', []):
print(f" - {func.get('name')}()")
except Exception as e:
print(f"❌ 编译失败: {e}")
任务 1.5: 查询账户信息
from aeternity.node import NodeClient, Config
from aeternity.utils import format_amount
client = NodeClient(Config(external_url='https://testnet.aeternity.io'))
# 使用一个已知的测试网账户
test_account = "ak_2swhLkgBPeeADxVTAVCJnZLY5NZtCFiM93JxsEaMuC59euuFRQ"
try:
# 获取账户信息
account = client.get_account(test_account)
print(f"账户信息:")
print(f" 地址: {account.address}")
print(f" 余额: {account.balance} aettos")
print(f" 余额: {format_amount(account.balance)}")
print(f" Nonce: {account.nonce}")
print(f" 类型: {account.kind}")
except Exception as e:
print(f"查询失败: {e}")
print("提示: 新账户在链上没有交易记录时会返回 404")
4. 常见问题
# 升级 pip
pip install --upgrade pip
# 使用国内镜像
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple aeternity
# 如果 pynacl 编译失败,先安装预编译版本
pip install pynacl
- 检查网络连接
- 尝试更换节点 URL
- 检查防火墙设置
知识检查点
完成 Day 1 后,你应该能够:
- 解释 Aeternity 的核心特性(FATE VM、Sophia、状态通道)
- 说明主网和测试网的区别
- 安装并验证 SDK
- 连接到节点并查询区块信息
- 使用编译器编译简单合约
本页目录
预计学习时间: 2-3 小时