Repository navigation
refactor(relay): derive the NIP-11 PoW floor from the handler's difficulty - #1262
Conversation
🦋 Changeset detectedLatest commit: 3bd6bd2 The changes in this PR will be included in the next version bump. This PR includes changesets to release 0 packagesWhen changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the
|
[Medium impact] This refactor appears safe to merge.
|
The NIP-11 document and the event handler now agree on the PoW difficulty by construction, not by value.
The mechanism.
nostream_relay::handlers::event::required_difficulty(event: Option<&EventLimits>, events_per_second: f64, distance: Option<u32>) -> u32owns the whole derivation: the adaptive arm mapslimits.event.powintoAdaptivePowConfigandWotPowThreshold, computes the difficulty at the given rate, and applies the WoT policy at the given distance; the static arm answerslimits.event.eventId.minLeadingZeroBits.0means "no requirement".Both callers. The document calls it at
(event, 0.0, None)— a zero rate is the floor at rest and no distance is a reader outside the trust graph — and folds the answer to an absent key at0. The handler calls it once withcontext.pow's measured rate, collapsing base'sif/else ifpair of PoW arms into one comparison with the same rejection string at the same gate position. Base's privatemin_pow_difficultyand its doc comment are deleted; the rule now lives on the shared function.Why it matters. At
54a5a39fthe two ends agree only becauseevent.rs:371hard-codesNoneas the distance. The moment the WoT graph is wired, the document would keep advertising the floor while the handler began granting discounts. With one function, the advertisement is the enforced derivation:docs/rewrite/decisions/0016-relay-status-and-nip11.mdD4 already namedrequired_difficultyas the source, and this branch makes that literal.Verified. Equivalence checked rather than asserted: the deleted
nip11::min_pow_difficultyand the handler's two arms were re-implemented verbatim from base and diffed against the branch over 5 407 settings shapes × 7 rates (includingNaNand∞) × 9 observed bit counts — 340 578 gate decisions with zero rejection-string drift, 3 107 validator-accepted shapes with zero document drift. The one behaviour that moves is 240 validator-rejected shapes where base advertised a floor it would not enforce; the branch advertises the enforced value.Tests. New
the_advertised_difficulty_is_the_shared_functions_answer— six rows (no limits; pow disabled with an event-id floor; floor 0; floor 16; floor 16 with a 0.5 threshold, which answers 8 at distance 2; and 32 at 100 ev/s), each asserting the function's literal and the document's folded value. The two-ended socket casethe_advertised_minimum_difficulty_is_the_one_enforcedis untouched and green. nip11 8 (base 7), handlers 66 (base 66), workspace 116 targets green, fmt, both clippy passes and release clean.Four paths:
event.rs,nip11.rs,tests/nip11.rs, and one empty changeset.Rust only: no version bump.
Closes #Nis inert on this repository's integration branch; #1217 is closed by hand with the merge sha.