Skip to content

Commit 9610e34

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 78585d7 commit 9610e34

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-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: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,22 @@ pub enum ShareValidationError {
5050
/// - highest difficulty seen in validated shares
5151
///
5252
/// **Acceptance phase** (updated by the application layer via [`on_share_acknowledgement`]):
53-
/// - total accepted shares (confirmed by upstream [`SubmitSharesSuccess`])
53+
/// - total acknowledged shares (confirmed by upstream [`SubmitSharesSuccess`])
54+
/// - total rejected shares (reported by upstream [`SubmitSharesError`])
5455
/// - cumulative work from accepted shares
5556
/// - number of blocks found
5657
///
5758
/// [`validate_share`]: super::extended::ExtendedChannel::validate_share
5859
/// [`track_validated_share`]: ShareAccounting::track_validated_share
5960
/// [`on_share_acknowledgement`]: ShareAccounting::on_share_acknowledgement
6061
/// [`SubmitSharesSuccess`]: mining_sv2::SubmitSharesSuccess
62+
/// [`SubmitSharesError`]: mining_sv2::SubmitSharesError
6163
#[derive(Clone, Debug)]
6264
pub struct ShareAccounting {
6365
last_share_sequence_number: u32,
64-
shares_accepted: u32,
66+
acknowledged_shares: u32,
67+
validated_shares: u32,
68+
rejected_shares: u32,
6569
share_work_sum: f64,
6670
seen_shares: HashSet<Hash>,
6771
best_diff: f64,
@@ -79,7 +83,9 @@ impl ShareAccounting {
7983
pub fn new() -> Self {
8084
Self {
8185
last_share_sequence_number: 0,
82-
shares_accepted: 0,
86+
acknowledged_shares: 0,
87+
validated_shares: 0,
88+
rejected_shares: 0,
8389
share_work_sum: 0.0,
8490
seen_shares: HashSet::new(),
8591
best_diff: 0.0,
@@ -99,10 +105,18 @@ impl ShareAccounting {
99105
new_submits_accepted_count: u32,
100106
new_shares_sum: f64,
101107
) {
102-
self.shares_accepted += new_submits_accepted_count;
108+
self.acknowledged_shares += new_submits_accepted_count;
103109
self.share_work_sum += new_shares_sum;
104110
}
105111

112+
/// Updates rejection accounting based on a [`SubmitSharesError`] message from the upstream
113+
/// server.
114+
///
115+
/// One call corresponds to one rejected share.
116+
pub fn on_share_rejection(&mut self) {
117+
self.rejected_shares += 1;
118+
}
119+
106120
/// Records a share that passed local validation.
107121
///
108122
/// Adds the hash to the seen set for duplicate detection and updates the last sequence
@@ -111,6 +125,7 @@ impl ShareAccounting {
111125
/// called when the upstream server confirms via [`SubmitSharesSuccess`].
112126
pub fn track_validated_share(&mut self, share_sequence_number: u32, share_hash: Hash) {
113127
self.last_share_sequence_number = share_sequence_number;
128+
self.validated_shares += 1;
114129
self.seen_shares.insert(share_hash);
115130
}
116131

@@ -127,9 +142,19 @@ impl ShareAccounting {
127142
self.last_share_sequence_number
128143
}
129144

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

135160
/// 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)