NAC_Blockchain/protocol/nac-constitution-clauses/src/lib.rs

159 lines
4.5 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! NAC宪法条款管理系统
//!
//! 提供完整的宪法条款管理功能,包括:
//! - 三级分层Eternal/Strategic/Tactical
//! - 条款验证(内容、层级、依赖、冲突)
//! - 条款持久化(保存、加载、查询)
//! - 条款修改(添加、更新、删除、版本管理)
//! - 生命周期管理(激活、停用、废止、优先级)
use nac_udm::primitives::Hash;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
// 导出子模块
pub mod validator;
pub mod storage;
pub mod manager;
pub mod lifecycle;
pub mod upgrade;
pub mod node_sharing;
// 重新导出常用类型
pub use validator::{ClauseValidator, ValidationError};
pub use storage::{ClauseStorage, StorageError};
pub use manager::{ConstitutionManager, ManagerError, ClauseVersion, ClauseStatistics};
pub use lifecycle::{LifecycleManager, ClauseLifecycle, ClauseStatus, StatusStatistics};
pub use upgrade::{
UpgradeManager, UpgradeExecutor, UpgradeProposal, ProposalStatus, ProposalType,
ReviewResult, ExecutionRecord, ProposalStatistics,
};
/// 宪法条款
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConstitutionalClause {
/// 条款索引
pub clause_index: u64,
/// 条款标题
pub title: String,
/// 条款内容
pub content: String,
/// 条款哈希SHA3-384
pub clause_hash: Hash,
/// 生效时间Unix时间戳
pub effective_from: u64,
/// 条款层级
pub tier: ClauseTier,
}
/// 条款层级
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ClauseTier {
/// 永恒级索引1-100
/// 最高级别,定义核心价值观和基本原则
Eternal,
/// 战略级索引101-1000
/// 中级别,定义长期战略和重要规则
Strategic,
/// 战术级索引1001+
/// 基础级别,定义具体操作和细节规范
Tactical,
}
/// 宪法注册表(保持向后兼容)
pub struct ConstitutionRegistry {
clauses: HashMap<u64, ConstitutionalClause>,
}
impl ConstitutionRegistry {
/// 创建新的注册表
pub fn new() -> Self {
Self {
clauses: HashMap::new(),
}
}
/// 添加条款
pub fn add_clause(&mut self, clause: ConstitutionalClause) {
self.clauses.insert(clause.clause_index, clause);
}
/// 获取条款
pub fn get_clause(&self, index: u64) -> Option<&ConstitutionalClause> {
self.clauses.get(&index)
}
/// 获取所有条款
pub fn list_all_clauses(&self) -> Vec<&ConstitutionalClause> {
self.clauses.values().collect()
}
/// 按层级获取条款
pub fn list_clauses_by_tier(&self, tier: ClauseTier) -> Vec<&ConstitutionalClause> {
self.clauses
.values()
.filter(|clause| clause.tier == tier)
.collect()
}
}
impl Default for ConstitutionRegistry {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_constitution_registry() {
let mut registry = ConstitutionRegistry::new();
let clause = ConstitutionalClause {
clause_index: 1,
title: "测试条款".to_string(),
content: "测试内容".to_string(),
clause_hash: Hash::zero(),
effective_from: 1000,
tier: ClauseTier::Eternal,
};
registry.add_clause(clause);
let loaded = registry.get_clause(1).expect("mainnet: handle error");
assert_eq!(loaded.clause_index, 1);
assert_eq!(loaded.title, "测试条款");
}
#[test]
fn test_list_by_tier() {
let mut registry = ConstitutionRegistry::new();
registry.add_clause(ConstitutionalClause {
clause_index: 1,
title: "永恒级条款".to_string(),
content: "内容".to_string(),
clause_hash: Hash::zero(),
effective_from: 1000,
tier: ClauseTier::Eternal,
});
registry.add_clause(ConstitutionalClause {
clause_index: 101,
title: "战略级条款".to_string(),
content: "内容".to_string(),
clause_hash: Hash::zero(),
effective_from: 1000,
tier: ClauseTier::Strategic,
});
let eternal = registry.list_clauses_by_tier(ClauseTier::Eternal);
assert_eq!(eternal.len(), 1);
let strategic = registry.list_clauses_by_tier(ClauseTier::Strategic);
assert_eq!(strategic.len(), 1);
}
}