Branch data Line data Source code
1 : : // Copyright (c) 2011-2022 The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <test/data/tx_invalid.json.h>
6 : : #include <test/data/tx_valid.json.h>
7 : : #include <test/util/setup_common.h>
8 : :
9 : : #include <checkqueue.h>
10 : : #include <clientversion.h>
11 : : #include <consensus/amount.h>
12 : : #include <consensus/tx_check.h>
13 : : #include <consensus/tx_verify.h>
14 : : #include <consensus/validation.h>
15 : : #include <core_io.h>
16 : : #include <key.h>
17 : : #include <policy/policy.h>
18 : : #include <policy/settings.h>
19 : : #include <script/script.h>
20 : : #include <script/script_error.h>
21 : : #include <script/sigcache.h>
22 : : #include <script/sign.h>
23 : : #include <script/signingprovider.h>
24 : : #include <script/solver.h>
25 : : #include <streams.h>
26 : : #include <test/util/json.h>
27 : : #include <test/util/random.h>
28 : : #include <test/util/script.h>
29 : : #include <test/util/transaction_utils.h>
30 : : #include <util/strencodings.h>
31 : : #include <util/string.h>
32 : : #include <util/transaction_identifier.h>
33 : : #include <validation.h>
34 : :
35 : : #include <functional>
36 : : #include <map>
37 : : #include <string>
38 : :
39 : : #include <boost/test/unit_test.hpp>
40 : :
41 : : #include <univalue.h>
42 : :
43 : : using namespace util::hex_literals;
44 : : using util::SplitString;
45 : : using util::ToString;
46 : :
47 : : typedef std::vector<unsigned char> valtype;
48 : :
49 : : static CFeeRate g_dust{DUST_RELAY_TX_FEE};
50 : : static bool g_bare_multi{DEFAULT_PERMIT_BAREMULTISIG};
51 : :
52 : : static std::map<std::string, unsigned int> mapFlagNames = {
53 : : {std::string("P2SH"), (unsigned int)SCRIPT_VERIFY_P2SH},
54 : : {std::string("STRICTENC"), (unsigned int)SCRIPT_VERIFY_STRICTENC},
55 : : {std::string("DERSIG"), (unsigned int)SCRIPT_VERIFY_DERSIG},
56 : : {std::string("LOW_S"), (unsigned int)SCRIPT_VERIFY_LOW_S},
57 : : {std::string("SIGPUSHONLY"), (unsigned int)SCRIPT_VERIFY_SIGPUSHONLY},
58 : : {std::string("MINIMALDATA"), (unsigned int)SCRIPT_VERIFY_MINIMALDATA},
59 : : {std::string("NULLDUMMY"), (unsigned int)SCRIPT_VERIFY_NULLDUMMY},
60 : : {std::string("DISCOURAGE_UPGRADABLE_NOPS"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS},
61 : : {std::string("CLEANSTACK"), (unsigned int)SCRIPT_VERIFY_CLEANSTACK},
62 : : {std::string("MINIMALIF"), (unsigned int)SCRIPT_VERIFY_MINIMALIF},
63 : : {std::string("NULLFAIL"), (unsigned int)SCRIPT_VERIFY_NULLFAIL},
64 : : {std::string("CHECKLOCKTIMEVERIFY"), (unsigned int)SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY},
65 : : {std::string("CHECKSEQUENCEVERIFY"), (unsigned int)SCRIPT_VERIFY_CHECKSEQUENCEVERIFY},
66 : : {std::string("WITNESS"), (unsigned int)SCRIPT_VERIFY_WITNESS},
67 : : {std::string("DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM},
68 : : {std::string("WITNESS_PUBKEYTYPE"), (unsigned int)SCRIPT_VERIFY_WITNESS_PUBKEYTYPE},
69 : : {std::string("CONST_SCRIPTCODE"), (unsigned int)SCRIPT_VERIFY_CONST_SCRIPTCODE},
70 : : {std::string("TAPROOT"), (unsigned int)SCRIPT_VERIFY_TAPROOT},
71 : : {std::string("DISCOURAGE_UPGRADABLE_PUBKEYTYPE"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE},
72 : : {std::string("DISCOURAGE_OP_SUCCESS"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS},
73 : : {std::string("DISCOURAGE_UPGRADABLE_TAPROOT_VERSION"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION},
74 : : };
75 : :
76 : 1416 : unsigned int ParseScriptFlags(std::string strFlags)
77 : : {
78 : 1416 : unsigned int flags = SCRIPT_VERIFY_NONE;
79 [ + + + + ]: 1416 : if (strFlags.empty() || strFlags == "NONE") return flags;
80 : :
81 : 1216 : std::vector<std::string> words = SplitString(strFlags, ',');
82 [ + + ]: 3473 : for (const std::string& word : words)
83 : : {
84 [ - + ]: 2257 : if (!mapFlagNames.count(word))
85 [ # # # # ]: 0 : BOOST_ERROR("Bad test: unknown verification flag '" << word << "'");
86 [ + - ]: 2257 : flags |= mapFlagNames[word];
87 : : }
88 : 1216 : return flags;
89 : 1216 : }
90 : :
91 : : // Check that all flags in STANDARD_SCRIPT_VERIFY_FLAGS are present in mapFlagNames.
92 : 1 : bool CheckMapFlagNames()
93 : : {
94 : 1 : unsigned int standard_flags_missing{STANDARD_SCRIPT_VERIFY_FLAGS};
95 [ + + ]: 22 : for (const auto& pair : mapFlagNames) {
96 : 21 : standard_flags_missing &= ~(pair.second);
97 : : }
98 : 1 : return standard_flags_missing == 0;
99 : : }
100 : :
101 : 134 : std::string FormatScriptFlags(unsigned int flags)
102 : : {
103 [ + + ]: 134 : if (flags == SCRIPT_VERIFY_NONE) {
104 : 39 : return "";
105 : : }
106 : 95 : std::string ret;
107 : 95 : std::map<std::string, unsigned int>::const_iterator it = mapFlagNames.begin();
108 [ + + ]: 2090 : while (it != mapFlagNames.end()) {
109 [ + + ]: 1995 : if (flags & it->second) {
110 [ + - ]: 334 : ret += it->first + ",";
111 : : }
112 : 1995 : it++;
113 : : }
114 [ + - ]: 95 : return ret.substr(0, ret.size() - 1);
115 : 95 : }
116 : :
117 : : /*
118 : : * Check that the input scripts of a transaction are valid/invalid as expected.
119 : : */
120 : 8962 : bool CheckTxScripts(const CTransaction& tx, const std::map<COutPoint, CScript>& map_prevout_scriptPubKeys,
121 : : const std::map<COutPoint, int64_t>& map_prevout_values, unsigned int flags,
122 : : const PrecomputedTransactionData& txdata, const std::string& strTest, bool expect_valid)
123 : : {
124 : 8962 : bool tx_valid = true;
125 [ + + ]: 8962 : ScriptError err = expect_valid ? SCRIPT_ERR_UNKNOWN_ERROR : SCRIPT_ERR_OK;
126 [ + + + + ]: 20623 : for (unsigned int i = 0; i < tx.vin.size() && tx_valid; ++i) {
127 : 11661 : const CTxIn input = tx.vin[i];
128 [ + - + + ]: 11661 : const CAmount amount = map_prevout_values.count(input.prevout) ? map_prevout_values.at(input.prevout) : 0;
129 : 11661 : try {
130 [ + - + - : 11661 : tx_valid = VerifyScript(input.scriptSig, map_prevout_scriptPubKeys.at(input.prevout),
+ + ]
131 [ + - ]: 11661 : &input.scriptWitness, flags, TransactionSignatureChecker(&tx, i, amount, txdata, MissingDataBehavior::ASSERT_FAIL), &err);
132 : 0 : } catch (...) {
133 [ - - - - : 0 : BOOST_ERROR("Bad test: " << strTest);
- - ]
134 : 0 : return true; // The test format is bad and an error is thrown. Return true to silence further error.
135 [ - - ]: 0 : }
136 [ + + ]: 11661 : if (expect_valid) {
137 [ + - + - : 15280 : BOOST_CHECK_MESSAGE(tx_valid, strTest);
+ - ]
138 [ + - + - : 15280 : BOOST_CHECK_MESSAGE((err == SCRIPT_ERR_OK), ScriptErrorString(err));
+ - ]
139 : 7640 : err = SCRIPT_ERR_UNKNOWN_ERROR;
140 : : }
141 : 11661 : }
142 [ + + ]: 8962 : if (!expect_valid) {
143 [ + - ]: 7426 : BOOST_CHECK_MESSAGE(!tx_valid, strTest);
144 [ + - ]: 7426 : BOOST_CHECK_MESSAGE((err != SCRIPT_ERR_OK), ScriptErrorString(err));
145 : : }
146 : 8962 : return (tx_valid == expect_valid);
147 : : }
148 : :
149 : : /*
150 : : * Trim or fill flags to make the combination valid:
151 : : * WITNESS must be used with P2SH
152 : : * CLEANSTACK must be used WITNESS and P2SH
153 : : */
154 : :
155 : 9324 : unsigned int TrimFlags(unsigned int flags)
156 : : {
157 : : // WITNESS requires P2SH
158 [ + + ]: 9324 : if (!(flags & SCRIPT_VERIFY_P2SH)) flags &= ~(unsigned int)SCRIPT_VERIFY_WITNESS;
159 : :
160 : : // CLEANSTACK requires WITNESS (and transitively CLEANSTACK requires P2SH)
161 [ + + ]: 9324 : if (!(flags & SCRIPT_VERIFY_WITNESS)) flags &= ~(unsigned int)SCRIPT_VERIFY_CLEANSTACK;
162 : 9324 : Assert(IsValidFlagCombination(flags));
163 : 9324 : return flags;
164 : : }
165 : :
166 : 3732 : unsigned int FillFlags(unsigned int flags)
167 : : {
168 : : // CLEANSTACK implies WITNESS
169 [ + + ]: 3732 : if (flags & SCRIPT_VERIFY_CLEANSTACK) flags |= SCRIPT_VERIFY_WITNESS;
170 : :
171 : : // WITNESS implies P2SH (and transitively CLEANSTACK implies P2SH)
172 [ + + ]: 3732 : if (flags & SCRIPT_VERIFY_WITNESS) flags |= SCRIPT_VERIFY_P2SH;
173 : 3732 : Assert(IsValidFlagCombination(flags));
174 : 3732 : return flags;
175 : : }
176 : :
177 : : // Exclude each possible script verify flag from flags. Returns a set of these flag combinations
178 : : // that are valid and without duplicates. For example: if flags=1111 and the 4 possible flags are
179 : : // 0001, 0010, 0100, and 1000, this should return the set {0111, 1011, 1101, 1110}.
180 : : // Assumes that mapFlagNames contains all script verify flags.
181 : 204 : std::set<unsigned int> ExcludeIndividualFlags(unsigned int flags)
182 : : {
183 : 204 : std::set<unsigned int> flags_combos;
184 [ + + ]: 4488 : for (const auto& pair : mapFlagNames) {
185 [ + - ]: 4284 : const unsigned int flags_excluding_one = TrimFlags(flags & ~(pair.second));
186 [ + + ]: 4284 : if (flags != flags_excluding_one) {
187 [ + - ]: 611 : flags_combos.insert(flags_excluding_one);
188 : : }
189 : : }
190 : 204 : return flags_combos;
191 : 0 : }
192 : :
193 : : BOOST_FIXTURE_TEST_SUITE(transaction_tests, BasicTestingSetup)
194 : :
195 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(tx_valid)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
196 : : {
197 [ + - ]: 2 : BOOST_CHECK_MESSAGE(CheckMapFlagNames(), "mapFlagNames is missing a script verification flag");
198 : : // Read tests from test/data/tx_valid.json
199 : 1 : UniValue tests = read_json(json_tests::tx_valid);
200 : :
201 [ + + ]: 248 : for (unsigned int idx = 0; idx < tests.size(); idx++) {
202 [ + - ]: 247 : const UniValue& test = tests[idx];
203 [ + - ]: 247 : std::string strTest = test.write();
204 [ + - + + ]: 247 : if (test[0].isArray())
205 : : {
206 [ + - + - : 120 : if (test.size() != 3 || !test[1].isStr() || !test[2].isStr())
+ - + - -
+ ]
207 : : {
208 [ # # # # ]: 0 : BOOST_ERROR("Bad test: " << strTest);
209 : 0 : continue;
210 : : }
211 : :
212 [ + - ]: 120 : std::map<COutPoint, CScript> mapprevOutScriptPubKeys;
213 : 120 : std::map<COutPoint, int64_t> mapprevOutValues;
214 [ + - + - : 120 : UniValue inputs = test[0].get_array();
+ - ]
215 : 295 : bool fValid = true;
216 [ + + ]: 295 : for (unsigned int inpIdx = 0; inpIdx < inputs.size(); inpIdx++) {
217 [ + - ]: 175 : const UniValue& input = inputs[inpIdx];
218 [ + - ]: 175 : if (!input.isArray()) {
219 : : fValid = false;
220 : : break;
221 : : }
222 [ + - ]: 175 : const UniValue& vinput = input.get_array();
223 [ + - + - ]: 175 : if (vinput.size() < 3 || vinput.size() > 4)
224 : : {
225 : : fValid = false;
226 : : break;
227 : : }
228 [ + - + - : 350 : COutPoint outpoint{Txid::FromHex(vinput[0].get_str()).value(), uint32_t(vinput[1].getInt<int>())};
+ - + - +
- + - ]
229 [ + - + - : 175 : mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str());
+ - + - ]
230 [ + + ]: 175 : if (vinput.size() >= 4)
231 : : {
232 [ + - + - : 81 : mapprevOutValues[outpoint] = vinput[3].getInt<int64_t>();
+ - ]
233 : : }
234 : : }
235 [ - + ]: 120 : if (!fValid)
236 : : {
237 [ # # # # ]: 0 : BOOST_ERROR("Bad test: " << strTest);
238 : 0 : continue;
239 : : }
240 : :
241 [ + - + - : 120 : std::string transaction = test[1].get_str();
+ - ]
242 [ + - + - ]: 120 : DataStream stream(ParseHex(transaction));
243 [ + - ]: 120 : CTransaction tx(deserialize, TX_WITH_WITNESS, stream);
244 : :
245 [ + - ]: 120 : TxValidationState state;
246 [ + - + - : 240 : BOOST_CHECK_MESSAGE(CheckTransaction(tx, state), strTest);
+ - + - ]
247 [ + - + - : 240 : BOOST_CHECK(state.IsValid());
+ - ]
248 : :
249 [ + - ]: 120 : PrecomputedTransactionData txdata(tx);
250 [ + - + - : 120 : unsigned int verify_flags = ParseScriptFlags(test[2].get_str());
+ - + - ]
251 : :
252 : : // Check that the test gives a valid combination of flags (otherwise VerifyScript will throw). Don't edit the flags.
253 [ + - - + ]: 120 : if (~verify_flags != FillFlags(~verify_flags)) {
254 [ # # # # ]: 0 : BOOST_ERROR("Bad test flags: " << strTest);
255 : : }
256 : :
257 [ + - + - : 240 : BOOST_CHECK_MESSAGE(CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, ~verify_flags, txdata, strTest, /*expect_valid=*/true),
+ - ]
258 : : "Tx unexpectedly failed: " << strTest);
259 : :
260 : : // Backwards compatibility of script verification flags: Removing any flag(s) should not invalidate a valid transaction
261 [ + - + + ]: 2640 : for (const auto& [name, flag] : mapFlagNames) {
262 : : // Removing individual flags
263 [ + - ]: 2520 : unsigned int flags = TrimFlags(~(verify_flags | flag));
264 [ + - - + ]: 2520 : if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /*expect_valid=*/true)) {
265 [ # # # # ]: 0 : BOOST_ERROR("Tx unexpectedly failed with flag " << name << " unset: " << strTest);
266 : : }
267 : : // Removing random combinations of flags
268 [ + - ]: 2520 : flags = TrimFlags(~(verify_flags | (unsigned int)m_rng.randbits(mapFlagNames.size())));
269 [ + - - + ]: 2520 : if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /*expect_valid=*/true)) {
270 [ # # # # : 0 : BOOST_ERROR("Tx unexpectedly failed with random flags " << ToString(flags) << ": " << strTest);
# # ]
271 : : }
272 : : }
273 : :
274 : : // Check that flags are maximal: transaction should fail if any unset flags are set.
275 [ + - + + ]: 221 : for (auto flags_excluding_one : ExcludeIndividualFlags(verify_flags)) {
276 [ + - - + ]: 101 : if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, ~flags_excluding_one, txdata, strTest, /*expect_valid=*/false)) {
277 [ # # # # ]: 0 : BOOST_ERROR("Too many flags unset: " << strTest);
278 : : }
279 : 120 : }
280 : 360 : }
281 : 247 : }
282 : 1 : }
283 : :
284 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(tx_invalid)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
285 : : {
286 : : // Read tests from test/data/tx_invalid.json
287 : 1 : UniValue tests = read_json(json_tests::tx_invalid);
288 : :
289 [ + + ]: 202 : for (unsigned int idx = 0; idx < tests.size(); idx++) {
290 [ + - ]: 201 : const UniValue& test = tests[idx];
291 [ + - ]: 201 : std::string strTest = test.write();
292 [ + - + + ]: 201 : if (test[0].isArray())
293 : : {
294 [ + - + - : 93 : if (test.size() != 3 || !test[1].isStr() || !test[2].isStr())
+ - + - -
+ ]
295 : : {
296 [ # # # # ]: 0 : BOOST_ERROR("Bad test: " << strTest);
297 : 0 : continue;
298 : : }
299 : :
300 [ + - ]: 93 : std::map<COutPoint, CScript> mapprevOutScriptPubKeys;
301 : 93 : std::map<COutPoint, int64_t> mapprevOutValues;
302 [ + - + - : 93 : UniValue inputs = test[0].get_array();
+ - ]
303 : 203 : bool fValid = true;
304 [ + + ]: 203 : for (unsigned int inpIdx = 0; inpIdx < inputs.size(); inpIdx++) {
305 [ + - ]: 110 : const UniValue& input = inputs[inpIdx];
306 [ + - ]: 110 : if (!input.isArray()) {
307 : : fValid = false;
308 : : break;
309 : : }
310 [ + - ]: 110 : const UniValue& vinput = input.get_array();
311 [ + - + - ]: 110 : if (vinput.size() < 3 || vinput.size() > 4)
312 : : {
313 : : fValid = false;
314 : : break;
315 : : }
316 [ + - + - : 220 : COutPoint outpoint{Txid::FromHex(vinput[0].get_str()).value(), uint32_t(vinput[1].getInt<int>())};
+ - + - +
- + - ]
317 [ + - + - : 110 : mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str());
+ - + - ]
318 [ + + ]: 110 : if (vinput.size() >= 4)
319 : : {
320 [ + - + - : 29 : mapprevOutValues[outpoint] = vinput[3].getInt<int64_t>();
+ - ]
321 : : }
322 : : }
323 [ - + ]: 93 : if (!fValid)
324 : : {
325 [ # # # # ]: 0 : BOOST_ERROR("Bad test: " << strTest);
326 : 0 : continue;
327 : : }
328 : :
329 [ + - + - : 93 : std::string transaction = test[1].get_str();
+ - ]
330 [ + - + - ]: 93 : DataStream stream(ParseHex(transaction));
331 [ + - ]: 93 : CTransaction tx(deserialize, TX_WITH_WITNESS, stream);
332 : :
333 [ + - ]: 93 : TxValidationState state;
334 [ + - + + : 93 : if (!CheckTransaction(tx, state) || state.IsInvalid()) {
+ - ]
335 [ + - + - : 18 : BOOST_CHECK_MESSAGE(test[2].get_str() == "BADTX", strTest);
+ - + - ]
336 : 9 : continue;
337 : : }
338 : :
339 [ + - ]: 84 : PrecomputedTransactionData txdata(tx);
340 [ + - + - : 84 : unsigned int verify_flags = ParseScriptFlags(test[2].get_str());
+ - + - ]
341 : :
342 : : // Check that the test gives a valid combination of flags (otherwise VerifyScript will throw). Don't edit the flags.
343 [ + - - + ]: 84 : if (verify_flags != FillFlags(verify_flags)) {
344 [ # # # # ]: 0 : BOOST_ERROR("Bad test flags: " << strTest);
345 : : }
346 : :
347 : : // Not using FillFlags() in the main test, in order to detect invalid verifyFlags combination
348 [ + - + - : 168 : BOOST_CHECK_MESSAGE(CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, verify_flags, txdata, strTest, /*expect_valid=*/false),
+ - ]
349 : : "Tx unexpectedly passed: " << strTest);
350 : :
351 : : // Backwards compatibility of script verification flags: Adding any flag(s) should not validate an invalid transaction
352 [ + - + + ]: 1848 : for (const auto& [name, flag] : mapFlagNames) {
353 [ + - ]: 1764 : unsigned int flags = FillFlags(verify_flags | flag);
354 : : // Adding individual flags
355 [ + - - + ]: 1764 : if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /*expect_valid=*/false)) {
356 [ # # # # ]: 0 : BOOST_ERROR("Tx unexpectedly passed with flag " << name << " set: " << strTest);
357 : : }
358 : : // Adding random combinations of flags
359 [ + - ]: 1764 : flags = FillFlags(verify_flags | (unsigned int)m_rng.randbits(mapFlagNames.size()));
360 [ + - - + ]: 1764 : if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /*expect_valid=*/false)) {
361 [ # # # # ]: 0 : BOOST_ERROR("Tx unexpectedly passed with random flags " << name << ": " << strTest);
362 : : }
363 : : }
364 : :
365 : : // Check that flags are minimal: transaction should succeed if any set flags are unset.
366 [ + - + + ]: 173 : for (auto flags_excluding_one : ExcludeIndividualFlags(verify_flags)) {
367 [ + - - + ]: 89 : if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags_excluding_one, txdata, strTest, /*expect_valid=*/true)) {
368 [ # # # # ]: 0 : BOOST_ERROR("Too many flags set: " << strTest);
369 : : }
370 : 84 : }
371 : 270 : }
372 : 201 : }
373 : 1 : }
374 : :
375 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(tx_no_inputs)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
376 : : {
377 : 1 : CMutableTransaction empty;
378 : :
379 [ + - ]: 1 : TxValidationState state;
380 [ + - + - : 3 : BOOST_CHECK_MESSAGE(!CheckTransaction(CTransaction(empty), state), "Transaction with no inputs should be invalid.");
+ - + - +
- ]
381 [ + - + - : 2 : BOOST_CHECK(state.GetRejectReason() == "bad-txns-vin-empty");
+ - ]
382 : 2 : }
383 : :
384 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(tx_oversized)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
385 : : {
386 : 4 : auto createTransaction =[](size_t payloadSize) {
387 : 3 : CMutableTransaction tx;
388 [ + - ]: 3 : tx.vin.resize(1);
389 [ + - + - : 6 : tx.vout.emplace_back(1, CScript() << OP_RETURN << std::vector<unsigned char>(payloadSize));
+ - ]
390 [ + - ]: 3 : return CTransaction(tx);
391 : 3 : };
392 : 1 : const auto maxTransactionSize = MAX_BLOCK_WEIGHT / WITNESS_SCALE_FACTOR;
393 : 1 : const auto oversizedTransactionBaseSize = ::GetSerializeSize(TX_NO_WITNESS(createTransaction(maxTransactionSize))) - maxTransactionSize;
394 : :
395 : 1 : auto maxPayloadSize = maxTransactionSize - oversizedTransactionBaseSize;
396 : 1 : {
397 [ + - ]: 1 : TxValidationState state;
398 [ + - + - ]: 1 : CheckTransaction(createTransaction(maxPayloadSize), state);
399 [ + - + - : 2 : BOOST_CHECK(state.GetRejectReason() != "bad-txns-oversize");
+ - ]
400 : 0 : }
401 : :
402 : 1 : maxPayloadSize += 1;
403 : 1 : {
404 [ + - ]: 1 : TxValidationState state;
405 [ + - + - : 3 : BOOST_CHECK_MESSAGE(!CheckTransaction(createTransaction(maxPayloadSize), state), "Oversized transaction should be invalid");
+ - + - +
- ]
406 [ + - + - : 2 : BOOST_CHECK(state.GetRejectReason() == "bad-txns-oversize");
+ - ]
407 : 1 : }
408 : 1 : }
409 : :
410 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(basic_transaction_tests)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
411 : : {
412 : : // Random real transaction (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436)
413 : 1 : unsigned char ch[] = {0x01, 0x00, 0x00, 0x00, 0x01, 0x6b, 0xff, 0x7f, 0xcd, 0x4f, 0x85, 0x65, 0xef, 0x40, 0x6d, 0xd5, 0xd6, 0x3d, 0x4f, 0xf9, 0x4f, 0x31, 0x8f, 0xe8, 0x20, 0x27, 0xfd, 0x4d, 0xc4, 0x51, 0xb0, 0x44, 0x74, 0x01, 0x9f, 0x74, 0xb4, 0x00, 0x00, 0x00, 0x00, 0x8c, 0x49, 0x30, 0x46, 0x02, 0x21, 0x00, 0xda, 0x0d, 0xc6, 0xae, 0xce, 0xfe, 0x1e, 0x06, 0xef, 0xdf, 0x05, 0x77, 0x37, 0x57, 0xde, 0xb1, 0x68, 0x82, 0x09, 0x30, 0xe3, 0xb0, 0xd0, 0x3f, 0x46, 0xf5, 0xfc, 0xf1, 0x50, 0xbf, 0x99, 0x0c, 0x02, 0x21, 0x00, 0xd2, 0x5b, 0x5c, 0x87, 0x04, 0x00, 0x76, 0xe4, 0xf2, 0x53, 0xf8, 0x26, 0x2e, 0x76, 0x3e, 0x2d, 0xd5, 0x1e, 0x7f, 0xf0, 0xbe, 0x15, 0x77, 0x27, 0xc4, 0xbc, 0x42, 0x80, 0x7f, 0x17, 0xbd, 0x39, 0x01, 0x41, 0x04, 0xe6, 0xc2, 0x6e, 0xf6, 0x7d, 0xc6, 0x10, 0xd2, 0xcd, 0x19, 0x24, 0x84, 0x78, 0x9a, 0x6c, 0xf9, 0xae, 0xa9, 0x93, 0x0b, 0x94, 0x4b, 0x7e, 0x2d, 0xb5, 0x34, 0x2b, 0x9d, 0x9e, 0x5b, 0x9f, 0xf7, 0x9a, 0xff, 0x9a, 0x2e, 0xe1, 0x97, 0x8d, 0xd7, 0xfd, 0x01, 0xdf, 0xc5, 0x22, 0xee, 0x02, 0x28, 0x3d, 0x3b, 0x06, 0xa9, 0xd0, 0x3a, 0xcf, 0x80, 0x96, 0x96, 0x8d, 0x7d, 0xbb, 0x0f, 0x91, 0x78, 0xff, 0xff, 0xff, 0xff, 0x02, 0x8b, 0xa7, 0x94, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xba, 0xde, 0xec, 0xfd, 0xef, 0x05, 0x07, 0x24, 0x7f, 0xc8, 0xf7, 0x42, 0x41, 0xd7, 0x3b, 0xc0, 0x39, 0x97, 0x2d, 0x7b, 0x88, 0xac, 0x40, 0x94, 0xa8, 0x02, 0x00, 0x00, 0x00, 0x00, 0x19, 0x76, 0xa9, 0x14, 0xc1, 0x09, 0x32, 0x48, 0x3f, 0xec, 0x93, 0xed, 0x51, 0xf5, 0xfe, 0x95, 0xe7, 0x25, 0x59, 0xf2, 0xcc, 0x70, 0x43, 0xf9, 0x88, 0xac, 0x00, 0x00, 0x00, 0x00, 0x00};
414 : 1 : std::vector<unsigned char> vch(ch, ch + sizeof(ch) -1);
415 [ + - ]: 1 : DataStream stream(vch);
416 [ + - ]: 1 : CMutableTransaction tx;
417 [ + - ]: 1 : stream >> TX_WITH_WITNESS(tx);
418 [ + - ]: 1 : TxValidationState state;
419 [ + - + - : 3 : BOOST_CHECK_MESSAGE(CheckTransaction(CTransaction(tx), state) && state.IsValid(), "Simple deserialized transaction should be valid.");
+ - + - -
+ + - +
- ]
420 : :
421 : : // Check that duplicate txins fail
422 [ + - ]: 1 : tx.vin.push_back(tx.vin[0]);
423 [ + - + - : 3 : BOOST_CHECK_MESSAGE(!CheckTransaction(CTransaction(tx), state) || !state.IsValid(), "Transaction with duplicate txins should be invalid.");
+ - - + -
- + - ]
424 : 2 : }
425 : :
426 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(test_Get)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
427 : : {
428 : 1 : FillableSigningProvider keystore;
429 : 1 : CCoinsView coinsDummy;
430 [ + - ]: 1 : CCoinsViewCache coins(&coinsDummy);
431 : 1 : std::vector<CMutableTransaction> dummyTransactions =
432 [ + - ]: 1 : SetupDummyInputs(keystore, coins, {11*CENT, 50*CENT, 21*CENT, 22*CENT});
433 : :
434 [ + - ]: 1 : CMutableTransaction t1;
435 [ + - ]: 1 : t1.vin.resize(3);
436 [ + - ]: 1 : t1.vin[0].prevout.hash = dummyTransactions[0].GetHash();
437 : 1 : t1.vin[0].prevout.n = 1;
438 [ + - + - ]: 1 : t1.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
439 [ + - ]: 1 : t1.vin[1].prevout.hash = dummyTransactions[1].GetHash();
440 : 1 : t1.vin[1].prevout.n = 0;
441 [ + - + - : 1 : t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
+ - ]
442 [ + - ]: 1 : t1.vin[2].prevout.hash = dummyTransactions[1].GetHash();
443 : 1 : t1.vin[2].prevout.n = 1;
444 [ + - + - : 1 : t1.vin[2].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
+ - ]
445 [ + - ]: 1 : t1.vout.resize(2);
446 [ + - ]: 1 : t1.vout[0].nValue = 90*CENT;
447 [ + - ]: 1 : t1.vout[0].scriptPubKey << OP_1;
448 : :
449 [ + - + - : 3 : BOOST_CHECK(AreInputsStandard(CTransaction(t1), coins));
+ - + - ]
450 : 1 : }
451 : :
452 : 24 : static void CreateCreditAndSpend(const FillableSigningProvider& keystore, const CScript& outscript, CTransactionRef& output, CMutableTransaction& input, bool success = true)
453 : : {
454 : 24 : CMutableTransaction outputm;
455 : 24 : outputm.version = 1;
456 [ + - ]: 24 : outputm.vin.resize(1);
457 : 24 : outputm.vin[0].prevout.SetNull();
458 : 24 : outputm.vin[0].scriptSig = CScript();
459 [ + - ]: 24 : outputm.vout.resize(1);
460 : 24 : outputm.vout[0].nValue = 1;
461 : 24 : outputm.vout[0].scriptPubKey = outscript;
462 : 24 : DataStream ssout;
463 [ + - ]: 24 : ssout << TX_WITH_WITNESS(outputm);
464 [ + - ]: 24 : ssout >> TX_WITH_WITNESS(output);
465 [ - + ]: 24 : assert(output->vin.size() == 1);
466 [ - + ]: 24 : assert(output->vin[0] == outputm.vin[0]);
467 [ - + ]: 24 : assert(output->vout.size() == 1);
468 [ - + ]: 24 : assert(output->vout[0] == outputm.vout[0]);
469 : :
470 [ + - ]: 24 : CMutableTransaction inputm;
471 : 24 : inputm.version = 1;
472 [ + - ]: 24 : inputm.vin.resize(1);
473 [ + - ]: 24 : inputm.vin[0].prevout.hash = output->GetHash();
474 : 24 : inputm.vin[0].prevout.n = 0;
475 [ + - ]: 24 : inputm.vout.resize(1);
476 : 24 : inputm.vout[0].nValue = 1;
477 : 24 : inputm.vout[0].scriptPubKey = CScript();
478 : 24 : SignatureData empty;
479 [ + - ]: 24 : bool ret = SignSignature(keystore, *output, inputm, 0, SIGHASH_ALL, empty);
480 [ - + ]: 24 : assert(ret == success);
481 : 24 : DataStream ssin;
482 [ + - ]: 24 : ssin << TX_WITH_WITNESS(inputm);
483 [ + - ]: 24 : ssin >> TX_WITH_WITNESS(input);
484 [ - + ]: 24 : assert(input.vin.size() == 1);
485 [ - + ]: 24 : assert(input.vin[0] == inputm.vin[0]);
486 [ - + ]: 24 : assert(input.vout.size() == 1);
487 [ - + ]: 24 : assert(input.vout[0] == inputm.vout[0]);
488 [ - + ]: 24 : assert(input.vin[0].scriptWitness.stack == inputm.vin[0].scriptWitness.stack);
489 : 72 : }
490 : :
491 : 69 : static void CheckWithFlag(const CTransactionRef& output, const CMutableTransaction& input, uint32_t flags, bool success)
492 : : {
493 : 69 : ScriptError error;
494 : 69 : CTransaction inputi(input);
495 [ + - - + ]: 69 : bool ret = VerifyScript(inputi.vin[0].scriptSig, output->vout[0].scriptPubKey, &inputi.vin[0].scriptWitness, flags, TransactionSignatureChecker(&inputi, 0, output->vout[0].nValue, MissingDataBehavior::ASSERT_FAIL), &error);
496 [ - + ]: 69 : assert(ret == success);
497 : 69 : }
498 : :
499 : 3 : static CScript PushAll(const std::vector<valtype>& values)
500 : : {
501 : 3 : CScript result;
502 [ + + ]: 8 : for (const valtype& v : values) {
503 [ - + ]: 5 : if (v.size() == 0) {
504 [ # # ]: 0 : result << OP_0;
505 [ - + - - : 5 : } else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) {
- - ]
506 [ # # ]: 0 : result << CScript::EncodeOP_N(v[0]);
507 [ - + - - ]: 5 : } else if (v.size() == 1 && v[0] == 0x81) {
508 [ # # ]: 0 : result << OP_1NEGATE;
509 : : } else {
510 : 5 : result << v;
511 : : }
512 : : }
513 : 3 : return result;
514 : 0 : }
515 : :
516 : 3 : static void ReplaceRedeemScript(CScript& script, const CScript& redeemScript)
517 : : {
518 : 3 : std::vector<valtype> stack;
519 [ + - ]: 3 : EvalScript(stack, script, SCRIPT_VERIFY_STRICTENC, BaseSignatureChecker(), SigVersion::BASE);
520 [ - + ]: 3 : assert(stack.size() > 0);
521 [ + + + - : 6 : stack.back() = std::vector<unsigned char>(redeemScript.begin(), redeemScript.end());
+ - ]
522 [ + - ]: 6 : script = PushAll(stack);
523 : 3 : }
524 : :
525 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(test_big_witness_transaction)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
526 : : {
527 : 1 : CMutableTransaction mtx;
528 : 1 : mtx.version = 1;
529 : :
530 : 1 : CKey key = GenerateRandomKey(); // Need to use compressed keys in segwit or the signing will fail
531 : 1 : FillableSigningProvider keystore;
532 [ + - + - : 2 : BOOST_CHECK(keystore.AddKeyPubKey(key, key.GetPubKey()));
+ - + - +
- ]
533 [ + - + - ]: 1 : CKeyID hash = key.GetPubKey().GetID();
534 [ + - + - ]: 1 : CScript scriptPubKey = CScript() << OP_0 << std::vector<unsigned char>(hash.begin(), hash.end());
535 : :
536 : 1 : std::vector<int> sigHashes;
537 [ + - ]: 1 : sigHashes.push_back(SIGHASH_NONE | SIGHASH_ANYONECANPAY);
538 [ + - ]: 1 : sigHashes.push_back(SIGHASH_SINGLE | SIGHASH_ANYONECANPAY);
539 [ + - ]: 1 : sigHashes.push_back(SIGHASH_ALL | SIGHASH_ANYONECANPAY);
540 [ + - ]: 1 : sigHashes.push_back(SIGHASH_NONE);
541 [ + - ]: 1 : sigHashes.push_back(SIGHASH_SINGLE);
542 [ + - ]: 1 : sigHashes.push_back(SIGHASH_ALL);
543 : :
544 : : // create a big transaction of 4500 inputs signed by the same key
545 [ + + ]: 4501 : for(uint32_t ij = 0; ij < 4500; ij++) {
546 [ + - ]: 4500 : uint32_t i = mtx.vin.size();
547 [ + - + - ]: 9000 : COutPoint outpoint(Txid::FromHex("0000000000000000000000000000000000000000000000000000000000000100").value(), i);
548 : :
549 [ + - ]: 4500 : mtx.vin.resize(mtx.vin.size() + 1);
550 : 4500 : mtx.vin[i].prevout = outpoint;
551 : 4500 : mtx.vin[i].scriptSig = CScript();
552 : :
553 [ + - ]: 4500 : mtx.vout.resize(mtx.vout.size() + 1);
554 [ + - ]: 4500 : mtx.vout[i].nValue = 1000;
555 [ + - ]: 4500 : mtx.vout[i].scriptPubKey = CScript() << OP_1;
556 : : }
557 : :
558 : : // sign all inputs
559 [ + + ]: 4501 : for(uint32_t i = 0; i < mtx.vin.size(); i++) {
560 : 4500 : SignatureData empty;
561 [ + - + - ]: 4500 : bool hashSigned = SignSignature(keystore, scriptPubKey, mtx, i, 1000, sigHashes.at(i % sigHashes.size()), empty);
562 [ - + ]: 4500 : assert(hashSigned);
563 : 4500 : }
564 : :
565 : 1 : DataStream ssout;
566 [ + - ]: 1 : ssout << TX_WITH_WITNESS(mtx);
567 [ + - ]: 1 : CTransaction tx(deserialize, TX_WITH_WITNESS, ssout);
568 : :
569 : : // check all inputs concurrently, with the cache
570 [ + - ]: 1 : PrecomputedTransactionData txdata(tx);
571 [ + - ]: 1 : CCheckQueue<CScriptCheck> scriptcheckqueue(/*batch_size=*/128, /*worker_threads_num=*/20);
572 [ + - ]: 1 : CCheckQueueControl<CScriptCheck> control(scriptcheckqueue);
573 : :
574 : 1 : std::vector<Coin> coins;
575 [ + + ]: 4501 : for(uint32_t i = 0; i < mtx.vin.size(); i++) {
576 : 4500 : Coin coin;
577 : 4500 : coin.nHeight = 1;
578 : 4500 : coin.fCoinBase = false;
579 : 4500 : coin.out.nValue = 1000;
580 : 4500 : coin.out.scriptPubKey = scriptPubKey;
581 [ + - ]: 4500 : coins.emplace_back(std::move(coin));
582 : 4500 : }
583 : :
584 [ + - ]: 1 : SignatureCache signature_cache{DEFAULT_SIGNATURE_CACHE_BYTES};
585 : :
586 [ + + ]: 4501 : for(uint32_t i = 0; i < mtx.vin.size(); i++) {
587 : 4500 : std::vector<CScriptCheck> vChecks;
588 [ + - ]: 4500 : vChecks.emplace_back(coins[tx.vin[i].prevout.n].out, tx, signature_cache, i, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false, &txdata);
589 [ + - ]: 4500 : control.Add(std::move(vChecks));
590 : 4500 : }
591 : :
592 [ + - ]: 1 : bool controlCheck = !control.Complete().has_value();
593 [ - + ]: 1 : assert(controlCheck);
594 : 3 : }
595 : :
596 : 4 : SignatureData CombineSignatures(const CMutableTransaction& input1, const CMutableTransaction& input2, const CTransactionRef tx)
597 : : {
598 : 4 : SignatureData sigdata;
599 [ + - ]: 4 : sigdata = DataFromTransaction(input1, 0, tx->vout[0]);
600 [ + - + - ]: 4 : sigdata.MergeSignatureData(DataFromTransaction(input2, 0, tx->vout[0]));
601 [ + - + - ]: 4 : ProduceSignature(DUMMY_SIGNING_PROVIDER, MutableTransactionSignatureCreator(input1, 0, tx->vout[0].nValue, SIGHASH_ALL), tx->vout[0].scriptPubKey, sigdata);
602 : 4 : return sigdata;
603 : 0 : }
604 : :
605 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(test_witness)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
606 : : {
607 : 1 : FillableSigningProvider keystore, keystore2;
608 : 1 : CKey key1 = GenerateRandomKey();
609 : 1 : CKey key2 = GenerateRandomKey();
610 : 1 : CKey key3 = GenerateRandomKey();
611 : 1 : CKey key1L = GenerateRandomKey(/*compressed=*/false);
612 : 1 : CKey key2L = GenerateRandomKey(/*compressed=*/false);
613 [ + - ]: 1 : CPubKey pubkey1 = key1.GetPubKey();
614 [ + - ]: 1 : CPubKey pubkey2 = key2.GetPubKey();
615 [ + - ]: 1 : CPubKey pubkey3 = key3.GetPubKey();
616 [ + - ]: 1 : CPubKey pubkey1L = key1L.GetPubKey();
617 [ + - ]: 1 : CPubKey pubkey2L = key2L.GetPubKey();
618 [ + - + - : 2 : BOOST_CHECK(keystore.AddKeyPubKey(key1, pubkey1));
+ - + - ]
619 [ + - + - : 2 : BOOST_CHECK(keystore.AddKeyPubKey(key2, pubkey2));
+ - + - ]
620 [ + - + - : 2 : BOOST_CHECK(keystore.AddKeyPubKey(key1L, pubkey1L));
+ - + - ]
621 [ + - + - : 2 : BOOST_CHECK(keystore.AddKeyPubKey(key2L, pubkey2L));
+ - + - ]
622 : 1 : CScript scriptPubkey1, scriptPubkey2, scriptPubkey1L, scriptPubkey2L, scriptMulti;
623 [ + - + - ]: 1 : scriptPubkey1 << ToByteVector(pubkey1) << OP_CHECKSIG;
624 [ + - + - ]: 1 : scriptPubkey2 << ToByteVector(pubkey2) << OP_CHECKSIG;
625 [ + - + - ]: 1 : scriptPubkey1L << ToByteVector(pubkey1L) << OP_CHECKSIG;
626 [ + - + - ]: 1 : scriptPubkey2L << ToByteVector(pubkey2L) << OP_CHECKSIG;
627 : 1 : std::vector<CPubKey> oneandthree;
628 [ + - ]: 1 : oneandthree.push_back(pubkey1);
629 [ + - ]: 1 : oneandthree.push_back(pubkey3);
630 [ + - ]: 2 : scriptMulti = GetScriptForMultisig(2, oneandthree);
631 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(scriptPubkey1));
+ - + - ]
632 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(scriptPubkey2));
+ - + - ]
633 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(scriptPubkey1L));
+ - + - ]
634 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(scriptPubkey2L));
+ - + - ]
635 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(scriptMulti));
+ - + - ]
636 : 1 : CScript destination_script_1, destination_script_2, destination_script_1L, destination_script_2L, destination_script_multi;
637 [ + - + - ]: 2 : destination_script_1 = GetScriptForDestination(WitnessV0KeyHash(pubkey1));
638 [ + - + - ]: 2 : destination_script_2 = GetScriptForDestination(WitnessV0KeyHash(pubkey2));
639 [ + - + - ]: 2 : destination_script_1L = GetScriptForDestination(WitnessV0KeyHash(pubkey1L));
640 [ + - + - ]: 2 : destination_script_2L = GetScriptForDestination(WitnessV0KeyHash(pubkey2L));
641 [ + - + - ]: 2 : destination_script_multi = GetScriptForDestination(WitnessV0ScriptHash(scriptMulti));
642 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(destination_script_1));
+ - + - ]
643 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(destination_script_2));
+ - + - ]
644 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(destination_script_1L));
+ - + - ]
645 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(destination_script_2L));
+ - + - ]
646 [ + - + - : 2 : BOOST_CHECK(keystore.AddCScript(destination_script_multi));
+ - + - ]
647 [ + - + - : 2 : BOOST_CHECK(keystore2.AddCScript(scriptMulti));
+ - + - ]
648 [ + - + - : 2 : BOOST_CHECK(keystore2.AddCScript(destination_script_multi));
+ - + - ]
649 [ + - + - : 2 : BOOST_CHECK(keystore2.AddKeyPubKey(key3, pubkey3));
+ - + - ]
650 : :
651 : 1 : CTransactionRef output1, output2;
652 [ + - + - ]: 1 : CMutableTransaction input1, input2;
653 : :
654 : : // Normal pay-to-compressed-pubkey.
655 [ + - ]: 1 : CreateCreditAndSpend(keystore, scriptPubkey1, output1, input1);
656 [ + - ]: 1 : CreateCreditAndSpend(keystore, scriptPubkey2, output2, input2);
657 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_NONE, true);
658 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
659 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
660 [ + - ]: 1 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
661 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_NONE, false);
662 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
663 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
664 [ + - ]: 1 : CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
665 : :
666 : : // P2SH pay-to-compressed-pubkey.
667 [ + - + - : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(scriptPubkey1)), output1, input1);
+ - ]
668 [ + - + - : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(scriptPubkey2)), output2, input2);
+ - ]
669 [ + - ]: 1 : ReplaceRedeemScript(input2.vin[0].scriptSig, scriptPubkey1);
670 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_NONE, true);
671 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
672 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
673 [ + - ]: 1 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
674 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_NONE, true);
675 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
676 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
677 [ + - ]: 1 : CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
678 : :
679 : : // Witness pay-to-compressed-pubkey (v0).
680 [ + - ]: 1 : CreateCreditAndSpend(keystore, destination_script_1, output1, input1);
681 [ + - ]: 1 : CreateCreditAndSpend(keystore, destination_script_2, output2, input2);
682 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_NONE, true);
683 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
684 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
685 [ + - ]: 1 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
686 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_NONE, true);
687 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, true);
688 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
689 [ + - ]: 1 : CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
690 : :
691 : : // P2SH witness pay-to-compressed-pubkey (v0).
692 [ + - + - : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(destination_script_1)), output1, input1);
+ - ]
693 [ + - + - : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(destination_script_2)), output2, input2);
+ - ]
694 [ + - ]: 1 : ReplaceRedeemScript(input2.vin[0].scriptSig, destination_script_1);
695 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_NONE, true);
696 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
697 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
698 [ + - ]: 1 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
699 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_NONE, true);
700 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, true);
701 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
702 [ + - ]: 1 : CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
703 : :
704 : : // Normal pay-to-uncompressed-pubkey.
705 [ + - ]: 1 : CreateCreditAndSpend(keystore, scriptPubkey1L, output1, input1);
706 [ + - ]: 1 : CreateCreditAndSpend(keystore, scriptPubkey2L, output2, input2);
707 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_NONE, true);
708 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
709 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
710 [ + - ]: 1 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
711 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_NONE, false);
712 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
713 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
714 [ + - ]: 1 : CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
715 : :
716 : : // P2SH pay-to-uncompressed-pubkey.
717 [ + - + - : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(scriptPubkey1L)), output1, input1);
+ - ]
718 [ + - + - : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(scriptPubkey2L)), output2, input2);
+ - ]
719 [ + - ]: 1 : ReplaceRedeemScript(input2.vin[0].scriptSig, scriptPubkey1L);
720 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_NONE, true);
721 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
722 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
723 [ + - ]: 1 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
724 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_NONE, true);
725 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
726 [ + - ]: 1 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
727 [ + - ]: 1 : CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
728 : :
729 : : // Signing disabled for witness pay-to-uncompressed-pubkey (v1).
730 [ + - ]: 1 : CreateCreditAndSpend(keystore, destination_script_1L, output1, input1, false);
731 [ + - ]: 1 : CreateCreditAndSpend(keystore, destination_script_2L, output2, input2, false);
732 : :
733 : : // Signing disabled for P2SH witness pay-to-uncompressed-pubkey (v1).
734 [ + - + - : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(destination_script_1L)), output1, input1, false);
+ - ]
735 [ + - + - : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(destination_script_2L)), output2, input2, false);
+ - ]
736 : :
737 : : // Normal 2-of-2 multisig
738 [ + - ]: 1 : CreateCreditAndSpend(keystore, scriptMulti, output1, input1, false);
739 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_NONE, false);
740 [ + - ]: 1 : CreateCreditAndSpend(keystore2, scriptMulti, output2, input2, false);
741 [ + - ]: 1 : CheckWithFlag(output2, input2, SCRIPT_VERIFY_NONE, false);
742 [ + - + - : 2 : BOOST_CHECK(*output1 == *output2);
+ - ]
743 [ + - + - : 2 : UpdateInput(input1.vin[0], CombineSignatures(input1, input2, output1));
+ - + - ]
744 [ + - ]: 1 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
745 : :
746 : : // P2SH 2-of-2 multisig
747 [ + - + - : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(scriptMulti)), output1, input1, false);
+ - ]
748 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_NONE, true);
749 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, false);
750 [ + - + - : 2 : CreateCreditAndSpend(keystore2, GetScriptForDestination(ScriptHash(scriptMulti)), output2, input2, false);
+ - ]
751 [ + - ]: 1 : CheckWithFlag(output2, input2, SCRIPT_VERIFY_NONE, true);
752 [ + - ]: 1 : CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH, false);
753 [ + - + - : 2 : BOOST_CHECK(*output1 == *output2);
+ - ]
754 [ + - + - : 2 : UpdateInput(input1.vin[0], CombineSignatures(input1, input2, output1));
+ - + - ]
755 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
756 [ + - ]: 1 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
757 : :
758 : : // Witness 2-of-2 multisig
759 [ + - ]: 1 : CreateCreditAndSpend(keystore, destination_script_multi, output1, input1, false);
760 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_NONE, true);
761 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
762 [ + - ]: 1 : CreateCreditAndSpend(keystore2, destination_script_multi, output2, input2, false);
763 [ + - ]: 1 : CheckWithFlag(output2, input2, SCRIPT_VERIFY_NONE, true);
764 [ + - ]: 1 : CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
765 [ + - + - : 2 : BOOST_CHECK(*output1 == *output2);
+ - ]
766 [ + - + - : 2 : UpdateInput(input1.vin[0], CombineSignatures(input1, input2, output1));
+ - + - ]
767 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true);
768 [ + - ]: 1 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
769 : :
770 : : // P2SH witness 2-of-2 multisig
771 [ + - + - : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(destination_script_multi)), output1, input1, false);
+ - ]
772 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
773 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
774 [ + - + - : 2 : CreateCreditAndSpend(keystore2, GetScriptForDestination(ScriptHash(destination_script_multi)), output2, input2, false);
+ - ]
775 [ + - ]: 1 : CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH, true);
776 [ + - ]: 1 : CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
777 [ + - + - : 2 : BOOST_CHECK(*output1 == *output2);
+ - ]
778 [ + - + - : 2 : UpdateInput(input1.vin[0], CombineSignatures(input1, input2, output1));
+ - + - ]
779 [ + - ]: 1 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true);
780 [ + - ]: 1 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
781 [ + - + - ]: 4 : }
782 : :
783 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(test_IsStandard)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
784 : : {
785 : 1 : FillableSigningProvider keystore;
786 : 1 : CCoinsView coinsDummy;
787 [ + - ]: 1 : CCoinsViewCache coins(&coinsDummy);
788 : 1 : std::vector<CMutableTransaction> dummyTransactions =
789 [ + - ]: 1 : SetupDummyInputs(keystore, coins, {11*CENT, 50*CENT, 21*CENT, 22*CENT});
790 : :
791 [ + - ]: 1 : CMutableTransaction t;
792 [ + - ]: 1 : t.vin.resize(1);
793 [ + - ]: 1 : t.vin[0].prevout.hash = dummyTransactions[0].GetHash();
794 : 1 : t.vin[0].prevout.n = 1;
795 [ + - + - ]: 1 : t.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
796 [ + - ]: 1 : t.vout.resize(1);
797 : 1 : t.vout[0].nValue = 90*CENT;
798 : 1 : CKey key = GenerateRandomKey();
799 [ + - + - : 1 : t.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
+ - ]
800 : :
801 [ + - ]: 51 : constexpr auto CheckIsStandard = [](const auto& t, const unsigned int max_op_return_relay = MAX_OP_RETURN_RELAY) {
802 : 50 : std::string reason;
803 [ + - + - : 150 : BOOST_CHECK(IsStandardTx(CTransaction{t}, max_op_return_relay, g_bare_multi, g_dust, reason));
+ - + - +
- ]
804 [ + - + - ]: 100 : BOOST_CHECK(reason.empty());
805 : 50 : };
806 [ + - ]: 102 : constexpr auto CheckIsNotStandard = [](const auto& t, const std::string& reason_in, const unsigned int max_op_return_relay = MAX_OP_RETURN_RELAY) {
807 : 101 : std::string reason;
808 [ + - + - : 303 : BOOST_CHECK(!IsStandardTx(CTransaction{t}, max_op_return_relay, g_bare_multi, g_dust, reason));
+ - + - +
- ]
809 [ + - + - ]: 101 : BOOST_CHECK_EQUAL(reason_in, reason);
810 : 101 : };
811 : :
812 [ + - ]: 1 : CheckIsStandard(t);
813 : :
814 : : // Check dust with default relay fee:
815 [ + - ]: 1 : CAmount nDustThreshold = 182 * g_dust.GetFeePerK() / 1000;
816 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(nDustThreshold, 546);
817 : :
818 : : // Add dust outputs up to allowed maximum, still standard!
819 [ + + ]: 2 : for (size_t i{0}; i < MAX_DUST_OUTPUTS_PER_TX; ++i) {
820 [ + - ]: 1 : t.vout.emplace_back(0, t.vout[0].scriptPubKey);
821 [ + - ]: 1 : CheckIsStandard(t);
822 : : }
823 : :
824 : : // dust:
825 [ + - ]: 1 : t.vout[0].nValue = nDustThreshold - 1;
826 [ + - + - ]: 1 : CheckIsNotStandard(t, "dust");
827 : : // not dust:
828 [ + - ]: 1 : t.vout[0].nValue = nDustThreshold;
829 [ + - ]: 1 : CheckIsStandard(t);
830 : :
831 : : // Disallowed version
832 : 1 : t.version = std::numeric_limits<uint32_t>::max();
833 [ + - + - ]: 1 : CheckIsNotStandard(t, "version");
834 : :
835 : 1 : t.version = 0;
836 [ + - + - ]: 1 : CheckIsNotStandard(t, "version");
837 : :
838 : 1 : t.version = TX_MAX_STANDARD_VERSION + 1;
839 [ + - + - ]: 1 : CheckIsNotStandard(t, "version");
840 : :
841 : : // Allowed version
842 : 1 : t.version = 1;
843 [ + - ]: 1 : CheckIsStandard(t);
844 : :
845 : 1 : t.version = 2;
846 [ + - ]: 1 : CheckIsStandard(t);
847 : :
848 : : // Check dust with odd relay fee to verify rounding:
849 : : // nDustThreshold = 182 * 3702 / 1000
850 [ + - ]: 1 : g_dust = CFeeRate(3702);
851 : : // dust:
852 [ + - ]: 1 : t.vout[0].nValue = 674 - 1;
853 [ + - + - ]: 1 : CheckIsNotStandard(t, "dust");
854 : : // not dust:
855 [ + - ]: 1 : t.vout[0].nValue = 674;
856 [ + - ]: 1 : CheckIsStandard(t);
857 [ + - ]: 1 : g_dust = CFeeRate{DUST_RELAY_TX_FEE};
858 : :
859 [ + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_1;
860 [ + - + - ]: 1 : CheckIsNotStandard(t, "scriptpubkey");
861 : :
862 : : // Custom 83-byte TxoutType::NULL_DATA (standard with max_op_return_relay of 83)
863 [ + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38"_hex;
864 [ + - + - : 2 : BOOST_CHECK_EQUAL(83, t.vout[0].scriptPubKey.size());
+ - ]
865 [ + - ]: 1 : CheckIsStandard(t, /*max_op_return_relay=*/83);
866 : :
867 : : // Non-standard if max_op_return_relay datacarrier arg is one less
868 [ + - + - ]: 1 : CheckIsNotStandard(t, "datacarrier", /*max_op_return_relay=*/82);
869 : :
870 : : // Data payload can be encoded in any way...
871 [ + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << ""_hex;
872 [ + - ]: 1 : CheckIsStandard(t);
873 [ + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << "00"_hex << "01"_hex;
874 [ + - ]: 1 : CheckIsStandard(t);
875 : : // OP_RESERVED *is* considered to be a PUSHDATA type opcode by IsPushOnly()!
876 [ + - + - : 1 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << OP_RESERVED << -1 << 0 << "01"_hex << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12 << 13 << 14 << 15 << 16;
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
877 [ + - ]: 1 : CheckIsStandard(t);
878 [ + - + - : 1 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << 0 << "01"_hex << 2 << "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"_hex;
+ - ]
879 [ + - ]: 1 : CheckIsStandard(t);
880 : :
881 : : // ...so long as it only contains PUSHDATA's
882 [ + - + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << OP_RETURN;
883 [ + - + - ]: 1 : CheckIsNotStandard(t, "scriptpubkey");
884 : :
885 : : // TxoutType::NULL_DATA w/o PUSHDATA
886 [ + - ]: 1 : t.vout.resize(1);
887 [ + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_RETURN;
888 [ + - ]: 1 : CheckIsStandard(t);
889 : :
890 : : // Multiple TxoutType::NULL_DATA are permitted
891 [ + - ]: 1 : t.vout.resize(2);
892 [ + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38"_hex;
893 [ + - ]: 1 : t.vout[0].nValue = 0;
894 [ + - ]: 1 : t.vout[1].scriptPubKey = CScript() << OP_RETURN << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38"_hex;
895 [ + - ]: 1 : t.vout[1].nValue = 0;
896 [ + - ]: 1 : CheckIsStandard(t);
897 : :
898 [ + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38"_hex;
899 [ + - ]: 1 : t.vout[1].scriptPubKey = CScript() << OP_RETURN;
900 [ + - ]: 1 : CheckIsStandard(t);
901 : :
902 [ + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_RETURN;
903 [ + - ]: 1 : t.vout[1].scriptPubKey = CScript() << OP_RETURN;
904 [ + - ]: 1 : CheckIsStandard(t);
905 : :
906 [ + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38"_hex;
907 [ + - ]: 1 : t.vout[1].scriptPubKey = CScript() << OP_RETURN << "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38"_hex;
908 [ + - + - ]: 2 : const auto datacarrier_size = t.vout[0].scriptPubKey.size() + t.vout[1].scriptPubKey.size();
909 [ + - ]: 1 : CheckIsStandard(t); // Default max relay should never trigger
910 [ + - ]: 1 : CheckIsStandard(t, /*max_op_return_relay=*/datacarrier_size);
911 [ + - + - ]: 1 : CheckIsNotStandard(t, "datacarrier", /*max_op_return_relay=*/datacarrier_size-1);
912 : :
913 : : // Check large scriptSig (non-standard if size is >1650 bytes)
914 [ + - ]: 1 : t.vout.resize(1);
915 [ + - ]: 1 : t.vout[0].nValue = MAX_MONEY;
916 [ + - + - : 1 : t.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
+ - ]
917 : : // OP_PUSHDATA2 with len (3 bytes) + data (1647 bytes) = 1650 bytes
918 [ + - ]: 1 : t.vin[0].scriptSig = CScript() << std::vector<unsigned char>(1647, 0); // 1650
919 [ + - ]: 1 : CheckIsStandard(t);
920 : :
921 [ + - ]: 1 : t.vin[0].scriptSig = CScript() << std::vector<unsigned char>(1648, 0); // 1651
922 [ + - + - ]: 1 : CheckIsNotStandard(t, "scriptsig-size");
923 : :
924 : : // Check scriptSig format (non-standard if there are any other ops than just PUSHs)
925 [ + - ]: 2 : t.vin[0].scriptSig = CScript()
926 [ + - + - : 1 : << OP_TRUE << OP_0 << OP_1NEGATE << OP_16 // OP_n (single byte pushes: n = 1, 0, -1, 16)
+ - + - +
- ]
927 [ + - + - ]: 2 : << std::vector<unsigned char>(75, 0) // OP_PUSHx [...x bytes...]
928 [ + - + - ]: 2 : << std::vector<unsigned char>(235, 0) // OP_PUSHDATA1 x [...x bytes...]
929 [ + - ]: 2 : << std::vector<unsigned char>(1234, 0) // OP_PUSHDATA2 x [...x bytes...]
930 [ + - ]: 1 : << OP_9;
931 [ + - ]: 1 : CheckIsStandard(t);
932 : :
933 : 1 : const std::vector<unsigned char> non_push_ops = { // arbitrary set of non-push operations
934 : : OP_NOP, OP_VERIFY, OP_IF, OP_ROT, OP_3DUP, OP_SIZE, OP_EQUAL, OP_ADD, OP_SUB,
935 [ + - ]: 1 : OP_HASH256, OP_CODESEPARATOR, OP_CHECKSIG, OP_CHECKLOCKTIMEVERIFY };
936 : :
937 [ - + ]: 2 : CScript::const_iterator pc = t.vin[0].scriptSig.begin();
938 [ + + ]: 9 : while (pc < t.vin[0].scriptSig.end()) {
939 : 8 : opcodetype opcode;
940 : 8 : CScript::const_iterator prev_pc = pc;
941 [ + - ]: 8 : t.vin[0].scriptSig.GetOp(pc, opcode); // advance to next op
942 : : // for the sake of simplicity, we only replace single-byte push operations
943 [ + + ]: 8 : if (opcode >= 1 && opcode <= OP_PUSHDATA4)
944 : 3 : continue;
945 : :
946 [ - + ]: 10 : int index = prev_pc - t.vin[0].scriptSig.begin();
947 : 5 : unsigned char orig_op = *prev_pc; // save op
948 : : // replace current push-op with each non-push-op
949 [ + + ]: 70 : for (auto op : non_push_ops) {
950 [ - + ]: 65 : t.vin[0].scriptSig[index] = op;
951 [ + - + - ]: 130 : CheckIsNotStandard(t, "scriptsig-not-pushonly");
952 : : }
953 [ - + ]: 5 : t.vin[0].scriptSig[index] = orig_op; // restore op
954 [ + - ]: 5 : CheckIsStandard(t);
955 : : }
956 : :
957 : : // Check tx-size (non-standard if transaction weight is > MAX_STANDARD_TX_WEIGHT)
958 : 1 : t.vin.clear();
959 [ + - ]: 1 : t.vin.resize(2438); // size per input (empty scriptSig): 41 bytes
960 [ + - + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << std::vector<unsigned char>(19, 0); // output size: 30 bytes
961 : : // tx header: 12 bytes => 48 weight units
962 : : // 2438 inputs: 2438*41 = 99958 bytes => 399832 weight units
963 : : // 1 output: 30 bytes => 120 weight units
964 : : // ======================================
965 : : // total: 400000 weight units
966 [ + - + - : 1 : BOOST_CHECK_EQUAL(GetTransactionWeight(CTransaction(t)), 400000);
+ - ]
967 [ + - ]: 1 : CheckIsStandard(t);
968 : :
969 : : // increase output size by one byte, so we end up with 400004 weight units
970 [ + - + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << std::vector<unsigned char>(20, 0); // output size: 31 bytes
971 [ + - + - : 1 : BOOST_CHECK_EQUAL(GetTransactionWeight(CTransaction(t)), 400004);
+ - ]
972 [ + - + - ]: 1 : CheckIsNotStandard(t, "tx-size");
973 : :
974 : : // Check bare multisig (standard if policy flag g_bare_multi is set)
975 : 1 : g_bare_multi = true;
976 [ + - + - : 1 : t.vout[0].scriptPubKey = GetScriptForMultisig(1, {key.GetPubKey()}); // simple 1-of-1
+ - + - ]
977 [ + - ]: 1 : t.vin.resize(1);
978 [ + - ]: 1 : t.vin[0].scriptSig = CScript() << std::vector<unsigned char>(65, 0);
979 [ + - ]: 1 : CheckIsStandard(t);
980 : :
981 : 1 : g_bare_multi = false;
982 [ + - + - ]: 1 : CheckIsNotStandard(t, "bare-multisig");
983 : 1 : g_bare_multi = DEFAULT_PERMIT_BAREMULTISIG;
984 : :
985 : : // Add dust outputs up to allowed maximum
986 [ - + ]: 1 : assert(t.vout.size() == 1);
987 [ + - + - ]: 2 : t.vout.insert(t.vout.end(), MAX_DUST_OUTPUTS_PER_TX, {0, t.vout[0].scriptPubKey});
988 : :
989 : : // Check compressed P2PK outputs dust threshold (must have leading 02 or 03)
990 [ + - + - ]: 1 : t.vout[0].scriptPubKey = CScript() << std::vector<unsigned char>(33, 0x02) << OP_CHECKSIG;
991 [ + - ]: 1 : t.vout[0].nValue = 576;
992 [ + - ]: 1 : CheckIsStandard(t);
993 [ + - ]: 1 : t.vout[0].nValue = 575;
994 [ + - + - ]: 1 : CheckIsNotStandard(t, "dust");
995 : :
996 : : // Check uncompressed P2PK outputs dust threshold (must have leading 04/06/07)
997 [ + - + - ]: 1 : t.vout[0].scriptPubKey = CScript() << std::vector<unsigned char>(65, 0x04) << OP_CHECKSIG;
998 [ + - ]: 1 : t.vout[0].nValue = 672;
999 [ + - ]: 1 : CheckIsStandard(t);
1000 [ + - ]: 1 : t.vout[0].nValue = 671;
1001 [ + - + - ]: 1 : CheckIsNotStandard(t, "dust");
1002 : :
1003 : : // Check P2PKH outputs dust threshold
1004 [ + - + - : 1 : t.vout[0].scriptPubKey = CScript() << OP_DUP << OP_HASH160 << std::vector<unsigned char>(20, 0) << OP_EQUALVERIFY << OP_CHECKSIG;
+ - + - +
- ]
1005 [ + - ]: 1 : t.vout[0].nValue = 546;
1006 [ + - ]: 1 : CheckIsStandard(t);
1007 [ + - ]: 1 : t.vout[0].nValue = 545;
1008 [ + - + - ]: 1 : CheckIsNotStandard(t, "dust");
1009 : :
1010 : : // Check P2SH outputs dust threshold
1011 [ + - + - : 1 : t.vout[0].scriptPubKey = CScript() << OP_HASH160 << std::vector<unsigned char>(20, 0) << OP_EQUAL;
+ - ]
1012 [ + - ]: 1 : t.vout[0].nValue = 540;
1013 [ + - ]: 1 : CheckIsStandard(t);
1014 [ + - ]: 1 : t.vout[0].nValue = 539;
1015 [ + - + - ]: 1 : CheckIsNotStandard(t, "dust");
1016 : :
1017 : : // Check P2WPKH outputs dust threshold
1018 [ + - + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_0 << std::vector<unsigned char>(20, 0);
1019 [ + - ]: 1 : t.vout[0].nValue = 294;
1020 [ + - ]: 1 : CheckIsStandard(t);
1021 [ + - ]: 1 : t.vout[0].nValue = 293;
1022 [ + - + - ]: 1 : CheckIsNotStandard(t, "dust");
1023 : :
1024 : : // Check P2WSH outputs dust threshold
1025 [ + - + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_0 << std::vector<unsigned char>(32, 0);
1026 [ + - ]: 1 : t.vout[0].nValue = 330;
1027 [ + - ]: 1 : CheckIsStandard(t);
1028 [ + - ]: 1 : t.vout[0].nValue = 329;
1029 [ + - + - ]: 1 : CheckIsNotStandard(t, "dust");
1030 : :
1031 : : // Check P2TR outputs dust threshold (Invalid xonly key ok!)
1032 [ + - + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_1 << std::vector<unsigned char>(32, 0);
1033 [ + - ]: 1 : t.vout[0].nValue = 330;
1034 [ + - ]: 1 : CheckIsStandard(t);
1035 [ + - ]: 1 : t.vout[0].nValue = 329;
1036 [ + - + - ]: 1 : CheckIsNotStandard(t, "dust");
1037 : :
1038 : : // Check future Witness Program versions dust threshold (non-32-byte pushes are undefined for version 1)
1039 [ + + ]: 17 : for (int op = OP_1; op <= OP_16; op += 1) {
1040 [ + - + - ]: 16 : t.vout[0].scriptPubKey = CScript() << (opcodetype)op << std::vector<unsigned char>(2, 0);
1041 [ + - ]: 16 : t.vout[0].nValue = 240;
1042 [ + - ]: 16 : CheckIsStandard(t);
1043 : :
1044 [ + - ]: 16 : t.vout[0].nValue = 239;
1045 [ + - + - ]: 32 : CheckIsNotStandard(t, "dust");
1046 : : }
1047 : :
1048 : : // Check anchor outputs
1049 [ + - ]: 1 : t.vout[0].scriptPubKey = CScript() << OP_1 << ANCHOR_BYTES;
1050 [ + - + - : 2 : BOOST_CHECK(t.vout[0].scriptPubKey.IsPayToAnchor());
+ - + - ]
1051 [ + - ]: 1 : t.vout[0].nValue = 240;
1052 [ + - ]: 1 : CheckIsStandard(t);
1053 [ + - ]: 1 : t.vout[0].nValue = 239;
1054 [ + - + - ]: 2 : CheckIsNotStandard(t, "dust");
1055 : 2 : }
1056 : :
1057 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(max_standard_legacy_sigops)
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1058 : : {
1059 : 1 : CCoinsView coins_dummy;
1060 [ + - ]: 1 : CCoinsViewCache coins(&coins_dummy);
1061 : 1 : CKey key;
1062 [ + - ]: 1 : key.MakeNewKey(true);
1063 : :
1064 : : // Create a pathological P2SH script padded with as many sigops as is standard.
1065 [ + - ]: 1 : CScript max_sigops_redeem_script{CScript() << std::vector<unsigned char>{} << key.GetPubKey()};
1066 [ + - + - : 15 : for (unsigned i{0}; i < MAX_P2SH_SIGOPS - 1; ++i) max_sigops_redeem_script << OP_2DUP << OP_CHECKSIG << OP_DROP;
+ - + + ]
1067 [ + - + - ]: 1 : max_sigops_redeem_script << OP_CHECKSIG << OP_NOT;
1068 [ + - + - ]: 1 : const CScript max_sigops_p2sh{GetScriptForDestination(ScriptHash(max_sigops_redeem_script))};
1069 : :
1070 : : // Create a transaction fanning out as many such P2SH outputs as is standard to spend in a
1071 : : // single transaction, and a transaction spending them.
1072 [ + - + - ]: 1 : CMutableTransaction tx_create, tx_max_sigops;
1073 : 1 : const unsigned p2sh_inputs_count{MAX_TX_LEGACY_SIGOPS / MAX_P2SH_SIGOPS};
1074 [ + - ]: 1 : tx_create.vout.reserve(p2sh_inputs_count);
1075 [ + + ]: 167 : for (unsigned i{0}; i < p2sh_inputs_count; ++i) {
1076 [ + - ]: 166 : tx_create.vout.emplace_back(424242 + i, max_sigops_p2sh);
1077 : : }
1078 [ + - ]: 1 : auto prev_txid{tx_create.GetHash()};
1079 [ + - ]: 1 : tx_max_sigops.vin.reserve(p2sh_inputs_count);
1080 [ + + ]: 167 : for (unsigned i{0}; i < p2sh_inputs_count; ++i) {
1081 [ + - + - ]: 332 : tx_max_sigops.vin.emplace_back(prev_txid, i, CScript() << ToByteVector(max_sigops_redeem_script));
1082 : : }
1083 : :
1084 : : // p2sh_inputs_count is truncated to 166 (from 166.6666..)
1085 [ + - + - ]: 1 : BOOST_CHECK_LT(p2sh_inputs_count * MAX_P2SH_SIGOPS, MAX_TX_LEGACY_SIGOPS);
1086 [ + - + - ]: 1 : AddCoins(coins, CTransaction(tx_create), 0, false);
1087 : :
1088 : : // 2490 sigops is below the limit.
1089 [ + - + - : 1 : BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(tx_max_sigops), coins), 2490);
+ - + - ]
1090 [ + - + - : 3 : BOOST_CHECK(::AreInputsStandard(CTransaction(tx_max_sigops), coins));
+ - + - +
- ]
1091 : :
1092 : : // Adding one more input will bump this to 2505, hitting the limit.
1093 [ + - ]: 1 : tx_create.vout.emplace_back(424242, max_sigops_p2sh);
1094 [ + - ]: 1 : prev_txid = tx_create.GetHash();
1095 [ + + ]: 167 : for (unsigned i{0}; i < p2sh_inputs_count; ++i) {
1096 [ + - + - ]: 332 : tx_max_sigops.vin[i] = CTxIn(COutPoint(prev_txid, i), CScript() << ToByteVector(max_sigops_redeem_script));
1097 : : }
1098 [ + - + - ]: 2 : tx_max_sigops.vin.emplace_back(prev_txid, p2sh_inputs_count, CScript() << ToByteVector(max_sigops_redeem_script));
1099 [ + - + - ]: 1 : AddCoins(coins, CTransaction(tx_create), 0, false);
1100 [ + - + - ]: 1 : BOOST_CHECK_GT((p2sh_inputs_count + 1) * MAX_P2SH_SIGOPS, MAX_TX_LEGACY_SIGOPS);
1101 [ + - + - : 1 : BOOST_CHECK_EQUAL(GetP2SHSigOpCount(CTransaction(tx_max_sigops), coins), 2505);
+ - + - ]
1102 [ + - + - : 3 : BOOST_CHECK(!::AreInputsStandard(CTransaction(tx_max_sigops), coins));
+ - + - +
- ]
1103 : :
1104 : : // Now, check the limit can be reached with regular P2PK outputs too. Use a separate
1105 : : // preparation transaction, to demonstrate spending coins from a single tx is irrelevant.
1106 [ + - ]: 1 : CMutableTransaction tx_create_p2pk;
1107 [ + - + - ]: 1 : const auto p2pk_script{CScript() << key.GetPubKey() << OP_CHECKSIG};
1108 : 1 : unsigned p2pk_inputs_count{10}; // From 2490 to 2500.
1109 [ + + ]: 11 : for (unsigned i{0}; i < p2pk_inputs_count; ++i) {
1110 [ + - ]: 10 : tx_create_p2pk.vout.emplace_back(212121 + i, p2pk_script);
1111 : : }
1112 [ + - ]: 1 : prev_txid = tx_create_p2pk.GetHash();
1113 [ + - ]: 1 : tx_max_sigops.vin.resize(p2sh_inputs_count); // Drop the extra input.
1114 [ + + ]: 11 : for (unsigned i{0}; i < p2pk_inputs_count; ++i) {
1115 [ + - ]: 10 : tx_max_sigops.vin.emplace_back(prev_txid, i);
1116 : : }
1117 [ + - + - ]: 1 : AddCoins(coins, CTransaction(tx_create_p2pk), 0, false);
1118 : :
1119 : : // The transaction now contains exactly 2500 sigops, the check should pass.
1120 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(p2sh_inputs_count * MAX_P2SH_SIGOPS + p2pk_inputs_count * 1, MAX_TX_LEGACY_SIGOPS);
1121 [ + - + - : 3 : BOOST_CHECK(::AreInputsStandard(CTransaction(tx_max_sigops), coins));
+ - + - +
- ]
1122 : :
1123 : : // Now, add some Segwit inputs. We add one for each defined Segwit output type. The limit
1124 : : // is exclusively on non-witness sigops and therefore those should not be counted.
1125 [ + - ]: 1 : CMutableTransaction tx_create_segwit;
1126 [ + - + - ]: 1 : const auto witness_script{CScript() << key.GetPubKey() << OP_CHECKSIG};
1127 [ + - + - : 2 : tx_create_segwit.vout.emplace_back(121212, GetScriptForDestination(WitnessV0KeyHash(key.GetPubKey())));
+ - + - ]
1128 [ + - + - : 2 : tx_create_segwit.vout.emplace_back(131313, GetScriptForDestination(WitnessV0ScriptHash(witness_script)));
+ - ]
1129 [ + - + - : 2 : tx_create_segwit.vout.emplace_back(141414, GetScriptForDestination(WitnessV1Taproot{XOnlyPubKey(key.GetPubKey())}));
+ - ]
1130 [ + - ]: 1 : prev_txid = tx_create_segwit.GetHash();
1131 [ + + ]: 4 : for (unsigned i{0}; i < tx_create_segwit.vout.size(); ++i) {
1132 [ + - ]: 3 : tx_max_sigops.vin.emplace_back(prev_txid, i);
1133 : : }
1134 : :
1135 : : // The transaction now still contains exactly 2500 sigops, the check should pass.
1136 [ + - + - ]: 1 : AddCoins(coins, CTransaction(tx_create_segwit), 0, false);
1137 [ + - + - : 3 : BOOST_REQUIRE(::AreInputsStandard(CTransaction(tx_max_sigops), coins));
+ - + - +
- ]
1138 : :
1139 : : // Add one more P2PK input. We'll reach the limit.
1140 [ + - ]: 1 : tx_create_p2pk.vout.emplace_back(212121, p2pk_script);
1141 [ + - ]: 1 : prev_txid = tx_create_p2pk.GetHash();
1142 [ + - ]: 1 : tx_max_sigops.vin.resize(p2sh_inputs_count);
1143 : 1 : ++p2pk_inputs_count;
1144 [ + + ]: 12 : for (unsigned i{0}; i < p2pk_inputs_count; ++i) {
1145 [ + - ]: 11 : tx_max_sigops.vin.emplace_back(prev_txid, i);
1146 : : }
1147 [ + - + - ]: 1 : AddCoins(coins, CTransaction(tx_create_p2pk), 0, false);
1148 [ + - + - ]: 1 : BOOST_CHECK_GT(p2sh_inputs_count * MAX_P2SH_SIGOPS + p2pk_inputs_count * 1, MAX_TX_LEGACY_SIGOPS);
1149 [ + - + - : 3 : BOOST_CHECK(!::AreInputsStandard(CTransaction(tx_max_sigops), coins));
+ - + - ]
1150 : 5 : }
1151 : :
1152 : : BOOST_AUTO_TEST_SUITE_END()
|