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