def type_check(tx, txs): if not tools.E_check(tx, 'type', [str, unicode]): out[0] += 'blockchain type' return False if tx['type'] not in transactions.tx_check: out[0] += 'bad type' return False return True
def block_check(block, DB): def log_(txt): pass #return tools.log(txt) def tx_check(txs): start = copy.deepcopy(txs) out = [] start_copy = [] while start != start_copy: if start == []: return False # Block passes this test start_copy = copy.deepcopy(start) if transactions.tx_check[start[-1]['type']](start[-1], out, [''], DB): out.append(start.pop()) else: return True # Block is invalid return True # Block is invalid if not isinstance(block, dict): return False if 'error' in block: return False if not tools.E_check(block, 'length', [int]): log_('no length') return False length = DB['length'] if int(block['length']) != int(length) + 1: log_('wrong longth') return False if block['diffLength'] != hexSum(DB['diffLength'], hexInvert(block['target'])): log_('diflength error') return False if length >= 0: if tools.det_hash(tools.db_get(length, DB)) != block['prevHash']: log_('det hash error') return False a = copy.deepcopy(block) a.pop('nonce') if u'target' not in block.keys(): log_('target error') return False half_way = {u'nonce': block['nonce'], u'halfHash': tools.det_hash(a)} if tools.det_hash(half_way) > block['target']: log_('det hash error 2') return False if block['target'] != target.target(DB, block['length']): log_('wrong target') return False earliest = median(recent_blockthings('times', DB, custom.mmm)) if 'time' not in block: log_('no time') return False if block['time'] > time.time()+60*6: log_('too late') return False if block['time'] < earliest: log_('too early') return False if tx_check(block['txs']): log_('tx check') return False return True
def type_check(tx, txs): if not tools.E_check(tx, 'type', [str, unicode]): out[0] += 'Blockchain type' return False # mint type cannot be added to database if tx['type'] == 'mint': return False # type is not recognized if tx['type'] not in transactions.tx_check: out[0] += 'Bad type' return False return True
def block_check(block, DB): def log_(txt): pass #return tools.log(txt) def tx_check(txs): start = copy.deepcopy(txs) start.reverse() out = [] start_copy = [] error_msg = [''] while True: if start == []: return False # Block passes this test if transactions.tx_check[start[-1]['type']](start[-1], out, error_msg, DB): out.append(start.pop()) else: log_('bad block: ' + str(txs)) log_('error message: ' + str(error_msg)) return True # Block is invalid return True if not isinstance(block, dict): return False if 'error' in block: return False if not tools.E_check(block, 'length', [int]): log_('no length') return False length = tools.db_get('length') if type(block['length']) != type(1): log_('wrong length type') return False if int(block['length']) != int(length) + 1: log_('wrong longth') return False if block['diffLength'] != hexSum(tools.db_get('diffLength'), hexInvert(block['target'])): log_('diflength error') return False if length >= 0: if tools.det_hash(tools.db_get(length, DB)) != block['prevHash']: log_('det hash error') return False if u'target' not in block.keys(): log_('target error') return False half_way = tools.make_half_way(block) if tools.det_hash(half_way) > block['target']: log_('det hash error 2') return False if block['target'] != target.target(block['length']): log_('block: ' + str(block)) log_('target: ' + str(target.target(block['length']))) log_('wrong target') return False earliest = median(recent_blockthings('times', custom.mmm)) if 'time' not in block: log_('no time') return False if block['time'] > time.time() + 60 * 6: log_('too late') return False if block['time'] < earliest: log_('too early') return False if tx_check(block['txs']): log_('tx check') return False return True
def block_check(block): def log_(txt): pass # return tools.log(txt) def check_txs(txs): start = copy.deepcopy(txs) out = [] start_copy = [] # until starting copy of transactions are empty while start != start_copy: if start == []: return False # Block passes this test start_copy = copy.deepcopy(start) # if transaction is valid, then add the transaction to the out list if transactions.tx_check[start[-1]['type']](start[-1], out, ['']): out.append(start.pop()) else: return True # Block is invalid return True # Block is invalid # if block is not a dict, return false if not isinstance(block, dict): return False # block contains error if 'error' in block: return False # E_check function is responsible for checking the block(dict) if it has a length attribute which is int if not tools.E_check(block, 'length', [int]): log_('no length') return False # get the length of blockchain length = tools.db_get('length') # check length if it is integer, yeah yeah we have done that so what? if type(block['length']) != type(1): log_('wrong length type') return False # check length condition. It must be one bigger than our current blockchain length, or we would be missing something if int(block['length']) != int(length) + 1: log_('wrong longth') return False # checking if prevHash was actually the previous block's hash if length >= 0: if tools.det_hash(tools.db_get(length)) != block['prevHash']: log_('det hash error') return False # --------------------- START OF THE NEW PROOF-OF-WORK CHECK--------------------------------- # Returns a dictionary with auth_sign and halfHash of block. # Authority must sign the block with its pubkey. This way, we are going to know which authority signed the block half_way = tools.make_half_way(block) if not half_way.get('auth_pubkey') in tools.db_get('authorities'): print 'no auth pubkey' return False if not tools.verify(half_way.get('halfHash'), half_way.get('auth_sign'), half_way.get('auth_pubkey')): print 'no verify' return False # --------------------- END OF THE NEW PROOF-OF-WORK CHECK----------------------------------- # recent_blockthings returns a map with get_val function and range starting from 100 early blocks to most recent block # then earliest is the median of sorted blocks earliest = median(recent_blockthings('times', custom.mmm)) # time has to be present in block if 'time' not in block: log_('no time') return False # it is late to check this block if block['time'] > time.time() + 60 * 6: log_('too late') return False # block does not seem to be created at correct time if block['time'] < earliest: log_('too early') return False # check the transactions in the block # this function returns True on negative situation, because f**k the logic! # please someone fix this. I left it for now to show how a joken source this project is! if check_txs(block['txs']): log_('tx check') return False return True