Пример #1
0
def start_node(node_name='Node', port=2231):
    n = node.Node(port)
    queue = multiprocessing.Queue(1)
    queue.put(0)
    lock = multiprocessing.Lock()
    n.sync_with_other_nodes()
    while True:
        print_break()
        print("get request type")
        re_type, re_pkt = n.get_request_type(user_port)
        (hexdump(re_pkt))
        if 'read' == re_type:
            #print("sr1 pkt from port")
            re_pkt = n.sr1_pkt_from_storage_to_user(re_pkt, storage_port)
            #print("send pkt to port")
            n.send_new_header_hash_to_user(port, user_port)
        elif 'write' == re_type:
            broad_pkt = n.change_request_type(re_pkt, b"write", b"broadcast")
            n.broadcast_to_all_nodes(broad_pkt.load)

            #print("start child process in write mode")
            p1 = multiprocessing.Process(target = listen_from_other_nodes, \
                    args = (n, port, queue, lock))
            p1.start()

            p2 = multiprocessing.Process(target = do_proof_work_job, \
                    args = (re_pkt.load[9:].decode(), n, port, queue, lock))
            p2.start()
            p1.join()
            p2.join()

            #print("pre header hash:", re_pkt.load[5:])
            #re_pkt = n.gen_pkt_with_pre_header_hash(port, storage_port, re_pkt.load[5:].decode())
            #time.sleep(1)
            #n.send_new_header_hash_to_user(port, user_port)
            #print(hexdump(re_pkt))
            #n.send_pkt_to_storage(re_pkt, storage_port)
        elif 'broadcast' == re_type:
            #print("start child process in broadcast mode")
            p1 = multiprocessing.Process(target = listen_from_other_nodes, \
                    args = (n, port, queue, lock))
            p1.start()

            p2 = multiprocessing.Process(target = do_proof_work_job, \
                    args = (re_pkt.load[9:].decode(), n, port, queue, lock))
            p2.start()
            p1.join()
            p2.join()
        elif 'sync' == re_type:
            target_node_port = re_pkt.sport
            #print("recv sync request, target port:", target_node_port)
            n.sr1_pkt_from_storage_to_node(re_pkt, storage_port,
                                           target_node_port)
        elif 'exit' == re_type:
            n.broadcast_to_all_nodes(re_pkt.load)
            n.send_pkt_to_storage(re_pkt, storage_port)
            break
    print("You have start a user: {}[port:{}]".format(node_name, port))
Пример #2
0
def _onduty_mail(node_public_key, gte_height_2: int, height: int):
    mail_text = "<tr><td width=" + str(20) + ">序号</td><td width=" + str(
        100) + ">nodepublickey</td><td width=" + str(
            100) + ">连续没有轮值区块高度范围</td></tr>"

    mail_text = mail_text + "<tr><td>" + str(
        0) + "</td><td>" + node_public_key + "</td><td>" + str(
            gte_height_2) + "-" + str(height) + "</td></tr>"
    return mail_text


if __name__ == '__main__':
    arbiter_num = 36

    n = node.Node()
    node_public_key = n.node_public_key
    ''' connect to the database to get the latest height and total data '''
    obj = db_onduty_info.OndutyMongoDBCont(n.mongo_host, n.mongo_port,
                                           n.mongo_db)
    onduty = obj.findOne_onduty_info()
    # print('onduty : ', onduty)

    height = onduty.get(interface.height)

    onduty_count = obj.findOne_onduty_count()
    # print('onduty_count : ', onduty_count)
    ''' 连续两次当值仲裁人不出块逻辑, 判断数据库数据是否大于24个 '''
    if onduty_count >= arbiter_num * 2:

        gte_height_1 = height - arbiter_num + 1