This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Allow events to be created with no prev_events
(MSC2716)
#11243
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
39efad1
Allow events to be created with no prev_events
MadLittleMods 66f0859
Add changelog
MadLittleMods 9f45d09
Allow member state events without prev_events
MadLittleMods e093481
More clarification and specificity in errors/comments
MadLittleMods 4bb5fd3
Add tests for creating events with empty prev_events
MadLittleMods f421a2d
Fix lints
MadLittleMods d10625e
Update test description to be accurate
MadLittleMods 2370dca
Remove redundant length check
MadLittleMods e2928b5
No new room version necessary and allow no prev_events via allow_no_p…
MadLittleMods 85364c5
Remove extra verbose check
MadLittleMods 0ed300e
Only worry about having auth_events when allowing no prev_events
MadLittleMods 93ca0f8
Update changelog to describe intended usage
MadLittleMods 9b9deff
Merge branch 'develop' into madlittlemods/empty-prev-events
MadLittleMods 8fada7e
Fix lint
MadLittleMods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Allow specific, experimental events to be created without `prev_events`. Used by [MSC2716](https://github.com/matrix-org/matrix-doc/pull/2716). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
from synapse.util.stringutils import random_string | ||
|
||
from tests import unittest | ||
from tests.test_utils.event_injection import create_event | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
@@ -51,6 +52,24 @@ def prepare(self, reactor, clock, hs): | |
|
||
self.requester = create_requester(self.user_id, access_token_id=self.token_id) | ||
|
||
def _create_and_persist_member_event(self) -> Tuple[EventBase, EventContext]: | ||
# Create a member event we can use as an auth_event | ||
memberEvent, memberEventContext = self.get_success( | ||
create_event( | ||
self.hs, | ||
room_id=self.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) | ||
) | ||
|
||
return memberEvent, memberEventContext | ||
|
||
def _create_duplicate_event(self, txn_id: str) -> Tuple[EventBase, EventContext]: | ||
"""Create a new event with the given transaction ID. All events produced | ||
by this method will be considered duplicates. | ||
|
@@ -156,6 +175,90 @@ 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_when_empty_prev_events_allowed_create_event_with_empty_prev_events(self): | ||
"""When we set allow_no_prev_events=True, should be able to create a | ||
event without any prev_events (only auth_events). | ||
""" | ||
# Create a member event we can use as an auth_event | ||
memberEvent, _ = self._create_and_persist_member_event() | ||
|
||
# Try to create the event with empty prev_events bit with some auth_events | ||
event, _ = self.get_success( | ||
self.handler.create_event( | ||
self.requester, | ||
{ | ||
"type": EventTypes.Message, | ||
"room_id": self.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=[], | ||
# But with some auth_events | ||
auth_event_ids=[memberEvent.event_id], | ||
# Allow no prev_events! | ||
allow_no_prev_events=True, | ||
) | ||
) | ||
self.assertIsNotNone(event) | ||
anoadragon453 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def test_when_empty_prev_events_not_allowed_reject_event_with_empty_prev_events( | ||
self, | ||
): | ||
"""When we set allow_no_prev_events=False, shouldn't be able to create a | ||
event without any prev_events even if it has auth_events. Expect an | ||
exception to be raised. | ||
""" | ||
# Create a member event we can use as an auth_event | ||
memberEvent, _ = self._create_and_persist_member_event() | ||
|
||
# Try to create the event with empty prev_events but with some auth_events | ||
self.get_failure( | ||
self.handler.create_event( | ||
self.requester, | ||
{ | ||
"type": EventTypes.Message, | ||
"room_id": self.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=[], | ||
# But with some auth_events | ||
auth_event_ids=[memberEvent.event_id], | ||
# We expect the test to fail because empty prev_events are not | ||
# allowed here! | ||
allow_no_prev_events=False, | ||
), | ||
AssertionError, | ||
) | ||
|
||
def test_when_empty_prev_events_allowed_reject_event_with_empty_prev_events_and_auth_events( | ||
self, | ||
): | ||
"""When we set allow_no_prev_events=True, should be able to create a | ||
event without any prev_events or auth_events. Expect an exception to be | ||
raised. | ||
""" | ||
# 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": self.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=[], | ||
# Allow no prev_events! | ||
allow_no_prev_events=True, | ||
), | ||
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 = [ | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The insertion event is connected to the state chain so the whole historical batch can share the same
state_group
, see #10975. It also looks nice in the DAG semantically to be able to see the state that authed the batch.For the federation cases, it seems to work 🤔🤷.
auth_event_ids
probablyThere 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.
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 comment
The 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
outliers
🤔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/
server_name
problem mapping to a port onlocalhost
(tips welcome) and basing the config off of the demo script. I'm attempting to use/etc/hosts
and a nginx reverse proxy to127.0.0.1:8448
(non-tls) . Synapse is configured to betls: false
on8008
and8448
. This appears to work to getmy.matrix.host
andother.matrix.host
resolving but doesn't make Synapse happy enough to be able to successfully fetch the room list between each other.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 comment
The 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
./demo/start.sh
(and./demo/stop.sh
)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.
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
insertion
event, it does show up before the creation event. Not sure how Element handles events before the creation event 🤷♀️(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 comment
The reason will be displayed to describe this comment to others. Learn more.
I created #11487 to add a test to ensure the
state_groups
are shared.Things seem to work just fine if I disconnect the floating state chain from the
insertion
event so we can move forward with that in #11114 (comment) after this merges.Both the state chain and the
insertion
event chain can float with noprev_events
. No issues with sharedstate_groups
, accepting over federation, and the historical state does not show up at all in/messages