LCOV - code coverage report
Current view: top level - src/test - txvalidationcache_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 97.7 % 219 214
Test Date: 2026-09-23 06:58:52 Functions: 100.0 % 9 9
Branches: 50.7 % 964 489

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