Ejemplo n.º 1
0
def run_n_nodes(application: str, nodes_amount: int) -> ExonumNetwork:
    """Creates and runs a network with N validators and return an
    `ExonumNetwork` object with it."""

    address = "127.0.0.1:{}"

    # Assign peer ports starting from 6331.
    available_peer_port = 6331

    # Assign API ports starting from 8080.
    available_api_port = 8080

    network = ExonumNetwork(application)
    network.generate_template(nodes_amount)

    for i in range(nodes_amount):
        network.generate_config(i, address.format(available_peer_port))
        available_peer_port += 1

    for i in range(nodes_amount):
        public_api_address = address.format(available_api_port)
        private_api_address = address.format(available_api_port + 1)
        network.finalize(i, public_api_address, private_api_address)
        available_api_port += 2

    for i in range(nodes_amount):
        network.run_node(i)

    return network
Ejemplo n.º 2
0
def run_n_nodes(application: str, nodes_amount: int) -> ExonumNetwork:
    """Creates and runs a network with N validators and return an
    `ExonumNetwork` object with it."""

    global available_peer_port, available_api_port

    address = "127.0.0.1:{}"

    network = ExonumNetwork(application)
    network.generate_template(nodes_amount)

    for i in range(nodes_amount):
        network.generate_config(i, address.format(available_peer_port))
        available_peer_port += 1

    if available_peer_port > MIN_PEER_PORT + PORT_RANGE:
        available_peer_port = MIN_PEER_PORT

    for i in range(nodes_amount):
        public_api_address = address.format(available_api_port)
        private_api_address = address.format(available_api_port + 1)
        network.finalize(i, public_api_address, private_api_address)
        available_api_port += 2

    if available_api_port > MIN_API_PORT + PORT_RANGE:
        available_api_port = MIN_API_PORT

    for i in range(nodes_amount):
        network.run_node(i)

    return network
Ejemplo n.º 3
0
def wait_for_block(network: ExonumNetwork, height: int = 1) -> None:
    """Wait for block at specific height"""
    for validator_id in range(network.validators_count()):
        host, public_port, private_port = network.api_address(validator_id)
        client = ExonumClient(host, public_port, private_port)
        for _ in range(RETRIES_AMOUNT):
            if client.public_api.get_block(height).status_code == 200:
                break
            time.sleep(0.5)
Ejemplo n.º 4
0
def wait_api_to_start(network: ExonumNetwork) -> None:
    """Wait for api starting"""
    for validator_id in range(network.validators_count()):
        host, public_port, private_port = network.api_address(validator_id)
        client = ExonumClient(host, public_port, private_port)
        for _ in range(RETRIES_AMOUNT):
            try:
                client.public_api.health_info()
                break
            except ConnectionError:
                time.sleep(0.5)
Ejemplo n.º 5
0
def launcher_networks(network: ExonumNetwork) -> List[Dict[str, Any]]:
    """Builds a network configuration for `exonum-launcher` from the
    `ExonumNetwork` object."""
    networks = []
    for validator_id in range(network.validators_count()):
        host, public_port, private_port = network.api_address(validator_id)
        node_network = {"host": host, "ssl": False, "public-api-port": public_port, "private-api-port": private_port}
        networks.append(node_network)

    # Temporary workaround: supervisor works in simple mode and we need only one node.
    return networks[:1]
Ejemplo n.º 6
0
def run_dev_node(application: str) -> ExonumNetwork:
    """Starts a single node in the run-dev mode and returns
    `ExonumNetwork` object with the running node.

    Example:

    >>> network = run_dev_node("exonum-cryptocurrency-advanced")"""
    network = ExonumNetwork(application)

    network.run_dev()

    return network
Ejemplo n.º 7
0
def launcher_networks(network: ExonumNetwork) -> List[Dict[str, Any]]:
    """Builds a network configuration for `exonum-launcher` from the
    `ExonumNetwork` object."""
    networks = []
    for validator_id in range(network.validators_count()):
        host, public_port, private_port = network.api_address(validator_id)
        node_network = {
            "host": host,
            "ssl": False,
            "public-api-port": public_port,
            "private-api-port": private_port
        }
        networks.append(node_network)

    return networks
Ejemplo n.º 8
0
def wait_for_block(network: ExonumNetwork, height: int = 1) -> None:
    """Wait for block at specific height"""

    for validator_id in range(network.validators_count()):
        host, public_port, private_port = network.api_address(validator_id)
        client = ExonumClient(host, public_port, private_port)
        for _ in range(RETRIES_AMOUNT):
            try:
                block = client.public_api.get_block(height)
                if block.status_code == 200:
                    break
            except ConnectionError:
                pass
            time.sleep(0.5)
        else:
            raise Exception(f"Waiting for block {height} failed for validator {validator_id}")