Ejemplo n.º 1
0
def finding_new_block():
    i = 1
    while i < 12:
        try:
            st = StopMine()
            st.mine_h = i
        except:
            pass
        bc1 = BlockChain()
        tx3, total_fee = packing()
        log.info("------return these information:" + str(tx3) +
                 str(total_fee) + "------")
        try:
            bc1.add_block(tx3,
                          total_fee)  # wait try when there's no transaction
        except:
            log.info("------fall behind in mine------")
            try:
                st = StopMine()
                log.info("------with longest " + str(st.h) + " and local " +
                         str(i) + "------")
                while i < st.h:
                    tx3, total_fee = packing()
                    i += 1
            except:
                pass
        i += 1
Ejemplo n.º 2
0
def finding_new_block():
    i = 1
    while i < 12:
        try:
            st = StopMine()
            st.mine_h = i
        except:
            pass
        bc1 = BlockChain()
        tx3, total_fee = packing()
        log.info("------return these information:" + str(tx3) +
                 str(total_fee) + "------")
        try:
            if tx3:
                bc1.add_block(tx3, total_fee)
            elif total_fee == "no":
                log.info("no transaction left")
                bc1.add_block()
                log.info("mine a empty block")
            else:
                log.info("error in packing")
        except:
            log.info("------fall behind in mine------")
            try:
                st = StopMine()
                log.info("------with longest " + str(st.h) + " and local " +
                         str(i) + "------")
                while i < st.h:
                    tx3, total_fee = packing()
                    i += 1
            except:
                pass
        i += 1
Ejemplo n.º 3
0
    def run(self):
        nonce = 0
        found = False
        hash_hex = None
        while nonce < self.MAX_SIZE:
            data = self._prepare_data(nonce)
            hash_hex = utils.sum256_hex(data)

            hash_val = int(hash_hex, 16)
            # sys.stdout.write("data: %s\n" % data) # 7.11
            # sys.stdout.write("try nonce == %d\thash_hex == %s \n" % (nonce, hash_hex))    # 7.11
            if (hash_val < self._target_bits):
                found = True
                break

            nonce += 1
        if found:
            for i in range(0, 60):
                try:
                    st = StopMine()
                    if st.h >= st.mine_h:
                        raise NonceNotFoundError
                    time.sleep(1)
                except:
                    time.sleep(1)
        else:
            # print('Not Found nonce')
            raise NonceNotFoundError('nonce not found')
        return nonce, hash_hex
Ejemplo n.º 4
0
    def run(self):
        nonce = 0
        found = False
        hash_hex = None
        # print('Mining a new block')   # change delete
        while nonce < self.MAX_SIZE:
            try:
                st = StopMine()
                if st.h >= st.mine_h:
                    raise NonceNotFoundError
            except:
                pass
            data = self._prepare_data(nonce)
            hash_hex = utils.sum256_hex(data)

            hash_val = int(hash_hex, 16)
            # sys.stdout.write("data: %s\n" % data) # 7.11
            # sys.stdout.write("try nonce == %d\thash_hex == %s \n" % (nonce, hash_hex))    # 7.11
            if (hash_val < self._target_bits):
                found = True
                break

            nonce += 1
        if found:
            # print('Found nonce == %d' % nonce)  # change delete
            pass  # change
        else:
            # print('Not Found nonce')
            raise NonceNotFoundError('nonce not found')
        return nonce, hash_hex
Ejemplo n.º 5
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
Ejemplo n.º 6
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()
Ejemplo n.º 7
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)
Ejemplo n.º 8
0
    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
Ejemplo n.º 9
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()
Ejemplo n.º 10
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