diff --git a/Cargo.toml b/Cargo.toml index 53d0c68e..fc28c60d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,13 @@ name = "state-res" version = "0.1.0" authors = ["Devin R "] edition = "2018" +categories = ["api-bindings", "web-programming"] +description = "An abstraction for Matrix state resolution." +homepage = "https://www.ruma.io/" +keywords = ["matrix", "chat", "state resolution", "ruma"] +license = "MIT" +readme = "README.md" +repository = "https://github.com/ruma/state-res" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] @@ -19,7 +26,6 @@ tracing-subscriber = "0.2.8" [dependencies.ruma] git = "https://github.com/DevinR528/ruma" branch = "pdu-deserialize" -# path = "../__forks__/ruma/ruma" features = ["client-api", "federation-api", "appservice-api"] [dev-dependencies] @@ -27,5 +33,5 @@ lazy_static = "1.4.0" criterion = "0.3.3" [[bench]] -name = "state_bench" +name = "state_res_bench" harness = false \ No newline at end of file diff --git a/benches/event_auth_bench.rs b/benches/event_auth_bench.rs new file mode 100644 index 00000000..e69de29b diff --git a/benches/state_bench.rs b/benches/state_res_bench.rs similarity index 98% rename from benches/state_bench.rs rename to benches/state_res_bench.rs index c4fa0e40..555677d6 100644 --- a/benches/state_bench.rs +++ b/benches/state_res_bench.rs @@ -37,7 +37,7 @@ fn lexico_topo_sort(c: &mut Criterion) { event_id("p") => vec![event_id("o")], }; b.iter(|| { - let mut resolver = StateResolution::default(); + let resolver = StateResolution::default(); let _ = resolver .lexicographical_topological_sort(&graph, |id| (0, UNIX_EPOCH, Some(id.clone()))); @@ -47,7 +47,7 @@ fn lexico_topo_sort(c: &mut Criterion) { fn resolution_shallow_auth_chain(c: &mut Criterion) { c.bench_function("resolve state of 5 events one fork", |b| { - let mut resolver = StateResolution::default(); + let resolver = StateResolution::default(); let store = TestStore(RefCell::new(btreemap! {})); @@ -72,7 +72,7 @@ fn resolution_shallow_auth_chain(c: &mut Criterion) { fn resolve_deeper_event_set(c: &mut Criterion) { c.bench_function("resolve state of 10 events 3 conflicting", |b| { - let mut resolver = StateResolution::default(); + let resolver = StateResolution::default(); let init = INITIAL_EVENTS(); let ban = BAN_STATE_SET(); diff --git a/src/lib.rs b/src/lib.rs index 0849694f..a0867678 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,7 +11,6 @@ use ruma::{ events::EventType, identifiers::{EventId, RoomId, RoomVersionId}, }; -use serde::{Deserialize, Serialize}; mod error; mod event_auth; @@ -40,29 +39,14 @@ pub type StateMap = BTreeMap<(EventType, String), T>; /// A mapping of `EventId` to `T`, usually a `StateEvent`. pub type EventMap = BTreeMap; -#[derive(Debug, Default, Deserialize, Serialize)] // TODO make the ser/de impls useful -pub struct StateResolution { - // TODO remove pub after initial testing - /// The set of resolved events over time. - pub resolved_events: Vec, - /// The resolved state, kept to have easy access to the last resolved - /// layer of state. - pub state: BTreeMap>, - /// The graph of authenticated events, kept to find the most recent auth event - /// in a chain for incoming state sets. - pub auth_graph: BTreeMap>>, - /// The last known point in the state graph. - pub most_recent_resolved: Option<(EventType, String)>, - - // fields for temp storage during resolution - pub conflicting_events: Vec, -} +#[derive(Default)] +pub struct StateResolution; impl StateResolution { /// Resolve sets of state events as they come in. Internally `StateResolution` builds a graph /// and an auth chain to allow for state conflict resolution. pub fn resolve( - &mut self, + &self, room_id: &RoomId, room_version: &RoomVersionId, state_sets: &[StateMap], @@ -247,7 +231,7 @@ impl StateResolution { /// /// The tuple looks like `(unconflicted, conflicted)`. pub fn separate( - &mut self, + &self, state_sets: &[StateMap], ) -> (StateMap, StateMap>) { use itertools::Itertools; @@ -299,7 +283,7 @@ impl StateResolution { /// Returns a Vec of deduped EventIds that appear in some chains but no others. pub fn get_auth_chain_diff( - &mut self, + &self, room_id: &RoomId, state_sets: &[StateMap], store: &dyn StateStore, @@ -321,7 +305,7 @@ impl StateResolution { } pub fn reverse_topological_power_sort( - &mut self, + &self, room_id: &RoomId, power_events: &[EventId], event_map: &mut EventMap, @@ -379,7 +363,7 @@ impl StateResolution { /// `key_fn` is used as a tie breaker. The tie breaker happens based on /// power level, age, and event_id. pub fn lexicographical_topological_sort( - &mut self, + &self, graph: &BTreeMap>, key_fn: F, ) -> Vec @@ -446,7 +430,7 @@ impl StateResolution { } fn get_power_level_for_sender( - &mut self, + &self, room_id: &RoomId, event_id: &EventId, event_map: &mut EventMap, @@ -506,7 +490,7 @@ impl StateResolution { } fn iterative_auth_check( - &mut self, + &self, room_id: &RoomId, room_version: &RoomVersionId, power_events: &[EventId], @@ -581,7 +565,7 @@ impl StateResolution { /// NOTE we rely on the `event_map` beign full at this point. /// TODO is this ok? fn mainline_sort( - &mut self, + &self, room_id: &RoomId, to_sort: &[EventId], resolved_power_level: Option<&EventId>, @@ -659,7 +643,7 @@ impl StateResolution { // TODO make `event` not clone every loop fn get_mainline_depth( - &mut self, + &self, room_id: &RoomId, mut event: Option, mainline_map: &EventMap, @@ -692,7 +676,7 @@ impl StateResolution { } fn add_event_and_auth_chain_to_graph( - &mut self, + &self, room_id: &RoomId, graph: &mut BTreeMap>, event_id: &EventId, @@ -726,7 +710,7 @@ impl StateResolution { /// TODO update self if we go that route just as event_map will be updated fn _get_event( - &mut self, + &self, _room_id: &RoomId, ev_id: &EventId, event_map: &mut EventMap, diff --git a/tests/auth_ids.rs b/tests/auth_ids.rs index 45bbd1da..be1d8d7e 100644 --- a/tests/auth_ids.rs +++ b/tests/auth_ids.rs @@ -34,7 +34,7 @@ fn do_check(events: &[StateEvent], edges: Vec>, expected_state_ids: .init() }); - let mut resolver = StateResolution::default(); + let resolver = StateResolution::default(); let store = TestStore(RefCell::new( INITIAL_EVENTS() @@ -568,7 +568,7 @@ fn ban_with_auth_chains() { #[test] fn base_with_auth_chains() { - let mut resolver = StateResolution::default(); + let resolver = StateResolution::default(); let store = TestStore(RefCell::new(INITIAL_EVENTS())); @@ -608,7 +608,7 @@ fn base_with_auth_chains() { #[test] fn ban_with_auth_chains2() { - let mut resolver = StateResolution::default(); + let resolver = StateResolution::default(); let init = INITIAL_EVENTS(); let ban = BAN_STATE_SET(); diff --git a/tests/state_res.rs b/tests/state_res.rs index ba4393f8..ab08b0cc 100644 --- a/tests/state_res.rs +++ b/tests/state_res.rs @@ -278,7 +278,7 @@ fn do_check(events: &[StateEvent], edges: Vec>, expected_state_ids: .init() }); - let mut resolver = StateResolution::default(); + let resolver = StateResolution::default(); let store = TestStore(RefCell::new( INITIAL_EVENTS() @@ -691,7 +691,7 @@ fn topic_setting() { #[test] fn test_event_map_none() { - let mut resolver = StateResolution::default(); + let resolver = StateResolution::default(); let store = TestStore(RefCell::new(btreemap! {})); @@ -715,7 +715,7 @@ fn test_event_map_none() { #[test] fn test_lexicographical_sort() { - let mut resolver = StateResolution::default(); + let resolver = StateResolution::default(); let graph = btreemap! { event_id("l") => vec![event_id("o")],