* Check whether all of this transaction's input scripts succeed. * * This involves ECDSA signature checks so can be computationally intensive. This function should * only be called after the cheap sanity checks in CheckTxInputs passed. * * If pvChecks is not nullptr, script checks are pushed onto it instead of being performed inline. Any * script checks which are not necessary (eg due to scr
| 2065 | * Non-static (and redeclared) in src/test/txvalidationcache_tests.cpp |
| 2066 | */ |
| 2067 | bool CheckInputScripts(const CTransaction& tx, TxValidationState& state, |
| 2068 | const CCoinsViewCache& inputs, script_verify_flags flags, bool cacheSigStore, |
| 2069 | bool cacheFullScriptStore, PrecomputedTransactionData& txdata, |
| 2070 | ValidationCache& validation_cache, |
| 2071 | std::vector<CScriptCheck>* pvChecks) |
| 2072 | { |
| 2073 | if (tx.IsCoinBase()) return true; |
| 2074 | |
| 2075 | if (pvChecks) { |
| 2076 | pvChecks->reserve(tx.vin.size()); |
| 2077 | } |
| 2078 | |
| 2079 | // First check if script executions have been cached with the same |
| 2080 | // flags. Note that this assumes that the inputs provided are |
| 2081 | // correct (ie that the transaction hash which is in tx's prevouts |
| 2082 | // properly commits to the scriptPubKey in the inputs view of that |
| 2083 | // transaction). |
| 2084 | uint256 hashCacheEntry; |
| 2085 | CSHA256 hasher = validation_cache.ScriptExecutionCacheHasher(); |
| 2086 | hasher.Write(UCharCast(tx.GetWitnessHash().begin()), 32).Write((unsigned char*)&flags, sizeof(flags)).Finalize(hashCacheEntry.begin()); |
| 2087 | AssertLockHeld(cs_main); //TODO: Remove this requirement by making CuckooCache not require external locks |
| 2088 | if (validation_cache.m_script_execution_cache.contains(hashCacheEntry, !cacheFullScriptStore)) { |
| 2089 | return true; |
| 2090 | } |
| 2091 | |
| 2092 | if (!txdata.m_spent_outputs_ready) { |
| 2093 | std::vector<CTxOut> spent_outputs; |
| 2094 | spent_outputs.reserve(tx.vin.size()); |
| 2095 | |
| 2096 | for (const auto& txin : tx.vin) { |
| 2097 | const COutPoint& prevout = txin.prevout; |
| 2098 | const Coin& coin = inputs.AccessCoin(prevout); |
| 2099 | assert(!coin.IsSpent()); |
| 2100 | spent_outputs.emplace_back(coin.out); |
| 2101 | } |
| 2102 | txdata.Init(tx, std::move(spent_outputs)); |
| 2103 | } |
| 2104 | assert(txdata.m_spent_outputs.size() == tx.vin.size()); |
| 2105 | |
| 2106 | for (unsigned int i = 0; i < tx.vin.size(); i++) { |
| 2107 | |
| 2108 | // We very carefully only pass in things to CScriptCheck which |
| 2109 | // are clearly committed to by tx' witness hash. This provides |
| 2110 | // a sanity check that our caching is not introducing consensus |
| 2111 | // failures through additional data in, eg, the coins being |
| 2112 | // spent being checked as a part of CScriptCheck. |
| 2113 | |
| 2114 | // Verify signature |
| 2115 | CScriptCheck check(txdata.m_spent_outputs[i], tx, validation_cache.m_signature_cache, i, flags, cacheSigStore, &txdata); |
| 2116 | if (pvChecks) { |
| 2117 | pvChecks->emplace_back(std::move(check)); |
| 2118 | } else if (auto result = check(); result.has_value()) { |
| 2119 | // Tx failures never trigger disconnections/bans. |
| 2120 | // This is so that network splits aren't triggered |
| 2121 | // either due to non-consensus relay policies (such as |
| 2122 | // non-standard DER encodings or non-null dummy |
| 2123 | // arguments) or due to new consensus rules introduced in |
| 2124 | // soft forks. |