Dash Core Source Documentation (0.16.0.1)
Find detailed information regarding the Dash Core source code.
quorums_chainlocks.cpp
Go to the documentation of this file.
40 scheduler_thread = new boost::thread(boost::bind(&TraceThread<CScheduler::Function>, "cl-scheduler", serviceLoop));
57 // regularly retry signing the current chaintip as it might have failed before due to missing ixlocks
93 void CChainLocksHandler::ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman)
109 void CChainLocksHandler::ProcessNewChainLock(NodeId from, const llmq::CChainLockSig& clsig, const uint256& hash)
130 if (!quorumSigningManager->VerifyRecoveredSig(Params().GetConsensus().llmqTypeChainLocks, clsig.nHeight, requestId, msgHash, clsig.sig)) {
131 LogPrint(BCLog::CHAINLOCKS, "CChainLocksHandler::%s -- invalid CLSIG (%s), peer=%d\n", __func__, clsig.ToString(), from);
143 // This should not happen. If it happens, it means that a malicious entity controls a large part of the MN
145 LogPrintf("CChainLocksHandler::%s -- new CLSIG (%s) tries to reorg previous CLSIG (%s), peer=%d\n",
165 LogPrintf("CChainLocksHandler::%s -- height of CLSIG (%s) does not match the specified block's height (%d)\n",
189 LogPrintf("CChainLocksHandler::%s -- block header %s came in late, updating and enforcing\n", __func__, pindexNew->GetBlockHash().ToString());
193 LogPrintf("CChainLocksHandler::%s -- height of CLSIG (%s) does not match the specified block's height (%d)\n",
198 // when EnforceBestChainLock is called later, it might end up invalidating other chains but not activating the
199 // CLSIG locked chain. This happens when only the header is known but the block is still missing yet. The usual
208 // don't call TrySignChainTip directly but instead let the scheduler call it. This way we ensure that cs_main is
209 // never locked and TrySignChainTip is not called twice in parallel. Also avoids recursive calls due to
230 fDIP0008Active = chainActive.Tip() && VersionBitsState(chainActive.Tip()->pprev, Params().GetConsensus(), Consensus::DEPLOYMENT_DIP0008, versionbitscache) == THRESHOLD_ACTIVE;
239 // ChainLocks got activated just recently, but it's possible that it was already running before, leaving
240 // us with some stale values which we should not try to enforce anymore (there probably was a good reason
270 // DIP8 defines a process called "Signing attempts" which should run before the CLSIG is finalized
271 // To simplify the initial implementation, we skip this process and directly try to create a CLSIG
272 // This will fail when multiple blocks compete, but we accept this for the initial implementation.
293 // don't sign if another conflicting CLSIG is already present. EnforceBestChainLock will later enforce
299 LogPrint(BCLog::CHAINLOCKS, "CChainLocksHandler::%s -- trying to sign %s, height=%d\n", __func__, pindex->GetBlockHash().ToString(), pindex->nHeight);
301 // When the new IX system is activated, we only try to ChainLock blocks which include safe transactions. A TX is
302 // considered safe when it is ixlocked or at least known since 10 minutes (from mempool or block). These checks are
303 // performed for the tip (which we try to sign) and the previous 5 blocks. If a ChainLocked block is found on the
305 if (IsInstantSendEnabled() && sporkManager.IsSporkActive(SPORK_3_INSTANTSEND_BLOCK_FILTERING)) {
311 LogPrint(BCLog::CHAINLOCKS, "CChainLocksHandler::%s -- tip and previous 5 blocks all safe\n", __func__);
316 LogPrint(BCLog::CHAINLOCKS, "CChainLocksHandler::%s -- chainlock at height %d \n", __func__, pindexWalk->nHeight);
337 LogPrint(BCLog::CHAINLOCKS, "CChainLocksHandler::%s -- not signing block %s due to TX %s not being ixlocked and not old enough. age=%d\n", __func__,
361 quorumSigningManager->AsyncSignIfMember(Params().GetConsensus().llmqTypeChainLocks, requestId, msgHash);
364 void CChainLocksHandler::TransactionAddedToMempool(const CTransactionRef& tx, int64_t nAcceptTime)
374 void CChainLocksHandler::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex, const std::vector<CTransactionRef>& vtxConflicted)
380 // We listen for BlockConnected so that we can collect all TX ids of all included TXs of newly received blocks
381 // We need this information later when we try to sign a new tip, so that we can determine if all included TXs are
388 // we must create this entry even if there are no lockable transactions in the block, so that TrySignChainTip
390 it = blockTxs.emplace(pindex->GetBlockHash(), std::make_shared<std::unordered_set<uint256, StaticSaltedHasher>>()).first;
407 void CChainLocksHandler::BlockDisconnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindexDisconnected)
413 CChainLocksHandler::BlockTxs::mapped_type CChainLocksHandler::GetBlockTxs(const uint256& blockHash)
429 // If running for some time, SyncTransaction should have been called before which fills blockTxs.
430 LogPrint(BCLog::CHAINLOCKS, "CChainLocksHandler::%s -- blockTxs for %s not found. Trying ReadBlockFromDisk\n", __func__,
490 // This should also not be called from validation signals, as this might result in recursive calls
516 // Go backwards through the chain referenced by clsig until we find a block that is part of the main chain.
517 // For each of these blocks, check if there are children that are NOT part of the chain referenced by clsig
533 // In case blocks from the correct chain are invalid at the moment, reconsider them. The only case where this
534 // can happen right now is when missing superblock triggers caused the main chain to be dismissed first. When
535 // the trigger later appears, this should bring us to the correct chain eventually. Please note that this does
541 activateNeeded = chainActive.Tip()->GetAncestor(currentBestChainLockBlockIndex->nHeight) != currentBestChainLockBlockIndex;
546 LogPrintf("CChainLocksHandler::%s -- ActivateBestChain failed: %s\n", __func__, FormatStateMessage(state));
553 chainActive.Tip()->GetAncestor(currentBestChainLockBlockIndex->nHeight) == currentBestChainLockBlockIndex) {
602 LogPrintf("CChainLocksHandler::%s -- InvalidateBlock failed: %s\n", __func__, FormatStateMessage(state));
715 if (chainActive.Tip()->GetAncestor(pindex->nHeight) == pindex && chainActive.Height() - pindex->nHeight >= 6) {
void UpdatedBlockTip(const CBlockIndex *pindexNew)
Definition: quorums_chainlocks.cpp:206
CTxMemPool mempool
void TransactionAddedToMempool(const CTransactionRef &tx, int64_t nAcceptTime)
Definition: quorums_chainlocks.cpp:364
Definition: spork.h:28
CChainLockSig bestChainLock
Definition: quorums_chainlocks.h:64
bool GetTransaction(const uint256 &hash, CTransactionRef &txOut, const Consensus::Params &consensusParams, uint256 &hashBlock, bool fAllowSlow, CBlockIndex *blockIndex)
Return transaction in txOut, and if it was found inside a block, its hash is placed in hashBlock...
Definition: validation.cpp:950
void ProcessNewChainLock(NodeId from, const CChainLockSig &clsig, const uint256 &hash)
Definition: quorums_chainlocks.cpp:109
~CChainLocksHandler()
Definition: quorums_chainlocks.cpp:43
CChainLockSig bestChainLockWithKnownBlock
Definition: quorums_chainlocks.h:66
bool VerifyRecoveredSig(Consensus::LLMQType llmqType, int signedAtHeight, const uint256 &id, const uint256 &msgHash, const CBLSSignature &sig)
Definition: quorums_signing.cpp:931
Definition: block.h:72
bool InvalidateBlock(CValidationState &state, const CChainParams &chainparams, CBlockIndex *pindex)
Mark a block as invalid.
Definition: validation.cpp:3180
bool HasChainLock(int nHeight, const uint256 &blockHash)
Definition: quorums_chainlocks.cpp:608
void TrySignChainTip()
Definition: quorums_chainlocks.cpp:248
BlockTxs::mapped_type GetBlockTxs(const uint256 &blockHash)
Definition: quorums_chainlocks.cpp:413
uint256 bestChainLockHash
Definition: quorums_chainlocks.h:63
void RegisterRecoveredSigsListener(CRecoveredSigsListener *l)
Definition: quorums_signing.cpp:771
void EnforceBestChainLock()
Definition: quorums_chainlocks.cpp:491
void NotifyChainLock(const CBlockIndex *pindex, const llmq::CChainLockSig &clsig)
Definition: validationinterface.cpp:230
void ProcessMessage(CNode *pfrom, const std::string &strCommand, CDataStream &vRecv, CConnman &connman)
Definition: quorums_chainlocks.cpp:93
void scheduleEvery(Function f, int64_t deltaMilliSeconds)
Definition: scheduler.cpp:126
Double ended buffer combining vector and stream-like interfaces.
Definition: streams.h:103
Definition: deterministicmns.h:26
bool AsyncSignIfMember(Consensus::LLMQType llmqType, const uint256 &id, const uint256 &msgHash, bool allowReSign=false)
Definition: quorums_signing.cpp:784
void scheduleFromNow(Function f, int64_t deltaMilliSeconds)
Definition: scheduler.cpp:115
uint256 lastSignedMsgHash
Definition: quorums_chainlocks.h:72
bool ActivateBestChain(CValidationState &state, const CChainParams &chainparams, std::shared_ptr< const CBlock > pblock)
Find the best known block, and make it the tip of the block chain.
Definition: validation.cpp:3072
void UnregisterRecoveredSigsListener(CRecoveredSigsListener *l)
Definition: quorums_signing.cpp:777
std::map< uint256, int64_t > seenChainLocks
Definition: quorums_chainlocks.h:79
bool tryLockChainTipScheduled
Definition: quorums_chainlocks.h:59
Definition: params.h:22
bool IsValid(enum BlockStatus nUpTo=BLOCK_VALID_TRANSACTIONS) const
Check whether this block index entry is valid up to the passed validity level.
Definition: chain.h:332
uint256 SerializeHash(const T &obj, int nType=SER_GETHASH, int nVersion=PROTOCOL_VERSION)
Compute the 256-bit hash of an object's serialization.
Definition: hash.h:254
void Misbehaving(NodeId pnode, int howmuch, const std::string &message)
Increase a node's misbehavior score.
Definition: net_processing.cpp:1024
void CheckActiveState()
Definition: quorums_chainlocks.cpp:225
std::unordered_map< uint256, int64_t > txFirstSeenTime
Definition: quorums_chainlocks.h:77
static const std::string CLSIG_REQUESTID_PREFIX
Definition: quorums_chainlocks.cpp:22
uint256 lastSignedRequestId
Definition: quorums_chainlocks.h:71
bool IsTxSafeForMining(const uint256 &txid)
Definition: quorums_chainlocks.cpp:462
virtual void HandleNewRecoveredSig(const CRecoveredSig &recoveredSig)
Definition: quorums_chainlocks.cpp:564
boost::thread * scheduler_thread
Definition: quorums_chainlocks.h:57
bool InternalHasConflictingChainLock(int nHeight, const uint256 &blockHash)
Definition: quorums_chainlocks.cpp:644
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:471
void BlockDisconnected(const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindexDisconnected)
Definition: quorums_chainlocks.cpp:407
Definition: protocol.h:424
static const int64_t CLEANUP_SEEN_TIMEOUT
Definition: quorums_chainlocks.h:50
Definition: quorums_signing.h:21
CChainLocksHandler()
Definition: quorums_chainlocks.cpp:36
Definition: versionbits.h:24
static const int64_t CLEANUP_INTERVAL
Definition: quorums_chainlocks.h:49
Definition: spork.h:25
const CBlockIndex * lastNotifyChainLockBlockIndex
Definition: quorums_chainlocks.h:68
std::string FormatStateMessage(const CValidationState &state)
Convert CValidationState to a human-readable message for logging.
Definition: validation.cpp:513
bool ResetBlockFailureFlags(CBlockIndex *pindex)
Remove invalidity status from a block and its descendants.
Definition: validation.cpp:3218
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:947
void BlockConnected(const std::shared_ptr< const CBlock > &pblock, const CBlockIndex *pindex, const std::vector< CTransactionRef > &vtxConflicted)
Definition: quorums_chainlocks.cpp:374
Definition: quorums_chainlocks.h:47
Definition: util.h:133
bool AlreadyHave(const CInv &inv)
Definition: quorums_chainlocks.cpp:68
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:453
bool HasConflictingChainLock(int nHeight, const uint256 &blockHash)
Definition: quorums_chainlocks.cpp:638
bool InternalHasChainLock(int nHeight, const uint256 &blockHash)
Definition: quorums_chainlocks.cpp:614
CChainLockSig GetBestChainLock()
Definition: quorums_chainlocks.cpp:87
void EraseObjectRequest(CNodeState *nodestate, const CInv &inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: net_processing.cpp:676
Definition: quorums_chainlocks.h:25
bool IsSporkActive(SporkId nSporkID)
IsSporkActive returns a bool for time-based sporks, and should be used to determine whether the spork...
Definition: spork.cpp:211
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:54
bool ReadBlockFromDisk(CBlock &block, const CDiskBlockPos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
Definition: validation.cpp:1043
Definition: scheduler.h:37
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:110
bool GetChainLockByHash(const uint256 &hash, CChainLockSig &ret)
Definition: quorums_chainlocks.cpp:74
CChain & chainActive
The currently-connected chain of blocks (protected by cs_main).
Definition: validation.cpp:217
AssertLockHeld(g_cs_orphans)
ThresholdState VersionBitsState(const CBlockIndex *pindexPrev, const Consensus::Params ¶ms, Consensus::DeploymentPos pos, VersionBitsCache &cache)
Definition: versionbits.cpp:245
const CBlockIndex * bestChainLockBlockIndex
Definition: quorums_chainlocks.h:67
int32_t lastSignedHeight
Definition: quorums_chainlocks.h:70
void DoInvalidateBlock(const CBlockIndex *pindex)
Definition: quorums_chainlocks.cpp:591
void AcceptedBlockHeader(const CBlockIndex *pindexNew)
Definition: quorums_chainlocks.cpp:184