Skip to content

Commit eb9500f

Browse files
committed
Improve tests
1 parent cf3d919 commit eb9500f

File tree

1 file changed

+60
-6
lines changed
  • src/descriptor/covenants

1 file changed

+60
-6
lines changed

src/descriptor/covenants/mod.rs

+60-6
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,9 @@ impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for CovenantDescripto
720720
mod tests {
721721

722722
use super::*;
723-
use elements::confidential;
723+
use elements::hashes::hex::ToHex;
724+
use elements::{confidential, opcodes::all::OP_PUSHNUM_1};
725+
use elements::{opcodes, script};
724726
use elements::{AssetId, AssetIssuance, OutPoint, TxIn, TxInWitness, Txid};
725727
use interpreter::SatisfiedConstraint;
726728
use std::str::FromStr;
@@ -734,12 +736,20 @@ mod tests {
734736
0xe1, 0xb2,
735737
];
736738

739+
fn string_rtt(desc_str: &str) {
740+
let desc = Descriptor::<String>::from_str(desc_str).unwrap();
741+
assert_eq!(desc.to_string_no_chksum(), desc_str);
742+
let cov_desc = desc.as_cov();
743+
assert_eq!(cov_desc.to_string(), desc.to_string());
744+
}
737745
#[test]
738746
fn parse_cov() {
739-
Descriptor::<String>::from_str("elcovwsh(A,pk(B))").unwrap();
740-
Descriptor::<String>::from_str("elcovwsh(A,or_i(pk(B),pk(C)))").expect("Failed");
741-
Descriptor::<String>::from_str("elcovwsh(A,multi(2,B,C,D))").unwrap();
742-
Descriptor::<String>::from_str("elcovwsh(A,and_v(v:pk(B),pk(C)))").unwrap();
747+
string_rtt("elcovwsh(A,pk(B))");
748+
string_rtt("elcovwsh(A,or_i(pk(B),pk(C)))");
749+
string_rtt("elcovwsh(A,multi(2,B,C,D))");
750+
string_rtt("elcovwsh(A,and_v(v:pk(B),pk(C)))");
751+
string_rtt("elcovwsh(A,thresh(2,ver_eq(1),s:pk(C),s:pk(B)))");
752+
string_rtt("elcovwsh(A,outputs_pref(01020304))");
743753
}
744754

745755
fn script_rtt(desc_str: &str) {
@@ -768,6 +778,14 @@ mod tests {
768778
"elcovwsh({},and_v(v:pk({}),pk({})))",
769779
pks[0], pks[1], pks[2]
770780
));
781+
script_rtt(&format!(
782+
"elcovwsh({},and_v(v:ver_eq(2),pk({})))",
783+
pks[0], pks[1]
784+
));
785+
script_rtt(&format!(
786+
"elcovwsh({},and_v(v:outputs_pref(f2f233),pk({})))",
787+
pks[0], pks[1]
788+
));
771789
}
772790

773791
// Some deterministic keys for ease of testing
@@ -831,7 +849,10 @@ mod tests {
831849
};
832850

833851
spend_tx.output.push(TxOut::default());
834-
spend_tx.output[0].script_pubkey = desc.script_pubkey(); // send back to self
852+
spend_tx.output[0].script_pubkey = script::Builder::new()
853+
.push_opcode(opcodes::all::OP_PUSHNUM_1)
854+
.into_script()
855+
.to_v0_p2wsh();
835856
spend_tx.output[0].value = confidential::Value::Explicit(99_000);
836857
spend_tx.output[0].asset =
837858
confidential::Asset::Explicit(AssetId::from_slice(&BTC_ASSET).unwrap());
@@ -934,6 +955,39 @@ mod tests {
934955
sks[0],
935956
)
936957
.unwrap_err();
958+
959+
// Outputs Pref test
960+
// 1. Correct case
961+
let mut out = TxOut::default();
962+
out.script_pubkey = script::Builder::new()
963+
.push_opcode(opcodes::all::OP_PUSHNUM_1)
964+
.into_script()
965+
.to_v0_p2wsh();
966+
out.value = confidential::Value::Explicit(99_000);
967+
out.asset = confidential::Asset::Explicit(AssetId::from_slice(&BTC_ASSET).unwrap());
968+
let desc = Descriptor::<bitcoin::PublicKey>::from_str(&format!(
969+
"elcovwsh({},outputs_pref({}))",
970+
pks[0],
971+
serialize(&out).to_hex(),
972+
))
973+
.unwrap();
974+
_satisfy_and_interpret(desc, sks[0]).unwrap();
975+
976+
// 2. Chaning the amount should fail the test
977+
let mut out = TxOut::default();
978+
out.script_pubkey = script::Builder::new()
979+
.push_opcode(opcodes::all::OP_PUSHNUM_1)
980+
.into_script()
981+
.to_v0_p2wsh();
982+
out.value = confidential::Value::Explicit(99_001); // Changed to +1
983+
out.asset = confidential::Asset::Explicit(AssetId::from_slice(&BTC_ASSET).unwrap());
984+
let desc = Descriptor::<bitcoin::PublicKey>::from_str(&format!(
985+
"elcovwsh({},outputs_pref({}))",
986+
pks[0],
987+
serialize(&out).to_hex(),
988+
))
989+
.unwrap();
990+
_satisfy_and_interpret(desc, sks[0]).unwrap_err();
937991
}
938992

939993
// Fund output and spend tx are tests handy with code for

0 commit comments

Comments
 (0)