Changeset View
Changeset View
Standalone View
Standalone View
src/rest.cpp
Show First 20 Lines • Show All 150 Lines • ▼ Show 20 Lines | static bool rest_headers(Config &config, HTTPRequest *req, | ||||
} | } | ||||
std::string hashStr = path[1]; | std::string hashStr = path[1]; | ||||
uint256 hash; | uint256 hash; | ||||
if (!ParseHashStr(hashStr, hash)) { | if (!ParseHashStr(hashStr, hash)) { | ||||
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr); | return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr); | ||||
} | } | ||||
const CBlockIndex *tip = nullptr; | |||||
std::vector<const CBlockIndex *> headers; | std::vector<const CBlockIndex *> headers; | ||||
headers.reserve(count); | headers.reserve(count); | ||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
tip = chainActive.Tip(); | |||||
const CBlockIndex *pindex = LookupBlockIndex(hash); | const CBlockIndex *pindex = LookupBlockIndex(hash); | ||||
while (pindex != nullptr && chainActive.Contains(pindex)) { | while (pindex != nullptr && chainActive.Contains(pindex)) { | ||||
headers.push_back(pindex); | headers.push_back(pindex); | ||||
if (headers.size() == size_t(count)) { | if (headers.size() == size_t(count)) { | ||||
break; | break; | ||||
} | } | ||||
pindex = chainActive.Next(pindex); | pindex = chainActive.Next(pindex); | ||||
} | } | ||||
Show All 16 Lines | switch (rf) { | ||||
std::string strHex = | std::string strHex = | ||||
HexStr(ssHeader.begin(), ssHeader.end()) + "\n"; | HexStr(ssHeader.begin(), ssHeader.end()) + "\n"; | ||||
req->WriteHeader("Content-Type", "text/plain"); | req->WriteHeader("Content-Type", "text/plain"); | ||||
req->WriteReply(HTTP_OK, strHex); | req->WriteReply(HTTP_OK, strHex); | ||||
return true; | return true; | ||||
} | } | ||||
case RetFormat::JSON: { | case RetFormat::JSON: { | ||||
UniValue jsonHeaders(UniValue::VARR); | UniValue jsonHeaders(UniValue::VARR); | ||||
{ | |||||
LOCK(cs_main); | |||||
for (const CBlockIndex *pindex : headers) { | for (const CBlockIndex *pindex : headers) { | ||||
jsonHeaders.push_back(blockheaderToJSON(pindex)); | jsonHeaders.push_back(blockheaderToJSON(tip, pindex)); | ||||
} | |||||
} | } | ||||
std::string strJSON = jsonHeaders.write() + "\n"; | std::string strJSON = jsonHeaders.write() + "\n"; | ||||
req->WriteHeader("Content-Type", "application/json"); | req->WriteHeader("Content-Type", "application/json"); | ||||
req->WriteReply(HTTP_OK, strJSON); | req->WriteReply(HTTP_OK, strJSON); | ||||
return true; | return true; | ||||
} | } | ||||
default: { | default: { | ||||
return RESTERR(req, HTTP_NOT_FOUND, | return RESTERR(req, HTTP_NOT_FOUND, | ||||
Show All 17 Lines | static bool rest_block(const Config &config, HTTPRequest *req, | ||||
uint256 hash; | uint256 hash; | ||||
if (!ParseHashStr(hashStr, hash)) { | if (!ParseHashStr(hashStr, hash)) { | ||||
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr); | return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr); | ||||
} | } | ||||
CBlock block; | CBlock block; | ||||
CBlockIndex *pblockindex = nullptr; | CBlockIndex *pblockindex = nullptr; | ||||
CBlockIndex *tip = nullptr; | |||||
{ | { | ||||
LOCK(cs_main); | LOCK(cs_main); | ||||
tip = chainActive.Tip(); | |||||
pblockindex = LookupBlockIndex(hash); | pblockindex = LookupBlockIndex(hash); | ||||
if (!pblockindex) { | if (!pblockindex) { | ||||
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found"); | return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found"); | ||||
} | } | ||||
if (fHavePruned && !pblockindex->nStatus.hasData() && | if (fHavePruned && !pblockindex->nStatus.hasData() && | ||||
pblockindex->nTx > 0) { | pblockindex->nTx > 0) { | ||||
return RESTERR(req, HTTP_NOT_FOUND, | return RESTERR(req, HTTP_NOT_FOUND, | ||||
Show All 20 Lines | switch (rf) { | ||||
case RetFormat::HEX: { | case RetFormat::HEX: { | ||||
std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()) + "\n"; | std::string strHex = HexStr(ssBlock.begin(), ssBlock.end()) + "\n"; | ||||
req->WriteHeader("Content-Type", "text/plain"); | req->WriteHeader("Content-Type", "text/plain"); | ||||
req->WriteReply(HTTP_OK, strHex); | req->WriteReply(HTTP_OK, strHex); | ||||
return true; | return true; | ||||
} | } | ||||
case RetFormat::JSON: { | case RetFormat::JSON: { | ||||
UniValue objBlock; | UniValue objBlock = | ||||
{ | blockToJSON(block, tip, pblockindex, showTxDetails); | ||||
LOCK(cs_main); | |||||
objBlock = blockToJSON(block, pblockindex, showTxDetails); | |||||
} | |||||
std::string strJSON = objBlock.write() + "\n"; | std::string strJSON = objBlock.write() + "\n"; | ||||
req->WriteHeader("Content-Type", "application/json"); | req->WriteHeader("Content-Type", "application/json"); | ||||
req->WriteReply(HTTP_OK, strJSON); | req->WriteReply(HTTP_OK, strJSON); | ||||
return true; | return true; | ||||
} | } | ||||
default: { | default: { | ||||
return RESTERR(req, HTTP_NOT_FOUND, | return RESTERR(req, HTTP_NOT_FOUND, | ||||
▲ Show 20 Lines • Show All 419 Lines • Show Last 20 Lines |