-
Notifications
You must be signed in to change notification settings - Fork 233
feat(pow): add relay-load-aware adaptive PoW difficulty #756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Priyanshubhartistm
wants to merge
6
commits into
cameri:main
Choose a base branch
from
Priyanshubhartistm:feat/adaptive-pow-pipeline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
cbb161e
feat(pow): add relay-load-aware adaptive PoW difficulty
Priyanshubhartistm c3555e5
test(pow): add unit tests for adaptive pow difficulty scaling
Priyanshubhartistm b34c467
Merge branch 'main' into feat/adaptive-pow-pipeline
Priyanshubhartistm e454e26
fix(pow): address review feedback on adaptive difficulty scaling
Priyanshubhartistm 106e398
fix(pow): keep adaptive difficulty scoped to eventId, not pubkey
Priyanshubhartistm 4326c2a
Merge branch 'main' into feat/adaptive-pow-pipeline
Priyanshubhartistm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| --- | ||
| "nostream": minor | ||
| --- | ||
|
|
||
| feat: add relay-load-aware adaptive PoW difficulty (NIP-13) | ||
|
|
||
| Adds `limits.event.pow` settings that scale the required proof-of-work difficulty between a | ||
| configured floor and ceiling based on the observed event rate, in place of the existing static | ||
| `minLeadingZeroBits` values. The event rate is tracked per worker process with the same EWMA shape | ||
| already used by the relay's rate limiter. Disabled by default (`limits.event.pow.enabled: false`), | ||
| so existing static PoW configuration is unaffected unless explicitly opted in. |
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { AdaptivePowSettings } from '../@types/settings' | ||
| import { calculateEWMA } from './ewma-rate-limiter' | ||
|
|
||
| // Per-worker in-process state: adaptive PoW is a soft anti-spam gate, not a | ||
| // hard cross-worker limit, so there's no need to pay a Redis round-trip on | ||
| // every single event just to read a difficulty threshold. | ||
| let rate = 0 | ||
| // 0, not Date.now(): the first recordEvent() call computes a huge deltaT | ||
| // against it, which decays rOld (0) to effectively nothing before adding | ||
| // the new hit -- exactly "never recorded before" without a special case. | ||
| let lastEventAt = 0 | ||
|
|
||
| export const recordEvent = (periodMs: number, now: number = Date.now()): void => { | ||
| rate = calculateEWMA(rate, Math.max(0, now - lastEventAt), periodMs, 1) | ||
| lastEventAt = now | ||
| } | ||
|
|
||
| export const getCurrentRate = (): number => rate | ||
|
|
||
| // calculateEWMA's `rate` is a recency-weighted event count, not a per-second | ||
| // rate: at a steady R events/sec it converges to R * periodMs/1000/ln(2) -- | ||
| // about 86.6x R at the default 60s half-life. Divide back out by that same | ||
| // factor before comparing against targetEventsPerSecond, which is per-second. | ||
| export const getCurrentEventsPerSecond = (periodMs: number): number => rate / (periodMs / 1000 / Math.LN2) | ||
|
|
||
| export const resetAdaptivePowState = (): void => { | ||
| rate = 0 | ||
| lastEventAt = 0 | ||
| } | ||
|
|
||
| export const getCurrentDifficulty = (config: AdaptivePowSettings): number => { | ||
| const eventsPerSecond = getCurrentEventsPerSecond(config.periodMs) | ||
|
|
||
| if (config.targetEventsPerSecond <= 0 || eventsPerSecond <= config.targetEventsPerSecond) { | ||
| return config.floorBits | ||
| } | ||
|
|
||
| const ratio = eventsPerSecond / config.targetEventsPerSecond | ||
| const scaled = config.floorBits + Math.ceil((ratio - 1) * (config.ceilingBits - config.floorBits)) | ||
|
Priyanshubhartistm marked this conversation as resolved.
|
||
|
|
||
| return Math.max(config.floorBits, Math.min(config.ceilingBits, scaled)) | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.