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
示例#2
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
def test_routing_mocked_pfs_happy_path_with_updated_iou(
    happy_path_fixture,
    our_address,
):
    addresses, chain_state, channel_states, response, token_network_state = happy_path_fixture
    address1, address2, _ = addresses
    channel_state1, channel_state2 = channel_states

    iou = make_iou(
        config=dict(
            pathfinding_eth_address=to_checksum_address(
                factories.UNIT_TRANSFER_TARGET),
            pathfinding_iou_timeout=100,
            pathfinding_max_fee=13,
        ),
        our_address=factories.UNIT_TRANSFER_SENDER,
        privkey=PRIVKEY,
        block_number=10,
    )
    last_iou = copy(iou)

    with patch.object(requests, 'post', return_value=response) as patched:
        routes = get_best_routes_with_iou_request_mocked(
            chain_state=chain_state,
            token_network_state=token_network_state,
            from_address=our_address,
            to_address=address1,
            amount=50,
            iou_json_data=dict(last_iou=iou),
        )

    assert_checksum_address_in_url(patched.call_args[0][0])

    assert routes[0].node_address == address2
    assert routes[0].channel_identifier == channel_state2.identifier
    assert routes[1].node_address == address1
    assert routes[1].channel_identifier == channel_state1.identifier

    # Check for iou arguments in request payload
    payload = patched.call_args[1]['json']
    config = CONFIG['services']
    old_amount = last_iou['amount']
    assert old_amount < payload['iou'][
        'amount'] <= config['pathfinding_max_fee'] + old_amount
    assert all(payload['iou'][k] == last_iou[k]
               for k in ('expiration_block', 'sender', 'receiver'))
    assert 'signature' in payload['iou']
示例#4
0
def test_make_iou():
    privkey = bytes([2] * 32)
    sender = Address(privatekey_to_address(privkey))
    receiver = Address(bytes([1] * 20))
    config = {
        "pathfinding_eth_address": encode_hex(receiver),
        "pathfinding_iou_timeout": 10000,
        "pathfinding_max_fee": 100,
    }

    iou = make_iou(config,
                   our_address=sender,
                   privkey=privkey,
                   block_number=10)

    assert iou["sender"] == to_checksum_address(sender)
    assert iou["receiver"] == encode_hex(receiver)
    assert 0 < iou["amount"] <= config["pathfinding_max_fee"]
def test_make_iou():
    privkey = bytes([2] * 32)
    sender = Address(privatekey_to_address(privkey))
    receiver = Address(bytes([1] * 20))
    config = {
        'pathfinding_eth_address': encode_hex(receiver),
        'pathfinding_iou_timeout': 10000,
        'pathfinding_max_fee': 100,
    }

    iou = make_iou(config,
                   our_address=sender,
                   privkey=privkey,
                   block_number=10)

    assert iou['sender'] == to_checksum_address(sender)
    assert iou['receiver'] == encode_hex(receiver)
    assert 0 < iou['amount'] <= config['pathfinding_max_fee']
def test_routing_mocked_pfs_happy_path_with_updated_iou(
        happy_path_fixture, one_to_n_address, our_address):
    addresses, chain_state, channel_states, response, token_network_state = happy_path_fixture
    _, address2, _, address4 = addresses
    _, channel_state2 = channel_states

    iou = make_iou(
        pfs_config=PFS_CONFIG,
        our_address=factories.UNIT_TRANSFER_SENDER,
        one_to_n_address=one_to_n_address,
        privkey=PRIVKEY,
        block_number=BlockNumber(10),
        chain_id=ChainID(5),
        offered_fee=TokenAmount(1),
    )
    last_iou = copy(iou)

    with patch.object(requests, "post", return_value=response) as patched:
        routes, feedback_token = get_best_routes_with_iou_request_mocked(
            chain_state=chain_state,
            token_network_state=token_network_state,
            one_to_n_address=one_to_n_address,
            from_address=our_address,
            to_address=address4,
            amount=50,
            iou_json_data=dict(last_iou=last_iou.as_json()),
        )

    assert_checksum_address_in_url(patched.call_args[0][0])

    assert routes[0].next_hop_address == address2
    assert routes[0].forward_channel_id == channel_state2.identifier
    assert feedback_token == DEFAULT_FEEDBACK_TOKEN

    # Check for iou arguments in request payload
    payload = patched.call_args[1]["json"]
    pfs_config = CONFIG["pfs_config"]
    old_amount = last_iou.amount
    assert old_amount < payload["iou"][
        "amount"] <= pfs_config.maximum_fee + old_amount
    assert payload["iou"]["expiration_block"] == last_iou.expiration_block
    assert payload["iou"]["sender"] == to_checksum_address(last_iou.sender)
    assert payload["iou"]["receiver"] == to_checksum_address(last_iou.receiver)
    assert "signature" in payload["iou"]
示例#7
0
def test_routing_mocked_pfs_happy_path_with_updated_iou(
        happy_path_fixture, our_address):
    addresses, chain_state, channel_states, response, token_network_state = happy_path_fixture
    _, address2, _, address4 = addresses
    _, channel_state2 = channel_states

    iou = make_iou(
        config=dict(
            pathfinding_eth_address=to_checksum_address(
                factories.UNIT_TRANSFER_TARGET),
            pathfinding_iou_timeout=100,
            pathfinding_max_fee=13,
        ),
        our_address=factories.UNIT_TRANSFER_SENDER,
        privkey=PRIVKEY,
        block_number=10,
    )
    last_iou = copy(iou)

    with patch.object(requests, "post", return_value=response) as patched:
        routes = get_best_routes_with_iou_request_mocked(
            chain_state=chain_state,
            token_network_state=token_network_state,
            from_address=our_address,
            to_address=address4,
            amount=50,
            iou_json_data=dict(last_iou=iou),
        )

    assert_checksum_address_in_url(patched.call_args[0][0])

    assert routes[0].node_address == address2
    assert routes[0].channel_identifier == channel_state2.identifier

    # Check for iou arguments in request payload
    payload = patched.call_args[1]["json"]
    config = CONFIG["services"]
    old_amount = last_iou["amount"]
    assert old_amount < payload["iou"][
        "amount"] <= config["pathfinding_max_fee"] + old_amount
    for key in ("expiration_block", "sender", "receiver"):
        assert payload["iou"][key] == last_iou[key]
    assert "signature" in payload["iou"]
def test_make_iou():
    privkey = bytes([2] * 32)
    sender = Address(privatekey_to_address(privkey))
    receiver = Address(bytes([1] * 20))
    one_to_n_address = Address(bytes([2] * 20))
    chain_id = ChainID(4)
    max_fee = 100

    pfs_config_copy = replace(PFS_CONFIG)
    pfs_config_copy.info = replace(pfs_config_copy.info, payment_address=receiver)
    iou = make_iou(
        pfs_config=pfs_config_copy,
        our_address=sender,
        privkey=privkey,
        block_number=10,
        one_to_n_address=one_to_n_address,
        chain_id=chain_id,
        offered_fee=TokenAmount(1),
    )

    assert iou.sender == sender
    assert iou.receiver == receiver
    assert 0 < iou.amount <= max_fee