2014-08-12 16:10:52 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-09-03 18:29:13 +02:00
|
|
|
# Copyright 2014 OpenMarket Ltd
|
2014-08-12 16:10:52 +02:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# 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.
|
2014-08-13 04:14:34 +02:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
""" Defines the JSON structure of the protocol units used by the server to
|
|
|
|
server protocol.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from synapse.util.jsonobject import JsonEncodedObject
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Pdu(JsonEncodedObject):
|
|
|
|
""" A Pdu represents a piece of data sent from a server and is associated
|
|
|
|
with a context.
|
|
|
|
|
|
|
|
A Pdu can be classified as "state". For a given context, we can efficiently
|
|
|
|
retrieve all state pdu's that haven't been clobbered. Clobbering is done
|
2014-11-03 14:06:58 +01:00
|
|
|
via a unique constraint on the tuple (context, type, state_key). A pdu
|
2014-08-12 16:10:52 +02:00
|
|
|
is a state pdu if `is_state` is True.
|
|
|
|
|
|
|
|
Example pdu::
|
|
|
|
|
|
|
|
{
|
2014-11-03 14:06:58 +01:00
|
|
|
"event_id": "$78c:example.com",
|
2014-10-17 18:12:25 +02:00
|
|
|
"origin_server_ts": 1404835423000,
|
2014-08-12 16:10:52 +02:00
|
|
|
"origin": "bar",
|
|
|
|
"prev_ids": [
|
|
|
|
["23b", "foo"],
|
|
|
|
["56a", "bar"],
|
|
|
|
],
|
|
|
|
"content": { ... },
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
valid_keys = [
|
2014-11-03 14:06:58 +01:00
|
|
|
"event_id",
|
|
|
|
"room_id",
|
2014-08-12 16:10:52 +02:00
|
|
|
"origin",
|
2014-10-17 18:12:25 +02:00
|
|
|
"origin_server_ts",
|
2014-11-03 14:06:58 +01:00
|
|
|
"type",
|
2014-08-12 16:10:52 +02:00
|
|
|
"destinations",
|
2014-11-03 14:06:58 +01:00
|
|
|
"prev_events",
|
2014-08-12 16:10:52 +02:00
|
|
|
"depth",
|
|
|
|
"content",
|
2014-10-15 18:09:04 +02:00
|
|
|
"hashes",
|
2014-11-10 11:21:32 +01:00
|
|
|
"user_id",
|
|
|
|
"auth_events",
|
2014-11-03 18:51:42 +01:00
|
|
|
"signatures", # Below this are keys valid only for State Pdus.
|
2014-08-12 16:10:52 +02:00
|
|
|
"state_key",
|
2014-11-03 14:06:58 +01:00
|
|
|
"prev_state",
|
2014-08-12 16:10:52 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
internal_keys = [
|
|
|
|
"destinations",
|
|
|
|
"transaction_id",
|
|
|
|
"outlier",
|
|
|
|
]
|
|
|
|
|
|
|
|
required_keys = [
|
2014-11-03 14:06:58 +01:00
|
|
|
"event_id",
|
|
|
|
"room_id",
|
2014-08-12 16:10:52 +02:00
|
|
|
"origin",
|
2014-10-17 18:12:25 +02:00
|
|
|
"origin_server_ts",
|
2014-11-03 14:06:58 +01:00
|
|
|
"type",
|
2014-08-12 16:10:52 +02:00
|
|
|
"content",
|
|
|
|
]
|
|
|
|
|
|
|
|
# TODO: We need to make this properly load content rather than
|
|
|
|
# just leaving it as a dict. (OR DO WE?!)
|
|
|
|
|
2014-11-03 18:51:42 +01:00
|
|
|
def __init__(self, destinations=[], prev_events=[],
|
2014-10-15 18:09:04 +02:00
|
|
|
outlier=False, hashes={}, signatures={}, **kwargs):
|
2014-08-12 16:10:52 +02:00
|
|
|
super(Pdu, self).__init__(
|
|
|
|
destinations=destinations,
|
2014-11-03 14:06:58 +01:00
|
|
|
prev_events=prev_events,
|
2014-08-12 16:10:52 +02:00
|
|
|
outlier=outlier,
|
2014-10-15 18:09:04 +02:00
|
|
|
hashes=hashes,
|
|
|
|
signatures=signatures,
|
2014-08-12 16:10:52 +02:00
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return "(%s, %s)" % (self.__class__.__name__, repr(self.__dict__))
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "<%s, %s>" % (self.__class__.__name__, repr(self.__dict__))
|
|
|
|
|
|
|
|
|
|
|
|
class Edu(JsonEncodedObject):
|
|
|
|
""" An Edu represents a piece of data sent from one homeserver to another.
|
|
|
|
|
|
|
|
In comparison to Pdus, Edus are not persisted for a long time on disk, are
|
|
|
|
not meaningful beyond a given pair of homeservers, and don't have an
|
|
|
|
internal ID or previous references graph.
|
|
|
|
"""
|
|
|
|
|
|
|
|
valid_keys = [
|
|
|
|
"origin",
|
|
|
|
"destination",
|
|
|
|
"edu_type",
|
|
|
|
"content",
|
|
|
|
]
|
|
|
|
|
|
|
|
required_keys = [
|
|
|
|
"edu_type",
|
|
|
|
]
|
|
|
|
|
2014-11-13 16:48:51 +01:00
|
|
|
internal_keys = [
|
|
|
|
"origin",
|
|
|
|
"destination",
|
|
|
|
]
|
2014-10-14 17:44:27 +02:00
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
|
|
|
|
class Transaction(JsonEncodedObject):
|
|
|
|
""" A transaction is a list of Pdus and Edus to be sent to a remote home
|
|
|
|
server with some extra metadata.
|
|
|
|
|
|
|
|
Example transaction::
|
|
|
|
|
|
|
|
{
|
|
|
|
"origin": "foo",
|
|
|
|
"prev_ids": ["abc", "def"],
|
|
|
|
"pdus": [
|
|
|
|
...
|
|
|
|
],
|
|
|
|
}
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
valid_keys = [
|
|
|
|
"transaction_id",
|
|
|
|
"origin",
|
|
|
|
"destination",
|
2014-10-17 18:12:25 +02:00
|
|
|
"origin_server_ts",
|
2014-08-12 16:10:52 +02:00
|
|
|
"previous_ids",
|
|
|
|
"pdus",
|
|
|
|
"edus",
|
|
|
|
"transaction_id",
|
|
|
|
"destination",
|
2014-11-04 15:17:55 +01:00
|
|
|
"pdu_failures",
|
2014-08-12 16:10:52 +02:00
|
|
|
]
|
|
|
|
|
2014-10-13 12:49:40 +02:00
|
|
|
internal_keys = [
|
|
|
|
"transaction_id",
|
|
|
|
"destination",
|
|
|
|
]
|
|
|
|
|
2014-08-12 16:10:52 +02:00
|
|
|
required_keys = [
|
|
|
|
"transaction_id",
|
|
|
|
"origin",
|
|
|
|
"destination",
|
2014-10-17 18:12:25 +02:00
|
|
|
"origin_server_ts",
|
2014-08-12 16:10:52 +02:00
|
|
|
"pdus",
|
|
|
|
]
|
|
|
|
|
|
|
|
def __init__(self, transaction_id=None, pdus=[], **kwargs):
|
|
|
|
""" If we include a list of pdus then we decode then as PDU's
|
|
|
|
automatically.
|
|
|
|
"""
|
|
|
|
|
|
|
|
# If there's no EDUs then remove the arg
|
|
|
|
if "edus" in kwargs and not kwargs["edus"]:
|
|
|
|
del kwargs["edus"]
|
|
|
|
|
|
|
|
super(Transaction, self).__init__(
|
|
|
|
transaction_id=transaction_id,
|
|
|
|
pdus=pdus,
|
|
|
|
**kwargs
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create_new(pdus, **kwargs):
|
|
|
|
""" Used to create a new transaction. Will auto fill out
|
2014-10-17 18:12:25 +02:00
|
|
|
transaction_id and origin_server_ts keys.
|
2014-08-12 16:10:52 +02:00
|
|
|
"""
|
2014-10-17 18:12:25 +02:00
|
|
|
if "origin_server_ts" not in kwargs:
|
2014-11-10 14:46:44 +01:00
|
|
|
raise KeyError(
|
|
|
|
"Require 'origin_server_ts' to construct a Transaction"
|
|
|
|
)
|
2014-08-12 16:10:52 +02:00
|
|
|
if "transaction_id" not in kwargs:
|
|
|
|
raise KeyError(
|
|
|
|
"Require 'transaction_id' to construct a Transaction"
|
|
|
|
)
|
|
|
|
|
|
|
|
for p in pdus:
|
|
|
|
p.transaction_id = kwargs["transaction_id"]
|
|
|
|
|
|
|
|
kwargs["pdus"] = [p.get_dict() for p in pdus]
|
|
|
|
|
|
|
|
return Transaction(**kwargs)
|