Repository: matrix-org/synapse

Issue:
synapse fails if `prev_state` is missing from an event received over federation
This field is not specced, and we shouldn't be relying on what the remote server is telling us anyway


Discussion:
```
2019-03-03 15:56:11,267 - synapse.http.server - 112 - ERROR - PUT-4279- Failed handle request via 'FederationSendJoinServlet': <SynapseRequest at 0x7f3738494e10 method='PUT' uri='/_matrix/federati
Traceback (most recent call last):
  File "/home/rav/work/synapse/synapse/http/server.py", line 81, in wrapped_request_handler
    yield h(self, request)
  File "/home/rav/work/synapse/synapse/http/server.py", line 316, in _async_render
    callback_return = yield callback(request, **kwargs)
  File "/home/rav/work/synapse/synapse/federation/transport/server.py", line 289, in new_func
    origin, content, request.args, *args, **kwargs
  File "/home/rav/work/synapse/synapse/federation/transport/server.py", line 502, in on_PUT
    content = yield self.handler.on_send_join_request(origin, content, context)
  File "/home/rav/work/synapse/synapse/federation/federation_server.py", line 419, in on_send_join_request
    res_pdus = yield self.handler.on_send_join_request(origin, pdu)
  File "/home/rav/work/synapse/synapse/handlers/federation.py", line 1257, in on_send_join_request
    origin, event
  File "/home/rav/work/synapse/synapse/handlers/federation.py", line 1598, in _handle_new_event
    backfilled=backfilled,
  File "/home/rav/work/synapse/synapse/handlers/federation.py", line 2527, in persist_events_and_notify
    backfilled=backfilled,
  File "/home/rav/work/synapse/synapse/storage/events.py", line 277, in persist_events
    defer.gatherResults(deferreds, consumeErrors=True)
twisted.internet.defer.FirstError: FirstError[#0, [Failure instance: Traceback: <class 'AttributeError'>: prev_state
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/internet/defer.py:654:_runCallbacks
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/internet/defer.py:1475:gotResult
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/internet/defer.py:1416:_inlineCallbacks
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/python/failure.py:491:throwExceptionIntoGenerator
--- <exception caught here> ---
/home/rav/work/synapse/synapse/storage/events.py:157:handle_queue_loop
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/internet/defer.py:1416:_inlineCallbacks
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/python/failure.py:491:throwExceptionIntoGenerator
/home/rav/work/synapse/synapse/storage/events.py:316:persisting_queue
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/internet/defer.py:1416:_inlineCallbacks
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/python/failure.py:491:throwExceptionIntoGenerator
/home/rav/work/synapse/synapse/storage/events.py:198:f
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/internet/defer.py:1416:_inlineCallbacks
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/python/failure.py:491:throwExceptionIntoGenerator
/home/rav/work/synapse/synapse/storage/events.py:493:_persist_events
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/internet/defer.py:1416:_inlineCallbacks
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/python/failure.py:491:throwExceptionIntoGenerator
/home/rav/work/synapse/synapse/storage/_base.py:408:runInteraction
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/internet/defer.py:1416:_inlineCallbacks
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/python/failure.py:491:throwExceptionIntoGenerator
/home/rav/work/synapse/synapse/storage/_base.py:457:runWithConnection
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/python/threadpool.py:250:inContext
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/python/threadpool.py:266:<lambda>
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/python/context.py:122:callWithContext
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/python/context.py:85:callWithContext
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/enterprise/adbapi.py:306:_runWithConnection
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/python/compat.py:464:reraise
/home/rav/work/synapse/env3/lib/python3.6/site-packages/twisted/enterprise/adbapi.py:297:_runWithConnection
/home/rav/work/synapse/synapse/storage/_base.py:453:inner_func
/home/rav/work/synapse/synapse/storage/_base.py:329:_new_transaction
/home/rav/work/synapse/synapse/util/logutils.py:88:wrapped
/home/rav/work/synapse/synapse/storage/events.py:902:_persist_events_txn
/home/rav/work/synapse/synapse/storage/events.py:1419:_update_metadata_tables_txn
/home/rav/work/synapse/synapse/storage/events.py:1420:<listcomp>
/home/rav/work/synapse/synapse/events/__init__.py:89:getter
]]
```
sytest branch at https://github.com/matrix-org/sytest/tree/rav/remove_prev_state

Resolving patch:
diff --git a/synapse/events/__init__.py b/synapse/events/__init__.py
--- a/synapse/events/__init__.py
+++ b/synapse/events/__init__.py
@@ -141,7 +141,6 @@ def __init__(self, event_dict, signatures={}, unsigned={},
     origin = _event_dict_property("origin")
     origin_server_ts = _event_dict_property("origin_server_ts")
     prev_events = _event_dict_property("prev_events")
-    prev_state = _event_dict_property("prev_state")
     redacts = _event_dict_property("redacts")
     room_id = _event_dict_property("room_id")
     sender = _event_dict_property("sender")
diff --git a/synapse/storage/events.py b/synapse/storage/events.py
--- a/synapse/storage/events.py
+++ b/synapse/storage/events.py
@@ -1407,21 +1407,6 @@ def _update_metadata_tables_txn(self, txn, events_and_contexts,
             values=state_values,
         )
 
-        self._simple_insert_many_txn(
-            txn,
-            table="event_edges",
-            values=[
-                {
-                    "event_id": event.event_id,
-                    "prev_event_id": prev_id,
-                    "room_id": event.room_id,
-                    "is_state": True,
-                }
-                for event, _ in state_events_and_contexts
-                for prev_id, _ in event.prev_state
-            ],
-        )
-
         # Prefill the event cache
         self._add_to_cache(txn, events_and_contexts)
 


Test patch: