From db4644434817a58d4ce9ab1eb976f921cde0c5cb Mon Sep 17 00:00:00 2001 From: Sergi Delgado Segura Date: Thu, 9 Jan 2020 18:20:00 +0100 Subject: [PATCH] Adapts the tests to work with data in db instead of memory --- test/pisa/unit/test_cleaner.py | 19 +++++----- test/pisa/unit/test_responder.py | 59 ++++++++++++++++++++++---------- test/pisa/unit/test_watcher.py | 21 ++++++++++-- 3 files changed, 67 insertions(+), 32 deletions(-) diff --git a/test/pisa/unit/test_cleaner.py b/test/pisa/unit/test_cleaner.py index b7467f3..5b9eaf2 100644 --- a/test/pisa/unit/test_cleaner.py +++ b/test/pisa/unit/test_cleaner.py @@ -4,7 +4,7 @@ from uuid import uuid4 from pisa.responder import TransactionTracker from pisa.cleaner import Cleaner from common.appointment import Appointment -from pisa.db_manager import WATCHER_PREFIX +from pisa.db_manager import WATCHER_PREFIX, TRIGGERED_APPOINTMENTS_PREFIX from test.pisa.unit.conftest import get_random_value_hex @@ -26,7 +26,7 @@ def set_up_appointments(db_manager, total_appointments): locator = get_random_value_hex(LOCATOR_LEN_BYTES) appointment = Appointment(locator, None, None, None, None) - appointments[uuid] = appointment + appointments[uuid] = {"locator": appointment.locator} locator_uuid_map[locator] = [uuid] db_manager.store_watcher_appointment(uuid, appointment.to_json()) @@ -36,7 +36,7 @@ def set_up_appointments(db_manager, total_appointments): if i % 2: uuid = uuid4().hex - appointments[uuid] = appointment + appointments[uuid] = {"locator": appointment.locator} locator_uuid_map[locator].append(uuid) db_manager.store_watcher_appointment(uuid, appointment.to_json()) @@ -59,7 +59,7 @@ def set_up_trackers(db_manager, total_trackers): # Assign both penalty_txid and dispute_txid the same id (it shouldn't matter) tracker = TransactionTracker(locator, dispute_txid, penalty_txid, None, None) - trackers[uuid] = tracker + trackers[uuid] = {"locator": tracker.locator, "penalty_txid": tracker.penalty_txid} tx_tracker_map[penalty_txid] = [uuid] db_manager.store_responder_tracker(uuid, tracker.to_json()) @@ -69,7 +69,7 @@ def set_up_trackers(db_manager, total_trackers): if i % 2: uuid = uuid4().hex - trackers[uuid] = tracker + trackers[uuid] = {"locator": tracker.locator, "penalty_txid": tracker.penalty_txid} tx_tracker_map[penalty_txid].append(uuid) db_manager.store_responder_tracker(uuid, tracker.to_json()) @@ -99,9 +99,8 @@ def test_delete_completed_appointments(db_manager): assert len(appointments) == 0 # Make sure that all appointments are flagged as triggered in the db - db_appointments = db_manager.load_appointments_db(prefix=WATCHER_PREFIX) for uuid in uuids: - assert db_appointments[uuid]["triggered"] is True + assert db_manager.db.get((TRIGGERED_APPOINTMENTS_PREFIX + uuid).encode("utf-8")) is not None def test_delete_completed_trackers_db_match(db_manager): @@ -128,12 +127,12 @@ def test_delete_completed_trackers_no_db_match(db_manager): # Let's change some uuid's by creating new trackers that are not included in the db and share a penalty_txid # with another tracker that is stored in the db. for uuid in selected_trackers[: ITEMS // 2]: - penalty_txid = trackers[uuid].penalty_txid + penalty_txid = trackers[uuid].get("penalty_txid") dispute_txid = get_random_value_hex(32) locator = dispute_txid[:LOCATOR_LEN_HEX] new_uuid = uuid4().hex - trackers[new_uuid] = TransactionTracker(locator, dispute_txid, penalty_txid, None, None) + trackers[new_uuid] = {"locator": locator, "penalty_txid": penalty_txid} tx_tracker_map[penalty_txid].append(new_uuid) selected_trackers.append(new_uuid) @@ -144,7 +143,7 @@ def test_delete_completed_trackers_no_db_match(db_manager): dispute_txid = get_random_value_hex(32) locator = dispute_txid[:LOCATOR_LEN_HEX] - trackers[uuid] = TransactionTracker(locator, dispute_txid, penalty_txid, None, None) + trackers[uuid] = {"locator": locator, "penalty_txid": penalty_txid} tx_tracker_map[penalty_txid] = [uuid] selected_trackers.append(uuid) diff --git a/test/pisa/unit/test_responder.py b/test/pisa/unit/test_responder.py index a9e99da..cfb0be0 100644 --- a/test/pisa/unit/test_responder.py +++ b/test/pisa/unit/test_responder.py @@ -228,11 +228,9 @@ def test_add_tracker(responder): # Check that the rest of tracker data also matches tracker = responder.trackers[uuid] assert ( - tracker.dispute_txid == dispute_txid - and tracker.penalty_txid == penalty_txid - and tracker.penalty_rawtx == penalty_rawtx - and tracker.appointment_end == appointment_end - and tracker.appointment_end == appointment_end + tracker.get("penalty_txid") == penalty_txid + and tracker.get("locator") == locator + and tracker.get("appointment_end") == appointment_end ) @@ -255,11 +253,9 @@ def test_add_tracker_same_penalty_txid(responder): for uuid in [uuid_1, uuid_2]: tracker = responder.trackers[uuid] assert ( - tracker.dispute_txid == dispute_txid - and tracker.penalty_txid == penalty_txid - and tracker.penalty_rawtx == penalty_rawtx - and tracker.appointment_end == appointment_end - and tracker.appointment_end == appointment_end + tracker.get("penalty_txid") == penalty_txid + and tracker.get("locator") == locator + and tracker.get("appointment_end") == appointment_end ) @@ -308,11 +304,19 @@ def test_do_watch(temp_db_manager): for tracker in trackers: uuid = uuid4().hex - responder.trackers[uuid] = tracker + responder.trackers[uuid] = { + "locator": tracker.locator, + "penalty_txid": tracker.penalty_txid, + "appointment_end": tracker.appointment_end, + } responder.tx_tracker_map[tracker.penalty_txid] = [uuid] responder.missed_confirmations[tracker.penalty_txid] = 0 responder.unconfirmed_txs.append(tracker.penalty_txid) + # We also need to store the info in the db + responder.db_manager.create_triggered_appointment_flag(uuid) + responder.db_manager.store_responder_tracker(uuid, tracker.to_json()) + # Let's start to watch watch_thread = Thread(target=responder.do_watch) watch_thread.daemon = True @@ -434,12 +438,20 @@ def test_get_completed_trackers(db_manager): tracker.appointment_end += 10 trackers_no_end[uuid4().hex] = tracker - # Let's add all to the responder - responder.trackers.update(trackers_end_conf) - responder.trackers.update(trackers_end_no_conf) - responder.trackers.update(trackers_no_end) + all_trackers = {} + all_trackers.update(trackers_end_conf) + all_trackers.update(trackers_end_no_conf) + all_trackers.update(trackers_no_end) - for uuid, tracker in responder.trackers.items(): + # Let's add all to the responder + for uuid, tracker in all_trackers.items(): + responder.trackers[uuid] = { + "locator": tracker.locator, + "penalty_txid": tracker.penalty_txid, + "appointment_end": tracker.appointment_end, + } + + for uuid, tracker in all_trackers.items(): bitcoin_cli().sendrawtransaction(tracker.penalty_rawtx) # The dummy appointments have a end_appointment time of current + 2, but trackers need at least 6 confs by default @@ -474,9 +486,18 @@ def test_rebroadcast(db_manager): penalty_rawtx=TX.create_dummy_transaction() ) - responder.trackers[uuid] = TransactionTracker( - locator, dispute_txid, penalty_txid, penalty_rawtx, appointment_end - ) + tracker = TransactionTracker(locator, dispute_txid, penalty_txid, penalty_rawtx, appointment_end) + + responder.trackers[uuid] = { + "locator": locator, + "penalty_txid": penalty_txid, + "appointment_end": appointment_end, + } + + # We need to add it to the db too + responder.db_manager.create_triggered_appointment_flag(uuid) + responder.db_manager.store_responder_tracker(uuid, tracker.to_json()) + responder.tx_tracker_map[penalty_txid] = [uuid] responder.unconfirmed_txs.append(penalty_txid) diff --git a/test/pisa/unit/test_watcher.py b/test/pisa/unit/test_watcher.py index 8ec331c..a05b6fd 100644 --- a/test/pisa/unit/test_watcher.py +++ b/test/pisa/unit/test_watcher.py @@ -139,7 +139,16 @@ def test_do_subscribe(watcher): def test_do_watch(watcher): # We will wipe all the previous data and add 5 appointments - watcher.appointments, watcher.locator_uuid_map, dispute_txs = create_appointments(APPOINTMENTS) + appointments, locator_uuid_map, dispute_txs = create_appointments(APPOINTMENTS) + + # Set the data into the Watcher and in the db + watcher.locator_uuid_map = locator_uuid_map + watcher.appointments = {} + + for uuid, appointment in appointments.items(): + watcher.appointments[uuid] = {"locator": appointment.locator, "end_time": appointment.end_time} + watcher.db_manager.store_watcher_appointment(uuid, appointment.to_json()) + watcher.db_manager.store_update_locator_map(appointment.locator, uuid) watch_thread = Thread(target=watcher.do_watch) watch_thread.daemon = True @@ -190,7 +199,9 @@ def test_filter_valid_breaches_random_data(watcher): for i in range(TEST_SET_SIZE): dummy_appointment, _ = generate_dummy_appointment() uuid = uuid4().hex - appointments[uuid] = dummy_appointment + appointments[uuid] = {"locator": dummy_appointment.locator, "end_time": dummy_appointment.end_time} + watcher.db_manager.store_watcher_appointment(uuid, dummy_appointment.to_json()) + watcher.db_manager.store_update_locator_map(dummy_appointment.locator, uuid) locator_uuid_map[dummy_appointment.locator] = [uuid] @@ -226,7 +237,11 @@ def test_filter_valid_breaches(watcher): locator_uuid_map = {dummy_appointment.locator: [uuid]} breaches = {dummy_appointment.locator: dispute_txid} - watcher.appointments = appointments + for uuid, appointment in appointments.items(): + watcher.appointments[uuid] = {"locator": appointment.locator, "end_time": appointment.end_time} + watcher.db_manager.store_watcher_appointment(uuid, dummy_appointment.to_json()) + watcher.db_manager.store_update_locator_map(dummy_appointment.locator, uuid) + watcher.locator_uuid_map = locator_uuid_map filtered_valid_breaches = watcher.filter_valid_breaches(breaches)