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
Refactor create_new_client_event
to use a new parameter, state_event_ids
, which accurately describes the usage with MSC2716 instead of abusing auth_event_ids
#12083
Merged
MadLittleMods
merged 13 commits into
develop
from
madlittlemods/10975-r813183430-refactor-to-full-state-parameter
Mar 25, 2022
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
93bf1f1
Refactor create_new_client_event to use full state parameter
MadLittleMods 6a7879f
Better parameter name and docstring description
MadLittleMods f4fde12
Clarify which state events are being persisted
MadLittleMods 0942f53
Fix type checking lint
MadLittleMods ffa58ed
Add changelog
MadLittleMods 09be642
Fix changelog grammar
MadLittleMods f3e234e
Merge branch 'develop' into madlittlemods/10975-r813183430-refactor-t…
MadLittleMods 878bdda
Only define state at start of floating chain and derive rest
MadLittleMods 225675b
Merge branch 'develop' into madlittlemods/10975-r813183430-refactor-t…
MadLittleMods aa0a109
More comment clarification and fix argument typos
MadLittleMods 9f902da
Explain why we want to set the state there
MadLittleMods 289faf1
Update comment docs
MadLittleMods cd93056
Some cleanup
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 @@ | ||
Refactor `create_new_client_event` to use a new parameter, `state_event_ids`, which accurately describes the usage with [MSC2716](https://github.com/matrix-org/matrix-doc/pull/2716) instead of abusing `auth_event_ids`. |
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 |
---|---|---|
|
@@ -493,6 +493,7 @@ async def create_event( | |
allow_no_prev_events: bool = False, | ||
prev_event_ids: Optional[List[str]] = None, | ||
auth_event_ids: Optional[List[str]] = None, | ||
state_event_ids: Optional[List[str]] = None, | ||
require_consent: bool = True, | ||
outlier: bool = False, | ||
historical: bool = False, | ||
|
@@ -527,6 +528,15 @@ async def create_event( | |
|
||
If non-None, prev_event_ids must also be provided. | ||
|
||
state_event_ids: | ||
The full state at a given event. This is used particularly by the MSC2716 | ||
/batch_send endpoint. One use case is with insertion events which float at | ||
the beginning of a historical batch and don't have any `prev_events` to | ||
derive from; we add all of these state events as the explicit state so the | ||
rest of the historical batch can inherit the same state and state_group. | ||
This should normally be left as None, which will cause the auth_event_ids | ||
to be calculated based on the room state at the prev_events. | ||
|
||
require_consent: Whether to check if the requester has | ||
consented to the privacy policy. | ||
|
||
|
@@ -612,6 +622,7 @@ async def create_event( | |
allow_no_prev_events=allow_no_prev_events, | ||
prev_event_ids=prev_event_ids, | ||
auth_event_ids=auth_event_ids, | ||
state_event_ids=state_event_ids, | ||
depth=depth, | ||
) | ||
|
||
|
@@ -772,6 +783,7 @@ async def create_and_send_nonmember_event( | |
allow_no_prev_events: bool = False, | ||
prev_event_ids: Optional[List[str]] = None, | ||
auth_event_ids: Optional[List[str]] = None, | ||
state_event_ids: Optional[List[str]] = None, | ||
ratelimit: bool = True, | ||
txn_id: Optional[str] = None, | ||
ignore_shadow_ban: bool = False, | ||
|
@@ -801,6 +813,14 @@ async def create_and_send_nonmember_event( | |
based on the room state at the prev_events. | ||
|
||
If non-None, prev_event_ids must also be provided. | ||
state_event_ids: | ||
The full state at a given event. This is used particularly by the MSC2716 | ||
/batch_send endpoint. One use case is with insertion events which float at | ||
the beginning of a historical batch and don't have any `prev_events` to | ||
derive from; we add all of these state events as the explicit state so the | ||
rest of the historical batch can inherit the same state and state_group. | ||
This should normally be left as None, which will cause the auth_event_ids | ||
to be calculated based on the room state at the prev_events. | ||
ratelimit: Whether to rate limit this send. | ||
txn_id: The transaction ID. | ||
ignore_shadow_ban: True if shadow-banned users should be allowed to | ||
|
@@ -856,8 +876,10 @@ async def create_and_send_nonmember_event( | |
requester, | ||
event_dict, | ||
txn_id=txn_id, | ||
allow_no_prev_events=allow_no_prev_events, | ||
prev_event_ids=prev_event_ids, | ||
auth_event_ids=auth_event_ids, | ||
state_event_ids=state_event_ids, | ||
outlier=outlier, | ||
historical=historical, | ||
depth=depth, | ||
|
@@ -893,6 +915,7 @@ async def create_new_client_event( | |
allow_no_prev_events: bool = False, | ||
prev_event_ids: Optional[List[str]] = None, | ||
auth_event_ids: Optional[List[str]] = None, | ||
state_event_ids: Optional[List[str]] = None, | ||
depth: Optional[int] = None, | ||
) -> Tuple[EventBase, EventContext]: | ||
"""Create a new event for a local client | ||
|
@@ -915,38 +938,42 @@ async def create_new_client_event( | |
Should normally be left as None, which will cause them to be calculated | ||
based on the room state at the prev_events. | ||
|
||
state_event_ids: | ||
The full state at a given event. This is used particularly by the MSC2716 | ||
/batch_send endpoint. One use case is with insertion events which float at | ||
the beginning of a historical batch and don't have any `prev_events` to | ||
derive from; we add all of these state events as the explicit state so the | ||
rest of the historical batch can inherit the same state and state_group. | ||
This should normally be left as None, which will cause the auth_event_ids | ||
to be calculated based on the room state at the prev_events. | ||
|
||
depth: Override the depth used to order the event in the DAG. | ||
Should normally be set to None, which will cause the depth to be calculated | ||
based on the prev_events. | ||
|
||
Returns: | ||
Tuple of created event, context | ||
""" | ||
# Strip down the auth_event_ids to only what we need to auth the event. | ||
# Strip down the state_event_ids to only what we need to auth the event. | ||
# For example, we don't need extra m.room.member that don't match event.sender | ||
full_state_ids_at_event = None | ||
if auth_event_ids is not None: | ||
# If auth events are provided, prev events must be also. | ||
if state_event_ids is not None: | ||
# Do a quick check to make sure that prev_event_ids is present to | ||
# make the type-checking around `builder.build` happy. | ||
# 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 | ||
full_state_ids_at_event = auth_event_ids.copy() | ||
|
||
temp_event = await builder.build( | ||
prev_event_ids=prev_event_ids, | ||
auth_event_ids=auth_event_ids, | ||
auth_event_ids=state_event_ids, | ||
depth=depth, | ||
) | ||
auth_events = await self.store.get_events_as_list(auth_event_ids) | ||
state_events = await self.store.get_events_as_list(state_event_ids) | ||
# Create a StateMap[str] | ||
auth_event_state_map = { | ||
(e.type, e.state_key): e.event_id for e in auth_events | ||
} | ||
# Actually strip down and use the necessary auth events | ||
state_map = {(e.type, e.state_key): e.event_id for e in state_events} | ||
# Actually strip down and only use the necessary auth events | ||
auth_event_ids = self._event_auth_handler.compute_auth_events( | ||
event=temp_event, | ||
current_state_ids=auth_event_state_map, | ||
current_state_ids=state_map, | ||
for_verification=False, | ||
) | ||
|
||
|
@@ -989,12 +1016,16 @@ async def create_new_client_event( | |
context = EventContext.for_outlier() | ||
elif ( | ||
event.type == EventTypes.MSC2716_INSERTION | ||
and full_state_ids_at_event | ||
and state_event_ids | ||
and builder.internal_metadata.is_historical() | ||
): | ||
# Add explicit state to the insertion event so it has state to derive | ||
# from even though it's floating with no `prev_events`. The rest of | ||
# the batch can derive from this state and state_group. | ||
# | ||
# TODO(faster_joins): figure out how this works, and make sure that the | ||
# old state is complete. | ||
old_state = await self.store.get_events_as_list(full_state_ids_at_event) | ||
old_state = await self.store.get_events_as_list(state_event_ids) | ||
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. Added some nice context for why we're doing this that was only a PR review comment previously, #10975 (comment) |
||
context = await self.state.compute_event_context(event, old_state=old_state) | ||
else: | ||
context = await self.state.compute_event_context(event) | ||
|
Oops, something went wrong.
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.
was the previous omission of this a bug?
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.
(could we get rid of
allow_no_prev_events
and just allow there to be no prev events whenstate_event_ids
is given?)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.
Yes and seems to be why this PR fixes @tulir's issue, #12083 (comment)
I'm leaning to no although technically yes probably.
The assertion nature of
allow_no_prev_events
disallowing the case for the majority of normal event sending cases is nice.Also in our case, we only want the first event to have no
prev_event_ids
. The rest in the chain usingstate_event_ids
should haveprev_event_ids
pointing at the event before it in the chain.state_event_ids
is really just used to deriveauth_event_ids
. Although I do see how they're related,auth_event_ids
explicitly set bystate_event_ids
or derived fromprev_event_ids
in normal cases.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.
For context: we see a fair few "can't create event with no prev events" errors already, so having an
allow_no_prev_events
flag should hopefully make it easier to reason about what is going on when investigating that issue.