860 lines
20 KiB
Markdown
860 lines
20 KiB
Markdown
# nac-constitution-clauses 模块深度分析报告
|
||
|
||
**模块名称**: nac-constitution-clauses
|
||
**版本**: 0.1.0
|
||
**分析日期**: 2026-02-18
|
||
**分析人员**: NAC开发团队
|
||
|
||
---
|
||
|
||
## 📋 模块概览
|
||
|
||
**功能定位**: NAC宪法条款管理 - 定义和管理NAC宪法条款
|
||
**英文全称**: NAC Constitution Clauses Registry
|
||
**代码行数**: 49行
|
||
**完成度**: 25%
|
||
**测试覆盖**: 0% (无测试)
|
||
**编译状态**: ✅ 通过
|
||
|
||
---
|
||
|
||
## 🏗️ 架构设计
|
||
|
||
### 核心功能
|
||
|
||
nac-constitution-clauses实现了NAC公链的宪法条款注册表,负责:
|
||
|
||
1. **条款定义**: 定义宪法条款的数据结构
|
||
2. **条款分级**: 永恒级、战略级、战术级三层分级
|
||
3. **条款管理**: 添加、查询宪法条款
|
||
4. **条款验证**: 通过哈希验证条款完整性
|
||
|
||
### 技术特点
|
||
|
||
1. **三级分层**: Eternal(永恒级)、Strategic(战略级)、Tactical(战术级)
|
||
2. **哈希验证**: 每个条款都有唯一的哈希值
|
||
3. **时间戳**: 记录条款生效时间
|
||
4. **索引管理**: 使用HashMap快速查询
|
||
|
||
### 目录结构
|
||
|
||
```
|
||
nac-constitution-clauses/
|
||
├── Cargo.toml
|
||
└── src/
|
||
└── lib.rs (49行) - 完整实现
|
||
```
|
||
|
||
---
|
||
|
||
## 📦 依赖关系
|
||
|
||
```toml
|
||
[dependencies]
|
||
nac-udm = { path = "../nac-udm" } # NAC统一定义模块(Hash类型)
|
||
serde = { version = "1.0", features = ["derive"] } # 序列化
|
||
serde_json = "1.0" # JSON序列化
|
||
```
|
||
|
||
**依赖分析**:
|
||
- **nac-udm**: 提供Hash类型(用于条款哈希)
|
||
- **serde**: 支持序列化/反序列化
|
||
- **serde_json**: JSON格式支持(用于配置文件)
|
||
|
||
---
|
||
|
||
## 🔍 核心功能详解
|
||
|
||
### 1. 宪法条款 (ConstitutionalClause)
|
||
|
||
```rust
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct ConstitutionalClause {
|
||
pub clause_index: u64, // 条款索引
|
||
pub title: String, // 条款标题
|
||
pub content: String, // 条款内容
|
||
pub clause_hash: Hash, // 条款哈希
|
||
pub effective_from: u64, // 生效时间
|
||
pub tier: ClauseTier, // 条款层级
|
||
}
|
||
```
|
||
|
||
**字段说明**:
|
||
|
||
| 字段 | 类型 | 说明 | 示例 |
|
||
|------|------|------|------|
|
||
| `clause_index` | u64 | 条款索引(唯一标识) | 1, 2, 3, ... |
|
||
| `title` | String | 条款标题 | "NET_CONN_MIN_CBP" |
|
||
| `content` | String | 条款内容 | "CBP节点最小连接数为12" |
|
||
| `clause_hash` | Hash | 条款哈希(SHA3-384) | 0x1234...5678 |
|
||
| `effective_from` | u64 | 生效时间戳 | 1708300800 |
|
||
| `tier` | ClauseTier | 条款层级 | Eternal/Strategic/Tactical |
|
||
|
||
**设计说明**:
|
||
- `clause_index`: 全局唯一,用于快速查询
|
||
- `title`: 简短标识符,符合常量命名规范
|
||
- `content`: 完整的条款描述
|
||
- `clause_hash`: 用于验证条款未被篡改
|
||
- `effective_from`: 支持延迟生效
|
||
- `tier`: 决定修改难度和投票要求
|
||
|
||
---
|
||
|
||
### 2. 条款层级 (ClauseTier)
|
||
|
||
```rust
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||
pub enum ClauseTier {
|
||
Eternal, // 永恒级
|
||
Strategic, // 战略级
|
||
Tactical, // 战术级
|
||
}
|
||
```
|
||
|
||
**层级说明**:
|
||
|
||
| 层级 | 中文名 | 修改难度 | 投票要求 | 示例条款 |
|
||
|------|--------|---------|---------|---------|
|
||
| `Eternal` | 永恒级 | 极难 | 95%+ | 公链名称、核心价值观 |
|
||
| `Strategic` | 战略级 | 困难 | 80%+ | 共识机制、经济模型 |
|
||
| `Tactical` | 战术级 | 中等 | 67%+ | 参数调整、功能开关 |
|
||
|
||
**设计思想**:
|
||
- **Eternal(永恒级)**: 几乎不可变,定义公链的根本属性
|
||
- **Strategic(战略级)**: 重大决策,需要广泛共识
|
||
- **Tactical(战术级)**: 日常调整,相对灵活
|
||
|
||
**示例**:
|
||
```rust
|
||
// 永恒级条款
|
||
let eternal_clause = ConstitutionalClause {
|
||
clause_index: 1,
|
||
title: "CHAIN_NAME".to_string(),
|
||
content: "本公链名称为New Asset Chain(NAC)".to_string(),
|
||
clause_hash: Hash::zero(),
|
||
effective_from: 0,
|
||
tier: ClauseTier::Eternal,
|
||
};
|
||
|
||
// 战略级条款
|
||
let strategic_clause = ConstitutionalClause {
|
||
clause_index: 10,
|
||
title: "CONSENSUS_PROTOCOL".to_string(),
|
||
content: "本公链采用CBPP共识协议".to_string(),
|
||
clause_hash: Hash::zero(),
|
||
effective_from: 0,
|
||
tier: ClauseTier::Strategic,
|
||
};
|
||
|
||
// 战术级条款
|
||
let tactical_clause = ConstitutionalClause {
|
||
clause_index: 100,
|
||
title: "NET_CONN_MIN_CBP".to_string(),
|
||
content: "CBP节点最小连接数为12".to_string(),
|
||
clause_hash: Hash::zero(),
|
||
effective_from: 1708300800,
|
||
tier: ClauseTier::Tactical,
|
||
};
|
||
```
|
||
|
||
---
|
||
|
||
### 3. 宪法注册表 (ConstitutionRegistry)
|
||
|
||
```rust
|
||
pub struct ConstitutionRegistry {
|
||
clauses: HashMap<u64, ConstitutionalClause>,
|
||
}
|
||
```
|
||
|
||
**数据结构**:
|
||
- 使用`HashMap`存储条款(key=clause_index)
|
||
- O(1)查询复杂度
|
||
|
||
---
|
||
|
||
#### 3.1 创建注册表
|
||
|
||
```rust
|
||
pub fn new() -> Self {
|
||
Self {
|
||
clauses: HashMap::new(),
|
||
}
|
||
}
|
||
```
|
||
|
||
**功能**: 创建空的条款注册表
|
||
|
||
---
|
||
|
||
#### 3.2 添加条款
|
||
|
||
```rust
|
||
pub fn add_clause(&mut self, clause: ConstitutionalClause) {
|
||
self.clauses.insert(clause.clause_index, clause);
|
||
}
|
||
```
|
||
|
||
**功能**: 添加或更新条款
|
||
|
||
**问题**: 缺少验证逻辑:
|
||
- 没有检查条款索引是否重复
|
||
- 没有验证条款哈希
|
||
- 没有检查权限
|
||
|
||
**示例**:
|
||
```rust
|
||
let mut registry = ConstitutionRegistry::new();
|
||
|
||
let clause = ConstitutionalClause {
|
||
clause_index: 1,
|
||
title: "CHAIN_NAME".to_string(),
|
||
content: "NAC".to_string(),
|
||
clause_hash: Hash::zero(),
|
||
effective_from: 0,
|
||
tier: ClauseTier::Eternal,
|
||
};
|
||
|
||
registry.add_clause(clause);
|
||
```
|
||
|
||
---
|
||
|
||
#### 3.3 查询条款
|
||
|
||
```rust
|
||
pub fn get_clause(&self, index: u64) -> Option<&ConstitutionalClause> {
|
||
self.clauses.get(&index)
|
||
}
|
||
```
|
||
|
||
**功能**: 根据索引查询条款
|
||
|
||
**返回**: 条款引用(零拷贝)
|
||
|
||
**示例**:
|
||
```rust
|
||
if let Some(clause) = registry.get_clause(1) {
|
||
println!("条款标题: {}", clause.title);
|
||
println!("条款内容: {}", clause.content);
|
||
println!("条款层级: {:?}", clause.tier);
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
## 🐛 发现的问题
|
||
|
||
### 问题1: 缺少测试覆盖
|
||
|
||
**严重程度**: ⚠️ 高
|
||
|
||
**描述**: 模块没有任何单元测试
|
||
|
||
**影响**:
|
||
- 无法验证条款添加的正确性
|
||
- 无法验证条款查询的正确性
|
||
- 无法验证层级系统
|
||
|
||
**建议测试用例**:
|
||
```rust
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn test_create_registry() {
|
||
let registry = ConstitutionRegistry::new();
|
||
assert_eq!(registry.clauses.len(), 0);
|
||
}
|
||
|
||
#[test]
|
||
fn test_add_clause() {
|
||
let mut registry = ConstitutionRegistry::new();
|
||
|
||
let clause = ConstitutionalClause {
|
||
clause_index: 1,
|
||
title: "TEST".to_string(),
|
||
content: "Test clause".to_string(),
|
||
clause_hash: Hash::zero(),
|
||
effective_from: 1000,
|
||
tier: ClauseTier::Tactical,
|
||
};
|
||
|
||
registry.add_clause(clause.clone());
|
||
assert_eq!(registry.clauses.len(), 1);
|
||
|
||
let retrieved = registry.get_clause(1).unwrap();
|
||
assert_eq!(retrieved.title, "TEST");
|
||
}
|
||
|
||
#[test]
|
||
fn test_clause_tiers() {
|
||
let eternal = ClauseTier::Eternal;
|
||
let strategic = ClauseTier::Strategic;
|
||
let tactical = ClauseTier::Tactical;
|
||
|
||
assert_ne!(eternal, strategic);
|
||
assert_ne!(strategic, tactical);
|
||
}
|
||
|
||
#[test]
|
||
fn test_get_nonexistent_clause() {
|
||
let registry = ConstitutionRegistry::new();
|
||
assert!(registry.get_clause(999).is_none());
|
||
}
|
||
}
|
||
```
|
||
|
||
**状态**: ❌ 待添加
|
||
|
||
---
|
||
|
||
### 问题2: 缺少条款验证
|
||
|
||
**严重程度**: ⚠️ 高
|
||
|
||
**描述**: `add_clause`方法没有任何验证逻辑
|
||
|
||
**问题**:
|
||
1. 可以覆盖已存在的条款
|
||
2. 可以使用无效的哈希
|
||
3. 可以使用过去的生效时间
|
||
4. 没有权限检查
|
||
|
||
**建议实现**:
|
||
```rust
|
||
#[derive(Debug)]
|
||
pub enum ClauseError {
|
||
DuplicateIndex(u64),
|
||
InvalidHash,
|
||
InvalidEffectiveTime(u64, u64), // (current_time, effective_time)
|
||
InvalidTier,
|
||
PermissionDenied,
|
||
}
|
||
|
||
impl ConstitutionRegistry {
|
||
pub fn add_clause(&mut self, clause: ConstitutionalClause, current_time: u64) -> Result<(), ClauseError> {
|
||
// 1. 检查索引是否重复
|
||
if self.clauses.contains_key(&clause.clause_index) {
|
||
return Err(ClauseError::DuplicateIndex(clause.clause_index));
|
||
}
|
||
|
||
// 2. 验证哈希非零
|
||
if clause.clause_hash == Hash::zero() {
|
||
return Err(ClauseError::InvalidHash);
|
||
}
|
||
|
||
// 3. 验证生效时间
|
||
if clause.effective_from < current_time {
|
||
return Err(ClauseError::InvalidEffectiveTime(
|
||
current_time,
|
||
clause.effective_from
|
||
));
|
||
}
|
||
|
||
// 4. 插入条款
|
||
self.clauses.insert(clause.clause_index, clause);
|
||
Ok(())
|
||
}
|
||
}
|
||
```
|
||
|
||
**状态**: ❌ 待实现
|
||
|
||
---
|
||
|
||
### 问题3: 缺少条款修改功能
|
||
|
||
**严重程度**: ⚠️ 中等
|
||
|
||
**描述**: 只能添加条款,无法修改或删除
|
||
|
||
**建议添加**:
|
||
```rust
|
||
impl ConstitutionRegistry {
|
||
// 修改条款(需要验证权限和层级)
|
||
pub fn update_clause(
|
||
&mut self,
|
||
index: u64,
|
||
new_content: String,
|
||
new_hash: Hash,
|
||
approval_rate: f64,
|
||
) -> Result<(), ClauseError> {
|
||
let clause = self.clauses.get_mut(&index)
|
||
.ok_or(ClauseError::ClauseNotFound(index))?;
|
||
|
||
// 根据层级检查投票率
|
||
let required_rate = match clause.tier {
|
||
ClauseTier::Eternal => 0.95,
|
||
ClauseTier::Strategic => 0.80,
|
||
ClauseTier::Tactical => 0.67,
|
||
};
|
||
|
||
if approval_rate < required_rate {
|
||
return Err(ClauseError::InsufficientApproval {
|
||
required: required_rate,
|
||
actual: approval_rate,
|
||
});
|
||
}
|
||
|
||
// 更新条款
|
||
clause.content = new_content;
|
||
clause.clause_hash = new_hash;
|
||
|
||
Ok(())
|
||
}
|
||
|
||
// 删除条款(仅限战术级)
|
||
pub fn remove_clause(&mut self, index: u64) -> Result<(), ClauseError> {
|
||
let clause = self.clauses.get(&index)
|
||
.ok_or(ClauseError::ClauseNotFound(index))?;
|
||
|
||
// 只有战术级条款可以删除
|
||
if clause.tier != ClauseTier::Tactical {
|
||
return Err(ClauseError::CannotRemoveClause(clause.tier));
|
||
}
|
||
|
||
self.clauses.remove(&index);
|
||
Ok(())
|
||
}
|
||
}
|
||
```
|
||
|
||
**状态**: ❌ 待实现
|
||
|
||
---
|
||
|
||
### 问题4: 缺少查询功能
|
||
|
||
**严重程度**: ⚠️ 中等
|
||
|
||
**描述**: 只能按索引查询,缺少其他查询方式
|
||
|
||
**建议添加**:
|
||
```rust
|
||
impl ConstitutionRegistry {
|
||
// 按标题查询
|
||
pub fn get_clause_by_title(&self, title: &str) -> Option<&ConstitutionalClause> {
|
||
self.clauses.values().find(|c| c.title == title)
|
||
}
|
||
|
||
// 按层级查询
|
||
pub fn get_clauses_by_tier(&self, tier: ClauseTier) -> Vec<&ConstitutionalClause> {
|
||
self.clauses.values()
|
||
.filter(|c| c.tier == tier)
|
||
.collect()
|
||
}
|
||
|
||
// 查询所有条款
|
||
pub fn get_all_clauses(&self) -> Vec<&ConstitutionalClause> {
|
||
self.clauses.values().collect()
|
||
}
|
||
|
||
// 查询条款数量
|
||
pub fn count(&self) -> usize {
|
||
self.clauses.len()
|
||
}
|
||
|
||
// 按生效时间查询
|
||
pub fn get_effective_clauses(&self, timestamp: u64) -> Vec<&ConstitutionalClause> {
|
||
self.clauses.values()
|
||
.filter(|c| c.effective_from <= timestamp)
|
||
.collect()
|
||
}
|
||
}
|
||
```
|
||
|
||
**状态**: ❌ 待实现
|
||
|
||
---
|
||
|
||
### 问题5: 缺少持久化支持
|
||
|
||
**严重程度**: ⚠️ 高
|
||
|
||
**描述**: 虽然实现了`Serialize`和`Deserialize`,但没有实际的持久化方法
|
||
|
||
**影响**: 重启后丢失所有条款
|
||
|
||
**建议实现**:
|
||
```rust
|
||
use std::fs::File;
|
||
use std::io::{Read, Write};
|
||
|
||
impl ConstitutionRegistry {
|
||
// 保存到JSON文件
|
||
pub fn save_to_file(&self, path: &str) -> Result<(), Box<dyn std::error::Error>> {
|
||
let json = serde_json::to_string_pretty(&self.clauses)?;
|
||
let mut file = File::create(path)?;
|
||
file.write_all(json.as_bytes())?;
|
||
Ok(())
|
||
}
|
||
|
||
// 从JSON文件加载
|
||
pub fn load_from_file(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
||
let mut file = File::open(path)?;
|
||
let mut contents = String::new();
|
||
file.read_to_string(&mut contents)?;
|
||
let clauses = serde_json::from_str(&contents)?;
|
||
Ok(Self { clauses })
|
||
}
|
||
}
|
||
```
|
||
|
||
**状态**: ❌ 待实现
|
||
|
||
---
|
||
|
||
### 问题6: 缺少条款哈希计算
|
||
|
||
**严重程度**: ⚠️ 中等
|
||
|
||
**描述**: 条款哈希需要手动提供,容易出错
|
||
|
||
**建议**:
|
||
```rust
|
||
impl ConstitutionalClause {
|
||
// 计算条款哈希
|
||
pub fn calculate_hash(&self) -> Hash {
|
||
use sha3::{Digest, Sha3_384};
|
||
|
||
let data = format!(
|
||
"{}{}{}{}{}",
|
||
self.clause_index,
|
||
self.title,
|
||
self.content,
|
||
self.effective_from,
|
||
self.tier as u8
|
||
);
|
||
|
||
let hash = Sha3_384::digest(data.as_bytes());
|
||
Hash::from_slice(&hash[..]).unwrap()
|
||
}
|
||
|
||
// 验证哈希
|
||
pub fn verify_hash(&self) -> bool {
|
||
self.clause_hash == self.calculate_hash()
|
||
}
|
||
}
|
||
```
|
||
|
||
**状态**: ❌ 待实现
|
||
|
||
---
|
||
|
||
### 问题7: 缺少条款历史记录
|
||
|
||
**严重程度**: ⚠️ 低
|
||
|
||
**描述**: 条款修改后,旧版本丢失
|
||
|
||
**建议**:
|
||
```rust
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct ClauseHistory {
|
||
pub clause_index: u64,
|
||
pub versions: Vec<ConstitutionalClause>,
|
||
}
|
||
|
||
pub struct ConstitutionRegistry {
|
||
clauses: HashMap<u64, ConstitutionalClause>,
|
||
history: HashMap<u64, ClauseHistory>, // 新增
|
||
}
|
||
```
|
||
|
||
**状态**: ❌ 待实现
|
||
|
||
---
|
||
|
||
## 📊 完成度评估
|
||
|
||
| 功能模块 | 代码行数 | 完成度 | 状态 |
|
||
|---------|---------|--------|------|
|
||
| 数据结构 | 24行 | 100% | ✅ 完成 |
|
||
| 注册表创建 | 6行 | 100% | ✅ 完成 |
|
||
| 条款添加 | 3行 | 30% | ❌ 缺少验证 |
|
||
| 条款查询 | 3行 | 40% | ⚠️ 功能不足 |
|
||
| 条款修改 | 0行 | 0% | ❌ 未实现 |
|
||
| 持久化 | 0行 | 0% | ❌ 未实现 |
|
||
| 测试覆盖 | 0行 | 0% | ❌ 无测试 |
|
||
| **总计** | **49行** | **25%** | **🚧 进行中** |
|
||
|
||
### 待完善功能
|
||
|
||
1. **高优先级**:
|
||
- ❌ 添加条款验证逻辑
|
||
- ❌ 添加单元测试
|
||
- ❌ 实现持久化支持
|
||
- ❌ 实现条款修改功能
|
||
|
||
2. **中优先级**:
|
||
- ❌ 添加查询功能
|
||
- ❌ 实现哈希计算和验证
|
||
- ⏳ 添加条款历史记录
|
||
|
||
3. **低优先级**:
|
||
- ⏳ 优化查询性能
|
||
- ⏳ 添加索引
|
||
- ⏳ 添加文档注释
|
||
|
||
---
|
||
|
||
## 🌟 设计亮点
|
||
|
||
1. **三级分层**
|
||
- Eternal(永恒级)、Strategic(战略级)、Tactical(战术级)
|
||
- 不同层级有不同的修改难度
|
||
|
||
2. **哈希验证**
|
||
- 每个条款都有唯一的哈希值
|
||
- 防止条款被篡改
|
||
|
||
3. **简洁的API**
|
||
- 只有3个公共方法
|
||
- 易于理解和使用
|
||
|
||
4. **序列化支持**
|
||
- 实现了`Serialize`和`Deserialize`
|
||
- 支持JSON格式
|
||
|
||
---
|
||
|
||
## 🔗 模块依赖关系
|
||
|
||
```
|
||
nac-constitution-clauses
|
||
├── 依赖
|
||
│ ├── nac-udm (Hash类型)
|
||
│ ├── serde (序列化)
|
||
│ └── serde_json (JSON序列化)
|
||
├── 被依赖
|
||
│ ├── nac-cee (宪法执行引擎)
|
||
│ ├── nac-constitution-state (宪法状态机)
|
||
│ └── nac-api-server (API接口)
|
||
└── 协作模块
|
||
└── nac-constitution-macros (宪法宏)
|
||
```
|
||
|
||
---
|
||
|
||
## 📝 开发建议
|
||
|
||
### 短期目标 (1周)
|
||
|
||
1. **添加单元测试** (优先级P1)
|
||
- 创建测试
|
||
- 添加测试
|
||
- 查询测试
|
||
|
||
2. **添加条款验证** (优先级P1)
|
||
- 索引验证
|
||
- 哈希验证
|
||
- 时间验证
|
||
|
||
3. **实现哈希计算** (优先级P1)
|
||
- `calculate_hash()`方法
|
||
- `verify_hash()`方法
|
||
|
||
### 中期目标 (2周)
|
||
|
||
4. **实现持久化** (优先级P2)
|
||
- 保存到JSON文件
|
||
- 从JSON文件加载
|
||
- 错误处理
|
||
|
||
5. **实现条款修改** (优先级P2)
|
||
- `update_clause()`方法
|
||
- 权限验证
|
||
- 投票率检查
|
||
|
||
6. **添加查询功能** (优先级P2)
|
||
- 按标题查询
|
||
- 按层级查询
|
||
- 按时间查询
|
||
|
||
### 长期目标 (1个月)
|
||
|
||
7. **添加历史记录** (优先级P3)
|
||
- 条款版本管理
|
||
- 历史查询
|
||
- 历史导出
|
||
|
||
8. **性能优化** (优先级P3)
|
||
- 添加索引
|
||
- 优化查询
|
||
- 减少克隆
|
||
|
||
9. **集成治理** (优先级P4)
|
||
- 与治理模块集成
|
||
- 支持投票
|
||
- 支持提案
|
||
|
||
---
|
||
|
||
## 💡 使用示例
|
||
|
||
```rust
|
||
use nac_constitution_clauses::{ConstitutionRegistry, ConstitutionalClause, ClauseTier};
|
||
use nac_udm::primitives::Hash;
|
||
|
||
// 1. 创建注册表
|
||
let mut registry = ConstitutionRegistry::new();
|
||
|
||
// 2. 添加永恒级条款
|
||
let eternal_clause = ConstitutionalClause {
|
||
clause_index: 1,
|
||
title: "CHAIN_NAME".to_string(),
|
||
content: "本公链名称为New Asset Chain(NAC)".to_string(),
|
||
clause_hash: Hash::from_hex("1234...").unwrap(),
|
||
effective_from: 0,
|
||
tier: ClauseTier::Eternal,
|
||
};
|
||
registry.add_clause(eternal_clause);
|
||
|
||
// 3. 添加战略级条款
|
||
let strategic_clause = ConstitutionalClause {
|
||
clause_index: 10,
|
||
title: "CONSENSUS_PROTOCOL".to_string(),
|
||
content: "本公链采用CBPP共识协议".to_string(),
|
||
clause_hash: Hash::from_hex("5678...").unwrap(),
|
||
effective_from: 0,
|
||
tier: ClauseTier::Strategic,
|
||
};
|
||
registry.add_clause(strategic_clause);
|
||
|
||
// 4. 添加战术级条款
|
||
let tactical_clause = ConstitutionalClause {
|
||
clause_index: 100,
|
||
title: "NET_CONN_MIN_CBP".to_string(),
|
||
content: "CBP节点最小连接数为12".to_string(),
|
||
clause_hash: Hash::from_hex("abcd...").unwrap(),
|
||
effective_from: 1708300800,
|
||
tier: ClauseTier::Tactical,
|
||
};
|
||
registry.add_clause(tactical_clause);
|
||
|
||
// 5. 查询条款
|
||
if let Some(clause) = registry.get_clause(100) {
|
||
println!("条款标题: {}", clause.title);
|
||
println!("条款内容: {}", clause.content);
|
||
println!("条款层级: {:?}", clause.tier);
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
## 🔄 与其他模块的协作
|
||
|
||
### 与nac-cee的协作
|
||
|
||
```rust
|
||
// nac-cee使用条款注册表执行宪法规则
|
||
use nac_cee::ConstitutionEngine;
|
||
use nac_constitution_clauses::ConstitutionRegistry;
|
||
|
||
let registry = ConstitutionRegistry::load_from_file("clauses.json")?;
|
||
let cee = ConstitutionEngine::new(registry);
|
||
|
||
// 执行条款
|
||
if let Some(clause) = registry.get_clause(100) {
|
||
cee.execute_clause(clause)?;
|
||
}
|
||
```
|
||
|
||
### 与nac-constitution-state的协作
|
||
|
||
```rust
|
||
// nac-constitution-state使用条款注册表管理版本
|
||
use nac_constitution_state::{ConstitutionStateMachine, ConstitutionVersion};
|
||
use nac_constitution_clauses::ConstitutionRegistry;
|
||
|
||
let registry = ConstitutionRegistry::load_from_file("clauses.json")?;
|
||
let clause_count = registry.count() as u64;
|
||
|
||
let version = ConstitutionVersion {
|
||
version: 1,
|
||
constitution_hash: Hash::zero(),
|
||
effective_from: 0,
|
||
clause_count,
|
||
};
|
||
|
||
let state_machine = ConstitutionStateMachine::new(version);
|
||
```
|
||
|
||
### 与nac-api-server的协作
|
||
|
||
```rust
|
||
// nac-api-server提供条款查询API
|
||
use nac_api_server::handlers;
|
||
use nac_constitution_clauses::ConstitutionRegistry;
|
||
|
||
async fn get_clause(index: u64) -> Result<String, Error> {
|
||
let registry = ConstitutionRegistry::load_from_file("clauses.json")?;
|
||
if let Some(clause) = registry.get_clause(index) {
|
||
Ok(serde_json::to_string(clause)?)
|
||
} else {
|
||
Err(Error::ClauseNotFound(index))
|
||
}
|
||
}
|
||
|
||
async fn get_all_clauses() -> Result<String, Error> {
|
||
let registry = ConstitutionRegistry::load_from_file("clauses.json")?;
|
||
let clauses = registry.get_all_clauses();
|
||
Ok(serde_json::to_string(&clauses)?)
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
## 📈 扩展建议
|
||
|
||
### 1. 支持条款依赖
|
||
|
||
```rust
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct ConstitutionalClause {
|
||
pub clause_index: u64,
|
||
pub title: String,
|
||
pub content: String,
|
||
pub clause_hash: Hash,
|
||
pub effective_from: u64,
|
||
pub tier: ClauseTier,
|
||
pub dependencies: Vec<u64>, // 新增:依赖的条款索引
|
||
}
|
||
```
|
||
|
||
### 2. 支持条款分类
|
||
|
||
```rust
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||
pub enum ClauseCategory {
|
||
Network, // 网络相关
|
||
Consensus, // 共识相关
|
||
Economic, // 经济相关
|
||
Governance, // 治理相关
|
||
Security, // 安全相关
|
||
}
|
||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct ConstitutionalClause {
|
||
// ...
|
||
pub category: ClauseCategory, // 新增
|
||
}
|
||
```
|
||
|
||
### 3. 支持条款标签
|
||
|
||
```rust
|
||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||
pub struct ConstitutionalClause {
|
||
// ...
|
||
pub tags: Vec<String>, // 新增:标签
|
||
}
|
||
```
|
||
|
||
---
|
||
|
||
**分析完成时间**: 2026-02-18
|
||
**下一步**: 添加条款验证逻辑和单元测试
|