use ccp_randomx::cache::Cache;
use ccp_randomx::Dataset;
use ccp_shared::types::LogicalCoreId;
use super::to_utility_message::ToUtilityInlet;
use super::to_utility_message::ToUtilityMessage;
use super::STResult;
use crate::cu::proving_thread::messages::*;
use crate::hashrate::ThreadHashrateRecord;
use crate::utility_thread::message::ProvingThreadSyncError;
use crate::utility_thread::message::RawProof;
#[derive(Clone, Debug)]
pub(crate) struct ToAsync {
to_async: SyncToAsyncInlet,
}
impl ToAsync {
pub(crate) fn new(to_async: SyncToAsyncInlet) -> Self {
Self { to_async }
}
pub(crate) fn send_cache(&self, cache: Cache) -> STResult<()> {
let to_async_message = CacheCreated::new(cache);
let to_async_message = SyncToAsyncMessage::CacheCreated(to_async_message);
self.to_async
.blocking_send(to_async_message)
.map_err(Into::into)
}
pub(crate) fn send_dataset(&self, dataset: Dataset) -> STResult<()> {
let to_async_message = DatasetAllocated::new(dataset);
let to_async_message = SyncToAsyncMessage::DatasetAllocated(to_async_message);
self.to_async
.blocking_send(to_async_message)
.map_err(Into::into)
}
pub(crate) fn notify_dataset_initialized(&self) -> STResult<()> {
let to_async_message = SyncToAsyncMessage::DatasetInitialized;
self.to_async
.blocking_send(to_async_message)
.map_err(Into::into)
}
pub(crate) fn notify_paused(&self) -> STResult<()> {
let to_async_message = SyncToAsyncMessage::Paused;
self.to_async
.blocking_send(to_async_message)
.map_err(Into::into)
}
}
#[derive(Clone, Debug)]
pub(crate) struct ToUtility {
to_utility: ToUtilityInlet,
}
impl ToUtility {
pub(crate) fn new(to_utility: ToUtilityInlet) -> Self {
Self { to_utility }
}
pub(crate) fn send_proof(&self, core_id: LogicalCoreId, proof: RawProof) -> STResult<()> {
let message = ToUtilityMessage::proof_found(core_id, proof);
self.to_utility.blocking_send(message).map_err(Into::into)
}
pub(crate) fn send_hashrate(&self, hashrate_message: ThreadHashrateRecord) -> STResult<()> {
let to_utility_message = ToUtilityMessage::hashrate(hashrate_message);
self.to_utility
.blocking_send(to_utility_message)
.map_err(Into::into)
}
pub(crate) fn send_error(
&self,
core_id: LogicalCoreId,
error: ProvingThreadSyncError,
) -> STResult<()> {
let to_utility_message = ToUtilityMessage::error_happened(core_id, error);
self.to_utility
.blocking_send(to_utility_message)
.map_err(Into::into)
}
}