105 lines
3.3 KiB
Rust
105 lines
3.3 KiB
Rust
//! 区块链集成适配器
|
||
//!
|
||
//! 调用NVM和CBPP进行链上操作
|
||
|
||
use crate::error::{OnboardingError, Result};
|
||
use crate::types::{BlockchainResult};
|
||
use nac_udm::l1_protocol::nvm::NVMClient;
|
||
use nac_udm::l1_protocol::cbpp::CBPPConsensus;
|
||
use chrono::Utc;
|
||
use tracing::{info, error};
|
||
|
||
/// 区块链集成适配器
|
||
pub struct BlockchainAdapter {
|
||
nvm_client: NVMClient,
|
||
cbpp: CBPPConsensus,
|
||
}
|
||
|
||
impl BlockchainAdapter {
|
||
/// 创建新的适配器
|
||
pub fn new(rpc_url: String) -> Result<Self> {
|
||
let nvm_client = NVMClient::new(&rpc_url)
|
||
.map_err(|e| OnboardingError::BlockchainIntegrationError(format!("NVM初始化失败: {}", e)))?;
|
||
|
||
let cbpp = CBPPConsensus::new()
|
||
.map_err(|e| OnboardingError::BlockchainIntegrationError(format!("CBPP初始化失败: {}", e)))?;
|
||
|
||
Ok(Self { nvm_client, cbpp })
|
||
}
|
||
|
||
/// 提交到区块链
|
||
pub async fn submit_to_chain(
|
||
&self,
|
||
dna_hash: &str,
|
||
token_address: &str,
|
||
) -> Result<BlockchainResult> {
|
||
info!("开始提交到区块链: dna={}", dna_hash);
|
||
|
||
// 构建交易数据
|
||
let tx_data = self.build_transaction_data(dna_hash, token_address)?;
|
||
|
||
// 提交交易
|
||
let tx_hash = self.nvm_client.send_transaction(&tx_data)
|
||
.await
|
||
.map_err(|e| OnboardingError::BlockchainIntegrationError(format!("交易提交失败: {}", e)))?;
|
||
|
||
// 等待确认
|
||
let receipt = self.nvm_client.wait_for_receipt(&tx_hash)
|
||
.await
|
||
.map_err(|e| OnboardingError::BlockchainIntegrationError(format!("等待确认失败: {}", e)))?;
|
||
|
||
// 获取区块号
|
||
let block_number = receipt.block_number;
|
||
|
||
// 获取区块哈希(48字节SHA3-384)
|
||
let block_hash = receipt.block_hash;
|
||
|
||
info!("区块链提交完成: block={}, hash={}", block_number, block_hash);
|
||
|
||
Ok(BlockchainResult {
|
||
block_number,
|
||
block_hash,
|
||
transaction_hash: tx_hash,
|
||
timestamp: Utc::now(),
|
||
})
|
||
}
|
||
|
||
/// 构建交易数据
|
||
fn build_transaction_data(&self, dna_hash: &str, token_address: &str) -> Result<Vec<u8>> {
|
||
// 简化实现:编码DNA哈希和代币地址
|
||
let mut data = Vec::new();
|
||
data.extend_from_slice(dna_hash.as_bytes());
|
||
data.extend_from_slice(token_address.as_bytes());
|
||
Ok(data)
|
||
}
|
||
|
||
/// 查询区块信息
|
||
pub async fn get_block(&self, block_number: u64) -> Result<String> {
|
||
let block = self.nvm_client.get_block(block_number)
|
||
.await
|
||
.map_err(|e| OnboardingError::BlockchainIntegrationError(format!("查询区块失败: {}", e)))?;
|
||
|
||
Ok(format!("{:?}", block))
|
||
}
|
||
|
||
/// 查询交易信息
|
||
pub async fn get_transaction(&self, tx_hash: &str) -> Result<String> {
|
||
let tx = self.nvm_client.get_transaction(tx_hash)
|
||
.await
|
||
.map_err(|e| OnboardingError::BlockchainIntegrationError(format!("查询交易失败: {}", e)))?;
|
||
|
||
Ok(format!("{:?}", tx))
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
#[test]
|
||
fn test_adapter_creation() {
|
||
let adapter = BlockchainAdapter::new("http://localhost:8545".to_string());
|
||
assert!(adapter.is_ok());
|
||
}
|
||
}
|