mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-06-13 10:21:34 +02:00
rest: read raw block in rest_block and deserialize for json
Note that for speed this commit also removes the proof of work and signet signature checks before returning the block in getblock. It is assumed if a block is stored it will be valid.
This commit is contained in:
parent
95ce0783a6
commit
e710cefd57
20
src/rest.cpp
20
src/rest.cpp
@ -13,6 +13,7 @@
|
|||||||
#include <chain.h>
|
#include <chain.h>
|
||||||
#include <chainparams.h>
|
#include <chainparams.h>
|
||||||
#include <core_io.h>
|
#include <core_io.h>
|
||||||
|
#include <flatfile.h>
|
||||||
#include <httpserver.h>
|
#include <httpserver.h>
|
||||||
#include <index/blockfilterindex.h>
|
#include <index/blockfilterindex.h>
|
||||||
#include <index/txindex.h>
|
#include <index/txindex.h>
|
||||||
@ -34,7 +35,7 @@
|
|||||||
#include <validation.h>
|
#include <validation.h>
|
||||||
|
|
||||||
#include <any>
|
#include <any>
|
||||||
#include <string>
|
#include <vector>
|
||||||
|
|
||||||
#include <univalue.h>
|
#include <univalue.h>
|
||||||
|
|
||||||
@ -295,7 +296,7 @@ static bool rest_block(const std::any& context,
|
|||||||
if (!ParseHashStr(hashStr, hash))
|
if (!ParseHashStr(hashStr, hash))
|
||||||
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
|
return RESTERR(req, HTTP_BAD_REQUEST, "Invalid hash: " + hashStr);
|
||||||
|
|
||||||
CBlock block;
|
FlatFilePos pos{};
|
||||||
const CBlockIndex* pblockindex = nullptr;
|
const CBlockIndex* pblockindex = nullptr;
|
||||||
const CBlockIndex* tip = nullptr;
|
const CBlockIndex* tip = nullptr;
|
||||||
ChainstateManager* maybe_chainman = GetChainman(context, req);
|
ChainstateManager* maybe_chainman = GetChainman(context, req);
|
||||||
@ -311,32 +312,33 @@ static bool rest_block(const std::any& context,
|
|||||||
if (chainman.m_blockman.IsBlockPruned(*pblockindex)) {
|
if (chainman.m_blockman.IsBlockPruned(*pblockindex)) {
|
||||||
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
|
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not available (pruned data)");
|
||||||
}
|
}
|
||||||
|
pos = pblockindex->GetBlockPos();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!chainman.m_blockman.ReadBlockFromDisk(block, *pblockindex)) {
|
std::vector<uint8_t> block_data{};
|
||||||
|
if (!chainman.m_blockman.ReadRawBlockFromDisk(block_data, pos)) {
|
||||||
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
|
return RESTERR(req, HTTP_NOT_FOUND, hashStr + " not found");
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (rf) {
|
switch (rf) {
|
||||||
case RESTResponseFormat::BINARY: {
|
case RESTResponseFormat::BINARY: {
|
||||||
DataStream ssBlock;
|
const std::string binaryBlock{block_data.begin(), block_data.end()};
|
||||||
ssBlock << TX_WITH_WITNESS(block);
|
|
||||||
std::string binaryBlock = ssBlock.str();
|
|
||||||
req->WriteHeader("Content-Type", "application/octet-stream");
|
req->WriteHeader("Content-Type", "application/octet-stream");
|
||||||
req->WriteReply(HTTP_OK, binaryBlock);
|
req->WriteReply(HTTP_OK, binaryBlock);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
case RESTResponseFormat::HEX: {
|
case RESTResponseFormat::HEX: {
|
||||||
DataStream ssBlock;
|
const std::string strHex{HexStr(block_data) + "\n"};
|
||||||
ssBlock << TX_WITH_WITNESS(block);
|
|
||||||
std::string strHex = HexStr(ssBlock) + "\n";
|
|
||||||
req->WriteHeader("Content-Type", "text/plain");
|
req->WriteHeader("Content-Type", "text/plain");
|
||||||
req->WriteReply(HTTP_OK, strHex);
|
req->WriteReply(HTTP_OK, strHex);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
case RESTResponseFormat::JSON: {
|
case RESTResponseFormat::JSON: {
|
||||||
|
CBlock block{};
|
||||||
|
DataStream block_stream{block_data};
|
||||||
|
block_stream >> TX_WITH_WITNESS(block);
|
||||||
UniValue objBlock = blockToJSON(chainman.m_blockman, block, *tip, *pblockindex, tx_verbosity);
|
UniValue objBlock = blockToJSON(chainman.m_blockman, block, *tip, *pblockindex, tx_verbosity);
|
||||||
std::string strJSON = objBlock.write() + "\n";
|
std::string strJSON = objBlock.write() + "\n";
|
||||||
req->WriteHeader("Content-Type", "application/json");
|
req->WriteHeader("Content-Type", "application/json");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user