Skip to content

Commit

Permalink
Adds std::atomic<bool> g_reorg_in_progress
Browse files Browse the repository at this point in the history
This implements an atomic boolean that is used by the core to
indicate a reorganization is in progress. This is in turn used
by modules such as poll tallies, which use minimal locking, to
determine when core state has changed and take actions accordingly.
  • Loading branch information
jamescowens committed Jan 26, 2023
1 parent 4651af0 commit fda492b
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 126 deletions.
17 changes: 4 additions & 13 deletions src/gridcoin/voting/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,8 +954,6 @@ void PollRegistry::Add(const ContractContext& ctx) EXCLUSIVE_LOCKS_REQUIRED(cs_m
} else {
AddPoll(ctx);
}

DetectReorg();
}

void PollRegistry::Delete(const ContractContext& ctx) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Expand Down Expand Up @@ -1075,32 +1073,25 @@ void PollRegistry::DeleteVote(const ContractContext& ctx) EXCLUSIVE_LOCKS_REQUIR
}

void PollRegistry::DetectReorg()
EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
// Reorg detector
// Note that doing the reorg detection here, in the contract handler, means that we only flag a reorg IF
// a transaction happened to occur that involves a poll or vote contract in the scope of the reorg, because
// these handlers are only triggered by those two contract types.
LogPrint(BCLog::LogFlags::VOTE, "INFO: %s: registry_traversal_in_progress = %u, reorg_occurred_during_reg_traversal = %u, "
"m_block_height_hw = %i, nBestHeight = %i",
"nBestHeight = %i",
__func__,
registry_traversal_in_progress,
reorg_occurred_during_reg_traversal,
m_block_height_hw,
nBestHeight);
reorg_occurred_during_reg_traversal
);

if (registry_traversal_in_progress && nBestHeight < m_block_height_hw) {
if (registry_traversal_in_progress && g_reorg_in_progress) {
reorg_occurred_during_reg_traversal = true;
LogPrint(BCLog::LogFlags::VOTE, "INFO: %s: Setting reorg_occurred_during_reg_traversal to true.", __func__);
} else {
reorg_occurred_during_reg_traversal = false;
LogPrint(BCLog::LogFlags::VOTE, "INFO: %s: Setting reorg_occurred_during_reg_traversal to false.", __func__);
}

if (nBestHeight > m_block_height_hw) {
m_block_height_hw = nBestHeight;
LogPrint(BCLog::LogFlags::VOTE, "INFO: %s: Setting m_block_height_hw to nBestHeight", __func__);
}
}
// -----------------------------------------------------------------------------
// Class: PollRegistry::Sequence
Expand Down
1 change: 0 additions & 1 deletion src/gridcoin/voting/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,6 @@ class PollRegistry : public IContractHandler
//!
void DetectReorg();

std::atomic<int> m_block_height_hw = 0; //!< High water block height from contracts.
std::atomic<bool> registry_traversal_in_progress = false; //!< Boolean that registry traversal is in progress.
std::atomic<bool> reorg_occurred_during_reg_traversal = false; //!< Boolean to indicate whether a reorg occurred.

Expand Down
Loading

0 comments on commit fda492b

Please sign in to comment.