Skip to content

Commit 943b322

Browse files
committed
Merge pull request #5486
c45c7ea [REST] add JSON support for /rest/headers/ (Jonas Schnelli)
2 parents 4452205 + c45c7ea commit 943b322

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

doc/REST-interface.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ The HTTP request and response are both handled entirely in-memory, thus making m
2626
With the /notxdetails/ option JSON response will only contain the transaction hash instead of the complete transaction details. The option only affects the JSON response.
2727

2828
####Blockheaders
29-
`GET /rest/headers/<COUNT>/<BLOCK-HASH>.<bin|hex>`
29+
`GET /rest/headers/<COUNT>/<BLOCK-HASH>.<bin|hex|json>`
3030

3131
Given a block hash,
3232
Returns <COUNT> amount of blockheaders in upward direction.
3333

34-
JSON is not supported.
35-
3634
####Chaininfos
3735
`GET /rest/chaininfo.json`
3836

qa/rpc-tests/rest.py

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,12 +235,43 @@ def run_test(self):
235235
assert_equal(response_header_str.encode("hex")[0:160], response_header_hex_str[0:160])
236236

237237
# check json format
238-
json_string = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+'json')
239-
json_obj = json.loads(json_string)
240-
assert_equal(json_obj['hash'], bb_hash)
238+
block_json_string = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+'json')
239+
block_json_obj = json.loads(block_json_string)
240+
assert_equal(block_json_obj['hash'], bb_hash)
241+
242+
# compare with json block header
243+
response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"json", "", True)
244+
assert_equal(response_header_json.status, 200)
245+
response_header_json_str = response_header_json.read()
246+
json_obj = json.loads(response_header_json_str)
247+
assert_equal(len(json_obj), 1) #ensure that there is one header in the json response
248+
assert_equal(json_obj[0]['hash'], bb_hash) #request/response hash should be the same
249+
250+
#compare with normal RPC block response
251+
rpc_block_json = self.nodes[0].getblock(bb_hash)
252+
assert_equal(json_obj[0]['hash'], rpc_block_json['hash'])
253+
assert_equal(json_obj[0]['confirmations'], rpc_block_json['confirmations'])
254+
assert_equal(json_obj[0]['height'], rpc_block_json['height'])
255+
assert_equal(json_obj[0]['version'], rpc_block_json['version'])
256+
assert_equal(json_obj[0]['merkleroot'], rpc_block_json['merkleroot'])
257+
assert_equal(json_obj[0]['time'], rpc_block_json['time'])
258+
assert_equal(json_obj[0]['nonce'], rpc_block_json['nonce'])
259+
assert_equal(json_obj[0]['bits'], rpc_block_json['bits'])
260+
assert_equal(json_obj[0]['difficulty'], rpc_block_json['difficulty'])
261+
assert_equal(json_obj[0]['chainwork'], rpc_block_json['chainwork'])
262+
assert_equal(json_obj[0]['previousblockhash'], rpc_block_json['previousblockhash'])
263+
264+
#see if we can get 5 headers in one response
265+
self.nodes[1].generate(5)
266+
self.sync_all()
267+
response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/'+bb_hash+self.FORMAT_SEPARATOR+"json", "", True)
268+
assert_equal(response_header_json.status, 200)
269+
response_header_json_str = response_header_json.read()
270+
json_obj = json.loads(response_header_json_str)
271+
assert_equal(len(json_obj), 5) #now we should have 5 header objects
241272

242273
# do tx test
243-
tx_hash = json_obj['tx'][0]['txid'];
274+
tx_hash = block_json_obj['tx'][0]['txid'];
244275
json_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"json")
245276
json_obj = json.loads(json_string)
246277
assert_equal(json_obj['txid'], tx_hash)

src/rest.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class RestErr
6565
extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry);
6666
extern UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false);
6767
extern void ScriptPubKeyToJSON(const CScript& scriptPubKey, UniValue& out, bool fIncludeHex);
68+
extern UniValue blockheaderToJSON(const CBlockIndex* blockindex);
6869

6970
static RestErr RESTERR(enum HTTPStatusCode status, string message)
7071
{
@@ -134,23 +135,23 @@ static bool rest_headers(AcceptedConnection* conn,
134135
if (!ParseHashStr(hashStr, hash))
135136
throw RESTERR(HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
136137

137-
std::vector<CBlockHeader> headers;
138+
std::vector<const CBlockIndex *> headers;
138139
headers.reserve(count);
139140
{
140141
LOCK(cs_main);
141142
BlockMap::const_iterator it = mapBlockIndex.find(hash);
142143
const CBlockIndex *pindex = (it != mapBlockIndex.end()) ? it->second : NULL;
143144
while (pindex != NULL && chainActive.Contains(pindex)) {
144-
headers.push_back(pindex->GetBlockHeader());
145+
headers.push_back(pindex);
145146
if (headers.size() == (unsigned long)count)
146147
break;
147148
pindex = chainActive.Next(pindex);
148149
}
149150
}
150151

151152
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
152-
BOOST_FOREACH(const CBlockHeader &header, headers) {
153-
ssHeader << header;
153+
BOOST_FOREACH(const CBlockIndex *pindex, headers) {
154+
ssHeader << pindex->GetBlockHeader();
154155
}
155156

156157
switch (rf) {
@@ -166,6 +167,16 @@ static bool rest_headers(AcceptedConnection* conn,
166167
return true;
167168
}
168169

170+
case RF_JSON: {
171+
UniValue jsonHeaders(UniValue::VARR);
172+
BOOST_FOREACH(const CBlockIndex *pindex, headers) {
173+
jsonHeaders.push_back(blockheaderToJSON(pindex));
174+
}
175+
string strJSON = jsonHeaders.write() + "\n";
176+
conn->stream() << HTTPReply(HTTP_OK, strJSON, fRun) << std::flush;
177+
return true;
178+
}
179+
169180
default: {
170181
throw RESTERR(HTTP_NOT_FOUND, "output format not found (available: .bin, .hex)");
171182
}

src/rpcblockchain.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
7777
return result;
7878
}
7979

80-
8180
UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false)
8281
{
8382
UniValue result(UniValue::VOBJ);
@@ -118,7 +117,6 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
118117
return result;
119118
}
120119

121-
122120
UniValue getblockcount(const UniValue& params, bool fHelp)
123121
{
124122
if (fHelp || params.size() != 0)

0 commit comments

Comments
 (0)