diff --git a/chain/force-upgrade/src/lib.rs b/chain/force-upgrade/src/lib.rs index 26ffa76894..5157b67297 100644 --- a/chain/force-upgrade/src/lib.rs +++ b/chain/force-upgrade/src/lib.rs @@ -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 { let package_file = "12/11-12/stdlib.blob".to_string(); @@ -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(), ))) diff --git a/chain/tests/test_force_upgrade.rs b/chain/tests/test_force_upgrade.rs index bfbb2c562e..fecac59bec 100644 --- a/chain/tests/test_force_upgrade.rs +++ b/chain/tests/test_force_upgrade.rs @@ -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; @@ -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) = { @@ -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 @@ -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, @@ -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!( @@ -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 { chain_state_db .get_on_chain_config::()? diff --git a/chain/tests/test_txn_info_and_proof.rs b/chain/tests/test_txn_info_and_proof.rs index d817366953..227bdc74fe 100644 --- a/chain/tests/test_txn_info_and_proof.rs +++ b/chain/tests/test_txn_info_and_proof.rs @@ -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(); @@ -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(); @@ -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 { @@ -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(), )? @@ -135,7 +153,7 @@ 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(), ); @@ -143,7 +161,7 @@ fn test_transaction_info_and_proof() -> Result<()> { assert!( result.is_ok(), "txn index: {}, {:?} verify failed, reason: {:?}", - txn_global_index, + real_txn_global_index, txn_proof, result.err().unwrap() ); diff --git a/executor/benchmark/src/lib.rs b/executor/benchmark/src/lib.rs index 3706971393..59511092e0 100644 --- a/executor/benchmark/src/lib.rs +++ b/executor/benchmark/src/lib.rs @@ -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)); diff --git a/executor/tests/executor_test.rs b/executor/tests/executor_test.rs index 9b057d9b24..17ec48367d 100644 --- a/executor/tests/executor_test.rs +++ b/executor/tests/executor_test.rs @@ -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; @@ -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, diff --git a/test-helper/src/lib.rs b/test-helper/src/lib.rs index 847ae6d8c0..f58193d807 100644 --- a/test-helper/src/lib.rs +++ b/test-helper/src/lib.rs @@ -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};