Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions protocols/v2/channels-sv2/src/client/extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,11 +533,11 @@ impl<'a> ExtendedChannel<'a> {
// check if a block was found
if network_target.is_met_by(hash) {
self.share_accounting.update_share_accounting(
self.target.difficulty_float() as u64,
self.target.difficulty_float(),
share.sequence_number,
hash.to_raw_hash(),
);
return Ok(ShareValidationResult::BlockFound);
return Ok(ShareValidationResult::BlockFound(hash.to_raw_hash()));
}

// check if the share hash meets the channel target
Expand All @@ -547,15 +547,15 @@ impl<'a> ExtendedChannel<'a> {
}

self.share_accounting.update_share_accounting(
self.target.difficulty_float() as u64,
self.target.difficulty_float(),
share.sequence_number,
hash.to_raw_hash(),
);

// update the best diff
self.share_accounting.update_best_diff(hash_as_diff);

return Ok(ShareValidationResult::Valid);
return Ok(ShareValidationResult::Valid(hash.to_raw_hash()));
}

Err(ShareValidationError::DoesNotMeetTarget)
Expand Down Expand Up @@ -815,7 +815,7 @@ mod tests {

let res = channel.validate_share(share_valid_block);

assert!(matches!(res, Ok(ShareValidationResult::BlockFound)));
assert!(matches!(res, Ok(ShareValidationResult::BlockFound(_))));
}

#[test]
Expand Down Expand Up @@ -1002,7 +1002,7 @@ mod tests {

let res = channel.validate_share(valid_share);

assert!(matches!(res, Ok(ShareValidationResult::Valid)));
assert!(matches!(res, Ok(ShareValidationResult::Valid(_))));

// try to cheat by re-submitting the same share
// with a different sequence number
Expand Down
12 changes: 6 additions & 6 deletions protocols/v2/channels-sv2/src/client/share_accounting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use bitcoin::hashes::sha256d::Hash;
/// - `BlockFound`: The submitted share resulted in a new block being found.
#[derive(Debug)]
pub enum ShareValidationResult {
Valid,
BlockFound,
Valid(Hash),
BlockFound(Hash),
}

/// Possible errors encountered during share validation.
Expand Down Expand Up @@ -53,7 +53,7 @@ pub enum ShareValidationError {
pub struct ShareAccounting {
last_share_sequence_number: u32,
shares_accepted: u32,
share_work_sum: u64,
share_work_sum: f64,
seen_shares: HashSet<Hash>,
best_diff: f64,
}
Expand All @@ -70,7 +70,7 @@ impl ShareAccounting {
Self {
last_share_sequence_number: 0,
shares_accepted: 0,
share_work_sum: 0,
share_work_sum: 0.0,
seen_shares: HashSet::new(),
best_diff: 0.0,
}
Expand All @@ -83,7 +83,7 @@ impl ShareAccounting {
/// - Records share hash to detect duplicates.
pub fn update_share_accounting(
&mut self,
share_work: u64,
share_work: f64,
share_sequence_number: u32,
share_hash: Hash,
) {
Expand Down Expand Up @@ -112,7 +112,7 @@ impl ShareAccounting {
}

/// Returns the cumulative work of all accepted shares.
pub fn get_share_work_sum(&self) -> u64 {
pub fn get_share_work_sum(&self) -> f64 {
self.share_work_sum
}

Expand Down
12 changes: 6 additions & 6 deletions protocols/v2/channels-sv2/src/client/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,11 @@ impl<'a> StandardChannel<'a> {
// check if a block was found
if network_target.is_met_by(hash) {
self.share_accounting.update_share_accounting(
self.target.difficulty_float() as u64,
self.target.difficulty_float(),
share.sequence_number,
hash.to_raw_hash(),
);
return Ok(ShareValidationResult::BlockFound);
return Ok(ShareValidationResult::BlockFound(hash.to_raw_hash()));
}

// check if the share hash meets the channel target
Expand All @@ -344,15 +344,15 @@ impl<'a> StandardChannel<'a> {
}

self.share_accounting.update_share_accounting(
self.target.difficulty_float() as u64,
self.target.difficulty_float(),
share.sequence_number,
hash.to_raw_hash(),
);

// update the best diff
self.share_accounting.update_best_diff(hash_as_diff);

return Ok(ShareValidationResult::Valid);
return Ok(ShareValidationResult::Valid(hash.to_raw_hash()));
}

Err(ShareValidationError::DoesNotMeetTarget)
Expand Down Expand Up @@ -542,7 +542,7 @@ mod tests {

let res = channel.validate_share(share_valid_block);

assert!(matches!(res, Ok(ShareValidationResult::BlockFound)));
assert!(matches!(res, Ok(ShareValidationResult::BlockFound(_))));
}

#[test]
Expand Down Expand Up @@ -691,6 +691,6 @@ mod tests {

let res = channel.validate_share(valid_share);

assert!(matches!(res, Ok(ShareValidationResult::Valid)));
assert!(matches!(res, Ok(ShareValidationResult::Valid(_))));
}
}
34 changes: 14 additions & 20 deletions protocols/v2/channels-sv2/src/server/extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,7 @@ where
// check if a block was found
if network_target.is_met_by(hash) {
self.share_accounting.update_share_accounting(
self.target.difficulty_float() as u64,
self.target.difficulty_float(),
share.sequence_number,
hash.to_raw_hash(),
);
Expand All @@ -687,12 +687,17 @@ where
JobOrigin::NewTemplate(template) => {
let template_id = template.template_id;
return Ok(ShareValidationResult::BlockFound(
hash.to_raw_hash(),
Some(template_id),
coinbase,
));
}
JobOrigin::SetCustomMiningJob(_set_custom_mining_job) => {
return Ok(ShareValidationResult::BlockFound(None, coinbase));
return Ok(ShareValidationResult::BlockFound(
hash.to_raw_hash(),
None,
coinbase,
));
}
}
}
Expand All @@ -704,29 +709,15 @@ where
}

self.share_accounting.update_share_accounting(
self.target.difficulty_float() as u64,
self.target.difficulty_float(),
share.sequence_number,
hash.to_raw_hash(),
);

// update the best diff
self.share_accounting.update_best_diff(hash_as_diff);

let last_sequence_number = self.share_accounting.get_last_share_sequence_number();
let new_submits_accepted_count = self.share_accounting.get_shares_accepted();
let new_shares_sum = self.share_accounting.get_share_work_sum();

// if sequence number is a multiple of share_batch_size
// it's time to send a SubmitShares.Success
if self.share_accounting.should_acknowledge() {
Ok(ShareValidationResult::ValidWithAcknowledgement(
last_sequence_number,
new_submits_accepted_count,
new_shares_sum,
))
} else {
Ok(ShareValidationResult::Valid)
}
Ok(ShareValidationResult::Valid(hash.to_raw_hash()))
} else {
Err(ShareValidationError::DoesNotMeetTarget)
}
Expand Down Expand Up @@ -1206,7 +1197,10 @@ mod tests {

let res = channel.validate_share(share_valid_block);

assert!(matches!(res, Ok(ShareValidationResult::BlockFound(_, _))));
assert!(matches!(
res,
Ok(ShareValidationResult::BlockFound(_, _, _))
));
}

#[test]
Expand Down Expand Up @@ -1429,7 +1423,7 @@ mod tests {
};

let res = channel.validate_share(valid_share);
assert!(matches!(res, Ok(ShareValidationResult::Valid)));
assert!(matches!(res, Ok(ShareValidationResult::Valid(_))));

// try to cheat by re-submitting the same share
// with a different sequence number
Expand Down
19 changes: 7 additions & 12 deletions protocols/v2/channels-sv2/src/server/share_accounting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,13 @@ use std::collections::HashSet;
#[derive(Debug)]
pub enum ShareValidationResult {
/// The share is valid and accepted.
Valid,
/// The share is valid and triggers a batch acknowledgment.
/// Contains:
/// - `last_sequence_number`: The sequence number of the last accepted share in the batch.
/// - `new_submits_accepted_count`: The number of new shares accepted in this batch.
/// - `new_shares_sum`: The total work contributed by shares in this batch.
ValidWithAcknowledgement(u32, u32, u64),
Valid(Hash),
/// The share solves a block.
/// Contains:
/// - `share_hash`: The hash of the share that solved the block.
/// - `template_id`: The template ID associated with the job, or `None` for custom jobs.
/// - `coinbase`: The serialized coinbase transaction for the block.
BlockFound(Option<u64>, Vec<u8>),
BlockFound(Hash, Option<u64>, Vec<u8>),
}

/// The error variants that can occur during share validation.
Expand Down Expand Up @@ -84,7 +79,7 @@ pub enum ShareValidationError {
pub struct ShareAccounting {
last_share_sequence_number: u32,
shares_accepted: u32,
share_work_sum: u64,
share_work_sum: f64,
share_batch_size: usize,
seen_shares: HashSet<Hash>,
best_diff: f64,
Expand All @@ -98,7 +93,7 @@ impl ShareAccounting {
Self {
last_share_sequence_number: 0,
shares_accepted: 0,
share_work_sum: 0,
share_work_sum: 0.0,
share_batch_size,
seen_shares: HashSet::new(),
best_diff: 0.0,
Expand All @@ -112,7 +107,7 @@ impl ShareAccounting {
/// - Records the share hash to detect duplicates.
pub fn update_share_accounting(
&mut self,
share_work: u64,
share_work: f64,
share_sequence_number: u32,
share_hash: Hash,
) {
Expand Down Expand Up @@ -141,7 +136,7 @@ impl ShareAccounting {
}

/// Returns the sum of work contributed by all accepted shares.
pub fn get_share_work_sum(&self) -> u64 {
pub fn get_share_work_sum(&self) -> f64 {
self.share_work_sum
}

Expand Down
28 changes: 9 additions & 19 deletions protocols/v2/channels-sv2/src/server/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ where
// check if a block was found
if network_target.is_met_by(hash) {
self.share_accounting.update_share_accounting(
self.target.difficulty_float() as u64,
self.target.difficulty_float(),
share.sequence_number,
hash.to_raw_hash(),
);
Expand Down Expand Up @@ -628,6 +628,7 @@ where
.map_err(|_| ShareValidationError::InvalidCoinbase)?;

return Ok(ShareValidationResult::BlockFound(
hash.to_raw_hash(),
Some(job.get_template().template_id),
serialized_coinbase,
));
Expand All @@ -640,29 +641,15 @@ where
}

self.share_accounting.update_share_accounting(
self.target.difficulty_float() as u64,
self.target.difficulty_float(),
share.sequence_number,
hash.to_raw_hash(),
);

// update the best diff
self.share_accounting.update_best_diff(hash_as_diff);

let last_sequence_number = self.share_accounting.get_last_share_sequence_number();
let new_submits_accepted_count = self.share_accounting.get_shares_accepted();
let new_shares_sum = self.share_accounting.get_share_work_sum();

// if sequence number is a multiple of share_batch_size
// it's time to send a SubmitShares.Success
if self.share_accounting.should_acknowledge() {
Ok(ShareValidationResult::ValidWithAcknowledgement(
last_sequence_number,
new_submits_accepted_count,
new_shares_sum,
))
} else {
Ok(ShareValidationResult::Valid)
}
Ok(ShareValidationResult::Valid(hash.to_raw_hash()))
} else {
Err(ShareValidationError::DoesNotMeetTarget)
}
Expand Down Expand Up @@ -1022,7 +1009,10 @@ mod tests {

let res = standard_channel.validate_share(share_valid_block);

assert!(matches!(res, Ok(ShareValidationResult::BlockFound(_, _))));
assert!(matches!(
res,
Ok(ShareValidationResult::BlockFound(_, _, _))
));
}

#[test]
Expand Down Expand Up @@ -1238,7 +1228,7 @@ mod tests {
};
let res = standard_channel.validate_share(valid_share);

assert!(matches!(res, Ok(ShareValidationResult::Valid)));
assert!(matches!(res, Ok(ShareValidationResult::Valid(_))));
}

#[test]
Expand Down
Loading
Loading