LCOV - code coverage report
Current view: top level - src/test - transaction_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 96.8 % 942 912
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 51 51
Branches: 50.2 % 3890 1953

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

Generated by: LCOV version 2.0-1