Skip to content

Commit 5e521d3

Browse files
committed
Merge pull request #5391
932ef50 [REST] JSON output: remove block infos from tx details if it is nested in block (Jonas Schnelli) cae5486 [REST] added /rest/block/notxdetails/<hash> into REST-interface.md documentation (Jonas Schnelli) 73351c3 [REST] /rest/block response with full tx details (Jonas Schnelli)
2 parents 3446806 + 932ef50 commit 5e521d3

File tree

4 files changed

+65
-7
lines changed

4 files changed

+65
-7
lines changed

doc/REST-interface.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ Given a transaction hash,
1111
Returns a transaction, in binary, hex-encoded binary or JSON formats.
1212

1313
`GET /rest/block/BLOCK-HASH.{bin|hex|json}`
14+
`GET /rest/block/notxdetails/BLOCK-HASH.{bin|hex|json}`
1415

1516
Given a block hash,
1617
Returns a block, in binary, hex-encoded binary or JSON formats.
1718

1819
The HTTP request and response are both handled entirely in-memory, thus making maximum memory usage at least 2.66MB (1 MB max block, plus hex encoding) per request.
1920

21+
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.
22+
2023
For full TX query capability, one must enable the transaction index via "txindex=1" command line / configuration option.
2124

2225
Risks

qa/rpc-tests/rest.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def run_test(self):
4848
assert_equal(json_obj['hash'], bb_hash)
4949

5050
# do tx test
51-
tx_hash = json_obj['tx'][0];
51+
tx_hash = json_obj['tx'][0]['txid'];
5252
json_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"json")
5353
json_obj = json.loads(json_string)
5454
assert_equal(json_obj['txid'], tx_hash)
@@ -57,6 +57,33 @@ def run_test(self):
5757
hex_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"hex", True)
5858
assert_equal(response.status, 200)
5959
assert_greater_than(int(response.getheader('content-length')), 10)
60+
61+
# check block tx details
62+
# let's make 3 tx and mine them on node 1
63+
txs = []
64+
txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11))
65+
txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11))
66+
txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11))
67+
self.sync_all()
68+
69+
# now mine the transactions
70+
newblockhash = self.nodes[1].setgenerate(True, 1)
71+
self.sync_all()
72+
73+
#check if the 3 tx show up in the new block
74+
json_string = http_get_call(url.hostname, url.port, '/rest/block/'+newblockhash[0]+self.FORMAT_SEPARATOR+'json')
75+
json_obj = json.loads(json_string)
76+
for tx in json_obj['tx']:
77+
if not 'coinbase' in tx['vin'][0]: #exclude coinbase
78+
assert_equal(tx['txid'] in txs, True)
79+
80+
#check the same but without tx details
81+
json_string = http_get_call(url.hostname, url.port, '/rest/block/notxdetails/'+newblockhash[0]+self.FORMAT_SEPARATOR+'json')
82+
json_obj = json.loads(json_string)
83+
for tx in txs:
84+
assert_equal(tx in json_obj['tx'], True)
85+
86+
6087

6188
if __name__ == '__main__':
6289
RESTTest ().main ()

src/rest.cpp

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class RestErr
4242
};
4343

4444
extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry);
45-
extern Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex);
45+
extern Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false);
4646

4747
static RestErr RESTERR(enum HTTPStatusCode status, string message)
4848
{
@@ -92,7 +92,8 @@ static bool ParseHashStr(const string& strReq, uint256& v)
9292
static bool rest_block(AcceptedConnection* conn,
9393
string& strReq,
9494
map<string, string>& mapHeaders,
95-
bool fRun)
95+
bool fRun,
96+
bool showTxDetails)
9697
{
9798
vector<string> params;
9899
enum RetFormat rf = ParseDataFormat(params, strReq);
@@ -131,7 +132,7 @@ static bool rest_block(AcceptedConnection* conn,
131132
}
132133

133134
case RF_JSON: {
134-
Object objBlock = blockToJSON(block, pblockindex);
135+
Object objBlock = blockToJSON(block, pblockindex, showTxDetails);
135136
string strJSON = write_string(Value(objBlock), false) + "\n";
136137
conn->stream() << HTTPReply(HTTP_OK, strJSON, fRun) << std::flush;
137138
return true;
@@ -146,6 +147,22 @@ static bool rest_block(AcceptedConnection* conn,
146147
return true; // continue to process further HTTP reqs on this cxn
147148
}
148149

150+
static bool rest_block_extended(AcceptedConnection* conn,
151+
string& strReq,
152+
map<string, string>& mapHeaders,
153+
bool fRun)
154+
{
155+
return rest_block(conn, strReq, mapHeaders, fRun, true);
156+
}
157+
158+
static bool rest_block_notxdetails(AcceptedConnection* conn,
159+
string& strReq,
160+
map<string, string>& mapHeaders,
161+
bool fRun)
162+
{
163+
return rest_block(conn, strReq, mapHeaders, fRun, false);
164+
}
165+
149166
static bool rest_tx(AcceptedConnection* conn,
150167
string& strReq,
151168
map<string, string>& mapHeaders,
@@ -205,7 +222,8 @@ static const struct {
205222
bool fRun);
206223
} uri_prefixes[] = {
207224
{"/rest/tx/", rest_tx},
208-
{"/rest/block/", rest_block},
225+
{"/rest/block/notxdetails/", rest_block_notxdetails},
226+
{"/rest/block/", rest_block_extended},
209227
};
210228

211229
bool HTTPReq_REST(AcceptedConnection* conn,

src/rpcblockchain.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using namespace json_spirit;
1717
using namespace std;
1818

19+
extern void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry);
1920
void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out, bool fIncludeHex);
2021

2122
double GetDifficulty(const CBlockIndex* blockindex)
@@ -50,7 +51,7 @@ double GetDifficulty(const CBlockIndex* blockindex)
5051
}
5152

5253

53-
Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex)
54+
Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool txDetails = false)
5455
{
5556
Object result;
5657
result.push_back(Pair("hash", block.GetHash().GetHex()));
@@ -65,7 +66,16 @@ Object blockToJSON(const CBlock& block, const CBlockIndex* blockindex)
6566
result.push_back(Pair("merkleroot", block.hashMerkleRoot.GetHex()));
6667
Array txs;
6768
BOOST_FOREACH(const CTransaction&tx, block.vtx)
68-
txs.push_back(tx.GetHash().GetHex());
69+
{
70+
if(txDetails)
71+
{
72+
Object objTx;
73+
TxToJSON(tx, uint256(0), objTx);
74+
txs.push_back(objTx);
75+
}
76+
else
77+
txs.push_back(tx.GetHash().GetHex());
78+
}
6979
result.push_back(Pair("tx", txs));
7080
result.push_back(Pair("time", block.GetBlockTime()));
7181
result.push_back(Pair("nonce", (uint64_t)block.nNonce));

0 commit comments

Comments
 (0)