一起來讀源碼1-eos net_plugin插件

摘要:

插件功能:

1.負責端點的連接與斷開,狀態管理;

2.請求塊,驗證塊,處理塊;

 

詳情:

定義類:

net_plugin: 網絡插件類

    //連接端點 endpoint

    string    connect( const string& endpoint );

    //斷開與endpoint的連接

    string    disconnect( const string& endpoint );

    //返回與endpoint的狀態

    optional<connection_status>    status( const string& endpoint ) const;

    //返回所有的連接

    vector<connection_status>    connections() const;

sync_manager: 同步管理類

    //返回狀態string

    constexpr static auto stage_str( stages s );

    //設置狀態

    void set_state( stages s );

    //頭塊號是否是同步請求的

    bool is_sync_required( uint32_t fork_head_block_num );

    // 請求下一個塊
    /* 1.如果當前塊不存在:使用傳進來的conn
    *  2.如果當前連接數爲0:重置同步源
    *  3.如果找不到同步源對應的連接:拿連接集合中的第一個
    *  4.如果上一個請求的塊號!=已知請求的塊號(說明同步不一致):請求同步塊
    */

    void request_next_chunk( std::unique_lock<std::mutex> g_sync,

        const connection_ptr& conn = connection_ptr() ); 

    //開啓同步

    start_sync( const connection_ptr& c, uint32_t target );

    //捕獲塊驗證(先驗證是否是最新的塊:是->設置最新塊 否->不處理)

    verify_catchup( const connection_ptr& c, uint32_t num, const block_id_type& id );

    //發送握手

    static void send_handshakes(); 

    //同步peer

    bool syncing_with_peer() const { return sync_state == lib_catchup; }

    //同步重置塊號(如果上一個確定的塊號 > 已知同步的塊號: 賦值給已知同步的塊號)

    void sync_reset_lib_num( const connection_ptr& conn );

    //同步重新指定fetch

    void sync_reassign_fetch( const connection_ptr& c, go_away_reason reason ); 

     // 拒絕塊(如果拒絕的數超過了最大塊數限制,重置同步源)

    void rejected_block( const connection_ptr& c, uint32_t blk_num );

    // 同步接收塊

    void sync_recv_block( const connection_ptr& c, const block_id_type& blk_id,

        uint32_t blk_num, bool blk_applied ); 

    // 同步更新指定塊(如果指定的塊號不是默認下一個要請求的塊號,就賦值給它)

    void sync_update_expected( const connection_ptr& c, const block_id_type& blk_id,

        uint32_t blk_num, bool blk_applied );

    //接收握手消息

    void recv_handshake( const connection_ptr& c, const handshake_message& msg ); 

    //同步接收通知信息

    void sync_recv_notice( const connection_ptr& c, const notice_message& msg ); 

dispatch_manager: 遞送管理器

    //確定多線程執行順序

    boost::asio::io_context:;strand strand;

    //廣播傳輸塊

    void bcast_transaction( const packed_transaction& trx );

    //拒絕傳輸塊

    void rejected_transaction( const packed_transaction_ptr& trx, uint32_t head_blk_num );

    // 廣播塊(線程安全,先看看塊id存不存在,不存在就廣播)

    void bcast_block( const signed_block_ptr& b, const block_id_type& id ); 

    //廣播通知

    void bcast_notice( const block_id_type& id ); 

    //拒絕塊

    void rejected_block( const block_id_type& id );

    //接受塊

    void recv_block( const connection_ptr& conn, const block_id_type& msg, uint32_t bnum );

    //傳輸過期塊(刪掉到目前位置已過期的傳輸塊)

    void expire_blocks( uint32_t bnum );

net_plugin_impl:網絡插件的實現

    //更新鏈信息(只由主程序線程調用)

    void update_chain_info();

    //獲得鏈信息

    std::tuple<uint32_t, uint32_t, uint32_t, block_id_type, block_id_type,

        block_id_type> get_chain_info() const;

    //開始監聽

    void start_listen_loop();

    //接受塊

    void on_accepted_block( const block_state_ptr& bs );

    //預接受塊

    void on_pre_accepted_block( cosnt signed_block_ptr& bs );

    //傳輸問訊

    void transaction_ack( const std::pair<fc::exception_ptr, transaction_metadata_ptr>& );

    //固定塊,調用update_chain_info

    void on_irreversible_block( const block_state_ptr& blk );

    //開啓連接計時器

    void start_conn_timer(boost::asio::steady_timer::duration du,

        std::weak_ptr<connection> from_connection);

    //開啓過期計時器(線程安全)

    void start_expire_timer();

    //開啓監視器

    void start_monitors();

    //過期

    void expire();

    //連接管理其(1ms檢查一次,如果連接不可用,刪除它)

    void connection_monitor( std::weak_ptr<connection> from_connection, bool reschedule );

    //節點心跳滴答

    void ticker();

    //驗證握手信息,判定是否可以連接

    bool authenticate_peer( const handshake_message& msg ) const;

    //獲得公鑰

    chain::public_key_type get_authentication_key() const;

    //合約簽名(簽名器, 簽名摘要)

    chain::signature_type sign_compact( const chain::public_key_type& signer,

        const fc::sha256& digest ) const;

    //返回協議版本

    constexpr uint16_t to_protocol_version(uint16_t v);

    //根據host找到連接

    connection_ptr find_connection( const string& host ) const;

定義協議:

net_message:

    handshake_message //握手消息

    chain_size_message //鏈大小消息

    go_away_message //離開信息

    time_message //時間信息

    notice_message //通知消息

    request_message //請求消息

    sync_request_message //同步請求消息

    signed_block //已簽名塊

    packed_transaction //已打包的傳輸塊

 

定義表:

node_transaction_index:傳輸表

主鍵 分索引1 分索引2
id 傳輸塊id 連接id
過期時間    
塊號    

peer_block_state_index:節點塊表

主鍵 分索引1 分索引2
id 連接id 塊id
塊id 塊id 塊是否存在
塊號    

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章