Skip to content

Commit d48ce48

Browse files
committed
Merge #5548: [REST] add /rest/chaininfos
2c0f901 [REST] rest/chaininfos add documentation (Jonas Schnelli) 59582c8 [REST] add /rest/chaininfos (Jonas Schnelli)
2 parents a956586 + 2c0f901 commit d48ce48

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

doc/REST-interface.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Supported API
1010
Given a transaction hash,
1111
Returns a transaction, in binary, hex-encoded binary or JSON formats.
1212

13+
For full TX query capability, one must enable the transaction index via "txindex=1" command line / configuration option.
14+
1315
`GET /rest/block/BLOCK-HASH.{bin|hex|json}`
1416
`GET /rest/block/notxdetails/BLOCK-HASH.{bin|hex|json}`
1517

@@ -20,7 +22,17 @@ The HTTP request and response are both handled entirely in-memory, thus making m
2022

2123
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.
2224

23-
For full TX query capability, one must enable the transaction index via "txindex=1" command line / configuration option.
25+
`GET /rest/chaininfo.json`
26+
27+
Returns various state info regarding block chain processing.
28+
Only supports JSON as output format.
29+
* chain : (string) current network name as defined in BIP70 (main, test, regtest)
30+
* blocks : (numeric) the current number of blocks processed in the server
31+
* headers : (numeric) the current number of headers we have validated
32+
* bestblockhash : (string) the hash of the currently best block
33+
* difficulty : (numeric) the current difficulty
34+
* verificationprogress : (numeric) estimate of verification progress [0..1]
35+
* chainwork : (string) total amount of work in active chain, in hexadecimal
2436

2537
Risks
2638
-------------

qa/rpc-tests/rest.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def run_test(self):
7878

7979
# check hex format response
8080
hex_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"hex", True)
81-
assert_equal(response.status, 200)
81+
assert_equal(hex_string.status, 200)
8282
assert_greater_than(int(response.getheader('content-length')), 10)
8383

8484
# check block tx details
@@ -106,5 +106,12 @@ def run_test(self):
106106
for tx in txs:
107107
assert_equal(tx in json_obj['tx'], True)
108108

109+
#test rest bestblock
110+
bb_hash = self.nodes[0].getbestblockhash()
111+
112+
json_string = http_get_call(url.hostname, url.port, '/rest/chaininfo.json')
113+
json_obj = json.loads(json_string)
114+
assert_equal(json_obj['bestblockhash'], bb_hash)
115+
109116
if __name__ == '__main__':
110117
RESTTest ().main ()

src/rest.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ static bool rest_headers(AcceptedConnection* conn,
9595
bool fRun)
9696
{
9797
vector<string> params;
98-
enum RetFormat rf = ParseDataFormat(params, strReq);
98+
const RetFormat rf = ParseDataFormat(params, strReq);
9999
vector<string> path;
100100
boost::split(path, params[0], boost::is_any_of("/"));
101101

@@ -159,7 +159,7 @@ static bool rest_block(AcceptedConnection* conn,
159159
bool showTxDetails)
160160
{
161161
vector<string> params;
162-
enum RetFormat rf = ParseDataFormat(params, strReq);
162+
const RetFormat rf = ParseDataFormat(params, strReq);
163163

164164
string hashStr = params[0];
165165
uint256 hash;
@@ -226,13 +226,39 @@ static bool rest_block_notxdetails(AcceptedConnection* conn,
226226
return rest_block(conn, strReq, mapHeaders, fRun, false);
227227
}
228228

229+
static bool rest_chaininfo(AcceptedConnection* conn,
230+
const std::string& strReq,
231+
const std::map<std::string, std::string>& mapHeaders,
232+
bool fRun)
233+
{
234+
vector<string> params;
235+
const RetFormat rf = ParseDataFormat(params, strReq);
236+
237+
switch (rf) {
238+
case RF_JSON: {
239+
Array rpcParams;
240+
Value chainInfoObject = getblockchaininfo(rpcParams, false);
241+
242+
string strJSON = write_string(chainInfoObject, false) + "\n";
243+
conn->stream() << HTTPReply(HTTP_OK, strJSON, fRun) << std::flush;
244+
return true;
245+
}
246+
default: {
247+
throw RESTERR(HTTP_NOT_FOUND, "output format not found (available: json)");
248+
}
249+
}
250+
251+
// not reached
252+
return true; // continue to process further HTTP reqs on this cxn
253+
}
254+
229255
static bool rest_tx(AcceptedConnection* conn,
230256
const std::string& strReq,
231257
const std::map<std::string, std::string>& mapHeaders,
232258
bool fRun)
233259
{
234260
vector<string> params;
235-
enum RetFormat rf = ParseDataFormat(params, strReq);
261+
const RetFormat rf = ParseDataFormat(params, strReq);
236262

237263
string hashStr = params[0];
238264
uint256 hash;
@@ -287,6 +313,7 @@ static const struct {
287313
{"/rest/tx/", rest_tx},
288314
{"/rest/block/notxdetails/", rest_block_notxdetails},
289315
{"/rest/block/", rest_block_extended},
316+
{"/rest/chaininfo", rest_chaininfo},
290317
{"/rest/headers/", rest_headers},
291318
};
292319

0 commit comments

Comments
 (0)