From 151e75a4fd100977c494336ddb188d7313d2201c Mon Sep 17 00:00:00 2001 From: xjd Date: Tue, 30 Apr 2024 09:01:46 +0800 Subject: [PATCH] Add exec test case (#50) * Add exec test case --- tests/omni_lock_rust/tests/misc.rs | 43 +++++++++++++++++++- tests/omni_lock_rust/tests/test_omni_lock.rs | 16 ++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/tests/omni_lock_rust/tests/misc.rs b/tests/omni_lock_rust/tests/misc.rs index 2052b3a..97f3162 100644 --- a/tests/omni_lock_rust/tests/misc.rs +++ b/tests/omni_lock_rust/tests/misc.rs @@ -577,7 +577,7 @@ pub fn sign_tx_by_input_group( sig = wrong_sig; } let hash = blake160(pubkey.as_ref()); - let preimage = gen_exec_preimage(&config.rsa_script, &hash); + let preimage = gen_dl_preimage(&config.rsa_script, &hash); preimage_hash = blake160(preimage.as_ref()); let sig_bytes = Bytes::from(sig); @@ -589,6 +589,22 @@ pub fn sign_tx_by_input_group( &identity, Some(preimage), ) + } else if config.id.flags == IDENTITY_FLAGS_EXEC { + let script = build_always_success_script(); + // always success script is used here. Anything is OK. + let hash = blake160(&[0u8; 20]); + let preimage = gen_exec_preimage(&script, &hash); + preimage_hash = blake160(preimage.as_ref()); + + let sig_bytes = Bytes::from(vec![0u8; 65]); + gen_witness_lock( + sig_bytes, + config.use_rc, + config.use_rc_identity, + &proof_vec, + &identity, + Some(preimage), + ) } else if config.id.flags == IDENTITY_FLAGS_MULTISIG { let sig = config.multisig.sign(&message.into()); gen_witness_lock(sig, config.use_rc, config.use_rc_identity, &proof_vec, &identity, None) @@ -696,7 +712,13 @@ pub fn sign_tx_by_input_group( signed_witnesses.push(tx.witnesses().get(i).unwrap()); } if preimage_hash.len() == 20 { - write_back_preimage_hash(dummy, IDENTITY_FLAGS_DL, preimage_hash); + if config.id.flags == IDENTITY_FLAGS_DL { + write_back_preimage_hash(dummy, IDENTITY_FLAGS_DL, preimage_hash); + } else if config.id.flags == IDENTITY_FLAGS_EXEC { + write_back_preimage_hash(dummy, IDENTITY_FLAGS_EXEC, preimage_hash); + } else { + panic!("preimage_hash"); + } } match &config.custom_extension_witnesses_beginning { @@ -1679,14 +1701,31 @@ pub fn gen_zero_witness_lock( res.freeze() } +pub fn gen_dl_preimage(script: &Script, blake160: &Bytes) -> Bytes { + let mut result = BytesMut::new(); + result.put_slice(script.code_hash().as_slice()); + result.put_slice(script.hash_type().as_slice()); + result.put_slice(blake160.clone().as_ref()); + + result.freeze() +} + +// code hash: 32 bytes +// hash type: 1 byte +// place: 1 byte +// bounds: 8 bytes +// pubkey hash: 20 bytes pub fn gen_exec_preimage(script: &Script, blake160: &Bytes) -> Bytes { let mut result = BytesMut::new(); result.put_slice(script.code_hash().as_slice()); result.put_slice(script.hash_type().as_slice()); + result.put_slice(&[0u8; 1]); + result.put_slice(&[0u8; 8]); result.put_slice(blake160.clone().as_ref()); result.freeze() } + // first generate N RCE cells with each contained one RCRule // then collect all these RCE cell hash and create the final RCE cell. pub fn generate_rce_cell( diff --git a/tests/omni_lock_rust/tests/test_omni_lock.rs b/tests/omni_lock_rust/tests/test_omni_lock.rs index 8db3cfa..31136e1 100644 --- a/tests/omni_lock_rust/tests/test_omni_lock.rs +++ b/tests/omni_lock_rust/tests/test_omni_lock.rs @@ -273,6 +273,22 @@ fn test_pubkey_hash_emergency_halt_mode() { assert_script_error(verify_result.unwrap_err(), ERROR_RCE_EMERGENCY_HALT) } +#[test] +fn test_via_exec_unlock() { + let mut data_loader = DummyDataLoader::new(); + + let mut config = TestConfig::new(IDENTITY_FLAGS_EXEC, false); + + 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_rsa_via_dl_unlock() { let mut data_loader = DummyDataLoader::new();