def validate_block(block : dict) -> tuple: block_contents = json.loads(block['contents']) """Ensure a block is to the appropriate destination, of the minimum amount, etc""" if block_contents['link_as_account'] != AppConfig.MONKEYTALKS_ACCOUNT: return (False, "Transaction wasnt sent to MonkeyTalks account") elif int(block['amount']) - FeeModel.get_fee() <= 0: return (False, "Transaction amount wasn't enough to cover fee") elif not Nanote().validate_message(block['amount']): return (False, "Message has invalid checksum - can't be decoded") return (True, "Valid")
def check_missings(): """Browse acount history for items missing in our database""" # Only run this job once/per process, use a distributed lock for that try: lock = rd.lock(lock_key, timeout=300) have_lock = lock.acquire(blocking=False) if have_lock: rpc = RPC() app = scheduler.app with app.app_context(): account = app.config['MONKEYTALKS_ACCOUNT'] resp = rpc.account_history(account, count=200) if resp is None: return # List of hashes to check (links on receive blocks) links = [] linkObjs = [] for item in resp: if item["subtype"] == "receive": linkObj = { "link": item["link"], "amount": item["amount"], "account": item["account"] } links.append(item["link"]) linkObjs.append(linkObj) # Check links we don't have - remove duplicates for item in Message.select().where( Message.block_hash.in_(links)): links.remove(item.block_hash) # Validate, save and emit messages for obj in linkObjs: if obj['link'] not in links: continue if int(obj['amount']) - FeeModel.get_fee() <= 0: continue elif not Nanote().validate_message(obj['amount']): continue # Is a valid message msg = Message.save_as_message(obj['amount'], obj['link'], obj['account'], account) if msg is not None: emit('new_message', json.dumps(Message.format_message(msg)), namespace='/mtchannel', broadcast=True) finally: lock.release()
def save_as_message(cls, amount: str, block_hash: str, account: str, destination: str): premium = False if int(amount) - FeeModel().get_premium_fee() > 0: premium = True message = Message( block_hash = block_hash, destination = destination, message_in_raw = str(int(amount)), created_at = datetime.datetime.utcnow(), premium = premium, address = account ) if message.save() > 0: message.inc_message_count() return message return None
def get_fees(): return jsonify({ 'fee': str(FeeModel.get_fee()), 'premium': str(FeeModel.get_premium_fee()) })
def index(path): return render_template('app.html', mt_account=AppConfig.MONKEYTALKS_ACCOUNT, fee=FeeModel.get_fee(), premium=FeeModel.get_premium_fee())