refactor: More constness and thread safety for LLMQ modules (#4392)

* refactor: Add `const` qualifier to various llmq related functions

* refactor: Add thread safety annotations to various llmq related functions

And around them where it's required
This commit is contained in:
UdjinM6 2021-09-08 01:33:02 +03:00 committed by GitHub
parent 7bd7b9b38a
commit 2723fd13a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
20 changed files with 132 additions and 106 deletions

View File

@ -91,6 +91,8 @@ bool UndoSpecialTx(const CTransaction& tx, const CBlockIndex* pindex)
bool ProcessSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, CValidationState& state, const CCoinsViewCache& view, bool fJustCheck, bool fCheckCbTxMerleRoots)
{
AssertLockHeld(cs_main);
try {
static int64_t nTimeLoop = 0;
static int64_t nTimeQuorum = 0;
@ -146,6 +148,8 @@ bool ProcessSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, CV
bool UndoSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex)
{
AssertLockHeld(cs_main);
try {
for (int i = (int)block.vtx.size() - 1; i >= 0; --i) {
const CTransaction& tx = *block.vtx[i];

View File

@ -6,6 +6,8 @@
#define BITCOIN_EVO_SPECIALTX_H
#include <streams.h>
#include <sync.h>
#include <threadsafety.h>
#include <version.h>
class CBlock;
@ -13,9 +15,11 @@ class CBlockIndex;
class CCoinsViewCache;
class CValidationState;
extern CCriticalSection cs_main;
bool CheckSpecialTx(const CTransaction& tx, const CBlockIndex* pindexPrev, CValidationState& state, const CCoinsViewCache& view);
bool ProcessSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, CValidationState& state, const CCoinsViewCache& view, bool fJustCheck, bool fCheckCbTxMerleRoots);
bool UndoSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex);
bool ProcessSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex, CValidationState& state, const CCoinsViewCache& view, bool fJustCheck, bool fCheckCbTxMerleRoots) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool UndoSpecialTxsInBlock(const CBlock& block, const CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
template <typename T>
inline bool GetTxPayload(const std::vector<unsigned char>& payload, T& obj)

View File

@ -237,7 +237,7 @@ private:
// all private methods here are cs_main-free
void EnsureQuorumConnections(Consensus::LLMQType llmqType, const CBlockIndex *pindexNew) const;
CQuorumPtr BuildQuorumFromCommitment(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum) const;
CQuorumPtr BuildQuorumFromCommitment(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum) const EXCLUSIVE_LOCKS_REQUIRED(quorumsCacheCs);
bool BuildQuorumContributions(const CFinalCommitmentPtr& fqc, const std::shared_ptr<CQuorum>& quorum) const;
CQuorumCPtr GetQuorum(Consensus::LLMQType llmqType, const CBlockIndex* pindex) const;

View File

@ -187,6 +187,8 @@ static std::tuple<std::string, Consensus::LLMQType, uint32_t> BuildInversedHeigh
bool CQuorumBlockProcessor::ProcessCommitment(int nHeight, const uint256& blockHash, const CFinalCommitment& qc, CValidationState& state, bool fJustCheck)
{
AssertLockHeld(cs_main);
const auto& llmq_params = GetLLMQParams(qc.llmqType);
uint256 quorumHash = GetQuorumBlockHash(qc.llmqType, nHeight);
@ -375,8 +377,10 @@ bool CQuorumBlockProcessor::IsMiningPhase(Consensus::LLMQType llmqType, int nHei
return false;
}
bool CQuorumBlockProcessor::IsCommitmentRequired(Consensus::LLMQType llmqType, int nHeight)
bool CQuorumBlockProcessor::IsCommitmentRequired(Consensus::LLMQType llmqType, int nHeight) const
{
AssertLockHeld(cs_main);
uint256 quorumHash = GetQuorumBlockHash(llmqType, nHeight);
// perform extra check for quorumHash.IsNull as the quorum hash is unknown for the first block of a session
@ -402,7 +406,7 @@ uint256 CQuorumBlockProcessor::GetQuorumBlockHash(Consensus::LLMQType llmqType,
return quorumBlockHash;
}
bool CQuorumBlockProcessor::HasMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash)
bool CQuorumBlockProcessor::HasMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash) const
{
bool fExists;
{
@ -420,7 +424,7 @@ bool CQuorumBlockProcessor::HasMinedCommitment(Consensus::LLMQType llmqType, con
return fExists;
}
CFinalCommitmentPtr CQuorumBlockProcessor::GetMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash, uint256& retMinedBlockHash)
CFinalCommitmentPtr CQuorumBlockProcessor::GetMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash, uint256& retMinedBlockHash) const
{
auto key = std::make_pair(DB_MINED_COMMITMENT, std::make_pair(llmqType, quorumHash));
std::pair<CFinalCommitment, uint256> p;
@ -432,7 +436,7 @@ CFinalCommitmentPtr CQuorumBlockProcessor::GetMinedCommitment(Consensus::LLMQTyp
}
// The returned quorums are in reversed order, so the most recent one is at index 0
std::vector<const CBlockIndex*> CQuorumBlockProcessor::GetMinedCommitmentsUntilBlock(Consensus::LLMQType llmqType, const CBlockIndex* pindex, size_t maxCount)
std::vector<const CBlockIndex*> CQuorumBlockProcessor::GetMinedCommitmentsUntilBlock(Consensus::LLMQType llmqType, const CBlockIndex* pindex, size_t maxCount) const
{
LOCK(evoDb.cs);
@ -476,7 +480,7 @@ std::vector<const CBlockIndex*> CQuorumBlockProcessor::GetMinedCommitmentsUntilB
}
// The returned quorums are in reversed order, so the most recent one is at index 0
std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> CQuorumBlockProcessor::GetMinedAndActiveCommitmentsUntilBlock(const CBlockIndex* pindex)
std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> CQuorumBlockProcessor::GetMinedAndActiveCommitmentsUntilBlock(const CBlockIndex* pindex) const
{
std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> ret;
@ -490,7 +494,7 @@ std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> CQuorumBlockProce
return ret;
}
bool CQuorumBlockProcessor::HasMineableCommitment(const uint256& hash)
bool CQuorumBlockProcessor::HasMineableCommitment(const uint256& hash) const
{
LOCK(minableCommitmentsCs);
return minableCommitments.count(hash) != 0;
@ -528,7 +532,7 @@ void CQuorumBlockProcessor::AddMineableCommitment(const CFinalCommitment& fqc)
}
}
bool CQuorumBlockProcessor::GetMineableCommitmentByHash(const uint256& commitmentHash, llmq::CFinalCommitment& ret)
bool CQuorumBlockProcessor::GetMineableCommitmentByHash(const uint256& commitmentHash, llmq::CFinalCommitment& ret) const
{
LOCK(minableCommitmentsCs);
auto it = minableCommitments.find(commitmentHash);
@ -541,7 +545,7 @@ bool CQuorumBlockProcessor::GetMineableCommitmentByHash(const uint256& commitmen
// Will return false if no commitment should be mined
// Will return true and a null commitment if no mineable commitment is known and none was mined yet
bool CQuorumBlockProcessor::GetMineableCommitment(Consensus::LLMQType llmqType, int nHeight, CFinalCommitment& ret)
bool CQuorumBlockProcessor::GetMineableCommitment(Consensus::LLMQType llmqType, int nHeight, CFinalCommitment& ret) const
{
AssertLockHeld(cs_main);
@ -570,7 +574,7 @@ bool CQuorumBlockProcessor::GetMineableCommitment(Consensus::LLMQType llmqType,
return true;
}
bool CQuorumBlockProcessor::GetMineableCommitmentTx(Consensus::LLMQType llmqType, int nHeight, CTransactionRef& ret)
bool CQuorumBlockProcessor::GetMineableCommitmentTx(Consensus::LLMQType llmqType, int nHeight, CTransactionRef& ret) const
{
AssertLockHeld(cs_main);

View File

@ -21,6 +21,8 @@ class CConnman;
class CValidationState;
class CEvoDB;
extern CCriticalSection cs_main;
namespace llmq
{
@ -33,11 +35,11 @@ private:
CEvoDB& evoDb;
// TODO cleanup
CCriticalSection minableCommitmentsCs;
mutable CCriticalSection minableCommitmentsCs;
std::map<std::pair<Consensus::LLMQType, uint256>, uint256> minableCommitmentsByQuorum GUARDED_BY(minableCommitmentsCs);
std::map<uint256, CFinalCommitment> minableCommitments GUARDED_BY(minableCommitmentsCs);
std::map<Consensus::LLMQType, unordered_lru_cache<uint256, bool, StaticSaltedHasher>> mapHasMinedCommitmentCache GUARDED_BY(minableCommitmentsCs);
mutable std::map<Consensus::LLMQType, unordered_lru_cache<uint256, bool, StaticSaltedHasher>> mapHasMinedCommitmentCache GUARDED_BY(minableCommitmentsCs);
public:
explicit CQuorumBlockProcessor(CEvoDB& _evoDb);
@ -46,27 +48,27 @@ public:
void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv);
bool ProcessBlock(const CBlock& block, const CBlockIndex* pindex, CValidationState& state, bool fJustCheck);
bool UndoBlock(const CBlock& block, const CBlockIndex* pindex);
bool ProcessBlock(const CBlock& block, const CBlockIndex* pindex, CValidationState& state, bool fJustCheck) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool UndoBlock(const CBlock& block, const CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
void AddMineableCommitment(const CFinalCommitment& fqc);
bool HasMineableCommitment(const uint256& hash);
bool GetMineableCommitmentByHash(const uint256& commitmentHash, CFinalCommitment& ret);
bool GetMineableCommitment(Consensus::LLMQType llmqType, int nHeight, CFinalCommitment& ret);
bool GetMineableCommitmentTx(Consensus::LLMQType llmqType, int nHeight, CTransactionRef& ret);
bool HasMineableCommitment(const uint256& hash) const;
bool GetMineableCommitmentByHash(const uint256& commitmentHash, CFinalCommitment& ret) const;
bool GetMineableCommitment(Consensus::LLMQType llmqType, int nHeight, CFinalCommitment& ret) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool GetMineableCommitmentTx(Consensus::LLMQType llmqType, int nHeight, CTransactionRef& ret) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool HasMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash);
CFinalCommitmentPtr GetMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash, uint256& retMinedBlockHash);
bool HasMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash) const;
CFinalCommitmentPtr GetMinedCommitment(Consensus::LLMQType llmqType, const uint256& quorumHash, uint256& retMinedBlockHash) const;
std::vector<const CBlockIndex*> GetMinedCommitmentsUntilBlock(Consensus::LLMQType llmqType, const CBlockIndex* pindex, size_t maxCount);
std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> GetMinedAndActiveCommitmentsUntilBlock(const CBlockIndex* pindex);
std::vector<const CBlockIndex*> GetMinedCommitmentsUntilBlock(Consensus::LLMQType llmqType, const CBlockIndex* pindex, size_t maxCount) const;
std::map<Consensus::LLMQType, std::vector<const CBlockIndex*>> GetMinedAndActiveCommitmentsUntilBlock(const CBlockIndex* pindex) const;
private:
static bool GetCommitmentsFromBlock(const CBlock& block, const CBlockIndex* pindex, std::map<Consensus::LLMQType, CFinalCommitment>& ret, CValidationState& state);
bool ProcessCommitment(int nHeight, const uint256& blockHash, const CFinalCommitment& qc, CValidationState& state, bool fJustCheck);
static bool GetCommitmentsFromBlock(const CBlock& block, const CBlockIndex* pindex, std::map<Consensus::LLMQType, CFinalCommitment>& ret, CValidationState& state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool ProcessCommitment(int nHeight, const uint256& blockHash, const CFinalCommitment& qc, CValidationState& state, bool fJustCheck) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
static bool IsMiningPhase(Consensus::LLMQType llmqType, int nHeight);
bool IsCommitmentRequired(Consensus::LLMQType llmqType, int nHeight);
static uint256 GetQuorumBlockHash(Consensus::LLMQType llmqType, int nHeight);
bool IsCommitmentRequired(Consensus::LLMQType llmqType, int nHeight) const EXCLUSIVE_LOCKS_REQUIRED(cs_main);
static uint256 GetQuorumBlockHash(Consensus::LLMQType llmqType, int nHeight) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
};
extern CQuorumBlockProcessor* quorumBlockProcessor;

View File

@ -66,13 +66,13 @@ void CChainLocksHandler::Stop()
quorumSigningManager->UnregisterRecoveredSigsListener(this);
}
bool CChainLocksHandler::AlreadyHave(const CInv& inv)
bool CChainLocksHandler::AlreadyHave(const CInv& inv) const
{
LOCK(cs);
return seenChainLocks.count(inv.hash) != 0;
}
bool CChainLocksHandler::GetChainLockByHash(const uint256& hash, llmq::CChainLockSig& ret)
bool CChainLocksHandler::GetChainLockByHash(const uint256& hash, llmq::CChainLockSig& ret) const
{
LOCK(cs);
@ -85,7 +85,7 @@ bool CChainLocksHandler::GetChainLockByHash(const uint256& hash, llmq::CChainLoc
return true;
}
CChainLockSig CChainLocksHandler::GetBestChainLock()
CChainLockSig CChainLocksHandler::GetBestChainLock() const
{
LOCK(cs);
return bestChainLock;
@ -463,7 +463,7 @@ CChainLocksHandler::BlockTxs::mapped_type CChainLocksHandler::GetBlockTxs(const
return ret;
}
bool CChainLocksHandler::IsTxSafeForMining(const uint256& txid)
bool CChainLocksHandler::IsTxSafeForMining(const uint256& txid) const
{
if (!RejectConflictingBlocks()) {
return true;
@ -601,13 +601,13 @@ void CChainLocksHandler::HandleNewRecoveredSig(const llmq::CRecoveredSig& recove
ProcessNewChainLock(-1, clsig, ::SerializeHash(clsig));
}
bool CChainLocksHandler::HasChainLock(int nHeight, const uint256& blockHash)
bool CChainLocksHandler::HasChainLock(int nHeight, const uint256& blockHash) const
{
LOCK(cs);
return InternalHasChainLock(nHeight, blockHash);
}
bool CChainLocksHandler::InternalHasChainLock(int nHeight, const uint256& blockHash)
bool CChainLocksHandler::InternalHasChainLock(int nHeight, const uint256& blockHash) const
{
AssertLockHeld(cs);
@ -631,13 +631,13 @@ bool CChainLocksHandler::InternalHasChainLock(int nHeight, const uint256& blockH
return pAncestor && pAncestor->GetBlockHash() == blockHash;
}
bool CChainLocksHandler::HasConflictingChainLock(int nHeight, const uint256& blockHash)
bool CChainLocksHandler::HasConflictingChainLock(int nHeight, const uint256& blockHash) const
{
LOCK(cs);
return InternalHasConflictingChainLock(nHeight, blockHash);
}
bool CChainLocksHandler::InternalHasConflictingChainLock(int nHeight, const uint256& blockHash)
bool CChainLocksHandler::InternalHasConflictingChainLock(int nHeight, const uint256& blockHash) const
{
AssertLockHeld(cs);

View File

@ -55,7 +55,7 @@ class CChainLocksHandler : public CRecoveredSigsListener
private:
CScheduler* scheduler;
boost::thread* scheduler_thread;
CCriticalSection cs;
mutable CCriticalSection cs;
bool tryLockChainTipScheduled GUARDED_BY(cs) {false};
bool isEnabled GUARDED_BY(cs) {false};
bool isEnforced GUARDED_BY(cs) {false};
@ -87,9 +87,9 @@ public:
void Start();
void Stop();
bool AlreadyHave(const CInv& inv);
bool GetChainLockByHash(const uint256& hash, CChainLockSig& ret);
CChainLockSig GetBestChainLock();
bool AlreadyHave(const CInv& inv) const;
bool GetChainLockByHash(const uint256& hash, CChainLockSig& ret) const;
CChainLockSig GetBestChainLock() const;
void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv);
void ProcessNewChainLock(NodeId from, const CChainLockSig& clsig, const uint256& hash);
@ -103,15 +103,15 @@ public:
void EnforceBestChainLock();
void HandleNewRecoveredSig(const CRecoveredSig& recoveredSig) override;
bool HasChainLock(int nHeight, const uint256& blockHash);
bool HasConflictingChainLock(int nHeight, const uint256& blockHash);
bool HasChainLock(int nHeight, const uint256& blockHash) const;
bool HasConflictingChainLock(int nHeight, const uint256& blockHash) const;
bool IsTxSafeForMining(const uint256& txid);
bool IsTxSafeForMining(const uint256& txid) const;
private:
// these require locks to be held already
bool InternalHasChainLock(int nHeight, const uint256& blockHash);
bool InternalHasConflictingChainLock(int nHeight, const uint256& blockHash);
bool InternalHasChainLock(int nHeight, const uint256& blockHash) const EXCLUSIVE_LOCKS_REQUIRED(cs);
bool InternalHasConflictingChainLock(int nHeight, const uint256& blockHash) const EXCLUSIVE_LOCKS_REQUIRED(cs);
BlockTxs::mapped_type GetBlockTxs(const uint256& blockHash);

View File

@ -132,7 +132,7 @@ UniValue CDKGDebugStatus::ToJson(int detailLevel) const
return ret;
}
void CDKGDebugManager::GetLocalDebugStatus(llmq::CDKGDebugStatus& ret)
void CDKGDebugManager::GetLocalDebugStatus(llmq::CDKGDebugStatus& ret) const
{
LOCK(cs);
ret = localStatus;

View File

@ -89,13 +89,13 @@ public:
class CDKGDebugManager
{
private:
CCriticalSection cs;
mutable CCriticalSection cs;
CDKGDebugStatus localStatus GUARDED_BY(cs);
public:
CDKGDebugManager();
void GetLocalDebugStatus(CDKGDebugStatus& ret);
void GetLocalDebugStatus(CDKGDebugStatus& ret) const;
void ResetLocalSessionStatus(Consensus::LLMQType llmqType);
void InitLocalSessionStatus(Consensus::LLMQType llmqType, const uint256& quorumHash, int quorumHeight);

View File

@ -298,7 +298,7 @@ public:
void SendContributions(CDKGPendingMessages& pendingMessages);
bool PreVerifyMessage(const CDKGContribution& qc, bool& retBan) const;
void ReceiveMessage(const CDKGContribution& qc, bool& retBan);
void VerifyPendingContributions();
void VerifyPendingContributions() EXCLUSIVE_LOCKS_REQUIRED(cs_pending);
// Phase 2: complaint
void VerifyAndComplain(CDKGPendingMessages& pendingMessages);

View File

@ -248,7 +248,7 @@ void CDKGSessionHandler::WaitForNewQuorum(const uint256& oldQuorumHash) const
void CDKGSessionHandler::SleepBeforePhase(QuorumPhase curPhase,
const uint256& expectedQuorumHash,
double randomSleepFactor,
const WhileWaitFunc& runWhileWaiting)
const WhileWaitFunc& runWhileWaiting) const
{
if (!curSession->AreWeMember()) {
// Non-members do not participate and do not create any network load, no need to sleep.

View File

@ -142,7 +142,7 @@ private:
typedef std::function<bool()> WhileWaitFunc;
void WaitForNextPhase(QuorumPhase curPhase, QuorumPhase nextPhase, const uint256& expectedQuorumHash, const WhileWaitFunc& runWhileWaiting) const;
void WaitForNewQuorum(const uint256& oldQuorumHash) const;
void SleepBeforePhase(QuorumPhase curPhase, const uint256& expectedQuorumHash, double randomSleepFactor, const WhileWaitFunc& runWhileWaiting);
void SleepBeforePhase(QuorumPhase curPhase, const uint256& expectedQuorumHash, double randomSleepFactor, const WhileWaitFunc& runWhileWaiting) const;
void HandlePhase(QuorumPhase curPhase, QuorumPhase nextPhase, const uint256& expectedQuorumHash, double randomSleepFactor, const StartPhaseFunc& startPhaseFunc, const WhileWaitFunc& runWhileWaiting);
void HandleDKGRound();
void PhaseHandlerThread();

View File

@ -303,7 +303,7 @@ void CDKGSessionManager::WriteEncryptedContributions(Consensus::LLMQType llmqTyp
db->Write(std::make_tuple(DB_ENC_CONTRIB, llmqType, pindexQuorum->GetBlockHash(), proTxHash), contributions);
}
bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const std::vector<bool>& validMembers, std::vector<uint16_t>& memberIndexesRet, std::vector<BLSVerificationVectorPtr>& vvecsRet, BLSSecretKeyVector& skContributionsRet)
bool CDKGSessionManager::GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const std::vector<bool>& validMembers, std::vector<uint16_t>& memberIndexesRet, std::vector<BLSVerificationVectorPtr>& vvecsRet, BLSSecretKeyVector& skContributionsRet) const
{
LOCK(contributionsCacheCs);
auto members = CLLMQUtils::GetAllQuorumMembers(llmqType, pindexQuorum);
@ -368,7 +368,7 @@ bool CDKGSessionManager::GetEncryptedContributions(Consensus::LLMQType llmqType,
return true;
}
void CDKGSessionManager::CleanupCache()
void CDKGSessionManager::CleanupCache() const
{
LOCK(contributionsCacheCs);
auto curTime = GetTimeMillis();

View File

@ -26,7 +26,7 @@ private:
std::map<Consensus::LLMQType, CDKGSessionHandler> dkgSessionHandlers;
CCriticalSection contributionsCacheCs;
mutable CCriticalSection contributionsCacheCs;
struct ContributionsCacheKey {
Consensus::LLMQType llmqType;
uint256 quorumHash;
@ -43,7 +43,7 @@ private:
BLSVerificationVectorPtr vvec;
CBLSSecretKey skContribution;
};
std::map<ContributionsCacheKey, ContributionsCacheEntry> contributionsCache GUARDED_BY(contributionsCacheCs);
mutable std::map<ContributionsCacheKey, ContributionsCacheEntry> contributionsCache GUARDED_BY(contributionsCacheCs);
public:
CDKGSessionManager(CBLSWorker& _blsWorker, bool unitTests, bool fWipe);
@ -64,7 +64,7 @@ public:
// Contributions are written while in the DKG
void WriteVerifiedVvecContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, const BLSVerificationVectorPtr& vvec);
void WriteVerifiedSkContribution(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, const CBLSSecretKey& skContribution);
bool GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const std::vector<bool>& validMembers, std::vector<uint16_t>& memberIndexesRet, std::vector<BLSVerificationVectorPtr>& vvecsRet, BLSSecretKeyVector& skContributionsRet);
bool GetVerifiedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const std::vector<bool>& validMembers, std::vector<uint16_t>& memberIndexesRet, std::vector<BLSVerificationVectorPtr>& vvecsRet, BLSSecretKeyVector& skContributionsRet) const;
/// Write encrypted (unverified) DKG contributions for the member with the given proTxHash to the llmqDb
void WriteEncryptedContributions(Consensus::LLMQType llmqType, const CBlockIndex* pindexQuorum, const uint256& proTxHash, const CBLSIESMultiRecipientObjects<CBLSSecretKey>& contributions);
/// Read encrypted (unverified) DKG contributions for the member with the given proTxHash from the llmqDb
@ -72,7 +72,7 @@ public:
private:
void MigrateDKG();
void CleanupCache();
void CleanupCache() const;
};
bool IsQuorumDKGEnabled();

View File

@ -455,6 +455,8 @@ void CInstantSendManager::InterruptWorkerThread()
void CInstantSendManager::ProcessTx(const CTransaction& tx, bool fRetroactive, const Consensus::Params& params)
{
AssertLockNotHeld(cs);
if (!fMasternodeMode || !IsInstantSendEnabled() || !masternodeSync.IsBlockchainSynced()) {
return;
}
@ -563,6 +565,8 @@ bool CInstantSendManager::CheckCanLock(const CTransaction& tx, bool printDebug,
bool CInstantSendManager::CheckCanLock(const COutPoint& outpoint, bool printDebug, const uint256& txHash, const Consensus::Params& params) const
{
AssertLockNotHeld(cs);
int nInstantSendConfirmationsRequired = params.nInstantSendConfirmationsRequired;
if (IsLocked(outpoint.hash)) {
@ -745,6 +749,8 @@ void CInstantSendManager::ProcessMessage(CNode* pfrom, const std::string& strCom
void CInstantSendManager::ProcessMessageInstantSendLock(const CNode* pfrom, const llmq::CInstantSendLockPtr& islock)
{
AssertLockNotHeld(cs);
auto hash = ::SerializeHash(*islock);
{
@ -1398,6 +1404,8 @@ void CInstantSendManager::ResolveBlockConflicts(const uint256& islockHash, const
void CInstantSendManager::RemoveConflictingLock(const uint256& islockHash, const llmq::CInstantSendLock& islock)
{
AssertLockNotHeld(cs);
LogPrintf("CInstantSendManager::%s -- txid=%s, islock=%s: Removing ISLOCK and its chained children\n", __func__,
islock.txid.ToString(), islockHash.ToString());
int tipHeight;

View File

@ -199,9 +199,9 @@ public:
void InterruptWorkerThread();
private:
void ProcessTx(const CTransaction& tx, bool fRetroactive, const Consensus::Params& params);
void ProcessTx(const CTransaction& tx, bool fRetroactive, const Consensus::Params& params) LOCKS_EXCLUDED(cs);
bool CheckCanLock(const CTransaction& tx, bool printDebug, const Consensus::Params& params) const;
bool CheckCanLock(const COutPoint& outpoint, bool printDebug, const uint256& txHash, const Consensus::Params& params) const;
bool CheckCanLock(const COutPoint& outpoint, bool printDebug, const uint256& txHash, const Consensus::Params& params) const LOCKS_EXCLUDED(cs);
bool IsConflicted(const CTransaction& tx) const;
void HandleNewInputLockRecoveredSig(const CRecoveredSig& recoveredSig, const uint256& txid);
@ -210,16 +210,16 @@ private:
bool TrySignInputLocks(const CTransaction& tx, bool allowResigning, Consensus::LLMQType llmqType);
void TrySignInstantSendLock(const CTransaction& tx);
void ProcessMessageInstantSendLock(const CNode* pfrom, const CInstantSendLockPtr& islock);
void ProcessMessageInstantSendLock(const CNode* pfrom, const CInstantSendLockPtr& islock) LOCKS_EXCLUDED(cs);
static bool PreVerifyInstantSendLock(const CInstantSendLock& islock);
bool ProcessPendingInstantSendLocks();
std::unordered_set<uint256> ProcessPendingInstantSendLocks(int signOffset, const std::unordered_map<uint256, std::pair<NodeId, CInstantSendLockPtr>, StaticSaltedHasher>& pend, bool ban);
void ProcessInstantSendLock(NodeId from, const uint256& hash, const CInstantSendLockPtr& islock);
void AddNonLockedTx(const CTransactionRef& tx, const CBlockIndex* pindexMined);
void RemoveNonLockedTx(const uint256& txid, bool retryChildren);
void RemoveConflictedTx(const CTransaction& tx);
void TruncateRecoveredSigsForInputs(const CInstantSendLock& islock);
void AddNonLockedTx(const CTransactionRef& tx, const CBlockIndex* pindexMined) EXCLUSIVE_LOCKS_REQUIRED(cs);
void RemoveNonLockedTx(const uint256& txid, bool retryChildren) EXCLUSIVE_LOCKS_REQUIRED(cs);
void RemoveConflictedTx(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(cs);
void TruncateRecoveredSigsForInputs(const CInstantSendLock& islock) EXCLUSIVE_LOCKS_REQUIRED(cs);
void RemoveMempoolConflictsForLock(const uint256& hash, const CInstantSendLock& islock);
void ResolveBlockConflicts(const uint256& islockHash, const CInstantSendLock& islock);
@ -251,7 +251,7 @@ public:
void NotifyChainLock(const CBlockIndex* pindexChainLock);
void UpdatedBlockTip(const CBlockIndex* pindexNew);
void RemoveConflictingLock(const uint256& islockHash, const CInstantSendLock& islock);
void RemoveConflictingLock(const uint256& islockHash, const CInstantSendLock& islock) LOCKS_EXCLUDED(cs);
size_t GetInstantSendLockCount() const;
};

View File

@ -234,7 +234,7 @@ bool CRecoveredSigsDb::HasRecoveredSig(Consensus::LLMQType llmqType, const uint2
return db->Exists(k);
}
bool CRecoveredSigsDb::HasRecoveredSigForId(Consensus::LLMQType llmqType, const uint256& id)
bool CRecoveredSigsDb::HasRecoveredSigForId(Consensus::LLMQType llmqType, const uint256& id) const
{
auto cacheKey = std::make_pair(llmqType, id);
bool ret;
@ -254,7 +254,7 @@ bool CRecoveredSigsDb::HasRecoveredSigForId(Consensus::LLMQType llmqType, const
return ret;
}
bool CRecoveredSigsDb::HasRecoveredSigForSession(const uint256& signHash)
bool CRecoveredSigsDb::HasRecoveredSigForSession(const uint256& signHash) const
{
bool ret;
{
@ -272,7 +272,7 @@ bool CRecoveredSigsDb::HasRecoveredSigForSession(const uint256& signHash)
return ret;
}
bool CRecoveredSigsDb::HasRecoveredSigForHash(const uint256& hash)
bool CRecoveredSigsDb::HasRecoveredSigForHash(const uint256& hash) const
{
bool ret;
{
@ -307,7 +307,7 @@ bool CRecoveredSigsDb::ReadRecoveredSig(Consensus::LLMQType llmqType, const uint
}
}
bool CRecoveredSigsDb::GetRecoveredSigByHash(const uint256& hash, CRecoveredSig& ret)
bool CRecoveredSigsDb::GetRecoveredSigByHash(const uint256& hash, CRecoveredSig& ret) const
{
auto k1 = std::make_tuple(std::string("rs_h"), hash);
std::pair<Consensus::LLMQType, uint256> k2;
@ -318,7 +318,7 @@ bool CRecoveredSigsDb::GetRecoveredSigByHash(const uint256& hash, CRecoveredSig&
return ReadRecoveredSig(k2.first, k2.second, ret);
}
bool CRecoveredSigsDb::GetRecoveredSigById(Consensus::LLMQType llmqType, const uint256& id, CRecoveredSig& ret)
bool CRecoveredSigsDb::GetRecoveredSigById(Consensus::LLMQType llmqType, const uint256& id, CRecoveredSig& ret) const
{
return ReadRecoveredSig(llmqType, id, ret);
}
@ -545,7 +545,7 @@ CSigningManager::CSigningManager(bool fMemory, bool fWipe) :
{
}
bool CSigningManager::AlreadyHave(const CInv& inv)
bool CSigningManager::AlreadyHave(const CInv& inv) const
{
if (inv.type != MSG_QUORUM_RECOVERED_SIG) {
return false;
@ -560,7 +560,7 @@ bool CSigningManager::AlreadyHave(const CInv& inv)
return db.HasRecoveredSigForHash(inv.hash);
}
bool CSigningManager::GetRecoveredSigForGetData(const uint256& hash, CRecoveredSig& ret)
bool CSigningManager::GetRecoveredSigForGetData(const uint256& hash, CRecoveredSig& ret) const
{
if (!db.GetRecoveredSigByHash(hash, ret)) {
return false;
@ -950,22 +950,22 @@ bool CSigningManager::AsyncSignIfMember(Consensus::LLMQType llmqType, const uint
return true;
}
bool CSigningManager::HasRecoveredSig(Consensus::LLMQType llmqType, const uint256& id, const uint256& msgHash)
bool CSigningManager::HasRecoveredSig(Consensus::LLMQType llmqType, const uint256& id, const uint256& msgHash) const
{
return db.HasRecoveredSig(llmqType, id, msgHash);
}
bool CSigningManager::HasRecoveredSigForId(Consensus::LLMQType llmqType, const uint256& id)
bool CSigningManager::HasRecoveredSigForId(Consensus::LLMQType llmqType, const uint256& id) const
{
return db.HasRecoveredSigForId(llmqType, id);
}
bool CSigningManager::HasRecoveredSigForSession(const uint256& signHash)
bool CSigningManager::HasRecoveredSigForSession(const uint256& signHash) const
{
return db.HasRecoveredSigForSession(signHash);
}
bool CSigningManager::GetRecoveredSigForId(Consensus::LLMQType llmqType, const uint256& id, llmq::CRecoveredSig& retRecSig)
bool CSigningManager::GetRecoveredSigForId(Consensus::LLMQType llmqType, const uint256& id, llmq::CRecoveredSig& retRecSig) const
{
if (!db.GetRecoveredSigById(llmqType, id, retRecSig)) {
return false;
@ -973,7 +973,7 @@ bool CSigningManager::GetRecoveredSigForId(Consensus::LLMQType llmqType, const u
return true;
}
bool CSigningManager::IsConflicting(Consensus::LLMQType llmqType, const uint256& id, const uint256& msgHash)
bool CSigningManager::IsConflicting(Consensus::LLMQType llmqType, const uint256& id, const uint256& msgHash) const
{
if (!db.HasRecoveredSigForId(llmqType, id)) {
// no recovered sig present, so no conflict
@ -989,12 +989,12 @@ bool CSigningManager::IsConflicting(Consensus::LLMQType llmqType, const uint256&
return false;
}
bool CSigningManager::HasVotedOnId(Consensus::LLMQType llmqType, const uint256& id)
bool CSigningManager::HasVotedOnId(Consensus::LLMQType llmqType, const uint256& id) const
{
return db.HasVotedOnId(llmqType, id);
}
bool CSigningManager::GetVoteForId(Consensus::LLMQType llmqType, const uint256& id, uint256& msgHashRet)
bool CSigningManager::GetVoteForId(Consensus::LLMQType llmqType, const uint256& id, uint256& msgHashRet) const
{
return db.GetVoteForId(llmqType, id, msgHashRet);
}

View File

@ -68,20 +68,20 @@ class CRecoveredSigsDb
private:
std::unique_ptr<CDBWrapper> db{nullptr};
CCriticalSection cs;
unordered_lru_cache<std::pair<Consensus::LLMQType, uint256>, bool, StaticSaltedHasher, 30000> hasSigForIdCache GUARDED_BY(cs);
unordered_lru_cache<uint256, bool, StaticSaltedHasher, 30000> hasSigForSessionCache GUARDED_BY(cs);
unordered_lru_cache<uint256, bool, StaticSaltedHasher, 30000> hasSigForHashCache GUARDED_BY(cs);
mutable CCriticalSection cs;
mutable unordered_lru_cache<std::pair<Consensus::LLMQType, uint256>, bool, StaticSaltedHasher, 30000> hasSigForIdCache GUARDED_BY(cs);
mutable unordered_lru_cache<uint256, bool, StaticSaltedHasher, 30000> hasSigForSessionCache GUARDED_BY(cs);
mutable unordered_lru_cache<uint256, bool, StaticSaltedHasher, 30000> hasSigForHashCache GUARDED_BY(cs);
public:
explicit CRecoveredSigsDb(bool fMemory, bool fWipe);
bool HasRecoveredSig(Consensus::LLMQType llmqType, const uint256& id, const uint256& msgHash) const;
bool HasRecoveredSigForId(Consensus::LLMQType llmqType, const uint256& id);
bool HasRecoveredSigForSession(const uint256& signHash);
bool HasRecoveredSigForHash(const uint256& hash);
bool GetRecoveredSigByHash(const uint256& hash, CRecoveredSig& ret);
bool GetRecoveredSigById(Consensus::LLMQType llmqType, const uint256& id, CRecoveredSig& ret);
bool HasRecoveredSigForId(Consensus::LLMQType llmqType, const uint256& id) const;
bool HasRecoveredSigForSession(const uint256& signHash) const;
bool HasRecoveredSigForHash(const uint256& hash) const;
bool GetRecoveredSigByHash(const uint256& hash, CRecoveredSig& ret) const;
bool GetRecoveredSigById(Consensus::LLMQType llmqType, const uint256& id, CRecoveredSig& ret) const;
void WriteRecoveredSig(const CRecoveredSig& recSig);
void RemoveRecoveredSig(Consensus::LLMQType llmqType, const uint256& id);
void TruncateRecoveredSig(Consensus::LLMQType llmqType, const uint256& id);
@ -99,7 +99,7 @@ private:
void MigrateRecoveredSigs();
bool ReadRecoveredSig(Consensus::LLMQType llmqType, const uint256& id, CRecoveredSig& ret) const;
void RemoveRecoveredSig(CDBBatch& batch, Consensus::LLMQType llmqType, const uint256& id, bool deleteHashKey, bool deleteTimeKey);
void RemoveRecoveredSig(CDBBatch& batch, Consensus::LLMQType llmqType, const uint256& id, bool deleteHashKey, bool deleteTimeKey) EXCLUSIVE_LOCKS_REQUIRED(cs);
};
class CRecoveredSigsListener
@ -120,7 +120,7 @@ class CSigningManager
static const int SIGN_HEIGHT_OFFSET = 8;
private:
CCriticalSection cs;
mutable CCriticalSection cs;
CRecoveredSigsDb db;
@ -137,8 +137,8 @@ private:
public:
CSigningManager(bool fMemory, bool fWipe);
bool AlreadyHave(const CInv& inv);
bool GetRecoveredSigForGetData(const uint256& hash, CRecoveredSig& ret);
bool AlreadyHave(const CInv& inv) const;
bool GetRecoveredSigForGetData(const uint256& hash, CRecoveredSig& ret) const;
void ProcessMessage(CNode* pnode, const std::string& strCommand, CDataStream& vRecv);
@ -170,14 +170,14 @@ public:
void UnregisterRecoveredSigsListener(CRecoveredSigsListener* l);
bool AsyncSignIfMember(Consensus::LLMQType llmqType, const uint256& id, const uint256& msgHash, const uint256& quorumHash = uint256(), bool allowReSign = false);
bool HasRecoveredSig(Consensus::LLMQType llmqType, const uint256& id, const uint256& msgHash);
bool HasRecoveredSigForId(Consensus::LLMQType llmqType, const uint256& id);
bool HasRecoveredSigForSession(const uint256& signHash);
bool GetRecoveredSigForId(Consensus::LLMQType llmqType, const uint256& id, CRecoveredSig& retRecSig);
bool IsConflicting(Consensus::LLMQType llmqType, const uint256& id, const uint256& msgHash);
bool HasRecoveredSig(Consensus::LLMQType llmqType, const uint256& id, const uint256& msgHash) const;
bool HasRecoveredSigForId(Consensus::LLMQType llmqType, const uint256& id) const;
bool HasRecoveredSigForSession(const uint256& signHash) const;
bool GetRecoveredSigForId(Consensus::LLMQType llmqType, const uint256& id, CRecoveredSig& retRecSig) const;
bool IsConflicting(Consensus::LLMQType llmqType, const uint256& id, const uint256& msgHash) const;
bool HasVotedOnId(Consensus::LLMQType llmqType, const uint256& id);
bool GetVoteForId(Consensus::LLMQType llmqType, const uint256& id, uint256& msgHashRet);
bool HasVotedOnId(Consensus::LLMQType llmqType, const uint256& id) const;
bool GetVoteForId(Consensus::LLMQType llmqType, const uint256& id, uint256& msgHashRet) const;
static std::vector<CQuorumCPtr> GetActiveQuorumSet(Consensus::LLMQType llmqType, int signHeight);
static CQuorumCPtr SelectQuorumForSigning(Consensus::LLMQType llmqType, const uint256& selectionHash, int signHeight = -1 /*chain tip*/, int signOffset = SIGN_HEIGHT_OFFSET);

View File

@ -212,7 +212,7 @@ public:
return internalMap.empty();
}
const std::unordered_map<uint16_t, T>* GetAllForSignHash(const uint256& signHash)
const std::unordered_map<uint16_t, T>* GetAllForSignHash(const uint256& signHash) const
{
auto it = internalMap.find(signHash);
if (it == internalMap.end()) {
@ -411,16 +411,16 @@ private:
static CSigShare RebuildSigShare(const CSigSharesNodeState::SessionInfo& session, const CBatchedSigShares& batchedSigShares, size_t idx);
void Cleanup();
void RemoveSigSharesForSession(const uint256& signHash);
void RemoveSigSharesForSession(const uint256& signHash) EXCLUSIVE_LOCKS_REQUIRED(cs);
void RemoveBannedNodeStates();
void BanNode(NodeId nodeId);
bool SendMessages();
void CollectSigSharesToRequest(std::unordered_map<NodeId, std::unordered_map<uint256, CSigSharesInv, StaticSaltedHasher>>& sigSharesToRequest);
void CollectSigSharesToSend(std::unordered_map<NodeId, std::unordered_map<uint256, CBatchedSigShares, StaticSaltedHasher>>& sigSharesToSend);
void CollectSigSharesToSendConcentrated(std::unordered_map<NodeId, std::vector<CSigShare>>& sigSharesToSend, const std::vector<CNode*>& vNodes);
void CollectSigSharesToAnnounce(std::unordered_map<NodeId, std::unordered_map<uint256, CSigSharesInv, StaticSaltedHasher>>& sigSharesToAnnounce);
void CollectSigSharesToRequest(std::unordered_map<NodeId, std::unordered_map<uint256, CSigSharesInv, StaticSaltedHasher>>& sigSharesToRequest) EXCLUSIVE_LOCKS_REQUIRED(cs);
void CollectSigSharesToSend(std::unordered_map<NodeId, std::unordered_map<uint256, CBatchedSigShares, StaticSaltedHasher>>& sigSharesToSend) EXCLUSIVE_LOCKS_REQUIRED(cs);
void CollectSigSharesToSendConcentrated(std::unordered_map<NodeId, std::vector<CSigShare>>& sigSharesToSend, const std::vector<CNode*>& vNodes) EXCLUSIVE_LOCKS_REQUIRED(cs);
void CollectSigSharesToAnnounce(std::unordered_map<NodeId, std::unordered_map<uint256, CSigSharesInv, StaticSaltedHasher>>& sigSharesToAnnounce) EXCLUSIVE_LOCKS_REQUIRED(cs);
void SignPendingSigShares();
void WorkThreadMain();
};

View File

@ -197,7 +197,7 @@ public:
bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const FlatFilePos* dbp, bool* fNewBlock) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
// Block (dis)connection on a given view:
DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view);
DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex,
CCoinsViewCache& view, const CChainParams& chainparams, bool fJustCheck = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
@ -1668,6 +1668,8 @@ int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out)
* When FAILED is returned, view is left in an indeterminate state. */
DisconnectResult CChainState::DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view)
{
AssertLockHeld(cs_main);
bool fDIP0003Active = pindex->nHeight >= Params().GetConsensus().DIP0003Height;
if (fDIP0003Active && !evoDb->VerifyBestBlock(pindex->GetBlockHash())) {
// Nodes that upgraded after DIP3 activation will have to reindex to ensure evodb consistency
@ -2675,6 +2677,8 @@ void static UpdateTip(const CBlockIndex *pindexNew, const CChainParams& chainPar
*/
bool CChainState::DisconnectTip(CValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions *disconnectpool)
{
AssertLockHeld(cs_main);
CBlockIndex *pindexDelete = chainActive.Tip();
assert(pindexDelete);
// Read block from disk.