美文网首页
比特币源码研读之三

比特币源码研读之三

作者: 剑有偏锋 | 来源:发表于2017-10-14 23:18 被阅读80次

区块链研习社比特币源码研读班
今天研读第四步,AppInit初始化

一总体结构图

image.png

二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;
}

相关文章

  • 比特币源码研读

    forest21000版 比特币源码研读之一比特币源码研读之二比特币源码研读之三比特币源码研读之四比特币源码研读之...

  • 比特币源码研读之十一

    比特币源码研读系列已经发表了十篇了,通过这十篇源码研读系列让我对比特币源码及比特币运行原理有了进一步的理解,也让我...

  • 比特币源码研读之一

    比特币源码研读之一——区块链研习社 《比特币源码研读班》 一看文件夹结构 和 github编译依赖,分析的依赖库 ...

  • 比特币源码研读之一

    作者:区块链研习比特币源码研读班 菜菜子 一、源码下载 本文比特币源码下载地址为:https://github.c...

  • 比特币源码研读之三

    自从发表了两篇比特币源码研读总结系列之后,很多朋友都表示对编程和研读源码产生了兴趣,有些朋友提出想加入我们区块链研...

  • 比特币源码研读之三

    区块链研习社比特币源码研读班今天研读第四步,AppInit初始化 一总体结构图 二AppInit函数

  • 比特币源码研读之三

    接下来我们来看下一些程序初始化设置的一些操作,主要涉及的文件: bitcoind.cpp、init.cpp、uti...

  • 比特币源码研读之二

    区块链研习社比特币源码研读班今天研读第二,第三流程,SetupEnvironment和noui_connect函数...

  • 01:比特币源码编译|比特币源码研读

    2018年8月报名了区块链研习社的「比特币源码研读班」(5期)。首先要准备比特币源码的编译,因为对这块几乎是小白水...

  • 编译bitcoin源码

    ——区块链研习社 《比特币源码研读班》 本文基于ubuntu 16.04 64编译ok 一 下载源码(如没安装gi...

网友评论

      本文标题:比特币源码研读之三

      本文链接:https://www.haomeiwen.com/subject/zjbfuxtx.html