diff --git a/src/coinjoin/server.cpp b/src/coinjoin/server.cpp index 379d46069732..b0fe77f72d5a 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); @@ -546,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()) { @@ -581,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()); @@ -1278,17 +1283,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); } } @@ -1328,11 +1332,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 8c8def30154a..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); @@ -518,6 +521,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, @@ -739,6 +751,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()};