async def generate_bounties(self, chain): """Submit bounty from the filesystem Args: chain (str): Chain sample is being requested from """ amount = await self.client.bounties.parameters[chain].get('bounty_amount_minimum') while True: try: filename = random.choice(self.artifacts) logger.info('Submitting file %s', filename) ipfs_uri = await self.client.post_artifacts([(filename, None)]) if not ipfs_uri: logger.error('Error uploading artifact to IPFS, continuing') continue metadata = BountyMetadata() with open(filename, 'rb') as f: computed = Ambassador.generate_metadata(f.read()) metadata.add_file_artifact(computed['mimetype'], filename=filename, filesize=str(computed['size']), sha256=computed['sha256'], sha1=computed['sha1'], md5=computed['md5']) await self.push_bounty(ArtifactType.FILE, amount, ipfs_uri, BOUNTY_TEST_DURATION_BLOCKS, chain, metadata=metadata.json()) except CancelledError: logger.warning('Cancel requested') break except Exception: logger.exception('Exception in bounty generation task, continuing') continue
def post_assertion_metadata(): config = app.config['POLYSWARMD'] session = app.config['REQUESTS_SESSION'] body = request.get_json() loaded_body = json.loads(body) if isinstance(body, str) else body try: if not AssertionMetadata.validate(loaded_body, silent=True) and \ not BountyMetadata.validate(loaded_body, silent=True): return failure('Invalid metadata', 400) except json.JSONDecodeError: # Expected when people provide incorrect metadata. Not stack worthy return failure('Invalid Assertion metadata', 400) try: uri = config.artifact.client.add_artifact(json.dumps(loaded_body), session, redis=config.redis.client) response = success(uri) except HTTPError as e: response = failure(e.response.content, e.response.status_code) except ArtifactException as e: response = failure(e.message, 500) except Exception: logger.exception('Received error posting to IPFS got response') response = failure('Could not add metadata to ipfs', 500) return response
async def generate_bounties(self, chain): """Submit either the EICAR test string or a benign sample Args: chain (str): Chain sample is being requested from """ amount = await self.client.bounties.parameters[chain].get( 'bounty_amount_minimum') while True: try: filename, content = random.choice(ARTIFACTS) logger.info('Submitting %s', filename) ipfs_uri = await self.client.post_artifacts([(filename, content)]) if not ipfs_uri: logger.error( 'Error uploading artifact to IPFS, continuing') continue computed = Ambassador.generate_metadata(content) metadata = BountyMetadata().add_file_artifact( mimetype=computed['mimetype'], filename=filename, filesize=str(computed['size']), sha256=computed['sha256'], sha1=computed['sha1'], md5=computed['md5']) await self.push_bounty(ArtifactType.FILE, amount, ipfs_uri, BOUNTY_TEST_DURATION_BLOCKS, chain, metadata=metadata.json()) except CancelledError: logger.info('Cancel requested') break except Exception: logger.exception( 'Exception in bounty generation task, continuing') continue
def pad_metadata(metadata, min_length): """ Pad out the metadata list with None values to match a given length Args: metadata (list[dict|None]): List of metadata dicts min_length (int): Min size for the metadata list after padding Returns: list of metadata dicts, or None values """ result = metadata if not metadata or not Bounty.validate(metadata): result = [{}] * min_length elif len(metadata) < min_length: result.extend([{}] * (min_length - len(metadata))) logger.info('Padded result %s:', result) return result