Branch data Line data Source code
# 1 : : // Copyright (c) 2011-2021 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/validation.h>
# 14 : : #include <core_io.h>
# 15 : : #include <key.h>
# 16 : : #include <policy/policy.h>
# 17 : : #include <policy/settings.h>
# 18 : : #include <script/script.h>
# 19 : : #include <script/script_error.h>
# 20 : : #include <script/sign.h>
# 21 : : #include <script/signingprovider.h>
# 22 : : #include <script/standard.h>
# 23 : : #include <streams.h>
# 24 : : #include <test/util/script.h>
# 25 : : #include <test/util/transaction_utils.h>
# 26 : : #include <util/strencodings.h>
# 27 : : #include <util/string.h>
# 28 : : #include <validation.h>
# 29 : :
# 30 : : #include <functional>
# 31 : : #include <map>
# 32 : : #include <string>
# 33 : :
# 34 : : #include <boost/algorithm/string/classification.hpp>
# 35 : : #include <boost/algorithm/string/split.hpp>
# 36 : : #include <boost/test/unit_test.hpp>
# 37 : :
# 38 : : #include <univalue.h>
# 39 : :
# 40 : : typedef std::vector<unsigned char> valtype;
# 41 : :
# 42 : : // In script_tests.cpp
# 43 : : UniValue read_json(const std::string& jsondata);
# 44 : :
# 45 : : static std::map<std::string, unsigned int> mapFlagNames = {
# 46 : : {std::string("P2SH"), (unsigned int)SCRIPT_VERIFY_P2SH},
# 47 : : {std::string("STRICTENC"), (unsigned int)SCRIPT_VERIFY_STRICTENC},
# 48 : : {std::string("DERSIG"), (unsigned int)SCRIPT_VERIFY_DERSIG},
# 49 : : {std::string("LOW_S"), (unsigned int)SCRIPT_VERIFY_LOW_S},
# 50 : : {std::string("SIGPUSHONLY"), (unsigned int)SCRIPT_VERIFY_SIGPUSHONLY},
# 51 : : {std::string("MINIMALDATA"), (unsigned int)SCRIPT_VERIFY_MINIMALDATA},
# 52 : : {std::string("NULLDUMMY"), (unsigned int)SCRIPT_VERIFY_NULLDUMMY},
# 53 : : {std::string("DISCOURAGE_UPGRADABLE_NOPS"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS},
# 54 : : {std::string("CLEANSTACK"), (unsigned int)SCRIPT_VERIFY_CLEANSTACK},
# 55 : : {std::string("MINIMALIF"), (unsigned int)SCRIPT_VERIFY_MINIMALIF},
# 56 : : {std::string("NULLFAIL"), (unsigned int)SCRIPT_VERIFY_NULLFAIL},
# 57 : : {std::string("CHECKLOCKTIMEVERIFY"), (unsigned int)SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY},
# 58 : : {std::string("CHECKSEQUENCEVERIFY"), (unsigned int)SCRIPT_VERIFY_CHECKSEQUENCEVERIFY},
# 59 : : {std::string("WITNESS"), (unsigned int)SCRIPT_VERIFY_WITNESS},
# 60 : : {std::string("DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM},
# 61 : : {std::string("WITNESS_PUBKEYTYPE"), (unsigned int)SCRIPT_VERIFY_WITNESS_PUBKEYTYPE},
# 62 : : {std::string("CONST_SCRIPTCODE"), (unsigned int)SCRIPT_VERIFY_CONST_SCRIPTCODE},
# 63 : : {std::string("TAPROOT"), (unsigned int)SCRIPT_VERIFY_TAPROOT},
# 64 : : {std::string("DISCOURAGE_UPGRADABLE_PUBKEYTYPE"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE},
# 65 : : {std::string("DISCOURAGE_OP_SUCCESS"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS},
# 66 : : {std::string("DISCOURAGE_UPGRADABLE_TAPROOT_VERSION"), (unsigned int)SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION},
# 67 : : };
# 68 : :
# 69 : : unsigned int ParseScriptFlags(std::string strFlags)
# 70 : 2820 : {
# 71 [ + + ][ + + ]: 2820 : if (strFlags.empty() || strFlags == "NONE") return 0;
# 72 : 2422 : unsigned int flags = 0;
# 73 : 2422 : std::vector<std::string> words;
# 74 : 2422 : boost::algorithm::split(words, strFlags, boost::algorithm::is_any_of(","));
# 75 : :
# 76 [ + + ]: 2422 : for (const std::string& word : words)
# 77 : 4486 : {
# 78 [ - + ]: 4486 : if (!mapFlagNames.count(word))
# 79 : 0 : BOOST_ERROR("Bad test: unknown verification flag '" << word << "'");
# 80 : 4486 : flags |= mapFlagNames[word];
# 81 : 4486 : }
# 82 : :
# 83 : 2422 : return flags;
# 84 : 2820 : }
# 85 : :
# 86 : : // Check that all flags in STANDARD_SCRIPT_VERIFY_FLAGS are present in mapFlagNames.
# 87 : : bool CheckMapFlagNames()
# 88 : 2 : {
# 89 : 2 : unsigned int standard_flags_missing{STANDARD_SCRIPT_VERIFY_FLAGS};
# 90 [ + + ]: 42 : for (const auto& pair : mapFlagNames) {
# 91 : 42 : standard_flags_missing &= ~(pair.second);
# 92 : 42 : }
# 93 : 2 : return standard_flags_missing == 0;
# 94 : 2 : }
# 95 : :
# 96 : : std::string FormatScriptFlags(unsigned int flags)
# 97 : 268 : {
# 98 [ + + ]: 268 : if (flags == 0) {
# 99 : 78 : return "";
# 100 : 78 : }
# 101 : 190 : std::string ret;
# 102 : 190 : std::map<std::string, unsigned int>::const_iterator it = mapFlagNames.begin();
# 103 [ + + ]: 4180 : while (it != mapFlagNames.end()) {
# 104 [ + + ]: 3990 : if (flags & it->second) {
# 105 : 334 : ret += it->first + ",";
# 106 : 334 : }
# 107 : 3990 : it++;
# 108 : 3990 : }
# 109 : 190 : return ret.substr(0, ret.size() - 1);
# 110 : 268 : }
# 111 : :
# 112 : : /*
# 113 : : * Check that the input scripts of a transaction are valid/invalid as expected.
# 114 : : */
# 115 : : bool CheckTxScripts(const CTransaction& tx, const std::map<COutPoint, CScript>& map_prevout_scriptPubKeys,
# 116 : : const std::map<COutPoint, int64_t>& map_prevout_values, unsigned int flags,
# 117 : : const PrecomputedTransactionData& txdata, const std::string& strTest, bool expect_valid)
# 118 : 17838 : {
# 119 : 17838 : bool tx_valid = true;
# 120 [ + + ]: 17838 : ScriptError err = expect_valid ? SCRIPT_ERR_UNKNOWN_ERROR : SCRIPT_ERR_OK;
# 121 [ + + ][ + + ]: 41074 : for (unsigned int i = 0; i < tx.vin.size() && tx_valid; ++i) {
# 122 : 23236 : const CTxIn input = tx.vin[i];
# 123 [ + + ]: 23236 : const CAmount amount = map_prevout_values.count(input.prevout) ? map_prevout_values.at(input.prevout) : 0;
# 124 : 23236 : try {
# 125 : 23236 : tx_valid = VerifyScript(input.scriptSig, map_prevout_scriptPubKeys.at(input.prevout),
# 126 : 23236 : &input.scriptWitness, flags, TransactionSignatureChecker(&tx, i, amount, txdata, MissingDataBehavior::ASSERT_FAIL), &err);
# 127 : 23236 : } catch (...) {
# 128 : 0 : BOOST_ERROR("Bad test: " << strTest);
# 129 : 0 : return true; // The test format is bad and an error is thrown. Return true to silence further error.
# 130 : 0 : }
# 131 [ + + ]: 23236 : if (expect_valid) {
# 132 : 15194 : BOOST_CHECK_MESSAGE(tx_valid, strTest);
# 133 : 15194 : BOOST_CHECK_MESSAGE((err == SCRIPT_ERR_OK), ScriptErrorString(err));
# 134 : 15194 : err = SCRIPT_ERR_UNKNOWN_ERROR;
# 135 : 15194 : }
# 136 : 23236 : }
# 137 [ + + ]: 17838 : if (!expect_valid) {
# 138 : 7426 : BOOST_CHECK_MESSAGE(!tx_valid, strTest);
# 139 : 7426 : BOOST_CHECK_MESSAGE((err != SCRIPT_ERR_OK), ScriptErrorString(err));
# 140 : 7426 : }
# 141 : 17838 : return (tx_valid == expect_valid);
# 142 : 17838 : }
# 143 : :
# 144 : : /*
# 145 : : * Trim or fill flags to make the combination valid:
# 146 : : * WITNESS must be used with P2SH
# 147 : : * CLEANSTACK must be used WITNESS and P2SH
# 148 : : */
# 149 : :
# 150 : : unsigned int TrimFlags(unsigned int flags)
# 151 : 18522 : {
# 152 : : // WITNESS requires P2SH
# 153 [ + + ]: 18522 : if (!(flags & SCRIPT_VERIFY_P2SH)) flags &= ~(unsigned int)SCRIPT_VERIFY_WITNESS;
# 154 : :
# 155 : : // CLEANSTACK requires WITNESS (and transitively CLEANSTACK requires P2SH)
# 156 [ + + ]: 18522 : if (!(flags & SCRIPT_VERIFY_WITNESS)) flags &= ~(unsigned int)SCRIPT_VERIFY_CLEANSTACK;
# 157 : 18522 : Assert(IsValidFlagCombination(flags));
# 158 : 18522 : return flags;
# 159 : 18522 : }
# 160 : :
# 161 : : unsigned int FillFlags(unsigned int flags)
# 162 : 7462 : {
# 163 : : // CLEANSTACK implies WITNESS
# 164 [ + + ]: 7462 : if (flags & SCRIPT_VERIFY_CLEANSTACK) flags |= SCRIPT_VERIFY_WITNESS;
# 165 : :
# 166 : : // WITNESS implies P2SH (and transitively CLEANSTACK implies P2SH)
# 167 [ + + ]: 7462 : if (flags & SCRIPT_VERIFY_WITNESS) flags |= SCRIPT_VERIFY_P2SH;
# 168 : 7462 : Assert(IsValidFlagCombination(flags));
# 169 : 7462 : return flags;
# 170 : 7462 : }
# 171 : :
# 172 : : // Exclude each possible script verify flag from flags. Returns a set of these flag combinations
# 173 : : // that are valid and without duplicates. For example: if flags=1111 and the 4 possible flags are
# 174 : : // 0001, 0010, 0100, and 1000, this should return the set {0111, 1011, 1101, 1110}.
# 175 : : // Assumes that mapFlagNames contains all script verify flags.
# 176 : : std::set<unsigned int> ExcludeIndividualFlags(unsigned int flags)
# 177 : 406 : {
# 178 : 406 : std::set<unsigned int> flags_combos;
# 179 [ + + ]: 8526 : for (const auto& pair : mapFlagNames) {
# 180 : 8526 : const unsigned int flags_excluding_one = TrimFlags(flags & ~(pair.second));
# 181 [ + + ]: 8526 : if (flags != flags_excluding_one) {
# 182 : 1222 : flags_combos.insert(flags_excluding_one);
# 183 : 1222 : }
# 184 : 8526 : }
# 185 : 406 : return flags_combos;
# 186 : 406 : }
# 187 : :
# 188 : : BOOST_FIXTURE_TEST_SUITE(transaction_tests, BasicTestingSetup)
# 189 : :
# 190 : : BOOST_AUTO_TEST_CASE(tx_valid)
# 191 : 2 : {
# 192 : 2 : BOOST_CHECK_MESSAGE(CheckMapFlagNames(), "mapFlagNames is missing a script verification flag");
# 193 : : // Read tests from test/data/tx_valid.json
# 194 : 2 : UniValue tests = read_json(std::string(json_tests::tx_valid, json_tests::tx_valid + sizeof(json_tests::tx_valid)));
# 195 : :
# 196 [ + + ]: 492 : for (unsigned int idx = 0; idx < tests.size(); idx++) {
# 197 : 490 : UniValue test = tests[idx];
# 198 : 490 : std::string strTest = test.write();
# 199 [ + + ]: 490 : if (test[0].isArray())
# 200 : 238 : {
# 201 [ - + ][ - + ]: 238 : if (test.size() != 3 || !test[1].isStr() || !test[2].isStr())
# [ - + ]
# 202 : 0 : {
# 203 : 0 : BOOST_ERROR("Bad test: " << strTest);
# 204 : 0 : continue;
# 205 : 0 : }
# 206 : :
# 207 : 238 : std::map<COutPoint, CScript> mapprevOutScriptPubKeys;
# 208 : 238 : std::map<COutPoint, int64_t> mapprevOutValues;
# 209 : 238 : UniValue inputs = test[0].get_array();
# 210 : 238 : bool fValid = true;
# 211 [ + + ]: 586 : for (unsigned int inpIdx = 0; inpIdx < inputs.size(); inpIdx++) {
# 212 : 348 : const UniValue& input = inputs[inpIdx];
# 213 [ - + ]: 348 : if (!input.isArray()) {
# 214 : 0 : fValid = false;
# 215 : 0 : break;
# 216 : 0 : }
# 217 : 348 : UniValue vinput = input.get_array();
# 218 [ - + ][ - + ]: 348 : if (vinput.size() < 3 || vinput.size() > 4)
# 219 : 0 : {
# 220 : 0 : fValid = false;
# 221 : 0 : break;
# 222 : 0 : }
# 223 : 348 : COutPoint outpoint{uint256S(vinput[0].get_str()), uint32_t(vinput[1].get_int())};
# 224 : 348 : mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str());
# 225 [ + + ]: 348 : if (vinput.size() >= 4)
# 226 : 162 : {
# 227 : 162 : mapprevOutValues[outpoint] = vinput[3].get_int64();
# 228 : 162 : }
# 229 : 348 : }
# 230 [ - + ]: 238 : if (!fValid)
# 231 : 0 : {
# 232 : 0 : BOOST_ERROR("Bad test: " << strTest);
# 233 : 0 : continue;
# 234 : 0 : }
# 235 : :
# 236 : 238 : std::string transaction = test[1].get_str();
# 237 : 238 : CDataStream stream(ParseHex(transaction), SER_NETWORK, PROTOCOL_VERSION);
# 238 : 238 : CTransaction tx(deserialize, stream);
# 239 : :
# 240 : 238 : TxValidationState state;
# 241 : 238 : BOOST_CHECK_MESSAGE(CheckTransaction(tx, state), strTest);
# 242 : 238 : BOOST_CHECK(state.IsValid());
# 243 : :
# 244 : 238 : PrecomputedTransactionData txdata(tx);
# 245 : 238 : unsigned int verify_flags = ParseScriptFlags(test[2].get_str());
# 246 : :
# 247 : : // Check that the test gives a valid combination of flags (otherwise VerifyScript will throw). Don't edit the flags.
# 248 [ - + ]: 238 : if (~verify_flags != FillFlags(~verify_flags)) {
# 249 : 0 : BOOST_ERROR("Bad test flags: " << strTest);
# 250 : 0 : }
# 251 : :
# 252 : 238 : BOOST_CHECK_MESSAGE(CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, ~verify_flags, txdata, strTest, /*expect_valid=*/true),
# 253 : 238 : "Tx unexpectedly failed: " << strTest);
# 254 : :
# 255 : : // Backwards compatibility of script verification flags: Removing any flag(s) should not invalidate a valid transaction
# 256 [ + + ]: 4998 : for (const auto& [name, flag] : mapFlagNames) {
# 257 : : // Removing individual flags
# 258 : 4998 : unsigned int flags = TrimFlags(~(verify_flags | flag));
# 259 [ - + ]: 4998 : if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /*expect_valid=*/true)) {
# 260 : 0 : BOOST_ERROR("Tx unexpectedly failed with flag " << name << " unset: " << strTest);
# 261 : 0 : }
# 262 : : // Removing random combinations of flags
# 263 : 4998 : flags = TrimFlags(~(verify_flags | (unsigned int)InsecureRandBits(mapFlagNames.size())));
# 264 [ - + ]: 4998 : if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /*expect_valid=*/true)) {
# 265 : 0 : BOOST_ERROR("Tx unexpectedly failed with random flags " << ToString(flags) << ": " << strTest);
# 266 : 0 : }
# 267 : 4998 : }
# 268 : :
# 269 : : // Check that flags are maximal: transaction should fail if any unset flags are set.
# 270 [ + + ]: 238 : for (auto flags_excluding_one : ExcludeIndividualFlags(verify_flags)) {
# 271 [ - + ]: 202 : if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, ~flags_excluding_one, txdata, strTest, /*expect_valid=*/false)) {
# 272 : 0 : BOOST_ERROR("Too many flags unset: " << strTest);
# 273 : 0 : }
# 274 : 202 : }
# 275 : 238 : }
# 276 : 490 : }
# 277 : 2 : }
# 278 : :
# 279 : : BOOST_AUTO_TEST_CASE(tx_invalid)
# 280 : 2 : {
# 281 : : // Read tests from test/data/tx_invalid.json
# 282 : 2 : UniValue tests = read_json(std::string(json_tests::tx_invalid, json_tests::tx_invalid + sizeof(json_tests::tx_invalid)));
# 283 : :
# 284 [ + + ]: 404 : for (unsigned int idx = 0; idx < tests.size(); idx++) {
# 285 : 402 : UniValue test = tests[idx];
# 286 : 402 : std::string strTest = test.write();
# 287 [ + + ]: 402 : if (test[0].isArray())
# 288 : 186 : {
# 289 [ - + ][ - + ]: 186 : if (test.size() != 3 || !test[1].isStr() || !test[2].isStr())
# [ - + ]
# 290 : 0 : {
# 291 : 0 : BOOST_ERROR("Bad test: " << strTest);
# 292 : 0 : continue;
# 293 : 0 : }
# 294 : :
# 295 : 186 : std::map<COutPoint, CScript> mapprevOutScriptPubKeys;
# 296 : 186 : std::map<COutPoint, int64_t> mapprevOutValues;
# 297 : 186 : UniValue inputs = test[0].get_array();
# 298 : 186 : bool fValid = true;
# 299 [ + + ]: 406 : for (unsigned int inpIdx = 0; inpIdx < inputs.size(); inpIdx++) {
# 300 : 220 : const UniValue& input = inputs[inpIdx];
# 301 [ - + ]: 220 : if (!input.isArray()) {
# 302 : 0 : fValid = false;
# 303 : 0 : break;
# 304 : 0 : }
# 305 : 220 : UniValue vinput = input.get_array();
# 306 [ - + ][ - + ]: 220 : if (vinput.size() < 3 || vinput.size() > 4)
# 307 : 0 : {
# 308 : 0 : fValid = false;
# 309 : 0 : break;
# 310 : 0 : }
# 311 : 220 : COutPoint outpoint{uint256S(vinput[0].get_str()), uint32_t(vinput[1].get_int())};
# 312 : 220 : mapprevOutScriptPubKeys[outpoint] = ParseScript(vinput[2].get_str());
# 313 [ + + ]: 220 : if (vinput.size() >= 4)
# 314 : 58 : {
# 315 : 58 : mapprevOutValues[outpoint] = vinput[3].get_int64();
# 316 : 58 : }
# 317 : 220 : }
# 318 [ - + ]: 186 : if (!fValid)
# 319 : 0 : {
# 320 : 0 : BOOST_ERROR("Bad test: " << strTest);
# 321 : 0 : continue;
# 322 : 0 : }
# 323 : :
# 324 : 186 : std::string transaction = test[1].get_str();
# 325 : 186 : CDataStream stream(ParseHex(transaction), SER_NETWORK, PROTOCOL_VERSION );
# 326 : 186 : CTransaction tx(deserialize, stream);
# 327 : :
# 328 : 186 : TxValidationState state;
# 329 [ + + ][ - + ]: 186 : if (!CheckTransaction(tx, state) || state.IsInvalid()) {
# 330 : 18 : BOOST_CHECK_MESSAGE(test[2].get_str() == "BADTX", strTest);
# 331 : 18 : continue;
# 332 : 18 : }
# 333 : :
# 334 : 168 : PrecomputedTransactionData txdata(tx);
# 335 : 168 : unsigned int verify_flags = ParseScriptFlags(test[2].get_str());
# 336 : :
# 337 : : // Check that the test gives a valid combination of flags (otherwise VerifyScript will throw). Don't edit the flags.
# 338 [ - + ]: 168 : if (verify_flags != FillFlags(verify_flags)) {
# 339 : 0 : BOOST_ERROR("Bad test flags: " << strTest);
# 340 : 0 : }
# 341 : :
# 342 : : // Not using FillFlags() in the main test, in order to detect invalid verifyFlags combination
# 343 : 168 : BOOST_CHECK_MESSAGE(CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, verify_flags, txdata, strTest, /*expect_valid=*/false),
# 344 : 168 : "Tx unexpectedly passed: " << strTest);
# 345 : :
# 346 : : // Backwards compatibility of script verification flags: Adding any flag(s) should not validate an invalid transaction
# 347 [ + + ]: 3528 : for (const auto& [name, flag] : mapFlagNames) {
# 348 : 3528 : unsigned int flags = FillFlags(verify_flags | flag);
# 349 : : // Adding individual flags
# 350 [ - + ]: 3528 : if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /*expect_valid=*/false)) {
# 351 : 0 : BOOST_ERROR("Tx unexpectedly passed with flag " << name << " set: " << strTest);
# 352 : 0 : }
# 353 : : // Adding random combinations of flags
# 354 : 3528 : flags = FillFlags(verify_flags | (unsigned int)InsecureRandBits(mapFlagNames.size()));
# 355 [ - + ]: 3528 : if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags, txdata, strTest, /*expect_valid=*/false)) {
# 356 : 0 : BOOST_ERROR("Tx unexpectedly passed with random flags " << name << ": " << strTest);
# 357 : 0 : }
# 358 : 3528 : }
# 359 : :
# 360 : : // Check that flags are minimal: transaction should succeed if any set flags are unset.
# 361 [ + + ]: 178 : for (auto flags_excluding_one : ExcludeIndividualFlags(verify_flags)) {
# 362 [ - + ]: 178 : if (!CheckTxScripts(tx, mapprevOutScriptPubKeys, mapprevOutValues, flags_excluding_one, txdata, strTest, /*expect_valid=*/true)) {
# 363 : 0 : BOOST_ERROR("Too many flags set: " << strTest);
# 364 : 0 : }
# 365 : 178 : }
# 366 : 168 : }
# 367 : 402 : }
# 368 : 2 : }
# 369 : :
# 370 : : BOOST_AUTO_TEST_CASE(basic_transaction_tests)
# 371 : 2 : {
# 372 : : // Random real transaction (e2769b09e784f32f62ef849763d4f45b98e07ba658647343b915ff832b110436)
# 373 : 2 : 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};
# 374 : 2 : std::vector<unsigned char> vch(ch, ch + sizeof(ch) -1);
# 375 : 2 : CDataStream stream(vch, SER_DISK, CLIENT_VERSION);
# 376 : 2 : CMutableTransaction tx;
# 377 : 2 : stream >> tx;
# 378 : 2 : TxValidationState state;
# 379 : 2 : BOOST_CHECK_MESSAGE(CheckTransaction(CTransaction(tx), state) && state.IsValid(), "Simple deserialized transaction should be valid.");
# 380 : :
# 381 : : // Check that duplicate txins fail
# 382 : 2 : tx.vin.push_back(tx.vin[0]);
# 383 : 2 : BOOST_CHECK_MESSAGE(!CheckTransaction(CTransaction(tx), state) || !state.IsValid(), "Transaction with duplicate txins should be invalid.");
# 384 : 2 : }
# 385 : :
# 386 : : BOOST_AUTO_TEST_CASE(test_Get)
# 387 : 2 : {
# 388 : 2 : FillableSigningProvider keystore;
# 389 : 2 : CCoinsView coinsDummy;
# 390 : 2 : CCoinsViewCache coins(&coinsDummy);
# 391 : 2 : std::vector<CMutableTransaction> dummyTransactions =
# 392 : 2 : SetupDummyInputs(keystore, coins, {11*CENT, 50*CENT, 21*CENT, 22*CENT});
# 393 : :
# 394 : 2 : CMutableTransaction t1;
# 395 : 2 : t1.vin.resize(3);
# 396 : 2 : t1.vin[0].prevout.hash = dummyTransactions[0].GetHash();
# 397 : 2 : t1.vin[0].prevout.n = 1;
# 398 : 2 : t1.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
# 399 : 2 : t1.vin[1].prevout.hash = dummyTransactions[1].GetHash();
# 400 : 2 : t1.vin[1].prevout.n = 0;
# 401 : 2 : t1.vin[1].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
# 402 : 2 : t1.vin[2].prevout.hash = dummyTransactions[1].GetHash();
# 403 : 2 : t1.vin[2].prevout.n = 1;
# 404 : 2 : t1.vin[2].scriptSig << std::vector<unsigned char>(65, 0) << std::vector<unsigned char>(33, 4);
# 405 : 2 : t1.vout.resize(2);
# 406 : 2 : t1.vout[0].nValue = 90*CENT;
# 407 : 2 : t1.vout[0].scriptPubKey << OP_1;
# 408 : :
# 409 : 2 : BOOST_CHECK(AreInputsStandard(CTransaction(t1), coins));
# 410 : 2 : }
# 411 : :
# 412 : : static void CreateCreditAndSpend(const FillableSigningProvider& keystore, const CScript& outscript, CTransactionRef& output, CMutableTransaction& input, bool success = true)
# 413 : 48 : {
# 414 : 48 : CMutableTransaction outputm;
# 415 : 48 : outputm.nVersion = 1;
# 416 : 48 : outputm.vin.resize(1);
# 417 : 48 : outputm.vin[0].prevout.SetNull();
# 418 : 48 : outputm.vin[0].scriptSig = CScript();
# 419 : 48 : outputm.vout.resize(1);
# 420 : 48 : outputm.vout[0].nValue = 1;
# 421 : 48 : outputm.vout[0].scriptPubKey = outscript;
# 422 : 48 : CDataStream ssout(SER_NETWORK, PROTOCOL_VERSION);
# 423 : 48 : ssout << outputm;
# 424 : 48 : ssout >> output;
# 425 : 48 : assert(output->vin.size() == 1);
# 426 : 0 : assert(output->vin[0] == outputm.vin[0]);
# 427 : 0 : assert(output->vout.size() == 1);
# 428 : 0 : assert(output->vout[0] == outputm.vout[0]);
# 429 : :
# 430 : 0 : CMutableTransaction inputm;
# 431 : 48 : inputm.nVersion = 1;
# 432 : 48 : inputm.vin.resize(1);
# 433 : 48 : inputm.vin[0].prevout.hash = output->GetHash();
# 434 : 48 : inputm.vin[0].prevout.n = 0;
# 435 : 48 : inputm.vout.resize(1);
# 436 : 48 : inputm.vout[0].nValue = 1;
# 437 : 48 : inputm.vout[0].scriptPubKey = CScript();
# 438 : 48 : bool ret = SignSignature(keystore, *output, inputm, 0, SIGHASH_ALL);
# 439 : 48 : assert(ret == success);
# 440 : 0 : CDataStream ssin(SER_NETWORK, PROTOCOL_VERSION);
# 441 : 48 : ssin << inputm;
# 442 : 48 : ssin >> input;
# 443 : 48 : assert(input.vin.size() == 1);
# 444 : 0 : assert(input.vin[0] == inputm.vin[0]);
# 445 : 0 : assert(input.vout.size() == 1);
# 446 : 0 : assert(input.vout[0] == inputm.vout[0]);
# 447 : 0 : assert(input.vin[0].scriptWitness.stack == inputm.vin[0].scriptWitness.stack);
# 448 : 48 : }
# 449 : :
# 450 : : static void CheckWithFlag(const CTransactionRef& output, const CMutableTransaction& input, uint32_t flags, bool success)
# 451 : 138 : {
# 452 : 138 : ScriptError error;
# 453 : 138 : CTransaction inputi(input);
# 454 : 138 : 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);
# 455 : 138 : assert(ret == success);
# 456 : 138 : }
# 457 : :
# 458 : : static CScript PushAll(const std::vector<valtype>& values)
# 459 : 6 : {
# 460 : 6 : CScript result;
# 461 [ + + ]: 10 : for (const valtype& v : values) {
# 462 [ - + ]: 10 : if (v.size() == 0) {
# 463 : 0 : result << OP_0;
# 464 [ - + ][ # # ]: 10 : } else if (v.size() == 1 && v[0] >= 1 && v[0] <= 16) {
# [ # # ]
# 465 : 0 : result << CScript::EncodeOP_N(v[0]);
# 466 [ - + ][ # # ]: 10 : } else if (v.size() == 1 && v[0] == 0x81) {
# 467 : 0 : result << OP_1NEGATE;
# 468 : 10 : } else {
# 469 : 10 : result << v;
# 470 : 10 : }
# 471 : 10 : }
# 472 : 6 : return result;
# 473 : 6 : }
# 474 : :
# 475 : : static void ReplaceRedeemScript(CScript& script, const CScript& redeemScript)
# 476 : 6 : {
# 477 : 6 : std::vector<valtype> stack;
# 478 : 6 : EvalScript(stack, script, SCRIPT_VERIFY_STRICTENC, BaseSignatureChecker(), SigVersion::BASE);
# 479 : 6 : assert(stack.size() > 0);
# 480 : 0 : stack.back() = std::vector<unsigned char>(redeemScript.begin(), redeemScript.end());
# 481 : 6 : script = PushAll(stack);
# 482 : 6 : }
# 483 : :
# 484 : : BOOST_AUTO_TEST_CASE(test_big_witness_transaction)
# 485 : 2 : {
# 486 : 2 : CMutableTransaction mtx;
# 487 : 2 : mtx.nVersion = 1;
# 488 : :
# 489 : 2 : CKey key;
# 490 : 2 : key.MakeNewKey(true); // Need to use compressed keys in segwit or the signing will fail
# 491 : 2 : FillableSigningProvider keystore;
# 492 : 2 : BOOST_CHECK(keystore.AddKeyPubKey(key, key.GetPubKey()));
# 493 : 2 : CKeyID hash = key.GetPubKey().GetID();
# 494 : 2 : CScript scriptPubKey = CScript() << OP_0 << std::vector<unsigned char>(hash.begin(), hash.end());
# 495 : :
# 496 : 2 : std::vector<int> sigHashes;
# 497 : 2 : sigHashes.push_back(SIGHASH_NONE | SIGHASH_ANYONECANPAY);
# 498 : 2 : sigHashes.push_back(SIGHASH_SINGLE | SIGHASH_ANYONECANPAY);
# 499 : 2 : sigHashes.push_back(SIGHASH_ALL | SIGHASH_ANYONECANPAY);
# 500 : 2 : sigHashes.push_back(SIGHASH_NONE);
# 501 : 2 : sigHashes.push_back(SIGHASH_SINGLE);
# 502 : 2 : sigHashes.push_back(SIGHASH_ALL);
# 503 : :
# 504 : : // create a big transaction of 4500 inputs signed by the same key
# 505 [ + + ]: 9002 : for(uint32_t ij = 0; ij < 4500; ij++) {
# 506 : 9000 : uint32_t i = mtx.vin.size();
# 507 : 9000 : uint256 prevId;
# 508 : 9000 : prevId.SetHex("0000000000000000000000000000000000000000000000000000000000000100");
# 509 : 9000 : COutPoint outpoint(prevId, i);
# 510 : :
# 511 : 9000 : mtx.vin.resize(mtx.vin.size() + 1);
# 512 : 9000 : mtx.vin[i].prevout = outpoint;
# 513 : 9000 : mtx.vin[i].scriptSig = CScript();
# 514 : :
# 515 : 9000 : mtx.vout.resize(mtx.vout.size() + 1);
# 516 : 9000 : mtx.vout[i].nValue = 1000;
# 517 : 9000 : mtx.vout[i].scriptPubKey = CScript() << OP_1;
# 518 : 9000 : }
# 519 : :
# 520 : : // sign all inputs
# 521 [ + + ]: 9002 : for(uint32_t i = 0; i < mtx.vin.size(); i++) {
# 522 : 9000 : bool hashSigned = SignSignature(keystore, scriptPubKey, mtx, i, 1000, sigHashes.at(i % sigHashes.size()));
# 523 : 9000 : assert(hashSigned);
# 524 : 9000 : }
# 525 : :
# 526 : 2 : CDataStream ssout(SER_NETWORK, PROTOCOL_VERSION);
# 527 : 2 : ssout << mtx;
# 528 : 2 : CTransaction tx(deserialize, ssout);
# 529 : :
# 530 : : // check all inputs concurrently, with the cache
# 531 : 2 : PrecomputedTransactionData txdata(tx);
# 532 : 2 : CCheckQueue<CScriptCheck> scriptcheckqueue(128);
# 533 : 2 : CCheckQueueControl<CScriptCheck> control(&scriptcheckqueue);
# 534 : :
# 535 : 2 : scriptcheckqueue.StartWorkerThreads(20);
# 536 : :
# 537 : 2 : std::vector<Coin> coins;
# 538 [ + + ]: 9002 : for(uint32_t i = 0; i < mtx.vin.size(); i++) {
# 539 : 9000 : Coin coin;
# 540 : 9000 : coin.nHeight = 1;
# 541 : 9000 : coin.fCoinBase = false;
# 542 : 9000 : coin.out.nValue = 1000;
# 543 : 9000 : coin.out.scriptPubKey = scriptPubKey;
# 544 : 9000 : coins.emplace_back(std::move(coin));
# 545 : 9000 : }
# 546 : :
# 547 [ + + ]: 9002 : for(uint32_t i = 0; i < mtx.vin.size(); i++) {
# 548 : 9000 : std::vector<CScriptCheck> vChecks;
# 549 : 9000 : CScriptCheck check(coins[tx.vin[i].prevout.n].out, tx, i, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false, &txdata);
# 550 : 9000 : vChecks.push_back(CScriptCheck());
# 551 : 9000 : check.swap(vChecks.back());
# 552 : 9000 : control.Add(vChecks);
# 553 : 9000 : }
# 554 : :
# 555 : 2 : bool controlCheck = control.Wait();
# 556 : 2 : assert(controlCheck);
# 557 : 0 : scriptcheckqueue.StopWorkerThreads();
# 558 : 2 : }
# 559 : :
# 560 : : SignatureData CombineSignatures(const CMutableTransaction& input1, const CMutableTransaction& input2, const CTransactionRef tx)
# 561 : 8 : {
# 562 : 8 : SignatureData sigdata;
# 563 : 8 : sigdata = DataFromTransaction(input1, 0, tx->vout[0]);
# 564 : 8 : sigdata.MergeSignatureData(DataFromTransaction(input2, 0, tx->vout[0]));
# 565 : 8 : ProduceSignature(DUMMY_SIGNING_PROVIDER, MutableTransactionSignatureCreator(&input1, 0, tx->vout[0].nValue, SIGHASH_ALL), tx->vout[0].scriptPubKey, sigdata);
# 566 : 8 : return sigdata;
# 567 : 8 : }
# 568 : :
# 569 : : BOOST_AUTO_TEST_CASE(test_witness)
# 570 : 2 : {
# 571 : 2 : FillableSigningProvider keystore, keystore2;
# 572 : 2 : CKey key1, key2, key3, key1L, key2L;
# 573 : 2 : CPubKey pubkey1, pubkey2, pubkey3, pubkey1L, pubkey2L;
# 574 : 2 : key1.MakeNewKey(true);
# 575 : 2 : key2.MakeNewKey(true);
# 576 : 2 : key3.MakeNewKey(true);
# 577 : 2 : key1L.MakeNewKey(false);
# 578 : 2 : key2L.MakeNewKey(false);
# 579 : 2 : pubkey1 = key1.GetPubKey();
# 580 : 2 : pubkey2 = key2.GetPubKey();
# 581 : 2 : pubkey3 = key3.GetPubKey();
# 582 : 2 : pubkey1L = key1L.GetPubKey();
# 583 : 2 : pubkey2L = key2L.GetPubKey();
# 584 : 2 : BOOST_CHECK(keystore.AddKeyPubKey(key1, pubkey1));
# 585 : 2 : BOOST_CHECK(keystore.AddKeyPubKey(key2, pubkey2));
# 586 : 2 : BOOST_CHECK(keystore.AddKeyPubKey(key1L, pubkey1L));
# 587 : 2 : BOOST_CHECK(keystore.AddKeyPubKey(key2L, pubkey2L));
# 588 : 2 : CScript scriptPubkey1, scriptPubkey2, scriptPubkey1L, scriptPubkey2L, scriptMulti;
# 589 : 2 : scriptPubkey1 << ToByteVector(pubkey1) << OP_CHECKSIG;
# 590 : 2 : scriptPubkey2 << ToByteVector(pubkey2) << OP_CHECKSIG;
# 591 : 2 : scriptPubkey1L << ToByteVector(pubkey1L) << OP_CHECKSIG;
# 592 : 2 : scriptPubkey2L << ToByteVector(pubkey2L) << OP_CHECKSIG;
# 593 : 2 : std::vector<CPubKey> oneandthree;
# 594 : 2 : oneandthree.push_back(pubkey1);
# 595 : 2 : oneandthree.push_back(pubkey3);
# 596 : 2 : scriptMulti = GetScriptForMultisig(2, oneandthree);
# 597 : 2 : BOOST_CHECK(keystore.AddCScript(scriptPubkey1));
# 598 : 2 : BOOST_CHECK(keystore.AddCScript(scriptPubkey2));
# 599 : 2 : BOOST_CHECK(keystore.AddCScript(scriptPubkey1L));
# 600 : 2 : BOOST_CHECK(keystore.AddCScript(scriptPubkey2L));
# 601 : 2 : BOOST_CHECK(keystore.AddCScript(scriptMulti));
# 602 : 2 : CScript destination_script_1, destination_script_2, destination_script_1L, destination_script_2L, destination_script_multi;
# 603 : 2 : destination_script_1 = GetScriptForDestination(WitnessV0KeyHash(pubkey1));
# 604 : 2 : destination_script_2 = GetScriptForDestination(WitnessV0KeyHash(pubkey2));
# 605 : 2 : destination_script_1L = GetScriptForDestination(WitnessV0KeyHash(pubkey1L));
# 606 : 2 : destination_script_2L = GetScriptForDestination(WitnessV0KeyHash(pubkey2L));
# 607 : 2 : destination_script_multi = GetScriptForDestination(WitnessV0ScriptHash(scriptMulti));
# 608 : 2 : BOOST_CHECK(keystore.AddCScript(destination_script_1));
# 609 : 2 : BOOST_CHECK(keystore.AddCScript(destination_script_2));
# 610 : 2 : BOOST_CHECK(keystore.AddCScript(destination_script_1L));
# 611 : 2 : BOOST_CHECK(keystore.AddCScript(destination_script_2L));
# 612 : 2 : BOOST_CHECK(keystore.AddCScript(destination_script_multi));
# 613 : 2 : BOOST_CHECK(keystore2.AddCScript(scriptMulti));
# 614 : 2 : BOOST_CHECK(keystore2.AddCScript(destination_script_multi));
# 615 : 2 : BOOST_CHECK(keystore2.AddKeyPubKey(key3, pubkey3));
# 616 : :
# 617 : 2 : CTransactionRef output1, output2;
# 618 : 2 : CMutableTransaction input1, input2;
# 619 : :
# 620 : : // Normal pay-to-compressed-pubkey.
# 621 : 2 : CreateCreditAndSpend(keystore, scriptPubkey1, output1, input1);
# 622 : 2 : CreateCreditAndSpend(keystore, scriptPubkey2, output2, input2);
# 623 : 2 : CheckWithFlag(output1, input1, 0, true);
# 624 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
# 625 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
# 626 : 2 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
# 627 : 2 : CheckWithFlag(output1, input2, 0, false);
# 628 : 2 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
# 629 : 2 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
# 630 : 2 : CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
# 631 : :
# 632 : : // P2SH pay-to-compressed-pubkey.
# 633 : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(scriptPubkey1)), output1, input1);
# 634 : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(scriptPubkey2)), output2, input2);
# 635 : 2 : ReplaceRedeemScript(input2.vin[0].scriptSig, scriptPubkey1);
# 636 : 2 : CheckWithFlag(output1, input1, 0, true);
# 637 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
# 638 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
# 639 : 2 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
# 640 : 2 : CheckWithFlag(output1, input2, 0, true);
# 641 : 2 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
# 642 : 2 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
# 643 : 2 : CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
# 644 : :
# 645 : : // Witness pay-to-compressed-pubkey (v0).
# 646 : 2 : CreateCreditAndSpend(keystore, destination_script_1, output1, input1);
# 647 : 2 : CreateCreditAndSpend(keystore, destination_script_2, output2, input2);
# 648 : 2 : CheckWithFlag(output1, input1, 0, true);
# 649 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
# 650 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
# 651 : 2 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
# 652 : 2 : CheckWithFlag(output1, input2, 0, true);
# 653 : 2 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, true);
# 654 : 2 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
# 655 : 2 : CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
# 656 : :
# 657 : : // P2SH witness pay-to-compressed-pubkey (v0).
# 658 : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(destination_script_1)), output1, input1);
# 659 : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(destination_script_2)), output2, input2);
# 660 : 2 : ReplaceRedeemScript(input2.vin[0].scriptSig, destination_script_1);
# 661 : 2 : CheckWithFlag(output1, input1, 0, true);
# 662 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
# 663 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
# 664 : 2 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
# 665 : 2 : CheckWithFlag(output1, input2, 0, true);
# 666 : 2 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, true);
# 667 : 2 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
# 668 : 2 : CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
# 669 : :
# 670 : : // Normal pay-to-uncompressed-pubkey.
# 671 : 2 : CreateCreditAndSpend(keystore, scriptPubkey1L, output1, input1);
# 672 : 2 : CreateCreditAndSpend(keystore, scriptPubkey2L, output2, input2);
# 673 : 2 : CheckWithFlag(output1, input1, 0, true);
# 674 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
# 675 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
# 676 : 2 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
# 677 : 2 : CheckWithFlag(output1, input2, 0, false);
# 678 : 2 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
# 679 : 2 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
# 680 : 2 : CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
# 681 : :
# 682 : : // P2SH pay-to-uncompressed-pubkey.
# 683 : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(scriptPubkey1L)), output1, input1);
# 684 : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(scriptPubkey2L)), output2, input2);
# 685 : 2 : ReplaceRedeemScript(input2.vin[0].scriptSig, scriptPubkey1L);
# 686 : 2 : CheckWithFlag(output1, input1, 0, true);
# 687 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
# 688 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, true);
# 689 : 2 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
# 690 : 2 : CheckWithFlag(output1, input2, 0, true);
# 691 : 2 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_P2SH, false);
# 692 : 2 : CheckWithFlag(output1, input2, SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_P2SH, false);
# 693 : 2 : CheckWithFlag(output1, input2, STANDARD_SCRIPT_VERIFY_FLAGS, false);
# 694 : :
# 695 : : // Signing disabled for witness pay-to-uncompressed-pubkey (v1).
# 696 : 2 : CreateCreditAndSpend(keystore, destination_script_1L, output1, input1, false);
# 697 : 2 : CreateCreditAndSpend(keystore, destination_script_2L, output2, input2, false);
# 698 : :
# 699 : : // Signing disabled for P2SH witness pay-to-uncompressed-pubkey (v1).
# 700 : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(destination_script_1L)), output1, input1, false);
# 701 : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(destination_script_2L)), output2, input2, false);
# 702 : :
# 703 : : // Normal 2-of-2 multisig
# 704 : 2 : CreateCreditAndSpend(keystore, scriptMulti, output1, input1, false);
# 705 : 2 : CheckWithFlag(output1, input1, 0, false);
# 706 : 2 : CreateCreditAndSpend(keystore2, scriptMulti, output2, input2, false);
# 707 : 2 : CheckWithFlag(output2, input2, 0, false);
# 708 : 2 : BOOST_CHECK(*output1 == *output2);
# 709 : 2 : UpdateInput(input1.vin[0], CombineSignatures(input1, input2, output1));
# 710 : 2 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
# 711 : :
# 712 : : // P2SH 2-of-2 multisig
# 713 : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(scriptMulti)), output1, input1, false);
# 714 : 2 : CheckWithFlag(output1, input1, 0, true);
# 715 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, false);
# 716 : 2 : CreateCreditAndSpend(keystore2, GetScriptForDestination(ScriptHash(scriptMulti)), output2, input2, false);
# 717 : 2 : CheckWithFlag(output2, input2, 0, true);
# 718 : 2 : CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH, false);
# 719 : 2 : BOOST_CHECK(*output1 == *output2);
# 720 : 2 : UpdateInput(input1.vin[0], CombineSignatures(input1, input2, output1));
# 721 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
# 722 : 2 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
# 723 : :
# 724 : : // Witness 2-of-2 multisig
# 725 : 2 : CreateCreditAndSpend(keystore, destination_script_multi, output1, input1, false);
# 726 : 2 : CheckWithFlag(output1, input1, 0, true);
# 727 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
# 728 : 2 : CreateCreditAndSpend(keystore2, destination_script_multi, output2, input2, false);
# 729 : 2 : CheckWithFlag(output2, input2, 0, true);
# 730 : 2 : CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
# 731 : 2 : BOOST_CHECK(*output1 == *output2);
# 732 : 2 : UpdateInput(input1.vin[0], CombineSignatures(input1, input2, output1));
# 733 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true);
# 734 : 2 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
# 735 : :
# 736 : : // P2SH witness 2-of-2 multisig
# 737 : 2 : CreateCreditAndSpend(keystore, GetScriptForDestination(ScriptHash(destination_script_multi)), output1, input1, false);
# 738 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH, true);
# 739 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
# 740 : 2 : CreateCreditAndSpend(keystore2, GetScriptForDestination(ScriptHash(destination_script_multi)), output2, input2, false);
# 741 : 2 : CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH, true);
# 742 : 2 : CheckWithFlag(output2, input2, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, false);
# 743 : 2 : BOOST_CHECK(*output1 == *output2);
# 744 : 2 : UpdateInput(input1.vin[0], CombineSignatures(input1, input2, output1));
# 745 : 2 : CheckWithFlag(output1, input1, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, true);
# 746 : 2 : CheckWithFlag(output1, input1, STANDARD_SCRIPT_VERIFY_FLAGS, true);
# 747 : 2 : }
# 748 : :
# 749 : : BOOST_AUTO_TEST_CASE(test_IsStandard)
# 750 : 2 : {
# 751 : 2 : LOCK(cs_main);
# 752 : 2 : FillableSigningProvider keystore;
# 753 : 2 : CCoinsView coinsDummy;
# 754 : 2 : CCoinsViewCache coins(&coinsDummy);
# 755 : 2 : std::vector<CMutableTransaction> dummyTransactions =
# 756 : 2 : SetupDummyInputs(keystore, coins, {11*CENT, 50*CENT, 21*CENT, 22*CENT});
# 757 : :
# 758 : 2 : CMutableTransaction t;
# 759 : 2 : t.vin.resize(1);
# 760 : 2 : t.vin[0].prevout.hash = dummyTransactions[0].GetHash();
# 761 : 2 : t.vin[0].prevout.n = 1;
# 762 : 2 : t.vin[0].scriptSig << std::vector<unsigned char>(65, 0);
# 763 : 2 : t.vout.resize(1);
# 764 : 2 : t.vout[0].nValue = 90*CENT;
# 765 : 2 : CKey key;
# 766 : 2 : key.MakeNewKey(true);
# 767 : 2 : t.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
# 768 : :
# 769 : 74 : constexpr auto CheckIsStandard = [](const auto& t) {
# 770 : 74 : std::string reason;
# 771 : 74 : BOOST_CHECK(IsStandardTx(CTransaction(t), reason));
# 772 : 74 : BOOST_CHECK(reason.empty());
# 773 : 74 : };
# 774 : 192 : constexpr auto CheckIsNotStandard = [](const auto& t, const std::string& reason_in) {
# 775 : 192 : std::string reason;
# 776 : 192 : BOOST_CHECK(!IsStandardTx(CTransaction(t), reason));
# 777 : 192 : BOOST_CHECK_EQUAL(reason_in, reason);
# 778 : 192 : };
# 779 : :
# 780 : 2 : CheckIsStandard(t);
# 781 : :
# 782 : : // Check dust with default relay fee:
# 783 : 2 : CAmount nDustThreshold = 182 * dustRelayFee.GetFeePerK() / 1000;
# 784 : 2 : BOOST_CHECK_EQUAL(nDustThreshold, 546);
# 785 : : // dust:
# 786 : 2 : t.vout[0].nValue = nDustThreshold - 1;
# 787 : 2 : CheckIsNotStandard(t, "dust");
# 788 : : // not dust:
# 789 : 2 : t.vout[0].nValue = nDustThreshold;
# 790 : 2 : CheckIsStandard(t);
# 791 : :
# 792 : : // Disallowed nVersion
# 793 : 2 : t.nVersion = -1;
# 794 : 2 : CheckIsNotStandard(t, "version");
# 795 : :
# 796 : 2 : t.nVersion = 0;
# 797 : 2 : CheckIsNotStandard(t, "version");
# 798 : :
# 799 : 2 : t.nVersion = 3;
# 800 : 2 : CheckIsNotStandard(t, "version");
# 801 : :
# 802 : : // Allowed nVersion
# 803 : 2 : t.nVersion = 1;
# 804 : 2 : CheckIsStandard(t);
# 805 : :
# 806 : 2 : t.nVersion = 2;
# 807 : 2 : CheckIsStandard(t);
# 808 : :
# 809 : : // Check dust with odd relay fee to verify rounding:
# 810 : : // nDustThreshold = 182 * 3702 / 1000
# 811 : 2 : dustRelayFee = CFeeRate(3702);
# 812 : : // dust:
# 813 : 2 : t.vout[0].nValue = 674 - 1;
# 814 : 2 : CheckIsNotStandard(t, "dust");
# 815 : : // not dust:
# 816 : 2 : t.vout[0].nValue = 674;
# 817 : 2 : CheckIsStandard(t);
# 818 : 2 : dustRelayFee = CFeeRate(DUST_RELAY_TX_FEE);
# 819 : :
# 820 : 2 : t.vout[0].scriptPubKey = CScript() << OP_1;
# 821 : 2 : CheckIsNotStandard(t, "scriptpubkey");
# 822 : :
# 823 : : // MAX_OP_RETURN_RELAY-byte TxoutType::NULL_DATA (standard)
# 824 : 2 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
# 825 : 2 : BOOST_CHECK_EQUAL(MAX_OP_RETURN_RELAY, t.vout[0].scriptPubKey.size());
# 826 : 2 : CheckIsStandard(t);
# 827 : :
# 828 : : // MAX_OP_RETURN_RELAY+1-byte TxoutType::NULL_DATA (non-standard)
# 829 : 2 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3804678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef3800");
# 830 : 2 : BOOST_CHECK_EQUAL(MAX_OP_RETURN_RELAY + 1, t.vout[0].scriptPubKey.size());
# 831 : 2 : CheckIsNotStandard(t, "scriptpubkey");
# 832 : :
# 833 : : // Data payload can be encoded in any way...
# 834 : 2 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("");
# 835 : 2 : CheckIsStandard(t);
# 836 : 2 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("00") << ParseHex("01");
# 837 : 2 : CheckIsStandard(t);
# 838 : : // OP_RESERVED *is* considered to be a PUSHDATA type opcode by IsPushOnly()!
# 839 : 2 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << OP_RESERVED << -1 << 0 << ParseHex("01") << 2 << 3 << 4 << 5 << 6 << 7 << 8 << 9 << 10 << 11 << 12 << 13 << 14 << 15 << 16;
# 840 : 2 : CheckIsStandard(t);
# 841 : 2 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << 0 << ParseHex("01") << 2 << ParseHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
# 842 : 2 : CheckIsStandard(t);
# 843 : :
# 844 : : // ...so long as it only contains PUSHDATA's
# 845 : 2 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << OP_RETURN;
# 846 : 2 : CheckIsNotStandard(t, "scriptpubkey");
# 847 : :
# 848 : : // TxoutType::NULL_DATA w/o PUSHDATA
# 849 : 2 : t.vout.resize(1);
# 850 : 2 : t.vout[0].scriptPubKey = CScript() << OP_RETURN;
# 851 : 2 : CheckIsStandard(t);
# 852 : :
# 853 : : // Only one TxoutType::NULL_DATA permitted in all cases
# 854 : 2 : t.vout.resize(2);
# 855 : 2 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
# 856 : 2 : t.vout[0].nValue = 0;
# 857 : 2 : t.vout[1].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
# 858 : 2 : t.vout[1].nValue = 0;
# 859 : 2 : CheckIsNotStandard(t, "multi-op-return");
# 860 : :
# 861 : 2 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << ParseHex("04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38");
# 862 : 2 : t.vout[1].scriptPubKey = CScript() << OP_RETURN;
# 863 : 2 : CheckIsNotStandard(t, "multi-op-return");
# 864 : :
# 865 : 2 : t.vout[0].scriptPubKey = CScript() << OP_RETURN;
# 866 : 2 : t.vout[1].scriptPubKey = CScript() << OP_RETURN;
# 867 : 2 : CheckIsNotStandard(t, "multi-op-return");
# 868 : :
# 869 : : // Check large scriptSig (non-standard if size is >1650 bytes)
# 870 : 2 : t.vout.resize(1);
# 871 : 2 : t.vout[0].nValue = MAX_MONEY;
# 872 : 2 : t.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
# 873 : : // OP_PUSHDATA2 with len (3 bytes) + data (1647 bytes) = 1650 bytes
# 874 : 2 : t.vin[0].scriptSig = CScript() << std::vector<unsigned char>(1647, 0); // 1650
# 875 : 2 : CheckIsStandard(t);
# 876 : :
# 877 : 2 : t.vin[0].scriptSig = CScript() << std::vector<unsigned char>(1648, 0); // 1651
# 878 : 2 : CheckIsNotStandard(t, "scriptsig-size");
# 879 : :
# 880 : : // Check scriptSig format (non-standard if there are any other ops than just PUSHs)
# 881 : 2 : t.vin[0].scriptSig = CScript()
# 882 : 2 : << OP_TRUE << OP_0 << OP_1NEGATE << OP_16 // OP_n (single byte pushes: n = 1, 0, -1, 16)
# 883 : 2 : << std::vector<unsigned char>(75, 0) // OP_PUSHx [...x bytes...]
# 884 : 2 : << std::vector<unsigned char>(235, 0) // OP_PUSHDATA1 x [...x bytes...]
# 885 : 2 : << std::vector<unsigned char>(1234, 0) // OP_PUSHDATA2 x [...x bytes...]
# 886 : 2 : << OP_9;
# 887 : 2 : CheckIsStandard(t);
# 888 : :
# 889 : 2 : const std::vector<unsigned char> non_push_ops = { // arbitrary set of non-push operations
# 890 : 2 : OP_NOP, OP_VERIFY, OP_IF, OP_ROT, OP_3DUP, OP_SIZE, OP_EQUAL, OP_ADD, OP_SUB,
# 891 : 2 : OP_HASH256, OP_CODESEPARATOR, OP_CHECKSIG, OP_CHECKLOCKTIMEVERIFY };
# 892 : :
# 893 : 2 : CScript::const_iterator pc = t.vin[0].scriptSig.begin();
# 894 [ + + ]: 18 : while (pc < t.vin[0].scriptSig.end()) {
# 895 : 16 : opcodetype opcode;
# 896 : 16 : CScript::const_iterator prev_pc = pc;
# 897 : 16 : t.vin[0].scriptSig.GetOp(pc, opcode); // advance to next op
# 898 : : // for the sake of simplicity, we only replace single-byte push operations
# 899 [ + + ][ + + ]: 16 : if (opcode >= 1 && opcode <= OP_PUSHDATA4)
# 900 : 6 : continue;
# 901 : :
# 902 : 10 : int index = prev_pc - t.vin[0].scriptSig.begin();
# 903 : 10 : unsigned char orig_op = *prev_pc; // save op
# 904 : : // replace current push-op with each non-push-op
# 905 [ + + ]: 130 : for (auto op : non_push_ops) {
# 906 : 130 : t.vin[0].scriptSig[index] = op;
# 907 : 130 : CheckIsNotStandard(t, "scriptsig-not-pushonly");
# 908 : 130 : }
# 909 : 10 : t.vin[0].scriptSig[index] = orig_op; // restore op
# 910 : 10 : CheckIsStandard(t);
# 911 : 10 : }
# 912 : :
# 913 : : // Check tx-size (non-standard if transaction weight is > MAX_STANDARD_TX_WEIGHT)
# 914 : 2 : t.vin.clear();
# 915 : 2 : t.vin.resize(2438); // size per input (empty scriptSig): 41 bytes
# 916 : 2 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << std::vector<unsigned char>(19, 0); // output size: 30 bytes
# 917 : : // tx header: 12 bytes => 48 vbytes
# 918 : : // 2438 inputs: 2438*41 = 99958 bytes => 399832 vbytes
# 919 : : // 1 output: 30 bytes => 120 vbytes
# 920 : : // ===============================
# 921 : : // total: 400000 vbytes
# 922 : 2 : BOOST_CHECK_EQUAL(GetTransactionWeight(CTransaction(t)), 400000);
# 923 : 2 : CheckIsStandard(t);
# 924 : :
# 925 : : // increase output size by one byte, so we end up with 400004 vbytes
# 926 : 2 : t.vout[0].scriptPubKey = CScript() << OP_RETURN << std::vector<unsigned char>(20, 0); // output size: 31 bytes
# 927 : 2 : BOOST_CHECK_EQUAL(GetTransactionWeight(CTransaction(t)), 400004);
# 928 : 2 : CheckIsNotStandard(t, "tx-size");
# 929 : :
# 930 : : // Check bare multisig (standard if policy flag fIsBareMultisigStd is set)
# 931 : 2 : fIsBareMultisigStd = true;
# 932 : 2 : t.vout[0].scriptPubKey = GetScriptForMultisig(1, {key.GetPubKey()}); // simple 1-of-1
# 933 : 2 : t.vin.resize(1);
# 934 : 2 : t.vin[0].scriptSig = CScript() << std::vector<unsigned char>(65, 0);
# 935 : 2 : CheckIsStandard(t);
# 936 : :
# 937 : 2 : fIsBareMultisigStd = false;
# 938 : 2 : CheckIsNotStandard(t, "bare-multisig");
# 939 : 2 : fIsBareMultisigStd = DEFAULT_PERMIT_BAREMULTISIG;
# 940 : :
# 941 : : // Check P2WPKH outputs dust threshold
# 942 : 2 : t.vout[0].scriptPubKey = CScript() << OP_0 << ParseHex("ffffffffffffffffffffffffffffffffffffffff");
# 943 : 2 : t.vout[0].nValue = 294;
# 944 : 2 : CheckIsStandard(t);
# 945 : 2 : t.vout[0].nValue = 293;
# 946 : 2 : CheckIsNotStandard(t, "dust");
# 947 : :
# 948 : : // Check P2WSH outputs dust threshold
# 949 : 2 : t.vout[0].scriptPubKey = CScript() << OP_0 << ParseHex("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
# 950 : 2 : t.vout[0].nValue = 330;
# 951 : 2 : CheckIsStandard(t);
# 952 : 2 : t.vout[0].nValue = 329;
# 953 : 2 : CheckIsNotStandard(t, "dust");
# 954 : :
# 955 : : // Check future Witness Program versions dust threshold
# 956 [ + + ]: 32 : for (int op = OP_2; op <= OP_16; op += 1) {
# 957 : 30 : t.vout[0].scriptPubKey = CScript() << (opcodetype)op << ParseHex("ffff");
# 958 : 30 : t.vout[0].nValue = 240;
# 959 : 30 : CheckIsStandard(t);
# 960 : :
# 961 : 30 : t.vout[0].nValue = 239;
# 962 : 30 : CheckIsNotStandard(t, "dust");
# 963 : 30 : }
# 964 : 2 : }
# 965 : :
# 966 : : BOOST_AUTO_TEST_SUITE_END()
|