Skip to content

Commit ead3997

Browse files
authored
test(relay): pin the handler's PoW rate conversion at non-zero rates (#1263) (#1343)
## Description Pins the event handler's dynamic PoW rate conversion at a non-zero rate with a socket-level test where accepted events advance `context.pow` and raise required difficulty above the floor. ## Related Issue Closes #1263 ## Motivation and Context In `crates/nostream-relay/src/handlers/event.rs`, `context.pow` computes the current events-per-second rate and passes it to `required_difficulty(Some(limits), rate, None)`. Previously, all socket-level tests ran at a rate of zero (`HandlerContext.pow` starts empty and was never advanced in tests), meaning substituting the literal `0.0` for that conversion would leave the whole suite green. This PR adds: - A socket-level test in `tests/handlers.rs` where an accepted event advances `context.pow` under `target_events_per_second: 0.01`, verifying that the gate dynamically raises required difficulty above the floor and rejects a subsequent event. - A unit test in `handlers/event.rs` verifying `required_difficulty` scaling at rates above target. ## How Has This Been Tested? - Ran socket-level tests in `crates/nostream-relay/tests/handlers.rs`: - `cargo test -p nostream-relay --test handlers -- the_handler_scales_difficulty_above_floor_as_accepted_events_arrive` (passed) - `cargo test -p nostream-relay --test handlers` (67/67 passed) - Ran unit tests: - `cargo test -p nostream-relay --lib` (30/30 passed) - Ran formatting and lints: - `cargo fmt --all --check` - `cargo clippy -p nostream-relay --all-targets --all-features -- -D warnings` - Strict clippy with `-D clippy::unwrap_used -D clippy::expect_used` ## Types of changes - [x] Non-functional change (docs, style, minor refactor) - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) ## Checklist: - [x] My code follows the code style of this project. - [ ] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [x] I have read the **CONTRIBUTING** document. - [x] I have added tests to cover my code changes. - [x] I added a changeset, or this is docs-only and I added an empty changeset. - [x] All new and existing tests passed.
2 parents 781d8d3 + e92993f commit ead3997

3 files changed

Lines changed: 60 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
---
3+
4+
test(relay): pin the handler's PoW rate conversion at non-zero rates (#1263)

‎crates/nostream-relay/src/handlers/event.rs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,4 +735,27 @@ mod tests {
735735
20
736736
);
737737
}
738+
739+
#[test]
740+
fn required_difficulty_scales_with_rate_above_target() {
741+
let limits = EventLimits {
742+
pow: Some(AdaptivePowSettings {
743+
enabled: true,
744+
floor_bits: 16,
745+
ceiling_bits: 32,
746+
target_events_per_second: 50.0,
747+
period_ms: 60_000,
748+
wot_thresholds: None,
749+
}),
750+
..Default::default()
751+
};
752+
753+
// At rate <= target (50.0), required difficulty is the floor (16).
754+
assert_eq!(required_difficulty(Some(&limits), 0.0, None), 16);
755+
assert_eq!(required_difficulty(Some(&limits), 50.0, None), 16);
756+
757+
// At rate > target, difficulty scales up to ceiling (32).
758+
assert_eq!(required_difficulty(Some(&limits), 100.0, None), 32);
759+
assert_eq!(required_difficulty(Some(&limits), 187.8, None), 32);
760+
}
738761
}

‎crates/nostream-relay/tests/handlers.rs‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,39 @@ async fn the_advertised_minimum_difficulty_is_the_one_enforced() {
424424
);
425425
}
426426

427+
#[tokio::test]
428+
async fn the_handler_scales_difficulty_above_floor_as_accepted_events_arrive() {
429+
let configured = settings_with(event_limits(EventLimits {
430+
pow: Some(AdaptivePowSettings {
431+
enabled: true,
432+
floor_bits: 0,
433+
ceiling_bits: 16,
434+
target_events_per_second: 0.01,
435+
period_ms: 60_000,
436+
wot_thresholds: None,
437+
}),
438+
..Default::default()
439+
}));
440+
let server = start(Fixture::new(configured)).await;
441+
442+
let first = signed_event(&CLIENT_SECRET, 1, vec![], "first", NOW_SECS);
443+
assert_eq!(
444+
send_event(&server, &first).await,
445+
format!(r#"["OK","{}",true,""]"#, first.id),
446+
"first event with 0 difficulty is accepted at the zero floor"
447+
);
448+
449+
// The accepted event advanced context.pow. At target_events_per_second = 0.01,
450+
// one event in 60s yields ~0.01155 ev/s (> 0.01 target), scaling required difficulty to 3.
451+
let second = signed_event(&CLIENT_SECRET, 1, vec![], "second", NOW_SECS);
452+
let frame = send_event(&server, &second).await;
453+
let observed = nostream_core::pow::leading_zero_bits(second.id.as_bytes());
454+
assert!(
455+
frame.ends_with(&format!(r#"false,"pow: difficulty {observed}<3"]"#)),
456+
"second event is rejected because recorded load raised required difficulty above floor: {frame}"
457+
);
458+
}
459+
427460
#[tokio::test]
428461
async fn pubkey_whitelist_and_blacklist_block() {
429462
use nostream_core::SettingsPubkey;

0 commit comments

Comments
 (0)