网络部分:
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
p2p-server-address = 172.26.247.122:9006
#这里我们同步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;
acceptor->async_accept( socket, [socket,this]( boost::system::error_code ec )
//从上面看到,都是异步通信机制。
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
点击关注:
原文地址:https://blog.51cto.com/13878196/2385383