Exemple #1
0
def receive_event(p_thrd_name, p_inq):
    total_tx_count = 1
    while True:
        monitoring.log("log.Wait for transaction creation request.")

        dequeued = p_inq.get()

        tx = transaction.Transaction(dequeued)
        # temp = json.dumps(
        #     tx, indent=4, default=lambda o: o.__dict__, sort_keys=True)

        # for try
        temp = json.dumps(tx, default=lambda o: o.__dict__, sort_keys=True)

        # sender.send_to_all(temp)
        sender.send_to_all_peers(temp, nodeproperty.My_receiver_port)

        monitoring.log("log.Transaction creation request - rcvd: " +
                       str(dequeued))
        monitoring.log("log.Transaction creation request - rcvd(json): " +
                       str(temp))
        monitoring.log("log.Total number of transaction creation request: " +
                       str(total_tx_count))
        # monitoring.log("log."+str(p_inq.qsize()))
        total_tx_count = total_tx_count + 1
def generate_block(difficulty, merkle_root, transactions):

    try:
        'set block header info'

        monitoring.log("log.=================================")
        monitoring.log("log.Generate Block.")

        start_time = time.time()


        prev_block_height, prev_hash = file_controller.get_last_block() # TODO: type error 발생 must be str , not int

        # if type(prev_block_height) is not int:
        #     prev_block_height = int(prev_block_height)
        # if type(prev_hash) is not str:
        #     prev_hash = str(prev_hash)

        block_info = merkle_root + prev_hash
        vote_result = difficulty

        monitoring.log("log.Mining Block.")
        block_hash, nonce, tryanderror = proof_of_work.proof_of_work(
            block_info, difficulty)

        timestamp = int(round(time.time()*1000.0))     # UTC timestamp rule

        monitoring.log("log.Create Block Header.")
        block_header = block.BlockHeader(
            prev_hash, nonce, merkle_root, vote_result, str(timestamp))
        block_header.block_id = "test_block_id"
        block_header.block_number = int(prev_block_height) + 1
        block_header.block_hash = block_hash
        block_header.block_info = block_info
        block_header.miner = nodeproperty.Total_peer_num
        block_header.num_tx = len(transactions)  

        monitoring.log("log.Create Block.")
        new_block = block.Block(block_header, transactions)
        json_new_block = json.dumps(
            new_block, indent=4, default=lambda o: o.__dict__, sort_keys=True)
        monitoring.log("log.Generate block complete")
        monitoring.log("log. ")

        # end_time = time.time()
        # elapsed_time = end_time - start_time

        # monitoring.log("log.Block Generate Time Time: "+str(elapsed_time)+" seconds")
        # tps = voting.TransactionCountForConsensus / elapsed_time
        # print("log.Transaction per second : "+ str(tps))
        file_controller.remove_all_transactions()
        file_controller.remove_all_voting()

        sender.send_to_all_peers(json_new_block,nodeproperty.My_receiver_port)
        # sender.send_to_all(json_new_block)
        monitoring.log("log.Send block complete")


    except TypeError as te:
        print("@generate block", te)
def receive_event(p_thrd_name, p_inq):
    total_tx_count = 1
    while True:
        monitoring.log("log.Waiting the request for contract deployment.")
        (restapi_request_json, restapi_request_ip) = p_inq.get()

        tx = transaction.Transaction('CT', restapi_request_ip,
                                     restapi_request_json)
        # temp = json.dumps(
        #     tx, indent=4, default=lambda o: o.__dict__, sort_keys=True)

        # for try
        temp = json.dumps(tx, default=lambda o: o.__dict__, sort_keys=True)

        # sender.send_to_all(temp)
        sender.send_to_all_peers(temp, nodeproperty.My_receiver_port)

        monitoring.log("log.Contract Deployment request - rcvd: " +
                       str(restapi_request_json))
        monitoring.log("log.Contract Deployment request - rcvd(json): " +
                       str(temp))
        monitoring.log("log.Total number of Contract Deployment request: " +
                       str(total_tx_count))
        # monitoring.log("log."+str(p_inq.qsize()))
        total_tx_count = total_tx_count + 1
Exemple #4
0
    def run(self):
        while True:
            rcvd_data = self.rcvddata_q.get()
            request_sock = self.socket_q.get()
            socketip = self.socketip_q.get()

            # Assuming the format of the incoming message is json
            rcvd_data_json = json.loads(rcvd_data)
            peerid = rcvd_data_json['ID']
            logging.debug("The ID of ConnectedPeer: " + peerid)
            logging.debug("The IP of ConnectedPeer: " + socketip)

            if peerid in self.peer_list:
                logging.debug("Add new peer's IP to ConnectedPeerList.")
                nodeproperty.ConnectedPeerList.append([peerid, socketip])
                nodeproperty.ConnectedPeerList.sort()
                request_sock.close()
                connected_peer_list_json = json.dumps(
                    nodeproperty.ConnectedPeerList, indent=4, default=lambda o: o.__dict__, sort_keys=True)
                logging.debug("Updated ConnectedPeerList(json format): "+connected_peer_list_json)
                sender.send_to_all_peers(connected_peer_list_json, nodeproperty.PeerConnector_Port)
                set_peer.set_my_peer_num()
                set_peer.set_total_peer_num()

            else:
                request_sock.close()
                # If there is no ID in the peer list, ignore it.
                logging.debug("Ignore it because there is no corresponding ID in the predefined list.")