util, swaps: fix nostr announcement PoW mining - #10859
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.
3da2f72 to
bbcf925
Compare
|
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. |
| @@ -0,0 +1,197 @@ | |||
| """Tests for the nostr announcement PoW of the swapserver plugin. | |||
There was a problem hiding this comment.
Adding this whole new test module for the simple pow functionality feels a bit overkill, I'd rather drop it
| self.config.SWAPSERVER_POW_TARGET, | ||
| ) | ||
| self.logger.debug(f"Found {pow_amount} bits of work for Nostr announcement.") | ||
| if pow_amount < self.config.SWAPSERVER_POW_TARGET: |
There was a problem hiding this comment.
| if pow_amount < self.config.SWAPSERVER_POW_TARGET: | |
| assert pow_amount >= self.config.SWAPSERVER_POW_TARGET, pow_amount |
Considering this shouldn't happen I think an assert would make this more clear here instead of making the reader reason about how this could happen
| if int.from_bytes(digest, 'big') < (1 << (hash_len_bits - target_bits)): | ||
| shutdown.set() | ||
| return hash, nonce | ||
| return digest, nonce |
There was a problem hiding this comment.
considering the hash/digest return value is never used, it is probably best to remove it completely, making the result access in gen_nostr_ann_pow more readable too
This is hallucinated, see: However still, good catch, it could cause a swap provider to announce itself without PoW, making it invisible to clients |
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)