比特币源码阅读(HTTPReq_JSONRPC的注册和实现)
2018-08-11 本文已影响0人
坠叶飘香
1.注册处理RPC请求的函数的堆栈
1.1.AppInit函数
src/bitcoind.cpp
static bool AppInit(int argc, char* argv[])
{
fRet = AppInitMain();
}
1.2.AppInitMain函数
src/init.cpp
bool AppInitMain()
{
RegisterAllCoreRPCCommands(tableRPC);
if (!AppInitServers())
return InitError(_("Unable to start HTTP server. See debug log for details."));
}
1.3.AppInitServers函数
src/init.cpp
static bool AppInitServers()
{
if (!StartHTTPRPC())
return false;
}
1.4.注册处理RPC请求的函数
src/httprpc.cpp
bool StartHTTPRPC()
{
LogPrint(BCLog::RPC, "Starting HTTP RPC server\n");
if (!InitRPCAuthentication())
return false;
RegisterHTTPHandler("/", true, HTTPReq_JSONRPC); //注册处理RPC请求的函数
#ifdef ENABLE_WALLET
// ifdef can be removed once we switch to better endpoint support and API versioning
RegisterHTTPHandler("/wallet/", false, HTTPReq_JSONRPC);
#endif
assert(EventBase());
httpRPCTimerInterface = MakeUnique<HTTPRPCTimerInterface>(EventBase());
RPCSetTimerInterface(httpRPCTimerInterface.get());
return true;
}
2.对RPC命令的处理
src/httprpc.cpp
static bool HTTPReq_JSONRPC(HTTPRequest* req, const std::string &)
{
jreq.parse(valRequest);
UniValue result = tableRPC.execute(jreq); //执行CRPCTable的execute函数
}