自动化 — 设计

目标

让 OracleHub (0x8016) 完全自动化运行:每个 L1 Batch 开头自动注入来自 CoinGecko/Binance 的最新价格,零人工干预。

架构

                    ┌─────────────────────────┐
                    │   CoinGecko / Binance   │
                    └────────────┬────────────┘
                                 │ HTTP (每 12 秒)
                    ┌────────────▼────────────┐
                    │   PriceAggregator       │
                    │   (中位数 + 置信度)      │
                    └────────────┬────────────┘

                    ┌────────────▼────────────┐
                    │  SharedOracleService    │
                    │  (Arc<Mutex>)           │
                    └────────────┬────────────┘

   ┌─────────────────────────────▼──────────────────────┐
   │ MempoolIO.get_oracle_calldata()                    │
   │   → ABI encode batchUpdatePrices(symbols,prices,..)│
   └─────────────────────────────┬──────────────────────┘

   ┌─────────────────────────────▼──────────────────────┐
   │ keeper.rs: process_oracle_tx()                     │
   │   → 作为每个 L1 Batch 的第一笔 tx 注入             │
   │   → 失败时: warn + 继续 (永不阻塞 batch)           │
   └─────────────────────────────┬──────────────────────┘

   ┌─────────────────────────────▼──────────────────────┐
   │ OracleHub (0x8016) — 链上系统合约                   │
   │   → batchUpdatePrices() 写入存储                    │
   │   → DApps 通过 getLatestPrice() 零 gas 查询         │
   └────────────────────────────────────────────────────┘

已实现组件(无需修改)

组件
文件
说明

价格注入

keeper.rs:process_oracle_tx()

Batch 开始时注入 Oracle tx

交易构造

oracle_tx.rs:build_oracle_update_tx()

构造 L2Tx (target=0x8016, gas=0)

ABI 编码

service.rs:encode_oracle_calldata()

编码 batchUpdatePrices 参数

价格聚合

aggregator.rs:PriceAggregator

中位数 + 置信度评分

CoinGecko

coingecko.rs:CoinGeckoSource

免费 API,批量查询

Binance

binance.rs:BinanceSource

免费 API,符号映射

Mock

mock.rs:MockSource

测试用,默认价格

IO 接口

mempool.rs:get_oracle_calldata()

MempoolIO accessor

Trait 方法

io/mod.rs:get_oracle_tx_calldata()

StateKeeperIO trait

后台任务

wiring.rs:oracle_price_fetcher()

tokio::spawn 定时获取

配置

baby-chain.toml

31 交易对 + 源配置

需要修改的部分

1. Rust 节点启动集成(~30 行)

文件: era-core/core/node/mempool_io.rs

当前代码 (line 152):

改为:

2. 链上配置调整

操作
调用
说明

降低最低源数量

setConfig(3600, 500, 1)

minSourceCount 3→1 (dev mode)

注册 BTC/USD

addSymbol(keccak256("BTC/USD"))

新增交易对

注册 USDC/USD

addSymbol(keccak256("USDC/USD"))

新增交易对

注册 SOL/USD

addSymbol(keccak256("SOL/USD"))

新增交易对

注册 BNB/USD

addSymbol(keccak256("BNB/USD"))

新增交易对

3. 配置文件

文件: config/baby-chain.toml

  • min_source_count = 1 (从 1 保持不变,与链上匹配)

执行顺序

每个 Batch 的 tx 执行顺序:

  1. Protocol Upgrade Tx(如有版本变更,必须成功

  2. Oracle Price Update Tx(如有价格数据,失败不阻塞

  3. 用户交易(从 mempool 获取)

失败处理

  • API 全部失败 → 无 Oracle calldata → 跳过注入 → batch 正常继续

  • Oracle tx revert → warn 日志 → batch 正常继续

  • isPriceFresh() 返回 false → DApps 可检测过期价格

  • 不使用 circuit breaker(YAGNI)

范围

  • ~30 行 Rust 代码修改

  • 5 笔链上交易(setConfig + 4x addSymbol)

  • 0 个新文件

  • E2E 验证:重启节点 → 观察 batch 日志含 Oracle tx → 查询价格

Last updated