Skip to content

Commit d7824ac

Browse files
committed
rest: Use existing NodeContext
1 parent 3f08934 commit d7824ac

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

src/rest.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,14 @@ static bool rest_headers(const std::any& context,
181181
headers.reserve(count);
182182
{
183183
LOCK(cs_main);
184-
tip = ::ChainActive().Tip();
185-
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash);
186-
while (pindex != nullptr && ::ChainActive().Contains(pindex)) {
184+
ChainstateManager& chainman = EnsureChainman(context);
185+
tip = chainman.ActiveChain().Tip();
186+
const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
187+
while (pindex != nullptr && chainman.ActiveChain().Contains(pindex)) {
187188
headers.push_back(pindex);
188189
if (headers.size() == (unsigned long)count)
189190
break;
190-
pindex = ::ChainActive().Next(pindex);
191+
pindex = chainman.ActiveChain().Next(pindex);
191192
}
192193
}
193194

@@ -250,8 +251,9 @@ static bool rest_block(const std::any& context,
250251
CBlockIndex* tip = nullptr;
251252
{
252253
LOCK(cs_main);
253-
tip = ::ChainActive().Tip();
254-
pblockindex = g_chainman.m_blockman.LookupBlockIndex(hash);
254+
ChainstateManager& chainman = EnsureChainman(context);
255+
tip = chainman.ActiveChain().Tip();
256+
pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
255257
if (!pblockindex) {
256258
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
257259
}
@@ -536,6 +538,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
536538
std::string bitmapStringRepresentation;
537539
std::vector<bool> hits;
538540
bitmap.resize((vOutPoints.size() + 7) / 8);
541+
ChainstateManager& chainman = EnsureChainman(context);
539542
{
540543
auto process_utxos = [&vOutPoints, &outs, &hits](const CCoinsView& view, const CTxMemPool& mempool) {
541544
for (const COutPoint& vOutPoint : vOutPoints) {
@@ -551,12 +554,12 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
551554
if (!mempool) return false;
552555
// use db+mempool as cache backend in case user likes to query mempool
553556
LOCK2(cs_main, mempool->cs);
554-
CCoinsViewCache& viewChain = ::ChainstateActive().CoinsTip();
557+
CCoinsViewCache& viewChain = chainman.ActiveChainstate().CoinsTip();
555558
CCoinsViewMemPool viewMempool(&viewChain, *mempool);
556559
process_utxos(viewMempool, *mempool);
557560
} else {
558561
LOCK(cs_main); // no need to lock mempool!
559-
process_utxos(::ChainstateActive().CoinsTip(), CTxMemPool());
562+
process_utxos(chainman.ActiveChainstate().CoinsTip(), CTxMemPool());
560563
}
561564

562565
for (size_t i = 0; i < hits.size(); ++i) {
@@ -571,7 +574,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
571574
// serialize data
572575
// use exact same output as mentioned in Bip64
573576
CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
574-
ssGetUTXOResponse << ::ChainActive().Height() << ::ChainActive().Tip()->GetBlockHash() << bitmap << outs;
577+
ssGetUTXOResponse << chainman.ActiveChain().Height() << chainman.ActiveChain().Tip()->GetBlockHash() << bitmap << outs;
575578
std::string ssGetUTXOResponseString = ssGetUTXOResponse.str();
576579

577580
req->WriteHeader("Content-Type", "application/octet-stream");
@@ -581,7 +584,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
581584

582585
case RetFormat::HEX: {
583586
CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
584-
ssGetUTXOResponse << ::ChainActive().Height() << ::ChainActive().Tip()->GetBlockHash() << bitmap << outs;
587+
ssGetUTXOResponse << chainman.ActiveChain().Height() << chainman.ActiveChain().Tip()->GetBlockHash() << bitmap << outs;
585588
std::string strHex = HexStr(ssGetUTXOResponse) + "\n";
586589

587590
req->WriteHeader("Content-Type", "text/plain");
@@ -594,8 +597,8 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
594597

595598
// pack in some essentials
596599
// use more or less the same output as mentioned in Bip64
597-
objGetUTXOResponse.pushKV("chainHeight", ::ChainActive().Height());
598-
objGetUTXOResponse.pushKV("chaintipHash", ::ChainActive().Tip()->GetBlockHash().GetHex());
600+
objGetUTXOResponse.pushKV("chainHeight", chainman.ActiveChain().Height());
601+
objGetUTXOResponse.pushKV("chaintipHash", chainman.ActiveChain().Tip()->GetBlockHash().GetHex());
599602
objGetUTXOResponse.pushKV("bitmap", bitmapStringRepresentation);
600603

601604
UniValue utxos(UniValue::VARR);
@@ -639,10 +642,11 @@ static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
639642
CBlockIndex* pblockindex = nullptr;
640643
{
641644
LOCK(cs_main);
642-
if (blockheight > ::ChainActive().Height()) {
645+
const CChain& active_chain = EnsureChainman(context).ActiveChain();
646+
if (blockheight > active_chain.Height()) {
643647
return RESTERR(req, HTTP_NOT_FOUND, "Block height out of range");
644648
}
645-
pblockindex = ::ChainActive()[blockheight];
649+
pblockindex = active_chain[blockheight];
646650
}
647651
switch (rf) {
648652
case RetFormat::BINARY: {

0 commit comments

Comments
 (0)