def sync_block(db, block): parser = MemechainParser(block) parser.collect_memetxs() memetxs = parser.return_memetxs() if memetxs: prev_block_memes = db.get_prev_block_memes() for meme in memetxs: memetx = MemeTx(meme['ipfs_id']) memetx.generate_hashlink(prev_block_memes) memetx.txid = meme['txid'] Validate(memetx, db=db, ipfs_dir=config['DATA_DIR'], prev_block_memes=prev_block_memes, sync=True) if memetx.is_meme_valid(): meme_filepath = IPFSTools().get_meme(meme['ipfs_id'], config['DATA_DIR']) ext = meme_filepath.split(".")[-1] if ext in ["jpg", "png", "gif"]: db.add_meme( **{ "ipfs_id": meme['ipfs_id'], "hashlink": meme['hashlink'], "txid": meme['txid'], "author": meme['author'], "block": block, "imgformat": ext }) logger.info( 'COMMAND %s Success %s: %s' % ('Sync', 'Memechain', "Meme %s added to database." % meme['ipfs_id'])) else: # Delete invalid Meme os.remove(meme_filepath) logger.info('COMMAND %s Failed %s: %s' % ('Sync', 'Memechain', "Invalid Meme image extension %s." % ext)) else: logger.info('COMMAND %s Failed %s: %s' % ('Sync', 'Memechain', "Invalid MemeTx %s." % meme['ipfs_id'])) else: logger.info( 'COMMAND %s Failed %s: %s' % ('Sync', 'Memechain', "No Meme TXs found in block %s." % block))
def sync_block(db, block): parser = MemechainParser(block) parser.collect_memetxs() memetxs = parser.return_memetxs() if memetxs: prev_block_memes = db.get_prev_block_memes() for meme in memetxs: memetx = MemeTx(meme['ipfs_id']) memetx.generate_hashlink(prev_block_memes) memetx.txid = meme['txid'] Validate(memetx, db=db, ipfs_dir=config['DATA_DIR'], prev_block_memes=prev_block_memes, sync=True) valid_state = memetx.is_meme_valid() if valid_state == -1: meme_filepath = IPFSTools().get_meme(meme['ipfs_id'], config['DATA_DIR']) ext = meme_filepath.split(".")[-1] if db.search_by_ipfs_id(meme['ipfs_id']): db.update_meme(meme['ipfs_id'], block) logger.info( 'COMMAND %s Success %s: %s' % ('Sync', 'Memechain', "Meme %s update in database." % meme['ipfs_id'])) else: db.add_meme( **{ "ipfs_id": meme['ipfs_id'], "hashlink": meme['hashlink'], "txid": meme['txid'], "author": meme['author'], "block": block, "imgformat": ext, "status": "confirm" }) logger.info( 'COMMAND %s Success %s: %s' % ('Sync', 'Memechain', "Meme %s added to database." % meme['ipfs_id'])) elif valid_state != 3: meme_filepath = IPFSTools().get_meme(meme['ipfs_id'], config['DATA_DIR']) os.remove(meme_filepath) logger.info('COMMAND %s Failed %s: %s' % ('Sync', 'Memechain', "Invalid MemeTx %s." % meme['ipfs_id'])) else: if config["ENABLE_LOG_MEMTX_NOT_FOUND"]: logger.info('COMMAND %s Failed %s: %s' % ('Sync', 'Memechain', "No Meme TXs found in block %s." % block))
check_files_status(db) # Check blockheight block_height = get_block_height() if db.get_memechain_height() == 0: # Load genesis meme genesis_meme = GenesisMeme() memetx = MemeTx(genesis_meme.genesis_ipfs_id) memetx.generate_genesis_hashlink() memetx.txid = genesis_meme.genesis_txid Validate(memetx, db=db, ipfs_dir=config['DATA_DIR'], prev_block_memes=[], sync=True, genesis=True) # Add genesis meme to database db.add_meme( **{ "ipfs_id": genesis_meme.get_ipfs_id(), "hashlink": genesis_meme.get_hashlink(), "txid": genesis_meme.genesis_txid, "author": genesis_meme.genesis_author, "block": genesis_meme.genesis_kekcoin_block, "imgformat": genesis_meme.genesis_img_format, "status": "confirm" })
def on_post(self, req, resp): logger.info('COMMAND %s Received' % self.__class__.__name__) db = MemeChainDB(os.path.join(config['DATA_DIR'], 'memechain.json')) while check_running(): time.sleep(3) logger.info('Waiting for the synchronization process to complete add_meme') # Generate random placeholder img name img_placeholder_name = str(random.random()).split(".")[1] ext = mimetypes.guess_extension(req.content_type) if ext == '.jpe': ext = '.jpg' name = '{img_name}{ext}'.format(img_name=img_placeholder_name, ext=ext) image_path = os.path.join(config['DATA_DIR'], name) # Write image to local storage with io.open(image_path, 'wb') as image_file: while True: chunk = req.stream.read(self._CHUNK_SIZE_BYTES) if not chunk: break image_file.write(chunk) if magic.from_file(image_path).lower().find(mimetypes.guess_extension(req.content_type)[1:]) < 0: raise falcon.HTTPBadRequest( "Memechain error", "Meme has not passed validation, file extension is wrong.") # Check file size meme_filesize = os.path.getsize(image_path) * 0.000001 # in MB if meme_filesize > 10: os.remove(image_path) logger.error('COMMAND %s Failed %s: %s' % (self.__class__.__name__, 'Upload Error', "Meme filesize too large.")) raise falcon.HTTPError(falcon.HTTP_400, "Upload error", "Meme filesize too large.") # Add image to ipfs ipfs_id = IPFSTools().add_meme(image_path)['Hash'] # Rename local img file to ipfs_id for easy reference new_name = '{img_name}{ext}'.format(img_name=ipfs_id, ext=ext) os.rename(image_path, os.path.join(config['DATA_DIR'], new_name)) # Add to Kekcoin chain memetx = MemeTx(ipfs_id) prev_block_memes = db.get_prev_block_memes() if prev_block_memes: memetx.generate_hashlink(prev_block_memes) try: Validate(memetx, db=db, ipfs_dir=config['DATA_DIR'], prev_block_memes=prev_block_memes) except TypeError as e: # Delete invalid Meme os.remove(os.path.join(config['DATA_DIR'], new_name)) logger.error('COMMAND %s Failed %s: %s' % (self.__class__.__name__, 'Memechain Error', "Meme has not passed memechain validation, file extension not supported.%s") ) raise falcon.HTTPError(falcon.HTTP_400, "Memechain error", "Meme has not passed validation, file extension not supported.%s" % e ) if memetx.is_meme_valid(): memetx.blockchain_write() db.add_meme(**{"ipfs_id": ipfs_id, "hashlink": memetx.get_hashlink(), "txid": memetx.get_txid(), "author": memetx.get_author(), "block": 0, "imgformat": ext[1:], "status": "unconfirm"}) resp.status = falcon.HTTP_201 resp.set_header('Powered-By', 'Memechain') logger.info('COMMAND %s Success' % self.__class__.__name__) resp.body = json.dumps({ 'success': True, 'result': {'ipfs_id' : ipfs_id, 'txid' : memetx.get_txid(), 'hashlink' : memetx.get_hashlink(), 'author' : memetx.get_author()}}) else: # Delete invalid Meme os.remove(os.path.join(config['DATA_DIR'], new_name)) logger.error('COMMAND %s Failed %s: %s' % (self.__class__.__name__, 'Memechain Error', "Meme has not passed memechain validation: ")) raise falcon.HTTPError(falcon.HTTP_400, "Memechain error", "Meme has not passed validation: ") else: # Genesis block logic memetx = MemeTx(ipfs_id) memetx.generate_genesis_hashlink() memetx.blockchain_write() resp.status = falcon.HTTP_201 resp.set_header('Powered-By', 'Memechain') logger.info('COMMAND %s Success' % self.__class__.__name__) resp.body = json.dumps({ 'success': True, 'result': {'ipfs_id' : ipfs_id, 'txid' : memetx.get_txid(), 'hashlink' : memetx.get_hashlink(), 'author' : memetx.get_author()}})
def on_post(self, req, resp): logger.info('COMMAND %s Received' % self.__class__.__name__) db = MemeChainDB(os.path.join(config['DATA_DIR'], 'memechain.json')) # Generate random placeholder img name img_placeholder_name = random.random().split(".")[1] ext = mimetypes.guess_extension(req.content_type) name = '{img_name}{ext}'.format(img_name=img_placeholder_name, ext=ext) image_path = os.path.join(config['DATA_DIR'], name) # Write image to local storage with io.open(image_path, 'wb') as image_file: while True: chunk = req.stream.read(self._CHUNK_SIZE_BYTES) if not chunk: break image_file.write(chunk) # Add image to ipfs ipfs_id = IPFSTools().add_meme(imgage_path)['Hash'] # Rename local img file to ipfs_id for easy reference new_name = '{img_name}{ext}'.format(img_name=ipfs_id, ext=ext) os.rename(image_path, os.path.join(config['DATA_DIR'], new_name)) # Add to Kekcoin chain memetx = MemeTx(ipfs_id) prev_block_memes = db.get_prev_block_memes() if prev_block_memes: memetx.generate_hashlink(prev_block_memes) Validate(memetx, db=db, ipfs_dir=config['DATA_DIR'], prev_block_memes=prev_block_memes) if memetx.is_meme_valid(): memetx.blockchain_write() resp.status = falcon.HTTP_201 resp.set_header('Powered-By', 'Memechain') logger.info('COMMAND %s Success' % self.__class__.__name__) resp.body = json.dumps({ 'success': True, 'result': ipfs_id}) else: logger.error('COMMAND %s Failed %s: %s' % (self.__class__.__name__, 'Memechain Error', "Meme has not passed memechain validation.")) raise falcon.HTTPError(falcon.HTTP_400, "Memechain error", "Meme has not passed " + " validation.") else: # Genesis block logic memetx = MemeTx(ipfs_id) meme.generate_genesis_hashlink() memetx.blockchain_write() resp.status = falcon.HTTP_201 resp.set_header('Powered-By', 'Memechain') logger.info('COMMAND %s Success' % self.__class__.__name__) resp.body = json.dumps({ 'success': True, 'result': ipfs_id})