Example #1
0
 def handle_get_block(self, msg):
     height = msg.get("data", 1)
     block_chain = BlockChain()
     block = block_chain.get_block_by_height(height)
     data = block.serialize()
     msg = Msg(Msg.GET_BLOCK_MSG, data)
     return msg
Example #2
0
 def handle_synchronize(self, msg):  # 7.10
     synchronize_range = msg.get("data", 1)
     block_chain = BlockChain()
     data = []
     log.info("------client handle_synchronize with range " +
              str(synchronize_range[0]) + " " + str(synchronize_range[1]) +
              "------")
     for height in range(synchronize_range[0], synchronize_range[1]):
         already_get = False
         for i in range(0, 2):
             block = None
             try:
                 block = block_chain.get_block_by_height(height)
             except:
                 continue
             if block:
                 already_get = True
                 break
         if already_get:
             data.append(block.serialize())
         elif data:
             msg = Msg(Msg.SYNCHRONIZE_MSG, data)
             self.send(msg)
             return
         else:
             msg = Msg(Msg.NONE_MSG, "")
             self.send(msg)
             return
     msg = Msg(Msg.SYNCHRONIZE_MSG, data)
     self.send(msg)
Example #3
0
 def handle_shake(self, msg):
     log.info("------client handle_shake from " + str(self.ip) + "------")
     data = msg.get("data", "")
     last_height = data.get("last_height", 0)
     log.info("------with last height " + str(last_height) + "------")
     block_chain = BlockChain()
     block = block_chain.get_last_block()
     log.info('------c handle_sh ls_blo ' + str(block) + '------')
     if block:
         local_last_height = block.block_header.height
     else:
         local_last_height = -1
     log.info("client local_last_height %d, last_height %d" %
              (local_last_height, last_height))
     if local_last_height > last_height:
         try:
             st = StopMine()
             log.info('------works------')
             if st.h < local_last_height:
                 st.h = local_last_height
         except:
             log.info('------dont work------')
         log.info("------error shake------")
         log.info("client local_last_height %d, last_height %d" %
                  (local_last_height, last_height))
         send_data = []
         for i in range(1, local_last_height + 1):
             block = None
             while not block:
                 try:
                     block = block_chain.get_block_by_height(i)
                     time.sleep(2)
                 except:
                     time.sleep(2)
             send_data.append(block.serialize())
         msg = Msg(Msg.SYNCHRONIZE_MSG, send_data)
         self.send(msg)
         log.info("------client handle_shake send synchronize msg to" +
                  str(self.ip) + "------")
     elif local_last_height < last_height:
         try:
             st = StopMine()
             log.info('------works------')
             if st.h < last_height:
                 st.h = last_height
                 st.ip = self.ip
                 log.info("------" + str(self.ip) + ' is the highest ' +
                          str(st.h) + "------")
         except:
             log.info('------dont work------')
         start_height = 0 if local_last_height == -1 else local_last_height
         get_range = [start_height + 1, last_height + 1]
         send_msg = Msg(Msg.GET_BLOCK_MSG, get_range)
         self.send(send_msg)
     else:
         t = threading.Thread(target=self.shake_loop(), args=())
         t.start()
Example #4
0
 def handle_transaction(self, msg, conn, addr):  # 7.20
     log.info("------server handle_transaction------")  # 7.8
     tx_pool = TxPool()
     txs = msg.get("data", {})
     for tx_data in txs:
         log.info("------server handle_transaction: for------")  # 7.8
         tx = Transaction.deserialize(tx_data)
         is_new = True
         if tx_pool.is_new(tx):  # 7.20
             # 7.20
             log.info(
                 "------server never get this transaction before------")
             bc = BlockChain()
             ls_bl = bc.get_last_block()
             if ls_bl:
                 ls_height = ls_bl.block_header.height
                 for i in range(0, ls_height + 1):
                     while True:
                         block = None
                         try:
                             block = bc.get_block_by_height(i)
                         except:
                             continue
                         if block:
                             break
                     bc_txs = block._transactions
                     if bc_txs:
                         for transaction in bc_txs:
                             if transaction.txid == tx.txid:
                                 log.info("------old transaction------")
                                 log.info("------the id is: " +
                                          str(tx.txid) + "------")  # 7.20
                                 is_new = False
                                 # break
                             else:
                                 log.info("------brand new------")
                                 log.info("------the id is: " +
                                          str(tx.txid) + "------")  # 7.20
                     if not is_new:
                         break
             if is_new:
                 tx_pool.add(tx)
                 log.info("------server add this transaction------")
                 log.info("------the id is: " + str(tx.txid) + "------")
                 server1 = PeerServer()
                 server1.broadcast_tx(tx)
                 log.info("------server handle_transaction broadcast------")
     msg = Msg(Msg.NONE_MSG, "")
     return msg
Example #5
0
 def handle_miss(self, msg):
     log.info("------client handle_miss------")
     tx_pool = TxPool()
     txs = msg.get("data", {})
     for tx_data in txs:
         log.info("------server handle_miss: for------")
         tx = Transaction.deserialize(tx_data)
         is_new = True
         if tx_pool.is_new(tx):
             log.info("------client miss this transaction before------")
             bc = BlockChain()
             ls_bl = bc.get_last_block()
             log.info('------c handle_m ls_blo ' + str(ls_bl) + '------')
             if ls_bl:
                 ls_height = ls_bl.block_header.height
                 for i in range(0, ls_height + 1):
                     while True:
                         block = None
                         try:
                             block = bc.get_block_by_height(i)
                         except:
                             continue
                         if block:
                             break
                     bc_txs = block._transactions
                     if bc_txs:
                         for transaction in bc_txs:
                             if transaction.txid == tx.txid:
                                 log.info("------old transaction------")
                                 log.info("------the id is: " +
                                          str(tx.txid) + "------")
                                 is_new = False
                             else:
                                 log.info("------brand new miss------")
                                 log.info("------the id is: " +
                                          str(tx.txid) + "------")
                     if not is_new:
                         break
             if is_new:
                 tx_pool.add(tx)
                 log.info("------client miss add this transaction------")
                 log.info("------the id is: " + str(tx.txid) + "------")
                 log.info("------client handle_miss broadcast------")
     t = threading.Thread(target=self.shake_loop(), args=())
     t.start()
Example #6
0
 def handle_synchronize(self, msg):
     synchronize_range = msg.get("data", 1)
     block_chain = BlockChain()
     data = []
     log.info("------client handle_synchronize with range " +
              str(synchronize_range[0]) + " " + str(synchronize_range[1]) +
              "------")
     for height in range(1, synchronize_range[1]):
         block = None
         while not block:
             try:
                 block = block_chain.get_block_by_height(height)
                 time.sleep(2)
             except:
                 time.sleep(2)
         data.append(block.serialize())
     msg = Msg(Msg.SYNCHRONIZE_MSG, data)
     self.send(msg)
Example #7
0
 def handle_get_block(self, msg, conn, addr):
     log.info("------server handle_get_block from " + str(addr) + "------")
     get_range = msg.get("data", 1)
     log.info("------with range " + str(get_range[0]) + " " +
              str(get_range[1]) + "------")
     block_chain = BlockChain()
     data = []
     for height in range(1, get_range[1]):
         block = None
         while not block:
             try:
                 block = block_chain.get_block_by_height(height)
                 time.sleep(2)
             except:
                 time.sleep(2)
         data.append(block.serialize())
     msg = Msg(Msg.GET_BLOCK_MSG, data)
     log.info("------server send get_block msg" + str(data) + "------")
     return msg
Example #8
0
    def handle_transaction(self, msg):
        log.info("------client handle_transaction------")  # 7.8
        data = msg.get("data", {})
        tx = Transaction.deserialize(data)
        tx_pool = TxPool()
        is_new = True  # 7.20
        if tx_pool.is_new(tx):
            log.info(
                "------client never get this transaction before------")  # 7.20
            bc = BlockChain()
            ls_bl = bc.get_last_block()
            if ls_bl:
                ls_height = ls_bl.block_header.height
                for i in range(0, ls_height + 1):
                    while True:
                        block = None
                        try:
                            block = bc.get_block_by_height(i)
                        except:
                            continue
                        if block:
                            break
                    bc_txs = block._transactions
                    for transaction in bc_txs:
                        if transaction.txid == tx.txid:
                            is_new = False
                            break
                    if not is_new:
                        break
            if is_new:
                tx_pool.add(tx)
                log.info("------client handel_transaction txpool added------"
                         )  # 7.8
                server2 = PeerServer()
                server2.broadcast_tx(tx)
                log.info("------client handle_transaction broadcast------")

        msg = Msg(Msg.NONE_MSG, "")  # 7.23
        self.send(msg)  # 7.23
Example #9
0
    def handle_transaction(self, msg):
        log.info("------client handle_transaction------")
        data = msg.get("data", {})
        tx = Transaction.deserialize(data)
        tx_pool = TxPool()
        is_new = True
        if tx_pool.is_new(tx):
            log.info("------client never get this transaction before------")
            bc = BlockChain()
            ls_bl = bc.get_last_block()
            log.info('------c handle_tran ls_blo ' + str(ls_bl) + '------')
            if ls_bl:
                ls_height = ls_bl.block_header.height
                for i in range(0, ls_height + 1):
                    while True:
                        block = None
                        try:
                            block = bc.get_block_by_height(i)
                        except:
                            continue
                        if block:
                            break
                    bc_txs = block._transactions
                    for transaction in bc_txs:
                        if transaction.txid == tx.txid:
                            is_new = False
                            break
                    if not is_new:
                        break
            if is_new:
                tx_pool.add(tx)
                log.info("------client handel_transaction txpool added------")
                server2 = PeerServer()
                server2.broadcast_tx(tx)
                log.info("------client handle_transaction broadcast------")

        t = threading.Thread(target=self.shake_loop(), args=())
        t.start()
Example #10
0
 def handle_get_block(self, msg, conn, addr):
     log.info("------server handle_get_block from " + str(addr) +
              "------")  # 7.8
     get_range = msg.get("data", 1)
     log.info("------with range " + str(get_range[0]) + " " +
              str(get_range[1]) + "------")
     block_chain = BlockChain()
     data = []
     for height in range(get_range[0], get_range[1]):
         already_get = False
         for i in range(0, 2):
             block = None
             try:
                 block = block_chain.get_block_by_height(height)
             except:
                 continue
             if block:
                 already_get = True
                 break
         if already_get:
             block = block.serialize()
             data.append(block)
         elif data:
             msg = Msg(Msg.GET_BLOCK_MSG, data)
             log.info("------server send get_block msg------")  # 7.10
             return msg
         else:
             msg = Msg(Msg.NONE_MSG, "")
             return msg
     log.info(
         "------server handle_get_block: get_block_by_height------")  # 7.8
     # data = block.serialize()
     msg = Msg(Msg.GET_BLOCK_MSG, data)
     log.info("------server send get_block msg" + str(data) +
              "------")  # 7.10
     return msg
Example #11
0
 def handle_shake(self, msg):
     log.info("------client handle_shake from " + str(self.ip) +
              "------")  # 7.10
     data = msg.get("data", "")
     last_height = data.get("last_height", 0)
     log.info("------with last height " + str(last_height) + "------")
     block_chain = BlockChain()
     block = block_chain.get_last_block()
     if block:
         local_last_height = block.block_header.height
     else:
         local_last_height = -1
     log.info("client local_last_height %d, last_height %d" %
              (local_last_height, last_height))
     if local_last_height > last_height:  # pass
         try:
             st = StopMine()
             if st.h < local_last_height:
                 st.h = local_last_height
         except:
             pass
         log.info("------error shake------")
         log.info("client local_last_height %d, last_height %d" %
                  (local_last_height, last_height))
         send_data = []
         for i in range(last_height + 1, local_last_height + 1):
             already_get = False
             for i in range(0, 2):
                 block = None
                 try:
                     block = block_chain.get_block_by_height(i)
                 except:
                     continue
                 if block:
                     already_get = True
                     break
             if already_get:
                 send_data.append(block.serialize())
             elif send_data:
                 msg = Msg(Msg.SYNCHRONIZE_MSG, send_data)
                 self.send(msg)
                 return
             else:
                 msg = Msg(Msg.NONE_MSG, "")
                 self.send(msg)
                 return
             # send_data = block.serialize()
             msg = Msg(Msg.SYNCHRONIZE_MSG, send_data)
             self.send(msg)
             log.info("------client handle_shake send synchronize msg to" +
                      str(self.ip) + "------")
     elif local_last_height < last_height:
         try:
             st = StopMine()
             if st.h < last_height:
                 st.h = last_height
         except:
             pass
         start_height = 0 if local_last_height == -1 else local_last_height
         # for i in range(start_height, last_height + 1):
         #     log.info("------client handle_shake send block msg------")  # 7.10
         #     send_msg = Msg(Msg.GET_BLOCK_MSG, i)
         #     self.send(send_msg)
         get_range = [start_height + 1, last_height + 1]
         send_msg = Msg(Msg.GET_BLOCK_MSG, get_range)
         self.send(send_msg)
     else:
         send_msg = Msg(Msg.NONE_MSG, "")
         self.send(send_msg)
Example #12
0
 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()
Example #13
0
 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