Skip to content

Commit

Permalink
handle new Declare mining job mempool updation
Browse files Browse the repository at this point in the history
  • Loading branch information
Shourya742 committed Nov 12, 2024
1 parent c3cb83d commit 5d0462d
Showing 1 changed file with 74 additions and 2 deletions.
76 changes: 74 additions & 2 deletions roles/jd-server/src/lib/job_declarator/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ use roles_logic_sv2::{
ProvideMissingTransactions, ProvideMissingTransactionsSuccess, SubmitSolutionJd,
},
parsers::JobDeclaration,
utils::Mutex,
};
use std::{convert::TryInto, io::Cursor};
use std::{convert::TryInto, io::Cursor, sync::Arc};
use stratum_common::bitcoin::{Transaction, Txid};
pub type SendTo = SendTo_<JobDeclaration<'static>, ()>;
use crate::mempool::JDsMempool;

use super::{signed_token, TransactionState};
use roles_logic_sv2::{errors::Error, parsers::PoolMessages as AllMessages};
use stratum_common::bitcoin::consensus::Decodable;
use tracing::info;
use tracing::{debug, info};

use super::JobDeclaratorDownstream;

Expand Down Expand Up @@ -61,6 +64,10 @@ impl ParseClientJobDeclarationMessages for JobDeclaratorDownstream {
}

fn handle_declare_mining_job(&mut self, message: DeclareMiningJob) -> Result<SendTo, Error> {
if let Some(old_mining_job) = self.declared_mining_job.0.take() {
clear_declared_mining_job(old_mining_job, &message, self.mempool.clone())?;

Check warning on line 68 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L67-L68

Added lines #L67 - L68 were not covered by tests
}

// the transactions that are present in the mempool are stored here, that is sent to the
// mempool which use the rpc client to retrieve the whole data for each transaction.
// The unknown transactions is a vector that contains the transactions that are not in the
Expand Down Expand Up @@ -110,6 +117,8 @@ impl ParseClientJobDeclarationMessages for JobDeclaratorDownstream {
.known_transactions
.append(&mut known_transactions);

dbg!(missing_txs.len());

Check warning on line 120 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L120

Added line #L120 was not covered by tests

if missing_txs.is_empty() {
let message_success = DeclareMiningJobSuccess {
request_id: message.request_id,
Expand Down Expand Up @@ -219,3 +228,66 @@ impl ParseClientJobDeclarationMessages for JobDeclaratorDownstream {
Ok(SendTo::None(Some(m)))
}
}

fn clear_declared_mining_job(

Check warning on line 232 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L232

Added line #L232 was not covered by tests
old_mining_job: DeclareMiningJob,
new_mining_job: &DeclareMiningJob,
mempool: Arc<Mutex<JDsMempool>>,
) -> Result<(), Error> {
let old_transactions = old_mining_job.tx_short_hash_list.inner_as_ref();
let new_transactions = new_mining_job.tx_short_hash_list.inner_as_ref();

if old_transactions.is_empty() {
info!("No transactions to remove from mempool");
return Ok(());

Check warning on line 242 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L240-L242

Added lines #L240 - L242 were not covered by tests
}

let nonce = old_mining_job.tx_short_hash_nonce;

Check warning on line 245 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L245

Added line #L245 was not covered by tests

let result = mempool
.safe_lock(|mempool_| -> Result<(), Error> {
let short_ids_map = mempool_
.to_short_ids(nonce)
.ok_or(Error::JDSMissingTransactions)?;

Check warning on line 251 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L247-L251

Added lines #L247 - L251 were not covered by tests

for short_id in old_transactions

Check warning on line 253 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L253

Added line #L253 was not covered by tests
.iter()
.filter(|&id| !new_transactions.contains(id))

Check warning on line 255 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L255

Added line #L255 was not covered by tests
{
if let Some(transaction_with_hash) = short_ids_map.get(*short_id) {
let txid = transaction_with_hash.id;
match mempool_.mempool.get_mut(&txid) {

Check warning on line 259 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L257-L259

Added lines #L257 - L259 were not covered by tests
Some(Some((_transaction, counter))) => {
if *counter > 1 {
*counter -= 1;
debug!(

Check warning on line 263 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L261-L263

Added lines #L261 - L263 were not covered by tests
"Fat transaction {:?} counter decremented; job id {:?} dropped",
txid, old_mining_job.request_id
);
} else {
mempool_.mempool.insert(txid, None);
debug!(

Check warning on line 269 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L268-L269

Added lines #L268 - L269 were not covered by tests
"Fat transaction {:?} with job id {:?} removed from mempool",
txid, old_mining_job.request_id
);
}
}
Some(None) => debug!(

Check warning on line 275 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L275

Added line #L275 was not covered by tests
"Thin transaction {:?} with job id {:?} removed from mempool",
txid, old_mining_job.request_id
),
None => {}
}
} else {
debug!(

Check warning on line 282 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L282

Added line #L282 was not covered by tests
"Transaction with short id {:?} not found in mempool for old jobs",
short_id
);
}
}
Ok(())

Check warning on line 288 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L288

Added line #L288 was not covered by tests
})
.map_err(|e| Error::PoisonLock(e.to_string()))?;

Check warning on line 290 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L290

Added line #L290 was not covered by tests

result.map_err(|err| Error::PoisonLock(err.to_string()))

Check warning on line 292 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View check run for this annotation

Codecov / codecov/patch

roles/jd-server/src/lib/job_declarator/message_handler.rs#L292

Added line #L292 was not covered by tests
}

0 comments on commit 5d0462d

Please sign in to comment.