Aleo 开发实战:从环境搭建到隐私合约上链的全过程
Table of Contents
Aleo 开发实战:从环境搭建到隐私合约上链的全过程
在传统公链生态中,“透明即公开”的底层逻辑虽然保证了去中心化,但也带来了严重的数据隐私痛点。Aleo 作为一个定位为 “ZK by Design” 的 Layer 1 协议,为这个问题交出了一份硬核答卷——它是全球首个默认隐私、可编程且无需许可的去中心化应用平台。
如果你对零知识证明(ZKP)感兴趣,却苦于不知如何落地代码,这篇文章将为你彻底打通脉络。我们将从零开始,手把手带你完成 Aleo 环境搭建、Leo 语言编译构建、本地节点沙盒测试,直到将你的第一个隐私合约成功部署到 Aleo 测试网,带你亲历一次端到端的零知识计算开发之旅。
本文完整记录了 Aleo 隐私公链的本地开发与测试网上链流程。涵盖 snarkOS 与 Leo 语言的环境搭建、项目初始化、合约编译与本地沙盒部署,并重点分享了如何通过优化程序名称规避高额“命名费”的实战避坑技巧,助你快速跑通 Aleo 零知识证明智能合约开发。
Aleo 作为一个定位为“ZK by Design“的 Layer 1 协议,是全球首个默认隐私、可编程、高性能的 Layer1 区块链,专注解决传统公链“透明即公开“的底层缺陷,通过零知识密码学为 Web3 应用提供端到端数据隐私保护,同时保留智能合约的可编程性与区块链的去中心化特性。
Aleo 是首个提供无需许可、可编程、隐私保护的去中心化应用平台。
Aleo 原生即零知识,为去中心化计算创建了一种全新的模式。
通过Aleo,用户可以在网上运行零知识应用程序,开源、去中心化且无需许可
| 开发者 | 证明者 | 验证者 |
|---|---|---|
| Leo 一种专门用于零知识电路的函数式编程领域特定语言(DSL) | snarkVM 一个用于零知识执行的分布式虚拟机 | snarkOS 一个用于零知识应用程序的去中心化操作系统 |
Leo
由 ACL2 进行形式化验证 (Formally verified by ACL2)
语法类似于 Rust
通过 Aleo Instruction 编译为 R1CS
leo
- 首个经过形式化验证的编程语言,用于编写可编译为 R1CS 的程序以创建零知识证明。
- 将包含测试框架、导入解析器、远程编译器和定理证明器,以提供最佳的开发者体验。
- 函数式、静态类型语言,语法类似 Rust。
- 易于使用、阅读和理解。
Aleo Instruction
- Aleo Instruction 代表了 R1CS 与 Leo 之间的中间表示 (IR),语法类似 Assembly。
- Leo 编译器将高层逻辑转换为 Aleo Instruction。
- 链上注册中心的应用将以此格式(Aleo Instruction)存储。
- 通过 Aleo Instruction 操作码 + 通用参考串(CRS),证明者与验证者可以合成电路并生成/验证证明。
Aleo 零知识编程的两层语言结构:
- Leo:面向开发者的高级语言(类似 Rust)
- Aleo Instruction:中间表示层(类似汇编),最终上链存储
实操
安装 Leo
cargo install leo-lang
查看版本信息
leo --version
leo 4.0.2 (unknown unknown) features=[]
安装 snarkOS
git clone --branch mainnet --single-branch https://github.com/ProvableHQ/snarkOS.git
cd snarkOS
cargo install --locked --path .
进入 snarkOS 目录并重新编译
cargo build --release --features test_network
snarkOS on mainnet is 📦 snarkos@4.6.3 via 🦀 1.88.0
➜ /Users/qiaopengjun/Code/Aleo/snarkOS/target/release/snarkos --version
snarkos refs/heads/mainnet 187f9830b6491b640fa3fa9832658f4de6665f08 features=[default,snarkos_node_metrics,test_network]
这会重新编译一个带 test_network 特性的版本
验证安装
snarkos -h
Command-line interface for snarkOS
Usage: snarkos [OPTIONS] <COMMAND>
Commands:
account Commands to manage Aleo accounts
clean Cleans the snarkOS node storage
developer Commands to deploy and execute transactions
start
update Update snarkOS
help Print this message or the help of the given subcommand(s)
Options:
--noupdater Disable checking for new versions at startup
-h, --help Print help
-V, --version Print version
snarkos -V
snarkos refs/heads/mainnet 187f9830b6491b640fa3fa9832658f4de6665f08 features=[default,snarkos_node_metrics]
snarkos --version
snarkos refs/heads/mainnet 187f9830b6491b640fa3fa9832658f4de6665f08 features=[default,snarkos_node_metrics]
创建项目
leo new hello
Created program hello at `/Users/qiaopengjun/Code/Aleo/hello`.
cd hello
查看项目目录结构
Code/Aleo/hello
➜ tree . -L 6 -I ".gitignore|.github|.git|target|node_modules|data"
.
├── program.json
├── src
│ └── main.leo
└── tests
└── test_hello.leo
3 directories, 3 files
main.leo 文件
// The 'hello' program.
program hello.aleo {
// This is the constructor for the program.
// The constructor allows you to manage program upgrades.
// It is called when the program is deployed or upgraded.
// It is currently configured to **prevent** upgrades.
// Other configurations include:
// - @admin(address="aleo1...")
// - @checksum(mapping="credits.aleo/fixme", key="0field")
// - @custom
// For more information, please refer to the documentation: `https://docs.leo-lang.org/guides/upgradability`
@noupgrade
constructor() {}
fn main(public a: u32, b: u32) -> u32 {
let c: u32 = a + b;
return c;
}
}
这是一个 Aleo Leo 语言 的 hello 程序。让我为您解释:
结构概览:
program hello.aleo { }— 定义了一个名为hello的程序
构造函数部分:
@noupgrade— 装饰器,禁止该程序升级constructor() {}— 空的构造函数,在程序部署或升级时调用
主函数:
fn main(public a: u32, b: u32) -> u32 {
let c: u32 = a + b;
return c;
}
public a— 公开参数(在区块链上可见)b— 私有参数(仅证明者知道)- 返回
u32(32位无符号整数) - 功能:将
a和b相加,返回结果
这是一个在 Aleo 零知识证明平台上运行的简单加法程序。
编译构建
Code/Aleo/hello
➜ leo build
⚠️ No network specified, defaulting to 'testnet'.
⚠️ No endpoint specified, defaulting to 'https://api.explorer.provable.com/v1'.
Leo 🔨 Compiling 'hello.aleo'
Leo 2 statements before dead code elimination.
Leo 2 statements after dead code elimination.
Leo The program checksum is: '[212u8, 91u8, 180u8, 186u8, 11u8, 135u8, 133u8, 41u8, 42u8, 76u8, 102u8, 23u8, 196u8, 194u8, 100u8, 73u8, 111u8, 221u8, 183u8, 142u8, 23u8, 199u8, 228u8, 153u8, 158u8, 48u8, 97u8, 213u8, 199u8, 148u8, 74u8, 107u8]'.
Leo Program size: 0.18 KB / 500.00 KB
Leo ✅ Compiled 'hello.aleo' into Aleo instructions.
Leo ✅ Generated ABI at 'build/abi.json'.
运行
Code/Aleo/hello
➜ leo run main 1u32 2u32
⚠️ No network specified, defaulting to 'testnet'.
⚠️ No endpoint specified, defaulting to 'https://api.explorer.provable.com/v1'.
Leo 🔨 Compiling 'hello.aleo'
Leo 2 statements before dead code elimination.
Leo 2 statements after dead code elimination.
Leo The program checksum is: '[212u8, 91u8, 180u8, 186u8, 11u8, 135u8, 133u8, 41u8, 42u8, 76u8, 102u8, 23u8, 196u8, 194u8, 100u8, 73u8, 111u8, 221u8, 183u8, 142u8, 23u8, 199u8, 228u8, 153u8, 158u8, 48u8, 97u8, 213u8, 199u8, 148u8, 74u8, 107u8]'.
Leo Program size: 0.18 KB / 500.00 KB
Leo ✅ Compiled 'hello.aleo' into Aleo instructions.
Leo ✅ Generated ABI at 'build/abi.json'.
⚠️ No network specified, defaulting to 'testnet'.
⚠️ No valid private key specified, defaulting to 'APrivateKey1zkp8CZNn3yeCseEtxuVPbDCwSyhGW6yZKUYKfgXmcpoGPWH'.
➕Adding programs to the VM in the following order:
- hello.aleo (local)
➡️ Output
• 3u32
测试
Code/Aleo/hello
➜ leo test
⚠️ No network specified, defaulting to 'testnet'.
⚠️ No endpoint specified, defaulting to 'https://api.explorer.provable.com/v1'.
Leo 🔨 Compiling 'hello.aleo'
Leo 2 statements before dead code elimination.
Leo 2 statements after dead code elimination.
Leo The program checksum is: '[212u8, 91u8, 180u8, 186u8, 11u8, 135u8, 133u8, 41u8, 42u8, 76u8, 102u8, 23u8, 196u8, 194u8, 100u8, 73u8, 111u8, 221u8, 183u8, 142u8, 23u8, 199u8, 228u8, 153u8, 158u8, 48u8, 97u8, 213u8, 199u8, 148u8, 74u8, 107u8]'.
Leo Program size: 0.18 KB / 500.00 KB
Leo ✅ Compiled 'hello.aleo' into Aleo instructions.
Leo ✅ Generated ABI at 'build/abi.json'.
Leo 🔨 Compiling 'test_hello.aleo'
Leo 6 statements before dead code elimination.
Leo 6 statements after dead code elimination.
Leo The program checksum is: '[189u8, 163u8, 254u8, 213u8, 77u8, 14u8, 245u8, 80u8, 169u8, 64u8, 55u8, 10u8, 208u8, 239u8, 144u8, 63u8, 100u8, 115u8, 255u8, 30u8, 207u8, 43u8, 158u8, 34u8, 237u8, 2u8, 161u8, 145u8, 194u8, 154u8, 33u8, 129u8]'.
Leo Program size: 0.18 KB / 500.00 KB
Leo ✅ Compiled 'test_hello.aleo' into Aleo instructions.
Leo Import 'hello.aleo': checksum = '[212u8, 91u8, 180u8, 186u8, 11u8, 135u8, 133u8, 41u8, 42u8, 76u8, 102u8, 23u8, 196u8, 194u8, 100u8, 73u8, 111u8, 221u8, 183u8, 142u8, 23u8, 199u8, 228u8, 153u8, 158u8, 48u8, 97u8, 213u8, 199u8, 148u8, 74u8, 107u8]'
Leo Loading the ledger from storage...
1 / 1 tests passed.
PASSED: test_hello.aleo/test_main_fails
本地部署
终端 1:启动 devnet
Code/Aleo/hello took 2m 41.5s
➜ leo devnet --snarkos /Users/qiaopengjun/Code/Aleo/snarkOS/target/release/snarkos
📢 Loading environment variables from a `.env` file in the directory tree.
- NETWORK=testnet
- PRIVATE_KEY=xxxxxxxxxxx
- ENDPOINT=http://localhost:3034
🔧 Starting devnet with the following options:
• Network: testnet
• Validators: 4
• Clients: 2
• Storage: ./
• Using snarkOS binary at: /Users/qiaopengjun/Code/Aleo/snarkOS/target/release/snarkos
• Consensus heights: default (based on your snarkOS binary)
• Clear storage: no
• Verbosity: 1
• tmux: no
🔍 Detected: snarkos refs/heads/mainnet 187f9830b6491b640fa3fa9832658f4de6665f08 features=[default,snarkos_node_metrics]
?
✔
Proceed with devnet startup? · yes
⚙️ Spawning nodes as background tasks …
• validator 0 (pid = 97795)
• validator 1 (pid = 97796)
• validator 2 (pid = 97797)
• validator 3 (pid = 97798)
• client 0 (pid = 97799)
• client 1 (pid = 97800)
📌 Main process ID: 97785
Devnet running – Ctrl+C, SIGTERM, or terminal close to stop.
🎉 完美!本地 devnet 成功启动了!
现在可以看到:
- ✅ 4 个验证节点
- ✅ 2 个客户端节点
- ✅ 都在后台运行
保持这个终端运行,不要关闭。
终端 2:部署程序(新终端窗口)
第一次部署失败
Code/Aleo/hello
➜ leo deploy --devnet --broadcast
📢 Loading environment variables from a `.env` file in the directory tree.
- NETWORK=testnet
- PRIVATE_KEY=xxxxxxxxxxx
- ENDPOINT=http://localhost:3034
Leo 🔨 Compiling 'hello.aleo'
Leo 2 statements before dead code elimination.
Leo 2 statements after dead code elimination.
Leo The program checksum is: '[212u8, 91u8, 180u8, 186u8, 11u8, 135u8, 133u8, 41u8, 42u8, 76u8, 102u8, 23u8, 196u8, 194u8, 100u8, 73u8, 111u8, 221u8, 183u8, 142u8, 23u8, 199u8, 228u8, 153u8, 158u8, 48u8, 97u8, 213u8, 199u8, 148u8, 74u8, 107u8]'.
Leo Program size: 0.18 KB / 500.00 KB
Leo ✅ Compiled 'hello.aleo' into Aleo instructions.
Leo ✅ Generated ABI at 'build/abi.json'.
📢 Using the following consensus heights: 0,5,6,7,8,9,10,11,12,13,14,15,16,17
To override, pass in `--consensus-heights` or override the environment variable `CONSENSUS_VERSION_HEIGHTS`.
Attempting to determine the consensus version from the latest block height at http://localhost:3034...
🛠️ Deployment Plan Summary
──────────────────────────────────────────────
🔧 Configuration:
Private Key: APrivateKey1zkpGR9X1koAE...
Address: aleo1cu0xk4tt99pgxglpqlt...
Endpoint: http://localhost:3034
Network: testnet
Consensus Version: 1
📦 Deployment Tasks:
• hello.aleo │ priority fee: 0 │ fee record: no (public fee)
⚙️ Actions:
• Transaction(s) will NOT be printed to the console.
• Transaction(s) will NOT be saved to a file.
• Transaction(s) will be broadcast to http://localhost:3034
⚠️ Warnings:
• The program 'hello.aleo' uses V9 features but the consensus version is less than V9. The deployment will likely fail
──────────────────────────────────────────────
✔ Do you want to proceed with deployment? · yes
🔧 Your program 'hello.aleo' has the following constructor.
──────────────────────────────────────────────
constructor:
assert.eq edition 0u16;
──────────────────────────────────────────────
Once it is deployed, it CANNOT be changed.
✔ Would you like to proceed? · yes
📦 Creating deployment transaction for 'hello.aleo'...
Execution-cost computation for authorization relies on proof-size estimation, which is only implemented for Varuna version >= V2 (consensus version >= V4)
原因:你的程序需要 V9 特性,但本地 devnet 的 consensus version 是 V1,而且 V1 不支持估算执行成本。要支持你的程序,至少需要 V4 或更高版本。
第二次部署失败
Code/Aleo/hello took 1m 15.3s
➜ leo deploy --devnet --broadcast --consensus-version 9 --yes
📢 Loading environment variables from a `.env` file in the directory tree.
- NETWORK=testnet
- PRIVATE_KEY=xxxxxxxxxxx
- ENDPOINT=http://localhost:3034
Leo 🔨 Compiling 'hello.aleo'
Leo 2 statements before dead code elimination.
Leo 2 statements after dead code elimination.
Leo The program checksum is: '[212u8, 91u8, 180u8, 186u8, 11u8, 135u8, 133u8, 41u8, 42u8, 76u8, 102u8, 23u8, 196u8, 194u8, 100u8, 73u8, 111u8, 221u8, 183u8, 142u8, 23u8, 199u8, 228u8, 153u8, 158u8, 48u8, 97u8, 213u8, 199u8, 148u8, 74u8, 107u8]'.
Leo Program size: 0.18 KB / 500.00 KB
Leo ✅ Compiled 'hello.aleo' into Aleo instructions.
Leo ✅ Generated ABI at 'build/abi.json'.
📢 Using the following consensus heights: 0,5,6,7,8,9,10,11,12,13,14,15,16,17
To override, pass in `--consensus-heights` or override the environment variable `CONSENSUS_VERSION_HEIGHTS`.
⚠️ Warning: Expected consensus version 9 but found 1 at http://localhost:3034
🛠️ Deployment Plan Summary
──────────────────────────────────────────────
🔧 Configuration:
Private Key: APrivateKey1zkpGR9X1koAE...
Address: aleo1cu0xk4tt99pgxglpqlt...
Endpoint: http://localhost:3034
Network: testnet
Consensus Version: 9
📦 Deployment Tasks:
• hello.aleo │ priority fee: 0 │ fee record: no (public fee)
⚙️ Actions:
• Transaction(s) will NOT be printed to the console.
• Transaction(s) will NOT be saved to a file.
• Transaction(s) will be broadcast to http://localhost:3034
⚠️ Warnings:
• Expected consensus version 9 but found 1 at http://localhost:3034. In some cases, the deployment may fail
──────────────────────────────────────────────
🔧 Your program 'hello.aleo' has the following constructor.
──────────────────────────────────────────────
constructor:
assert.eq edition 0u16;
──────────────────────────────────────────────
Once it is deployed, it CANNOT be changed.
📦 Creating deployment transaction for 'hello.aleo'...
📊 Deployment Summary for hello.aleo
──────────────────────────────────────────────
Program Size: 0.18 KB / 500.00 KB
Total Variables: 16,787
Total Constraints: 12,927
Max Variables: 2,097,152
Max Constraints: 2,097,152
💰 Cost Breakdown (credits)
Transaction Storage: 0.810000
Program Synthesis: 0.742850
Namespace: 100000.000000
Constructor: 0.050000
Priority Fee: 0.000000
Total Fee: 100001.602850
Function 'main'
Total Execution Cost: 0.001316
|- Storage Cost: 0.001316
|- Finalize Cost: 0.000000
──────────────────────────────────────────────
📡 Broadcasting deployment for hello.aleo...
Error [ECLI0377041]: Invalid public balance for account: aleo1cu0xk4tt99pgxglpqltzk3tmpgh7qftjwukxcmewzpy0fkqghvgsxu0g03
|
= Make sure the account has enough balance to pay for the deployment.
你的账户在本地 devnet 上没有足够的余额来支付部署费用(需要 100,001.60 积分)。
第三次部署
第一步:彻底清理环境
在重新开始前,必须杀掉所有可能占用端口的进程:
# 杀掉所有 snarkos 进程
pkill -9 snarkos
第二步:启动本地节点 (使用 leo devnode)
- 设置共识高度环境变量(确保
constructor语法能被识别)
export CONSENSUS_VERSION_HEIGHTS="0,1,2,3,4,5,6,7,8,9,10,11,12,13"
- 启动节点
leo devnode start

注意:请保持这个窗口不要关闭,观察日志直到看到 add block (height 1, round 1) 以上。
第三步:配置项目环境
进入你的 hello 项目目录,修改 .env 文件。
NETWORK=testnet
PRIVATE_KEY=APrivateKey1zkp8CZNn3yeCseEtxuVPbDCwSyhGW6yZKUYKfgXmcpoGPWH # 非常重要
ENDPOINT=http://localhost:3034
第四步:执行部署命令
Code/Aleo/hello
➜ leo deploy --devnet \
--endpoint http://127.0.0.1:3030 \
--broadcast \
--consensus-version 9 \
--consensus-heights 0,1,2,3,4,5,6,7,8,9,10,11,12,13 \
--yes
📢 Loading environment variables from a `.env` file in the directory tree.
- NETWORK=testnet
- PRIVATE_KEY=APrivateKey1zkp8CZNn3yeCseEtxuVPbDCwSyhGW6yZKUYKfgXmcpoGPWH
- ENDPOINT=http://localhost:3034
Leo 🔨 Compiling 'hello.aleo'
Leo 2 statements before dead code elimination.
Leo 2 statements after dead code elimination.
Leo The program checksum is: '[212u8, 91u8, 180u8, 186u8, 11u8, 135u8, 133u8, 41u8, 42u8, 76u8, 102u8, 23u8, 196u8, 194u8, 100u8, 73u8, 111u8, 221u8, 183u8, 142u8, 23u8, 199u8, 228u8, 153u8, 158u8, 48u8, 97u8, 213u8, 199u8, 148u8, 74u8, 107u8]'.
Leo Program size: 0.18 KB / 500.00 KB
Leo ✅ Compiled 'hello.aleo' into Aleo instructions.
Leo ✅ Generated ABI at 'build/abi.json'.
📢 Using the following consensus heights: 0,1,2,3,4,5,6,7,8,9,10,11,12,13
To override, pass in `--consensus-heights` or override the environment variable `CONSENSUS_VERSION_HEIGHTS`.
⚠️ Warning: Expected consensus version 9 but found 14 at http://127.0.0.1:3030
🛠️ Deployment Plan Summary
──────────────────────────────────────────────
🔧 Configuration:
Private Key: APrivateKey1zkp8CZNn3yeC...
Address: aleo1rhgdu77hgyqd3xjj8uc...
Endpoint: http://127.0.0.1:3030
Network: testnet
Consensus Version: 9
📦 Deployment Tasks:
• hello.aleo │ priority fee: 0 │ fee record: no (public fee)
⚙️ Actions:
• Transaction(s) will NOT be printed to the console.
• Transaction(s) will NOT be saved to a file.
• Transaction(s) will be broadcast to http://127.0.0.1:3030
⚠️ Warnings:
• Expected consensus version 9 but found 14 at http://127.0.0.1:3030. In some cases, the deployment may fail
──────────────────────────────────────────────
🔧 Your program 'hello.aleo' has the following constructor.
──────────────────────────────────────────────
constructor:
assert.eq edition 0u16;
──────────────────────────────────────────────
Once it is deployed, it CANNOT be changed.
📦 Creating deployment transaction for 'hello.aleo'...
📊 Deployment Summary for hello.aleo
──────────────────────────────────────────────
Program Size: 0.18 KB / 500.00 KB
Total Variables: 16,787
Total Constraints: 12,927
Max Variables: 2,097,152
Max Constraints: 2,097,152
💰 Cost Breakdown (credits)
Transaction Storage: 0.874000
Program Synthesis: 0.742850
Namespace: 100000.000000
Constructor: 0.050000
Priority Fee: 0.000000
Total Fee: 100001.666850
Function 'main'
Total Execution Cost: 0.001316
|- Storage Cost: 0.001316
|- Finalize Cost: 0.000000
──────────────────────────────────────────────
📡 Broadcasting deployment for hello.aleo...
💰Your current public balance is 9374999.894112 credits.
✉️ Broadcasted transaction with:
- transaction ID: 'at1ta5adsclhrp4zn9s98lxsnlgldm09t3kwdj8rugevumndf2kcugss2qfdz'
- fee ID: 'au1fgmpxw5yzc0csdd3q9p7596ynm0khw72hr5f20xrsc2km52vfsqs9eus9x'
- fee transaction ID: 'at1n89lu9shr9ertcy6jpjeec3q57pts9p0a64zaj73hqjmmjkv85zsjdds37'
(use this to check for rejected transactions)
🔄 Searching up to 12 blocks to confirm transaction (this may take several seconds)...
Explored 1 blocks.
Transaction accepted.
✅ Deployment confirmed!
以下是对日志中各个关键部分的详细解读:
1. 编译与检查 (Compilation)
- Program checksum: 这是你合约代码的哈希值。如果代码有任何变动,这个校验和就会改变。
- Dead code elimination: Leo 编译器非常智能,它会自动删掉代码中没用的部分以节省空间(你的代码只有 2 个语句,非常精简)。
- Program size: 0.18 KB。Aleo 对合约大小有限制(500 KB),你的合约非常小,运行效率会很高。
2. 共识版本警告 (Consensus Version Warning)
⚠️ Warning: Expected consensus version 9 but found 14
- 含义:你在命令中指定了
--consensus-version 9(因为你的代码里有constructor语法),但此时你的本地节点由于一直在运行,高度已经增加,系统自动步进到了版本 14。 - 结果:由于 V14 兼容 V9 的功能,所以这次部署虽然有警告,但依然成功了。
3. 费用清单 (Cost Breakdown)
Aleo 的部署成本由几部分组成:
- Namespace (100,000 credits): 这是最贵的一项。在 Aleo 中,注册一个新的合约名称(Namespace)需要支付一笔固定的巨额费用。这是为了防止恶意用户在链上抢注大量的合约名。
- Transaction Storage & Synthesis: 存储和生成证明的费用,与合约复杂度成正比。
- Total Fee (100,001.66+): 部署这个合约总共花掉了大约 10 万个积分。
4. 广播与确认 (Broadcast & Confirmation)
- Transaction ID:
'at1ta5a...'。这是你这笔部署交易的唯一身份证。你可以用它在本地节点的 API 中查询详细数据。 - Broadcasting: Leo 将签名后的交易发送到了
[http://127.0.0.1:3030](http://127.0.0.1:3030)。 - Transaction accepted: 节点已经将这笔交易打包进区块。
- ✅ Deployment confirmed!: 这是最终的胜利标志,说明合约已经正式永久存在于你的本地区块链上了。
🛠️ 验证部署
既然已经 Deployment confirmed,你可以运行以下命令直接从节点拉取你刚刚部署的合约内容:
# 验证合约是否真的上链
curl http://127.0.0.1:3030/testnet/program/hello.aleo
实操
Code/Aleo/hello took 2.7s
➜ curl http://127.0.0.1:3030/testnet/program/hello.aleo
"program hello.aleo;\n\nfunction main:\n input r0 as u32.public;\n input r1 as u32.private;\n add r0 r1 into r2;\n output r2 as u32.private;\n\nconstructor:\n assert.eq edition 0u16;\n"%
Code/Aleo/hello
➜ # 使用 python 格式化输出
curl -s http://127.0.0.1:3030/testnet/program/hello.aleo | python3 -c "import sys, json; print(json.load(sys.stdin))"
program hello.aleo;
function main:
input r0 as u32.public;
input r1 as u32.private;
add r0 r1 into r2;
output r2 as u32.private;
constructor:
assert.eq edition 0u16;
看到 curl 返回了合约代码,这说明 hello.aleo 已经实实在在地躺在你的本地区块链账本里了。
你得到的字符串其实就是合约的 Aleo Instructions (AUM),也就是 Aleo 的汇编语言。
- Proof of Existence (存在性证明):
curl能抓到它,说明交易 IDat1ta5a...已经被矿工(虽然是本地模拟矿工)打包进区块了。 - Constructor 验证:你会发现返回的内容里保留了
constructor部分。这证明你部署的版本确实包含了 V9 以上的特性,并且被节点正确解析了。
🛠️ Aleo 本地开发工作流全景图
在本地进行 Aleo 开发,本质上是在你的机器上运行一个高度简化的、单节点的区块链沙盒。整个工作流由以下四个核心环节组成:
1. 开发者环境 (Source & Config)
- 角色:策略中心。
- 动作:编写 Leo 源代码(
.leo),定义程序逻辑、状态存储(Mapping)和数据类型。 - 关键配置:通过
.env文件配置私钥(控制权)和网络终点(Endpoint),决定代码往哪里发。
2. Leo CLI (Compiler & Client)
- 角色:翻译官与信使。
- 动作:
- 编译:将高级语言 Leo 代码静态分析并转换成 AUM 指令集(Aleo Unit Model,即你看到的
\n字符串)。 - 签名与打包:使用你的私钥对部署请求进行签名,构造出符合 Aleo 协议规范的交易包(Transaction)。
- 编译:将高级语言 Leo 代码静态分析并转换成 AUM 指令集(Aleo Unit Model,即你看到的
3. REST API (Communication Interface)
- 角色:银行柜台 / 交互门户。
- 动作:
- 节点默认监听 3030 端口。
- 它作为“中间人”,接收来自 Leo CLI 的广播请求,并将其转发给后端的账本引擎。
- 同时,它也负责响应你的
curl查询,将链上的二进制状态回显为可读的字符串。
4. Devnode (Local Ledger Engine)
- 角色:微型区块链实验室 / 记账员。
- 动作:
- 验证与执行:检查交易签名是否合法、合约名是否重复、共识版本是否对齐。
- 区块产出:通过本地模拟的共识过程,将交易打包进区块(Block),并实现状态持久化(存储在本地磁盘)。
🔍 核心验证逻辑总结
当你完成上述流程后,你应该执行以下“信心三步走”来确认一切正常:
- 部署确认:看到
✅ Deployment confirmed!,意味着交易已进入内存池并被成功打包。 - 存在性验证:通过
curl [http://127.0.0.1:3030/testnet/program/](http://127.0.0.1:3030/testnet/program/){name}.aleo能够拉取到 AUM 指令,证明合约已上链。 - AUM 指令分析:回显内容中包含
constructor和正确的function定义,证明上链的代码逻辑与你的源码完全一致。
测试部署
领水

第一次部署
hello on main
➜ source .env
hello on main
➜ leo deploy --network testnet --broadcast
📢 Loading environment variables from a `.env` file in the directory tree.
- NETWORK=testnet
- PRIVATE_KEY=APriv6zCGts
- ENDPOINT=https://api.explorer.provable.com/v1
Leo 🔨 Compiling 'hello.aleo'
Leo 2 statements before dead code elimination.
Leo 2 statements after dead code elimination.
Leo The program checksum is: '[212u8, 91u8, 180u8, 186u8, 11u8, 135u8, 133u8, 41u8, 42u8, 76u8, 102u8, 23u8, 196u8, 194u8, 100u8, 73u8, 111u8, 221u8, 183u8, 142u8, 23u8, 199u8, 228u8, 153u8, 158u8, 48u8, 97u8, 213u8, 199u8, 148u8, 74u8, 107u8]'.
Leo Program size: 0.18 KB / 500.00 KB
Leo ✅ Compiled 'hello.aleo' into Aleo instructions.
Leo ✅ Generated ABI at 'build/abi.json'.
📢 Using the following consensus heights: 0,2950000,4800000,6625000,6765000,7600000,8365000,9173000,9800000,10525000,11952000,12669000,14906000,15370000
To override, pass in `--consensus-heights` or override the environment variable `CONSENSUS_VERSION_HEIGHTS`.
Attempting to determine the consensus version from the latest block height at https://api.explorer.provable.com/v1...
🛠️ Deployment Plan Summary
──────────────────────────────────────────────
🔧 Configuration:
Private Key: APrivateKey1zkpGR9X1koAE...
Address: aleo1cu0xk4tt99pgxglpqlt...
Endpoint: https://api.explorer.provable.com/v1
Network: testnet
Consensus Version: 14
📦 Deployment Tasks:
• hello.aleo │ priority fee: 0 │ fee record: no (public fee)
⚙️ Actions:
• Transaction(s) will NOT be printed to the console.
• Transaction(s) will NOT be saved to a file.
• Transaction(s) will be broadcast to https://api.explorer.provable.com/v1
──────────────────────────────────────────────
✔ Do you want to proceed with deployment? · yes
🔧 Your program 'hello.aleo' has the following constructor.
──────────────────────────────────────────────
constructor:
assert.eq edition 0u16;
──────────────────────────────────────────────
Once it is deployed, it CANNOT be changed.
✔ Would you like to proceed? · yes
📦 Creating deployment transaction for 'hello.aleo'...
📊 Deployment Summary for hello.aleo
──────────────────────────────────────────────
Program Size: 0.18 KB / 500.00 KB
Total Variables: 16,787
Total Constraints: 12,927
Max Variables: 2,097,152
Max Constraints: 2,097,152
💰 Cost Breakdown (credits)
Transaction Storage: 0.874000
Program Synthesis: 0.029714
Namespace: 100000.000000
Constructor: 0.002000
Priority Fee: 0.000000
Total Fee: 100000.905714
Function 'main'
Total Execution Cost: 0.001316
|- Storage Cost: 0.001316
|- Finalize Cost: 0.000000
──────────────────────────────────────────────
📡 Broadcasting deployment for hello.aleo...
💰Your current public balance is 20 credits.
Error [EPAK0375050]: ❌ Your public balance of 20 for aleo1cu0xk4tt99pgxglpqltzk3tmpgh7qftjwukxcmewzpy0fkqghvgsxu0g03 is insufficient to pay the base fee of 100000.905714
部署失败 - 账户余额不足
核心问题总结
- 高额命名费:Aleo 的部署费用包含 Namespace Cost(命名空间成本)。其计算公式为 $10^{(10 - \text{字符数})}$。
- 短名惩罚:你的程序名
hello.aleo前缀仅 5 个字符,计算结果为 $10^{(10-5)} = 100,000$。这意味着名字越短,注册费越贵。 - 余额缺口:官方领水(Faucet)每次通常只给 20 积分,与 10 万积分的门槛相比杯水车薪。
第二次部署
增加程序名长度
hello on main [!]
➜ source .env
hello on main [!]
➜ leo deploy --network testnet --broadcast
📢 Loading environment variables from a `.env` file in the directory tree.
- NETWORK=testnet
- PRIVATE_KEY=APrixxxxxxxxxxxGts
- ENDPOINT=https://api.explorer.provable.com/v1
Leo 🔨 Compiling 'hello_paxon_2026.aleo'
Leo 2 statements before dead code elimination.
Leo 2 statements after dead code elimination.
Leo The program checksum is: '[43u8, 61u8, 41u8, 134u8, 253u8, 126u8, 77u8, 6u8, 169u8, 240u8, 110u8, 112u8, 50u8, 142u8, 64u8, 13u8, 118u8, 131u8, 138u8, 87u8, 75u8, 103u8, 113u8, 61u8, 0u8, 186u8, 80u8, 34u8, 123u8, 136u8, 113u8, 72u8]'.
Leo Program size: 0.19 KB / 500.00 KB
Leo ✅ Compiled 'hello_paxon_2026.aleo' into Aleo instructions.
Leo ✅ Generated ABI at 'build/abi.json'.
📢 Using the following consensus heights: 0,2950000,4800000,6625000,6765000,7600000,8365000,9173000,9800000,10525000,11952000,12669000,14906000,15370000
To override, pass in `--consensus-heights` or override the environment variable `CONSENSUS_VERSION_HEIGHTS`.
Attempting to determine the consensus version from the latest block height at https://api.explorer.provable.com/v1...
🛠️ Deployment Plan Summary
──────────────────────────────────────────────
🔧 Configuration:
Private Key: APrivateKey1zkpGR9X1koAE...
Address: aleo1cu0xk4tt99pgxglpqlt...
Endpoint: https://api.explorer.provable.com/v1
Network: testnet
Consensus Version: 14
📦 Deployment Tasks:
• hello_paxon_2026.aleo │ priority fee: 0 │ fee record: no (public fee)
⚙️ Actions:
• Transaction(s) will NOT be printed to the console.
• Transaction(s) will NOT be saved to a file.
• Transaction(s) will be broadcast to https://api.explorer.provable.com/v1
──────────────────────────────────────────────
✔ Do you want to proceed with deployment? · yes
🔧 Your program 'hello_paxon_2026.aleo' has the following constructor.
──────────────────────────────────────────────
constructor:
assert.eq edition 0u16;
──────────────────────────────────────────────
Once it is deployed, it CANNOT be changed.
✔ Would you like to proceed? · yes
📦 Creating deployment transaction for 'hello_paxon_2026.aleo'...
📊 Deployment Summary for hello_paxon_2026.aleo
──────────────────────────────────────────────
Program Size: 0.19 KB / 500.00 KB
Total Variables: 17,235
Total Constraints: 12,927
Max Variables: 2,097,152
Max Constraints: 2,097,152
💰 Cost Breakdown (credits)
Transaction Storage: 0.885000
Program Synthesis: 0.030162
Namespace: 1.000000
Constructor: 0.002000
Priority Fee: 0.000000
Total Fee: 1.917162
Function 'main'
Total Execution Cost: 0.001327
|- Storage Cost: 0.001327
|- Finalize Cost: 0.000000
──────────────────────────────────────────────
📡 Broadcasting deployment for hello_paxon_2026.aleo...
💰Your current public balance is 20 credits.
✔ This transaction will cost you 1.917162 credits. Do you want to proceed? · yes
✉️ Broadcasted transaction with:
- transaction ID: 'at1yp24p2jl5jfex0p09ug8lv82stjfuy6jwfu0v7ap4vakhha7ycyqhqv293'
- fee ID: 'au1erhmfdzeeq6a5le0hn6edjqynmugypw3qckt78k69gz9jaywfuysdrdf8w'
- fee transaction ID: 'at1u82xcggau7xa4wpvnyrjm8xe3985px07l8lvr7pwfrktr2ju25pq7dvg0s'
(use this to check for rejected transactions)
🔄 Searching up to 12 blocks to confirm transaction (this may take several seconds)...
Explored 4 blocks.
Transaction accepted.
✅ Deployment confirmed!
🚀 部署成功核心要点总结
- 命名空间费用(Namespace Cost)骤降:
- 通过将程序名改为
hello_paxon_2026.aleo(长度超过 10 字符),你的 Namespace 费用从之前的 100,000 直接降到了 1.000000。 - 根据 Aleo 费用公式 $10^{(10 - \text{字符数})}$,当长度大于等于 10 时,该项费用将不再是障碍。
- 通过将程序名改为
- 总费用(Total Fee):
- 本次部署总共花费约 1.917162 Credits。
- 这对于只有 20 Credits 余额的账户来说绰绰有余,验证了“增加名称长度”是规避高额成本最有效的工程手段。
- 构造函数验证(Constructor):
- 日志显示
constructor部分已被正确打包。 - 这标志着你在测试网上成功运行了包含 V9+ 特性(
edition 0u16校验)的最新版合约。
- 日志显示
- 上链确认:
- 交易 ID
at1yp24p2...已被接收并确认(Transaction accepted)。
- 交易 ID
你现在已经完整走通了从 本地开发 -> 环境切换 -> 费用优化 -> 测试网部署 的全流程。
浏览器查看交易详情

调用合约方法
hello on main [!] took 27.1s
➜ leo execute main 10u32 20u32 \
--network testnet \
--broadcast \
--yes
📢 Loading environment variables from a `.env` file in the directory tree.
- NETWORK=testnet
- PRIVATE_KEY=xxxxxxxxxxx
- ENDPOINT=https://api.explorer.provable.com/v1
Leo 🔨 Compiling 'hello_paxon_2026.aleo'
Leo 2 statements before dead code elimination.
Leo 2 statements after dead code elimination.
Leo The program checksum is: '[43u8, 61u8, 41u8, 134u8, 253u8, 126u8, 77u8, 6u8, 169u8, 240u8, 110u8, 112u8, 50u8, 142u8, 64u8, 13u8, 118u8, 131u8, 138u8, 87u8, 75u8, 103u8, 113u8, 61u8, 0u8, 186u8, 80u8, 34u8, 123u8, 136u8, 113u8, 72u8]'.
Leo Program size: 0.19 KB / 500.00 KB
Leo ✅ Compiled 'hello_paxon_2026.aleo' into Aleo instructions.
Leo ✅ Generated ABI at 'build/abi.json'.
📢 Using the following consensus heights: 0,2950000,4800000,6625000,6765000,7600000,8365000,9173000,9800000,10525000,11952000,12669000,14906000,15370000
To override, pass in `--consensus-heights` or override the environment variable `CONSENSUS_VERSION_HEIGHTS`.
Attempting to determine the consensus version from the latest block height at https://api.explorer.provable.com/v1...
🚀 Execution Plan Summary
──────────────────────────────────────────────
🔧 Configuration:
Private Key: APrivateKey1zkpGR9X1koAE...
Address: aleo1cu0xk4tt99pgxglpqlt...
Endpoint: https://api.explorer.provable.com/v1
Network: testnet
Consensus Version: 14
🎯 Execution Target:
Program: hello_paxon_2026.aleo
Function: main
Source: local
💸 Fee Info:
Priority Fee: 0 μcredits
Fee Record: no (public fee)
⚙️ Actions:
- Transaction will NOT be printed to the console.
- Transaction will NOT be saved to a file.
- Transaction will be broadcast to https://api.explorer.provable.com/v1
──────────────────────────────────────────────
➕Adding programs to the VM in the following order:
- hello_paxon_2026.aleo (local)
⚙️ Executing hello_paxon_2026.aleo/main...
📊 Execution Cost Summary for hello_paxon_2026.aleo
──────────────────────────────────────────────
💰 Cost Breakdown (credits)
Transaction Storage: 0.001327
On-chain Execution: 0.000000
Priority Fee: 0.000000
Total Fee: 0.001327
──────────────────────────────────────────────
Typeless, Typeless, Typeless.
Thank you.
➡️ Output
• 30u32
📡 Broadcasting execution for hello_paxon_2026.aleo...
💰Your current public balance is 18.082838 credits.
✉️ Broadcasted transaction with:
- transaction ID: 'at1q5t7f79yl2evmzaaktesxcsru882healc6f9pawmq45fg3np9u8ssspkst'
- fee ID: 'au1wf8la2adgypkjk7767kgzzj67at58l3hc98cllnx5u6x3txusvpqrum58p'
- fee transaction ID: 'at1t9cjlc7rd0cyn0jtztd3w35zr60egc0aust0nq9udgg6cr88hyqqfh8833'
(use this to check for rejected transactions)
🔄 Searching up to 12 blocks to confirm transaction (this may take several seconds)...
Explored 3 blocks.
Transaction accepted.
✅ Execution confirmed!
这次执行结果标志着你已经在 Aleo 测试网上完成了一个完整且成功的隐私计算生命周期。
以下是对执行日志的详细拆解与说明:
1. 计算结果验证
- 输入内容:你通过命令行输入了
10u32(公开)和20u32(私有)。 - 计算输出:日志显示
➡️ Output • 30u32。这证明合约逻辑运行正确,ZKVM(零知识虚拟机)成功执行了加法运算并输出了结果。
2. 成本效益分析
- 极低费用:本次执行的 Total Fee 仅为 0.001327 Credits。
- 费用构成:主要支出是 Transaction Storage(交易存储费),而 On-chain Execution 费用为 0。这说明在 Aleo 上,一旦合约部署完成,单纯的计算调用成本是非常低廉的,你剩余的 18.08 Credits 余额足以支撑你进行上万次类似的调用。
3. 隐私与安全确认
- 零知识属性:虽然你在本地输入了私有的
20u32,但在广播的交易 IDat1q5t7f7...中,外界无法看到这个具体数值,只能看到证明(Proof)和最终输出。 - 状态确认:日志显示
Transaction accepted和✅ Execution confirmed!,这意味着你的这笔计算已经通过了网络共识,被永久记录在 Aleo 测试网的账本中。
4. 总结与意义
通过这次操作,你不仅验证了 leo execute 的工作流,更证明了你之前通过增加程序名长度来规避 10 万积分“起名费”的策略是完全正确的。
你现在已经从一个“学习者”正式转变为一个在 Aleo 公链上有真实上链资产与交互记录的开发者。
为什么需要隐私?
Q1. Aleo 被定义为首个提供哪三个特性的去中心化应用平台?
Aleo 是首个提供无需许可、可编程、隐私保护的去中心化应用平台。
Q2. 在去中心化账本中,为什么“重复执行“既是核心优势也是主要弱点?
去中心化账本的核心优势同时也是其主要弱点
- 账本的重复执行损害了扩展性,每个节点都必须重新执行每一笔交易,以确保账本的完整性
- 账本的重复执行损害了隐私,所有状态转换的历史记录都永久性地公开存储
Q3. Aleo 采用的记录模型(Record Model)与传统的账户模型相比,最大的优势是什么?
账户模型:
- 简易
- 低扩展性/ 高费率
- 无隐私
隐私模型:
- 无并发:程序的整个状态是一个承诺(Commitment),更新程序状态需要最新的承诺
- 无隐私:每个人仍然可以看到谁更新了哪个程序
特性:
- 无账本重复执行 使用零知识证明(ZKP)实现链上扩展性+简洁性
- 数据隐私 通过承诺(Commitments)和加密隐藏程序状态
- 账户隐私 地址无法加密,因为账本依赖地址来索引全局状态
- 高效状态更新 谓词(Predicate)必须消耗整个程序状态才能对其进行更新
- 并发性 同一时间只能发生一次状态转换
记录模型(Record Model)
- 并发性:用户通过消耗与自身上下文相关的记录(Records)来执行程序
- 禁止双重支出:去中心化账本强制规定任何记录都不能被重复消耗
特性
- 无账本重复执行 使用零知识证明(ZKP)实现链上扩展性+简洁性
- 数据隐私 通过承诺(Commitments)和加密技术隐藏程序状态
- 账户隐私 去中心化账本基于程序ID(Program IDs)对全局状态进行索引
- 高效状态更新 谓词(Predicate)仅消耗正在更新的程序记录(Records)
- 并发性 通过记录实现对程序状态的同步更新(且账本确保程序记录不会被双重支出)
Q4. Aleo 开发 Leo 语言的初衷(设计目标)是什么?
Aleo 开发 Leo 语言的核心目的,是大幅降低零知识证明应用的开发门槛:通过提供一种语法类似 Rust、经过形式化验证的高级编程语言,让开发者无需成为密码学专家,也能安全、高效地为 Aleo 区块链编写隐私保护程序,从而推动零知识证明技术的主流化应用。
Q5. 相比其他隐私方案,Aleo 在证明上有哪些显著优势?
- 极速证明 - 批处理聚合(Batching)
- 证明大小(0.7KB)
- 极速验证(3ms)
总结
回顾整个实操流程,在 Aleo 上开发并不仅仅是写一段代码并将其广播到网络上,更是一次对全新计算范式的体验。
从构建 hello.aleo 开始,我们完整验证了 Aleo 的双层语言结构(面向开发者的 Leo 与底层汇编 Aleo Instruction)带来的顺滑体验。通过本地 Devnet 沙盒的预演,我们不仅排查了共识版本(Consensus Version)的兼容性问题,还掌握了通过增加程序名长度来大幅削减高额“命名空间费”的工程避坑技巧,最终以极低的成本(不到 2 Credits)在测试网完成了隐私合约的部署与执行。
Aleo 独创的记录模型(Record Model)巧妙地将传统公链“全网重复执行账本”的性能噩梦,转化为链下计算、链上验证的高效模式。这不仅做到了极速的证明与验证,更在保留智能合约可编程性的同时,实现了真正意义上的端到端数据隐私。
当你看到终端里亮起的 ✅ Execution confirmed! 时,意味着你已经不仅仅是一个框架的学习者,而是正式成为了一名在 Aleo 公链上拥有真实交互记录的隐私计算开发者。Web3 的下一代隐私基础设施,正等待着你去构建更多可能。