Example #1
0
def setup_testchain(eth_client: EthClient, free_port_generator: Iterator[Port],
                    base_datadir: str,
                    base_logdir: str) -> Iterator[Dict[str, Any]]:

    ensure_executable(eth_client.value)

    rpc_port = next(free_port_generator)
    p2p_port = next(free_port_generator)

    eth_rpc_endpoint = URI(f"http://127.0.0.1:{rpc_port}")
    web3 = Web3(HTTPProvider(endpoint_uri=eth_rpc_endpoint))
    web3.middleware_onion.inject(make_sane_poa_middleware, layer=0)

    eth_nodes = [
        EthNodeDescription(
            private_key=TEST_PRIVKEY,
            rpc_port=rpc_port,
            p2p_port=p2p_port,
            miner=True,
            extra_config={},
            blockchain_type=eth_client.value,
        )
    ]

    random_marker = remove_0x_prefix(HexStr(hex(random.getrandbits(100))))
    genesis_description = GenesisDescription(
        prefunded_accounts=[
            AccountDescription(TEST_ACCOUNT_ADDRESS, DEFAULT_BALANCE)
        ],
        random_marker=random_marker,
        chain_id=CHAINNAME_TO_ID["smoketest"],
    )

    datadir = eth_node_to_datadir(privatekey_to_address(TEST_PRIVKEY),
                                  base_datadir)
    if eth_client is EthClient.GETH:
        keystore = geth_keystore(datadir)
    elif eth_client is EthClient.PARITY:
        keystore = parity_keystore(datadir)

    eth_node_runner = run_private_blockchain(
        web3=web3,
        eth_nodes=eth_nodes,
        base_datadir=base_datadir,
        log_dir=base_logdir,
        verbosity="info",
        genesis_description=genesis_description,
    )
    with eth_node_runner as node_executors:
        yield dict(
            eth_client=eth_client,
            base_datadir=base_datadir,
            eth_rpc_endpoint=eth_rpc_endpoint,
            keystore=keystore,
            node_executors=node_executors,
            web3=web3,
        )
Example #2
0
def setup_testchain(
        eth_client: EthClient, print_step: Callable,
        free_port_generator: Iterator[int]) -> ContextManager[Dict[str, Any]]:
    print_step("Starting Ethereum node")

    ensure_executable(eth_client.value)

    rpc_port = next(free_port_generator)
    p2p_port = next(free_port_generator)
    base_datadir = os.environ["RST_DATADIR"]

    eth_rpc_endpoint = f"http://127.0.0.1:{rpc_port}"
    web3 = Web3(HTTPProvider(endpoint_uri=eth_rpc_endpoint))
    web3.middleware_stack.inject(geth_poa_middleware, layer=0)

    eth_nodes = [
        EthNodeDescription(
            private_key=TEST_PRIVKEY,
            rpc_port=rpc_port,
            p2p_port=p2p_port,
            miner=True,
            extra_config={},
            blockchain_type=eth_client.value,
        )
    ]

    random_marker = remove_0x_prefix(hex(random.getrandbits(100)))
    genesis_description = GenesisDescription(
        prefunded_accounts=[TEST_ACCOUNT_ADDRESS, TEST_PARTNER_ADDRESS],
        random_marker=random_marker,
        chain_id=NETWORKNAME_TO_ID["smoketest"],
    )

    nodekeyhex = remove_0x_prefix(encode_hex(TEST_PRIVKEY))
    datadir = eth_node_to_datadir(nodekeyhex, base_datadir)
    if eth_client is EthClient.GETH:
        keystore = geth_keystore(datadir)
    elif eth_client is EthClient.PARITY:
        keystore = parity_keystore(datadir)

    eth_node_runner = run_private_blockchain(
        web3=web3,
        eth_nodes=eth_nodes,
        base_datadir=base_datadir,
        log_dir=os.path.join(base_datadir, "logs"),
        verbosity="info",
        genesis_description=genesis_description,
    )
    with eth_node_runner as node_executors:
        yield dict(
            eth_client=eth_client,
            base_datadir=base_datadir,
            eth_rpc_endpoint=eth_rpc_endpoint,
            keystore=keystore,
            node_executors=node_executors,
            web3=web3,
        )
Example #3
0
def main() -> None:
    tmpdir = tempfile.mkdtemp()

    geth_nodes = []
    for i in range(NUM_GETH_NODES):
        is_miner = i == 0
        node_key = PrivateKey(sha3(f"node:{i}".encode()))
        p2p_port = Port(START_PORT + i)
        rpc_port = Port(START_RPCPORT + i)

        description = EthNodeDescription(
            private_key=node_key,
            rpc_port=rpc_port,
            p2p_port=p2p_port,
            miner=is_miner,
            extra_config={},
        )

        geth_nodes.append(description)

    rpc_endpoint = f"http://127.0.0.1:{START_RPCPORT}"
    web3 = Web3(HTTPProvider(rpc_endpoint))

    random_marker = remove_0x_prefix(hex(random.getrandbits(100)))
    genesis_description = GenesisDescription(
        prefunded_accounts=DEFAULT_ACCOUNTS,
        random_marker=random_marker,
        chain_id=ChainID(NETWORKNAME_TO_ID["smoketest"]),
    )
    private_chain: ContextManager[
        List[JSONRPCExecutor]] = run_private_blockchain(
            web3=web3,
            eth_nodes=geth_nodes,
            base_datadir=tmpdir,
            log_dir=tmpdir,
            verbosity="info",
            genesis_description=genesis_description,
        )

    with private_chain:
        from IPython import embed

        embed()
Example #4
0
def main():
    tmpdir = tempfile.mkdtemp()

    geth_nodes = []
    for i in range(NUM_GETH_NODES):
        is_miner = i == 0
        node_key = sha3(f'node:{i}'.encode())
        p2p_port = START_PORT + i
        rpc_port = START_RPCPORT + i

        description = EthNodeDescription(
            private_key=node_key,
            rpc_port=rpc_port,
            p2p_port=p2p_port,
            miner=is_miner,
            extra_config={},
        )

        geth_nodes.append(description)

    rpc_endpoint = f'http://127.0.0.1:{START_RPCPORT}'
    web3 = Web3(HTTPProvider(rpc_endpoint))

    verbosity = 0
    random_marker = remove_0x_prefix(hex(random.getrandbits(100)))
    genesis_description = GenesisDescription(
        prefunded_accounts=DEFAULT_ACCOUNTS,
        random_marker=random_marker,
        chain_id=NETWORKNAME_TO_ID['smoketest'],
    )
    private_chain = run_private_blockchain(  # NOQA
        web3=web3,
        eth_nodes=geth_nodes,
        base_datadir=tmpdir,
        log_dir=tmpdir,
        verbosity=verbosity,
        genesis_description=genesis_description,
    )

    with private_chain:
        from IPython import embed
        embed()
Example #5
0
def eth_nodes_configuration(
    blockchain_number_of_nodes,
    blockchain_key_seed,
    port_generator,
    blockchain_type,
    blockchain_extra_config,
) -> List[EthNodeDescription]:
    eth_nodes = list()

    for position in range(blockchain_number_of_nodes):
        key = keccak(blockchain_key_seed.format(position).encode())
        eth_node = EthNodeDescription(
            private_key=key,
            rpc_port=next(port_generator),
            p2p_port=next(port_generator),
            miner=(position == 0),
            extra_config=blockchain_extra_config,
            blockchain_type=blockchain_type,
        )

        eth_nodes.append(eth_node)

    return eth_nodes
Example #6
0
def setup_testchain(print_step):
    print_step('Starting Ethereum node')

    ensure_executable('geth')

    free_port = get_free_port('127.0.0.1')
    rpc_port = next(free_port)
    p2p_port = next(free_port)
    base_datadir = os.environ['RST_DATADIR']

    description = EthNodeDescription(
        private_key=TEST_PRIVKEY,
        rpc_port=rpc_port,
        p2p_port=p2p_port,
        miner=True,
    )

    eth_rpc_endpoint = f'http://127.0.0.1:{rpc_port}'
    web3 = Web3(HTTPProvider(endpoint_uri=eth_rpc_endpoint))
    web3.middleware_stack.inject(geth_poa_middleware, layer=0)

    config = eth_node_config(
        description.private_key,
        description.p2p_port,
        description.rpc_port,
    )

    config.update({
        'unlock': 0,
        'mine': True,
        'password': os.path.join(base_datadir, 'pw'),
    })

    nodes_configuration = [config]
    eth_node_config_set_bootnodes(nodes_configuration)
    keystore = os.path.join(eth_node_to_datadir(config, base_datadir),
                            'keystore')

    logdir = os.path.join(base_datadir, 'logs')

    processes_list = eth_run_nodes(
        eth_nodes=[description],
        nodes_configuration=nodes_configuration,
        base_datadir=base_datadir,
        genesis_file=os.path.join(get_project_root(),
                                  'smoketest_genesis.json'),
        chain_id=NETWORKNAME_TO_ID['smoketest'],
        verbosity=0,
        logdir=logdir,
    )

    try:
        # the marker is hardcoded in the genesis file
        random_marker = remove_0x_prefix(encode_hex(b'raiden'))
        eth_wait_and_check(
            web3=web3,
            accounts_addresses=[],
            random_marker=random_marker,
            processes_list=processes_list,
        )
    except (ValueError, RuntimeError) as e:
        # If geth_wait_and_check or the above loop throw an exception make sure
        # we don't end up with a rogue geth process running in the background
        for process in processes_list:
            process.terminate()
        raise e

    return dict(
        base_datadir=base_datadir,
        eth_rpc_endpoint=eth_rpc_endpoint,
        keystore=keystore,
        processes_list=processes_list,
        web3=web3,
    )
Example #7
0
def web3(
    blockchain_p2p_ports,
    blockchain_private_keys,
    blockchain_rpc_ports,
    blockchain_type,
    blockchain_extra_config,
    deploy_key,
    private_keys,
    account_genesis_eth_balance,
    random_marker,
    request,
    tmpdir,
    chain_id,
    logs_storage,
):
    """ Starts a private chain with accounts funded. """
    # include the deploy key in the list of funded accounts
    keys_to_fund = set(private_keys)
    keys_to_fund.add(deploy_key)
    keys_to_fund = sorted(keys_to_fund)

    if blockchain_type not in {client.value for client in EthClient}:
        raise ValueError(f"unknown blockchain_type {blockchain_type}")

    host = "127.0.0.1"
    rpc_port = blockchain_rpc_ports[0]
    endpoint = f"http://{host}:{rpc_port}"
    web3 = Web3(HTTPProvider(endpoint))

    assert len(blockchain_private_keys) == len(blockchain_rpc_ports)
    assert len(blockchain_private_keys) == len(blockchain_p2p_ports)

    eth_nodes = [
        EthNodeDescription(
            private_key=key,
            rpc_port=rpc,
            p2p_port=p2p,
            miner=(pos == 0),
            extra_config=blockchain_extra_config,
            blockchain_type=blockchain_type,
        )
        for pos, (key, rpc, p2p) in enumerate(
            zip(blockchain_private_keys, blockchain_rpc_ports, blockchain_p2p_ports)
        )
    ]

    accounts_to_fund = [
        AccountDescription(privatekey_to_address(key), account_genesis_eth_balance)
        for key in keys_to_fund
    ]

    # The private chain data is always discarded on the CI
    base_datadir = str(tmpdir)

    # Save the Ethereum node's log for debugging
    base_logdir = os.path.join(logs_storage, blockchain_type)

    genesis_description = GenesisDescription(
        prefunded_accounts=accounts_to_fund, chain_id=chain_id, random_marker=random_marker
    )
    eth_node_runner = run_private_blockchain(
        web3=web3,
        eth_nodes=eth_nodes,
        base_datadir=base_datadir,
        log_dir=base_logdir,
        verbosity="info",
        genesis_description=genesis_description,
    )
    with eth_node_runner:
        yield web3

    cleanup_tasks()
Example #8
0
def setup_testchain(eth_client: EthClient,
                    print_step: Callable) -> ContextManager[Dict[str, Any]]:
    # TODO: This has a lot of overlap with `raiden.tests.utils.eth_node.run_private_blockchain` -
    #       refactor into a unified utility
    print_step('Starting Ethereum node')

    ensure_executable(eth_client.value)

    free_port = get_free_port()
    rpc_port = next(free_port)
    p2p_port = next(free_port)
    base_datadir = os.environ['RST_DATADIR']

    description = EthNodeDescription(
        private_key=TEST_PRIVKEY,
        rpc_port=rpc_port,
        p2p_port=p2p_port,
        miner=True,
        extra_config={},
        blockchain_type=eth_client.value,
    )

    eth_rpc_endpoint = f'http://127.0.0.1:{rpc_port}'
    web3 = Web3(HTTPProvider(endpoint_uri=eth_rpc_endpoint))
    web3.middleware_stack.inject(geth_poa_middleware, layer=0)

    config = eth_node_config(
        description.private_key,
        description.p2p_port,
        description.rpc_port,
    )

    config.update({
        'unlock': 0,
        'mine': True,
        'password': os.path.join(base_datadir, 'pw'),
    })

    nodes_configuration = [config]
    logdir = os.path.join(base_datadir, 'logs')

    # the marker is hardcoded in the genesis file
    random_marker = remove_0x_prefix(encode_hex(b'raiden'))
    seal_account = privatekey_to_address(description.private_key)
    accounts_to_fund = [TEST_ACCOUNT_ADDRESS, TEST_PARTNER_ADDRESS]

    genesis_description = GenesisDescription(
        prefunded_accounts=accounts_to_fund,
        random_marker=random_marker,
        chain_id=NETWORKNAME_TO_ID['smoketest'],
    )

    if eth_client is EthClient.GETH:
        keystore = os.path.join(eth_node_to_datadir(config, base_datadir),
                                'keystore')
        genesis_path = os.path.join(base_datadir, 'custom_genesis.json')
        geth_generate_poa_genesis(
            genesis_path=genesis_path,
            genesis_description=genesis_description,
            seal_account=seal_account,
        )
    elif eth_client is EthClient.PARITY:
        genesis_path = f'{base_datadir}/chainspec.json'
        parity_generate_chain_spec(
            genesis_path=genesis_path,
            genesis_description=genesis_description,
            seal_account=seal_account,
        )
        keystore = parity_create_account(nodes_configuration[0], base_datadir,
                                         genesis_path)
    else:
        raise RuntimeError(f'Invalid eth client type: {eth_client.value}')

    node_runner = eth_run_nodes(
        eth_node_descs=[description],
        nodes_configuration=nodes_configuration,
        base_datadir=base_datadir,
        genesis_file=genesis_path,
        chain_id=NETWORKNAME_TO_ID['smoketest'],
        random_marker=random_marker,
        verbosity='info',
        logdir=logdir,
    )
    with node_runner as node_executors:
        yield dict(
            eth_client=eth_client,
            base_datadir=base_datadir,
            eth_rpc_endpoint=eth_rpc_endpoint,
            keystore=keystore,
            node_executors=node_executors,
            web3=web3,
        )
Example #9
0
def web3(
    blockchain_p2p_ports,
    blockchain_private_keys,
    blockchain_rpc_ports,
    blockchain_type,
    blockchain_extra_config,
    deploy_key,
    private_keys,
    random_marker,
    request,
    tmpdir,
    chain_id,
):
    """ Starts a private chain with accounts funded. """
    # include the deploy key in the list of funded accounts
    keys_to_fund = set(private_keys)
    keys_to_fund.add(deploy_key)
    keys_to_fund = sorted(keys_to_fund)

    if blockchain_type not in {client.value for client in EthClient}:
        raise ValueError(f'unknown blockchain_type {blockchain_type}')

    host = '127.0.0.1'
    rpc_port = blockchain_rpc_ports[0]
    endpoint = f'http://{host}:{rpc_port}'
    web3 = Web3(HTTPProvider(endpoint))

    assert len(blockchain_private_keys) == len(blockchain_rpc_ports)
    assert len(blockchain_private_keys) == len(blockchain_p2p_ports)

    eth_nodes = [
        EthNodeDescription(
            private_key=key,
            rpc_port=rpc,
            p2p_port=p2p,
            miner=(pos == 0),
            extra_config=blockchain_extra_config,
            blockchain_type=blockchain_type,
        ) for pos, (key, rpc, p2p) in enumerate(
            zip(
                blockchain_private_keys,
                blockchain_rpc_ports,
                blockchain_p2p_ports,
            ), )
    ]

    accounts_to_fund = [privatekey_to_address(key) for key in keys_to_fund]

    base_datadir = str(tmpdir)

    if _ETH_LOGDIR:
        base_logdir = os.path.join(_ETH_LOGDIR, blockchain_type,
                                   request.node.name)
    else:
        base_logdir = os.path.join(base_datadir, 'logs')

    genesis_description = GenesisDescription(
        prefunded_accounts=accounts_to_fund,
        chain_id=chain_id,
        random_marker=random_marker,
    )
    eth_node_runner = run_private_blockchain(
        web3=web3,
        eth_nodes=eth_nodes,
        base_datadir=base_datadir,
        log_dir=base_logdir,
        verbosity='info',
        genesis_description=genesis_description,
    )
    with eth_node_runner:
        yield web3

    cleanup_tasks()