Easy-Go-Web3
知识图谱Go 教程React Web3智能合约
需求分析系统设计设计模式Go 微服务
项目实战DevOps
Go 生态React 生态智能合约生态Web3 生态AI × Web3工具箱Web3 公司远程Web3求职
🎯 AA 工程师面试手册博客
GitHub
项目实战去中心化预言机网络
高级基础设施10-12周

去中心化预言机网络

构建类 Chainlink 的去中心化预言机网络,支持链下数据聚合、多节点共识与链上数据投喂

技术栈

GoSoliditylibp2pPostgreSQLPrometheus

核心功能

P2P 节点网络构建
链下数据聚合与共识
多数据源适配器
链上合约数据投喂
节点质押与惩罚机制
请求-响应与订阅模式

系统架构

┌─────────────────────────────────────────────────────────────┐
│                    Oracle Node Cluster                       │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐          │
│  │   Node 1    │  │   Node 2    │  │   Node N    │          │
│  │  (Leader)   │◄─┤  (Follower) │◄─┤  (Follower) │          │
│  └─────────────┘  └─────────────┘  └─────────────┘          │
│         │                │                │                  │
│         └────────────────┼────────────────┘                  │
│                          │ libp2p                            │
├─────────────────────────────────────────────────────────────┤
│                   Consensus Layer (OCR)                      │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐       │
│  │ Observation  │  │  Report Gen  │  │ Transmission │       │
│  └──────────────┘  └──────────────┘  └──────────────┘       │
├─────────────────────────────────────────────────────────────┤
│                    Data Source Adapters                      │
│  ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐ ┌────────┐    │
│  │Binance │ │Coinbase│ │Uniswap │ │  API   │ │  IPFS  │    │
│  └────────┘ └────────┘ └────────┘ └────────┘ └────────┘    │
├─────────────────────────────────────────────────────────────┤
│  On-Chain Contracts (Aggregator, AccessControl, Staking)    │
└─────────────────────────────────────────────────────────────┘

课程章节

第一章:P2P 网络层

libp2p 节点搭建4小时
节点发现与路由3小时
消息广播与订阅3小时

第二章:数据聚合引擎

多源数据适配器4小时
数据清洗与异常检测3小时
中位数/加权平均算法3小时

第三章:链下共识协议

OCR 协议实现5小时
Leader 选举机制3小时
报告签名与验证4小时

第四章:链上合约系统

Aggregator 合约开发4小时
质押与惩罚机制4小时
权限控制与升级3小时

核心代码实现

OCR 共识协议

go
1// OCRProtocol 链下报告共识
2type OCRProtocol struct {
3 nodeID peer.ID
4 peers []peer.ID
5 threshold int
6 currentRound uint64
7 observations map[uint64][]Observation
8}
9
10// Observation 节点观测值
11type Observation struct {
12 NodeID peer.ID
13 Value *big.Int
14 Timestamp time.Time
15 Signature []byte
16}
17
18// RunConsensusRound 执行共识轮次
19func (o *OCRProtocol) RunConsensusRound(
20 ctx context.Context,
21) (*Report, error) {
22 round := atomic.AddUint64(&o.currentRound, 1)
23
24 // 1. 收集观测值
25 observation, err := o.collectObservation(ctx)
26 if err != nil {
27 return nil, err
28 }
29
30 // 2. 广播观测值给所有节点
31 signedObs := o.signObservation(observation)
32 o.broadcast(ctx, &ObservationMessage{
33 Round: round,
34 Observation: signedObs,
35 })
36
37 // 3. 等待收集足够的观测值
38 observations, err := o.waitForObservations(ctx, round, o.threshold)
39 if err != nil {
40 return nil, fmt.Errorf("collect observations: %w", err)
41 }
42
43 // 4. 如果是 Leader,生成报告
44 if o.isLeader(round) {
45 report := o.generateReport(round, observations)
46
47 // 5. 收集签名
48 signatures, err := o.collectSignatures(ctx, report)
49 if err != nil {
50 return nil, err
51 }
52
53 report.Signatures = signatures
54 return report, nil
55 }
56
57 // Follower: 等待 Leader 的报告并签名
58 return o.waitAndSignReport(ctx, round)
59}
60
61// generateReport 生成共识报告
62func (o *OCRProtocol) generateReport(
63 round uint64,
64 observations []Observation,
65) *Report {
66 // 提取所有值并排序
67 values := make([]*big.Int, len(observations))
68 for i, obs := range observations {
69 values[i] = obs.Value
70 }
71 sort.Slice(values, func(i, j int) bool {
72 return values[i].Cmp(values[j]) < 0
73 })
74
75 // 取中位数
76 median := values[len(values)/2]
77
78 return &Report{
79 Round: round,
80 MedianValue: median,
81 Observations: observations,
82 Timestamp: time.Now(),
83 }
84}
高性能交易引擎Layer2 Rollup 服务
Easy-Go-Web3

构建 Go 后端与 Web3 的学习之路。从基础到进阶,从理论到实践,助你成为全栈区块链开发者。

学习路径

  • 知识图谱
  • Go 教程
  • Go 微服务
  • 面试手册

资源中心

  • 工具箱
  • DevOps 工具
  • Web3 生态
  • 博客

© 2025 Easy-Go-Web3. All rights reserved.

Created withbyhardybao