mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-19 05:45:05 +01:00
RPC: Extract InvalidateBlock helper
This commit is contained in:
parent
5abb9b1af4
commit
446ce51c21
@ -1577,6 +1577,27 @@ static RPCHelpMan preciousblock()
|
||||
};
|
||||
}
|
||||
|
||||
void InvalidateBlock(ChainstateManager& chainman, const uint256 block_hash) {
|
||||
BlockValidationState state;
|
||||
CBlockIndex* pblockindex;
|
||||
{
|
||||
LOCK(chainman.GetMutex());
|
||||
pblockindex = chainman.m_blockman.LookupBlockIndex(block_hash);
|
||||
if (!pblockindex) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
|
||||
}
|
||||
}
|
||||
chainman.ActiveChainstate().InvalidateBlock(state, pblockindex);
|
||||
|
||||
if (state.IsValid()) {
|
||||
chainman.ActiveChainstate().ActivateBestChain(state);
|
||||
}
|
||||
|
||||
if (!state.IsValid()) {
|
||||
throw JSONRPCError(RPC_DATABASE_ERROR, state.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
static RPCHelpMan invalidateblock()
|
||||
{
|
||||
return RPCHelpMan{"invalidateblock",
|
||||
@ -1591,27 +1612,10 @@ static RPCHelpMan invalidateblock()
|
||||
},
|
||||
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
|
||||
{
|
||||
uint256 hash(ParseHashV(request.params[0], "blockhash"));
|
||||
BlockValidationState state;
|
||||
|
||||
ChainstateManager& chainman = EnsureAnyChainman(request.context);
|
||||
CBlockIndex* pblockindex;
|
||||
{
|
||||
LOCK(cs_main);
|
||||
pblockindex = chainman.m_blockman.LookupBlockIndex(hash);
|
||||
if (!pblockindex) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
|
||||
}
|
||||
}
|
||||
chainman.ActiveChainstate().InvalidateBlock(state, pblockindex);
|
||||
uint256 hash(ParseHashV(request.params[0], "blockhash"));
|
||||
|
||||
if (state.IsValid()) {
|
||||
chainman.ActiveChainstate().ActivateBestChain(state);
|
||||
}
|
||||
|
||||
if (!state.IsValid()) {
|
||||
throw JSONRPCError(RPC_DATABASE_ERROR, state.ToString());
|
||||
}
|
||||
InvalidateBlock(chainman, hash);
|
||||
|
||||
return UniValue::VNULL;
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user