Skip to content

Commit

Permalink
chore: adjust tests and tolls to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
keroro520 committed Nov 7, 2022
1 parent df230c5 commit 3e96828
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 49 deletions.
1 change: 1 addition & 0 deletions crates/benches/benches/benchmarks/smt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ impl BenchExecutionEnvironment {
let rollup_context = RollupContext {
rollup_config: genesis_config.rollup_config.clone().into(),
rollup_script_hash: ROLLUP_TYPE_HASH.into(),
..Default::default()
};

let backend_manage = {
Expand Down
1 change: 1 addition & 0 deletions crates/benches/benches/benchmarks/sudt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ fn run_contract_get_result<S: State + CodeStore + JournalDB>(
let rollup_ctx = RollupContext {
rollup_config: rollup_config.clone(),
rollup_script_hash: [42u8; 32].into(),
..Default::default()
};
let generator = Generator::new(
backend_manage,
Expand Down
23 changes: 14 additions & 9 deletions crates/generator/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::convert::TryInto;
use gw_common::{builtins::CKB_SUDT_ACCOUNT_ID, state::State, H256};
use gw_config::BackendType;
use gw_traits::CodeStore;
use gw_types::core::Timepoint;
use gw_types::{
bytes::Bytes,
core::{AllowedContractType, ScriptHashType},
Expand Down Expand Up @@ -43,15 +44,15 @@ pub fn build_withdrawal_cell_output(
rollup_context: &RollupContext,
req: &WithdrawalRequestExtra,
block_hash: &H256,
block_number: u64,
block_timepoint: &Timepoint,
opt_asset_script: Option<Script>,
) -> Result<(CellOutput, Bytes), WithdrawalCellError> {
let withdrawal_capacity: u64 = req.raw().capacity().unpack();
let lock_args: Bytes = {
let withdrawal_lock_args = WithdrawalLockArgs::new_builder()
.account_script_hash(req.raw().account_script_hash())
.withdrawal_block_hash(Into::<[u8; 32]>::into(*block_hash).pack())
.withdrawal_block_number(block_number.pack())
.withdrawal_block_number(block_timepoint.full_value().pack())
.owner_lock_hash(req.raw().owner_lock_hash())
.build();

Expand Down Expand Up @@ -138,7 +139,7 @@ mod test {
use gw_common::h256_ext::H256Ext;
use gw_common::H256;
use gw_types::bytes::Bytes;
use gw_types::core::ScriptHashType;
use gw_types::core::{ScriptHashType, Timepoint};
use gw_types::offchain::RollupContext;
use gw_types::packed::{
RawWithdrawalRequest, RollupConfig, Script, WithdrawalRequest, WithdrawalRequestExtra,
Expand All @@ -155,6 +156,7 @@ mod test {
rollup_config: RollupConfig::new_builder()
.withdrawal_script_type_hash(H256::from_u32(100).pack())
.build(),
..Default::default()
};
let sudt_script = Script::new_builder()
.code_hash(H256::from_u32(1).pack())
Expand Down Expand Up @@ -188,12 +190,12 @@ mod test {
.build();

let block_hash = H256::from_u32(11);
let block_number = 11u64;
let block_timepoint = Timepoint::from_block_number(11);
let (output, data) = build_withdrawal_cell_output(
&rollup_context,
&withdrawal,
&block_hash,
block_number,
&block_timepoint,
Some(sudt_script.clone()),
)
.unwrap();
Expand Down Expand Up @@ -226,15 +228,18 @@ mod test {
req.raw().account_script_hash()
);
assert_eq!(lock_args.withdrawal_block_hash(), block_hash.pack());
assert_eq!(lock_args.withdrawal_block_number().unpack(), block_number);
assert_eq!(
lock_args.withdrawal_block_number().unpack(),
block_timepoint.full_value()
);
assert_eq!(lock_args.owner_lock_hash(), owner_lock.hash().pack());

// ## None asset script
let (output2, data2) = build_withdrawal_cell_output(
&rollup_context,
&withdrawal,
&block_hash,
block_number,
&block_timepoint,
None,
)
.unwrap();
Expand All @@ -261,7 +266,7 @@ mod test {
.owner_lock(owner_lock)
.build(),
&block_hash,
block_number,
&block_timepoint,
Some(sudt_script.clone()),
)
.unwrap_err();
Expand All @@ -286,7 +291,7 @@ mod test {
.owner_lock(err_owner_lock)
.build(),
&block_hash,
block_number,
&block_timepoint,
Some(sudt_script),
)
.unwrap_err();
Expand Down
8 changes: 6 additions & 2 deletions crates/tests/src/testing_tool/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ pub fn chain_generator(chain: &Chain, rollup_type_script: Script) -> Arc<Generat
let rollup_context = RollupContext {
rollup_script_hash: rollup_type_script.hash().into(),
rollup_config,
..Default::default()
};
Arc::new(Generator::new(
backend_manage,
Expand Down Expand Up @@ -499,6 +500,7 @@ pub async fn setup_chain_with_account_lock_manage(
let rollup_context = RollupContext {
rollup_script_hash: rollup_script_hash.into(),
rollup_config: rollup_config.clone(),
..Default::default()
};
let generator = Arc::new(Generator::new(
backend_manage,
Expand All @@ -519,7 +521,7 @@ pub async fn setup_chain_with_account_lock_manage(
};
let mem_pool = MemPool::create(args).await.unwrap();
Chain::create(
&rollup_config,
rollup_config.clone(),
&rollup_type_script,
&ChainConfig::default(),
store,
Expand Down Expand Up @@ -615,7 +617,7 @@ pub async fn construct_block_with_timestamp(
let stake_cell_owner_lock_hash = H256::zero();
let db = &chain.store().begin_transaction();
let generator = chain.generator();
let rollup_config_hash = (*chain.rollup_config_hash()).into();
let rollup_config_hash = chain.rollup_config_hash().into();

let provider = DummyMemPoolProvider {
deposit_cells: deposit_info_vec.unpack(),
Expand All @@ -636,10 +638,12 @@ pub async fn construct_block_with_timestamp(
let remaining_capacity = mem_block.take_finalized_custodians_capacity();
let block_param = generate_produce_block_param(chain.store(), mem_block, post_merkle_state)?;
let reverted_block_root = db.get_reverted_block_smt_root().unwrap();
let l1_confirmed_header_timestamp = 0;
let param = ProduceBlockParam {
stake_cell_owner_lock_hash,
rollup_config_hash,
reverted_block_root,
l1_confirmed_header_timestamp,
block_param,
};
produce_block(db, generator, param).map(|mut r| {
Expand Down
6 changes: 3 additions & 3 deletions crates/tests/src/tests/export_import_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use gw_generator::account_lock_manage::always_success::AlwaysSuccess;
use gw_generator::account_lock_manage::secp256k1::Secp256k1Eth;
use gw_generator::account_lock_manage::AccountLockManage;
use gw_store::{readonly::StoreReadonly, traits::chain_store::ChainStore, Store};
use gw_types::core::Status;
use gw_types::core::{Status, Timepoint};
use gw_types::packed::DepositInfoVec;
use gw_types::{
bytes::Bytes,
Expand Down Expand Up @@ -67,9 +67,9 @@ async fn test_export_import_block() {
.finality_blocks(0u64.pack())
.build();

let last_finalized_block_number = 100u64;
let last_finalized_timepoint = Timepoint::from_block_number(100);
let global_state = GlobalState::new_builder()
.last_finalized_block_number(last_finalized_block_number.pack())
.last_finalized_block_number(last_finalized_timepoint.full_value().pack())
.rollup_config_hash(rollup_config.hash().pack())
.build();

Expand Down
2 changes: 2 additions & 0 deletions crates/tests/src/tests/mem_block_repackage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ async fn test_repackage_mem_block() {
let smt = db.reverted_block_smt().unwrap();
smt.root().to_owned()
};
let l1_confirmed_header_timestamp = 0;
let param = ProduceBlockParam {
stake_cell_owner_lock_hash: random_always_success_script(&rollup_script_hash)
.hash()
.into(),
reverted_block_root,
rollup_config_hash: rollup_context.rollup_config.hash().into(),
l1_confirmed_header_timestamp,
block_param,
};
let store = chain.store();
Expand Down
26 changes: 24 additions & 2 deletions crates/tests/src/tests/restore_mem_pool_pending_withdrawal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::time::Duration;

use crate::testing_tool::chain::{
build_sync_tx, construct_block, construct_block_with_timestamp, into_deposit_info_cell,
produce_empty_block, restart_chain, setup_chain, DEFAULT_FINALITY_BLOCKS, TEST_CHAIN_ID,
restart_chain, setup_chain, DEFAULT_FINALITY_BLOCKS, TEST_CHAIN_ID,
};
use crate::testing_tool::common::random_always_success_script;
use crate::testing_tool::mem_pool_provider::DummyMemPoolProvider;
Expand Down Expand Up @@ -77,7 +77,29 @@ async fn test_restore_mem_pool_pending_withdrawal() {
assert!(chain.last_sync_event().is_success());

for _ in 0..DEFAULT_FINALITY_BLOCKS {
produce_empty_block(&mut chain).await.unwrap();
let block_result = {
let mem_pool = chain.mem_pool().as_ref().unwrap();
let mut mem_pool = mem_pool.lock().await;
construct_block(&chain, &mut mem_pool, Default::default())
.await
.unwrap()
};
let empty_l1action = L1Action {
context: L1ActionContext::SubmitBlock {
l2block: block_result.block.clone(),
deposit_info_vec: Default::default(),
deposit_asset_scripts: Default::default(),
withdrawals: Default::default(),
},
transaction: build_sync_tx(rollup_cell.clone(), block_result),
};
let param = SyncParam {
updates: vec![empty_l1action],
reverts: Default::default(),
};
chain.sync(param).await.unwrap();
chain.notify_new_tip().await.unwrap();
assert!(chain.last_sync_event().is_success());
}

// Generate withdrawals
Expand Down
Loading

0 comments on commit 3e96828

Please sign in to comment.