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