1use std::collections::HashSet;
15use std::sync::{Arc, LazyLock, Mutex as StdMutex};
16
17use nostr_sdk::prelude::{Client, Event, Filter, Kind, PublicKey, RelayStatus, RelayUrl, SubscriptionId};
18use tokio::sync::mpsc::UnboundedSender;
19use tokio::sync::Mutex;
20
21use super::community::CommunityV2;
22use super::stream;
23use super::{derive, inbound};
24use crate::community::{CommunityId, ConcordProtocol, Epoch};
25use crate::event_handler::InboundEventHandler;
26use crate::state::SessionGuard;
27
28static V2_SUB_ID: LazyLock<Mutex<Option<SubscriptionId>>> = LazyLock::new(|| Mutex::new(None));
30static V2_POOLWIDE_SUB_ID: LazyLock<Mutex<Option<SubscriptionId>>> = LazyLock::new(|| Mutex::new(None));
32static V2_SUB_SET: LazyLock<Mutex<Vec<String>>> = LazyLock::new(|| Mutex::new(Vec::new()));
35static V2_SEEN_WRAPS: LazyLock<Mutex<HashSet<[u8; 32]>>> = LazyLock::new(|| Mutex::new(HashSet::new()));
43const SEEN_WRAPS_CAP: usize = 8192;
45static V2_FOLLOW_TX: LazyLock<StdMutex<Option<UnboundedSender<CommunityId>>>> = LazyLock::new(|| StdMutex::new(None));
55static V2_FOLLOW_PENDING: LazyLock<StdMutex<HashSet<[u8; 32]>>> = LazyLock::new(|| StdMutex::new(HashSet::new()));
57static V2_FOLLOW_LOCKS: LazyLock<StdMutex<std::collections::HashMap<[u8; 32], Arc<Mutex<()>>>>> =
64 LazyLock::new(|| StdMutex::new(std::collections::HashMap::new()));
65
66pub(crate) fn follow_lock(id: &CommunityId) -> Arc<Mutex<()>> {
68 V2_FOLLOW_LOCKS.lock().unwrap().entry(id.0).or_default().clone()
69}
70
71pub async fn subscription_id() -> Option<SubscriptionId> {
72 V2_SUB_ID.lock().await.clone()
73}
74
75pub async fn poolwide_subscription_id() -> Option<SubscriptionId> {
76 V2_POOLWIDE_SUB_ID.lock().await.clone()
77}
78
79pub async fn clear() {
82 *V2_SUB_ID.lock().await = None;
83 *V2_POOLWIDE_SUB_ID.lock().await = None;
84 V2_SUB_SET.lock().await.clear();
85 V2_SEEN_WRAPS.lock().await.clear();
86 *V2_FOLLOW_TX.lock().unwrap() = None;
89 V2_FOLLOW_PENDING.lock().unwrap().clear();
90 V2_FOLLOW_LOCKS.lock().unwrap().clear();
91 super::streamauth::clear();
94}
95
96pub fn plane_authors(communities: &[CommunityV2]) -> Vec<PublicKey> {
107 let mut out = Vec::new();
108 for c in communities {
109 out.push(derive::guestbook_group_key(&c.community_root, c.id(), c.root_epoch).pk());
110 out.push(control_author(c));
111 out.push(super::derive::dissolved_group_key(c.id()).pk());
113 for ch in &c.channels {
114 if ch.private && ch.key.is_none() {
118 continue;
119 }
120 let (secret, epoch) = c.channel_secret(ch);
121 out.push(derive::channel_group_key(&secret, &ch.id, epoch).pk());
122 }
123 out.extend(rekey_authors(c));
124 }
125 out.sort_by_key(|p| p.to_hex());
126 out.dedup();
127 out
128}
129
130pub(crate) fn control_author(c: &CommunityV2) -> PublicKey {
134 derive::control_group_key(&c.community_root, c.id(), c.root_epoch).pk()
135}
136
137pub(crate) fn rekey_authors(c: &CommunityV2) -> Vec<PublicKey> {
144 let mut out = vec![derive::base_rekey_group_key(&c.community_root, c.id(), Epoch(c.root_epoch.0.saturating_add(1))).pk()];
148 for ch in &c.channels {
149 if ch.private {
150 out.push(derive::channel_rekey_group_key(&c.community_root, &ch.id, Epoch(ch.epoch.0.saturating_add(1))).pk());
151 }
152 }
153 out
154}
155
156pub fn load_held_v2() -> Vec<CommunityV2> {
163 let ids = crate::db::community::list_community_ids().unwrap_or_default();
164 ids.iter()
165 .filter(|id| matches!(crate::db::community::community_protocol(id).ok().flatten(), Some(ConcordProtocol::V2)))
166 .filter(|id| !crate::db::community::get_community_dissolved(&crate::simd::hex::bytes_to_hex_32(&id.0)).unwrap_or(false))
167 .filter_map(|id| crate::db::community::load_community_v2(id).ok().flatten())
168 .collect()
169}
170
171pub async fn refresh_subscription(client: &Client) {
175 {
179 let communities = load_held_v2();
180 let mut relays: Vec<String> = communities.iter().flat_map(|c| c.relays.iter().cloned()).collect();
181 relays.sort();
182 relays.dedup();
183 if !relays.is_empty() {
184 for r in &relays {
186 let _ = client.pool().add_relay(r.as_str(), crate::community_relay_options()).await;
187 }
188 client.connect().await;
189 let wanted: Vec<RelayUrl> = relays.iter().filter_map(|r| RelayUrl::parse(r).ok()).collect();
193 for _ in 0..24 {
194 let pool = client.pool().all_relays().await;
195 if wanted.iter().any(|u| pool.get(u).map(|r| r.status() == RelayStatus::Connected).unwrap_or(false)) {
196 break;
197 }
198 tokio::time::sleep(std::time::Duration::from_millis(250)).await;
199 }
200 for c in &communities {
205 super::streamauth::register_community(c);
206 }
207 super::streamauth::prime_auth(client, &relays).await;
208 }
209 }
210
211 let mut sub_guard = V2_SUB_ID.lock().await;
219 let mut set_guard = V2_SUB_SET.lock().await;
220
221 let communities = load_held_v2();
222 let authors = plane_authors(&communities);
223 let mut relays: Vec<String> = communities.iter().flat_map(|c| c.relays.iter().cloned()).collect();
224 relays.sort();
225 relays.dedup();
226
227 let mut new_set: Vec<String> = authors.iter().map(|p| p.to_hex()).collect();
228 new_set.sort();
229
230 if sub_guard.is_some() && *set_guard == new_set && (authors.is_empty() || V2_POOLWIDE_SUB_ID.lock().await.is_some()) {
234 return; }
236 if let Some(old) = sub_guard.take() {
237 client.unsubscribe(&old).await;
238 }
239 *set_guard = new_set;
240
241 if authors.is_empty() {
242 if let Some(old_pw) = V2_POOLWIDE_SUB_ID.lock().await.take() {
243 client.unsubscribe(&old_pw).await;
244 }
245 return;
246 }
247
248 let filter = Filter::new()
249 .kinds([Kind::Custom(stream::KIND_WRAP), Kind::Custom(stream::KIND_WRAP_EPHEMERAL)])
250 .authors(authors)
251 .limit(0);
252
253 {
254 let mut pw = V2_POOLWIDE_SUB_ID.lock().await;
255 if let Some(old) = pw.take() {
256 client.unsubscribe(&old).await;
257 }
258 if let Ok(out) = client.subscribe(filter.clone(), None).await {
259 *pw = Some(out.val);
260 }
261 }
262 if let Ok(out) = client.subscribe_to(relays.iter().cloned(), filter, None).await {
263 *sub_guard = Some(out.val);
264 }
265}
266
267pub(crate) async fn resubscribe_relay(client: &Client, relay: &RelayUrl) {
275 let targeted = V2_SUB_ID.lock().await.clone();
276 let poolwide = V2_POOLWIDE_SUB_ID.lock().await.clone();
277 if targeted.is_none() && poolwide.is_none() {
278 return; }
280 let communities = load_held_v2();
281 let authors = plane_authors(&communities);
282 if authors.is_empty() {
283 return;
284 }
285 let filter = Filter::new()
286 .kinds([Kind::Custom(stream::KIND_WRAP), Kind::Custom(stream::KIND_WRAP_EPHEMERAL)])
287 .authors(authors)
288 .limit(0);
289 for id in [targeted, poolwide].into_iter().flatten() {
290 let _ = client.subscribe_with_id_to([relay.clone()], id, filter.clone(), None).await;
291 }
292}
293
294pub async fn dispatch_event(session: &SessionGuard, event: Event, handler: Arc<dyn InboundEventHandler>) {
299 let Some(my_pk) = crate::my_public_key() else {
300 return;
301 };
302 if !session.is_valid() {
303 return;
304 }
305 {
308 let mut seen = V2_SEEN_WRAPS.lock().await;
309 if !seen.insert(event.id.to_bytes()) {
310 return;
311 }
312 if seen.len() > SEEN_WRAPS_CAP {
313 let keep = event.id.to_bytes();
314 seen.clear();
315 seen.insert(keep);
316 }
317 }
318 let communities = load_held_v2();
319 for c in &communities {
320 match inbound::dispatch_wrap(&event, c, &my_pk, &*handler) {
321 inbound::DispatchedV2::NotOurs => continue,
322 inbound::DispatchedV2::Control { .. } | inbound::DispatchedV2::Rekey { .. } => {
327 enqueue_follow(c.id());
328 return;
329 }
330 inbound::DispatchedV2::Dissolved { community_id } => {
331 if crate::db::community::set_community_dissolved(&community_id).unwrap_or(false) {
336 handler.on_community_dissolved(&community_id);
337 if let Some(client) = crate::state::nostr_client() {
338 refresh_subscription(&client).await;
339 }
340 }
341 return;
342 }
343 inbound::DispatchedV2::Chat { channel_id, event } => {
350 if !session.is_valid() {
351 return;
352 }
353 match inbound::persist_chat_event(&event, &channel_id, &my_pk, session).await {
354 Some(inbound::ChatPersist::New(message)) => handler.on_community_message(&channel_id, &message, true),
355 Some(inbound::ChatPersist::Updated { message, .. }) => handler.on_community_update(&channel_id, &message.id, &message),
358 Some(inbound::ChatPersist::Removed(target_id)) => handler.on_community_removed(&channel_id, &target_id),
359 None => {}
360 }
361 return;
362 }
363 _ => return, }
365 }
366}
367
368pub(crate) fn follow_worker_running() -> bool {
372 V2_FOLLOW_TX.lock().unwrap().as_ref().map(|tx| !tx.is_closed()).unwrap_or(false)
373}
374
375pub(crate) fn enqueue_follow(id: &CommunityId) {
381 let mut pending = V2_FOLLOW_PENDING.lock().unwrap();
382 if !pending.insert(id.0) {
383 return; }
385 match V2_FOLLOW_TX.lock().unwrap().as_ref() {
386 Some(tx) if tx.send(*id).is_ok() => {}
387 _ => {
388 pending.remove(&id.0); }
390 }
391}
392
393pub(crate) fn spawn_follow_worker(handler: Arc<dyn InboundEventHandler>) {
398 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<CommunityId>();
399 *V2_FOLLOW_TX.lock().unwrap() = Some(tx);
400 V2_FOLLOW_PENDING.lock().unwrap().clear();
401 let session = SessionGuard::capture();
402 tokio::spawn(async move {
403 while let Some(id) = rx.recv().await {
404 if !session.is_valid() {
405 break;
406 }
407 V2_FOLLOW_PENDING.lock().unwrap().remove(&id.0);
410 follow_community(&session, &id, &*handler).await;
411 }
412 });
413}
414
415async fn follow_community(session: &SessionGuard, id: &CommunityId, handler: &dyn InboundEventHandler) {
422 let Some(client) = crate::state::nostr_client() else {
423 return;
424 };
425 let lock = follow_lock(id);
428 let _guard = lock.lock().await;
429 let community_id = crate::simd::hex::bytes_to_hex_32(&id.0);
430 let transport = crate::community::transport::LiveTransport::with_timeout(std::time::Duration::from_secs(12));
431
432 let Ok(Some(current)) = crate::db::community::load_community_v2(id) else {
434 return; };
436 match super::service::follow_rekeys(&transport, ¤t, session).await {
437 Ok(follow) if follow.dissolved => {
440 if !session.is_valid() {
441 return;
442 }
443 handler.on_community_dissolved(&community_id);
444 return;
445 }
446 Ok(follow) if follow.self_removed => {
447 if !session.is_valid() {
448 return;
449 }
450 let _ = crate::db::community::delete_community(&community_id);
451 refresh_subscription(&client).await;
452 handler.on_community_self_removed(&community_id);
453 return;
454 }
455 Ok(follow) if follow.updated.is_some() => {
456 if !session.is_valid() {
457 return;
458 }
459 refresh_subscription(&client).await;
460 handler.on_community_refreshed(&community_id);
461 }
462 Ok(_) => {}
463 Err(_) => return,
464 }
465
466 let Ok(Some(current)) = crate::db::community::load_community_v2(id) else {
468 return;
469 };
470 if let Ok(Some(_)) = super::service::follow_control(&transport, ¤t, session).await {
471 if !session.is_valid() {
472 return;
473 }
474 refresh_subscription(&client).await;
475 handler.on_community_refreshed(&community_id);
476 enqueue_follow(id);
482 }
483}
484
485#[cfg(test)]
486mod tests {
487 use super::*;
488 use super::super::control::{genesis, CommunityMetadata};
489 use crate::community::Epoch;
490 use nostr_sdk::prelude::Keys;
491
492 fn a_community(name: &str) -> CommunityV2 {
493 let owner = Keys::generate();
494 let g = genesis(&owner, CommunityMetadata { name: name.into(), ..Default::default() }, 1_000).unwrap();
495 CommunityV2::from_genesis(&g, name, None, vec!["wss://r".into()], 0)
496 }
497
498 #[test]
499 fn plane_authors_covers_the_dispatched_planes_only() {
500 let c = a_community("A");
501 let authors = plane_authors(std::slice::from_ref(&c));
502
503 let gb = derive::guestbook_group_key(&c.community_root, c.id(), c.root_epoch).pk();
506 let control = derive::control_group_key(&c.community_root, c.id(), c.root_epoch).pk();
507 let general = {
508 let (s, e) = c.channel_secret(&c.channels[0]);
509 derive::channel_group_key(&s, &c.channels[0].id, e).pk()
510 };
511 let next_base = derive::base_rekey_group_key(&c.community_root, c.id(), Epoch(1)).pk();
514 let dissolved = derive::dissolved_group_key(c.id()).pk();
516 assert!(
517 authors.contains(&gb)
518 && authors.contains(&control)
519 && authors.contains(&general)
520 && authors.contains(&next_base)
521 && authors.contains(&dissolved)
522 );
523 assert_eq!(authors.len(), 5, "guestbook + control + dissolved + chat + base-rekey planes are subscribed");
524 }
525
526 #[test]
527 fn plane_authors_is_deterministic_deduped_and_multi_community() {
528 let a = a_community("A");
529 let b = a_community("B");
530 let one = plane_authors(std::slice::from_ref(&a));
531 assert_eq!(plane_authors(std::slice::from_ref(&a)), one);
533 let two = plane_authors(&[a.clone(), b.clone()]);
535 assert_eq!(two.len(), one.len() * 2);
536 assert_eq!(plane_authors(&[b, a]), two);
538 }
539
540 #[tokio::test]
541 async fn dispatch_event_routes_a_v2_message_to_the_handler() {
542 use crate::community::transport::memory::MemoryRelay;
543 use crate::community::transport::{Query, Transport};
544 use crate::types::Message;
545 use std::sync::Mutex as StdMutex;
546
547 #[derive(Default)]
548 struct Recorder {
549 got: StdMutex<Vec<(String, String)>>,
550 }
551 impl InboundEventHandler for Recorder {
552 fn on_community_message(&self, chat_id: &str, msg: &Message, _new: bool) {
553 self.got.lock().unwrap().push((chat_id.to_string(), msg.content.clone()));
554 }
555 }
556
557 let _g = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
559 crate::db::close_database();
560 crate::db::clear_id_caches();
561 let tmp = tempfile::tempdir().unwrap();
562 let acct = {
563 const B: &[u8] = b"qpzry9x8gf2tvdw0s3jn54khce6mua7l";
564 let mut s = String::from("npub1");
565 for i in 0..58 {
566 s.push(B[(i * 5 + 1) % 32] as char);
567 }
568 s
569 };
570 std::fs::create_dir_all(tmp.path().join(&acct)).unwrap();
571 crate::db::set_app_data_dir(tmp.path().to_path_buf());
572 crate::db::set_current_account(acct.clone()).unwrap();
573 crate::db::init_database(&acct).unwrap();
574 let _ = crate::state::take_nostr_client();
575 let me = Keys::generate();
576 crate::state::MY_SECRET_KEY.store_from_keys(&me, &[]);
577 crate::state::set_my_public_key(me.public_key());
578
579 let relay = MemoryRelay::new();
583 let community = super::super::service::create_community(&relay, "Live", vec!["wss://r".into()], None).await.unwrap();
584 let general = community.channels[0].id;
585 let member = Keys::generate();
586 let group = derive::channel_group_key(&community.community_root, &general, community.root_epoch);
587 let rumor = super::super::chat::build_message_rumor(member.public_key(), &general, community.root_epoch, "live ping", None, &[], vec![], 5_000);
588 let (wrap, _) = super::super::chat::seal_chat_rumor(&rumor, &group, &member, nostr_sdk::prelude::Timestamp::from_secs(5), false).unwrap();
589 let _ = relay.publish(&wrap, &community.relays).await;
590 let q = Query { kinds: vec![stream::KIND_WRAP], authors: vec![group.pk_hex()], ..Default::default() };
591 let wrap = relay.fetch(&q, &community.relays).await.unwrap().into_iter().find(|w| w.pubkey == group.pk()).unwrap();
592
593 let rec = Arc::new(Recorder::default());
599 let session = SessionGuard::capture();
600 crate::community::v2::realtime::clear().await; dispatch_event(&session, wrap.clone(), rec.clone()).await;
602 dispatch_event(&session, wrap, rec.clone()).await;
603
604 let got = rec.got.lock().unwrap();
605 assert_eq!(got.len(), 1, "a re-delivered wrap fires the handler exactly once");
606 assert_eq!(got[0].1, "live ping");
607 assert_eq!(got[0].0, crate::simd::hex::bytes_to_hex_32(&general.0));
608 }
609
610 #[tokio::test]
611 async fn follow_queue_coalesces_a_burst_and_re_enqueues_after_processing() {
612 let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<CommunityId>();
615 *V2_FOLLOW_TX.lock().unwrap() = Some(tx);
616 V2_FOLLOW_PENDING.lock().unwrap().clear();
617
618 let id = CommunityId([0x11; 32]);
619 enqueue_follow(&id);
621 enqueue_follow(&id);
622 enqueue_follow(&id);
623 assert_eq!(rx.recv().await, Some(id), "first trigger queues a follow");
624 assert!(rx.try_recv().is_err(), "the burst coalesced to exactly one");
625
626 V2_FOLLOW_PENDING.lock().unwrap().remove(&id.0);
629 enqueue_follow(&id);
630 assert_eq!(rx.recv().await, Some(id), "a trigger after processing re-queues");
631
632 let id2 = CommunityId([0x22; 32]);
634 enqueue_follow(&id2);
635 assert_eq!(rx.recv().await, Some(id2));
636
637 *V2_FOLLOW_TX.lock().unwrap() = None;
638 V2_FOLLOW_PENDING.lock().unwrap().clear();
639 }
640
641 #[tokio::test]
642 async fn a_dissolved_community_honors_no_new_events_and_fires_death_once() {
643 use super::super::service;
644 use crate::community::transport::memory::MemoryRelay;
645 use crate::community::transport::Transport;
646 use crate::types::Message;
647 use std::sync::Mutex as StdMutex;
648
649 #[derive(Default)]
650 struct Recorder {
651 messages: StdMutex<Vec<String>>,
652 deaths: StdMutex<Vec<String>>,
653 }
654 impl InboundEventHandler for Recorder {
655 fn on_community_message(&self, _chat: &str, msg: &Message, _new: bool) {
656 self.messages.lock().unwrap().push(msg.content.clone());
657 }
658 fn on_community_dissolved(&self, community_id: &str) {
659 self.deaths.lock().unwrap().push(community_id.to_string());
660 }
661 }
662
663 let _g = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
664 crate::db::close_database();
665 crate::db::clear_id_caches();
666 let tmp = tempfile::tempdir().unwrap();
667 let acct = {
668 const B: &[u8] = b"qpzry9x8gf2tvdw0s3jn54khce6mua7l";
669 let mut s = String::from("npub1");
670 for i in 0..58 {
671 s.push(B[(i * 3 + 2) % 32] as char);
672 }
673 s
674 };
675 std::fs::create_dir_all(tmp.path().join(&acct)).unwrap();
676 crate::db::set_app_data_dir(tmp.path().to_path_buf());
677 crate::db::set_current_account(acct.clone()).unwrap();
678 crate::db::init_database(&acct).unwrap();
679 let _ = crate::state::take_nostr_client();
680 let me = Keys::generate();
681 crate::state::MY_SECRET_KEY.store_from_keys(&me, &[]);
682 crate::state::set_my_public_key(me.public_key());
683
684 let relay = MemoryRelay::new();
685 let community = service::create_community(&relay, "Doomed", vec!["wss://r".into()], None).await.unwrap();
686 let general = community.channels[0].id;
687
688 let rumor = super::super::dissolution::dissolved_tombstone_rumor(me.public_key(), community.id(), 8_000);
693 let tombstone = super::super::dissolution::seal_dissolved(&rumor, community.id(), &me, nostr_sdk::prelude::Timestamp::from_secs(8_000)).unwrap();
694 let _ = relay.publish(&tombstone, &community.relays).await;
695 assert!(!crate::db::community::get_community_dissolved(&crate::simd::hex::bytes_to_hex_32(&community.id().0)).unwrap(), "not yet locally sealed");
696
697 let rec = Arc::new(Recorder::default());
698 let session = SessionGuard::capture();
699 clear().await;
700 dispatch_event(&session, tombstone.clone(), rec.clone()).await;
703 dispatch_event(&session, tombstone, rec.clone()).await;
704 assert!(crate::db::community::get_community_dissolved(&crate::simd::hex::bytes_to_hex_32(&community.id().0)).unwrap());
705
706 let member = Keys::generate();
710 let cgroup = derive::channel_group_key(&community.community_root, &general, community.root_epoch);
711 let rumor = super::super::chat::build_message_rumor(member.public_key(), &general, community.root_epoch, "into the grave", None, &[], vec![], 9_000);
712 let (mw, _) = super::super::chat::seal_chat_rumor(&rumor, &cgroup, &member, nostr_sdk::prelude::Timestamp::from_secs(9), false).unwrap();
713 dispatch_event(&session, mw, rec.clone()).await;
714
715 assert_eq!(rec.deaths.lock().unwrap().len(), 1, "death is announced exactly once");
716 assert!(rec.messages.lock().unwrap().is_empty(), "a post-tombstone message is never honored (CORD-02 §9)");
717 }
718
719 #[test]
720 fn a_private_channel_subscribes_to_its_own_chat_plane() {
721 let mut c = a_community("Priv");
722 c.channels.push(super::super::community::ChannelV2 {
723 id: crate::community::ChannelId([0x33; 32]),
724 name: "mods".into(),
725 private: true,
726 key: Some([0x44; 32]),
727 epoch: Epoch(1),
728 });
729 let authors = plane_authors(std::slice::from_ref(&c));
730 let priv_chat = derive::channel_group_key(&[0x44; 32], &c.channels[1].id, Epoch(1)).pk();
732 assert!(authors.contains(&priv_chat), "a private channel subscribes to its own chat plane");
733 let next_rekey = derive::channel_rekey_group_key(&c.community_root, &c.channels[1].id, Epoch(2)).pk();
736 assert!(authors.contains(&next_rekey), "a private channel's next rekey plane is subscribed");
737 }
738}