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
18 changes: 11 additions & 7 deletions src/governance/governance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1188,10 +1188,14 @@ void CGovernanceManager::RemoveInvalidVotes()

std::vector<COutPoint> changedKeyMNs;
for (const auto& [internalId, pdmnState] : diff.updatedMNs) {
auto oldDmn = lastMNListForVotingKeys->GetMNByInternalId(internalId);
// BuildDiff will construct itself with MNs that we already have knowledge
// of, meaning that fetch operations should never fail.
assert(oldDmn);
auto newDmn = tip_mn_list.GetMNByInternalId(internalId);
if (!Assume(newDmn)) {
continue;
}
auto oldDmn = lastMNListForVotingKeys->GetMN(newDmn->proTxHash);
if (!Assume(oldDmn)) {
continue;
}
if ((pdmnState.fields & CDeterministicMNStateDiff::Field_keyIDVoting) && pdmnState.state.keyIDVoting != oldDmn->pdmnState->keyIDVoting) {
changedKeyMNs.emplace_back(oldDmn->collateralOutpoint);
} else if ((pdmnState.fields & CDeterministicMNStateDiff::Field_pubKeyOperator) && pdmnState.state.pubKeyOperator != oldDmn->pdmnState->pubKeyOperator) {
Expand All @@ -1200,9 +1204,9 @@ void CGovernanceManager::RemoveInvalidVotes()
}
for (const auto& id : diff.removedMns) {
auto oldDmn = lastMNListForVotingKeys->GetMNByInternalId(id);
// BuildDiff will construct itself with MNs that we already have knowledge
// of, meaning that fetch operations should never fail.
assert(oldDmn);
if (!Assume(oldDmn)) {
continue;
}
changedKeyMNs.emplace_back(oldDmn->collateralOutpoint);
}

Expand Down
73 changes: 71 additions & 2 deletions src/test/governance_vote_processing_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <bls/bls.h>
#include <consensus/amount.h>
#include <evo/deterministicmns.h>
#include <evo/providertx.h>
#include <evo/specialtx.h>
#include <governance/governance.h>
#include <governance/object.h>
#include <governance/vote.h>
Expand Down Expand Up @@ -87,8 +89,8 @@ struct GovernanceVoteSetup : public TestChainSetup {
int64_t proposal_time{0};
std::string proposal_payment_address;

GovernanceVoteSetup() :
TestChainSetup(DIP3_ACTIVATION_HEIGHT - 2, ChainType::REGTEST, {"-dip3params=109:500"})
explicit GovernanceVoteSetup(const char* dip3_params = "-dip3params=109:500") :
TestChainSetup(DIP3_ACTIVATION_HEIGHT - 2, ChainType::REGTEST, {dip3_params})
{
// A failed BOOST_REQUIRE below throws, and a throwing constructor means no destructor runs.
// Tear the globals down by hand so one broken invariant here doesn't leave a live tx index
Expand Down Expand Up @@ -232,6 +234,73 @@ struct TestGovernanceStore : GovernanceStore {

BOOST_FIXTURE_TEST_SUITE(governance_vote_processing_tests, GovernanceVoteSetup)

struct GovernanceReorgSetup : GovernanceVoteSetup {
GovernanceReorgSetup() :
GovernanceVoteSetup("-dip3params=109:109")
{
}
};

BOOST_FIXTURE_TEST_CASE(votes_follow_masternode_identity_across_reorg, GovernanceReorgSetup)
{
auto& chainman = *m_node.chainman;
auto& govman = *m_node.govman;
CKey owner_key, other_owner_key, voting_key;
CBLSSecretKey operator_key, other_operator_key;
voting_key.MakeNewKey(true);
const auto registration = CreateProRegTx(chainman, utxos, 2, payout_script(), coinbaseKey, owner_key, operator_key);
const auto other_registration = CreateProRegTx(chainman, utxos, 3, payout_script(), coinbaseKey, other_owner_key,
other_operator_key);
MineBlock({registration});
CBlockIndex* fork_block{WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip())};
const auto old_id = tip_mn_list().GetMN(registration.GetHash())->GetInternalId();

CProUpRegTx update;
update.nVersion = ProTxVersion::GetMax(!bls::bls_legacy_scheme, /*is_extended_addr=*/false);
update.proTxHash = registration.GetHash();
update.pubKeyOperator.Set(operator_key.GetPublicKey(), /*specificLegacyScheme=*/bls::bls_legacy_scheme);
update.keyIDVoting = voting_key.GetPubKey().GetID();
update.scriptPayout = payout_script();
CMutableTransaction tx;
tx.nVersion = 3;
tx.nType = TRANSACTION_PROVIDER_UPDATE_REGISTRAR;
const auto spent = FundTransaction(chainman, tx, utxos, payout_script(), COIN);
update.inputsHash = CalcTxInputsHash(CTransaction(tx));
BOOST_REQUIRE(CHashSigner::SignHash(::SerializeHash(update), owner_key, update.vchSig));
SetTxPayload(tx, update);
SignTransaction(tx, spent, coinbaseKey);
MineBlock({tx});
govman.UpdatedBlockTip(WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip()));

const auto proposal = MakeProposal(uint256::ONE);
const auto parent_hash = proposal.GetHash();
govman.AddGovernanceObjectForTesting(proposal);
auto unchanged_vote = MakeVote(parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES);
SignWithVotingKey(unchanged_vote, mn_voting_key);
CGovernanceVote changed_vote{COutPoint{registration.GetHash(), 0}, parent_hash, VOTE_SIGNAL_FUNDING, VOTE_OUTCOME_YES};
SignWithVotingKey(changed_vote, voting_key);
CGovernanceException exception;
uint256 hash_to_request;
BOOST_REQUIRE(govman.ProcessVote(unchanged_vote, exception, hash_to_request));
BOOST_REQUIRE(govman.ProcessVote(changed_vote, exception, hash_to_request));
BOOST_REQUIRE(govman.HaveVoteForHash(changed_vote.GetHash()));

BlockValidationState state;
BOOST_REQUIRE(chainman.ActiveChainstate().InvalidateBlock(state, fork_block));
MineBlock({other_registration});
MineBlock({registration});
BOOST_REQUIRE(old_id != tip_mn_list().GetMN(registration.GetHash())->GetInternalId());
govman.UpdatedBlockTip(WITH_LOCK(::cs_main, return chainman.ActiveChain().Tip()));

BOOST_CHECK(!govman.HaveVoteForHash(changed_vote.GetHash()));
BOOST_CHECK(govman.GetCurrentVotes(parent_hash, changed_vote.GetMasternodeOutpoint()).empty());
BOOST_CHECK(govman.HaveVoteForHash(unchanged_vote.GetHash()));
BOOST_CHECK_EQUAL(govman.GetCurrentVotes(parent_hash, mn_collateral).size(), 1U);
const auto stored = govman.FindConstGovernanceObject(parent_hash);
BOOST_REQUIRE(stored != nullptr);
BOOST_CHECK_EQUAL(stored->GetAbsoluteYesCount(tip_mn_list(), VOTE_SIGNAL_FUNDING), 1);
}

// A vote that arrives before its parent object must be kept and replayed once the object shows up,
// otherwise every vote that races ahead of its proposal is lost for good.
BOOST_AUTO_TEST_CASE(orphan_vote_is_cached_and_applied_when_parent_arrives)
Expand Down
Loading