区块链研习社比特币源码研读班
今天研读第四步,AppInit初始化
一总体结构图
二AppInit函数
bool AppInit(int argc, char* argv[])
{
boost::thread_group threadGroup;
CScheduler scheduler;
bool fRet = false;
//《1 解析main函数的命令行参数,放入gArgs变量中
gArgs.ParseParameters(argc, argv);
if (gArgs.IsArgSet("-?") || gArgs.IsArgSet("-h") || gArgs.IsArgSet("-help") || gArgs.IsArgSet("-version"))
{
//《2 打印信息增加包名,版本信息
std::string strUsage = strprintf(_("%s Daemon"), _(PACKAGE_NAME)) + " " + _("version") + " " + FormatFullVersion() + "\n";
if (gArgs.IsArgSet("-version"))
{
//《3 打印信息增加搬迁证书信息信息
strUsage += FormatParagraph(LicenseInfo());
}
else
{
//《4 增加命令行参数使用帮助信息
strUsage += "\n" + _("Usage:") + "\n" +
" bitcoind [options] " + strprintf(_("Start %s Daemon"), _(PACKAGE_NAME)) + "\n";
strUsage += "\n" + HelpMessage(HMM_BITCOIND);
}
fprintf(stdout, "%s", strUsage.c_str());
return true;
}
try
{
//《5 检查区块数据保存目录是否存在
if (!fs::is_directory(GetDataDir(false)))
{
fprintf(stderr, "Error: Specified data directory \"%s\" does not exist.\n", gArgs.GetArg("-datadir", "").c_str());
return false;
}
//《6 读取配置文件
try
{
gArgs.ReadConfigFile(gArgs.GetArg("-conf", BITCOIN_CONF_FILENAME));
} catch (const std::exception& e) {
fprintf(stderr,"Error reading configuration file: %s\n", e.what());
return false;
}
//《7 检查测试参数有效
try {
SelectParams(ChainNameFromCommandLine());
} catch (const std::exception& e) {
fprintf(stderr, "Error: %s\n", e.what());
return false;
}
// 《8 检查命令行的引导参数有异常,必须带- 或 /
for (int i = 1; i < argc; i++) {
if (!IsSwitchChar(argv[i][0])) {
fprintf(stderr, "Error: Command line contains unexpected token '%s', see bitcoind -h for a list of options.\n", argv[i]);
exit(EXIT_FAILURE);
}
}
// 《8 bitcoind 命令行,启用server参数为默认值true
gArgs.SoftSetBoolArg("-server", true);
// Set this early so that parameter interactions go to console
InitLogging();//初始化日志格式
InitParameterInteraction();//初始化交互参数
if (!AppInitBasicSetup())//设置异常报告重定向, 启动网络,中断处理等
{
// InitError will have been called with detailed error, which ends up on console
exit(EXIT_FAILURE);
}
//《9 应用程序初始化参数交互
if (!AppInitParameterInteraction())
{
// InitError will have been called with detailed error, which ends up on console
exit(EXIT_FAILURE);
}
//《10 应用程序参数完整性检查
if (!AppInitSanityChecks())
{
// InitError will have been called with detailed error, which ends up on console
exit(EXIT_FAILURE);
}
//《11 开启守护进程
if (gArgs.GetBoolArg("-daemon", false))
{
#if HAVE_DECL_DAEMON
fprintf(stdout, "Bitcoin server starting\n");
// Daemonize
if (daemon(1, 0)) { // don't chdir (1), do close FDs (0)
fprintf(stderr, "Error: daemon() failed: %s\n", strerror(errno));
return false;
}
#else
fprintf(stderr, "Error: -daemon is not supported on this operating system\n");
return false;
#endif // HAVE_DECL_DAEMON
}
// 《12 开启守护进程后,锁住区块数据文件夹
if (!AppInitLockDataDirectory())
{
// If locking the data directory failed, exit immediately
exit(EXIT_FAILURE);
}
fRet = AppInitMain(threadGroup, scheduler);//初始化线程池和调度器
}
catch (const std::exception& e) {
PrintExceptionContinue(&e, "AppInit()");
} catch (...) {
PrintExceptionContinue(nullptr, "AppInit()");
}
if (!fRet)
{
//初始化初始化线程池和调度器失败,主动退出
Interrupt(threadGroup); //中断线程池
threadGroup.join_all();//等待所有线程退出
} else {
WaitForShutdown(&threadGroup);//等待threadGroup句柄可用,既等待程序正常退出
}
Shutdown(); //关闭程序
return fRet;
}