diff --git a/doc/REST-interface.md b/doc/REST-interface.md --- a/doc/REST-interface.md +++ b/doc/REST-interface.md @@ -31,6 +31,11 @@ Given a block hash: returns amount of blockheaders in upward direction. +#### Blockhash by height +`GET /rest/blockhashbyheight/.` + +Given a height: returns hash of block in best-block-chain at height provided. + #### Chaininfos `GET /rest/chaininfo.json` diff --git a/src/rest.cpp b/src/rest.cpp --- a/src/rest.cpp +++ b/src/rest.cpp @@ -658,6 +658,57 @@ } } +static bool rest_blockhash_by_height(Config &config, 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)(Config &config, HTTPRequest *req, @@ -671,6 +722,7 @@ {"/rest/mempool/contents", rest_mempool_contents}, {"/rest/headers/", rest_headers}, {"/rest/getutxos", rest_getutxos}, + {"/rest/blockhashbyheight/", rest_blockhash_by_height}, }; void StartREST() { diff --git a/test/functional/interface_rest.py b/test/functional/interface_rest.py --- a/test/functional/interface_rest.py +++ b/test/functional/interface_rest.py @@ -228,7 +228,7 @@ 1) self.sync_all() - self.log.info("Test the /block and /headers URIs") + self.log.info("Test the /block, /blockhashbyheight and /headers URIs") bb_hash = self.nodes[0].getbestblockhash() # Check binary format @@ -263,6 +263,50 @@ # Check json format block_json_obj = self.test_rest_request("/block/{}".format(bb_hash)) assert_equal(block_json_obj['hash'], bb_hash) + assert_equal(self.test_rest_request( + "/blockhashbyheight/{}".format(block_json_obj['height']))['blockhash'], bb_hash) + + # Check hex/bin format + resp_hex = self.test_rest_request( + "/blockhashbyheight/{}".format( + block_json_obj['height']), + req_type=ReqType.HEX, + ret_type=RetType.OBJ) + assert_equal(resp_hex.read().decode('utf-8').rstrip(), bb_hash) + resp_bytes = self.test_rest_request( + "/blockhashbyheight/{}".format( + block_json_obj['height']), + req_type=ReqType.BIN, + ret_type=RetType.BYTES) + blockhash = binascii.hexlify(resp_bytes[::-1]).decode('utf-8') + assert_equal(blockhash, bb_hash) + + # Check invalid blockhashbyheight requests + resp = self.test_rest_request( + "/blockhashbyheight/abc", + ret_type=RetType.OBJ, + status=400) + assert_equal( + resp.read().decode('utf-8').rstrip(), + "Invalid height: abc") + resp = self.test_rest_request( + "/blockhashbyheight/1000000", + ret_type=RetType.OBJ, + status=404) + assert_equal( + resp.read().decode('utf-8').rstrip(), + "Block height out of range") + resp = self.test_rest_request( + "/blockhashbyheight/-1", + ret_type=RetType.OBJ, + status=400) + assert_equal( + resp.read().decode('utf-8').rstrip(), + "Invalid height: -1") + self.test_rest_request( + "/blockhashbyheight/", + ret_type=RetType.OBJ, + status=400) # Compare with json block header json_obj = self.test_rest_request("/headers/1/{}".format(bb_hash))