hash_history.py 2.7 KB

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