* Check whether all inputs of this transaction are valid (no double spends, scripts & sigs, amounts) * This does not modify the UTXO set. * * 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 script execution cache hits) are, obviously, * not pushed onto pvChecks/run. * * Setting cacheSigSto
| 1418 | * Non-static (and re-declared) in src/test/txvalidationcache_tests.cpp |
| 1419 | */ |
| 1420 | bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks) |
| 1421 | { |
| 1422 | if (!tx.IsCoinBase()) |
| 1423 | { |
| 1424 | if (pvChecks) |
| 1425 | pvChecks->reserve(tx.vin.size()); |
| 1426 | |
| 1427 | // The first loop above does all the inexpensive checks. |
| 1428 | // Only if ALL inputs pass do we perform expensive ECDSA signature checks. |
| 1429 | // Helps prevent CPU exhaustion attacks. |
| 1430 | |
| 1431 | // Skip script verification when connecting blocks under the |
| 1432 | // assumevalid block. Assuming the assumevalid block is valid this |
| 1433 | // is safe because block merkle hashes are still computed and checked, |
| 1434 | // Of course, if an assumed valid block is invalid due to false scriptSigs |
| 1435 | // this optimization would allow an invalid chain to be accepted. |
| 1436 | if (fScriptChecks) { |
| 1437 | // First check if script executions have been cached with the same |
| 1438 | // flags. Note that this assumes that the inputs provided are |
| 1439 | // correct (ie that the transaction hash which is in tx's prevouts |
| 1440 | // properly commits to the scriptPubKey in the inputs view of that |
| 1441 | // transaction). |
| 1442 | uint256 hashCacheEntry; |
| 1443 | // We only use the first 19 bytes of nonce to avoid a second SHA |
| 1444 | // round - giving us 19 + 32 + 4 = 55 bytes (+ 8 + 1 = 64) |
| 1445 | static_assert(55 - sizeof(flags) - 32 >= 128/8, "Want at least 128 bits of nonce for script execution cache"); |
| 1446 | CSHA256().Write(scriptExecutionCacheNonce.begin(), 55 - sizeof(flags) - 32).Write(tx.GetWitnessHash().begin(), 32).Write((unsigned char*)&flags, sizeof(flags)).Finalize(hashCacheEntry.begin()); |
| 1447 | AssertLockHeld(cs_main); //TODO: Remove this requirement by making CuckooCache not require external locks |
| 1448 | if (scriptExecutionCache.contains(hashCacheEntry, !cacheFullScriptStore)) { |
| 1449 | return true; |
| 1450 | } |
| 1451 | |
| 1452 | for (unsigned int i = 0; i < tx.vin.size(); i++) { |
| 1453 | const COutPoint &prevout = tx.vin[i].prevout; |
| 1454 | const Coin& coin = inputs.AccessCoin(prevout); |
| 1455 | assert(!coin.IsSpent()); |
| 1456 | |
| 1457 | // We very carefully only pass in things to CScriptCheck which |
| 1458 | // are clearly committed to by tx' witness hash. This provides |
| 1459 | // a sanity check that our caching is not introducing consensus |
| 1460 | // failures through additional data in, eg, the coins being |
| 1461 | // spent being checked as a part of CScriptCheck. |
| 1462 | |
| 1463 | // Verify signature |
| 1464 | CScriptCheck check(coin.out, tx, i, flags, cacheSigStore, &txdata); |
| 1465 | if (pvChecks) { |
| 1466 | pvChecks->push_back(CScriptCheck()); |
| 1467 | check.swap(pvChecks->back()); |
| 1468 | } else if (!check()) { |
| 1469 | if (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) { |
| 1470 | // Check whether the failure was caused by a |
| 1471 | // non-mandatory script verification check, such as |
| 1472 | // non-standard DER encodings or non-null dummy |
| 1473 | // arguments; if so, don't trigger DoS protection to |
| 1474 | // avoid splitting the network between upgraded and |
| 1475 | // non-upgraded nodes. |
| 1476 | CScriptCheck check2(coin.out, tx, i, |
| 1477 | flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, cacheSigStore, &txdata); |