-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Allow events to be created with no prev_events
(MSC2716)
#11243
Changes from 5 commits
39efad1
66f0859
9f45d09
e093481
4bb5fd3
f421a2d
d10625e
2370dca
e2928b5
85364c5
0ed300e
93ca0f8
9b9deff
8fada7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add experimental room version `org.matrix.msc2716v4` to allow events to be created without `prev_events` (only `auth_events`). |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -918,6 +918,7 @@ async def create_new_client_event( | |
full_state_ids_at_event = None | ||
if auth_event_ids is not None: | ||
# If auth events are provided, prev events must be also. | ||
# prev_event_ids could be an empty array though. | ||
assert prev_event_ids is not None | ||
|
||
# Copy the full auth state before it stripped down | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The insertion event is connected to the state chain so the whole historical batch can share the same For the federation cases, it seems to work 🤔🤷. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, that surprises me a bit. I was very much imagining that the federation code would ask for the state at the insertion event. I guess it probably works because we fetch the state at the start of the chain of state events? And we return the full state, so we then can accept all the state events we add? I don't think that is necessary to make the whole batch have the same state group. Also, I wonder if that'll make the client UI show the state events when backpaginating, making it look like the state was changed at those points, when actually they were changed much further back? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I can play around with it but that seems like something for another PR. It's pretty finicky to get right.
Doesn't seem to be a problem for the local origin homeserver with Element probably because they are marked as I want to test this on a federated homeserver though. I'm trying to setup a federated homeserver locally to see if it's a problem for remote federated servers. Currently working out the hostname/ Might just end up creating a Complement test around making sure the state isn't visible between batches ⏩ but would be nice to double-check with real Element about how it looks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
FYI you can start some preconfigured HSes that federate with each other by running There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I updated the Complement test which tests multiple batches over federation to check for historical state and didn't see any problems between batches. But when the historical state chain is connected to the (image is in scrollback order, oldest messages at the top) /messages response
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I created #11487 to add a test to ensure the Things seem to work just fine if I disconnect the floating state chain from the Both the state chain and the |
||
|
@@ -949,14 +950,24 @@ async def create_new_client_event( | |
else: | ||
prev_event_ids = await self.store.get_prev_events_for_room(builder.room_id) | ||
|
||
# we now ought to have some prev_events (unless it's a create event). | ||
# | ||
# do a quick sanity check here, rather than waiting until we've created the | ||
# Do a quick sanity check here, rather than waiting until we've created the | ||
# event and then try to auth it (which fails with a somewhat confusing "No | ||
# create event in auth events") | ||
assert ( | ||
builder.type == EventTypes.Create or len(prev_event_ids) > 0 | ||
), "Attempting to create an event with no prev_events" | ||
room_version_obj = await self.store.get_room_version(builder.room_id) | ||
if room_version_obj.msc2716_empty_prev_events: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Does this actually require a new room version? (discussed at #11114 (comment)) My thinking is that this code is only for creating events, not accepting events. So technically any other homeserver nowadays can create events with no Am I missing something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not that surprised that we accept outliers with empty prev-events, but we'll need special handling for the insertion event to be accepted, i.e. we'll need some sort of check like "this is an insertion event so we need to go and fetch the state rather than trying to calculate it". Though that can be part of the history import MSC I guess There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why is this the case? It seems to work in my Complement tests without any of this 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spooky! 👻 Is it doing something silly like dropping the event with no extremities and then doing a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure yet. If you really want me to dive into it, I can ⛳ |
||
# We allow events with no `prev_events` but it better have some `auth_events` | ||
assert ( | ||
builder.type == EventTypes.Create | ||
or len(prev_event_ids) > 0 | ||
MadLittleMods marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Allow an event to have empty list of prev_event_ids | ||
# only if it has auth_event_ids. | ||
or (auth_event_ids and len(auth_event_ids) > 0) | ||
erikjohnston marked this conversation as resolved.
Show resolved
Hide resolved
|
||
), "Attempting to create a non-m.room.create event with no prev_events or auth_event_ids" | ||
else: | ||
# we now ought to have some prev_events (unless it's a create event). | ||
assert ( | ||
builder.type == EventTypes.Create or len(prev_event_ids) > 0 | ||
), "Attempting to create a non-m.room.create event with no prev_events" | ||
|
||
event = await builder.build( | ||
prev_event_ids=prev_event_ids, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,14 @@ | |
from typing import Tuple | ||
|
||
from synapse.api.constants import EventTypes | ||
from synapse.api.room_versions import RoomVersions | ||
from synapse.events import EventBase | ||
from synapse.events.snapshot import EventContext | ||
from synapse.rest import admin | ||
from synapse.rest.client import login, room | ||
from synapse.types import create_requester | ||
from synapse.util.stringutils import random_string | ||
from tests.test_utils.event_injection import create_event | ||
|
||
from tests import unittest | ||
|
||
|
@@ -156,6 +158,79 @@ def test_duplicated_txn_id_one_call(self): | |
self.assertEqual(len(events), 2) | ||
self.assertEqual(events[0].event_id, events[1].event_id) | ||
|
||
def test_create_empty_prev_events_in_msc2716_room_version(self): | ||
"""Try to create an event without any prev_events (only auth_events). | ||
|
||
This is currently only supported in the experimental MSC2716 room versions. | ||
MadLittleMods marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
room_id = self.helper.create_room_as( | ||
self.user_id, | ||
tok=self.access_token, | ||
room_version=RoomVersions.MSC2716v4.identifier, | ||
) | ||
|
||
# Create a member event we can use as an auth_event | ||
memberEvent, memberEventContext = self.get_success( | ||
create_event( | ||
self.hs, | ||
room_id=room_id, | ||
type="m.room.member", | ||
sender=self.requester.user.to_string(), | ||
state_key=self.requester.user.to_string(), | ||
content={"membership": "join"}, | ||
) | ||
) | ||
self.get_success( | ||
self.persist_event_storage.persist_event(memberEvent, memberEventContext) | ||
) | ||
|
||
# Try to create the event with empty prev_events (only auth_events) | ||
event, _ = self.get_success( | ||
self.handler.create_event( | ||
self.requester, | ||
{ | ||
"type": EventTypes.Message, | ||
"room_id": room_id, | ||
"sender": self.requester.user.to_string(), | ||
"content": {"msgtype": "m.text", "body": random_string(5)}, | ||
}, | ||
# Empty prev_events is the key thing we're testing here | ||
prev_event_ids=[], | ||
auth_event_ids=[memberEvent.event_id], | ||
) | ||
) | ||
self.assertIsNotNone(event) | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_reject_empty_prev_events_and_auth_events_in_msc2716_room_version( | ||
self, | ||
): | ||
"""Try to create an event without any prev_events (only auth_events). | ||
MadLittleMods marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
This is currently only supported in the experimental MSC2716 room versions. | ||
MadLittleMods marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
room_id = self.helper.create_room_as( | ||
self.user_id, | ||
tok=self.access_token, | ||
room_version=RoomVersions.MSC2716v4.identifier, | ||
) | ||
|
||
# Try to create the event with empty prev_events and empty auth_events | ||
self.get_failure( | ||
self.handler.create_event( | ||
self.requester, | ||
{ | ||
"type": EventTypes.Message, | ||
"room_id": room_id, | ||
"sender": self.requester.user.to_string(), | ||
"content": {"msgtype": "m.text", "body": random_string(5)}, | ||
}, | ||
prev_event_ids=[], | ||
# The event should be rejected when there are no auth_events | ||
auth_event_ids=[], | ||
), | ||
AssertionError, | ||
) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
All of the MSC2716 Complement tests cover this use case since we use it so much there. I've just added some integration tests for this. I didn't add tests for every case to be completely exhaustive but did make sure this extra functionality works and that we don't allow events with empty
|
||
|
||
class ServerAclValidationTestCase(unittest.HomeserverTestCase): | ||
servlets = [ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review @anoadragon453 🦈