Skip to content

Commit

Permalink
Allow a message for the failure
Browse files Browse the repository at this point in the history
  • Loading branch information
brianp committed Nov 6, 2023
1 parent 92fca8f commit e1beaab
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
4 changes: 2 additions & 2 deletions infrastructure/tari_script/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ pub enum ScriptError {
VerifyFailed,
#[error("as_hash requires a Digest function that returns at least 32 bytes")]
InvalidDigest,
#[error("A compare opcode failed, aborting the script immediately")]
CompareFailed,
#[error("A compare opcode failed, aborting the script immediately with reason: `{0}`")]
CompareFailed(String),
}

impl From<TryFromIntError> for ScriptError {
Expand Down
14 changes: 11 additions & 3 deletions infrastructure/tari_script/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,11 @@ impl TariScript {
// This differs from compare_height due to a stack number being used, which can be lower than 0
let item = match block_height.checked_sub(height) {
Some(num) => StackItem::Number(num),
None => return Err(ScriptError::CompareFailed),
None => {
return Err(ScriptError::CompareFailed(
"Subtraction of given height from current block height failed".to_string(),
))
},
};

stack.push(item)
Expand All @@ -370,7 +374,11 @@ impl TariScript {
// height does not use a stack number and it's minimum can't be lower than 0.
let item = match block_height.checked_sub(target_height) {
Some(num) => StackItem::Number(num),
None => return Err(ScriptError::CompareFailed),
None => {
return Err(ScriptError::CompareFailed(
"Couldn't subtract the target height from the current block height".to_string(),
))
},
};

stack.push(item)
Expand Down Expand Up @@ -1757,7 +1765,7 @@ mod test {
let inputs = ExecutionStack::new(vec![Number(i64::MIN)]);
let ctx = context_with_height(i64::MAX as u64);
let stack_item = script.execute_with_context(&inputs, &ctx);
assert!(matches!(stack_item, Err(ScriptError::CompareFailed)));
assert!(matches!(stack_item, Err(ScriptError::CompareFailed(_))));
}

#[test]
Expand Down

0 comments on commit e1beaab

Please sign in to comment.