def test_token_registration(blockchain_type, raiden_network, tester_state): if blockchain_type == 'tester': pytest.skip( 'current version of the pyethereum dependency does not support the REVERT opcode' ) node1 = raiden_network[0] token_amount = 1000 token_address = node1.raiden.chain.deploy_contract( contract_name='HumanStandardToken', contract_path=get_contract_path('HumanStandardToken.sol'), constructor_parameters=(token_amount, 'raiden', 2, 'Rd'), ) api1 = RaidenAPI(node1.raiden) assert not api1.get_tokens_list() assert api1.manager_address_if_token_registered(token_address) is None node1.raiden.poll_blockchain_events() assert not api1.get_tokens_list() api1.register_token(token_address) assert api1.manager_address_if_token_registered(token_address) is not None assert api1.get_tokens_list() == [token_address]
def run_test_token_registered_race(raiden_chain, token_amount, retry_timeout, contract_manager): app0, app1 = raiden_chain api0 = RaidenAPI(app0.raiden) api1 = RaidenAPI(app1.raiden) # Recreate the race condition by making sure the non-registering app won't # register at all by watching for the TokenAdded blockchain event. event_listeners = app1.raiden.blockchain_events.event_listeners app1.raiden.blockchain_events.event_listeners = list() token_address = deploy_contract_web3( contract_name=CONTRACT_HUMAN_STANDARD_TOKEN, deploy_client=app1.raiden.chain.client, contract_manager=contract_manager, constructor_arguments=( token_amount, 2, 'raiden', 'Rd', ), ) gevent.sleep(1) registry_address = app0.raiden.default_registry.address assert token_address not in api0.get_tokens_list(registry_address) assert token_address not in api1.get_tokens_list(registry_address) api0.token_network_register( registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=UINT256_MAX, token_network_deposit_limit=UINT256_MAX, ) exception = RuntimeError('Did not see the token registration within 30 seconds') with gevent.Timeout(seconds=30, exception=exception): wait_for_state_change( app0.raiden, ContractReceiveNewTokenNetwork, { 'token_network': { 'token_address': token_address, }, }, retry_timeout, ) assert token_address in api0.get_tokens_list(registry_address) assert token_address not in api1.get_tokens_list(registry_address) # The next time when the event is polled, the token is registered app1.raiden.blockchain_events.event_listeners = event_listeners waiting.wait_for_block( app1.raiden, app1.raiden.get_block_number() + 1, retry_timeout, ) assert token_address in api1.get_tokens_list(registry_address)
def test_second_manager_address_if_token_registered(raiden_chain, token_addresses): """Test recreating the scenario described on issue: https://github.com/raiden-network/raiden/issues/784""" app0, app1 = raiden_chain # pylint: disable=unbalanced-tuple-unpacking api0 = RaidenAPI(app0.raiden) api1 = RaidenAPI(app1.raiden) # Recreate the race condition by making sure the non-registering app won't # register at all by watching for the TokenAdded blockchain event. app1.raiden.alarm.remove_callback(app1.raiden.poll_blockchain_events) manager_0token = api0.register_token(token_addresses[0]) # The second node does not register but just confirms token is registered. # This is the behaviour the api call implement in register_token(). manager_1token = api1.manager_address_if_token_registered(token_addresses[0]) assert manager_0token == manager_1token # Now make sure the token lists are populated for both nodes tokens0_list = api0.get_tokens_list() tokens1_list = api1.get_tokens_list() assert tokens0_list == tokens1_list assert len(tokens1_list) == 1 assert token_addresses[0] == tokens1_list[0]
def test_second_manager_address_if_token_registered(raiden_chain, token_addresses): """Test recreating the scenario described on issue: https://github.com/raiden-network/raiden/issues/784""" app0, app1 = raiden_chain # pylint: disable=unbalanced-tuple-unpacking api0 = RaidenAPI(app0.raiden) api1 = RaidenAPI(app1.raiden) # Recreate the race condition by making sure the non-registering app won't # register at all by watching for the TokenAdded blockchain event. app1.raiden.alarm.remove_callback(app1.raiden.poll_blockchain_events) manager_0token = api0.register_token(token_addresses[0]) # The second node does not register but just confirms token is registered. # This is the behaviour the api call implement in register_token(). manager_1token = api1.manager_address_if_token_registered( token_addresses[0]) assert manager_0token == manager_1token # Now make sure the token lists are populated for both nodes tokens0_list = api0.get_tokens_list() tokens1_list = api1.get_tokens_list() assert tokens0_list == tokens1_list assert len(tokens1_list) == 1 assert token_addresses[0] == tokens1_list[0]
def test_register_token(raiden_network, token_amount): app1 = raiden_network[0] registry_address = app1.raiden.default_registry.address token_address = deploy_contract_web3( CONTRACT_HUMAN_STANDARD_TOKEN, app1.raiden.chain.client, num_confirmations=None, constructor_arguments=( token_amount, 2, 'raiden', 'Rd', ), ) api1 = RaidenAPI(app1.raiden) assert token_address not in api1.get_tokens_list(registry_address) api1.token_network_register(registry_address, token_address) assert token_address in api1.get_tokens_list(registry_address) # Exception if we try to reregister with pytest.raises(AlreadyRegisteredTokenAddress): api1.token_network_register(registry_address, token_address)
def test_token_registered_race(raiden_chain, token_amount): """Test recreating the scenario described on issue: https://github.com/raiden-network/raiden/issues/784""" app0, app1 = raiden_chain api0 = RaidenAPI(app0.raiden) api1 = RaidenAPI(app1.raiden) # Recreate the race condition by making sure the non-registering app won't # register at all by watching for the TokenAdded blockchain event. app1.raiden.alarm.remove_callback(app1.raiden.poll_blockchain_events) token_address = app1.raiden.chain.deploy_contract( contract_name='HumanStandardToken', contract_path=get_contract_path('HumanStandardToken.sol'), constructor_parameters=(token_amount, 'raiden', 2, 'Rd'), ) gevent.sleep(1) assert token_address not in api0.get_tokens_list() assert token_address not in api1.get_tokens_list() api0.token_network_register(token_address) gevent.sleep(1) assert token_address in api0.get_tokens_list() assert token_address not in api1.get_tokens_list() # The next time when the event is polled, the token is registered app1.raiden.poll_blockchain_events() assert token_address in api1.get_tokens_list()
def run_test_set_deposit_limit_crash( raiden_network, token_amount, contract_manager, retry_timeout, ): app1 = raiden_network[0] registry_address = app1.raiden.default_registry.address token_address = deploy_contract_web3( contract_name=CONTRACT_HUMAN_STANDARD_TOKEN, deploy_client=app1.raiden.chain.client, contract_manager=contract_manager, constructor_arguments=( token_amount, 2, 'raiden', 'Rd', ), ) api1 = RaidenAPI(app1.raiden) assert token_address not in api1.get_tokens_list(registry_address) api1.token_network_register( registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=RED_EYES_PER_CHANNEL_PARTICIPANT_LIMIT, token_network_deposit_limit=RED_EYES_PER_TOKEN_NETWORK_LIMIT, ) exception = RuntimeError('Did not see the token registration within 30 seconds') with gevent.Timeout(seconds=30, exception=exception): wait_for_state_change( app1.raiden, ContractReceiveNewTokenNetwork, { 'token_network': { 'token_address': token_address, }, }, retry_timeout, ) assert token_address in api1.get_tokens_list(registry_address) partner_address = make_address() api1.channel_open( registry_address=app1.raiden.default_registry.address, token_address=token_address, partner_address=partner_address, ) with pytest.raises(DepositOverLimit): api1.set_total_channel_deposit( registry_address=app1.raiden.default_registry.address, token_address=token_address, partner_address=partner_address, total_deposit=10000000000000000000000, )
def test_token_registered_race(raiden_chain, token_amount, retry_timeout, contract_manager): """If a token is registered it must appear on the token list. If two nodes register the same token one of the transactions will fail. The node that receives an error for "already registered token" must see the token in the token list. Issue: #784 """ app0, app1 = raiden_chain api0 = RaidenAPI(app0.raiden) api1 = RaidenAPI(app1.raiden) # Recreate the race condition by making sure the non-registering app won't # register at all by watching for the TokenAdded blockchain event. event_listeners = app1.raiden.blockchain_events.event_listeners app1.raiden.blockchain_events.event_listeners = list() token_address = deploy_contract_web3( contract_name=CONTRACT_HUMAN_STANDARD_TOKEN, deploy_client=app1.raiden.chain.client, contract_manager=contract_manager, num_confirmations=None, constructor_arguments=( token_amount, 2, 'raiden', 'Rd', ), ) gevent.sleep(1) registry_address = app0.raiden.default_registry.address assert token_address not in api0.get_tokens_list(registry_address) assert token_address not in api1.get_tokens_list(registry_address) api0.token_network_register(registry_address, token_address) gevent.sleep(1) assert token_address in api0.get_tokens_list(registry_address) assert token_address not in api1.get_tokens_list(registry_address) # The next time when the event is polled, the token is registered app1.raiden.blockchain_events.event_listeners = event_listeners waiting.wait_for_block( app1.raiden, app1.raiden.get_block_number() + 1, retry_timeout, ) assert token_address in api1.get_tokens_list(registry_address)
def test_set_deposit_limit_crash(raiden_network, token_amount, contract_manager, retry_timeout): """The development contracts as of 10/12/2018 were crashing if more than an amount was given Regression test for https://github.com/raiden-network/raiden/issues/3135 """ app1 = raiden_network[0] registry_address = app1.raiden.default_registry.address token_address = deploy_contract_web3( contract_name=CONTRACT_HUMAN_STANDARD_TOKEN, deploy_client=app1.raiden.chain.client, contract_manager=contract_manager, constructor_arguments=( token_amount, 2, 'raiden', 'Rd', ), ) api1 = RaidenAPI(app1.raiden) assert token_address not in api1.get_tokens_list(registry_address) api1.token_network_register(registry_address, token_address) exception = RuntimeError( 'Did not see the token registration within 30 seconds') with gevent.Timeout(seconds=30, exception=exception): wait_for_state_change( app1.raiden, ContractReceiveNewTokenNetwork, { 'token_network': { 'token_address': token_address, }, }, retry_timeout, ) assert token_address in api1.get_tokens_list(registry_address) partner_address = make_address() api1.channel_open( registry_address=app1.raiden.default_registry.address, token_address=token_address, partner_address=partner_address, ) with pytest.raises(DepositOverLimit): api1.set_total_channel_deposit( registry_address=app1.raiden.default_registry.address, token_address=token_address, partner_address=partner_address, total_deposit=10000000000000000000000, )
def test_set_deposit_limit_crash(raiden_network, token_amount, contract_manager, retry_timeout): """The development contracts as of 10/12/2018 were crashing if more than an amount was given Regression test for https://github.com/raiden-network/raiden/issues/3135 """ app1 = raiden_network[0] registry_address = app1.raiden.default_registry.address token_address = deploy_contract_web3( contract_name=CONTRACT_HUMAN_STANDARD_TOKEN, deploy_client=app1.raiden.chain.client, contract_manager=contract_manager, constructor_arguments=( token_amount, 2, 'raiden', 'Rd', ), ) api1 = RaidenAPI(app1.raiden) assert token_address not in api1.get_tokens_list(registry_address) api1.token_network_register(registry_address, token_address) exception = RuntimeError('Did not see the token registration within 30 seconds') with gevent.Timeout(seconds=30, exception=exception): wait_for_state_change( app1.raiden, ContractReceiveNewTokenNetwork, { 'token_network': { 'token_address': token_address, }, }, retry_timeout, ) assert token_address in api1.get_tokens_list(registry_address) partner_address = make_address() api1.channel_open( registry_address=app1.raiden.default_registry.address, token_address=token_address, partner_address=partner_address, ) with pytest.raises(DepositOverLimit): api1.set_total_channel_deposit( registry_address=app1.raiden.default_registry.address, token_address=token_address, partner_address=partner_address, total_deposit=10000000000000000000000, )
def test_register_token(raiden_network, token_amount, contract_manager, retry_timeout): app1 = raiden_network[0] registry_address = app1.raiden.default_registry.address token_address = deploy_contract_web3( contract_name=CONTRACT_HUMAN_STANDARD_TOKEN, deploy_client=app1.raiden.rpc_client, contract_manager=contract_manager, constructor_arguments=(token_amount, 2, "raiden", "Rd"), ) # Wait until Raiden can start using the token contract. # Here, the block at which the contract was deployed should be confirmed by Raiden. # Therefore, until that block is received. waiting.wait_for_block( raiden=app1.raiden, block_number=app1.raiden.get_block_number() + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + 1, retry_timeout=retry_timeout, ) api1 = RaidenAPI(app1.raiden) assert token_address not in api1.get_tokens_list(registry_address) api1.token_network_register( registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=UINT256_MAX, token_network_deposit_limit=UINT256_MAX, ) exception = RuntimeError("Did not see the token registration within 30 seconds") with gevent.Timeout(seconds=30, exception=exception): wait_for_state_change( app1.raiden, ContractReceiveNewTokenNetwork, {"token_network": {"token_address": token_address}}, retry_timeout, ) assert token_address in api1.get_tokens_list(registry_address) # Exception if we try to reregister with pytest.raises(AlreadyRegisteredTokenAddress): api1.token_network_register( registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=UINT256_MAX, token_network_deposit_limit=UINT256_MAX, )
def run_test_register_token(raiden_network, token_amount, contract_manager, retry_timeout): app1 = raiden_network[0] registry_address = app1.raiden.default_registry.address token_address = deploy_contract_web3( contract_name=CONTRACT_HUMAN_STANDARD_TOKEN, deploy_client=app1.raiden.chain.client, contract_manager=contract_manager, constructor_arguments=( token_amount, 2, 'raiden', 'Rd', ), ) api1 = RaidenAPI(app1.raiden) assert token_address not in api1.get_tokens_list(registry_address) api1.token_network_register( registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=UINT256_MAX, token_network_deposit_limit=UINT256_MAX, ) exception = RuntimeError('Did not see the token registration within 30 seconds') with gevent.Timeout(seconds=30, exception=exception): wait_for_state_change( app1.raiden, ContractReceiveNewTokenNetwork, { 'token_network': { 'token_address': token_address, }, }, retry_timeout, ) assert token_address in api1.get_tokens_list(registry_address) # Exception if we try to reregister with pytest.raises(AlreadyRegisteredTokenAddress): api1.token_network_register( registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=UINT256_MAX, token_network_deposit_limit=UINT256_MAX, )
def test_register_token_insufficient_eth(raiden_network, token_amount): app1 = raiden_network[0] registry_address = app1.raiden.default_registry.address token_address = deploy_contract_web3( CONTRACT_HUMAN_STANDARD_TOKEN, app1.raiden.chain.client, num_confirmations=None, constructor_arguments=( token_amount, 2, 'raiden', 'Rd', ), ) api1 = RaidenAPI(app1.raiden) assert token_address not in api1.get_tokens_list(registry_address) # app1.raiden loses all its ETH because it has been naughty burn_all_eth(app1.raiden) # At this point we should get the InsufficientFunds exception with pytest.raises(InsufficientFunds): api1.token_network_register(registry_address, token_address)
def test_register_token_insufficient_eth(raiden_network, retry_timeout, unregistered_token): app1 = raiden_network[0] registry_address = app1.raiden.default_registry.address token_address = unregistered_token # Wait until Raiden can start using the token contract. # Here, the block at which the contract was deployed should be confirmed by Raiden. # Therefore, until that block is received. waiting.wait_for_block( raiden=app1.raiden, block_number=app1.raiden.get_block_number() + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + 1, retry_timeout=retry_timeout, ) api1 = RaidenAPI(app1.raiden) assert token_address not in api1.get_tokens_list(registry_address) # app1.raiden loses all its ETH because it has been naughty burn_eth(app1.raiden.rpc_client) with pytest.raises(InsufficientEth): api1.token_network_register( registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=TokenAmount(UINT256_MAX), token_network_deposit_limit=TokenAmount(UINT256_MAX), )
def test_register_token_insufficient_eth(raiden_network, token_amount, contract_manager): app1 = raiden_network[0] registry_address = app1.raiden.default_registry.address token_address = deploy_contract_web3( contract_name=CONTRACT_HUMAN_STANDARD_TOKEN, deploy_client=app1.raiden.chain.client, contract_manager=contract_manager, constructor_arguments=( token_amount, 2, 'raiden', 'Rd', ), ) api1 = RaidenAPI(app1.raiden) assert token_address not in api1.get_tokens_list(registry_address) # app1.raiden loses all its ETH because it has been naughty burn_eth(app1.raiden) # At this point we should get an UnrecoverableError due to InsufficientFunds with pytest.raises(InsufficientFunds): api1.token_network_register(registry_address, token_address)
def test_token_registered_race(raiden_chain, token_amount, events_poll_timeout): """If a token is registered it must appear on the token list. If two nodes register the same token one of the transactions will fail. The node that receives an error for "already registered token" must see the token in the token list. Issue: #784 """ app0, app1 = raiden_chain api0 = RaidenAPI(app0.raiden) api1 = RaidenAPI(app1.raiden) # Recreate the race condition by making sure the non-registering app won't # register at all by watching for the TokenAdded blockchain event. event_listeners = app1.raiden.blockchain_events.event_listeners app1.raiden.blockchain_events.event_listeners = list() token_address = app1.raiden.chain.deploy_contract( contract_name='HumanStandardToken', contract_path=get_contract_path('HumanStandardToken.sol'), constructor_parameters=(token_amount, 'raiden', 2, 'Rd'), ) gevent.sleep(1) registry_address = app0.raiden.default_registry.address assert token_address not in api0.get_tokens_list(registry_address) assert token_address not in api1.get_tokens_list(registry_address) api0.token_network_register(registry_address, token_address) gevent.sleep(1) assert token_address in api0.get_tokens_list(registry_address) assert token_address not in api1.get_tokens_list(registry_address) # The next time when the event is polled, the token is registered app1.raiden.blockchain_events.event_listeners = event_listeners waiting.wait_for_block( app1.raiden, app1.raiden.get_block_number() + 1, events_poll_timeout, ) assert token_address in api1.get_tokens_list(registry_address)
def test_register_token(raiden_network: List[RaidenService], retry_timeout, unregistered_token): app1 = raiden_network[0] registry_address = app1.default_registry.address token_address = unregistered_token # Wait until Raiden can start using the token contract. # Here, the block at which the contract was deployed should be confirmed by Raiden. # Therefore, until that block is received. waiting.wait_for_block( raiden=app1, block_number=BlockNumber(app1.get_block_number() + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + 1), retry_timeout=retry_timeout, ) api1 = RaidenAPI(app1) assert token_address not in api1.get_tokens_list(registry_address) api1.token_network_register( registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=TokenAmount(UINT256_MAX), token_network_deposit_limit=TokenAmount(UINT256_MAX), ) exception = RuntimeError( "Did not see the token registration within 30 seconds") with gevent.Timeout(seconds=30, exception=exception): wait_for_state_change( app1, ContractReceiveNewTokenNetwork, {"token_network": { "token_address": token_address }}, retry_timeout, ) assert token_address in api1.get_tokens_list(registry_address) # Exception if we try to reregister with pytest.raises(AlreadyRegisteredTokenAddress): api1.token_network_register( registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=TokenAmount(UINT256_MAX), token_network_deposit_limit=TokenAmount(UINT256_MAX), )
def test_register_token(raiden_network, token_amount): app1 = raiden_network[0] registry_address = app1.raiden.default_registry.address token_address = app1.raiden.chain.deploy_contract( contract_name='HumanStandardToken', contract_path=get_contract_path('HumanStandardToken.sol'), constructor_parameters=(token_amount, 'raiden', 2, 'Rd'), ) api1 = RaidenAPI(app1.raiden) assert token_address not in api1.get_tokens_list(registry_address) api1.token_network_register(registry_address, token_address) assert token_address in api1.get_tokens_list(registry_address) # Exception if we try to reregister with pytest.raises(AlreadyRegisteredTokenAddress): api1.token_network_register(registry_address, token_address)
def test_token_registration(raiden_network, tester_chain): node1 = raiden_network[0] token_amount = 1000 token_address = node1.raiden.chain.deploy_contract( contract_name='HumanStandardToken', contract_path=get_contract_path('HumanStandardToken.sol'), constructor_parameters=(token_amount, 'raiden', 2, 'Rd'), ) api1 = RaidenAPI(node1.raiden) assert not api1.get_tokens_list() assert api1.manager_address_if_token_registered(token_address) is None node1.raiden.poll_blockchain_events() assert not api1.get_tokens_list() api1.register_token(token_address) assert api1.manager_address_if_token_registered(token_address) is not None assert api1.get_tokens_list() == [token_address]
def test_register_token(raiden_network, token_amount, contract_manager, retry_timeout): app1 = raiden_network[0] registry_address = app1.raiden.default_registry.address token_address = deploy_contract_web3( contract_name=CONTRACT_HUMAN_STANDARD_TOKEN, deploy_client=app1.raiden.chain.client, contract_manager=contract_manager, constructor_arguments=( token_amount, 2, 'raiden', 'Rd', ), ) api1 = RaidenAPI(app1.raiden) assert token_address not in api1.get_tokens_list(registry_address) api1.token_network_register(registry_address, token_address) exception = RuntimeError('Did not see the token registration within 30 seconds') with gevent.Timeout(seconds=30, exception=exception): wait_for_state_change( app1.raiden, ContractReceiveNewTokenNetwork, { 'token_network': { 'token_address': token_address, }, }, retry_timeout, ) assert token_address in api1.get_tokens_list(registry_address) # Exception if we try to reregister with pytest.raises(AlreadyRegisteredTokenAddress): api1.token_network_register(registry_address, token_address)
def test_token_addresses(raiden_network, token_addresses): node1, node2 = raiden_network token_address = token_addresses[0] api1 = RaidenAPI(node1.raiden) api2 = RaidenAPI(node2.raiden) assert api1.address == node1.raiden.address assert set(api1.tokens) == set(token_addresses) assert set(api1.get_tokens_list()) == set(token_addresses) channels = api1.get_channel_list(token_address, api2.address) assert api1.get_channel_list(token_address) == channels assert len(api1.get_channel_list()) == 2 assert api1.get_node_network_state(api2.address) == NODE_NETWORK_REACHABLE
def test_register_token_insufficient_eth( raiden_network, token_amount, contract_manager, retry_timeout ): app1 = raiden_network[0] registry_address = app1.raiden.default_registry.address token_address = deploy_contract_web3( contract_name=CONTRACT_HUMAN_STANDARD_TOKEN, deploy_client=app1.raiden.rpc_client, contract_manager=contract_manager, constructor_arguments=(token_amount, 2, "raiden", "Rd"), ) # Wait until Raiden can start using the token contract. # Here, the block at which the contract was deployed should be confirmed by Raiden. # Therefore, until that block is received. waiting.wait_for_block( raiden=app1.raiden, block_number=app1.raiden.get_block_number() + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + 1, retry_timeout=retry_timeout, ) api1 = RaidenAPI(app1.raiden) assert token_address not in api1.get_tokens_list(registry_address) # app1.raiden loses all its ETH because it has been naughty burn_eth(app1.raiden.rpc_client) # At this point we should get an UnrecoverableError due to InsufficientFunds with pytest.raises(InsufficientFunds): api1.token_network_register( registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=UINT256_MAX, token_network_deposit_limit=UINT256_MAX, )
def run_test_token_addresses(raiden_network: List[RaidenService], token_addresses): app = raiden_network[0] api = RaidenAPI(app) registry_address = app.default_registry.address assert set(api.get_tokens_list(registry_address)) == set(token_addresses)
def test_token_addresses(raiden_network, token_addresses): app = raiden_network[0] api = RaidenAPI(app.raiden) registry_address = app.raiden.default_registry.address assert set(api.get_tokens_list(registry_address)) == set(token_addresses)
def test_token_registered_race(raiden_chain, token_amount, retry_timeout, contract_manager): """If a token is registered it must appear on the token list. If two nodes register the same token one of the transactions will fail. The node that receives an error for "already registered token" must see the token in the token list. Issue: #784 """ app0, app1 = raiden_chain api0 = RaidenAPI(app0.raiden) api1 = RaidenAPI(app1.raiden) # Recreate the race condition by making sure the non-registering app won't # register at all by watching for the TokenAdded blockchain event. event_listeners = app1.raiden.blockchain_events.event_listeners app1.raiden.blockchain_events.event_listeners = list() token_address = deploy_contract_web3( contract_name=CONTRACT_HUMAN_STANDARD_TOKEN, deploy_client=app1.raiden.rpc_client, contract_manager=contract_manager, constructor_arguments=(token_amount, 2, "raiden", "Rd"), ) # Wait until Raiden can start using the token contract. # Here, the block at which the contract was deployed should be confirmed by Raiden. # Therefore, until that block is received. waiting.wait_for_block( raiden=app0.raiden, block_number=app0.raiden.get_block_number() + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + 1, retry_timeout=retry_timeout, ) waiting.wait_for_block( raiden=app1.raiden, block_number=app1.raiden.get_block_number() + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + 1, retry_timeout=retry_timeout, ) registry_address = app0.raiden.default_registry.address assert token_address not in api0.get_tokens_list(registry_address) assert token_address not in api1.get_tokens_list(registry_address) api0.token_network_register( registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=UINT256_MAX, token_network_deposit_limit=UINT256_MAX, ) exception = RuntimeError("Did not see the token registration within 30 seconds") with gevent.Timeout(seconds=30, exception=exception): wait_for_state_change( app0.raiden, ContractReceiveNewTokenNetwork, {"token_network": {"token_address": token_address}}, retry_timeout, ) assert token_address in api0.get_tokens_list(registry_address) assert token_address not in api1.get_tokens_list(registry_address) # The next time when the event is polled, the token is registered app1.raiden.blockchain_events.event_listeners = event_listeners waiting.wait_for_block(app1.raiden, app1.raiden.get_block_number() + 1, retry_timeout) assert token_address in api1.get_tokens_list(registry_address)
def test_token_addresses(raiden_network, token_addresses): app = raiden_network[0] api = RaidenAPI(app.raiden) assert set(api.get_tokens_list()) == set(token_addresses)
def test_token_registered_race(raiden_chain, token_amount, retry_timeout, contract_manager): """If a token is registered it must appear on the token list. If two nodes register the same token one of the transactions will fail. The node that receives an error for "already registered token" must see the token in the token list. Issue: #784 """ app0, app1 = raiden_chain api0 = RaidenAPI(app0.raiden) api1 = RaidenAPI(app1.raiden) # Recreate the race condition by making sure the non-registering app won't # register at all by watching for the TokenAdded blockchain event. event_listeners = app1.raiden.blockchain_events.event_listeners app1.raiden.blockchain_events.event_listeners = list() token_address = deploy_contract_web3( contract_name=CONTRACT_HUMAN_STANDARD_TOKEN, deploy_client=app1.raiden.chain.client, contract_manager=contract_manager, constructor_arguments=( token_amount, 2, 'raiden', 'Rd', ), ) gevent.sleep(1) registry_address = app0.raiden.default_registry.address assert token_address not in api0.get_tokens_list(registry_address) assert token_address not in api1.get_tokens_list(registry_address) api0.token_network_register(registry_address, token_address) exception = RuntimeError('Did not see the token registration within 30 seconds') with gevent.Timeout(seconds=30, exception=exception): wait_for_state_change( app0.raiden, ContractReceiveNewTokenNetwork, { 'token_network': { 'token_address': token_address, }, }, retry_timeout, ) assert token_address in api0.get_tokens_list(registry_address) assert token_address not in api1.get_tokens_list(registry_address) # The next time when the event is polled, the token is registered app1.raiden.blockchain_events.event_listeners = event_listeners waiting.wait_for_block( app1.raiden, app1.raiden.get_block_number() + 1, retry_timeout, ) assert token_address in api1.get_tokens_list(registry_address)
def test_deposit_amount_must_be_smaller_than_the_token_network_limit( raiden_network: List[App], contract_manager: ContractManager, retry_timeout: float) -> None: """The Python API must properly check the requested deposit will not exceed the token network deposit limit. This is a regression test for #3135. As of version `v0.18.1` (commit 786347b23), the proxy was not properly checking that the requested deposit amount was smaller than the smart contract deposit limit. This led to two errors: - The error message was vague and incorrect: "Deposit amount decreased" - The exception used was not handled and crashed the node. This test checks the limit is properly check from the REST API. """ app1 = raiden_network[0] registry_address = app1.raiden.default_registry.address token_supply = 1_000_000 token_address = TokenAddress( deploy_contract_web3( contract_name=CONTRACT_HUMAN_STANDARD_TOKEN, deploy_client=app1.raiden.rpc_client, contract_manager=contract_manager, constructor_arguments=(token_supply, 2, "raiden", "Rd"), )) # Wait until Raiden can start using the token contract. # Here, the block at which the contract was deployed should be confirmed by Raiden. # Therefore, until that block is received. waiting.wait_for_block( raiden=app1.raiden, block_number=BlockNumber(app1.raiden.get_block_number() + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + 1), retry_timeout=retry_timeout, ) api1 = RaidenAPI(app1.raiden) msg = "Token is not registered yet, it must not be in the token list." assert token_address not in api1.get_tokens_list(registry_address), msg token_network_deposit_limit = TokenAmount(100) api1.token_network_register( registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=token_network_deposit_limit, token_network_deposit_limit=token_network_deposit_limit, ) exception = RuntimeError( "Did not see the token registration within 30 seconds") with gevent.Timeout(seconds=30, exception=exception): wait_for_state_change( app1.raiden, ContractReceiveNewTokenNetwork, {"token_network": { "token_address": token_address }}, retry_timeout, ) msg = "Token has been registered, yet must be available in the token list." assert token_address in api1.get_tokens_list(registry_address), msg partner_address = make_address() api1.channel_open( registry_address=app1.raiden.default_registry.address, token_address=token_address, partner_address=partner_address, ) with pytest.raises(DepositOverLimit): api1.set_total_channel_deposit( registry_address=app1.raiden.default_registry.address, token_address=token_address, partner_address=partner_address, total_deposit=TokenAmount(token_network_deposit_limit + 1), ) pytest.fail( "The deposit must fail if the requested deposit exceeds the token " "network deposit limit.")
def test_participant_deposit_amount_must_be_smaller_than_the_limit( raiden_network: List[App], contract_manager: ContractManager, retry_timeout: float) -> None: """The Python API must properly check the requested participant deposit will not exceed the smart contract limit. This is companion test for `test_deposit_amount_must_be_smaller_than_the_token_network_limit`. The participant deposit limit was introduced for the bug bounty with the PR https://github.com/raiden-network/raiden-contracts/pull/276/ , the limit is available since version 0.4.0 of the smart contract. """ app1 = raiden_network[0] registry_address = app1.raiden.default_registry.address token_supply = 1_000_000 token_address = TokenAddress( deploy_contract_web3( contract_name=CONTRACT_HUMAN_STANDARD_TOKEN, deploy_client=app1.raiden.rpc_client, contract_manager=contract_manager, constructor_arguments=(token_supply, 2, "raiden", "Rd"), )) api1 = RaidenAPI(app1.raiden) msg = "Token is not registered yet, it must not be in the token list." assert token_address not in api1.get_tokens_list(registry_address), msg # Wait until Raiden can start using the token contract. # Here, the block at which the contract was deployed should be confirmed by Raiden. # Therefore, until that block is received. waiting.wait_for_block( raiden=app1.raiden, block_number=BlockNumber(app1.raiden.get_block_number() + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + 1), retry_timeout=retry_timeout, ) token_network_participant_deposit_limit = TokenAmount(100) api1.token_network_register( registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit= token_network_participant_deposit_limit, token_network_deposit_limit=TokenAmount(UINT256_MAX), ) exception = RuntimeError( "Did not see the token registration within 30 seconds") with gevent.Timeout(seconds=30, exception=exception): wait_for_state_change( app1.raiden, ContractReceiveNewTokenNetwork, {"token_network": { "token_address": token_address }}, retry_timeout, ) msg = "Token has been registered, yet must be available in the token list." assert token_address in api1.get_tokens_list(registry_address), msg partner_address = make_address() api1.channel_open( registry_address=app1.raiden.default_registry.address, token_address=token_address, partner_address=partner_address, ) with pytest.raises(DepositOverLimit): api1.set_total_channel_deposit( registry_address=app1.raiden.default_registry.address, token_address=token_address, partner_address=partner_address, total_deposit=TokenAmount(token_network_participant_deposit_limit + 1), ) pytest.fail( "The deposit must fail if the requested deposit exceeds the participant deposit limit." )
def test_token_registered_race(raiden_chain, retry_timeout, unregistered_token): """If a token is registered it must appear on the token list. If two nodes register the same token one of the transactions will fail. The node that receives an error for "already registered token" must see the token in the token list. Issue: #784 """ app0, app1 = raiden_chain token_address = unregistered_token api0 = RaidenAPI(app0.raiden) api1 = RaidenAPI(app1.raiden) # Wait until Raiden can start using the token contract. # Here, the block at which the contract was deployed should be confirmed by Raiden. # Therefore, until that block is received. waiting.wait_for_block( raiden=app0.raiden, block_number=app0.raiden.get_block_number() + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + 1, retry_timeout=retry_timeout, ) waiting.wait_for_block( raiden=app1.raiden, block_number=app1.raiden.get_block_number() + DEFAULT_NUMBER_OF_BLOCK_CONFIRMATIONS + 1, retry_timeout=retry_timeout, ) registry_address = app0.raiden.default_registry.address assert token_address not in api0.get_tokens_list(registry_address) assert token_address not in api1.get_tokens_list(registry_address) greenlets: set = { gevent.spawn( api0.token_network_register, registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=TokenAmount(UINT256_MAX), token_network_deposit_limit=TokenAmount(UINT256_MAX), ), gevent.spawn( api0.token_network_register, registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=TokenAmount(UINT256_MAX), token_network_deposit_limit=TokenAmount(UINT256_MAX), ), } # One of the nodes will lose the race with pytest.raises(RaidenRecoverableError): gevent.joinall(greenlets, raise_error=True) exception = RuntimeError( "Did not see the token registration within 30 seconds") with gevent.Timeout(seconds=30, exception=exception): wait_for_state_change( app0.raiden, ContractReceiveNewTokenNetwork, {"token_network": { "token_address": token_address }}, retry_timeout, ) wait_for_state_change( app1.raiden, ContractReceiveNewTokenNetwork, {"token_network": { "token_address": token_address }}, retry_timeout, ) assert token_address in api0.get_tokens_list(registry_address) assert token_address in api1.get_tokens_list(registry_address) for api in (api0, api1): with pytest.raises(AlreadyRegisteredTokenAddress): api.token_network_register( registry_address=registry_address, token_address=token_address, channel_participant_deposit_limit=TokenAmount(UINT256_MAX), token_network_deposit_limit=TokenAmount(UINT256_MAX), )