pow: a fast miner that hashes the bytes before the nonce once (about 100 times faster)

pow.Mine finds the smallest nonce, exactly what trying 0, 1, 2, ... finds, so every signed record and golden fixture stays the same. It saves the SHA-256 state after the whole blocks before the nonce, hashes only the number and what follows per attempt, and uses every core but one from difficulty 14 up. Listing, report, revocation and message mining use it; each re-checks the result before signing. One listing at difficulty 16 takes about 19 ms instead of about 2 s. docs/PROTOCOL.md says what proof-of-work can and cannot do against fast hardware.

allmende-load fills a data directory with signed invented listings for capacity tests and measures mining time (-mine-only).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01AeFqV96dVeBX94inzcgZKw
This commit is contained in:
2026-11-19 00:15:52 +01:00
co-authored by Claude Sonnet 5.5
parent 1177754daf
commit 52a6d87553
13 changed files with 747 additions and 44 deletions
+21
View File
@@ -533,6 +533,27 @@ Proof-of-work per listing and per revocation, abuse reports (below), and
subscribable blocklists (not built yet). The format leaves room for vouching
signatures later.
**Mining** (`internal/pow`, `pow.Mine`): the nonce is the smallest number that
makes the canonical bytes hash to enough leading zero bits, so every miner that
tries 0, 1, 2, ... finds the same one. The node hashes the bytes before the
nonce once and, per attempt, only the number and what follows, on every core
but one. Measured on an 8-core machine under other load, mining one listing
takes about 19 ms at difficulty 16 (the default), 0.14 s at 20, 1.1 s at 22,
3.6 s at 24 and 15 s at 26: about 16 times longer per 4 bits. (Before this,
every attempt re-encoded the JSON and one listing at 16 took about 2 s.)
**What it can and cannot do.** The difficulty is a number of hashes, so it
prices the work the same for everyone, but hardware is not equal: an attacker
with an optimized miner or a GPU does what the node does in a fraction of the
time. A difficulty that takes an honest laptop 30 s (about 27 bits) takes a GPU
well under a second, and one that takes a GPU 30 s would take the laptop hours.
So proof-of-work stops casual bulk posting, not a determined spammer, and is
only one layer: it is paired with what a node does with the traffic it gets
(rate limits and peer scoring, the reports and the hide rule, later vouching
and blocklists). The difficulty is network-wide: nodes check it against their
own `--difficulty`, so changing the default needs every node to change, and a
node with a higher value refuses listings mined for a lower one.
## Abuse reports
Implemented in `internal/report` (the record), `internal/store/reports.go`