コード例 #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
コード例 #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()
コード例 #3
0
ファイル: network.py プロジェクト: ksl20200108/8.3
 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)
コード例 #4
0
ファイル: network.py プロジェクト: ksl20200108/8.3
    def handle_handshake(self, msg, conn, addr):
        log.info("------server handle_handshake from " + str(addr) +
                 "------")  # 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("server local_last_height %d, last_height %d" %
                 (local_last_height, last_height))

        if local_last_height >= last_height:
            try:
                st = StopMine()
                if st.h < local_last_height:
                    st.h = local_last_height
            except:
                pass
            log.info("------server handle_handshake precede------")
            try:
                genesis_block = block_chain[0]
            except IndexError as e:
                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
            # send_data = json.dumps(msg.__dict__)
            # time.sleep(1)  # 7.13
            # send_bytes = send_data.encode()
            # header_json = json.dumps({"send_size": len(send_bytes)})
            # header_bytes = header_json.encode()
            # header_size = len(header_bytes)
            # conn.sendall(struct.pack('i', header_size))
            # conn.sendall(header_bytes)
            # conn.sendall(send_bytes)
            # log.info("------server handle_handshake precede send msg: " + str(data) + "------")

        elif local_last_height < last_height:
            try:
                st = StopMine()
                if st.h < last_height:
                    st.h = last_height
            except:
                pass
            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