refactor: coinjoin/server no more depends on net_processing

This commit is contained in:
Konstantin Akimov 2023-12-18 18:25:54 +07:00 committed by pasta
parent 1681eb8f3a
commit 91eca516e2
No known key found for this signature in database
GPG Key ID: 52527BEDABE87984
3 changed files with 26 additions and 24 deletions

View File

@ -10,10 +10,11 @@
#include <masternode/meta.h> #include <masternode/meta.h>
#include <masternode/node.h> #include <masternode/node.h>
#include <masternode/sync.h> #include <masternode/sync.h>
#include <net_processing.h> #include <net.h>
#include <netmessagemaker.h> #include <netmessagemaker.h>
#include <script/interpreter.h> #include <script/interpreter.h>
#include <shutdown.h> #include <shutdown.h>
#include <streams.h>
#include <txmempool.h> #include <txmempool.h>
#include <util/moneystr.h> #include <util/moneystr.h>
#include <util/ranges.h> #include <util/ranges.h>
@ -25,20 +26,21 @@
constexpr static CAmount DEFAULT_MAX_RAW_TX_FEE{COIN / 10}; constexpr static CAmount DEFAULT_MAX_RAW_TX_FEE{COIN / 10};
void CCoinJoinServer::ProcessMessage(CNode& peer, PeerManager& peerman, std::string_view msg_type, CDataStream& vRecv) PeerMsgRet CCoinJoinServer::ProcessMessage(CNode& peer, std::string_view msg_type, CDataStream& vRecv)
{ {
if (!fMasternodeMode) return; if (!fMasternodeMode) return {};
if (!m_mn_sync.IsBlockchainSynced()) return; if (!m_mn_sync.IsBlockchainSynced()) return {};
if (msg_type == NetMsgType::DSACCEPT) { if (msg_type == NetMsgType::DSACCEPT) {
ProcessDSACCEPT(peer, vRecv); ProcessDSACCEPT(peer, vRecv);
} else if (msg_type == NetMsgType::DSQUEUE) { } else if (msg_type == NetMsgType::DSQUEUE) {
ProcessDSQUEUE(peer, peerman, vRecv); return ProcessDSQUEUE(peer, vRecv);
} else if (msg_type == NetMsgType::DSVIN) { } else if (msg_type == NetMsgType::DSVIN) {
ProcessDSVIN(peer, vRecv); ProcessDSVIN(peer, vRecv);
} else if (msg_type == NetMsgType::DSSIGNFINALTX) { } else if (msg_type == NetMsgType::DSSIGNFINALTX) {
ProcessDSSIGNFINALTX(vRecv); ProcessDSSIGNFINALTX(vRecv);
} }
return {};
} }
void CCoinJoinServer::ProcessDSACCEPT(CNode& peer, CDataStream& vRecv) void CCoinJoinServer::ProcessDSACCEPT(CNode& peer, CDataStream& vRecv)
@ -106,14 +108,13 @@ void CCoinJoinServer::ProcessDSACCEPT(CNode& peer, CDataStream& vRecv)
} }
} }
void CCoinJoinServer::ProcessDSQUEUE(const CNode& peer, PeerManager& peerman, CDataStream& vRecv) PeerMsgRet CCoinJoinServer::ProcessDSQUEUE(const CNode& peer, CDataStream& vRecv)
{ {
CCoinJoinQueue dsq; CCoinJoinQueue dsq;
vRecv >> dsq; vRecv >> dsq;
if (dsq.masternodeOutpoint.IsNull() && dsq.m_protxHash.IsNull()) { if (dsq.masternodeOutpoint.IsNull() && dsq.m_protxHash.IsNull()) {
peerman.Misbehaving(peer.GetId(), 100); return tl::unexpected{100};
return;
} }
if (dsq.masternodeOutpoint.IsNull()) { if (dsq.masternodeOutpoint.IsNull()) {
@ -121,43 +122,41 @@ void CCoinJoinServer::ProcessDSQUEUE(const CNode& peer, PeerManager& peerman, CD
if (auto dmn = mnList.GetValidMN(dsq.m_protxHash)) { if (auto dmn = mnList.GetValidMN(dsq.m_protxHash)) {
dsq.masternodeOutpoint = dmn->collateralOutpoint; dsq.masternodeOutpoint = dmn->collateralOutpoint;
} else { } else {
peerman.Misbehaving(peer.GetId(), 10); return tl::unexpected{10};
return;
} }
} }
{ {
TRY_LOCK(cs_vecqueue, lockRecv); TRY_LOCK(cs_vecqueue, lockRecv);
if (!lockRecv) return; if (!lockRecv) return {};
// process every dsq only once // process every dsq only once
for (const auto& q : vecCoinJoinQueue) { for (const auto& q : vecCoinJoinQueue) {
if (q == dsq) { if (q == dsq) {
return; return {};
} }
if (q.fReady == dsq.fReady && q.masternodeOutpoint == dsq.masternodeOutpoint) { if (q.fReady == dsq.fReady && q.masternodeOutpoint == dsq.masternodeOutpoint) {
// no way the same mn can send another dsq with the same readiness this soon // no way the same mn can send another dsq with the same readiness this soon
LogPrint(BCLog::COINJOIN, "DSQUEUE -- Peer %s is sending WAY too many dsq messages for a masternode with collateral %s\n", peer.GetLogString(), dsq.masternodeOutpoint.ToStringShort()); LogPrint(BCLog::COINJOIN, "DSQUEUE -- Peer %s is sending WAY too many dsq messages for a masternode with collateral %s\n", peer.GetLogString(), dsq.masternodeOutpoint.ToStringShort());
return; return {};
} }
} }
} // cs_vecqueue } // cs_vecqueue
LogPrint(BCLog::COINJOIN, "DSQUEUE -- %s new\n", dsq.ToString()); LogPrint(BCLog::COINJOIN, "DSQUEUE -- %s new\n", dsq.ToString());
if (dsq.IsTimeOutOfBounds()) return; if (dsq.IsTimeOutOfBounds()) return {};
auto mnList = deterministicMNManager->GetListAtChainTip(); auto mnList = deterministicMNManager->GetListAtChainTip();
auto dmn = mnList.GetValidMNByCollateral(dsq.masternodeOutpoint); auto dmn = mnList.GetValidMNByCollateral(dsq.masternodeOutpoint);
if (!dmn) return; if (!dmn) return {};
if (dsq.m_protxHash.IsNull()) { if (dsq.m_protxHash.IsNull()) {
dsq.m_protxHash = dmn->proTxHash; dsq.m_protxHash = dmn->proTxHash;
} }
if (!dsq.CheckSignature(dmn->pdmnState->pubKeyOperator.Get())) { if (!dsq.CheckSignature(dmn->pdmnState->pubKeyOperator.Get())) {
peerman.Misbehaving(peer.GetId(), 10); return tl::unexpected{10};
return;
} }
if (!dsq.fReady) { if (!dsq.fReady) {
@ -167,17 +166,18 @@ void CCoinJoinServer::ProcessDSQUEUE(const CNode& peer, PeerManager& peerman, CD
//don't allow a few nodes to dominate the queuing process //don't allow a few nodes to dominate the queuing process
if (nLastDsq != 0 && nDsqThreshold > mmetaman->GetDsqCount()) { if (nLastDsq != 0 && nDsqThreshold > mmetaman->GetDsqCount()) {
LogPrint(BCLog::COINJOIN, "DSQUEUE -- Masternode %s is sending too many dsq messages\n", dmn->pdmnState->addr.ToString()); LogPrint(BCLog::COINJOIN, "DSQUEUE -- Masternode %s is sending too many dsq messages\n", dmn->pdmnState->addr.ToString());
return; return {};
} }
mmetaman->AllowMixing(dmn->proTxHash); mmetaman->AllowMixing(dmn->proTxHash);
LogPrint(BCLog::COINJOIN, "DSQUEUE -- new CoinJoin queue (%s) from masternode %s\n", dsq.ToString(), dmn->pdmnState->addr.ToString()); LogPrint(BCLog::COINJOIN, "DSQUEUE -- new CoinJoin queue (%s) from masternode %s\n", dsq.ToString(), dmn->pdmnState->addr.ToString());
TRY_LOCK(cs_vecqueue, lockRecv); TRY_LOCK(cs_vecqueue, lockRecv);
if (!lockRecv) return; if (!lockRecv) return {};
vecCoinJoinQueue.push_back(dsq); vecCoinJoinQueue.push_back(dsq);
dsq.Relay(connman); dsq.Relay(connman);
} }
return {};
} }
void CCoinJoinServer::ProcessDSVIN(CNode& peer, CDataStream& vRecv) void CCoinJoinServer::ProcessDSVIN(CNode& peer, CDataStream& vRecv)

View File

@ -6,12 +6,14 @@
#define BITCOIN_COINJOIN_SERVER_H #define BITCOIN_COINJOIN_SERVER_H
#include <coinjoin/coinjoin.h> #include <coinjoin/coinjoin.h>
#include <net.h>
#include <net_types.h>
class CChainState; class CChainState;
class CCoinJoinServer; class CCoinJoinServer;
class CDataStream;
class CNode;
class CTxMemPool; class CTxMemPool;
class PeerManager;
class UniValue; class UniValue;
@ -71,7 +73,7 @@ private:
void RelayCompletedTransaction(PoolMessage nMessageID) LOCKS_EXCLUDED(cs_coinjoin); void RelayCompletedTransaction(PoolMessage nMessageID) LOCKS_EXCLUDED(cs_coinjoin);
void ProcessDSACCEPT(CNode& peer, CDataStream& vRecv) LOCKS_EXCLUDED(cs_vecqueue); void ProcessDSACCEPT(CNode& peer, CDataStream& vRecv) LOCKS_EXCLUDED(cs_vecqueue);
void ProcessDSQUEUE(const CNode& peer, PeerManager& peerman, CDataStream& vRecv) LOCKS_EXCLUDED(cs_vecqueue); PeerMsgRet ProcessDSQUEUE(const CNode& peer, CDataStream& vRecv) LOCKS_EXCLUDED(cs_vecqueue);
void ProcessDSVIN(CNode& peer, CDataStream& vRecv) LOCKS_EXCLUDED(cs_coinjoin); void ProcessDSVIN(CNode& peer, CDataStream& vRecv) LOCKS_EXCLUDED(cs_coinjoin);
void ProcessDSSIGNFINALTX(CDataStream& vRecv) LOCKS_EXCLUDED(cs_coinjoin); void ProcessDSSIGNFINALTX(CDataStream& vRecv) LOCKS_EXCLUDED(cs_coinjoin);
@ -87,7 +89,7 @@ public:
fUnitTest(false) fUnitTest(false)
{} {}
void ProcessMessage(CNode& pfrom, PeerManager& peerman, std::string_view msg_type, CDataStream& vRecv); PeerMsgRet ProcessMessage(CNode& pfrom, std::string_view msg_type, CDataStream& vRecv);
bool HasTimedOut() const; bool HasTimedOut() const;
void CheckTimeout(); void CheckTimeout();

View File

@ -4347,7 +4347,7 @@ void PeerManagerImpl::ProcessMessage(
pair.second->ProcessMessage(pfrom, m_connman, m_mempool, msg_type, vRecv); pair.second->ProcessMessage(pfrom, m_connman, m_mempool, msg_type, vRecv);
} }
#endif // ENABLE_WALLET #endif // ENABLE_WALLET
m_cj_ctx->server->ProcessMessage(pfrom, *this, msg_type, vRecv); ProcessPeerMsgRet(m_cj_ctx->server->ProcessMessage(pfrom, msg_type, vRecv), pfrom);
sporkManager->ProcessMessage(pfrom, *this, m_connman, msg_type, vRecv); sporkManager->ProcessMessage(pfrom, *this, m_connman, msg_type, vRecv);
::masternodeSync->ProcessMessage(pfrom, msg_type, vRecv); ::masternodeSync->ProcessMessage(pfrom, msg_type, vRecv);
m_govman.ProcessMessage(pfrom, *this, m_connman, msg_type, vRecv); m_govman.ProcessMessage(pfrom, *this, m_connman, msg_type, vRecv);