Skip to content

Commit 446ce51

Browse files
committed
RPC: Extract InvalidateBlock helper
1 parent 5abb9b1 commit 446ce51

File tree

1 file changed

+23
-19
lines changed

1 file changed

+23
-19
lines changed

src/rpc/blockchain.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,28 +1577,12 @@ static RPCHelpMan preciousblock()
15771577
};
15781578
}
15791579

1580-
static RPCHelpMan invalidateblock()
1581-
{
1582-
return RPCHelpMan{"invalidateblock",
1583-
"\nPermanently marks a block as invalid, as if it violated a consensus rule.\n",
1584-
{
1585-
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hash of the block to mark as invalid"},
1586-
},
1587-
RPCResult{RPCResult::Type::NONE, "", ""},
1588-
RPCExamples{
1589-
HelpExampleCli("invalidateblock", "\"blockhash\"")
1590-
+ HelpExampleRpc("invalidateblock", "\"blockhash\"")
1591-
},
1592-
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
1593-
{
1594-
uint256 hash(ParseHashV(request.params[0], "blockhash"));
1580+
void InvalidateBlock(ChainstateManager& chainman, const uint256 block_hash) {
15951581
BlockValidationState state;
1596-
1597-
ChainstateManager& chainman = EnsureAnyChainman(request.context);
15981582
CBlockIndex* pblockindex;
15991583
{
1600-
LOCK(cs_main);
1601-
pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
1584+
LOCK(chainman.GetMutex());
1585+
pblockindex = chainman.m_blockman.LookupBlockIndex(block_hash);
16021586
if (!pblockindex) {
16031587
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
16041588
}
@@ -1612,6 +1596,26 @@ static RPCHelpMan invalidateblock()
16121596
if (!state.IsValid()) {
16131597
throw JSONRPCError(RPC_DATABASE_ERROR, state.ToString());
16141598
}
1599+
}
1600+
1601+
static RPCHelpMan invalidateblock()
1602+
{
1603+
return RPCHelpMan{"invalidateblock",
1604+
"\nPermanently marks a block as invalid, as if it violated a consensus rule.\n",
1605+
{
1606+
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hash of the block to mark as invalid"},
1607+
},
1608+
RPCResult{RPCResult::Type::NONE, "", ""},
1609+
RPCExamples{
1610+
HelpExampleCli("invalidateblock", "\"blockhash\"")
1611+
+ HelpExampleRpc("invalidateblock", "\"blockhash\"")
1612+
},
1613+
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
1614+
{
1615+
ChainstateManager& chainman = EnsureAnyChainman(request.context);
1616+
uint256 hash(ParseHashV(request.params[0], "blockhash"));
1617+
1618+
InvalidateBlock(chainman, hash);
16151619

16161620
return UniValue::VNULL;
16171621
},

0 commit comments

Comments
 (0)