Esempio n. 1
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
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