def test_ipns(self, mocked_ipfs_client, _): """ Test storage: upload, download, create_new_ipns_link, etc """ credentials = { "gas_payer": "0x1413862C2B7054CDbfdc181B83962CB0FC11fD92", "gas_payer_priv": "28e516f1e2f99e96a48a23cea1f94ee5f073403a1c68e818263f0eb898f1c8e5" } pub_key = b"2dbc2c2c86052702e7c219339514b2e8bd4687ba1236c478ad41b43330b08488c12c8c1797aa181f3a4596a1bd8a0c18344ea44d6655f61fa73e56e743f79e0d" job = Job(credentials=credentials, escrow_manifest=Manifest({ 'task_bid_price': 9, 'request_type': 'image_label_binary', 'job_total_tasks': 10 })) name = 'abc' mocked_ipfs_client.key.list.side_effect = MI.key_list mocked_ipfs_client.key.gen.side_effect = MI.key_gen mocked_ipfs_client.name.publish.side_effect = MI.publish mocked_ipfs_client.add_bytes.side_effect = MI.add_bytes mocked_ipfs_client.resolve.side_effect = MI.resolve mocked_ipfs_client.cat.side_effect = MI.cat ipns_id = create_new_ipns_link(name) # Upload 1 (hash_, manifest_url) = upload(job.serialized_manifest, pub_key, name) manifest_dict = download(ipns_id, job.gas_payer_priv) dl_equals_up = manifest_dict == job.serialized_manifest link_exist = ipns_link_exists(name) ipns_urls_match = ipns_id == get_ipns_link(name).split('/')[-1] self.assertTrue(dl_equals_up) self.assertTrue(link_exist) self.assertTrue(ipns_urls_match) # Upload 2 data2 = dict( Manifest({ 'task_bid_price': 999999, 'request_type': 'image_label_binary', 'job_total_tasks': 30010 }).serialize()) (hash_, manifest_url) = upload(data2, pub_key, name) manifest_dict = download(ipns_id, job.gas_payer_priv) dl_equals_up = manifest_dict == data2 link_exist = ipns_link_exists(name) ipns_urls_match = ipns_id == get_ipns_link(name).split('/')[-1] self.assertTrue(dl_equals_up) self.assertTrue(link_exist) self.assertTrue(ipns_urls_match)
def validate_manifest(manifest_url): # noqa: E501 """Validates a manifest provided by a public URL Validates a manifest provided by a public URL # noqa: E501 :param manifest_url: Publicly available manifest URL :type manifest_url: str :rtype: ManifestValidityResponse """ try: req = Request(manifest_url) req.add_header( "User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36" ) req.add_header("X-Requested-With", "XMLHttpRequest") data = urlopen(req).read() model = json.loads(data) except Exception as e: return ErrorNotexistResponse(str(e)), 404 try: Manifest(model).validate() return ManifestValidityResponse(True), 200 except Exception as e: return ManifestValidityResponse( False, [ErrorParameterResponse(str(e), manifest_url)]), 200
def _init_job(self, manifest: Manifest): """Initialize a Job's class attributes with a given manifest. Args: manifest (Manifest): a dict representation of the Manifest model. """ serialized_manifest = dict(manifest.serialize()) per_job_cost = Decimal(serialized_manifest['task_bid_price']) number_of_answers = int(serialized_manifest['job_total_tasks']) self.serialized_manifest = serialized_manifest self.amount = Decimal(per_job_cost * number_of_answers)
def get_contract_from_address(escrow_address: str, private_key: bytes, gas=DEFAULT_GAS) -> Contract: wcontract = get_job_from_address(escrow_address) url = _getURL(wcontract, gas=gas) manifest_dict = download(url, private_key) contract_m = Manifest(manifest_dict) contract = Contract(contract_m) task_bid = Decimal(manifest_dict['task_bid_price']) number_of_tasks = int(manifest_dict['job_total_tasks']) contract.oracle_stake = int( _convert_to_hmt_cents(Decimal(manifest_dict['oracle_stake']))) contract.amount = int(_convert_to_hmt_cents(task_bid) * number_of_tasks) contract.initialize(wcontract, contract.amount, contract.oracle_stake, number_of_tasks) return contract
def _access_job(self, factory_addr: str, escrow_addr: str, **credentials): """Given a factory and escrow address and credentials, access an already launched manifest of an already deployed escrow contract. Args: factory_addr (str): an ethereum address of the escrow factory contract. escrow_addr (str): an ethereum address of the escrow contract. **credentials: an unpacked dict of an ethereum address and its private key. """ gas_payer = credentials["gas_payer"] rep_oracle_priv_key = credentials["rep_oracle_priv_key"] self.factory_contract = get_factory(factory_addr) self.job_contract = get_escrow(escrow_addr) self.manifest_url = manifest_url(self.job_contract, gas_payer) self.manifest_hash = manifest_hash(self.job_contract, gas_payer) manifest_dict = self.manifest(rep_oracle_priv_key) escrow_manifest = Manifest(manifest_dict) self._init_job(escrow_manifest)
def new_job(body=None): # noqa: E501 """Creates a new Job and returns the address Creates a new job and returns the address # noqa: E501 :param body: :type body: dict | bytes :rtype: StringDataResponse """ if connexion.request.is_json: body = JobCreateBody.from_dict( connexion.request.get_json()) # noqa: E501 try: req = Request(body.manifest_url) req.add_header( "User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36" ) req.add_header("X-Requested-With", "XMLHttpRequest") data = urlopen(req).read() manifest = json.loads(data) job = Job( { "gas_payer": body.gas_payer, "gas_payer_priv": body.gas_payer_private }, Manifest(manifest), body.factory_address) except Exception as e: return ErrorParameterResponse( str(e), "manifest_url or gas_payer_private"), 401 try: job.launch(bytes(body.rep_oracle_pub, encoding="utf-8")) job.setup() return StringDataResponse(job.job_contract.address), 200 except Exception as e: return ErrorParameterResponse(str(e), "rep_oracle_pub_key"), 401