Skip to content

Commit 9ae4d7b

Browse files
committed
Check stack execution height in tapscript execution
1 parent 34342f1 commit 9ae4d7b

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/miniscript/context.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ pub enum ScriptContextError {
6262
ImpossibleSatisfaction,
6363
/// No Multi Node in Taproot context
6464
TaprootMultiDisabled,
65+
/// Stack size exceeded in script execution
66+
StackSizeLimitExceeded,
6567
}
6668

6769
impl fmt::Display for ScriptContextError {
@@ -119,6 +121,12 @@ impl fmt::Display for ScriptContextError {
119121
ScriptContextError::TaprootMultiDisabled => {
120122
write!(f, "No Multi node in taproot context")
121123
}
124+
ScriptContextError::StackSizeLimitExceeded => {
125+
write!(
126+
f,
127+
"Stack limit can exceed in atleast one script path during script execution"
128+
)
129+
}
122130
}
123131
}
124132
}
@@ -512,7 +520,7 @@ impl ScriptContext for Tap {
512520
}
513521

514522
fn check_local_consensus_validity<Pk: MiniscriptKey, Ctx: ScriptContext>(
515-
_ms: &Miniscript<Pk, Ctx>,
523+
ms: &Miniscript<Pk, Ctx>,
516524
) -> Result<(), ScriptContextError> {
517525
// Taproot introduces the concept of sigops budget.
518526
// In all possible valid miniscripts satisfy the given sigops constraint
@@ -521,8 +529,15 @@ impl ScriptContext for Tap {
521529
// own unique signature. That is there is no way to re-use signatures for another
522530
// checksig. Therefore, for each successfully executed checksig, we will have
523531
// 64 bytes signature and thus sigops budget is always covered.
524-
// There is overall limit of consensus
525532
// TODO: track height during execution
533+
if let (Some(s), Some(h)) = (
534+
ms.ext.exec_stack_elem_count_sat,
535+
ms.ext.stack_elem_count_sat,
536+
) {
537+
if s + h > MAX_STACK_SIZE {
538+
return Err(ScriptContextError::StackSizeLimitExceeded);
539+
}
540+
}
526541
Ok(())
527542
}
528543

@@ -536,7 +551,6 @@ impl ScriptContext for Tap {
536551
fn check_local_policy_validity<Pk: MiniscriptKey, Ctx: ScriptContext>(
537552
_ms: &Miniscript<Pk, Ctx>,
538553
) -> Result<(), ScriptContextError> {
539-
// TODO: check for policy execution.
540554
Ok(())
541555
}
542556

0 commit comments

Comments
 (0)