//! L2宪政/治理/网络层适配器 //! //! 提供NAC公链L2层的核心功能: //! - 宪政层:宪法合规性检查和修正案管理 //! - 治理层:提案创建、投票和执行 //! - 网络层:CSNP网络通信和节点管理 //! //! # 示例 //! //! ```rust //! use nac_sdk::adapters::{L2Adapter, config::L2Config}; //! //! # async fn example() -> Result<(), Box> { //! // 创建L2适配器 //! let config = L2Config { //! constitutional_url: "http://localhost:8546".to_string(), //! governance_url: "http://localhost:8547".to_string(), //! network_peers: vec!["peer1:9000".to_string(), "peer2:9000".to_string()], //! }; //! let l2 = L2Adapter::new(&config).await?; //! //! // 查询投票权重 //! let voting_power = l2.get_voting_power(&my_address).await?; //! println!("Voting power: {}", voting_power); //! //! # Ok(()) //! # } //! ``` use crate::error::{NACError, Result}; use super::config::L2Config; use nac_udm::primitives::{Address, Hash, Decimal}; use nac_udm::types::{ Transaction, SignedTransaction, Block, Amendment, AmendmentStatus, Proposal, ProposalDetails, Vote, ComplianceResult, PeerInfo, }; use nac_lens::client::NRPC4Client; use nac_csnp::network::CSNPNetwork; use std::sync::Arc; /// 提案ID类型 pub type ProposalId = u64; /// L2宪政/治理/网络层适配器 /// /// 统一封装宪政、治理、网络三个子系统 #[derive(Debug, Clone)] pub struct L2Adapter { /// 宪政层客户端 constitutional_client: NRPC4Client, /// 治理层客户端 governance_client: NRPC4Client, /// CSNP网络 network: Arc, } impl L2Adapter { /// 创建新的L2适配器 /// /// # 参数 /// /// * `config` - L2层配置 /// /// # 返回 /// /// 返回初始化完成的L2适配器实例 pub async fn new(config: &L2Config) -> Result { let constitutional_client = NRPC4Client::new(&config.constitutional_url, std::time::Duration::from_secs(30)) .map_err(|e| NACError::NetworkError(format!("Failed to create constitutional client: {}", e)))?; let governance_client = NRPC4Client::new(&config.governance_url, std::time::Duration::from_secs(30)) .map_err(|e| NACError::NetworkError(format!("Failed to create governance client: {}", e)))?; let network = Arc::new(CSNPNetwork::new(&config.network_peers).await .map_err(|e| NACError::NetworkError(format!("Failed to create CSNP network: {}", e)))?); Ok(Self { constitutional_client, governance_client, network, }) } // ===== 宪政层 ===== /// 检查交易的宪政合规性 /// /// # 参数 /// /// * `tx` - 待检查的交易 /// /// # 返回 /// /// 返回合规性检查结果 pub async fn check_constitutional_compliance( &self, tx: &Transaction, ) -> Result { self.constitutional_client .check_compliance(tx) .await .map_err(|e| NACError::ValidationError(format!("Failed to check constitutional compliance: {}", e))) } /// 提出宪法修正案 /// /// # 参数 /// /// * `amendment` - 修正案内容 /// * `proposer` - 提案者地址 /// /// # 返回 /// /// 返回提案ID pub async fn propose_amendment( &self, amendment: &Amendment, proposer: &Address, ) -> Result { self.constitutional_client .propose_amendment(amendment, proposer) .await .map_err(|e| NACError::NetworkError(format!("Failed to propose amendment: {}", e))) } /// 对修正案投票 /// /// # 参数 /// /// * `proposal_id` - 提案ID /// * `vote` - 投票选项 /// * `voter` - 投票者地址 /// /// # 返回 /// /// 返回投票交易哈希 pub async fn vote_on_amendment( &self, proposal_id: ProposalId, vote: Vote, voter: &Address, ) -> Result { self.constitutional_client .vote_on_amendment(proposal_id, vote, voter) .await .map_err(|e| NACError::NetworkError(format!("Failed to vote on amendment: {}", e))) } /// 查询修正案状态 /// /// # 参数 /// /// * `proposal_id` - 提案ID /// /// # 返回 /// /// 返回修正案状态 pub async fn get_amendment_status( &self, proposal_id: ProposalId, ) -> Result { self.constitutional_client .get_amendment_status(proposal_id) .await .map_err(|e| NACError::NetworkError(format!("Failed to get amendment status: {}", e))) } // ===== 治理层 ===== /// 创建治理提案 /// /// # 参数 /// /// * `proposal` - 提案内容 /// * `proposer` - 提案者地址 /// /// # 返回 /// /// 返回提案ID pub async fn create_proposal( &self, proposal: &Proposal, proposer: &Address, ) -> Result { self.governance_client .create_proposal(proposal, proposer) .await .map_err(|e| NACError::NetworkError(format!("Failed to create proposal: {}", e))) } /// 对提案投票 /// /// # 参数 /// /// * `proposal_id` - 提案ID /// * `vote` - 投票选项 /// * `voter` - 投票者地址 /// /// # 返回 /// /// 返回投票交易哈希 pub async fn vote_on_proposal( &self, proposal_id: ProposalId, vote: Vote, voter: &Address, ) -> Result { self.governance_client .vote_on_proposal(proposal_id, vote, voter) .await .map_err(|e| NACError::NetworkError(format!("Failed to vote on proposal: {}", e))) } /// 执行通过的提案 /// /// # 参数 /// /// * `proposal_id` - 提案ID /// * `executor` - 执行者地址 /// /// # 返回 /// /// 返回执行交易哈希 pub async fn execute_proposal( &self, proposal_id: ProposalId, executor: &Address, ) -> Result { self.governance_client .execute_proposal(proposal_id, executor) .await .map_err(|e| NACError::NetworkError(format!("Failed to execute proposal: {}", e))) } /// 查询提案详情 /// /// # 参数 /// /// * `proposal_id` - 提案ID /// /// # 返回 /// /// 返回提案详情 pub async fn get_proposal( &self, proposal_id: ProposalId, ) -> Result { self.governance_client .get_proposal(proposal_id) .await .map_err(|e| NACError::NetworkError(format!("Failed to get proposal: {}", e))) } /// 查询投票权重 /// /// # 参数 /// /// * `voter` - 投票者地址 /// /// # 返回 /// /// 返回投票权重 pub async fn get_voting_power( &self, voter: &Address, ) -> Result { self.governance_client .get_voting_power(voter) .await .map_err(|e| NACError::NetworkError(format!("Failed to get voting power: {}", e))) } // ===== 网络层 (CSNP) ===== /// 广播交易到网络 /// /// # 参数 /// /// * `tx` - 已签名的交易 /// /// # 返回 /// /// 成功返回Ok(()) pub async fn broadcast_transaction( &self, tx: &SignedTransaction, ) -> Result<()> { self.network .broadcast_transaction(tx) .await .map_err(|e| NACError::NetworkError(format!("Failed to broadcast transaction: {}", e))) } /// 广播区块到网络 /// /// # 参数 /// /// * `block` - 区块对象 /// /// # 返回 /// /// 成功返回Ok(()) pub async fn broadcast_block( &self, block: &Block, ) -> Result<()> { self.network .broadcast_block(block) .await .map_err(|e| NACError::NetworkError(format!("Failed to broadcast block: {}", e))) } /// 同步区块 /// /// # 参数 /// /// * `from_height` - 起始区块高度 /// * `to_height` - 结束区块高度 /// /// # 返回 /// /// 返回区块列表 pub async fn sync_blocks( &self, from_height: u64, to_height: u64, ) -> Result> { self.network .sync_blocks(from_height, to_height) .await .map_err(|e| NACError::NetworkError(format!("Failed to sync blocks: {}", e))) } /// 查询网络节点 /// /// # 返回 /// /// 返回节点信息列表 pub async fn get_peers(&self) -> Result> { self.network .get_peers() .await .map_err(|e| NACError::NetworkError(format!("Failed to get peers: {}", e))) } /// 连接到节点 /// /// # 参数 /// /// * `peer_addr` - 节点地址 /// /// # 返回 /// /// 成功返回Ok(()) pub async fn connect_to_peer( &self, peer_addr: &str, ) -> Result<()> { self.network .connect_to_peer(peer_addr) .await .map_err(|e| NACError::NetworkError(format!("Failed to connect to peer: {}", e))) } }