Working at get_auth_chain_diff

This commit is contained in:
Devin R 2020-07-21 00:11:03 -04:00
parent 0c21f38cb1
commit d69e712dba
3 changed files with 82 additions and 25 deletions

View File

@ -81,7 +81,13 @@ impl StateResolution {
let mut auth_diff = let mut auth_diff =
self.get_auth_chain_diff(room_id, &state_sets, &mut event_map, store)?; self.get_auth_chain_diff(room_id, &state_sets, &mut event_map, store)?;
println!("{:?}", auth_diff); println!(
"AUTH DIFF {:?}",
auth_diff
.iter()
.map(ToString::to_string)
.collect::<Vec<_>>()
);
// add the auth_diff to conflicting now we have a full set of conflicting events // add the auth_diff to conflicting now we have a full set of conflicting events
auth_diff.extend(conflicting.values().cloned().flatten()); auth_diff.extend(conflicting.values().cloned().flatten());
@ -191,6 +197,8 @@ impl StateResolution {
&mut self, &mut self,
state_sets: &[StateMap<EventId>], state_sets: &[StateMap<EventId>],
) -> (StateMap<EventId>, StateMap<Vec<EventId>>) { ) -> (StateMap<EventId>, StateMap<Vec<EventId>>) {
use itertools::Itertools;
let mut unconflicted_state = StateMap::new(); let mut unconflicted_state = StateMap::new();
let mut conflicted_state = StateMap::new(); let mut conflicted_state = StateMap::new();
@ -198,6 +206,7 @@ impl StateResolution {
let mut event_ids = state_sets let mut event_ids = state_sets
.iter() .iter()
.flat_map(|map| map.get(key).cloned()) .flat_map(|map| map.get(key).cloned())
.dedup()
.collect::<Vec<EventId>>(); .collect::<Vec<EventId>>();
if event_ids.len() == 1 { if event_ids.len() == 1 {
@ -221,12 +230,22 @@ impl StateResolution {
) -> Result<Vec<EventId>, String> { ) -> Result<Vec<EventId>, String> {
use itertools::Itertools; use itertools::Itertools;
println!(
"{:?}",
state_sets
.iter()
.flat_map(|map| map.values())
.map(ToString::to_string)
.dedup()
.collect::<Vec<_>>()
);
tracing::debug!("calculating auth chain difference"); tracing::debug!("calculating auth chain difference");
store.auth_chain_diff( store.auth_chain_diff(
room_id, room_id,
&state_sets state_sets
.iter() .iter()
.flat_map(|map| map.values()) .map(|map| map.values().cloned().collect())
.dedup() .dedup()
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
) )

View File

@ -20,6 +20,6 @@ pub trait StateStore {
fn auth_chain_diff( fn auth_chain_diff(
&self, &self,
room_id: &RoomId, room_id: &RoomId,
event_id: &[&EventId], event_id: Vec<Vec<EventId>>,
) -> Result<Vec<EventId>, String>; ) -> Result<Vec<EventId>, String>;
} }

View File

@ -1,6 +1,7 @@
#![allow(unused)] #![allow(unused)]
use std::{ use std::{
cell::RefCell,
collections::{BTreeMap, BTreeSet}, collections::{BTreeMap, BTreeSet},
convert::TryFrom, convert::TryFrom,
time::{Duration, SystemTime, UNIX_EPOCH}, time::{Duration, SystemTime, UNIX_EPOCH},
@ -28,20 +29,20 @@ fn id(id: &str) -> EventId {
} }
fn alice() -> UserId { fn alice() -> UserId {
UserId::try_from("@alice:example.com").unwrap() UserId::try_from("@alice:foo").unwrap()
} }
fn bobo() -> UserId { fn bobo() -> UserId {
UserId::try_from("@bobo:example.com").unwrap() UserId::try_from("@bobo:foo").unwrap()
} }
fn devin() -> UserId { fn devin() -> UserId {
UserId::try_from("@devin:example.com").unwrap() UserId::try_from("@devin:foo").unwrap()
} }
fn zera() -> UserId { fn zera() -> UserId {
UserId::try_from("@zera:example.com").unwrap() UserId::try_from("@zera:foo").unwrap()
} }
fn room_id() -> RoomId { fn room_id() -> RoomId {
RoomId::try_from("!test:example.com").unwrap() RoomId::try_from("!test:foo").unwrap()
} }
fn member_content_ban() -> JsonValue { fn member_content_ban() -> JsonValue {
@ -297,26 +298,41 @@ impl StateStore for TestStore {
fn auth_chain_diff( fn auth_chain_diff(
&self, &self,
room_id: &RoomId, room_id: &RoomId,
event_ids: &[&EventId], event_ids: Vec<Vec<EventId>>,
) -> Result<Vec<EventId>, String> { ) -> Result<Vec<EventId>, String> {
let mut chains = BTreeSet::new(); use itertools::Itertools;
let mut list = vec![];
for id in event_ids { println!(
"EVENTS FOR AUTH {:?}",
event_ids
.iter()
.map(|v| v.iter().map(ToString::to_string).collect::<Vec<_>>())
.collect::<Vec<_>>()
);
let mut chains = vec![];
for ids in event_ids {
let chain = self let chain = self
.auth_event_ids(room_id, &[(*id).clone()])? .auth_event_ids(room_id, &ids)?
.into_iter() .into_iter()
.collect::<BTreeSet<_>>(); .collect::<BTreeSet<_>>();
list.push(chain.clone()); chains.push(chain);
chains.insert(chain);
} }
if let Some(chain) = list.first() {
let set = maplit::btreeset!(chain.clone()); if let Some(chain) = chains.first() {
let common = set.intersection(&chains).flatten().collect::<Vec<_>>(); let rest = chains.iter().skip(1).flatten().cloned().collect();
let common = chain.intersection(&rest).collect::<Vec<_>>();
println!(
"COMMON {:?}",
common.iter().map(ToString::to_string).collect::<Vec<_>>()
);
Ok(chains Ok(chains
.iter() .iter()
.flatten() .flatten()
.filter(|id| common.contains(&id)) .filter(|id| !common.contains(&id))
.cloned() .cloned()
.collect::<BTreeSet<_>>()
.into_iter()
.collect()) .collect())
} else { } else {
Ok(vec![]) Ok(vec![])
@ -325,6 +341,8 @@ impl StateStore for TestStore {
} }
fn do_check(events: &[StateEvent], edges: Vec<Vec<EventId>>, expected_state_ids: Vec<EventId>) { fn do_check(events: &[StateEvent], edges: Vec<Vec<EventId>>, expected_state_ids: Vec<EventId>) {
use itertools::Itertools;
let mut resolver = StateResolution::default(); let mut resolver = StateResolution::default();
// TODO what do we fill this with, everything ?? // TODO what do we fill this with, everything ??
let store = TestStore( let store = TestStore(
@ -340,12 +358,19 @@ fn do_check(events: &[StateEvent], edges: Vec<Vec<EventId>>, expected_state_ids:
// this is the same as in `resolve` event_id -> StateEvent // this is the same as in `resolve` event_id -> StateEvent
let mut fake_event_map = BTreeMap::new(); let mut fake_event_map = BTreeMap::new();
// create the DB of events that led up to this point
// TODO maybe clean up some of these clones it is just tests but... // TODO maybe clean up some of these clones it is just tests but...
for ev in INITIAL_EVENTS().values().chain(events) { for ev in INITIAL_EVENTS().values().chain(events) {
graph.insert(ev.event_id().unwrap().clone(), vec![]); graph.insert(ev.event_id().unwrap().clone(), vec![]);
fake_event_map.insert(ev.event_id().unwrap().clone(), ev.clone()); fake_event_map.insert(ev.event_id().unwrap().clone(), ev.clone());
} }
for pair in INITIAL_EDGES().windows(2) {
if let &[a, b] = &pair {
graph.entry(a.clone()).or_insert(vec![]).push(b.clone());
}
}
for edge_list in edges { for edge_list in edges {
for pair in edge_list.windows(2) { for pair in edge_list.windows(2) {
if let &[a, b] = &pair { if let &[a, b] = &pair {
@ -392,8 +417,12 @@ fn do_check(events: &[StateEvent], edges: Vec<Vec<EventId>>, expected_state_ids:
// .collect::<Vec<_>>() // .collect::<Vec<_>>()
// ); // );
let resolved = let resolved = resolver.resolve(
resolver.resolve(&room_id(), &RoomVersionId::version_1(), &state_sets, &store); &room_id(),
&RoomVersionId::version_1(),
&state_sets,
&TestStore(event_map.clone()),
);
match resolved { match resolved {
Ok(ResolutionResult::Resolved(state)) => state, Ok(ResolutionResult::Resolved(state)) => state,
_ => panic!("resolution for {} failed", node), _ => panic!("resolution for {} failed", node),
@ -428,6 +457,7 @@ fn do_check(events: &[StateEvent], edges: Vec<Vec<EventId>>, expected_state_ids:
// TODO The event is just remade, adding the auth_events and prev_events here // TODO The event is just remade, adding the auth_events and prev_events here
// UPDATE: the `to_pdu_event` was split into `init` and the fn below, could be better // UPDATE: the `to_pdu_event` was split into `init` and the fn below, could be better
let e = fake_event; let e = fake_event;
let ev_id = e.event_id().unwrap();
let event = to_pdu_event( let event = to_pdu_event(
&e.event_id().unwrap().to_string(), &e.event_id().unwrap().to_string(),
e.sender().clone(), e.sender().clone(),
@ -437,6 +467,9 @@ fn do_check(events: &[StateEvent], edges: Vec<Vec<EventId>>, expected_state_ids:
&auth_events, &auth_events,
prev_events, prev_events,
); );
// we have to update our store, an actual user of this lib would do this
// with the result of the resolution>
// *store.0.borrow_mut().get_mut(ev_id).unwrap() = event.clone();
state_at_event.insert(node, state_after); state_at_event.insert(node, state_after);
event_map.insert(event_id.clone(), event); event_map.insert(event_id.clone(), event);
@ -462,13 +495,18 @@ fn do_check(events: &[StateEvent], edges: Vec<Vec<EventId>>, expected_state_ids:
.get(&EventId::try_from("$START:foo").unwrap()) .get(&EventId::try_from("$START:foo").unwrap())
.unwrap(); .unwrap();
println!("{:?}", start_state);
let end_state = state_at_event let end_state = state_at_event
.get(&EventId::try_from("$END:foo").unwrap()) .get(&EventId::try_from("$END:foo").unwrap())
.unwrap() .unwrap()
.iter() .iter()
.filter(|(k, v)| expected_state.contains_key(k) || start_state.get(k) != Some(*v)) .filter(|(k, v)| {
println!(
"{:?} == {:?}",
start_state.get(k).map(ToString::to_string),
Some(v.to_string())
);
expected_state.contains_key(k) || start_state.get(k) != Some(*v)
})
.map(|(k, v)| (k.clone(), v.clone())) .map(|(k, v)| (k.clone(), v.clone()))
.collect::<StateMap<EventId>>(); .collect::<StateMap<EventId>>();