EOS Code Analysis 5 receives network information
Network part:
Main ()
{
App () set_version (eosio::nodeos::config::version)
App () .register_plugin (); / / registers the plug-in to the plugins plug-in collection of application through the register_plugin () function, where plugins is a map container
Auto root = fc::app_path (); / / set data and configuration path
App () .set_default_data_dir (root / "eosio/nodeos/data")
App () .set_default_config_dir (root / "eosio/nodeos/config")
/ / Application initialization part: 1, input parameter processing, 2, plug-in initialization and installation
If (! app () .initialize (argc, argv))
Return INITIALIZE_FAIL
Initialize_logging ()
App () .startup (); / / launch plug-in app () .exec (); / / start the network server
}
1. P2P communication construction
1.1 initialization to build the network
Http-server-address = 172.26.247.122 http-server-address 8886 / / HTTP Server
P2p-listen-endpoint = 172.26.247.122 9006 / / Node Server
P2p-server-address = 172.26.247.122purl 9006
# here we synchronize 9004 and 9005 of the data
P2p-peer-address = 172.26.247.122purl 9004
P2p-peer-address = 172.26.247.122
Because there are only 21 nodes, it can be set directly without too many dynamic requirements.
Receive network information
My- > start_listen_loop (); enter the monitoring loop
Auto socket = std::make_shared (std::ref (app (). Get_io_service ())); / / obtained io_service / / return io_serv
Acceptor- > async_accept (socket, [socket,this] (boost::system::error_code ec)
/ / from the above, we can see that they are all asynchronous communication mechanisms.
Connections.insert (c)
Start_session (c); / / 111starts processing
Start_read_message (con); / / 111Read messages
Conn- > process_next_message handles messages
MsgHandler m (impl, shared_from_this ()); / / 111functions that handle different messages
Many signal functions are registered in the middle, and the network thread sends a signal to the registration point through the emit function of push_transaction to do the sending operation. This operation is the response function of the signal registration.
1 receive transactions
Void net_plugin_impl::handle_message (connection_ptr c, const packed_transaction & msg) {
/ / stored in the received_transactions list
Dispatcher- > recv_transaction (c, tid)
/ / received_transactions.emplace_back ((transaction_origin) {id, c})
Chain_plug- > accept_transaction ()
On_incoming_transaction_async (); / / Productor registers at initialization
{
Send_response () / / an error returns a response
Chain.push_transaction (std::make_shared (* trx), deadline)
}
2 receive handshake information
Handle_message ()
1 the message is correct
2 node ID repeat (do not link yourself)
3 chain ID is the same
4 whether the version of the protocol is the same
All errors are sent: go_away_message (error type)
5 if (! authenticate_peer (msg)) {/ / authenticate the other party
Elog ("Peer not authenticated. Closing connection.")
C-> enqueue (go_away_message (authentication))
Return
}
6 sync_master- > recv_handshake (cMagneMsg); / / check the status of each chain and start synchronizing block data
/ / 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 block num ssend a notice catchup if this is not the first generation
Click to follow: