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
6 changes: 6 additions & 0 deletions sv2/channels-sv2/src/client/extended.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ impl<'a> ExtendedChannel<'a> {
.on_share_acknowledgement(new_submits_accepted_count, new_shares_sum);
}

/// Updates share accounting based on a [`SubmitSharesError`] message from the upstream
/// server. Delegates to [`ShareAccounting::on_share_rejection`].
pub fn on_share_rejection(&mut self) {
self.share_accounting.on_share_rejection();
}

/// Handles a [`NewExtendedMiningJob`] message received from upstream.
///
/// The message could be either directed at this channel, or at a group channel it belongs to.
Expand Down
40 changes: 33 additions & 7 deletions sv2/channels-sv2/src/client/share_accounting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,28 @@ pub enum ShareValidationError {
/// Used only on Mining Clients. Share accounting is split into two phases:
///
/// **Validation phase** (updated by [`validate_share`] via [`track_validated_share`]):
/// - total validated shares (shares that passed local validation)
/// - hashes of seen shares (for duplicate detection)
/// - last received share's sequence number
/// - highest difficulty seen in validated shares
///
/// **Acceptance phase** (updated by the application layer via [`on_share_acknowledgement`]):
/// - total accepted shares (confirmed by upstream [`SubmitSharesSuccess`])
/// - total acknowledged shares (confirmed by upstream [`SubmitSharesSuccess`])
/// - total rejected shares (reported by upstream [`SubmitSharesError`])
/// - cumulative work from accepted shares
/// - number of blocks found
///
/// [`validate_share`]: super::extended::ExtendedChannel::validate_share
/// [`track_validated_share`]: ShareAccounting::track_validated_share
/// [`on_share_acknowledgement`]: ShareAccounting::on_share_acknowledgement
/// [`SubmitSharesSuccess`]: mining_sv2::SubmitSharesSuccess
/// [`SubmitSharesError`]: mining_sv2::SubmitSharesError
#[derive(Clone, Debug)]
pub struct ShareAccounting {
last_share_sequence_number: u32,
shares_accepted: u32,
acknowledged_shares: u32,
validated_shares: u32,
rejected_shares: u32,
share_work_sum: f64,
seen_shares: HashSet<Hash>,
best_diff: f64,
Expand All @@ -79,7 +84,9 @@ impl ShareAccounting {
pub fn new() -> Self {
Self {
last_share_sequence_number: 0,
shares_accepted: 0,
acknowledged_shares: 0,
validated_shares: 0,
rejected_shares: 0,
share_work_sum: 0.0,
seen_shares: HashSet::new(),
best_diff: 0.0,
Expand All @@ -99,10 +106,18 @@ impl ShareAccounting {
new_submits_accepted_count: u32,
new_shares_sum: f64,
) {
self.shares_accepted += new_submits_accepted_count;
self.acknowledged_shares += new_submits_accepted_count;
self.share_work_sum += new_shares_sum;
}

/// Updates rejection accounting based on a [`SubmitSharesError`] message from the upstream
/// server.
///
/// One call corresponds to one rejected share.
pub fn on_share_rejection(&mut self) {
self.rejected_shares += 1;
}

/// Records a share that passed local validation.
///
/// Adds the hash to the seen set for duplicate detection and updates the last sequence
Expand All @@ -111,6 +126,7 @@ impl ShareAccounting {
/// called when the upstream server confirms via [`SubmitSharesSuccess`].
pub fn track_validated_share(&mut self, share_sequence_number: u32, share_hash: Hash) {
self.last_share_sequence_number = share_sequence_number;
self.validated_shares += 1;
self.seen_shares.insert(share_hash);
}

Expand All @@ -127,9 +143,19 @@ impl ShareAccounting {
self.last_share_sequence_number
}

/// Returns the total number of shares accepted.
pub fn get_shares_accepted(&self) -> u32 {
self.shares_accepted
/// Returns the total number of shares acknowledged by upstream.
pub fn get_acknowledged_shares(&self) -> u32 {
self.acknowledged_shares
}

/// Returns the total number of locally validated shares.
pub fn get_validated_shares(&self) -> u32 {
self.validated_shares
}

/// Returns the total number of shares rejected by upstream.
pub fn get_rejected_shares(&self) -> u32 {
self.rejected_shares
}

/// Returns the cumulative work of all accepted shares.
Expand Down
6 changes: 6 additions & 0 deletions sv2/channels-sv2/src/client/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ impl<'a> StandardChannel<'a> {
.on_share_acknowledgement(new_submits_accepted_count, new_shares_sum);
}

/// Updates share accounting based on a [`SubmitSharesError`] message from the upstream
/// server. Delegates to [`ShareAccounting::on_share_rejection`].
pub fn on_share_rejection(&mut self) {
self.share_accounting.on_share_rejection();
}

/// Handles a new group channel job by converting it into a standard job
/// and activating it in this channel's context.
///
Expand Down
Loading