Skip to content

Commit 2f9c9b1

Browse files
author
Carl
committed
fix(relay): clarify snapshot event-array byte budget
Signed-off-by: Carl <32a2e2c9d428ee08902cab75d956da2c1d235a22d4766b0dd4138bf6e2e5db1d@buzz.block.builderlab.xyz>
1 parent bfef17f commit 2f9c9b1

3 files changed

Lines changed: 61 additions & 8 deletions

File tree

‎crates/buzz-relay/src/api/bridge/read_state_snapshot.rs‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,57 @@ mod postgres_tests {
342342
assert_ne!(body["complete"], true);
343343
}
344344

345+
#[tokio::test]
346+
#[ignore = "requires Postgres"]
347+
async fn read_state_snapshot_router_advertised_event_array_byte_boundary() {
348+
let (state, host, community, key) = setup().await;
349+
let info = crate::nip11::nip11_document(&state, &host).await;
350+
let descriptor = info.read_state_snapshot.unwrap();
351+
let budget = descriptor["max_event_array_bytes"].as_u64().unwrap() as usize;
352+
assert_eq!(budget, buzz_db::read_state::MAX_SNAPSHOT_BYTES);
353+
assert!(descriptor.get("max_bytes").is_none());
354+
355+
// ASCII content adds exactly one encoded byte per character; timestamp,
356+
// event id and signature widths stay fixed across these signed events.
357+
let make = |content: String, timestamp| {
358+
EventBuilder::new(Kind::Custom(30078), content)
359+
.tags([Tag::identifier("byte-boundary")])
360+
.custom_created_at(nostr::Timestamp::from(timestamp))
361+
.sign_with_keys(&key)
362+
.unwrap()
363+
};
364+
let overhead = serde_json::to_vec(&json!([make(String::new(), 1789090000)]))
365+
.unwrap()
366+
.len();
367+
for extra in 0..=1 {
368+
let event = make(
369+
"x".repeat(budget - overhead + extra),
370+
1789090001 + extra as u64,
371+
);
372+
assert_eq!(
373+
serde_json::to_vec(&json!([event])).unwrap().len(),
374+
budget + extra
375+
);
376+
state
377+
.db
378+
.replace_parameterized_event(community, &event, "byte-boundary", None)
379+
.await
380+
.unwrap();
381+
let (status, body) = request(state.clone(), &host, Some(&key), filter(&key)).await;
382+
if extra == 0 {
383+
assert_eq!(status, StatusCode::OK);
384+
assert_eq!(body["complete"], true);
385+
assert_eq!(serde_json::to_vec(&body["events"]).unwrap().len(), budget);
386+
// Discovery bounds the array, not the larger success envelope.
387+
assert!(serde_json::to_vec(&body).unwrap().len() > budget);
388+
} else {
389+
assert_eq!(status, StatusCode::PAYLOAD_TOO_LARGE);
390+
assert_ne!(body["complete"], true);
391+
assert!(body.get("events").is_none());
392+
}
393+
}
394+
}
395+
345396
#[tokio::test]
346397
#[ignore = "requires Postgres"]
347398
async fn read_state_snapshot_router_beyond_page_cap_and_overflow() {

‎crates/buzz-relay/src/nip11.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ pub(crate) async fn nip11_document(state: &crate::state::AppState, raw_host: &st
302302
"version": 1,
303303
"community_id": tenant.community().as_uuid(),
304304
"max_events": buzz_db::read_state::MAX_SNAPSHOT_EVENTS,
305-
"max_bytes": buzz_db::read_state::MAX_SNAPSHOT_BYTES,
305+
"max_event_array_bytes": buzz_db::read_state::MAX_SNAPSHOT_BYTES,
306306
}));
307307
}
308308
let tenant_host = if state.config.push_enabled {

‎docs/nips/NIP-RS.md‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,15 @@ conformance properties above.
388388

389389
Discovery: obtain NIP-11 from the configured, trusted relay origin. Its optional
390390
`read_state_snapshot` descriptor contains `version: 1`, `community_id` (canonical
391-
UUID string), `max_events: 4096`, and `max_bytes: 8388608`. The community identifier
392-
is resolved from that request's Host through the active community map; it is not
393-
supplied by the client or derived from its key. An unresolved host omits the
394-
descriptor. This is TLS/origin-bound discovery, not an independently signed global
395-
community identity. Clients MUST bind discovery and queries to the same trusted
396-
relay origin and community session, and fence asynchronous results on identity or
397-
community changes.
391+
UUID string), `max_events: 4096`, and `max_event_array_bytes: 8388608`. The byte
392+
budget applies only to the compact JSON `events` array, not the complete HTTP
393+
response; clients MUST allow additional space for the success envelope metadata.
394+
The community identifier is resolved from that request's Host through the active
395+
community map; it is not supplied by the client or derived from its key. An
396+
unresolved host omits the descriptor. This is TLS/origin-bound discovery, not an
397+
independently signed global community identity. Clients MUST bind discovery and
398+
queries to the same trusted relay origin and community session, and fence
399+
asynchronous results on identity or community changes.
398400

399401
Request: NIP-98 authenticated `POST /query` with exactly one raw filter:
400402

0 commit comments

Comments
 (0)