Skip to content

Commit 15e117f

Browse files
committed
Review feedback
1 parent b375b7d commit 15e117f

File tree

7 files changed

+29
-24
lines changed

7 files changed

+29
-24
lines changed

src/descriptor/covenants/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ mod tests {
739739
fn string_rtt(desc_str: &str) {
740740
let desc = Descriptor::<String>::from_str(desc_str).unwrap();
741741
assert_eq!(desc.to_string_no_chksum(), desc_str);
742-
let cov_desc = desc.as_cov();
742+
let cov_desc = desc.as_cov().unwrap();
743743
assert_eq!(cov_desc.to_string(), desc.to_string());
744744
}
745745
#[test]
@@ -836,7 +836,7 @@ mod tests {
836836
cov_sk: secp256k1::SecretKey,
837837
) -> Result<(), Error> {
838838
assert_eq!(desc.desc_type(), DescriptorType::Cov);
839-
let desc = desc.as_cov();
839+
let desc = desc.as_cov().unwrap();
840840
// Now create a transaction spending this.
841841
let mut spend_tx = Transaction {
842842
version: 2,
@@ -1062,7 +1062,7 @@ mod tests {
10621062
spend_tx.output[2].value = confidential::Value::Explicit(2_000);
10631063

10641064
// Try to satisfy the covenant part
1065-
let desc = desc.as_cov();
1065+
let desc = desc.as_cov().unwrap();
10661066
let script_code = desc.cov_script_code();
10671067
let cov_sat = CovSatisfier::new_segwitv0(
10681068
&spend_tx,

src/descriptor/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,11 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
432432

433433
/// Unwrap a descriptor as a covenant descriptor
434434
/// Panics if the descriptor is not of [DescriptorType::Cov]
435-
pub fn as_cov(&self) -> &CovenantDescriptor<Pk> {
435+
pub fn as_cov(&self) -> Result<&CovenantDescriptor<Pk>, Error> {
436436
if let Descriptor::Cov(cov) = self {
437-
cov
437+
Ok(cov)
438438
} else {
439-
panic!("Called as_cov on a non-covenant descriptor")
439+
Err(Error::CovError(CovError::BadCovDescriptor))
440440
}
441441
}
442442

src/interpreter/stack.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use elements::{self, opcodes, script};
2323
use {ElementsSig, ToPublicKey};
2424

2525
use super::{verify_sersig, Error, HashLockType, SatisfiedConstraint};
26+
use miniscript::limits::{MAX_SCRIPT_ELEMENT_SIZE, MAX_STANDARD_P2WSH_STACK_ITEM_SIZE};
2627
use util;
2728
/// Definition of Stack Element of the Stack used for interpretation of Miniscript.
2829
/// All stack elements with vec![] go to Dissatisfied and vec![1] are marked to Satisfied.
@@ -431,21 +432,23 @@ impl<'txin> Stack<'txin> {
431432
if let Err(e) = hash_outputs.try_push() {
432433
return Some(Err(e));
433434
}
435+
// Maximum number of suffix elements
436+
let max_elems = MAX_SCRIPT_ELEMENT_SIZE / MAX_STANDARD_P2WSH_STACK_ITEM_SIZE + 1;
434437
let hash_outputs = hash_outputs.as_push();
435438
if hash_outputs.len() == 32 {
436439
// We want to cat the last 6 elements(5 cats) in suffix
437-
if self.len() < 6 {
440+
if self.len() < max_elems {
438441
return Some(Err(Error::UnexpectedStackEnd));
439442
}
440443
let mut outputs_builder = Vec::new();
441444
outputs_builder.extend(pref);
442445
let len = self.len();
443-
// Add the 6 suffix elements
444-
for i in 0..6 {
445-
outputs_builder.extend(self[len - 6 + i].into_slice());
446+
// Add the max_elems suffix elements
447+
for i in 0..max_elems {
448+
outputs_builder.extend(self[len - max_elems + i].into_slice());
446449
}
447-
// Pop the 6 suffix elements
448-
for _ in 0..6 {
450+
// Pop the max_elems suffix elements
451+
for _ in 0..max_elems {
449452
self.pop().unwrap();
450453
}
451454
if sha256d::Hash::hash(&outputs_builder).as_inner() == hash_outputs {

src/miniscript/astelem.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,8 @@ impl StackCtxOperations for script::Builder {
632632
fn check_item_pref(self, idx: u32, pref: &[u8]) -> Self {
633633
let mut builder = self;
634634
// Initial Witness
635-
let max_elems = MAX_SCRIPT_ELEMENT_SIZE / MAX_STANDARD_P2WSH_STACK_ITEM_SIZE;
635+
// The nuumber of maximum witness elements in the suffix
636+
let max_elems = MAX_SCRIPT_ELEMENT_SIZE / MAX_STANDARD_P2WSH_STACK_ITEM_SIZE + 1;
636637
for _ in 0..(max_elems - 1) {
637638
builder = builder.push_opcode(opcodes::all::OP_CAT);
638639
}
@@ -801,9 +802,9 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
801802
Terminal::False => 1,
802803
Terminal::Version(_n) => 4 + 1 + 1 + 4, // opcodes + push opcodes + target size
803804
Terminal::OutputsPref(ref pref) => {
804-
// CAT CAT CAT CAT CAT <pref> SWAP CAT /*Now we hashoutputs on stack */
805+
// CAT CAT CAT CAT CAT CAT <pref> SWAP CAT /*Now we hashoutputs on stack */
805806
// HASH256 DEPTH <10> SUB PICK EQUAL
806-
7 + pref.len() + 1 /* line1 opcodes + pref.push */
807+
8 + pref.len() + 1 /* line1 opcodes + pref.push */
807808
+ 6 /* line 2 */
808809
}
809810
Terminal::Alt(ref sub) => sub.node.script_size() + 2,

src/miniscript/decode.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ pub fn parse<Ctx: ScriptContext>(
293293
tokens,
294294
Tk::Num(10) => match_token!(
295295
tokens,
296-
Tk::Hash256, Tk::Cat, Tk::Swap, Tk::Push(bytes), Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat =>
296+
Tk::Hash256, Tk::Cat, Tk::Swap, Tk::Push(bytes), Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat =>
297297
{
298298
non_term.push(NonTerm::Verify);
299299
term.reduce0(Terminal::OutputsPref(bytes))?
@@ -368,7 +368,7 @@ pub fn parse<Ctx: ScriptContext>(
368368
tokens,
369369
Tk::Num(10) => match_token!(
370370
tokens,
371-
Tk::Hash256, Tk::Cat, Tk::Swap, Tk::Push(bytes), Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat =>
371+
Tk::Hash256, Tk::Cat, Tk::Swap, Tk::Push(bytes), Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat, Tk::Cat =>
372372
term.reduce0(Terminal::OutputsPref(bytes))?,
373373
),
374374
),

src/miniscript/satisfy.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,8 @@ impl Witness {
750750
match sat.lookup_outputs() {
751751
Some(outs) => {
752752
let mut ser_out = Vec::new();
753-
let num_wit_elems = MAX_SCRIPT_ELEMENT_SIZE / MAX_STANDARD_P2WSH_STACK_ITEM_SIZE;
753+
let num_wit_elems =
754+
MAX_SCRIPT_ELEMENT_SIZE / MAX_STANDARD_P2WSH_STACK_ITEM_SIZE + 1;
754755
let mut witness = Vec::with_capacity(num_wit_elems);
755756
for out in outs {
756757
ser_out.extend(serialize(out));

src/miniscript/types/extra_props.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -347,13 +347,13 @@ impl Property for ExtData {
347347
// Assume txouts fill out all the 520 bytes
348348
let max_wit_sz = MAX_SCRIPT_ELEMENT_SIZE - pref.len();
349349
ExtData {
350-
pk_cost: 7 + pref.len() + 1 + 6, // See script_size() in astelem.rs
350+
pk_cost: 8 + pref.len() + 1 + 6, // See script_size() in astelem.rs
351351
has_free_verify: true,
352-
ops_count_static: 12,
353-
ops_count_sat: Some(12),
354-
ops_count_nsat: Some(12),
355-
stack_elem_count_sat: Some(6),
356-
stack_elem_count_dissat: Some(6),
352+
ops_count_static: 13,
353+
ops_count_sat: Some(13),
354+
ops_count_nsat: Some(13),
355+
stack_elem_count_sat: Some(7),
356+
stack_elem_count_dissat: Some(7),
357357
max_sat_size: Some((max_wit_sz, max_wit_sz)),
358358
max_dissat_size: Some((0, 0)), // all empty should dissatisfy
359359
timelock_info: TimeLockInfo::default(),

0 commit comments

Comments
 (0)