diff --git a/src/coinjoin/server.cpp b/src/coinjoin/server.cpp index 8146bb269542..b06652025431 100644 --- a/src/coinjoin/server.cpp +++ b/src/coinjoin/server.cpp @@ -42,7 +42,6 @@ CCoinJoinServer::CCoinJoinServer(PeerManagerInternal* peer_manager, ChainstateMa m_mn_activeman{mn_activeman}, m_mn_sync{mn_sync}, m_isman{isman}, - vecSessionCollaterals{}, fUnitTest{false} { } @@ -87,7 +86,7 @@ void CCoinJoinServer::ProcessDSACCEPT(CNode& peer, CDataStream& vRecv) return; } - if (WITH_LOCK(cs_coinjoin, return vecSessionCollaterals.empty())) { + if (WITH_LOCK(cs_coinjoin, return m_session_collaterals.empty())) { { const auto hasQueue = m_queueman.TryHasQueueFromMasternode(m_mn_activeman.GetOutPoint()); if (!hasQueue.has_value()) return; @@ -233,14 +232,14 @@ void CCoinJoinServer::ProcessDSSIGNFINALTX(CNode& peer, CDataStream& vRecv) // Only accept signatures while we are actually collecting them, and only // from peers that are active participants in this session. Otherwise a // stray or unauthenticated peer could abort the session for everyone. - if (nState != POOL_STATE_SIGNING) { - LogPrint(BCLog::COINJOIN, "DSSIGNFINALTX -- wrong state, nState=%d, peer=%d\n", - nState.load(), peer.GetId()); - PushStatus(peer, STATUS_REJECTED, ERR_SESSION); - return; - } + int session_id{0}; { LOCK(cs_coinjoin); + if (nState != POOL_STATE_SIGNING) { + LogPrint(BCLog::COINJOIN, "DSSIGNFINALTX -- wrong state, nState=%d, peer=%d\n", nState.load(), peer.GetId()); + PushStatus(peer, STATUS_REJECTED, ERR_SESSION); + return; + } const bool is_participant = std::ranges::any_of( vecEntries, [&peer](const auto& entry) { return entry.addr == peer.addr; }); if (!is_participant) { @@ -249,6 +248,7 @@ void CCoinJoinServer::ProcessDSSIGNFINALTX(CNode& peer, CDataStream& vRecv) PushStatus(peer, STATUS_REJECTED, ERR_INVALID_INPUT); return; } + session_id = nSessionID; } const size_t max_txins{CoinJoin::GetMaxPoolInputOutputCount()}; @@ -265,18 +265,30 @@ void CCoinJoinServer::ProcessDSSIGNFINALTX(CNode& peer, CDataStream& vRecv) LogPrint(BCLog::COINJOIN, "DSSIGNFINALTX -- vecTxIn.size() %s\n", vecTxIn.size()); - int nTxInIndex = 0; - int nTxInsCount = static_cast(vecTxIn.size()); - - for (const auto& txin : vecTxIn) { - nTxInIndex++; - if (!AddScriptSig(txin)) { - LogPrint(BCLog::COINJOIN, "DSSIGNFINALTX -- AddScriptSig() failed at %d/%d, session: %d\n", nTxInIndex, nTxInsCount, nSessionID); - LOCK(cs_coinjoin); - RelayStatus(STATUS_REJECTED); + { + LOCK(cs_coinjoin); + // Apply the whole message in one critical section, against the signing session it was + // checked for above. The lock was released while the body was decoded, and releasing it + // between inputs would let a timeout reset the session halfway through one sender's + // already-received signatures, after charging that sender as a non-signer. + if (nSessionID != session_id || nState != POOL_STATE_SIGNING) { + LogPrint(BCLog::COINJOIN, "DSSIGNFINALTX -- session %d is no longer signing, peer=%d\n", session_id, + peer.GetId()); + PushStatus(peer, STATUS_REJECTED, ERR_SESSION); return; } - LogPrint(BCLog::COINJOIN, "DSSIGNFINALTX -- AddScriptSig() %d/%d success\n", nTxInIndex, nTxInsCount); + int nTxInIndex = 0; + int nTxInsCount = static_cast(vecTxIn.size()); + for (const auto& txin : vecTxIn) { + nTxInIndex++; + if (!AddScriptSig(txin)) { + LogPrint(BCLog::COINJOIN, "DSSIGNFINALTX -- AddScriptSig() failed at %d/%d, session: %d\n", nTxInIndex, + nTxInsCount, session_id); + RelayStatus(STATUS_REJECTED); + return; + } + LogPrint(BCLog::COINJOIN, "DSSIGNFINALTX -- AddScriptSig() %d/%d success\n", nTxInIndex, nTxInsCount); + } } // all is good CheckPool(); @@ -286,8 +298,7 @@ void CCoinJoinServer::SetNull() { AssertLockHeld(cs_coinjoin); // MN side - vecSessionCollaterals.clear(); - setSessionCollateralPrevouts.clear(); + m_session_collaterals.Clear(); m_fRebalanceSession = false; m_fHasLegacyParticipant = false; m_mapDeclaredShapes.clear(); @@ -301,12 +312,20 @@ void CCoinJoinServer::SetNull() // void CCoinJoinServer::CheckPool() { + AssertLockNotHeld(cs_coinjoin); + + // Both the scheduler thread and the message-handling thread get here. Skip the round if the + // other one is already in it rather than blocking msghand behind its mempool work: whichever + // thread holds the lock is performing the same check we would. + TRY_LOCK(cs_check_pool, lock_check_pool); + if (!lock_check_pool) return; + // Every decision below reads several pieces of session state at once, so take them as one // snapshot. Sampling them separately let the message-handling thread commit an entry // between two reads: the side counts could be read before the last entry arrived while the // entry count was read after it, so a session that was about to finalize looked like one // whose entries were all in but left a side uncovered - and got reset. Reading - // vecSessionCollaterals without the lock could also race SetNull() clearing it. + // the session collaterals without the lock could also race SetNull() clearing them. const auto snap = GetPoolSnapshot(); if (snap.entries != 0) @@ -320,7 +339,7 @@ void CCoinJoinServer::CheckPool() if (snap.state == POOL_STATE_ACCEPTING_ENTRIES && snap.entries == snap.collaterals) { if (snap.sides.IsCovered()) { LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CheckPool -- FINALIZE TRANSACTIONS\n"); - CreateFinalTransaction(snap.session_id); + CreateFinalTransaction(snap.session_id, /*charge_fees=*/false); return; } // The participant set is frozen once entries are being accepted, so this session can @@ -328,13 +347,18 @@ void CCoinJoinServer::CheckPool() // timeout. Shouldn't happen: admission checks the declared shapes up front. LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CheckPool -- all entries received but a session denom side is uncovered (%d in, %d out), resetting session\n", snap.sides.inputs, snap.sides.outputs); - // Tell the participants before dropping them, so they release their inputs and collateral - // right away instead of waiting out their own timeout. Must precede SetNull(), which - // clears the entries this iterates. - RelayCompletedTransaction(ERR_SESSION); // Only drop the session the snapshot described; the message-handling thread may have - // reset and restarted one while we were relaying. - WITH_LOCK(cs_coinjoin, if (IsCurrentSession(snap.session_id)) SetNull()); + // reset and restarted one since. Capture its participants in the same step, then tell + // them, so they release their inputs and collateral right away instead of waiting out + // their own timeout. + std::vector participants; + { + LOCK(cs_coinjoin); + if (!IsCurrentSession(snap.session_id)) return; + participants = GetParticipantAddrs(); + SetNull(); + } + RelayCompletedTransaction(snap.session_id, participants, ERR_SESSION); return; } @@ -342,17 +366,15 @@ void CCoinJoinServer::CheckPool() // If we timed out while accepting entries, then if we have more than minimum, create final tx if (snap.state == POOL_STATE_ACCEPTING_ENTRIES && CCoinJoinServer::HasTimedOut() && snap.sides.IsCovered() && snap.entries >= static_cast(CoinJoin::GetMinPoolParticipants())) { - // Punish misbehaving participants - ChargeFees(); - // Try to complete this session ignoring the misbehaving ones - CreateFinalTransaction(snap.session_id); + // Punish misbehaving participants and try to complete this session ignoring them + CreateFinalTransaction(snap.session_id, /*charge_fees=*/true); return; } // If we have all the signatures, try to compile the transaction - if (snap.state == POOL_STATE_SIGNING && IsSignaturesComplete()) { + if (snap.state == POOL_STATE_SIGNING && snap.signatures_complete) { LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CheckPool -- SIGNING\n"); - CommitFinalTransaction(); + CommitFinalTransaction(snap.session_id); return; } } @@ -361,65 +383,93 @@ CCoinJoinServer::PoolSnapshot CCoinJoinServer::GetPoolSnapshot() const { AssertLockNotHeld(cs_coinjoin); LOCK(cs_coinjoin); - return PoolSnapshot{nSessionID, nState, vecEntries.size(), vecSessionCollaterals.size(), - GetMixSideCountsLocked()}; + return PoolSnapshot{nSessionID, nState, vecEntries.size(), m_session_collaterals.size(), + GetMixSideCountsLocked(), IsSignaturesComplete()}; } -void CCoinJoinServer::CreateFinalTransaction(int session_id) +void CCoinJoinServer::CreateFinalTransaction(int session_id, bool charge_fees) { AssertLockNotHeld(cs_coinjoin); LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CreateFinalTransaction -- FINALIZE TRANSACTIONS\n"); - LOCK(cs_coinjoin); - - // The decision to finalize came from a snapshot taken before this lock, so make sure it - // still describes an eligible live session. An entry can finish validation and commit while - // the timeout path charges fees, changing a covered side from empty to a lone participant. - // Check the live entries under the same lock used to build the transaction so that entry - // admission cannot invalidate the decision before the state moves to signing. - if (!IsCurrentSession(session_id)) { - LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CreateFinalTransaction -- session changed, not finalizing\n"); - return; - } - const auto sides = GetMixSideCountsLocked(); - if (vecEntries.size() < static_cast(CoinJoin::GetMinPoolParticipants()) || !sides.IsCovered()) { - LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CreateFinalTransaction -- session no longer eligible, entries=%d, sides=%d/%d\n", - vecEntries.size(), sides.inputs, sides.outputs); - // Deliberately no timer refresh: on the timeout path the session stays timed out and - // the scheduler's next CheckTimeout() resets it. The missing side already had the full - // entry window, so waiting another one would only keep everyone else's coins locked. - return; - } + CTransactionRef collateral_to_charge; + { + LOCK(cs_coinjoin); - CMutableTransaction txNew; + // The decision to finalize came from a snapshot taken before this lock, so make sure it + // still describes an eligible live session. An entry can finish validation and commit in + // between, changing a covered side from empty to a lone participant. Check the live + // entries under the same lock used to build the transaction so that entry admission + // cannot invalidate the decision before the state moves to signing. + if (!IsCurrentSession(session_id)) { + LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CreateFinalTransaction -- session changed, not finalizing\n"); + return; + } + const auto sides = GetMixSideCountsLocked(); + if (vecEntries.size() < static_cast(CoinJoin::GetMinPoolParticipants()) || !sides.IsCovered()) { + LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CreateFinalTransaction -- session no longer eligible, entries=%d, sides=%d/%d\n", + vecEntries.size(), sides.inputs, sides.outputs); + // Deliberately no timer refresh: on the timeout path the session stays timed out and + // the scheduler's next CheckTimeout() resets it. The missing side already had the full + // entry window, so waiting another one would only keep everyone else's coins locked. + return; + } - // make our new transaction - for (const auto& entry : vecEntries) { - for (const auto& txout : entry.vecTxOut) { - txNew.vout.push_back(txout); + // Selecting the offenders and moving to signing below happen under one lock, so a + // participant whose entry commits before the cutoff can no longer be charged as missing. + if (charge_fees) { + collateral_to_charge = SelectCollateralToCharge(); } - for (const auto& txdsin : entry.vecTxDSIn) { - txNew.vin.push_back(txdsin); + + CMutableTransaction txNew; + + // make our new transaction + for (const auto& entry : vecEntries) { + for (const auto& txout : entry.vecTxOut) { + txNew.vout.push_back(txout); + } + for (const auto& txdsin : entry.vecTxDSIn) { + txNew.vin.push_back(txdsin); + } } - } - sort(txNew.vin.begin(), txNew.vin.end(), CompareInputBIP69()); - sort(txNew.vout.begin(), txNew.vout.end(), CompareOutputBIP69()); + sort(txNew.vin.begin(), txNew.vin.end(), CompareInputBIP69()); + sort(txNew.vout.begin(), txNew.vout.end(), CompareOutputBIP69()); - finalMutableTransaction = txNew; - LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CreateFinalTransaction -- finalMutableTransaction=%s", /* Continued */ - txNew.ToString()); + finalMutableTransaction = txNew; + LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CreateFinalTransaction -- finalMutableTransaction=%s", /* Continued */ + txNew.ToString()); - // request signatures from clients - SetState(POOL_STATE_SIGNING); - RelayFinalTransaction(CTransaction(finalMutableTransaction)); + // request signatures from clients + SetState(POOL_STATE_SIGNING); + RelayFinalTransaction(CTransaction(finalMutableTransaction)); + } + + if (collateral_to_charge) { + ConsumeReservedCollateral(collateral_to_charge); + } } -void CCoinJoinServer::CommitFinalTransaction() +void CCoinJoinServer::CommitFinalTransaction(int session_id) { AssertLockNotHeld(cs_coinjoin); - CTransactionRef finalTransaction = WITH_LOCK(cs_coinjoin, return MakeTransactionRef(finalMutableTransaction)); + CTransactionRef finalTransaction; + std::vector participants; + std::vector collaterals; + { + LOCK(cs_coinjoin); + // Committing a session that is already gone would push a cleared finalMutableTransaction + // through ATMP and notify the participants of a failure that never happened. + if (nSessionID != session_id || nState != POOL_STATE_SIGNING) { + LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CommitFinalTransaction -- session %d is gone, not committing\n", + session_id); + return; + } + finalTransaction = MakeTransactionRef(finalMutableTransaction); + participants = GetParticipantAddrs(); + collaterals = m_session_collaterals.txs(); + } uint256 hashTx = finalTransaction->GetHash(); LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CommitFinalTransaction -- finalTransaction=%s", /* Continued */ @@ -432,9 +482,9 @@ void CCoinJoinServer::CommitFinalTransaction() if (!lockMain || !ATMPIfSaneFee(m_chainman, finalTransaction)) { LogPrint(BCLog::COINJOIN, /* Continued */ "CCoinJoinServer::CommitFinalTransaction -- ATMPIfSaneFee() error: Transaction not valid\n"); - WITH_LOCK(cs_coinjoin, SetNull()); // not much we can do in this case, just notify clients - RelayCompletedTransaction(ERR_INVALID_TX); + RelayCompletedTransaction(session_id, participants, ERR_INVALID_TX); + ResetSigningSessionIfCurrent(session_id); return; } } @@ -455,14 +505,14 @@ void CCoinJoinServer::CommitFinalTransaction() m_peer_manager->PeerRelayInv(inv); // Tell the clients it was successful - RelayCompletedTransaction(MSG_SUCCESS); + RelayCompletedTransaction(session_id, participants, MSG_SUCCESS); // Randomly charge clients - ChargeRandomFees(); + ChargeRandomFees(collaterals); // Reset LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CommitFinalTransaction -- COMPLETED -- RESETTING\n"); - WITH_LOCK(cs_coinjoin, SetNull()); + ResetSigningSessionIfCurrent(session_id); } // @@ -477,18 +527,25 @@ void CCoinJoinServer::CommitFinalTransaction() // transaction for the client to be able to enter the pool. This transaction is kept by the Masternode // until the transaction is either complete or fails. // -void CCoinJoinServer::ChargeFees() const +/* + * Select one offender while cs_coinjoin still binds the state, entries and collaterals to the + * same session. The caller must close the relevant admission path before releasing the lock and + * consuming the returned collateral, so a late entry or signature cannot make the snapshot stale. + * The returned collateral stays reserved until ConsumeReservedCollateral() has run. + */ +CTransactionRef CCoinJoinServer::SelectCollateralToCharge() { - AssertLockNotHeld(cs_coinjoin); + AssertLockHeld(cs_coinjoin); //we don't need to charge collateral for every offence. - if (GetRand(/*nMax=*/100) > 33) return; + if (GetRand(/*nMax=*/100) > 33) return {}; std::vector vecOffendersCollaterals; + const PoolState state{nState}; + const size_t nSessionCollaterals{m_session_collaterals.size()}; - if (nState == POOL_STATE_ACCEPTING_ENTRIES) { - LOCK(cs_coinjoin); - for (const auto& txCollateral : vecSessionCollaterals) { + if (state == POOL_STATE_ACCEPTING_ENTRIES) { + for (const auto& txCollateral : m_session_collaterals.txs()) { bool fFound = std::ranges::any_of(vecEntries, [&txCollateral](const auto& entry) { return *entry.txCollateral == *txCollateral; }); @@ -496,21 +553,19 @@ void CCoinJoinServer::ChargeFees() const // This queue entry didn't send us the promised transaction if (!fFound) { LogPrint(BCLog::COINJOIN, /* Continued */ - "CCoinJoinServer::ChargeFees -- found uncooperative node (didn't send transaction), found " - "offence\n"); + "CCoinJoinServer::SelectCollateralToCharge -- found uncooperative node (didn't send " + "transaction), found offence\n"); vecOffendersCollaterals.push_back(txCollateral); } } - } - - if (nState == POOL_STATE_SIGNING) { + } else if (state == POOL_STATE_SIGNING) { // who didn't sign? - LOCK(cs_coinjoin); for (const auto& entry : vecEntries) { for (const auto& txdsin : entry.vecTxDSIn) { if (!txdsin.fHasSig) { LogPrint(BCLog::COINJOIN, /* Continued */ - "CCoinJoinServer::ChargeFees -- found uncooperative node (didn't sign), found offence\n"); + "CCoinJoinServer::SelectCollateralToCharge -- found uncooperative node (didn't sign), " + "found offence\n"); vecOffendersCollaterals.push_back(entry.txCollateral); } } @@ -518,23 +573,23 @@ void CCoinJoinServer::ChargeFees() const } // no offences found - if (vecOffendersCollaterals.empty()) return; + if (vecOffendersCollaterals.empty()) return {}; //mostly offending? Charge sometimes - if (vecOffendersCollaterals.size() >= vecSessionCollaterals.size() - 1 && GetRand(/*nMax=*/100) > 33) return; + if (vecOffendersCollaterals.size() >= nSessionCollaterals - 1 && GetRand(/*nMax=*/100) > 33) return {}; //everyone is an offender? That's not right - if (vecOffendersCollaterals.size() >= vecSessionCollaterals.size()) return; + if (vecOffendersCollaterals.size() >= nSessionCollaterals) return {}; //charge one of the offenders randomly Shuffle(vecOffendersCollaterals.begin(), vecOffendersCollaterals.end(), FastRandomContext()); - if (nState == POOL_STATE_ACCEPTING_ENTRIES || nState == POOL_STATE_SIGNING) { - LogPrint(BCLog::COINJOIN, /* Continued */ - "CCoinJoinServer::ChargeFees -- found uncooperative node (didn't %s transaction), charging fees: %s", - (nState == POOL_STATE_SIGNING) ? "sign" : "send", vecOffendersCollaterals[0]->ToString()); - ConsumeCollateral(vecOffendersCollaterals[0]); - } + LogPrint(BCLog::COINJOIN, /* Continued */ + "CCoinJoinServer::SelectCollateralToCharge -- found uncooperative node (didn't %s transaction), charging " + "fees: %s", + (state == POOL_STATE_SIGNING) ? "sign" : "send", vecOffendersCollaterals[0]->ToString()); + m_collaterals_being_charged.push_back(vecOffendersCollaterals[0]); + return vecOffendersCollaterals[0]; } /* @@ -549,17 +604,11 @@ void CCoinJoinServer::ChargeFees() const stop these kinds of attacks 1 in 10 successful transactions are charged. This adds up to a cost of 0.001DRK per transaction on average. */ -void CCoinJoinServer::ChargeRandomFees() const +void CCoinJoinServer::ChargeRandomFees(const std::vector& collaterals) const { AssertLockNotHeld(cs_coinjoin); - std::vector session_collaterals; - { - LOCK(cs_coinjoin); - session_collaterals = vecSessionCollaterals; - } - - for (const auto& txCollateral : session_collaterals) { + for (const auto& txCollateral : collaterals) { if (GetRand(/*nMax=*/100) > 10) return; LogPrint(BCLog::COINJOIN, /* Continued */ "CCoinJoinServer::ChargeRandomFees -- charging random fees, txCollateral=%s", txCollateral->ToString()); @@ -578,7 +627,29 @@ void CCoinJoinServer::ConsumeCollateral(const CTransactionRef& txref) const } } -void CCoinJoinServer::ConsumeCollateralIfCurrentSession(int session_id, const CTransactionRef& txref) const +void CCoinJoinServer::ConsumeReservedCollateral(const CTransactionRef& txref) +{ + AssertLockNotHeld(cs_coinjoin); + ConsumeCollateral(txref); + + // From here on the mempool, not the reservation, keeps a consumed collateral out of new sessions. + LOCK(cs_coinjoin); + const auto it = std::ranges::find(m_collaterals_being_charged, txref); + if (Assume(it != m_collaterals_being_charged.end())) m_collaterals_being_charged.erase(it); +} + +bool CCoinJoinServer::SpendsCollateralBeingCharged(const CMutableTransaction& txCollateral) const +{ + AssertLockHeld(cs_coinjoin); + return std::ranges::any_of(m_collaterals_being_charged, [&txCollateral](const CTransactionRef& charged) { + return std::ranges::any_of(charged->vin, [&txCollateral](const CTxIn& charged_in) { + return std::ranges::any_of(txCollateral.vin, + [&charged_in](const CTxIn& txin) { return txin.prevout == charged_in.prevout; }); + }); + }); +} + +void CCoinJoinServer::ConsumeCollateralIfCurrentSession(int session_id, const CTransactionRef& txref) { AssertLockNotHeld(cs_coinjoin); @@ -586,14 +657,18 @@ void CCoinJoinServer::ConsumeCollateralIfCurrentSession(int session_id, const CT // we get here the scheduler thread may have timed the session out and started another one. // Charging then would spend the collateral of a participant that is no longer in any // session - it may even be a replay of an innocent participant's collateral. - const bool fStillCurrent = - WITH_LOCK(cs_coinjoin, return IsCurrentSession(session_id) && HasSessionCollateral(txref)); + bool fStillCurrent{false}; + { + LOCK(cs_coinjoin); + fStillCurrent = IsCurrentSession(session_id) && HasSessionCollateral(txref); + if (fStillCurrent) m_collaterals_being_charged.push_back(txref); + } if (!fStillCurrent) { LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- session changed, not consuming collateral %s\n", __func__, txref->GetHash().ToString()); return; } - ConsumeCollateral(txref); + ConsumeReservedCollateral(txref); } bool CCoinJoinServer::IsCurrentSession(int session_id) const @@ -605,7 +680,7 @@ bool CCoinJoinServer::IsCurrentSession(int session_id) const bool CCoinJoinServer::HasSessionCollateral(const CTransactionRef& txref) const { AssertLockHeld(cs_coinjoin); - return std::ranges::any_of(vecSessionCollaterals, + return std::ranges::any_of(m_session_collaterals.txs(), [&txref](const CTransactionRef& ref) { return *ref == *txref; }); } @@ -618,6 +693,19 @@ bool CCoinJoinServer::HasTimedOut() const return GetTime() - nTimeLastSuccessfulStep >= nTimeout; } +bool ShouldResetTimedOutSession(const CoinJoinSessionProgress& session) +{ + if (!session.timed_out) return false; + + const bool can_advance{(session.state == POOL_STATE_QUEUE && session.queue_ready) || + (session.state == POOL_STATE_ACCEPTING_ENTRIES && + (session.entries == session.collaterals || + (session.entries >= static_cast(CoinJoin::GetMinPoolParticipants()) && + session.sides.IsCovered()))) || + (session.state == POOL_STATE_SIGNING && session.signatures_complete)}; + return !can_advance; +} + // // Check for extraneous timeout // @@ -625,13 +713,41 @@ void CCoinJoinServer::CheckTimeout() { m_queueman.CheckQueue(); - // Too early to do anything - if (!CCoinJoinServer::HasTimedOut()) return; + // CheckPool can be finalizing or committing on the message-handling thread. Skipping this tick + // keeps timeout reset and finalization/commit single-flight without blocking the scheduler. + TRY_LOCK(cs_check_pool, lock_check_pool); + if (!lock_check_pool) return; + + CTransactionRef collateral_to_charge; + { + LOCK(cs_coinjoin); + + // Decide while holding the lock so selecting an offender and closing the session form one + // atomic cutoff for late entries and signatures. + // CheckForCompleteQueue() and CheckPool() run before this method on the scheduler thread, + // but a final collateral, entry, or signature can arrive after their snapshots. The + // message-handling thread then skips its own CheckPool() if this scheduler tick still holds + // cs_check_pool. A session that can now advance takes priority over resetting it; the next + // scheduler tick will perform the transition, finalization, or commit. + if (!ShouldResetTimedOutSession({.state = nState, + .timed_out = CCoinJoinServer::HasTimedOut(), + .queue_ready = IsSessionReady(), + .entries = vecEntries.size(), + .collaterals = m_session_collaterals.size(), + .sides = GetMixSideCountsLocked(), + .signatures_complete = IsSignaturesComplete()})) { + return; + } - LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CheckTimeout -- %s timed out -- resetting\n", - (nState == POOL_STATE_SIGNING) ? "Signing" : "Session"); - ChargeFees(); - WITH_LOCK(cs_coinjoin, SetNull()); + LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CheckTimeout -- %s timed out -- resetting\n", + (nState == POOL_STATE_SIGNING) ? "Signing" : "Session"); + collateral_to_charge = SelectCollateralToCharge(); + SetNull(); + } + + if (collateral_to_charge) { + ConsumeReservedCollateral(collateral_to_charge); + } } /* @@ -641,6 +757,8 @@ void CCoinJoinServer::CheckTimeout() */ void CCoinJoinServer::CheckForCompleteQueue() { + AssertLockNotHeld(cs_coinjoin); + int session_denom; size_t participants; { @@ -649,7 +767,7 @@ void CCoinJoinServer::CheckForCompleteQueue() SetState(POOL_STATE_ACCEPTING_ENTRIES); session_denom = nSessionDenom; - participants = vecSessionCollaterals.size(); + participants = m_session_collaterals.size(); } CCoinJoinQueue dsq(session_denom, m_mn_activeman.GetOutPoint(), m_mn_activeman.GetProTxHash(), GetAdjustedTime(), true); @@ -733,7 +851,7 @@ bool CCoinJoinServer::AddEntry(const CCoinJoinEntry& entry, PoolMessage& nMessag return false; } - if (static_cast(GetEntriesCountLocked()) >= vecSessionCollaterals.size()) { + if (static_cast(GetEntriesCountLocked()) >= m_session_collaterals.size()) { LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- ERROR: entries is full!\n", __func__); nMessageIDRet = ERR_ENTRIES_FULL; return false; @@ -872,10 +990,9 @@ bool CCoinJoinServer::AddEntry(const CCoinJoinEntry& entry, PoolMessage& nMessag bool CCoinJoinServer::AddScriptSig(const CTxIn& txinNew) { - AssertLockNotHeld(cs_coinjoin); + AssertLockHeld(cs_coinjoin); LogPrint(BCLog::COINJOIN, "CCoinJoinServer::AddScriptSig -- scriptSig=%s\n", ScriptToAsmStr(txinNew.scriptSig).substr(0, 24)); - LOCK(cs_coinjoin); for (const auto& entry : vecEntries) { if (std::ranges::any_of(entry.vecTxDSIn, [&txinNew](const auto& txdsin) { return txdsin.scriptSig == txinNew.scriptSig; })) { @@ -911,8 +1028,7 @@ bool CCoinJoinServer::AddScriptSig(const CTxIn& txinNew) // Check to make sure everything is signed bool CCoinJoinServer::IsSignaturesComplete() const { - AssertLockNotHeld(cs_coinjoin); - LOCK(cs_coinjoin); + AssertLockHeld(cs_coinjoin); return std::ranges::all_of(vecEntries, [](const auto& entry) { return std::ranges::all_of(entry.vecTxDSIn, [](const auto& txdsin) { return txdsin.fHasSig; }); @@ -938,15 +1054,6 @@ bool CCoinJoinServer::IsAcceptableDSA(const CCoinJoinAccept& dsa, PoolMessage& n return true; } -void CCoinJoinServer::CommitSessionCollateral(const CMutableTransaction& txCollateral) -{ - AssertLockHeld(cs_coinjoin); - vecSessionCollaterals.push_back(MakeTransactionRef(txCollateral)); - for (const auto& txin : txCollateral.vin) { - setSessionCollateralPrevouts.insert(txin.prevout); - } -} - //! Which side of the session denomination a participant will occupy, from its declared dsa //! direction. A participant that declares nothing is mixing 1:1 and occupies both sides. static CoinJoin::MixShape DeclaredShape(const CCoinJoinAccept& dsa) @@ -998,6 +1105,8 @@ bool CCoinJoinServer::CreateNewSession(const CCoinJoinAccept& dsa, int nPeerVers return false; } + int nDenom{0}; + size_t nParticipants{0}; { LOCK(cs_coinjoin); @@ -1008,6 +1117,11 @@ bool CCoinJoinServer::CreateNewSession(const CCoinJoinAccept& dsa, int nPeerVers nMessageIDRet = ERR_MODE; return false; } + if (SpendsCollateralBeingCharged(dsa.txCollateral)) { + LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CreateNewSession -- collateral is being charged\n"); + nMessageIDRet = ERR_INVALID_COLLATERAL; + return false; + } // start new session nMessageIDRet = MSG_NOERR; @@ -1019,21 +1133,25 @@ bool CCoinJoinServer::CreateNewSession(const CCoinJoinAccept& dsa, int nPeerVers SetState(POOL_STATE_QUEUE); - CommitSessionCollateral(dsa.txCollateral); + m_session_collaterals.Add(dsa.txCollateral); + nDenom = nSessionDenom; + nParticipants = m_session_collaterals.size(); } if (!fUnitTest) { //broadcast that I'm accepting entries, only if it's the first entry through - CCoinJoinQueue dsq(nSessionDenom, m_mn_activeman.GetOutPoint(), m_mn_activeman.GetProTxHash(), - GetAdjustedTime(), false); + CCoinJoinQueue dsq(nDenom, m_mn_activeman.GetOutPoint(), m_mn_activeman.GetProTxHash(), GetAdjustedTime(), false); LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CreateNewSession -- signing and relaying new queue: %s\n", dsq.ToString()); dsq.vchSig = m_mn_activeman.SignBasic(dsq.GetSignatureHash()); m_peer_manager->PeerRelayDSQ(dsq); m_queueman.AddQueue(std::move(dsq)); } - LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CreateNewSession -- new session created, nSessionID: %d nSessionDenom: %d (%s) vecSessionCollaterals.size(): %d CoinJoin::GetMaxPoolParticipants(): %d\n", - nSessionID, nSessionDenom, CoinJoin::DenominationToString(nSessionDenom), vecSessionCollaterals.size(), CoinJoin::GetMaxPoolParticipants()); + LogPrint(BCLog::COINJOIN, /* Continued */ + "CCoinJoinServer::CreateNewSession -- new session created, nSessionID: %d nSessionDenom: %d (%s) " + "participants: %d CoinJoin::GetMaxPoolParticipants(): %d\n", + nSessionID, nSessionDenom, CoinJoin::DenominationToString(nSessionDenom), nParticipants, + CoinJoin::GetMaxPoolParticipants()); return true; } @@ -1120,22 +1238,25 @@ bool CCoinJoinServer::AddUserToExistingSession(const CCoinJoinAccept& dsa, int n // IsSessionReady() can now hold a full session back waiting for a missing counterparty, so // the participant limit has to be enforced here rather than implied by session readiness - if (static_cast(vecSessionCollaterals.size()) >= CoinJoin::GetMaxPoolParticipants()) { + if (static_cast(m_session_collaterals.size()) >= CoinJoin::GetMaxPoolParticipants()) { LogPrint(BCLog::COINJOIN, "CCoinJoinServer::AddUserToExistingSession -- session is full\n"); nMessageIDRet = ERR_QUEUE_FULL; return false; } - // Session collaterals are only ever test-accepted, never added to the mempool, so nothing - // pins their identity: the same UTXO can be re-signed into arbitrarily many distinct txids. - // Match on input prevouts so a resent or replayed dsa cannot be counted as a new participant. - for (const auto& txin : dsa.txCollateral.vin) { - if (setSessionCollateralPrevouts.contains(txin.prevout)) { - LogPrint(BCLog::COINJOIN, "CCoinJoinServer::AddUserToExistingSession -- collateral %s spends prevout %s already committed to this session\n", - dsa.txCollateral.GetHash().ToString(), txin.prevout.ToStringShort()); - nMessageIDRet = ERR_ALREADY_HAVE; - return false; - } + // A resent or replayed dsa must not be counted as a new participant; see SessionCollaterals. + if (const auto prevout = m_session_collaterals.FindCommittedPrevout(dsa.txCollateral)) { + LogPrint(BCLog::COINJOIN, /* Continued */ + "CCoinJoinServer::AddUserToExistingSession -- collateral %s spends prevout %s already committed to " + "this session\n", + dsa.txCollateral.GetHash().ToString(), prevout->ToStringShort()); + nMessageIDRet = ERR_ALREADY_HAVE; + return false; + } + if (SpendsCollateralBeingCharged(dsa.txCollateral)) { + LogPrint(BCLog::COINJOIN, "CCoinJoinServer::AddUserToExistingSession -- collateral is being charged\n"); + nMessageIDRet = ERR_INVALID_COLLATERAL; + return false; } // count new user as accepted to an existing session @@ -1146,10 +1267,13 @@ bool CCoinJoinServer::AddUserToExistingSession(const CCoinJoinAccept& dsa, int n m_fRebalanceSession |= dsa.IsRebalance(); m_fHasLegacyParticipant |= nPeerVersion < COINJOIN_REBALANCE_VERSION; m_mapDeclaredShapes.emplace(dsa.txCollateral.GetHash(), DeclaredShape(dsa)); - CommitSessionCollateral(dsa.txCollateral); + m_session_collaterals.Add(dsa.txCollateral); - LogPrint(BCLog::COINJOIN, "CCoinJoinServer::AddUserToExistingSession -- new user accepted, nSessionID: %d nSessionDenom: %d (%s) vecSessionCollaterals.size(): %d CoinJoin::GetMaxPoolParticipants(): %d\n", - nSessionID, nSessionDenom, CoinJoin::DenominationToString(nSessionDenom), vecSessionCollaterals.size(), CoinJoin::GetMaxPoolParticipants()); + LogPrint(BCLog::COINJOIN, /* Continued */ + "CCoinJoinServer::AddUserToExistingSession -- new user accepted, nSessionID: %d nSessionDenom: %d (%s) " + "participants: %d CoinJoin::GetMaxPoolParticipants(): %d\n", + nSessionID, nSessionDenom, CoinJoin::DenominationToString(nSessionDenom), m_session_collaterals.size(), + CoinJoin::GetMaxPoolParticipants()); return true; } @@ -1157,15 +1281,17 @@ bool CCoinJoinServer::AddUserToExistingSession(const CCoinJoinAccept& dsa, int n // Returns true if either max size has been reached or if the mix timed out and min size was reached bool CCoinJoinServer::IsSessionReady() const { + AssertLockHeld(cs_coinjoin); + if (nState == POOL_STATE_QUEUE) { // PRIVACY: don't start mixing until each side of the session denomination is occupied // by nobody or by at least two participants. A session that never attracts the missing // counterparty simply expires in queue state, where no collateral is charged. if (!GetDeclaredSideCounts().IsCovered()) return false; - if (static_cast(vecSessionCollaterals.size()) >= CoinJoin::GetMaxPoolParticipants()) { + if (static_cast(m_session_collaterals.size()) >= CoinJoin::GetMaxPoolParticipants()) { return true; } - if (CCoinJoinServer::HasTimedOut() && static_cast(vecSessionCollaterals.size()) >= CoinJoin::GetMinPoolParticipants()) { + if (CCoinJoinServer::HasTimedOut() && static_cast(m_session_collaterals.size()) >= CoinJoin::GetMinPoolParticipants()) { return true; } } @@ -1239,30 +1365,49 @@ void CCoinJoinServer::RelayStatus(PoolStatusUpdate nStatusUpdate, PoolMessage nM } } -void CCoinJoinServer::RelayCompletedTransaction(PoolMessage nMessageID) +void CCoinJoinServer::RelayCompletedTransaction(int session_id, const std::vector& participants, + PoolMessage nMessageID) { AssertLockNotHeld(cs_coinjoin); - LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- nSessionID: %d nSessionDenom: %d (%s)\n", - __func__, nSessionID, nSessionDenom, CoinJoin::DenominationToString(nSessionDenom)); + LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- nSessionID: %d\n", __func__, session_id); - // final mixing tx with empty signatures should be relayed to mixing participants only - LOCK(cs_coinjoin); - for (const auto& entry : vecEntries) { - bool fOk = connman.ForNode(entry.addr, [&nMessageID, this](CNode* pnode) { + for (const auto& addr : participants) { + const bool fOk = connman.ForNode(addr, [&nMessageID, session_id, this](CNode* pnode) { CNetMsgMaker msgMaker(pnode->GetCommonVersion()); - connman.PushMessage(pnode, msgMaker.Make(NetMsgType::DSCOMPLETE, nSessionID.load(), nMessageID)); + connman.PushMessage(pnode, msgMaker.Make(NetMsgType::DSCOMPLETE, session_id, nMessageID)); return true; }); if (!fOk) { - // no such node? maybe client disconnected or our own connection went down - RelayStatus(STATUS_REJECTED); - break; + LogPrint(BCLog::COINJOIN, "CCoinJoinServer::%s -- participant disconnected before completion\n", __func__); } } } +std::vector CCoinJoinServer::GetParticipantAddrs() const +{ + AssertLockHeld(cs_coinjoin); + std::vector participants; + participants.reserve(vecEntries.size()); + std::ranges::transform(vecEntries, std::back_inserter(participants), [](const auto& entry) { return entry.addr; }); + return participants; +} + +void CCoinJoinServer::ResetSigningSessionIfCurrent(int session_id) +{ + AssertLockNotHeld(cs_coinjoin); + LOCK(cs_coinjoin); + if (nSessionID != session_id || nState != POOL_STATE_SIGNING) { + LogPrint(BCLog::COINJOIN, /* Continued */ + "CCoinJoinServer::%s -- signing session %d is no longer current, not resetting\n", __func__, session_id); + return; + } + SetNull(); +} + void CCoinJoinServer::SetState(PoolState nStateNew) { + AssertLockHeld(cs_coinjoin); + if (nStateNew == POOL_STATE_ERROR) { LogPrint(BCLog::COINJOIN, "CCoinJoinServer::SetState -- Can't set state to ERROR as a Masternode. \n"); return; diff --git a/src/coinjoin/server.h b/src/coinjoin/server.h index 475808c3052b..d5abb3d218b7 100644 --- a/src/coinjoin/server.h +++ b/src/coinjoin/server.h @@ -13,6 +13,7 @@ #include #include +#include #include class CActiveMasternodeManager; @@ -27,6 +28,27 @@ class CTxMemPool; class UniValue; +/** What the server's timeout check reads from a mixing session to decide whether to reset it */ +struct CoinJoinSessionProgress { + PoolState state{POOL_STATE_IDLE}; + bool timed_out{false}; + //! The queue has enough collaterals covering both sides to start accepting entries + bool queue_ready{false}; + size_t entries{0}; + size_t collaterals{0}; + //! The sides of the session denomination occupied by the submitted entries + CoinJoin::MixSideCounts sides{}; + bool signatures_complete{false}; +}; + +/** + * Should a session be reset for timing out? Not while it can still make the transition that + * CheckForCompleteQueue() or CheckPool() would make for it: start accepting entries, finalize + * (or, with an entry for every collateral but a side uncovered, reset with the participants + * told), or commit. + */ +bool ShouldResetTimedOutSession(const CoinJoinSessionProgress& session); + /** Used to keep track of current status of mixing pool */ class CCoinJoinServer : public CCoinJoinBaseSession, public NetHandler @@ -48,12 +70,47 @@ class CCoinJoinServer : public CCoinJoinBaseSession, public NetHandler // Session state and entry admission live in the protected section so unit tests can seed // and drive them through a test subclass. - // Mixing uses collateral transactions to trust parties entering the pool - // to behave honestly. If they don't it takes their money. - std::vector vecSessionCollaterals; - // Input prevouts of every transaction in vecSessionCollaterals, so a dsa whose collateral - // reuses one of them can be rejected without rescanning them all. - std::unordered_set setSessionCollateralPrevouts GUARDED_BY(cs_coinjoin); + /// The collateral transactions of every peer admitted to the current session. + /// + /// Mixing uses collateral transactions to trust parties entering the pool to behave + /// honestly. If they don't it takes their money. + /// + /// Session collaterals are only ever test-accepted, never added to the mempool, so nothing + /// pins their identity: the same UTXO can be re-signed into arbitrarily many distinct txids. + /// Matching on input prevouts is what makes a resent or replayed dsa recognisable as the + /// same participant. + class SessionCollaterals + { + public: + void Add(const CMutableTransaction& txCollateral) + { + m_txs.push_back(MakeTransactionRef(txCollateral)); + for (const auto& txin : txCollateral.vin) { + m_prevouts.insert(txin.prevout); + } + } + void Clear() + { + m_txs.clear(); + m_prevouts.clear(); + } + //! The first input of txCollateral that an already admitted collateral also spends, if any. + std::optional FindCommittedPrevout(const CMutableTransaction& txCollateral) const + { + for (const auto& txin : txCollateral.vin) { + if (m_prevouts.contains(txin.prevout)) return txin.prevout; + } + return std::nullopt; + } + const std::vector& txs() const { return m_txs; } + size_t size() const { return m_txs.size(); } + bool empty() const { return m_txs.empty(); } + + private: + std::vector m_txs; + std::unordered_set m_prevouts; + }; + SessionCollaterals m_session_collaterals GUARDED_BY(cs_coinjoin); // Post-V24: true once a participant has been admitted that declared a promotion/demotion, // i.e. the final transaction may come out unbalanced. Latched on admission rather than @@ -74,26 +131,45 @@ class CCoinJoinServer : public CCoinJoinBaseSession, public NetHandler /// Add a clients entry to the pool bool AddEntry(const CCoinJoinEntry& entry, PoolMessage& nMessageIDRet) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); - /// Record an accepted collateral and index its input prevouts - void CommitSessionCollateral(const CMutableTransaction& txCollateral) EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin); - /// Build and relay the final transaction if the live session is still eligible - void CreateFinalTransaction(int session_id) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); + /// Build and relay the final transaction if the live session is still eligible, charging one + /// missing participant first if charge_fees is set + void CreateFinalTransaction(int session_id, bool charge_fees) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); private: bool fUnitTest; + /// Serializes CheckPool() against itself and against CheckTimeout(). CheckPool() runs both on + /// the scheduler thread and on the message-handling thread, and its finalize and commit steps + /// have to be single-shot: relaying DSFINALTX twice makes every client sign twice, and the + /// duplicate signatures then abort the session for all of them. CheckTimeout() uses the same + /// guard so it cannot reset a session during finalization or commit. Always acquired with + /// TRY_LOCK, so a contended caller skips the round rather than blocking msghand. + Mutex cs_check_pool; + /// Add signature to a txin - bool AddScriptSig(const CTxIn& txin) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); + bool AddScriptSig(const CTxIn& txin) EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin); - /// Charge fees to bad actors (Charge clients a fee if they're abusive) - void ChargeFees() const EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); + /// Collaterals chosen to be charged whose consumption has not finished yet. They are chosen + /// under cs_coinjoin, but ConsumeCollateral() takes cs_main and runs after cs_coinjoin is + /// released, by which time the session may have been reset. Until then admission refuses any + /// collateral that spends one of their inputs, so a charged participant cannot be readmitted + /// into the next session with a collateral that is about to be spent. Survives SetNull(). + std::vector m_collaterals_being_charged GUARDED_BY(cs_coinjoin); + /// Does txCollateral spend an input of a collateral that is being charged? + bool SpendsCollateralBeingCharged(const CMutableTransaction& txCollateral) const EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin); + + /// Choose one bad actor whose collateral should be consumed, if any, and reserve it. The + /// result must be passed to ConsumeReservedCollateral(). + CTransactionRef SelectCollateralToCharge() EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin); /// Rarely charge fees to pay miners - void ChargeRandomFees() const EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); + void ChargeRandomFees(const std::vector& collaterals) const EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); /// Consume collateral in cases when peer misbehaved. Takes cs_main, which this class never /// takes under cs_coinjoin. void ConsumeCollateral(const CTransactionRef& txref) const EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); + /// Consume a collateral reserved in m_collaterals_being_charged, then release the reservation + void ConsumeReservedCollateral(const CTransactionRef& txref) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); /// Consume collateral, but only while session_id is still the live session holding it - void ConsumeCollateralIfCurrentSession(int session_id, const CTransactionRef& txref) const + void ConsumeCollateralIfCurrentSession(int session_id, const CTransactionRef& txref) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); /// Is session_id still the session we are accepting entries for? @@ -108,15 +184,22 @@ class CCoinJoinServer : public CCoinJoinBaseSession, public NetHandler size_t entries{0}; size_t collaterals{0}; CoinJoin::MixSideCounts sides; + bool signatures_complete{false}; }; PoolSnapshot GetPoolSnapshot() const EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); /// Is txref one of the collaterals accepted into the current session? bool HasSessionCollateral(const CTransactionRef& txref) const EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin); /// Check for process - void CheckPool(); + void CheckPool() EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin, !cs_check_pool); - void CommitFinalTransaction() EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); + void CommitFinalTransaction(int session_id) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); + /// Tell the given participants of session_id how it ended. Side-effect free: the participant + /// list is captured by the caller, so a reset or replacement session cannot be notified. + void RelayCompletedTransaction(int session_id, const std::vector& participants, PoolMessage nMessageID) + EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); + /// Reset the pool, but only while session_id is still the signing session being completed + void ResetSigningSessionIfCurrent(int session_id) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); /// Is this nDenom and txCollateral acceptable? bool IsAcceptableDSA(const CCoinJoinAccept& dsa, PoolMessage& nMessageIDRet) const; @@ -126,23 +209,27 @@ class CCoinJoinServer : public CCoinJoinBaseSession, public NetHandler bool IsSessionReady() const EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin); /// Check that all inputs are signed. (Are all inputs signed?) - bool IsSignaturesComplete() const EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); + bool IsSignaturesComplete() const EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin); /// Check to make sure a given input matches an input in the pool and its scriptSig is valid bool IsInputScriptSigValid(const CTxIn& txin) const EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin); - // Set the 'state' value, with some logging and capturing when the state changed - void SetState(PoolState nStateNew); + // Set the 'state' value, with some logging and capturing when the state changed. + // Requires cs_coinjoin so that a transition and the session data it describes are always + // observed together: code that revalidates nState under the lock must not have it changed + // out from under it by a concurrent transition. + void SetState(PoolState nStateNew) EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin); /// Relay mixing Messages void RelayFinalTransaction(const CTransaction& txFinal) EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin); void PushStatus(CNode& peer, PoolStatusUpdate nStatusUpdate, PoolMessage nMessageID) const; void RelayStatus(PoolStatusUpdate nStatusUpdate, PoolMessage nMessageID = MSG_NOERR) EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin); - void RelayCompletedTransaction(PoolMessage nMessageID) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); + /// Addresses of the participants that submitted an entry to the current session + std::vector GetParticipantAddrs() const EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin); void ProcessDSACCEPT(CNode& peer, CDataStream& vRecv) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); void ProcessDSQUEUE(NodeId from, CDataStream& vRecv); - void ProcessDSVIN(CNode& peer, CDataStream& vRecv) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); - void ProcessDSSIGNFINALTX(CNode& peer, CDataStream& vRecv) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); + void ProcessDSVIN(CNode& peer, CDataStream& vRecv) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin, !cs_check_pool); + void ProcessDSSIGNFINALTX(CNode& peer, CDataStream& vRecv) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin, !cs_check_pool); void SetNull() override EXCLUSIVE_LOCKS_REQUIRED(cs_coinjoin); @@ -156,14 +243,15 @@ class CCoinJoinServer : public CCoinJoinBaseSession, public NetHandler const CMasternodeSync& mn_sync, const llmq::CInstantSendManager& isman); ~CCoinJoinServer() override; - void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv) override; + void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv) override + EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin, !cs_check_pool); bool ProcessGetData(CNode& pfrom, const CInv& inv, const CNetMsgMaker& msgMaker) override; bool AlreadyHave(const CInv& inv) override; void Schedule(CScheduler& scheduler) override; bool HasTimedOut() const; - void CheckTimeout(); - void CheckForCompleteQueue(); + void CheckTimeout() EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin, !cs_check_pool); + void CheckForCompleteQueue() EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin); void GetJsonInfo(UniValue& obj) const; }; diff --git a/src/test/coinjoin_inouts_tests.cpp b/src/test/coinjoin_inouts_tests.cpp index 0bbd76b6a793..c5dea2930c42 100644 --- a/src/test/coinjoin_inouts_tests.cpp +++ b/src/test/coinjoin_inouts_tests.cpp @@ -175,8 +175,7 @@ BOOST_AUTO_TEST_CASE(entry_addscriptsig_matches_and_rejects) } } -// Test-only subclass exposing the minimal seams needed to observe how -// ProcessDSSIGNFINALTX treats messages from participants vs. non-participants +// Test-only subclass exposing the minimal seams needed to put the server into a session state // without standing up a full DKG-backed signing session. class TestableCoinJoinServer : public CCoinJoinServer { @@ -203,7 +202,7 @@ class TestableCoinJoinServer : public CCoinJoinServer { LOCK(cs_coinjoin); m_mapDeclaredShapes.emplace(txCollateral.GetHash(), shape); - CommitSessionCollateral(txCollateral); + m_session_collaterals.Add(txCollateral); } void SeedEntry(CCoinJoinEntry entry) EXCLUSIVE_LOCKS_REQUIRED(!cs_coinjoin) @@ -457,15 +456,67 @@ BOOST_AUTO_TEST_CASE(server_finalization_rechecks_live_side_coverage) server.EnterAcceptingEntriesState(); // A timeout snapshot could have observed only the three demotions as covered (0/3), then - // this first promotion could commit while ChargeFees() ran. Finalization must use the live - // 1/3 side counts and refuse to build the uncovered transaction, staying out of + // this first promotion could commit before finalization took the lock. Finalization must use + // the live 1/3 side counts and refuse to build the uncovered transaction, staying out of // POOL_STATE_SIGNING; the still-timed-out session is then reset by the scheduler's // regular CheckTimeout() pass instead of leaking a lone promoter on-chain. - server.CreateFinalTransaction(/*session_id=*/1); + server.CreateFinalTransaction(/*session_id=*/1, /*charge_fees=*/true); BOOST_CHECK_EQUAL(server.GetState(), int{POOL_STATE_ACCEPTING_ENTRIES}); BOOST_CHECK_EQUAL(server.GetEntriesCount(), 4); } +BOOST_AUTO_TEST_CASE(server_timeout_resets_only_sessions_that_cannot_advance) +{ + // The final collateral, entry or signature can arrive after the scheduler's own CheckPool() + // snapshot. A timed-out session that can still advance must be left for the next tick + // instead of being discarded, and one that cannot must still be reset. + const size_t min{static_cast(CoinJoin::GetMinPoolParticipants())}; + const CoinJoin::MixSideCounts covered{.inputs = static_cast(min), .outputs = static_cast(min)}; + // One promoter and min - 1 demoters: the promoter would hold the only session-denom input + const CoinJoin::MixSideCounts lone_promoter{.inputs = 1, .outputs = static_cast(min) - 1}; + struct Case { + std::string name; + CoinJoinSessionProgress session; + bool reset; + }; + const std::vector cases{ + {"not timed out", + {.state = POOL_STATE_SIGNING, .timed_out = false, .entries = min, .collaterals = min, .sides = covered}, + false}, + {"ready queue", {.state = POOL_STATE_QUEUE, .timed_out = true, .queue_ready = true, .collaterals = min}, false}, + {"queue not ready", {.state = POOL_STATE_QUEUE, .timed_out = true, .collaterals = min - 1}, true}, + {"every entry in, a side uncovered", + {.state = POOL_STATE_ACCEPTING_ENTRIES, .timed_out = true, .entries = min, .collaterals = min, + .sides = lone_promoter}, + false}, + {"enough covered entries, one straggler", + {.state = POOL_STATE_ACCEPTING_ENTRIES, .timed_out = true, .entries = min, .collaterals = min + 1, + .sides = covered}, + false}, + {"too few entries", + {.state = POOL_STATE_ACCEPTING_ENTRIES, .timed_out = true, .entries = min - 1, .collaterals = min, + .sides = covered}, + true}, + {"enough entries, a side uncovered, one straggler", + {.state = POOL_STATE_ACCEPTING_ENTRIES, .timed_out = true, .entries = min, .collaterals = min + 1, + .sides = lone_promoter}, + true}, + {"every input signed", + {.state = POOL_STATE_SIGNING, .timed_out = true, .entries = min, .collaterals = min, + .sides = covered, .signatures_complete = true}, + false}, + {"a signature missing", + {.state = POOL_STATE_SIGNING, .timed_out = true, .entries = min, .collaterals = min, .sides = covered}, + true}, + }; + for (const auto& [name, session, reset] : cases) { + BOOST_TEST_CONTEXT(name) + { + BOOST_CHECK_EQUAL(ShouldResetTimedOutSession(session), reset); + } + } +} + BOOST_AUTO_TEST_CASE(entry_deserializes_vectors_through_wire_cap) { const size_t wire_cap{CoinJoin::GetMaxPoolInputOutputCount()};