Skip to content

Commit

Permalink
Merge pull request stratum-mining#1035 from Shourya742/feature/asyncF…
Browse files Browse the repository at this point in the history
…lagSet

Enhance SetupConnection Handling and Implement Job declaration Flag Checks
  • Loading branch information
plebhash authored Jul 29, 2024
2 parents 5583b3d + 51a20d8 commit dcaa92d
Show file tree
Hide file tree
Showing 17 changed files with 587 additions and 69 deletions.
65 changes: 56 additions & 9 deletions protocols/v2/subprotocols/common-messages/src/setup_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ pub struct SetupConnection<'decoder> {

impl<'decoder> SetupConnection<'decoder> {
pub fn set_requires_standard_job(&mut self) {
self.flags |= 0b_0000_0000_0000_0000_0000_0000_0000_0001
self.flags |= 0b_0000_0000_0000_0000_0000_0000_0000_0001;
}

pub fn set_async_job_nogotiation(&mut self) {
self.flags |= 0b_0000_0000_0000_0000_0000_0000_0000_0001
self.flags |= 0b_0000_0000_0000_0000_0000_0000_0000_0001;
}

/// Check if passed flags support self flag
Expand All @@ -69,13 +69,24 @@ impl<'decoder> SetupConnection<'decoder> {
// [1] [1] -> true
// [0] [1] -> false
Protocol::MiningProtocol => {
// Evaluates protocol requirements based on flag bits.
//
// Checks if the current protocol meets the required flags for work selection and version rolling
// by reversing the bits of `available_flags` and `required_flags`. It extracts the 30th and 29th
// bits to determine if work selection and version rolling are needed.
//
// Returns `true` if:
// - The work selection requirement is satisfied or not needed.
// - The version rolling requirement is satisfied or not needed.
//
// Otherwise, returns `false`.
let available = available_flags.reverse_bits();
let required_flags = required_flags.reverse_bits();
let requires_work_selection_passed = (required_flags >> 30) > 0;
let requires_version_rolling_passed = (required_flags >> 29) > 0;
let requires_work_selection_passed = required_flags >> 30 > 0;
let requires_version_rolling_passed = required_flags >> 29 > 0;

let requires_work_selection_self = (available >> 30) > 0;
let requires_version_rolling_self = (available >> 29) > 0;
let requires_work_selection_self = available >> 30 > 0;
let requires_version_rolling_self = available >> 29 > 0;

let work_selection =
!requires_work_selection_self || requires_work_selection_passed;
Expand All @@ -84,8 +95,34 @@ impl<'decoder> SetupConnection<'decoder> {

work_selection && version_rolling
}
// TODO
_ => todo!(),
Protocol::JobDeclarationProtocol => {
// Determines if asynchronous job mining is required based on flag bits.
//
// Reverses the bits of `available_flags` and `required_flags`, extracts the 31st bit from each,
// and evaluates if the condition is met using these bits. Returns `true` or `false` based on:
// - True if `requires_async_job_mining_self` is true, or both are true.
// - False if `requires_async_job_mining_self` is false and `requires_async_job_mining_passed` is true.
// - True otherwise.
let available = available_flags.reverse_bits();
let required = required_flags.reverse_bits();

let requires_async_job_mining_passed = (required >> 31) & 1 > 0;
let requires_async_job_mining_self = (available >> 31) & 1 > 0;

match (
requires_async_job_mining_self,
requires_async_job_mining_passed,
) {
(true, true) => true,
(true, false) => true,
(false, true) => false,
(false, false) => true,
}
}
Protocol::TemplateDistributionProtocol | Protocol::JobDistributionProtocol => {
// These protocols do not define flags for setting up a connection.
false
}
}
}

Expand Down Expand Up @@ -310,7 +347,7 @@ impl<'decoder> binary_sv2::Decodable<'decoder> for Protocol {
fn get_structure(
_: &[u8],
) -> core::result::Result<alloc::vec::Vec<FieldMarker>, binary_sv2::Error> {
let field: FieldMarker = 0_u8.into();
let field: FieldMarker = (0_u8).into();
Ok(alloc::vec![field])
}
fn from_decoded_fields(
Expand Down Expand Up @@ -398,6 +435,16 @@ mod test {
flag_available,
flag_required
));

let protocol = crate::Protocol::JobDeclarationProtocol;

let available_flags = 0b_1000_0000_0000_0000_0000_0000_0000_0000;
let required_flags = 0b_1000_0000_0000_0000_0000_0000_0000_0000;
assert!(SetupConnection::check_flags(
protocol,
available_flags,
required_flags
));
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion roles/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Async Job Support
async_mining_allowed = true

# SRI Pool config
authority_public_key = "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72"
authority_secret_key = "mkDLTBBRxdBv998612qipDYoTK3YUrqLe8uWw7gu3iXbSrn2n"
Expand Down
3 changes: 3 additions & 0 deletions roles/jd-server/config-examples/jds-config-local-example.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Async Job Support
async_mining_allowed = true

# SRI Pool config
authority_public_key = "9auqWEzQDVyd2oe1JVGFLMLHZtCo2FFqZwtKA5gd9xbuEu7PH72"
authority_secret_key = "mkDLTBBRxdBv998612qipDYoTK3YUrqLe8uWw7gu3iXbSrn2n"
Expand Down
2 changes: 1 addition & 1 deletion roles/jd-server/src/lib/job_declarator/message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl ParseClientJobDeclarationMessages for JobDeclaratorDownstream {
request_id: message.request_id,
mining_job_token: token.to_le_bytes().to_vec().try_into().unwrap(),
coinbase_output_max_additional_size: 100,
async_mining_allowed: true,
async_mining_allowed: self.async_mining_allowed,
coinbase_output: self.coinbase_output.clone().try_into().unwrap(),
};
let message_enum = JobDeclaration::AllocateMiningJobTokenSuccess(message_success);
Expand Down
Loading

0 comments on commit dcaa92d

Please sign in to comment.