1
0

hash_history.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from synapse.storage.pdu import PduStore
  2. from synapse.storage.signatures import SignatureStore
  3. from synapse.storage._base import SQLBaseStore
  4. from synapse.federation.units import Pdu
  5. from synapse.crypto.event_signing import (
  6. add_event_pdu_content_hash, compute_pdu_event_reference_hash
  7. )
  8. from synapse.api.events.utils import prune_pdu
  9. from unpaddedbase64 import encode_base64, decode_base64
  10. from canonicaljson import encode_canonical_json
  11. import sqlite3
  12. import sys
  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__["_get_pdu_origin_signatures_txn"]
  18. _store_pdu_content_hash_txn = SignatureStore.__dict__["_store_pdu_content_hash_txn"]
  19. _store_pdu_reference_hash_txn = SignatureStore.__dict__["_store_pdu_reference_hash_txn"]
  20. _store_prev_pdu_hash_txn = SignatureStore.__dict__["_store_prev_pdu_hash_txn"]
  21. _simple_insert_txn = SQLBaseStore.__dict__["_simple_insert_txn"]
  22. store = Store()
  23. def select_pdus(cursor):
  24. cursor.execute(
  25. "SELECT pdu_id, origin FROM pdus ORDER BY depth ASC"
  26. )
  27. ids = cursor.fetchall()
  28. pdu_tuples = store._get_pdu_tuples(cursor, ids)
  29. pdus = [Pdu.from_pdu_tuple(p) for p in pdu_tuples]
  30. reference_hashes = {}
  31. for pdu in pdus:
  32. try:
  33. if pdu.prev_pdus:
  34. print "PROCESS", pdu.pdu_id, pdu.origin, pdu.prev_pdus
  35. for pdu_id, origin, hashes in pdu.prev_pdus:
  36. ref_alg, ref_hsh = reference_hashes[(pdu_id, origin)]
  37. hashes[ref_alg] = encode_base64(ref_hsh)
  38. store._store_prev_pdu_hash_txn(cursor, pdu.pdu_id, pdu.origin, pdu_id, origin, ref_alg, ref_hsh)
  39. print "SUCCESS", pdu.pdu_id, pdu.origin, pdu.prev_pdus
  40. pdu = add_event_pdu_content_hash(pdu)
  41. ref_alg, ref_hsh = compute_pdu_event_reference_hash(pdu)
  42. reference_hashes[(pdu.pdu_id, pdu.origin)] = (ref_alg, ref_hsh)
  43. store._store_pdu_reference_hash_txn(cursor, pdu.pdu_id, pdu.origin, ref_alg, ref_hsh)
  44. for alg, hsh_base64 in pdu.hashes.items():
  45. print alg, hsh_base64
  46. store._store_pdu_content_hash_txn(cursor, pdu.pdu_id, pdu.origin, alg, decode_base64(hsh_base64))
  47. except:
  48. print "FAILED_", pdu.pdu_id, pdu.origin, pdu.prev_pdus
  49. def main():
  50. conn = sqlite3.connect(sys.argv[1])
  51. cursor = conn.cursor()
  52. select_pdus(cursor)
  53. conn.commit()
  54. if __name__=='__main__':
  55. main()