Skip to content

Commit

Permalink
fix: differentiate max witness script size upon context
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisCho-H committed Oct 17, 2024
1 parent 7ae7596 commit 5a3989c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
19 changes: 12 additions & 7 deletions src/miniscript/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub enum ScriptContextError {
MaxOpCountExceeded,
/// The Miniscript(under segwit context) corresponding
/// Script would be larger than `MAX_STANDARD_P2WSH_SCRIPT_SIZE` bytes.
MaxWitnessScriptSizeExceeded,
MaxWitnessScriptSizeExceeded(usize),
/// The Miniscript (under p2sh context) corresponding Script would be
/// larger than `MAX_SCRIPT_ELEMENT_SIZE` bytes.
MaxRedeemScriptSizeExceeded,
Expand Down Expand Up @@ -81,7 +81,7 @@ impl error::Error for ScriptContextError {
| UncompressedKeysNotAllowed
| MaxWitnessItemssExceeded { .. }
| MaxOpCountExceeded
| MaxWitnessScriptSizeExceeded
| MaxWitnessScriptSizeExceeded(_)
| MaxRedeemScriptSizeExceeded
| MaxBareScriptSizeExceeded
| MaxScriptSigSizeExceeded
Expand Down Expand Up @@ -121,10 +121,11 @@ impl fmt::Display for ScriptContextError {
"At least one satisfaction path in the Miniscript fragment contains \
more than MAX_OPS_PER_SCRIPT opcodes."
),
ScriptContextError::MaxWitnessScriptSizeExceeded => write!(
ScriptContextError::MaxWitnessScriptSizeExceeded(max_size) => write!(
f,
"The Miniscript corresponding Script would be larger than \
MAX_STANDARD_P2WSH_SCRIPT_SIZE bytes."
{} bytes.",
max_size
),
ScriptContextError::MaxRedeemScriptSizeExceeded => write!(
f,
Expand Down Expand Up @@ -525,7 +526,7 @@ impl ScriptContext for Segwitv0 {
match node_checked {
Ok(_) => {
if ms.ext.pk_cost > MAX_SCRIPT_SIZE {
Err(ScriptContextError::MaxWitnessScriptSizeExceeded)
Err(ScriptContextError::MaxWitnessScriptSizeExceeded(MAX_SCRIPT_SIZE))
} else {
Ok(())
}
Expand All @@ -550,7 +551,9 @@ impl ScriptContext for Segwitv0 {
ms: &Miniscript<Pk, Self>,
) -> Result<(), ScriptContextError> {
if ms.ext.pk_cost > MAX_STANDARD_P2WSH_SCRIPT_SIZE {
return Err(ScriptContextError::MaxWitnessScriptSizeExceeded);
return Err(ScriptContextError::MaxWitnessScriptSizeExceeded(
MAX_STANDARD_P2WSH_SCRIPT_SIZE,
));
}
Ok(())
}
Expand Down Expand Up @@ -644,7 +647,9 @@ impl ScriptContext for Tap {
// some guarantees are not easy to satisfy because of knapsack
// constraints
if ms.ext.pk_cost as u64 > Weight::MAX_BLOCK.to_wu() {
Err(ScriptContextError::MaxWitnessScriptSizeExceeded)
Err(ScriptContextError::MaxWitnessScriptSizeExceeded(
Weight::MAX_BLOCK.to_wu() as usize
))
} else {
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/miniscript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,7 @@ mod tests {
);
assert_eq!(
segwit_multi_ms.unwrap_err().to_string(),
"The Miniscript corresponding Script would be larger than MAX_STANDARD_P2WSH_SCRIPT_SIZE bytes."
"The Miniscript corresponding Script would be larger than 3600 bytes."
);
assert_eq!(
bare_multi_ms.unwrap_err().to_string(),
Expand Down

0 comments on commit 5a3989c

Please sign in to comment.