hash_history.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from __future__ import print_function
  2. import sqlite3
  3. import sys
  4. from unpaddedbase64 import decode_base64, encode_base64
  5. from synapse.crypto.event_signing import (
  6. add_event_pdu_content_hash,
  7. compute_pdu_event_reference_hash,
  8. )
  9. from synapse.federation.units import Pdu
  10. from synapse.storage._base import SQLBaseStore
  11. from synapse.storage.pdu import PduStore
  12. from synapse.storage.signatures import SignatureStore
  13. class Store(object):
  14. _get_pdu_tuples = PduStore.__dict__["_get_pdu_tuples"]
  15. _get_pdu_content_hashes_txn = SignatureStore.__dict__["_get_pdu_content_hashes_txn"]
  16. _get_prev_pdu_hashes_txn = SignatureStore.__dict__["_get_prev_pdu_hashes_txn"]
  17. _get_pdu_origin_signatures_txn = SignatureStore.__dict__[
  18. "_get_pdu_origin_signatures_txn"
  19. ]
  20. _store_pdu_content_hash_txn = SignatureStore.__dict__["_store_pdu_content_hash_txn"]
  21. _store_pdu_reference_hash_txn = SignatureStore.__dict__[
  22. "_store_pdu_reference_hash_txn"
  23. ]
  24. _store_prev_pdu_hash_txn = SignatureStore.__dict__["_store_prev_pdu_hash_txn"]
  25. simple_insert_txn = SQLBaseStore.__dict__["simple_insert_txn"]
  26. store = Store()
  27. def select_pdus(cursor):
  28. cursor.execute("SELECT pdu_id, origin FROM pdus ORDER BY depth ASC")
  29. ids = cursor.fetchall()
  30. pdu_tuples = store._get_pdu_tuples(cursor, ids)
  31. pdus = [Pdu.from_pdu_tuple(p) for p in pdu_tuples]
  32. reference_hashes = {}
  33. for pdu in pdus:
  34. try:
  35. if pdu.prev_pdus:
  36. print("PROCESS", pdu.pdu_id, pdu.origin, pdu.prev_pdus)
  37. for pdu_id, origin, hashes in pdu.prev_pdus:
  38. ref_alg, ref_hsh = reference_hashes[(pdu_id, origin)]
  39. hashes[ref_alg] = encode_base64(ref_hsh)
  40. store._store_prev_pdu_hash_txn(
  41. cursor, pdu.pdu_id, pdu.origin, pdu_id, origin, ref_alg, ref_hsh
  42. )
  43. print("SUCCESS", pdu.pdu_id, pdu.origin, pdu.prev_pdus)
  44. pdu = add_event_pdu_content_hash(pdu)
  45. ref_alg, ref_hsh = compute_pdu_event_reference_hash(pdu)
  46. reference_hashes[(pdu.pdu_id, pdu.origin)] = (ref_alg, ref_hsh)
  47. store._store_pdu_reference_hash_txn(
  48. cursor, pdu.pdu_id, pdu.origin, ref_alg, ref_hsh
  49. )
  50. for alg, hsh_base64 in pdu.hashes.items():
  51. print(alg, hsh_base64)
  52. store._store_pdu_content_hash_txn(
  53. cursor, pdu.pdu_id, pdu.origin, alg, decode_base64(hsh_base64)
  54. )
  55. except Exception:
  56. print("FAILED_", pdu.pdu_id, pdu.origin, pdu.prev_pdus)
  57. def main():
  58. conn = sqlite3.connect(sys.argv[1])
  59. cursor = conn.cursor()
  60. select_pdus(cursor)
  61. conn.commit()
  62. if __name__ == "__main__":
  63. main()