From ad078cf3af640033b68fb5043e4fc5d709f2db5d Mon Sep 17 00:00:00 2001 From: Turtle Date: Mon, 6 Apr 2020 01:20:30 -0400 Subject: [PATCH] Test get_all_appointments e2e --- test/teos/e2e/conftest.py | 27 +++++++++++++- test/teos/e2e/test_basic_e2e.py | 63 +++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/test/teos/e2e/conftest.py b/test/teos/e2e/conftest.py index eb892c0..38c6485 100644 --- a/test/teos/e2e/conftest.py +++ b/test/teos/e2e/conftest.py @@ -38,7 +38,7 @@ def prng_seed(): def setup_node(bitcoin_cli): # This method will create a new address a mine bitcoin so the node can be used for testing new_addr = bitcoin_cli.getnewaddress() - bitcoin_cli.generatetoaddress(101, new_addr) + bitcoin_cli.generatetoaddress(106, new_addr) @pytest.fixture() @@ -60,6 +60,31 @@ def create_txs(bitcoin_cli): return signed_commitment_tx, signed_penalty_tx +@pytest.fixture() +def create_five_txs(bitcoin_cli): + utxos = bitcoin_cli.listunspent() + + signed_commitment_txs = [] + signed_penalty_txs = [] + + for i in range(5): + if len(utxos) == 0: + raise ValueError("There're no UTXOs.") + + utxo = utxos.pop(0) + while utxo.get("amount") < Decimal(2 / pow(10, 5)): + utxo = utxos.pop(0) + + signed_commitment_tx = create_commitment_tx(bitcoin_cli, utxo) + + signed_commitment_txs.append(signed_commitment_tx) + decoded_commitment_tx = bitcoin_cli.decoderawtransaction(signed_commitment_tx) + + signed_penalty_txs.append(create_penalty_tx(bitcoin_cli, decoded_commitment_tx)) + + return signed_commitment_txs, signed_penalty_txs + + def run_teosd(): teosd_process = Process(target=main, kwargs={"command_line_conf": {}}, daemon=True) teosd_process.start() diff --git a/test/teos/e2e/test_basic_e2e.py b/test/teos/e2e/test_basic_e2e.py index ab47c49..8b13ab3 100644 --- a/test/teos/e2e/test_basic_e2e.py +++ b/test/teos/e2e/test_basic_e2e.py @@ -1,3 +1,4 @@ +import json from time import sleep from riemann.tx import Tx from binascii import hexlify @@ -27,6 +28,7 @@ common.cryptographer.logger = Logger(actor="Cryptographer", log_name_prefix="") teos_base_endpoint = "http://{}:{}".format(cli_config.get("TEOS_SERVER"), cli_config.get("TEOS_PORT")) teos_add_appointment_endpoint = "{}/add_appointment".format(teos_base_endpoint) teos_get_appointment_endpoint = "{}/get_appointment".format(teos_base_endpoint) +teos_get_all_appointments_endpoint = "{}/get_all_appointments".format(teos_base_endpoint) # Run teosd teosd_process = run_teosd() @@ -53,6 +55,11 @@ def add_appointment(appointment_data, sk=cli_sk): ) +def get_all_appointments(): + r = teos_cli.get_all_appointments(teos_base_endpoint) + return json.loads(r) + + def test_commands_non_registered(bitcoin_cli, create_txs): # All commands should fail if the user is not registered @@ -101,6 +108,11 @@ def test_appointment_life_cycle(bitcoin_cli, create_txs): assert appointment_info is not None assert appointment_info.get("status") == "being_watched" + all_appointments = get_all_appointments() + watching = all_appointments.get("watcher_appointments") + responding = all_appointments.get("responder_trackers") + assert len(watching) == 1 and len(responding) == 0 + new_addr = bitcoin_cli.getnewaddress() broadcast_transaction_and_mine_block(bitcoin_cli, commitment_tx, new_addr) @@ -108,6 +120,11 @@ def test_appointment_life_cycle(bitcoin_cli, create_txs): assert appointment_info is not None assert appointment_info.get("status") == "dispute_responded" + all_appointments = get_all_appointments() + watching = all_appointments.get("watcher_appointments") + responding = all_appointments.get("responder_trackers") + assert len(watching) == 0 and len(responding) == 1 + # It can be also checked by ensuring that the penalty transaction made it to the network penalty_tx_id = bitcoin_cli.decoderawtransaction(penalty_tx).get("txid") @@ -129,6 +146,52 @@ def test_appointment_life_cycle(bitcoin_cli, create_txs): assert get_appointment_info(locator) is None +def test_multiple_appointments_life_cycle(bitcoin_cli, create_five_txs): + # Tests that get_all_appointments returns all the appointments the tower is storing at various stages in the appointment lifecycle. + appointments = [] + + commitment_txs, penalty_txs = create_five_txs + + # Create five appointments. + for i in range(5): + appointment = {} + + appointment["commitment_tx"] = commitment_txs[i] + appointment["penalty_tx"] = penalty_txs[i] + commitment_tx_id = bitcoin_cli.decoderawtransaction(commitment_txs[i]).get("txid") + appointment_data = build_appointment_data(bitcoin_cli, commitment_tx_id, penalty_txs[i]) + appointment["appointment_data"] = appointment_data + locator = compute_locator(commitment_tx_id) + appointment["locator"] = locator + + appointments.append(appointment) + + # Send all of them to watchtower. + for appt in appointments: + add_appointment(appt.get("appointment_data")) + + # Two of these appointments are breached, and the watchtower responds to them. + for i in range(2): + new_addr = bitcoin_cli.getnewaddress() + broadcast_transaction_and_mine_block(bitcoin_cli, appointments[i]["commitment_tx"], new_addr) + bitcoin_cli.generatetoaddress(3, new_addr) + sleep(1) + + # Test that they all show up in get_all_appointments at the correct stages. + all_appointments = get_all_appointments() + watching = all_appointments.get("watcher_appointments") + responding = all_appointments.get("responder_trackers") + assert len(watching) == 3 and len(responding) == 2 + + # Now let's mine some blocks so these appointments reach the end of their lifecycle. + # Since we are running all the nodes remotely data may take more time than normal, and some confirmations may be + # missed, so we generate more than enough confirmations and add some delays. + new_addr = bitcoin_cli.getnewaddress() + for _ in range(int(1.5 * END_TIME_DELTA) + 5): + sleep(1) + bitcoin_cli.generatetoaddress(1, new_addr) + + def test_appointment_malformed_penalty(bitcoin_cli, create_txs): # Lets start by creating two valid transaction commitment_tx, penalty_tx = create_txs