use super::{HashMap, HashSet};
use crate::client::error::GroupChannelError;
use mining_sv2::{NewExtendedMiningJob, SetNewPrevHash as SetNewPrevHashMp};
#[derive(Debug, Clone)]
pub struct GroupChannel<'a> {
group_channel_id: u32,
standard_channel_ids: HashSet<u32>,
future_jobs: HashMap<u32, NewExtendedMiningJob<'a>>,
active_job: Option<NewExtendedMiningJob<'a>>,
}
impl<'a> GroupChannel<'a> {
pub fn new(group_channel_id: u32) -> Self {
Self {
group_channel_id,
standard_channel_ids: HashSet::new(),
future_jobs: HashMap::new(),
active_job: None,
}
}
pub fn add_standard_channel_id(&mut self, standard_channel_id: u32) {
self.standard_channel_ids.insert(standard_channel_id);
}
pub fn remove_standard_channel_id(&mut self, standard_channel_id: u32) {
self.standard_channel_ids.remove(&standard_channel_id);
}
pub fn get_group_channel_id(&self) -> u32 {
self.group_channel_id
}
pub fn get_standard_channel_ids(&self) -> &HashSet<u32> {
&self.standard_channel_ids
}
pub fn get_active_job(&self) -> Option<&NewExtendedMiningJob<'a>> {
self.active_job.as_ref()
}
pub fn get_future_jobs(&self) -> &HashMap<u32, NewExtendedMiningJob<'a>> {
&self.future_jobs
}
pub fn on_new_extended_mining_job(
&mut self,
new_extended_mining_job: NewExtendedMiningJob<'a>,
) {
match new_extended_mining_job.min_ntime.clone().into_inner() {
Some(_min_ntime) => {
self.active_job = Some(new_extended_mining_job);
}
None => {
self.future_jobs
.insert(new_extended_mining_job.job_id, new_extended_mining_job);
}
}
}
pub fn on_set_new_prev_hash(
&mut self,
set_new_prev_hash: SetNewPrevHashMp<'a>,
) -> Result<(), GroupChannelError> {
match self.future_jobs.remove(&set_new_prev_hash.job_id) {
Some(job) => {
self.active_job = Some(job);
}
None => return Err(GroupChannelError::JobIdNotFound),
}
self.future_jobs.clear();
Ok(())
}
}