Repository navigation
Expand file tree
/
Copy pathgovernance_collateral_tests.cpp
More file actions
246 lines (212 loc) · 9.88 KB
/
Copy pathgovernance_collateral_tests.cpp
File metadata and controls
246 lines (212 loc) · 9.88 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// 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 <consensus/validation.h>
#include <dbwrapper.h>
#include <governance/collateral.h>
#include <governance/object.h>
#include <index/govcollateralindex.h>
#include <interfaces/chain.h>
#include <key.h>
#include <key_io.h>
#include <primitives/transaction.h>
#include <script/standard.h>
#include <util/strencodings.h>
#include <validation.h>
#include <validationinterface.h>
#include <test/util/index.h>
#include <test/util/masternode.h>
#include <test/util/setup_common.h>
#include <boost/test/unit_test.hpp>
#include <memory>
#include <string>
#include <vector>
namespace {
CScript BurnScript(const uint256& hash) { return CScript() << OP_RETURN << ToByteVector(hash); }
struct CollateralSetup : public TestChain100Setup {
std::unique_ptr<GovCollateralIndex> index;
SimpleUTXOMap utxos;
std::string payment_address;
CollateralSetup()
{
CKey payment_key;
payment_key.MakeNewKey(true);
payment_address = EncodeDestination(PKHash(payment_key.GetPubKey()));
// Matures a few coinbases so the burns below can be funded
for (int i = 0; i < 5; ++i) {
MineBlock({});
}
utxos = BuildSimpleUtxoMap(m_coinbase_txns);
}
~CollateralSetup()
{
SyncWithValidationInterfaceQueue();
if (index) index->Stop();
}
CScript ChangeScript() const { return GetScriptForDestination(PKHash(coinbaseKey.GetPubKey())); }
CBlock MineBlock(const std::vector<CMutableTransaction>& txns)
{
return CreateAndProcessBlock(txns, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
}
//! Mines `tx` and enough blocks on top of it for its collateral to be fully confirmed
void MineConfirmed(const CMutableTransaction& tx)
{
MineBlock({tx});
for (int i = 1; i < GOVERNANCE_FEE_CONFIRMATIONS; ++i) {
MineBlock({});
}
}
CMutableTransaction MakeBurn(const uint256& hash, CAmount amount, const std::vector<CTxOut>& extra_outputs = {})
{
CMutableTransaction tx;
const auto spent{FundTransaction(*m_node.chainman, tx, utxos, BurnScript(hash), amount, ChangeScript())};
tx.vout.insert(tx.vout.end(), extra_outputs.begin(), extra_outputs.end());
SignTransaction(tx, spent, coinbaseKey);
return tx;
}
void CreateIndex()
{
// A block notification still queued from MineBlock would reach the index after it synced past that block
SyncWithValidationInterfaceQueue();
index = std::make_unique<GovCollateralIndex>(interfaces::MakeChain(m_node), /*n_cache_size=*/0, /*f_memory=*/true);
}
void StartIndex()
{
CreateIndex();
BOOST_REQUIRE(index->Start());
IndexWaitSynced(*index);
}
//! The object hash leaves out the collateral hash, so every call returns the same proposal
CGovernanceObject MakeProposal(const uint256& collateral_hash) const
{
const std::string data{
R"({"type":1,"name":"proposal","start_epoch":1700000000,"end_epoch":1700100000,"payment_amount":1.0,"payment_address":")" +
payment_address + R"(","url":"https://dash.org"})"};
return CGovernanceObject{uint256{}, /*revision=*/1, /*nTime=*/1700000000, collateral_hash, HexStr(data)};
}
bool IsCollateralValid(const CGovernanceObject& govobj, std::string& error) const
{
bool missing_confirmations;
return WITH_LOCK(::cs_main,
return govobj.IsCollateralValid(index.get(), *m_node.chainman, error, missing_confirmations));
}
};
} // namespace
BOOST_FIXTURE_TEST_SUITE(governance_collateral_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(collateral_info_counts_only_exact_burn_scripts)
{
const uint256 hash{uint256S("aa")};
const uint256 other_hash{uint256S("bb")};
std::vector<unsigned char> short_payload(31, 0xcc);
CMutableTransaction tx;
tx.vout.emplace_back(COIN, GetScriptForDestination(PKHash(uint160{})));
tx.vout.emplace_back(2 * COIN, BurnScript(hash));
tx.vout.emplace_back(3 * COIN, CScript() << OP_RETURN << short_payload);
// As long as a 32-byte burn, but pushed with OP_PUSHDATA1
CScript pushdata_burn;
pushdata_burn << OP_RETURN << OP_PUSHDATA1;
pushdata_burn.push_back(31);
pushdata_burn.insert(pushdata_burn.end(), other_hash.begin(), other_hash.end() - 1);
BOOST_REQUIRE_EQUAL(pushdata_burn.size(), BurnScript(other_hash).size());
tx.vout.emplace_back(4 * COIN, pushdata_burn);
const uint256 block_hash{uint256S("01")};
const auto info{governance::GetCollateralInfo(CTransaction{tx}, block_hash)};
BOOST_CHECK(info.block_hash == block_hash);
BOOST_CHECK(info.outputs_standard);
BOOST_REQUIRE_EQUAL(info.burns.size(), 1U);
BOOST_CHECK(info.BurnsAtLeast(hash, 2 * COIN));
BOOST_CHECK(!info.BurnsAtLeast(hash, 2 * COIN + 1));
BOOST_CHECK(!info.BurnsAtLeast(other_hash, 1));
tx.vout.emplace_back(COIN, GetScriptForDestination(ScriptHash(uint160{})));
BOOST_CHECK(!governance::GetCollateralInfo(CTransaction{tx}, block_hash).outputs_standard);
}
BOOST_FIXTURE_TEST_CASE(index_records_transactions_burning_the_proposal_fee, CollateralSetup)
{
const uint256 fee_hash{uint256S("aa")};
const uint256 short_hash{uint256S("bb")};
// Its smaller burn can never pay for a proposal, so the index leaves it out
const auto fee_burn{MakeBurn(fee_hash, GOVERNANCE_PROPOSAL_FEE_TX, {CTxOut{0, BurnScript(short_hash)}})};
const auto short_burn{MakeBurn(short_hash, GOVERNANCE_PROPOSAL_FEE_TX - 1)};
const CBlock block{MineBlock({fee_burn, short_burn})};
// Blocks connected before the index started
StartIndex();
const auto collateral{index->FindCollateral(fee_burn.GetHash())};
BOOST_REQUIRE(collateral);
BOOST_CHECK(collateral->block_hash == block.GetHash());
BOOST_CHECK(collateral->outputs_standard);
BOOST_CHECK(collateral->BurnsAtLeast(fee_hash, GOVERNANCE_PROPOSAL_FEE_TX));
BOOST_CHECK_EQUAL(collateral->burns.size(), 1U);
BOOST_CHECK(!index->FindCollateral(short_burn.GetHash()));
BOOST_CHECK(!index->FindCollateral(block.vtx[0]->GetHash()));
// Blocks connected while the index is running
const auto later_burn{MakeBurn(uint256S("cc"), GOVERNANCE_PROPOSAL_FEE_TX)};
const CBlock later_block{MineBlock({later_burn})};
BOOST_REQUIRE(index->BlockUntilSyncedToCurrentChain());
const auto later_collateral{index->FindCollateral(later_burn.GetHash())};
BOOST_REQUIRE(later_collateral);
BOOST_CHECK(later_collateral->block_hash == later_block.GetHash());
// Blocks reorged away
BlockValidationState state;
m_node.chainman->ActiveChainstate().InvalidateBlock(state, WITH_LOCK(::cs_main, return m_node.chainman->ActiveChain().Tip()));
MineBlock({});
BOOST_REQUIRE(index->BlockUntilSyncedToCurrentChain());
BOOST_CHECK(!index->FindCollateral(later_burn.GetHash()));
BOOST_CHECK(index->FindCollateral(fee_burn.GetHash()));
}
BOOST_FIXTURE_TEST_CASE(proposal_collateral_is_checked_against_the_index, CollateralSetup)
{
const auto burn{MakeBurn(MakeProposal(uint256{}).GetHash(), GOVERNANCE_PROPOSAL_FEE_TX)};
const CGovernanceObject proposal{MakeProposal(burn.GetHash())};
MineConfirmed(burn);
// A collateral burning to a different object hash
const auto other_burn{MakeBurn(uint256S("aa"), GOVERNANCE_PROPOSAL_FEE_TX)};
const CGovernanceObject wrong_burn{MakeProposal(other_burn.GetHash())};
MineConfirmed(other_burn);
std::string error;
BOOST_CHECK(!IsCollateralValid(proposal, error));
BOOST_CHECK_EQUAL(error, "Can't look up collateral, the governance collateral index isn't running");
CreateIndex();
BOOST_CHECK(!IsCollateralValid(proposal, error));
BOOST_CHECK_EQUAL(error, "Can't look up collateral until the governance collateral index has synced");
StartIndex();
BOOST_CHECK_MESSAGE(IsCollateralValid(proposal, error), error);
BOOST_CHECK(!IsCollateralValid(wrong_burn, error));
BOOST_CHECK(error.find("Couldn't find opReturn") == 0);
}
// Startup resumes from the tip a skipped commit recorded, so the sync check must only need the blocks above it
BOOST_FIXTURE_TEST_CASE(sync_check_needs_only_blocks_above_the_resume_point, TestChain100Setup)
{
Chainstate& chainstate{Assert(m_node.chainman)->ActiveChainstate()};
// Nothing has been flushed, so the end-of-sync commit is skipped and only the uncommitted tip (100) is recorded
BOOST_REQUIRE(WITH_LOCK(::cs_main, return !chainstate.GetLastFlushedBlock()));
{
GovCollateralIndex synced_index{interfaces::MakeChain(m_node), /*n_cache_size=*/1 << 20};
BOOST_REQUIRE(synced_index.Start());
IndexWaitSynced(synced_index);
synced_index.Stop();
}
for (int i = 0; i < 3; ++i) {
CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
}
const auto can_sync_without_data_at = [&](int height) {
GovCollateralIndex index{interfaces::MakeChain(m_node), /*n_cache_size=*/1 << 20};
LOCK(::cs_main);
CBlockIndex* pruned{chainstate.m_chain[height]};
pruned->nStatus &= ~BLOCK_HAVE_DATA;
const bool can_sync{index.CanSync(chainstate)};
pruned->nStatus |= BLOCK_HAVE_DATA;
return can_sync;
};
BOOST_CHECK(can_sync_without_data_at(50));
BOOST_CHECK(!can_sync_without_data_at(102));
// Startup fails when the block index lacks the locator's block, so the sync check must not rely on the
// uncommitted tip then
{
CDBWrapper db{DBParams{.path = m_args.GetDataDirNet() / "indexes" / "govcollateralindex", .cache_bytes = 1 << 20}};
CDBBatch batch{db};
batch.Write(uint8_t{'B'}, CBlockLocator{{uint256::ONE}});
BOOST_REQUIRE(db.WriteBatch(batch));
}
BOOST_CHECK(!can_sync_without_data_at(50));
}
BOOST_AUTO_TEST_SUITE_END()