区块链研习社比特币源码研读班
今天研读第五步,参数解析
一总体结构图
二今天阅读的代码段
//《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);
}
}
三 分析
《1 解析main函数的命令行参数,放入gArgs变量中
gArgs.ParseParameters(argc, argv);
gArgs.ParseParameters(argc, argv);
(1)argc为main函数传入的参数个数
(2)argv为main函数传入的参数字符串的二维指针
此函数定义在util.cpp中
void ArgsManager::ParseParameters(int argc, const char* const argv[])
函数意图是处理形如 -externalip=<ip> 这类格式的参数,写入全局参数map中mapArgs中,后面使用
《2 打印信息增加包名,版本信息
std::string strUsage = strprintf(_("%s Daemon"), _(PACKAGE_NAME)) + " " + _("version") + " " + FormatFullVersion() + "\n";
其中
(1)PACKAGE_NAME是一个宏定义, 在xxx中。内容为"Bitcoin Core"
(2)FormatFullVersion()返回当前版本,见cientversion.cpp中
std::string FormatFullVersion()
{
return CLIENT_BUILD;
}
const std::string CLIENT_BUILD(BUILD_DESC CLIENT_VERSION_SUFFIX); //此处用两个宏构造版本号CLIENT_BUILD
(1)BUILD_DESC =》
#ifndef BUILD_DESC
#ifdef BUILD_SUFFIX
#define BUILD_DESC BUILD_DESC_WITH_SUFFIX(CLIENT_VERSION_MAJOR, CLIENT_VERSION_MINOR, CLIENT_VERSION_REVISION, CLIENT_VERSION_BUILD, BUILD_SUFFIX)
#elif defined(GIT_COMMIT_ID)
#define BUILD_DESC BUILD_DESC_FROM_COMMIT(CLIENT_VERSION_MAJOR, CLIENT_VERSION_MINOR, CLIENT_VERSION_REVISION, CLIENT_VERSION_BUILD, GIT_COMMIT_ID)
#else
#define BUILD_DESC BUILD_DESC_FROM_UNKNOWN(CLIENT_VERSION_MAJOR, CLIENT_VERSION_MINOR, CLIENT_VERSION_REVISION, CLIENT_VERSION_BUILD)
#endif
#endif
//这四个宏,见bitcoin-config.h
/* Version Build */
#define CLIENT_VERSION_BUILD 0
/* Major version */
#define CLIENT_VERSION_MAJOR 0
/* Minor version */
#define CLIENT_VERSION_MINOR 15
/* Build revision */
#define CLIENT_VERSION_REVISION 0
注意:bitcoin-config.h该文件在我们下载的源码中一开始是不存在的,需经过对源码进行./configure命令后才能生成。源码的./configure过程可参见菜菜子的[《聊聊比特币(Bitcoin)客户端源码编译那些事》](http://www.jianshu.com/p/8b4ebd0e4b3d)一文。
(2)CLIENT_VERSION_SUFFIX=》
#define CLIENT_VERSION_SUFFIX ""
《3 打印信息增加搬迁证书信息信息
strUsage += FormatParagraph(LicenseInfo());
(1)
FormatParagraph函数声明在bitcoin/src/utilstrencodings.h中
/**
* Format a paragraph of text to a fixed width, adding spaces for
* indentation to any added line.
*/将文本段落格式设置为固定宽度, 将缩进的空格添加到任何添加的行中
std::string FormatParagraph(const std::string& in, size_t width = 79, size_t indent = 0);
(2)LicenseInfo函数在init.h中,主要返回版权信息(MIT协议等等)
/** Returns licensing information (for -version) */
std::string LicenseInfo();
std::string LicenseInfo()
{
const std::string URL_SOURCE_CODE = "<https://github.com/bitcoin/bitcoin>";
const std::string URL_WEBSITE = "<https://bitcoincore.org>";
return CopyrightHolders(strprintf(_("Copyright (C) %i-%i"), 2009, COPYRIGHT_YEAR) + " ") + "\n" +
"\n" +
strprintf(_("Please contribute if you find %s useful. "
"Visit %s for further information about the software."),
PACKAGE_NAME, URL_WEBSITE) +
"\n" +
strprintf(_("The source code is available from %s."),
URL_SOURCE_CODE) +
"\n" +
"\n" +
_("This is experimental software.") + "\n" +
strprintf(_("Distributed under the MIT software license, see the accompanying file %s or %s"), "COPYING", "<https://opensource.org/licenses/MIT>") + "\n" +
"\n" +
strprintf(_("This product includes software developed by the OpenSSL Project for use in the OpenSSL Toolkit %s and cryptographic software written by Eric Young and UPnP software written by Thomas Bernard."), "<https://www.openssl.org>") +
"\n";
}
《4 增加命令行参数使用帮助信息
strUsage += "\n" + _("Usage:") + "\n" +
" bitcoind [options] " + strprintf(_("Start %s Daemon"), _(PACKAGE_NAME)) + "\n";
strUsage += "\n" + HelpMessage(HMM_BITCOIND);
(1)PACKAGE_NAME为包名,见
PACKAGE_NAME的定义位于src/config/bitcoin-config.h中
#define PACKAGE_NAME "Bitcoin Core"
注意:bitcoin-config.h该文件在我们下载的源码中一开始是不存在的,需经过对源码进行./configure命令后才能生成。源码的./configure过程可参见菜菜子的[《聊聊比特币(Bitcoin)客户端源码编译那些事》](http://www.jianshu.com/p/8b4ebd0e4b3d)一文。
(2)HelpMessage,在init.cpp。根据消息模式不同,返回不同的帮助字符串
std::string HelpMessage(HelpMessageMode mode)
/** The help message mode determines what help message to show */
enum HelpMessageMode {
HMM_BITCOIND, //推理是命令行模式
HMM_BITCOIN_QT //推理是qt界面模式
};