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

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

    fee_info = FeeInfo(token_network_address=token_network_addresses[0],
                       channel_identifier=ChannelIdentifier(ID_123),
                       chain_id=1,
                       nonce=1,
                       relative_fee=1000)
    fee_info.signature = encode_hex(
        sign_data(private_keys[0], fee_info.serialize_bin()))

    body = fee_info.serialize_data()
    body['message_type'] = 'FeeInfo'

    # remove the fee to make it an invalid message
    del body['relative_fee']

    response = requests.put(url, json=body)
    assert response.status_code == 400
    assert response.json()['error'] == "'relative_fee' is a required property"
Ejemplo n.º 2
0
def test_put_fee(
    api_sut: ServiceApi,
    api_url: str,
    token_networks: List[TokenNetwork],
    token_network_addresses: List[Address],
    private_keys: List[str],
):
    url = api_url + '/{}/{}/fee'.format(token_network_addresses[0], ID_123)

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

    fee_info = FeeInfo(token_network_address=token_network_addresses[0],
                       channel_identifier=ChannelIdentifier(ID_123),
                       chain_id=1,
                       nonce=1,
                       relative_fee=1000)
    fee_info.signature = encode_hex(
        sign_data(private_keys[0], fee_info.serialize_bin()))

    body = fee_info.serialize_full()

    response = requests.put(url, json=body)
    assert response.status_code == 200
    token_networks[0].update_fee.assert_called_once()  # type: ignore
    call_args = token_networks[0].update_fee.call_args[0]  # type: ignore

    channel_id: int = call_args[0]
    sender: str = call_args[1]
    nonce: int = call_args[2]
    fee: float = call_args[3]

    assert channel_id == ID_123
    assert is_same_address(sender, private_key_to_address(private_keys[0]))
    assert nonce == 1
    assert fee == 1000
Ejemplo n.º 3
0
 def get_fee_info(self, partner_address: Address, **kwargs) -> FeeInfo:
     """Get a signed fee info message for an open channel.
     Parameters:
         partner_address - address of a partner the node has channel open with
         **kwargs - arguments to FeeInfo constructor
     """
     channel_id = self.partner_to_channel_id[partner_address]
     fi = FeeInfo(self.contract.address, channel_id, **kwargs)
     fi.signature = encode_hex(sign_data(self.privkey, fi.serialize_bin()))
     return fi
Ejemplo n.º 4
0
def test_put_fee_sync_check(
    api_sut: ServiceApi,
    api_url: str,
    token_networks: List[TokenNetwork],
    token_network_addresses: List[Address],
    private_keys: List[str],
):
    url = api_url + '/{}/{}/fee'.format(token_network_addresses[0], ID_12)

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

    fee_info = FeeInfo(token_network_address=token_network_addresses[0],
                       channel_identifier=ChannelIdentifier(ID_123),
                       chain_id=1,
                       nonce=1,
                       relative_fee=1000)
    fee_info.signature = encode_hex(
        sign_data(private_keys[0], fee_info.serialize_bin()))

    body = fee_info.serialize_full()

    # path channel id and FeeInfo 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 fee info '
        f'({ID_123}) and the request ({ID_12}) do not match')

    fee_info = FeeInfo(token_network_address=token_network_addresses[1],
                       channel_identifier=ChannelIdentifier(ID_123),
                       chain_id=1,
                       nonce=1,
                       relative_fee=1000)
    fee_info.signature = encode_hex(
        sign_data(private_keys[0], fee_info.serialize_bin()))

    body = fee_info.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 fee info')