Skip to content

Commit

Permalink
REST: add "blockhashbyheight" call, fetch blockhash by height
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasschnelli committed Jan 21, 2019
1 parent c932730 commit eb9ef04
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/rest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,52 @@ static bool rest_getutxos(HTTPRequest* req, const std::string& strURIPart)
}
}

static bool rest_blockhash_by_height(HTTPRequest* req,
const std::string& str_uri_part)
{
if (!CheckWarmup(req)) return false;
std::string height_str;
const RetFormat rf = ParseDataFormat(height_str, str_uri_part);

int32_t blockheight;
if (!ParseInt32(height_str, &blockheight) || blockheight < 0) {
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid height: " + SanitizeString(height_str));
}

CBlockIndex* pblockindex = nullptr;
{
LOCK(cs_main);
if (blockheight > chainActive.Height()) {
return RESTERR(req, HTTP_NOT_FOUND, "Block height out of range");
}
pblockindex = chainActive[blockheight];
}
switch (rf) {
case RetFormat::BINARY: {
CDataStream ss_blockhash(SER_NETWORK, PROTOCOL_VERSION);
ss_blockhash << pblockindex->GetBlockHash();
req->WriteHeader("Content-Type", "application/octet-stream");
req->WriteReply(HTTP_OK, ss_blockhash.str());
return true;
}
case RetFormat::HEX: {
req->WriteHeader("Content-Type", "text/plain");
req->WriteReply(HTTP_OK, pblockindex->GetBlockHash().GetHex() + "\n");
return true;
}
case RetFormat::JSON: {
req->WriteHeader("Content-Type", "application/json");
UniValue resp = UniValue(UniValue::VOBJ);
resp.pushKV("blockhash", pblockindex->GetBlockHash().GetHex());
req->WriteReply(HTTP_OK, resp.write() + "\n");
return true;
}
default: {
return RESTERR(req, HTTP_NOT_FOUND, "output format not found (available: " + AvailableDataFormatsString() + ")");
}
}
}

static const struct {
const char* prefix;
bool (*handler)(HTTPRequest* req, const std::string& strReq);
Expand All @@ -589,6 +635,7 @@ static const struct {
{"/rest/mempool/contents", rest_mempool_contents},
{"/rest/headers/", rest_headers},
{"/rest/getutxos", rest_getutxos},
{"/rest/blockhashbyheight/", rest_blockhash_by_height},
};

void StartREST()
Expand Down

0 comments on commit eb9ef04

Please sign in to comment.