rest: Use existing NodeContext

This commit is contained in:
Carl Dong
2020-10-14 16:16:34 -04:00
parent 3f08934799
commit d7824acdb9

View File

@ -181,13 +181,14 @@ static bool rest_headers(const std::any& context,
headers.reserve(count); headers.reserve(count);
{ {
LOCK(cs_main); LOCK(cs_main);
tip = ::ChainActive().Tip(); ChainstateManager& chainman = EnsureChainman(context);
const CBlockIndex* pindex = g_chainman.m_blockman.LookupBlockIndex(hash); tip = chainman.ActiveChain().Tip();
while (pindex != nullptr && ::ChainActive().Contains(pindex)) { const CBlockIndex* pindex = chainman.m_blockman.LookupBlockIndex(hash);
while (pindex != nullptr && chainman.ActiveChain().Contains(pindex)) {
headers.push_back(pindex); headers.push_back(pindex);
if (headers.size() == (unsigned long)count) if (headers.size() == (unsigned long)count)
break; break;
pindex = ::ChainActive().Next(pindex); pindex = chainman.ActiveChain().Next(pindex);
} }
} }
@ -250,8 +251,9 @@ static bool rest_block(const std::any& context,
CBlockIndex* tip = nullptr; CBlockIndex* tip = nullptr;
{ {
LOCK(cs_main); LOCK(cs_main);
tip = ::ChainActive().Tip(); ChainstateManager& chainman = EnsureChainman(context);
pblockindex = g_chainman.m_blockman.LookupBlockIndex(hash); tip = chainman.ActiveChain().Tip();
pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
if (!pblockindex) { if (!pblockindex) {
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found"); return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
} }
@ -536,6 +538,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
std::string bitmapStringRepresentation; std::string bitmapStringRepresentation;
std::vector<bool> hits; std::vector<bool> hits;
bitmap.resize((vOutPoints.size() + 7) / 8); bitmap.resize((vOutPoints.size() + 7) / 8);
ChainstateManager& chainman = EnsureChainman(context);
{ {
auto process_utxos = [&vOutPoints, &outs, &hits](const CCoinsView& view, const CTxMemPool& mempool) { auto process_utxos = [&vOutPoints, &outs, &hits](const CCoinsView& view, const CTxMemPool& mempool) {
for (const COutPoint& vOutPoint : vOutPoints) { for (const COutPoint& vOutPoint : vOutPoints) {
@ -551,12 +554,12 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
if (!mempool) return false; if (!mempool) return false;
// use db+mempool as cache backend in case user likes to query mempool // use db+mempool as cache backend in case user likes to query mempool
LOCK2(cs_main, mempool->cs); LOCK2(cs_main, mempool->cs);
CCoinsViewCache& viewChain = ::ChainstateActive().CoinsTip(); CCoinsViewCache& viewChain = chainman.ActiveChainstate().CoinsTip();
CCoinsViewMemPool viewMempool(&viewChain, *mempool); CCoinsViewMemPool viewMempool(&viewChain, *mempool);
process_utxos(viewMempool, *mempool); process_utxos(viewMempool, *mempool);
} else { } else {
LOCK(cs_main); // no need to lock mempool! LOCK(cs_main); // no need to lock mempool!
process_utxos(::ChainstateActive().CoinsTip(), CTxMemPool()); process_utxos(chainman.ActiveChainstate().CoinsTip(), CTxMemPool());
} }
for (size_t i = 0; i < hits.size(); ++i) { 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::
// serialize data // serialize data
// use exact same output as mentioned in Bip64 // use exact same output as mentioned in Bip64
CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
ssGetUTXOResponse << ::ChainActive().Height() << ::ChainActive().Tip()->GetBlockHash() << bitmap << outs; ssGetUTXOResponse << chainman.ActiveChain().Height() << chainman.ActiveChain().Tip()->GetBlockHash() << bitmap << outs;
std::string ssGetUTXOResponseString = ssGetUTXOResponse.str(); std::string ssGetUTXOResponseString = ssGetUTXOResponse.str();
req->WriteHeader("Content-Type", "application/octet-stream"); req->WriteHeader("Content-Type", "application/octet-stream");
@ -581,7 +584,7 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
case RetFormat::HEX: { case RetFormat::HEX: {
CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION); CDataStream ssGetUTXOResponse(SER_NETWORK, PROTOCOL_VERSION);
ssGetUTXOResponse << ::ChainActive().Height() << ::ChainActive().Tip()->GetBlockHash() << bitmap << outs; ssGetUTXOResponse << chainman.ActiveChain().Height() << chainman.ActiveChain().Tip()->GetBlockHash() << bitmap << outs;
std::string strHex = HexStr(ssGetUTXOResponse) + "\n"; std::string strHex = HexStr(ssGetUTXOResponse) + "\n";
req->WriteHeader("Content-Type", "text/plain"); req->WriteHeader("Content-Type", "text/plain");
@ -594,8 +597,8 @@ static bool rest_getutxos(const std::any& context, HTTPRequest* req, const std::
// pack in some essentials // pack in some essentials
// use more or less the same output as mentioned in Bip64 // use more or less the same output as mentioned in Bip64
objGetUTXOResponse.pushKV("chainHeight", ::ChainActive().Height()); objGetUTXOResponse.pushKV("chainHeight", chainman.ActiveChain().Height());
objGetUTXOResponse.pushKV("chaintipHash", ::ChainActive().Tip()->GetBlockHash().GetHex()); objGetUTXOResponse.pushKV("chaintipHash", chainman.ActiveChain().Tip()->GetBlockHash().GetHex());
objGetUTXOResponse.pushKV("bitmap", bitmapStringRepresentation); objGetUTXOResponse.pushKV("bitmap", bitmapStringRepresentation);
UniValue utxos(UniValue::VARR); UniValue utxos(UniValue::VARR);
@ -639,10 +642,11 @@ static bool rest_blockhash_by_height(const std::any& context, HTTPRequest* req,
CBlockIndex* pblockindex = nullptr; CBlockIndex* pblockindex = nullptr;
{ {
LOCK(cs_main); LOCK(cs_main);
if (blockheight > ::ChainActive().Height()) { const CChain& active_chain = EnsureChainman(context).ActiveChain();
if (blockheight > active_chain.Height()) {
return RESTERR(req, HTTP_NOT_FOUND, "Block height out of range"); return RESTERR(req, HTTP_NOT_FOUND, "Block height out of range");
} }
pblockindex = ::ChainActive()[blockheight]; pblockindex = active_chain[blockheight];
} }
switch (rf) { switch (rf) {
case RetFormat::BINARY: { case RetFormat::BINARY: {