Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/governance/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CGovernanceObject>& govobj)
{
AssertLockHeld(cs_store);
AssertLockNotHeld(cs_relay);

uint256 nHash = govobj.GetHash();
uint256 nHash = Assert(govobj)->GetHash();
std::vector<governance::OrphanVote> orphan_votes;
cmmapOrphanVotes.GetAll(nHash, orphan_votes);

Expand All @@ -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) {
Expand Down Expand Up @@ -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<const Governance::Object>(govobj->Object()), nHash.ToString());
Expand Down
2 changes: 1 addition & 1 deletion src/governance/governance.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<CGovernanceObject>& govobj)
EXCLUSIVE_LOCKS_REQUIRED(cs_store, !cs_relay);

void RebuildIndexes()
Expand Down
4 changes: 2 additions & 2 deletions src/governance/net_governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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
Expand All @@ -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<int>(invs.size())));
for (const auto& inv : invs) {
m_peer_manager->PeerRelayInv(inv);
m_peer_manager->PeerPushInventory(peer.GetId(), inv);
}
}
}
Expand Down
15 changes: 11 additions & 4 deletions src/test/governance_vote_processing_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <util/time.h>
#include <validation.h>
#include <validationinterface.h>
#include <version.h>

#include <test/util/index.h>
#include <test/util/masternode.h>
Expand Down Expand Up @@ -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;
Expand Down
51 changes: 49 additions & 2 deletions test/functional/feature_governance.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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)
Expand Down Expand Up @@ -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("<IIB", 1, 0, 0)
requester.send_and_ping(msg_generic(b"govsync", ser_uint256(object_hash) + bloom_filter))
requester.wait_until(lambda: requester.governance_invs == {(inv_type, h) for h in expected_hashes})
bystander.sync_with_ping()
with p2p_lock:
assert_equal(bystander.governance_invs, set())
node.disconnect_p2ps()

def check_budget_cannot_be_kept_by_the_miner(self, node):
"""A superblock whose miner keeps the budget stays within the superblock limits, so only a node that knows
the trigger can tell it is invalid."""
Expand Down Expand Up @@ -223,6 +268,8 @@ def run_test(self):
self.wait_until(lambda: self.nodes[1].gobject("get", self.p2_hash)["FundingResult"]["YesCount"] == self.mn_count - 2, timeout = 5)
self.wait_until(lambda: self.nodes[1].gobject("get", self.p2_hash)["FundingResult"]["NoCount"] == 2, timeout = 5)

self.check_govsync_is_peer_local()

assert_equal(len(self.nodes[0].gobject("list", "valid", "triggers")), 0)
# 5 nodes voted on 3 proposals so we expect to see 15 votes total
assert_equal(self.nodes[0].gobject("count")["votes"], 15)
Expand Down
Loading