Example #1
0
    def __init__(self,
                 client: ExonumClient,
                 instance_name: str = "crypto",
                 version: str = "0.2.0"):
        self.client = client
        service_name = "exonum-cryptocurrency"
        self.service_version = version
        self.instance_name = instance_name
        self.loader = client.protobuf_loader()
        self.loader.initialize()
        self.loader.load_main_proto_files()
        self.loader.load_service_proto_files(runtime_id=0,
                                             artifact_name="exonum-supervisor",
                                             artifact_version="1.0.0")
        self.loader.load_service_proto_files(
            runtime_id=0,
            artifact_name=service_name,
            artifact_version=self.service_version)

        self.cryptocurrency_module = ModuleManager.import_service_module(
            service_name, self.service_version, "service")
        self.types_module = ModuleManager.import_service_module(
            service_name, self.service_version, "exonum.crypto.types")
        instance_id = client.public_api.get_instance_id_by_name(
            self.instance_name)
        self.msg_generator = MessageGenerator(
            instance_id=instance_id,
            artifact_name=service_name,
            artifact_version=self.service_version)
    def __init__(self, client: ExonumClient):
        self.client = client
        cryptocurrency_service_name = "exonum-cryptocurrency-advanced"
        cryptocurrency_service_version = "0.13.0-rc.2"
        self.loader = client.protobuf_loader()
        self.loader.initialize()
        self.loader.load_main_proto_files()
        self.loader.load_service_proto_files(
            runtime_id=0,
            artifact_name="exonum-supervisor",
            artifact_version="0.13.0-rc.2",
        )
        self.loader.load_service_proto_files(
            runtime_id=0,
            artifact_name=cryptocurrency_service_name,
            artifact_version=cryptocurrency_service_version,
        )

        self.cryptocurrency_module = ModuleManager.import_service_module(
            cryptocurrency_service_name, cryptocurrency_service_version,
            "service")
        self.types_module = ModuleManager.import_service_module(
            cryptocurrency_service_name, cryptocurrency_service_version,
            "types")
        instance_id = client.public_api.get_instance_id_by_name("crypto")
        self.msg_generator = MessageGenerator(
            instance_id=instance_id,
            artifact_name=cryptocurrency_service_name,
            artifact_version=cryptocurrency_service_version)
Example #3
0
def run() -> None:
    """Example of downloading the Protobuf sources and using the compiled
    module."""
    client = ExonumClient(hostname="127.0.0.1", public_api_port=8080, private_api_port=8081)

    # Create ProtobufLoader via context manager (so that we will not have to
    # initialize/deinitialize it manually):
    with client.protobuf_loader() as loader:
        # Load core proto files:
        loader.load_main_proto_files()
        # Load proto files for the Exonum supervisor service:
        loader.load_service_proto_files(RUST_RUNTIME_ID, "exonum-supervisor:0.12.0")

        # Load the main module (runtime.proto):
        main_module = ModuleManager.import_main_module("runtime")

        # Create a Protobuf message object:
        artifact_id = main_module.ArtifactId()
        artifact_id.runtime_id = RUST_RUNTIME_ID
        artifact_id.name = "some_service:0.1.0"

        # Working with Protobuf objects, you have to follow Protobuf Python API
        # conventions. See Protobuf Python API docs for details:
        instance_spec = main_module.InstanceSpec()
        instance_spec.artifact.CopyFrom(artifact_id)

        # Load the service module (service.proto from the supervisor service):
        service_module = ModuleManager.import_service_module("exonum-supervisor:0.12.0", "service")

        # Workflow is the same as for the main modules:
        _deploy_request = service_module.DeployRequest()
Example #4
0
def run() -> None:
    """This example creates two wallets (for Alice and Bob) and performs several
    transactions between these wallets."""
    client = ExonumClient(hostname="127.0.0.1",
                          public_api_port=8080,
                          private_api_port=8081)

    with client.protobuf_loader() as loader:
        # Load and compile proto files:
        loader.load_main_proto_files()
        loader.load_service_proto_files(RUST_RUNTIME_ID,
                                        CRYPTOCURRENCY_ARTIFACT_NAME,
                                        CRYPTOCURRENCY_ARTIFACT_VERSION)

        instance_id = get_cryptocurrency_instance_id(client)

        cryptocurrency_message_generator = MessageGenerator(
            instance_id, CRYPTOCURRENCY_ARTIFACT_NAME,
            CRYPTOCURRENCY_ARTIFACT_VERSION)

        alice_keypair = create_wallet(client, cryptocurrency_message_generator,
                                      "Alice")
        bob_keypair = create_wallet(client, cryptocurrency_message_generator,
                                    "Bob")

        alice_balance = get_balance(client, alice_keypair.public_key)
        bob_balance = get_balance(client, bob_keypair.public_key)
        print("Created the wallets for Alice and Bob. Balance:")
        print(f" Alice => {alice_balance}")
        print(f" Bob => {bob_balance}")

        amount = 10
        alice_balance, bob_balance = transfer(
            client, cryptocurrency_message_generator, alice_keypair,
            bob_keypair.public_key, amount)

        print(f"Transferred {amount} tokens from Alice's wallet to Bob's one")
        print(f" Alice => {alice_balance}")
        print(f" Bob => {bob_balance}")

        amount = 25
        bob_balance, alice_balance = transfer(
            client, cryptocurrency_message_generator, bob_keypair,
            alice_keypair.public_key, amount)

        print(f"Transferred {amount} tokens from Bob's wallet to Alice's one")
        print(f" Alice => {alice_balance}")
        print(f" Bob => {bob_balance}")
Example #5
0
    def __init__(self, client: ExonumClient):
        self.client = client
        cryptocurrency_service_name = 'exonum-cryptocurrency-advanced:0.13.0-rc.2'
        self.loader = client.protobuf_loader()
        self.loader.initialize()
        self.loader.load_main_proto_files()
        self.loader.load_service_proto_files(
            runtime_id=0, service_name='exonum-supervisor:0.13.0-rc.2')
        self.loader.load_service_proto_files(
            runtime_id=0, service_name=cryptocurrency_service_name)

        self.cryptocurrency_module = ModuleManager.import_service_module(
            cryptocurrency_service_name, 'service')
        self.types_module = ModuleManager.import_service_module(
            cryptocurrency_service_name, 'types')
        instance_id = client.get_instance_id_by_name("crypto")
        self.msg_generator = MessageGenerator(
            instance_id=instance_id, artifact_name=cryptocurrency_service_name)
Example #6
0
def run() -> None:
    """This is an example script for deploying the Cryptocurrency Advanced
    service.
    This script is intended only to demonstrate client possibilities.
    For actual deployment consider using `exonum-launcher` tool."""
    client = ExonumClient(hostname="127.0.0.1",
                          public_api_port=8080,
                          private_api_port=8081)

    # Create better-looking aliases for constants.
    service_name = CRYPTOCURRENCY_ARTIFACT_NAME
    service_version = CRYPTOCURRENCY_ARTIFACT_VERSION
    instance_name = CRYPTOCURRENCY_INSTANCE_NAME

    with client.protobuf_loader() as loader:
        # Load and compile proto files:
        loader.load_main_proto_files()
        loader.load_service_proto_files(RUST_RUNTIME_ID,
                                        SUPERVISOR_ARTIFACT_NAME,
                                        SUPERVISOR_ARTIFACT_VERSION)

        try:
            print(f"Started deploying `{service_name}` artifact.")
            deploy_service(client, service_name, service_version)

            print(f"Artifact `{service_name}` successfully deployed.")

            print(f"Started enablind `{instance_name}` instance.")

            instance_id = start_service(client, service_name, service_version,
                                        instance_name)

            # If no exception has occurred during the previous calls, the service
            # has started successfully:
            print(
                f"Service instance '{instance_name}' (artifact '{service_name}') started with ID {instance_id}."
            )
        except RuntimeError as err:
            print(
                f"Service instance '{instance_name}' (artifact '{service_name}') deployment failed with error {err}"
            )
def run_example(source: str) -> None:
    # Create client.
    client = ExonumClient(hostname="127.0.0.1", public_api_port=8080, private_api_port=8081)

    # Setup protobuf provider.
    setup_protobuf_provider(client.protobuf_provider, source)

    # Create ProtobufLoader via context manager (so that we will not have to
    # initialize/deinitialize it manually):
    with client.protobuf_loader() as loader:
        # Load core proto files:
        loader.load_main_proto_files()
        # Load proto files for the Exonum supervisor service:
        loader.load_service_proto_files(RUST_RUNTIME_ID, SERVICE_NAME, SERVICE_VERSION)

        # Load the main module (exonum/crypto/type.proto).
        types_module = ModuleManager.import_main_module("exonum.crypto.types")

        # Create a Protobuf message object:
        public_key = types_module.PublicKey()
        public_key.data = bytes(i for i in range(32))

        # Load the service module (service.proto from the cryptocurrency-advanced service).
        # Note that we load "service" module, which is absent in current version
        # of the service, it only exists in Exonum 1.0.
        service_module = ModuleManager.import_service_module(SERVICE_NAME, SERVICE_VERSION, "service")
        # Note that if we want to work with service module, we should use types also from that module.
        # That's required because of the inner python Protobuf implementation check system.
        types_module = ModuleManager.import_service_module(SERVICE_NAME, SERVICE_VERSION, "exonum.crypto.types")

        # Workflow is the same as for the main modules:
        transfer = service_module.Transfer()
        to = types_module.Hash()
        to.data = bytes(i for i in range(32))

        # Working with Protobuf objects, you have to follow Protobuf Python API conventions.
        # See Protobuf Python API docs for details.
        transfer.to.CopyFrom(to)
        transfer.amount = 10
        transfer.seed = 1
Example #8
0
def run() -> None:
    """This is an example script for deploying the Cryptocurrency Advanced
    service.
    This script is intended only to demonstrate client possibilities.
    For actual deployment consider using `exonum-launcher` tool."""
    client = ExonumClient(hostname="127.0.0.1",
                          public_api_port=8080,
                          private_api_port=8081)

    # Name of the artifact:
    service_name = "exonum-cryptocurrency-advanced:0.12.0"

    # Name of the service instance that we want to create:
    instance_name = "XNM"

    with client.protobuf_loader() as loader:
        # Load and compile proto files:
        loader.load_main_proto_files()
        loader.load_service_proto_files(RUST_RUNTIME_ID,
                                        "exonum-supervisor:0.12.0")

        try:
            print(f"Started deploying `{service_name}` artifact.")
            deploy_service(client, service_name)

            print(f"Artifact `{service_name}` successfully deployed.")

            print(f"Started enablind `{instance_name}` instance.")

            instance_id = start_service(client, service_name, instance_name)

            # If no exception has occured during the previous calls, the service
            # has started successfully:
            print(
                f"Service instance '{instance_name}' (artifact '{service_name}') started with ID {instance_id}."
            )
        except RuntimeError as err:
            print(
                f"Service instance '{instance_name}' (artifact '{service_name}') deployment failed with error {err}"
            )
Example #9
0
def run() -> None:
    """This example creates a wallet in the Cryptocurrency service, retrieves
    proofs for the wallet and verifies them.
    For the example to work, be sure to have `exonum-cryptocurrency-advanced`
    service instance with name `XNM` deployed."""
    client = ExonumClient(hostname="127.0.0.1",
                          public_api_port=8080,
                          private_api_port=8081)

    with client.protobuf_loader() as loader:
        # Load and compile proto files:
        loader.load_main_proto_files()
        loader.load_service_proto_files(RUST_RUNTIME_ID,
                                        CRYPTOCURRENCY_ARTIFACT_NAME)

        instance_id = get_cryptocurrency_instance_id(client)

        cryptocurrency_message_generator = MessageGenerator(
            instance_id, CRYPTOCURRENCY_ARTIFACT_NAME)

        alice_keypair = create_wallet(client, cryptocurrency_message_generator,
                                      "Alice")

        wallet_info_response = client.get_service(
            CRYPTOCURRENCY_INSTANCE_NAME, "v1/wallets/info?pub_key={}".format(
                alice_keypair.public_key.hex()))
        ensure_status_code(wallet_info_response)
        wallet_info = wallet_info_response.json()

        # `MapProof` to the whole Exonum state hash:
        proof_to_table = wallet_info["wallet_proof"]["to_table"]
        # Expected hash of the proof to the table is a state hash of the block:
        expected_to_table_hash_raw = wallet_info["block_proof"]["block"][
            "state_hash"]
        expected_to_table_hash = Hash(
            bytes.fromhex(expected_to_table_hash_raw))

        # Verify the proof to the table:
        verify_proof_to_table(proof_to_table, expected_to_table_hash)

        # `MapProof` to the wallet as a part of the Cryptocurrency schema:
        proof_to_wallet = wallet_info["wallet_proof"]["to_wallet"]
        # Expected hash of the proof to the wallet is the value stored in the
        # proof to the table:
        expected_to_wallet_hash_raw = wallet_info["wallet_proof"]["to_table"][
            "entries"][0]["value"]
        expected_to_wallet_hash = Hash(
            bytes.fromhex(expected_to_wallet_hash_raw))

        # Verify the proof to the wallet:
        verify_proof_to_wallet(proof_to_wallet, expected_to_wallet_hash)

        # `ListProof` for the transactions associtated with the wallet:
        proof_wallet_history = wallet_info["wallet_history"]["proof"]
        # Expected hash for the wallet history is the hash stored in the proof
        # to the wallet:
        expected_history_hash_raw = wallet_info["wallet_proof"]["to_wallet"][
            "entries"][0]["value"]["history_hash"]
        expected_history_hash = Hash(bytes(expected_history_hash_raw["data"]))

        # Verify the proof for the wallet history:
        verify_wallet_history_proof(proof_wallet_history,
                                    expected_history_hash)