refactor: more llmq refactoring (#4552)

* replace raw owning ptr with unique ptr

Signed-off-by: pasta <pasta@dashboost.org>

* Add GUARDED_BY annotation to llmq_versionbitscache

Signed-off-by: pasta <pasta@dashboost.org>

* limit scope of locking cs_llmq_vbc

Signed-off-by: pasta <pasta@dashboost.org>

* use llmq_versionbitscache instead of versionbitscache in UpdatedBlockTip to avoid cs_main locking

Signed-off-by: pasta <pasta@dashboost.org>

* drop unneeded cs_main ::mempool.cs

* lock cs_main and mempool.cs in Db::Upgrade
This commit is contained in:
PastaPastaPasta 2021-10-28 15:11:34 -04:00 committed by GitHub
parent a0b68ca856
commit 5781bd5ee3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 10 additions and 18 deletions

View File

@ -37,17 +37,15 @@ std::string CChainLockSig::ToString() const
CChainLocksHandler::CChainLocksHandler() CChainLocksHandler::CChainLocksHandler()
{ {
scheduler = new CScheduler(); scheduler = std::make_unique<CScheduler>();
CScheduler::Function serviceLoop = std::bind(&CScheduler::serviceQueue, scheduler); CScheduler::Function serviceLoop = std::bind(&CScheduler::serviceQueue, scheduler.get());
scheduler_thread = new std::thread(std::bind(&TraceThread<CScheduler::Function>, "cl-schdlr", serviceLoop)); scheduler_thread = std::make_unique<std::thread>(std::bind(&TraceThread<CScheduler::Function>, "cl-schdlr", serviceLoop));
} }
CChainLocksHandler::~CChainLocksHandler() CChainLocksHandler::~CChainLocksHandler()
{ {
scheduler->stop(); scheduler->stop();
scheduler_thread->join(); scheduler_thread->join();
delete scheduler_thread;
delete scheduler;
} }
void CChainLocksHandler::Start() void CChainLocksHandler::Start()

View File

@ -52,8 +52,8 @@ class CChainLocksHandler : public CRecoveredSigsListener
static constexpr int64_t WAIT_FOR_ISLOCK_TIMEOUT = 10 * 60; static constexpr int64_t WAIT_FOR_ISLOCK_TIMEOUT = 10 * 60;
private: private:
CScheduler* scheduler; std::unique_ptr<CScheduler> scheduler;
std::thread* scheduler_thread; std::unique_ptr<std::thread> scheduler_thread;
mutable CCriticalSection cs; mutable CCriticalSection cs;
std::atomic<bool> tryLockChainTipScheduled{false}; std::atomic<bool> tryLockChainTipScheduled{false};
std::atomic<bool> isEnabled{false}; std::atomic<bool> isEnabled{false};

View File

@ -58,11 +58,9 @@ CInstantSendDb::CInstantSendDb(bool unitTests, bool fWipe)
db = std::make_unique<CDBWrapper>(unitTests ? "" : (GetDataDir() / "llmq/isdb"), 32 << 20, unitTests, fWipe); db = std::make_unique<CDBWrapper>(unitTests ? "" : (GetDataDir() / "llmq/isdb"), 32 << 20, unitTests, fWipe);
} }
void CInstantSendDb::Upgrade() EXCLUSIVE_LOCKS_REQUIRED(cs_main, ::mempool.cs) void CInstantSendDb::Upgrade()
{ {
AssertLockHeld(cs_main); LOCK2(cs_main, ::mempool.cs);
AssertLockHeld(::mempool.cs);
LOCK(cs_db); LOCK(cs_db);
int v{0}; int v{0};
if (!db->Read(DB_VERSION, v) || v < CInstantSendDb::CURRENT_VERSION) { if (!db->Read(DB_VERSION, v) || v < CInstantSendDb::CURRENT_VERSION) {
@ -1270,8 +1268,7 @@ void CInstantSendManager::NotifyChainLock(const CBlockIndex* pindexChainLock)
void CInstantSendManager::UpdatedBlockTip(const CBlockIndex* pindexNew) void CInstantSendManager::UpdatedBlockTip(const CBlockIndex* pindexNew)
{ {
if (!fUpgradedDB) { if (!fUpgradedDB) {
LOCK2(cs_main, ::mempool.cs); // for GetTransaction in Upgrade if (WITH_LOCK(cs_llmq_vbc, return VersionBitsState(pindexNew, Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0020, llmq_versionbitscache) == ThresholdState::ACTIVE)) {
if (VersionBitsState(pindexNew, Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0020, versionbitscache) == ThresholdState::ACTIVE) {
db.Upgrade(); db.Upgrade();
fUpgradedDB = true; fUpgradedDB = true;
} }

View File

@ -301,10 +301,7 @@ bool CLLMQUtils::IsQuorumActive(Consensus::LLMQType llmqType, const uint256& quo
bool CLLMQUtils::IsQuorumTypeEnabled(Consensus::LLMQType llmqType, const CBlockIndex* pindex) bool CLLMQUtils::IsQuorumTypeEnabled(Consensus::LLMQType llmqType, const CBlockIndex* pindex)
{ {
LOCK(cs_llmq_vbc);
const Consensus::Params& consensusParams = Params().GetConsensus(); const Consensus::Params& consensusParams = Params().GetConsensus();
bool f_dip0020_Active = VersionBitsState(pindex, consensusParams, Consensus::DEPLOYMENT_DIP0020, llmq_versionbitscache) == ThresholdState::ACTIVE;
switch (llmqType) switch (llmqType)
{ {
@ -314,7 +311,7 @@ bool CLLMQUtils::IsQuorumTypeEnabled(Consensus::LLMQType llmqType, const CBlockI
break; break;
case Consensus::LLMQType::LLMQ_100_67: case Consensus::LLMQType::LLMQ_100_67:
case Consensus::LLMQType::LLMQ_TEST_V17: case Consensus::LLMQType::LLMQ_TEST_V17:
if (!f_dip0020_Active) { if (LOCK(cs_llmq_vbc); VersionBitsState(pindex, consensusParams, Consensus::DEPLOYMENT_DIP0020, llmq_versionbitscache) != ThresholdState::ACTIVE) {
return false; return false;
} }
break; break;

View File

@ -25,7 +25,7 @@ namespace llmq
// Use a separate cache instance instead of versionbitscache to avoid locking cs_main // Use a separate cache instance instead of versionbitscache to avoid locking cs_main
// and dealing with all kinds of deadlocks. // and dealing with all kinds of deadlocks.
extern CCriticalSection cs_llmq_vbc; extern CCriticalSection cs_llmq_vbc;
extern VersionBitsCache llmq_versionbitscache; extern VersionBitsCache llmq_versionbitscache GUARDED_BY(cs_llmq_vbc);
static const bool DEFAULT_ENABLE_QUORUM_DATA_RECOVERY = true; static const bool DEFAULT_ENABLE_QUORUM_DATA_RECOVERY = true;