def command_line_interface():

        cmd = None

        while cmd != 'q':
            cmd = input('[t: send transaction, b: send block] > ')
            # 추후에 노드 관리를 하는 부분을 이용하여 매인 시나리오를 잘 돌릴수 있도록..nodeinfo.txt를 생성.
            if cmd == 't':
                # receiver, message가 결국 내용이다. '내가 누구에게 무슨 내용을 보낸다.' 라는 내용의 트랜잭션
                #receiver = raw_input('Receiver IP: ')
                #message = raw_input('Message: ')
                # 이미 만들어진 트랜잭션을 이용하여 전송. new_transaction.txt.로 저장이 되어있다면.
                #trx_jstr = TransactionCotroller.create_transaction(Property.pub_key, Property.pri_key, receiver, message)

                # new_transaction.txt open
                f = open("transaction_new0.txt", 'r')
                transaction = f.read()
                transactions = json.loads(transaction)

                sender.send_to_all(transaction)
                # print trx_jstr
                # Sender.send_to_all(trx_jstr)
                nodeproperty.tx_count += 1
                f.close()
            if cmd == 'b':
                f = open("block0.txt", 'r')
                block = f.read()

                sender.send_to_all(block)
Beispiel #2
0
def generate_block(difficulty, merkle_root, transactions):

    try:
        'set block header info'


        print("=================================")
        print("Generate Block")

        start_time = time.time()


        prev_block_height, prev_hash = file_controller.get_last_block()
        block_info = merkle_root + prev_hash
        vote_result = difficulty

        'mining block'
        block_hash, nonce, tryanderror = proof_of_work.proof_of_work(
            block_info, difficulty)
        timestamp = time.strftime('%Y%m%d%H%M%S', time.localtime())


        'create block header'
        block_header = block.BlockHeader(
            prev_hash, nonce, merkle_root, vote_result, 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)


        '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)
        print("Generate block complete")
        print(" ")

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

        print("Block Generate Time Time: %.8f seconds" % elapsed_time)
        print("   ")
        print("Transaction per second : ", 30 / elapsed_time)
        print("")
        file_controller.remove_all_transactions()
        file_controller.remove_all_voting()

        sender.send_to_all(json_new_block)
        print("send block complete")


    except TypeError as te:
        print("@generate block", te)
Beispiel #3
0
def receive_event(p_thrd_name, p_inq):
    count = 1
    while True:
        logging.debug("waiting for event")

        dequeued = p_inq.get()

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

        sender.send_to_all(temp)  # 노드들 연동 후 테스트 필요 2017-09-27

        logging.debug(str(dequeued))
        logging.debug(str(temp))

        logging.debug(count)
        logging.debug(str(p_inq.qsize()))
        count = count + 1
        time.sleep(queue_strategy.SAVE_TX_DEQUEUE_INTERVAL)
Beispiel #4
0
def main():
    'Remove all transaction in mempool'
    file_controller.remove_all_transactions()
    # file_controller.remove_all_Block()
    file_controller.remove_all_voting()

    print("Web Server Start")

    'Peer setting'
    nodeproperty.My_IP_address = file_controller.get_my_ip()
    set_peer.set_peer()
    print("my peer : " + nodeproperty.My_peer_num)

    # node_mapping_table.set_node()와 set_peer()는 중복 기능이나, 일단 디버깅용으로 중복으로 유지함
    node_mapping_table.set_node()

    'Send to all node 10 transaction for one iteration'

    while True:
        transaction_count = 0
        # socket open

        while transaction_count < 30:
            # recv_addr = "1AVsffe"
            extra = "Coldchain service rule event"
            tx = transaction.Transaction(extra, transaction_count + 1)
            temp = json.dumps(tx,
                              indent=4,
                              default=lambda o: o.__dict__,
                              sort_keys=True)

            # send_to_all은 mapping table에 있는 정보로 broadcasting
            # send_to_all_node는 file_controller로 부터 IP List 가지고 와서 broadcasting
            # 테스트 후에 안전한 방식으로 선택
            sender.send_to_all(temp)
            #sender.send_to_all_node(temps, nodeproperty.my_ip_address, nodeproperty.port)

            transaction_count += 1

        'For block mining, time sleep'
        time.sleep(120)