NAC_Blockchain/nac-csnp-l1/src/lib.rs

243 lines
5.8 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 CSNP L1层 - Charter合约实现
//!
//! 实现CSNP网络配置合约
//! 基于CSNP V2.0技术白皮书
use nac_udm::primitives::{Address, Hash};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum CsnpL1Error {
#[error("Invalid configuration: {0}")]
InvalidConfiguration(String),
#[error("Unauthorized: {0:?}")]
Unauthorized(Address),
#[error("Configuration not found: {0}")]
NotFound(String),
}
/// GIDS配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GidsConfig {
/// DHT节点数量
pub dht_node_count: u32,
/// 链上注册表合约地址
pub registry_contract: Address,
/// 信誉聚合权重
pub reputation_weights: HashMap<u64, f64>, // chain_id -> weight
/// 缓存TTL
pub cache_ttl_secs: u64,
}
impl Default for GidsConfig {
fn default() -> Self {
Self {
dht_node_count: 100,
registry_contract: Address::zero(),
reputation_weights: HashMap::new(),
cache_ttl_secs: 3600,
}
}
}
/// AA-PE传播策略配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AaPeConfig {
/// 即时广播的最大延迟(毫秒)
pub immediate_broadcast_max_latency_ms: u64,
/// 定向推送的目标节点数
pub targeted_push_node_count: u32,
/// 警报通道优先级
pub alert_channel_priority: u8,
/// 缓存TTL
pub cache_ttl_secs: u64,
}
impl Default for AaPeConfig {
fn default() -> Self {
Self {
immediate_broadcast_max_latency_ms: 200,
targeted_push_node_count: 10,
alert_channel_priority: 255,
cache_ttl_secs: 3600,
}
}
}
/// FTAN配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FtanConfig {
/// 聚合节点数量
pub aggregator_count: u32,
/// 批量大小
pub batch_size: u32,
/// 聚合超时(毫秒)
pub aggregation_timeout_ms: u64,
}
impl Default for FtanConfig {
fn default() -> Self {
Self {
aggregator_count: 50,
batch_size: 100,
aggregation_timeout_ms: 500,
}
}
}
/// UCA配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UcaConfig {
/// 审计日志保留期(天)
pub audit_log_retention_days: u32,
/// 跨链验证节点数
pub cross_chain_validator_count: u32,
/// 违规惩罚系数
pub violation_penalty_factor: f64,
}
impl Default for UcaConfig {
fn default() -> Self {
Self {
audit_log_retention_days: 365,
cross_chain_validator_count: 21,
violation_penalty_factor: 0.1,
}
}
}
/// CSNP网络配置合约
pub struct CsnpConfigContract {
/// GIDS配置
gids_config: GidsConfig,
/// AA-PE配置
aape_config: AaPeConfig,
/// FTAN配置
ftan_config: FtanConfig,
/// UCA配置
uca_config: UcaConfig,
/// 管理员地址
admin: Address,
}
impl CsnpConfigContract {
/// 创建新的CSNP配置合约
pub fn new(admin: Address) -> Self {
Self {
gids_config: GidsConfig::default(),
aape_config: AaPeConfig::default(),
ftan_config: FtanConfig::default(),
uca_config: UcaConfig::default(),
admin,
}
}
/// 更新GIDS配置
pub fn update_gids_config(
&mut self,
caller: Address,
config: GidsConfig,
) -> Result<(), CsnpL1Error> {
if caller != self.admin {
return Err(CsnpL1Error::Unauthorized(caller));
}
// 验证配置
if config.dht_node_count < 10 {
return Err(CsnpL1Error::InvalidConfiguration(
"DHT节点数量至少为10".to_string()
));
}
self.gids_config = config;
Ok(())
}
/// 更新AA-PE配置
pub fn update_aape_config(
&mut self,
caller: Address,
config: AaPeConfig,
) -> Result<(), CsnpL1Error> {
if caller != self.admin {
return Err(CsnpL1Error::Unauthorized(caller));
}
// 验证配置
if config.immediate_broadcast_max_latency_ms > 1000 {
return Err(CsnpL1Error::InvalidConfiguration(
"即时广播延迟不能超过1000ms".to_string()
));
}
self.aape_config = config;
Ok(())
}
/// 更新FTAN配置
pub fn update_ftan_config(
&mut self,
caller: Address,
config: FtanConfig,
) -> Result<(), CsnpL1Error> {
if caller != self.admin {
return Err(CsnpL1Error::Unauthorized(caller));
}
self.ftan_config = config;
Ok(())
}
/// 更新UCA配置
pub fn update_uca_config(
&mut self,
caller: Address,
config: UcaConfig,
) -> Result<(), CsnpL1Error> {
if caller != self.admin {
return Err(CsnpL1Error::Unauthorized(caller));
}
self.uca_config = config;
Ok(())
}
/// 获取GIDS配置
pub fn get_gids_config(&self) -> &GidsConfig {
&self.gids_config
}
/// 获取AA-PE配置
pub fn get_aape_config(&self) -> &AaPeConfig {
&self.aape_config
}
/// 获取FTAN配置
pub fn get_ftan_config(&self) -> &FtanConfig {
&self.ftan_config
}
/// 获取UCA配置
pub fn get_uca_config(&self) -> &UcaConfig {
&self.uca_config
}
/// 转移管理员权限
pub fn transfer_admin(
&mut self,
caller: Address,
new_admin: Address,
) -> Result<(), CsnpL1Error> {
if caller != self.admin {
return Err(CsnpL1Error::Unauthorized(caller));
}
self.admin = new_admin;
Ok(())
}
}