|
|
|
@ -1,7 +1,4 @@ |
|
|
|
use crate::{ |
|
|
|
use crate::{client_server, utils, ConduitResult, Database, Error, PduEvent, Result, Ruma}; |
|
|
|
client_server, database::rooms::ClosestParent, utils, ConduitResult, Database, Error, PduEvent, |
|
|
|
|
|
|
|
Result, Ruma, |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
use get_profile_information::v1::ProfileField; |
|
|
|
use get_profile_information::v1::ProfileField; |
|
|
|
use http::header::{HeaderValue, AUTHORIZATION, HOST}; |
|
|
|
use http::header::{HeaderValue, AUTHORIZATION, HOST}; |
|
|
|
use log::{error, warn}; |
|
|
|
use log::{error, warn}; |
|
|
|
@ -21,10 +18,10 @@ use ruma::{ |
|
|
|
OutgoingRequest, |
|
|
|
OutgoingRequest, |
|
|
|
}, |
|
|
|
}, |
|
|
|
directory::{IncomingFilter, IncomingRoomNetwork}, |
|
|
|
directory::{IncomingFilter, IncomingRoomNetwork}, |
|
|
|
EventId, RoomId, ServerName, ServerSigningKeyId, UserId, |
|
|
|
EventId, RoomId, RoomVersionId, ServerName, ServerSigningKeyId, UserId, |
|
|
|
}; |
|
|
|
}; |
|
|
|
use std::{ |
|
|
|
use std::{ |
|
|
|
collections::BTreeMap, |
|
|
|
collections::{BTreeMap, BTreeSet}, |
|
|
|
convert::TryFrom, |
|
|
|
convert::TryFrom, |
|
|
|
fmt::Debug, |
|
|
|
fmt::Debug, |
|
|
|
sync::Arc, |
|
|
|
sync::Arc, |
|
|
|
@ -390,6 +387,34 @@ pub async fn get_public_rooms_route( |
|
|
|
.into()) |
|
|
|
.into()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Ord, PartialOrd, Eq, PartialEq)] |
|
|
|
|
|
|
|
pub enum PrevEvents<T> { |
|
|
|
|
|
|
|
Sequential(T), |
|
|
|
|
|
|
|
Fork(Vec<T>), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<T> IntoIterator for PrevEvents<T> { |
|
|
|
|
|
|
|
type Item = T; |
|
|
|
|
|
|
|
type IntoIter = std::vec::IntoIter<Self::Item>; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter { |
|
|
|
|
|
|
|
match self { |
|
|
|
|
|
|
|
Self::Sequential(item) => vec![item].into_iter(), |
|
|
|
|
|
|
|
Self::Fork(list) => list.into_iter(), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<T: Clone> PrevEvents<T> { |
|
|
|
|
|
|
|
pub fn new(id: &[T]) -> Self { |
|
|
|
|
|
|
|
match id { |
|
|
|
|
|
|
|
[] => panic!("All events must have previous event"), |
|
|
|
|
|
|
|
[single_id] => Self::Sequential(single_id.clone()), |
|
|
|
|
|
|
|
rest => Self::Fork(rest.to_vec()), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
#[cfg_attr(
|
|
|
|
feature = "conduit_bin", |
|
|
|
feature = "conduit_bin", |
|
|
|
put("/_matrix/federation/v1/send/<_>", data = "<body>") |
|
|
|
put("/_matrix/federation/v1/send/<_>", data = "<body>") |
|
|
|
@ -445,16 +470,23 @@ pub async fn send_transaction_message_route<'a>( |
|
|
|
// discard the event whereas the Client Server API's /send/{eventType} endpoint
|
|
|
|
// discard the event whereas the Client Server API's /send/{eventType} endpoint
|
|
|
|
// would return a M_BAD_JSON error.
|
|
|
|
// would return a M_BAD_JSON error.
|
|
|
|
let mut resolved_map = BTreeMap::new(); |
|
|
|
let mut resolved_map = BTreeMap::new(); |
|
|
|
for pdu in &body.pdus { |
|
|
|
let mut pdu_idx = 0; |
|
|
|
// Ruma/PduEvent/StateEvent satisfies - 1. Is a valid event, otherwise it is dropped.
|
|
|
|
// This is `for pdu in &body.pdus` but we need to do fancy continue/break so we need loop labels
|
|
|
|
|
|
|
|
'outer: loop { |
|
|
|
|
|
|
|
if pdu_idx == body.pdus.len() { |
|
|
|
|
|
|
|
break 'outer; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
let pdu = &body.pdus[pdu_idx]; |
|
|
|
|
|
|
|
pdu_idx += 1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Ruma/PduEvent/StateEvent satisfies - 1. Is a valid event, otherwise it is dropped.
|
|
|
|
// state-res checks signatures - 2. Passes signature checks, otherwise event is dropped.
|
|
|
|
// state-res checks signatures - 2. Passes signature checks, otherwise event is dropped.
|
|
|
|
|
|
|
|
|
|
|
|
// 3. Passes hash checks, otherwise it is redacted before being processed further.
|
|
|
|
// 3. Passes hash checks, otherwise it is redacted before being processed further.
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: redact event if hashing fails
|
|
|
|
// TODO: redact event if hashing fails
|
|
|
|
let (event_id, value) = crate::pdu::process_incoming_pdu(pdu); |
|
|
|
let (event_id, value) = crate::pdu::process_incoming_pdu(pdu); |
|
|
|
|
|
|
|
|
|
|
|
let pdu = serde_json::from_value::<PduEvent>( |
|
|
|
let mut pdu = serde_json::from_value::<PduEvent>( |
|
|
|
serde_json::to_value(&value).expect("CanonicalJsonObj is a valid JsonValue"), |
|
|
|
serde_json::to_value(&value).expect("CanonicalJsonObj is a valid JsonValue"), |
|
|
|
) |
|
|
|
) |
|
|
|
.expect("all ruma pdus are conduit pdus"); |
|
|
|
.expect("all ruma pdus are conduit pdus"); |
|
|
|
@ -466,47 +498,59 @@ pub async fn send_transaction_message_route<'a>( |
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// If it is not a state event, we can skip state-res... maybe
|
|
|
|
// The events that must be resolved to catch up to the incoming event
|
|
|
|
if value.get("state_key").is_none() { |
|
|
|
let mut missing = vec![]; |
|
|
|
if !db.rooms.is_joined(&pdu.sender, room_id)? { |
|
|
|
let mut seen = state_res::StateMap::new(); |
|
|
|
warn!("Sender is not joined {}", pdu.kind); |
|
|
|
let mut seen_id = None; |
|
|
|
resolved_map.insert(event_id, Err("User is not in this room".into())); |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let count = db.globals.next_count()?; |
|
|
|
|
|
|
|
let mut pdu_id = room_id.as_bytes().to_vec(); |
|
|
|
|
|
|
|
pdu_id.push(0xff); |
|
|
|
|
|
|
|
pdu_id.extend_from_slice(&count.to_be_bytes()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.rooms.append_to_state(&pdu_id, &pdu)?; |
|
|
|
let mut prev_ids = vec![PrevEvents::new(&pdu.prev_events)]; |
|
|
|
|
|
|
|
|
|
|
|
db.rooms.append_pdu( |
|
|
|
// This is `while let Some(event_id) = prev_ids.pop_front()` but with a fancy continue
|
|
|
|
&pdu, |
|
|
|
// in the case of a failed request to the server that sent the event
|
|
|
|
&value, |
|
|
|
'inner: loop { |
|
|
|
count, |
|
|
|
match prev_ids.pop() { |
|
|
|
pdu_id.into(), |
|
|
|
Some(PrevEvents::Sequential(id)) => match db |
|
|
|
&db.globals, |
|
|
|
.rooms |
|
|
|
&db.account_data, |
|
|
|
.pdu_state_hash(&db.rooms.get_pdu_id(&id)?.unwrap_or_default())? |
|
|
|
&db.admin, |
|
|
|
{ |
|
|
|
)?; |
|
|
|
// We found a common ancestor
|
|
|
|
|
|
|
|
Some(state_hash) => { |
|
|
|
resolved_map.insert(event_id, Ok::<(), String>(())); |
|
|
|
seen_id = Some(id.clone()); |
|
|
|
continue; |
|
|
|
seen = db.rooms.state_full(&state_hash)?; |
|
|
|
|
|
|
|
if let Some(pdu) = db.rooms.get_pdu(&id)? { |
|
|
|
|
|
|
|
if pdu.state_key.is_some() { |
|
|
|
|
|
|
|
// This becomes the state after the common event
|
|
|
|
|
|
|
|
seen.insert( |
|
|
|
|
|
|
|
(pdu.kind.clone(), pdu.state_key.clone().unwrap()), |
|
|
|
|
|
|
|
pdu, |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
// We have a state event so we need info for state-res
|
|
|
|
break 'inner; |
|
|
|
let get_state_response = match send_request( |
|
|
|
} |
|
|
|
|
|
|
|
// We need to fill in information about this event's `prev_events` (parents)
|
|
|
|
|
|
|
|
None => { |
|
|
|
|
|
|
|
match send_request( |
|
|
|
&db.globals, |
|
|
|
&db.globals, |
|
|
|
body.body.origin.clone(), |
|
|
|
body.body.origin.clone(), |
|
|
|
ruma::api::federation::event::get_room_state::v1::Request { |
|
|
|
ruma::api::federation::event::get_event::v1::Request::new(&id), |
|
|
|
room_id, |
|
|
|
|
|
|
|
event_id: &event_id, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
) |
|
|
|
) |
|
|
|
.await |
|
|
|
.await |
|
|
|
{ |
|
|
|
{ |
|
|
|
Ok(res) => res, |
|
|
|
Ok(res) => { |
|
|
|
|
|
|
|
let (_, val) = crate::pdu::process_incoming_pdu(&res.pdu); |
|
|
|
|
|
|
|
let prev_pdu = serde_json::from_value::<PduEvent>( |
|
|
|
|
|
|
|
serde_json::to_value(&val) |
|
|
|
|
|
|
|
.expect("CanonicalJsonObj is a valid JsonValue"), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.expect("all ruma pdus are conduit pdus"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: do we need this
|
|
|
|
|
|
|
|
assert_eq!(room_id, &prev_pdu.room_id); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
prev_ids.push(PrevEvents::new(&prev_pdu.prev_events)); |
|
|
|
|
|
|
|
missing.push(PrevEvents::Sequential(prev_pdu)); |
|
|
|
|
|
|
|
} |
|
|
|
// We can't hard fail because there are some valid errors, just
|
|
|
|
// We can't hard fail because there are some valid errors, just
|
|
|
|
// keep checking PDU's
|
|
|
|
// keep checking PDU's
|
|
|
|
//
|
|
|
|
//
|
|
|
|
@ -514,80 +558,409 @@ pub async fn send_transaction_message_route<'a>( |
|
|
|
// {"errcode":"M_FORBIDDEN","error":"Host not in room."}
|
|
|
|
// {"errcode":"M_FORBIDDEN","error":"Host not in room."}
|
|
|
|
Err(err) => { |
|
|
|
Err(err) => { |
|
|
|
resolved_map.insert(event_id, Err(err.to_string())); |
|
|
|
resolved_map.insert(event_id, Err(err.to_string())); |
|
|
|
continue; |
|
|
|
// We have to give up on this PDU
|
|
|
|
|
|
|
|
continue 'outer; |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
Some(PrevEvents::Fork(ids)) => { |
|
|
|
|
|
|
|
error!( |
|
|
|
|
|
|
|
"prev_events > 1: {}", |
|
|
|
|
|
|
|
serde_json::to_string_pretty(&pdu).unwrap() |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
// Don't kill our server with state-res
|
|
|
|
|
|
|
|
// TODO: set this at a reasonable level this is for debug/wip purposes
|
|
|
|
|
|
|
|
if ids.len() > 5 { |
|
|
|
|
|
|
|
error!( |
|
|
|
|
|
|
|
"prev_events > 1: {}", |
|
|
|
|
|
|
|
serde_json::to_string_pretty(&pdu).unwrap() |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
resolved_map.insert( |
|
|
|
|
|
|
|
event_id, |
|
|
|
|
|
|
|
Err("Previous events are too large for state-res".into()), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
continue 'outer; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let their_current_state = get_state_response |
|
|
|
// We want this to stay unique incase the fork comes together?
|
|
|
|
.pdus |
|
|
|
let mut prev_fork_ids = BTreeSet::new(); |
|
|
|
.iter() |
|
|
|
let mut missing_fork = vec![]; |
|
|
|
.chain(get_state_response.auth_chain.iter()) // add auth events
|
|
|
|
for id in &ids { |
|
|
|
.map(|pdu| { |
|
|
|
match db |
|
|
|
let (event_id, json) = crate::pdu::process_incoming_pdu(pdu); |
|
|
|
.rooms |
|
|
|
( |
|
|
|
.pdu_state_hash(&db.rooms.get_pdu_id(&id)?.unwrap_or_default())? |
|
|
|
event_id.clone(), |
|
|
|
{ |
|
|
|
Arc::new( |
|
|
|
// We found a common ancestor
|
|
|
|
// When creating a StateEvent the event_id arg will be used
|
|
|
|
Some(state_hash) => { |
|
|
|
// over any found in the json and it will not use ruma::reference_hash
|
|
|
|
seen_id = Some(id.clone()); |
|
|
|
// to generate one
|
|
|
|
seen = db.rooms.state_full(&state_hash)?; |
|
|
|
state_res::StateEvent::from_id_canon_obj(event_id, json) |
|
|
|
if let Some(pdu) = db.rooms.get_pdu(&id)? { |
|
|
|
.expect("valid pdu json"), |
|
|
|
if pdu.state_key.is_some() { |
|
|
|
), |
|
|
|
// This becomes the state after the common event
|
|
|
|
|
|
|
|
seen.insert( |
|
|
|
|
|
|
|
(pdu.kind.clone(), pdu.state_key.clone().unwrap()), |
|
|
|
|
|
|
|
pdu, |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break 'inner; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
None => match send_request( |
|
|
|
|
|
|
|
&db.globals, |
|
|
|
|
|
|
|
body.body.origin.clone(), |
|
|
|
|
|
|
|
ruma::api::federation::event::get_event::v1::Request::new(&id), |
|
|
|
) |
|
|
|
) |
|
|
|
}) |
|
|
|
.await |
|
|
|
.collect::<BTreeMap<_, _>>(); |
|
|
|
{ |
|
|
|
|
|
|
|
Ok(res) => { |
|
|
|
|
|
|
|
let (_, val) = crate::pdu::process_incoming_pdu(&res.pdu); |
|
|
|
|
|
|
|
let prev_pdu = serde_json::from_value::<PduEvent>( |
|
|
|
|
|
|
|
serde_json::to_value(&val) |
|
|
|
|
|
|
|
.expect("CanonicalJsonObj is a valid JsonValue"), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.expect("all ruma pdus are conduit pdus"); |
|
|
|
|
|
|
|
|
|
|
|
let our_current_state = db.rooms.room_state_full(room_id)?; |
|
|
|
// TODO: do we need this
|
|
|
|
// State resolution takes care of these checks
|
|
|
|
assert_eq!(room_id, &prev_pdu.room_id); |
|
|
|
// 4. Passes authorization rules based on the event's auth events, otherwise it is rejected.
|
|
|
|
|
|
|
|
// 5. Passes authorization rules based on the state at the event, otherwise it is rejected.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: 6. Passes authorization rules based on the current state of the room, otherwise it is "soft failed".
|
|
|
|
for id in &prev_pdu.prev_events { |
|
|
|
match state_res::StateResolution::resolve( |
|
|
|
prev_fork_ids.insert(id.clone()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
missing_fork.push(prev_pdu); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// We can't hard fail because there are some valid errors, just
|
|
|
|
|
|
|
|
Err(err) => { |
|
|
|
|
|
|
|
resolved_map.insert(event_id, Err(err.to_string())); |
|
|
|
|
|
|
|
// We have to give up on this PDU
|
|
|
|
|
|
|
|
continue 'outer; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
prev_ids.push(PrevEvents::new( |
|
|
|
|
|
|
|
&prev_fork_ids.into_iter().collect::<Vec<_>>(), |
|
|
|
|
|
|
|
)); |
|
|
|
|
|
|
|
missing.push(PrevEvents::new(&missing_fork)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// All done finding missing events
|
|
|
|
|
|
|
|
None => { |
|
|
|
|
|
|
|
break 'inner; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We can treat this event as sequential and simply apply it against the current state of the room
|
|
|
|
|
|
|
|
// because we know that state
|
|
|
|
|
|
|
|
if missing.is_empty() { |
|
|
|
|
|
|
|
// Back to the original incoming event
|
|
|
|
|
|
|
|
// If it is a non state event we still must add it and associate a statehash with the pdu_id
|
|
|
|
|
|
|
|
if value.get("state_key").is_none() { |
|
|
|
|
|
|
|
// TODO: Some auth needs to be done for non state events
|
|
|
|
|
|
|
|
if !db.rooms.is_joined(&pdu.sender, room_id)? { |
|
|
|
|
|
|
|
error!("Sender is not joined {}", pdu.kind); |
|
|
|
|
|
|
|
resolved_map.insert(event_id, Err("Sender not found in room".into())); |
|
|
|
|
|
|
|
continue 'outer; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
append_state(&db, &pdu)?; |
|
|
|
|
|
|
|
resolved_map.insert(event_id, Ok::<(), String>(())); |
|
|
|
|
|
|
|
continue 'outer; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
let incoming = pdu.convert_for_state_res(); |
|
|
|
|
|
|
|
match state_res::StateResolution::apply_event( |
|
|
|
room_id, |
|
|
|
room_id, |
|
|
|
&ruma::RoomVersionId::Version6, |
|
|
|
&RoomVersionId::Version6, |
|
|
|
&[ |
|
|
|
incoming.clone(), |
|
|
|
our_current_state |
|
|
|
&seen |
|
|
|
.iter() |
|
|
|
|
|
|
|
.map(|((ev, sk), v)| ((ev.clone(), sk.to_owned()), v.event_id.clone())) |
|
|
|
|
|
|
|
.collect::<BTreeMap<_, _>>(), |
|
|
|
|
|
|
|
their_current_state |
|
|
|
|
|
|
|
.iter() |
|
|
|
.iter() |
|
|
|
.map(|(_id, v)| ((v.kind(), v.state_key()), v.event_id())) |
|
|
|
.map(|(k, v)| (k.clone(), v.event_id.clone())) |
|
|
|
.collect::<BTreeMap<_, _>>(), |
|
|
|
.collect::<BTreeMap<_, _>>(), |
|
|
|
], |
|
|
|
|
|
|
|
Some( |
|
|
|
Some( |
|
|
|
our_current_state |
|
|
|
seen.iter() |
|
|
|
.iter() |
|
|
|
|
|
|
|
.map(|(_k, v)| (v.event_id.clone(), v.convert_for_state_res())) |
|
|
|
.map(|(_k, v)| (v.event_id.clone(), v.convert_for_state_res())) |
|
|
|
|
|
|
|
.collect::<BTreeMap<_, _>>(), |
|
|
|
|
|
|
|
), // TODO: make mut and keep around, this is all the auth events
|
|
|
|
|
|
|
|
&db.rooms, |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
Ok(true) => { |
|
|
|
|
|
|
|
append_state(&db, &pdu)?; |
|
|
|
|
|
|
|
resolved_map.insert(event_id, Ok::<(), String>(())); |
|
|
|
|
|
|
|
continue 'outer; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Ok(false) => { |
|
|
|
|
|
|
|
resolved_map.insert(event_id, Err("Failed event auth".to_string())); |
|
|
|
|
|
|
|
error!("Failed sequential event auth for incoming"); |
|
|
|
|
|
|
|
continue 'outer; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Err(err) => { |
|
|
|
|
|
|
|
resolved_map.insert(event_id, Err(err.to_string())); |
|
|
|
|
|
|
|
error!("{}", err); |
|
|
|
|
|
|
|
continue 'outer; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Well, now we have to actually do a bunch of work :(
|
|
|
|
|
|
|
|
// The steps are as follows
|
|
|
|
|
|
|
|
// 1. Rebuild the sending servers forward extremity, ignoring our own fork
|
|
|
|
|
|
|
|
// a) iterate "oldest" -> most recent authenticating each event with the state after the previous
|
|
|
|
|
|
|
|
// b) build a `snapshot_map` containing the state after the event for each missing event (EventId -> StateMap<EventId>)
|
|
|
|
|
|
|
|
// 2. Build our side of the fork (TODO do we have to re-auth these, is state at an event relative to the server its from)
|
|
|
|
|
|
|
|
// 3. resolve the two states (our current with the state after the most recent missing event)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Now build up state
|
|
|
|
|
|
|
|
let mut state_snapshot = seen |
|
|
|
|
|
|
|
.iter() |
|
|
|
|
|
|
|
.map(|(k, v)| (k.clone(), v.event_id.clone())) |
|
|
|
|
|
|
|
.collect::<BTreeMap<_, _>>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: So this is super memory inefficient we clone the state_snapshot every time
|
|
|
|
|
|
|
|
// we need it for state events and non
|
|
|
|
|
|
|
|
let mut snapshot_map = BTreeMap::new(); |
|
|
|
|
|
|
|
snapshot_map.insert(seen_id.clone().unwrap(), state_snapshot.clone()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let accum_event_map = seen |
|
|
|
|
|
|
|
.iter() |
|
|
|
|
|
|
|
.map(|(_, v)| (v.event_id.clone(), v.convert_for_state_res())) |
|
|
|
.chain( |
|
|
|
.chain( |
|
|
|
their_current_state |
|
|
|
missing |
|
|
|
.iter() |
|
|
|
.iter() |
|
|
|
.map(|(id, ev)| (id.clone(), ev.clone())), |
|
|
|
.cloned() |
|
|
|
|
|
|
|
.flatten() |
|
|
|
|
|
|
|
.map(|pdu| (pdu.event_id.clone(), pdu.convert_for_state_res())), |
|
|
|
) |
|
|
|
) |
|
|
|
.collect::<BTreeMap<_, _>>(), |
|
|
|
.collect::<BTreeMap<_, Arc<state_res::StateEvent>>>(); |
|
|
|
), |
|
|
|
|
|
|
|
|
|
|
|
// 4. Passes authorization rules based on the event's auth events, otherwise it is rejected.
|
|
|
|
|
|
|
|
// 5. Passes authorization rules based on the state at the event, otherwise it is rejected.
|
|
|
|
|
|
|
|
for missing_pdu in missing.iter().rev() { |
|
|
|
|
|
|
|
match missing_pdu { |
|
|
|
|
|
|
|
PrevEvents::Sequential(missing_pdu) => { |
|
|
|
|
|
|
|
// For state events
|
|
|
|
|
|
|
|
if missing_pdu.state_key.is_some() { |
|
|
|
|
|
|
|
let missing_pdu = missing_pdu.convert_for_state_res(); |
|
|
|
|
|
|
|
match state_res::StateResolution::apply_event( |
|
|
|
|
|
|
|
room_id, |
|
|
|
|
|
|
|
&RoomVersionId::Version6, |
|
|
|
|
|
|
|
missing_pdu.clone(), |
|
|
|
|
|
|
|
&state_snapshot, |
|
|
|
|
|
|
|
Some(accum_event_map.clone()), // TODO: make mut and check on Ok(..) ?
|
|
|
|
&db.rooms, |
|
|
|
&db.rooms, |
|
|
|
) { |
|
|
|
) { |
|
|
|
Ok(resolved) if resolved.values().any(|id| &event_id == id) => { |
|
|
|
Ok(true) => { |
|
|
|
// If the event is older than the last event in pduid_pdu Tree then find the
|
|
|
|
// TODO: do we need this
|
|
|
|
// closest ancestor we know of and insert after the known ancestor by
|
|
|
|
assert_eq!(room_id, missing_pdu.room_id()); |
|
|
|
// altering the known events pduid to = same roomID + same count bytes + 0x1
|
|
|
|
|
|
|
|
// pushing a single byte every time a simple append cannot be done.
|
|
|
|
// We can't add to DB yet since we don't know if it passes
|
|
|
|
match db.rooms.get_latest_pduid_before( |
|
|
|
// current room state
|
|
|
|
|
|
|
|
// append_state(&db, &PduEvent::from(&*missing_pdu))?;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Only after the state is recorded in the DB can we update the state_snapshot
|
|
|
|
|
|
|
|
// This will update the state snapshot so it is correct next loop through
|
|
|
|
|
|
|
|
state_snapshot.insert( |
|
|
|
|
|
|
|
(missing_pdu.kind(), missing_pdu.state_key()), |
|
|
|
|
|
|
|
missing_pdu.event_id(), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
// Keep track of the state after for resolution
|
|
|
|
|
|
|
|
snapshot_map.insert(missing_pdu.event_id(), state_snapshot.clone()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Ok(false) => { |
|
|
|
|
|
|
|
error!( |
|
|
|
|
|
|
|
"apply missing: {}", |
|
|
|
|
|
|
|
serde_json::to_string_pretty(&*missing_pdu).unwrap() |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Err(e) => { |
|
|
|
|
|
|
|
error!("{}", e); |
|
|
|
|
|
|
|
// This is not a fatal error but we do eventually need to handle
|
|
|
|
|
|
|
|
// events failing that are not the incoming events
|
|
|
|
|
|
|
|
// TODO: what to do when missing events fail (not incoming events)
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// TODO: Some auth needs to be done for non state events
|
|
|
|
|
|
|
|
if !db |
|
|
|
|
|
|
|
.rooms |
|
|
|
|
|
|
|
.is_joined(&missing_pdu.sender, &missing_pdu.room_id)? |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
error!("fork Sender is not joined {}", missing_pdu.kind); |
|
|
|
|
|
|
|
// TODO: we probably should not be getting events for different rooms
|
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: a better way to signal non state events...
|
|
|
|
|
|
|
|
snapshot_map.insert(missing_pdu.event_id.clone(), state_snapshot.clone()); |
|
|
|
|
|
|
|
// Continue this inner for loop adding all the missing events
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
PrevEvents::Fork(pdus) => { |
|
|
|
|
|
|
|
let mut state_sets = BTreeSet::new(); |
|
|
|
|
|
|
|
for pdu in pdus { |
|
|
|
|
|
|
|
let mut state_at = state_snapshot.clone(); |
|
|
|
|
|
|
|
if pdu.state_key.is_some() { |
|
|
|
|
|
|
|
state_at.insert( |
|
|
|
|
|
|
|
(pdu.kind.clone(), pdu.state_key.clone().unwrap()), |
|
|
|
|
|
|
|
pdu.event_id.clone(), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
state_sets.insert(state_at); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if state_sets.len() <= 1 { |
|
|
|
|
|
|
|
for missing_pdu in pdus.iter() { |
|
|
|
|
|
|
|
// For state events
|
|
|
|
|
|
|
|
if missing_pdu.state_key.is_some() { |
|
|
|
|
|
|
|
let missing_pdu = missing_pdu.convert_for_state_res(); |
|
|
|
|
|
|
|
match state_res::StateResolution::apply_event( |
|
|
|
|
|
|
|
room_id, |
|
|
|
|
|
|
|
&RoomVersionId::Version6, |
|
|
|
|
|
|
|
missing_pdu.clone(), |
|
|
|
|
|
|
|
&state_snapshot, |
|
|
|
|
|
|
|
Some(accum_event_map.clone()), // TODO: make mut and check on Ok(..) ?
|
|
|
|
|
|
|
|
&db.rooms, |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
Ok(true) => { |
|
|
|
|
|
|
|
// TODO: do we need this
|
|
|
|
|
|
|
|
assert_eq!(room_id, missing_pdu.room_id()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
state_snapshot.insert( |
|
|
|
|
|
|
|
(missing_pdu.kind(), missing_pdu.state_key()), |
|
|
|
|
|
|
|
missing_pdu.event_id(), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
snapshot_map |
|
|
|
|
|
|
|
.insert(missing_pdu.event_id(), state_snapshot.clone()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Ok(false) => { |
|
|
|
|
|
|
|
error!( |
|
|
|
|
|
|
|
"apply missing fork: {}", |
|
|
|
|
|
|
|
serde_json::to_string_pretty(&*missing_pdu).unwrap() |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Err(e) => { |
|
|
|
|
|
|
|
error!("fork state-res: {}", e); |
|
|
|
|
|
|
|
// TODO: what to do when missing events fail (not incoming events)
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// TODO: Some auth needs to be done for non state events
|
|
|
|
|
|
|
|
if !db |
|
|
|
|
|
|
|
.rooms |
|
|
|
|
|
|
|
.is_joined(&missing_pdu.sender, &missing_pdu.room_id)? |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
error!("fork Sender is not joined {}", missing_pdu.kind); |
|
|
|
|
|
|
|
// TODO: we probably should not be getting events for different rooms
|
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
snapshot_map |
|
|
|
|
|
|
|
.insert(missing_pdu.event_id.clone(), state_snapshot.clone()); |
|
|
|
|
|
|
|
// Continue this inner for loop adding all the missing events
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
match state_res::StateResolution::resolve( |
|
|
|
room_id, |
|
|
|
room_id, |
|
|
|
&pdu.prev_events, |
|
|
|
&RoomVersionId::Version6, |
|
|
|
&their_current_state, |
|
|
|
&state_sets.into_iter().collect::<Vec<_>>(), |
|
|
|
)? { |
|
|
|
Some(accum_event_map.clone()), // TODO: make mut and check on Ok(..) ?
|
|
|
|
Some(ClosestParent::Append) => { |
|
|
|
&db.rooms, |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
Ok(resolved) => { |
|
|
|
|
|
|
|
for id in resolved |
|
|
|
|
|
|
|
.values() |
|
|
|
|
|
|
|
.filter(|id| pdus.iter().any(|pduid| pduid.event_id == **id)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
snapshot_map.insert(id.clone(), resolved.clone()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Err(err) => { |
|
|
|
|
|
|
|
error!("{}", err); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We know these events are valid for each state at the event
|
|
|
|
|
|
|
|
// we have already authed them
|
|
|
|
|
|
|
|
let known_fork = if let Some(id) = seen_id { |
|
|
|
|
|
|
|
db.rooms.state_from(room_id, &id)? |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
vec![] |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
for pdu in known_fork.clone().into_iter().rev() { |
|
|
|
|
|
|
|
if pdu.state_key.is_some() { |
|
|
|
|
|
|
|
seen.insert((pdu.kind.clone(), pdu.state_key.clone().unwrap()), pdu); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let current_room_state = seen |
|
|
|
|
|
|
|
.into_iter() |
|
|
|
|
|
|
|
.map(|(k, v)| (k, v.event_id)) |
|
|
|
|
|
|
|
.collect::<BTreeMap<_, _>>(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut state_sets = BTreeSet::new(); |
|
|
|
|
|
|
|
state_sets.insert(current_room_state.clone()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The first item is the most recent
|
|
|
|
|
|
|
|
if let Some(fork) = missing.first() { |
|
|
|
|
|
|
|
for pdu in fork.clone().into_iter() { |
|
|
|
|
|
|
|
if let Some(map) = snapshot_map.get(&pdu.event_id) { |
|
|
|
|
|
|
|
state_sets.insert(map.clone()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut state_sets = state_sets.into_iter().collect::<Vec<_>>(); |
|
|
|
|
|
|
|
// If we have actual differences resolve
|
|
|
|
|
|
|
|
if state_sets.len() > 1 { |
|
|
|
|
|
|
|
// Set the incoming event to have parents from both forks
|
|
|
|
|
|
|
|
// ours/theirs
|
|
|
|
|
|
|
|
pdu.prev_events = missing |
|
|
|
|
|
|
|
.first() |
|
|
|
|
|
|
|
.cloned() |
|
|
|
|
|
|
|
.into_iter() |
|
|
|
|
|
|
|
.flatten() |
|
|
|
|
|
|
|
.map(|pdu| pdu.event_id) |
|
|
|
|
|
|
|
.chain(known_fork.first().cloned().map(|pdu| pdu.event_id)) |
|
|
|
|
|
|
|
.collect(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The incoming event is a child of both forks now
|
|
|
|
|
|
|
|
for set in &mut state_sets { |
|
|
|
|
|
|
|
set.insert( |
|
|
|
|
|
|
|
(pdu.kind.clone(), pdu.state_key.clone().unwrap()), |
|
|
|
|
|
|
|
pdu.event_id.clone(), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// If we have holes or a fork I am less sure what can be guaranteed about our state?
|
|
|
|
|
|
|
|
// Or what must be done to fix holes and forks?
|
|
|
|
|
|
|
|
match state_res::StateResolution::resolve( |
|
|
|
|
|
|
|
room_id, |
|
|
|
|
|
|
|
&RoomVersionId::Version6, |
|
|
|
|
|
|
|
&state_sets, |
|
|
|
|
|
|
|
Some(accum_event_map.clone()), // TODO: make mut and check on Ok(..) ?
|
|
|
|
|
|
|
|
&db.rooms, |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
Ok(resolved) if resolved.values().any(|id| id == &event_id) => { |
|
|
|
|
|
|
|
for resolved_missing in missing |
|
|
|
|
|
|
|
.into_iter() |
|
|
|
|
|
|
|
.rev() // We want the oldest pdu's first
|
|
|
|
|
|
|
|
.flatten() |
|
|
|
|
|
|
|
.filter(|pdu| resolved.values().any(|res_id| res_id == &pdu.event_id)) |
|
|
|
|
|
|
|
{ |
|
|
|
let count = db.globals.next_count()?; |
|
|
|
let count = db.globals.next_count()?; |
|
|
|
let mut pdu_id = room_id.as_bytes().to_vec(); |
|
|
|
let mut pdu_id = room_id.as_bytes().to_vec(); |
|
|
|
pdu_id.push(0xff); |
|
|
|
pdu_id.push(0xff); |
|
|
|
pdu_id.extend_from_slice(&count.to_be_bytes()); |
|
|
|
pdu_id.extend_from_slice(&count.to_be_bytes()); |
|
|
|
|
|
|
|
|
|
|
|
db.rooms.append_pdu( |
|
|
|
db.rooms.append_pdu( |
|
|
|
&pdu, |
|
|
|
&resolved_missing, |
|
|
|
&value, |
|
|
|
&crate::utils::to_canonical_object(&resolved_missing) |
|
|
|
|
|
|
|
.expect("PDU is valid canonical JSON"), |
|
|
|
count, |
|
|
|
count, |
|
|
|
pdu_id.into(), |
|
|
|
pdu_id.into(), |
|
|
|
&db.globals, |
|
|
|
&db.globals, |
|
|
|
@ -595,15 +968,33 @@ pub async fn send_transaction_message_route<'a>( |
|
|
|
&db.admin, |
|
|
|
&db.admin, |
|
|
|
)?; |
|
|
|
)?; |
|
|
|
} |
|
|
|
} |
|
|
|
Some(ClosestParent::Insert(old_count)) => { |
|
|
|
|
|
|
|
let count = old_count; |
|
|
|
let count = db.globals.next_count()?; |
|
|
|
let mut pdu_id = room_id.as_bytes().to_vec(); |
|
|
|
let mut pdu_id = room_id.as_bytes().to_vec(); |
|
|
|
pdu_id.push(0xff); |
|
|
|
pdu_id.push(0xff); |
|
|
|
pdu_id.extend_from_slice(&count.to_be_bytes()); |
|
|
|
pdu_id.extend_from_slice(&count.to_be_bytes()); |
|
|
|
// Create a new count that is after old_count but before
|
|
|
|
|
|
|
|
// the pdu appended after
|
|
|
|
|
|
|
|
pdu_id.push(1); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Since we know the state we force_state
|
|
|
|
|
|
|
|
// saving the incoming pdu's id with our new state
|
|
|
|
|
|
|
|
db.rooms.force_state_with_pdu( |
|
|
|
|
|
|
|
&*pdu_id, |
|
|
|
|
|
|
|
room_id, |
|
|
|
|
|
|
|
resolved |
|
|
|
|
|
|
|
.iter() |
|
|
|
|
|
|
|
.map(|(k, v)| { |
|
|
|
|
|
|
|
( |
|
|
|
|
|
|
|
k.clone(), |
|
|
|
|
|
|
|
serde_json::to_vec( |
|
|
|
|
|
|
|
&db.rooms |
|
|
|
|
|
|
|
.get_pdu(v) |
|
|
|
|
|
|
|
.expect("db err") |
|
|
|
|
|
|
|
.expect("we know of all the pdus"), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.expect("serde can serialize pdus"), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
.collect(), |
|
|
|
|
|
|
|
)?; |
|
|
|
db.rooms.append_pdu( |
|
|
|
db.rooms.append_pdu( |
|
|
|
&pdu, |
|
|
|
&pdu, |
|
|
|
&value, |
|
|
|
&value, |
|
|
|
@ -613,29 +1004,101 @@ pub async fn send_transaction_message_route<'a>( |
|
|
|
&db.account_data, |
|
|
|
&db.account_data, |
|
|
|
&db.admin, |
|
|
|
&db.admin, |
|
|
|
)?; |
|
|
|
)?; |
|
|
|
|
|
|
|
resolved_map.insert(event_id, Ok::<(), String>(())); |
|
|
|
} |
|
|
|
} |
|
|
|
_ => { |
|
|
|
Ok(resolved) => { |
|
|
|
error!("Not a sequential event or no parents found"); |
|
|
|
for resolved_missing in missing |
|
|
|
continue; |
|
|
|
.into_iter() |
|
|
|
|
|
|
|
.flatten() |
|
|
|
|
|
|
|
.filter(|pdu| resolved.values().any(|res_id| res_id == &pdu.event_id)) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
let count = db.globals.next_count()?; |
|
|
|
|
|
|
|
let mut pdu_id = room_id.as_bytes().to_vec(); |
|
|
|
|
|
|
|
pdu_id.push(0xff); |
|
|
|
|
|
|
|
pdu_id.extend_from_slice(&count.to_be_bytes()); |
|
|
|
|
|
|
|
db.rooms.append_pdu( |
|
|
|
|
|
|
|
&resolved_missing, |
|
|
|
|
|
|
|
&crate::utils::to_canonical_object(&resolved_missing) |
|
|
|
|
|
|
|
.expect("PDU is valid canonical JSON"), |
|
|
|
|
|
|
|
count, |
|
|
|
|
|
|
|
pdu_id.into(), |
|
|
|
|
|
|
|
&db.globals, |
|
|
|
|
|
|
|
&db.account_data, |
|
|
|
|
|
|
|
&db.admin, |
|
|
|
|
|
|
|
)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
resolved_map.insert(event_id, Err("Failed event auth".into())); |
|
|
|
|
|
|
|
error!( |
|
|
|
|
|
|
|
"auth failed: {}", |
|
|
|
|
|
|
|
serde_json::to_string_pretty(&pdu).unwrap() |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Err(err) => { |
|
|
|
|
|
|
|
resolved_map.insert(event_id, Err(err.to_string())); |
|
|
|
|
|
|
|
error!("{}", err); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// If we have iterated through the incoming missing events sequentially we know that
|
|
|
|
|
|
|
|
// the original incoming event is the youngest child and so can be simply authed and appended
|
|
|
|
|
|
|
|
// to the state
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// If it is a non state event we still must add it and associate a statehash with the pdu_id
|
|
|
|
|
|
|
|
if pdu.state_key.is_none() { |
|
|
|
|
|
|
|
// TODO: Some auth needs to be done for non state events
|
|
|
|
|
|
|
|
if !db.rooms.is_joined(&pdu.sender, room_id)? { |
|
|
|
|
|
|
|
error!("Sender is not joined {}", pdu.kind); |
|
|
|
|
|
|
|
resolved_map.insert(event_id, Err("Sender not found in room".into())); |
|
|
|
|
|
|
|
continue 'outer; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
append_state(&db, &pdu)?; |
|
|
|
|
|
|
|
resolved_map.insert(event_id, Ok::<(), String>(())); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
let incoming = pdu.convert_for_state_res(); |
|
|
|
|
|
|
|
match state_res::StateResolution::apply_event( |
|
|
|
|
|
|
|
room_id, |
|
|
|
|
|
|
|
&RoomVersionId::Version6, |
|
|
|
|
|
|
|
incoming, |
|
|
|
|
|
|
|
¤t_room_state, |
|
|
|
|
|
|
|
Some(accum_event_map), // TODO: make mut and check on Ok(..) ?
|
|
|
|
|
|
|
|
&db.rooms, |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
Ok(true) => { |
|
|
|
|
|
|
|
append_state(&db, &pdu)?; |
|
|
|
resolved_map.insert(event_id, Ok::<(), String>(())); |
|
|
|
resolved_map.insert(event_id, Ok::<(), String>(())); |
|
|
|
} |
|
|
|
} |
|
|
|
// If the eventId is not found in the resolved state auth has failed
|
|
|
|
Ok(false) => { |
|
|
|
Ok(_) => { |
|
|
|
resolved_map.insert(event_id, Err("Failed event auth".to_string())); |
|
|
|
resolved_map.insert( |
|
|
|
error!("Failed sequential event auth for incoming"); |
|
|
|
event_id, |
|
|
|
} |
|
|
|
Err("This event failed authentication, not found in resolved set".into()), |
|
|
|
Err(err) => { |
|
|
|
); |
|
|
|
resolved_map.insert(event_id, Err(err.to_string())); |
|
|
|
|
|
|
|
error!("{}", err); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
Err(e) => { |
|
|
|
|
|
|
|
resolved_map.insert(event_id, Err(e.to_string())); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Ok(send_transaction_message::v1::Response { pdus: resolved_map }.into()) |
|
|
|
Ok(dbg!(send_transaction_message::v1::Response { pdus: resolved_map }).into()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn append_state(db: &Database, pdu: &PduEvent) -> Result<()> { |
|
|
|
|
|
|
|
let count = db.globals.next_count()?; |
|
|
|
|
|
|
|
let mut pdu_id = pdu.room_id.as_bytes().to_vec(); |
|
|
|
|
|
|
|
pdu_id.push(0xff); |
|
|
|
|
|
|
|
pdu_id.extend_from_slice(&count.to_be_bytes()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
db.rooms.append_to_state(&pdu_id, pdu)?; |
|
|
|
|
|
|
|
db.rooms.append_pdu( |
|
|
|
|
|
|
|
pdu, |
|
|
|
|
|
|
|
&utils::to_canonical_object(pdu).expect("Pdu is valid canonical object"), |
|
|
|
|
|
|
|
count, |
|
|
|
|
|
|
|
pdu_id.clone().into(), |
|
|
|
|
|
|
|
&db.globals, |
|
|
|
|
|
|
|
&db.account_data, |
|
|
|
|
|
|
|
&db.admin, |
|
|
|
|
|
|
|
) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[cfg_attr(
|
|
|
|
#[cfg_attr(
|
|
|
|
|