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

Allow events to be created with no prev_events (MSC2716) #11243

Merged
merged 14 commits into from
Dec 11, 2021
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/11243.misc
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).
24 changes: 18 additions & 6 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ async def create_event(
require_consent: bool = True,
outlier: bool = False,
historical: bool = False,
allow_no_prev_events: bool = False,
depth: Optional[int] = None,
) -> Tuple[EventBase, EventContext]:
"""
Expand Down Expand Up @@ -607,6 +608,7 @@ async def create_event(
prev_event_ids=prev_event_ids,
auth_event_ids=auth_event_ids,
depth=depth,
allow_no_prev_events=allow_no_prev_events,
)

# In an ideal world we wouldn't need the second part of this condition. However,
Expand Down Expand Up @@ -882,6 +884,7 @@ async def create_new_client_event(
prev_event_ids: Optional[List[str]] = None,
auth_event_ids: Optional[List[str]] = None,
depth: Optional[int] = None,
allow_no_prev_events: bool = False,
) -> Tuple[EventBase, EventContext]:
"""Create a new event for a local client

Expand Down Expand Up @@ -912,6 +915,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
Copy link
Contributor Author

@MadLittleMods MadLittleMods Nov 17, 2021

Choose a reason for hiding this comment

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

Separately: I'm not sure its correct that the insertion event points to anything else, as we need want federation to ask for the state at the insertion event, rather than any of the state events that it references?

-- @erikjohnston

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 probably

Copy link
Member

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?

Copy link
Contributor Author

@MadLittleMods MadLittleMods Dec 1, 2021

Choose a reason for hiding this comment

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

I don't think that is necessary to make the whole batch have the same state group.

I can play around with it but that seems like something for another PR. It's pretty finicky to get right.

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?

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 on localhost (tips welcome) and basing the config off of the demo script. I'm attempting to use /etc/hosts and a nginx reverse proxy to 127.0.0.1:8448 (non-tls) . Synapse is configured to be tls: false on 8008 and 8448. This appears to work to get my.matrix.host and other.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.

Copy link
Member

Choose a reason for hiding this comment

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

I'm trying to setup a federated homeserver locally to see if it's a problem for remote federated servers.

FYI you can start some preconfigured HSes that federate with each other by running ./demo/start.sh (and ./demo/stop.sh)

Copy link
Contributor Author

@MadLittleMods MadLittleMods Dec 2, 2021

Choose a reason for hiding this comment

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

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?

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.

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
{"chunk":[{"content":{"org.matrix.msc2716.marker.insertion":"$WaBcgnXayK2kF09ZpmfCftYGIxrtVkFoNvHh6aeU424"},"origin_server_ts":1638421073731,"room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@the-bridge-user:hs1","type":"org.matrix.msc2716.marker","unsigned":{"age":7480},"event_id":"$lS2ED7HslhXZkRx8VL1TXKDY3f_BKwFHUiQWs00dYa0","user_id":"@the-bridge-user:hs1","age":7480},{"type":"m.room.member","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@charlie:hs2","content":{"membership":"join","displayname":"charlie","avatar_url":null},"state_key":"@charlie:hs2","origin_server_ts":1638421073600,"unsigned":{"age":7604},"event_id":"$O-pdVPKIVSMkeM3HYVrZlGOOlT_vOB-a89neIAtc6xM","user_id":"@charlie:hs2","age":7604},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@alice:hs1","content":{"body":"Message 1 (eventIDsAfter)","msgtype":"m.text"},"origin_server_ts":1638421072318,"unsigned":{"age":1578},"event_id":"$g1ARnN19QFCcjwn1uhB_i8QOyWI-DO0re-UkcD-vx-w","user_id":"@alice:hs1","age":1578},{"type":"org.matrix.msc2716.insertion","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@the-bridge-user:hs1","content":{"org.matrix.msc2716.next_batch_id":"bWoojCNr","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072237,"unsigned":{"age":1464},"event_id":"$WaBcgnXayK2kF09ZpmfCftYGIxrtVkFoNvHh6aeU424","user_id":"@the-bridge-user:hs1","age":1464},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@alice:hs1","content":{"body":"Message 0 (eventIDsAfter)","msgtype":"m.text"},"origin_server_ts":1638421072266,"unsigned":{"age":1630},"event_id":"$FIvwe-eNldbj7tBHuF2T7WDxQm9V359KWOf-cRtKWSo","user_id":"@alice:hs1","age":1630},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 9 (batch=0)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072237,"unsigned":{"age":1612},"event_id":"$umIFPlRr2XmB3fick6hV7Y3QhkpxcYjnjukqmSLE5aQ","user_id":"@maria:hs1","age":1612},{"type":"org.matrix.msc2716.batch","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@the-bridge-user:hs1","content":{"org.matrix.msc2716.batch_id":"bWoojCNr","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072237,"unsigned":{"age":1433},"event_id":"$0bFJFfzv7E6COheiVMKxA1YfzX3ashIRPZXSZHROj7w","user_id":"@the-bridge-user:hs1","age":1433},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 8 (batch=0)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072236,"unsigned":{"age":1640},"event_id":"$z3OxR4a5TDfrSKEOie2NNaXmKmf6JPNsDPrPeT7_wYQ","user_id":"@maria:hs1","age":1640},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 7 (batch=0)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072235,"unsigned":{"age":1670},"event_id":"$vTZCRWjTrhK4patXggIjatGjSPZCFwOaYdSprU0q6wM","user_id":"@maria:hs1","age":1670},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 6 (batch=0)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072234,"unsigned":{"age":2205},"event_id":"$leVj2y-j_050_u6JADUiLwpEUdgnU5KUNDwxgcqb8L4","user_id":"@maria:hs1","age":2205},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 5 (batch=0)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072233,"unsigned":{"age":2745},"event_id":"$rQCHqPz54-EzCBPjM8euve-9bx3UOnYQvzctG1FrCe8","user_id":"@maria:hs1","age":2745},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 4 (batch=0)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072232,"unsigned":{"age":2780},"event_id":"$zmvZmUOtdMf32G4zW-VkfRCeN7y7vQY0jKcAmK3Dpeo","user_id":"@maria:hs1","age":2780},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 3 (batch=0)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072231,"unsigned":{"age":2813},"event_id":"$bZje4pt6EBeUQWO7itQsvjLpS-BzqXyaemqlUvxejW8","user_id":"@maria:hs1","age":2813},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 2 (batch=0)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072230,"unsigned":{"age":2846},"event_id":"$870hp6qsrfMrouJtEfpUAQwmRJCqaVbz4-P0OFeCMgo","user_id":"@maria:hs1","age":2846},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 1 (batch=0)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072229,"unsigned":{"age":3378},"event_id":"$Hj66b_hjH4day9qZX9RB3QNV9sTEg-0r2u4jhQECFQM","user_id":"@maria:hs1","age":3378},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 0 (batch=0)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072228,"unsigned":{"age":3921},"event_id":"$YMU4z7whUw8clargTpTYJ_rE8TbdIsGZpSyReHKrhm8","user_id":"@maria:hs1","age":3921},{"type":"org.matrix.msc2716.insertion","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@the-bridge-user:hs1","content":{"org.matrix.msc2716.next_batch_id":"DLkCgJwG","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072228,"unsigned":{"age":3965},"event_id":"$waIk9kxoQ6m8BQOrBAt0XMfmmVfD4WDXe5A4NJnPAM0","user_id":"@the-bridge-user:hs1","age":3965},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 9 (batch=1)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072227,"unsigned":{"age":3767},"event_id":"$wHINyaKdsd5xjxqO-BMDThuEBk2aOt2sJZGZzhwipCs","user_id":"@maria:hs1","age":3767},{"type":"org.matrix.msc2716.batch","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@the-bridge-user:hs1","content":{"org.matrix.msc2716.batch_id":"DLkCgJwG","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072227,"unsigned":{"age":1253},"event_id":"$sztVY6kBjSev1X3Hm6BViRNYyc1cQS5Vhjyt9lAMyMU","user_id":"@the-bridge-user:hs1","age":1253},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 8 (batch=1)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072226,"unsigned":{"age":3803},"event_id":"$GqXxKglEtttl-iIcpjDqShn5OTkW07SipgCzCq2iAjY","user_id":"@maria:hs1","age":3803},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 7 (batch=1)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072225,"unsigned":{"age":3838},"event_id":"$ZFDgIDCUMfs-qHDlSQW8F-LWiKIFRiHgTCqaXlPLe4k","user_id":"@maria:hs1","age":3838},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 6 (batch=1)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072224,"unsigned":{"age":4376},"event_id":"$oEVuLh6ZcbQGD3aeFkr2tYUwtCCkyO5qGAFGz2fLTnw","user_id":"@maria:hs1","age":4376},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 5 (batch=1)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072223,"unsigned":{"age":4917},"event_id":"$5x2BQurlJ3T4GcspRlF8fOW9CrwKwkauAgkgBXOjVSc","user_id":"@maria:hs1","age":4917},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 4 (batch=1)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072222,"unsigned":{"age":4956},"event_id":"$r4e4rp9HuJ2gW_-1U4favhRTiqIsYeQXlG4L9b8RvEA","user_id":"@maria:hs1","age":4956},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 3 (batch=1)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072221,"unsigned":{"age":4990},"event_id":"$I-Vc7nCmA2UI9zCW0jsjA9QU-xo8XrRa9crUCikQ484","user_id":"@maria:hs1","age":4990},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 2 (batch=1)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072220,"unsigned":{"age":5023},"event_id":"$kew5mWiogSEWq5WJiIwqoylrmC4Mbi63EXT63H-lmIY","user_id":"@maria:hs1","age":5023},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 1 (batch=1)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072219,"unsigned":{"age":5554},"event_id":"$SKCLLf0Aok4urhbf-0wY_r_ACEAjaPi9pjec62fYpVE","user_id":"@maria:hs1","age":5554},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 0 (batch=1)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072218,"unsigned":{"age":6099},"event_id":"$14kxr9vQItjx2JawLphSmrj1Jwr8kfc_EOx-gT-iong","user_id":"@maria:hs1","age":6099},{"type":"org.matrix.msc2716.insertion","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@the-bridge-user:hs1","content":{"org.matrix.msc2716.next_batch_id":"CoyLEmTJ","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072218,"unsigned":{"age":6140},"event_id":"$gZx6dAB98BmyjAdCVZKgayF_c4L_gz1cy45i-8N4fSI","user_id":"@the-bridge-user:hs1","age":6140},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 9 (batch=2)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072217,"unsigned":{"age":5947},"event_id":"$pFssNAX_rVeY_PhxSSMquxHLZXr5oN05Fiv_QfPCvkE","user_id":"@maria:hs1","age":5947},{"type":"org.matrix.msc2716.batch","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@the-bridge-user:hs1","content":{"org.matrix.msc2716.batch_id":"CoyLEmTJ","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072217,"unsigned":{"age":1069},"event_id":"$JVTwaVHhgnW8cfjVqW-pS_5mhODVJZa4PVHxdG1Efzc","user_id":"@the-bridge-user:hs1","age":1069},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 8 (batch=2)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072216,"unsigned":{"age":5980},"event_id":"$qCMOwxe1GrVNzyWKN3zaQ-lg3lW1DUNrpfoRnebWFm4","user_id":"@maria:hs1","age":5980},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 7 (batch=2)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072215,"unsigned":{"age":6012},"event_id":"$UfDaJL_LPXVwXKKQs73XzYmI-Uir4jN7W6IziZ2SCYI","user_id":"@maria:hs1","age":6012},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 6 (batch=2)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072214,"unsigned":{"age":6552},"event_id":"$TeMnyuzXPRShgq3Fh1pQN96cssNHqtH4iGTvK8ZHAMw","user_id":"@maria:hs1","age":6552},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 5 (batch=2)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072213,"unsigned":{"age":7089},"event_id":"$Cn4JfL0SeaTBiRd_LNqZAVuvMmR8AvGUKYCOjg5uTt0","user_id":"@maria:hs1","age":7089},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 4 (batch=2)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072212,"unsigned":{"age":7126},"event_id":"$V9_aO69DgrYi2WMiPmgPcUuvD05dyncCldrKtUfzZSA","user_id":"@maria:hs1","age":7126},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 3 (batch=2)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072211,"unsigned":{"age":7164},"event_id":"$tJvxEsxTR9HJliBPcNSvJFmOY94ciipYTAXUov_K_Yo","user_id":"@maria:hs1","age":7164},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 2 (batch=2)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072210,"unsigned":{"age":7200},"event_id":"$Rdp3bV85LI-s3PvGmUOruK2TltWpULtpYcJ5WKQg6m8","user_id":"@maria:hs1","age":7200},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 1 (batch=2)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072209,"unsigned":{"age":7741},"event_id":"$gs5cV3RM2-TuKjGz0BGPsxlrMdUNVD0cjgdx1H7hQdE","user_id":"@maria:hs1","age":7741},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"body":"Historical 0 (batch=2)","msgtype":"m.text","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072208,"unsigned":{"age":8286},"event_id":"$kqSb-7ejvJqCbMw0n3HoNtlX1xLV_vqoj0p5pmyp6Os","user_id":"@maria:hs1","age":8286},{"type":"org.matrix.msc2716.insertion","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@the-bridge-user:hs1","content":{"org.matrix.msc2716.next_batch_id":"nhKNQtdq","org.matrix.msc2716.historical":true},"origin_server_ts":1638421072208,"unsigned":{"age":8335},"event_id":"$QWeEjdorD08UUrOSv4V51uruiFwN9AW2pyhMtjTKdWc","user_id":"@the-bridge-user:hs1","age":8335},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@alice:hs1","content":{"body":"Message 1 (eventIDsBefore)","msgtype":"m.text"},"origin_server_ts":1638421072192,"unsigned":{"age":1704},"event_id":"$o9SYeHYTCBmh7I8oQUuYrYiunNnRh6hm-hajCz_uVn8","user_id":"@alice:hs1","age":1704},{"type":"m.room.message","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@alice:hs1","content":{"body":"Message 0 (eventIDsBefore)","msgtype":"m.text"},"origin_server_ts":1638421072119,"unsigned":{"age":1777},"event_id":"$tvHWwYy5am7_ivgPNMRMevMdbEnYldR0cOJzV4IK9D0","user_id":"@alice:hs1","age":1777},{"type":"m.room.member","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@alice:hs1","content":{"membership":"join","displayname":"alice"},"state_key":"@alice:hs1","origin_server_ts":1638421072077,"unsigned":{"age":1565},"event_id":"$ejmNHLgOrZ_dDN0CsooWespSg0vZ8zVTl77M12ke_PY","user_id":"@alice:hs1","age":1565},{"type":"m.room.name","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@the-bridge-user:hs1","content":{"name":"the hangout spot"},"state_key":"","origin_server_ts":1638421072038,"unsigned":{"age":1604},"event_id":"$MM3mC6Ig2bVODUcI1EEphKLBO_q2t0jXCErMxpdFgaM","user_id":"@the-bridge-user:hs1","age":1604},{"type":"m.room.history_visibility","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@the-bridge-user:hs1","content":{"history_visibility":"shared"},"state_key":"","origin_server_ts":1638421072010,"unsigned":{"age":1632},"event_id":"$M6OepRC0jQYBjmv7sQNup4MiJIdMG8TbdDBO0cMkVZ8","user_id":"@the-bridge-user:hs1","age":1632},{"type":"m.room.join_rules","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@the-bridge-user:hs1","content":{"join_rule":"public"},"state_key":"","origin_server_ts":1638421071982,"unsigned":{"age":1660},"event_id":"$H0JEhKMu0cbWH6lnIu_UpKWWh9ooVq3Wu3Dgj9SPUF0","user_id":"@the-bridge-user:hs1","age":1660},{"type":"m.room.power_levels","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@the-bridge-user:hs1","content":{"users":{"@the-bridge-user:hs1":100},"users_default":0,"events":{"m.room.name":50,"m.room.power_levels":100,"m.room.history_visibility":100,"m.room.canonical_alias":50,"m.room.avatar":50,"m.room.tombstone":100,"m.room.server_acl":100,"m.room.encryption":100},"events_default":0,"state_default":50,"ban":50,"kick":50,"redact":50,"invite":50,"historical":100},"state_key":"","origin_server_ts":1638421071938,"unsigned":{"age":1704},"event_id":"$5vDhHe7-M7M_hET9i2-MhLESIDILl9r9HadKksWbtos","user_id":"@the-bridge-user:hs1","age":1704},{"type":"m.room.member","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@the-bridge-user:hs1","content":{"membership":"join"},"state_key":"@the-bridge-user:hs1","origin_server_ts":1638421071897,"unsigned":{"age":1745},"event_id":"$7IaD95Sp4SRExyY971nzbcXgI9S67pQ9FnfdMIEjheI","user_id":"@the-bridge-user:hs1","age":1745},{"type":"m.room.create","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@the-bridge-user:hs1","content":{"room_version":"org.matrix.msc2716v4","creator":"@the-bridge-user:hs1"},"state_key":"","origin_server_ts":1638421071834,"unsigned":{"age":1808},"event_id":"$yigMLKdi72kpZFdwTpJL-s71lV_vLqJAKgVeboqXA-g","user_id":"@the-bridge-user:hs1","age":1808},{"type":"m.room.member","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"displayname":"some-display-name-for-@maria:hs1","membership":"join","org.matrix.msc2716.historical":true},"state_key":"@maria:hs1","origin_server_ts":1638421072739,"unsigned":{"age":1158},"event_id":"$Ean2gLNBtIH6CHaSQ5XicbIZ1pYk-N7PFIO9RTytSM0","user_id":"@maria:hs1","age":1158},{"type":"m.room.member","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"displayname":"some-display-name-for-@maria:hs1","membership":"join","org.matrix.msc2716.historical":true},"state_key":"@maria:hs1","origin_server_ts":1638421072570,"unsigned":{"age":1327},"event_id":"$LJUfF9Eo58Nbq7cNYoN-oGowd_OwfzgUkux4O3Ksikk","user_id":"@maria:hs1","age":1327},{"type":"m.room.member","room_id":"!FRNexMHUjKhRxPvPzq:hs1","sender":"@maria:hs1","content":{"displayname":"some-display-name-for-@maria:hs1","membership":"join","org.matrix.msc2716.historical":true},"state_key":"@maria:hs1","origin_server_ts":1638421072370,"unsigned":{"age":1527},"event_id":"$OIbefO5BM9jGxwk7_lF3Wdy9gO-PG1ZuGq2rQscznOg","user_id":"@maria:hs1","age":1527}],"start":"s3_0_0_0_0_0_0_0_0","end":"t1--13_0_0_0_0_0_0_0_0"}

Copy link
Contributor Author

@MadLittleMods MadLittleMods Dec 2, 2021

Choose a reason for hiding this comment

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

I don't think that is necessary to make the whole batch have the same state group.

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 no prev_events. No issues with shared state_groups, accepting over federation, and the historical state does not show up at all in /messages

Expand Down Expand Up @@ -943,14 +947,22 @@ 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"
if allow_no_prev_events:
# We allow events with no `prev_events` but it better have some `auth_events`
assert (
builder.type == EventTypes.Create
# Allow an event to have empty list of prev_event_ids
# only if it has auth_event_ids.
or auth_event_ids
), "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 prev_event_ids
), "Attempting to create a non-m.room.create event with no prev_events"

event = await builder.build(
prev_event_ids=prev_event_ids,
Expand Down
3 changes: 2 additions & 1 deletion synapse/handlers/room_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,8 @@ async def update_membership_locked(
if block_invite:
raise SynapseError(403, "Invites have been disabled on this server")

if prev_event_ids:
# An empty prev_events list is allowed as long as the auth_event_ids are present
if prev_event_ids is not None:
MadLittleMods marked this conversation as resolved.
Show resolved Hide resolved
return await self._local_membership_update(
requester=requester,
target=target,
Expand Down
103 changes: 103 additions & 0 deletions tests/handlers/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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,
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be nice to have some tests for this, unless it's expected to be covered by matrix-org/complement#214?

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 auth_events

  • Creating a m.room.create event
    • ❌ In MSC2716 room version
    • ❌ In existing stable room version
  • Creating event with empty prev_event
    • ✅ In MSC2716 room version
    • ❌ In existing stable room version
  • Creating event with empty prev_event and empty auth_event (make sure rejected)
    • ✅ In MSC2716 room version
    • ❌ In existing stable room version
  • Creating event with prev_events
    • ❌ In MSC2716 room version
    • ❌ In existing stable room version
  • Creating event with prev_events but empty auth_event (make sure rejected)
    • ❌ In MSC2716 room version
    • ❌ In existing stable room version


class ServerAclValidationTestCase(unittest.HomeserverTestCase):
servlets = [
Expand Down