LCOV - code coverage report
Current view: top level - src/test - txvalidationcache_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 99.5 % 212 211
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 15 15
Branches: 50.6 % 1126 570

             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 <consensus/validation.h>
       6                 :             : #include <key.h>
       7                 :             : #include <random.h>
       8                 :             : #include <script/sigcache.h>
       9                 :             : #include <script/sign.h>
      10                 :             : #include <script/signingprovider.h>
      11                 :             : #include <test/util/setup_common.h>
      12                 :             : #include <txmempool.h>
      13                 :             : #include <util/chaintype.h>
      14                 :             : #include <validation.h>
      15                 :             : 
      16                 :             : #include <boost/test/unit_test.hpp>
      17                 :             : 
      18                 :           2 : struct Dersig100Setup : public TestChain100Setup {
      19                 :           5 :     Dersig100Setup()
      20         [ +  - ]:          10 :         : TestChain100Setup{ChainType::REGTEST, {.extra_args = {"-testactivationheight=dersig@102"}}} {}
      21                 :             : };
      22                 :             : 
      23                 :             : BOOST_AUTO_TEST_SUITE(txvalidationcache_tests)
      24                 :             : 
      25                 :             : // Make sure skipping validation of transactions that were
      26                 :             : // validated going into the memory pool does not allow
      27                 :             : // double-spends in blocks to pass validation when they should not.
      28                 :             : struct DoubleSpendSetup : public Dersig100Setup {
      29                 :             :     const CScript scriptPubKey{CScript{} << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG};
      30                 :             : 
      31                 :             :     // Returns a signed spend of the first mature coinbase txn, paying the given value.
      32                 :           8 :     CMutableTransaction MakeSpend(CAmount value) const
      33                 :             :     {
      34                 :           8 :         CMutableTransaction spend;
      35                 :           8 :         spend.version = 1;
      36   [ +  -  -  +  :          16 :         spend.vin = {CTxIn{m_coinbase_txns[0]->GetHash(), 0}};
             +  +  -  - ]
      37   [ -  +  +  +  :          24 :         spend.vout = {CTxOut{value, scriptPubKey}};
                   -  - ]
      38                 :             : 
      39                 :             :         // Sign:
      40                 :           8 :         std::vector<unsigned char> vchSig;
      41         [ +  - ]:           8 :         uint256 hash = SignatureHash(scriptPubKey, spend, /*nIn=*/0, SIGHASH_ALL, /*amount=*/0, SigVersion::BASE);
      42   [ +  -  +  -  :          16 :         BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
             +  -  +  - ]
      43         [ +  - ]:           8 :         vchSig.push_back((unsigned char)SIGHASH_ALL);
      44         [ -  + ]:           8 :         spend.vin[0].scriptSig << vchSig;
      45                 :           8 :         return spend;
      46   [ +  -  +  - ]:          24 :     }
      47                 :             : 
      48                 :             :     // A double-spend of a mature coinbase txn.
      49                 :             :     const std::vector<CMutableTransaction> spends{MakeSpend(11 * CENT), MakeSpend(12 * CENT)};
      50                 :             : 
      51                 :           3 :     bool ToMemPool(const CMutableTransaction& tx)
      52                 :             :     {
      53                 :           3 :         LOCK(cs_main);
      54                 :             : 
      55   [ +  -  +  - ]:           6 :         const MempoolAcceptResult result = m_node.chainman->ProcessTransaction(MakeTransactionRef(tx));
      56                 :           6 :         return result.m_result_type == MempoolAcceptResult::ResultType::VALID;
      57         [ +  - ]:           6 :     }
      58                 :             : 
      59                 :           4 :     bool IsTip(const CBlock& block)
      60                 :             :     {
      61                 :           4 :         LOCK(cs_main);
      62   [ +  -  +  -  :          11 :         return m_node.chainman->ActiveChain().Tip()->GetBlockHash() == block.GetHash();
          -  +  +  +  +  
                      - ]
      63                 :           4 :     }
      64                 :             : };
      65                 :             : 
      66   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend_both_in_block, DoubleSpendSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      67                 :             : {
      68                 :             :     // A block with both of those transactions should be rejected.
      69                 :           1 :     const CBlock block{CreateAndProcessBlock(spends, scriptPubKey)};
      70   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsTip(block));
                   +  - ]
      71                 :           1 : }
      72                 :             : 
      73   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend_first_in_mempool, DoubleSpendSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      74                 :             : {
      75                 :             :     // ... and should be rejected if spend1 is in the memory pool
      76   [ +  -  +  - ]:           2 :     BOOST_CHECK(ToMemPool(spends[0]));
      77                 :           1 :     const CBlock block{CreateAndProcessBlock(spends, scriptPubKey)};
      78   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsTip(block));
             +  -  +  - ]
      79   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(m_node.mempool->size(), 1U);
                   +  - ]
      80                 :           1 : }
      81                 :             : 
      82   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend_second_in_mempool, DoubleSpendSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      83                 :             : {
      84                 :             :     // ... and should be rejected if spend2 is in the memory pool
      85   [ +  -  +  - ]:           2 :     BOOST_CHECK(ToMemPool(spends[1]));
      86                 :           1 :     const CBlock block{CreateAndProcessBlock(spends, scriptPubKey)};
      87   [ +  -  +  -  :           2 :     BOOST_CHECK(!IsTip(block));
             +  -  +  - ]
      88   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(m_node.mempool->size(), 1U);
                   +  - ]
      89                 :           1 : }
      90                 :             : 
      91   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(tx_mempool_block_doublespend_one_in_mempool_other_in_block, DoubleSpendSetup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      92                 :             : {
      93                 :             :     // The spends must be distinct transactions, so that the one in the
      94                 :             :     // memory pool is removed as a conflict rather than as mined:
      95   [ +  -  +  -  :           3 :     BOOST_REQUIRE(spends[0].GetHash() != spends[1].GetHash());
                   +  - ]
      96                 :             : 
      97                 :             :     // Final sanity test: second spend in the memory pool, first in block, that's OK:
      98   [ +  -  +  - ]:           2 :     BOOST_CHECK(ToMemPool(spends[1]));
      99   [ +  -  +  -  :           2 :     const CBlock block{CreateAndProcessBlock({spends[0]}, scriptPubKey)};
             +  +  -  - ]
     100   [ +  -  +  -  :           2 :     BOOST_CHECK(IsTip(block));
             +  -  +  - ]
     101                 :             :     // spends[1] should have been removed from the mempool when the
     102                 :             :     // block with spends[0] is accepted:
     103   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(m_node.mempool->size(), 0U);
                   +  - ]
     104                 :           2 : }
     105                 :             : 
     106                 :             : // Run CheckInputScripts (using CoinsTip()) on the given transaction, for all script
     107                 :             : // flags.  Test that CheckInputScripts passes for all flags that don't overlap with
     108                 :             : // the failing_flags argument, but otherwise fails.
     109                 :             : // CHECKLOCKTIMEVERIFY and CHECKSEQUENCEVERIFY (and future NOP codes that may
     110                 :             : // get reassigned) have an interaction with DISCOURAGE_UPGRADABLE_NOPS: if
     111                 :             : // the script flags used contain DISCOURAGE_UPGRADABLE_NOPS but don't contain
     112                 :             : // CHECKLOCKTIMEVERIFY (or CHECKSEQUENCEVERIFY), but the script does contain
     113                 :             : // OP_CHECKLOCKTIMEVERIFY (or OP_CHECKSEQUENCEVERIFY), then script execution
     114                 :             : // should fail.
     115                 :           9 : static void ValidateCheckInputsForAllFlags(const CTransaction &tx, script_verify_flags failing_flags, bool add_to_cache, CCoinsViewCache& active_coins_tip, ValidationCache& validation_cache) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
     116                 :             : {
     117                 :           9 :     PrecomputedTransactionData txdata;
     118                 :             : 
     119                 :           9 :     FastRandomContext insecure_rand(true);
     120                 :             : 
     121         [ +  + ]:       90009 :     for (int count = 0; count < 10000; ++count) {
     122                 :       90000 :         TxValidationState state;
     123                 :             : 
     124                 :             :         // Randomly selects flag combinations
     125         [ +  + ]:       90000 :         script_verify_flags test_flags = script_verify_flags::from_int(insecure_rand.randrange(MAX_SCRIPT_VERIFY_FLAGS));
     126                 :             : 
     127                 :             :         // Filter out incompatible flag choices
     128         [ +  + ]:       90000 :         if ((test_flags & SCRIPT_VERIFY_CLEANSTACK)) {
     129                 :             :             // CLEANSTACK requires P2SH and WITNESS, see VerifyScript() in
     130                 :             :             // script/interpreter.cpp
     131                 :       45387 :             test_flags |= SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS;
     132                 :             :         }
     133         [ +  + ]:       90000 :         if ((test_flags & SCRIPT_VERIFY_TAPROOT)) {
     134                 :             :             // TAPROOT requires WITNESS
     135                 :       45099 :             test_flags |= SCRIPT_VERIFY_WITNESS;
     136                 :             :         }
     137         [ +  + ]:       90000 :         if ((test_flags & SCRIPT_VERIFY_WITNESS)) {
     138                 :             :             // WITNESS requires P2SH
     139                 :       78759 :             test_flags |= SCRIPT_VERIFY_P2SH;
     140                 :             :         }
     141   [ +  -  +  - ]:       90000 :         bool ret = CheckInputScripts(tx, state, &active_coins_tip, test_flags, CheckInputScriptsFor::Mempool, true, add_to_cache, txdata, validation_cache, nullptr);
     142                 :             :         // CheckInputScripts should succeed iff test_flags doesn't intersect with
     143                 :             :         // failing_flags
     144         [ +  - ]:       90000 :         bool expected_return_value = !(test_flags & failing_flags);
     145   [ +  -  +  - ]:       90000 :         BOOST_CHECK_EQUAL(ret, expected_return_value);
     146                 :             : 
     147                 :             :         // Test the caching
     148   [ +  +  +  + ]:       90000 :         if (ret && add_to_cache) {
     149                 :             :             // Check that we get a cache hit if the tx was valid
     150                 :       46838 :             std::vector<CScriptCheck> scriptchecks;
     151   [ +  -  +  -  :       93676 :             BOOST_CHECK(CheckInputScripts(tx, state, &active_coins_tip, test_flags, CheckInputScriptsFor::Mempool, true, add_to_cache, txdata, validation_cache, &scriptchecks));
          +  -  +  -  +  
                      - ]
     152   [ +  -  +  - ]:       93676 :             BOOST_CHECK(scriptchecks.empty());
     153                 :       46838 :         } else {
     154                 :             :             // Check that we get script executions to check, if the transaction
     155                 :             :             // was invalid, or we didn't add to cache.
     156                 :       43162 :             std::vector<CScriptCheck> scriptchecks;
     157   [ +  -  +  -  :       86324 :             BOOST_CHECK(CheckInputScripts(tx, state, &active_coins_tip, test_flags, CheckInputScriptsFor::Mempool, true, add_to_cache, txdata, validation_cache, &scriptchecks));
          +  -  +  -  +  
                      - ]
     158   [ +  -  -  +  :       43162 :             BOOST_CHECK_EQUAL(scriptchecks.size(), tx.vin.size());
             -  +  +  - ]
     159                 :       43162 :         }
     160                 :       90000 :     }
     161                 :           9 : }
     162                 :             : 
     163   [ +  -  +  -  :           7 : BOOST_FIXTURE_TEST_CASE(checkinputs_test, Dersig100Setup)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     164                 :             : {
     165                 :             :     // Test that passing CheckInputScripts with one set of script flags doesn't imply
     166                 :             :     // that we would pass again with a different set of flags.
     167   [ +  -  +  -  :           2 :     CScript p2pk_scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
                   +  - ]
     168   [ +  -  +  - ]:           1 :     CScript p2sh_scriptPubKey = GetScriptForDestination(ScriptHash(p2pk_scriptPubKey));
     169   [ +  -  +  -  :           1 :     CScript p2pkh_scriptPubKey = GetScriptForDestination(PKHash(coinbaseKey.GetPubKey()));
                   +  - ]
     170   [ +  -  +  -  :           1 :     CScript p2wpkh_scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(coinbaseKey.GetPubKey()));
                   +  - ]
     171   [ +  -  +  - ]:           1 :     CScript p2tr_scriptPubKey = GetScriptForDestination(WitnessV1Taproot(XOnlyPubKey(coinbaseKey.GetPubKey())));
     172                 :             : 
     173                 :           1 :     FillableSigningProvider keystore;
     174   [ +  -  +  -  :           2 :     BOOST_CHECK(keystore.AddKey(coinbaseKey));
             +  -  +  - ]
     175   [ +  -  +  -  :           2 :     BOOST_CHECK(keystore.AddCScript(p2pk_scriptPubKey));
             +  -  +  - ]
     176                 :             : 
     177                 :             :     // flags to test: SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, SCRIPT_VERIFY_CHECKSEQUENCE_VERIFY, SCRIPT_VERIFY_NULLDUMMY, uncompressed pubkey thing
     178                 :             : 
     179                 :             :     // Create 2 outputs that match the three scripts above, spending the first
     180                 :             :     // coinbase tx.
     181         [ +  - ]:           1 :     CMutableTransaction spend_tx;
     182                 :             : 
     183                 :           1 :     spend_tx.version = 1;
     184   [ +  -  -  +  :           2 :     spend_tx.vin = {CTxIn{m_coinbase_txns[0]->GetHash(), 0}};
             +  +  -  - ]
     185                 :           1 :     spend_tx.vout = {
     186                 :           1 :         CTxOut{11*CENT, p2sh_scriptPubKey},
     187                 :           2 :         CTxOut{11*CENT, p2wpkh_scriptPubKey},
     188   [ +  -  +  -  :           3 :         CTxOut{11*CENT, CScript() << OP_CHECKLOCKTIMEVERIFY << OP_DROP << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG},
          +  -  +  -  +  
                      - ]
     189   [ +  -  +  -  :           3 :         CTxOut{11*CENT, CScript() << OP_CHECKSEQUENCEVERIFY << OP_DROP << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG},
          +  -  +  -  +  
                      - ]
     190                 :           2 :         CTxOut{11*CENT, p2tr_scriptPubKey},
     191   [ -  +  +  +  :           6 :     };
                   -  - ]
     192                 :             : 
     193                 :             :     // Sign, with a non-DER signature
     194                 :           1 :     {
     195                 :           1 :         std::vector<unsigned char> vchSig;
     196         [ +  - ]:           1 :         uint256 hash = SignatureHash(p2pk_scriptPubKey, spend_tx, 0, SIGHASH_ALL, 0, SigVersion::BASE);
     197   [ +  -  +  -  :           2 :         BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
             +  -  +  - ]
     198         [ +  - ]:           1 :         vchSig.push_back((unsigned char) 0); // padding byte makes this non-DER
     199         [ +  - ]:           1 :         vchSig.push_back((unsigned char)SIGHASH_ALL);
     200         [ -  + ]:           1 :         spend_tx.vin[0].scriptSig << vchSig;
     201                 :           0 :     }
     202                 :             : 
     203                 :             :     // Test that invalidity under a set of flags doesn't preclude validity
     204                 :             :     // under other (eg consensus) flags.
     205                 :             :     // spend_tx is invalid according to DERSIG
     206                 :           1 :     {
     207         [ +  - ]:           1 :         LOCK(cs_main);
     208                 :             : 
     209         [ +  - ]:           1 :         TxValidationState state;
     210                 :           1 :         PrecomputedTransactionData ptd_spend_tx;
     211                 :             : 
     212   [ +  -  +  -  :           3 :         BOOST_CHECK(!CheckInputScripts(CTransaction(spend_tx), state, &m_node.chainman->ActiveChainstate().CoinsTip(), SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, CheckInputScriptsFor::Mempool, true, true, ptd_spend_tx, m_node.chainman->m_validation_cache, nullptr));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     213                 :             : 
     214                 :             :         // If we call again asking for scriptchecks (as happens in
     215                 :             :         // ConnectBlock), we should add a script check object for this -- we're
     216                 :             :         // not caching invalidity (if that changes, delete this test case).
     217                 :           1 :         std::vector<CScriptCheck> scriptchecks;
     218   [ +  -  +  -  :           3 :         BOOST_CHECK(CheckInputScripts(CTransaction(spend_tx), state, &m_node.chainman->ActiveChainstate().CoinsTip(), SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_DERSIG, CheckInputScriptsFor::Mempool, true, true, ptd_spend_tx, m_node.chainman->m_validation_cache, &scriptchecks));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     219   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(scriptchecks.size(), 1U);
                   +  - ]
     220                 :             : 
     221                 :             :         // Test that CheckInputScripts returns true iff DERSIG-enforcing flags are
     222                 :             :         // not present.  Don't add these checks to the cache, so that we can
     223                 :             :         // test later that block validation works fine in the absence of cached
     224                 :             :         // successes.
     225   [ +  -  +  -  :           1 :         ValidateCheckInputsForAllFlags(CTransaction(spend_tx), SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_LOW_S | SCRIPT_VERIFY_STRICTENC, false, m_node.chainman->ActiveChainstate().CoinsTip(), m_node.chainman->m_validation_cache);
             +  -  +  - ]
     226         [ +  - ]:           2 :     }
     227                 :             : 
     228                 :             :     // And if we produce a block with this tx, it should be valid (DERSIG not
     229                 :             :     // enabled yet), even though there's no cache entry.
     230                 :           1 :     CBlock block;
     231                 :             : 
     232   [ +  -  +  -  :           2 :     block = CreateAndProcessBlock({spend_tx}, p2pk_scriptPubKey);
             +  +  -  - ]
     233         [ +  - ]:           1 :     LOCK(cs_main);
     234   [ +  -  +  -  :           3 :     BOOST_CHECK(m_node.chainman->ActiveChain().Tip()->GetBlockHash() == block.GetHash());
          +  -  -  +  -  
             +  +  -  +  
                      - ]
     235   [ +  -  +  -  :           3 :     BOOST_CHECK(m_node.chainman->ActiveChainstate().CoinsTip().GetBestBlock() == block.GetHash());
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     236                 :             : 
     237                 :             :     // Test P2SH: construct a transaction that is valid without P2SH, and
     238                 :             :     // then test validity with P2SH.
     239                 :           1 :     {
     240         [ +  - ]:           1 :         CMutableTransaction invalid_under_p2sh_tx;
     241                 :           1 :         invalid_under_p2sh_tx.version = 1;
     242   [ +  -  -  +  :           3 :         invalid_under_p2sh_tx.vin = {CTxIn{spend_tx.GetHash(), 0}};
             +  +  -  - ]
     243   [ -  +  +  +  :           3 :         invalid_under_p2sh_tx.vout = {CTxOut{11*CENT, p2pk_scriptPubKey}};
                   -  - ]
     244   [ -  +  +  - ]:           1 :         std::vector<unsigned char> vchSig2(p2pk_scriptPubKey.begin(), p2pk_scriptPubKey.end());
     245         [ -  + ]:           1 :         invalid_under_p2sh_tx.vin[0].scriptSig << vchSig2;
     246                 :             : 
     247   [ +  -  +  -  :           2 :         ValidateCheckInputsForAllFlags(CTransaction(invalid_under_p2sh_tx), SCRIPT_VERIFY_P2SH, true, m_node.chainman->ActiveChainstate().CoinsTip(), m_node.chainman->m_validation_cache);
             +  -  +  - ]
     248                 :           1 :     }
     249                 :             : 
     250                 :             :     // Test CHECKLOCKTIMEVERIFY
     251                 :           1 :     {
     252         [ +  - ]:           1 :         CMutableTransaction invalid_with_cltv_tx;
     253                 :           1 :         invalid_with_cltv_tx.version = 1;
     254                 :           1 :         invalid_with_cltv_tx.nLockTime = 100;
     255   [ +  -  -  +  :           3 :         invalid_with_cltv_tx.vin = {CTxIn{spend_tx.GetHash(), 2, {}, /*nSequenceIn=*/0}};
             +  +  -  - ]
     256   [ -  +  +  +  :           3 :         invalid_with_cltv_tx.vout = {CTxOut{11*CENT, p2pk_scriptPubKey}};
                   -  - ]
     257                 :             : 
     258                 :             :         // Sign
     259                 :           1 :         std::vector<unsigned char> vchSig;
     260         [ +  - ]:           1 :         uint256 hash = SignatureHash(spend_tx.vout[2].scriptPubKey, invalid_with_cltv_tx, 0, SIGHASH_ALL, 0, SigVersion::BASE);
     261   [ +  -  +  -  :           2 :         BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
             +  -  +  - ]
     262         [ +  - ]:           1 :         vchSig.push_back((unsigned char)SIGHASH_ALL);
     263   [ -  +  +  - ]:           1 :         invalid_with_cltv_tx.vin[0].scriptSig = CScript() << vchSig << 101;
     264                 :             : 
     265   [ +  -  +  -  :           1 :         ValidateCheckInputsForAllFlags(CTransaction(invalid_with_cltv_tx), SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, true, m_node.chainman->ActiveChainstate().CoinsTip(), m_node.chainman->m_validation_cache);
             +  -  +  - ]
     266                 :             : 
     267                 :             :         // Make it valid, and check again
     268   [ -  +  +  - ]:           1 :         invalid_with_cltv_tx.vin[0].scriptSig = CScript() << vchSig << 100;
     269         [ +  - ]:           1 :         TxValidationState state;
     270                 :           1 :         PrecomputedTransactionData txdata;
     271   [ +  -  +  -  :           3 :         BOOST_CHECK(CheckInputScripts(CTransaction(invalid_with_cltv_tx), state, m_node.chainman->ActiveChainstate().CoinsTip(), SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY, CheckInputScriptsFor::Mempool, true, true, txdata, m_node.chainman->m_validation_cache, nullptr));
          +  -  +  -  +  
                -  +  - ]
     272                 :           2 :     }
     273                 :             : 
     274                 :             :     // TEST CHECKSEQUENCEVERIFY
     275                 :           1 :     {
     276         [ +  - ]:           1 :         CMutableTransaction invalid_with_csv_tx;
     277                 :           1 :         invalid_with_csv_tx.version = 2;
     278   [ +  -  -  +  :           3 :         invalid_with_csv_tx.vin = {CTxIn{spend_tx.GetHash(), 3, {}, /*nSequenceIn=*/100}};
             +  +  -  - ]
     279   [ -  +  +  +  :           3 :         invalid_with_csv_tx.vout = {CTxOut{11*CENT, p2pk_scriptPubKey}};
                   -  - ]
     280                 :             : 
     281                 :             :         // Sign
     282                 :           1 :         std::vector<unsigned char> vchSig;
     283         [ +  - ]:           1 :         uint256 hash = SignatureHash(spend_tx.vout[3].scriptPubKey, invalid_with_csv_tx, 0, SIGHASH_ALL, 0, SigVersion::BASE);
     284   [ +  -  +  -  :           2 :         BOOST_CHECK(coinbaseKey.Sign(hash, vchSig));
             +  -  +  - ]
     285         [ +  - ]:           1 :         vchSig.push_back((unsigned char)SIGHASH_ALL);
     286   [ -  +  +  - ]:           1 :         invalid_with_csv_tx.vin[0].scriptSig = CScript() << vchSig << 101;
     287                 :             : 
     288   [ +  -  +  -  :           1 :         ValidateCheckInputsForAllFlags(CTransaction(invalid_with_csv_tx), SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, true, m_node.chainman->ActiveChainstate().CoinsTip(), m_node.chainman->m_validation_cache);
             +  -  +  - ]
     289                 :             : 
     290                 :             :         // Make it valid, and check again
     291   [ -  +  +  - ]:           1 :         invalid_with_csv_tx.vin[0].scriptSig = CScript() << vchSig << 100;
     292         [ +  - ]:           1 :         TxValidationState state;
     293                 :           1 :         PrecomputedTransactionData txdata;
     294   [ +  -  +  -  :           3 :         BOOST_CHECK(CheckInputScripts(CTransaction(invalid_with_csv_tx), state, &m_node.chainman->ActiveChainstate().CoinsTip(), SCRIPT_VERIFY_CHECKSEQUENCEVERIFY, CheckInputScriptsFor::Mempool, true, true, txdata, m_node.chainman->m_validation_cache, nullptr));
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     295                 :           2 :     }
     296                 :             : 
     297                 :             :     // TODO: add tests for remaining script flags
     298                 :             : 
     299                 :             :     // Test that passing CheckInputScripts with a valid witness doesn't imply success
     300                 :             :     // for the same tx with a different witness.
     301                 :           1 :     {
     302         [ +  - ]:           1 :         CMutableTransaction valid_with_witness_tx;
     303                 :           1 :         valid_with_witness_tx.version = 1;
     304   [ +  -  -  +  :           3 :         valid_with_witness_tx.vin = {CTxIn{spend_tx.GetHash(), 1}};
             +  +  -  - ]
     305   [ -  +  +  +  :           3 :         valid_with_witness_tx.vout = {CTxOut{11*CENT, p2pk_scriptPubKey}};
                   -  - ]
     306                 :             : 
     307                 :             :         // Sign
     308                 :           1 :         SignatureData sigdata;
     309   [ +  -  +  -  :           2 :         BOOST_CHECK(ProduceSignature(keystore, MutableTransactionSignatureCreator(valid_with_witness_tx, 0, 11 * CENT, {.sighash_type = SIGHASH_DEFAULT}), spend_tx.vout[1].scriptPubKey, sigdata));
          +  -  +  -  +  
                      - ]
     310         [ +  - ]:           1 :         UpdateInput(valid_with_witness_tx.vin[0], sigdata);
     311                 :             : 
     312                 :             :         // This should be valid under all script flags.
     313   [ +  -  +  -  :           1 :         ValidateCheckInputsForAllFlags(CTransaction(valid_with_witness_tx), 0, true, m_node.chainman->ActiveChainstate().CoinsTip(), m_node.chainman->m_validation_cache);
             +  -  +  - ]
     314                 :             : 
     315                 :             :         // Remove the witness, and check that it is now invalid.
     316   [ +  -  +  - ]:           2 :         valid_with_witness_tx.vin[0].scriptWitness = {};
     317   [ +  -  +  -  :           1 :         ValidateCheckInputsForAllFlags(CTransaction(valid_with_witness_tx), SCRIPT_VERIFY_WITNESS, true, m_node.chainman->ActiveChainstate().CoinsTip(), m_node.chainman->m_validation_cache);
             +  -  +  - ]
     318                 :           1 :     }
     319                 :             : 
     320                 :             :     // Test a Taproot (witness v1) key-path spend, to exercise the Schnorr branch of the signature cache.
     321                 :           1 :     {
     322         [ +  - ]:           1 :         CMutableTransaction tr_tx;
     323   [ +  -  -  +  :           3 :         tr_tx.vin = {CTxIn{spend_tx.GetHash(), 4}};
             +  +  -  - ]
     324   [ -  +  +  +  :           3 :         tr_tx.vout = {CTxOut{11*CENT, p2pk_scriptPubKey}};
                   -  - ]
     325                 :             : 
     326                 :             :         // Sign P2TR output for key-path spending (i.e. add Schnorr signature to witness stack)
     327                 :           1 :         FlatSigningProvider tr_keystore;
     328   [ +  -  +  -  :           1 :         tr_keystore.keys.emplace(coinbaseKey.GetPubKey().GetID(), coinbaseKey);
                   +  - ]
     329                 :           1 :         const std::map<COutPoint, Coin> coins{
     330                 :           1 :             {tr_tx.vin[0].prevout, Coin(spend_tx.vout[4], /*nHeightIn=*/0, /*fCoinBaseIn=*/false)}
     331   [ -  +  +  +  :           2 :         };
                   -  - ]
     332         [ +  - ]:           1 :         std::map<int, bilingual_str> input_errors;
     333   [ +  -  +  -  :           2 :         BOOST_REQUIRE(SignTransaction(tr_tx, &tr_keystore, coins, {.sighash_type = SIGHASH_DEFAULT}, input_errors));
             +  -  +  - ]
     334         [ +  - ]:           1 :         auto witness_stack = tr_tx.vin[0].scriptWitness.ToVectors();
     335   [ +  -  -  +  :           2 :         BOOST_REQUIRE(witness_stack.size() == 1 && witness_stack[0].size() == 64);
          +  -  -  +  -  
             +  +  -  +  
                      - ]
     336                 :             : 
     337                 :             :         // Invalidate signature; an invalid Taproot key-path spend is only invalid if SCRIPT_VERIFY_TAPROOT is set
     338         [ +  - ]:           1 :         witness_stack[0][63] ^= 0x01; // damage signature
     339   [ +  -  +  -  :           2 :         tr_tx.vin[0].scriptWitness = CScriptWitness{witness_stack};
                   -  + ]
     340   [ +  -  +  -  :           1 :         ValidateCheckInputsForAllFlags(CTransaction(tr_tx), SCRIPT_VERIFY_TAPROOT, true, m_node.chainman->ActiveChainstate().CoinsTip(), m_node.chainman->m_validation_cache);
             +  -  +  - ]
     341         [ +  - ]:           1 :         witness_stack[0][63] ^= 0x01; // repair signature
     342   [ +  -  +  -  :           2 :         tr_tx.vin[0].scriptWitness = CScriptWitness{witness_stack};
                   -  + ]
     343                 :             : 
     344                 :             :         // A valid Taproot key-path spend is valid under all flags
     345   [ +  -  +  -  :           1 :         ValidateCheckInputsForAllFlags(CTransaction(tr_tx), 0, true, m_node.chainman->ActiveChainstate().CoinsTip(), m_node.chainman->m_validation_cache);
             +  -  +  - ]
     346                 :           1 :     }
     347                 :             : 
     348                 :           1 :     {
     349                 :             :         // Test a transaction with multiple inputs.
     350         [ +  - ]:           1 :         CMutableTransaction tx;
     351                 :             : 
     352                 :           1 :         tx.version = 1;
     353                 :           1 :         tx.vin = {
     354         [ +  - ]:           1 :             CTxIn{spend_tx.GetHash(), 0},
     355         [ +  - ]:           2 :             CTxIn{spend_tx.GetHash(), 1},
     356   [ -  +  +  +  :           3 :         };
                   -  - ]
     357   [ -  +  +  +  :           3 :         tx.vout = {CTxOut{22*CENT, p2pk_scriptPubKey}};
                   -  - ]
     358                 :             : 
     359                 :             :         // Sign
     360         [ +  + ]:           3 :         for (int i = 0; i < 2; ++i) {
     361                 :           2 :             SignatureData sigdata;
     362   [ +  -  +  -  :           4 :             BOOST_CHECK(ProduceSignature(keystore, MutableTransactionSignatureCreator(tx, i, 11 * CENT, {.sighash_type = SIGHASH_DEFAULT}), spend_tx.vout[i].scriptPubKey, sigdata));
          +  -  +  -  +  
                      - ]
     363         [ +  - ]:           2 :             UpdateInput(tx.vin[i], sigdata);
     364                 :           2 :         }
     365                 :             : 
     366                 :             :         // This should be valid under all script flags
     367   [ +  -  +  -  :           1 :         ValidateCheckInputsForAllFlags(CTransaction(tx), 0, true, m_node.chainman->ActiveChainstate().CoinsTip(), m_node.chainman->m_validation_cache);
             +  -  +  - ]
     368                 :             : 
     369                 :             :         // Check that if the second input is invalid, but the first input is
     370                 :             :         // valid, the transaction is not cached.
     371                 :             :         // Invalidate vin[1]
     372   [ +  -  +  - ]:           2 :         tx.vin[1].scriptWitness = {};
     373                 :             : 
     374         [ +  - ]:           1 :         TxValidationState state;
     375                 :           1 :         PrecomputedTransactionData txdata;
     376                 :             :         // This transaction is now invalid under segwit, because of the second input.
     377   [ +  -  +  -  :           3 :         BOOST_CHECK(!CheckInputScripts(CTransaction(tx), state, &m_node.chainman->ActiveChainstate().CoinsTip(), SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, CheckInputScriptsFor::Mempool, true, true, txdata, m_node.chainman->m_validation_cache, nullptr));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     378                 :             : 
     379                 :           1 :         std::vector<CScriptCheck> scriptchecks;
     380                 :             :         // Make sure this transaction was not cached (ie because the first
     381                 :             :         // input was valid)
     382   [ +  -  +  -  :           3 :         BOOST_CHECK(CheckInputScripts(CTransaction(tx), state, &m_node.chainman->ActiveChainstate().CoinsTip(), SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS, CheckInputScriptsFor::Mempool, true, true, txdata, m_node.chainman->m_validation_cache, &scriptchecks));
          +  -  +  -  +  
          -  +  -  +  -  
                   +  - ]
     383                 :             :         // Should get 2 script checks back -- caching is on a whole-transaction basis.
     384   [ +  -  -  +  :           1 :         BOOST_CHECK_EQUAL(scriptchecks.size(), 2U);
                   +  - ]
     385         [ +  - ]:           3 :     }
     386   [ +  -  +  -  :          23 : }
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  +  -  +  
             -  -  -  -  
                      - ]
     387                 :             : 
     388                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1