From 80b645d8e9d0e6213d66946d2f71f48492507718 Mon Sep 17 00:00:00 2001 From: stringhandler Date: Tue, 25 Feb 2025 11:15:23 +0200 Subject: [PATCH] fix tests --- .../src/block_template_data.rs | 65 ++----------------- 1 file changed, 5 insertions(+), 60 deletions(-) diff --git a/applications/minotari_merge_mining_proxy/src/block_template_data.rs b/applications/minotari_merge_mining_proxy/src/block_template_data.rs index c5afd3a479..88b5b812e4 100644 --- a/applications/minotari_merge_mining_proxy/src/block_template_data.rs +++ b/applications/minotari_merge_mining_proxy/src/block_template_data.rs @@ -42,14 +42,13 @@ const LOG_TARGET: &str = "minotari_mm_proxy::xmrig"; /// Structure for holding hashmap of hashes -> [BlockRepositoryItem] and [TemplateRepositoryItem]. #[derive(Debug, Clone)] -pub struct BlockTemplateRepository { +pub(crate) struct BlockTemplateRepository { blocks: Arc, BlockRepositoryItem>>>, - templates: Arc, TemplateRepositoryItem>>>, } /// Structure holding [NewBlockTemplate] along with a timestamp. #[derive(Debug, Clone)] -pub struct TemplateRepositoryItem { +pub(crate) struct TemplateRepositoryItem { pub new_block_template: NewBlockTemplateData, pub template_with_coinbase: grpc::NewBlockTemplate, datetime: DateTime, @@ -97,7 +96,6 @@ impl BlockTemplateRepository { pub fn new() -> Self { Self { blocks: Arc::new(RwLock::new(HashMap::new())), - templates: Arc::new(RwLock::new(HashMap::new())), } } @@ -107,16 +105,6 @@ impl BlockTemplateRepository { b.get(merge_mining_hash.as_ref()).map(|item| item.data.clone()) } - /// Return [BlockTemplateData] with the associated hash. None if the hash is not stored. - pub async fn get_new_template>( - &self, - best_block_hash: T, - ) -> Option<(NewBlockTemplateData, grpc::NewBlockTemplate)> { - let b = self.templates.read().await; - b.get(best_block_hash.as_ref()) - .map(|item| (item.new_block_template.clone(), item.template_with_coinbase.clone())) - } - /// Store [FinalBlockTemplateData] at the hash value if the key does not exist. pub async fn save_final_block_template_if_key_unique(&self, block_template: FinalBlockTemplateData) { let merge_mining_hash = block_template.aux_chain_mr.to_vec(); @@ -125,24 +113,6 @@ impl BlockTemplateRepository { .or_insert_with(|| BlockRepositoryItem::new(block_template)); } - /// Store [NewBlockTemplate] at the hash value if the key does not exist. - pub async fn save_new_block_template_if_key_unique( - &self, - best_block_hash: Vec, - new_block_template: NewBlockTemplateData, - template_with_coinbase: grpc::NewBlockTemplate, - ) { - let mut b = self.templates.write().await; - b.entry(best_block_hash.clone()).or_insert_with(|| { - trace!( - target: LOG_TARGET, - "Saving new block template for best block hash: {:?}", - hex::encode(&best_block_hash) - ); - TemplateRepositoryItem::new(new_block_template, template_with_coinbase) - }); - } - /// Check if the repository contains a block template with best_previous_block_hash pub async fn blocks_contains(&self, current_best_block_hash: FixedHash) -> Option { let b = self.blocks.read().await; @@ -163,13 +133,6 @@ impl BlockTemplateRepository { #[cfg(not(test))] let threshold = Utc::now() - Duration::minutes(20); *b = b.drain().filter(|(_, i)| i.datetime() >= threshold).collect(); - trace!(target: LOG_TARGET, "Removing outdated new block templates"); - let mut b = self.templates.write().await; - #[cfg(test)] - let threshold = Utc::now(); - #[cfg(not(test))] - let threshold = Utc::now() - Duration::minutes(20); - *b = b.drain().filter(|(_, i)| i.datetime() >= threshold).collect(); } /// Remove a particularfinla block template for hash and return the associated [BlockRepositoryItem] if any. @@ -182,17 +145,6 @@ impl BlockTemplateRepository { let mut b = self.blocks.write().await; b.remove(hash.as_ref()) } - - /// Remove a particular new block template for hash and return the associated [BlockRepositoryItem] if any. - pub async fn remove_new_block_template>(&self, hash: T) -> Option { - trace!( - target: LOG_TARGET, - "New block template removed with best block hash {:?}", - hex::encode(hash.as_ref()) - ); - let mut b = self.templates.write().await; - b.remove(hash.as_ref()) - } } /// Setup values for the new block. @@ -349,24 +301,17 @@ pub mod test { #[tokio::test] async fn test_block_template_repository() { let btr = BlockTemplateRepository::new(); - let hash1 = vec![1; 32]; - let hash2 = vec![2; 32]; - let hash3 = vec![3; 32]; let block_template = create_block_template_data(); + let hash1 = block_template.aux_chain_mr.to_vec(); btr.save_final_block_template_if_key_unique(block_template.clone()) .await; - btr.save_final_block_template_if_key_unique(block_template).await; assert!(btr.get_final_template(hash1.clone()).await.is_some()); - assert!(btr.get_final_template(hash2.clone()).await.is_some()); - assert!(btr.get_final_template(hash3.clone()).await.is_none()); assert!(btr.remove_final_block_template(hash1.clone()).await.is_some()); assert!(btr.get_final_template(hash1.clone()).await.is_none()); - assert!(btr.get_final_template(hash2.clone()).await.is_some()); - assert!(btr.get_final_template(hash3.clone()).await.is_none()); + btr.save_final_block_template_if_key_unique(block_template).await; + assert!(btr.get_final_template(hash1.clone()).await.is_some()); btr.remove_outdated().await; assert!(btr.get_final_template(hash1).await.is_none()); - assert!(btr.get_final_template(hash2).await.is_none()); - assert!(btr.get_final_template(hash3).await.is_none()); } #[test]