Skip to content

Commit

Permalink
Stop using extract_enum_value
Browse files Browse the repository at this point in the history
  • Loading branch information
someone235 committed Sep 14, 2022
1 parent 96008df commit f5465c3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ mod tests {
tx::{Transaction, TransactionInput, TransactionOutpoint},
};
use hashes::Hash;
use kaspa_core::extract_enum_value;
use std::sync::Arc;

#[tokio::test]
Expand All @@ -117,7 +116,7 @@ mod tests {
{
let block = consensus.build_block_with_parents_and_transactions(2.into(), vec![1.into()], vec![]);
// We expect a missing parents error since the parent is header only.
extract_enum_value!(body_processor.validate_body_in_context(&block),Err(RuleError::MissingParents(_)) => ());
assert!(matches!(body_processor.validate_body_in_context(&block), Err(RuleError::MissingParents(_))));
}

let valid_block =
Expand All @@ -132,10 +131,12 @@ mod tests {
block.header.hash_merkle_root = calc_hash_merkle_root(block.transactions.iter());

let block = Arc::new(block);
extract_enum_value!(consensus.validate_and_insert_block(block.clone()).await,Err(RuleError::WrongSubsidy(expected,_)) => (assert_eq!(expected, 50000000000)));
assert!(
matches!(consensus.validate_and_insert_block(block.clone()).await,Err(RuleError::WrongSubsidy(expected,_)) if expected == 50000000000)
);

// The second time we send an invalid block we expect it to be a known invalid.
extract_enum_value!(consensus.validate_and_insert_block(block).await,Err(RuleError::KnownInvalid) => ());
assert!(matches!(consensus.validate_and_insert_block(block).await, Err(RuleError::KnownInvalid)));
}

let valid_block_child =
Expand All @@ -149,7 +150,9 @@ mod tests {
let mut block = consensus.build_block_with_parents_and_transactions(5.into(), vec![4.into()], vec![]);
Arc::make_mut(&mut block.transactions)[0].payload[8..16].copy_from_slice(&(5_u64).to_le_bytes());
block.header.hash_merkle_root = calc_hash_merkle_root(block.transactions.iter());
extract_enum_value!(consensus.validate_and_insert_block(Arc::new(block)).await,Err(RuleError::WrongSubsidy(expected,_)) => (assert_eq!(expected, 44000000000)));
assert!(
matches!(consensus.validate_and_insert_block(Arc::new(block)).await,Err(RuleError::WrongSubsidy(expected,_)) if expected == 44000000000)
);
}

{
Expand Down Expand Up @@ -276,7 +279,12 @@ mod tests {
.await
.unwrap();
} else {
extract_enum_value!(consensus.validate_and_insert_block(Arc::new(block)).await,Err(RuleError::TxInContextFailed(_,e)) => (extract_enum_value!(e, TxRuleError::NotFinalized(_) => ())));
assert!(matches!(
consensus
.validate_and_insert_block(Arc::new(block))
.await,
Err(RuleError::TxInContextFailed(_, e)) if matches!(e, TxRuleError::NotFinalized(_))
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ mod tests {
tx::{ScriptPublicKey, Transaction, TransactionId, TransactionInput, TransactionOutpoint, TransactionOutput},
};
use hashes::Hash;
use kaspa_core::extract_enum_value;

use crate::{consensus::test_consensus::TestConsensus, errors::RuleError, params::MAINNET_PARAMS};

Expand Down Expand Up @@ -444,56 +443,59 @@ mod tests {

let mut block = example_block.clone();
Arc::make_mut(&mut block.transactions)[1].version += 1;
extract_enum_value!(body_processor.validate_body_in_isolation(&block),Err(RuleError::BadMerkleRoot(_, _)) => ());
assert!(matches!(body_processor.validate_body_in_isolation(&block), Err(RuleError::BadMerkleRoot(_, _))));

let mut block = example_block.clone();
let txs = Arc::make_mut(&mut block.transactions);
Arc::make_mut(&mut txs[1].inputs[0]).sig_op_count = 255;
Arc::make_mut(&mut txs[1].inputs[1]).sig_op_count = 255;
extract_enum_value!(body_processor.validate_body_in_isolation(&block),Err(RuleError::ExceedsMassLimit(_)) => ());
assert!(matches!(body_processor.validate_body_in_isolation(&block), Err(RuleError::ExceedsMassLimit(_))));

let mut block = example_block.clone();
let txs = Arc::make_mut(&mut block.transactions);
txs.push(txs[1].clone());
block.header.hash_merkle_root = calc_hash_merkle_root(txs.iter());
extract_enum_value!(body_processor.validate_body_in_isolation(&block),Err(RuleError::DuplicateTransactions(_)) => ());
assert!(matches!(body_processor.validate_body_in_isolation(&block), Err(RuleError::DuplicateTransactions(_))));

let mut block = example_block.clone();
let txs = Arc::make_mut(&mut block.transactions);
txs[1].subnetwork_id = SUBNETWORK_ID_COINBASE;
block.header.hash_merkle_root = calc_hash_merkle_root(txs.iter());
extract_enum_value!(body_processor.validate_body_in_isolation(&block),Err(RuleError::MultipleCoinbases(_)) => ());
assert!(matches!(body_processor.validate_body_in_isolation(&block), Err(RuleError::MultipleCoinbases(_))));

let mut block = example_block.clone();
let txs = Arc::make_mut(&mut block.transactions);
txs[0].payload = vec![];
block.header.hash_merkle_root = calc_hash_merkle_root(txs.iter());
extract_enum_value!(body_processor.validate_body_in_isolation(&block),Err(RuleError::BadCoinbasePayload(_)) => ());
assert!(matches!(body_processor.validate_body_in_isolation(&block), Err(RuleError::BadCoinbasePayload(_))));

let mut block = example_block.clone();
let txs = Arc::make_mut(&mut block.transactions);
Arc::make_mut(&mut txs[2].inputs[0]).previous_outpoint = txs[1].inputs[0].previous_outpoint;
block.header.hash_merkle_root = calc_hash_merkle_root(txs.iter());
extract_enum_value!(body_processor.validate_body_in_isolation(&block),Err(RuleError::DoubleSpendInSameBlock(_)) => ());
assert!(matches!(body_processor.validate_body_in_isolation(&block), Err(RuleError::DoubleSpendInSameBlock(_))));

let mut block = example_block.clone();
let txs = Arc::make_mut(&mut block.transactions);
txs[0].subnetwork_id = SUBNETWORK_ID_NATIVE;
block.header.hash_merkle_root = calc_hash_merkle_root(txs.iter());
extract_enum_value!(body_processor.validate_body_in_isolation(&block),Err(RuleError::FirstTxNotCoinbase) => ());
assert!(matches!(body_processor.validate_body_in_isolation(&block), Err(RuleError::FirstTxNotCoinbase)));

let mut block = example_block.clone();
let txs = Arc::make_mut(&mut block.transactions);
txs[1].inputs = vec![];
block.header.hash_merkle_root = calc_hash_merkle_root(txs.iter());
extract_enum_value!(body_processor.validate_body_in_isolation(&block),Err(RuleError::TxInIsolationValidationFailed(_, _)) => ());
assert!(matches!(
body_processor.validate_body_in_isolation(&block),
Err(RuleError::TxInIsolationValidationFailed(_, _))
));

let mut block = example_block;
let txs = Arc::make_mut(&mut block.transactions);
Arc::make_mut(&mut txs[3].inputs[0]).previous_outpoint =
TransactionOutpoint { transaction_id: txs[2].id(), index: 0 };
block.header.hash_merkle_root = calc_hash_merkle_root(txs.iter());
extract_enum_value!(body_processor.validate_body_in_isolation(&block),Err(RuleError::ChainedTransaction(_)) => ());
assert!(matches!(body_processor.validate_body_in_isolation(&block), Err(RuleError::ChainedTransaction(_))));

consensus.shutdown(wait_handles);
}
Expand All @@ -509,20 +511,30 @@ mod tests {
Arc::make_mut(&mut block.transactions)[0].version += 1;

let block = Arc::new(block);
extract_enum_value!(consensus.validate_and_insert_block(block.clone()).await,Err(RuleError::BadMerkleRoot(_, _)) => ());
assert!(matches!(
consensus
.validate_and_insert_block(block.clone())
.await,
Err(RuleError::BadMerkleRoot(_, _))
));

// BadMerkleRoot shouldn't mark the block as known invalid
extract_enum_value!(consensus.validate_and_insert_block(block).await,Err(RuleError::BadMerkleRoot(_, _)) => ());
assert!(matches!(consensus.validate_and_insert_block(block).await, Err(RuleError::BadMerkleRoot(_, _))));

let mut block =
consensus.build_block_with_parents_and_transactions(1.into(), vec![params.genesis_hash], vec![]);
block.header.parents_by_level[0][0] = 0.into();

let block = Arc::new(block);
extract_enum_value!(consensus.validate_and_insert_block(block.clone()).await,Err(RuleError::MissingParents( _)) => ());
assert!(matches!(
consensus
.validate_and_insert_block(block.clone())
.await,
Err(RuleError::MissingParents(_))
));

// MissingParents shouldn't mark the block as known invalid
extract_enum_value!(consensus.validate_and_insert_block(block).await,Err(RuleError::MissingParents( _)) => ());
assert!(matches!(consensus.validate_and_insert_block(block).await, Err(RuleError::MissingParents(_))));

consensus.shutdown(wait_handles);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ mod tests {
subnets::{SUBNETWORK_ID_COINBASE, SUBNETWORK_ID_NATIVE},
tx::{ScriptPublicKey, Transaction, TransactionId, TransactionInput, TransactionOutpoint, TransactionOutput},
};
use kaspa_core::extract_enum_value;

use crate::{
constants::TX_VERSION,
Expand Down Expand Up @@ -239,43 +238,43 @@ mod tests {

let mut tx = valid_tx.clone();
tx.inputs = vec![];
extract_enum_value!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::NoTxInputs) => ());
assert!(matches!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::NoTxInputs)));

let mut tx = valid_tx.clone();
tx.inputs = (0..params.max_tx_inputs + 1)
.map(|i| valid_tx.inputs[0].clone())
.collect();
extract_enum_value!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::TooManyInputs(_, _)) => ());
assert!(matches!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::TooManyInputs(_, _))));

let mut tx = valid_tx.clone();
Arc::make_mut(&mut tx.inputs[0]).signature_script = vec![0; params.max_signature_script_len + 1];
extract_enum_value!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::TooBigSignatureScript(_, _)) => ());
assert!(matches!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::TooBigSignatureScript(_, _))));

let mut tx = valid_tx.clone();
tx.outputs = (0..params.max_tx_outputs + 1)
.map(|i| valid_tx.outputs[0].clone())
.collect();
extract_enum_value!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::TooManyOutputs(_, _)) => ());
assert!(matches!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::TooManyOutputs(_, _))));

let mut tx = valid_tx.clone();
Arc::make_mut(&mut Arc::make_mut(&mut tx.outputs[0]).script_public_key).script =
vec![0; params.max_script_public_key_len + 1];
extract_enum_value!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::TooBigScriptPublicKey(_, _)) => ());
assert!(matches!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::TooBigScriptPublicKey(_, _))));

let mut tx = valid_tx.clone();
tx.inputs.push(tx.inputs[0].clone());
extract_enum_value!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::TxDuplicateInputs) => ());
assert!(matches!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::TxDuplicateInputs)));

let mut tx = valid_tx.clone();
tx.gas = 1;
extract_enum_value!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::TxHasGas) => ());
assert!(matches!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::TxHasGas)));

let mut tx = valid_tx.clone();
tx.payload = vec![0];
extract_enum_value!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::NonCoinbaseTxHasPayload) => ());
assert!(matches!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::NonCoinbaseTxHasPayload)));

let mut tx = valid_tx;
tx.version = TX_VERSION + 1;
extract_enum_value!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::UnknownTxVersion(_)) => ());
assert!(matches!(tv.validate_tx_in_isolation(&tx), Err(TxRuleError::UnknownTxVersion(_))));
}
}

0 comments on commit f5465c3

Please sign in to comment.