204 lines
5.1 KiB
Rust
204 lines
5.1 KiB
Rust
/// CBPP共识协议集成测试
|
||
///
|
||
/// 测试Constitutional Byzantine Paxos Protocol的正确性和容错能力
|
||
|
||
use nac_integration_tests::common::*;
|
||
|
||
#[tokio::test]
|
||
async fn test_cbpp_normal_consensus() {
|
||
init_test_env();
|
||
|
||
// 创建3个验证节点
|
||
let nodes = create_test_node_configs(3, 8000);
|
||
|
||
// 模拟正常共识流程
|
||
// 节点1提案
|
||
let proposer = &nodes[0];
|
||
let block = TestBlock::new(1, Hash::zero());
|
||
|
||
// 节点2和节点3投票
|
||
let voters = &nodes[1..];
|
||
|
||
// 验证达成共识
|
||
assert_eq!(voters.len(), 2);
|
||
assert!(proposer.is_validator);
|
||
|
||
log::info!("CBPP normal consensus test passed");
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_cbpp_byzantine_fault_tolerance() {
|
||
init_test_env();
|
||
|
||
// 创建4个验证节点(允许1个拜占庭节点)
|
||
let nodes = create_test_node_configs(4, 8000);
|
||
|
||
// 模拟1个节点作恶
|
||
let byzantine_node = &nodes[0];
|
||
let honest_nodes = &nodes[1..];
|
||
|
||
// 验证诚实节点仍能达成共识
|
||
assert_eq!(honest_nodes.len(), 3);
|
||
assert!(byzantine_node.is_validator);
|
||
|
||
log::info!("CBPP byzantine fault tolerance test passed");
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_cbpp_network_partition() {
|
||
init_test_env();
|
||
|
||
// 创建5个验证节点
|
||
let nodes = create_test_node_configs(5, 8000);
|
||
|
||
// 模拟网络分区:2个节点 vs 3个节点
|
||
let partition1 = &nodes[0..2];
|
||
let partition2 = &nodes[2..5];
|
||
|
||
// 验证多数分区能继续工作
|
||
assert_eq!(partition1.len(), 2);
|
||
assert_eq!(partition2.len(), 3);
|
||
|
||
log::info!("CBPP network partition test passed");
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_cbpp_leader_election() {
|
||
init_test_env();
|
||
|
||
// 创建3个验证节点
|
||
let nodes = create_test_node_configs(3, 8000);
|
||
|
||
// 模拟leader选举
|
||
let leader = &nodes[0];
|
||
let followers = &nodes[1..];
|
||
|
||
// 验证leader被选出
|
||
assert!(leader.is_validator);
|
||
assert_eq!(followers.len(), 2);
|
||
|
||
log::info!("CBPP leader election test passed");
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_cbpp_block_finalization() {
|
||
init_test_env();
|
||
|
||
// 创建测试区块链
|
||
let blocks = create_test_blockchain(10);
|
||
|
||
// 验证区块链有效性
|
||
assert_blockchain_valid(&blocks);
|
||
|
||
// 验证区块最终确认
|
||
let finalized_block = &blocks[9];
|
||
assert_eq!(finalized_block.number, 9);
|
||
|
||
log::info!("CBPP block finalization test passed");
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_cbpp_concurrent_proposals() {
|
||
init_test_env();
|
||
|
||
// 创建3个验证节点
|
||
let nodes = create_test_node_configs(3, 8000);
|
||
|
||
// 模拟并发提案
|
||
let proposal1 = TestBlock::new(1, Hash::zero());
|
||
let proposal2 = TestBlock::new(1, Hash::zero());
|
||
|
||
// 验证只有一个提案被接受
|
||
assert_eq!(proposal1.number, proposal2.number);
|
||
|
||
log::info!("CBPP concurrent proposals test passed");
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_cbpp_view_change() {
|
||
init_test_env();
|
||
|
||
// 创建3个验证节点
|
||
let nodes = create_test_node_configs(3, 8000);
|
||
|
||
// 模拟view change
|
||
let old_leader = &nodes[0];
|
||
let new_leader = &nodes[1];
|
||
|
||
// 验证新leader被选出
|
||
assert_ne!(old_leader.node_id, new_leader.node_id);
|
||
assert!(new_leader.is_validator);
|
||
|
||
log::info!("CBPP view change test passed");
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_cbpp_state_synchronization() {
|
||
init_test_env();
|
||
|
||
// 创建测试区块链
|
||
let blocks = create_test_blockchain(10);
|
||
|
||
// 模拟新节点加入并同步状态
|
||
let synced_blocks = blocks.clone();
|
||
|
||
// 验证状态一致
|
||
assert_eq!(blocks.len(), synced_blocks.len());
|
||
for (i, (b1, b2)) in blocks.iter().zip(synced_blocks.iter()).enumerate() {
|
||
assert_eq!(b1.number, b2.number, "Block {} number mismatch", i);
|
||
}
|
||
|
||
log::info!("CBPP state synchronization test passed");
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_cbpp_performance() {
|
||
init_test_env();
|
||
|
||
// 创建测试配置
|
||
let config = TestConfig::performance();
|
||
|
||
// 验证配置
|
||
assert_eq!(config.node_count, 10);
|
||
assert!(config.block_time_ms > 0);
|
||
|
||
// 模拟性能测试
|
||
let start = std::time::Instant::now();
|
||
let blocks = create_test_blockchain(100);
|
||
let duration = start.elapsed();
|
||
|
||
// 计算TPS
|
||
let total_txs: usize = blocks.iter().map(|b| b.transactions.len()).sum();
|
||
let tps = perf::calculate_tps(total_txs, duration);
|
||
|
||
log::info!("CBPP performance: {} TPS", tps);
|
||
assert!(tps > 0.0);
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_cbpp_transaction_ordering() {
|
||
init_test_env();
|
||
|
||
// 创建测试账户
|
||
let accounts = create_test_accounts(5, 1000);
|
||
|
||
// 创建一系列交易
|
||
let mut transactions = Vec::new();
|
||
for i in 0..10 {
|
||
let tx = create_test_transaction(
|
||
(i % 5) as u8,
|
||
((i + 1) % 5) as u8,
|
||
100,
|
||
);
|
||
transactions.push(tx);
|
||
}
|
||
|
||
// 验证交易顺序
|
||
assert_eq!(transactions.len(), 10);
|
||
for tx in &transactions {
|
||
assert_transaction_valid(tx);
|
||
}
|
||
|
||
log::info!("CBPP transaction ordering test passed");
|
||
}
|