接下来主要是分析Bitcoin Core初始化中的网络初始化过程,对应的源码是src/init.cpp的AppInitMain()方法的Step 6: network initialization这部分,详见:https://github.com/bitcoin/bitcoin/blob/v0.16.1/src/init.cpp
这部分源码会用到CConnman 和PeerLogicValidation 类,这些类的使用参见 比特币源码分析(四) - 关键类的分析
接下去开始分析源码:
assert(!g_connman);
g_connman = std::unique_ptr<CConnman>(new CConnman(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())));
CConnman& connman = *g_connman;
peerLogic.reset(new PeerLogicValidation(&connman, scheduler));
RegisterValidationInterface(peerLogic.get());
创建了一个连接管理对象,以及一个新的PeerLogicValidation对象。
// sanitize comments per BIP-0014, format user agent and check total size
std::vector<std::string> uacomments;
for (const std::string& cmt : gArgs.GetArgs("-uacomment")) {
if (cmt != SanitizeString(cmt, SAFE_CHARS_UA_COMMENT))
return InitError(strprintf(_("User Agent comment (%s) contains unsafe characters."), cmt));
uacomments.push_back(cmt);
}
strSubVersion = FormatSubVersion(CLIENT_NAME, CLIENT_VERSION, uacomments);
if (strSubVersion.size() > MAX_SUBVERSION_LENGTH) {
return InitError(strprintf(_("Total length of network version string (%i) exceeds maximum length (%i). Reduce the number or size of uacomments."),
strSubVersion.size(), MAX_SUBVERSION_LENGTH));
}
按照BIP-0014检查uacomment,格式化uacomment,并且检查是否超过最大长度