Skip to content

Commit

Permalink
net: move CConnman::RelayInv{Filtered} into PeerManager
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Apr 23, 2024
1 parent 313a7e9 commit bfd33cd
Show file tree
Hide file tree
Showing 26 changed files with 173 additions and 147 deletions.
2 changes: 1 addition & 1 deletion src/coinjoin/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ void CCoinJoinServer::CommitFinalTransaction()
LogPrint(BCLog::COINJOIN, "CCoinJoinServer::CommitFinalTransaction -- TRANSMITTING DSTX\n");

CInv inv(MSG_DSTX, hashTx);
RelayInv(connman, inv);
m_peerman->RelayInv(inv);

// Tell the clients it was successful
RelayCompletedTransaction(MSG_SUCCESS);
Expand Down
5 changes: 4 additions & 1 deletion src/dsnotificationinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <dsnotificationinterface.h>
#include <governance/governance.h>
#include <masternode/sync.h>
#include <net_processing.h>
#include <validation.h>

#include <evo/deterministicmns.h>
Expand All @@ -26,13 +27,15 @@
CDSNotificationInterface::CDSNotificationInterface(CConnman& connman,
CMasternodeSync& mn_sync,
CGovernanceManager& govman,
PeerManager& peerman,
const CActiveMasternodeManager* const mn_activeman,
const std::unique_ptr<CDeterministicMNManager>& dmnman,
const std::unique_ptr<LLMQContext>& llmq_ctx,
const std::unique_ptr<CJContext>& cj_ctx)
: m_connman(connman),
m_mn_sync(mn_sync),
m_govman(govman),
m_peerman(peerman),
m_mn_activeman(mn_activeman),
m_dmnman(dmnman),
m_llmq_ctx(llmq_ctx),
Expand Down Expand Up @@ -96,7 +99,7 @@ void CDSNotificationInterface::UpdatedBlockTip(const CBlockIndex *pindexNew, con
m_llmq_ctx->qdkgsman->UpdatedBlockTip(pindexNew, fInitialDownload);
m_llmq_ctx->ehfSignalsHandler->UpdatedBlockTip(pindexNew);

if (!fDisableGovernance) m_govman.UpdatedBlockTip(pindexNew, m_connman, m_mn_activeman);
if (!fDisableGovernance) m_govman.UpdatedBlockTip(pindexNew, m_connman, m_peerman, m_mn_activeman);
}

void CDSNotificationInterface::TransactionAddedToMempool(const CTransactionRef& ptx, int64_t nAcceptTime)
Expand Down
5 changes: 3 additions & 2 deletions src/dsnotificationinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class CConnman;
class CDeterministicMNManager;
class CGovernanceManager;
class CMasternodeSync;
class PeerManager;
struct CJContext;
struct LLMQContext;

Expand All @@ -21,6 +22,7 @@ class CDSNotificationInterface : public CValidationInterface
explicit CDSNotificationInterface(CConnman& connman,
CMasternodeSync& mn_sync,
CGovernanceManager& govman,
PeerManager& peerman,
const CActiveMasternodeManager* const mn_activeman,
const std::unique_ptr<CDeterministicMNManager>& dmnman,
const std::unique_ptr<LLMQContext>& llmq_ctx,
Expand All @@ -45,10 +47,9 @@ class CDSNotificationInterface : public CValidationInterface

private:
CConnman& m_connman;

CMasternodeSync& m_mn_sync;
CGovernanceManager& m_govman;

PeerManager& m_peerman;
const CActiveMasternodeManager* const m_mn_activeman;
const std::unique_ptr<CDeterministicMNManager>& m_dmnman;
const std::unique_ptr<LLMQContext>& m_llmq_ctx;
Expand Down
48 changes: 24 additions & 24 deletions src/governance/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ bool CGovernanceManager::SerializeVoteForHash(const uint256& nHash, CDataStream&
return cmapVoteToObject.Get(nHash, pGovobj) && pGovobj->GetVoteFile().SerializeVoteToStream(nHash, ss);
}

PeerMsgRet CGovernanceManager::ProcessMessage(CNode& peer, CConnman& connman, std::string_view msg_type, CDataStream& vRecv)
PeerMsgRet CGovernanceManager::ProcessMessage(CNode& peer, CConnman& connman, PeerManager& peerman, std::string_view msg_type, CDataStream& vRecv)
{
if (fDisableGovernance) return {};
if (m_mn_sync == nullptr || !m_mn_sync->IsBlockchainSynced()) return {};
Expand Down Expand Up @@ -213,7 +213,7 @@ PeerMsgRet CGovernanceManager::ProcessMessage(CNode& peer, CConnman& connman, st
return {};
}

AddGovernanceObject(govobj, connman, &peer);
AddGovernanceObject(govobj, peerman, &peer);
}

// A NEW GOVERNANCE OBJECT VOTE HAS ARRIVED
Expand Down Expand Up @@ -248,7 +248,7 @@ PeerMsgRet CGovernanceManager::ProcessMessage(CNode& peer, CConnman& connman, st
if (ProcessVote(&peer, vote, exception, connman)) {
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- %s new\n", strHash);
m_mn_sync->BumpAssetLastTime("MNGOVERNANCEOBJECTVOTE");
vote.Relay(connman, *m_mn_sync, tip_mn_list);
vote.Relay(peerman, *m_mn_sync, tip_mn_list);
} else {
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- Rejected vote, error = %s\n", exception.what());
if ((exception.GetNodePenalty() != 0) && m_mn_sync->IsSynced()) {
Expand All @@ -260,7 +260,7 @@ PeerMsgRet CGovernanceManager::ProcessMessage(CNode& peer, CConnman& connman, st
return {};
}

void CGovernanceManager::CheckOrphanVotes(CGovernanceObject& govobj, CConnman& connman)
void CGovernanceManager::CheckOrphanVotes(CGovernanceObject& govobj, PeerManager& peerman)
{
uint256 nHash = govobj.GetHash();
std::vector<vote_time_pair_t> vecVotePairs;
Expand All @@ -277,7 +277,7 @@ void CGovernanceManager::CheckOrphanVotes(CGovernanceObject& govobj, CConnman& c
if (pairVote.second < nNow) {
fRemove = true;
} else if (govobj.ProcessVote(m_mn_metaman, *this, tip_mn_list, vote, e)) {
vote.Relay(connman, *Assert(m_mn_sync), tip_mn_list);
vote.Relay(peerman, *Assert(m_mn_sync), tip_mn_list);
fRemove = true;
}
if (fRemove) {
Expand All @@ -286,7 +286,7 @@ void CGovernanceManager::CheckOrphanVotes(CGovernanceObject& govobj, CConnman& c
}
}

void CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj, CConnman& connman, const CNode* pfrom)
void CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj, PeerManager& peerman, const CNode* pfrom)
{
uint256 nHash = govobj.GetHash();
std::string strHash = nHash.ToString();
Expand Down Expand Up @@ -331,7 +331,7 @@ void CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj, CConnman
}

LogPrint(BCLog::GOBJECT, "CGovernanceManager::AddGovernanceObject -- %s new, received from peer %s\n", strHash, pfrom ? pfrom->GetLogString() : "nullptr");
govobj.Relay(connman, *Assert(m_mn_sync));
govobj.Relay(peerman, *Assert(m_mn_sync));

// Update the rate buffer
MasternodeRateUpdate(govobj);
Expand All @@ -340,7 +340,7 @@ void CGovernanceManager::AddGovernanceObject(CGovernanceObject& govobj, CConnman

// WE MIGHT HAVE PENDING/ORPHAN VOTES FOR THIS OBJECT

CheckOrphanVotes(govobj, connman);
CheckOrphanVotes(govobj, peerman);

// SEND NOTIFICATION TO SCRIPT/ZMQ
GetMainSignals().NotifyGovernanceObject(std::make_shared<const Governance::Object>(govobj.Object()));
Expand Down Expand Up @@ -672,7 +672,7 @@ std::optional<const CSuperblock> CGovernanceManager::CreateSuperblockCandidate(i
return CSuperblock(nNextSuperblock, std::move(payments));
}

std::optional<const CGovernanceObject> CGovernanceManager::CreateGovernanceTrigger(const std::optional<const CSuperblock>& sb_opt, CConnman& connman,
std::optional<const CGovernanceObject> CGovernanceManager::CreateGovernanceTrigger(const std::optional<const CSuperblock>& sb_opt, PeerManager& peerman,
const CActiveMasternodeManager* const mn_activeman)
{
if (!fMasternodeMode) return std::nullopt;
Expand Down Expand Up @@ -718,11 +718,11 @@ std::optional<const CGovernanceObject> CGovernanceManager::CreateGovernanceTrigg
}

// The trigger we just created looks good, submit it
AddGovernanceObject(gov_sb, connman);
AddGovernanceObject(gov_sb, peerman);
return std::make_optional<CGovernanceObject>(gov_sb);
}

void CGovernanceManager::VoteGovernanceTriggers(const std::optional<const CGovernanceObject>& trigger_opt, CConnman& connman,
void CGovernanceManager::VoteGovernanceTriggers(const std::optional<const CGovernanceObject>& trigger_opt, CConnman& connman, PeerManager& peerman,
const CActiveMasternodeManager* const mn_activeman)
{
// only active masternodes can vote on triggers
Expand All @@ -736,7 +736,7 @@ void CGovernanceManager::VoteGovernanceTriggers(const std::optional<const CGover
assert(!votedFundingYesTriggerHash.has_value());
// Vote YES-FUNDING for the trigger we like
const uint256 gov_sb_hash = trigger_opt.value().GetHash();
if (!VoteFundingTrigger(gov_sb_hash, VOTE_OUTCOME_YES, connman, mn_activeman)) {
if (!VoteFundingTrigger(gov_sb_hash, VOTE_OUTCOME_YES, connman, peerman, mn_activeman)) {
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s Voting YES-FUNDING for new trigger:%s failed\n", __func__, gov_sb_hash.ToString());
// this should never happen, bail out
return;
Expand All @@ -759,7 +759,7 @@ void CGovernanceManager::VoteGovernanceTriggers(const std::optional<const CGover
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s Not voting NO-FUNDING for trigger:%s, we voted yes for it already\n", __func__, trigger_hash.ToString());
continue;
}
if (!VoteFundingTrigger(trigger_hash, VOTE_OUTCOME_NO, connman, mn_activeman)) {
if (!VoteFundingTrigger(trigger_hash, VOTE_OUTCOME_NO, connman, peerman, mn_activeman)) {
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s Voting NO-FUNDING for trigger:%s failed\n", __func__, trigger_hash.ToString());
// failing here is ok-ish
continue;
Expand All @@ -768,7 +768,7 @@ void CGovernanceManager::VoteGovernanceTriggers(const std::optional<const CGover
}
}

bool CGovernanceManager::VoteFundingTrigger(const uint256& nHash, const vote_outcome_enum_t outcome, CConnman& connman,
bool CGovernanceManager::VoteFundingTrigger(const uint256& nHash, const vote_outcome_enum_t outcome, CConnman& connman, PeerManager& peerman,
const CActiveMasternodeManager* const mn_activeman)
{
if (!fMasternodeMode) return false;
Expand All @@ -778,7 +778,7 @@ bool CGovernanceManager::VoteFundingTrigger(const uint256& nHash, const vote_out
vote.Sign(*mn_activeman);

CGovernanceException exception;
if (!ProcessVoteAndRelay(vote, exception, connman)) {
if (!ProcessVoteAndRelay(vote, exception, connman, peerman)) {
LogPrint(BCLog::GOBJECT, "CGovernanceManager::%s Vote FUNDING %d for trigger:%s failed:%s\n", __func__, outcome, nHash.ToString(), exception.what());
return false;
}
Expand Down Expand Up @@ -1066,11 +1066,11 @@ bool CGovernanceManager::MasternodeRateCheck(const CGovernanceObject& govobj, bo
return false;
}

bool CGovernanceManager::ProcessVoteAndRelay(const CGovernanceVote& vote, CGovernanceException& exception, CConnman& connman)
bool CGovernanceManager::ProcessVoteAndRelay(const CGovernanceVote& vote, CGovernanceException& exception, CConnman& connman, PeerManager& peerman)
{
bool fOK = ProcessVote(/* pfrom = */ nullptr, vote, exception, connman);
if (fOK) {
vote.Relay(connman, *Assert(m_mn_sync), Assert(m_dmnman)->GetListAtChainTip());
vote.Relay(peerman, *Assert(m_mn_sync), Assert(m_dmnman)->GetListAtChainTip());
}
return fOK;
}
Expand Down Expand Up @@ -1129,7 +1129,7 @@ bool CGovernanceManager::ProcessVote(CNode* pfrom, const CGovernanceVote& vote,
return fOk;
}

void CGovernanceManager::CheckPostponedObjects(CConnman& connman)
void CGovernanceManager::CheckPostponedObjects(PeerManager& peerman)
{
if (!Assert(m_mn_sync)->IsSynced()) return;

Expand All @@ -1146,7 +1146,7 @@ void CGovernanceManager::CheckPostponedObjects(CConnman& connman)
bool fMissingConfirmations;
if (govobj.IsCollateralValid(strError, fMissingConfirmations)) {
if (govobj.IsValidLocally(Assert(m_dmnman)->GetListAtChainTip(), strError, false)) {
AddGovernanceObject(govobj, connman);
AddGovernanceObject(govobj, peerman);
} else {
LogPrint(BCLog::GOBJECT, "CGovernanceManager::CheckPostponedObjects -- %s invalid\n", nHash.ToString());
}
Expand Down Expand Up @@ -1179,7 +1179,7 @@ void CGovernanceManager::CheckPostponedObjects(CConnman& connman)
if (fValid) {
if (fReady) {
LogPrint(BCLog::GOBJECT, "CGovernanceManager::CheckPostponedObjects -- additional relay: hash = %s\n", govobj.GetHash().ToString());
govobj.Relay(connman, *m_mn_sync);
govobj.Relay(peerman, *m_mn_sync);
} else {
it++;
continue;
Expand Down Expand Up @@ -1474,7 +1474,7 @@ UniValue CGovernanceManager::ToJson() const
return jsonObj;
}

void CGovernanceManager::UpdatedBlockTip(const CBlockIndex* pindex, CConnman& connman, const CActiveMasternodeManager* const mn_activeman)
void CGovernanceManager::UpdatedBlockTip(const CBlockIndex* pindex, CConnman& connman, PeerManager& peerman, const CActiveMasternodeManager* const mn_activeman)
{
// Note this gets called from ActivateBestChain without cs_main being held
// so it should be safe to lock our mutex here without risking a deadlock
Expand All @@ -1486,8 +1486,8 @@ void CGovernanceManager::UpdatedBlockTip(const CBlockIndex* pindex, CConnman& co
}

const auto sb_opt = CreateSuperblockCandidate(pindex->nHeight);
const auto trigger_opt = CreateGovernanceTrigger(sb_opt, connman, mn_activeman);
VoteGovernanceTriggers(trigger_opt, connman, mn_activeman);
const auto trigger_opt = CreateGovernanceTrigger(sb_opt, peerman, mn_activeman);
VoteGovernanceTriggers(trigger_opt, connman, peerman, mn_activeman);

nCachedBlockHeight = pindex->nHeight;
LogPrint(BCLog::GOBJECT, "CGovernanceManager::UpdatedBlockTip -- nCachedBlockHeight: %d\n", nCachedBlockHeight);
Expand All @@ -1496,7 +1496,7 @@ void CGovernanceManager::UpdatedBlockTip(const CBlockIndex* pindex, CConnman& co
RemoveInvalidVotes();
}

CheckPostponedObjects(connman);
CheckPostponedObjects(peerman);

CSuperblockManager::ExecuteBestSuperblock(*this, Assert(m_dmnman)->GetListAtChainTip(), pindex->nHeight);
}
Expand Down
20 changes: 11 additions & 9 deletions src/governance/governance.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

class CBloomFilter;
class CBlockIndex;
class CConnman;
template<typename T>
class CFlatDB;
class CInv;
class PeerManager;

class CDeterministicMNManager;
class CGovernanceManager;
Expand Down Expand Up @@ -292,7 +294,7 @@ class CGovernanceManager : public GovernanceStore
void SyncSingleObjVotes(CNode& peer, const uint256& nProp, const CBloomFilter& filter, CConnman& connman);
PeerMsgRet SyncObjects(CNode& peer, CConnman& connman) const;

PeerMsgRet ProcessMessage(CNode& peer, CConnman& connman, std::string_view msg_type, CDataStream& vRecv);
PeerMsgRet ProcessMessage(CNode& peer, CConnman& connman, PeerManager& peerman, std::string_view msg_type, CDataStream& vRecv);

void ResetVotedFundingTrigger();

Expand All @@ -307,13 +309,13 @@ class CGovernanceManager : public GovernanceStore
std::vector<CGovernanceVote> GetCurrentVotes(const uint256& nParentHash, const COutPoint& mnCollateralOutpointFilter) const;
void GetAllNewerThan(std::vector<CGovernanceObject>& objs, int64_t nMoreThanTime) const;

void AddGovernanceObject(CGovernanceObject& govobj, CConnman& connman, const CNode* pfrom = nullptr);
void AddGovernanceObject(CGovernanceObject& govobj, PeerManager& peerman, const CNode* pfrom = nullptr);

void CheckAndRemove();

UniValue ToJson() const;

void UpdatedBlockTip(const CBlockIndex* pindex, CConnman& connman, const CActiveMasternodeManager* const mn_activeman);
void UpdatedBlockTip(const CBlockIndex* pindex, CConnman& connman, PeerManager& peerman, const CActiveMasternodeManager* const mn_activeman);
int64_t GetLastDiffTime() const { return nTimeLastDiff; }
void UpdateLastDiffTime(int64_t nTimeIn) { nTimeLastDiff = nTimeIn; }

Expand Down Expand Up @@ -342,9 +344,9 @@ class CGovernanceManager : public GovernanceStore

bool MasternodeRateCheck(const CGovernanceObject& govobj, bool fUpdateFailStatus, bool fForce, bool& fRateCheckBypassed);

bool ProcessVoteAndRelay(const CGovernanceVote& vote, CGovernanceException& exception, CConnman& connman);
bool ProcessVoteAndRelay(const CGovernanceVote& vote, CGovernanceException& exception, CConnman& connman, PeerManager& peerman);

void CheckPostponedObjects(CConnman& connman);
void CheckPostponedObjects(PeerManager& peerman);

bool AreRateChecksEnabled() const
{
Expand All @@ -368,11 +370,11 @@ class CGovernanceManager : public GovernanceStore

private:
std::optional<const CSuperblock> CreateSuperblockCandidate(int nHeight) const;
std::optional<const CGovernanceObject> CreateGovernanceTrigger(const std::optional<const CSuperblock>& sb_opt, CConnman& connman,
std::optional<const CGovernanceObject> CreateGovernanceTrigger(const std::optional<const CSuperblock>& sb_opt, PeerManager& peerman,
const CActiveMasternodeManager* const mn_activeman);
void VoteGovernanceTriggers(const std::optional<const CGovernanceObject>& trigger_opt, CConnman& connman,
void VoteGovernanceTriggers(const std::optional<const CGovernanceObject>& trigger_opt, CConnman& connman, PeerManager& peerman,
const CActiveMasternodeManager* const mn_activeman);
bool VoteFundingTrigger(const uint256& nHash, const vote_outcome_enum_t outcome, CConnman& connman,
bool VoteFundingTrigger(const uint256& nHash, const vote_outcome_enum_t outcome, CConnman& connman, PeerManager& peerman,
const CActiveMasternodeManager* const mn_activeman);
bool HasAlreadyVotedFundingTrigger() const;

Expand All @@ -393,7 +395,7 @@ class CGovernanceManager : public GovernanceStore

static bool AcceptMessage(const uint256& nHash, hash_s_t& setHash);

void CheckOrphanVotes(CGovernanceObject& govobj, CConnman& connman);
void CheckOrphanVotes(CGovernanceObject& govobj, PeerManager& peerman);

void RebuildIndexes();

Expand Down
6 changes: 3 additions & 3 deletions src/governance/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <masternode/node.h>
#include <masternode/sync.h>
#include <messagesigner.h>
#include <net.h>
#include <net_processing.h>
#include <timedata.h>
#include <util/time.h>
#include <validation.h>
Expand Down Expand Up @@ -629,7 +629,7 @@ bool CGovernanceObject::GetCurrentMNVotes(const COutPoint& mnCollateralOutpoint,
return true;
}

void CGovernanceObject::Relay(CConnman& connman, const CMasternodeSync& mn_sync) const
void CGovernanceObject::Relay(PeerManager& peerman, const CMasternodeSync& mn_sync) const
{
// Do not relay until fully synced
if (!mn_sync.IsSynced()) {
Expand All @@ -651,7 +651,7 @@ void CGovernanceObject::Relay(CConnman& connman, const CMasternodeSync& mn_sync)
}

CInv inv(MSG_GOVERNANCE_OBJECT, GetHash());
RelayInv(connman, inv, minProtoVersion);
peerman.RelayInv(inv, minProtoVersion);
}

void CGovernanceObject::UpdateSentinelVariables(const CDeterministicMNList& tip_mn_list)
Expand Down
3 changes: 2 additions & 1 deletion src/governance/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class CGovernanceVote;
class CMasternodeMetaMan;
class CMasternodeSync;
class CNode;
class PeerManager;

extern RecursiveMutex cs_main;

Expand Down Expand Up @@ -249,7 +250,7 @@ class CGovernanceObject

UniValue GetJSONObject() const;

void Relay(CConnman& connman, const CMasternodeSync& mn_sync) const;
void Relay(PeerManager& peerman, const CMasternodeSync& mn_sync) const;

uint256 GetHash() const;
uint256 GetDataHash() const;
Expand Down
Loading

0 comments on commit bfd33cd

Please sign in to comment.