Ejemplo n.º 1
0
def test_put_balance_sync_check(
    api_sut: ServiceApi,
    api_url: str,
    token_networks: List[TokenNetwork],
    token_network_addresses: List[Address],
    private_keys: List[str],
):
    url = api_url + '/{}/{}/balance'.format(token_network_addresses[0], ID_12)

    token_networks[0].update_balance = mock.Mock(
        return_value=None)  # type: ignore

    balance_proof = BalanceProof(
        channel_identifier=ChannelIdentifier(ID_123),
        token_network_address=token_network_addresses[0],
        nonce=1,
        chain_id=321,
        additional_hash="0x%064x" % 0,
        balance_hash="0x%064x" % 0,
    )
    balance_proof.signature = encode_hex(
        sign_data(private_keys[0], balance_proof.serialize_bin()))

    body = balance_proof.serialize_full()

    # path channel id and BP channel id are not equal
    response = requests.put(url, json=body)
    assert response.status_code == 400
    assert response.json()['error'].startswith(
        'The channel identifier from the balance proof '
        f'({ID_123}) and the request ({ID_12}) do not match')

    balance_proof = BalanceProof(
        channel_identifier=ChannelIdentifier(ID_123),
        token_network_address=token_network_addresses[1],
        nonce=1,
        chain_id=321,
        additional_hash="0x%064x" % 0,
        balance_hash="0x%064x" % 0,
    )
    balance_proof.signature = encode_hex(
        sign_data(private_keys[0], balance_proof.serialize_bin()))

    body = balance_proof.serialize_full()

    # now the network address doesn't match
    response = requests.put(url, json=body)
    assert response.status_code == 400
    assert response.json()['error'].startswith(
        'The token network address from the balance proof')
Ejemplo n.º 2
0
def test_put_balance(
    api_sut: ServiceApi,
    api_url: str,
    token_networks: List[TokenNetwork],
    token_network_addresses: List[Address],
    private_keys: List[str],
):
    url = api_url + '/{}/{}/balance'.format(token_network_addresses[0], ID_123)

    token_networks[0].update_balance = mock.Mock(
        return_value=None)  # type: ignore

    balance_proof = BalanceProof(
        channel_identifier=ChannelIdentifier(ID_123),
        token_network_address=token_network_addresses[0],
        nonce=1,
        chain_id=1,
        locksroot="0x%064x" % 0,
        transferred_amount=1,
        locked_amount=0,
        additional_hash="0x%064x" % 0,
    )
    balance_proof.signature = encode_hex(
        sign_data(private_keys[0], balance_proof.serialize_bin()))

    body = balance_proof.serialize_full()

    response = requests.put(url, json=body)
    assert response.status_code == 200

    token_networks[0].update_balance.assert_called_once()  # type: ignore
    call_args = token_networks[0].update_balance.call_args[0]  # type: ignore

    channel_identifier: ChannelIdentifier = call_args[0]
    signer: Address = call_args[1]
    nonce: int = call_args[2]
    transferred_amount: int = call_args[3]
    locked_amount: int = call_args[4]

    assert channel_identifier == ID_123
    assert is_same_address(signer, private_key_to_address(private_keys[0]))
    assert nonce == 1
    assert transferred_amount == 1
    assert locked_amount == 0