|
|
|
@ -24,7 +24,7 @@ use ruma::{ |
|
|
|
EventId, RoomId, RoomVersionId, 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 +390,22 @@ pub async fn get_public_rooms_route( |
|
|
|
.into()) |
|
|
|
.into()) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Ord, PartialOrd, Eq, PartialEq)] |
|
|
|
|
|
|
|
pub enum PrevEvents<T> { |
|
|
|
|
|
|
|
Sequential(T), |
|
|
|
|
|
|
|
Fork(Vec<T>), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>") |
|
|
|
@ -475,78 +491,140 @@ pub async fn send_transaction_message_route<'a>( |
|
|
|
|
|
|
|
|
|
|
|
// The events that must be resolved to catch up to the incoming event
|
|
|
|
// The events that must be resolved to catch up to the incoming event
|
|
|
|
let mut missing = vec![]; |
|
|
|
let mut missing = vec![]; |
|
|
|
let mut seen = BTreeMap::new(); |
|
|
|
let mut seen = state_res::StateMap::new(); |
|
|
|
|
|
|
|
|
|
|
|
let mut prev_ids = pdu.prev_events.to_vec(); |
|
|
|
|
|
|
|
// Don't kill our server with state-res
|
|
|
|
|
|
|
|
// TODO: set this at a reasonable level this is for debug/wip purposes
|
|
|
|
|
|
|
|
if prev_ids.len() > 5 { |
|
|
|
|
|
|
|
resolved_map.insert( |
|
|
|
|
|
|
|
event_id, |
|
|
|
|
|
|
|
Err("Event has abnormally large prev_events count".into()), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This is `while let Some(event_id) = prev_ids.pop()` but with a fancy continue
|
|
|
|
let mut prev_ids = vec![PrevEvents::new(&pdu.prev_events)]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// This is `while let Some(event_id) = prev_ids.pop_front()` but with a fancy continue
|
|
|
|
// in the case of a failed request to the server that sent the event
|
|
|
|
// in the case of a failed request to the server that sent the event
|
|
|
|
'inner: loop { |
|
|
|
'inner: loop { |
|
|
|
let id = if let Some(id) = prev_ids.pop() { |
|
|
|
// TODO: if this is ever more that 1 at a time we must do actual
|
|
|
|
id |
|
|
|
// full state resolution not just auth
|
|
|
|
} else { |
|
|
|
match prev_ids.pop() { |
|
|
|
break 'inner; |
|
|
|
Some(PrevEvents::Sequential(id)) => match db |
|
|
|
}; |
|
|
|
.rooms |
|
|
|
|
|
|
|
.pdu_state_hash(&db.rooms.get_pdu_id(&id)?.unwrap_or_default())? |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// We know the state snapshot for this events parents so we can simply auth the
|
|
|
|
|
|
|
|
// incoming event and append to DB and append to state if it passes
|
|
|
|
|
|
|
|
Some(state_hash) => { |
|
|
|
|
|
|
|
seen = db.rooms.state_full(&state_hash)?; |
|
|
|
|
|
|
|
break 'inner; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// We need to fill in information about this event's `prev_events` (parents)
|
|
|
|
|
|
|
|
None => { |
|
|
|
|
|
|
|
match send_request( |
|
|
|
|
|
|
|
&db.globals, |
|
|
|
|
|
|
|
body.body.origin.clone(), |
|
|
|
|
|
|
|
ruma::api::federation::event::get_event::v1::Request::new(&id), |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
.await |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// keep checking PDU's
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// As an example a possible error
|
|
|
|
|
|
|
|
// {"errcode":"M_FORBIDDEN","error":"Host not in room."}
|
|
|
|
|
|
|
|
Err(err) => { |
|
|
|
|
|
|
|
resolved_map.insert(event_id, Err(err.to_string())); |
|
|
|
|
|
|
|
// 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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
match db |
|
|
|
// We want this to stay unique incase the fork comes together?
|
|
|
|
.rooms |
|
|
|
let mut prev_fork_ids = BTreeSet::new(); |
|
|
|
.pdu_state_hash(&db.rooms.get_pdu_id(&id)?.unwrap_or_default())? |
|
|
|
let mut missing_fork = vec![]; |
|
|
|
{ |
|
|
|
for id in &ids { |
|
|
|
// We know the state snapshot for this events parents so we can simply auth the
|
|
|
|
match db |
|
|
|
// incoming event and append to DB and append to state if it passes
|
|
|
|
.rooms |
|
|
|
Some(state_hash) => { |
|
|
|
.pdu_state_hash(&db.rooms.get_pdu_id(&id)?.unwrap_or_default())? |
|
|
|
seen = db.rooms.state_full(&state_hash)?; |
|
|
|
{ |
|
|
|
break 'inner; |
|
|
|
// We know the state snapshot for this events parents so we can simply auth the
|
|
|
|
} |
|
|
|
// incoming event and append to DB and append to state if it passes
|
|
|
|
// We need to fill in information about this event's `prev_events` (parents)
|
|
|
|
Some(state_hash) => { |
|
|
|
None => { |
|
|
|
seen = db.rooms.state_full(&state_hash)?; |
|
|
|
// We have a state event so we need info for state-res
|
|
|
|
break 'inner; |
|
|
|
match send_request( |
|
|
|
} |
|
|
|
&db.globals, |
|
|
|
None => match send_request( |
|
|
|
body.body.origin.clone(), |
|
|
|
&db.globals, |
|
|
|
ruma::api::federation::event::get_event::v1::Request::new(&id), |
|
|
|
body.body.origin.clone(), |
|
|
|
) |
|
|
|
ruma::api::federation::event::get_event::v1::Request::new(&id), |
|
|
|
.await |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
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"); |
|
|
|
.await |
|
|
|
|
|
|
|
{ |
|
|
|
// TODO: do we need this
|
|
|
|
Ok(res) => { |
|
|
|
assert_eq!(room_id, &prev_pdu.room_id); |
|
|
|
let (_, val) = crate::pdu::process_incoming_pdu(&res.pdu); |
|
|
|
|
|
|
|
let prev_pdu = serde_json::from_value::<PduEvent>( |
|
|
|
prev_ids.extend(prev_pdu.prev_events.to_vec()); |
|
|
|
serde_json::to_value(&val) |
|
|
|
missing.push(prev_pdu); |
|
|
|
.expect("CanonicalJsonObj is a valid JsonValue"), |
|
|
|
} |
|
|
|
) |
|
|
|
// We can't hard fail because there are some valid errors, just
|
|
|
|
.expect("all ruma pdus are conduit pdus"); |
|
|
|
// keep checking PDU's
|
|
|
|
|
|
|
|
//
|
|
|
|
// TODO: do we need this
|
|
|
|
// As an example a possible error
|
|
|
|
assert_eq!(room_id, &prev_pdu.room_id); |
|
|
|
// {"errcode":"M_FORBIDDEN","error":"Host not in room."}
|
|
|
|
|
|
|
|
Err(err) => { |
|
|
|
for id in &prev_pdu.prev_events { |
|
|
|
resolved_map.insert(event_id, Err(err.to_string())); |
|
|
|
prev_fork_ids.insert(id.clone()); |
|
|
|
// We have to give up on this PDU
|
|
|
|
} |
|
|
|
continue 'outer; |
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Now build up
|
|
|
|
// Now build up state
|
|
|
|
let mut state_snapshot = seen |
|
|
|
let mut state_snapshot = seen |
|
|
|
.iter() |
|
|
|
.iter() |
|
|
|
.map(|(k, v)| (k.clone(), v.event_id.clone())) |
|
|
|
.map(|(k, v)| (k.clone(), v.event_id.clone())) |
|
|
|
@ -556,124 +634,222 @@ pub async fn send_transaction_message_route<'a>( |
|
|
|
.map(|(_, v)| (v.event_id.clone(), v.convert_for_state_res())) |
|
|
|
.map(|(_, v)| (v.event_id.clone(), v.convert_for_state_res())) |
|
|
|
.collect::<BTreeMap<_, Arc<state_res::StateEvent>>>(); |
|
|
|
.collect::<BTreeMap<_, Arc<state_res::StateEvent>>>(); |
|
|
|
// TODO: this only accounts for sequentially missing events no holes will be filled
|
|
|
|
// TODO: this only accounts for sequentially missing events no holes will be filled
|
|
|
|
// and I'm still not sure what happens when fork introduces multiple `prev_events`
|
|
|
|
// and I'm still not sure what happens when a fork introduces multiple `prev_events`
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// We need to go from oldest (furthest ancestor of the incoming event) to the
|
|
|
|
// We need to go from oldest (furthest ancestor of the incoming event) to the
|
|
|
|
// prev_event of the incoming event so we reverse the order oldest -> most recent
|
|
|
|
// prev_event of the incoming event so we reverse the order oldest -> most recent
|
|
|
|
for missing_pdu in missing.into_iter().rev() { |
|
|
|
for missing_pdu in missing.into_iter().rev() { |
|
|
|
// For state events
|
|
|
|
match missing_pdu { |
|
|
|
if missing_pdu.state_key.is_some() { |
|
|
|
PrevEvents::Sequential(missing_pdu) => { |
|
|
|
let missing_pdu = missing_pdu.convert_for_state_res(); |
|
|
|
// For state events
|
|
|
|
match state_res::StateResolution::apply_event( |
|
|
|
if missing_pdu.state_key.is_some() { |
|
|
|
room_id, |
|
|
|
let missing_pdu = missing_pdu.convert_for_state_res(); |
|
|
|
&RoomVersionId::Version6, |
|
|
|
match state_res::StateResolution::apply_event( |
|
|
|
missing_pdu.clone(), |
|
|
|
room_id, |
|
|
|
&state_snapshot, |
|
|
|
&RoomVersionId::Version6, |
|
|
|
Some(accum_event_map.clone()), // TODO: make mut and check on Ok(..) ?
|
|
|
|
missing_pdu.clone(), |
|
|
|
&db.rooms, |
|
|
|
&state_snapshot, |
|
|
|
) { |
|
|
|
Some(accum_event_map.clone()), // TODO: make mut and check on Ok(..) ?
|
|
|
|
Ok(true) => { |
|
|
|
&db.rooms, |
|
|
|
// TODO: do we need this
|
|
|
|
) { |
|
|
|
assert_eq!(room_id, missing_pdu.room_id()); |
|
|
|
Ok(true) => { |
|
|
|
|
|
|
|
// TODO: do we need this
|
|
|
|
|
|
|
|
assert_eq!(room_id, missing_pdu.room_id()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let count = db.globals.next_count()?; |
|
|
|
|
|
|
|
let mut pdu_id = missing_pdu.room_id().as_bytes().to_vec(); |
|
|
|
|
|
|
|
pdu_id.push(0xff); |
|
|
|
|
|
|
|
pdu_id.extend_from_slice(&count.to_be_bytes()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Since we know the state from event to event we can do the
|
|
|
|
|
|
|
|
// // slightly more efficient force_state
|
|
|
|
|
|
|
|
// db.rooms.force_state(
|
|
|
|
|
|
|
|
// room_id,
|
|
|
|
|
|
|
|
// state_snapshot
|
|
|
|
|
|
|
|
// .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_to_state(&pdu_id, &PduEvent::from(&*missing_pdu))?; |
|
|
|
|
|
|
|
// Now append the new missing event to DB it will be part of the
|
|
|
|
|
|
|
|
// next events state_snapshot
|
|
|
|
|
|
|
|
db.rooms.append_pdu( |
|
|
|
|
|
|
|
&PduEvent::from(&*missing_pdu), |
|
|
|
|
|
|
|
&utils::to_canonical_object(&*missing_pdu) |
|
|
|
|
|
|
|
.expect("Pdu is valid canonical object"), |
|
|
|
|
|
|
|
count, |
|
|
|
|
|
|
|
pdu_id.clone().into(), |
|
|
|
|
|
|
|
&db.globals, |
|
|
|
|
|
|
|
&db.account_data, |
|
|
|
|
|
|
|
&db.admin, |
|
|
|
|
|
|
|
)?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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(), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
accum_event_map.insert(missing_pdu.event_id(), missing_pdu); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// All events between the event we know about and the incoming event need to be accounted
|
|
|
|
|
|
|
|
// for
|
|
|
|
|
|
|
|
} 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!("Sender is not joined {}", missing_pdu.kind); |
|
|
|
|
|
|
|
// TODO: we probably should not be getting events for different rooms
|
|
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
// I think we need to keep going until we reach
|
|
|
|
|
|
|
|
// the incoming pdu so do not error here
|
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let count = db.globals.next_count()?; |
|
|
|
let count = db.globals.next_count()?; |
|
|
|
let mut pdu_id = missing_pdu.room_id().as_bytes().to_vec(); |
|
|
|
let mut pdu_id = missing_pdu.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()); |
|
|
|
|
|
|
|
|
|
|
|
// Since we know the state from event to event we can do the
|
|
|
|
db.rooms.append_to_state(&pdu_id, &missing_pdu)?; |
|
|
|
// slightly more efficient force_state
|
|
|
|
|
|
|
|
db.rooms.force_state( |
|
|
|
|
|
|
|
room_id, |
|
|
|
|
|
|
|
state_snapshot |
|
|
|
|
|
|
|
.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(), |
|
|
|
|
|
|
|
)?; |
|
|
|
|
|
|
|
// Now append the new missing event to DB it will be part of the
|
|
|
|
|
|
|
|
// next events state_snapshot
|
|
|
|
|
|
|
|
db.rooms.append_pdu( |
|
|
|
db.rooms.append_pdu( |
|
|
|
&PduEvent::from(&*missing_pdu), |
|
|
|
&missing_pdu, |
|
|
|
&utils::to_canonical_object(&*missing_pdu) |
|
|
|
&utils::to_canonical_object(&missing_pdu) |
|
|
|
.expect("Pdu is valid canonical object"), |
|
|
|
.expect("Pdu is valid canonical object"), |
|
|
|
count, |
|
|
|
count, |
|
|
|
pdu_id.clone().into(), |
|
|
|
pdu_id.into(), |
|
|
|
&db.globals, |
|
|
|
&db.globals, |
|
|
|
&db.account_data, |
|
|
|
&db.account_data, |
|
|
|
&db.admin, |
|
|
|
&db.admin, |
|
|
|
)?; |
|
|
|
)?; |
|
|
|
|
|
|
|
// Continue this inner for loop adding all the missing events
|
|
|
|
// 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(), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
accum_event_map.insert(missing_pdu.event_id(), missing_pdu); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Ok(false) => { |
|
|
|
|
|
|
|
error!("{:?}", missing_pdu); |
|
|
|
|
|
|
|
continue; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Err(e) => { |
|
|
|
|
|
|
|
error!("{}", e); |
|
|
|
|
|
|
|
// I think we need to keep going until we reach
|
|
|
|
|
|
|
|
// the incoming pdu so do not error here
|
|
|
|
|
|
|
|
continue; // The continue is not needed but to remind us that this is not an error
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// All between the event we know about and the incoming event need to be accounted
|
|
|
|
PrevEvents::Fork(pdus) => { |
|
|
|
// for
|
|
|
|
for missing_pdu in pdus.into_iter().rev() { |
|
|
|
} else { |
|
|
|
// For state events
|
|
|
|
// TODO: Some auth needs to be done for non state events
|
|
|
|
if missing_pdu.state_key.is_some() { |
|
|
|
if !db |
|
|
|
let missing_pdu = missing_pdu.convert_for_state_res(); |
|
|
|
.rooms |
|
|
|
match state_res::StateResolution::apply_event( |
|
|
|
.is_joined(&missing_pdu.sender, &missing_pdu.room_id)? |
|
|
|
room_id, |
|
|
|
{ |
|
|
|
&RoomVersionId::Version6, |
|
|
|
error!("Sender is not joined {}", missing_pdu.kind); |
|
|
|
missing_pdu.clone(), |
|
|
|
// I think we need to keep going until we reach
|
|
|
|
&state_snapshot, |
|
|
|
// the incoming pdu so do not error here
|
|
|
|
Some(accum_event_map.clone()), // TODO: make mut and check on Ok(..) ?
|
|
|
|
continue; |
|
|
|
&db.rooms, |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
Ok(true) => { |
|
|
|
|
|
|
|
// TODO: do we need this
|
|
|
|
|
|
|
|
assert_eq!(room_id, missing_pdu.room_id()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let count = db.globals.next_count()?; |
|
|
|
|
|
|
|
let mut pdu_id = missing_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, &PduEvent::from(&*missing_pdu))?; |
|
|
|
|
|
|
|
db.rooms.append_pdu( |
|
|
|
|
|
|
|
&PduEvent::from(&*missing_pdu), |
|
|
|
|
|
|
|
&utils::to_canonical_object(&*missing_pdu) |
|
|
|
|
|
|
|
.expect("Pdu is valid canonical object"), |
|
|
|
|
|
|
|
count, |
|
|
|
|
|
|
|
pdu_id.clone().into(), |
|
|
|
|
|
|
|
&db.globals, |
|
|
|
|
|
|
|
&db.account_data, |
|
|
|
|
|
|
|
&db.admin, |
|
|
|
|
|
|
|
)?; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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(), |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
accum_event_map.insert(missing_pdu.event_id(), missing_pdu); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let count = db.globals.next_count()?; |
|
|
|
|
|
|
|
let mut pdu_id = missing_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, &missing_pdu)?; |
|
|
|
|
|
|
|
db.rooms.append_pdu( |
|
|
|
|
|
|
|
&missing_pdu, |
|
|
|
|
|
|
|
&utils::to_canonical_object(&missing_pdu) |
|
|
|
|
|
|
|
.expect("Pdu is valid canonical object"), |
|
|
|
|
|
|
|
count, |
|
|
|
|
|
|
|
pdu_id.into(), |
|
|
|
|
|
|
|
&db.globals, |
|
|
|
|
|
|
|
&db.account_data, |
|
|
|
|
|
|
|
&db.admin, |
|
|
|
|
|
|
|
)?; |
|
|
|
|
|
|
|
// Continue this inner for loop adding all the missing events
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let count = db.globals.next_count()?; |
|
|
|
|
|
|
|
let mut pdu_id = missing_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, &missing_pdu)?; |
|
|
|
|
|
|
|
db.rooms.append_pdu( |
|
|
|
|
|
|
|
&missing_pdu, |
|
|
|
|
|
|
|
&value, |
|
|
|
|
|
|
|
count, |
|
|
|
|
|
|
|
pdu_id.into(), |
|
|
|
|
|
|
|
&db.globals, |
|
|
|
|
|
|
|
&db.account_data, |
|
|
|
|
|
|
|
&db.admin, |
|
|
|
|
|
|
|
)?; |
|
|
|
|
|
|
|
// Continue this inner for loop adding all the missing events
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Back to the original incoming event
|
|
|
|
// Back to the original incoming event
|
|
|
|
// If it is a non state event we still must add it and create a statehash for it
|
|
|
|
// 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() { |
|
|
|
if value.get("state_key").is_none() { |
|
|
|
// TODO: Some auth needs to be done for non state events
|
|
|
|
// TODO: Some auth needs to be done for non state events
|
|
|
|
if !db.rooms.is_joined(&pdu.sender, room_id)? { |
|
|
|
if !db.rooms.is_joined(&pdu.sender, room_id)? { |
|
|
|
error!("Sender is not joined {}", pdu.kind); |
|
|
|
error!("Sender is not joined {}", pdu.kind); |
|
|
|
// I think we need to keep going until we reach
|
|
|
|
resolved_map.insert(event_id, Err("Sender not found in room".into())); |
|
|
|
// the incoming pdu so do not error here
|
|
|
|
|
|
|
|
continue; |
|
|
|
continue; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -700,6 +876,7 @@ pub async fn send_transaction_message_route<'a>( |
|
|
|
// the original incoming event is the youngest child and so can be simply authed and append
|
|
|
|
// the original incoming event is the youngest child and so can be simply authed and append
|
|
|
|
// to the state
|
|
|
|
// to the state
|
|
|
|
// If we have holes or a fork I am less sure what can be guaranteed about our state?
|
|
|
|
// 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::apply_event( |
|
|
|
match state_res::StateResolution::apply_event( |
|
|
|
room_id, |
|
|
|
room_id, |
|
|
|
&RoomVersionId::Version6, |
|
|
|
&RoomVersionId::Version6, |
|
|
|
@ -728,7 +905,10 @@ pub async fn send_transaction_message_route<'a>( |
|
|
|
} |
|
|
|
} |
|
|
|
Ok(false) => { |
|
|
|
Ok(false) => { |
|
|
|
resolved_map.insert(event_id, Err("Failed event auth".into())); |
|
|
|
resolved_map.insert(event_id, Err("Failed event auth".into())); |
|
|
|
error!("{:?}", pdu); |
|
|
|
error!( |
|
|
|
|
|
|
|
"auth failed: {}", |
|
|
|
|
|
|
|
serde_json::to_string_pretty(&pdu).unwrap() |
|
|
|
|
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
Err(err) => { |
|
|
|
Err(err) => { |
|
|
|
resolved_map.insert(event_id, Err(err.to_string())); |
|
|
|
resolved_map.insert(event_id, Err(err.to_string())); |
|
|
|
|