Example #1
0
def start_service(client: ExonumClient, service_name: str,
                  instance_name: str) -> int:
    """This function starts the previously deployed service instance."""

    # Create a start request:
    service_module = ModuleManager.import_service_module(
        "exonum-supervisor:0.12.0", "service")
    start_request = service_module.StartService()
    start_request.artifact.runtime_id = 0  # Rust runtime ID.
    start_request.artifact.name = service_name
    start_request.name = instance_name
    start_request.deadline_height = 1000000  # Some big number.

    # Convert the request from a Protobuf message to bytes:
    request_bytes = start_request.SerializeToString()

    # Send the request and wait for Exonum to process it:
    send_request(client, "start-service", request_bytes)

    # Ensure that the service is added to the running instances list:
    available_services = client.available_services().json()
    if instance_name not in map(lambda x: x["name"],
                                available_services["services"]):
        raise RuntimeError(
            f"{instance_name} is not listed in running instances after starting"
        )

    # Service has started.
    # Return the running instance ID:
    for instance in available_services["services"]:
        if instance["name"] == instance_name:
            return instance["id"]

    raise RuntimeError("Instance ID was not found")
Example #2
0
def deploy_service(client: ExonumClient, service_name: str) -> None:
    """This function sends a deploy request for the desired service
    and waits until it is deployed."""

    # Create a deploy request message:
    service_module = ModuleManager.import_service_module(
        "exonum-supervisor:0.12.0", "service")

    deploy_request = service_module.DeployRequest()
    deploy_request.artifact.runtime_id = 0  # Rust runtime ID.
    deploy_request.artifact.name = service_name
    deploy_request.deadline_height = 1000000  # Some big number (we won't have to wait that long, it's just a deadline).

    # Convert the request from a Protobuf message to bytes:
    request_bytes = deploy_request.SerializeToString()

    # Send the request and wait for Exonum to process it:
    send_request(client, "deploy-artifact", request_bytes)

    # Ensure that the service is added to the available modules:
    available_services = client.available_services().json()
    if service_name not in map(lambda x: x["name"],
                               available_services["artifacts"]):
        raise RuntimeError(
            f"{service_name} is not listed in available services after deployment"
        )
Example #3
0
def run() -> None:
    """Example of a simple API interaction."""
    client = ExonumClient(hostname="127.0.0.1",
                          public_api_port=8080,
                          private_api_port=8081)

    # Get the available services:
    print("Available services:")
    available_services_response = client.available_services()
    if available_services_response.status_code == 200:
        available_services = available_services_response.json()
        print(" Artifacts:")
        for artifact in available_services["artifacts"]:
            print(
                f"  - {artifact['name']} (runtime ID {artifact['runtime_id']})"
            )
        print(" Instances:")
        for instance in available_services["services"]:
            print(
                f"  - ID {instance['id']} => {instance['name']} (artifact {instance['artifact']['name']})"
            )
    else:
        print("Available services request failed")
    print("")

    # Get the health info:
    print("Health info:")
    health_info_response = client.health_info()
    if health_info_response.status_code == 200:
        health_info = health_info_response.json()
        print(f"Consensus status: {health_info['consensus_status']}")
        print(f"Connected peers: {health_info['connected_peers']}")
    else:
        print("Health info request failed.")
    print("")

    # Get the Exonum stats:
    print("Exonum stats:")
    stats_response = client.stats()
    if stats_response.status_code == 200:
        stats = stats_response.json()
        print(f"Tx pool size: {stats['tx_pool_size']}")
        print(f"Tx count: {stats['tx_count']}")
        print(f"Tx cache size: {stats['tx_cache_size']}")
    else:
        print("Stats request failed.")
    print("")

    # Get the user agent:
    print("Exonum user agent:")
    user_agent_response = client.user_agent()
    if user_agent_response.status_code == 200:
        user_agent = user_agent_response.json()
        print(f"User agent: {user_agent}")
    else:
        print("User agent request failed.")
Example #4
0
def get_cryptocurrency_instance_id(client: ExonumClient) -> int:
    """Ensures that the service is added to the running instances list and gets
    the ID of the instance."""
    instance_name = CRYPTOCURRENCY_INSTANCE_NAME
    available_services = client.available_services().json()
    if instance_name not in map(lambda x: x["name"], available_services["services"]):
        raise RuntimeError(f"{instance_name} is not listed in the running instances after the start")

    # Service starts.
    # Return the running instance ID:
    for instance in available_services["services"]:
        if instance["name"] == instance_name:
            return instance["id"]

    raise RuntimeError("Instance ID was not found")