From 9ecade14252ad1972f668d2d2e4ef44fdfcb944a Mon Sep 17 00:00:00 2001 From: Carl Dong Date: Fri, 23 Apr 2021 15:18:04 -0400 Subject: rest: Add GetChainman function and use it This is not the cleanest change but: 1. It fixes the erroneous use of RPC's Ensure*() in rest.cpp, which cause crashes in REST contexts. RPC code wraps all calls in a try/except, REST code does not. Ensure*(), being part of RPC, expects that its throw's will get caught by a try/except. But if you use Ensure*() in REST code, since it doesn't have a try/except wrap, a crash will happen. 2. It is consistent with other functions like GetMemPool. Someone can probably make this a bit prettier. --- src/rest.cpp | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'src/rest.cpp') diff --git a/src/rest.cpp b/src/rest.cpp index d41f374c49..747c7aea19 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -107,6 +107,27 @@ static CTxMemPool* GetMemPool(const std::any& context, HTTPRequest* req) return node_context->mempool.get(); } +/** + * Get the node context chainstatemanager. + * + * @param[in] req The HTTP request, whose status code will be set if node + * context chainstatemanager is not found. + * @returns Pointer to the chainstatemanager or nullptr if none found. + */ +static ChainstateManager* GetChainman(const std::any& context, HTTPRequest* req) +{ + auto node_context = util::AnyPtr(context); + if (!node_context || !node_context->chainman) { + RESTERR(req, HTTP_INTERNAL_SERVER_ERROR, + strprintf("%s:%d (%s)\n" + "Internal bug detected: Chainman disabled or instance not found!\n" + "You may report this issue here: %s\n", + __FILE__, __LINE__, __func__, PACKAGE_BUGREPORT)); + return nullptr; + } + return node_context->chainman; +} + static RetFormat ParseDataFormat(std::string& param, const std::string& strReq) { const std::string::size_type pos = strReq.rfind('.'); @@ -181,7 +202,9 @@ static bool rest_headers(const std::any& context, std::vector headers; headers.reserve(count); { - ChainstateManager& chainman = EnsureAnyChainman(context); + ChainstateManager* maybe_chainman = GetChainman(context, req); + if (!maybe_chainman) return false; + ChainstateManager& chainman = *maybe_chainman; LOCK(cs_main); CChain& active_chain = chainman.ActiveChain(); tip = active_chain.Tip(); @@ -252,7 +275,9 @@ static bool rest_block(const std::any& context, CBlockIndex* pblockindex = nullptr; CBlockIndex* tip = nullptr; { - ChainstateManager& chainman = EnsureAnyChainman(context); + ChainstateManager* maybe_chainman = GetChainman(context, req); + if (!maybe_chainman) return false; + ChainstateManager& chainman = *maybe_chainman; LOCK(cs_main); tip = chainman.ActiveChain().Tip(); pblockindex = chainman.m_blockman.LookupBlockIndex(hash); @@ -541,7 +566,9 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std:: std::string bitmapStringRepresentation; std::vector hits; bitmap.resize((vOutPoints.size() + 7) / 8); - ChainstateManager& chainman = EnsureAnyChainman(context); + ChainstateManager* maybe_chainman = GetChainman(context, req); + if (!maybe_chainman) return false; + ChainstateManager& chainman = *maybe_chainman; { auto process_utxos = [&vOutPoints, &outs, &hits](const CCoinsView& view, const CTxMemPool& mempool) { for (const COutPoint& vOutPoint : vOutPoints) { @@ -644,7 +671,9 @@ static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req, CBlockIndex* pblockindex = nullptr; { - ChainstateManager& chainman = EnsureAnyChainman(context); + ChainstateManager* maybe_chainman = GetChainman(context, req); + if (!maybe_chainman) return false; + ChainstateManager& chainman = *maybe_chainman; LOCK(cs_main); const CChain& active_chain = chainman.ActiveChain(); if (blockheight > active_chain.Height()) { -- cgit v1.2.3