Skip to content

refactor(relay): derive the NIP-11 PoW floor from the handler's difficulty - #1262

Merged
chappie-daemon merged 1 commit into
rewrite/mainfrom
factory/039-nip11-pow-derivation
Nov 27, 2026
Merged

chappie-daemon merged 1 commit into
rewrite/mainfrom
factory/039-nip11-pow-derivation

Conversation

@chappie-daemon

Copy link
Copy Markdown
Collaborator

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>) -> u32 owns the whole derivation: the adaptive arm maps limits.event.pow into AdaptivePowConfig and WotPowThreshold, computes the difficulty at the given rate, and applies the WoT policy at the given distance; the static arm answers limits.event.eventId.minLeadingZeroBits. 0 means "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 at 0. The handler calls it once with context.pow's measured rate, collapsing base's if/else if pair of PoW arms into one comparison with the same rejection string at the same gate position. Base's private min_pow_difficulty and its doc comment are deleted; the rule now lives on the shared function.

Why it matters. At 54a5a39f the two ends agree only because event.rs:371 hard-codes None as 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.md D4 already named required_difficulty as the source, and this branch makes that literal.

Verified. Equivalence checked rather than asserted: the deleted nip11::min_pow_difficulty and the handler's two arms were re-implemented verbatim from base and diffed against the branch over 5 407 settings shapes × 7 rates (including NaN and ∞) × 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 case the_advertised_minimum_difficulty_is_the_one_enforced is 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 #N is inert on this repository's integration branch; #1217 is closed by hand with the merge sha.

@changeset-bot

changeset-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3bd6bd2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 0 packages

When 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

@coderabbitai

coderabbitai Bot commented Nov 27, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 86d16235-7ab3-48d6-a443-7c9a97a65b7b

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Confidence Score: 5/5 · This refactor appears safe to merge.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This refactor appears safe to merge.

Summary

The handler and NIP-11 builder now share required_difficulty.

  • The relay derives its published and enforced proof-of-work floor from one rule.

No actionable issues found. Tests were reviewed but not run.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart LR
  H[Event handler] -->|Measured rate, no distance| R[required_difficulty]
  N[NIP-11 builder] -->|Zero rate, no distance| R
  R --> A{Adaptive PoW enabled?}
  A -->|Yes| P[Compute difficulty and apply WoT policy]
  A -->|No| S[Static event-id floor or zero]
  P --> D[Required difficulty]
  S --> D
Loading

Reviews (1) · Last reviewed commit: "refactor(relay): derive the NIP-11 PoW f..." · Reviewed by Greptile

@chappie-daemon
chappie-daemon merged commit f112973 into rewrite/main Nov 27, 2026
19 checks passed
@chappie-daemon
chappie-daemon deleted the factory/039-nip11-pow-derivation branch November 27, 2026 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant