Example #1
0
def test_get_pfs_iou(one_to_n_address):
    token_network_address = TokenNetworkAddress(bytes([1] * 20))
    privkey = bytes([2] * 32)
    sender = privatekey_to_address(privkey)
    receiver = factories.make_address()

    response = mocked_json_response(response_data={"last_iou": None})
    with patch.object(session, "get", return_value=response):
        assert (get_last_iou("http://example.com", token_network_address,
                             sender, receiver, PRIVKEY) is None)

        # Previous IOU
        iou = IOU(
            sender=sender,
            receiver=receiver,
            amount=10,
            expiration_block=1000,
            one_to_n_address=one_to_n_address,
            chain_id=4,
        )
        iou.sign(privkey)

    response = mocked_json_response(response_data={"last_iou": iou.as_json()})
    with patch.object(session, "get", return_value=response):
        assert (get_last_iou("http://example.com", token_network_address,
                             sender, receiver, PRIVKEY) == iou)
def test_get_pfs_iou():
    token_network_address = TokenNetworkAddress(bytes([1] * 20))
    privkey = bytes([2] * 32)
    sender = to_checksum_address(privatekey_to_address(privkey))
    receiver = factories.make_checksum_address()
    with patch('raiden.network.pathfinding.requests.get') as get_mock:
        # No previous IOU
        get_mock.return_value.json.return_value = {'last_iou': None}
        assert get_last_iou('http://example.com', token_network_address,
                            sender, receiver) is None

        # Previous IOU
        iou = dict(sender=sender,
                   receiver=receiver,
                   amount=10,
                   expiration_block=1000)
        iou['signature'] = sign_one_to_n_iou(
            privatekey=encode_hex(privkey),
            sender=sender,
            receiver=receiver,
            amount=iou['amount'],
            expiration=iou['expiration_block'],
        )
        get_mock.return_value.json.return_value = {'last_iou': iou}
        assert get_last_iou('http://example.com', token_network_address,
                            sender, receiver) == iou
Example #3
0
def test_get_pfs_iou():
    token_network_address = TokenNetworkAddress(bytes([1] * 20))
    privkey = bytes([2] * 32)
    sender = privatekey_to_address(privkey)
    receiver = factories.make_address()
    with patch("raiden.network.pathfinding.requests.get") as get_mock:
        # No previous IOU
        get_mock.return_value.json.return_value = {"last_iou": None}
        assert (get_last_iou("http://example.com", token_network_address,
                             sender, receiver, PRIVKEY) is None)

        # Previous IOU
        iou = dict(sender=sender,
                   receiver=receiver,
                   amount=10,
                   expiration_block=1000)
        iou["signature"] = sign_one_to_n_iou(
            privatekey=encode_hex(privkey),
            sender=to_checksum_address(sender),
            receiver=to_checksum_address(receiver),
            amount=iou["amount"],
            expiration=iou["expiration_block"],
        )
        get_mock.return_value.json.return_value = {"last_iou": iou}
        assert (get_last_iou("http://example.com", token_network_address,
                             sender, receiver, PRIVKEY) == iou)
def test_get_and_update_iou():

    request_args = dict(
        url='url',
        token_network_address=factories.UNIT_TOKEN_NETWORK_ADDRESS,
        sender=factories.make_checksum_address(),
        receiver=factories.make_checksum_address(),
    )
    # RequestExceptions should be reraised as ServiceRequestFailed
    with pytest.raises(ServiceRequestFailed):
        with patch.object(requests,
                          'get',
                          side_effect=requests.RequestException):
            get_last_iou(**request_args)

    # invalid JSON should raise a ServiceRequestFailed
    response = Mock()
    response.configure_mock(status_code=200)
    response.json = Mock(side_effect=ValueError)
    with pytest.raises(ServiceRequestFailed):
        with patch.object(requests, 'get', return_value=response):
            get_last_iou(**request_args)

    response = Mock()
    response.configure_mock(status_code=200)
    response.json = Mock(return_value={'other_key': 'other_value'})
    with patch.object(requests, 'get', return_value=response):
        iou = get_last_iou(**request_args)
    assert iou is None, 'get_pfs_iou should return None if pfs returns no iou.'

    response = Mock()
    response.configure_mock(status_code=200)
    last_iou = make_iou(
        config=dict(
            pathfinding_eth_address=to_checksum_address(
                factories.UNIT_TRANSFER_TARGET),
            pathfinding_iou_timeout=500,
            pathfinding_max_fee=100,
        ),
        our_address=factories.UNIT_TRANSFER_INITIATOR,
        privkey=PRIVKEY,
        block_number=10,
    )
    response.json = Mock(return_value=dict(last_iou=last_iou))
    with patch.object(requests, 'get', return_value=response):
        iou = get_last_iou(**request_args)
    assert iou == last_iou

    new_iou_1 = update_iou(iou.copy(), PRIVKEY, added_amount=10)
    assert new_iou_1['amount'] == last_iou['amount'] + 10
    assert all(new_iou_1[k] == iou[k]
               for k in ('expiration_block', 'sender', 'receiver'))
    assert 'signature' in new_iou_1

    new_iou_2 = update_iou(iou, PRIVKEY, expiration_block=45)
    assert new_iou_2['expiration_block'] == 45
    assert all(new_iou_2[k] == iou[k]
               for k in ('amount', 'sender', 'receiver'))
    assert 'signature' in new_iou_2
Example #5
0
def test_get_and_update_iou(one_to_n_address):

    request_args = dict(
        url="url",
        token_network_address=factories.UNIT_TOKEN_NETWORK_ADDRESS,
        sender=factories.make_address(),
        receiver=factories.make_address(),
        privkey=PRIVKEY,
    )
    # RequestExceptions should be reraised as ServiceRequestFailed
    with pytest.raises(ServiceRequestFailed):
        with patch.object(session,
                          "get",
                          side_effect=requests.RequestException):
            get_last_iou(**request_args)

    # invalid JSON should raise a ServiceRequestFailed
    response = mocked_failed_response(error=ValueError)

    with pytest.raises(ServiceRequestFailed):
        with patch.object(session, "get", return_value=response):
            get_last_iou(**request_args)

    response = mocked_json_response(response_data={"other_key": "other_value"})
    with patch.object(session, "get", return_value=response):
        iou = get_last_iou(**request_args)
    assert iou is None, "get_pfs_iou should return None if pfs returns no iou."

    last_iou = make_iou(
        pfs_config=PFS_CONFIG,
        our_address=factories.UNIT_TRANSFER_INITIATOR,
        privkey=PRIVKEY,
        block_number=10,
        one_to_n_address=one_to_n_address,
        chain_id=4,
        offered_fee=TokenAmount(1),
    )

    response = mocked_json_response(response_data=dict(
        last_iou=last_iou.as_json()))

    with patch.object(session, "get", return_value=response):
        iou = get_last_iou(**request_args)
    assert iou == last_iou

    new_iou_1 = update_iou(replace(iou), PRIVKEY, added_amount=10)
    assert new_iou_1.amount == last_iou.amount + 10
    assert new_iou_1.sender == last_iou.sender
    assert new_iou_1.receiver == last_iou.receiver
    assert new_iou_1.expiration_block == last_iou.expiration_block
    assert new_iou_1.signature is not None

    new_iou_2 = update_iou(replace(iou), PRIVKEY, expiration_block=45)
    assert new_iou_2.expiration_block == 45
    assert new_iou_1.sender == iou.sender
    assert new_iou_1.receiver == iou.receiver
    assert new_iou_1.expiration_block == iou.expiration_block
    assert new_iou_2.signature is not None