Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl check_error_condition #241

Merged
merged 5 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
64 changes: 34 additions & 30 deletions packages/engine/src/engine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ pub trait EngineExtrasTrait<T> {
fn is_witness_active(ref self: Engine<T>, version: i64) -> bool;
// Return the script since last OP_CODESEPARATOR
fn sub_script(ref self: Engine<T>) -> ByteArray;
// Check if the script has failed and return an error if it has
fn check_error_condition(ref self: Engine<T>) -> Result<ByteArray, felt252>;
}

pub impl EngineExtrasImpl<T, +Drop<T>> of EngineExtrasTrait<T> {
Expand Down Expand Up @@ -203,6 +205,36 @@ pub impl EngineExtrasImpl<T, +Drop<T>> of EngineExtrasTrait<T> {
};
return sub_script;
}

fn check_error_condition(ref self: Engine<T>) -> Result<ByteArray, felt252> {
// Check if execution is actually done
if self.script_idx < self.scripts.len() {
return Result::Err(Error::SCRIPT_UNFINISHED);
}

// Check if witness stack is clean
if self.is_witness_active(0)
&& self.dstack.len() != 1 { // TODO: Hardcoded 0
return Result::Err(Error::SCRIPT_NON_CLEAN_STACK);
}
if self.has_flag(ScriptFlags::ScriptVerifyCleanStack)
&& self.dstack.len() != 1 {
return Result::Err(Error::SCRIPT_NON_CLEAN_STACK);
}

// Check if stack has at least one item
if self.dstack.len() < 1 {
return Result::Err(Error::SCRIPT_EMPTY_STACK);
} else {
// Check the final stack value
let is_ok = self.dstack.peek_bool(0)?;
if is_ok {
return Result::Ok(self.dstack.peek_byte_array(0)?);
} else {
return Result::Err(Error::SCRIPT_FAILED);
}
}
}
}

pub trait EngineInternalTrait {
Expand Down Expand Up @@ -640,36 +672,8 @@ pub impl EngineInternalImpl of EngineInternalTrait {
return Result::Err(err);
}

// TODO: CheckErrorCondition
if self.is_witness_active(0) && self.dstack.len() != 1 { // TODO: Hardcoded 0
return Result::Err(Error::SCRIPT_NON_CLEAN_STACK);
}
if self.has_flag(ScriptFlags::ScriptVerifyCleanStack) && self.dstack.len() != 1 {
return Result::Err(Error::SCRIPT_NON_CLEAN_STACK);
}

if self.dstack.len() < 1 {
return Result::Err(Error::SCRIPT_EMPTY_STACK);
} else {
// TODO: pop bool?
let top_stack = self.dstack.peek_byte_array(0)?;
let ret_val = top_stack.clone();
let mut is_ok = false;
let mut i = 0;
while i != top_stack.len() {
if top_stack[i] != 0 {
is_ok = true;
break;
}
i += 1;
};
if is_ok {
return Result::Ok(ret_val);
} else {
return Result::Err(Error::SCRIPT_FAILED);
}
}
}
return self.check_error_condition();
}

fn verify_witness(
ref self: Engine<Transaction>, witness: Span<ByteArray>
Expand Down
2 changes: 2 additions & 0 deletions packages/engine/src/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub mod Error {
pub const DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM: felt252 = 'Upgradable witness program';
pub const WITNESS_PROGRAM_INVALID: felt252 = 'Invalid witness program';
pub const SCRIPT_TOO_LARGE: felt252 = 'Script is too large';

pub const SCRIPT_UNFINISHED: felt252 = 'Script unfinished';
}

pub fn byte_array_err(err: felt252) -> ByteArray {
Expand Down