Expand file tree
/
Copy pathgovernance_superblock_tests.cpp
More file actions
150 lines (132 loc) · 7.86 KB
/
Copy pathgovernance_superblock_tests.cpp
File metadata and controls
150 lines (132 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright (c) 2026 The Dash Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <addresstype.h>
#include <chainparams.h>
#include <consensus/amount.h>
#include <governance/superblock.h>
#include <masternode/payments.h>
#include <key.h>
#include <primitives/transaction.h>
#include <pubkey.h>
#include <script/script.h>
#include <test/util/setup_common.h>
#include <boost/test/unit_test.hpp>
#include <algorithm>
#include <cstdint>
#include <limits>
#include <vector>
struct SuperblockRegtestSetup : public BasicTestingSetup {
SuperblockRegtestSetup() : BasicTestingSetup(ChainType::REGTEST) {}
};
BOOST_FIXTURE_TEST_SUITE(governance_superblock_tests, SuperblockRegtestSetup)
// Regression test for: CSuperblock::IsValid was matching expected payments
// against coinbase outputs using a forward scan that re-started at the
// previously matched index, which allowed two adjacent expected payments
// with identical scriptPubKey and amount to both match the same coinbase
// output. Each expected payment must consume a distinct output.
BOOST_AUTO_TEST_CASE(isvalid_duplicate_payments_require_distinct_outputs)
{
const auto& consensus = Params().GetConsensus();
const int nBlockHeight = consensus.nSuperblockStartBlock + consensus.nSuperblockCycle;
BOOST_REQUIRE(CSuperblock::IsValidBlockHeight(nBlockHeight));
CKey key;
key.MakeNewKey(/*fCompressed=*/true);
const CTxDestination dest{PKHash(key.GetPubKey())};
const CScript scriptPayee = GetScriptForDestination(dest);
const CAmount nPayAmount = 1 * COIN;
// Two identical expected payments (same script, same amount).
const SuperblockStatus superblock{.state = SuperblockStatus::State::Triggered, .payments = {{nPayAmount, scriptPayee}, {nPayAmount, scriptPayee}}};
const CAmount nPaymentsLimit = CSuperblock::GetPaymentsLimit(nBlockHeight);
const CScript scriptMinerOrMN = CScript() << OP_RETURN;
const CAmount blockReward = 500 * COIN;
// Case 1 (regression, V24): coinbase carries only ONE output matching the
// duplicate expected payment. With the buggy forward scan that restarted
// at the previously matched index, both expected payments would match the
// single matching vout and IsValid would (incorrectly) return true.
// From V24 on, the second expected payment must find a distinct output
// and validation must fail.
{
CMutableTransaction txNew;
txNew.vout.emplace_back(blockReward - nPayAmount, scriptMinerOrMN);
txNew.vout.emplace_back(nPayAmount, scriptPayee); // single matching output
BOOST_CHECK(!IsSuperblockValid(superblock, CTransaction(txNew), blockReward, /*is_v24=*/true, nPaymentsLimit));
}
// Case 2 (V24): coinbase carries TWO outputs matching the duplicate expected
// payments. The fix must still accept this legitimate case.
{
CMutableTransaction txNew;
txNew.vout.emplace_back(blockReward - 2 * nPayAmount, scriptMinerOrMN);
txNew.vout.emplace_back(nPayAmount, scriptPayee);
txNew.vout.emplace_back(nPayAmount, scriptPayee);
BOOST_CHECK(IsSuperblockValid(superblock, CTransaction(txNew), blockReward, /*is_v24=*/true, nPaymentsLimit));
}
// Case 3 (pre-V24): the stricter distinct-output rule is gated behind V24.
// Before activation the legacy scan is preserved, so the single-output
// coinbase from Case 1 must still be accepted to avoid changing consensus
// for already-validated history.
{
CMutableTransaction txNew;
txNew.vout.emplace_back(blockReward - nPayAmount, scriptMinerOrMN);
txNew.vout.emplace_back(nPayAmount, scriptPayee); // single matching output
BOOST_CHECK(IsSuperblockValid(superblock, CTransaction(txNew), blockReward, /*is_v24=*/false, nPaymentsLimit));
}
}
// The masternode building a trigger estimates the superblock's time from its own height and clock, within the last
// maturity window before the superblock, and accepts a proposal whose window is at most two hours off that estimate.
// Counting from any superblock, the payments left in the window are the chosen superblocks not yet behind it.
BOOST_AUTO_TEST_CASE(proposal_window_holds_exactly_the_chosen_superblocks)
{
const int cycle{16616};
const int maturity_window{1662};
const int next_superblock{155 * cycle};
const int64_t now{1'800'000'000};
const int64_t fudge{2 * 60 * 60};
for (const double block_seconds : {2.62 * 60 * 0.99, 2.62 * 60, 2.62 * 60 * 1.01}) {
for (const int blocks_to_next : {maturity_window + 1, cycle / 2 - 1, cycle / 2 + 1, cycle - 1}) {
const int tip{next_superblock - blocks_to_next};
const auto clock_at = [&](int height) { return static_cast<int64_t>(now + (height - tip) * block_seconds); };
for (int skipped{0}; skipped < 12; ++skipped) {
for (int payments{1}; payments <= 12; ++payments) {
const int first_superblock{next_superblock + skipped * cycle};
const auto window{governance::EstimateProposalWindow(first_superblock, payments, cycle, tip, now)};
for (int n{-1}; n <= payments; ++n) {
const int superblock{first_superblock + n * cycle};
if (superblock <= tip) continue;
const bool chosen{n >= 0 && n < payments};
const int64_t slack{chosen ? 0 : fudge};
for (const int trigger_height : {superblock - maturity_window, superblock - 1}) {
const int64_t trigger_time{clock_at(trigger_height)};
const int64_t time{governance::EstimateBlockTime(superblock, trigger_height, trigger_time)};
const bool inside{window.start_epoch - slack <= time && time <= window.end_epoch + slack};
BOOST_CHECK_MESSAGE(inside == chosen,
"blocks_to_next=" << blocks_to_next << " skipped=" << skipped
<< " payments=" << payments << " n=" << n
<< " trigger_height=" << trigger_height);
BOOST_CHECK_EQUAL(governance::CountRemainingPayments(window, superblock, cycle,
trigger_height, trigger_time),
payments - std::max(n, 0));
}
}
}
}
}
}
const int tip{next_superblock - cycle / 2};
const int64_t cycle_seconds{governance::EstimateBlockTime(cycle, 0, 0)};
BOOST_CHECK_EQUAL(governance::CountRemainingPayments({now - 3 * cycle_seconds, now - cycle_seconds},
next_superblock, cycle, tip, now),
0);
BOOST_CHECK_EQUAL(governance::CountRemainingPayments({now, now + cycle_seconds}, next_superblock,
/*superblock_cycle=*/0, tip, now),
0);
BOOST_CHECK_EQUAL(governance::CountRemainingPayments({std::numeric_limits<int64_t>::min(),
std::numeric_limits<int64_t>::max()},
next_superblock, cycle, tip, now),
(std::numeric_limits<int>::max() - next_superblock) / cycle);
BOOST_CHECK_EQUAL(
governance::CountRemainingPayments({std::numeric_limits<int64_t>::min(), std::numeric_limits<int64_t>::max()},
/*next_superblock=*/0, /*superblock_cycle=*/1, /*ref_height=*/0, now),
std::numeric_limits<int>::max());
}
BOOST_AUTO_TEST_SUITE_END()