|
17 | 17 | #include <core_io.h>
|
18 | 18 | #include <deploymentinfo.h>
|
19 | 19 | #include <deploymentstatus.h>
|
| 20 | +#include <flatfile.h> |
20 | 21 | #include <hash.h>
|
21 | 22 | #include <index/blockfilterindex.h>
|
22 | 23 | #include <index/coinstatsindex.h>
|
@@ -595,6 +596,28 @@ static CBlock GetBlockChecked(BlockManager& blockman, const CBlockIndex& blockin
|
595 | 596 | return block;
|
596 | 597 | }
|
597 | 598 |
|
| 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 | + |
598 | 621 | static CBlockUndo GetUndoChecked(BlockManager& blockman, const CBlockIndex& blockindex)
|
599 | 622 | {
|
600 | 623 | CBlockUndo blockUndo;
|
@@ -735,15 +758,16 @@ static RPCHelpMan getblock()
|
735 | 758 | }
|
736 | 759 | }
|
737 | 760 |
|
738 |
| - const CBlock block{GetBlockChecked(chainman.m_blockman, *pblockindex)}; |
| 761 | + const std::vector<uint8_t> block_data{GetRawBlockChecked(chainman.m_blockman, *pblockindex)}; |
739 | 762 |
|
740 | 763 | 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); |
745 | 765 | }
|
746 | 766 |
|
| 767 | + DataStream block_stream{block_data}; |
| 768 | + CBlock block{}; |
| 769 | + block_stream >> TX_WITH_WITNESS(block); |
| 770 | + |
747 | 771 | TxVerbosity tx_verbosity;
|
748 | 772 | if (verbosity == 1) {
|
749 | 773 | tx_verbosity = TxVerbosity::SHOW_TXID;
|
|
0 commit comments