Exemplo n.º 1
0
    def handle_handshake(self, msg, conn, addr):
        log.info("------server handle_handshake from " + str(addr) + "------")
        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('------s hand_shake ls_blo ' + str(block) + '------')

        if block:
            local_last_height = block.block_header.height
        else:
            local_last_height = -1
        log.info("server 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
                    log.info("------" + str(addr) + " is the highest " +
                             str(st.h) + "------")
            except:
                log.info('------dont work------')
            log.info("------server handle_handshake precede------")
            try:
                genesis_block = block_chain[0]
            except:
                genesis_block = None
            data = {"last_height": -1, "genesis_block": ""}
            if genesis_block:
                data = {
                    "last_height": local_last_height,
                    "genesis_block": genesis_block.serialize()
                }
            msg = Msg(Msg.HAND_SHAKE_MSG, data)
            return msg

        elif local_last_height < last_height:
            try:
                st = StopMine()
                if st.h < last_height:
                    st.h = last_height
                    st.ip = addr
                    log.info('------works------')
                    log.info("------" + str(addr) + ' is the highest ' +
                             str(st.h) + "------")
            except:
                log.info('failed to stop mine')
            log.info("------server handle_handshake fall behind------")
            start_height = 0 if local_last_height == -1 else local_last_height
            synchronize_range = [start_height + 1, last_height + 1]
            log.info("------server need synchronize range " +
                     str(synchronize_range[0]) + " " +
                     str(synchronize_range[1]) + "------")
            send_msg = Msg(Msg.SYNCHRONIZE_MSG, synchronize_range)
            return send_msg
Exemplo n.º 2
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()