Skip to content

Commit

Permalink
calculate sig ops on fly
Browse files Browse the repository at this point in the history
  • Loading branch information
biryukovmaxim committed Nov 14, 2024
1 parent 1d3b9a9 commit 3e023f7
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use kaspa_consensus_core::{
tx::{TransactionInput, VerifiableTransaction},
};
use kaspa_core::warn;
use kaspa_txscript::{caches::Cache, get_sig_op_count, SigCacheKey, TxScriptEngine};
use kaspa_txscript::{caches::Cache, SigCacheKey, TxScriptEngine};
use kaspa_txscript_errors::TxScriptError;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use rayon::ThreadPool;
Expand Down Expand Up @@ -60,7 +60,7 @@ impl TransactionValidator {

match flags {
TxValidationFlags::Full | TxValidationFlags::SkipMassCheck => {
Self::check_sig_op_counts(tx)?;
// Self::check_sig_op_counts(tx)?;
self.check_scripts(tx, pov_daa_score)?;
}
TxValidationFlags::SkipScriptChecks => {}
Expand Down Expand Up @@ -162,12 +162,14 @@ impl TransactionValidator {
Ok(())
}

fn check_sig_op_counts<T: VerifiableTransaction>(tx: &T) -> TxResult<()> {
fn check_sig_op_counts<T: VerifiableTransaction>(tx: &T, kip10_enabled: bool) -> Result<(), TxScriptError> {
// Initialize a cache for signature verification
let sig_cache = Cache::new(10_000);
// Prepare to reuse values for signature hashing
let reused_values = SigHashReusedValuesUnsync::new();
for (i, (input, entry)) in tx.populated_inputs().enumerate() {
let calculated = get_sig_op_count::<T, SigHashReusedValuesUnsync>(&input.signature_script, &entry.script_public_key);
if calculated != input.sig_op_count as u64 {
return Err(TxRuleError::WrongSigOpCount(i, input.sig_op_count as u64, calculated));
}
let mut vm = TxScriptEngine::from_transaction_input(tx, input, i, entry, &reused_values, &sig_cache, kip10_enabled);
vm.execute()?;
}
Ok(())
}
Expand Down Expand Up @@ -823,6 +825,7 @@ mod tests {
let signed_tx = sign(MutableTransaction::with_entries(unsigned_tx, entries), schnorr_key);
let populated_tx = signed_tx.as_verifiable();
assert_eq!(tv.check_scripts(&populated_tx, u64::MAX), Ok(()));
assert_eq!(TransactionValidator::check_sig_op_counts(&populated_tx), Ok(()));
// check scripts internally checks sig op count
// assert_eq!(TransactionValidator::check_sig_op_counts(&populated_tx), Ok(()));
}
}
2 changes: 2 additions & 0 deletions crypto/txscript/errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub enum TxScriptError {
InvalidOutputIndex(i32, usize),
#[error(transparent)]
Serialization(#[from] SerializationError),
#[error("sig op count exceed passed limit of {1}")]
SigOpCountTooLow(u16, u8),
}

#[derive(Error, PartialEq, Eq, Debug, Clone, Copy)]
Expand Down
Loading

0 comments on commit 3e023f7

Please sign in to comment.