Beispiel #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
Beispiel #3
0
def deploy_and_open_channel_alloc(deployment_key):
    """ Compiles, deploys and dumps a minimal raiden smart contract environment for use in a
    genesis block. This will:
        - deploy the raiden Registry contract stack
        - deploy a token contract
        - open a channel for the TEST_ACCOUNT address
        - deploy the EndpointRegistry/discovery contract
        - register a known value for the TEST_ACCOUNT address
        - dump the complete state in a genesis['alloc'] compatible format
        - return the state dump and the contract addresses
    """
    deployment_key_bin = deployment_key.decode('hex')
    state = tester_state(deployment_key_bin, [deployment_key_bin], 6 * 10**6)

    registry_address = tester_deploy_contract(
        state,
        deployment_key_bin,
        'Registry',
        get_contract_path('Registry.sol'),
    )

    discovery_address = tester_deploy_contract(
        state,
        deployment_key_bin,
        'EndpointRegistry',
        get_contract_path('EndpointRegistry.sol'),
    )

    client = BlockChainServiceTesterMock(
        deployment_key_bin,
        state,
    )

    registry = client.registry(registry_address)

    token_address = client.deploy_and_register_token(
        registry,
        'HumanStandardToken',
        get_contract_path('HumanStandardToken.sol'),
        constructor_parameters=(100, 'smoketesttoken', 2, 'RST'))

    manager = registry.manager_by_token(token_address)

    assert manager.private_key == deployment_key_bin
    our_address = TEST_ACCOUNT['address']

    channel_address = manager.new_netting_channel(
        our_address.decode('hex'), TEST_PARTNER_ADDRESS.decode('hex'), 50)

    client.token(token_address).approve(channel_address, TEST_DEPOSIT_AMOUNT)
    channel = NettingChannelTesterMock(state, deployment_key_bin,
                                       channel_address)
    channel.deposit(TEST_DEPOSIT_AMOUNT)

    discovery = client.discovery(discovery_address)
    discovery.proxy.registerEndpoint(TEST_ENDPOINT)

    contracts = dict(
        registry_address=registry_address,
        token_address=token_address,
        discovery_address=discovery_address,
        channel_address=channel_address,
    )
    for k, v in contracts.iteritems():
        contracts[k] = hexlify(v)

    alloc = dict()
    # preserve all accounts and contracts
    for address in state.block.state.to_dict().keys():
        address = hexlify(address)
        alloc[address] = state.block.account_to_dict(address)

    for account, content in alloc.iteritems():
        alloc[account]['storage'] = fix_tester_storage(content['storage'])

    return dict(
        alloc=alloc,
        contracts=contracts,
    )
def deploy_and_open_channel_alloc(deployment_key):
    """ Compiles, deploys and dumps a minimal raiden smart contract environment for use in a
    genesis block. This will:
        - deploy the raiden Registry contract stack
        - deploy a token contract
        - open a channel for the TEST_ACCOUNT address
        - deploy the EndpointRegistry/discovery contract
        - register a known value for the TEST_ACCOUNT address
        - dump the complete state in a genesis['alloc'] compatible format
        - return the state dump and the contract addresses
    """
    deployment_key_bin = unhexlify(deployment_key)
    state = create_tester_chain(
        deployment_key_bin,
        [deployment_key_bin],
        6 * 10 ** 6
    )

    registry_address = tester_deploy_contract(
        state,
        deployment_key_bin,
        'Registry',
        get_contract_path('Registry.sol'),
    )

    discovery_address = tester_deploy_contract(
        state,
        deployment_key_bin,
        'EndpointRegistry',
        get_contract_path('EndpointRegistry.sol'),
    )

    client = BlockChainServiceTesterMock(
        deployment_key_bin,
        state,
    )

    registry = client.registry(registry_address)

    token_address = client.deploy_and_register_token(
        registry,
        'HumanStandardToken',
        get_contract_path('HumanStandardToken.sol'),
        constructor_parameters=(100, 'smoketesttoken', 2, 'RST')
    )

    manager = registry.manager_by_token(token_address)
    assert manager.private_key == deployment_key_bin

    channel_address = manager.new_netting_channel(
        unhexlify(TEST_PARTNER_ADDRESS),
        50
    )

    client.token(token_address).approve(channel_address, TEST_DEPOSIT_AMOUNT)
    channel = NettingChannelTesterMock(
        state,
        deployment_key_bin,
        channel_address
    )
    channel.deposit(TEST_DEPOSIT_AMOUNT)

    discovery = client.discovery(discovery_address)
    discovery.proxy.registerEndpoint(TEST_ENDPOINT)

    contracts = dict(
        registry_address=address_encoder(registry_address),
        token_address=address_encoder(token_address),
        discovery_address=address_encoder(discovery_address),
        channel_address=address_encoder(channel_address),
    )

    alloc = dict()
    # preserve all accounts and contracts
    for address in state.head_state.to_dict().keys():
        alloc[address] = state.head_state.account_to_dict(address)

    for account, content in alloc.items():
        alloc[account]['storage'] = fix_tester_storage(content['storage'])

    return dict(
        alloc=alloc,
        contracts=contracts,
    )