Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add helpers for getting prev and auth events #4139

Merged
merged 3 commits into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/4139.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add helpers functions for getting prev and auth events of an event
4 changes: 2 additions & 2 deletions synapse/event_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ def _is_membership_change_allowed(event, auth_events):
membership = event.content["membership"]

# Check if this is the room creator joining:
if len(event.prev_events) == 1 and Membership.JOIN == membership:
if len(event.prev_event_ids()) == 1 and Membership.JOIN == membership:
# Get room creation event:
key = (EventTypes.Create, "", )
create = auth_events.get(key)
if create and event.prev_events[0][0] == create.event_id:
if create and event.prev_event_ids()[0] == create.event_id:
if create.content["creator"] == event.state_key:
return

Expand Down
18 changes: 18 additions & 0 deletions synapse/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,24 @@ def items(self):
def keys(self):
return six.iterkeys(self._event_dict)

def prev_event_ids(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these ordered? Should we always return them as a set, vs a list?

also docstrings plz

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So many good points to counter my crankiness

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I also ask wrt set because a set comprehension will mean we don't have to cast it to a set in various places, if we declare the events have no fixed order)

"""Returns the list of prev event IDs. The order matches the order
specified in the event, though there is no meaning to it.

Returns:
list[str]: The list of event IDs of this event's prev_events
"""
return [e for e, _ in self.prev_events]

def auth_event_ids(self):
"""Returns the list of auth event IDs. The order matches the order
specified in the event, though there is no meaning to it.

Returns:
list[str]: The list of event IDs of this event's auth_events
"""
return [e for e, _ in self.auth_events]


class FrozenEvent(EventBase):
def __init__(self, event_dict, internal_metadata_dict={}, rejected_reason=None):
Expand Down
4 changes: 1 addition & 3 deletions synapse/federation/transaction_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,7 @@ def handle_event(event):
# banned then it won't receive the event because it won't
# be in the room after the ban.
destinations = yield self.state.get_current_hosts_in_room(
event.room_id, latest_event_ids=[
prev_id for prev_id, _ in event.prev_events
],
event.room_id, latest_event_ids=event.prev_event_ids(),
)
except Exception:
logger.exception(
Expand Down
48 changes: 24 additions & 24 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def on_receive_pdu(
room_id, event_id, min_depth,
)

prevs = {e_id for e_id, _ in pdu.prev_events}
prevs = set(pdu.prev_event_ids())
seen = yield self.store.have_seen_events(prevs)

if min_depth and pdu.depth < min_depth:
Expand Down Expand Up @@ -607,7 +607,7 @@ def _process_received_pdu(self, origin, event, state, auth_chain):
if e.event_id in seen_ids:
continue
e.internal_metadata.outlier = True
auth_ids = [e_id for e_id, _ in e.auth_events]
auth_ids = e.auth_event_ids()
auth = {
(e.type, e.state_key): e for e in auth_chain
if e.event_id in auth_ids or e.type == EventTypes.Create
Expand Down Expand Up @@ -726,7 +726,7 @@ def backfill(self, dest, room_id, limit, extremities):
edges = [
ev.event_id
for ev in events
if set(e_id for e_id, _ in ev.prev_events) - event_ids
if set(ev.prev_event_ids()) - event_ids
]

logger.info(
Expand All @@ -753,7 +753,7 @@ def backfill(self, dest, room_id, limit, extremities):
required_auth = set(
a_id
for event in events + list(state_events.values()) + list(auth_events.values())
for a_id, _ in event.auth_events
for a_id in event.auth_event_ids()
)
auth_events.update({
e_id: event_map[e_id] for e_id in required_auth if e_id in event_map
Expand All @@ -769,7 +769,7 @@ def backfill(self, dest, room_id, limit, extremities):
auth_events.update(ret_events)

required_auth.update(
a_id for event in ret_events.values() for a_id, _ in event.auth_events
a_id for event in ret_events.values() for a_id in event.auth_event_ids()
)
missing_auth = required_auth - set(auth_events)

Expand All @@ -796,7 +796,7 @@ def backfill(self, dest, room_id, limit, extremities):
required_auth.update(
a_id
for event in results if event
for a_id, _ in event.auth_events
for a_id in event.auth_event_ids()
)
missing_auth = required_auth - set(auth_events)

Expand All @@ -816,7 +816,7 @@ def backfill(self, dest, room_id, limit, extremities):
"auth_events": {
(auth_events[a_id].type, auth_events[a_id].state_key):
auth_events[a_id]
for a_id, _ in a.auth_events
for a_id in a.auth_event_ids()
if a_id in auth_events
}
})
Expand All @@ -828,7 +828,7 @@ def backfill(self, dest, room_id, limit, extremities):
"auth_events": {
(auth_events[a_id].type, auth_events[a_id].state_key):
auth_events[a_id]
for a_id, _ in event_map[e_id].auth_events
for a_id in event_map[e_id].auth_event_ids()
if a_id in auth_events
}
})
Expand Down Expand Up @@ -1041,17 +1041,17 @@ def _sanity_check_event(self, ev):
Raises:
SynapseError if the event does not pass muster
"""
if len(ev.prev_events) > 20:
if len(ev.prev_event_ids()) > 20:
logger.warn("Rejecting event %s which has %i prev_events",
ev.event_id, len(ev.prev_events))
ev.event_id, len(ev.prev_event_ids()))
raise SynapseError(
http_client.BAD_REQUEST,
"Too many prev_events",
)

if len(ev.auth_events) > 10:
if len(ev.auth_event_ids()) > 10:
logger.warn("Rejecting event %s which has %i auth_events",
ev.event_id, len(ev.auth_events))
ev.event_id, len(ev.auth_event_ids()))
raise SynapseError(
http_client.BAD_REQUEST,
"Too many auth_events",
Expand All @@ -1076,7 +1076,7 @@ def send_invite(self, target_host, event):
def on_event_auth(self, event_id):
event = yield self.store.get_event(event_id)
auth = yield self.store.get_auth_chain(
[auth_id for auth_id, _ in event.auth_events],
[auth_id for auth_id in event.auth_event_ids()],
include_given=True
)
defer.returnValue([e for e in auth])
Expand Down Expand Up @@ -1698,7 +1698,7 @@ def _persist_auth_tree(self, origin, auth_events, state, event):

missing_auth_events = set()
for e in itertools.chain(auth_events, state, [event]):
for e_id, _ in e.auth_events:
for e_id in e.auth_event_ids():
if e_id not in event_map:
missing_auth_events.add(e_id)

Expand All @@ -1717,7 +1717,7 @@ def _persist_auth_tree(self, origin, auth_events, state, event):
for e in itertools.chain(auth_events, state, [event]):
auth_for_e = {
(event_map[e_id].type, event_map[e_id].state_key): event_map[e_id]
for e_id, _ in e.auth_events
for e_id in e.auth_event_ids()
if e_id in event_map
}
if create_event:
Expand Down Expand Up @@ -1785,10 +1785,10 @@ def _prep_event(self, origin, event, state=None, auth_events=None):

# This is a hack to fix some old rooms where the initial join event
# didn't reference the create event in its auth events.
if event.type == EventTypes.Member and not event.auth_events:
if len(event.prev_events) == 1 and event.depth < 5:
if event.type == EventTypes.Member and not event.auth_event_ids():
if len(event.prev_event_ids()) == 1 and event.depth < 5:
c = yield self.store.get_event(
event.prev_events[0][0],
event.prev_event_ids()[0],
allow_none=True,
)
if c and c.type == EventTypes.Create:
Expand Down Expand Up @@ -1835,7 +1835,7 @@ def on_query_auth(self, origin, event_id, room_id, remote_auth_chain, rejects,

# Now get the current auth_chain for the event.
local_auth_chain = yield self.store.get_auth_chain(
[auth_id for auth_id, _ in event.auth_events],
[auth_id for auth_id in event.auth_event_ids()],
include_given=True
)

Expand Down Expand Up @@ -1891,7 +1891,7 @@ def do_auth(self, origin, event, context, auth_events):
"""
# Check if we have all the auth events.
current_state = set(e.event_id for e in auth_events.values())
event_auth_events = set(e_id for e_id, _ in event.auth_events)
event_auth_events = set(event.auth_event_ids())

if event.is_state():
event_key = (event.type, event.state_key)
Expand Down Expand Up @@ -1935,7 +1935,7 @@ def do_auth(self, origin, event, context, auth_events):
continue

try:
auth_ids = [e_id for e_id, _ in e.auth_events]
auth_ids = e.auth_event_ids()
auth = {
(e.type, e.state_key): e for e in remote_auth_chain
if e.event_id in auth_ids or e.type == EventTypes.Create
Expand All @@ -1956,7 +1956,7 @@ def do_auth(self, origin, event, context, auth_events):
pass

have_events = yield self.store.get_seen_events_with_rejections(
[e_id for e_id, _ in event.auth_events]
event.auth_event_ids()
)
seen_events = set(have_events.keys())
except Exception:
Expand Down Expand Up @@ -2058,7 +2058,7 @@ def do_auth(self, origin, event, context, auth_events):
continue

try:
auth_ids = [e_id for e_id, _ in ev.auth_events]
auth_ids = ev.auth_event_ids()
auth = {
(e.type, e.state_key): e
for e in result["auth_chain"]
Expand Down Expand Up @@ -2250,7 +2250,7 @@ def get_next(it, opt=None):
missing_remote_ids = [e.event_id for e in missing_remotes]
base_remote_rejected = list(missing_remotes)
for e in missing_remotes:
for e_id, _ in e.auth_events:
for e_id in e.auth_event_ids():
if e_id in missing_remote_ids:
try:
base_remote_rejected.remove(e)
Expand Down
2 changes: 1 addition & 1 deletion synapse/state/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ def compute_event_context(self, event, old_state=None):
logger.debug("calling resolve_state_groups from compute_event_context")

entry = yield self.resolve_state_groups_for_events(
event.room_id, [e for e, _ in event.prev_events],
event.room_id, event.prev_event_ids(),
)

prev_state_ids = entry.state
Expand Down
16 changes: 8 additions & 8 deletions synapse/state/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ def _get_power_level_for_sender(event_id, event_map, state_res_store):
event = yield _get_event(event_id, event_map, state_res_store)

pl = None
for aid, _ in event.auth_events:
for aid in event.auth_event_ids():
aev = yield _get_event(aid, event_map, state_res_store)
if (aev.type, aev.state_key) == (EventTypes.PowerLevels, ""):
pl = aev
break

if pl is None:
# Couldn't find power level. Check if they're the creator of the room
for aid, _ in event.auth_events:
for aid in event.auth_event_ids():
aev = yield _get_event(aid, event_map, state_res_store)
if (aev.type, aev.state_key) == (EventTypes.Create, ""):
if aev.content.get("creator") == event.sender:
Expand Down Expand Up @@ -299,7 +299,7 @@ def _add_event_and_auth_chain_to_graph(graph, event_id, event_map,
graph.setdefault(eid, set())

event = yield _get_event(eid, event_map, state_res_store)
for aid, _ in event.auth_events:
for aid in event.auth_event_ids():
if aid in auth_diff:
if aid not in graph:
state.append(aid)
Expand Down Expand Up @@ -369,7 +369,7 @@ def _iterative_auth_checks(event_ids, base_state, event_map, state_res_store):
event = event_map[event_id]

auth_events = {}
for aid, _ in event.auth_events:
for aid in event.auth_event_ids():
ev = yield _get_event(aid, event_map, state_res_store)

if ev.rejected_reason is None:
Expand Down Expand Up @@ -417,9 +417,9 @@ def _mainline_sort(event_ids, resolved_power_event_id, event_map,
while pl:
mainline.append(pl)
pl_ev = yield _get_event(pl, event_map, state_res_store)
auth_events = pl_ev.auth_events
auth_events = pl_ev.auth_event_ids()
pl = None
for aid, _ in auth_events:
for aid in auth_events:
ev = yield _get_event(aid, event_map, state_res_store)
if (ev.type, ev.state_key) == (EventTypes.PowerLevels, ""):
pl = aid
Expand Down Expand Up @@ -464,10 +464,10 @@ def _get_mainline_depth_for_event(event, mainline_map, event_map, state_res_stor
if depth is not None:
defer.returnValue(depth)

auth_events = event.auth_events
auth_events = event.auth_event_ids()
event = None

for aid, _ in auth_events:
for aid in auth_events:
aev = yield _get_event(aid, event_map, state_res_store)
if (aev.type, aev.state_key) == (EventTypes.PowerLevels, ""):
event = aev
Expand Down
4 changes: 2 additions & 2 deletions synapse/storage/event_federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ def _handle_mult_prev_events(self, txn, events):
"is_state": False,
}
for ev in events
for e_id, _ in ev.prev_events
for e_id in ev.prev_event_ids()
],
)

Expand Down Expand Up @@ -510,7 +510,7 @@ def _update_backward_extremeties(self, txn, events):

txn.executemany(query, [
(e_id, ev.room_id, e_id, ev.room_id, e_id, ev.room_id, False)
for ev in events for e_id, _ in ev.prev_events
for ev in events for e_id in ev.prev_event_ids()
if not ev.internal_metadata.is_outlier()
])

Expand Down
8 changes: 4 additions & 4 deletions synapse/storage/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ def _persist_events(self, events_and_contexts, backfilled=False,
)
if len_1:
all_single_prev_not_state = all(
len(event.prev_events) == 1
len(event.prev_event_ids()) == 1
and not event.is_state()
for event, ctx in ev_ctx_rm
)
Expand All @@ -440,7 +440,7 @@ def _persist_events(self, events_and_contexts, backfilled=False,
# guess this by looking at the prev_events and checking
# if they match the current forward extremities.
for ev, _ in ev_ctx_rm:
prev_event_ids = set(e for e, _ in ev.prev_events)
prev_event_ids = set(ev.prev_event_ids())
if latest_event_ids == prev_event_ids:
state_delta_reuse_delta_counter.inc()
break
Expand Down Expand Up @@ -551,7 +551,7 @@ def _calculate_new_extremities(self, room_id, event_contexts, latest_event_ids):
result.difference_update(
e_id
for event in new_events
for e_id, _ in event.prev_events
for e_id in event.prev_event_ids()
)

# Finally, remove any events which are prev_events of any existing events.
Expand Down Expand Up @@ -869,7 +869,7 @@ def _persist_events_txn(self, txn, events_and_contexts, backfilled,
"auth_id": auth_id,
}
for event, _ in events_and_contexts
for auth_id, _ in event.auth_events
for auth_id in event.auth_event_ids()
if event.is_state()
],
)
Expand Down
2 changes: 1 addition & 1 deletion tests/state/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ def get_auth_chain(self, event_ids):
result.add(event_id)

event = self.event_map[event_id]
for aid, _ in event.auth_events:
for aid in event.auth_event_ids():
stack.append(aid)

return list(result)