Skip to content

Commit

Permalink
Merge pull request stratum-mining#648 from lorbax/ask_missing_txs
Browse files Browse the repository at this point in the history
Ask missing txs
  • Loading branch information
Fi3 committed Oct 15, 2023
2 parents de4e022 + 72e9449 commit f669fef
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 48 deletions.
122 changes: 85 additions & 37 deletions roles/jd-server/src/lib/job_declarator/message_handler.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::convert::TryInto;
use stratum_common::bitcoin;

use binary_sv2::ShortTxId;
use roles_logic_sv2::{
handlers::{job_declaration::ParseClientJobDeclarationMessages, SendTo_},
job_declaration_sv2::{
AllocateMiningJobToken, AllocateMiningJobTokenSuccess, DeclareMiningJob,
DeclareMiningJobError, DeclareMiningJobSuccess, IdentifyTransactionsSuccess,
ProvideMissingTransactionsSuccess,
ProvideMissingTransactions, ProvideMissingTransactionsSuccess,
},
mining_sv2::{SubmitSharesError, SubmitSharesExtended, SubmitSharesSuccess},
parsers::JobDeclaration,
Expand Down Expand Up @@ -71,34 +72,56 @@ impl ParseClientJobDeclarationMessages for JobDeclaratorDownstream {
let nonce = message.tx_short_hash_nonce;
let mempool = self.mempool.safe_lock(|x| x.clone()).unwrap();

let mut unidentified_txs: Vec<ShortTxId> = Vec::new();
let mut identified_txs: Vec<(
stratum_common::bitcoin::Txid,
stratum_common::bitcoin::Transaction,
)> = Vec::new();
let mut declared_mining_job: Vec<Option<stratum_common::bitcoin::Transaction>> =
Vec::new();
// indicator for when are missing transactions
let mut there_are_some_missing_transactions: bool = false;
//TODO use references insted cloning!!!!
for tx_short_id in short_hash_list {
match mempool.verify_short_id(tx_short_id.clone(), nonce) {
Some(tx_with_id) => identified_txs.push(tx_with_id.clone()),
None => unidentified_txs.push(tx_short_id),
let len_transaction_list = short_hash_list.len();
for i in 0..len_transaction_list {
let tx_short_id = short_hash_list.get(i).unwrap();
match mempool.verify_short_id(tx_short_id, nonce) {
Some(tx_with_id) => declared_mining_job.push(Some(tx_with_id.1.clone())),
None => {
there_are_some_missing_transactions = true;
declared_mining_job.push(None)
}
}
}

// TODO
if !unidentified_txs.is_empty() {}

self.identified_txs = Some(identified_txs);
self.number_of_unidentified_txs = unidentified_txs.len() as u32;
let message_success = DeclareMiningJobSuccess {
request_id: message.request_id,
new_mining_job_token: signed_token(
message.tx_hash_list_hash,
&self.public_key.clone(),
&self.private_key.clone(),
),
};
let message_enum_success = JobDeclaration::DeclareMiningJobSuccess(message_success);
Ok(SendTo::Respond(message_enum_success))
// TODO important check that all the transactions in declare mining job have different
// id and there are no collisions
self.declared_mining_job = declared_mining_job.clone();
self.tx_hash_list_hash = Some(message.clone().tx_hash_list_hash.into_static());
if !there_are_some_missing_transactions {
let message_success = DeclareMiningJobSuccess {
request_id: message.request_id,
new_mining_job_token: signed_token(
message.tx_hash_list_hash.clone(),
&self.public_key.clone(),
&self.private_key.clone(),
),
};
let message_enum_success = JobDeclaration::DeclareMiningJobSuccess(message_success);
Ok(SendTo::Respond(message_enum_success))
} else {
let mut indexes_of_missing_transactions: Vec<u16> = Vec::new();
for i in 0..len_transaction_list {

Check failure on line 109 in roles/jd-server/src/lib/job_declarator/message_handler.rs

View workflow job for this annotation

GitHub Actions / clippy-check (macos-latest)

the loop variable `i` is used to index `declared_mining_job`
match declared_mining_job[i] {
Some(_) => continue,
None => indexes_of_missing_transactions.push(i.try_into().unwrap()),
}
}
let message_provide_missing_transactions = ProvideMissingTransactions {
request_id: message.request_id,
unknown_tx_position_list: indexes_of_missing_transactions.try_into().unwrap(),
};
let message_enum_provide_missing_transactions =
JobDeclaration::ProvideMissingTransactions(
message_provide_missing_transactions,
);
Ok(SendTo_::Respond(message_enum_provide_missing_transactions))
}
} else {
let message_error = DeclareMiningJobError {
request_id: message.request_id,
Expand All @@ -114,24 +137,49 @@ impl ParseClientJobDeclarationMessages for JobDeclaratorDownstream {
&mut self,
message: IdentifyTransactionsSuccess,
) -> Result<SendTo, Error> {
let message_success = IdentifyTransactionsSuccess {
request_id: message.request_id,
tx_data_hashes: Vec::new().try_into().unwrap(),
};
let message_enum = JobDeclaration::IdentifyTransactionsSuccess(message_success);
Ok(SendTo::Respond(message_enum))
drop(message);
Ok(SendTo::None(None))
}

fn handle_provide_missing_transactions_success(
&mut self,
message: ProvideMissingTransactionsSuccess,
) -> Result<SendTo, Error> {
let message_success = ProvideMissingTransactionsSuccess {
request_id: message.request_id,
transaction_list: Vec::new().try_into().unwrap(),
};
let message_enum = JobDeclaration::ProvideMissingTransactionsSuccess(message_success);
Ok(SendTo::Respond(message_enum))
let mut transactions: Vec<bitcoin::Transaction> = Vec::new();
//for transaction_undecoded in message.transaction_list.to_vec() {
// // TODO decode transactions and push them into transaction varuiable
// todo!()
//}

for declared_transaction in self.declared_mining_job.iter_mut() {
match declared_transaction {
Some(_) => {}
None => *declared_transaction = transactions.pop(),
}
}
let mut still_misses_some_transaction: bool = false;
for declared_transaction in &self.declared_mining_job {
match declared_transaction {
Some(_) => {}
None => still_misses_some_transaction = true,
}
}
if still_misses_some_transaction {
// why there are still some missing transactions? here should send some relevant error
todo!()
} else {
let tx_hash_list_hash = self.tx_hash_list_hash.clone().unwrap().into_static();
let message_success = DeclareMiningJobSuccess {
request_id: message.request_id,
new_mining_job_token: signed_token(
tx_hash_list_hash,
&self.public_key.clone(),
&self.private_key.clone(),
),
};
let message_enum_success = JobDeclaration::DeclareMiningJobSuccess(message_success);
Ok(SendTo::Respond(message_enum_success))
}
}

fn handle_submit_shares_extended(
Expand Down
14 changes: 5 additions & 9 deletions roles/jd-server/src/lib/job_declarator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ pub struct JobDeclaratorDownstream {
public_key: Secp256k1PublicKey,
private_key: Secp256k1SecretKey,
mempool: Arc<Mutex<JDsMempool>>,
identified_txs: Option<
Vec<(
stratum_common::bitcoin::Txid,
stratum_common::bitcoin::Transaction,
)>,
>,
number_of_unidentified_txs: u32,
declared_mining_job: Vec<Option<stratum_common::bitcoin::Transaction>>,
tx_hash_list_hash: Option<U256<'static>>,
}

impl JobDeclaratorDownstream {
Expand All @@ -55,6 +50,7 @@ impl JobDeclaratorDownstream {
// TODO: use next variables
let token_to_job_map = HashMap::with_hasher(BuildNoHashHasher::default());
let tokens = Id::new();
let declared_mining_job = Vec::new();
crate::get_coinbase_output(config).expect("Invalid coinbase output in config")[0]
.consensus_encode(&mut coinbase_output)
.expect("Invalid coinbase output in config");
Expand All @@ -68,8 +64,8 @@ impl JobDeclaratorDownstream {
public_key: config.authority_public_key.clone(),
private_key: config.authority_secret_key.clone(),
mempool,
identified_txs: None,
number_of_unidentified_txs: 0,
declared_mining_job,
tx_hash_list_hash: None,
}
}

Expand Down
4 changes: 2 additions & 2 deletions roles/jd-server/src/lib/mempool/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ impl JDsMempool {

pub fn verify_short_id(
&self,
tx_short_id: ShortTxId<'_>,
tx_short_id: &ShortTxId<'_>,
nonce: u64,
) -> Option<(bitcoin::Txid, bitcoin::Transaction)> {
let mempool: Vec<TransacrtionWithHash> = self.clone().mempool;
for tx_with_hash in mempool {
let btc_txid = tx_with_hash.id;
if roles_logic_sv2::utils::get_short_hash(btc_txid, nonce) == tx_short_id {
if &roles_logic_sv2::utils::get_short_hash(btc_txid, nonce) == tx_short_id {
return Some((btc_txid, tx_with_hash.tx));
} else {
continue;
Expand Down

0 comments on commit f669fef

Please sign in to comment.