Skip to content

Commit

Permalink
review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Jan 16, 2025
1 parent 68939e2 commit 9b5f3eb
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions applications/minotari_node/src/grpc/data_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,71 +38,91 @@ impl DataCache {
}

pub async fn get_randomx_estimated_hash_rate(&self, current_tip: &FixedHash) -> Option<u64> {
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<u64> {
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<NewBlockTemplate> {
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
}
}

pub async fn get_sha3x_new_block_template(&self, current_tip: &FixedHash) -> Option<NewBlockTemplate> {
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<u64>,
pub sha3x_estimated_hash_rate: DataCacheData<u64>,
pub sha3x_new_block_template: DataCacheData<NewBlockTemplate>,
pub randomx_new_block_template: DataCacheData<NewBlockTemplate>,
}
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<T> {
pub data: T,
pub tip: FixedHash,
}

impl<T> DataCacheData<T> {
pub fn new(data: T, tip: FixedHash) -> Self {
Self { data, tip }
}

pub fn new_empty(data: T) -> Self {
Self {
data,
tip: FixedHash::default(),
}
}
}

0 comments on commit 9b5f3eb

Please sign in to comment.