util, swaps: fix nostr announcement PoW mining - #10859
Open
Amperstrand wants to merge 2 commits into
Open
Conversation
These tests currently fail: three defects in the PoW path, see next commit. The tests do not run PoW searches; winning nonces are precomputed and the multiprocessing machinery is faked. The real searches in TestNostrPowSearch are skipped unless ELECTRUM_TESTS_NOSTR_POW is set, to keep hash loops off CI.
Three defects in the swapserver nostr announcement PoW path, all introduced in 947094c (PR spesmilo#9551): - nostr_pow_worker returned the builtin hash instead of the sha256 digest of the winning preimage - gen_nostr_ann_pow collected the result with done.pop() on the set returned by asyncio.wait(FIRST_COMPLETED). Workers that observe the shutdown event return (None, None) at their next block boundary, and their results can be delivered before the winner's own result, so the freshly mined winning nonce could be silently discarded. Wait for all workers and collect the result that carries a nonce. - set_nostr_proof_of_work stored the nonce without checking the achieved work against SWAPSERVER_POW_TARGET. A None nonce would later crash publish_offer with hex(None), killing run_nostr_server.
Amperstrand
force-pushed
the
nostr-pow-fix
branch
from
August 17, 2026 07:27
3da2f72 to
bbcf925
Compare
Author
|
Full A/B logs for the record: https://gist.github.com/Amperstrand/4b477e0fbc14b22f8601c601f964b210
If a different collection strategy is preferred (e.g. return as soon as any nonce-bearing result completes, instead of waiting for all workers — slightly more code, but zero added startup latency), happy to rework. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #10858
Summary
Three defects in the swapserver nostr announcement PoW path, all introduced in
947094c1b(PR #9551):nostr_pow_workerreturned the builtinhashinstead of the sha256digestof the winning preimage.gen_nostr_ann_powcollected the result viadone.pop()on the unordered set returned byasyncio.wait(FIRST_COMPLETED). Workers that observe the shutdown event return(None, None)at their next 1M-hash block boundary, and their results can be delivered to the event loop before the winner's own result (independent channels: manager socket vs executor result pipe), so the freshly-mined winning nonce could be silently discarded and(None, 0)propagated.set_nostr_proof_of_workstored the returned nonce without checkingpow_amount >= SWAPSERVER_POW_TARGET.ConfigVar._set_config_valueskips its type-check forNone, so the race outcome was persisted — the nextpublish_offerthen raisedTypeError: hex(None)outside its try/except, killingrun_nostr_server(swapserver stops announcing over nostr until restart, then re-mines from scratch).Changes (+16/−3 across
electrum/util.pyandelectrum/submarine_swaps.py)nostr_pow_worker:return digest, noncegen_nostr_ann_pow: wait for all workers (ALL_COMPLETED), collect the result that carries a nonce, raise if none doesset_nostr_proof_of_work: raise instead of storing a nonce whose achieved work is below the targetTesting
New
tests/test_nostr_pow.py(first commit of this PR; the second commit only changes the two source files):test_observer_result_completing_first_is_ignored) — deterministic worst-case interleaving, no flakes across repeated runstests/test_util.py+tests/test_submarine_swaps.py: 64/64 greenTestNostrPowSearchruns actual searches and is skipped by default to keep hash loops off CI; locally withELECTRUM_TESTS_NOSTR_POW=1 python -m pytest tests/test_nostr_pow.py -vit is 9/9 green (~2 s at a 16-bit target)