Skip to content

Commit

Permalink
completed txscript tests and fixed some opcodes (#242)
Browse files Browse the repository at this point in the history
* completed txscript tests and fixed some opcodes

* implemented bitcoind tests and fixed errors discovered in testing process

* changed to go-kaspad

* fixed clippy issue

* fixed from PR
  • Loading branch information
tmrlvi authored Oct 2, 2023
1 parent 41d14dd commit 222ae83
Show file tree
Hide file tree
Showing 10 changed files with 6,963 additions and 47 deletions.
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

0 comments on commit 222ae83

Please sign in to comment.