diff --git a/src/governance/governance.cpp b/src/governance/governance.cpp index 419818fcec6c..9dba091c5226 100644 --- a/src/governance/governance.cpp +++ b/src/governance/governance.cpp @@ -283,12 +283,12 @@ bool CGovernanceManager::ProcessObject(const std::string& peer_str, const uint25 return true; } -void CGovernanceManager::CheckOrphanVotes(CGovernanceObject& govobj) +void CGovernanceManager::CheckOrphanVotes(const std::shared_ptr& govobj) { AssertLockHeld(cs_store); AssertLockNotHeld(cs_relay); - uint256 nHash = govobj.GetHash(); + uint256 nHash = Assert(govobj)->GetHash(); std::vector orphan_votes; cmmapOrphanVotes.GetAll(nHash, orphan_votes); @@ -302,8 +302,10 @@ void CGovernanceManager::CheckOrphanVotes(CGovernanceObject& govobj) CGovernanceException e; if (orphan_vote.expiration < now) { fRemove = true; - } else if (govobj.ProcessVote(m_mn_metaman, fRateChecksEnabled, tip_mn_list, vote, e)) { - RelayVote(vote); + } else if (govobj->ProcessVote(m_mn_metaman, fRateChecksEnabled, tip_mn_list, vote, e)) { + if (cmapVoteToObject.Insert(vote.GetHash(), govobj)) { + RelayVote(vote); + } fRemove = true; } if (fRemove) { @@ -377,7 +379,7 @@ void CGovernanceManager::AddGovernanceObjectInternal(CGovernanceObject& insert_o // WE MIGHT HAVE PENDING/ORPHAN VOTES FOR THIS OBJECT - CheckOrphanVotes(*govobj); + CheckOrphanVotes(govobj); // SEND NOTIFICATION TO SCRIPT/ZMQ GetMainSignals().NotifyGovernanceObject(std::make_shared(govobj->Object()), nHash.ToString()); diff --git a/src/governance/governance.h b/src/governance/governance.h index 10be5d101e2c..80b449d8a2f7 100644 --- a/src/governance/governance.h +++ b/src/governance/governance.h @@ -424,7 +424,7 @@ class CGovernanceManager : public GovernanceStore void InitOnLoad() EXCLUSIVE_LOCKS_REQUIRED(!cs_store); - void CheckOrphanVotes(CGovernanceObject& govobj) + void CheckOrphanVotes(const std::shared_ptr& govobj) EXCLUSIVE_LOCKS_REQUIRED(cs_store, !cs_relay); void RebuildIndexes() diff --git a/src/governance/net_governance.cpp b/src/governance/net_governance.cpp index 520078e1c7e6..b6d22a4e5be6 100644 --- a/src/governance/net_governance.cpp +++ b/src/governance/net_governance.cpp @@ -153,7 +153,7 @@ void NetGovernance::ProcessMessage(CNode& peer, const std::string& msg_type, CDa m_connman.PushMessage(&peer, msgMaker.Make(NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_GOVOBJ, static_cast(invs.size()))); for (const auto& inv : invs) { - m_peer_manager->PeerRelayInv(inv); + m_peer_manager->PeerPushInventory(peer.GetId(), inv); } } else { // Sync votes for a specific governance object @@ -165,7 +165,7 @@ void NetGovernance::ProcessMessage(CNode& peer, const std::string& msg_type, CDa m_connman.PushMessage(&peer, msgMaker.Make(NetMsgType::SYNCSTATUSCOUNT, MASTERNODE_SYNC_GOVOBJ_VOTE, static_cast(invs.size()))); for (const auto& inv : invs) { - m_peer_manager->PeerRelayInv(inv); + m_peer_manager->PeerPushInventory(peer.GetId(), inv); } } } diff --git a/src/test/governance_vote_processing_tests.cpp b/src/test/governance_vote_processing_tests.cpp index 2b2f6f28bfb3..3ffd2ad0c140 100644 --- a/src/test/governance_vote_processing_tests.cpp +++ b/src/test/governance_vote_processing_tests.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -270,10 +271,16 @@ BOOST_AUTO_TEST_CASE(orphan_vote_is_cached_and_applied_when_parent_arrives) BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_FUNDING), 1); BOOST_CHECK_EQUAL(govman.GetCurrentVotes(parent_hash, mn_collateral).size(), 1U); BOOST_CHECK(govman.GetOrphanVoteObjectHashes().empty()); - // Known gap, pinned here so a fix has to update this test: CheckOrphanVotes() applies the - // vote to the object but never indexes it in cmapVoteToObject, so the vote inv it relays - // during the replay cannot be served to a peer that requests it. - BOOST_CHECK(!govman.HaveVoteForHash(vote.GetHash())); + BOOST_CHECK(govman.HaveVoteForHash(vote.GetHash())); + CDataStream serialized_vote{SER_NETWORK, PROTOCOL_VERSION}; + BOOST_CHECK(govman.SerializeVoteForHash(vote.GetHash(), serialized_vote)); + if (!serialized_vote.empty()) { + CGovernanceVote served_vote; + serialized_vote >> served_vote; + BOOST_CHECK_EQUAL(served_vote.GetHash(), vote.GetHash()); + BOOST_CHECK(served_vote.IsValid(tip_mn_list(), /*useVotingKey=*/true)); + BOOST_CHECK(serialized_vote.empty()); + } // A peer re-sending the same vote must not be punished for it. CGovernanceException replay_exception; diff --git a/test/functional/feature_governance.py b/test/functional/feature_governance.py index fad3c5833e36..42a7274fbfd8 100755 --- a/test/functional/feature_governance.py +++ b/test/functional/feature_governance.py @@ -10,8 +10,19 @@ import json import os import shutil - -from test_framework.messages import CBlock, from_hex, uint256_to_string +import struct + +from test_framework.messages import ( + CBlock, + MSG_GOVERNANCE_OBJECT, + MSG_GOVERNANCE_OBJECT_VOTE, + from_hex, + msg_generic, + ser_string, + ser_uint256, + uint256_to_string, +) +from test_framework.p2p import P2PInterface, p2p_lock from test_framework.test_framework import ( DashTestFramework, ) @@ -22,6 +33,18 @@ MIN_BLOCKS_TO_KEEP = 288 # src/validation.h BUDGET_ARGS = ["-budgetparams=10:10:10"] + +class GovernanceSyncPeer(P2PInterface): + def __init__(self): + super().__init__() + self.governance_invs = set() + + def on_inv(self, message): + for inv in message.inv: + if inv.type in (MSG_GOVERNANCE_OBJECT, MSG_GOVERNANCE_OBJECT_VOTE): + self.governance_invs.add((inv.type, inv.hash)) + + class DashGovernanceTest (DashTestFramework): def add_options(self, parser): self.add_wallet_options(parser) @@ -82,6 +105,28 @@ def count_own_trigger_votes(self, mns, trigger_hash): count += len(mn.get_node(self).gobject("getcurrentvotes", trigger_hash, collateral["collateralHash"], str(collateral["collateralIndex"]))) return count + def check_govsync_is_peer_local(self): + node = self.nodes[0] + self.log.info("Governance sync inventories go only to the requesting peer") + # Flush ordinary announcements before connecting peers that have never seen these inventories. + self.bump_mocktime(1) + for object_hash, inv_type, expected_hashes in ( + (0, MSG_GOVERNANCE_OBJECT, {int(h, 16) for h in node.gobject("list")}), + (int(self.p0_hash, 16), MSG_GOVERNANCE_OBJECT_VOTE, + {int(h, 16) for h in node.gobject("getcurrentvotes", self.p0_hash)}), + ): + assert expected_hashes + requester = node.add_p2p_connection(GovernanceSyncPeer()) + bystander = node.add_p2p_connection(GovernanceSyncPeer()) + # A one-byte, all-zero bloom filter requests every vote and is distinct from an object fetch. + bloom_filter = ser_string(b"\x00") + struct.pack("