Esempio n. 1
0
def test_state_traded(mocker, swap_wait_for_taker_execution,
                      taker_swap_execution_msg_signed, maker_transfer_receipt,
                      taker_transfer_receipt):
    # transition into the state again, because the actions are coupled with succesful transitions

    swap = swap_wait_for_taker_execution

    # mock timestamp.time() to a fixed value
    mocker.patch.object(timestamp, "time", autospec=True, return_value=1)

    swap.hand_swap_execution_msg(taker_swap_execution_msg_signed)

    assert_terminated_state(swap, 'traded')

    # swap completed should be broadcasted
    expected_swap_completed_msg = messages.SwapCompleted(123, timestamp=1)

    swap._send_func.assert_called_once_with(expected_swap_completed_msg, None)

    # maker/taker should be refunded, commitment_service should claim fee

    assert swap._refund_func.call_count == 2
    expected_calls = [
        mocker.call(taker_transfer_receipt, 1, True),
        mocker.call(maker_transfer_receipt, 1, True)
    ]
    # call order unimportant
    swap._refund_func.assert_has_calls(expected_calls, any_order=True)
Esempio n. 2
0
def test_message_sender_task_broadcast(mocker, message_broker, message_queue, commitment_service_account):
    def sign_func(msg):
        msg.sign(commitment_service_account.privatekey)

    # use some arbitrary message and receiver:
    message = messages.SwapCompleted(123, 1)
    receiver = None

    message_broker_broadcast_mock = mocker.patch.object(MessageBroker, 'broadcast', autospect=True)

    message_sender_task = MessageSenderTask(message_broker, message_queue, sign_func=sign_func)
    message_sender_task.start()
    switch_context()

    data = (message, receiver)
    message_queue.put(data)

    assert len(message_queue) == 1
    switch_context()
    assert len(message_queue) == 0

    message_broker_broadcast_mock.assert_called_once_with(message)