Esempio n. 1
0
def raiden_network(request, token_addresses, channels_per_node, deposit,
                   settle_timeout, blockchain_services, raiden_udp_ports,
                   transport_class, send_ping_time, max_unresponsive_time,
                   cached_genesis, reveal_timeout, database_paths):

    verbosity = request.config.option.verbose

    raiden_apps = create_apps(
        blockchain_services.blockchain_services,
        raiden_udp_ports,
        transport_class,
        verbosity,
        send_ping_time,
        max_unresponsive_time,
        reveal_timeout,
        settle_timeout,
        database_paths,
    )

    if not cached_genesis:
        create_network_channels(raiden_apps, token_addresses,
                                channels_per_node, deposit, settle_timeout)

    for app in raiden_apps:
        app.raiden.register_registry(app.raiden.chain.default_registry.address)

    _raiden_cleanup(request, raiden_apps)

    return raiden_apps
def raiden_network(
        request,
        token_addresses,
        channels_per_node,
        deposit,
        settle_timeout,
        blockchain_services,
        endpoint_discovery_services,
        raiden_udp_ports,
        transport_class,
        cached_genesis,
        reveal_timeout,
        database_paths,
        retry_interval,
        retries_before_backoff,
        throttle_capacity,
        throttle_fill_rate,
        nat_invitation_timeout,
        nat_keepalive_retries,
        nat_keepalive_timeout):

    raiden_apps = create_apps(
        blockchain_services.blockchain_services,
        endpoint_discovery_services,
        blockchain_services.deploy_registry.address,
        raiden_udp_ports,
        transport_class,
        reveal_timeout,
        settle_timeout,
        database_paths,
        retry_interval,
        retries_before_backoff,
        throttle_capacity,
        throttle_fill_rate,
        nat_invitation_timeout,
        nat_keepalive_retries,
        nat_keepalive_timeout,
    )

    if not cached_genesis:
        create_network_channels(
            raiden_apps,
            token_addresses,
            channels_per_node,
            deposit,
            settle_timeout
        )

    wait_for_partners(raiden_apps)
    _raiden_cleanup(request, raiden_apps)

    # The block_number is primed on the app creation, but after the app is
    # created all the channels are deployed, for the tester implementation this
    # will advance the block_number with synchronous execution, making the
    # apps' block_number to greatly fall behind.
    if not cached_genesis:
        for app in raiden_apps:
            app.raiden.alarm.poll_for_new_block()

    return raiden_apps
def raiden_network(
        request,
        token_addresses,
        channels_per_node,
        deposit,
        settle_timeout,
        blockchain_services,
        endpoint_discovery_services,
        raiden_udp_ports,
        transport_class,
        cached_genesis,
        reveal_timeout,
        database_paths,
        retry_interval,
        retries_before_backoff,
        throttle_capacity,
        throttle_fill_rate,
        nat_invitation_timeout,
        nat_keepalive_retries,
        nat_keepalive_timeout):

    raiden_apps = create_apps(
        blockchain_services.blockchain_services,
        endpoint_discovery_services,
        blockchain_services.deploy_registry.address,
        raiden_udp_ports,
        transport_class,
        reveal_timeout,
        settle_timeout,
        database_paths,
        retry_interval,
        retries_before_backoff,
        throttle_capacity,
        throttle_fill_rate,
        nat_invitation_timeout,
        nat_keepalive_retries,
        nat_keepalive_timeout,
    )

    if not cached_genesis:
        create_network_channels(
            raiden_apps,
            token_addresses,
            channels_per_node,
            deposit,
            settle_timeout
        )

    _raiden_cleanup(request, raiden_apps)

    # The block_number is primed on the app creation, but after the app is
    # created all the channels are deployed, for the tester implementation this
    # will advance the block_number with synchronous execution, making the
    # apps' block_number to greatly fall behind.
    if not cached_genesis:
        for app in raiden_apps:
            app.raiden.alarm.poll_for_new_block()

    return raiden_apps
Esempio n. 4
0
def raiden_network(
        request,
        token_addresses,
        channels_per_node,
        deposit,
        settle_timeout,
        blockchain_services,
        raiden_udp_ports,
        transport_class,
        send_ping_time,
        max_unresponsive_time,
        cached_genesis,
        reveal_timeout,
        database_paths):

    verbosity = request.config.option.verbose

    raiden_apps = create_apps(
        blockchain_services.blockchain_services,
        raiden_udp_ports,
        transport_class,
        verbosity,
        send_ping_time,
        max_unresponsive_time,
        reveal_timeout,
        settle_timeout,
        database_paths,
    )

    if not cached_genesis:
        create_network_channels(
            raiden_apps,
            token_addresses,
            channels_per_node,
            deposit,
            settle_timeout
        )

    for app in raiden_apps:
        app.raiden.register_registry(app.raiden.chain.default_registry.address)

    _raiden_cleanup(request, raiden_apps)

    # The block_number is primed on the app creation, but after the app is
    # created all the channels are deployed, for the tester implementation this
    # will advance the block_number with synchronous execution, making the
    # apps' block_number to greatly fall behind.
    if not cached_genesis:
        for app in raiden_apps:
            app.raiden.alarm.poll_for_new_block()

    return raiden_apps
Esempio n. 5
0
def raiden_network(
        request,
        assets_addresses,
        channels_per_node,
        deposit,
        settle_timeout,
        blockchain_services,
        transport_class,
        send_ping_time,
        max_unresponsive_time,
        cached_genesis):

    verbosity = request.config.option.verbose

    raiden_apps = create_apps(
        blockchain_services.blockchain_services,
        transport_class,
        verbosity,
        send_ping_time,
        max_unresponsive_time
    )

    if not cached_genesis:
        create_network_channels(
            raiden_apps,
            assets_addresses,
            channels_per_node,
            deposit,
            settle_timeout,
        )

    for app in raiden_apps:
        app.raiden.register_registry(app.raiden.chain.default_registry)

    _raiden_cleanup(request, raiden_apps)

    return raiden_apps
Esempio n. 6
0
def raiden_network(
    token_addresses,
    token_network_registry_address,
    one_to_n_address,
    channels_per_node,
    deposit,
    settle_timeout,
    chain_id,
    blockchain_services,
    endpoint_discovery_services,
    raiden_udp_ports,
    reveal_timeout,
    retry_interval,
    retries_before_backoff,
    throttle_capacity,
    throttle_fill_rate,
    nat_invitation_timeout,
    nat_keepalive_retries,
    nat_keepalive_timeout,
    environment_type,
    unrecoverable_error_should_crash,
    local_matrix_servers,
    private_rooms,
    blockchain_type,
    contracts_path,
    user_deposit_address,
    global_rooms,
    tmpdir,
    request,
):
    service_registry_address = None
    if blockchain_services.service_registry:
        service_registry_address = blockchain_services.service_registry.address

    if _ETH_LOGDIR:
        base_datadir = os.path.join(_ETH_LOGDIR, request.node.name, "raiden_nodes")
    else:
        base_datadir = os.path.join(tmpdir.strpath, "raiden_nodes")

    raiden_apps = create_apps(
        chain_id=chain_id,
        contracts_path=contracts_path,
        blockchain_services=blockchain_services.blockchain_services,
        endpoint_discovery_services=endpoint_discovery_services,
        token_network_registry_address=token_network_registry_address,
        secret_registry_address=blockchain_services.secret_registry.address,
        service_registry_address=service_registry_address,
        one_to_n_address=one_to_n_address,
        user_deposit_address=user_deposit_address,
        raiden_udp_ports=raiden_udp_ports,
        reveal_timeout=reveal_timeout,
        settle_timeout=settle_timeout,
        database_basedir=base_datadir,
        retry_interval=retry_interval,
        retries_before_backoff=retries_before_backoff,
        throttle_capacity=throttle_capacity,
        throttle_fill_rate=throttle_fill_rate,
        nat_invitation_timeout=nat_invitation_timeout,
        nat_keepalive_retries=nat_keepalive_retries,
        nat_keepalive_timeout=nat_keepalive_timeout,
        environment_type=environment_type,
        unrecoverable_error_should_crash=unrecoverable_error_should_crash,
        local_matrix_url=local_matrix_servers[0],
        private_rooms=private_rooms,
        global_rooms=global_rooms,
    )

    confirmed_block = raiden_apps[0].raiden.confirmation_blocks + 1
    blockchain_services.deploy_service.wait_until_block(target_block_number=confirmed_block)

    parallel_start_apps(raiden_apps)

    exception = RuntimeError("`raiden_chain` fixture setup failed, token networks unavailable")
    with gevent.Timeout(seconds=timeout(blockchain_type), exception=exception):
        wait_for_token_networks(
            raiden_apps=raiden_apps,
            token_network_registry_address=token_network_registry_address,
            token_addresses=token_addresses,
        )

    app_channels = create_network_channels(raiden_apps, channels_per_node)

    create_all_channels_for_network(
        app_channels=app_channels,
        token_addresses=token_addresses,
        channel_individual_deposit=deposit,
        channel_settle_timeout=settle_timeout,
    )

    exception = RuntimeError("`raiden_network` fixture setup failed, nodes are unreachable")
    with gevent.Timeout(seconds=timeout(blockchain_type), exception=exception):
        wait_for_channels(
            app_channels, blockchain_services.deploy_registry.address, token_addresses, deposit
        )

    # Force blocknumber update
    exception = RuntimeError("Alarm failed to start and set up start_block correctly")

    with gevent.Timeout(seconds=5, exception=exception):
        wait_for_alarm_start(raiden_apps)

    yield raiden_apps

    shutdown_apps_and_cleanup_tasks(raiden_apps)
Esempio n. 7
0
def cached_genesis(request, blockchain_type):
    """
    Deploy all contracts that are required by the fixtures into a tester and
    then serialize the accounts into a genesis block.

    Returns:
        dict: A dictionary representing the genesis block.
    """

    if not request.config.option.blockchain_cache:
        return

    if blockchain_type != 'geth':
        return

    # this will create the tester _and_ deploy the Registry
    deploy_key = request.getfixturevalue('deploy_key')
    private_keys = request.getfixturevalue('private_keys')
    deploy_service, blockchain_services = _tester_services(
        deploy_key,
        private_keys,
        request.getfixturevalue('tester_blockgas_limit'),
    )

    # create_network only registers the tokens,
    # the contracts must be deployed previously
    register = True
    participants = [privatekey_to_address(privatekey) for privatekey in private_keys]
    token_contract_addresses = _token_addresses(
        request.getfixturevalue('token_amount'),
        request.getfixturevalue('number_of_tokens'),
        deploy_service,
        participants,
        register
    )

    endpoint_discovery_address = deploy_service.deploy_contract(
        'EndpointRegistry',
        'EndpointRegistry.sol',
    )

    endpoint_discovery_services = [
        ContractDiscovery(
            chain.node_address,
            chain.discovery(endpoint_discovery_address),
        )
        for chain in blockchain_services
    ]

    raiden_apps = create_apps(
        blockchain_services,
        endpoint_discovery_services,
        request.getfixturevalue('raiden_udp_ports'),
        DummyTransport,  # Do not use a UDP server to avoid port reuse in MacOSX
        request.getfixturevalue('reveal_timeout'),
        request.getfixturevalue('settle_timeout'),
        request.getfixturevalue('database_paths'),
        request.getfixturevalue('retry_interval'),
        request.getfixturevalue('retries_before_backoff'),
        request.getfixturevalue('throttle_capacity'),
        request.getfixturevalue('throttle_fill_rate'),
        request.getfixturevalue('nat_invitation_timeout'),
        request.getfixturevalue('nat_keepalive_retries'),
        request.getfixturevalue('nat_keepalive_timeout'),
    )

    if 'raiden_network' in request.fixturenames:
        create_network_channels(
            raiden_apps,
            token_contract_addresses,
            request.getfixturevalue('channels_per_node'),
            request.getfixturevalue('deposit'),
            request.getfixturevalue('settle_timeout'),
        )

    elif 'raiden_chain' in request.fixturenames:
        create_sequential_channels(
            raiden_apps,
            token_contract_addresses[0],
            request.getfixturevalue('channels_per_node'),
            request.getfixturevalue('deposit'),
            request.getfixturevalue('settle_timeout'),
        )

    # else: a test that is not creating channels

    for app in raiden_apps:
        app.stop(leave_channels=False)

    # save the state from the last block into a genesis dict
    tester = blockchain_services[0].tester_state
    tester.mine()
    registry_address = blockchain_services[0].default_registry.address

    genesis_alloc = dict()
    for account_address in tester.block.state.to_dict():
        account_alloc = tester.block.account_to_dict(account_address)

        # Both keys and values of the account storage associative array
        # must now be encoded with 64 hex digits
        if account_alloc['storage']:
            account_alloc['storage'] = fix_tester_storage(account_alloc['storage'])

        # code must be hex encoded with 0x prefix
        account_alloc['code'] = account_alloc.get('code', '')

        # account_to_dict returns accounts with nonce=0 and the nonce must
        # be encoded with 16 hex digits
        account_alloc['nonce'] = '0x%016x' % tester.block.get_nonce(account_address)

        genesis_alloc[account_address] = account_alloc

    all_keys = set(private_keys)
    all_keys.add(deploy_key)
    all_keys = sorted(all_keys)
    account_addresses = [
        privatekey_to_address(key)
        for key in all_keys
    ]

    for address in account_addresses:
        genesis_alloc[address]['balance'] = DEFAULT_BALANCE_BIN

    alloc = {
        address_encoder(address_maybe_bin): data
        for address_maybe_bin, data in genesis_alloc.iteritems()
    }

    genesis = GENESIS_STUB.copy()
    genesis['config']['clique'] = {'period': 1, 'epoch': 30000}

    random_marker = request.getfixturevalue('random_marker')
    genesis['extraData'] = clique_extradata(
        random_marker,
        address_encoder(account_addresses[0])[2:],
    )
    genesis['alloc'] = alloc
    genesis['config']['defaultDiscoveryAddress'] = address_encoder(endpoint_discovery_address)
    genesis['config']['defaultRegistryAddress'] = address_encoder(registry_address)
    genesis['config']['tokenAddresses'] = [
        address_encoder(token_address)
        for token_address in token_contract_addresses
    ]

    return genesis
Esempio n. 8
0
def test_webui():  # pylint: disable=too-many-locals
    num_assets = 3
    num_nodes = 10
    verbose = 0
    settle_timeout = DEFAULT_SETTLE_TIMEOUT

    assets_addresses = [
        sha3('webui:asset:{}'.format(number))[:20]
        for number in range(num_assets)
    ]

    private_keys = [
        sha3('webui:{}'.format(position))
        for position in range(num_nodes)
    ]

    BlockChainServiceMock.reset()
    blockchain_service = BlockChainServiceMock(None, MOCK_REGISTRY_ADDRESS)
    registry = blockchain_service.registry(MOCK_REGISTRY_ADDRESS)

    for asset in assets_addresses:
        registry.add_asset(asset)

    channels_per_node = 2
    deposit = 100

    blockchain_services = [
        BlockChainServiceMock(privkey, MOCK_REGISTRY_ADDRESS)
        for privkey in private_keys
    ]

    app_list = create_apps(
        blockchain_services,
        UDPTransport,
        verbose,
    )

    create_network_channels(
        app_list,
        assets_addresses,
        channels_per_node,
        deposit,
        settle_timeout,
    )

    app0 = app_list[0]

    addresses = [
        app.raiden.address.encode('hex')
        for app in app_list
        if app != app_list[0]
    ]

    print '\nCreated nodes: \n',
    for node in addresses:
        print node

    setup_messages_cb()

    app0_assets = getattr(app0.raiden.api, 'assets')
    print '\nAvailable assets:'
    for asset in app0_assets:
        print asset.encode('hex')
    print '\n'

    wamp = WAMPRouter(app0.raiden, 8080, ['channel', 'test'])
    wamp.run()

    BlockChainServiceMock.reset()
Esempio n. 9
0
def cached_genesis(request, blockchain_type):
    """
    Deploy all contracts that are required by the fixtures into a tester and
    then serialize the accounts into a genesis block.

    Returns:
        dict: A dictionary representing the genesis block.
    """

    if not request.config.option.blockchain_cache:
        return

    # cannot cache for mock blockchain
    if blockchain_type == 'mock':
        return

    # this will create the tester _and_ deploy the Registry
    deploy_key = request.getfixturevalue('deploy_key')
    private_keys = request.getfixturevalue('private_keys')
    deploy_service, blockchain_services = _tester_services(
        deploy_key,
        private_keys,
        request.getfixturevalue('tester_blockgas_limit'),
    )

    # create_network only registers the tokens,
    # the contracts must be deployed previously
    token_contract_addresses = _tokens_addresses(
        request.getfixturevalue('token_amount'),
        request.getfixturevalue('number_of_tokens'),
        deploy_service,
        blockchain_services,
    )

    raiden_apps = create_apps(
        blockchain_services,
        request.getfixturevalue('raiden_udp_ports'),
        DummyTransport,  # Do not use a UDP server to avoid port reuse in MacOSX
        request.config.option.verbose,
        request.getfixturevalue('send_ping_time'),
        request.getfixturevalue('max_unresponsive_time'),
        request.getfixturevalue('reveal_timeout'),
    )

    if 'raiden_network' in request.fixturenames:
        create_network_channels(
            raiden_apps,
            token_contract_addresses,
            request.getfixturevalue('channels_per_node'),
            request.getfixturevalue('deposit'),
            request.getfixturevalue('settle_timeout'),
        )

    elif 'raiden_chain' in request.fixturenames:
        create_sequential_channels(
            raiden_apps,
            token_contract_addresses[0],
            request.getfixturevalue('channels_per_node'),
            request.getfixturevalue('deposit'),
            request.getfixturevalue('settle_timeout'),
        )

    # else: a test that is not creating channels

    for app in raiden_apps:
        app.stop()

    # save the state from the last block into a genesis dict
    tester = blockchain_services[0].tester_state
    tester.mine()
    registry_address = blockchain_services[0].default_registry.address

    genesis_alloc = dict()
    for account_address in tester.block.state.to_dict():
        account_alloc = tester.block.account_to_dict(account_address)

        # code must be hex encoded without 0x prefix
        account_alloc['code'] = safe_lstrip_hex(account_alloc.get('code', ''))

        # account_to_dict returns accounts with nonce=0
        account_alloc['nonce'] = tester.block.get_nonce(account_address)

        genesis_alloc[account_address] = account_alloc

    account_addresses = [
        privatekey_to_address(key) for key in set(private_keys)
    ]

    for address in account_addresses:
        genesis_alloc[address]['balance'] = DEFAULT_BALANCE_BIN

    alloc = {
        safe_lstrip_hex(address_encoder(address_maybe_bin)): data
        for address_maybe_bin, data in genesis_alloc.iteritems()
    }

    genesis = GENESIS_STUB.copy()
    genesis['alloc'] = alloc
    genesis['config']['defaultRegistryAddress'] = address_encoder(
        registry_address)
    genesis['config']['tokenAddresses'] = [
        address_encoder(token_address)
        for token_address in token_contract_addresses
    ]

    return genesis
Esempio n. 10
0
def raiden_network(
        token_addresses,
        token_network_registry_address,
        channels_per_node,
        deposit,
        settle_timeout,
        chain_id,
        blockchain_services,
        endpoint_discovery_services,
        raiden_udp_ports,
        reveal_timeout,
        database_paths,
        retry_interval,
        retries_before_backoff,
        throttle_capacity,
        throttle_fill_rate,
        nat_invitation_timeout,
        nat_keepalive_retries,
        nat_keepalive_timeout,
        network_type,
        local_matrix_server,
        private_rooms,
):

    raiden_apps = create_apps(
        chain_id=chain_id,
        blockchain_services=blockchain_services.blockchain_services,
        endpoint_discovery_services=endpoint_discovery_services,
        token_network_registry_address=token_network_registry_address,
        secret_registry_address=blockchain_services.secret_registry.address,
        raiden_udp_ports=raiden_udp_ports,
        reveal_timeout=reveal_timeout,
        settle_timeout=settle_timeout,
        database_paths=database_paths,
        retry_interval=retry_interval,
        retries_before_backoff=retries_before_backoff,
        throttle_capacity=throttle_capacity,
        throttle_fill_rate=throttle_fill_rate,
        nat_invitation_timeout=nat_invitation_timeout,
        nat_keepalive_retries=nat_keepalive_retries,
        nat_keepalive_timeout=nat_keepalive_timeout,
        network_type=network_type,
        local_matrix_url=local_matrix_server,
        private_rooms=private_rooms,
    )

    start_tasks = [gevent.spawn(app.raiden.start) for app in raiden_apps]
    gevent.joinall(start_tasks, raise_error=True)

    app_channels = create_network_channels(
        raiden_apps,
        channels_per_node,
    )

    greenlets = []
    for token_address in token_addresses:
        for app_pair in app_channels:
            greenlets.append(gevent.spawn(
                payment_channel_open_and_deposit,
                app_pair[0],
                app_pair[1],
                token_address,
                deposit,
                settle_timeout,
            ))
    gevent.joinall(greenlets, raise_error=True)

    exception = RuntimeError('`raiden_network` fixture setup failed, nodes are unreachable')
    with gevent.Timeout(seconds=30, exception=exception):
        wait_for_channels(
            app_channels,
            blockchain_services.deploy_registry.address,
            token_addresses,
            deposit,
        )

    # Force blocknumber update
    exception = RuntimeError('Alarm failed to start and set up start_block correctly')

    with gevent.Timeout(seconds=5, exception=exception):
        wait_for_alarm_start(raiden_apps)

    yield raiden_apps

    shutdown_apps_and_cleanup_tasks(raiden_apps)
def cached_genesis(request):
    """
    Deploy all contracts that are required by the fixtures into a tester and
    then serialize the accounts into a genesis block.

    Returns:
        dict: A dictionary representing the genesis block.
    """

    if not request.config.option.blockchain_cache:
        return

    # this will create the tester _and_ deploy the Registry
    deploy_key = request.getfixturevalue('deploy_key')
    private_keys = request.getfixturevalue('private_keys')
    registry, deploy_service, blockchain_services = _tester_services(
        deploy_key,
        private_keys,
        request.getfixturevalue('tester_blockgas_limit'),
    )

    registry_address = registry.address

    # create_network only registers the tokens,
    # the contracts must be deployed previously
    register = True
    participants = [privatekey_to_address(privatekey) for privatekey in private_keys]
    token_contract_addresses = _token_addresses(
        request.getfixturevalue('token_amount'),
        request.getfixturevalue('number_of_tokens'),
        deploy_service,
        registry,
        participants,
        register
    )

    endpoint_discovery_address = deploy_service.deploy_contract(
        'EndpointRegistry',
        get_contract_path('EndpointRegistry.sol'),
    )

    endpoint_discovery_services = [
        ContractDiscovery(
            chain.node_address,
            chain.discovery(endpoint_discovery_address),
        )
        for chain in blockchain_services
    ]

    raiden_apps = create_apps(
        blockchain_services,
        endpoint_discovery_services,
        registry_address,
        request.getfixturevalue('raiden_udp_ports'),
        DummyTransport,  # Do not use a UDP server to avoid port reuse in MacOSX
        request.getfixturevalue('reveal_timeout'),
        request.getfixturevalue('settle_timeout'),
        request.getfixturevalue('database_paths'),
        request.getfixturevalue('retry_interval'),
        request.getfixturevalue('retries_before_backoff'),
        request.getfixturevalue('throttle_capacity'),
        request.getfixturevalue('throttle_fill_rate'),
        request.getfixturevalue('nat_invitation_timeout'),
        request.getfixturevalue('nat_keepalive_retries'),
        request.getfixturevalue('nat_keepalive_timeout'),
    )

    if 'raiden_network' in request.fixturenames:
        create_network_channels(
            raiden_apps,
            token_contract_addresses,
            request.getfixturevalue('channels_per_node'),
            request.getfixturevalue('deposit'),
            request.getfixturevalue('settle_timeout'),
        )

    elif 'raiden_chain' in request.fixturenames:
        create_sequential_channels(
            raiden_apps,
            token_contract_addresses[0],
            request.getfixturevalue('channels_per_node'),
            request.getfixturevalue('deposit'),
            request.getfixturevalue('settle_timeout'),
        )

    # else: a test that is not creating channels

    for app in raiden_apps:
        app.stop(leave_channels=False)

    # save the state from the last block into a genesis dict
    tester = blockchain_services[0].tester_chain
    tester.mine()

    genesis_alloc = dict()
    for account_address in tester.head_state.to_dict():
        account_alloc = tester.head_state.account_to_dict(account_address)

        # Both keys and values of the account storage associative array
        # must now be encoded with 64 hex digits
        if account_alloc['storage']:
            account_alloc['storage'] = fix_tester_storage(account_alloc['storage'])

        # code must be hex encoded with 0x prefix
        account_alloc['code'] = account_alloc.get('code', '')

        # account_to_dict returns accounts with nonce=0 and the nonce must
        # be encoded with 16 hex digits
        account_alloc['nonce'] = '0x%016x' % tester.head_state.get_nonce(account_address)

        genesis_alloc[account_address] = account_alloc

    all_keys = set(private_keys)
    all_keys.add(deploy_key)
    all_keys = sorted(all_keys)
    account_addresses = [
        privatekey_to_address(key)
        for key in all_keys
    ]

    for address in account_addresses:
        address_hex = hexlify(address).decode()
        genesis_alloc[address_hex]['balance'] = DEFAULT_BALANCE_BIN

    genesis = GENESIS_STUB.copy()
    genesis['config']['clique'] = {'period': 1, 'epoch': 30000}

    random_marker = request.getfixturevalue('random_marker')
    genesis['extraData'] = clique_extradata(
        random_marker,
        address_encoder(account_addresses[0])[2:],
    )
    genesis['alloc'] = genesis_alloc
    genesis['config']['defaultDiscoveryAddress'] = address_encoder(endpoint_discovery_address)
    genesis['config']['defaultRegistryAddress'] = address_encoder(registry_address)
    genesis['config']['tokenAddresses'] = [
        address_encoder(token_address)
        for token_address in token_contract_addresses
    ]

    return genesis
Esempio n. 12
0
def raiden_network(
        request,
        token_addresses,
        channels_per_node,
        deposit,
        settle_timeout,
        blockchain_services,
        endpoint_discovery_services,
        raiden_udp_ports,
        transport_class,
        cached_genesis,
        reveal_timeout,
        database_paths,
        retry_interval,
        retries_before_backoff,
        throttle_capacity,
        throttle_fill_rate,
        nat_invitation_timeout,
        nat_keepalive_retries,
        nat_keepalive_timeout):

    raiden_apps = create_apps(
        blockchain_services.blockchain_services,
        endpoint_discovery_services,
        blockchain_services.deploy_registry.address,
        raiden_udp_ports,
        transport_class,
        reveal_timeout,
        settle_timeout,
        database_paths,
        retry_interval,
        retries_before_backoff,
        throttle_capacity,
        throttle_fill_rate,
        nat_invitation_timeout,
        nat_keepalive_retries,
        nat_keepalive_timeout,
    )

    app_channels = create_network_channels(
        raiden_apps,
        channels_per_node,
    )

    if not cached_genesis:
        greenlets = []
        for token_address in token_addresses:
            for app_pair in app_channels:
                greenlets.append(gevent.spawn(
                    netting_channel_open_and_deposit,
                    app_pair[0],
                    app_pair[1],
                    token_address,
                    deposit,
                    settle_timeout,
                ))
        gevent.wait(greenlets)

    exception = RuntimeError('fixture setup failed, nodes are unreachable')
    with gevent.Timeout(seconds=30, exception=exception):
        wait_for_channels(
            app_channels,
            blockchain_services.deploy_registry.address,
            token_addresses,
            deposit,
        )

    _raiden_cleanup(request, raiden_apps)

    # Force blocknumber update for the tester backend
    if not cached_genesis:
        for app in raiden_apps:
            app.raiden.alarm.poll_for_new_block()

    return raiden_apps
Esempio n. 13
0
def raiden_network(
        token_addresses,
        token_network_registry_address,
        channels_per_node,
        deposit,
        settle_timeout,
        chain_id,
        blockchain_services,
        endpoint_discovery_services,
        raiden_udp_ports,
        reveal_timeout,
        database_paths,
        retry_interval,
        retries_before_backoff,
        throttle_capacity,
        throttle_fill_rate,
        nat_invitation_timeout,
        nat_keepalive_retries,
        nat_keepalive_timeout,
        environment_type,
        unrecoverable_error_should_crash,
        local_matrix_servers,
        private_rooms,
        retry_timeout,
):

    raiden_apps = create_apps(
        chain_id=chain_id,
        blockchain_services=blockchain_services.blockchain_services,
        endpoint_discovery_services=endpoint_discovery_services,
        token_network_registry_address=token_network_registry_address,
        secret_registry_address=blockchain_services.secret_registry.address,
        raiden_udp_ports=raiden_udp_ports,
        reveal_timeout=reveal_timeout,
        settle_timeout=settle_timeout,
        database_paths=database_paths,
        retry_interval=retry_interval,
        retries_before_backoff=retries_before_backoff,
        throttle_capacity=throttle_capacity,
        throttle_fill_rate=throttle_fill_rate,
        nat_invitation_timeout=nat_invitation_timeout,
        nat_keepalive_retries=nat_keepalive_retries,
        nat_keepalive_timeout=nat_keepalive_timeout,
        environment_type=environment_type,
        unrecoverable_error_should_crash=unrecoverable_error_should_crash,
        local_matrix_url=local_matrix_servers[0],
        private_rooms=private_rooms,
    )

    start_tasks = [gevent.spawn(app.raiden.start) for app in raiden_apps]
    gevent.joinall(start_tasks, raise_error=True)

    exception = RuntimeError('`raiden_chain` fixture setup failed, token networks unavailable')
    with gevent.Timeout(seconds=30, exception=exception):
        wait_for_token_networks(
            raiden_apps=raiden_apps,
            token_network_registry_address=token_network_registry_address,
            token_addresses=token_addresses,
        )

    app_channels = create_network_channels(
        raiden_apps,
        channels_per_node,
    )

    create_all_channels_for_network(
        app_channels=app_channels,
        token_addresses=token_addresses,
        channel_individual_deposit=deposit,
        channel_settle_timeout=settle_timeout,
        token_network_registry_address=token_network_registry_address,
        retry_timeout=retry_timeout,
    )

    exception = RuntimeError('`raiden_network` fixture setup failed, nodes are unreachable')
    with gevent.Timeout(seconds=30, exception=exception):
        wait_for_channels(
            app_channels,
            blockchain_services.deploy_registry.address,
            token_addresses,
            deposit,
        )

    # Force blocknumber update
    exception = RuntimeError('Alarm failed to start and set up start_block correctly')

    with gevent.Timeout(seconds=5, exception=exception):
        wait_for_alarm_start(raiden_apps)

    yield raiden_apps

    shutdown_apps_and_cleanup_tasks(raiden_apps)
Esempio n. 14
0
def cached_genesis(request, blockchain_type):
    """
    Deploy all contracts that are required by the fixtures into a tester and
    then serialize the accounts into a genesis block.

    Returns:
        dict: A dictionary representing the genesis block.
    """

    if not request.config.option.blockchain_cache:
        return

    # cannot cache for mock blockchain
    if blockchain_type == 'mock':
        return

    # this will create the tester _and_ deploy the Registry
    deploy_key = request.getfixturevalue('deploy_key')
    private_keys = request.getfixturevalue('private_keys')
    deploy_service, blockchain_services = _tester_services(
        deploy_key,
        private_keys,
        request.getfixturevalue('tester_blockgas_limit'),
    )

    # create_network only registers the tokens,
    # the contracts must be deployed previously
    register = True
    token_contract_addresses = _token_addresses(
        request.getfixturevalue('token_amount'),
        request.getfixturevalue('number_of_tokens'), deploy_service,
        blockchain_services, register)

    raiden_apps = create_apps(
        blockchain_services,
        request.getfixturevalue('raiden_udp_ports'),
        DummyTransport,  # Do not use a UDP server to avoid port reuse in MacOSX
        request.config.option.verbose,
        request.getfixturevalue('send_ping_time'),
        request.getfixturevalue('max_unresponsive_time'),
        request.getfixturevalue('reveal_timeout'),
        request.getfixturevalue('settle_timeout'),
    )

    if 'raiden_network' in request.fixturenames:
        create_network_channels(
            raiden_apps,
            token_contract_addresses,
            request.getfixturevalue('channels_per_node'),
            request.getfixturevalue('deposit'),
            request.getfixturevalue('settle_timeout'),
        )

    elif 'raiden_chain' in request.fixturenames:
        create_sequential_channels(
            raiden_apps,
            token_contract_addresses[0],
            request.getfixturevalue('channels_per_node'),
            request.getfixturevalue('deposit'),
            request.getfixturevalue('settle_timeout'),
        )

    # else: a test that is not creating channels

    for app in raiden_apps:
        app.stop()

    # save the state from the last block into a genesis dict
    tester = blockchain_services[0].tester_state
    tester.mine()
    registry_address = blockchain_services[0].default_registry.address

    genesis_alloc = dict()
    for account_address in tester.block.state.to_dict():
        account_alloc = tester.block.account_to_dict(account_address)

        # Both keys and values of the account storage associative array
        # must now be encoded with 64 hex digits
        if account_alloc['storage']:
            new_storage = dict()
            for key, val in account_alloc['storage'].iteritems():
                # account_to_dict() from pyethereum can return 0x for a storage
                # position. That is an invalid way of representing 0x0, which we
                # have to take care of here.
                new_key = '0x%064x' % int(key if key != '0x' else '0x0', 16)
                new_val = '0x%064x' % int(val, 16)
                new_storage[new_key] = new_val

            account_alloc['storage'] = new_storage

        # code must be hex encoded with 0x prefix
        account_alloc['code'] = account_alloc.get('code', '')

        # account_to_dict returns accounts with nonce=0 and the nonce must
        # be encoded with 16 hex digits
        account_alloc['nonce'] = '0x%016x' % tester.block.get_nonce(
            account_address)

        genesis_alloc[account_address] = account_alloc

    account_addresses = [
        privatekey_to_address(key) for key in set(private_keys)
    ]

    for address in account_addresses:
        genesis_alloc[address]['balance'] = DEFAULT_BALANCE_BIN

    alloc = {
        address_encoder(address_maybe_bin): data
        for address_maybe_bin, data in genesis_alloc.iteritems()
    }

    genesis = GENESIS_STUB.copy()
    genesis['alloc'] = alloc
    genesis['config']['defaultRegistryAddress'] = address_encoder(
        registry_address)
    genesis['config']['tokenAddresses'] = [
        address_encoder(token_address)
        for token_address in token_contract_addresses
    ]

    return genesis
Esempio n. 15
0
def raiden_network(
        token_addresses,
        token_network_registry_address,
        channels_per_node,
        deposit,
        settle_timeout,
        chain_id,
        blockchain_services,
        endpoint_discovery_services,
        raiden_udp_ports,
        reveal_timeout,
        database_paths,
        retry_interval,
        retries_before_backoff,
        throttle_capacity,
        throttle_fill_rate,
        nat_invitation_timeout,
        nat_keepalive_retries,
        nat_keepalive_timeout,
        local_matrix_server,
):

    raiden_apps = create_apps(
        chain_id,
        blockchain_services.blockchain_services,
        endpoint_discovery_services,
        token_network_registry_address,
        blockchain_services.secret_registry.address,
        raiden_udp_ports,
        reveal_timeout,
        settle_timeout,
        database_paths,
        retry_interval,
        retries_before_backoff,
        throttle_capacity,
        throttle_fill_rate,
        nat_invitation_timeout,
        nat_keepalive_retries,
        nat_keepalive_timeout,
        local_matrix_server,
    )

    app_channels = create_network_channels(
        raiden_apps,
        channels_per_node,
    )

    greenlets = []
    for token_address in token_addresses:
        for app_pair in app_channels:
            greenlets.append(gevent.spawn(
                payment_channel_open_and_deposit,
                app_pair[0],
                app_pair[1],
                token_address,
                deposit,
                settle_timeout,
            ))
    gevent.wait(greenlets)

    exception = RuntimeError('`raiden_network` fixture setup failed, nodes are unreachable')
    with gevent.Timeout(seconds=30, exception=exception):
        wait_for_channels(
            app_channels,
            blockchain_services.deploy_registry.address,
            token_addresses,
            deposit,
        )

    # Force blocknumber update
    exception = RuntimeError('Alarm failed to start and set up start_block correctly')

    with gevent.Timeout(seconds=5, exception=exception):
        wait_for_alarm_start(raiden_apps)

    yield raiden_apps

    shutdown_apps_and_cleanup_tasks(raiden_apps)
Esempio n. 16
0
def raiden_network(
    token_addresses: List[TokenAddress],
    token_network_registry_address: TokenNetworkRegistryAddress,
    one_to_n_address: Optional[OneToNAddress],
    monitoring_service_address: MonitoringServiceAddress,
    channels_per_node: int,
    deposit: TokenAmount,
    settle_timeout: BlockTimeout,
    chain_id: ChainID,
    blockchain_services: BlockchainServices,
    reveal_timeout: BlockTimeout,
    retry_interval_initial: float,
    retry_interval_max: float,
    retries_before_backoff: int,
    environment_type: Environment,
    unrecoverable_error_should_crash: bool,
    local_matrix_servers: List[ParsedURL],
    blockchain_type: str,
    contracts_path: Path,
    user_deposit_address: Optional[UserDepositAddress],
    broadcast_rooms: List[str],
    logs_storage: str,
    register_tokens: bool,
    start_raiden_apps: bool,
    routing_mode: RoutingMode,
    blockchain_query_interval: float,
    resolver_ports: List[Optional[int]],
    enable_rest_api: bool,
    port_generator: Iterator[Port],
    capabilities: CapabilitiesConfig,
) -> Iterable[List[RaidenService]]:
    service_registry_address = None
    if blockchain_services.service_registry:
        service_registry_address = blockchain_services.service_registry.address

    base_datadir = os.path.join(logs_storage, "raiden_nodes")

    raiden_apps = create_apps(
        chain_id=chain_id,
        contracts_path=contracts_path,
        blockchain_services=blockchain_services.blockchain_services,
        token_network_registry_address=token_network_registry_address,
        secret_registry_address=blockchain_services.secret_registry.address,
        service_registry_address=service_registry_address,
        one_to_n_address=one_to_n_address,
        user_deposit_address=user_deposit_address,
        monitoring_service_contract_address=monitoring_service_address,
        reveal_timeout=reveal_timeout,
        settle_timeout=settle_timeout,
        database_basedir=base_datadir,
        retry_interval_initial=retry_interval_initial,
        retry_interval_max=retry_interval_max,
        retries_before_backoff=retries_before_backoff,
        environment_type=environment_type,
        unrecoverable_error_should_crash=unrecoverable_error_should_crash,
        local_matrix_url=local_matrix_servers[0],
        broadcast_rooms=broadcast_rooms,
        routing_mode=routing_mode,
        blockchain_query_interval=blockchain_query_interval,
        resolver_ports=resolver_ports,
        enable_rest_api=enable_rest_api,
        port_generator=port_generator,
        capabilities_config=capabilities,
    )

    confirmed_block = BlockNumber(raiden_apps[0].confirmation_blocks + 1)
    blockchain_services.proxy_manager.client.wait_until_block(target_block_number=confirmed_block)

    if start_raiden_apps:
        parallel_start_apps(raiden_apps)

        if register_tokens:
            exception = RuntimeError(
                "`raiden_chain` fixture setup failed, token networks unavailable"
            )
            with gevent.Timeout(seconds=timeout(blockchain_type), exception=exception):
                wait_for_token_networks(
                    raiden_apps=raiden_apps,
                    token_network_registry_address=token_network_registry_address,
                    token_addresses=token_addresses,
                )

    app_channels = create_network_channels(raiden_apps, channels_per_node)

    create_all_channels_for_network(
        app_channels=app_channels,
        token_addresses=token_addresses,
        channel_individual_deposit=deposit,
        channel_settle_timeout=settle_timeout,
    )

    if start_raiden_apps:
        exception = RuntimeError("`raiden_network` fixture setup failed, nodes are unreachable")
        with gevent.Timeout(seconds=timeout(blockchain_type), exception=exception):
            wait_for_channels(
                app_channels=app_channels,
                token_network_registry_address=blockchain_services.deploy_registry.address,
                token_addresses=token_addresses,
                deposit=deposit,
            )

        # Force blocknumber update
        exception = RuntimeError("Alarm failed to start and set up start_block correctly")

        with gevent.Timeout(seconds=5, exception=exception):
            wait_for_alarm_start(raiden_apps)

    yield raiden_apps

    shutdown_apps_and_cleanup_tasks(raiden_apps)
Esempio n. 17
0
def cached_genesis(request, blockchain_type):
    """
    Deploy all contracts that are required by the fixtures into a tester and
    then serialize the accounts into a genesis block.

    Returns:
        dict: A dictionary representing the genesis block.
    """

    if not request.config.option.blockchain_cache:
        return

    # cannot cache for mock blockchain
    if blockchain_type == 'mock':
        return

    # this will create the tester _and_ deploy the Registry
    deploy_key = request.getfixturevalue('deploy_key')
    private_keys = request.getfixturevalue('private_keys')
    deploy_service, blockchain_services = _tester_services(
        deploy_key,
        private_keys,
        request.getfixturevalue('tester_blockgas_limit'),
    )

    # create_network only register the assets, the contracts must be deployed
    # previously
    asset_contract_addresses = _assets_addresses(
        request.getfixturevalue('asset_amount'),
        request.getfixturevalue('number_of_assets'),
        deploy_service,
        blockchain_services,
    )

    raiden_apps = create_apps(
        blockchain_services,
        request.getfixturevalue('transport_class'),
        request.config.option.verbose,
        request.getfixturevalue('send_ping_time'),
        request.getfixturevalue('max_unresponsive_time'),
    )

    if 'raiden_network' in request.fixturenames:
        create_network_channels(
            raiden_apps,
            asset_contract_addresses,
            request.getfixturevalue('channels_per_node'),
            request.getfixturevalue('deposit'),
            request.getfixturevalue('settle_timeout'),
        )

    elif 'raiden_chain' in request.fixturenames:
        create_sequential_channels(
            raiden_apps,
            asset_contract_addresses[0],
            request.getfixturevalue('channels_per_node'),
            request.getfixturevalue('deposit'),
            request.getfixturevalue('settle_timeout'),
        )

    # else: a test that is not creating channels

    for app in raiden_apps:
        app.stop()

    # save the state from the last block into a genesis dict
    tester = blockchain_services[0].tester_state
    tester.mine()
    registry_address = blockchain_services[0].default_registry.address

    genesis_alloc = dict()
    for account_address in tester.block.state.to_dict():
        account_alloc = tester.block.account_to_dict(account_address)

        # code must be hex encoded without 0x prefix
        account_alloc['code'] = safe_lstrip_hex(account_alloc.get('code', ''))

        # account_to_dict returns accounts with nonce=0
        account_alloc['nonce'] = tester.block.get_nonce(account_address)

        genesis_alloc[account_address] = account_alloc

    account_addresses = [
        privatekey_to_address(key)
        for key in set(private_keys)
    ]

    for address in account_addresses:
        genesis_alloc[address]['balance'] = DEFAULT_BALANCE_BIN

    alloc = {
        safe_lstrip_hex(address_encoder(address_maybe_bin)): data
        for address_maybe_bin, data in genesis_alloc.iteritems()
    }

    genesis = GENESIS_STUB.copy()
    genesis['alloc'] = alloc
    genesis['config']['defaultRegistryAddress'] = address_encoder(registry_address)
    genesis['config']['assetAddresses'] = [
        address_encoder(asset_address)
        for asset_address in asset_contract_addresses
    ]

    return genesis
Esempio n. 18
0
def raiden_network(
    request,
    token_addresses,
    channels_per_node,
    deposit,
    settle_timeout,
    blockchain_services,
    endpoint_discovery_services,
    raiden_udp_ports,
    reveal_timeout,
    database_paths,
    retry_interval,
    retries_before_backoff,
    throttle_capacity,
    throttle_fill_rate,
    nat_invitation_timeout,
    nat_keepalive_retries,
    nat_keepalive_timeout,
    local_matrix_server,
):

    raiden_apps = create_apps(
        blockchain_services.blockchain_services,
        endpoint_discovery_services,
        blockchain_services.deploy_registry.address,
        blockchain_services.secret_registry.address,
        raiden_udp_ports,
        reveal_timeout,
        settle_timeout,
        database_paths,
        retry_interval,
        retries_before_backoff,
        throttle_capacity,
        throttle_fill_rate,
        nat_invitation_timeout,
        nat_keepalive_retries,
        nat_keepalive_timeout,
        local_matrix_server,
    )

    app_channels = create_network_channels(
        raiden_apps,
        channels_per_node,
    )

    greenlets = []
    for token_address in token_addresses:
        for app_pair in app_channels:
            greenlets.append(
                gevent.spawn(
                    netting_channel_open_and_deposit,
                    app_pair[0],
                    app_pair[1],
                    token_address,
                    deposit,
                    settle_timeout,
                ))
    gevent.wait(greenlets)

    exception = RuntimeError(
        '`raiden_network` fixture setup failed, nodes are unreachable')
    with gevent.Timeout(seconds=30, exception=exception):
        wait_for_channels(
            app_channels,
            blockchain_services.deploy_registry.address,
            token_addresses,
            deposit,
        )

    _raiden_cleanup(request, raiden_apps)

    # Force blocknumber update
    exception = RuntimeError(
        'Alarm failed to start and set up start_block correctly')
    with gevent.Timeout(seconds=5, exception=exception):
        wait_for_alarm_start(raiden_apps)
    for app in raiden_apps:
        app.raiden.alarm.poll_for_new_block()

    yield raiden_apps
    [app.stop() for app in raiden_apps]
Esempio n. 19
0
def raiden_network(
    token_addresses,
    token_network_registry_address,
    channels_per_node,
    deposit,
    settle_timeout,
    chain_id,
    blockchain_services,
    endpoint_discovery_services,
    raiden_udp_ports,
    reveal_timeout,
    database_paths,
    retry_interval,
    retries_before_backoff,
    throttle_capacity,
    throttle_fill_rate,
    nat_invitation_timeout,
    nat_keepalive_retries,
    nat_keepalive_timeout,
    environment_type,
    unrecoverable_error_should_crash,
    local_matrix_servers,
    private_rooms,
    retry_timeout,
    blockchain_type,
):
    raiden_apps = create_apps(
        chain_id=chain_id,
        blockchain_services=blockchain_services.blockchain_services,
        endpoint_discovery_services=endpoint_discovery_services,
        token_network_registry_address=token_network_registry_address,
        secret_registry_address=blockchain_services.secret_registry.address,
        raiden_udp_ports=raiden_udp_ports,
        reveal_timeout=reveal_timeout,
        settle_timeout=settle_timeout,
        database_paths=database_paths,
        retry_interval=retry_interval,
        retries_before_backoff=retries_before_backoff,
        throttle_capacity=throttle_capacity,
        throttle_fill_rate=throttle_fill_rate,
        nat_invitation_timeout=nat_invitation_timeout,
        nat_keepalive_retries=nat_keepalive_retries,
        nat_keepalive_timeout=nat_keepalive_timeout,
        environment_type=environment_type,
        unrecoverable_error_should_crash=unrecoverable_error_should_crash,
        local_matrix_url=local_matrix_servers[0],
        private_rooms=private_rooms,
    )

    wait_for_confirmed_block(blockchain_services, raiden_apps)
    parallel_start_apps(raiden_apps)

    exception = RuntimeError(
        '`raiden_chain` fixture setup failed, token networks unavailable')
    with gevent.Timeout(seconds=timeout(blockchain_type), exception=exception):
        wait_for_token_networks(
            raiden_apps=raiden_apps,
            token_network_registry_address=token_network_registry_address,
            token_addresses=token_addresses,
        )

    app_channels = create_network_channels(
        raiden_apps,
        channels_per_node,
    )

    create_all_channels_for_network(
        app_channels=app_channels,
        token_addresses=token_addresses,
        channel_individual_deposit=deposit,
        channel_settle_timeout=settle_timeout,
        token_network_registry_address=token_network_registry_address,
        retry_timeout=retry_timeout,
    )

    exception = RuntimeError(
        '`raiden_network` fixture setup failed, nodes are unreachable')
    with gevent.Timeout(seconds=timeout(blockchain_type), exception=exception):
        wait_for_channels(
            app_channels,
            blockchain_services.deploy_registry.address,
            token_addresses,
            deposit,
        )

    # Force blocknumber update
    exception = RuntimeError(
        'Alarm failed to start and set up start_block correctly')

    with gevent.Timeout(seconds=5, exception=exception):
        wait_for_alarm_start(raiden_apps)

    yield raiden_apps

    shutdown_apps_and_cleanup_tasks(raiden_apps)