def peer_check(peer, DB): peers=tools.db_get('peers') if peers[peer]['length']==0 or random.random()<0.1: ask_for_count(peer) out=trade_peers(peer) if type(out)==dict and 'error' in out: return 1 peers=tools.db_get('peers') length = tools.db_get('length') diffLength= tools.db_get('diffLength') size = max(len(diffLength), len(peers[peer]['diffLength'])) us = tools.buffer_(diffLength, size) them = tools.buffer_(peers[peer]['diffLength'], size) if them < us: return give_block(peer, DB, peers[peer]['length']) elif us == them: try: ask_for_count(peer) trade_peers(peer) return ask_for_txs(peer, DB) except Exception as exc: tools.log('ask for tx error') tools.log(exc) else: return download_blocks(peer, DB, peers[peer]['length'], length)
def peer_check(i, peers, DB): peer=peers[i][0] block_count = cmd(peer, {'type': 'blockCount'}) if not isinstance(block_count, dict): return if 'error' in block_count.keys(): return peers[i][2]=block_count['diffLength'] peers[i][3]=block_count['length'] length = tools.db_get('length') diffLength= tools.db_get('diffLength') size = max(len(diffLength), len(block_count['diffLength'])) us = tools.buffer_(diffLength, size) them = tools.buffer_(block_count['diffLength'], size) if them < us: give_block(peer, DB, block_count['length']) elif us == them: try: ask_for_txs(peer, DB) except Exception as exc: tools.log('ask for tx error') tools.log(exc) else: download_blocks(peer, DB, block_count, length) F=False my_peers=tools.db_get('peers_ranked') their_peers=cmd(peer, {'type':'peers'}) if type(my_peers)==list: for p in their_peers: if p not in my_peers: F=True my_peers.append(p) if F: tools.db_put('peers_ranked', my_peers)
def peer_check(peer, DB): def cmd(x): return networking.send_command(peer, x) def download_blocks(peer, DB, peers_block_count, length): def fork_check(newblocks, DB): block = blockchain.db_get(DB['length'], DB) recent_hash = tools.det_hash(block) their_hashes = map(tools.det_hash, newblocks) return recent_hash not in their_hashes def bounds(length, peers_block_count): if peers_block_count['length'] - length > custom.download_many: end = length + custom.download_many - 1 else: end = peers_block_count['length'] return [max(length - 2, 0), end] blocks = cmd({'type': 'rangeRequest', 'range': bounds(length, peers_block_count)}) if not isinstance(blocks, list): return [] for i in range(2): # Only delete a max of 2 blocks, otherwise a # peer might trick us into deleting everything over and over. if fork_check(blocks, DB): blockchain.delete_block(DB) DB['suggested_blocks'].extend(blocks) return def ask_for_txs(peer, DB): txs = cmd({'type': 'txs'}) DB['suggested_txs'].extend(txs) pushers = [x for x in DB['txs'] if x not in txs] for push in pushers: cmd({'type': 'pushtx', 'tx': push}) return [] def give_block(peer, DB, block_count): cmd({'type': 'pushblock', 'block': blockchain.db_get(block_count['length'] + 1, DB)}) return [] block_count = cmd({'type': 'blockCount'}) if not isinstance(block_count, dict): return if 'error' in block_count.keys(): return length = DB['length'] size = max(len(DB['diffLength']), len(block_count['diffLength'])) us = tools.buffer_(DB['diffLength'], size) them = tools.buffer_(block_count['diffLength'], size) if them < us: return give_block(peer, DB, block_count) if us == them: return ask_for_txs(peer, DB) return download_blocks(peer, DB, block_count, length)
def peer_check(peer, DB): block_count = cmd(peer, {'type': 'blockCount'}) if not isinstance(block_count, dict): return if 'error' in block_count.keys(): return length = DB['length'] size = max(len(DB['diffLength']), len(block_count['diffLength'])) us = tools.buffer_(DB['diffLength'], size) them = tools.buffer_(block_count['diffLength'], size) if them < us: return give_block(peer, DB, block_count['length']) if us == them: return ask_for_txs(peer, DB) return download_blocks(peer, DB, block_count, length)
def send_msg(data, sock): data=tools.package(data) data=tools.buffer_(str(len(data)), 5)+data while data: time.sleep(0.0001) try: sent = sock.send(data) except: return 'peer died' data = data[sent:] return 0
def hexInvert(n): # Use double-size for division, to reduce information leakage. return tools.buffer_(str(hex(int('f' * 128, 16) / int(n, 16)))[2: -1], 64)
def hexSum(a, b): # Sum of numbers expressed as hexidecimal strings return tools.buffer_(str(hex(int(a, 16)+int(b, 16)))[2: -1], 64)
def targetTimesFloat(target, number): a = int(str(target), 16) b = int(a * number) return tools.buffer_(str(hex(b))[2:-1], 64)
def targetTimesFloat(target, number): a = int(str(target), 16) b = int(a * number) return tools.buffer_(str(hex(b))[2: -1], 64)
def hexSum(a, b): # Sum of numbers expressed as hexidecimal strings return tools.buffer_(str(hex(int(a, 16) + int(b, 16)))[2:-1], 64)
def hexInvert(n): # Use double-size for division, to reduce information leakage. return tools.buffer_(str(hex(int('f' * 128, 16) / int(n, 16)))[2:-1], 64)
def targetTimesFloat(target, number): a = int(str(target), 16) b = int( a * number ) # this should be rational multiplication followed by integer estimation return tools.buffer_(str(hex(b))[2:-1], 64)
def targetTimesFloat(target, number): a = int(str(target), 16) b = int(a * number)#this should be rational multiplication followed by integer estimation return tools.buffer_(str(hex(b))[2: -1], 64)
def peer_check(peer, DB): def cmd(x): return networking.send_command(peer, x) def download_blocks(peer, DB, peers_block_count, length): def fork_check(newblocks, DB): length = copy.deepcopy(DB['length']) block = blockchain.db_get(length, DB) recent_hash = tools.det_hash(block) their_hashes = map(tools.det_hash, newblocks) return recent_hash not in map(tools.det_hash, newblocks) def bounds(length, peers_block_count, DB): if peers_block_count['length'] - length > custom.download_many: end = length + custom.download_many - 1 else: end = peers_block_count['length'] return [max(length - 2, 0), end] blocks = cmd({ 'type': 'rangeRequest', 'range': bounds(length, peers_block_count, DB) }) if type(blocks) != type([1, 2]): return [] for i in range(10): # Only delete a max of 20 blocks, otherwise a # peer might trick us into deleting everything over and over. if fork_check(blocks, DB): blockchain.delete_block(DB) DB['suggested_blocks'] += blocks return def ask_for_txs(peer, DB): txs = cmd({'type': 'txs'}) for tx in txs: DB['suggested_txs'].append(tx) pushers = [x for x in DB['txs'] if x not in txs] for push in pushers: cmd({'type': 'pushtx', 'tx': push}) return [] def give_block(peer, DB, block_count): cmd({ 'type': 'pushblock', 'block': blockchain.db_get(block_count['length'] + 1, DB) }) return [] block_count = cmd({'type': 'blockCount'}) if type(block_count) != type({'a': 1}): return if 'error' in block_count.keys(): return length = DB['length'] size = max(len(DB['diffLength']), len(block_count['diffLength'])) us = tools.buffer_(DB['diffLength'], size) them = tools.buffer_(block_count['diffLength'], size) if them < us: return give_block(peer, DB, block_count) if us == them: return ask_for_txs(peer, DB) return download_blocks(peer, DB, block_count, length)