Skip to content

Commit

Permalink
Merge #758: fix: differentiate max witness script size upon context
Browse files Browse the repository at this point in the history
13a1318 fix: differentiate max witness script size upon context (ChrisCho-H)

Pull request description:

  `MaxWitnessScriptSizeExceeded` is used in the context of `SegwitV0` and `Tap`, where each of max witness script size differs. Moreover, even in the same context of `SegwitV0`, max witness script size differ whether it's standard or consensus rule. I just let `MaxWitnessScriptSizeExceeded` receive param `usize` to differentiate max witness script size upon context, which can fix the wrong err message of `"The Miniscript corresponding Script would be larger than MAX_STANDARD_P2WSH_SCRIPT_SIZE  bytes."` when `SegwitV0` consensus and `Tap` context

ACKs for top commit:
  apoelstra:
    ACK 13a1318; successfully ran local tests

Tree-SHA512: 213d62443fa8b4d912495e1d60aa1e64165287348b145eb1c93f2e623589c49b0e37636f92d9185af4c3467006d0c0d5f6bf8d986ab1ff332dc5510900e0f1eb
  • Loading branch information
apoelstra committed Oct 18, 2024
2 parents 59506c6 + 13a1318 commit 0f03df0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
29 changes: 20 additions & 9 deletions src/miniscript/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ pub enum ScriptContextError {
/// than `MAX_OPS_PER_SCRIPT`(201) opcodes.
MaxOpCountExceeded,
/// The Miniscript(under segwit context) corresponding
/// Script would be larger than `MAX_STANDARD_P2WSH_SCRIPT_SIZE` bytes.
MaxWitnessScriptSizeExceeded,
/// Script would be larger than `MAX_STANDARD_P2WSH_SCRIPT_SIZE`,
/// `MAX_SCRIPT_SIZE` or `MAX_BLOCK`(`Tap`) bytes.
MaxWitnessScriptSizeExceeded { max: usize, got: usize },
/// The Miniscript (under p2sh context) corresponding Script would be
/// larger than `MAX_SCRIPT_ELEMENT_SIZE` bytes.
MaxRedeemScriptSizeExceeded,
Expand Down Expand Up @@ -81,7 +82,7 @@ impl error::Error for ScriptContextError {
| UncompressedKeysNotAllowed
| MaxWitnessItemsExceeded { .. }
| MaxOpCountExceeded
| MaxWitnessScriptSizeExceeded
| MaxWitnessScriptSizeExceeded { .. }
| MaxRedeemScriptSizeExceeded
| MaxBareScriptSizeExceeded
| MaxScriptSigSizeExceeded
Expand Down Expand Up @@ -121,10 +122,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, got } => write!(
f,
"The Miniscript corresponding Script would be larger than \
MAX_STANDARD_P2WSH_SCRIPT_SIZE bytes."
"The Miniscript corresponding Script cannot be larger than \
{} bytes, but got {} bytes.",
max, got
),
ScriptContextError::MaxRedeemScriptSizeExceeded => write!(
f,
Expand Down Expand Up @@ -525,7 +527,10 @@ impl ScriptContext for Segwitv0 {
match node_checked {
Ok(_) => {
if ms.ext.pk_cost > MAX_SCRIPT_SIZE {
Err(ScriptContextError::MaxWitnessScriptSizeExceeded)
Err(ScriptContextError::MaxWitnessScriptSizeExceeded {
max: MAX_SCRIPT_SIZE,
got: ms.ext.pk_cost,
})
} else {
Ok(())
}
Expand All @@ -550,7 +555,10 @@ 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: MAX_STANDARD_P2WSH_SCRIPT_SIZE,
got: ms.ext.pk_cost,
});
}
Ok(())
}
Expand Down Expand Up @@ -644,7 +652,10 @@ 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 {
max: Weight::MAX_BLOCK.to_wu() as usize,
got: ms.ext.pk_cost,
})
} 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 cannot be larger than 3600 bytes, but got 4110 bytes."
);
assert_eq!(
bare_multi_ms.unwrap_err().to_string(),
Expand Down

0 comments on commit 0f03df0

Please sign in to comment.