1313#include < chain.h>
1414#include < chainparams.h>
1515#include < core_io.h>
16+ #include < flatfile.h>
1617#include < httpserver.h>
1718#include < index/blockfilterindex.h>
1819#include < index/txindex.h>
3435#include < validation.h>
3536
3637#include < any>
37- #include < string >
38+ #include < vector >
3839
3940#include < univalue.h>
4041
@@ -295,7 +296,7 @@ static bool rest_block(const std::any& context,
295296 if (!ParseHashStr (hashStr, hash))
296297 return RESTERR (req, HTTP_BAD_REQUEST, " Invalid hash: " + hashStr);
297298
298- CBlock block ;
299+ FlatFilePos pos{} ;
299300 const CBlockIndex* pblockindex = nullptr ;
300301 const CBlockIndex* tip = nullptr ;
301302 ChainstateManager* maybe_chainman = GetChainman (context, req);
@@ -311,32 +312,33 @@ static bool rest_block(const std::any& context,
311312 if (chainman.m_blockman .IsBlockPruned (*pblockindex)) {
312313 return RESTERR (req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)" );
313314 }
315+ pos = pblockindex->GetBlockPos ();
314316 }
315317
316- if (!chainman.m_blockman .ReadBlockFromDisk (block, *pblockindex)) {
318+ std::vector<uint8_t > block_data{};
319+ if (!chainman.m_blockman .ReadRawBlockFromDisk (block_data, pos)) {
317320 return RESTERR (req, HTTP_NOT_FOUND, hashStr + " not found" );
318321 }
319322
320323 switch (rf) {
321324 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 ()};
325326 req->WriteHeader (" Content-Type" , " application/octet-stream" );
326327 req->WriteReply (HTTP_OK, binaryBlock);
327328 return true ;
328329 }
329330
330331 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 " };
334333 req->WriteHeader (" Content-Type" , " text/plain" );
335334 req->WriteReply (HTTP_OK, strHex);
336335 return true ;
337336 }
338337
339338 case RESTResponseFormat::JSON: {
339+ CBlock block{};
340+ DataStream block_stream{block_data};
341+ block_stream >> TX_WITH_WITNESS (block);
340342 UniValue objBlock = blockToJSON (chainman.m_blockman , block, *tip, *pblockindex, tx_verbosity);
341343 std::string strJSON = objBlock.write () + " \n " ;
342344 req->WriteHeader (" Content-Type" , " application/json" );
0 commit comments