Skip to content

fix(channels_sv2)!: keep seen shares when the tip returns to a prev_hash - #2421

Open
Jolah1 wants to merge 2 commits into
stratum-mining:mainfrom
Jolah1:fix-seen-shares-tip-revisit
Open

Jolah1 wants to merge 2 commits into
stratum-mining:mainfrom
Jolah1:fix-seen-shares-tip-revisit

Conversation

@Jolah1

@Jolah1 Jolah1 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

close #2396
close https://github.com/project-loupe/audit-stratum/issues/251
close https://github.com/project-loupe/audit-stratum/issues/267
close https://github.com/project-loupe/audit-stratum/issues/244

Channels cleared seen_shares on every prev_hash change, so after A→B→A a proof accepted under A could be counted again under the returning job.

Client channels now keep seen_shares across tip changes. The queue is already capped at MAX_SEEN_SHARES with oldest-first eviction, so memory stays bounded. flush_seen_shares is removed.

Server channels can't do that, since the set is a hard budget sized for one prev_hash. ShareAccounting::on_prev_hash_change(prev_hash, new_prev_hash) replaces flush_seen_shares. It keeps the set of the tip being left, tagged with its prev_hash, and makes it current again if the next change returns to it. Both server channels call it from update_chain_tip, so it covers on_set_new_prev_hash and activate_future_job. The extended channel also calls it from on_set_custom_mining_job, which flushed too but wasn't listed in the issue.

This differs from the server fix suggested in the issue. Keeping the previous set and checking both doesn't cover A→B→A: on B→A the kept set becomes B's and A's is dropped anyway. I tried it against the new tests and all three
server A→B→A cases still fail. Only the current set is checked, since header hashes commit to prev_hash and stale-job shares are rejected before the duplicate check.

Tests:

  • test_repeated_prev_hash_keeps_seen_shares in client extended, server extended and server standard now also goes A→B→A: the share under B goes through, and the original proof under the returning job is DuplicateShare. The two server tests also check that A's hash is not in the set while the tip is on B, and is back on the return.
  • Client standard didn't have that test, so it's added with both the repeated tip and the A→B→A case.
  • test_custom_job_returning_to_previous_prev_hash_keeps_seen_shares: the same round trip through on_set_custom_mining_job.
  • seen_shares_are_kept_for_the_previous_prev_hash: unit test for the swap, an unchanged prev_hash, and A→B→C→A where the older set is gone.

All the A→B→A tests fail with the old flush.

Side effects:

  • A server channel can hold up to twice its seen-shares budget.
  • A revisit across more than one tip (A→B→C→A) is still not covered.
  • On a return to A, the shares already accepted under A still count towards its budget. The budget covers a 10 h tip at twice the expected rate, so this only matters after a very long time on A.
  • The client queue isn't emptied every block anymore, so a duplicate check scans up to 4,096 hashes, which is negligible at client share rates.
  • flush_seen_shares is gone on both sides, so this is breaking. channels_sv2 goes 9.0.0 -> 10.0.0, and stratum_translation and stratum-core, which expose it, go to 0.7.0 and 0.8.0. sv2-apps doesn't call it and its main builds against this branch, so no companion PR.

Channels cleared seen_shares on every prev_hash change. When the tip
went A to B and back to A with the same coinbase and merkle root, a
proof accepted under A was not seen any more and was credited again
under the returning job ID.

Client channels now keep seen_shares across tip changes. The queue is
already capped at MAX_SEEN_SHARES and evicts oldest-first, so clearing
it was never needed to bound memory. flush_seen_shares is removed.

Server channels cannot do the same, since their set is a hard budget
sized for one prev_hash. ShareAccounting::on_prev_hash_change replaces
flush_seen_shares: it keeps the set of the prev_hash being left, tagged
with that prev_hash, and makes it current again if the next change
returns to it. Keeping that set without the tag is not enough: on the
way back to A it would be replaced by B's set and A's hashes would be
dropped anyway. The budget still applies to the current set, so a
server channel holds at most twice its budget. A revisit across more
than one tip (A to B to C to A) is not covered.

stratum-mining#2396
channels_sv2 breaks its API, so it takes a major bump. stratum_translation
exposes channels_sv2::bip141::StripBip141Error in StratumTranslationError,
and stratum-core re-exports both, so taking channels_sv2 10 breaks them too.
@Jolah1
Jolah1 force-pushed the fix-seen-shares-tip-revisit branch from 9094ea2 to b0fbd38 Compare November 13, 2026 04:02
@Jolah1

Jolah1 commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Rebased on main, conflicts are gone. Main now sets the server tip through update_chain_tip, so on_prev_hash_change is called from there, which also covers activate_future_job. The two server tests now check that A's hash
leaves the current set on B and comes back on the return. Added the same bump commit as #2422 (channels_sv2 10.0.0), whichever lands second drops it. Ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant