Skip to content

Commit

Permalink
fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
simonjiao committed Apr 14, 2024
1 parent 515a7fc commit fce9ffb
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 15 deletions.
7 changes: 3 additions & 4 deletions chain/force-upgrade/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ use stdlib::COMPILED_MOVE_CODE_DIR;
pub struct ForceUpgrade;

impl ForceUpgrade {
///
/// block_timestamp: *NOTE* by seconds,
// block_timestamp: *NOTE* by seconds,
pub fn force_deploy_txn(
account: Account,
sequence_number: u64,
block_timestamp: u64,
block_timestamp_in_secs: u64,
chain_id: &ChainId,
) -> anyhow::Result<SignedUserTransaction> {
let package_file = "12/11-12/stdlib.blob".to_string();
Expand Down Expand Up @@ -56,7 +55,7 @@ impl ForceUpgrade {
TransactionPayload::Package(package),
DEFAULT_MAX_GAS_AMOUNT,
1,
block_timestamp + DEFAULT_EXPIRATION_TIME,
block_timestamp_in_secs + DEFAULT_EXPIRATION_TIME,
*chain_id,
STC_TOKEN_CODE_STR.to_string(),
)))
Expand Down
28 changes: 28 additions & 0 deletions chain/tests/test_force_upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::format_err;
use starcoin_accumulator::Accumulator;
use starcoin_chain_api::{ChainReader, ChainWriter};
use starcoin_config::NodeConfig;
use starcoin_consensus::Consensus;
Expand All @@ -25,6 +26,9 @@ pub fn test_force_upgrade_1() -> anyhow::Result<()> {
let current_version = get_stdlib_version(miner_db)?;
assert_eq!(current_version, 11);

// 1 genesis meta
assert_eq!(miner.get_txn_accumulator().num_leaves(), 1);

// create two txns to deposit some tokens to two black addresses
// and a third random address which should not in black address list.
let (black1, txn1, black2, txn2, rand3, txn3) = {
Expand Down Expand Up @@ -79,6 +83,9 @@ pub fn test_force_upgrade_1() -> anyhow::Result<()> {

miner.apply(block)?;

// 1 meta + 3 user = 4 txns
assert_eq!(miner.get_txn_accumulator().num_leaves(), 5);

assert_eq!(
get_balance(black1, miner.chain_state()),
initial_balance + 1
Expand Down Expand Up @@ -111,6 +118,9 @@ pub fn test_force_upgrade_1() -> anyhow::Result<()> {

miner.apply(block2.clone())?;

// 1 meta + 1 extra = 2 txns
assert_eq!(miner.get_txn_accumulator().num_leaves(), 7);

assert_eq!(
get_balance(black1, miner.chain_state()),
0,
Expand All @@ -137,6 +147,9 @@ pub fn test_force_upgrade_1() -> anyhow::Result<()> {

chain_to_apply.apply(block_num_2)?;

// 1 meta + 1 extra = 2 txns
assert_eq!(chain_to_apply.get_txn_accumulator().num_leaves(), 7);

assert_eq!(get_balance(black1, chain_to_apply.chain_state()), 0);
assert_eq!(get_balance(black2, chain_to_apply.chain_state()), 0);
assert_eq!(
Expand All @@ -152,6 +165,21 @@ pub fn test_force_upgrade_1() -> anyhow::Result<()> {
Ok(())
}

#[stest::test]
fn test_force_upgrade_2() -> anyhow::Result<()> {
let config = Arc::new(NodeConfig::random_for_test());
let chain = test_helper::gen_blockchain_with_blocks_for_test(2, config.net())?;

// genesis 1 + block1 1 + block2 1meta+1extra.txn
assert_eq!(chain.get_txn_accumulator().num_leaves(), 4);

let chain = test_helper::gen_blockchain_with_blocks_for_test(3, config.net())?;
// genesis 1 + block1 1 + special block2 2 + block3 1
assert_eq!(chain.get_txn_accumulator().num_leaves(), 5);

Ok(())
}

fn get_stdlib_version(chain_state_db: &ChainStateDB) -> anyhow::Result<u64> {
chain_state_db
.get_on_chain_config::<Version>()?
Expand Down
34 changes: 26 additions & 8 deletions chain/tests/test_txn_info_and_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn test_transaction_info_and_proof() -> Result<()> {
let mut block_chain = test_helper::gen_blockchain_for_test(config.net())?;
let mut current_header = block_chain.current_header();
let miner_account = AccountInfo::random();
let mut force_upgrade_txn_idx_for_test = 0;

let mut rng = rand::thread_rng();

Expand Down Expand Up @@ -75,11 +76,22 @@ fn test_transaction_info_and_proof() -> Result<()> {
block.to_metadata(current_header.gas_used()),
));
all_txns.extend(txns.into_iter().map(Transaction::UserTransaction));
if block.header().number() == 2 {
seq_number += 1;
force_upgrade_txn_idx_for_test = all_txns.len();
}
current_header = block.header().clone();
});

assert_ne!(force_upgrade_txn_idx_for_test, 0);

let txn_index = rng.gen_range(0..all_txns.len());
debug!("all txns len: {}, txn index:{}", all_txns.len(), txn_index);
debug!(
"all txns len: {}, txn index:{}, total num of txns in chain {}",
all_txns.len(),
txn_index,
block_chain.get_txn_accumulator().num_leaves()
);

for txn_global_index in 0..all_txns.len() {
let txn = all_txns.get(txn_global_index).cloned().unwrap();
Expand All @@ -92,21 +104,27 @@ fn test_transaction_info_and_proof() -> Result<()> {
)
})?;

let real_txn_global_index = if txn_global_index >= force_upgrade_txn_idx_for_test {
txn_global_index + 1
} else {
txn_global_index
};

let txn_info_leaf = block_chain
.get_txn_accumulator()
.get_leaf(txn_global_index as u64)?
.get_leaf(real_txn_global_index as u64)?
.unwrap();
assert_eq!(
txn_info.transaction_info.id(),
txn_info_leaf,
"txn_info hash do not match txn info leaf in accumulator, index: {}",
txn_global_index
real_txn_global_index
);

assert_eq!(
txn_info.transaction_global_index, txn_global_index as u64,
txn_info.transaction_global_index, real_txn_global_index as u64,
"txn_global_index:{}",
txn_global_index
real_txn_global_index
);

let account_address = match &txn {
Expand All @@ -126,7 +144,7 @@ fn test_transaction_info_and_proof() -> Result<()> {
let txn_proof = block_chain
.get_transaction_proof(
current_header.id(),
txn_global_index as u64,
real_txn_global_index as u64,
Some(event_index as u64),
access_path.clone(),
)?
Expand All @@ -135,15 +153,15 @@ fn test_transaction_info_and_proof() -> Result<()> {

let result = txn_proof.verify(
current_header.txn_accumulator_root(),
txn_global_index as u64,
real_txn_global_index as u64,
Some(event_index as u64),
access_path.clone(),
);

assert!(
result.is_ok(),
"txn index: {}, {:?} verify failed, reason: {:?}",
txn_global_index,
real_txn_global_index,
txn_proof,
result.err().unwrap()
);
Expand Down
3 changes: 3 additions & 0 deletions executor/benchmark/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ impl TransactionGenerator {
self.net.chain_id(),
0,
);
if self.block_number == 3 {
self.sequence += 1;
}
self.block_number += 1;
transactions.push(Transaction::BlockMetadata(block_meta));

Expand Down
11 changes: 9 additions & 2 deletions executor/tests/executor_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use test_helper::executor::{
use starcoin_state_api::StateReaderExt;
use starcoin_types::account::Account;
use starcoin_types::account_config::G_STC_TOKEN_CODE;
use starcoin_vm_runtime::force_upgrade_management::get_force_upgrade_block_number;
use starcoin_vm_runtime::starcoin_vm::{chunk_block_transactions, StarcoinVM};
use starcoin_vm_types::account_config::core_code_address;
use starcoin_vm_types::state_store::state_key::StateKey;
Expand Down Expand Up @@ -402,13 +403,19 @@ fn test_block_execute_gas_limit() -> Result<()> {

assert_eq!(max_include_txn_num, txns.len() as u64);

txns.insert(0, Transaction::BlockMetadata(block_meta));
txns.insert(0, Transaction::BlockMetadata(block_meta.clone()));
let executed_data =
starcoin_executor::block_execute(&chain_state, txns, block_gas_limit, None)?;
let txn_infos = executed_data.txn_infos;

// all user txns can be included
assert_eq!(txn_infos.len() as u64, max_include_txn_num + 1);
if block_meta.number() == get_force_upgrade_block_number(&block_meta.chain_id()) {
// for this sepcial block, an extra package txn has been executed
assert_eq!(txn_infos.len() as u64, max_include_txn_num + 1 + 1);
} else {
assert_eq!(txn_infos.len() as u64, max_include_txn_num + 1);
}

let block_gas_used = txn_infos.iter().fold(0u64, |acc, i| acc + i.gas_used());
assert!(
block_gas_used <= block_gas_limit,
Expand Down
2 changes: 1 addition & 1 deletion test-helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod starcoin_dao;
pub mod txn;
pub mod txpool;

pub use chain::gen_blockchain_for_test;
pub use chain::{gen_blockchain_for_test, gen_blockchain_with_blocks_for_test};
pub use dummy_network_service::DummyNetworkService;
pub use network::{build_network, build_network_cluster, build_network_pair};
pub use node::{run_node_by_config, run_test_node};
Expand Down

0 comments on commit fce9ffb

Please sign in to comment.