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 9, 2024
1 parent c02215f commit dc411b1
Show file tree
Hide file tree
Showing 26 changed files with 177 additions and 150 deletions.
2 changes: 1 addition & 1 deletion src/coinjoin/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,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,12 +27,14 @@
CDSNotificationInterface::CDSNotificationInterface(CConnman& connman,
CMasternodeSync& mn_sync,
CGovernanceManager& govman,
PeerManager& peerman,
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_dmnman(dmnman),
m_llmq_ctx(llmq_ctx),
m_cj_ctx(cj_ctx) {}
Expand Down Expand Up @@ -94,7 +97,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);
if (!fDisableGovernance) m_govman.UpdatedBlockTip(pindexNew, m_peerman, m_connman);
}

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 @@ -11,6 +11,7 @@ class CConnman;
class CDeterministicMNManager;
class CGovernanceManager;
class CMasternodeSync;
class PeerManager;
struct CJContext;
struct LLMQContext;

Expand All @@ -20,6 +21,7 @@ class CDSNotificationInterface : public CValidationInterface
explicit CDSNotificationInterface(CConnman& connman,
CMasternodeSync& mn_sync,
CGovernanceManager& govman,
PeerManager& peerman,
const std::unique_ptr<CDeterministicMNManager>& dmnman,
const std::unique_ptr<LLMQContext>& llmq_ctx,
const std::unique_ptr<CJContext>& cj_ctx);
Expand All @@ -43,10 +45,9 @@ class CDSNotificationInterface : public CValidationInterface

private:
CConnman& m_connman;

CMasternodeSync& m_mn_sync;
CGovernanceManager& m_govman;

PeerManager& m_peerman;
const std::unique_ptr<CDeterministicMNManager>& m_dmnman;
const std::unique_ptr<LLMQContext>& m_llmq_ctx;
const std::unique_ptr<CJContext>& m_cj_ctx;
Expand Down
53 changes: 31 additions & 22 deletions src/governance/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,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, PeerManager& peerman, CConnman& connman, std::string_view msg_type, CDataStream& vRecv)
{
if (fDisableGovernance) return {};
if (::masternodeSync == nullptr || !::masternodeSync->IsBlockchainSynced()) return {};
Expand Down Expand Up @@ -209,7 +209,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 @@ -244,7 +244,7 @@ PeerMsgRet CGovernanceManager::ProcessMessage(CNode& peer, CConnman& connman, st
if (ProcessVote(&peer, vote, exception, connman)) {
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- %s new\n", strHash);
::masternodeSync->BumpAssetLastTime("MNGOVERNANCEOBJECTVOTE");
vote.Relay(connman, tip_mn_list);
vote.Relay(peerman, tip_mn_list);
} else {
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- Rejected vote, error = %s\n", exception.what());
if ((exception.GetNodePenalty() != 0) && ::masternodeSync->IsSynced()) {
Expand All @@ -256,7 +256,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 @@ -273,7 +273,7 @@ void CGovernanceManager::CheckOrphanVotes(CGovernanceObject& govobj, CConnman& c
if (pairVote.second < nNow) {
fRemove = true;
} else if (govobj.ProcessVote(*this, tip_mn_list, vote, e)) {
vote.Relay(connman, tip_mn_list);
vote.Relay(peerman, tip_mn_list);
fRemove = true;
}
if (fRemove) {
Expand All @@ -282,7 +282,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 @@ -327,7 +327,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);
govobj.Relay(peerman);

// Update the rate buffer
MasternodeRateUpdate(govobj);
Expand All @@ -336,7 +336,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 @@ -666,7 +666,16 @@ 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)
bool CGovernanceManager::ProcessVoteAndRelay(const CGovernanceVote& vote, CGovernanceException& exception, PeerManager& peerman, CConnman& connman)
{
bool fOK = ProcessVote(/* pfrom = */ nullptr, vote, exception, connman);
if (fOK) {
vote.Relay(peerman, Assert(m_dmnman)->GetListAtChainTip());
}
return fOK;
}

std::optional<const CGovernanceObject> CGovernanceManager::CreateGovernanceTrigger(const std::optional<const CSuperblock>& sb_opt, PeerManager& peerman)
{
// no sb_opt, no trigger
if (!sb_opt.has_value()) return std::nullopt;
Expand Down Expand Up @@ -709,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, PeerManager& peerman, CConnman& connman)
{
// only active masternodes can vote on triggers
if (!fMasternodeMode) return;
Expand All @@ -726,7 +735,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)) {
if (!VoteFundingTrigger(gov_sb_hash, VOTE_OUTCOME_YES, peerman, connman)) {
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 @@ -749,7 +758,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)) {
if (!VoteFundingTrigger(trigger_hash, VOTE_OUTCOME_NO, peerman, connman)) {
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 @@ -758,14 +767,14 @@ 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, PeerManager& peerman, CConnman& connman)
{
CGovernanceVote vote(::activeMasternodeManager->GetOutPoint(), nHash, VOTE_SIGNAL_FUNDING, outcome);
vote.SetTime(GetAdjustedTime());
vote.Sign(*::activeMasternodeManager);

CGovernanceException exception;
if (!ProcessVoteAndRelay(vote, exception, connman)) {
if (!ProcessVoteAndRelay(vote, exception, peerman, connman)) {
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 @@ -1107,7 +1116,7 @@ bool CGovernanceManager::ProcessVote(CNode* pfrom, const CGovernanceVote& vote,
return fOk;
}

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

Expand All @@ -1124,7 +1133,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 @@ -1157,7 +1166,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);
govobj.Relay(peerman);
} else {
it++;
continue;
Expand Down Expand Up @@ -1452,7 +1461,7 @@ UniValue CGovernanceManager::ToJson() const
return jsonObj;
}

void CGovernanceManager::UpdatedBlockTip(const CBlockIndex* pindex, CConnman& connman)
void CGovernanceManager::UpdatedBlockTip(const CBlockIndex* pindex, PeerManager& peerman, CConnman& connman)
{
// 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 @@ -1464,8 +1473,8 @@ void CGovernanceManager::UpdatedBlockTip(const CBlockIndex* pindex, CConnman& co
}

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

nCachedBlockHeight = pindex->nHeight;
LogPrint(BCLog::GOBJECT, "CGovernanceManager::UpdatedBlockTip -- nCachedBlockHeight: %d\n", nCachedBlockHeight);
Expand All @@ -1474,7 +1483,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
26 changes: 10 additions & 16 deletions src/governance/governance.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class CBlockIndex;
template<typename T>
class CFlatDB;
class CInv;
class PeerManager;

class CDeterministicMNManager;
class CGovernanceManager;
Expand Down Expand Up @@ -287,7 +288,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, PeerManager& peerman, CConnman& connman, std::string_view msg_type, CDataStream& vRecv);

void ResetVotedFundingTrigger();

Expand All @@ -302,15 +303,15 @@ 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 UpdateCachesAndClean();

void CheckAndRemove() { UpdateCachesAndClean(); }

UniValue ToJson() const;

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

Expand Down Expand Up @@ -339,16 +340,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 fOK = ProcessVote(nullptr, vote, exception, connman);
if (fOK) {
vote.Relay(connman, Assert(m_dmnman)->GetListAtChainTip());
}
return fOK;
}
bool ProcessVoteAndRelay(const CGovernanceVote& vote, CGovernanceException& exception, PeerManager& peerman, CConnman& connman);

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

bool AreRateChecksEnabled() const
{
Expand All @@ -372,9 +366,9 @@ 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);
void VoteGovernanceTriggers(const std::optional<const CGovernanceObject>& trigger_opt, CConnman& connman);
bool VoteFundingTrigger(const uint256& nHash, const vote_outcome_enum_t outcome, CConnman& connman);
std::optional<const CGovernanceObject> CreateGovernanceTrigger(const std::optional<const CSuperblock>& sb_opt, PeerManager& peerman);
void VoteGovernanceTriggers(const std::optional<const CGovernanceObject>& trigger_opt, PeerManager& peerman, CConnman& connman);
bool VoteFundingTrigger(const uint256& nHash, const vote_outcome_enum_t outcome, PeerManager& peerman, CConnman& connman);
bool HasAlreadyVotedFundingTrigger() const;

void RequestGovernanceObject(CNode* pfrom, const uint256& nHash, CConnman& connman, bool fUseFilter = false) const;
Expand All @@ -394,7 +388,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 @@ -626,7 +626,7 @@ bool CGovernanceObject::GetCurrentMNVotes(const COutPoint& mnCollateralOutpoint,
return true;
}

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

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 @@ -20,6 +20,7 @@ class CGovernanceManager;
class CGovernanceObject;
class CGovernanceVote;
class CNode;
class PeerManager;

extern RecursiveMutex cs_main;

Expand Down Expand Up @@ -247,7 +248,7 @@ class CGovernanceObject

UniValue GetJSONObject() const;

void Relay(CConnman& connman) const;
void Relay(PeerManager& peerman) const;

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

0 comments on commit dc411b1

Please sign in to comment.