Skip to content

Commit

Permalink
Closes Issue stratum-mining#840
Browse files Browse the repository at this point in the history
Modify the update_mempool function to update the jds mempool to save transactions declared by the downstream and get rid of others.
Create a clear_declared_mining_job to remove old fat transactions before a new mining job is declared.
Manage every unwrap by returning the proper error class and error message
  • Loading branch information
NonsoAmadi10 committed Jul 17, 2024
1 parent 04dee88 commit 1ef967b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
42 changes: 32 additions & 10 deletions roles/jd-server/src/lib/job_declarator/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,18 +52,40 @@ pub fn clear_declared_mining_job(
info!("No transactions to remove from mempool");
return Ok(());
}
let clear_transactions = |jds_mempool: &mut JDsMempool| {
for txid in transactions_to_remove {
match jds_mempool.mempool.remove(txid) {
Some(_) => info!("Transaction {:?} removed from mempool", txid),
None => debug!("Transaction {:?} not found in mempool", txid),
};

let nonce = mining_job.tx_short_hash_nonce;

for short_id in transactions_to_remove {
let result = mempool.safe_lock(|mempool_| -> Result<(), Error> {
// Try to manage this unwrap, we use .ok_or() method to return the proper error
let short_ids_map = mempool_
.to_short_ids(nonce)
.ok_or(Error::JDSMissingTransactions)?;
let transaction_with_hash = short_ids_map
.get(short_id);

match transaction_with_hash {
Some(transaction_with_hash) => {
let txid = transaction_with_hash.id;
match mempool_.mempool.remove(&txid) {
Some(transaction) => {
debug!("Fat transaction {:?} in job with request id {:?} removed from mempool", transaction, mining_job.request_id);
info!("Fat transaction {:?} in job with request id {:?} removed from mempool", txid, mining_job.request_id);
},
None => info!("Thin transaction {:?} in job with request id {:?} removed from mempool", txid, mining_job.request_id),
}
},
None => debug!("Transaction with short id {:?} not found in mempool while clearing old jobs", short_id),
}
Ok(()) // Explicitly return Ok(()) inside the closure for proper flow control
});

// Propagate any error from the closure
if let Err(err) = result {
return Err(Error::PoisonLock(err.to_string()));
}
};
match mempool.safe_lock(clear_transactions) {
Ok(_) => Ok(()),
Err(e) => Err(Error::PoisonLock(e.to_string())),
}
Ok(())
}

impl ParseClientJobDeclarationMessages for JobDeclaratorDownstream {
Expand Down
15 changes: 8 additions & 7 deletions roles/jd-server/src/lib/mempool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,13 @@ impl JDsMempool {
.safe_lock(|x| x.get_client())?
.ok_or(JdsMempoolError::NoClient)?;
let node_mempool: Vec<String> = client.get_raw_mempool().await?;
let node_mempool_deserialized: Result<Vec<Txid>, JdsMempoolError> = node_mempool
.iter()
.map(|id| Txid::from_str(id).map_err(|err| JdsMempoolError::Rpc(RpcError::Deserialization(err.to_string()))))
.collect();
let node_mempool_deserialized = node_mempool_deserialized?;

let mut node_mempool_deserialized: Vec<Txid> = vec![];
for id in &node_mempool {
let key_id = Txid::from_str(id)
.map_err(|err| JdsMempoolError::Rpc(RpcError::Deserialization(err.to_string())))?;
node_mempool_deserialized.push(key_id);
}

self_.safe_lock(|x| {
let jds_mempool = &mut x.mempool;
// the fat transactions in the jds-mempool are those declared by some downstream and we
Expand All @@ -113,7 +114,7 @@ impl JDsMempool {
// here we add all the new transactions
for id in &node_mempool_deserialized {
jds_mempool.entry(*id).or_insert(None);
};
}
if jds_mempool.is_empty() {
Err(JdsMempoolError::EmptyMempool)
} else {
Expand Down

0 comments on commit 1ef967b

Please sign in to comment.