summary refs log tree commit diff
path: root/scripts-dev/hash_history.py
blob: bf3862a386530d76f04c98d9bbd5e0e5b6e796b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from __future__ import print_function

import sqlite3
import sys

from unpaddedbase64 import decode_base64, encode_base64

from synapse.crypto.event_signing import (
    add_event_pdu_content_hash,
    compute_pdu_event_reference_hash,
)
from synapse.federation.units import Pdu
from synapse.storage._base import SQLBaseStore
from synapse.storage.pdu import PduStore
from synapse.storage.signatures import SignatureStore


class Store(object):
    _get_pdu_tuples = PduStore.__dict__["_get_pdu_tuples"]
    _get_pdu_content_hashes_txn = SignatureStore.__dict__["_get_pdu_content_hashes_txn"]
    _get_prev_pdu_hashes_txn = SignatureStore.__dict__["_get_prev_pdu_hashes_txn"]
    _get_pdu_origin_signatures_txn = SignatureStore.__dict__[
        "_get_pdu_origin_signatures_txn"
    ]
    _store_pdu_content_hash_txn = SignatureStore.__dict__["_store_pdu_content_hash_txn"]
    _store_pdu_reference_hash_txn = SignatureStore.__dict__[
        "_store_pdu_reference_hash_txn"
    ]
    _store_prev_pdu_hash_txn = SignatureStore.__dict__["_store_prev_pdu_hash_txn"]
    simple_insert_txn = SQLBaseStore.__dict__["simple_insert_txn"]


store = Store()


def select_pdus(cursor):
    cursor.execute("SELECT pdu_id, origin FROM pdus ORDER BY depth ASC")

    ids = cursor.fetchall()

    pdu_tuples = store._get_pdu_tuples(cursor, ids)

    pdus = [Pdu.from_pdu_tuple(p) for p in pdu_tuples]

    reference_hashes = {}

    for pdu in pdus:
        try:
            if pdu.prev_pdus:
                print("PROCESS", pdu.pdu_id, pdu.origin, pdu.prev_pdus)
                for pdu_id, origin, hashes in pdu.prev_pdus:
                    ref_alg, ref_hsh = reference_hashes[(pdu_id, origin)]
                    hashes[ref_alg] = encode_base64(ref_hsh)
                    store._store_prev_pdu_hash_txn(
                        cursor, pdu.pdu_id, pdu.origin, pdu_id, origin, ref_alg, ref_hsh
                    )
                print("SUCCESS", pdu.pdu_id, pdu.origin, pdu.prev_pdus)
            pdu = add_event_pdu_content_hash(pdu)
            ref_alg, ref_hsh = compute_pdu_event_reference_hash(pdu)
            reference_hashes[(pdu.pdu_id, pdu.origin)] = (ref_alg, ref_hsh)
            store._store_pdu_reference_hash_txn(
                cursor, pdu.pdu_id, pdu.origin, ref_alg, ref_hsh
            )

            for alg, hsh_base64 in pdu.hashes.items():
                print(alg, hsh_base64)
                store._store_pdu_content_hash_txn(
                    cursor, pdu.pdu_id, pdu.origin, alg, decode_base64(hsh_base64)
                )

        except Exception:
            print("FAILED_", pdu.pdu_id, pdu.origin, pdu.prev_pdus)


def main():
    conn = sqlite3.connect(sys.argv[1])
    cursor = conn.cursor()
    select_pdus(cursor)
    conn.commit()


if __name__ == "__main__":
    main()