Skip to content

Commit fc21e51

Browse files
committed
refactor: move ProcessMessage from CGovernanceManager to NetGovernance
1 parent fa0e173 commit fc21e51

File tree

5 files changed

+152
-151
lines changed

5 files changed

+152
-151
lines changed

src/governance/governance.cpp

Lines changed: 32 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -180,151 +180,50 @@ void CGovernanceManager::AddPostponedObjectInternal(const CGovernanceObject& gov
180180
mapPostponedObjects.emplace(govobj.GetHash(), std::make_shared<CGovernanceObject>(govobj));
181181
}
182182

183-
MessageProcessingResult CGovernanceManager::ProcessMessage(CNode& peer, CConnman& connman, std::string_view msg_type,
184-
CDataStream& vRecv)
183+
bool CGovernanceManager::ProcessObject(const CNode& peer, const uint256& nHash, CGovernanceObject& govobj)
185184
{
186-
AssertLockNotHeld(cs_store);
187-
AssertLockNotHeld(cs_relay);
188-
if (!IsValid()) return {};
189-
if (!m_mn_sync.IsBlockchainSynced()) return {};
185+
std::string strHash = nHash.ToString();
190186

191-
const auto tip_mn_list = Assert(m_dmnman)->GetListAtChainTip();
192-
// ANOTHER USER IS ASKING US TO HELP THEM SYNC GOVERNANCE OBJECT DATA
193-
if (msg_type == NetMsgType::MNGOVERNANCESYNC) {
194-
// Ignore such requests until we are fully synced.
195-
// We could start processing this after masternode list is synced
196-
// but this is a heavy one so it's better to finish sync first.
197-
if (!m_mn_sync.IsSynced()) return {};
198-
199-
uint256 nProp;
200-
CBloomFilter filter;
201-
vRecv >> nProp;
202-
vRecv >> filter;
203-
204-
LogPrint(BCLog::GOBJECT, "MNGOVERNANCESYNC -- syncing governance objects to our peer %s\n", peer.GetLogString());
205-
LOCK(cs_store);
206-
if (nProp == uint256()) {
207-
return SyncObjects(peer, connman);
208-
} else {
209-
return SyncSingleObjVotes(peer, nProp, filter, connman);
210-
}
187+
LOCK(cs_store);
211188

212-
return {};
189+
if (mapObjects.count(nHash) || mapPostponedObjects.count(nHash) || mapErasedGovernanceObjects.count(nHash)) {
190+
// TODO - print error code? what if it's GOVOBJ_ERROR_IMMATURE?
191+
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- Received already seen object: %s\n", strHash);
192+
return true;
213193
}
214194

215-
// A NEW GOVERNANCE OBJECT HAS ARRIVED
216-
else if (msg_type == NetMsgType::MNGOVERNANCEOBJECT) {
217-
// MAKE SURE WE HAVE A VALID REFERENCE TO THE TIP BEFORE CONTINUING
218-
219-
CGovernanceObject govobj;
220-
vRecv >> govobj;
221-
222-
uint256 nHash = govobj.GetHash();
223-
224-
MessageProcessingResult ret{};
225-
ret.m_to_erase = CInv{MSG_GOVERNANCE_OBJECT, nHash};
226-
227-
if (!m_mn_sync.IsBlockchainSynced()) {
228-
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- masternode list not synced\n");
229-
return ret;
230-
}
231-
232-
std::string strHash = nHash.ToString();
233-
234-
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- Received object: %s\n", strHash);
235-
236-
if (!AcceptMessage(nHash)) {
237-
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- Received unrequested object: %s\n", strHash);
238-
return ret;
239-
}
240-
241-
LOCK2(::cs_main, cs_store);
242-
243-
if (mapObjects.count(nHash) || mapPostponedObjects.count(nHash) || mapErasedGovernanceObjects.count(nHash)) {
244-
// TODO - print error code? what if it's GOVOBJ_ERROR_IMMATURE?
245-
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- Received already seen object: %s\n", strHash);
246-
return ret;
247-
}
248-
249-
bool fRateCheckBypassed = false;
250-
if (!MasternodeRateCheck(govobj, true, false, fRateCheckBypassed)) {
251-
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- masternode rate check failed - %s - (current block height %d) \n", strHash, nCachedBlockHeight);
252-
return ret;
253-
}
254-
255-
std::string strError;
256-
// CHECK OBJECT AGAINST LOCAL BLOCKCHAIN
257-
258-
bool fMissingConfirmations = false;
259-
bool fIsValid = govobj.IsValidLocally(tip_mn_list, m_chainman, strError, fMissingConfirmations, true);
260-
261-
bool unused_rcb;
262-
if (fRateCheckBypassed && fIsValid && !MasternodeRateCheck(govobj, true, true, unused_rcb)) {
263-
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- masternode rate check failed (after signature verification) - %s - (current block height %d)\n", strHash, nCachedBlockHeight);
264-
return ret;
265-
}
266-
267-
if (!fIsValid) {
268-
if (fMissingConfirmations) {
269-
AddPostponedObjectInternal(govobj);
270-
LogPrintf("MNGOVERNANCEOBJECT -- Not enough fee confirmations for: %s, strError = %s\n", strHash, strError);
271-
} else {
272-
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- Governance object is invalid - %s\n", strError);
273-
// apply node's ban score
274-
ret.m_error = MisbehavingError{20};
275-
return ret;
276-
}
277-
278-
return ret;
279-
}
280-
281-
AddGovernanceObjectInternal(govobj, &peer);
282-
return ret;
195+
bool fRateCheckBypassed = false;
196+
if (!MasternodeRateCheck(govobj, true, false, fRateCheckBypassed)) {
197+
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- masternode rate check failed - %s - (current block height %d) \n", strHash, nCachedBlockHeight);
198+
return true;
283199
}
284200

285-
// A NEW GOVERNANCE OBJECT VOTE HAS ARRIVED
286-
else if (msg_type == NetMsgType::MNGOVERNANCEOBJECTVOTE) {
287-
CGovernanceVote vote;
288-
vRecv >> vote;
289-
290-
uint256 nHash = vote.GetHash();
291-
292-
MessageProcessingResult ret{};
293-
ret.m_to_erase = CInv{MSG_GOVERNANCE_OBJECT_VOTE, nHash};
294-
295-
// Ignore such messages until masternode list is synced
296-
if (!m_mn_sync.IsBlockchainSynced()) {
297-
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- masternode list not synced\n");
298-
return ret;
299-
}
300-
301-
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- Received vote: %s\n", vote.ToString(tip_mn_list));
201+
std::string strError;
202+
// CHECK OBJECT AGAINST LOCAL BLOCKCHAIN
302203

303-
std::string strHash = nHash.ToString();
204+
const auto tip_mn_list = GetMNManager().GetListAtChainTip();
205+
bool fMissingConfirmations = false;
206+
bool fIsValid = govobj.IsValidLocally(tip_mn_list, m_chainman, strError, fMissingConfirmations, true);
304207

305-
if (!AcceptMessage(nHash)) {
306-
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- Received unrequested vote object: %s, hash: %s, peer = %d\n",
307-
vote.ToString(tip_mn_list), strHash, peer.GetId());
308-
return ret;
309-
}
208+
bool unused_rcb;
209+
if (fRateCheckBypassed && fIsValid && !MasternodeRateCheck(govobj, true, true, unused_rcb)) {
210+
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- masternode rate check failed (after signature verification) - %s - (current block height %d)\n", strHash, nCachedBlockHeight);
211+
return true;
212+
}
310213

311-
CGovernanceException exception;
312-
if (ProcessVote(&peer, vote, exception, connman)) {
313-
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- %s new\n", strHash);
314-
m_mn_sync.BumpAssetLastTime("MNGOVERNANCEOBJECTVOTE");
315-
RelayVote(vote);
214+
if (!fIsValid) {
215+
if (fMissingConfirmations) {
216+
AddPostponedObjectInternal(govobj);
217+
LogPrintf("MNGOVERNANCEOBJECT -- Not enough fee confirmations for: %s, strError = %s\n", strHash, strError);
218+
return true;
316219
} else {
317-
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- Rejected vote, error = %s\n", exception.what());
318-
if ((exception.GetNodePenalty() != 0) && m_mn_sync.IsSynced()) {
319-
ret.m_error = MisbehavingError{exception.GetNodePenalty()};
320-
return ret;
321-
}
322-
return ret;
220+
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- Governance object is invalid - %s\n", strError);
221+
return false;
323222
}
324-
return ret;
325223
}
326224

327-
return {};
225+
AddGovernanceObjectInternal(govobj, &peer);
226+
return true;
328227
}
329228

330229
void CGovernanceManager::CheckOrphanVotes(CGovernanceObject& govobj)
@@ -693,7 +592,7 @@ bool CGovernanceManager::ConfirmInventoryRequest(const CInv& inv)
693592

694593
MessageProcessingResult CGovernanceManager::SyncSingleObjVotes(CNode& peer, const uint256& nProp, const CBloomFilter& filter, CConnman& connman)
695594
{
696-
AssertLockHeld(cs_store);
595+
LOCK(cs_store);
697596
// do not provide any data until our node is synced
698597
if (!m_mn_sync.IsSynced()) return {};
699598

@@ -746,7 +645,7 @@ MessageProcessingResult CGovernanceManager::SyncSingleObjVotes(CNode& peer, cons
746645

747646
MessageProcessingResult CGovernanceManager::SyncObjects(CNode& peer, CConnman& connman) const
748647
{
749-
AssertLockHeld(cs_store);
648+
LOCK(cs_store);
750649
assert(m_netfulfilledman.IsValid());
751650

752651
// do not provide any data until our node is synced

src/governance/governance.h

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,6 @@ class CGovernanceManager : public GovernanceStore, public GovernanceSignerParent
311311
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);
312312
bool ProcessVoteAndRelay(const CGovernanceVote& vote, CGovernanceException& exception, CConnman& connman) override
313313
EXCLUSIVE_LOCKS_REQUIRED(!cs_store, !cs_relay);
314-
[[nodiscard]] MessageProcessingResult ProcessMessage(CNode& peer, CConnman& connman, std::string_view msg_type,
315-
CDataStream& vRecv)
316-
EXCLUSIVE_LOCKS_REQUIRED(!cs_store, !cs_relay);
317314
void RelayObject(const CGovernanceObject& obj)
318315
EXCLUSIVE_LOCKS_REQUIRED(!cs_relay);
319316
void RelayVote(const CGovernanceVote& vote)
@@ -376,17 +373,24 @@ class CGovernanceManager : public GovernanceStore, public GovernanceSignerParent
376373
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);
377374
void RequestGovernanceObject(CNode* pfrom, const uint256& nHash, CConnman& connman, bool fUseFilter = false) const
378375
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);
379-
CDeterministicMNManager& GetMNManager();
380-
381-
382-
private:
383-
// Branches of ProcessMessage
384376
[[nodiscard]] MessageProcessingResult SyncObjects(CNode& peer, CConnman& connman) const
385-
EXCLUSIVE_LOCKS_REQUIRED(cs_store);
377+
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);
386378
[[nodiscard]] MessageProcessingResult SyncSingleObjVotes(CNode& peer, const uint256& nProp, const CBloomFilter& filter,
387379
CConnman& connman)
388-
EXCLUSIVE_LOCKS_REQUIRED(cs_store);
380+
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);
381+
/// Called to indicate a requested object or vote has been received
382+
bool AcceptMessage(const uint256& nHash)
383+
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);
384+
bool ProcessObject(const CNode& peer, const uint256& hash, CGovernanceObject& govobj)
385+
EXCLUSIVE_LOCKS_REQUIRED(::cs_main, !cs_store, !cs_relay);
386+
387+
CDeterministicMNManager& GetMNManager();
388+
bool ProcessVote(CNode* pfrom, const CGovernanceVote& vote, CGovernanceException& exception, CConnman& connman)
389+
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);
390+
389391

392+
393+
private:
390394
// Internal counterparts to "Thread-safe accessors"
391395
void AddPostponedObjectInternal(const CGovernanceObject& govobj)
392396
EXCLUSIVE_LOCKS_REQUIRED(cs_store);
@@ -431,13 +435,6 @@ class CGovernanceManager : public GovernanceStore, public GovernanceSignerParent
431435
void ExecuteBestSuperblock(const CDeterministicMNList& tip_mn_list, int nBlockHeight)
432436
EXCLUSIVE_LOCKS_REQUIRED(cs_store);
433437

434-
bool ProcessVote(CNode* pfrom, const CGovernanceVote& vote, CGovernanceException& exception, CConnman& connman)
435-
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);
436-
437-
/// Called to indicate a requested object or vote has been received
438-
bool AcceptMessage(const uint256& nHash)
439-
EXCLUSIVE_LOCKS_REQUIRED(!cs_store);
440-
441438
void CheckOrphanVotes(CGovernanceObject& govobj)
442439
EXCLUSIVE_LOCKS_REQUIRED(cs_store, !cs_relay);
443440

src/governance/net_governance.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,3 +341,108 @@ void NetGovernance::ProcessTick(CConnman& connman)
341341
}
342342
}
343343
}
344+
345+
void NetGovernance::ProcessMessage(CNode& peer, CConnman& connman, const std::string& msg_type, CDataStream& vRecv)
346+
{
347+
if (!m_gov_manager.IsValid()) return;
348+
if (!m_node_sync.IsBlockchainSynced()) return;
349+
350+
// ANOTHER USER IS ASKING US TO HELP THEM SYNC GOVERNANCE OBJECT DATA
351+
if (msg_type == NetMsgType::MNGOVERNANCESYNC) {
352+
// Ignore such requests until we are fully synced.
353+
// We could start processing this after masternode list is synced
354+
// but this is a heavy one so it's better to finish sync first.
355+
if (!m_node_sync.IsSynced()) return;
356+
357+
uint256 nProp;
358+
CBloomFilter filter;
359+
vRecv >> nProp;
360+
vRecv >> filter;
361+
362+
LogPrint(BCLog::GOBJECT, "MNGOVERNANCESYNC -- syncing governance objects to our peer %s\n", peer.GetLogString());
363+
if (nProp == uint256()) {
364+
m_peer_manager->PeerPostProcessMessage(m_gov_manager.SyncObjects(peer, connman));
365+
} else {
366+
m_peer_manager->PeerPostProcessMessage(m_gov_manager.SyncSingleObjVotes(peer, nProp, filter, connman));
367+
}
368+
}
369+
// A NEW GOVERNANCE OBJECT HAS ARRIVED
370+
else if (msg_type == NetMsgType::MNGOVERNANCEOBJECT) {
371+
// MAKE SURE WE HAVE A VALID REFERENCE TO THE TIP BEFORE CONTINUING
372+
CGovernanceObject govobj;
373+
vRecv >> govobj;
374+
375+
uint256 nHash = govobj.GetHash();
376+
377+
WITH_LOCK(::cs_main, m_peer_manager->PeerEraseObjectRequest(peer.GetId(), CInv{MSG_GOVERNANCE_OBJECT, nHash}));
378+
379+
if (!m_node_sync.IsBlockchainSynced()) {
380+
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- masternode list not synced\n");
381+
return;
382+
}
383+
384+
std::string strHash = nHash.ToString();
385+
386+
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- Received object: %s\n", strHash);
387+
388+
if (!m_gov_manager.AcceptMessage(nHash)) {
389+
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECT -- Received unrequested object: %s\n", strHash);
390+
return;
391+
}
392+
393+
if (!WITH_LOCK(::cs_main, return m_gov_manager.ProcessObject(peer, nHash, govobj))) {
394+
// apply node's ban score
395+
m_peer_manager->PeerMisbehaving(peer.GetId(), 20);
396+
}
397+
}
398+
399+
// A NEW GOVERNANCE OBJECT VOTE HAS ARRIVED
400+
else if (msg_type == NetMsgType::MNGOVERNANCEOBJECTVOTE) {
401+
CGovernanceVote vote;
402+
vRecv >> vote;
403+
404+
uint256 nHash = vote.GetHash();
405+
406+
WITH_LOCK(::cs_main, m_peer_manager->PeerEraseObjectRequest(peer.GetId(), CInv{MSG_GOVERNANCE_OBJECT_VOTE, nHash}));
407+
408+
// Ignore such messages until masternode list is synced
409+
if (!m_node_sync.IsBlockchainSynced()) {
410+
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- masternode list not synced\n");
411+
return;
412+
}
413+
414+
const auto tip_mn_list = m_gov_manager.GetMNManager().GetListAtChainTip();
415+
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- Received vote: %s\n", vote.ToString(tip_mn_list));
416+
417+
std::string strHash = nHash.ToString();
418+
419+
if (!m_gov_manager.AcceptMessage(nHash)) {
420+
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- Received unrequested vote object: %s, hash: %s, peer = %d\n",
421+
vote.ToString(tip_mn_list), strHash, peer.GetId());
422+
return;
423+
}
424+
425+
CGovernanceException exception;
426+
if (m_gov_manager.ProcessVote(&peer, vote, exception, connman)) {
427+
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- %s new\n", strHash);
428+
m_node_sync.BumpAssetLastTime("MNGOVERNANCEOBJECTVOTE");
429+
430+
if (!m_node_sync.IsSynced()) {
431+
LogPrint(BCLog::GOBJECT, "%s -- won't relay until fully synced\n", __func__);
432+
return;
433+
}
434+
auto dmn = tip_mn_list.GetMNByCollateral(vote.GetMasternodeOutpoint());
435+
if (!dmn) {
436+
return;
437+
}
438+
m_gov_manager.RelayVote(vote);
439+
// TODO: figure out why immediate sending of inventory doesn't work here!
440+
// m_peer_manager->PeerRelayInv(CInv{MSG_GOVERNANCE_OBJECT_VOTE, nHash});
441+
} else {
442+
LogPrint(BCLog::GOBJECT, "MNGOVERNANCEOBJECTVOTE -- Rejected vote, error = %s\n", exception.what());
443+
if ((exception.GetNodePenalty() != 0) && m_node_sync.IsSynced()) {
444+
m_peer_manager->PeerMisbehaving(peer.GetId(), exception.GetNodePenalty());
445+
}
446+
}
447+
}
448+
}

src/governance/net_governance.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class NetGovernance final : public NetHandler
2424
}
2525
void Schedule(CScheduler& scheduler, CConnman& connman) override;
2626

27+
void ProcessMessage(CNode& peer, CConnman& connman, const std::string& msg_type, CDataStream& vRecv) override;
2728
private:
2829
void SendGovernanceSyncRequest(CNode* pnode, CConnman& connman) const;
2930
int RequestGovernanceObjectVotes(const std::vector<CNode*>& vNodesCopy, CConnman& connman) const;

src/net_processing.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5447,7 +5447,6 @@ void PeerManagerImpl::ProcessMessage(
54475447
}
54485448
PostProcessMessage(m_sporkman.ProcessMessage(pfrom, m_connman, msg_type, vRecv), pfrom.GetId());
54495449
m_mn_sync.ProcessMessage(pfrom, msg_type, vRecv);
5450-
PostProcessMessage(m_govman.ProcessMessage(pfrom, m_connman, msg_type, vRecv), pfrom.GetId());
54515450
PostProcessMessage(CMNAuth::ProcessMessage(pfrom, peer->m_their_services, m_connman, m_mn_metaman, m_mn_activeman, m_mn_sync, m_dmnman->GetListAtChainTip(), msg_type, vRecv), pfrom.GetId());
54525451
PostProcessMessage(m_llmq_ctx->quorum_block_processor->ProcessMessage(pfrom, msg_type, vRecv), pfrom.GetId());
54535452
PostProcessMessage(m_llmq_ctx->qdkgsman->ProcessMessage(pfrom, is_masternode, msg_type, vRecv), pfrom.GetId());

0 commit comments

Comments
 (0)