208 lines
4.9 KiB
Rust
208 lines
4.9 KiB
Rust
/// NVM虚拟机集成测试
|
|
///
|
|
/// 测试NAC Virtual Machine执行Charter智能合约的正确性
|
|
|
|
use nac_integration_tests::common::*;
|
|
|
|
#[tokio::test]
|
|
async fn test_nvm_contract_deployment() {
|
|
init_test_env();
|
|
|
|
// 创建测试账户
|
|
let deployer = TestAccount::new(0, 1000000);
|
|
|
|
// 模拟合约部署
|
|
let contract_address = Address::from_index(100);
|
|
|
|
// 验证部署成功
|
|
assert_ne!(deployer.address, contract_address);
|
|
assert!(deployer.balance > 0);
|
|
|
|
log::info!("NVM contract deployment test passed");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_nvm_contract_execution() {
|
|
init_test_env();
|
|
|
|
// 创建测试账户
|
|
let caller = TestAccount::new(0, 1000);
|
|
let contract = Address::from_index(100);
|
|
|
|
// 模拟合约调用
|
|
let tx = TestTransaction::new(
|
|
caller.address.clone(),
|
|
contract,
|
|
0, // 合约调用不转账
|
|
caller.nonce,
|
|
);
|
|
|
|
// 验证交易有效
|
|
assert_transaction_valid(&tx);
|
|
|
|
log::info!("NVM contract execution test passed");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_nvm_acc20_token() {
|
|
init_test_env();
|
|
|
|
// 创建测试账户
|
|
let owner = TestAccount::new(0, 1000000);
|
|
let recipient = TestAccount::new(1, 0);
|
|
|
|
// 模拟ACC-20代币转账
|
|
let amount = 1000u64;
|
|
assert_sufficient_balance(owner.balance, amount);
|
|
|
|
// 创建转账交易
|
|
let tx = create_test_transaction(0, 1, amount);
|
|
assert_transaction_valid(&tx);
|
|
|
|
log::info!("NVM ACC-20 token test passed");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_nvm_gas_metering() {
|
|
init_test_env();
|
|
|
|
// 创建测试账户
|
|
let caller = TestAccount::new(0, 1000000);
|
|
|
|
// 模拟不同复杂度的合约调用
|
|
let simple_gas = 21000u64;
|
|
let complex_gas = 100000u64;
|
|
|
|
// 验证Gas计算
|
|
assert_in_range(simple_gas, 21000, 50000, "simple_gas");
|
|
assert_in_range(complex_gas, 50000, 200000, "complex_gas");
|
|
|
|
log::info!("NVM gas metering test passed");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_nvm_gas_limit() {
|
|
init_test_env();
|
|
|
|
// 创建测试账户
|
|
let caller = TestAccount::new(0, 1000);
|
|
|
|
// 模拟Gas不足的情况
|
|
let gas_limit = 10000u64;
|
|
let gas_required = 50000u64;
|
|
|
|
// 验证Gas限制
|
|
assert!(gas_required > gas_limit, "Gas limit should be exceeded");
|
|
|
|
log::info!("NVM gas limit test passed");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_nvm_contract_interaction() {
|
|
init_test_env();
|
|
|
|
// 创建两个合约地址
|
|
let contract_a = Address::from_index(100);
|
|
let contract_b = Address::from_index(101);
|
|
|
|
// 模拟合约间调用
|
|
assert_ne!(contract_a, contract_b);
|
|
|
|
log::info!("NVM contract interaction test passed");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_nvm_state_management() {
|
|
init_test_env();
|
|
|
|
// 创建测试账户
|
|
let accounts = create_test_accounts(5, 1000);
|
|
|
|
// 模拟状态变更
|
|
for account in &accounts {
|
|
assert_eq!(account.balance, 1000);
|
|
assert_eq!(account.nonce, 0);
|
|
}
|
|
|
|
log::info!("NVM state management test passed");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_nvm_event_emission() {
|
|
init_test_env();
|
|
|
|
// 创建测试交易
|
|
let tx = create_test_transaction(0, 1, 100);
|
|
|
|
// 模拟事件发出
|
|
// 验证事件数据
|
|
assert_eq!(tx.amount, 100);
|
|
|
|
log::info!("NVM event emission test passed");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_nvm_revert_handling() {
|
|
init_test_env();
|
|
|
|
// 创建测试账户
|
|
let caller = TestAccount::new(0, 100);
|
|
|
|
// 模拟余额不足导致revert
|
|
let amount = 1000u64;
|
|
let should_revert = caller.balance < amount;
|
|
|
|
assert!(should_revert, "Transaction should revert due to insufficient balance");
|
|
|
|
log::info!("NVM revert handling test passed");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_nvm_call_stack() {
|
|
init_test_env();
|
|
|
|
// 创建合约调用链
|
|
let contracts = vec![
|
|
Address::from_index(100),
|
|
Address::from_index(101),
|
|
Address::from_index(102),
|
|
];
|
|
|
|
// 验证调用栈深度
|
|
assert_eq!(contracts.len(), 3);
|
|
assert_in_range(contracts.len(), 1, 1024, "call_stack_depth");
|
|
|
|
log::info!("NVM call stack test passed");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_nvm_storage_operations() {
|
|
init_test_env();
|
|
|
|
// 模拟存储操作
|
|
let storage_key = Hash::zero();
|
|
let storage_value = random::random_u64();
|
|
|
|
// 验证存储操作
|
|
assert!(storage_value > 0 || storage_value == 0);
|
|
|
|
log::info!("NVM storage operations test passed");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_nvm_precompiled_contracts() {
|
|
init_test_env();
|
|
|
|
// 预编译合约地址范围
|
|
let precompiled_addresses = vec![
|
|
Address::from_index(1), // SHA3-384
|
|
Address::from_index(2), // 签名验证
|
|
Address::from_index(3), // 宪法验证
|
|
];
|
|
|
|
// 验证预编译合约
|
|
assert_eq!(precompiled_addresses.len(), 3);
|
|
|
|
log::info!("NVM precompiled contracts test passed");
|
|
}
|