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

Optimise state resolution #1818

Merged
merged 8 commits into from
Jan 18, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
49 changes: 43 additions & 6 deletions synapse/event_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
logger = logging.getLogger(__name__)


def check(event, auth_events, do_sig_check=True):
def check(event, auth_events, do_sig_check=True, do_size_check=True):
""" Checks if this event is correctly authed.

Args:
Expand All @@ -38,7 +38,8 @@ def check(event, auth_events, do_sig_check=True):
Returns:
True if the auth checks pass.
"""
_check_size_limits(event)
if do_size_check:
_check_size_limits(event)

if not hasattr(event, "room_id"):
raise AuthError(500, "Event has no room_id: %s" % event)
Expand Down Expand Up @@ -119,10 +120,11 @@ def check(event, auth_events, do_sig_check=True):
)
return True

logger.debug(
"Auth events: %s",
[a.event_id for a in auth_events.values()]
)
if logger.isEnabledFor(logging.DEBUG):
logger.debug(
"Auth events: %s",
[a.event_id for a in auth_events.values()]
)

if event.type == EventTypes.Member:
allowed = _is_membership_change_allowed(
Expand Down Expand Up @@ -639,3 +641,38 @@ def get_public_keys(invite_event):
public_keys.append(o)
public_keys.extend(invite_event.content.get("public_keys", []))
return public_keys


def auth_types_for_event(event):
"""Given an event, return a list of (EventType, StateKey) that may be
needed to auth the event. The returned list may be a superset of what
would actually be required depending on the full state of the room.

Used to limit the number of events to fetch from the database to
actually auth the event.
"""
if event.type == EventTypes.Create:
return []

auth_types = []

auth_types.append((EventTypes.PowerLevels, "", ))
auth_types.append((EventTypes.Member, event.user_id, ))
auth_types.append((EventTypes.Create, "", ))

if event.type == EventTypes.Member:
e_type = event.content["membership"]
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe call this membership rather than e_type?

if e_type in [Membership.JOIN, Membership.INVITE]:
auth_types.append((EventTypes.JoinRules, "", ))

auth_types.append((EventTypes.Member, event.state_key, ))

if e_type == Membership.INVITE:
if "third_party_invite" in event.content:
key = (
EventTypes.ThirdPartyInvite,
event.content["third_party_invite"]["signed"]["token"]
)
auth_types.append(key)

return auth_types
8 changes: 5 additions & 3 deletions synapse/events/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ def __init__(self, event_dict, signatures={}, unsigned={},
auth_events = _event_dict_property("auth_events")
depth = _event_dict_property("depth")
content = _event_dict_property("content")
event_id = _event_dict_property("event_id")
hashes = _event_dict_property("hashes")
origin = _event_dict_property("origin")
origin_server_ts = _event_dict_property("origin_server_ts")
Expand All @@ -88,8 +87,6 @@ def __init__(self, event_dict, signatures={}, unsigned={},
redacts = _event_dict_property("redacts")
room_id = _event_dict_property("room_id")
sender = _event_dict_property("sender")
state_key = _event_dict_property("state_key")
type = _event_dict_property("type")
user_id = _event_dict_property("sender")

@property
Expand Down Expand Up @@ -162,6 +159,11 @@ def __init__(self, event_dict, internal_metadata_dict={}, rejected_reason=None):
else:
frozen_dict = event_dict

self.event_id = event_dict["event_id"]
self.type = event_dict["type"]
if "state_key" in event_dict:
self.state_key = event_dict["state_key"]

super(FrozenEvent, self).__init__(
frozen_dict,
signatures=signatures,
Expand Down
6 changes: 5 additions & 1 deletion synapse/events/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from . import EventBase, FrozenEvent
from . import EventBase, FrozenEvent, _event_dict_property

from synapse.types import EventID

Expand All @@ -34,6 +34,10 @@ def __init__(self, key_values={}, internal_metadata_dict={}):
internal_metadata_dict=internal_metadata_dict,
)

event_id = _event_dict_property("event_id")
state_key = _event_dict_property("state_key")
type = _event_dict_property("type")

def build(self):
return FrozenEvent.from_event(self)

Expand Down
6 changes: 4 additions & 2 deletions synapse/federation/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from synapse.util.caches.expiringcache import ExpiringCache
from synapse.util.logutils import log_function
from synapse.util.logcontext import preserve_fn, preserve_context_over_deferred
from synapse.events import FrozenEvent
from synapse.events import FrozenEvent, builder
import synapse.metrics

from synapse.util.retryutils import get_retry_limiter, NotRetryingDestination
Expand Down Expand Up @@ -499,8 +499,10 @@ def make_membership_event(self, destinations, room_id, user_id, membership,
if "prev_state" not in pdu_dict:
pdu_dict["prev_state"] = []

ev = builder.EventBuilder(pdu_dict)

defer.returnValue(
(destination, self.event_from_pdu_json(pdu_dict))
(destination, ev)
)
break
except CodeMessageException as e:
Expand Down
4 changes: 2 additions & 2 deletions synapse/handlers/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ def try_backfill(domains):
preserve_fn(self.state_handler.resolve_state_groups)(room_id, [e])
for e in event_ids
]))
states = dict(zip(event_ids, [s[1] for s in states]))
states = dict(zip(event_ids, [s.state for s in states]))

state_map = yield self.store.get_events(
[e_id for ids in states.values() for e_id in ids],
Expand Down Expand Up @@ -1530,7 +1530,7 @@ def do_auth(self, origin, event, context, auth_events):
(d.type, d.state_key): d for d in different_events if d
})

new_state, prev_state = self.state_handler.resolve_events(
new_state = self.state_handler.resolve_events(
[local_view.values(), remote_view.values()],
event
)
Expand Down
Loading