Remove auth_cache using a closure to fetch events in state-res

merge-requests/108/head
Devin Ragotzy 3 years ago committed by Timo Kösters
parent 05821d6fd5
commit 98f1480e2b
No known key found for this signature in database
GPG Key ID: 24DA7517711A2BA4

88
Cargo.lock generated

@ -272,9 +272,9 @@ dependencies = [
[[package]] [[package]]
name = "const-oid" name = "const-oid"
version = "0.5.2" version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "279bc8fc53f788a75c7804af68237d1fce02cde1e275a886a4b320604dc2aeda" checksum = "44c32f031ea41b4291d695026c023b95d59db2d8a2c7640800ed56bc8f510f22"
[[package]] [[package]]
name = "const_fn" name = "const_fn"
@ -394,9 +394,9 @@ dependencies = [
[[package]] [[package]]
name = "der" name = "der"
version = "0.3.5" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2eeb9d92785d1facb50567852ce75d0858630630e7eabea59cf7eb7474051087" checksum = "49f215f706081a44cb702c71c39a52c05da637822e9c1645a50b7202689e982d"
dependencies = [ dependencies = [
"const-oid", "const-oid",
] ]
@ -1475,9 +1475,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
[[package]] [[package]]
name = "pkcs8" name = "pkcs8"
version = "0.6.1" version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9c2f795bc591cb3384cb64082a578b89207ac92bb89c9d98c1ea2ace7cd8110" checksum = "09d156817ae0125e8aa5067710b0db24f0984830614f99875a70aa5e3b74db69"
dependencies = [ dependencies = [
"der", "der",
"spki", "spki",
@ -1882,8 +1882,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma" name = "ruma"
version = "0.1.2" version = "0.2.0"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"assign", "assign",
"js_int", "js_int",
@ -1903,8 +1903,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-api" name = "ruma-api"
version = "0.17.0" version = "0.17.1"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"bytes", "bytes",
"http", "http",
@ -1919,8 +1919,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-api-macros" name = "ruma-api-macros"
version = "0.17.0" version = "0.17.1"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@ -1930,8 +1930,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-appservice-api" name = "ruma-appservice-api"
version = "0.2.0" version = "0.3.0"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"ruma-api", "ruma-api",
"ruma-common", "ruma-common",
@ -1944,8 +1944,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-client-api" name = "ruma-client-api"
version = "0.10.2" version = "0.11.0"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"assign", "assign",
"bytes", "bytes",
@ -1964,8 +1964,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-common" name = "ruma-common"
version = "0.5.3" version = "0.5.4"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"indexmap", "indexmap",
"js_int", "js_int",
@ -1979,8 +1979,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-events" name = "ruma-events"
version = "0.22.2" version = "0.23.2"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"indoc", "indoc",
"js_int", "js_int",
@ -1994,8 +1994,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-events-macros" name = "ruma-events-macros"
version = "0.22.2" version = "0.23.2"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@ -2005,8 +2005,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-federation-api" name = "ruma-federation-api"
version = "0.1.0" version = "0.2.0"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-api", "ruma-api",
@ -2020,8 +2020,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers" name = "ruma-identifiers"
version = "0.19.2" version = "0.19.4"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"paste", "paste",
"rand 0.8.3", "rand 0.8.3",
@ -2034,8 +2034,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers-macros" name = "ruma-identifiers-macros"
version = "0.19.2" version = "0.19.4"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"quote", "quote",
"ruma-identifiers-validation", "ruma-identifiers-validation",
@ -2045,12 +2045,12 @@ dependencies = [
[[package]] [[package]]
name = "ruma-identifiers-validation" name = "ruma-identifiers-validation"
version = "0.4.0" version = "0.4.0"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
[[package]] [[package]]
name = "ruma-identity-service-api" name = "ruma-identity-service-api"
version = "0.1.0" version = "0.2.0"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-api", "ruma-api",
@ -2062,8 +2062,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-push-gateway-api" name = "ruma-push-gateway-api"
version = "0.1.0" version = "0.2.0"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"js_int", "js_int",
"ruma-api", "ruma-api",
@ -2077,8 +2077,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-serde" name = "ruma-serde"
version = "0.4.0" version = "0.4.1"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"bytes", "bytes",
"form_urlencoded", "form_urlencoded",
@ -2091,8 +2091,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-serde-macros" name = "ruma-serde-macros"
version = "0.4.0" version = "0.4.1"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"proc-macro-crate", "proc-macro-crate",
"proc-macro2", "proc-macro2",
@ -2102,8 +2102,8 @@ dependencies = [
[[package]] [[package]]
name = "ruma-signatures" name = "ruma-signatures"
version = "0.7.2" version = "0.8.0"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"base64 0.13.0", "base64 0.13.0",
"ed25519-dalek", "ed25519-dalek",
@ -2115,13 +2115,12 @@ dependencies = [
"sha2", "sha2",
"thiserror", "thiserror",
"tracing", "tracing",
"untrusted",
] ]
[[package]] [[package]]
name = "ruma-state-res" name = "ruma-state-res"
version = "0.1.0" version = "0.2.0"
source = "git+https://github.com/ruma/ruma?rev=5a7e2cddcf257e367465cced51442c91e8f557c9#5a7e2cddcf257e367465cced51442c91e8f557c9" source = "git+https://github.com/DevinR528/ruma?branch=state-closure#95208b9d03876e7c85543fe8655ceb2f7dc76363"
dependencies = [ dependencies = [
"itertools 0.10.0", "itertools 0.10.0",
"js_int", "js_int",
@ -2130,7 +2129,6 @@ dependencies = [
"ruma-events", "ruma-events",
"ruma-identifiers", "ruma-identifiers",
"ruma-serde", "ruma-serde",
"ruma-signatures",
"serde", "serde",
"serde_json", "serde_json",
"thiserror", "thiserror",
@ -2444,9 +2442,9 @@ checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]] [[package]]
name = "spki" name = "spki"
version = "0.3.0" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dae7e047abc519c96350e9484a96c6bf1492348af912fd3446dd2dc323f6268" checksum = "987637c5ae6b3121aba9d513f869bd2bff11c4cc086c22473befd6649c0bd521"
dependencies = [ dependencies = [
"der", "der",
] ]

@ -8,7 +8,6 @@ repository = "https://gitlab.com/famedly/conduit"
readme = "README.md" readme = "README.md"
version = "0.1.0" version = "0.1.0"
edition = "2018" edition = "2018"
rust = "1.50"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -18,7 +17,8 @@ rust = "1.50"
rocket = { git = "https://github.com/SergioBenitez/Rocket.git", rev = "801e04bd5369eb39e126c75f6d11e1e9597304d8", features = ["tls"] } # Used to handle requests rocket = { git = "https://github.com/SergioBenitez/Rocket.git", rev = "801e04bd5369eb39e126c75f6d11e1e9597304d8", features = ["tls"] } # Used to handle requests
# Used for matrix spec type definitions and helpers # Used for matrix spec type definitions and helpers
ruma = { git = "https://github.com/ruma/ruma", rev = "5a7e2cddcf257e367465cced51442c91e8f557c9", 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/ruma/ruma", rev = "5a7e2cddcf257e367465cced51442c91e8f557c9", 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", branch = "state-closure", 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"] } #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 # Used for long polling and federation sender, should be the same as rocket::tokio

@ -1 +1 @@
1.50.0 1.51.0

@ -25,7 +25,7 @@ use ruma::{
EventType, EventType,
}, },
serde::{to_canonical_value, CanonicalJsonObject, CanonicalJsonValue, Raw}, serde::{to_canonical_value, CanonicalJsonObject, CanonicalJsonValue, Raw},
state_res::{self, EventMap, RoomVersion}, state_res::{self, RoomVersion},
uint, EventId, RoomId, RoomVersionId, ServerName, UserId, uint, EventId, RoomId, RoomVersionId, ServerName, UserId,
}; };
use std::{ use std::{
@ -883,7 +883,6 @@ pub async fn invite_helper(
.await?; .await?;
let pub_key_map = RwLock::new(BTreeMap::new()); let pub_key_map = RwLock::new(BTreeMap::new());
let mut auth_cache = EventMap::new();
// We do not add the event_id field to the pdu here because of signature and hashes checks // We do not add the event_id field to the pdu here because of signature and hashes checks
let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(&response.event) { let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(&response.event) {
@ -906,26 +905,19 @@ pub async fn invite_helper(
) )
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Origin field is invalid."))?; .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Origin field is invalid."))?;
let pdu_id = server_server::handle_incoming_pdu( let pdu_id =
&origin, server_server::handle_incoming_pdu(&origin, &event_id, value, true, &db, &pub_key_map)
&event_id, .await
value, .map_err(|_| {
true, Error::BadRequest(
&db, ErrorKind::InvalidParam,
&pub_key_map, "Error while handling incoming PDU.",
&mut auth_cache, )
) })?
.await .ok_or(Error::BadRequest(
.map_err(|_| { ErrorKind::InvalidParam,
Error::BadRequest( "Could not accept incoming PDU as timeline event.",
ErrorKind::InvalidParam, ))?;
"Error while handling incoming PDU.",
)
})?
.ok_or(Error::BadRequest(
ErrorKind::InvalidParam,
"Could not accept incoming PDU as timeline event.",
))?;
for server in db for server in db
.rooms .rooms

@ -65,7 +65,7 @@ impl DatabaseEngine for SledEngine {
sled::Config::default() sled::Config::default()
.path(&config.database_path) .path(&config.database_path)
.cache_capacity(config.cache_capacity as u64) .cache_capacity(config.cache_capacity as u64)
.use_compression(false) .use_compression(true)
.open()?, .open()?,
))) )))
} }

@ -45,7 +45,7 @@ use ruma::{
receipt::ReceiptType, receipt::ReceiptType,
serde::Raw, serde::Raw,
signatures::{CanonicalJsonObject, CanonicalJsonValue}, signatures::{CanonicalJsonObject, CanonicalJsonValue},
state_res::{self, Event, EventMap, RoomVersion, StateMap}, state_res::{self, Event, RoomVersion, StateMap},
to_device::DeviceIdOrAllDevices, to_device::DeviceIdOrAllDevices,
uint, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName, uint, EventId, MilliSecondsSinceUnixEpoch, RoomId, RoomVersionId, ServerName,
ServerSigningKeyId, UserId, ServerSigningKeyId, UserId,
@ -612,7 +612,7 @@ pub async fn send_transaction_message_route(
// TODO: This could potentially also be some sort of trie (suffix tree) like structure so // TODO: This could potentially also be some sort of trie (suffix tree) like structure so
// that once an auth event is known it would know (using indexes maybe) all of the auth // that once an auth event is known it would know (using indexes maybe) all of the auth
// events that it references. // events that it references.
let mut auth_cache = EventMap::new(); // let mut auth_cache = EventMap::new();
for pdu in &body.pdus { for pdu in &body.pdus {
// We do not add the event_id field to the pdu here because of signature and hashes checks // We do not add the event_id field to the pdu here because of signature and hashes checks
@ -627,17 +627,9 @@ pub async fn send_transaction_message_route(
let start_time = Instant::now(); let start_time = Instant::now();
resolved_map.insert( resolved_map.insert(
event_id.clone(), event_id.clone(),
handle_incoming_pdu( handle_incoming_pdu(&body.origin, &event_id, value, true, &db, &pub_key_map)
&body.origin, .await
&event_id, .map(|_| ()),
value,
true,
&db,
&pub_key_map,
&mut auth_cache,
)
.await
.map(|_| ()),
); );
let elapsed = start_time.elapsed(); let elapsed = start_time.elapsed();
@ -820,7 +812,6 @@ pub fn handle_incoming_pdu<'a>(
is_timeline_event: bool, is_timeline_event: bool,
db: &'a Database, db: &'a Database,
pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>, pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>,
auth_cache: &'a mut EventMap<Arc<PduEvent>>,
) -> AsyncRecursiveResult<'a, Option<Vec<u8>>, String> { ) -> AsyncRecursiveResult<'a, Option<Vec<u8>>, String> {
Box::pin(async move { Box::pin(async move {
// TODO: For RoomVersion6 we must check that Raw<..> is canonical do we anywhere?: https://matrix.org/docs/spec/rooms/v6#canonical-json // TODO: For RoomVersion6 we must check that Raw<..> is canonical do we anywhere?: https://matrix.org/docs/spec/rooms/v6#canonical-json
@ -908,15 +899,9 @@ pub fn handle_incoming_pdu<'a>(
// 5. Reject "due to auth events" if can't get all the auth events or some of the auth events are also rejected "due to auth events" // 5. Reject "due to auth events" if can't get all the auth events or some of the auth events are also rejected "due to auth events"
// EDIT: Step 5 is not applied anymore because it failed too often // EDIT: Step 5 is not applied anymore because it failed too often
debug!("Fetching auth events for {}", incoming_pdu.event_id); debug!("Fetching auth events for {}", incoming_pdu.event_id);
fetch_and_handle_events( fetch_and_handle_events(db, origin, &incoming_pdu.auth_events, pub_key_map)
db, .await
origin, .map_err(|e| e.to_string())?;
&incoming_pdu.auth_events,
pub_key_map,
auth_cache,
)
.await
.map_err(|e| e.to_string())?;
// 6. Reject "due to auth events" if the event doesn't pass auth based on the auth events // 6. Reject "due to auth events" if the event doesn't pass auth based on the auth events
debug!( debug!(
@ -927,9 +912,13 @@ pub fn handle_incoming_pdu<'a>(
// Build map of auth events // Build map of auth events
let mut auth_events = BTreeMap::new(); let mut auth_events = BTreeMap::new();
for id in &incoming_pdu.auth_events { for id in &incoming_pdu.auth_events {
let auth_event = auth_cache.get(id).ok_or_else(|| { let auth_event = db
"Auth event not found, event failed recursive auth checks.".to_string() .rooms
})?; .get_pdu(id)
.map_err(|e| e.to_string())?
.ok_or_else(|| {
"Auth event not found, event failed recursive auth checks.".to_string()
})?;
match auth_events.entry(( match auth_events.entry((
auth_event.kind.clone(), auth_event.kind.clone(),
@ -963,9 +952,9 @@ pub fn handle_incoming_pdu<'a>(
let previous_create = if incoming_pdu.auth_events.len() == 1 let previous_create = if incoming_pdu.auth_events.len() == 1
&& incoming_pdu.prev_events == incoming_pdu.auth_events && incoming_pdu.prev_events == incoming_pdu.auth_events
{ {
auth_cache db.rooms
.get(&incoming_pdu.auth_events[0]) .get_pdu(&incoming_pdu.auth_events[0])
.cloned() .map_err(|e| e.to_string())?
.filter(|maybe_create| **maybe_create == *create_event) .filter(|maybe_create| **maybe_create == *create_event)
} else { } else {
None None
@ -1008,7 +997,6 @@ pub fn handle_incoming_pdu<'a>(
debug!("Requesting state at event."); debug!("Requesting state at event.");
let mut state_at_incoming_event = None; let mut state_at_incoming_event = None;
let mut incoming_auth_events = Vec::new();
if incoming_pdu.prev_events.len() == 1 { if incoming_pdu.prev_events.len() == 1 {
let prev_event = &incoming_pdu.prev_events[0]; let prev_event = &incoming_pdu.prev_events[0];
@ -1031,7 +1019,7 @@ pub fn handle_incoming_pdu<'a>(
state_vec.push(prev_event.clone()); state_vec.push(prev_event.clone());
} }
state_at_incoming_event = Some( state_at_incoming_event = Some(
fetch_and_handle_events(db, origin, &state_vec, pub_key_map, auth_cache) fetch_and_handle_events(db, origin, &state_vec, pub_key_map)
.await .await
.map_err(|_| "Failed to fetch state events locally".to_owned())? .map_err(|_| "Failed to fetch state events locally".to_owned())?
.into_iter() .into_iter()
@ -1069,18 +1057,12 @@ pub fn handle_incoming_pdu<'a>(
{ {
Ok(res) => { Ok(res) => {
debug!("Fetching state events at event."); debug!("Fetching state events at event.");
let state_vec = match fetch_and_handle_events( let state_vec =
&db, match fetch_and_handle_events(&db, origin, &res.pdu_ids, pub_key_map).await
origin, {
&res.pdu_ids, Ok(state) => state,
pub_key_map, Err(_) => return Err("Failed to fetch state events.".to_owned()),
auth_cache, };
)
.await
{
Ok(state) => state,
Err(_) => return Err("Failed to fetch state events.".to_owned()),
};
let mut state = BTreeMap::new(); let mut state = BTreeMap::new();
for pdu in state_vec { for pdu in state_vec {
@ -1106,14 +1088,8 @@ pub fn handle_incoming_pdu<'a>(
} }
debug!("Fetching auth chain events at event."); debug!("Fetching auth chain events at event.");
incoming_auth_events = match fetch_and_handle_events( match fetch_and_handle_events(&db, origin, &res.auth_chain_ids, pub_key_map)
&db, .await
origin,
&res.auth_chain_ids,
pub_key_map,
auth_cache,
)
.await
{ {
Ok(state) => state, Ok(state) => state,
Err(_) => return Err("Failed to fetch auth chain.".to_owned()), Err(_) => return Err("Failed to fetch auth chain.".to_owned()),
@ -1243,14 +1219,8 @@ pub fn handle_incoming_pdu<'a>(
for map in &fork_states { for map in &fork_states {
let mut state_auth = vec![]; let mut state_auth = vec![];
for auth_id in map.values().flat_map(|pdu| &pdu.auth_events) { for auth_id in map.values().flat_map(|pdu| &pdu.auth_events) {
match fetch_and_handle_events( match fetch_and_handle_events(&db, origin, &[auth_id.clone()], pub_key_map)
&db, .await
origin,
&[auth_id.clone()],
pub_key_map,
auth_cache,
)
.await
{ {
// This should always contain exactly one element when Ok // This should always contain exactly one element when Ok
Ok(events) => state_auth.extend_from_slice(&events), Ok(events) => state_auth.extend_from_slice(&events),
@ -1259,31 +1229,9 @@ pub fn handle_incoming_pdu<'a>(
} }
} }
} }
auth_cache.extend(
map.iter()
.map(|pdu| (pdu.1.event_id.clone(), pdu.1.clone())),
);
auth_events.push(state_auth); auth_events.push(state_auth);
} }
// Add everything we will need to event_map
auth_cache.extend(
auth_events
.iter()
.map(|pdus| pdus.iter().map(|pdu| (pdu.event_id.clone(), pdu.clone())))
.flatten(),
);
auth_cache.extend(
incoming_auth_events
.into_iter()
.map(|pdu| (pdu.event_id().clone(), pdu)),
);
auth_cache.extend(
state_after
.into_iter()
.map(|(_, pdu)| (pdu.event_id().clone(), pdu)),
);
match state_res::StateResolution::resolve( match state_res::StateResolution::resolve(
&room_id, &room_id,
room_version_id, room_version_id,
@ -1299,7 +1247,13 @@ pub fn handle_incoming_pdu<'a>(
.into_iter() .into_iter()
.map(|pdus| pdus.into_iter().map(|pdu| pdu.event_id().clone()).collect()) .map(|pdus| pdus.into_iter().map(|pdu| pdu.event_id().clone()).collect())
.collect(), .collect(),
auth_cache, &|id| {
let res = db.rooms.get_pdu(id);
if let Err(e) = &res {
error!("LOOK AT ME Failed to fetch event: {}", e);
}
res.ok().flatten()
},
) { ) {
Ok(new_state) => new_state, Ok(new_state) => new_state,
Err(_) => { Err(_) => {
@ -1373,7 +1327,6 @@ pub(crate) fn fetch_and_handle_events<'a>(
origin: &'a ServerName, origin: &'a ServerName,
events: &'a [EventId], events: &'a [EventId],
pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>, pub_key_map: &'a RwLock<BTreeMap<String, BTreeMap<String, String>>>,
auth_cache: &'a mut EventMap<Arc<PduEvent>>,
) -> AsyncRecursiveResult<'a, Vec<Arc<PduEvent>>, Error> { ) -> AsyncRecursiveResult<'a, Vec<Arc<PduEvent>>, Error> {
Box::pin(async move { Box::pin(async move {
let back_off = |id| match db.globals.bad_event_ratelimiter.write().unwrap().entry(id) { let back_off = |id| match db.globals.bad_event_ratelimiter.write().unwrap().entry(id) {
@ -1397,84 +1350,73 @@ pub(crate) fn fetch_and_handle_events<'a>(
continue; continue;
} }
} }
// a. Look at auth cache
let pdu = match auth_cache.get(id) { // a. Look in the main timeline (pduid_pdu tree)
// b. Look at outlier pdu tree
// (get_pdu checks both)
let pdu = match db.rooms.get_pdu(&id)? {
Some(pdu) => { Some(pdu) => {
// We already have the auth chain for events in cache trace!("Found {} in db", id);
pdu.clone() pdu
} }
// b. Look in the main timeline (pduid_pdu tree) None => {
// c. Look at outlier pdu tree // c. Ask origin server over federation
// (get_pdu checks both) debug!("Fetching {} over federation.", id);
None => match db.rooms.get_pdu(&id)? { match db
Some(pdu) => { .sending
trace!("Found {} in db", id); .send_federation_request(
// We need to fetch the auth chain &db.globals,
let _ = fetch_and_handle_events(
db,
origin, origin,
&pdu.auth_events, get_event::v1::Request { event_id: &id },
pub_key_map,
auth_cache,
) )
.await?; .await
pdu {
} Ok(res) => {
None => { debug!("Got {} over federation", id);
// d. Ask origin server over federation let (event_id, mut value) =
debug!("Fetching {} over federation.", id); crate::pdu::gen_event_id_canonical_json(&res.pdu)?;
match db // This will also fetch the auth chain
.sending match handle_incoming_pdu(
.send_federation_request(
&db.globals,
origin, origin,
get_event::v1::Request { event_id: &id }, &event_id,
value.clone(),
false,
db,
pub_key_map,
) )
.await .await
{ {
Ok(res) => { Ok(_) => {
debug!("Got {} over federation", id); value.insert(
let (event_id, mut value) = "event_id".to_owned(),
crate::pdu::gen_event_id_canonical_json(&res.pdu)?; CanonicalJsonValue::String(event_id.into()),
// This will also fetch the auth chain );
match handle_incoming_pdu(
origin, Arc::new(
&event_id, serde_json::from_value(
value.clone(), serde_json::to_value(value)
false, .expect("canonicaljsonobject is valid value"),
db, )
pub_key_map, .expect(
auth_cache, "This is possible because handle_incoming_pdu worked",
) ),
.await )
{ }
Ok(_) => { Err(e) => {
value.insert( warn!("Authentication of event {} failed: {:?}", id, e);
"event_id".to_owned(), back_off(id.clone());
CanonicalJsonValue::String(event_id.into()), continue;
);
Arc::new(serde_json::from_value(
serde_json::to_value(value).expect("canonicaljsonobject is valid value"),
).expect("This is possible because handle_incoming_pdu worked"))
}
Err(e) => {
warn!("Authentication of event {} failed: {:?}", id, e);
back_off(id.clone());
continue;
}
} }
}
Err(_) => {
warn!("Failed to fetch event: {}", id);
back_off(id.clone());
continue;
} }
} }
Err(_) => {
warn!("Failed to fetch event: {}", id);
back_off(id.clone());
continue;
}
} }
}, }
}; };
auth_cache.entry(id.clone()).or_insert_with(|| pdu.clone());
pdus.push(pdu); pdus.push(pdu);
} }
Ok(pdus) Ok(pdus)
@ -2155,7 +2097,7 @@ pub async fn create_join_event_route(
))?; ))?;
let pub_key_map = RwLock::new(BTreeMap::new()); let pub_key_map = RwLock::new(BTreeMap::new());
let mut auth_cache = EventMap::new(); // let mut auth_cache = EventMap::new();
// We do not add the event_id field to the pdu here because of signature and hashes checks // We do not add the event_id field to the pdu here because of signature and hashes checks
let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(&body.pdu) { let (event_id, value) = match crate::pdu::gen_event_id_canonical_json(&body.pdu) {
@ -2178,26 +2120,18 @@ pub async fn create_join_event_route(
) )
.map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Origin field is invalid."))?; .map_err(|_| Error::BadRequest(ErrorKind::InvalidParam, "Origin field is invalid."))?;
let pdu_id = handle_incoming_pdu( let pdu_id = handle_incoming_pdu(&origin, &event_id, value, true, &db, &pub_key_map)
&origin, .await
&event_id, .map_err(|_| {
value, Error::BadRequest(
true, ErrorKind::InvalidParam,
&db, "Error while handling incoming PDU.",
&pub_key_map, )
&mut auth_cache, })?
) .ok_or(Error::BadRequest(
.await
.map_err(|_| {
Error::BadRequest(
ErrorKind::InvalidParam, ErrorKind::InvalidParam,
"Error while handling incoming PDU.", "Could not accept incoming PDU as timeline event.",
) ))?;
})?
.ok_or(Error::BadRequest(
ErrorKind::InvalidParam,
"Could not accept incoming PDU as timeline event.",
))?;
let state_ids = db.rooms.state_full_ids(shortstatehash)?; let state_ids = db.rooms.state_full_ids(shortstatehash)?;

Loading…
Cancel
Save