Browse Source

Use Arc<EventId> in place of most EventIds

merge-requests/169/head
Devin Ragotzy 4 years ago
parent
commit
0e717a6c70
  1. 36
      Cargo.lock
  2. 2
      Cargo.toml
  3. 2
      src/client_server/directory.rs
  4. 1
      src/client_server/profile.rs
  5. 4
      src/client_server/sync.rs
  6. 2
      src/database/account_data.rs
  7. 43
      src/database/rooms.rs
  8. 70
      src/server_server.rs

36
Cargo.lock generated

@ -2045,7 +2045,7 @@ dependencies = [ @@ -2045,7 +2045,7 @@ dependencies = [
[[package]]
name = "ruma"
version = "0.3.0"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"assign",
"js_int",
@ -2066,7 +2066,7 @@ dependencies = [ @@ -2066,7 +2066,7 @@ dependencies = [
[[package]]
name = "ruma-api"
version = "0.18.3"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"bytes",
"http",
@ -2082,7 +2082,7 @@ dependencies = [ @@ -2082,7 +2082,7 @@ dependencies = [
[[package]]
name = "ruma-api-macros"
version = "0.18.3"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"proc-macro-crate",
"proc-macro2",
@ -2093,7 +2093,7 @@ dependencies = [ @@ -2093,7 +2093,7 @@ dependencies = [
[[package]]
name = "ruma-appservice-api"
version = "0.4.0"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"ruma-api",
"ruma-common",
@ -2107,7 +2107,7 @@ dependencies = [ @@ -2107,7 +2107,7 @@ dependencies = [
[[package]]
name = "ruma-client-api"
version = "0.12.2"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"assign",
"bytes",
@ -2127,7 +2127,7 @@ dependencies = [ @@ -2127,7 +2127,7 @@ dependencies = [
[[package]]
name = "ruma-common"
version = "0.6.0"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"indexmap",
"js_int",
@ -2142,7 +2142,7 @@ dependencies = [ @@ -2142,7 +2142,7 @@ dependencies = [
[[package]]
name = "ruma-events"
version = "0.24.4"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"indoc",
"js_int",
@ -2158,7 +2158,7 @@ dependencies = [ @@ -2158,7 +2158,7 @@ dependencies = [
[[package]]
name = "ruma-events-macros"
version = "0.24.4"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"proc-macro-crate",
"proc-macro2",
@ -2169,7 +2169,7 @@ dependencies = [ @@ -2169,7 +2169,7 @@ dependencies = [
[[package]]
name = "ruma-federation-api"
version = "0.3.0"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"js_int",
"ruma-api",
@ -2184,7 +2184,7 @@ dependencies = [ @@ -2184,7 +2184,7 @@ dependencies = [
[[package]]
name = "ruma-identifiers"
version = "0.20.0"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"paste",
"rand 0.8.4",
@ -2198,7 +2198,7 @@ dependencies = [ @@ -2198,7 +2198,7 @@ dependencies = [
[[package]]
name = "ruma-identifiers-macros"
version = "0.20.0"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"quote",
"ruma-identifiers-validation",
@ -2208,7 +2208,7 @@ dependencies = [ @@ -2208,7 +2208,7 @@ dependencies = [
[[package]]
name = "ruma-identifiers-validation"
version = "0.5.0"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"thiserror",
]
@ -2216,7 +2216,7 @@ dependencies = [ @@ -2216,7 +2216,7 @@ dependencies = [
[[package]]
name = "ruma-identity-service-api"
version = "0.3.0"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"js_int",
"ruma-api",
@ -2229,7 +2229,7 @@ dependencies = [ @@ -2229,7 +2229,7 @@ dependencies = [
[[package]]
name = "ruma-push-gateway-api"
version = "0.3.0"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"js_int",
"ruma-api",
@ -2244,7 +2244,7 @@ dependencies = [ @@ -2244,7 +2244,7 @@ dependencies = [
[[package]]
name = "ruma-serde"
version = "0.5.0"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"bytes",
"form_urlencoded",
@ -2258,7 +2258,7 @@ dependencies = [ @@ -2258,7 +2258,7 @@ dependencies = [
[[package]]
name = "ruma-serde-macros"
version = "0.5.0"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"proc-macro-crate",
"proc-macro2",
@ -2269,7 +2269,7 @@ dependencies = [ @@ -2269,7 +2269,7 @@ dependencies = [
[[package]]
name = "ruma-signatures"
version = "0.9.0"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"base64 0.13.0",
"ed25519-dalek",
@ -2286,7 +2286,7 @@ dependencies = [ @@ -2286,7 +2286,7 @@ dependencies = [
[[package]]
name = "ruma-state-res"
version = "0.3.0"
source = "git+https://github.com/timokoesters/ruma?rev=2215049b60a1c3358f5a52215adf1e7bb88619a1#2215049b60a1c3358f5a52215adf1e7bb88619a1"
source = "git+https://github.com/DevinR528/ruma?rev=c7860fcb89dbde636e2c83d0636934fb9924f40c#c7860fcb89dbde636e2c83d0636934fb9924f40c"
dependencies = [
"itertools 0.10.1",
"js_int",

2
Cargo.toml

@ -19,7 +19,7 @@ rocket = { version = "0.5.0-rc.1", features = ["tls"] } # Used to handle request @@ -19,7 +19,7 @@ rocket = { version = "0.5.0-rc.1", features = ["tls"] } # Used to handle request
# Used for matrix spec type definitions and helpers
#ruma = { git = "https://github.com/ruma/ruma", rev = "f5ab038e22421ed338396ece977b6b2844772ced", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }
ruma = { git = "https://github.com/timokoesters/ruma", rev = "2215049b60a1c3358f5a52215adf1e7bb88619a1", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }
ruma = { git = "https://github.com/DevinR528/ruma", rev = "c7860fcb89dbde636e2c83d0636934fb9924f40c", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }
#ruma = { path = "../ruma/crates/ruma", features = ["compat", "rand", "appservice-api-c", "client-api", "federation-api", "push-gateway-api-c", "state-res", "unstable-pre-spec", "unstable-exhaustive-types"] }
# Used for long polling and federation sender, should be the same as rocket::tokio

2
src/client_server/directory.rs

@ -234,7 +234,7 @@ pub async fn get_public_rooms_filtered_helper( @@ -234,7 +234,7 @@ pub async fn get_public_rooms_filtered_helper(
Error::bad_database("Invalid room name event in database.")
})?
.name
.map(|n| n.to_owned().into()))
.map(|n| n.to_owned()))
})?,
num_joined_members: db
.rooms

1
src/client_server/profile.rs

@ -166,6 +166,7 @@ pub async fn set_avatar_url_route( @@ -166,6 +166,7 @@ pub async fn set_avatar_url_route(
db.users.set_blurhash(&sender_user, body.blurhash.clone())?;
// Send a new membership event and presence update into all joined rooms
#[allow(clippy::needless_collect)] // Iterator<..> is not Send so fails to compile
let all_joined_rooms = db.rooms.rooms_joined(&sender_user).collect::<Vec<_>>();
for (pdu_builder, room_id) in all_joined_rooms

4
src/client_server/sync.rs

@ -403,7 +403,7 @@ async fn sync_helper( @@ -403,7 +403,7 @@ async fn sync_helper(
.filter(|(_, id)| {
!timeline_pdus
.iter()
.any(|(_, timeline_pdu)| timeline_pdu.event_id == **id)
.any(|(_, timeline_pdu)| timeline_pdu.event_id == ***id)
})
.map(|(_, id)| db.rooms.get_pdu(id))
.filter_map(|r| r.ok().flatten())
@ -472,7 +472,7 @@ async fn sync_helper( @@ -472,7 +472,7 @@ async fn sync_helper(
})
.map_or(MembershipState::Leave, |member| member.membership);
let user_id = UserId::try_from(user_id.clone())
let user_id = UserId::try_from(user_id)
.map_err(|_| Error::bad_database("Invalid UserId in member PDU."))?;
match (since_membership, current_membership) {

2
src/database/account_data.rs

@ -40,7 +40,7 @@ impl AccountData { @@ -40,7 +40,7 @@ impl AccountData {
roomuserdataid.push(0xff);
roomuserdataid.extend_from_slice(&event_type.as_bytes());
let mut key = prefix.clone();
let mut key = prefix;
key.extend_from_slice(event_type.as_bytes());
let json = serde_json::to_value(data).expect("all types here can be serialized"); // TODO: maybe add error handling

43
src/database/rooms.rs

@ -92,7 +92,7 @@ pub struct Rooms { @@ -92,7 +92,7 @@ pub struct Rooms {
pub(super) pdu_cache: Mutex<LruCache<EventId, Arc<PduEvent>>>,
pub(super) auth_chain_cache: Mutex<LruCache<Vec<u64>, HashSet<u64>>>,
pub(super) shorteventid_cache: Mutex<LruCache<u64, EventId>>,
pub(super) shorteventid_cache: Mutex<LruCache<u64, Arc<EventId>>>,
pub(super) eventidshort_cache: Mutex<LruCache<EventId, u64>>,
pub(super) statekeyshort_cache: Mutex<LruCache<(EventType, String), u64>>,
pub(super) shortstatekey_cache: Mutex<LruCache<u64, (EventType, String)>>,
@ -113,7 +113,7 @@ impl Rooms { @@ -113,7 +113,7 @@ impl Rooms {
/// Builds a StateMap by iterating over all keys that start
/// with state_hash, this gives the full state for the given state_hash.
#[tracing::instrument(skip(self))]
pub fn state_full_ids(&self, shortstatehash: u64) -> Result<BTreeMap<u64, EventId>> {
pub fn state_full_ids(&self, shortstatehash: u64) -> Result<BTreeMap<u64, Arc<EventId>>> {
let full_state = self
.load_shortstatehash_info(shortstatehash)?
.pop()
@ -164,7 +164,7 @@ impl Rooms { @@ -164,7 +164,7 @@ impl Rooms {
shortstatehash: u64,
event_type: &EventType,
state_key: &str,
) -> Result<Option<EventId>> {
) -> Result<Option<Arc<EventId>>> {
let shortstatekey = match self.get_shortstatekey(event_type, state_key)? {
Some(s) => s,
None => return Ok(None),
@ -377,7 +377,7 @@ impl Rooms { @@ -377,7 +377,7 @@ impl Rooms {
self.save_state_from_diff(
new_shortstatehash,
statediffnew.clone(),
statediffremoved.clone(),
statediffremoved,
2, // every state change is 2 event changes on average
states_parents,
)?;
@ -520,7 +520,7 @@ impl Rooms { @@ -520,7 +520,7 @@ impl Rooms {
pub fn parse_compressed_state_event(
&self,
compressed_event: CompressedStateEvent,
) -> Result<(u64, EventId)> {
) -> Result<(u64, Arc<EventId>)> {
Ok((
utils::u64_from_bytes(&compressed_event[0..size_of::<u64>()])
.expect("bytes have right length"),
@ -601,7 +601,7 @@ impl Rooms { @@ -601,7 +601,7 @@ impl Rooms {
return Ok(());
}
if parent_states.len() == 0 {
if parent_states.is_empty() {
// There is no parent layer, create a new state
let mut value = 0_u64.to_be_bytes().to_vec(); // 0 means no parent
for new in &statediffnew {
@ -836,7 +836,7 @@ impl Rooms { @@ -836,7 +836,7 @@ impl Rooms {
}
#[tracing::instrument(skip(self))]
pub fn get_eventid_from_short(&self, shorteventid: u64) -> Result<EventId> {
pub fn get_eventid_from_short(&self, shorteventid: u64) -> Result<Arc<EventId>> {
if let Some(id) = self
.shorteventid_cache
.lock()
@ -851,10 +851,12 @@ impl Rooms { @@ -851,10 +851,12 @@ impl Rooms {
.get(&shorteventid.to_be_bytes())?
.ok_or_else(|| Error::bad_database("Shorteventid does not exist"))?;
let event_id = EventId::try_from(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("EventID in shorteventid_eventid is invalid unicode.")
})?)
.map_err(|_| Error::bad_database("EventId in shorteventid_eventid is invalid."))?;
let event_id = Arc::new(
EventId::try_from(utils::string_from_bytes(&bytes).map_err(|_| {
Error::bad_database("EventID in shorteventid_eventid is invalid unicode.")
})?)
.map_err(|_| Error::bad_database("EventId in shorteventid_eventid is invalid."))?,
);
self.shorteventid_cache
.lock()
@ -926,7 +928,7 @@ impl Rooms { @@ -926,7 +928,7 @@ impl Rooms {
room_id: &RoomId,
event_type: &EventType,
state_key: &str,
) -> Result<Option<EventId>> {
) -> Result<Option<Arc<EventId>>> {
if let Some(current_shortstatehash) = self.current_shortstatehash(room_id)? {
self.state_get_id(current_shortstatehash, event_type, state_key)
} else {
@ -1675,8 +1677,8 @@ impl Rooms { @@ -1675,8 +1677,8 @@ impl Rooms {
};
self.save_state_from_diff(
shortstatehash,
statediffnew.clone(),
statediffremoved.clone(),
statediffnew,
statediffremoved,
1_000_000, // high number because no state will be based on this one
states_parents,
)?;
@ -1737,7 +1739,7 @@ impl Rooms { @@ -1737,7 +1739,7 @@ impl Rooms {
let mut statediffremoved = HashSet::new();
if let Some(replaces) = replaces {
statediffremoved.insert(replaces.clone());
statediffremoved.insert(*replaces);
}
self.save_state_from_diff(
@ -2360,16 +2362,15 @@ impl Rooms { @@ -2360,16 +2362,15 @@ impl Rooms {
self.roomuserid_leftcount.remove(&roomuser_id)?;
}
member::MembershipState::Leave | member::MembershipState::Ban => {
if update_joined_count {
if self
if update_joined_count
&& self
.room_members(room_id)
.chain(self.room_members_invited(room_id))
.filter_map(|r| r.ok())
.all(|u| u.server_name() != user_id.server_name())
{
self.roomserverids.remove(&roomserver_id)?;
self.serverroomids.remove(&serverroom_id)?;
}
{
self.roomserverids.remove(&roomserver_id)?;
self.serverroomids.remove(&serverroom_id)?;
}
self.userroomid_leftstate.insert(
&userroom_id,

70
src/server_server.rs

@ -893,7 +893,12 @@ pub async fn handle_incoming_pdu<'a>( @@ -893,7 +893,12 @@ pub async fn handle_incoming_pdu<'a>(
// 9. Fetch any missing prev events doing all checks listed here starting at 1. These are timeline events
let mut graph = HashMap::new();
let mut eventid_info = HashMap::new();
let mut todo_outlier_stack = incoming_pdu.prev_events.clone();
let mut todo_outlier_stack = incoming_pdu
.prev_events
.iter()
.cloned()
.map(Arc::new)
.collect::<Vec<_>>();
let mut amount = 0;
@ -929,13 +934,13 @@ pub async fn handle_incoming_pdu<'a>( @@ -929,13 +934,13 @@ pub async fn handle_incoming_pdu<'a>(
amount += 1;
for prev_prev in &pdu.prev_events {
if !graph.contains_key(prev_prev) {
todo_outlier_stack.push(dbg!(prev_prev.clone()));
todo_outlier_stack.push(dbg!(Arc::new(prev_prev.clone())));
}
}
graph.insert(
prev_event_id.clone(),
pdu.prev_events.iter().cloned().collect(),
pdu.prev_events.iter().cloned().map(Arc::new).collect(),
);
eventid_info.insert(prev_event_id.clone(), (pdu, json));
} else {
@ -964,9 +969,9 @@ pub async fn handle_incoming_pdu<'a>( @@ -964,9 +969,9 @@ pub async fn handle_incoming_pdu<'a>(
MilliSecondsSinceUnixEpoch(
eventid_info
.get(event_id)
.map_or_else(|| uint!(0), |info| info.0.origin_server_ts.clone()),
.map_or_else(|| uint!(0), |info| info.0.origin_server_ts),
),
ruma::event_id!("$notimportant"),
Arc::new(ruma::event_id!("$notimportant")),
))
})
.map_err(|_| "Error sorting prev events".to_owned())?;
@ -1084,7 +1089,12 @@ fn handle_outlier_pdu<'a>( @@ -1084,7 +1089,12 @@ fn handle_outlier_pdu<'a>(
fetch_and_handle_outliers(
db,
origin,
&incoming_pdu.auth_events,
&incoming_pdu
.auth_events
.iter()
.cloned()
.map(Arc::new)
.collect::<Vec<_>>(),
&create_event,
&room_id,
pub_key_map,
@ -1153,7 +1163,7 @@ fn handle_outlier_pdu<'a>( @@ -1153,7 +1163,7 @@ fn handle_outlier_pdu<'a>(
if !state_res::event_auth::auth_check(
&room_version,
&incoming_pdu,
previous_create.clone(),
previous_create,
None, // TODO: third party invite
|k, s| auth_events.get(&(k.clone(), s.to_owned())).map(Arc::clone),
)
@ -1219,7 +1229,7 @@ async fn upgrade_outlier_to_timeline_pdu( @@ -1219,7 +1229,7 @@ async fn upgrade_outlier_to_timeline_pdu(
.get_or_create_shortstatekey(&prev_pdu.kind, state_key, &db.globals)
.map_err(|_| "Failed to create shortstatekey.".to_owned())?;
state.insert(shortstatekey, prev_event.clone());
state.insert(shortstatekey, Arc::new(prev_event.clone()));
// Now it's the state after the pdu
}
@ -1249,7 +1259,11 @@ async fn upgrade_outlier_to_timeline_pdu( @@ -1249,7 +1259,11 @@ async fn upgrade_outlier_to_timeline_pdu(
let state_vec = fetch_and_handle_outliers(
&db,
origin,
&res.pdu_ids,
&res.pdu_ids
.iter()
.cloned()
.map(Arc::new)
.collect::<Vec<_>>(),
&create_event,
&room_id,
pub_key_map,
@ -1270,7 +1284,7 @@ async fn upgrade_outlier_to_timeline_pdu( @@ -1270,7 +1284,7 @@ async fn upgrade_outlier_to_timeline_pdu(
match state.entry(shortstatekey) {
btree_map::Entry::Vacant(v) => {
v.insert(pdu.event_id.clone());
v.insert(Arc::new(pdu.event_id.clone()));
}
btree_map::Entry::Occupied(_) => return Err(
"State event's type and state_key combination exists multiple times."
@ -1286,7 +1300,9 @@ async fn upgrade_outlier_to_timeline_pdu( @@ -1286,7 +1300,9 @@ async fn upgrade_outlier_to_timeline_pdu(
.map_err(|_| "Failed to talk to db.")?
.expect("Room exists");
if state.get(&create_shortstatekey) != Some(&create_event.event_id) {
if state.get(&create_shortstatekey).map(|id| id.as_ref())
!= Some(&create_event.event_id)
{
return Err("Incoming event refers to wrong create event.".to_owned());
}
@ -1440,7 +1456,7 @@ async fn upgrade_outlier_to_timeline_pdu( @@ -1440,7 +1456,7 @@ async fn upgrade_outlier_to_timeline_pdu(
.rooms
.get_or_create_shortstatekey(&leaf_pdu.kind, state_key, &db.globals)
.map_err(|_| "Failed to create shortstatekey.".to_owned())?;
leaf_state.insert(shortstatekey, leaf_pdu.event_id.clone());
leaf_state.insert(shortstatekey, Arc::new(leaf_pdu.event_id.clone()));
// Now it's the state after the pdu
}
@ -1455,9 +1471,9 @@ async fn upgrade_outlier_to_timeline_pdu( @@ -1455,9 +1471,9 @@ async fn upgrade_outlier_to_timeline_pdu(
.get_or_create_shortstatekey(&incoming_pdu.kind, state_key, &db.globals)
.map_err(|_| "Failed to create shortstatekey.".to_owned())?;
state_after.insert(shortstatekey, incoming_pdu.event_id.clone());
state_after.insert(shortstatekey, Arc::new(incoming_pdu.event_id.clone()));
}
fork_states.push(state_after.clone());
fork_states.push(state_after);
let mut update_state = false;
// 14. Use state resolution to find new room state
@ -1613,7 +1629,7 @@ async fn upgrade_outlier_to_timeline_pdu( @@ -1613,7 +1629,7 @@ async fn upgrade_outlier_to_timeline_pdu(
pub(crate) fn fetch_and_handle_outliers<'a>(
db: &'a Database,
origin: &'a ServerName,
events: &'a [EventId],
events: &'a [Arc<EventId>],
create_event: &'a PduEvent,
room_id: &'a RoomId,
pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>,
@ -1668,7 +1684,7 @@ pub(crate) fn fetch_and_handle_outliers<'a>( @@ -1668,7 +1684,7 @@ pub(crate) fn fetch_and_handle_outliers<'a>(
match crate::pdu::gen_event_id_canonical_json(&res.pdu) {
Ok(t) => t,
Err(_) => {
back_off(id.clone());
back_off((**id).clone());
continue;
}
};
@ -1688,14 +1704,14 @@ pub(crate) fn fetch_and_handle_outliers<'a>( @@ -1688,14 +1704,14 @@ pub(crate) fn fetch_and_handle_outliers<'a>(
Ok((pdu, json)) => (pdu, Some(json)),
Err(e) => {
warn!("Authentication of event {} failed: {:?}", id, e);
back_off(id.clone());
back_off((**id).clone());
continue;
}
}
}
Err(_) => {
warn!("Failed to fetch event: {}", id);
back_off(id.clone());
back_off((**id).clone());
continue;
}
}
@ -1971,9 +1987,9 @@ fn append_incoming_pdu( @@ -1971,9 +1987,9 @@ fn append_incoming_pdu(
#[tracing::instrument(skip(starting_events, db))]
fn get_auth_chain(
starting_events: Vec<EventId>,
starting_events: Vec<Arc<EventId>>,
db: &Database,
) -> Result<impl Iterator<Item = EventId> + '_> {
) -> Result<impl Iterator<Item = Arc<EventId>> + '_> {
let mut full_auth_chain = HashSet::new();
const NUM_BUCKETS: usize = 100;
@ -2136,12 +2152,12 @@ pub fn get_event_authorization_route( @@ -2136,12 +2152,12 @@ pub fn get_event_authorization_route(
return Err(Error::bad_config("Federation is disabled."));
}
let auth_chain_ids = get_auth_chain(vec![body.event_id.clone()], &db)?;
let auth_chain_ids = get_auth_chain(vec![Arc::new(body.event_id.clone())], &db)?;
Ok(get_event_authorization::v1::Response {
auth_chain: auth_chain_ids
.filter_map(|id| Some(db.rooms.get_pdu_json(&id).ok()??))
.map(|event| PduEvent::convert_to_outgoing_federation_event(event))
.filter_map(|id| db.rooms.get_pdu_json(&id).ok()?)
.map(PduEvent::convert_to_outgoing_federation_event)
.collect(),
}
.into())
@ -2179,7 +2195,7 @@ pub fn get_room_state_route( @@ -2179,7 +2195,7 @@ pub fn get_room_state_route(
})
.collect();
let auth_chain_ids = get_auth_chain(vec![body.event_id.clone()], &db)?;
let auth_chain_ids = get_auth_chain(vec![Arc::new(body.event_id.clone())], &db)?;
Ok(get_room_state::v1::Response {
auth_chain: auth_chain_ids
@ -2220,13 +2236,13 @@ pub fn get_room_state_ids_route( @@ -2220,13 +2236,13 @@ pub fn get_room_state_ids_route(
.rooms
.state_full_ids(shortstatehash)?
.into_iter()
.map(|(_, id)| id)
.map(|(_, id)| (*id).clone())
.collect();
let auth_chain_ids = get_auth_chain(vec![body.event_id.clone()], &db)?;
let auth_chain_ids = get_auth_chain(vec![Arc::new(body.event_id.clone())], &db)?;
Ok(get_room_state_ids::v1::Response {
auth_chain_ids: auth_chain_ids.collect(),
auth_chain_ids: auth_chain_ids.map(|id| (*id).clone()).collect(),
pdu_ids,
}
.into())

Loading…
Cancel
Save