From 4943cf5b1107649c74ad7dce1ecd76eb2fb762cd Mon Sep 17 00:00:00 2001 From: Kurt Roeckx Date: Sun, 15 Aug 2021 15:41:48 +0200 Subject: [PATCH] Get the auth chain non-recursive --- src/server_server.rs | 78 ++++++++++++++++++++------------------------ 1 file changed, 36 insertions(+), 42 deletions(-) diff --git a/src/server_server.rs b/src/server_server.rs index a4bfda5..c1ede52 100644 --- a/src/server_server.rs +++ b/src/server_server.rs @@ -1856,28 +1856,51 @@ fn get_auth_chain( ) -> Result + '_> { let mut full_auth_chain = HashSet::new(); - let starting_events = starting_events + let mut events = starting_events .iter() .map(|id| { db.rooms .get_or_create_shorteventid(id, &db.globals) - .map(|s| (s, id)) + .map(|s| (s, id.clone())) }) .collect::>>()?; let mut cache = db.rooms.auth_chain_cache(); - for (sevent_id, event_id) in starting_events { - if let Some(cached) = cache.get_mut(&sevent_id) { - full_auth_chain.extend(cached.iter().cloned()); - } else { - drop(cache); - let mut auth_chain = HashSet::new(); - get_auth_chain_recursive(&event_id, &mut auth_chain, db)?; - cache = db.rooms.auth_chain_cache(); - cache.insert(sevent_id, auth_chain.clone()); - full_auth_chain.extend(auth_chain); - }; + while events.len() != 0 { + let mut new_events = Vec::new(); + for (sevent_id, event_id) in &events { + if let Some(cached) = cache.get_mut(&sevent_id) { + full_auth_chain.extend(cached.iter().cloned()); + } else { + drop(cache); + let mut auth_chain = HashSet::new(); + let r = db.rooms.get_pdu(&event_id); + match r { + Ok(Some(pdu)) => { + for auth_event in &pdu.auth_events { + let sauthevent = db + .rooms + .get_or_create_shorteventid(auth_event, &db.globals)?; + if !auth_chain.contains(&sauthevent) { + auth_chain.insert(sauthevent); + new_events.push((sauthevent, auth_event.clone())); + } + } + } + Ok(None) => { + warn!("Could not find pdu mentioned in auth events."); + } + Err(e) => { + warn!("Could not load event in auth chain: {}", e); + } + } + cache = db.rooms.auth_chain_cache(); + cache.insert(*sevent_id, auth_chain.clone()); + full_auth_chain.extend(auth_chain); + }; + } + events = new_events; } drop(cache); @@ -1887,35 +1910,6 @@ fn get_auth_chain( .filter_map(move |sid| db.rooms.get_eventid_from_short(sid).ok())) } -fn get_auth_chain_recursive( - event_id: &EventId, - found: &mut HashSet, - db: &Database, -) -> Result<()> { - let r = db.rooms.get_pdu(&event_id); - match r { - Ok(Some(pdu)) => { - for auth_event in &pdu.auth_events { - let sauthevent = db - .rooms - .get_or_create_shorteventid(auth_event, &db.globals)?; - if !found.contains(&sauthevent) { - found.insert(sauthevent); - get_auth_chain_recursive(&auth_event, found, db)?; - } - } - } - Ok(None) => { - warn!("Could not find pdu mentioned in auth events."); - } - Err(e) => { - warn!("Could not load event in auth chain: {}", e); - } - } - - Ok(()) -} - #[cfg_attr( feature = "conduit_bin", get("/_matrix/federation/v1/event/<_>", data = "")