NAC_Blockchain/nac-udm/src/lib.rs

300 lines
10 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 Unified Definition Module (UDM)
//!
//! NAC统一定义模块UDM是NewAssetChain区块链生态系统的单一真理来源Single Source of Truth
//! 统一管理所有类型定义、函数签名、常量、版本信息和协议规范。
//!
//! ## 核心特性
//!
//! - ❌ **不使用任何ERC标准**ERC-20、ERC-721、ERC-1155等
//! - ❌ **不继承以太坊或其他链的实现**
//! - ❌ **不是P2P网络** - 使用CSNP宪政结构化网络协议
//! - ✅ **100% NAC原生设计**
//! - ✅ **专注于RWA真实世界资产应用场景**
//! - ✅ **基于CBPP宪政共识不需要投票治理**
//!
//! ## NAC 5层架构体系
//!
//! NAC采用5层架构L0-L5从底层基础设施到顶层应用层层递进
//!
//! ### L5: 应用层 (Application Layer)
//!
//! 最高层级,提供用户交互和业务应用:
//! - **DApps**: 去中心化应用(资产交易、资产管理、资产估值等)
//! - **钱包**: 用户资产管理工具
//! - **浏览器**: 区块链数据查询和可视化
//! - **Charter合约**: 智能合约执行环境
//!
//! ### L4: AI层 (AI Layer)
//!
//! AI服务层统一管理所有AI功能NAC是AI公链
//! - **宪法执行引擎(CEE)**: AI驱动的宪法规则执行
//! - **AI合规审批**: KYC/AML自动审核、欺诈检测
//! - **AI估值**: 资产估值、风险评估、市场预测
//! - **XTZH AI**: 宏观感知AI、汇率预测
//! - **AI预言机**: 多节点AI共识、zkML证明
//! - **AI治理**: 提案分析、投票建议、审计AI
//!
//! ### L3: 存储层 (Storage Layer)
//!
//! 独立的存储层,负责所有持久化数据的存储与证明:
//! - **状态数据库**: 账户状态、资产状态、合约状态
//! - **Merkle树**: 状态证明、交易证明
//! - **归档存储**: 历史区块、历史交易
//! - **证据存证**: 法典、合规证据、审计日志
//!
//! ### L2: 宪法规则层 (Constitutional Governance Layer)
//!
//! 宪法治理层,定义整个系统的治理规则(**不是投票治理,而是规则定义层**
//! - **CNNL宪法语言**: 宪法条款的定义语言
//! - **宪法状态机**: 宪法规则的执行引擎
//! - **辖区协商接口**: 多司法辖区的协商机制
//! - **法典管理**: 宪法法典、条款法典、主权法典、AI治理法典等
//!
//! ### L1: 协议层 (Protocol Layer)
//!
//! 核心协议层,实现"宪法即共识"
//! - **CBPP**: 宪政区块生产协议宪法收据CR、OPN、FBM、RVGP
//! - **CSNP**: 宪政结构化网络协议GIDS、MA-RCM、AA-PE、FTAN、UCA
//! - **NVM**: 虚拟机层225个基础操作码 + 125个RWA专属指令
//! - **ACC**: 13个原生资产协议ACC-20, ACC-721, ACC-1155, ACC-RWA等
//! - **XTZH**: 资产稳定币系统SDR锚定 + 黄金储备)
//!
//! ### L0: 基础设施层 (Foundation Layer)
//!
//! 最底层提供所有上层依赖的基础设施包含4个子层
//! - **密码学子层**: HashSHA3-384、签名Ed25519、随机数、加密
//! - **网络子层**: P2P通信、Gossip协议、节点发现
//! - **序列化子层**: nac_serdeNAC专用序列化
//! - **存储子层**: 持久化接口为L3存储层提供底层接口
//!
//! ## 设计哲学
//!
//! "宪法治下,节点产生区块,参与即是共识,交易扩张区块的大小和高度。"
//!
//! ## 快速开始
//!
//! ```rust
//! use nac_udm::primitives::{Hash, Timestamp, Signature};
//! use nac_udm::l1_protocol::cbpp::ConstitutionalReceipt;
//! use nac_udm::l1_protocol::gnacs::{GNACSCode, AssetCategory, Jurisdiction, ComplianceLevel, RiskLevel};
//!
//! // 创建宪法收据
//! let transaction_hash = Hash::sha3_384(b"tx");
//! let constitutional_hash = Hash::sha3_384(b"constitution");
//! let execution_result_hash = Hash::sha3_384(b"result");
//! let issuer_pubkey = vec![1u8; 33];
//!
//! let mut receipt = ConstitutionalReceipt::new(
//! transaction_hash,
//! constitutional_hash,
//! execution_result_hash,
//! issuer_pubkey,
//! 3600, // validity_window
//! );
//!
//! // 签名收据
//! receipt.sign(Signature::new(vec![0u8; 64]));
//!
//! // 验证收据
//! let current_time = Timestamp::now();
//! assert!(receipt.verify(current_time, &constitutional_hash));
//!
//! // 创建GNACS资产编码实物资产中国司法辖区高合规低风险
//! let gnacs = GNACSCode::new(
//! AssetCategory::Physical,
//! 0x01, // 子类别
//! Jurisdiction::CN,
//! ComplianceLevel::High,
//! RiskLevel::Low,
//! );
//!
//! // 验证GNACS编码
//! assert!(gnacs.verify_checksum());
//! assert_eq!(gnacs.category(), AssetCategory::Physical);
//! assert_eq!(gnacs.jurisdiction(), Jurisdiction::CN);
//! println!("GNACS: {}", gnacs.format_readable());
//! ```
#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]
#![warn(rustdoc::missing_crate_level_docs)]
/// 核心注册表系统
pub mod registry;
/// 基础原语类型
pub mod primitives;
/// 工具函数
pub mod utils;
/// L5: 应用层 (Application Layer) - DApps、钱包、浏览器、Charter合约
pub mod l5_application;
/// L4: AI层 (AI Layer) - CEE、AI合规、AI估值、XTZH AI、AI预言机
pub mod l4_ai;
/// L3: 存储层 (Storage Layer) - 状态数据库、Merkle树、归档存储、证据存证
pub mod l3_storage;
/// L2: 宪法规则层 (Constitutional Governance Layer) - CNNL、宪法状态机、法典管理
pub mod l2_governance;
/// L1: 协议层 (Protocol Layer) - CBPP、CSNP、NVM、ACC、XTZH
pub mod l1_protocol;
/// L0: 基础设施层 (Foundation Layer) - 密码学、网络、序列化、存储接口
pub mod l0_native;
/// 资产加密DNA管理系统模块
pub mod asset_dna;
/// 预导入模块包含最常用的类型和trait
pub mod prelude {
pub use crate::registry::{UID, Version, DefinitionRegistry};
pub use crate::primitives::prelude::*;
#[allow(unused_imports)]
pub use crate::utils::*;
pub use crate::l2_governance::*;
pub use crate::l1_protocol::*;
pub use crate::l0_native::*;
}
/// UDM版本信息
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
/// UDM名称
pub const NAME: &str = env!("CARGO_PKG_NAME");
/// UDM描述
pub const DESCRIPTION: &str = env!("CARGO_PKG_DESCRIPTION");
/// NAC区块链架构层级5层架构
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// ArchitectureLayer
pub enum ArchitectureLayer {
/// L5: 应用层DApps、钱包、浏览器、Charter合约
L5Application,
/// L4: AI层CEE、AI合规、AI估值、XTZH AI、AI预言机
L4AI,
/// L3: 存储层状态数据库、Merkle树、归档存储、证据存证
L3Storage,
/// L2: 宪法规则层CNNL、宪法状态机、法典管理
L2Governance,
/// L1: 协议层CBPP、CSNP、NVM、ACC、XTZH
L1Protocol,
/// L0: 基础设施层(密码学、网络、序列化、存储接口)
L0CSNP,
}
impl std::fmt::Display for ArchitectureLayer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::L5Application => write!(f, "L5: Application Layer (DApps, Wallet, Explorer, Charter)"),
Self::L4AI => write!(f, "L4: AI Layer (CEE, AI Compliance, AI Valuation)"),
Self::L3Storage => write!(f, "L3: Storage Layer (State DB, Merkle Tree, Archive)"),
Self::L2Governance => write!(f, "L2: Constitutional Governance Layer (Rule Definition)"),
Self::L1Protocol => write!(f, "L1: Multi-Chain Protocol Layer (CBPP Consensus)"),
Self::L0CSNP => write!(f, "L0: CSNP Structured Network Layer (Not P2P)"),
}
}
}
/// 网络类型
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// NetworkType
pub enum NetworkType {
/// CSNP宪政结构化网络NAC使用
CSNP,
/// P2P对等网络NAC不使用
#[deprecated(note = "NAC does not use P2P network, use CSNP instead")]
/// P2P
P2P,
}
impl std::fmt::Display for NetworkType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::CSNP => write!(f, "CSNP (Constitutional Structured Network Protocol)"),
#[allow(deprecated)]
Self::P2P => write!(f, "P2P (Peer-to-Peer) - NOT USED IN NAC"),
}
}
}
/// 共识类型
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
/// ConsensusType
pub enum ConsensusType {
/// CBPP宪政区块生产协议NAC使用
CBPP,
/// PoS权益证明NAC不使用
#[deprecated(note = "NAC does not use PoS, use CBPP instead")]
/// PoS
PoS,
/// PoW工作量证明NAC不使用
#[deprecated(note = "NAC does not use PoW, use CBPP instead")]
/// PoW
PoW,
}
impl std::fmt::Display for ConsensusType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::CBPP => write!(f, "CBPP (Constitutional Block Production Protocol)"),
#[allow(deprecated)]
Self::PoS => write!(f, "PoS (Proof of Stake) - NOT USED IN NAC"),
#[allow(deprecated)]
Self::PoW => write!(f, "PoW (Proof of Work) - NOT USED IN NAC"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_version() {
assert!(!VERSION.is_empty());
assert_eq!(VERSION, "1.0.0");
}
#[test]
fn test_name() {
assert_eq!(NAME, "nac-udm");
}
#[test]
fn test_architecture_layers() {
assert_eq!(
ArchitectureLayer::L2Governance.to_string(),
"L2: Constitutional Governance Layer (Rule Definition)"
);
assert_eq!(
ArchitectureLayer::L1Protocol.to_string(),
"L1: Multi-Chain Protocol Layer (CBPP Consensus)"
);
assert_eq!(
ArchitectureLayer::L0CSNP.to_string(),
"L0: CSNP Structured Network Layer (Not P2P)"
);
}
#[test]
fn test_network_type() {
assert_eq!(
NetworkType::CSNP.to_string(),
"CSNP (Constitutional Structured Network Protocol)"
);
}
#[test]
fn test_consensus_type() {
assert_eq!(
ConsensusType::CBPP.to_string(),
"CBPP (Constitutional Block Production Protocol)"
);
}
}