From d3a5ab87d43305ea9bf594dd37dd592dcdd4eddc Mon Sep 17 00:00:00 2001 From: Mohanson Date: Tue, 23 Jan 2024 10:24:28 +0800 Subject: [PATCH] Cobuild ownerlock/timelock/mutisig testcase (#12) * Add ownerlock and timelock testcase * Add sudt supply testcases * Add multisig testcase * Add ethereum/ethereum_displaying testcase --- tests/omni_lock_rust/tests/test_multisig.rs | 63 +++++ tests/omni_lock_rust/tests/test_omni_lock.rs | 243 ++++++++++++++++++ .../omni_lock_rust/tests/test_sudt_supply.rs | 106 ++++++++ 3 files changed, 412 insertions(+) diff --git a/tests/omni_lock_rust/tests/test_multisig.rs b/tests/omni_lock_rust/tests/test_multisig.rs index 129eb65..af60e3b 100644 --- a/tests/omni_lock_rust/tests/test_multisig.rs +++ b/tests/omni_lock_rust/tests/test_multisig.rs @@ -214,3 +214,66 @@ fn test_multisig_0_2_3_unlock_smt_in_input() { let verify_result = verifier.verify(MAX_CYCLES); verify_result.expect("pass verification"); } + +#[test] +fn test_cobuild_multisig_0_2_3_unlock() { + let mut data_loader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_MULTISIG, true); + config.cobuild_enabled = true; + config.set_multisig(0, 2, 3); + + config.scheme = TestScheme::OnWhiteList; + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + verify_result.expect("pass verification"); +} + +#[test] +fn test_cobuild_multisig_invalid_flags() { + let mut data_loader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_MULTISIG, true); + config.set_multisig(0, 2, 3); + config.multisig.set(0, 2, 4); + config.cobuild_enabled = true; + + config.scheme = TestScheme::OnWhiteList; + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + assert_script_error(verify_result.unwrap_err(), ERROR_MULTSIG_SCRIPT_HASH) +} + +#[test] +fn test_cobuild_multisig_invalid_flags2() { + let mut data_loader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_MULTISIG, true); + config.set_multisig(0, 2, 3); + config.multisig.set(0, 3, 3); + config.cobuild_enabled = true; + + config.scheme = TestScheme::OnWhiteList; + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + assert_script_error(verify_result.unwrap_err(), ERROR_MULTSIG_SCRIPT_HASH) +} diff --git a/tests/omni_lock_rust/tests/test_omni_lock.rs b/tests/omni_lock_rust/tests/test_omni_lock.rs index ac012fb..9d301d8 100644 --- a/tests/omni_lock_rust/tests/test_omni_lock.rs +++ b/tests/omni_lock_rust/tests/test_omni_lock.rs @@ -1135,3 +1135,246 @@ fn test_big_message() { let verify_result = verifier.verify(MAX_CYCLES); verify_result.expect("pass verification"); } + +#[test] +fn test_cobuild_simple_owner_lock() { + let mut data_loader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_OWNER_LOCK, false); + config.cobuild_enabled = true; + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + verify_result.expect("pass verification"); +} + +#[test] +fn test_cobuild_simple_owner_lock_mismatched() { + let mut data_loader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_OWNER_LOCK, false); + config.cobuild_enabled = true; + config.scheme = TestScheme::OwnerLockMismatched; + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + assert_script_error(verify_result.unwrap_err(), ERROR_LOCK_SCRIPT_HASH_NOT_FOUND) +} + +#[test] +fn test_cobuild_owner_lock_on_wl() { + let mut data_loader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_OWNER_LOCK, true); + config.cobuild_enabled = true; + config.scheme = TestScheme::OnWhiteList; + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + verify_result.expect("pass verification"); +} + +#[test] +fn test_cobuild_owner_lock_on_wl_without_witness() { + let mut data_loader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_OWNER_LOCK, true); + config.cobuild_enabled = true; + config.scheme = TestScheme::OnWhiteList; + config.scheme2 = TestScheme2::NoWitness; + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + assert!(verify_result.is_err()); +} + +#[test] +fn test_cobuild_rsa_via_dl_unlock_with_time_lock() { + let mut data_loader = DummyDataLoader::new(); + + let args_since = 0x2000_0000_0000_0000u64 + 200; + let input_since = 0x2000_0000_0000_0000u64 + 200; + let mut config = TestConfig::new(IDENTITY_FLAGS_DL, false); + config.cobuild_enabled = true; + config.set_rsa(); + config.set_since(args_since, input_since); + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + verify_result.expect("pass verification"); +} + +#[test] +fn test_cobuild_rsa_via_dl_unlock_with_time_lock_failed() { + let mut data_loader = DummyDataLoader::new(); + + let args_since = 0x2000_0000_0000_0000u64 + 200; + let input_since = 0x2000_0000_0000_0000u64 + 100; + let mut config = TestConfig::new(IDENTITY_FLAGS_DL, false); + config.cobuild_enabled = true; + config.set_rsa(); + config.set_since(args_since, input_since); + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + + assert_script_error(verify_result.unwrap_err(), ERROR_INCORRECT_SINCE_VALUE); +} + +#[test] +fn tested_by_append_witnessargs_acp() { + let mut data_loader: DummyDataLoader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_BITCOIN, false); + config.cobuild_enabled = true; + config.set_chain_config(Box::new(BitcoinConfig { + sign_vtype: BITCOIN_V_TYPE_P2PKHCOMPRESSED, + pubkey_err: false, + })); + config.set_acp_config(Some((0, 0))); + + config.custom_extension_witnesses = Some(vec![WitnessArgsBuilder::default() + .lock(Some(Bytes::from([0u8; 65].to_vec())).pack()) + .build() + .as_bytes()]); + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + verify_result.expect("pass verification"); +} + +#[test] +fn tested_by_append_witnessargs_since() { + let mut data_loader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_BITCOIN, false); + config.cobuild_enabled = true; + config.set_chain_config(Box::new(BitcoinConfig { + sign_vtype: BITCOIN_V_TYPE_P2PKHCOMPRESSED, + pubkey_err: false, + })); + + let args_since = 0x2000_0000_0000_0000u64 + 200; + let input_since = 0x2000_0000_0000_0000u64 + 200; + config.set_since(args_since, input_since); + + config.custom_extension_witnesses = Some(vec![WitnessArgsBuilder::default() + .lock(Some(Bytes::from([0u8; 65].to_vec())).pack()) + .build() + .as_bytes()]); + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + verify_result.expect("pass verification"); +} + +#[test] +fn tested_by_append_other_witnesslayout_acp() { + let mut data_loader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_BITCOIN, false); + config.cobuild_enabled = true; + config.set_chain_config(Box::new(BitcoinConfig { + sign_vtype: BITCOIN_V_TYPE_P2PKHCOMPRESSED, + pubkey_err: false, + })); + config.set_acp_config(Some((0, 0))); + + config.custom_extension_witnesses = Some(vec![WitnessLayoutBuilder::default() + .set(WitnessLayoutUnion::SighashAllOnly( + SighashAllOnlyBuilder::default() + .seal(Bytes::from([0u8; 32].to_vec()).pack()) + .build(), + )) + .build() + .as_bytes()]); + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + verify_result.expect("pass verification"); +} + +#[test] +fn test_cobuild_eth_unlock() { + let mut data_loader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_ETHEREUM, false); + config.cobuild_enabled = true; + config.set_chain_config(Box::new(EthereumConfig::default())); + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + verify_result.expect("pass verification"); +} + +#[test] +fn test_cobuild_eth_displaying_unlock() { + let mut data_loader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_ETHEREUM_DISPLAYING, false); + config.cobuild_enabled = true; + config.set_chain_config(Box::new(EthereumDisplayConfig::default())); + + let tx = gen_tx(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader); + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + verify_result.expect("pass verification"); +} diff --git a/tests/omni_lock_rust/tests/test_sudt_supply.rs b/tests/omni_lock_rust/tests/test_sudt_supply.rs index 3a4a3a5..a155d07 100644 --- a/tests/omni_lock_rust/tests/test_sudt_supply.rs +++ b/tests/omni_lock_rust/tests/test_sudt_supply.rs @@ -708,3 +708,109 @@ fn test_info_cell_max_supply_changed() { tx }); } + +fn run_sudt_supply_case_cobuild(error_code: i8, gen_tx_fn: F) +where + F: Fn(&mut DummyDataLoader, &mut TestConfig) -> TransactionView, +{ + let mut data_loader = DummyDataLoader::new(); + let mut config = TestConfig::new(IDENTITY_FLAGS_ETHEREUM, false); + config.cobuild_enabled = true; + config.set_chain_config(Box::new(EthereumConfig::default())); + + let tx = gen_tx_fn(&mut data_loader, &mut config); + let tx = sign_tx(&mut data_loader, tx, &mut config); + let resolved_tx = build_resolved_tx(&data_loader, &tx); + + let mut verifier = verify_tx(resolved_tx, data_loader.clone()); + verifier.set_debug_printer(debug_printer); + let verify_result = verifier.verify(MAX_CYCLES); + if error_code == 0 { + verify_result.expect("pass verification"); + } else { + assert_script_error(verify_result.unwrap_err(), error_code); + } +} + +// ==== SUCCESS cases ==== +#[test] +fn test_cobuild_success_issue_token() { + run_sudt_supply_case_cobuild(0, |data_loader, config| { + let (info_cell_type_script, cell_id) = gen_info_cell_type_script(); + config.set_sudt_supply(cell_id); + + let mut tx = gen_tx(data_loader, config); + let issue_amount: u128 = 336; + let max_supply: u128 = 6000; + let sudt_type_script = sudt_type_script(data_loader, &tx); + let sudt_type_script_hash = sudt_type_script.calc_script_hash(); + tx = add_sudt_dep(data_loader, tx); + tx = add_sudt_to_outputs(sudt_type_script.clone(), tx, issue_amount); + let input_info_cell_data = + build_info_cell_data(20, max_supply, sudt_type_script_hash.as_slice()); + let output_info_cell_data = build_info_cell_data( + 20 + issue_amount, + max_supply, + sudt_type_script_hash.as_slice(), + ); + tx = add_info_cell_to_inputs( + data_loader, + info_cell_type_script.clone(), + tx, + input_info_cell_data, + ); + tx = add_info_cell_to_outputs(info_cell_type_script.clone(), tx, output_info_cell_data); + tx + }); +} + +// ==== ERROR cases ==== +#[test] +fn test_cobuild_burn_token() { + run_sudt_supply_case_cobuild(ERROR_BURN, |data_loader, config| { + let (info_cell_type_script, cell_id) = gen_info_cell_type_script(); + config.set_sudt_supply(cell_id); + + let mut tx = gen_tx(data_loader, config); + let max_supply: u128 = 6000; + let sudt_type_script = sudt_type_script(data_loader, &tx); + let sudt_type_script_hash = sudt_type_script.calc_script_hash(); + tx = add_sudt_dep(data_loader, tx); + tx = add_sudt_to_inputs(data_loader, sudt_type_script.clone(), tx, 200); + let input_info_cell_data = + build_info_cell_data(400, max_supply, sudt_type_script_hash.as_slice()); + let output_info_cell_data = + build_info_cell_data(200, max_supply, sudt_type_script_hash.as_slice()); + tx = add_info_cell_to_inputs( + data_loader, + info_cell_type_script.clone(), + tx, + input_info_cell_data, + ); + tx = add_info_cell_to_outputs(info_cell_type_script.clone(), tx, output_info_cell_data); + tx + }); +} + +#[test] +fn test_cobuild_no_info_cell_in_inputs() { + run_sudt_supply_case_cobuild(ERROR_NO_INFO_CELL, |data_loader, config| { + let (info_cell_type_script, cell_id) = gen_info_cell_type_script(); + config.set_sudt_supply(cell_id); + + let mut tx = gen_tx(data_loader, config); + let issue_amount: u128 = 336; + let max_supply: u128 = 6000; + let sudt_type_script = sudt_type_script(data_loader, &tx); + let sudt_type_script_hash = sudt_type_script.calc_script_hash(); + tx = add_sudt_dep(data_loader, tx); + tx = add_sudt_to_outputs(sudt_type_script.clone(), tx, issue_amount); + let output_info_cell_data = build_info_cell_data( + 20 + issue_amount, + max_supply, + sudt_type_script_hash.as_slice(), + ); + tx = add_info_cell_to_outputs(info_cell_type_script.clone(), tx, output_info_cell_data); + tx + }); +}