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

completed txscript tests and fixed some opcodes #242

Merged
merged 5 commits into from
Oct 2, 2023
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crypto/txscript/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ serde.workspace = true
criterion.workspace = true
smallvec.workspace = true
hex = "0.4"
serde_json = "1.0"

[[bench]]
name = "bench"
Expand Down
8 changes: 5 additions & 3 deletions crypto/txscript/errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ pub enum TxScriptError {
MalformedPushSize(Vec<u8>),
#[error("opcode requires {0} bytes, but script only has {1} remaining")]
MalformedPush(usize, usize),
// We return error if stack entry is false
#[error("false stack entry at end of script execution")]
FalseStackEntry,
#[error("transaction input index {0} >= {1}")]
InvalidIndex(usize, usize),
#[error("combined stack size {0} > max allowed {1}")]
Expand All @@ -23,6 +20,7 @@ pub enum TxScriptError {
EmptyStack,
#[error("stack contains {0} unexpected items")]
CleanStack(usize),
// We return error if stack entry is false
#[error("false stack entry at end of script execution")]
EvalFalse,
#[error("script returned early")]
Expand Down Expand Up @@ -67,4 +65,8 @@ pub enum TxScriptError {
SignatureScriptNotPushOnly,
#[error("end of script reached in conditional execution")]
ErrUnbalancedConditional,
#[error("opcode requires at least {0} but stack has only {1}")]
InvalidStackOperation(usize, usize),
#[error("script of size {0} exceeded maximum allowed size of {1}")]
ScriptSize(usize, usize),
}
18 changes: 9 additions & 9 deletions crypto/txscript/src/data_stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl DataStack for Stack {
Vec<u8>: OpcodeData<T>,
{
if self.len() < SIZE {
return Err(TxScriptError::EmptyStack);
return Err(TxScriptError::InvalidStackOperation(SIZE, self.len()));
}
Ok(<[T; SIZE]>::try_from(self.split_off(self.len() - SIZE).iter().map(|v| v.deserialize()).collect::<Result<Vec<T>, _>>()?)
.expect("Already exact item"))
Expand All @@ -193,7 +193,7 @@ impl DataStack for Stack {
Vec<u8>: OpcodeData<T>,
{
if self.len() < SIZE {
return Err(TxScriptError::EmptyStack);
return Err(TxScriptError::InvalidStackOperation(SIZE, self.len()));
}
Ok(<[T; SIZE]>::try_from(self[self.len() - SIZE..].iter().map(|v| v.deserialize()).collect::<Result<Vec<T>, _>>()?)
.expect("Already exact item"))
Expand All @@ -202,15 +202,15 @@ impl DataStack for Stack {
#[inline]
fn pop_raw<const SIZE: usize>(&mut self) -> Result<[Vec<u8>; SIZE], TxScriptError> {
if self.len() < SIZE {
return Err(TxScriptError::EmptyStack);
return Err(TxScriptError::InvalidStackOperation(SIZE, self.len()));
}
Ok(<[Vec<u8>; SIZE]>::try_from(self.split_off(self.len() - SIZE)).expect("Already exact item"))
}

#[inline]
fn peek_raw<const SIZE: usize>(&self) -> Result<[Vec<u8>; SIZE], TxScriptError> {
if self.len() < SIZE {
return Err(TxScriptError::EmptyStack);
return Err(TxScriptError::InvalidStackOperation(SIZE, self.len()));
}
Ok(<[Vec<u8>; SIZE]>::try_from(self[self.len() - SIZE..].to_vec()).expect("Already exact item"))
}
Expand All @@ -230,7 +230,7 @@ impl DataStack for Stack {
self.truncate(self.len() - SIZE);
Ok(())
}
false => Err(TxScriptError::EmptyStack),
false => Err(TxScriptError::InvalidStackOperation(SIZE, self.len())),
}
}

Expand All @@ -241,7 +241,7 @@ impl DataStack for Stack {
self.extend_from_within(self.len() - SIZE..);
Ok(())
}
false => Err(TxScriptError::EmptyStack),
false => Err(TxScriptError::InvalidStackOperation(SIZE, self.len())),
}
}

Expand All @@ -252,7 +252,7 @@ impl DataStack for Stack {
self.extend_from_within(self.len() - 2 * SIZE..self.len() - SIZE);
Ok(())
}
false => Err(TxScriptError::EmptyStack),
false => Err(TxScriptError::InvalidStackOperation(2 * SIZE, self.len())),
}
}

Expand All @@ -264,7 +264,7 @@ impl DataStack for Stack {
self.extend(drained);
Ok(())
}
false => Err(TxScriptError::EmptyStack),
false => Err(TxScriptError::InvalidStackOperation(3 * SIZE, self.len())),
}
}

Expand All @@ -276,7 +276,7 @@ impl DataStack for Stack {
self.extend(drained);
Ok(())
}
false => Err(TxScriptError::EmptyStack),
false => Err(TxScriptError::InvalidStackOperation(2 * SIZE, self.len())),
}
}
}
Expand Down
Loading