Skip to content

Commit

Permalink
feat: add optional argument to ignore ChainLocks for reconsiderblocks…
Browse files Browse the repository at this point in the history
… RPC

Co-Authored-By: UdjinM6 <[email protected]>
  • Loading branch information
knst and UdjinM6 committed Dec 23, 2024
1 parent ad7a373 commit 64817da
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2125,6 +2125,7 @@ static RPCHelpMan reconsiderblock()
"This can be used to undo the effects of invalidateblock.\n",
{
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "the hash of the block to reconsider"},
{"ignore_chainlocks", RPCArg::Type::BOOL, RPCArg::Default{false}, "if true, existing chainlocks will be ignored"},
},
RPCResult{RPCResult::Type::NONE, "", ""},
RPCExamples{
Expand All @@ -2137,6 +2138,7 @@ static RPCHelpMan reconsiderblock()
CChainState& active_chainstate = chainman.ActiveChainstate();

uint256 hash(ParseHashV(request.params[0], "blockhash"));
const bool ignore_chainlocks{request.params[1].isNull() ? false : request.params[1].get_bool()};

{
LOCK(cs_main);
Expand All @@ -2145,7 +2147,7 @@ static RPCHelpMan reconsiderblock()
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
}

active_chainstate.ResetBlockFailureFlags(pblockindex);
active_chainstate.ResetBlockFailureFlags(pblockindex, ignore_chainlocks);
}

BlockValidationState state;
Expand Down
1 change: 1 addition & 0 deletions src/rpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
{ "waitforblockheight", 0, "height" },
{ "waitforblockheight", 1, "timeout" },
{ "waitforblock", 1, "timeout" },
{ "reconsiderblock", 1, "ignore_chainlocks" },
{ "waitfornewblock", 0, "timeout" },
{ "listtransactions", 1, "count" },
{ "listtransactions", 2, "skip" },
Expand Down
22 changes: 16 additions & 6 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3410,7 +3410,7 @@ bool CChainState::MarkConflictingBlock(BlockValidationState& state, CBlockIndex
return true;
}

void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex, bool ignore_chainlocks) {
AssertLockHeld(cs_main);

if (!pindex) {
Expand All @@ -3429,9 +3429,14 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {
for (auto& [_, block_index] : m_blockman.m_block_index) {
if (!block_index.IsValid() && block_index.GetAncestor(nHeight) == pindex) {
block_index.nStatus &= ~BLOCK_FAILED_MASK;
if (ignore_chainlocks) {
block_index.nStatus &= ~BLOCK_CONFLICT_CHAINLOCK;
}
m_blockman.m_dirty_blockindex.insert(&block_index);
if (block_index.IsValid(BLOCK_VALID_TRANSACTIONS) && !(block_index.nStatus & BLOCK_CONFLICT_CHAINLOCK) && block_index.HaveTxsDownloaded() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), &block_index)) {
setBlockIndexCandidates.insert(&block_index);
if (block_index.IsValid(BLOCK_VALID_TRANSACTIONS) && block_index.HaveTxsDownloaded() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), &block_index)) {
if (ignore_chainlocks || !(block_index.nStatus & BLOCK_CONFLICT_CHAINLOCK)) {
setBlockIndexCandidates.insert(&block_index);
}
}
if (&block_index == m_chainman.m_best_invalid) {
// Reset invalid block marker if it was pointing to one of those.
Expand All @@ -3443,11 +3448,16 @@ void CChainState::ResetBlockFailureFlags(CBlockIndex *pindex) {

// Remove the invalidity flag from all ancestors too.
while (pindex != nullptr) {
if (pindex->nStatus & BLOCK_FAILED_MASK) {
if (pindex->nStatus & (BLOCK_FAILED_MASK | BLOCK_CONFLICT_CHAINLOCK)) {
pindex->nStatus &= ~BLOCK_FAILED_MASK;
if (ignore_chainlocks) {
pindex->nStatus &= ~BLOCK_CONFLICT_CHAINLOCK;
}
m_blockman.m_dirty_blockindex.insert(pindex);
if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && !(pindex->nStatus & BLOCK_CONFLICT_CHAINLOCK) && pindex->HaveTxsDownloaded() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), pindex)) {
setBlockIndexCandidates.insert(pindex);
if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && pindex->HaveTxsDownloaded() && setBlockIndexCandidates.value_comp()(m_chain.Tip(), pindex)) {
if (ignore_chainlocks || !(pindex->nStatus & BLOCK_CONFLICT_CHAINLOCK)) {
setBlockIndexCandidates.insert(pindex);
}
}
if (pindex == m_chainman.m_best_invalid) {
// Reset invalid block marker if it was pointing to one of those.
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ class CChainState
LOCKS_EXCLUDED(::cs_main);

/** Remove invalidity status from a block and its descendants. */
void ResetBlockFailureFlags(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
void ResetBlockFailureFlags(CBlockIndex* pindex, bool ignore_chainlocks = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);

/** Replay blocks that aren't fully applied to the database. */
bool ReplayBlocks();
Expand Down

0 comments on commit 64817da

Please sign in to comment.