Browse Source

Merge #5548: [REST] add /rest/chaininfos

2c0f901 [REST] rest/chaininfos add documentation (Jonas Schnelli)
59582c8 [REST] add /rest/chaininfos (Jonas Schnelli)
0.13
Wladimir J. van der Laan 10 years ago
parent
commit
d48ce48093
No known key found for this signature in database
GPG Key ID: 74810B012346C9A6
  1. 14
      doc/REST-interface.md
  2. 9
      qa/rpc-tests/rest.py
  3. 33
      src/rest.cpp

14
doc/REST-interface.md

@ -10,6 +10,8 @@ Supported API
Given a transaction hash, Given a transaction hash,
Returns a transaction, in binary, hex-encoded binary or JSON formats. Returns a transaction, in binary, hex-encoded binary or JSON formats.
For full TX query capability, one must enable the transaction index via "txindex=1" command line / configuration option.
`GET /rest/block/BLOCK-HASH.{bin|hex|json}` `GET /rest/block/BLOCK-HASH.{bin|hex|json}`
`GET /rest/block/notxdetails/BLOCK-HASH.{bin|hex|json}` `GET /rest/block/notxdetails/BLOCK-HASH.{bin|hex|json}`
@ -20,7 +22,17 @@ The HTTP request and response are both handled entirely in-memory, thus making m
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. 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.
For full TX query capability, one must enable the transaction index via "txindex=1" command line / configuration option. `GET /rest/chaininfo.json`
Returns various state info regarding block chain processing.
Only supports JSON as output format.
* chain : (string) current network name as defined in BIP70 (main, test, regtest)
* blocks : (numeric) the current number of blocks processed in the server
* headers : (numeric) the current number of headers we have validated
* bestblockhash : (string) the hash of the currently best block
* difficulty : (numeric) the current difficulty
* verificationprogress : (numeric) estimate of verification progress [0..1]
* chainwork : (string) total amount of work in active chain, in hexadecimal
Risks Risks
------------- -------------

9
qa/rpc-tests/rest.py

@ -78,7 +78,7 @@ class RESTTest (BitcoinTestFramework):
# check hex format response # check hex format response
hex_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"hex", True) hex_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"hex", True)
assert_equal(response.status, 200) assert_equal(hex_string.status, 200)
assert_greater_than(int(response.getheader('content-length')), 10) assert_greater_than(int(response.getheader('content-length')), 10)
# check block tx details # check block tx details
@ -106,5 +106,12 @@ class RESTTest (BitcoinTestFramework):
for tx in txs: for tx in txs:
assert_equal(tx in json_obj['tx'], True) assert_equal(tx in json_obj['tx'], True)
#test rest bestblock
bb_hash = self.nodes[0].getbestblockhash()
json_string = http_get_call(url.hostname, url.port, '/rest/chaininfo.json')
json_obj = json.loads(json_string)
assert_equal(json_obj['bestblockhash'], bb_hash)
if __name__ == '__main__': if __name__ == '__main__':
RESTTest ().main () RESTTest ().main ()

33
src/rest.cpp

@ -95,7 +95,7 @@ static bool rest_headers(AcceptedConnection* conn,
bool fRun) bool fRun)
{ {
vector<string> params; vector<string> params;
enum RetFormat rf = ParseDataFormat(params, strReq); const RetFormat rf = ParseDataFormat(params, strReq);
vector<string> path; vector<string> path;
boost::split(path, params[0], boost::is_any_of("/")); boost::split(path, params[0], boost::is_any_of("/"));
@ -159,7 +159,7 @@ static bool rest_block(AcceptedConnection* conn,
bool showTxDetails) bool showTxDetails)
{ {
vector<string> params; vector<string> params;
enum RetFormat rf = ParseDataFormat(params, strReq); const RetFormat rf = ParseDataFormat(params, strReq);
string hashStr = params[0]; string hashStr = params[0];
uint256 hash; uint256 hash;
@ -226,13 +226,39 @@ static bool rest_block_notxdetails(AcceptedConnection* conn,
return rest_block(conn, strReq, mapHeaders, fRun, false); return rest_block(conn, strReq, mapHeaders, fRun, false);
} }
static bool rest_chaininfo(AcceptedConnection* conn,
const std::string& strReq,
const std::map<std::string, std::string>& mapHeaders,
bool fRun)
{
vector<string> params;
const RetFormat rf = ParseDataFormat(params, strReq);
switch (rf) {
case RF_JSON: {
Array rpcParams;
Value chainInfoObject = getblockchaininfo(rpcParams, false);
string strJSON = write_string(chainInfoObject, false) + "\n";
conn->stream() << HTTPReply(HTTP_OK, strJSON, fRun) << std::flush;
return true;
}
default: {
throw RESTERR(HTTP_NOT_FOUND, "output format not found (available: json)");
}
}
// not reached
return true; // continue to process further HTTP reqs on this cxn
}
static bool rest_tx(AcceptedConnection* conn, static bool rest_tx(AcceptedConnection* conn,
const std::string& strReq, const std::string& strReq,
const std::map<std::string, std::string>& mapHeaders, const std::map<std::string, std::string>& mapHeaders,
bool fRun) bool fRun)
{ {
vector<string> params; vector<string> params;
enum RetFormat rf = ParseDataFormat(params, strReq); const RetFormat rf = ParseDataFormat(params, strReq);
string hashStr = params[0]; string hashStr = params[0];
uint256 hash; uint256 hash;
@ -287,6 +313,7 @@ static const struct {
{"/rest/tx/", rest_tx}, {"/rest/tx/", rest_tx},
{"/rest/block/notxdetails/", rest_block_notxdetails}, {"/rest/block/notxdetails/", rest_block_notxdetails},
{"/rest/block/", rest_block_extended}, {"/rest/block/", rest_block_extended},
{"/rest/chaininfo", rest_chaininfo},
{"/rest/headers/", rest_headers}, {"/rest/headers/", rest_headers},
}; };

Loading…
Cancel
Save