def handle_get_block(self, msg): datas = msg.get("data", "") log.info("------client handle_get_block from " + str(self.ip) + "------") log.info("------with data " + str(datas) + "------") bc = BlockChain() log.info("------client deserialize block from peer------") try: st = StopMine() log.info('------works------') if st.ip != self.ip and st.ip: log.info('------not ip ' + str(st.ip) + '------') t = threading.Thread(target=self.shake_loop(), args=()) t.start() return except: log.info('------failed to stop mine------') try: last_block = None while not last_block: last_block = bc.get_last_block() time.sleep(1) last_height = last_block.block_header.height for i in range(len(datas) - 1, -1, -1): log.info("roll back others' block at " + str(i) + str(block)) block = Block.deserialize(datas[i]) if block.block_header.height > last_height: log.info("last >> at " + str(last_height)) last_height -= 1 continue elif block.block_header.height == last_height: last_height -= 1 local_block = None while not local_block: local_block = bc.get_block_by_height( block.block_header.height) time.sleep(1) if local_block != block: log.info("not the same ") log.info("local" + str(local_block)) log.info("block " + str(block)) utxo_set = UTXOSet() utxo.roll_back(local_block) bc.roll_back() else: log.info("the same at " + str(local_block.block_header.height)) break else: log.info("local >> " + str(last_block.block_header.height)) t = threading.Thread(target=self.shake_loop(), args=()) t.start() return last_height = last_block.block_header.height for data in datas: block = Block.deserialize(data) if block.block_header.height <= last_height: continue elif block.block_header.height == last_height + 1: last_height += 1 bc.add_block_from_peers(block, last_block) log.info("c add block from peers " + str(block)) last_block = block t = threading.Thread(target=self.shake_loop(), args=()) t.start() except: log.info("------client handle_get_block failed------") t = threading.Thread(target=self.shake_loop(), args=()) t.start()
query = { "selector": { "_id": { "$regex": "^UTXO" }, "pub_key_hash": address } } docs = self.db.find(query) utxos = [] for doc in docs: index = doc.get("index", None) if index is None: continue doc_id = doc.id txid_index_str = doc_id.replace(self.FLAG, "") _flag_index = txid_index_str.find("-") txid = txid_index_str[:_flag_index] ftxo = FullTXOutput(txid, TXOutput.deserialize(doc), index) utxos.append(ftxo) return utxos if __name__ == "__main__": utxo = UTXOSet() from block_chain import BlockChain bc = BlockChain() last_block = bc.get_last_block() utxo.roll_back(last_block) bc.roll_back()
def handle_synchronize(self, msg, conn, addr): datas = msg.get("data", "") log.info("------s handle_synchronize from " + str(addr) + "------") log.info("------with data " + str(datas) + "------") bc = BlockChain() try: st = StopMine() log.info('------works------') if st.ip != addr and st.ip: log.info('not ip ' + str(st.ip) + '------') return Msg(Msg.NONE_MSG, "") log.info('------is the highest ' + str(st.ip) + "------") except: return Msg(Msg.NONE_MSG, "") try: last_block = None while not last_block: last_block = bc.get_last_block() time.sleep(1) last_height = last_block.block_header.height for i in range(len(datas) - 1, -1, -1): block = Block.deserialize(datas[i]) log.info("roll back others' block at " + str(i) + str(block)) if block.block_header.height > last_height: last_height -= 1 continue elif block.block_header.height == last_height: last_height -= 1 local_block = None while not local_block: local_block = bc.get_block_by_height( block.block_header.height) time.sleep(1) if local_block != block: log.info("not the same ") log.info("local" + str(local_block)) log.info("block " + str(block)) utxo_set = UTXOSet() utxo.roll_back(local_block) bc.roll_back() else: log.info("the same at " + str(local_block.block_header.height)) break else: log.info("local >> " + str(last_block.block_header.height)) msg = Msg(Msg.NONE_MSG, "") return msg last_height = last_block.block_header.height for data in datas: block = Block.deserialize(data) if block.block_header.height <= last_height: continue elif block.block_header.height == last_height + 1: last_height += 1 bc.add_block_from_peers(block, last_block) log.info("s add block from peers " + str(block)) last_block = block msg = Msg(Msg.NONE_MSG, "") return msg except: log.info( "------server handle_get_block failed get last block------") msg = Msg(Msg.NONE_MSG, "") return msg