forked from dashpay/dash
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevo_trivialvalidation.cpp
More file actions
261 lines (224 loc) · 11 KB
/
Copy pathevo_trivialvalidation.cpp
File metadata and controls
261 lines (224 loc) · 11 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright (c) 2021-2025 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 <test/data/trivially_invalid.json.h>
#include <test/data/trivially_valid.json.h>
#include <test/util/json.h>
#include <test/util/setup_common.h>
#include <bls/bls.h>
#include <chain.h>
#include <chainparams.h>
#include <deploymentstatus.h>
#include <evo/dmn_types.h>
#include <evo/netinfo.h>
#include <evo/providertx.h>
#include <evo/specialtxman.h>
#include <key.h>
#include <primitives/transaction.h>
#include <script/script.h>
#include <script/standard.h>
#include <util/std23.h>
#include <validation.h>
#include <boost/test/unit_test.hpp>
#include <univalue.h>
struct TestChain100V19Activation : public TestChain100Setup {
TestChain100V19Activation()
: TestChain100Setup{CBaseChainParams::REGTEST, {"-testactivationheight=v19@100"}} {}
};
BOOST_FIXTURE_TEST_SUITE(evo_trivialvalidation, TestChain100V19Activation)
template <class T>
void TestTxHelper(const CMutableTransaction& tx, gsl::not_null<const CBlockIndex*> pindexPrev,
const std::optional<std::string>& expected_error, const ChainstateManager& chainman)
{
TxValidationState dummy_state;
auto opt_payload = GetValidatedPayload<T>(CTransaction{tx}, pindexPrev, chainman, dummy_state);
BOOST_CHECK_EQUAL(opt_payload.has_value(), !expected_error.has_value());
if (expected_error.has_value()) {
BOOST_CHECK_EQUAL(dummy_state.GetRejectReason(), expected_error.value());
}
}
void trivialvalidation_runner(ChainstateManager& chainman, const std::string& json)
{
LOCK(::cs_main);
const UniValue vectors = read_json(json);
for (size_t idx = 1; idx < vectors.size(); idx++) {
const UniValue& test = vectors[idx];
uint256 txHash;
std::string txType;
CMutableTransaction tx;
std::optional<std::string> expected_err;
try {
// Additional data
txHash = uint256S(test[0].get_str());
BOOST_TEST_MESSAGE("tx: " << test[0].get_str());
txType = test[1].get_str();
BOOST_CHECK(test[2].get_str() == "basic" || test[2].get_str() == "legacy");
// Determine which pindexPrev to supply based on whether we want to validate legacy or basic
// TODO: Introduce trivial validation test vectors for extended addresses
const CBlockIndex* pindexPrev{(test[2].get_str() == "basic") ? chainman.ActiveChain()[100]
: chainman.ActiveChain()[98]};
assert(pindexPrev);
// Raw transaction
CDataStream stream(ParseHex(test[3].get_str()), SER_NETWORK, PROTOCOL_VERSION);
stream >> tx;
expected_err = (test.size() > 4) ? std::make_optional(test[4].get_str()) : std::nullopt;
// Sanity check
BOOST_CHECK_EQUAL(tx.nVersion, 3);
BOOST_CHECK_EQUAL(tx.GetHash(), txHash);
// Deserialization based on transaction nType
switch (tx.nType) {
case TRANSACTION_PROVIDER_REGISTER: {
BOOST_CHECK_EQUAL(txType, "proregtx");
TestTxHelper<CProRegTx>(tx, pindexPrev, expected_err, chainman);
break;
}
case TRANSACTION_PROVIDER_UPDATE_SERVICE: {
BOOST_CHECK_EQUAL(txType, "proupservtx");
TestTxHelper<CProUpServTx>(tx, pindexPrev, expected_err, chainman);
break;
}
case TRANSACTION_PROVIDER_UPDATE_REGISTRAR: {
BOOST_CHECK_EQUAL(txType, "proupregtx");
TestTxHelper<CProUpRegTx>(tx, pindexPrev, expected_err, chainman);
break;
}
case TRANSACTION_PROVIDER_UPDATE_REVOKE: {
BOOST_CHECK_EQUAL(txType, "prouprevtx");
TestTxHelper<CProUpRevTx>(tx, pindexPrev, expected_err, chainman);
break;
}
default:
// TRANSACTION_COINBASE and TRANSACTION_NORMAL
// are not subject to trivial validation checks
BOOST_CHECK(false);
}
} catch (...) {
BOOST_ERROR("Bad test, couldn't deserialize data: " << test.write());
continue;
}
}
}
BOOST_AUTO_TEST_CASE(trivialvalidation_valid)
{
const std::string json(json_tests::trivially_valid, json_tests::trivially_valid + sizeof(json_tests::trivially_valid));
bls::bls_legacy_scheme.store(true);
trivialvalidation_runner(*m_node.chainman, json);
bls::bls_legacy_scheme.store(false);
trivialvalidation_runner(*m_node.chainman, json);
}
BOOST_AUTO_TEST_CASE(trivialvalidation_invalid)
{
const std::string json(json_tests::trivially_invalid, json_tests::trivially_invalid + sizeof(json_tests::trivially_invalid));
bls::bls_legacy_scheme.store(true);
trivialvalidation_runner(*m_node.chainman, json);
bls::bls_legacy_scheme.store(false);
trivialvalidation_runner(*m_node.chainman, json);
}
static CScript NewP2PKHScript(CKey& key)
{
key.MakeNewKey(true);
return GetScriptForDestination(PKHash(key.GetPubKey()));
}
static void CheckPayouts(const MasternodePayoutShares& payouts, const CKeyID& owner, const CKeyID& voting,
const std::optional<std::string>& expected_error)
{
TxValidationState state;
BOOST_CHECK_EQUAL(IsPayoutListTriviallyValid(payouts, owner, voting, state), !expected_error.has_value());
if (expected_error.has_value()) {
BOOST_CHECK_EQUAL(state.GetRejectReason(), *expected_error);
}
}
BOOST_AUTO_TEST_CASE(multipayout_list_validation)
{
CKey owner_key, voting_key, payout_key1, payout_key2, payout_key3;
const CScript payout1 = NewP2PKHScript(payout_key1);
const CScript payout2 = NewP2PKHScript(payout_key2);
const CScript payout3 = NewP2PKHScript(payout_key3);
owner_key.MakeNewKey(true);
voting_key.MakeNewKey(true);
const CKeyID owner_id = owner_key.GetPubKey().GetID();
const CKeyID voting_id = voting_key.GetPubKey().GetID();
CheckPayouts({{payout1, 10000}}, owner_id, voting_id, std::nullopt);
CheckPayouts({{payout1, 5000}, {payout2, 3000}, {payout3, 2000}}, owner_id, voting_id, std::nullopt);
CheckPayouts({}, owner_id, voting_id, "bad-protx-payouts-count");
MasternodePayoutShares too_many;
for (int i = 0; i < 9; ++i) {
CKey key;
too_many.emplace_back(NewP2PKHScript(key), 1000);
}
CheckPayouts(too_many, owner_id, voting_id, "bad-protx-payouts-count");
CheckPayouts({{payout1, 99}, {payout2, 9901}}, owner_id, voting_id, "bad-protx-payout-reward");
CheckPayouts({{payout1, MasternodePayoutShare::MAX_REWARD + 1}}, owner_id, voting_id, "bad-protx-payout-reward");
CheckPayouts({{payout1, 5000}, {payout2, 4999}}, owner_id, voting_id, "bad-protx-payout-reward-sum");
CheckPayouts({{payout1, 5000}, {payout1, 5000}}, owner_id, voting_id, "bad-protx-payee-dup");
CheckPayouts({{CScript() << OP_RETURN, 10000}}, owner_id, voting_id, "bad-protx-payee");
CheckPayouts({{GetScriptForDestination(PKHash(owner_id)), 10000}}, owner_id, voting_id, "bad-protx-payee-reuse");
CheckPayouts({{GetScriptForDestination(PKHash(voting_id)), 10000}}, owner_id, voting_id, "bad-protx-payee-reuse");
CheckPayouts({{GetScriptForDestination(PKHash(CKeyID{})), 10000}}, CKeyID{}, voting_id, std::nullopt);
CheckPayouts({{GetScriptForDestination(PKHash(voting_id)), 10000}}, CKeyID{}, voting_id, "bad-protx-payee-reuse");
TxValidationState state;
BOOST_CHECK(!IsPayoutListKeySafe({{payout1, 10000}}, CTxDestination(PKHash(payout_key1.GetPubKey().GetID())),
owner_id, voting_id, /*check_payout_collateral_reuse=*/true, state));
BOOST_CHECK_EQUAL(state.GetRejectReason(), "bad-protx-payee-reuse");
state = TxValidationState{};
BOOST_CHECK(IsPayoutListKeySafe({{payout1, 10000}}, CTxDestination(PKHash(payout_key1.GetPubKey().GetID())),
owner_id, voting_id, /*check_payout_collateral_reuse=*/false, state));
CScript p2sh_collateral = GetScriptForDestination(ScriptHash(payout1));
CTxDestination p2sh_dest;
BOOST_CHECK(ExtractDestination(p2sh_collateral, p2sh_dest));
state = TxValidationState{};
BOOST_CHECK(!IsPayoutListKeySafe({{p2sh_collateral, 10000}}, p2sh_dest, owner_id, voting_id,
/*check_payout_collateral_reuse=*/true, state));
BOOST_CHECK_EQUAL(state.GetRejectReason(), "bad-protx-payee-reuse");
}
// Regression: CProUpServTx::IsTriviallyValid must reject out-of-range nType,
// matching CProRegTx::IsTriviallyValid and the block-connect check in BuildNewListFromBlock.
// Without this, a ProUpServTx with nType >= MnType::COUNT is mempool-valid but block-invalid,
// which makes CreateNewBlock/getblocktemplate throw once the tx is relayed.
BOOST_AUTO_TEST_CASE(proupserv_rejects_invalid_ntype)
{
auto make_proupserv = [](MnType nType) {
CProUpServTx proTx;
proTx.nVersion = ProTxVersion::BasicBLS;
proTx.nType = nType;
proTx.netInfo = NetInfoInterface::MakeNetInfo(proTx.nVersion);
// Avoid the mainnet default port (9999); regtest rejects it for CORE_P2P.
BOOST_REQUIRE_EQUAL(proTx.netInfo->AddEntry(NetInfoPurpose::CORE_P2P, "1.1.1.1:1000"),
NetInfoStatus::Success);
return proTx;
};
// Valid types must still pass trivial validation.
{
TxValidationState state;
BOOST_CHECK(make_proupserv(MnType::Regular).IsTriviallyValid(state));
state = {};
BOOST_CHECK(make_proupserv(MnType::Evo).IsTriviallyValid(state));
}
// Out-of-range nType (raw uint16 deserialised into the enum) must be rejected.
for (const MnType bad_type : {static_cast<MnType>(2), static_cast<MnType>(42), static_cast<MnType>(0xFFFF)}) {
TxValidationState state;
BOOST_CHECK_MESSAGE(!make_proupserv(bad_type).IsTriviallyValid(state),
"nType=" << std23::to_underlying(bad_type) << " should be rejected");
BOOST_CHECK_EQUAL(state.GetRejectReason(), "bad-protx-type");
}
// ProRegTx already has the same guard; keep the two special-tx types aligned.
{
CProRegTx proReg;
proReg.nVersion = ProTxVersion::BasicBLS;
proReg.nType = static_cast<MnType>(42);
proReg.netInfo = NetInfoInterface::MakeNetInfo(proReg.nVersion);
CKey owner_key, voting_key, payout_key;
owner_key.MakeNewKey(true);
voting_key.MakeNewKey(true);
proReg.keyIDOwner = owner_key.GetPubKey().GetID();
proReg.keyIDVoting = voting_key.GetPubKey().GetID();
proReg.scriptPayout = NewP2PKHScript(payout_key);
CBLSSecretKey operator_key;
operator_key.MakeNewKey();
proReg.pubKeyOperator.Set(operator_key.GetPublicKey(), /*legacy=*/false);
TxValidationState state;
BOOST_CHECK(!proReg.IsTriviallyValid(state));
BOOST_CHECK_EQUAL(state.GetRejectReason(), "bad-protx-type");
}
}
BOOST_AUTO_TEST_SUITE_END()