132 lines
4.4 KiB
Rust
132 lines
4.4 KiB
Rust
//! L3 聚合层: 文明间路由(ICR)、意识分叉
|
||
//!
|
||
//! 文明间路由 (Inter-Civilization Routing, ICR):
|
||
//! - 基于特征向量的分布式哈希表(DHT)
|
||
//! - 键为特征向量哈希,值为文明接入点列表
|
||
//!
|
||
//! 意识分叉 (Consciousness Fork):
|
||
//! - 文明内对宪法修正产生不可调和分歧时触发
|
||
//! - 超过1/3节点支持分叉
|
||
//! - 新文明继承原文明的灵魂签名基础
|
||
|
||
use crate::types::{CivilizationId, CivilizationVector, ConsciousnessForkProposal, Hash};
|
||
use crate::error::{Nrpc4Error, Result};
|
||
use std::collections::HashMap;
|
||
use tracing::{info, warn};
|
||
|
||
/// 文明间路由器
|
||
pub struct InterCivilizationRouter {
|
||
/// DHT路由表 (文明特征哈希 -> 接入点列表)
|
||
dht: HashMap<Hash, Vec<String>>,
|
||
/// 已知文明列表
|
||
known_civilizations: HashMap<CivilizationId, CivilizationVector>,
|
||
}
|
||
|
||
impl InterCivilizationRouter {
|
||
/// 创建新的文明间路由器
|
||
pub fn new() -> Self {
|
||
info!("Creating new Inter-Civilization Router");
|
||
Self {
|
||
dht: HashMap::new(),
|
||
known_civilizations: HashMap::new(),
|
||
}
|
||
}
|
||
|
||
/// 注册文明
|
||
pub fn register_civilization(&mut self, vector: CivilizationVector) {
|
||
info!("Registering civilization: {}", vector.civilization_id);
|
||
self.known_civilizations.insert(vector.civilization_id.clone(), vector);
|
||
}
|
||
|
||
/// 查找文明接入点
|
||
pub fn find_civilization_entry_points(&self, civ_id: &CivilizationId) -> Result<Vec<String>> {
|
||
if let Some(_vector) = self.known_civilizations.get(civ_id) {
|
||
// 简化实现: 返回模拟的接入点
|
||
Ok(vec![format!("{}:9000", civ_id)])
|
||
} else {
|
||
warn!("Civilization not found: {}", civ_id);
|
||
Err(Nrpc4Error::CivilizationDiscoveryError(format!(
|
||
"Civilization not found: {}",
|
||
civ_id
|
||
)))
|
||
}
|
||
}
|
||
}
|
||
|
||
/// 意识分叉管理器
|
||
pub struct ConsciousnessForkManager {
|
||
/// 活跃的分叉提案
|
||
active_proposals: HashMap<Hash, ConsciousnessForkProposal>,
|
||
}
|
||
|
||
impl ConsciousnessForkManager {
|
||
/// 创建新的意识分叉管理器
|
||
pub fn new() -> Self {
|
||
info!("Creating new Consciousness Fork Manager");
|
||
Self {
|
||
active_proposals: HashMap::new(),
|
||
}
|
||
}
|
||
|
||
/// 提交分叉提案
|
||
pub fn submit_proposal(&mut self, proposal: ConsciousnessForkProposal) {
|
||
info!(
|
||
"Submitting fork proposal: {:?} ({} -> {})",
|
||
proposal.proposal_id, proposal.origin_civilization_id, proposal.new_civilization_id
|
||
);
|
||
self.active_proposals.insert(proposal.proposal_id, proposal);
|
||
}
|
||
|
||
/// 获取提案
|
||
pub fn get_proposal(&self, proposal_id: &Hash) -> Option<&ConsciousnessForkProposal> {
|
||
self.active_proposals.get(proposal_id)
|
||
}
|
||
|
||
/// 检查提案是否达到门限
|
||
pub fn check_threshold(&self, proposal_id: &Hash, total_nodes: u32) -> bool {
|
||
if let Some(proposal) = self.active_proposals.get(proposal_id) {
|
||
let support_count = proposal.supporters.len() as u32;
|
||
let threshold = total_nodes / 3; // 1/3门限
|
||
support_count >= threshold
|
||
} else {
|
||
false
|
||
}
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn test_icr_router() {
|
||
let mut router = InterCivilizationRouter::new();
|
||
let civ_id = "nac-mainnet-001".to_string();
|
||
let vector = CivilizationVector::new(civ_id.clone(), [0u8; 32], [1u8; 32]);
|
||
|
||
router.register_civilization(vector);
|
||
let entry_points = router.find_civilization_entry_points(&civ_id).unwrap();
|
||
assert!(!entry_points.is_empty());
|
||
}
|
||
|
||
#[test]
|
||
fn test_fork_manager() {
|
||
let mut manager = ConsciousnessForkManager::new();
|
||
let proposal = ConsciousnessForkProposal {
|
||
proposal_id: [1u8; 32],
|
||
origin_civilization_id: "nac-mainnet-001".to_string(),
|
||
new_civilization_id: "nac-mainnet-002".to_string(),
|
||
new_constitution_draft: vec![],
|
||
fork_reason: "Test fork".to_string(),
|
||
supporters: vec![[2u8; 32], [3u8; 32], [4u8; 32], [5u8; 32]],
|
||
created_at: 0,
|
||
};
|
||
|
||
manager.submit_proposal(proposal.clone());
|
||
assert!(manager.get_proposal(&proposal.proposal_id).is_some());
|
||
|
||
// 4个支持者,总共10个节点,超过1/3门限
|
||
assert!(manager.check_threshold(&proposal.proposal_id, 10));
|
||
}
|
||
}
|