Skip to content

Commit 95ce078

Browse files
committed
rpc: read raw block in getblock and deserialize for verbosity > 0
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.
1 parent 0865ab8 commit 95ce078

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/rpc/blockchain.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <core_io.h>
1818
#include <deploymentinfo.h>
1919
#include <deploymentstatus.h>
20+
#include <flatfile.h>
2021
#include <hash.h>
2122
#include <index/blockfilterindex.h>
2223
#include <index/coinstatsindex.h>
@@ -595,6 +596,28 @@ static CBlock GetBlockChecked(BlockManager& blockman, const CBlockIndex& blockin
595596
return block;
596597
}
597598

599+
static std::vector<uint8_t> GetRawBlockChecked(BlockManager& blockman, const CBlockIndex& blockindex)
600+
{
601+
std::vector<uint8_t> data{};
602+
FlatFilePos pos{};
603+
{
604+
LOCK(cs_main);
605+
if (blockman.IsBlockPruned(blockindex)) {
606+
throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)");
607+
}
608+
pos = blockindex.GetBlockPos();
609+
}
610+
611+
if (!blockman.ReadRawBlockFromDisk(data, pos)) {
612+
// Block not found on disk. This could be because we have the block
613+
// header in our index but not yet have the block or did not accept the
614+
// block. Or if the block was pruned right after we released the lock above.
615+
throw JSONRPCError(RPC_MISC_ERROR, "Block not found on disk");
616+
}
617+
618+
return data;
619+
}
620+
598621
static CBlockUndo GetUndoChecked(BlockManager& blockman, const CBlockIndex& blockindex)
599622
{
600623
CBlockUndo blockUndo;
@@ -735,15 +758,16 @@ static RPCHelpMan getblock()
735758
}
736759
}
737760

738-
const CBlock block{GetBlockChecked(chainman.m_blockman, *pblockindex)};
761+
const std::vector<uint8_t> block_data{GetRawBlockChecked(chainman.m_blockman, *pblockindex)};
739762

740763
if (verbosity <= 0) {
741-
DataStream ssBlock;
742-
ssBlock << TX_WITH_WITNESS(block);
743-
std::string strHex = HexStr(ssBlock);
744-
return strHex;
764+
return HexStr(block_data);
745765
}
746766

767+
DataStream block_stream{block_data};
768+
CBlock block{};
769+
block_stream >> TX_WITH_WITNESS(block);
770+
747771
TxVerbosity tx_verbosity;
748772
if (verbosity == 1) {
749773
tx_verbosity = TxVerbosity::SHOW_TXID;

0 commit comments

Comments
 (0)