EOS代碼分析5 接收網絡信息
Main()
{
app().set_version(eosio::nodeos::config::version);
app().register_plugin<history_plugin>(); //通過register_plugin()函數將插件註冊到application的plugins插件集合中,plugins是一個map容器
auto root = fc::app_path(); //設定數據和配置路徑
app().set_default_data_dir(root / "eosio/nodeos/data" );
app().set_default_config_dir(root / "eosio/nodeos/config" );
//應用程序初始化部分:1、輸入參數處理,2、插件的初始化和安裝
if(!app().initialize<chain_plugin, http_plugin, net_plugin, producer_plugin>(argc, argv))
return INITIALIZE_FAIL;
initialize_logging();
app().startup(); //啟動插件
app().exec(); //網絡服務器啟動
}
1、P2P通信構建
1.1 初始化構建網絡
http-server-address = 172.26.247.122:8886 //HTTP Server
p2p-listen-endpoint = 172.26.247.122:9006 //Node Server
#這裏我們同步9004和9005的數據
p2p-peer-address = 172.26.247.122:9004
p2p-peer-address = 172.26.247.122:9005
因為只有21個節點,所以,就可以直接設定了,無需太多動態需求。
接收網絡信息
my->start_listen_loop(); 進入監聽循環
auto socket = std::make_shared<tcp::socket>( std::ref( app().get_io_service() ) ); //獲io_service //return io_serv;
//從上面看到,都是異步通信機制。
connections.insert( c );
start_session( c ); //111 開始處理
start_read_message( con ); //111 讀取消息
conn->process_next_message 處理消息
msgHandler m(impl, shared_from_this() ); //111 處理不同的消息的函數
中間註冊很多信號函數,網絡線程通過push_transaction之emit函數發送信號到註冊點,做發送操作。這個操作是信號註冊的響應函數。
1接收交易
void net_plugin_impl::handle_message( connection_ptr c, const packed_transaction &msg) {
//存儲在received_transactions列表裏
dispatcher->recv_transaction(c, tid);
//received_transactions.emplace_back((transaction_origin){id, c});
chain_plug->accept_transaction();
on_incoming_transaction_async(); //Productor初始化時候註冊
{
send_response() //錯誤則返回響應
chain.push_transaction(std::make_shared<transaction_metadata>(*trx), deadline);
}
2 接收握手信息
Handle_message()
1 消息正確
2 node ID重復(不要自己鏈接自己)
3 chain ID 一樣
4 協議版本是否一樣
所有錯誤都發送:go_away_message(錯誤類型)
5 if(!authenticate_peer(msg)) { //認證對方
elog("Peer not authenticated. Closing connection.");
c->enqueue(go_away_message(authentication));
return;
}
6 sync_master->recv_handshake(c,msg); //互相檢測各自鏈的狀態,並開始同步區塊數據
// sync need checks; (lib == last irreversible block)
//
// 0. my head block id == peer head id means we are all caugnt up block wise
// 1. my head block num < peer lib - start sync locally
// 2. my lib > peer head num - send an last_irr_catch_up notice if not the first generation
//
// 3 my head block num <= peer head block num - update sync state and send a catchup request
// 4 my head block num > peer block num ssend a notice catchup if this is not the first generation
點擊關註:
EOS代碼分析5 接收網絡信息