def buy_shares_check(tx, txs, out, DB): #make sure that we can only buy the shares of undecided markets. if not transactions.signature_check(tx): out[0]+='signature check' return False a=copy.deepcopy(tx) a.pop('signatures') half_way=tools.make_half_way(a) if tools.det_hash(half_way)>custom.buy_shares_target: out[0]+='insufficient POW' return False if not E_check(tx, 'buy', list): out[0]+='buy error' return False if not E_check(tx, 'PM_id', [str, unicode]): out[0]+='pm id error' return False pm=tools.db_get(tx['PM_id'], DB) if 'decisions' not in pm: out[0]+='that is not a prediction market yet' return False if len(tx['buy'])!=len(pm['shares_purchased']): out[0]+='buy length error' return False stop=True for i in tx['buy']: if i!=0: stop=False if stop: out[0]+='you need to buy a non-zero amount of at least one share' return False if 'price_limit' in tx: price = txs_tools.cost_to_buy_shares(tx) if price>tx['price_limit']: out[0]+='that is outside the price limit for that tx '+str(price) + ' is bigger than ' +str(tx) return False for purchase in tx['buy']: if type(purchase)!=int: return False for i in range(len(tx['buy'])): if tx['buy'][i]+pm['shares_purchased'][i]<0: out[0]+='PM cannot have negative shares' return False if not txs_tools.fee_check(tx, txs, DB): out[0]+='fee check error' return False for dec in pm['decisions']: decision = tools.db_get(dec, DB) bad=True if decision['state'] not in ['yes', 'no']: bad=False if bad: out[0]+='this PM is already expired. you cannot buy shares.' 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 = [] 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 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
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 = 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