Пример #1
0
def test_update_iou():
    privkey = bytes([2] * 32)
    sender = Address(privatekey_to_address(privkey))
    receiver = Address(bytes([1] * 20))
    one_to_n_address = Address(bytes([2] * 20))

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

    # update and compare
    added_amount = 10
    new_iou = update_iou(iou=replace(iou),
                         privkey=privkey,
                         added_amount=added_amount)
    assert new_iou.amount == iou.amount + added_amount
    assert new_iou.sender == iou.sender
    assert new_iou.receiver == iou.receiver
    assert new_iou.signature != iou.signature

    # Previous IOU with increased amount by evil PFS
    tampered_iou = replace(new_iou)
    tampered_iou.amount += 10
    with pytest.raises(ServiceRequestFailed):
        update_iou(iou=tampered_iou,
                   privkey=privkey,
                   added_amount=added_amount)
Пример #2
0
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
Пример #3
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
Пример #4
0
def test_update_iou():
    privkey = bytes([2] * 32)
    sender = Address(privatekey_to_address(privkey))
    receiver = Address(bytes([1] * 20))

    # prepare iou
    iou = {
        "sender": encode_hex(sender),
        "receiver": encode_hex(receiver),
        "amount": 10,
        "expiration_block": 1000,
    }
    iou["signature"] = encode_hex(
        sign_one_to_n_iou(
            privatekey=encode_hex(privkey),
            sender=iou["sender"],
            receiver=iou["receiver"],
            amount=iou["amount"],
            expiration=iou["expiration_block"],
        ))

    # update and compare
    added_amount = 10
    new_iou = update_iou(iou=iou.copy(),
                         privkey=privkey,
                         added_amount=added_amount)
    assert new_iou["amount"] == iou["amount"] + added_amount
    assert new_iou["sender"] == iou["sender"]
    assert new_iou["receiver"] == iou["receiver"]
    assert new_iou["signature"] != iou["signature"]

    # Previous IOU with increased amount by evil PFS
    tampered_iou = new_iou.copy()
    tampered_iou["amount"] += 10
    with pytest.raises(ServiceRequestFailed):
        update_iou(iou=tampered_iou,
                   privkey=privkey,
                   added_amount=added_amount)