Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exec test case #50

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions tests/omni_lock_rust/tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand Down
16 changes: 16 additions & 0 deletions tests/omni_lock_rust/tests/test_omni_lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down