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

check for stack size before executing the opcode #182

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 17 additions & 1 deletion src/engine.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,14 @@ pub trait EngineTrait {
fn has_flag(ref self: Engine, flag: ScriptFlags) -> bool;
// Return the script since last OP_CODESEPARATOR
fn sub_script(ref self: Engine) -> ByteArray;
// Ensure the stack size is within limits
fn check_stack_size(ref self: Engine) -> Result<(), felt252>;
// Print engine data as a JSON object
fn json(ref self: Engine);
}

pub const MAX_STACK_SIZE: u32 = 1000;

pub impl EngineImpl of EngineTrait {
fn new(
script_pubkey: @ByteArray, transaction: Transaction, tx_idx: u32, flags: u32, amount: i64
Expand Down Expand Up @@ -135,8 +139,8 @@ pub impl EngineImpl of EngineTrait {
if res.is_err() {
return Result::Err(res.unwrap_err());
}
self.check_stack_size()?;
b-j-roberts marked this conversation as resolved.
Show resolved Hide resolved
self.opcode_idx += 1;

if self.opcode_idx >= script.len() {
if self.cond_stack.len() > 0 {
return Result::Err(Error::SCRIPT_UNBALANCED_CONDITIONAL_STACK);
Expand Down Expand Up @@ -183,6 +187,11 @@ pub impl EngineImpl of EngineTrait {
err = res.unwrap_err();
break;
}
let res = self.check_stack_size();
if res.is_err() {
err = res.unwrap_err();
break;
}
self.opcode_idx += 1;
};
if err != '' {
Expand Down Expand Up @@ -245,6 +254,13 @@ pub impl EngineImpl of EngineTrait {
return sub_script;
}

fn check_stack_size(ref self: Engine) -> Result<(), felt252> {
if self.dstack.len() + self.astack.len() > MAX_STACK_SIZE {
return Result::Err(Error::SCRIPT_STACK_SIZE_EXCEEDED);
}
return Result::Ok(());
}

fn json(ref self: Engine) {
self.dstack.json();
}
Expand Down
1 change: 1 addition & 0 deletions src/errors.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod Error {
pub const FINALIZED_TX_CLTV: felt252 = 'Finalized tx in OP_CLTV';
pub const INVALID_TX_VERSION: felt252 = 'Invalid transaction version';
pub const SCRIPT_INVALID: felt252 = 'Invalid script data';
pub const SCRIPT_STACK_SIZE_EXCEEDED: felt252 = 'Script stack size exceeded';
}

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