use std::sync::Arc;
use minotari_app_grpc::tari_rpc;
use tari_common_types::types::FixedHash;
use tari_node_components::blocks::NewBlockTemplate;
use tokio::sync::RwLock;
pub struct DataCache {
inner_data_cache: Arc<RwLock<InnerDataCache>>,
}
impl DataCache {
pub fn new() -> Self {
Self {
inner_data_cache: Arc::new(RwLock::new(InnerDataCache::default())),
}
}
pub async fn get_monero_randomx_estimated_hash_rate(&self, current_tip: &FixedHash) -> Option<u64> {
let res = &self.inner_data_cache.read().await.monero_randomx_estimated_hash_rate;
if res.tip == *current_tip { Some(res.data) } else { None }
}
pub async fn get_tari_randomx_estimated_hash_rate(&self, current_tip: &FixedHash) -> Option<u64> {
let res = &self.inner_data_cache.read().await.tari_randomx_estimated_hash_rate;
if res.tip == *current_tip { Some(res.data) } else { None }
}
pub async fn get_cuckaroo_estimated_hash_rate(&self, current_tip: &FixedHash) -> Option<tari_rpc::UDecimalValue> {
let res = &self.inner_data_cache.read().await.cuckaroo_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.tip == *current_tip { Some(res.data) } else { None }
}
pub async fn set_monero_randomx_estimated_hash_rate(&self, hash_rate: u64, current_tip: FixedHash) {
self.inner_data_cache.write().await.monero_randomx_estimated_hash_rate =
DataCacheData::new(hash_rate, current_tip);
}
pub async fn set_tari_randomx_estimated_hash_rate(&self, hash_rate: u64, current_tip: FixedHash) {
self.inner_data_cache.write().await.tari_randomx_estimated_hash_rate =
DataCacheData::new(hash_rate, current_tip);
}
pub async fn set_cuckaroo_estimated_hash_rate(&self, hash_rate: tari_rpc::UDecimalValue, current_tip: FixedHash) {
self.inner_data_cache.write().await.cuckaroo_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 = DataCacheData::new(hash_rate, current_tip);
}
pub async fn get_monero_randomx_new_block_template(&self, current_tip: &FixedHash) -> Option<NewBlockTemplate> {
let res = &self.inner_data_cache.read().await.monero_randomx_new_block_template;
if res.tip == *current_tip {
Some(res.data.clone())
} else {
None
}
}
pub async fn get_tari_randomx_new_block_template(&self, current_tip: &FixedHash) -> Option<NewBlockTemplate> {
let res = &self.inner_data_cache.read().await.tari_randomx_new_block_template;
if res.tip == *current_tip {
Some(res.data.clone())
} else {
None
}
}
pub async fn get_cuckaroo_new_block_template(&self, current_tip: &FixedHash) -> Option<NewBlockTemplate> {
let res = &self.inner_data_cache.read().await.cuckaroo_new_block_template;
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.tip == *current_tip {
Some(res.data.clone())
} else {
None
}
}
pub async fn set_monero_randomx_new_block_template(
&self,
new_block_template: NewBlockTemplate,
current_tip: FixedHash,
) {
self.inner_data_cache.write().await.monero_randomx_new_block_template =
DataCacheData::new(new_block_template, current_tip);
}
pub async fn set_tari_randomx_new_block_template(
&self,
new_block_template: NewBlockTemplate,
current_tip: FixedHash,
) {
self.inner_data_cache.write().await.tari_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 =
DataCacheData::new(new_block_template, current_tip);
}
pub async fn set_cuckaroo_new_block_template(&self, new_block_template: NewBlockTemplate, current_tip: FixedHash) {
self.inner_data_cache.write().await.cuckaroo_new_block_template =
DataCacheData::new(new_block_template, current_tip);
}
}
struct InnerDataCache {
pub monero_randomx_estimated_hash_rate: DataCacheData<u64>,
pub tari_randomx_estimated_hash_rate: DataCacheData<u64>,
pub sha3x_estimated_hash_rate: DataCacheData<u64>,
pub cuckaroo_estimated_hash_rate: DataCacheData<tari_rpc::UDecimalValue>,
pub sha3x_new_block_template: DataCacheData<NewBlockTemplate>,
pub cuckaroo_new_block_template: DataCacheData<NewBlockTemplate>,
pub monero_randomx_new_block_template: DataCacheData<NewBlockTemplate>,
pub tari_randomx_new_block_template: DataCacheData<NewBlockTemplate>,
}
impl Default for InnerDataCache {
fn default() -> Self {
Self {
monero_randomx_estimated_hash_rate: DataCacheData::new_empty(0),
tari_randomx_estimated_hash_rate: DataCacheData::new_empty(0),
sha3x_estimated_hash_rate: DataCacheData::new_empty(0),
cuckaroo_estimated_hash_rate: DataCacheData::new_empty(tari_rpc::UDecimalValue { units: 0, nanos: 0 }),
sha3x_new_block_template: DataCacheData::new_empty(NewBlockTemplate::empty()),
monero_randomx_new_block_template: DataCacheData::new_empty(NewBlockTemplate::empty()),
cuckaroo_new_block_template: DataCacheData::new_empty(NewBlockTemplate::empty()),
tari_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(),
}
}
}