Skip to content

Commit ba317a5

Browse files
TheSentinel454Elrondclaude
committed
test(relay): check global fan-out against a full filter scan
Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Luke Tornquist <[email protected]>
1 parent 1750e8f commit ba317a5

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,100 @@ mod tests {
20432043
assert!(registry.global_selector_index.is_empty());
20442044
}
20452045

2046+
/// `None`, an empty list, or a random subset of `pool`.
2047+
fn random_constraint<T: Clone>(
2048+
next: &mut impl FnMut(usize) -> usize,
2049+
pool: &[T],
2050+
) -> Option<Vec<T>> {
2051+
match next(4) {
2052+
0 => None,
2053+
1 => Some(Vec::new()),
2054+
_ => Some(pool.iter().filter(|_| next(2) == 0).cloned().collect()),
2055+
}
2056+
}
2057+
2058+
/// Indexed global fan-out delivers to exactly the subscriptions a full
2059+
/// `filters_match` scan selects, across random filter sets and `p`-tag
2060+
/// shapes, so the index's tag reading can't drift from the matcher's.
2061+
#[test]
2062+
fn global_fan_out_matches_a_full_filter_scan() {
2063+
// xorshift64*, seeded, so a failing case reproduces.
2064+
let mut state = 0x9E37_79B9_7F4A_7C15_u64;
2065+
let mut next = move |n: usize| -> usize {
2066+
state ^= state >> 12;
2067+
state ^= state << 25;
2068+
state ^= state >> 27;
2069+
(state.wrapping_mul(0x2545_F491_4F6C_DD1D) >> 33) as usize % n
2070+
};
2071+
let kinds = [
2072+
Kind::TextNote,
2073+
Kind::Reaction,
2074+
Kind::Custom(buzz_core::kind::KIND_PRESENCE_UPDATE as u16),
2075+
];
2076+
let authors: Vec<Keys> = (0..3).map(|_| Keys::generate()).collect();
2077+
let author_keys: Vec<PublicKey> = authors.iter().map(Keys::public_key).collect();
2078+
let p_values = ["a".repeat(64), "b".repeat(64), "not-hex".to_string()];
2079+
2080+
let registry = SubscriptionRegistry::new();
2081+
let mut subscriptions = Vec::new();
2082+
for i in 0..60 {
2083+
let filters: Vec<Filter> = (0..=next(3))
2084+
.map(|_| {
2085+
let mut filter = Filter::new();
2086+
if let Some(kinds) = random_constraint(&mut next, &kinds) {
2087+
filter = filter.kinds(kinds);
2088+
}
2089+
if let Some(authors) = random_constraint(&mut next, &author_keys) {
2090+
filter = filter.authors(authors);
2091+
}
2092+
if let Some(values) = random_constraint(&mut next, &p_values) {
2093+
filter = filter.custom_tags(p_tag(), values);
2094+
}
2095+
filter
2096+
})
2097+
.collect();
2098+
let (conn_id, sub_id) = (Uuid::new_v4(), format!("sub-{i}"));
2099+
registry.register(conn_id, sub_id.clone(), filters.clone(), None);
2100+
subscriptions.push((conn_id, sub_id, filters));
2101+
}
2102+
assert!(!registry.global_selector_index.is_empty());
2103+
2104+
for _ in 0..2_000 {
2105+
let tags: Vec<Tag> = (0..next(4))
2106+
.map(|_| {
2107+
let value = p_values[next(p_values.len())].as_str();
2108+
let parts = match next(5) {
2109+
0 => vec!["p", value],
2110+
1 => vec!["p", value, "wss://relay.example"],
2111+
2 => vec!["P", value],
2112+
3 => vec!["p"],
2113+
_ => vec!["e", value],
2114+
};
2115+
Tag::parse(parts).expect("valid tag")
2116+
})
2117+
.collect();
2118+
let event = EventBuilder::new(kinds[next(kinds.len())], "x")
2119+
.tags(tags)
2120+
.sign_with_keys(&authors[next(authors.len())])
2121+
.expect("sign");
2122+
let event = StoredEvent::with_received_at(event, Utc::now(), None, true);
2123+
2124+
let expected: HashSet<(ConnId, SubId)> = subscriptions
2125+
.iter()
2126+
.filter(|(_, _, filters)| filters_match(filters, &event))
2127+
.map(|(conn_id, sub_id, _)| (*conn_id, sub_id.clone()))
2128+
.collect();
2129+
let delivered = registry.fan_out(&event);
2130+
let delivered_set: HashSet<_> = delivered.iter().cloned().collect();
2131+
assert_eq!(delivered.len(), delivered_set.len(), "duplicate delivery");
2132+
assert_eq!(
2133+
delivered_set, expected,
2134+
"kind {:?}, tags {:?}",
2135+
event.event.kind, event.event.tags
2136+
);
2137+
}
2138+
}
2139+
20462140
#[test]
20472141
fn test_global_selector_index_kindless_filters_skip_wildcard_scan() {
20482142
let registry = SubscriptionRegistry::new();

0 commit comments

Comments
 (0)