def _get_channel_state_from_server(self, grpc_channel, channel_id): # Compile protobuf if needed codegen_dir = Path.home().joinpath(".snet", "mpe_client", "state_service") proto_dir = Path(__file__).absolute().parent.joinpath("resources", "proto") if (not codegen_dir.joinpath("state_service_pb2.py").is_file()): compile_proto(proto_dir, codegen_dir, proto_file = "state_service.proto") # make PaymentChannelStateService.GetChannelState call to the daemon stub_class, request_class, _ = import_protobuf_from_dir(codegen_dir, "GetChannelState") message = self.w3.soliditySha3(["uint256"], [channel_id]) signature = self.ident.sign_message_after_soliditySha3(message) request = request_class(channel_id = self.w3.toBytes(channel_id), signature = bytes(signature)) stub = stub_class(grpc_channel) response = getattr(stub, "GetChannelState")(request) # convert bytes to int state = dict() state["current_nonce"] = int.from_bytes(response.current_nonce, byteorder='big') state["current_signed_amount"] = int.from_bytes(response.current_signed_amount, byteorder='big') if (state["current_signed_amount"] > 0): good = self._verify_my_signature(bytes(response.current_signature), self.get_mpe_address(), channel_id, state["current_nonce"], state["current_signed_amount"]) if (not good): raise Exception("Error in _get_channel_state_from_server. My own signature from the server is not valid.") return state
def _get_channel_state_from_server(self, grpc_channel, channel_id): # Compile protobuf if needed codegen_dir = Path.home().joinpath(".snet", "mpe_client", "state_service") proto_dir = Path(__file__).absolute().parent.joinpath("resources", "proto") if (not codegen_dir.joinpath("state_service_pb2.py").is_file()): compile_proto(proto_dir, codegen_dir, proto_file = "state_service.proto") # make PaymentChannelStateService.GetChannelState call to the daemon stub_class, request_class, _ = import_protobuf_from_dir(codegen_dir, "GetChannelState") message = self.w3.soliditySha3(["uint256"], [channel_id]) signature = self.ident.sign_message_after_soliditySha3(message) request = request_class(channel_id = self.w3.toBytes(channel_id), signature = bytes(signature)) stub = stub_class(grpc_channel) response = getattr(stub, "GetChannelState")(request) # convert bytes to int state = dict() state["current_nonce"] = int.from_bytes(response.current_nonce, byteorder='big') state["current_signed_amount"] = int.from_bytes(response.current_signed_amount, byteorder='big') if (state["current_signed_amount"] > 0): good = self._verify_my_signature(bytes(response.current_signature), self.get_mpe_address(), channel_id, state["current_nonce"], state["current_signed_amount"]) if (not good): raise Exception("Error in _get_channel_state_from_server. My own signature from the server is not valid.") return state
def _get_stub_and_request_classes(self, service_name): """ import protobuf and return stub and request class """ # Compile protobuf if needed codegen_dir = Path.home().joinpath(".snet", "mpe_client", "control_service") proto_dir = Path(__file__).absolute().parent.joinpath("resources", "proto") if (not codegen_dir.joinpath("control_service_pb2.py").is_file()): compile_proto(proto_dir, codegen_dir, proto_file = "control_service.proto") stub_class, request_class, _ = import_protobuf_from_dir(codegen_dir, service_name) return stub_class, request_class
def install_and_compile_proto(): import snet_cli from snet_cli.utils import compile_proto as compile_proto from pathlib import Path proto_dir = Path(__file__).absolute().parent.joinpath("snet_cli", "resources", "proto") dest_dir = Path(snet_cli.__file__).absolute().parent.joinpath("resources", "proto") print(proto_dir, "->", dest_dir) for fn in proto_dir.glob('*.proto'): print("Compiling protobuf", fn) compile_proto(proto_dir, dest_dir, proto_file=fn)
def install_and_compile_proto(): import snet_cli from snet_cli.utils import compile_proto as compile_proto from pathlib import Path proto_dir = Path(__file__).absolute().parent.joinpath("snet_cli", "resources", "proto") dest_dir = Path(snet_cli.__file__).absolute().parent.joinpath("resources", "proto") print(proto_dir, "->", dest_dir) for fn in proto_dir.glob('*.proto'): print("Compiling protobuf", fn) compile_proto(proto_dir, dest_dir, proto_file=fn)
def import_grpc_modules(spec_hash): spec_dir = Path("~").expanduser().joinpath(".snet").joinpath( "service_spec").joinpath(spec_hash) codegen_dir = Path("~").expanduser().joinpath(".snet").joinpath( "py-codegen").joinpath(spec_hash) sys.path.append(str(codegen_dir)) compile_proto(spec_dir, codegen_dir) mods = [] for p in codegen_dir.glob("*_pb2*"): m = importlib.import_module(p.name.replace(".py", "")) mods.append(m) return mods
def _get_stub_and_request_classes(self, service_name): """ import protobuf and return stub and request class """ # Compile protobuf if needed codegen_dir = Path.home().joinpath(".snet", "mpe_client", "control_service") proto_dir = Path(__file__).absolute().parent.joinpath( "resources", "proto") if (not codegen_dir.joinpath("control_service_pb2.py").is_file()): compile_proto(proto_dir, codegen_dir, proto_file="control_service.proto") stub_class, request_class, _ = import_protobuf_from_dir( codegen_dir, service_name) return stub_class, request_class
def _init_channel_from_metadata(self, channel_dir, metadata, channel_info): self._check_mpe_address_metadata(metadata) if (os.path.exists(channel_dir)): raise Exception("Directory %s already exists" % channel_dir) os.makedirs(channel_dir, mode=0o700) try: spec_dir = os.path.join(channel_dir, "service_spec") os.makedirs(spec_dir, mode=0o700) safe_extract_proto_from_ipfs(self._get_ipfs_client(), metadata["model_ipfs_hash"], spec_dir) # compile .proto files if (not compile_proto(Path(spec_dir), channel_dir)): raise Exception("Fail to compile %s/*.proto" % spec_dir) # save service_metadata.json in channel_dir metadata.save_pretty( os.path.join(channel_dir, "service_metadata.json")) # save channel_info (we need sender and signer) self._save_channel_info_dir(channel_dir, channel_info) except: # it is secure to remove channel_dir, because we've created it shutil.rmtree(channel_dir) raise
def _init_channel_from_metadata(self, channel_dir, metadata): self._check_mpe_address_metadata(metadata) if (os.path.exists(channel_dir)): raise Exception("Directory %s already exists" % channel_dir) os.makedirs(channel_dir) try: spec_dir = os.path.join(channel_dir, "service_spec") os.makedirs(spec_dir) # take tar of .proto files from ipfs and extract them to channel_dir/service_spec spec_tar = get_from_ipfs_and_checkhash(self._get_ipfs_client(), metadata["model_ipfs_hash"]) with tarfile.open(fileobj=io.BytesIO(spec_tar)) as f: f.extractall(spec_dir) # compile .proto files if (not compile_proto(Path(spec_dir), channel_dir)): raise Exception("Fail to compile %s/*.proto" % spec_dir) # save service_metadata.json in channel_dir metadata.save_pretty( os.path.join(channel_dir, "service_metadata.json")) except: # it is secure to remove channel_dir, because we've created it shutil.rmtree(channel_dir) raise
def _init_or_update_service_if_needed(self, metadata, service_registration): # if service was already initialized and metadataURI hasn't changed we do nothing if self.is_service_initialized(): if self.is_metadataURI_has_changed(service_registration): self._printerr("# Service with org_id=%s and service_id=%s was updated"%(self.args.org_id, self.args.service_id)) self._printerr("# ATTENTION!!! price or other paramaters might have been changed!\n") else: return # we do nothing self._printerr("# Initilize service with org_id=%s and service_id=%s"%(self.args.org_id, self.args.service_id)) self._check_mpe_address_metadata(metadata) service_dir = self.get_service_spec_dir(self.args.org_id, self.args.service_id) # remove old service_dir # it is relatevely safe to remove service_dir because we know that service_dir = self.get_service_spec_dir() so it is not a normal dir if (os.path.exists(service_dir)): shutil.rmtree(service_dir) os.makedirs(service_dir, mode=0o700) try: spec_dir = os.path.join(service_dir, "service_spec") os.makedirs(spec_dir, mode=0o700) safe_extract_proto_from_ipfs(self._get_ipfs_client(), metadata["model_ipfs_hash"], spec_dir) # compile .proto files if (not compile_proto(Path(spec_dir), service_dir)): raise Exception("Fail to compile %s/*.proto"%spec_dir) # save service_metadata.json in channel_dir metadata.save_pretty(os.path.join(service_dir, "service_metadata.json")) except: # it is secure to remove channel_dir, because we've created it shutil.rmtree(service_dir) raise self._save_service_info(self.args.org_id, self.args.service_id, service_registration)
def _init_or_update_service_if_needed(self, metadata, service_registration): # if service was already initialized and metadataURI hasn't changed we do nothing if self.is_service_initialized(): if self.is_metadataURI_has_changed(service_registration): self._printerr( "# Service with org_id=%s and service_id=%s was updated" % (self.args.org_id, self.args.service_id)) self._printerr( "# ATTENTION!!! price or other paramaters might have been changed!\n" ) else: return # we do nothing self._printerr("# Initilize service with org_id=%s and service_id=%s" % (self.args.org_id, self.args.service_id)) self._check_mpe_address_metadata(metadata) service_dir = self.get_service_spec_dir(self.args.org_id, self.args.service_id) # remove old service_dir # it is relatevely safe to remove service_dir because we know that service_dir = self.get_service_spec_dir() so it is not a normal dir if (os.path.exists(service_dir)): shutil.rmtree(service_dir) os.makedirs(service_dir, mode=0o700) try: spec_dir = os.path.join(service_dir, "service_spec") os.makedirs(spec_dir, mode=0o700) safe_extract_proto_from_ipfs(self._get_ipfs_client(), metadata["model_ipfs_hash"], spec_dir) # compile .proto files if (not compile_proto(Path(spec_dir), service_dir)): raise Exception("Fail to compile %s/*.proto" % spec_dir) # save service_metadata.json in channel_dir metadata.save_pretty( os.path.join(service_dir, "service_metadata.json")) except: # it is secure to remove channel_dir, because we've created it shutil.rmtree(service_dir) raise self._save_service_info(self.args.org_id, self.args.service_id, service_registration)
from snet_cli.utils import compile_proto, DefaultAttributeObject from concurrent import futures import time import web3 from snet_cli.mpe_channel_command import MPEChannelCommand from snet_cli.config import Config compile_proto("../service_spec1", ".", proto_file="ExampleService.proto") compile_proto("../../../snet_cli/resources/proto/", ".", proto_file="state_service.proto") compile_proto("../../../snet_cli/resources/proto/", ".", proto_file="control_service.proto") PRICE = 10000 import grpc import ExampleService_pb2 import ExampleService_pb2_grpc import state_service_pb2 import state_service_pb2_grpc import control_service_pb2 import control_service_pb2_grpc payments_unclaimed = dict() payments_in_progress = dict()
def compile_protobuf_from_dir(self): codegen_dir = self.get_channel_dir() if (not compile_proto(Path(self.args.proto_dir), codegen_dir)): self._error("Fail to compile %s/*.proto"%self.args.proto_dir)
from snet_cli.utils import compile_proto, DefaultAttributeObject from concurrent import futures import time import web3 from snet_cli.mpe_channel_command import MPEChannelCommand from snet_cli.config import Config compile_proto("../service_spec1", ".", proto_file = "ExampleService.proto") compile_proto("../../../snet_cli/resources/proto/", ".", proto_file = "state_service.proto") compile_proto("../../../snet_cli/resources/proto/", ".", proto_file = "control_service.proto") PRICE = 10000 import grpc import ExampleService_pb2 import ExampleService_pb2_grpc import state_service_pb2 import state_service_pb2_grpc import control_service_pb2 import control_service_pb2_grpc payments_unclaimed = dict() payments_in_progress = dict() # we use MPEChannelCommand._get_channel_state_from_blockchain to get channel state from blockchain # we need it to remove already claimed payments from payments_in_progress # remove all payments_in_progress with nonce < blockchain nonce def remove_already_claimed_payments():
def generate_client_library(self): cur_dir_path = PurePath(os.getcwd()) if not self.args.protodir: client_libraries_base_dir_path = cur_dir_path.joinpath("client_libraries") if not os.path.exists(client_libraries_base_dir_path): os.makedirs(client_libraries_base_dir_path) else: if os.path.isabs(self.args.protodir): client_libraries_base_dir_path = PurePath(self.args.protodir) else: client_libraries_base_dir_path = cur_dir_path.joinpath(self.args.protodir) if not os.path.isdir(client_libraries_base_dir_path): self._error("directory {} does not exist. Please make sure that the specified path exists".format(client_libraries_base_dir_path)) # Create service client libraries path library_language = self.args.language library_org_id = self.args.org_id library_service_id = self.args.service_id library_dir_path = client_libraries_base_dir_path.joinpath(library_language, get_contract_address(self, "Registry"), library_org_id, library_service_id) metadata = self._get_service_metadata_from_registry() model_ipfs_hash = metadata["model_ipfs_hash"] with TemporaryDirectory() as temp_dir: temp_dir_path = PurePath(temp_dir) proto_temp_dir_path = temp_dir_path.joinpath(library_language, library_org_id, library_service_id) safe_extract_proto_from_ipfs(self._get_ipfs_client(), model_ipfs_hash, proto_temp_dir_path) # Compile proto files compile_proto(Path(proto_temp_dir_path), library_dir_path) self._printout('client libraries for service with id "{}" in org with id "{}" generated at {}'.format(library_service_id, library_org_id, library_dir_path))