diff --git a/applications/minotari_node/src/grpc/data_cache.rs b/applications/minotari_node/src/grpc/data_cache.rs index 2a898472b8..4c4c79680a 100644 --- a/applications/minotari_node/src/grpc/data_cache.rs +++ b/applications/minotari_node/src/grpc/data_cache.rs @@ -38,35 +38,35 @@ impl DataCache { } pub async fn get_randomx_estimated_hash_rate(&self, current_tip: &FixedHash) -> Option { - let res = self.inner_data_cache.read().await.randomx_estimated_hash_rate; - if res.1 == *current_tip { - Some(res.0) + let res = &self.inner_data_cache.read().await.randomx_estimated_hash_rate; + if res.tip == *current_tip { + Some(res.data) } else { None } } pub async fn get_sha3x_estimated_hash_rate(&self, current_tip: &FixedHash) -> Option { - let res = self.inner_data_cache.read().await.sha3x_estimated_hash_rate; - if res.1 == *current_tip { - Some(res.0) + let res = &self.inner_data_cache.read().await.sha3x_estimated_hash_rate; + if res.tip == *current_tip { + Some(res.data) } else { None } } pub async fn set_randomx_estimated_hash_rate(&self, hash_rate: u64, current_tip: FixedHash) { - self.inner_data_cache.write().await.randomx_estimated_hash_rate = (hash_rate, current_tip); + self.inner_data_cache.write().await.randomx_estimated_hash_rate = DataCacheData::new(hash_rate, current_tip); } pub async fn set_sha3x_estimated_hash_rate(&self, hash_rate: u64, current_tip: FixedHash) { - self.inner_data_cache.write().await.sha3x_estimated_hash_rate = (hash_rate, current_tip); + self.inner_data_cache.write().await.sha3x_estimated_hash_rate = DataCacheData::new(hash_rate, current_tip); } pub async fn get_randomx_new_block_template(&self, current_tip: &FixedHash) -> Option { let res = &self.inner_data_cache.read().await.randomx_new_block_template; - if res.1 == *current_tip { - Some(res.0.clone()) + if res.tip == *current_tip { + Some(res.data.clone()) } else { None } @@ -74,35 +74,55 @@ impl DataCache { pub async fn get_sha3x_new_block_template(&self, current_tip: &FixedHash) -> Option { let res = &self.inner_data_cache.read().await.sha3x_new_block_template; - if res.1 == *current_tip { - Some(res.0.clone()) + if res.tip == *current_tip { + Some(res.data.clone()) } else { None } } pub async fn set_randomx_new_block_template(&self, new_block_template: NewBlockTemplate, current_tip: FixedHash) { - self.inner_data_cache.write().await.randomx_new_block_template = (new_block_template, current_tip); + self.inner_data_cache.write().await.randomx_new_block_template = + DataCacheData::new(new_block_template, current_tip); } pub async fn set_sha3x_new_block_template(&self, new_block_template: NewBlockTemplate, current_tip: FixedHash) { - self.inner_data_cache.write().await.sha3x_new_block_template = (new_block_template, current_tip); + self.inner_data_cache.write().await.sha3x_new_block_template = + DataCacheData::new(new_block_template, current_tip); } } struct InnerDataCache { - pub randomx_estimated_hash_rate: (u64, FixedHash), - pub sha3x_estimated_hash_rate: (u64, FixedHash), - pub sha3x_new_block_template: (NewBlockTemplate, FixedHash), - pub randomx_new_block_template: (NewBlockTemplate, FixedHash), + pub randomx_estimated_hash_rate: DataCacheData, + pub sha3x_estimated_hash_rate: DataCacheData, + pub sha3x_new_block_template: DataCacheData, + pub randomx_new_block_template: DataCacheData, } impl Default for InnerDataCache { fn default() -> Self { Self { - randomx_estimated_hash_rate: (0, FixedHash::default()), - sha3x_estimated_hash_rate: (0, FixedHash::default()), - sha3x_new_block_template: (NewBlockTemplate::empty(), FixedHash::default()), - randomx_new_block_template: (NewBlockTemplate::empty(), FixedHash::default()), + randomx_estimated_hash_rate: DataCacheData::new_empty(0), + sha3x_estimated_hash_rate: DataCacheData::new_empty(0), + sha3x_new_block_template: DataCacheData::new_empty(NewBlockTemplate::empty()), + randomx_new_block_template: DataCacheData::new_empty(NewBlockTemplate::empty()), + } + } +} + +struct DataCacheData { + pub data: T, + pub tip: FixedHash, +} + +impl DataCacheData { + pub fn new(data: T, tip: FixedHash) -> Self { + Self { data, tip } + } + + pub fn new_empty(data: T) -> Self { + Self { + data, + tip: FixedHash::default(), } } }