From 446ce51c21cd2466cb12fa0166fd069d42b603bf Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Sat, 2 Mar 2024 16:37:00 +0100 Subject: [PATCH] RPC: Extract InvalidateBlock helper --- src/rpc/blockchain.cpp | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index b449444aff4..4da26e84bde 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -1577,28 +1577,12 @@ static RPCHelpMan preciousblock() }; } -static RPCHelpMan invalidateblock() -{ - return RPCHelpMan{"invalidateblock", - "\nPermanently marks a block as invalid, as if it violated a consensus rule.\n", - { - {"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hash of the block to mark as invalid"}, - }, - RPCResult{RPCResult::Type::NONE, "", ""}, - RPCExamples{ - HelpExampleCli("invalidateblock", "\"blockhash\"") - + HelpExampleRpc("invalidateblock", "\"blockhash\"") - }, - [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue -{ - uint256 hash(ParseHashV(request.params[0], "blockhash")); +void InvalidateBlock(ChainstateManager& chainman, const uint256 block_hash) { BlockValidationState state; - - ChainstateManager& chainman = EnsureAnyChainman(request.context); CBlockIndex* pblockindex; { - LOCK(cs_main); - pblockindex = chainman.m_blockman.LookupBlockIndex(hash); + LOCK(chainman.GetMutex()); + pblockindex = chainman.m_blockman.LookupBlockIndex(block_hash); if (!pblockindex) { throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found"); } @@ -1612,6 +1596,26 @@ static RPCHelpMan invalidateblock() if (!state.IsValid()) { throw JSONRPCError(RPC_DATABASE_ERROR, state.ToString()); } +} + +static RPCHelpMan invalidateblock() +{ + return RPCHelpMan{"invalidateblock", + "\nPermanently marks a block as invalid, as if it violated a consensus rule.\n", + { + {"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hash of the block to mark as invalid"}, + }, + RPCResult{RPCResult::Type::NONE, "", ""}, + RPCExamples{ + HelpExampleCli("invalidateblock", "\"blockhash\"") + + HelpExampleRpc("invalidateblock", "\"blockhash\"") + }, + [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue +{ + ChainstateManager& chainman = EnsureAnyChainman(request.context); + uint256 hash(ParseHashV(request.params[0], "blockhash")); + + InvalidateBlock(chainman, hash); return UniValue::VNULL; },