Skip to content

Commit 599f260

Browse files
committed
Faster getblock API
1 parent 100949a commit 599f260

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/rpc/blockchain.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -571,14 +571,18 @@ static RPCHelpMan getblockheader()
571571
};
572572
}
573573

574-
static CBlock GetBlockChecked(BlockManager& blockman, const CBlockIndex* pblockindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
574+
static void EnsureBlockNotPruned(BlockManager& blockman, const CBlockIndex* pblockindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
575575
{
576576
AssertLockHeld(::cs_main);
577-
CBlock block;
577+
578578
if (blockman.IsBlockPruned(pblockindex)) {
579579
throw JSONRPCError(RPC_MISC_ERROR, "Block not available (pruned data)");
580580
}
581+
}
581582

583+
static CBlock GetBlockChecked(const CBlockIndex* pblockindex)
584+
{
585+
CBlock block;
582586
if (!ReadBlockFromDisk(block, pblockindex, Params().GetConsensus())) {
583587
// Block not found on disk. This could be because we have the block
584588
// header in our index but not yet have the block or did not accept the
@@ -710,6 +714,7 @@ static RPCHelpMan getblock()
710714
}
711715

712716
CBlock block;
717+
bool havePruned;
713718
const CBlockIndex* pblockindex;
714719
const CBlockIndex* tip;
715720
ChainstateManager& chainman = EnsureAnyChainman(request.context);
@@ -722,7 +727,15 @@ static RPCHelpMan getblock()
722727
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
723728
}
724729

725-
block = GetBlockChecked(chainman.m_blockman, pblockindex);
730+
havePruned = chainman.m_blockman.m_have_pruned;
731+
if(havePruned) {
732+
EnsureBlockNotPruned(chainman.m_blockman, pblockindex);
733+
block = GetBlockChecked(pblockindex);
734+
}
735+
}
736+
737+
if(!havePruned) {
738+
block = GetBlockChecked(pblockindex);
726739
}
727740

728741
if (verbosity <= 0)
@@ -1795,7 +1808,8 @@ static RPCHelpMan getblockstats()
17951808
}
17961809
}
17971810

1798-
const CBlock& block = GetBlockChecked(chainman.m_blockman, &pindex);
1811+
EnsureBlockNotPruned(chainman.m_blockman, &pindex);
1812+
const CBlock& block = GetBlockChecked(&pindex);
17991813
const CBlockUndo& blockUndo = GetUndoChecked(chainman.m_blockman, &pindex);
18001814

18011815
const bool do_all = stats.size() == 0; // Calculate everything if nothing selected (default)

0 commit comments

Comments
 (0)