Skip to content

Commit e93e347

Browse files
committed
channels-sv2: refine client share accounting counters
replace shares_accepted with acknowledged_shares, validated_shares, and rejected_shares add on_share_rejection and delegate it from extended and standard channels update on_share_acknowledgement and track_validated_share to increment the new counters add getters for acknowledged, validated, and rejected shares
1 parent 1dcf5f9 commit e93e347

File tree

3 files changed

+45
-7
lines changed

3 files changed

+45
-7
lines changed

sv2/channels-sv2/src/client/extended.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@ impl<'a> ExtendedChannel<'a> {
223223
.on_share_acknowledgement(new_submits_accepted_count, new_shares_sum);
224224
}
225225

226+
/// Updates share accounting based on a [`SubmitSharesError`] message from the upstream
227+
/// server. Delegates to [`ShareAccounting::on_share_rejection`].
228+
pub fn on_share_rejection(&mut self) {
229+
self.share_accounting.on_share_rejection();
230+
}
231+
226232
/// Handles a [`NewExtendedMiningJob`] message received from upstream.
227233
///
228234
/// The message could be either directed at this channel, or at a group channel it belongs to.

sv2/channels-sv2/src/client/share_accounting.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,23 +45,28 @@ pub enum ShareValidationError {
4545
/// Used only on Mining Clients. Share accounting is split into two phases:
4646
///
4747
/// **Validation phase** (updated by [`validate_share`] via [`track_validated_share`]):
48+
/// - total validated shares (shares that passed local validation)
4849
/// - hashes of seen shares (for duplicate detection)
4950
/// - last received share's sequence number
5051
/// - highest difficulty seen in validated shares
5152
///
5253
/// **Acceptance phase** (updated by the application layer via [`on_share_acknowledgement`]):
53-
/// - total accepted shares (confirmed by upstream [`SubmitSharesSuccess`])
54+
/// - total acknowledged shares (confirmed by upstream [`SubmitSharesSuccess`])
55+
/// - total rejected shares (reported by upstream [`SubmitSharesError`])
5456
/// - cumulative work from accepted shares
5557
/// - number of blocks found
5658
///
5759
/// [`validate_share`]: super::extended::ExtendedChannel::validate_share
5860
/// [`track_validated_share`]: ShareAccounting::track_validated_share
5961
/// [`on_share_acknowledgement`]: ShareAccounting::on_share_acknowledgement
6062
/// [`SubmitSharesSuccess`]: mining_sv2::SubmitSharesSuccess
63+
/// [`SubmitSharesError`]: mining_sv2::SubmitSharesError
6164
#[derive(Clone, Debug)]
6265
pub struct ShareAccounting {
6366
last_share_sequence_number: u32,
64-
shares_accepted: u32,
67+
acknowledged_shares: u32,
68+
validated_shares: u32,
69+
rejected_shares: u32,
6570
share_work_sum: f64,
6671
seen_shares: HashSet<Hash>,
6772
best_diff: f64,
@@ -79,7 +84,9 @@ impl ShareAccounting {
7984
pub fn new() -> Self {
8085
Self {
8186
last_share_sequence_number: 0,
82-
shares_accepted: 0,
87+
acknowledged_shares: 0,
88+
validated_shares: 0,
89+
rejected_shares: 0,
8390
share_work_sum: 0.0,
8491
seen_shares: HashSet::new(),
8592
best_diff: 0.0,
@@ -99,10 +106,18 @@ impl ShareAccounting {
99106
new_submits_accepted_count: u32,
100107
new_shares_sum: f64,
101108
) {
102-
self.shares_accepted += new_submits_accepted_count;
109+
self.acknowledged_shares += new_submits_accepted_count;
103110
self.share_work_sum += new_shares_sum;
104111
}
105112

113+
/// Updates rejection accounting based on a [`SubmitSharesError`] message from the upstream
114+
/// server.
115+
///
116+
/// One call corresponds to one rejected share.
117+
pub fn on_share_rejection(&mut self) {
118+
self.rejected_shares += 1;
119+
}
120+
106121
/// Records a share that passed local validation.
107122
///
108123
/// Adds the hash to the seen set for duplicate detection and updates the last sequence
@@ -111,6 +126,7 @@ impl ShareAccounting {
111126
/// called when the upstream server confirms via [`SubmitSharesSuccess`].
112127
pub fn track_validated_share(&mut self, share_sequence_number: u32, share_hash: Hash) {
113128
self.last_share_sequence_number = share_sequence_number;
129+
self.validated_shares += 1;
114130
self.seen_shares.insert(share_hash);
115131
}
116132

@@ -127,9 +143,19 @@ impl ShareAccounting {
127143
self.last_share_sequence_number
128144
}
129145

130-
/// Returns the total number of shares accepted.
131-
pub fn get_shares_accepted(&self) -> u32 {
132-
self.shares_accepted
146+
/// Returns the total number of shares acknowledged by upstream.
147+
pub fn get_acknowledged_shares(&self) -> u32 {
148+
self.acknowledged_shares
149+
}
150+
151+
/// Returns the total number of locally validated shares.
152+
pub fn get_validated_shares(&self) -> u32 {
153+
self.validated_shares
154+
}
155+
156+
/// Returns the total number of shares rejected by upstream.
157+
pub fn get_rejected_shares(&self) -> u32 {
158+
self.rejected_shares
133159
}
134160

135161
/// Returns the cumulative work of all accepted shares.

sv2/channels-sv2/src/client/standard.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,12 @@ impl<'a> StandardChannel<'a> {
179179
.on_share_acknowledgement(new_submits_accepted_count, new_shares_sum);
180180
}
181181

182+
/// Updates share accounting based on a [`SubmitSharesError`] message from the upstream
183+
/// server. Delegates to [`ShareAccounting::on_share_rejection`].
184+
pub fn on_share_rejection(&mut self) {
185+
self.share_accounting.on_share_rejection();
186+
}
187+
182188
/// Handles a new group channel job by converting it into a standard job
183189
/// and activating it in this channel's context.
184190
///

0 commit comments

Comments
 (0)