Skip to content

Commit 9ecade1

Browse files
committed
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.
1 parent fc1c282 commit 9ecade1

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

src/rest.cpp

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,27 @@ static CTxMemPool* GetMemPool(const std::any& context, HTTPRequest* req)
107107
return node_context->mempool.get();
108108
}
109109

110+
/**
111+
* Get the node context chainstatemanager.
112+
*
113+
* @param[in] req The HTTP request, whose status code will be set if node
114+
* context chainstatemanager is not found.
115+
* @returns Pointer to the chainstatemanager or nullptr if none found.
116+
*/
117+
static ChainstateManager* GetChainman(const std::any& context, HTTPRequest* req)
118+
{
119+
auto node_context = util::AnyPtr<NodeContext>(context);
120+
if (!node_context || !node_context->chainman) {
121+
RESTERR(req, HTTP_INTERNAL_SERVER_ERROR,
122+
strprintf("%s:%d (%s)\n"
123+
"Internal bug detected: Chainman disabled or instance not found!\n"
124+
"You may report this issue here: %s\n",
125+
__FILE__, __LINE__, __func__, PACKAGE_BUGREPORT));
126+
return nullptr;
127+
}
128+
return node_context->chainman;
129+
}
130+
110131
static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
111132
{
112133
const std::string::size_type pos = strReq.rfind('.');
@@ -181,7 +202,9 @@ static bool rest_headers(const std::any& context,
181202
std::vector<const CBlockIndex *> headers;
182203
headers.reserve(count);
183204
{
184-
ChainstateManager& chainman = EnsureAnyChainman(context);
205+
ChainstateManager* maybe_chainman = GetChainman(context, req);
206+
if (!maybe_chainman) return false;
207+
ChainstateManager& chainman = *maybe_chainman;
185208
LOCK(cs_main);
186209
CChain& active_chain = chainman.ActiveChain();
187210
tip = active_chain.Tip();
@@ -252,7 +275,9 @@ static bool rest_block(const std::any& context,
252275
CBlockIndex* pblockindex = nullptr;
253276
CBlockIndex* tip = nullptr;
254277
{
255-
ChainstateManager& chainman = EnsureAnyChainman(context);
278+
ChainstateManager* maybe_chainman = GetChainman(context, req);
279+
if (!maybe_chainman) return false;
280+
ChainstateManager& chainman = *maybe_chainman;
256281
LOCK(cs_main);
257282
tip = chainman.ActiveChain().Tip();
258283
pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
@@ -541,7 +566,9 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
541566
std::string bitmapStringRepresentation;
542567
std::vector<bool> hits;
543568
bitmap.resize((vOutPoints.size() + 7) / 8);
544-
ChainstateManager& chainman = EnsureAnyChainman(context);
569+
ChainstateManager* maybe_chainman = GetChainman(context, req);
570+
if (!maybe_chainman) return false;
571+
ChainstateManager& chainman = *maybe_chainman;
545572
{
546573
auto process_utxos = [&vOutPoints, &outs, &hits](const CCoinsView& view, const CTxMemPool& mempool) {
547574
for (const COutPoint& vOutPoint : vOutPoints) {
@@ -644,7 +671,9 @@ static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
644671

645672
CBlockIndex* pblockindex = nullptr;
646673
{
647-
ChainstateManager& chainman = EnsureAnyChainman(context);
674+
ChainstateManager* maybe_chainman = GetChainman(context, req);
675+
if (!maybe_chainman) return false;
676+
ChainstateManager& chainman = *maybe_chainman;
648677
LOCK(cs_main);
649678
const CChain& active_chain = chainman.ActiveChain();
650679
if (blockheight > active_chain.Height()) {

0 commit comments

Comments
 (0)