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

Convert additional databases to async/await part 2 #8200

Merged
merged 9 commits into from
Sep 1, 2020
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Convert signatures.
  • Loading branch information
clokep committed Aug 28, 2020
commit 94262bdc9badf66cc35b67af70d125966a521a7f
19 changes: 12 additions & 7 deletions synapse/events/builder.py
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Optional
from typing import Any, Dict, List, Optional, Tuple, Union

import attr
from nacl.signing import SigningKey
@@ -97,14 +97,14 @@ def state_key(self):
def is_state(self):
return self._state_key is not None

async def build(self, prev_event_ids):
async def build(self, prev_event_ids: List[str]) -> EventBase:
"""Transform into a fully signed and hashed event

Args:
prev_event_ids (list[str]): The event IDs to use as the prev events
prev_event_ids: The event IDs to use as the prev events

Returns:
FrozenEvent
The signed and hashed event.
"""

state_ids = await self._state.get_current_state_ids(
@@ -114,8 +114,13 @@ async def build(self, prev_event_ids):

format_version = self.room_version.event_format
if format_version == EventFormatVersions.V1:
auth_events = await self._store.add_event_hashes(auth_ids)
prev_events = await self._store.add_event_hashes(prev_event_ids)
# The types of auth/prev events changes between event versions.
auth_events = await self._store.add_event_hashes(
auth_ids
) # type: Union[List[str], List[Tuple[str, Dict[str, str]]]]
prev_events = await self._store.add_event_hashes(
prev_event_ids
) # type: Union[List[str], List[Tuple[str, Dict[str, str]]]]
else:
auth_events = auth_ids
prev_events = prev_event_ids
@@ -138,7 +143,7 @@ async def build(self, prev_event_ids):
"unsigned": self.unsigned,
"depth": depth,
"prev_state": [],
}
} # type: Dict[str, Any]

if self.is_state():
event_dict["state_key"] = self._state_key
5 changes: 2 additions & 3 deletions synapse/handlers/message.py
Original file line number Diff line number Diff line change
@@ -50,7 +50,6 @@
from synapse.storage.databases.main.events_worker import EventRedactBehaviour
from synapse.storage.state import StateFilter
from synapse.types import (
Collection,
Requester,
RoomAlias,
StreamToken,
@@ -446,7 +445,7 @@ async def create_event(
event_dict: dict,
token_id: Optional[str] = None,
txn_id: Optional[str] = None,
prev_event_ids: Optional[Collection[str]] = None,
prev_event_ids: Optional[List[str]] = None,
Copy link
Member Author

Choose a reason for hiding this comment

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

There's a bunch of changes of Collection -> List which I think is actually necessary because this eventually gets serialized to JSON?

require_consent: bool = True,
) -> Tuple[EventBase, EventContext]:
"""
@@ -786,7 +785,7 @@ async def create_new_client_event(
self,
builder: EventBuilder,
requester: Optional[Requester] = None,
prev_event_ids: Optional[Collection[str]] = None,
prev_event_ids: Optional[List[str]] = None,
) -> Tuple[EventBase, EventContext]:
"""Create a new event for a local client

3 changes: 1 addition & 2 deletions synapse/handlers/room_member.py
Original file line number Diff line number Diff line change
@@ -39,7 +39,6 @@
from synapse.events.validator import EventValidator
from synapse.storage.roommember import RoomsForUser
from synapse.types import (
Collection,
JsonDict,
Requester,
RoomAlias,
@@ -184,7 +183,7 @@ async def _local_membership_update(
target: UserID,
room_id: str,
membership: str,
prev_event_ids: Collection[str],
prev_event_ids: List[str],
txn_id: Optional[str] = None,
ratelimit: bool = True,
content: Optional[dict] = None,
40 changes: 33 additions & 7 deletions synapse/storage/databases/main/signatures.py
Original file line number Diff line number Diff line change
@@ -13,9 +13,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Dict, Iterable, List, Tuple

from unpaddedbase64 import encode_base64

from synapse.storage._base import SQLBaseStore
from synapse.storage.types import Cursor
from synapse.util.caches.descriptors import cached, cachedList


@@ -29,16 +32,37 @@ def get_event_reference_hash(self, event_id):
@cachedList(
cached_method_name="get_event_reference_hash", list_name="event_ids", num_args=1
)
def get_event_reference_hashes(self, event_ids):
async def get_event_reference_hashes(
self, event_ids: Iterable[str]
) -> Dict[str, Dict[str, bytes]]:
"""Get all hashes for given events.

Args:
event_ids: The event IDs to get hashes for.

Returns:
A mapping of event ID to a mapping of algorithm to hash.
"""

def f(txn):
return {
event_id: self._get_event_reference_hashes_txn(txn, event_id)
for event_id in event_ids
}

return self.db_pool.runInteraction("get_event_reference_hashes", f)
return await self.db_pool.runInteraction("get_event_reference_hashes", f)

async def add_event_hashes(self, event_ids):
async def add_event_hashes(
self, event_ids: Iterable[str]
) -> List[Tuple[str, Dict[str, str]]]:
"""

Args:
event_ids: The event IDs

Returns:
A list of tuples of event ID and a mapping of algorithm to base-64 encoded hash.
"""
hashes = await self.get_event_reference_hashes(event_ids)
hashes = {
e_id: {k: encode_base64(v) for k, v in h.items() if k == "sha256"}
@@ -47,13 +71,15 @@ async def add_event_hashes(self, event_ids):

return list(hashes.items())

def _get_event_reference_hashes_txn(self, txn, event_id):
def _get_event_reference_hashes_txn(
self, txn: Cursor, event_id: str
) -> Dict[str, bytes]:
"""Get all the hashes for a given PDU.
Args:
txn (cursor):
event_id (str): Id for the Event.
txn:
event_id: Id for the Event.
Returns:
A dict[unicode, bytes] of algorithm -> hash.
A mapping of algorithm -> hash.
"""
query = (
"SELECT algorithm, hash"
7 changes: 3 additions & 4 deletions tests/test_utils/event_injection.py
Original file line number Diff line number Diff line change
@@ -13,14 +13,13 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Optional, Tuple
from typing import List, Optional, Tuple

import synapse.server
from synapse.api.constants import EventTypes
from synapse.api.room_versions import KNOWN_ROOM_VERSIONS
from synapse.events import EventBase
from synapse.events.snapshot import EventContext
from synapse.types import Collection

"""
Utility functions for poking events into the storage of the server under test.
@@ -58,7 +57,7 @@ async def inject_member_event(
async def inject_event(
hs: synapse.server.HomeServer,
room_version: Optional[str] = None,
prev_event_ids: Optional[Collection[str]] = None,
prev_event_ids: Optional[List[str]] = None,
**kwargs
) -> EventBase:
"""Inject a generic event into a room
@@ -80,7 +79,7 @@ async def inject_event(
async def create_event(
hs: synapse.server.HomeServer,
room_version: Optional[str] = None,
prev_event_ids: Optional[Collection[str]] = None,
prev_event_ids: Optional[List[str]] = None,
**kwargs
) -> Tuple[EventBase, EventContext]:
if room_version is None: