Skip to content

Commit 7b454d2

Browse files
authored
refactor: create State type to replace Map (#311)
1 parent 9dee443 commit 7b454d2

File tree

6 files changed

+31
-22
lines changed

6 files changed

+31
-22
lines changed

packages/federation-sdk/src/services/state.service.spec.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { beforeEach, describe, expect, it, spyOn, test } from 'bun:test';
22
import { type EventStore } from '@rocket.chat/federation-core';
3+
import type { State } from '@rocket.chat/federation-room';
34
import * as room from '@rocket.chat/federation-room';
45
import {
56
EventID,
@@ -11,7 +12,6 @@ import {
1112
PersistentEventFactory,
1213
RejectCodes,
1314
RoomVersion,
14-
StateMapKey,
1515
} from '@rocket.chat/federation-room';
1616
import { type WithId } from 'mongodb';
1717
import { EventRepository } from '../repositories/event.repository';
@@ -23,8 +23,6 @@ import { type ConfigService } from './config.service';
2323
import { DatabaseConnectionService } from './database-connection.service';
2424
import { StateService } from './state.service';
2525

26-
type State = Map<StateMapKey, PersistentEventBase>;
27-
2826
function getDefaultFields() {
2927
return {
3028
auth_events: [],

packages/federation-sdk/src/services/state.service.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
RoomID,
1515
RoomState,
1616
RoomVersion,
17+
State,
1718
type StateID,
1819
type StateMapKey,
1920
StateResolverAuthorizationError,
@@ -26,8 +27,6 @@ import { EventRepository } from '../repositories/event.repository';
2627
import { StateGraphRepository } from '../repositories/state-graph.repository';
2728
import { ConfigService } from './config.service';
2829

29-
type State = Map<StateMapKey, PersistentEventBase>;
30-
3130
type StrippedEvent = {
3231
content: PduContent;
3332
sender: string;
@@ -354,7 +353,7 @@ export class StateService {
354353
}
355354

356355
private buildStateFromEvents(events: PersistentEventBase[]): State {
357-
const state = new Map<StateMapKey, PersistentEventBase>();
356+
const state: State = new Map();
358357
for (const event of events) {
359358
state.set(event.getUniqueStateIdentifier(), event);
360359
}
@@ -372,7 +371,7 @@ export class StateService {
372371
const eventIds = stateMap.values().toArray();
373372

374373
const events = await this.eventRepository.findByIds(eventIds).toArray();
375-
const state = new Map<StateMapKey, PersistentEventBase>();
374+
const state: State = new Map();
376375
for (const event of events) {
377376
const e = PersistentEventFactory.createFromRawEvent(
378377
event.event,
@@ -836,7 +835,7 @@ export class StateService {
836835
private async _resolveState(
837836
stateIds: StateID[],
838837
roomVersion: RoomVersion,
839-
): Promise<Map<StateMapKey, PersistentEventBase>> {
838+
): Promise<State> {
840839
if (await this._isSameChain(stateIds)) {
841840
// pick the latest id from the chain, that's the state to use
842841
const latestDelta =

packages/room/src/manager/event-wrapper.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ export type PduWithHashesAndSignaturesOptional<T extends Pdu = Pdu> = Prettify<
4444

4545
export const REDACT_ALLOW_ALL_KEYS: unique symbol = Symbol.for('all');
4646

47+
export interface State extends Map<StateMapKey, PersistentEventBase> {
48+
get<T extends StateMapKey>(
49+
key: T,
50+
): T extends `${infer I}:${string}`
51+
? I extends PduType
52+
? PersistentEventBase<RoomVersion, I> | undefined
53+
: never
54+
: never;
55+
}
56+
4757
// convinient wrapper to manage schema differences when working with same algorithms across different versions
4858
export abstract class PersistentEventBase<
4959
Version extends RoomVersion = RoomVersion,

packages/room/src/state_resolution/definitions/algorithm/v2.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
// i am too early in this to remember everything by heart.
5959

6060
import assert from 'node:assert';
61-
import { PersistentEventBase } from '../../../manager/event-wrapper';
61+
import { PersistentEventBase, State } from '../../../manager/event-wrapper';
6262
import { PowerLevelEvent } from '../../../manager/power-level-event-wrapper';
6363
import type { EventID, StateMapKey } from '../../../types/_common';
6464
import {
@@ -83,7 +83,7 @@ export const isTruthy = <T>(
8383
export async function resolveStateV2Plus(
8484
states: ReadonlyMap<StateMapKey, PersistentEventBase>[],
8585
store: EventStore, // with cache
86-
): Promise<Map<StateMapKey, PersistentEventBase>> {
86+
): Promise<State> {
8787
// memory o'memory
8888
// const eventMap = new Map<string, PduV3>();
8989

@@ -138,7 +138,7 @@ export async function resolveStateV2Plus(
138138
assert(event, 'event should not be null');
139139
accum.set(stateKey, event);
140140
return accum;
141-
}, new Map<StateMapKey, PersistentEventBase>());
141+
}, new Map() as State);
142142

143143
if (conflicted.size === 0) {
144144
// no conflicted state, return the unconflicted state

packages/room/src/state_resolution/definitions/definitions.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { PriorityQueue } from '@datastructures-js/priority-queue';
2-
import type { EventID, State, StateMapKey } from '../../types/_common';
2+
import type {
3+
EventID,
4+
StateEventIdMap,
5+
StateMapKey,
6+
} from '../../types/_common';
37
import { type PduType } from '../../types/v3-11';
48

59
import assert from 'node:assert';
610
import { StateResolverAuthorizationError } from '../../authorizartion-rules/errors';
711
import { checkEventAuthWithState } from '../../authorizartion-rules/rules';
8-
import { PersistentEventBase } from '../../manager/event-wrapper';
12+
import { PersistentEventBase, State } from '../../manager/event-wrapper';
913
import { PowerLevelEvent } from '../../manager/power-level-event-wrapper';
1014
import { RoomVersion } from '../../manager/type';
1115

@@ -47,8 +51,8 @@ export function isPowerEvent(event: PersistentEventBase): boolean {
4751
// iout map takes care of the non-duplication of a set
4852
export function partitionState(
4953
events: Readonly<Iterable<PersistentEventBase>>,
50-
): [State, Map<StateMapKey, EventID[]>] {
51-
const unconflictedState: State = new Map();
54+
): [StateEventIdMap, Map<StateMapKey, EventID[]>] {
55+
const unconflictedState: StateEventIdMap = new Map();
5256

5357
// Note that the unconflicted state map only has one event for each key K, whereas the conflicted state set may contain multiple events with the same key.
5458
const conflictedStateEventsMap: Map<StateMapKey, EventID[]> = new Map();
@@ -148,7 +152,7 @@ export async function getAuthChain(
148152
// Auth difference
149153
// NOTE: https://github.com/element-hq/synapse/blob/a25a37002c851ef419d12925a11dd8bf2233470e/docs/auth_chain_difference_algorithm.md
150154
export async function getAuthChainDifference(
151-
states: Readonly<Iterable<State>>,
155+
states: Readonly<Iterable<StateEventIdMap>>,
152156
store: EventStore,
153157
) {
154158
const authChainSets = [] as Set<EventID>[];
@@ -574,13 +578,11 @@ export async function iterativeAuthChecks(
574578
events: PersistentEventBase[],
575579
stateMap: ReadonlyMap<StateMapKey, PersistentEventBase>,
576580
store: EventStore,
577-
): Promise<Map<StateMapKey, PersistentEventBase>> {
578-
const newState = new Map<StateMapKey, PersistentEventBase>(
579-
stateMap.entries(),
580-
);
581+
): Promise<State> {
582+
const newState: State = new Map(stateMap.entries()) as State;
581583

582584
for (const event of events) {
583-
const authEventStateMap = new Map<StateMapKey, PersistentEventBase>();
585+
const authEventStateMap: State = new Map();
584586
for (const authEvent of await store.getEvents(event.getAuthEventIds())) {
585587
authEventStateMap.set(authEvent.getUniqueStateIdentifier(), authEvent);
586588
}

packages/room/src/types/_common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type UserID = z.infer<typeof userIdSchema>;
2121

2222
export type StateMapKey = `${PduType}:${StateKey}`;
2323

24-
export type State = Map<StateMapKey, EventID>;
24+
export type StateEventIdMap = Map<StateMapKey, EventID>;
2525

2626
export type PduForType<P extends PduType = PduType> = Extract<Pdu, { type: P }>;
2727

0 commit comments

Comments
 (0)