Пример #1
0
def test_api_open_channel_invalid_input(
        api_backend,
        token_addresses,
        reveal_timeout,
):
    partner_address = '0x61C808D82A3Ac53231750daDc13c777b59310bD9'
    token_address = token_addresses[0]
    settle_timeout = TEST_SETTLE_TIMEOUT_MIN - 1
    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.CONFLICT)

    channel_data_obj['settle_timeout'] = TEST_SETTLE_TIMEOUT_MAX + 1
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.CONFLICT)
def test_api_open_channel_invalid_input(
        api_backend,
        api_test_context,
        api_raiden_service):

    # let's create a new channel
    partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9'
    token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8'
    settle_timeout = NETTINGCHANNEL_SETTLE_TIMEOUT_MIN - 1
    reveal_timeout = DEFAULT_REVEAL_TIMEOUT
    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
    }
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj
    )
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.CONFLICT)

    channel_data_obj['settle_timeout'] = NETTINGCHANNEL_SETTLE_TIMEOUT_MAX + 1
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj
    )
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.CONFLICT)
Пример #3
0
def test_api_deposit_limit(
        api_backend,
        token_addresses,
        reveal_timeout,
):
    # let's create a new channel and deposit exactly the limit amount
    first_partner_address = '0x61C808D82A3Ac53231750daDc13c777b59310bD9'
    token_address = token_addresses[0]
    settle_timeout = 1650
    balance_working = MAX_TOKENS_DEPLOY * (10 ** 2)  # token has two digits
    channel_data_obj = {
        'partner_address': first_partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'balance': balance_working,
    }

    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response

    assert_proper_response(response, HTTPStatus.CREATED)
    response = response.json()
    expected_response = channel_data_obj
    expected_response['balance'] = balance_working
    expected_response['state'] = CHANNEL_STATE_OPENED
    expected_response['channel_identifier'] = assert_dicts_are_equal.IGNORE_VALUE
    expected_response['token_network_identifier'] = assert_dicts_are_equal.IGNORE_VALUE
    assert_dicts_are_equal(response, expected_response)

    # now let's open a channel and deposit a bit more than the limit
    second_partner_address = '0x29FA6cf0Cce24582a9B20DB94Be4B6E017896038'
    balance_failing = balance_working + 1  # token has two digits
    channel_data_obj = {
        'partner_address': second_partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'balance': balance_failing,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response

    assert_proper_response(response, HTTPStatus.EXPECTATION_FAILED)
    response = response.json()
    assert response['errors'] == 'The deposit of 10001 is bigger than the current limit of 10000'
def test_get_connection_managers_info(
        api_backend,
        api_test_context,
        api_raiden_service):

    # check that no connection managers exists yet
    request = grequests.get(
        api_url_for(api_backend, 'connectionmanagersresource')
    )
    response = request.send().response
    token_addresses = response.json()
    assert token_addresses == dict()

    funds = 100
    token_address1 = '0xea674fdde714fd979de3edf0f56aa9716b898ec8'
    connect_data_obj = {
        'funds': funds,
    }
    request = grequests.put(
        api_url_for(api_backend, 'connectionsresource', token_address=token_address1),
        json=connect_data_obj,
    )
    response = request.send().response
    assert_no_content_response(response)

    # check that there now is one registered channel manager
    request = grequests.get(
        api_url_for(api_backend, 'connectionmanagersresource')
    )
    response = request.send().response
    token_addresses = response.json()
    assert isinstance(token_addresses, dict) and len(token_addresses.keys()) == 1
    assert token_address1 in token_addresses
    assert isinstance(token_addresses[token_address1], dict)
    assert set(token_addresses[token_address1].keys()) == {'funds', 'sum_deposits', 'channels'}

    funds = 100
    token_address2 = '0x3edf0f56aa9716b898ec8ea674fdde714fd979de'
    connect_data_obj = {
        'funds': funds,
    }
    request = grequests.put(
        api_url_for(api_backend, 'connectionsresource', token_address=token_address2),
        json=connect_data_obj,
    )
    response = request.send().response
    assert_no_content_response(response)

    # check that there now are two registered channel managers
    request = grequests.get(
        api_url_for(api_backend, 'connectionmanagersresource')
    )
    response = request.send().response
    token_addresses = response.json()
    assert isinstance(token_addresses, dict) and len(token_addresses.keys()) == 2
    assert token_address2 in token_addresses
    assert isinstance(token_addresses[token_address2], dict)
    assert set(token_addresses[token_address2].keys()) == {'funds', 'sum_deposits', 'channels'}
def test_api_token_swaps(
        api_backend,
        api_test_context,
        api_raiden_service):

    tokenswap_obj = {
        'role': 'maker',
        'sending_amount': 42,
        'sending_token': '0xea674fdde714fd979de3edf0f56aa9716b898ec8',
        'receiving_amount': 76,
        'receiving_token': '0x2a65aca4d5fc5b5c859090a6c34d164135398226'
    }
    target_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9'
    identifier = 1337
    api_test_context.specify_tokenswap_input(
        tokenswap_obj,
        target_address,
        identifier
    )
    request = grequests.put(
        api_url_for(
            api_backend,
            'tokenswapsresource',
            target_address=target_address,
            identifier=identifier
        ),
        json=tokenswap_obj
    )
    response = request.send().response
    assert_proper_response(response, status_code=HTTPStatus.CREATED)

    tokenswap_obj = {
        'role': 'taker',
        'sending_amount': 76,
        'sending_token': '0x2a65aca4d5fc5b5c859090a6c34d164135398226',
        'receiving_amount': 42,
        'receiving_token': '0xea674fdde714fd979de3edf0f56aa9716b898ec8'
    }
    target_address = '0xbbc5ee8be95683983df67260b0ab033c237bde60'
    api_test_context.specify_tokenswap_input(
        tokenswap_obj,
        target_address,
        identifier
    )
    request = grequests.put(
        api_url_for(
            api_backend,
            'tokenswapsresource',
            target_address=target_address,
            identifier=identifier
        ),
        json=tokenswap_obj
    )
    response = request.send().response
    assert_proper_response(response, status_code=HTTPStatus.CREATED)
Пример #6
0
def test_register_token(api_backend, token_amount, token_addresses, raiden_network):
    app0 = raiden_network[0]
    new_token_address = deploy_contract_web3(
        CONTRACT_HUMAN_STANDARD_TOKEN,
        app0.raiden.chain.client,
        num_confirmations=None,
        constructor_arguments=(
            token_amount,
            2,
            'raiden',
            'Rd',
        ),
    )
    other_token_address = deploy_contract_web3(
        CONTRACT_HUMAN_STANDARD_TOKEN,
        app0.raiden.chain.client,
        num_confirmations=None,
        constructor_arguments=(
            token_amount,
            2,
            'raiden',
            'Rd',
        ),
    )

    register_request = grequests.put(api_url_for(
        api_backend,
        'registertokenresource',
        token_address=to_checksum_address(new_token_address),
    ))
    register_response = register_request.send().response
    assert_proper_response(register_response, status_code=HTTPStatus.CREATED)
    response_json = register_response.json()
    assert 'token_network_address' in response_json
    assert is_checksum_address(response_json['token_network_address'])

    # now try to reregister it and get the error
    conflict_request = grequests.put(api_url_for(
        api_backend,
        'registertokenresource',
        token_address=to_checksum_address(new_token_address),
    ))
    conflict_response = conflict_request.send().response
    assert_response_with_error(conflict_response, HTTPStatus.CONFLICT)

    # Burn all the eth and then make sure we get the appropriate API error
    burn_all_eth(app0.raiden)
    poor_request = grequests.put(api_url_for(
        api_backend,
        'registertokenresource',
        token_address=to_checksum_address(other_token_address),
    ))
    poor_response = poor_request.send().response
    assert_response_with_error(poor_response, HTTPStatus.PAYMENT_REQUIRED)
Пример #7
0
def test_api_tokens(api_backend, blockchain_services, token_addresses):
    partner_address = '0x61C808D82A3Ac53231750daDc13c777b59310bD9'
    token_address1 = token_addresses[0]
    token_address2 = token_addresses[1]
    settle_timeout = 1650

    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': to_checksum_address(token_address1),
        'settle_timeout': settle_timeout,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CREATED)

    partner_address = '0x61C808D82A3Ac53231750daDc13c777b59310bD9'
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': to_checksum_address(token_address2),
        'settle_timeout': settle_timeout,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CREATED)

    # and now let's get the token list
    request = grequests.get(
        api_url_for(
            api_backend,
            'tokensresource',
        ),
    )
    response = request.send().response
    assert_proper_response(response)
    response = response.json()
    expected_response = [
        to_checksum_address(token_address1),
        to_checksum_address(token_address2),
    ]
    assert set(response) == set(expected_response)
Пример #8
0
def test_channel_events(api_backend, token_addresses):
    # let's create a new channel
    partner_address = '0x61C808D82A3Ac53231750daDc13c777b59310bD9'
    token_address = token_addresses[0]
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response

    assert_proper_response(response, status_code=HTTPStatus.CREATED)

    request = grequests.get(
        api_url_for(
            api_backend,
            'channeleventsresource',
            partner_address=partner_address,
            token_address=token_address,
            from_block=0,
        ),
    )
    response = request.send().response
    assert_proper_response(response, status_code=HTTPStatus.OK)
    assert len(response.json()) > 0
Пример #9
0
    def create_request(path):
        tags = get_tags(ctx, path)
        data = {"tags": ",".join(tags)}

        finger = dl_lib.hash_id(path)
        url = "%s/agents/%s/tags" % (api_host, finger)
        return grequests.put(url, json=data, headers=headers)
def test_api_tokens(
        api_backend,
        api_test_context,
        api_raiden_service):
    # let's create 2 new channels for 2 different tokens
    partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9'
    token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8'
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout
    }
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CREATED)

    partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9'
    token_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9'
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout
    }
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CREATED)

    # and now let's get the token list
    request = grequests.get(
        api_url_for(api_backend, 'tokensresource'),
    )
    response = request.send().response
    assert_proper_response(response)
    response = response.json()
    expected_response = [
        '0x61c808d82a3ac53231750dadc13c777b59310bd9',
        '0xea674fdde714fd979de3edf0f56aa9716b898ec8',
    ]
    assert set(response) == set(expected_response)
Пример #11
0
def test_api_open_close_and_settle_channel(
        api_backend,
        token_addresses,
        reveal_timeout,
):
    # let's create a new channel
    partner_address = '0x61C808D82A3Ac53231750daDc13c777b59310bD9'
    token_address = token_addresses[0]
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response

    balance = 0
    assert_proper_response(response, status_code=HTTPStatus.CREATED)
    response = response.json()
    expected_response = channel_data_obj
    expected_response['balance'] = balance
    expected_response['state'] = CHANNEL_STATE_OPENED
    expected_response['reveal_timeout'] = reveal_timeout
    expected_response['channel_identifier'] = assert_dicts_are_equal.IGNORE_VALUE
    expected_response['token_network_identifier'] = assert_dicts_are_equal.IGNORE_VALUE
    assert_dicts_are_equal(response, expected_response)

    token_network_identifier = response['token_network_identifier']

    # let's close the channel
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebytokenandpartneraddress',
            token_address=token_address,
            partner_address=partner_address,
        ),
        json={'state': CHANNEL_STATE_CLOSED},
    )
    response = request.send().response
    assert_proper_response(response)
    expected_response = {
        'token_network_identifier': token_network_identifier,
        'channel_identifier': assert_dicts_are_equal.IGNORE_VALUE,
        'partner_address': partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'state': CHANNEL_STATE_CLOSED,
        'balance': balance,
    }
    assert_dicts_are_equal(response.json(), expected_response)
def push():
	allRequests = set()
	for url in urls:
		allRequests.add( grequests.put(apiURI, params={'key':url}, headers=header, data=open(filePath, 'rb'), hooks={'response': printResponse}) )
		# params={'key':url,'datetime':datetime}
	# allRequests = (grequests.put(apiURI, params={'key':url}, headers=header, data=open(filePath, 'rb'), hooks={'response': printResponse}) for url in urls)

	responses = grequests.map(allRequests, stream=False, size=concurrency_limit)
def push(key, payload, pool):
	#logging.debug("+++++ Pushing: " + key + "\n")

	# TODO fetch date of resource somehow, otherwise server will use its own time
	params={'key':key}
	#asynchronus put-request
	req = grequests.put(apiURI, params=params, headers=header, data=payload, hooks={'response': printResponse})
	grequests.send(req, pool)
Пример #14
0
def change_hue_single(hue=10000, sat=254, bri=254, lights=[CHIEF, KEEF]):
    async_list = []

    data = json.dumps({'hue': hue, 'bri': bri, 'transitiontime': 3})
    for light in lights:
        cur_url = '/'.join([BRIX_URL, LIGHTS, str(light), STATE])

        item = grequests.put(cur_url, data=data)
        async_list.append(item)
    grequests.map(async_list)
Пример #15
0
def change_brightness_single(bri=235, lights=[CHIEF, KEEF]):
    async_list = []

    data = json.dumps({'bri': bri})
    for light in lights:
        cur_url = '/'.join([BRIX_URL, LIGHTS, str(light), STATE])

        item = grequests.put(cur_url, data=data)
        async_list.append(item)
    grequests.map(async_list)
def test_register_token(api_backend, api_test_context, api_raiden_service):
    token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8'
    request = grequests.put(api_url_for(
        api_backend,
        'registertokenresource',
        token_address=token_address,
    ))
    response = request.send().response
    assert_proper_response(response, status_code=HTTPStatus.CREATED)
    assert 'channel_manager_address' in response.json()

    # now try to reregister it and get the error
    request = grequests.put(api_url_for(
        api_backend,
        'registertokenresource',
        token_address=token_address,
    ))
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)
Пример #17
0
def test_api_close_insufficient_eth(
        api_backend,
        token_addresses,
        reveal_timeout,
):
    # let's create a new channel
    partner_address = '0x61C808D82A3Ac53231750daDc13c777b59310bD9'
    token_address = token_addresses[0]
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response

    balance = 0
    assert_proper_response(response, status_code=HTTPStatus.CREATED)
    response = response.json()
    expected_response = channel_data_obj
    expected_response['balance'] = balance
    expected_response['state'] = CHANNEL_STATE_OPENED
    expected_response['reveal_timeout'] = reveal_timeout
    expected_response['channel_identifier'] = assert_dicts_are_equal.IGNORE_VALUE
    expected_response['token_network_identifier'] = assert_dicts_are_equal.IGNORE_VALUE
    assert_dicts_are_equal(response, expected_response)

    # let's burn all eth and try to close the channel
    api_server, _ = api_backend
    burn_all_eth(api_server.rest_api.raiden_api.raiden)
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebytokenandpartneraddress',
            token_address=token_address,
            partner_address=partner_address,
        ),
        json={'state': CHANNEL_STATE_CLOSED},
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.PAYMENT_REQUIRED)
    response = response.json()
    assert 'Insufficient ETH' in response['errors']
Пример #18
0
def post_tasks(slug, tasks):
    # and fire!
    headers = {'content-type': 'application/json'}

    s = requests.session()
    rs = (grequests.put(
        mr_api_addtask_endpoint.format(
            slug=slug,
            id=identifier),
        session=s,
        data=payload,
        headers=headers) for identifier, payload in tasks)

    return grequests.map(rs)
Пример #19
0
 def request(self, url, payload):
     if self.sync:
         self.responses.append(
             requests.put(url,
                          data=payload,
                          headers=HEADERS
                          ))
     else:
         self.task_requests.append(
             grequests.put(url,
                           session=self.session,
                           data=payload,
                           headers=HEADERS
                           ))
def test_payload_with_address_invalid_length(api_backend):
    """ Encoded addresses must have the right length. """
    invalid_address = '0x61c808d82a3ac53231750dadc13c777b59310b'  # g at the end is invalid
    channel_data_obj = {
        'partner_address': invalid_address,
        'token_address': '0xea674fdde714fd979de3edf0f56aa9716b898ec8',
        'settle_timeout': 10,
    }
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)
Пример #21
0
 def turnLightOff(self, deviceId):
     # The grequests library sends the request as soon as we create "job" below. We then yield to the greenlet every hundredth of a second
     # in the main update method to ensure we capture the result.
     req = grequests.put(
         'http://{ip}/api/{username}/lights/{devId}/state'.format(
             ip=hub['IP'],
             username=hub['username'],
             devId=deviceId),
         callback=printStatus,
         data='{"on":false}',
         timeout=4)
     job = grequests.send(req, grequests.Pool(1))
     job.link_exception(lambda *kwargs: sys.stdout.write(
         "There was an exception with the Hue request"))
Пример #22
0
def test_api_get_channel_list(
        api_backend,
        token_addresses,
        reveal_timeout,
):
        partner_address = '0x61C808D82A3Ac53231750daDc13c777b59310bD9'

        request = grequests.get(
            api_url_for(
                api_backend,
                'channelsresource',
            ),
        )
        response = request.send().response
        assert_proper_response(response, HTTPStatus.OK)
        assert response.json() == []

        # let's create a new channel
        token_address = token_addresses[0]
        settle_timeout = 1650
        channel_data_obj = {
            'partner_address': partner_address,
            'token_address': to_checksum_address(token_address),
            'settle_timeout': settle_timeout,
            'reveal_timeout': reveal_timeout,
        }

        request = grequests.put(
            api_url_for(
                api_backend,
                'channelsresource',
            ),
            json=channel_data_obj,
        )
        response = request.send().response

        assert_proper_response(response, HTTPStatus.CREATED)

        request = grequests.get(
            api_url_for(
                api_backend,
                'channelsresource',
            ),
        )
        response = request.send().response
        assert_proper_response(response, HTTPStatus.OK)
        channel_info = response.json()[0]
        assert channel_info['partner_address'] == partner_address
        assert channel_info['token_address'] == to_checksum_address(token_address)
        assert 'token_network_identifier' in channel_info
def test_payload_with_address_without_prefix(api_backend):
    """ Addresses require leading 0x in the payload. """
    invalid_address = '61c808d82a3ac53231750dadc13c777b59310bd9'
    channel_data_obj = {
        'partner_address': invalid_address,
        'token_address': '0xea674fdde714fd979de3edf0f56aa9716b898ec8',
        'settle_timeout': 10,
    }
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)
Пример #24
0
def close_tasks(slug, closeids):
    payload = {"status": "deleted"}

    s = requests.session()

    task_requests = []
    for identifier in closeids:
        task_requests.append(
            grequests.put(
                mr_api_addtask_endpoint.format(slug=slug, id=identifier),
                session=s,
                data=payload,
                headers=HEADERS))

    return grequests.map(task_requests)
def push():
	allRequests = set()
	for url in urls:
		if useOwnDate:
			params = {'key':url,'datetime':datetime}
		else:
			params={'key':url}
		allRequests.add( grequests.put(apiURI, params=params, headers=header, data=open(filePath, 'rb'), hooks={'response': printResponse}) )
	
	# could also be done in one line, but would be more difficult to configure	
	# allRequests = (grequests.put(apiURI, params={'key':url}, headers=header, data=open(filePath, 'rb'), hooks={'response': printResponse}) for url in urls)

	startTime = time.time()
	print (str(len(allRequests)) + " keys registered for pushing. ")
	print("Starting Put-Requests:")
	responses = grequests.map(allRequests, stream=False, size=concurrency_limit)
Пример #26
0
def post_tasks(slug, tasks):
    # and fire!
    s = requests.session()

    task_requests = []
    newids = set()
    for identifier, payload in tasks:
        newids.add(identifier)
        task_requests.append(
            grequests.put(
                mr_api_addtask_endpoint.format(slug=slug, id=identifier),
                session=s,
                data=payload,
                headers=HEADERS))

    return grequests.map(task_requests), newids
Пример #27
0
def test_payload_with_address_not_eip55(api_backend):
    """ Provided addresses must be EIP55 encoded. """
    invalid_address = '0xf696209d2ca35e6c88e5b99b7cda3abf316bed69'
    channel_data_obj = {
        'partner_address': invalid_address,
        'token_address': '0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8',
        'settle_timeout': 90,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)
Пример #28
0
def test_payload_with_address_invalid_chars(api_backend):
    """ Addresses cannot have invalid characters in it. """
    invalid_address = '0x61c808d82a3ac53231750dadc13c777b59310bdg'  # g at the end is invalid
    channel_data_obj = {
        'partner_address': invalid_address,
        'token_address': '0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8',
        'settle_timeout': 10,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)
Пример #29
0
def update_tasks(slug, tasks, instruction=None, statuses=None):
    s = requests.session()

    task_requests = []
    for identifier, payload in tasks:
        if instruction is not None:
            payload = {"instruction": instruction,
                       "geometries": payload["geometries"]
                       }

        if identifier not in statuses.keys():
            continue

        task_requests.append(
            grequests.put(
                mr_api_addtask_endpoint.format(slug=slug, id=identifier),
                session=s,
                data=payload,
                headers=HEADERS))

    return grequests.map(task_requests)
Пример #30
0
def test_connect_insufficient_eth(api_backend, token_addresses):

    # Burn all eth and then try to connect to a token network
    api_server, _ = api_backend
    burn_all_eth(api_server.rest_api.raiden_api.raiden)
    funds = 100
    token_address1 = to_checksum_address(token_addresses[0])
    connect_data_obj = {
        'funds': funds,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'connectionsresource',
            token_address=token_address1,
        ),
        json=connect_data_obj,
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.PAYMENT_REQUIRED)
    response = response.json()
    assert 'Insufficient ETH' in response['errors']
Пример #31
0
def test_api_channel_open_close_and_settle(api_server_test_instance: APIServer,
                                           token_addresses, reveal_timeout):
    # let's create a new channel
    partner_address = "0x61C808D82A3Ac53231750daDc13c777b59310bD9"
    token_address = token_addresses[0]
    settle_timeout = 1650
    channel_data_obj = {
        "partner_address": partner_address,
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
    }
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response

    balance = 0
    assert_proper_response(response, status_code=HTTPStatus.CREATED)
    channel_identifier = 1
    json_response = get_json_response(response)
    expected_response = channel_data_obj.copy()
    expected_response.update({
        "balance": str(balance),
        "state": ChannelState.STATE_OPENED.value,
        "reveal_timeout": str(reveal_timeout),
        "channel_identifier": str(channel_identifier),
        "total_deposit": "0",
    })
    assert check_dict_nested_attrs(json_response, expected_response)

    token_network_address = json_response["token_network_address"]

    # let's close the channel
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json={"state": ChannelState.STATE_CLOSED.value},
    )
    response = request.send().response
    assert_proper_response(response)
    expected_response = {
        "token_network_address": token_network_address,
        "channel_identifier": str(channel_identifier),
        "partner_address": partner_address,
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
        "state": ChannelState.STATE_CLOSED.value,
        "balance": str(balance),
        "total_deposit": str(balance),
    }
    assert check_dict_nested_attrs(get_json_response(response),
                                   expected_response)

    # try closing the channel again
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json={"state": ChannelState.STATE_CLOSED.value},
    )
    # Closing the channel again should not work
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CONFLICT)

    # Try sending a payment when channel is closed
    request = grequests.post(
        api_url_for(
            api_server_test_instance,
            "token_target_paymentresource",
            token_address=to_checksum_address(token_address),
            target_address=to_checksum_address(partner_address),
        ),
        json={"amount": "1"},
    )
    # Payment should not work since channel is closing
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CONFLICT)

    # Try to create channel with the same partner again before previous channel settles
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    # Channel exists and is currently being settled so API request to open channel should fail
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CONFLICT)
Пример #32
0
def test_api_channel_state_change_errors(api_server_test_instance: APIServer,
                                         token_addresses, reveal_timeout):
    partner_address = "0x61C808D82A3Ac53231750daDc13c777b59310bD9"
    token_address = token_addresses[0]
    settle_timeout = 1650
    channel_data_obj = {
        "partner_address": partner_address,
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
    }
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CREATED)

    # let's try to set a random state
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(state="inlimbo"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)

    # let's try to set both new state and total_deposit
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(state=ChannelState.STATE_CLOSED.value, total_deposit="200"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    # let's try to set both new state and total_withdraw
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(state=ChannelState.STATE_CLOSED.value, total_withdraw="200"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    # let's try to set both total deposit and total_withdraw
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(total_deposit="500", total_withdraw="200"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    # let's try to set both new state and reveal_timeout
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(state=ChannelState.STATE_CLOSED.value, reveal_timeout="50"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    # let's try to set both total_deposit and reveal_timeout
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(total_deposit="500", reveal_timeout="50"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    # let's try to set both total_withdraw and reveal_timeout
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(total_withdraw="500", reveal_timeout="50"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)

    # let's try to patch with no arguments
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ))
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)

    # ok now let's close and settle for real
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(state=ChannelState.STATE_CLOSED.value),
    )
    response = request.send().response
    assert_proper_response(response)

    # let's try to deposit to a settled channel
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=partner_address,
        ),
        json=dict(total_deposit="500"),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)
Пример #33
0
def test_api_channel_open_and_deposit(api_server_test_instance: APIServer,
                                      token_addresses, reveal_timeout):

    first_partner_address = "0x61C808D82A3Ac53231750daDc13c777b59310bD9"
    token_address = token_addresses[0]
    token_address_hex = to_checksum_address(token_address)
    settle_timeout = 1650
    channel_data_obj = {
        "partner_address": first_partner_address,
        "token_address": token_address_hex,
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
    }
    # First let's try to create channel with the null address and see error is handled
    channel_data_obj["partner_address"] = NULL_ADDRESS_HEX
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_response_with_error(response, status_code=HTTPStatus.BAD_REQUEST)
    # now let's really create a new channel
    channel_data_obj["partner_address"] = first_partner_address
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response

    assert_proper_response(response, HTTPStatus.CREATED)
    first_channel_id = 1
    json_response = get_json_response(response)
    expected_response = channel_data_obj.copy()
    expected_response.update({
        "balance": "0",
        "state": ChannelState.STATE_OPENED.value,
        "channel_identifier": "1",
        "total_deposit": "0",
    })
    assert check_dict_nested_attrs(json_response, expected_response)
    token_network_address = json_response["token_network_address"]

    # Now let's try to open the same channel again, because it is possible for
    # the participants to race on the channel creation, this is not considered
    # an error.
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_proper_response(response, HTTPStatus.OK)
    json_response = get_json_response(response)
    assert check_dict_nested_attrs(json_response, expected_response)

    # now let's open a channel and make a deposit too
    second_partner_address = "0x29FA6cf0Cce24582a9B20DB94Be4B6E017896038"
    total_deposit = 100
    channel_data_obj = {
        "partner_address": second_partner_address,
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
        "total_deposit": str(total_deposit),
    }
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response

    assert_proper_response(response, HTTPStatus.CREATED)
    second_channel_id = 2
    json_response = get_json_response(response)
    expected_response = channel_data_obj.copy()
    expected_response.update({
        "balance": str(total_deposit),
        "state": ChannelState.STATE_OPENED.value,
        "channel_identifier": str(second_channel_id),
        "token_network_address": token_network_address,
        "total_deposit": str(total_deposit),
    })
    assert check_dict_nested_attrs(json_response, expected_response)

    # assert depositing again with less than the initial deposit returns 409
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=second_partner_address,
        ),
        json={"total_deposit": "99"},
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CONFLICT)

    # assert depositing negative amount fails
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=first_partner_address,
        ),
        json={"total_deposit": "-1000"},
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CONFLICT)

    # let's deposit on the first channel
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=first_partner_address,
        ),
        json={"total_deposit": str(total_deposit)},
    )
    response = request.send().response
    assert_proper_response(response)
    json_response = get_json_response(response)
    expected_response = {
        "channel_identifier": str(first_channel_id),
        "partner_address": first_partner_address,
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
        "state": ChannelState.STATE_OPENED.value,
        "balance": str(total_deposit),
        "total_deposit": str(total_deposit),
        "token_network_address": token_network_address,
    }
    assert check_dict_nested_attrs(json_response, expected_response)

    # let's try querying for the second channel
    request = grequests.get(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=second_partner_address,
        ))

    response = request.send().response
    assert_proper_response(response)
    json_response = get_json_response(response)
    expected_response = {
        "channel_identifier": str(second_channel_id),
        "partner_address": second_partner_address,
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
        "state": ChannelState.STATE_OPENED.value,
        "balance": str(total_deposit),
        "total_deposit": str(total_deposit),
        "token_network_address": token_network_address,
    }
    assert check_dict_nested_attrs(json_response, expected_response)

    # finally let's burn all eth and try to open another channel
    burn_eth(api_server_test_instance.rest_api.raiden_api.raiden.rpc_client)
    channel_data_obj = {
        "partner_address": "0xf3AF96F89b3d7CdcBE0C083690A28185Feb0b3CE",
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
    }
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response
    assert_proper_response(response, HTTPStatus.PAYMENT_REQUIRED)
    json_response = get_json_response(response)
    assert "The account balance is below the estimated amount" in json_response[
        "errors"]
Пример #34
0
def test_api_channel_deposit_limit(
    api_server_test_instance,
    proxy_manager,
    token_network_registry_address,
    token_addresses,
    reveal_timeout,
):
    token_address = token_addresses[0]

    registry = proxy_manager.token_network_registry(
        token_network_registry_address, BLOCK_ID_LATEST)
    token_network_address = registry.get_token_network(token_address,
                                                       BLOCK_ID_LATEST)
    token_network = proxy_manager.token_network(token_network_address,
                                                BLOCK_ID_LATEST)
    deposit_limit = token_network.channel_participant_deposit_limit(
        BLOCK_ID_LATEST)

    # let's create a new channel and deposit exactly the limit amount
    first_partner_address = "0x61C808D82A3Ac53231750daDc13c777b59310bD9"
    settle_timeout = 1650
    channel_data_obj = {
        "partner_address": first_partner_address,
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
        "total_deposit": str(deposit_limit),
    }

    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response

    assert_proper_response(response, HTTPStatus.CREATED)
    first_channel_identifier = 1
    json_response = get_json_response(response)
    expected_response = channel_data_obj.copy()
    expected_response.update({
        "balance":
        str(deposit_limit),
        "state":
        ChannelState.STATE_OPENED.value,
        "channel_identifier":
        str(first_channel_identifier),
        "total_deposit":
        str(deposit_limit),
    })
    assert check_dict_nested_attrs(json_response, expected_response)

    # now let's open a channel and deposit a bit more than the limit
    second_partner_address = "0x29FA6cf0Cce24582a9B20DB94Be4B6E017896038"
    balance_failing = deposit_limit + 1  # token has two digits
    channel_data_obj = {
        "partner_address": second_partner_address,
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
        "total_deposit": str(balance_failing),
    }
    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response

    assert_proper_response(response, HTTPStatus.CONFLICT)
    json_response = get_json_response(response)
    assert (
        json_response["errors"] ==
        "Deposit of 75000000000000001 is larger than the channel participant deposit limit"
    )
Пример #35
0
def test_api_open_close_and_settle_channel(
        api_backend,
        api_test_context,
        api_raiden_service):
    # let's create a new channel
    partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9'
    token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8'
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout
    }
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj
    )
    response = request.send().response

    balance = 0
    assert_proper_response(response)
    response = decode_response(response)
    expected_response = channel_data_obj
    expected_response['balance'] = balance
    expected_response['state'] = CHANNEL_STATE_OPENED
    # can't know the channel address beforehand but make sure we get one
    assert 'channel_address' in response
    channel_address = response['channel_address']
    expected_response['channel_address'] = response['channel_address']
    assert response == expected_response

    # let's the close the channel
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json={'state': CHANNEL_STATE_CLOSED}
    )
    response = request.send().response
    assert_proper_response(response)
    response = decode_response(response)
    expected_response = {
        'channel_address': channel_address,
        'partner_address': partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout,
        'state': CHANNEL_STATE_CLOSED,
        'balance': balance
    }
    assert response == expected_response

    # let's settle the channel
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json={'state': CHANNEL_STATE_SETTLED}
    )
    response = request.send().response
    assert_proper_response(response)
    response = decode_response(response)
    expected_response = {
        'channel_address': channel_address,
        'partner_address': partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout,
        'state': CHANNEL_STATE_SETTLED,
        'balance': balance
    }
    assert response == expected_response
Пример #36
0
def test_query_partners_by_token(
        api_backend,
        api_test_context,
        api_raiden_service):
    # let's create 2 new channels for the same token
    first_partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9'
    second_partner_address = '0x29fa6cf0cce24582a9b20db94be4b6e017896038'
    token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8'
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': first_partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout
    }
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj
    )
    response = request.send().response
    assert_proper_response(response)
    response = decode_response(response)
    first_channel_address = response['channel_address']

    channel_data_obj['partner_address'] = second_partner_address
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj,
    )
    response = request.send().response
    assert_proper_response(response)
    response = decode_response(response)
    second_channel_address = response['channel_address']

    # and a channel for another token
    channel_data_obj['partner_address'] = '0xb07937AbA15304FBBB0Bf6454a9377a76E3dD39E'
    channel_data_obj['token_address'] = '0x70faa28A6B8d6829a4b1E649d26eC9a2a39ba413'
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj
    )
    response = request.send().response
    assert_proper_response(response)

    # and now let's query our partners per token for the first token
    request = grequests.get(
        api_url_for(
            api_backend,
            'partnersresourcebytokenaddress',
            token_address=token_address,
        )
    )
    response = request.send().response
    assert_proper_response(response)
    response = decode_response(response)
    expected_response = [
        {
            'partner_address': first_partner_address,
            'channel': '/api/1/channels/{}'.format(first_channel_address)
        }, {
            'partner_address': second_partner_address,
            'channel': '/api/1/channels/{}'.format(second_channel_address)
        }
    ]
    assert all(r in response for r in expected_response)
Пример #37
0
def test_connect_and_leave_token_network(
        api_backend,
        api_test_context,
        api_raiden_service):

    # first check we don't have any open channels
    request = grequests.get(
        api_url_for(api_backend, 'channelsresource')
    )
    response = request.send().response
    assert_proper_response(response)
    channels = response.json()
    assert not channels
    assert not api_test_context.expect_channels()

    funds = 100
    initial_channel_target = DEFAULT_INITIAL_CHANNEL_TARGET
    joinable_funds_target = DEFAULT_JOINABLE_FUNDS_TARGET
    token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8'
    connect_data_obj = {
        'funds': funds,
    }
    request = grequests.put(
        api_url_for(api_backend, 'connectionsresource', token_address=token_address),
        json=connect_data_obj,
    )
    response = request.send().response
    assert_no_content_response(response)

    # check that channels got created
    request = grequests.get(
        api_url_for(api_backend, 'channelsresource')
    )
    response = request.send().response
    assert_proper_response(response)
    channels = response.json()
    # There should be three channels according to the default initial_channel_target
    assert len(channels) == DEFAULT_INITIAL_CHANNEL_TARGET
    assert response.json() == api_test_context.expect_channels()

    expected_balance = int((funds * joinable_funds_target) / initial_channel_target)
    assert channels[0]['balance'] == expected_balance
    assert channels[1]['balance'] == expected_balance
    assert channels[2]['balance'] == expected_balance
    assert channels[0]['state'] == CHANNEL_STATE_OPENED
    assert channels[1]['state'] == CHANNEL_STATE_OPENED
    assert channels[2]['state'] == CHANNEL_STATE_OPENED

    # Let's leave the token network
    request = grequests.delete(
        api_url_for(api_backend, 'connectionsresource', token_address=token_address),
    )
    response = request.send().response
    assert api_test_context.last_only_receiving
    assert_proper_response(response)
    response = response.json()
    expected_response = [channel['channel_address'] for channel in channels]
    assert set(response) == set(expected_response)

    # check that all channels were settled after calling `leave`
    request = grequests.get(
        api_url_for(api_backend, 'channelsresource')
    )
    response = request.send().response
    assert_proper_response(response)

    channels = response.json()
    assert channels[0]['state'] == CHANNEL_STATE_SETTLED
    assert channels[1]['state'] == CHANNEL_STATE_SETTLED
    assert channels[2]['state'] == CHANNEL_STATE_SETTLED

    # Let's try to leave again but this time set only_receiving to False
    request = grequests.delete(
        api_url_for(api_backend, 'connectionsresource', token_address=token_address),
        json={'only_receiving_channels': False},
    )
    response = request.send().response
    assert_proper_response(response)
    assert not api_test_context.last_only_receiving
Пример #38
0
def test_api_open_close_and_settle_channel(
        api_backend,
        api_test_context,
        api_raiden_service):
    # let's create a new channel
    partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9'
    token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8'
    settle_timeout = 1650
    reveal_timeout = DEFAULT_REVEAL_TIMEOUT
    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout,
    }
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj
    )
    response = request.send().response

    balance = 0
    assert_proper_response(response, status_code=httplib.CREATED)
    response = response.json()
    expected_response = channel_data_obj
    expected_response['balance'] = balance
    expected_response['state'] = CHANNEL_STATE_OPENED
    # reveal_timeout not specified, expect it to be the default
    expected_response['reveal_timeout'] = reveal_timeout
    # can't know the channel address beforehand but make sure we get one
    assert 'channel_address' in response
    channel_address = response['channel_address']
    expected_response['channel_address'] = response['channel_address']
    assert response == expected_response

    # let's close the channel
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json={'state': CHANNEL_STATE_CLOSED}
    )
    response = request.send().response
    assert_proper_response(response)
    expected_response = {
        'channel_address': channel_address,
        'partner_address': partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'state': CHANNEL_STATE_CLOSED,
        'balance': balance
    }
    assert response.json() == expected_response

    # let's settle the channel
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json=dict(state=CHANNEL_STATE_SETTLED)
    )
    response = request.send().response
    assert_proper_response(response)
    expected_response = {
        'channel_address': channel_address,
        'partner_address': partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'state': CHANNEL_STATE_SETTLED,
        'balance': balance
    }
    assert response.json() == expected_response
Пример #39
0
def test_query_partners_by_token(api_backend, blockchain_services, token_addresses):
    first_partner_address = '0x61C808D82A3Ac53231750daDc13c777b59310bD9'
    second_partner_address = '0x29FA6cf0Cce24582a9B20DB94Be4B6E017896038'
    token_address = token_addresses[0]
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': first_partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CREATED)
    response = response.json()
    first_channel_address = response['channel_address']

    channel_data_obj['partner_address'] = second_partner_address
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CREATED)
    response = response.json()
    second_channel_address = response['channel_address']

    # and a channel for another token
    channel_data_obj['partner_address'] = '0xb07937AbA15304FBBB0Bf6454a9377a76E3dD39E'
    channel_data_obj['token_address'] = to_checksum_address(token_address)
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CREATED)

    # and now let's query our partners per token for the first token
    request = grequests.get(
        api_url_for(
            api_backend,
            'partnersresourcebytokenaddress',
            token_address=to_checksum_address(token_address),
        ),
    )
    response = request.send().response
    assert_proper_response(response)
    response = response.json()
    expected_response = [
        {
            'partner_address': first_partner_address,
            'channel': '/api/1/channels/{}'.format(
                first_channel_address,
            ),
        }, {
            'partner_address': second_partner_address,
            'channel': '/api/1/channels/{}'.format(
                second_channel_address,
            ),
        },
    ]
    assert all(r in response for r in expected_response)
Пример #40
0
def test_api_channel_state_change_errors(
        api_backend,
        token_addresses,
        reveal_timeout,
):
    partner_address = '0x61C808D82A3Ac53231750daDc13c777b59310bD9'
    token_address = token_addresses[0]
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.CREATED)
    response = response.json()
    channel_address = response['channel_address']

    # let's try to set a random state
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address,
        ),
        json=dict(state='inlimbo'),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)
    # let's try to set both new state and balance
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address,
        ),
        json=dict(state=CHANNEL_STATE_CLOSED, total_deposit=200),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)
    # let's try to patch with no arguments
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address,
        ),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.BAD_REQUEST)

    # ok now let's close and settle for real
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address,
        ),
        json=dict(state=CHANNEL_STATE_CLOSED),
    )
    response = request.send().response
    assert_proper_response(response)

    # let's try to deposit to a settled channel
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address,
        ),
        json=dict(total_deposit=500),
    )
    response = request.send().response
    assert_response_with_error(response, HTTPStatus.CONFLICT)
Пример #41
0
def test_api_open_and_deposit_channel(
        api_backend,
        token_addresses,
        reveal_timeout,
):
    # let's create a new channel
    first_partner_address = '0x61C808D82A3Ac53231750daDc13c777b59310bD9'
    token_address = token_addresses[0]
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': first_partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
    }

    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response

    assert_proper_response(response, HTTPStatus.CREATED)
    response = response.json()
    expected_response = channel_data_obj
    expected_response['balance'] = 0
    expected_response['state'] = CHANNEL_STATE_OPENED
    # can't know the channel address beforehand but make sure we get one
    assert 'channel_address' in response
    first_channel_address = response['channel_address']
    expected_response['channel_address'] = response['channel_address']
    assert response == expected_response

    # now let's open a channel and make a deposit too
    second_partner_address = '0x29FA6cf0Cce24582a9B20DB94Be4B6E017896038'
    balance = 100
    channel_data_obj = {
        'partner_address': second_partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'balance': balance,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response

    assert_proper_response(response, HTTPStatus.CREATED)
    response = response.json()
    expected_response = channel_data_obj
    expected_response['balance'] = balance
    expected_response['state'] = CHANNEL_STATE_OPENED
    # can't know the channel address beforehand but make sure we get one
    assert 'channel_address' in response
    expected_response['channel_address'] = response['channel_address']
    second_channel_address = response['channel_address']
    assert response == expected_response

    # let's deposit on the first channel
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=first_channel_address,
        ),
        json={'total_deposit': balance},
    )
    response = request.send().response
    assert_proper_response(response)
    response = response.json()
    expected_response = {
        'channel_address': first_channel_address,
        'partner_address': first_partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'state': CHANNEL_STATE_OPENED,
        'balance': balance,
    }
    assert response == expected_response

    # finally let's try querying for the second channel
    request = grequests.get(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=second_channel_address,
        ),
    )

    response = request.send().response
    assert_proper_response(response)
    response = response.json()
    expected_response = {
        'channel_address': second_channel_address,
        'partner_address': second_partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'state': CHANNEL_STATE_OPENED,
        'balance': balance,
    }
    assert response == expected_response
Пример #42
0
def test_get_connection_managers_info(raiden_network, api_backend,
                                      token_addresses):
    # check that there are no registered tokens
    request = grequests.get(
        api_url_for(api_backend, 'connectionsinforesource'), )
    response = request.send().response
    result = response.json()
    assert len(result) == 0

    funds = 100
    token_address1 = to_checksum_address(token_addresses[0])
    connect_data_obj = {
        'funds': funds,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'connectionsresource',
            token_address=token_address1,
        ),
        json=connect_data_obj,
    )
    response = request.send().response
    assert_no_content_response(response)

    # check that there now is one registered channel manager
    request = grequests.get(
        api_url_for(api_backend, 'connectionsinforesource'), )
    response = request.send().response
    result = response.json()
    assert isinstance(result, dict) and len(result.keys()) == 1
    assert token_address1 in result
    assert isinstance(result[token_address1], dict)
    assert set(result[token_address1].keys()) == {
        'funds', 'sum_deposits', 'channels'
    }

    funds = 100
    token_address2 = to_checksum_address(token_addresses[1])
    connect_data_obj = {
        'funds': funds,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'connectionsresource',
            token_address=token_address2,
        ),
        json=connect_data_obj,
    )
    response = request.send().response
    assert_no_content_response(response)

    # check that there now are two registered channel managers
    request = grequests.get(
        api_url_for(api_backend, 'connectionsinforesource'), )
    response = request.send().response
    result = response.json()
    assert isinstance(result, dict) and len(result.keys()) == 2
    assert token_address2 in result
    assert isinstance(result[token_address2], dict)
    assert set(result[token_address2].keys()) == {
        'funds', 'sum_deposits', 'channels'
    }
Пример #43
0
def test_api_channel_open_and_deposit_race(
    api_server_test_instance: APIServer,
    raiden_network: List[RaidenService],
    token_addresses,
    reveal_timeout,
    token_network_registry_address,
    retry_timeout,
):
    """Tests that a race for the same deposit from the API is handled properly

    The proxy's approve_and_set_total_deposit is raising a
    RaidenRecoverableError in case of races. That needs to be properly handled
    and not allowed to bubble out of the greenlet.

    Regression test for https://github.com/raiden-network/raiden/issues/4937
    """
    app0 = raiden_network[0]
    # let's create a new channel
    first_partner_address = "0x61C808D82A3Ac53231750daDc13c777b59310bD9"
    token_address = token_addresses[0]
    settle_timeout = 1650
    channel_data_obj = {
        "partner_address": first_partner_address,
        "token_address": to_checksum_address(token_address),
        "settle_timeout": str(settle_timeout),
        "reveal_timeout": str(reveal_timeout),
    }

    request = grequests.put(api_url_for(api_server_test_instance,
                                        "channelsresource"),
                            json=channel_data_obj)
    response = request.send().response

    assert_proper_response(response, HTTPStatus.CREATED)
    json_response = get_json_response(response)
    expected_response = channel_data_obj.copy()
    expected_response.update({
        "balance": "0",
        "state": ChannelState.STATE_OPENED.value,
        "channel_identifier": "1",
        "total_deposit": "0",
    })
    assert check_dict_nested_attrs(json_response, expected_response)

    # Prepare the deposit api call
    deposit_amount = TokenAmount(99)
    request = grequests.patch(
        api_url_for(
            api_server_test_instance,
            "channelsresourcebytokenandpartneraddress",
            token_address=token_address,
            partner_address=first_partner_address,
        ),
        json={"total_deposit": str(deposit_amount)},
    )

    # Spawn two greenlets doing the same deposit request
    greenlets = [gevent.spawn(request.send), gevent.spawn(request.send)]
    gevent.joinall(set(greenlets), raise_error=True)
    # Make sure that both responses are fine
    g1_response = greenlets[0].get().response
    assert_proper_response(g1_response, HTTPStatus.OK)
    json_response = get_json_response(g1_response)
    expected_response.update({
        "total_deposit": str(deposit_amount),
        "balance": str(deposit_amount)
    })
    assert check_dict_nested_attrs(json_response, expected_response)
    g2_response = greenlets[0].get().response
    assert_proper_response(g2_response, HTTPStatus.OK)
    json_response = get_json_response(g2_response)
    assert check_dict_nested_attrs(json_response, expected_response)

    # Wait for the deposit to be seen
    timeout_seconds = 20
    exception = Exception(
        f"Expected deposit not seen within {timeout_seconds}")
    with gevent.Timeout(seconds=timeout_seconds, exception=exception):
        wait_for_participant_deposit(
            raiden=app0,
            token_network_registry_address=token_network_registry_address,
            token_address=token_address,
            partner_address=to_canonical_address(first_partner_address),
            target_address=app0.address,
            target_balance=deposit_amount,
            retry_timeout=retry_timeout,
        )

    request = grequests.get(
        api_url_for(api_server_test_instance, "channelsresource"))
    response = request.send().response
    assert_proper_response(response, HTTPStatus.OK)
    json_response = get_json_response(response)
    channel_info = json_response[0]
    assert channel_info["token_address"] == to_checksum_address(token_address)
    assert channel_info["total_deposit"] == str(deposit_amount)
Пример #44
0
def test_api_deposit_limit(
    api_backend,
    token_addresses,
    reveal_timeout,
):
    # let's create a new channel and deposit exactly the limit amount
    first_partner_address = '0x61C808D82A3Ac53231750daDc13c777b59310bD9'
    token_address = token_addresses[0]
    settle_timeout = 1650
    balance_working = MAX_TOKENS_DEPLOY * (10**2)  # token has two digits
    channel_data_obj = {
        'partner_address': first_partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'balance': balance_working,
    }

    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response

    assert_proper_response(response, HTTPStatus.CREATED)
    first_channel_identifier = 1
    response = response.json()
    expected_response = channel_data_obj
    expected_response['balance'] = balance_working
    expected_response['state'] = CHANNEL_STATE_OPENED
    expected_response['channel_identifier'] = first_channel_identifier
    expected_response[
        'token_network_identifier'] = assert_dicts_are_equal.IGNORE_VALUE
    assert_dicts_are_equal(response, expected_response)

    # now let's open a channel and deposit a bit more than the limit
    second_partner_address = '0x29FA6cf0Cce24582a9B20DB94Be4B6E017896038'
    balance_failing = balance_working + 1  # token has two digits
    channel_data_obj = {
        'partner_address': second_partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'balance': balance_failing,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response

    assert_proper_response(response, HTTPStatus.CONFLICT)
    response = response.json()
    assert response[
        'errors'] == 'The deposit of 10001 is bigger than the current limit of 10000'
Пример #45
0
 
 # Send depth/temp to external/depth api
 ext_depth = {}
 ext_depth['depth'] = max(min(100, recv_payload['depth']), 0)
 ext_depth['temp'] = max(min(100, recv_payload['temp']), 0)
 
 send_payload = json.dumps(ext_depth)
 
 headers = {'Content-type': 'application/json'}
 
 url = gpsUrl + "/api/v1/external/depth"
 print('sending', send_payload, 'to', url)
 
 # Equivalent
 # curl -X PUT -H "Content-Type: application/json" -d '{"depth":1,"temp":2}' "http://37.139.8.112:8000/api/v1/external/depth"
 request = grequests.put(url, session=s, headers=headers, data=send_payload, hooks={'response': notifyPutResponse})
 grequests.send(request)
 
 # Send heading to external/orientation api
 ext_orientation = {}
 ext_orientation['orientation'] = max(min(360, recv_payload['orientation']), 0)
 
 send_payload = json.dumps(ext_orientation)
 
 headers = {'Content-type': 'application/json'}
 
 url = gpsUrl + "/api/v1/external/orientation"
 print('sending', send_payload, 'to', url)
 
 request = grequests.put(url, session=s, headers=headers, data=send_payload, hooks={'response': notifyPutResponse})
 grequests.send(request)
Пример #46
0
def test_get_connection_managers_info(api_backend, blockchain_services):
    # check that no connection managers exists yet

    request = grequests.get(
        api_url_for(api_backend, 'connectionmanagersresource'), )
    response = request.send().response
    token_addresses = response.json()
    assert token_addresses == dict()

    funds = 100
    token_address1 = '0xea674fdde714fd979de3edf0f56aa9716b898ec8'
    connect_data_obj = {
        'funds': funds,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'connectionsresource',
            token_address=token_address1,
        ),
        json=connect_data_obj,
    )
    response = request.send().response
    assert_no_content_response(response)

    # check that there now is one registered channel manager
    request = grequests.get(
        api_url_for(api_backend, 'connectionmanagersresource'))
    response = request.send().response
    token_addresses = response.json()
    assert isinstance(token_addresses, dict) and len(
        token_addresses.keys()) == 1
    assert token_address1 in token_addresses
    assert isinstance(token_addresses[token_address1], dict)
    assert set(token_addresses[token_address1].keys()) == {
        'funds', 'sum_deposits', 'channels'
    }

    funds = 100
    token_address2 = '0x3edf0f56aa9716b898ec8ea674fdde714fd979de'
    connect_data_obj = {
        'funds': funds,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'connectionsresource',
            token_address=token_address2,
        ),
        json=connect_data_obj,
    )
    response = request.send().response
    assert_no_content_response(response)

    # check that there now are two registered channel managers
    request = grequests.get(
        api_url_for(api_backend, 'connectionmanagersresource'))
    response = request.send().response
    token_addresses = response.json()
    assert isinstance(token_addresses, dict) and len(
        token_addresses.keys()) == 2
    assert token_address2 in token_addresses
    assert isinstance(token_addresses[token_address2], dict)
    assert set(token_addresses[token_address2].keys()) == {
        'funds', 'sum_deposits', 'channels'
    }
Пример #47
0
def test_api_channel_state_change_errors(
        api_backend,
        api_test_context,
        api_raiden_service):
    # let's create a new channel
    partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9'
    token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8'
    settle_timeout = 1650
    reveal_timeout = 30
    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
    }
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj
    )
    response = request.send().response
    assert_proper_response(response, httplib.CREATED)
    response = response.json()
    channel_address = response['channel_address']

    # let's try to settle the channel (we are bad!)
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json=dict(state=CHANNEL_STATE_SETTLED)
    )
    response = request.send().response
    assert_response_with_error(response, httplib.CONFLICT)
    # let's try to set a random state
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json=dict(state='inlimbo')
    )
    response = request.send().response
    assert_response_with_error(response, httplib.BAD_REQUEST)
    # let's try to set both new state and balance
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json=dict(state=CHANNEL_STATE_CLOSED, balance=200)
    )
    response = request.send().response
    assert_response_with_error(response, httplib.CONFLICT)
    # let's try to path with no arguments
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
    )
    response = request.send().response
    assert_response_with_error(response, httplib.BAD_REQUEST)

    # ok now let's close and settle for real
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json=dict(state=CHANNEL_STATE_CLOSED)
    )
    response = request.send().response
    assert_proper_response(response)
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json=dict(state=CHANNEL_STATE_SETTLED)
    )
    response = request.send().response
    assert_proper_response(response)

    # let's try to deposit to a settled channel
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json=dict(balance=500)
    )
    response = request.send().response
    assert_response_with_error(response, httplib.CONFLICT)

    # and now let's try to settle again
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json=dict(state=CHANNEL_STATE_SETTLED)
    )
    response = request.send().response
    assert_response_with_error(response, httplib.CONFLICT)
Пример #48
0
def test_api_open_and_deposit_channel(
        api_backend,
        api_test_context,
        api_raiden_service):
    # let's create a new channel
    first_partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9'
    token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8'
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': first_partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout
    }
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj
    )
    response = request.send().response

    assert_proper_response(response)
    response = decode_response(response)
    expected_response = channel_data_obj
    expected_response['balance'] = 0
    expected_response['state'] = CHANNEL_STATE_OPENED
    # can't know the channel address beforehand but make sure we get one
    assert 'channel_address' in response
    first_channel_address = response['channel_address']
    expected_response['channel_address'] = response['channel_address']
    assert response == expected_response

    # now let's open a channel and make a deposit too
    second_partner_address = '0x29fa6cf0cce24582a9b20db94be4b6e017896038'
    balance = 100
    channel_data_obj = {
        'partner_address': second_partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout,
        'balance': balance
    }
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj
    )
    response = request.send().response

    assert_proper_response(response)
    response = decode_response(response)
    expected_response = channel_data_obj
    expected_response['balance'] = balance
    expected_response['state'] = CHANNEL_STATE_OPENED
    # can't know the channel address beforehand but make sure we get one
    assert 'channel_address' in response
    expected_response['channel_address'] = response['channel_address']
    second_channel_address = response['channel_address']
    assert response == expected_response

    # let's deposit on the first channel
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=first_channel_address
        ),
        json={'balance': balance}
    )
    response = request.send().response
    assert_proper_response(response)
    response = decode_response(response)
    expected_response = {
        'channel_address': first_channel_address,
        'partner_address': first_partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout,
        'state': CHANNEL_STATE_OPENED,
        'balance': balance
    }
    assert response == expected_response

    # finally let's try querying for the second channel
    request = grequests.get(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=second_channel_address
        )
    )

    response = request.send().response
    assert_proper_response(response)
    response = decode_response(response)
    expected_response = {
        'channel_address': second_channel_address,
        'partner_address': second_partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout,
        'state': CHANNEL_STATE_OPENED,
        'balance': balance
    }
    assert response == expected_response
Пример #49
0
def test_api_open_close_and_settle_channel(
    api_backend,
    token_addresses,
    reveal_timeout,
):
    # let's create a new channel
    partner_address = '0x61C808D82A3Ac53231750daDc13c777b59310bD9'
    token_address = token_addresses[0]
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response

    balance = 0
    assert_proper_response(response, status_code=HTTPStatus.CREATED)
    channel_identifier = 1
    response = response.json()
    expected_response = channel_data_obj
    expected_response['balance'] = balance
    expected_response['state'] = CHANNEL_STATE_OPENED
    expected_response['reveal_timeout'] = reveal_timeout
    expected_response['channel_identifier'] = channel_identifier
    expected_response[
        'token_network_identifier'] = assert_dicts_are_equal.IGNORE_VALUE
    assert_dicts_are_equal(response, expected_response)

    token_network_identifier = response['token_network_identifier']

    # let's close the channel
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebytokenandpartneraddress',
            token_address=token_address,
            partner_address=partner_address,
        ),
        json={'state': CHANNEL_STATE_CLOSED},
    )
    response = request.send().response
    assert_proper_response(response)
    expected_response = {
        'token_network_identifier': token_network_identifier,
        'channel_identifier': channel_identifier,
        'partner_address': partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'state': CHANNEL_STATE_CLOSED,
        'balance': balance,
    }
    assert_dicts_are_equal(response.json(), expected_response)
Пример #50
0
def test_api_channel_state_change_errors(
        api_backend,
        api_test_context,
        api_raiden_service):
    # let's create a new channel
    partner_address = '0x61c808d82a3ac53231750dadc13c777b59310bd9'
    token_address = '0xea674fdde714fd979de3edf0f56aa9716b898ec8'
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': partner_address,
        'token_address': token_address,
        'settle_timeout': settle_timeout
    }
    request = grequests.put(
        api_url_for(api_backend, 'channelsresource'),
        json=channel_data_obj
    )
    response = request.send().response
    assert_proper_response(response)
    response = decode_response(response)
    channel_address = response['channel_address']

    # let's try to settle the channel (we are bad!)
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json={'state': CHANNEL_STATE_SETTLED}
    )
    response = request.send().response
    assert response is not None and response.status_code == httplib.CONFLICT
    # let's try to set a random state
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json={'state': 'inlimbo'}
    )
    response = request.send().response
    assert response is not None and response.status_code == httplib.BAD_REQUEST
    # let's try to set both new state and balance
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json={'state': CHANNEL_STATE_CLOSED, 'balance': 200}
    )
    response = request.send().response
    assert response is not None and response.status_code == httplib.CONFLICT
    # let's try to path with no arguments
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
    )
    response = request.send().response
    assert response is not None and response.status_code == httplib.BAD_REQUEST

    # ok now let's close and settle for real
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json={'state': CHANNEL_STATE_CLOSED}
    )
    response = request.send().response
    assert_proper_response(response)
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json={'state': CHANNEL_STATE_SETTLED}
    )
    response = request.send().response
    assert_proper_response(response)

    # let's try to deposit to a settled channel
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json={'balance': 500}
    )
    response = request.send().response
    assert response is not None and response.status_code == httplib.CONFLICT

    # and now let's try to settle again
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebychanneladdress',
            channel_address=channel_address
        ),
        json={'state': CHANNEL_STATE_SETTLED}
    )
    response = request.send().response
    assert response is not None and response.status_code == httplib.CONFLICT
Пример #51
0
 def head(self, url, **kwargs):
     """HTTP Method"""
     kwargs['auth'] = self.auth
     req = grequests.put(url, **kwargs)
     return self._run(req)
Пример #52
0
def test_api_open_and_deposit_channel(
        api_backend,
        token_addresses,
        reveal_timeout,
):
    # let's create a new channel
    first_partner_address = '0x61C808D82A3Ac53231750daDc13c777b59310bD9'
    token_address = token_addresses[0]
    settle_timeout = 1650
    channel_data_obj = {
        'partner_address': first_partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
    }

    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response

    assert_proper_response(response, HTTPStatus.CREATED)
    response = response.json()
    expected_response = channel_data_obj
    expected_response['balance'] = 0
    expected_response['state'] = CHANNEL_STATE_OPENED
    expected_response['channel_identifier'] = assert_dicts_are_equal.IGNORE_VALUE
    expected_response['token_network_identifier'] = assert_dicts_are_equal.IGNORE_VALUE
    assert_dicts_are_equal(response, expected_response)

    token_network_identifier = response['token_network_identifier']

    # now let's open a channel and make a deposit too
    second_partner_address = '0x29FA6cf0Cce24582a9B20DB94Be4B6E017896038'
    balance = 100
    channel_data_obj = {
        'partner_address': second_partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'balance': balance,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response

    assert_proper_response(response, HTTPStatus.CREATED)
    response = response.json()
    expected_response = channel_data_obj
    expected_response['balance'] = balance
    expected_response['state'] = CHANNEL_STATE_OPENED
    expected_response['channel_identifier'] = assert_dicts_are_equal.IGNORE_VALUE
    expected_response['token_network_identifier'] = token_network_identifier
    assert_dicts_are_equal(response, expected_response)

    # let's deposit on the first channel
    request = grequests.patch(
        api_url_for(
            api_backend,
            'channelsresourcebytokenandpartneraddress',
            token_address=token_address,
            partner_address=first_partner_address,
        ),
        json={'total_deposit': balance},
    )
    response = request.send().response
    assert_proper_response(response)
    response = response.json()
    expected_response = {
        'channel_identifier': assert_dicts_are_equal.IGNORE_VALUE,
        'partner_address': first_partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'state': CHANNEL_STATE_OPENED,
        'balance': balance,
        'token_network_identifier': token_network_identifier,
    }
    assert_dicts_are_equal(response, expected_response)

    # let's try querying for the second channel
    request = grequests.get(
        api_url_for(
            api_backend,
            'channelsresourcebytokenandpartneraddress',
            token_address=token_address,
            partner_address=second_partner_address,
        ),
    )

    response = request.send().response
    assert_proper_response(response)
    response = response.json()
    expected_response = {
        'channel_identifier': assert_dicts_are_equal.IGNORE_VALUE,
        'partner_address': second_partner_address,
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'state': CHANNEL_STATE_OPENED,
        'balance': balance,
        'token_network_identifier': token_network_identifier,
    }
    assert_dicts_are_equal(response, expected_response)

    # finally let's burn all eth and try to open another channel
    api_server, _ = api_backend
    burn_all_eth(api_server.rest_api.raiden_api.raiden)
    channel_data_obj = {
        'partner_address': '0xf3AF96F89b3d7CdcBE0C083690A28185Feb0b3CE',
        'token_address': to_checksum_address(token_address),
        'settle_timeout': settle_timeout,
        'reveal_timeout': reveal_timeout,
        'balance': 1,
    }
    request = grequests.put(
        api_url_for(
            api_backend,
            'channelsresource',
        ),
        json=channel_data_obj,
    )
    response = request.send().response
    assert_proper_response(response, HTTPStatus.PAYMENT_REQUIRED)
    response = response.json()
    assert 'Insufficient ETH' in response['errors']