Skip to content

Commit

Permalink
hashmap introduction for last_declare_mining_jobs_sent
Browse files Browse the repository at this point in the history
  • Loading branch information
GitGab19 committed May 10, 2024
1 parent 5e48904 commit 43c8877
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
4 changes: 3 additions & 1 deletion roles/jd-client/src/lib/job_declarator/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ impl ParseServerJobDeclarationMessages for JobDeclarator {
message: ProvideMissingTransactions,
) -> Result<SendTo, Error> {
let tx_list = self
.last_declare_mining_job_sent
.last_declare_mining_jobs_sent
.get(&message.request_id)
.unwrap()
.clone()
.unwrap()
.tx_list
Expand Down
20 changes: 11 additions & 9 deletions roles/jd-client/src/lib/job_declarator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub struct JobDeclarator {
req_ids: Id,
min_extranonce_size: u16,
// (Sent DeclareMiningJob, is future, template id, merkle path)
last_declare_mining_job_sent: Option<LastDeclareJob>,
last_declare_mining_jobs_sent: HashMap<u32, Option<LastDeclareJob>>,
last_set_new_prev_hash: Option<SetNewPrevHash<'static>>,
#[allow(clippy::type_complexity)]
future_jobs: HashMap<
Expand Down Expand Up @@ -114,7 +114,7 @@ impl JobDeclarator {
allocated_tokens: vec![],
req_ids: Id::new(),
min_extranonce_size,
last_declare_mining_job_sent: None,
last_declare_mining_jobs_sent: HashMap::with_capacity(10),
last_set_new_prev_hash: None,
future_jobs: HashMap::with_hasher(BuildNoHashHasher::default()),
up,
Expand All @@ -128,20 +128,22 @@ impl JobDeclarator {
Ok(self_)
}

fn get_last_declare_job_sent(self_mutex: &Arc<Mutex<Self>>) -> LastDeclareJob {
fn get_last_declare_job_sent(self_mutex: &Arc<Mutex<Self>>, request_id: u32) -> LastDeclareJob {
self_mutex
.safe_lock(|s| {
s.last_declare_mining_job_sent
s.last_declare_mining_jobs_sent
.get(&request_id)
.expect("LastDeclareJob not found")
.clone()
.expect("unreachable code")
})
.unwrap()
}

fn update_last_declare_job_sent(self_mutex: &Arc<Mutex<Self>>, j: LastDeclareJob) {
fn update_last_declare_job_sent(self_mutex: &Arc<Mutex<Self>>, request_id: u32, j: LastDeclareJob) {
self_mutex
.safe_lock(|s| s.last_declare_mining_job_sent = Some(j))
.unwrap()
.safe_lock(|s| s.last_declare_mining_jobs_sent.insert(request_id, Some(j)))
.unwrap();
}

#[async_recursion]
Expand Down Expand Up @@ -245,7 +247,7 @@ impl JobDeclarator {
coinbase_pool_output,
tx_list: tx_list_.clone(),
};
Self::update_last_declare_job_sent(self_mutex, last_declare);
Self::update_last_declare_job_sent(self_mutex, id, last_declare);
let frame: StdFrame =
PoolMessages::JobDeclaration(JobDeclaration::DeclareMiningJob(declare_job))
.try_into()
Expand All @@ -272,7 +274,7 @@ impl JobDeclarator {
match next_message_to_send {
Ok(SendTo::None(Some(JobDeclaration::DeclareMiningJobSuccess(m)))) => {
let new_token = m.new_mining_job_token;
let last_declare = Self::get_last_declare_job_sent(&self_mutex);
let last_declare = Self::get_last_declare_job_sent(&self_mutex, m.request_id);
let mut last_declare_mining_job_sent = last_declare.declare_job;
let is_future = last_declare.template.future_template;
let id = last_declare.template.template_id;
Expand Down

0 comments on commit 43c8877

Please sign in to comment.