Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
27 changes: 12 additions & 15 deletions src/miniscript/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ impl ScriptContext for Legacy {
fn check_local_consensus_validity<Pk: MiniscriptKey>(
ms: &Miniscript<Pk, Self>,
) -> Result<(), ScriptContextError> {
match ms.ext.ops.op_count() {
match ms.ext.sat_op_count() {
None => Err(ScriptContextError::ImpossibleSatisfaction),
Some(op_count) if op_count > MAX_OPS_PER_SCRIPT => {
Err(ScriptContextError::MaxOpCountExceeded {
Expand Down Expand Up @@ -467,8 +467,7 @@ impl ScriptContext for Legacy {
}

fn max_satisfaction_size<Pk: MiniscriptKey>(ms: &Miniscript<Pk, Self>) -> Option<usize> {
// The scriptSig cost is the second element of the tuple
ms.ext.max_sat_size.map(|x| x.1)
ms.ext.sat_data.map(|data| data.max_script_sig_size)
}

fn pk_len<Pk: MiniscriptKey>(pk: &Pk) -> usize {
Expand Down Expand Up @@ -551,7 +550,7 @@ impl ScriptContext for Segwitv0 {
fn check_local_consensus_validity<Pk: MiniscriptKey>(
ms: &Miniscript<Pk, Self>,
) -> Result<(), ScriptContextError> {
match ms.ext.ops.op_count() {
match ms.ext.sat_op_count() {
None => Err(ScriptContextError::ImpossibleSatisfaction),
Some(op_count) if op_count > MAX_OPS_PER_SCRIPT => {
Err(ScriptContextError::MaxOpCountExceeded {
Expand Down Expand Up @@ -595,8 +594,7 @@ impl ScriptContext for Segwitv0 {
}

fn max_satisfaction_size<Pk: MiniscriptKey>(ms: &Miniscript<Pk, Self>) -> Option<usize> {
// The witness stack cost is the first element of the tuple
ms.ext.max_sat_size.map(|x| x.0)
ms.ext.sat_data.map(|data| data.max_witness_stack_size)
}

fn pk_len<Pk: MiniscriptKey>(_pk: &Pk) -> usize { 34 }
Expand Down Expand Up @@ -688,11 +686,10 @@ impl ScriptContext for Tap {
// will have it's corresponding 64 bytes signature.
// sigops budget = witness_script.len() + witness.size() + 50
// Each signature will cover it's own cost(64 > 50) and thus will will never exceed the budget
if let (Some(s), Some(h)) = (ms.ext.exec_stack_elem_count_sat, ms.ext.stack_elem_count_sat)
{
if s + h > MAX_STACK_SIZE {
if let Some(data) = ms.ext.sat_data {
if data.max_witness_stack_count + data.max_exec_stack_count > MAX_STACK_SIZE {
return Err(ScriptContextError::StackSizeLimitExceeded {
actual: s + h,
actual: data.max_witness_stack_count + data.max_exec_stack_count,
limit: MAX_STACK_SIZE,
});
}
Expand All @@ -714,8 +711,7 @@ impl ScriptContext for Tap {
}

fn max_satisfaction_size<Pk: MiniscriptKey>(ms: &Miniscript<Pk, Self>) -> Option<usize> {
// The witness stack cost is the first element of the tuple
ms.ext.max_sat_size.map(|x| x.0)
ms.ext.sat_data.map(|data| data.max_witness_stack_size)
}

fn sig_type() -> SigType { SigType::Schnorr }
Expand Down Expand Up @@ -787,7 +783,7 @@ impl ScriptContext for BareCtx {
fn check_local_consensus_validity<Pk: MiniscriptKey>(
ms: &Miniscript<Pk, Self>,
) -> Result<(), ScriptContextError> {
match ms.ext.ops.op_count() {
match ms.ext.sat_op_count() {
None => Err(ScriptContextError::ImpossibleSatisfaction),
Some(op_count) if op_count > MAX_OPS_PER_SCRIPT => {
Err(ScriptContextError::MaxOpCountExceeded {
Expand All @@ -812,8 +808,9 @@ impl ScriptContext for BareCtx {
}

fn max_satisfaction_size<Pk: MiniscriptKey>(ms: &Miniscript<Pk, Self>) -> Option<usize> {
// The witness stack cost is the first element of the tuple
ms.ext.max_sat_size.map(|x| x.1)
// For bare outputs the script appears in the scriptpubkey; its cost
// is the same as for a legacy scriptsig.
ms.ext.sat_data.map(|data| data.max_script_sig_size)
}

fn pk_len<Pk: MiniscriptKey>(pk: &Pk) -> usize {
Expand Down
6 changes: 3 additions & 3 deletions src/miniscript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Miniscript<Pk, Ctx> {
/// impossible to satisfy
pub fn max_satisfaction_witness_elements(&self) -> Result<usize, Error> {
self.ext
.stack_elem_count_sat
.map(|x| x + 1)
.sat_data
.map(|data| data.max_witness_stack_count + 1)
.ok_or(Error::ImpossibleSatisfaction)
}

Expand Down Expand Up @@ -1193,7 +1193,7 @@ mod tests {
assert_eq!(format!("{:x}", ms.encode()), expected_hex);
assert_eq!(ms.ty.mall.non_malleable, non_mal);
assert_eq!(ms.ty.mall.safe, need_sig);
assert_eq!(ms.ext.ops.op_count().unwrap(), ops);
assert_eq!(ms.ext.static_ops + ms.ext.sat_data.unwrap().max_exec_op_count, ops);
}
(Err(_), false) => {}
_ => unreachable!(),
Expand Down
Loading
Loading