Skip to content

Commit

Permalink
fix: respect SENDDSQUEUE message, move DSQ relay into net processing …
Browse files Browse the repository at this point in the history
…/ peerman

in 6148, I broke the functionality where a peer must opt in / opt out of DSQUEUE messages. This was mostly ok, and not immediately detected, as with this bug, simply everyone would receive DSQ messages over inventory (or classically, old proto versions were not affected by this bug). But this still would result in quite a bit of wasted bandwidth for peers which may not care about DSQ at all.

This commit should restore the prior functionality, where a node should send the SENDDSQUEUE message if they wish to receive DSQs. Once they've sent that, depending on their protocol version, they will either have the messages pushed to them as available, or on modern protocols, they will thereafter receive DSQs over the inventory system.

NOTE: I also refactor the code in this commit, moving some network proccessing into.... wait for it... net_processing.cpp! This allowed us to remove some dependencies in coinjoin.h. DSQ messages are now relayed to peers by calling peer_manager.RelayDSQ
  • Loading branch information
PastaPastaPasta committed Nov 23, 2024
1 parent f9d044d commit dafa736
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/coinjoin/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ PeerMsgRet CCoinJoinClientQueueManager::ProcessDSQueue(const CNode& peer, CDataS
WITH_LOCK(cs_vecqueue, vecCoinJoinQueue.push_back(dsq));
}
} // cs_ProcessDSQueue
dsq.Relay(connman, *peerman);
peerman->RelayDSQ(dsq);
return {};
}

Expand Down
1 change: 1 addition & 0 deletions src/coinjoin/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CMasternodeSync;
class CNode;
class CoinJoinWalletManager;
class CTxMemPool;
class PeerManager;

class UniValue;

Expand Down
14 changes: 0 additions & 14 deletions src/coinjoin/coinjoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <masternode/node.h>
#include <masternode/sync.h>
#include <messagesigner.h>
#include <net_processing.h>
#include <netmessagemaker.h>
#include <txmempool.h>
#include <util/moneystr.h>
Expand Down Expand Up @@ -71,19 +70,6 @@ bool CCoinJoinQueue::CheckSignature(const CBLSPublicKey& blsPubKey) const
return true;
}

bool CCoinJoinQueue::Relay(CConnman& connman, PeerManager& peerman)
{
CInv inv(MSG_DSQ, GetHash());
peerman.RelayInv(inv, DSQ_INV_VERSION);
connman.ForEachNode([&connman, this](CNode* pnode) {
CNetMsgMaker msgMaker(pnode->GetCommonVersion());
if (pnode->fSendDSQueue && pnode->nVersion < DSQ_INV_VERSION) {
connman.PushMessage(pnode, msgMaker.Make(NetMsgType::DSQUEUE, (*this)));
}
});
return true;
}

bool CCoinJoinQueue::IsTimeOutOfBounds(int64_t current_time) const
{
return current_time - nTime > COINJOIN_QUEUE_TIMEOUT ||
Expand Down
4 changes: 0 additions & 4 deletions src/coinjoin/coinjoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@

class CActiveMasternodeManager;
class CChainState;
class CConnman;
class CBLSPublicKey;
class CBlockIndex;
class ChainstateManager;
class CMasternodeSync;
class CTxMemPool;
class TxValidationState;
class PeerManager;

namespace llmq {
class CChainLocksHandler;
Expand Down Expand Up @@ -221,8 +219,6 @@ class CCoinJoinQueue
/// Check if we have a valid Masternode address
[[nodiscard]] bool CheckSignature(const CBLSPublicKey& blsPubKey) const;

bool Relay(CConnman& connman, PeerManager& peerman);

/// Check if a queue is too old or too far into the future
[[nodiscard]] bool IsTimeOutOfBounds(int64_t current_time = GetAdjustedTime()) const;

Expand Down
6 changes: 3 additions & 3 deletions src/coinjoin/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ PeerMsgRet CCoinJoinServer::ProcessDSQUEUE(const CNode& peer, CDataStream& vRecv
TRY_LOCK(cs_vecqueue, lockRecv);
if (!lockRecv) return {};
vecCoinJoinQueue.push_back(dsq);
dsq.Relay(connman, *m_peerman);
m_peerman->RelayDSQ(dsq);
}
return {};
}
Expand Down Expand Up @@ -519,7 +519,7 @@ void CCoinJoinServer::CheckForCompleteQueue()
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CheckForCompleteQueue -- queue is ready, signing and relaying (%s) " /* Continued */
"with %d participants\n", dsq.ToString(), vecSessionCollaterals.size());
dsq.Sign(*m_mn_activeman);
dsq.Relay(connman, *m_peerman);
m_peerman->RelayDSQ(dsq);
}
}

Expand Down Expand Up @@ -732,7 +732,7 @@ bool CCoinJoinServer::CreateNewSession(const CCoinJoinAccept& dsa, PoolMessage&
GetAdjustedTime(), false);
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CreateNewSession -- signing and relaying new queue: %s\n", dsq.ToString());
dsq.Sign(*m_mn_activeman);
dsq.Relay(connman, *m_peerman);
m_peerman->RelayDSQ(dsq);
LOCK(cs_vecqueue);
vecCoinJoinQueue.push_back(dsq);
}
Expand Down
1 change: 1 addition & 0 deletions src/coinjoin/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class CActiveMasternodeManager;
class CCoinJoinServer;
class CConnman;
class CDataStream;
class CDeterministicMNManager;
class CDSTXManager;
Expand Down
46 changes: 45 additions & 1 deletion src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,15 @@ struct Peer {
/** Whether the peer has signaled support for receiving ADDRv2 (BIP155)
* messages, indicating a preference to receive ADDRv2 instead of ADDR ones. */
std::atomic_bool m_wants_addrv2{false};

enum class WantsDSQ {
NONE, // Peer doesn't want DSQs
INV, // Peer will be notified of DSQs over Inventory System (see: DSQ_INV_VERSION)
ALL, // Peer will be notified of all DSQs, by simply sending them the DSQ
};

std::atomic<WantsDSQ> m_wants_dsq{WantsDSQ::NONE};

/** Whether this peer has already sent us a getaddr message. */
bool m_getaddr_recvd GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false};
/** Number of addresses that can be processed from this peer. Start at 1 to
Expand Down Expand Up @@ -610,6 +619,7 @@ class PeerManagerImpl final : public PeerManager
void RelayInvFiltered(CInv &inv, const uint256 &relatedTxHash, const int minProtoVersion) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
void RelayTransaction(const uint256& txid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
void RelayRecoveredSig(const uint256& sigHash) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
void RelayDSQ(const CCoinJoinQueue& queue) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
void SetBestHeight(int height) override { m_best_height = height; };
void Misbehaving(const NodeId pnode, const int howmuch, const std::string& message = "") override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex);
void ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRecv,
Expand Down Expand Up @@ -2279,6 +2289,34 @@ void PeerManagerImpl::RelayInv(CInv &inv, const int minProtoVersion)
});
}

void PeerManagerImpl::RelayDSQ(const CCoinJoinQueue& queue)
{
CInv inv{MSG_DSQ, queue.GetHash()};
std::vector<NodeId> nodes_send_all;
{
LOCK(m_peer_mutex);
for (const auto& [nodeid, peer] : m_peer_map) {
switch (peer->m_wants_dsq) {
case Peer::WantsDSQ::NONE:
break;
case Peer::WantsDSQ::INV:
PushInv(*peer, inv);
break;
case Peer::WantsDSQ::ALL:
nodes_send_all.push_back(nodeid);
break;
}
}
}
for (auto nodeId : nodes_send_all) {
m_connman.ForNode(nodeId, [&](CNode* pnode) -> bool {
CNetMsgMaker msgMaker(pnode->GetCommonVersion());
m_connman.PushMessage(pnode, msgMaker.Make(NetMsgType::DSQUEUE, queue));
return true;
});
}
}

void PeerManagerImpl::RelayInvFiltered(CInv &inv, const CTransaction& relatedTx, const int minProtoVersion)
{
// TODO: Migrate to iteration through m_peer_map
Expand Down Expand Up @@ -3954,7 +3992,13 @@ void PeerManagerImpl::ProcessMessage(
{
bool b;
vRecv >> b;
pfrom.fSendDSQueue = b;
if (!b) {
peer->m_wants_dsq = Peer::WantsDSQ::NONE;
} else if (pfrom.GetCommonVersion() < DSQ_INV_VERSION) {
peer->m_wants_dsq = Peer::WantsDSQ::ALL;
} else {
peer->m_wants_dsq = Peer::WantsDSQ::INV;
}
return;
}

Expand Down
4 changes: 4 additions & 0 deletions src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class CActiveMasternodeManager;
class AddrMan;
class CTxMemPool;
class CCoinJoinQueue;
class CDeterministicMNManager;
class CMasternodeMetaMan;
class CMasternodeSync;
Expand Down Expand Up @@ -93,6 +94,9 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
/** Broadcast inventory message to a specific peer. */
virtual void PushInventory(NodeId nodeid, const CInv& inv) = 0;

/** Relay DSQ based on peer preference */
virtual void RelayDSQ(const CCoinJoinQueue& queue) = 0;

/** Relay inventories to all peers */
virtual void RelayInv(CInv &inv, const int minProtoVersion = MIN_PEER_PROTO_VERSION) = 0;
virtual void RelayInvFiltered(CInv &inv, const CTransaction &relatedTx,
Expand Down

0 comments on commit dafa736

Please sign in to comment.