Ejemplo n.º 1
0
def test_two_appointment_same_locator_different_penalty(bitcoin_cli, create_txs):
    # This tests sending an appointment with two valid transaction with the same locator.
    commitment_tx, penalty_tx1 = create_txs
    commitment_tx_id = bitcoin_cli.decoderawtransaction(commitment_tx).get("txid")

    # We need to create a second penalty spending from the same commitment
    decoded_commitment_tx = bitcoin_cli.decoderawtransaction(commitment_tx)
    new_addr = bitcoin_cli.getnewaddress()
    penalty_tx2 = create_penalty_tx(bitcoin_cli, decoded_commitment_tx, new_addr)

    appointment1_data = build_appointment_data(bitcoin_cli, commitment_tx_id, penalty_tx1)
    appointment2_data = build_appointment_data(bitcoin_cli, commitment_tx_id, penalty_tx2)
    locator = compute_locator(commitment_tx_id)

    assert teos_cli.add_appointment([json.dumps(appointment1_data)], teos_add_appointment_endpoint, cli_config) is True
    assert teos_cli.add_appointment([json.dumps(appointment2_data)], teos_add_appointment_endpoint, cli_config) is True

    # Broadcast the commitment transaction and mine a block
    new_addr = bitcoin_cli.getnewaddress()
    broadcast_transaction_and_mine_block(bitcoin_cli, commitment_tx, new_addr)

    # The first appointment should have made it to the Responder, and the second one should have been dropped for
    # double-spending
    sleep(1)
    appointment_info = get_appointment_info(locator)

    assert appointment_info is not None
    assert len(appointment_info) == 1
    assert appointment_info[0].get("status") == "dispute_responded"
    assert appointment_info[0].get("penalty_rawtx") == penalty_tx1
Ejemplo n.º 2
0
def test_two_identical_appointments(bitcoin_cli, create_txs):
    # Tests sending two identical appointments to the tower.
    # At the moment there are no checks for identical appointments, so both will be accepted, decrypted and kept until
    # the end.
    # TODO: 34-exact-duplicate-appointment
    # This tests sending an appointment with two valid transaction with the same locator.
    commitment_tx, penalty_tx = create_txs
    commitment_tx_id = bitcoin_cli.decoderawtransaction(commitment_tx).get("txid")

    appointment_data = build_appointment_data(bitcoin_cli, commitment_tx_id, penalty_tx)
    locator = compute_locator(commitment_tx_id)

    # Send the appointment twice
    assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_add_appointment_endpoint, cli_config) is True
    assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_add_appointment_endpoint, cli_config) is True

    # Broadcast the commitment transaction and mine a block
    new_addr = bitcoin_cli.getnewaddress()
    broadcast_transaction_and_mine_block(bitcoin_cli, commitment_tx, new_addr)

    # The first appointment should have made it to the Responder, and the second one should have been dropped for
    # double-spending
    sleep(1)
    appointment_info = get_appointment_info(locator)

    assert appointment_info is not None
    assert len(appointment_info) == 2

    for info in appointment_info:
        assert info.get("status") == "dispute_responded"
        assert info.get("penalty_rawtx") == penalty_tx
Ejemplo n.º 3
0
def test_appointment_malformed_penalty(bitcoin_cli, create_txs):
    # Lets start by creating two valid transaction
    commitment_tx, penalty_tx = create_txs

    # Now we can modify the penalty so it is invalid when broadcast
    mod_penalty_tx = Tx.from_hex(penalty_tx)
    tx_in = mod_penalty_tx.tx_ins[0].copy(redeem_script=b"")
    mod_penalty_tx = mod_penalty_tx.copy(tx_ins=[tx_in])

    commitment_tx_id = bitcoin_cli.decoderawtransaction(commitment_tx).get("txid")
    appointment_data = build_appointment_data(bitcoin_cli, commitment_tx_id, mod_penalty_tx.hex())
    locator = compute_locator(commitment_tx_id)

    assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_add_appointment_endpoint, cli_config) is True

    # Broadcast the commitment transaction and mine a block
    new_addr = bitcoin_cli.getnewaddress()
    broadcast_transaction_and_mine_block(bitcoin_cli, commitment_tx, new_addr)

    # The appointment should have been removed since the penalty_tx was malformed.
    sleep(1)
    appointment_info = get_appointment_info(locator)

    assert appointment_info is not None
    assert len(appointment_info) == 1
    assert appointment_info[0].get("status") == "not_found"
Ejemplo n.º 4
0
def test_add_appointment_with_invalid_signature(monkeypatch):
    # Simulate a request to add_appointment for dummy_appointment, but sign with a different key,
    # make sure that the right endpoint is requested, but the return value is False

    response = {
        "locator": dummy_appointment.to_dict()["locator"],
        "signature": get_signature(dummy_appointment.serialize(),
                                   another_sk),  # Sign with a bad key
        "available_slots": 100,
    }

    responses.add(responses.POST,
                  add_appointment_endpoint,
                  json=response,
                  status=200)

    with pytest.raises(TowerResponseError):
        teos_cli.add_appointment(dummy_appointment_data, dummy_user_sk,
                                 dummy_teos_id, teos_url)
Ejemplo n.º 5
0
def test_add_appointment(monkeypatch):
    # Simulate a request to add_appointment for dummy_appointment, make sure that the right endpoint is requested
    # and the return value is True
    monkeypatch.setattr(teos_cli, "load_keys", load_dummy_keys)

    response = {"locator": dummy_appointment.locator, "signature": get_dummy_signature()}
    responses.add(responses.POST, teos_endpoint, json=response, status=200)
    result = teos_cli.add_appointment([json.dumps(dummy_appointment_request)], teos_endpoint, config)

    assert len(responses.calls) == 1
    assert responses.calls[0].request.url == teos_endpoint
    assert result
Ejemplo n.º 6
0
def test_add_appointment_with_invalid_signature(monkeypatch):
    # Simulate a request to add_appointment for dummy_appointment, but sign with a different key,
    # make sure that the right endpoint is requested, but the return value is False

    # Make sure the test uses the bad dummy signature
    monkeypatch.setattr(teos_cli, "load_keys", load_dummy_keys)

    response = {
        "locator": dummy_appointment.to_dict()["locator"],
        "signature": get_bad_signature(),  # Sign with a bad key
    }

    responses.add(responses.POST, teos_endpoint, json=response, status=200)
    result = teos_cli.add_appointment([json.dumps(dummy_appointment_request)], teos_endpoint, config)

    shutil.rmtree(config.get("APPOINTMENTS_FOLDER_NAME"))

    assert result is False
Ejemplo n.º 7
0
def test_add_appointment():
    # Simulate a request to add_appointment for dummy_appointment, make sure that the right endpoint is requested
    # and the return value is True
    response = {
        "locator": dummy_appointment.locator,
        "signature": get_signature(dummy_appointment.serialize(),
                                   dummy_teos_sk),
        "available_slots": 100,
    }
    responses.add(responses.POST,
                  add_appointment_endpoint,
                  json=response,
                  status=200)
    result = teos_cli.add_appointment(dummy_appointment_data, dummy_user_sk,
                                      dummy_teos_id, teos_url)

    assert len(responses.calls) == 1
    assert responses.calls[0].request.url == add_appointment_endpoint
    assert result
Ejemplo n.º 8
0
def test_appointment_life_cycle(bitcoin_cli, create_txs):
    commitment_tx, penalty_tx = create_txs
    commitment_tx_id = bitcoin_cli.decoderawtransaction(commitment_tx).get("txid")
    appointment_data = build_appointment_data(bitcoin_cli, commitment_tx_id, penalty_tx)
    locator = compute_locator(commitment_tx_id)

    assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_add_appointment_endpoint, cli_config) is True

    appointment_info = get_appointment_info(locator)
    assert appointment_info is not None
    assert len(appointment_info) == 1
    assert appointment_info[0].get("status") == "being_watched"

    new_addr = bitcoin_cli.getnewaddress()
    broadcast_transaction_and_mine_block(bitcoin_cli, commitment_tx, new_addr)

    appointment_info = get_appointment_info(locator)
    assert appointment_info is not None
    assert len(appointment_info) == 1
    assert appointment_info[0].get("status") == "dispute_responded"

    # 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")

    try:
        bitcoin_cli.getrawtransaction(penalty_tx_id)
        assert True

    except JSONRPCException:
        # If the transaction if not found.
        assert False

    # Now let's mine some blocks so the appointment reaches its end.
    # 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.
    for _ in range(int(1.5 * END_TIME_DELTA)):
        sleep(1)
        bitcoin_cli.generatetoaddress(1, new_addr)

    appointment_info = get_appointment_info(locator)
    assert appointment_info[0].get("status") == "not_found"
Ejemplo n.º 9
0
def test_appointment_shutdown_teos_trigger_while_offline(create_txs, bitcoin_cli):
    global teosd_process

    teos_pid = teosd_process.pid

    commitment_tx, penalty_tx = create_txs
    commitment_tx_id = bitcoin_cli.decoderawtransaction(commitment_tx).get("txid")
    appointment_data = build_appointment_data(bitcoin_cli, commitment_tx_id, penalty_tx)
    locator = compute_locator(commitment_tx_id)

    assert teos_cli.add_appointment([json.dumps(appointment_data)], teos_add_appointment_endpoint, cli_config) is True

    # Check that the appointment is still in the Watcher
    appointment_info = get_appointment_info(locator)
    assert appointment_info is not None
    assert len(appointment_info) == 1
    assert appointment_info[0].get("status") == "being_watched"

    # Shutdown and trigger
    teosd_process.terminate()
    new_addr = bitcoin_cli.getnewaddress()
    broadcast_transaction_and_mine_block(bitcoin_cli, commitment_tx, new_addr)

    # Restart
    teosd_process = run_teosd()
    assert teos_pid != teosd_process.pid

    # The appointment should have been moved to the Responder
    sleep(1)
    appointment_info = get_appointment_info(locator)

    assert appointment_info is not None
    assert len(appointment_info) == 1
    assert appointment_info[0].get("status") == "dispute_responded"

    teosd_process.terminate()
Ejemplo n.º 10
0
def add_appointment(appointment_data, sk=user_sk):
    return teos_cli.add_appointment(appointment_data, sk, teos_id, teos_base_endpoint)