Skip to content

Commit

Permalink
some more changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Stacks committed Nov 23, 2024
1 parent 4412379 commit ba8c7f8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 27 deletions.
12 changes: 4 additions & 8 deletions consensus/src/model/stores/block_transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,8 @@ impl DbBlockTransactionsStore {

impl BlockTransactionsStoreReader for DbBlockTransactionsStore {
fn get(&self, hash: Hash) -> Result<Arc<Vec<Arc<Transaction>>>, StoreError> {
self.cache
.get(&hash)
.map(|block_transactions| block_transactions.0.clone())
.ok_or_else(|| StoreError::BucketNotFound(hash.to_string()));
if self.cache.contains_key(&hash) {
Ok(self.cache.get(&hash).unwrap().0.clone())
if let Some(transaction_body) = self.cache.get(&hash) {
Ok(transaction_body.0.clone())
} else {
Ok(Arc::new(self.access.read_bucket(hash.as_bytes().as_ref())?))
}
Expand All @@ -155,9 +151,9 @@ impl BlockTransactionsStoreReader for DbBlockTransactionsStore {

impl BlockTransactionsStore for DbBlockTransactionsStore {
fn insert(&self, hash: Hash, transactions: Arc<Vec<Arc<Transaction>>>) -> Result<(), StoreError> {
if self.access.has_bucket(hash.as_bytes().as_ref())? {
if self.cache.contains_key(&hash) || self.access.has_bucket(hash.as_bytes().as_ref())? {
return Err(StoreError::HashAlreadyExists(hash));
}
};
self.cache.insert(hash, BlockBody(transactions.clone()));
self.access.write_many(
DirectDbWriter::new(&self.db),
Expand Down
4 changes: 2 additions & 2 deletions consensus/src/pipeline/virtual_processor/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use crate::{
use kaspa_consensus_core::{
acceptance_data::AcceptanceData,
api::args::{TransactionValidationArgs, TransactionValidationBatchArgs},
block::{Block, BlockTemplate, MutableBlock, TemplateBuildMode, TemplateTransactionSelector},
block::{BlockTemplate, TemplateBuildMode, TemplateTransactionSelector},
blockstatus::BlockStatus::{StatusDisqualifiedFromChain, StatusUTXOValid},
coinbase::MinerData,
config::{genesis::GenesisBlock, params::ForkActivation},
Expand Down Expand Up @@ -83,7 +83,7 @@ use super::errors::{PruningImportError, PruningImportResult};
use crossbeam_channel::{Receiver as CrossbeamReceiver, Sender as CrossbeamSender};
use itertools::Itertools;
use kaspa_consensus_core::tx::ValidatedTransaction;
use kaspa_utils::{arc::ArcExtensions, binary_heap::BinaryHeapExtensions};
use kaspa_utils::binary_heap::BinaryHeapExtensions;
use parking_lot::{RwLock, RwLockUpgradableReadGuard};
use rand::{seq::SliceRandom, Rng};
use rayon::{
Expand Down
27 changes: 13 additions & 14 deletions mining/src/manager_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod tests {
use kaspa_addresses::{Address, Prefix, Version};
use kaspa_consensus_core::{
api::ConsensusApi,
block::TemplateBuildMode,
block::{Block, TemplateBuildMode},
coinbase::MinerData,
constants::{MAX_TX_IN_SEQUENCE_NUM, SOMPI_PER_KASPA, TX_VERSION},
errors::tx::TxRuleError,
Expand Down Expand Up @@ -1186,9 +1186,8 @@ mod tests {
consensus: &dyn ConsensusApi,
address_prefix: Prefix,
mining_manager: &MiningManager,
transactions: Vec<Transaction>,
transactions: Vec<Arc<Transaction>>,
) {
let transactions = transactions.into_iter().map(Arc::new).collect::<Vec<_>>();
for _ in 0..4 {
// Run a few times to get more randomness
compare_modified_template_to_built(
Expand Down Expand Up @@ -1278,16 +1277,16 @@ mod tests {
assert!(result.is_ok(), "modify block template failed for miner data 1");
let mut modified_template = result.unwrap();
// Make sure timestamps are equal before comparing the hash
if modified_template.block.header.timestamp != expected_template.block.header.timestamp {
modified_template.block.header.timestamp = expected_template.block.header.timestamp;
modified_template.block.header.finalize();
if modified_template.header.timestamp != expected_template.header.timestamp {
modified_template.header.timestamp = expected_template.header.timestamp;
modified_template.header.finalize();
}

// Compare hashes
let expected_block = expected_template.clone().block.to_immutable();
let modified_block = modified_template.clone().block.to_immutable();
let expected_block = Block::from(expected_template.clone());
let modified_block = Block::from(modified_template.clone());
assert_ne!(
expected_template.block.header.hash, modified_template.block.header.hash,
expected_template.header.hash, modified_template.header.hash,
"built and modified block templates should have different hashes"
);
assert_ne!(expected_block.hash(), modified_block.hash(), "built and modified blocks should have different hashes");
Expand All @@ -1297,15 +1296,15 @@ mod tests {
assert!(result.is_ok(), "modify block template failed for miner data 2");
let mut modified_template_2 = result.unwrap();
// Make sure timestamps are equal before comparing the hash
if modified_template_2.block.header.timestamp != expected_template.block.header.timestamp {
modified_template_2.block.header.timestamp = expected_template.block.header.timestamp;
modified_template_2.block.header.finalize();
if modified_template_2.header.timestamp != expected_template.header.timestamp {
modified_template_2.header.timestamp = expected_template.header.timestamp;
modified_template_2.header.finalize();
}

// Compare hashes
let modified_block = modified_template_2.clone().block.to_immutable();
let modified_block = Block::from(modified_template.clone());
assert_eq!(
expected_template.block.header.hash, modified_template_2.block.header.hash,
expected_template.header.hash, modified_template_2.header.hash,
"built and modified block templates should have same hashes"
);
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion rpc/service/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use kaspa_consensus_core::{
config::Config,
constants::MAX_SOMPI,
network::NetworkType,
tx::{Transaction, COINBASE_TRANSACTION_INDEX},
tx::Transaction,
};
use kaspa_consensus_notify::{
notifier::ConsensusNotifier,
Expand Down
3 changes: 1 addition & 2 deletions simpa/src/simulator/miner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use kaspa_consensus::consensus::Consensus;
use kaspa_consensus::model::stores::virtual_state::VirtualStateStoreReader;
use kaspa_consensus::params::Params;
use kaspa_consensus_core::api::ConsensusApi;
use kaspa_consensus_core::block::{self, Block, TemplateBuildMode, TemplateTransactionSelector};
use kaspa_consensus_core::block::{Block, TemplateBuildMode, TemplateTransactionSelector};
use kaspa_consensus_core::coinbase::MinerData;
use kaspa_consensus_core::mass::{Kip9Version, MassCalculator};
use kaspa_consensus_core::sign::sign;
Expand All @@ -14,7 +14,6 @@ use kaspa_consensus_core::tx::{
};
use kaspa_consensus_core::utxo::utxo_view::UtxoView;
use kaspa_core::trace;
use kaspa_utils::arc::ArcExtensions;
use kaspa_utils::sim::{Environment, Process, Resumption, Suspension};
use rand::rngs::ThreadRng;
use rand::Rng;
Expand Down

0 comments on commit ba8c7f8

Please sign in to comment.