From 6ef72d5bae187ddf8f5dd7954b9d86fcc2e438cd Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 13 Nov 2026 00:13:45 -0600 Subject: [PATCH 1/3] fix(coinjoin): reject only the sender of an invalid signature When a DSSIGNFINALTX input failed AddScriptSig(), the masternode relayed STATUS_REJECTED to every participant of the session. Each client that receives it enters POOL_STATE_ERROR, releases its inputs and stops signing, but the server keeps the session until its signing timeout. There, the clients that obeyed the rejection are the non-signers, and one of them can be charged its collateral, while the participant that sent the bad signature may have signed everything and is not an offender at all. So after signing all of its own inputs, a participant could send an invalid signature, or one for an input it does not own, to abort the session for everyone, and an honest peer could be charged for it. A signature that fails validation comes from the sender, so reject only the sender and keep the session for everyone else. If the sender never supplies a valid signature its input stays unsigned, and the timeout treats it as the non-signer, under the existing probabilistic policy. Co-Authored-By: Claude Opus 5.5 Co-Authored-By: Claude Fable 5.2 --- src/coinjoin/server.cpp | 5 ++++- src/test/coinjoin_inouts_tests.cpp | 9 +++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/coinjoin/server.cpp b/src/coinjoin/server.cpp index 379d46069732..37db34899b8f 100644 --- a/src/coinjoin/server.cpp +++ b/src/coinjoin/server.cpp @@ -288,7 +288,10 @@ void CCoinJoinServer::ProcessDSSIGNFINALTX(CNode& peer, CDataStream& vRecv) if (!AddScriptSig(txin, peer.addr)) { LogPrint(BCLog::COINJOIN, "DSSIGNFINALTX -- AddScriptSig() failed at %d/%d, session: %d\n", nTxInIndex, nTxInsCount, session_id); - RelayStatus(STATUS_REJECTED); + // Only the sender is at fault, so only the sender is rejected. A session-wide + // rejection would let one participant abort the session for everyone, and those + // that obeyed it would be counted as non-signers at the timeout. + PushStatus(peer, STATUS_REJECTED, ERR_INVALID_INPUT); return; } LogPrint(BCLog::COINJOIN, "DSSIGNFINALTX -- AddScriptSig() %d/%d success\n", nTxInIndex, nTxInsCount); diff --git a/src/test/coinjoin_inouts_tests.cpp b/src/test/coinjoin_inouts_tests.cpp index 8c8def30154a..2dc3a64b93ee 100644 --- a/src/test/coinjoin_inouts_tests.cpp +++ b/src/test/coinjoin_inouts_tests.cpp @@ -518,6 +518,15 @@ BOOST_AUTO_TEST_CASE(server_signfinaltx_participant_oversized_count_is_rejected_ BOOST_CHECK_EQUAL(server.GetState(), int{POOL_STATE_SIGNING}); BOOST_CHECK_EQUAL(server.GetEntriesCount(), 1); + // Nor does a signature that fails validation. The participant is not connected, so a + // session-wide rejection would find it unreachable and reset the session. + CDataStream invalid_stream{SER_NETWORK, PROTOCOL_VERSION}; + invalid_stream << std::vector{CTxIn{COutPoint{uint256::ONE, 0}}}; + + BOOST_CHECK_NO_THROW(server.ProcessMessage(*participant, NetMsgType::DSSIGNFINALTX, invalid_stream)); + BOOST_CHECK_EQUAL(server.GetState(), int{POOL_STATE_SIGNING}); + BOOST_CHECK_EQUAL(server.GetEntriesCount(), 1); + // A count beyond the generic CompactSize cap is a malformed message, not a // CoinJoin-level violation: it throws the standard deserialization error out // to net processing. No txin is materialized and the session is left intact, From 388a52eecc167e809d092c94572c9a3935940089 Mon Sep 17 00:00:00 2001 From: pasta Date: Tue, 13 Oct 2026 13:15:03 -0500 Subject: [PATCH 2/3] fix(coinjoin): end a session once a participant is unreachable When the masternode cannot deliver the final transaction or a status update to one of the participants, RelayStatus() tells every other participant that the session is terminated. Each client that receives that enters POOL_STATE_ERROR, releases its inputs and stops, so the session can no longer complete. The server only reset the session if *every* participant was unreachable. Otherwise it kept the session until its timeout, where the clients that had obeyed were counted as participants that never sent or signed, and one of them could be charged its collateral. That charged honest participants for a failure the masternode cannot attribute to them: an unreachable peer is just as likely to be the masternode's own connection going down. Once the termination has been relayed, reset the session immediately so its timeout cannot charge anyone. A participant that never sends or signs while everyone is still reachable is handled exactly as before. RelayFinalTransaction() now finishes iterating vecEntries before calling RelayStatus(), which can now clear it. Co-Authored-By: Claude Opus 5.5 --- src/coinjoin/server.cpp | 23 ++++++++--------- src/test/coinjoin_inouts_tests.cpp | 41 ++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/src/coinjoin/server.cpp b/src/coinjoin/server.cpp index 37db34899b8f..0ba0336421ae 100644 --- a/src/coinjoin/server.cpp +++ b/src/coinjoin/server.cpp @@ -1281,17 +1281,16 @@ void CCoinJoinServer::RelayFinalTransaction(const CTransaction& txFinal) __func__, nSessionID, nSessionDenom, CoinJoin::DenominationToString(nSessionDenom)); // final mixing tx with empty signatures should be relayed to mixing participants only - for (const auto& entry : vecEntries) { - bool fOk = connman.ForNode(entry.addr, [&txFinal, this](CNode* pnode) { + const bool all_reached = std::ranges::all_of(vecEntries, [&txFinal, this](const auto& entry) { + return connman.ForNode(entry.addr, [&txFinal, this](CNode* pnode) { CNetMsgMaker msgMaker(pnode->GetCommonVersion()); connman.PushMessage(pnode, msgMaker.Make(NetMsgType::DSFINALTX, nSessionID.load(), txFinal)); return true; }); - if (!fOk) { - // no such node? maybe this client disconnected or our own connection went down - RelayStatus(STATUS_REJECTED); - break; - } + }); + if (!all_reached) { + // no such node? maybe this client disconnected or our own connection went down + RelayStatus(STATUS_REJECTED); } } @@ -1331,11 +1330,11 @@ void CCoinJoinServer::RelayStatus(PoolStatusUpdate nStatusUpdate, PoolMessage nM }); } - if (nDisconnected == vecEntries.size()) { - // all clients disconnected, there is probably some issues with our own connection - // do not charge any fees, just reset the pool - SetNull(); - } + // The participants told to terminate release their inputs and stop, so the session can no + // longer complete. End it now without charging anyone: at the timeout they would look like + // participants that never sent or signed, and a peer we cannot reach is no evidence of + // misbehavior - our own connection may be the one that failed. + SetNull(); } void CCoinJoinServer::RelayCompletedTransaction(int session_id, const std::vector& participants, diff --git a/src/test/coinjoin_inouts_tests.cpp b/src/test/coinjoin_inouts_tests.cpp index 2dc3a64b93ee..ff5aff02ec1a 100644 --- a/src/test/coinjoin_inouts_tests.cpp +++ b/src/test/coinjoin_inouts_tests.cpp @@ -748,6 +748,47 @@ BOOST_AUTO_TEST_CASE(server_timeout_resets_only_sessions_that_cannot_advance) } } +BOOST_AUTO_TEST_CASE(server_unreachable_participant_ends_session) +{ + CActiveMasternodeManager mn_activeman(*Assert(m_node.connman), *Assert(m_node.dmnman), MakeSecretKey()); + auto& connman = static_cast(*m_node.connman); + + // Every participant has submitted an entry, so the final transaction is relayed to them. If + // one of them can no longer be reached, the others are told to abandon the session; they + // cannot be charged at its timeout for obeying, so it must end right away. If everyone can be + // reached, it moves on to signing. + for (const bool all_reachable : {true, false}) { + BOOST_TEST_CONTEXT("all_reachable=" << all_reachable) + { + TestableCoinJoinServer server(m_node.peerman.get(), *Assert(m_node.chainman), *Assert(m_node.connman), + *Assert(m_node.dmnman), *Assert(m_node.dstxman), + *Assert(m_node.mn_metaman), *Assert(m_node.mempool), mn_activeman, + *Assert(m_node.mn_sync), *Assert(m_node.isman)); + for (int i = 0; i < CoinJoin::GetMinPoolParticipants(); ++i) { + auto peer = MakePeer(/*id=*/i, /*ipv4=*/0x0a000001 + i); + CMutableTransaction collateral; + collateral.vin.emplace_back(COutPoint{uint256::ONE, static_cast(i)}); + server.SeedSessionCollateral(collateral, CoinJoin::MixShape::STANDARD); + CCoinJoinEntry entry{{CTxDSIn{CTxIn{COutPoint{uint256::TWO, static_cast(i)}}, P2PKHScript(), 0}}, + {CTxOut{CoinJoin::GetSmallestDenomination(), P2PKHScript()}}, + CTransaction{collateral}}; + entry.addr = peer->addr; + server.SeedEntry(std::move(entry)); + if (all_reachable || i == 0) { + peer->fSuccessfullyConnected = true; + connman.AddTestNode(*peer.release()); + } + } + server.EnterAcceptingEntriesState(); + + server.CreateFinalTransaction(/*session_id=*/1, /*charge_fees=*/false); + BOOST_CHECK_EQUAL(server.GetState(), all_reachable ? int{POOL_STATE_SIGNING} : int{POOL_STATE_IDLE}); + + connman.ClearTestNodes(); + } + } +} + BOOST_AUTO_TEST_CASE(entry_deserializes_vectors_through_wire_cap) { const size_t wire_cap{CoinJoin::GetMaxPoolInputOutputCount()}; From b3f7788f9cc471d7d9f2f0604589e5faf31b8919 Mon Sep 17 00:00:00 2001 From: pasta Date: Fri, 13 Nov 2026 00:14:49 -0600 Subject: [PATCH 3/3] fix(coinjoin): measure signing offenders against the participants asked to sign At the signing timeout, SelectCollateralToCharge() compares the offender count with the number of session collaterals to decide whether almost everyone failed (charge only sometimes) or everyone failed (charge nobody, since that points at the masternode rather than the participants). A session finalized on timeout moves to signing with only the participants that sent an entry, but keeps the collaterals of those that did not. Compared with all of them, a session in which every participant asked to sign failed did not look like "everyone failed", and one of them could be charged. While signing, measure the offenders against the participants that were asked to sign rather than against every session collateral. For the same reason the "almost everyone failed" threshold is now one below the number of signers, not one below the number of collaterals. While entries are being accepted the total is still every collateral. Co-Authored-By: Claude Opus 5.5 Co-Authored-By: Claude Fable 5.2 --- src/coinjoin/server.cpp | 8 +++++--- src/test/coinjoin_inouts_tests.cpp | 11 +++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/coinjoin/server.cpp b/src/coinjoin/server.cpp index 0ba0336421ae..b0fe77f72d5a 100644 --- a/src/coinjoin/server.cpp +++ b/src/coinjoin/server.cpp @@ -549,7 +549,9 @@ CTransactionRef CCoinJoinServer::SelectCollateralToCharge() std::vector vecOffendersCollaterals; const PoolState state{nState}; - const size_t nSessionCollaterals{m_session_collaterals.size()}; + // Only the participants that sent an entry are asked to sign. A session finalized on timeout + // still holds the collaterals of those that did not. + const size_t nParticipants{state == POOL_STATE_SIGNING ? vecEntries.size() : m_session_collaterals.size()}; if (state == POOL_STATE_ACCEPTING_ENTRIES) { for (const auto& txCollateral : m_session_collaterals.txs()) { @@ -584,10 +586,10 @@ CTransactionRef CCoinJoinServer::SelectCollateralToCharge() if (vecOffendersCollaterals.empty()) return {}; //mostly offending? Charge sometimes - if (vecOffendersCollaterals.size() >= nSessionCollaterals - 1 && GetRand(/*nMax=*/100) > 33) return {}; + if (vecOffendersCollaterals.size() >= nParticipants - 1 && GetRand(/*nMax=*/100) > 33) return {}; //everyone is an offender? That's not right - if (vecOffendersCollaterals.size() >= nSessionCollaterals) return {}; + if (vecOffendersCollaterals.size() >= nParticipants) return {}; //charge one of the offenders randomly Shuffle(vecOffendersCollaterals.begin(), vecOffendersCollaterals.end(), FastRandomContext()); diff --git a/src/test/coinjoin_inouts_tests.cpp b/src/test/coinjoin_inouts_tests.cpp index ff5aff02ec1a..582c8f3c901d 100644 --- a/src/test/coinjoin_inouts_tests.cpp +++ b/src/test/coinjoin_inouts_tests.cpp @@ -42,6 +42,7 @@ #include #include #include +#include #include #include @@ -267,15 +268,16 @@ BOOST_AUTO_TEST_CASE(server_signing_timeout_charges_each_offender_once) const CScript previous_script{GetScriptForDestination(PKHash(key.GetPubKey()))}; // One missing signature, several missing signatures from the same participant, - // no offenders, and every participant offending. + // no offenders, every participant offending, and every participant that was asked + // to sign offending in a session finalized on timeout without one participant's entry. uint32_t collateral_index{0}; - const std::array, 4> scenarios{{{1, 1}, {3, 1}, {0, 0}, {3, 3}}}; - for (const auto& [unsigned_inputs, offenders] : scenarios) { + const std::array, 5> scenarios{{{1, 1, 3}, {3, 1, 3}, {0, 0, 3}, {3, 3, 3}, {3, 3, 4}}}; + for (const auto& [unsigned_inputs, offenders, session_collaterals] : scenarios) { TestableCoinJoinServer server(m_node.peerman.get(), *Assert(m_node.chainman), connman, *Assert(m_node.dmnman), *Assert(m_node.dstxman), *Assert(m_node.mn_metaman), *Assert(m_node.mempool), mn_activeman, *Assert(m_node.mn_sync), *Assert(m_node.isman)); std::vector collaterals; - for (int participant{0}; participant < 3; ++participant) { + for (int participant{0}; participant < session_collaterals; ++participant) { const uint32_t index{collateral_index++}; const COutPoint collateral_input{uint256::TWO, index}; { @@ -290,6 +292,7 @@ BOOST_AUTO_TEST_CASE(server_signing_timeout_charges_each_offender_once) BOOST_REQUIRE(SignSignature(provider, previous_script, collateral, 0, COIN / 10, SIGHASH_ALL, empty)); collaterals.push_back(collateral.GetHash()); server.SeedSessionCollateral(collateral, CoinJoin::MixShape::STANDARD); + if (participant >= 3) continue; // never sent an entry CCoinJoinEntry entry; entry.txCollateral = MakeTransactionRef(collateral);