Skip to content

Commit b1d249d

Browse files
authored
fix: avoid some crashes on invalidateblock (dashpay#5683)
## Issue being fixed or feature implemented ``` Assertion failure: assertion: quorum != nullptr file: quorums.cpp, line: 547 function: ScanQuorums ``` ## What was done? Hold cs_main while scanning to make sure tip doesn't move. Happened in `ProcessPendingInstantSendLocks()` only for me but I thought that it would probably make sense to apply the same fix in other places too. ## How Has This Been Tested? run `invalidateblock` for a deep enough height (100s of blocks) ## Breaking Changes n/a ## Checklist: - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have added or updated relevant unit/integration/functional/e2e tests - [ ] I have made corresponding changes to the documentation - [x] I have assigned this pull request to a milestone _(for repository code-owners and collaborators only)_
1 parent 6e639c7 commit b1d249d

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

src/llmq/instantsend.cpp

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,14 @@ void CInstantSendDb::RemoveAndArchiveInstantSendLock(const gsl::not_null<CInstan
454454

455455
////////////////
456456

457+
std::optional<Consensus::LLMQType> GetInstantSendLLMQTypeAtTip(const CQuorumManager& qman, const CChainState& chainstate)
458+
{
459+
LOCK(cs_main);
460+
const CBlockIndex* tip = chainstate.m_chain.Tip();
461+
if (tip == nullptr) return std::nullopt;
462+
return std::make_optional(utils::GetInstantSendLLMQType(qman, tip));
463+
}
464+
457465
void CInstantSendManager::Start()
458466
{
459467
// can't start new thread if we have one running already
@@ -511,7 +519,9 @@ void CInstantSendManager::ProcessTx(const CTransaction& tx, bool fRetroactive, c
511519
// block after we retroactively locked all transactions.
512520
if (!IsInstantSendMempoolSigningEnabled() && !fRetroactive) return;
513521

514-
if (!TrySignInputLocks(tx, fRetroactive, utils::GetInstantSendLLMQType(qman, WITH_LOCK(cs_main, return m_chainstate.m_chain.Tip())), params)) {
522+
if (auto llmqType_opt{GetInstantSendLLMQTypeAtTip(qman, m_chainstate)}; !llmqType_opt.has_value()) {
523+
return;
524+
} else if (!TrySignInputLocks(tx, fRetroactive, llmqType_opt.value(), params)) {
515525
return;
516526
}
517527

@@ -685,7 +695,9 @@ void CInstantSendManager::HandleNewInputLockRecoveredSig(const CRecoveredSig& re
685695

686696
void CInstantSendManager::TrySignInstantSendLock(const CTransaction& tx)
687697
{
688-
const auto llmqType = utils::GetInstantSendLLMQType(qman, WITH_LOCK(cs_main, return m_chainstate.m_chain.Tip()));
698+
const auto llmqType_opt{GetInstantSendLLMQTypeAtTip(qman, m_chainstate)};
699+
if (!llmqType_opt.has_value()) return;
700+
const auto llmqType = llmqType_opt.value();
689701

690702
for (const auto& in : tx.vin) {
691703
auto id = ::SerializeHash(std::make_pair(INPUTLOCK_REQUESTID_PREFIX, in.prevout));
@@ -855,9 +867,10 @@ bool CInstantSendLock::TriviallyValid() const
855867

856868
bool CInstantSendManager::ProcessPendingInstantSendLocks()
857869
{
858-
const CBlockIndex* pBlockIndexTip = WITH_LOCK(cs_main, return m_chainstate.m_chain.Tip());
859-
if (pBlockIndexTip && utils::GetInstantSendLLMQType(qman, pBlockIndexTip) == Params().GetConsensus().llmqTypeDIP0024InstantSend) {
860-
// Don't short circuit. Try to process both deterministic and not deterministic islocks independable
870+
if (auto llmqType_opt{GetInstantSendLLMQTypeAtTip(qman, m_chainstate)}; !llmqType_opt.has_value()) {
871+
return true; // not an error
872+
} else if (llmqType_opt.value() == Params().GetConsensus().llmqTypeDIP0024InstantSend) {
873+
// Don't short circuit. Try to process both deterministic and not deterministic islocks independable
861874
bool deterministicRes = ProcessPendingInstantSendLocks(true);
862875
bool nondeterministicRes = ProcessPendingInstantSendLocks(false);
863876
return deterministicRes && nondeterministicRes;

0 commit comments

Comments
 (0)