13
13
#include < chain.h>
14
14
#include < chainparams.h>
15
15
#include < core_io.h>
16
+ #include < flatfile.h>
16
17
#include < httpserver.h>
17
18
#include < index/blockfilterindex.h>
18
19
#include < index/txindex.h>
34
35
#include < validation.h>
35
36
36
37
#include < any>
37
- #include < string >
38
+ #include < vector >
38
39
39
40
#include < univalue.h>
40
41
@@ -295,7 +296,7 @@ static bool rest_block(const std::any& context,
295
296
if (!ParseHashStr (hashStr, hash))
296
297
return RESTERR (req, HTTP_BAD_REQUEST, " Invalid hash: " + hashStr);
297
298
298
- CBlock block ;
299
+ FlatFilePos pos{} ;
299
300
const CBlockIndex* pblockindex = nullptr ;
300
301
const CBlockIndex* tip = nullptr ;
301
302
ChainstateManager* maybe_chainman = GetChainman (context, req);
@@ -311,32 +312,33 @@ static bool rest_block(const std::any& context,
311
312
if (chainman.m_blockman .IsBlockPruned (*pblockindex)) {
312
313
return RESTERR (req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)" );
313
314
}
315
+ pos = pblockindex->GetBlockPos ();
314
316
}
315
317
316
- if (!chainman.m_blockman .ReadBlockFromDisk (block, *pblockindex)) {
318
+ std::vector<uint8_t > block_data{};
319
+ if (!chainman.m_blockman .ReadRawBlockFromDisk (block_data, pos)) {
317
320
return RESTERR (req, HTTP_NOT_FOUND, hashStr + " not found" );
318
321
}
319
322
320
323
switch (rf) {
321
324
case RESTResponseFormat::BINARY: {
322
- DataStream ssBlock;
323
- ssBlock << TX_WITH_WITNESS (block);
324
- std::string binaryBlock = ssBlock.str ();
325
+ const std::string binaryBlock{block_data.begin (), block_data.end ()};
325
326
req->WriteHeader (" Content-Type" , " application/octet-stream" );
326
327
req->WriteReply (HTTP_OK, binaryBlock);
327
328
return true ;
328
329
}
329
330
330
331
case RESTResponseFormat::HEX: {
331
- DataStream ssBlock;
332
- ssBlock << TX_WITH_WITNESS (block);
333
- std::string strHex = HexStr (ssBlock) + " \n " ;
332
+ const std::string strHex{HexStr (block_data) + " \n " };
334
333
req->WriteHeader (" Content-Type" , " text/plain" );
335
334
req->WriteReply (HTTP_OK, strHex);
336
335
return true ;
337
336
}
338
337
339
338
case RESTResponseFormat::JSON: {
339
+ CBlock block{};
340
+ DataStream block_stream{block_data};
341
+ block_stream >> TX_WITH_WITNESS (block);
340
342
UniValue objBlock = blockToJSON (chainman.m_blockman , block, *tip, *pblockindex, tx_verbosity);
341
343
std::string strJSON = objBlock.write () + " \n " ;
342
344
req->WriteHeader (" Content-Type" , " application/json" );
0 commit comments