From 82d41cbe9acf7dbba031b25a6296a0b4f5c121f8 Mon Sep 17 00:00:00 2001 From: joii2020 Date: Thu, 8 Feb 2024 15:28:46 +0800 Subject: [PATCH] Add otx testcase: All vaule of Otx Message Size are zero --- c/cobuild.c | 2 +- tests/omni_lock_rust/tests/test_otx.rs | 102 +++++++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) diff --git a/c/cobuild.c b/c/cobuild.c index f89b4a5..a497bae 100644 --- a/c/cobuild.c +++ b/c/cobuild.c @@ -906,7 +906,7 @@ int ckb_cobuild_entry(ScriptEntryType callback, bool *cobuild_enabled) { // 6.b if (size.input_cells == 0 && size.output_cells == 0 && size.cell_deps == 0 && size.header_deps == 0) { - // TODO + // test_cobuild_otx_msg_size_all_0 CHECK2(false, ERROR_WRONG_OTX); } // step 6.c diff --git a/tests/omni_lock_rust/tests/test_otx.rs b/tests/omni_lock_rust/tests/test_otx.rs index 5a280f5..d13e256 100644 --- a/tests/omni_lock_rust/tests/test_otx.rs +++ b/tests/omni_lock_rust/tests/test_otx.rs @@ -1060,6 +1060,76 @@ fn generate_otx_a3_fail(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core tx_builder.build() } + +fn generate_otx_a4_fail(dl: &mut Resource, px: &mut Pickaxer) -> ckb_types::core::TransactionView { + let tx_builder = ckb_types::core::TransactionBuilder::default(); + + // Create prior knowledge + let prikey = "0000000000000000000000000000000000000000000000000000000000000001"; + let prikey = ckb_crypto::secp::Privkey::from_str(prikey).unwrap(); + let pubkey = prikey.pubkey().unwrap(); + let pubkey_hash = hash_ripemd160_sha256(&pubkey.serialize()); + let args = [vec![IDENTITY_FLAGS_BITCOIN], pubkey_hash.to_vec(), vec![0x00]].concat(); + + // Create cell meta + let cell_meta_always_success = px.insert_cell_data(dl, BINARY_ALWAYS_SUCCESS); + let cell_meta_secp256k1_data = px.insert_cell_data(dl, BINARY_SECP256K1_DATA); + let cell_meta_omni_lock = px.insert_cell_data(dl, BINARY_OMNI_LOCK); + let cell_meta_i = px.insert_cell_fund(dl, px.create_script(&cell_meta_omni_lock, &args), None, &[]); + + // Create cell dep + let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_always_success)); + let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_secp256k1_data)); + let tx_builder = tx_builder.cell_dep(px.create_cell_dep(&cell_meta_omni_lock)); + + // Create input + let tx_builder = tx_builder.input(px.create_cell_input(&cell_meta_i)); + + // Create output + let tx_builder = tx_builder.output(px.create_cell_output( + px.create_script(&cell_meta_always_success, &[]), + Some(px.create_script(&cell_meta_always_success, &[])), + )); + + // Create output data + let tx_builder = tx_builder.output_data(Vec::new().pack()); + + // Create witness + let msgs = { + let action = schemas::basic::Action::new_builder() + .script_info_hash(ckb_types::packed::Byte32::from_slice(&[0x00; 32]).unwrap()) + .script_hash(px.create_script(&cell_meta_always_success, &[]).calc_script_hash()) + .data(ckb_types::bytes::Bytes::from(vec![0x42; 128]).pack()) + .build(); + let action_vec = schemas::basic::ActionVec::new_builder().push(action).build(); + let msgs = schemas::basic::Message::new_builder().actions(action_vec).build(); + msgs + }; + let sign = cobuild_create_signing_message_hash_otx(tx_builder.clone().build(), &dl, &msgs); + println_hex("smh", &sign); + let sign = sign_bitcoin_p2pkh_compressed(prikey, &sign); + let sign = omnilock_create_witness_lock(&sign); + let seal = [vec![0x00], sign].concat(); + println_hex("seal", seal.as_slice()); + let seal = schemas::basic::SealPair::new_builder() + .script_hash(px.create_script(&cell_meta_omni_lock, &args).calc_script_hash()) + .seal(seal.pack()) + .build(); + let seal = schemas::basic::SealPairVec::new_builder().push(seal).build(); + let ox = schemas::basic::Otx::new_builder() + .seals(seal) + .message(msgs) + .input_cells(0u32.pack()) + .output_cells(0u32.pack()) + .cell_deps(0u32.pack()) + .header_deps(0u32.pack()) + .build(); + let wl = schemas::top_level::WitnessLayout::new_builder().set(ox).build(); + let tx_builder = tx_builder.witness(wl.as_bytes().pack()); + + tx_builder.build() +} + fn assemble_otx(otxs: Vec) -> ckb_types::core::TransactionView { let mut tx_builder = ckb_types::core::TransactionBuilder::default(); let os = schemas::basic::OtxStart::new_builder().build(); @@ -1477,3 +1547,35 @@ fn test_cobuild_otx_noexistent_type_script_hash() { let verifier = Verifier::default(); assert_script_error(verifier.verify(&tx, &dl).unwrap_err(), ERROR_TYPESCRIPT_MISSING); } + +#[test] +fn test_cobuild_otx_msg_size_all_0() { + let mut dl = Resource::default(); + let mut px = Pickaxer::default(); + + let mut tx_builder = ckb_types::core::TransactionBuilder::default(); + let os = schemas::basic::OtxStart::new_builder().build(); + let wl = schemas::top_level::WitnessLayout::new_builder().set(os).build(); + tx_builder = tx_builder.witness(wl.as_bytes().pack()); + for otx in [generate_otx_a4_fail(&mut dl, &mut px)] { + for e in otx.cell_deps_iter() { + tx_builder = tx_builder.cell_dep(e); + } + for e in otx.inputs().into_iter() { + tx_builder = tx_builder.input(e); + } + for e in otx.outputs().into_iter() { + tx_builder = tx_builder.output(e); + } + for e in otx.outputs_data().into_iter() { + tx_builder = tx_builder.output_data(e); + } + for e in otx.witnesses().into_iter() { + tx_builder = tx_builder.witness(e); + } + } + + let tx = ckb_types::core::cell::resolve_transaction(tx_builder.build(), &mut HashSet::new(), &dl, &dl).unwrap(); + let verifier = Verifier::default(); + assert_script_error(verifier.verify(&tx, &dl).unwrap_err(), ERROR_WRONG_OTX); +}