Apply the effects of this block (with given index) on the UTXO set represented by coins. * Validity checks that depend on the UTXO set are also done; ConnectBlock() * can fail if those validity checks fail (among other reasons). */
| 2299 | * Validity checks that depend on the UTXO set are also done; ConnectBlock() |
| 2300 | * can fail if those validity checks fail (among other reasons). */ |
| 2301 | bool Chainstate::ConnectBlock(const CBlock& block, BlockValidationState& state, CBlockIndex* pindex, |
| 2302 | CCoinsViewCache& view, bool fJustCheck) |
| 2303 | { |
| 2304 | AssertLockHeld(cs_main); |
| 2305 | assert(pindex); |
| 2306 | |
| 2307 | uint256 block_hash{block.GetHash()}; |
| 2308 | assert(*pindex->phashBlock == block_hash); |
| 2309 | |
| 2310 | const auto time_start{SteadyClock::now()}; |
| 2311 | const CChainParams& params{m_chainman.GetParams()}; |
| 2312 | |
| 2313 | // Check it again in case a previous version let a bad block in |
| 2314 | // NOTE: We don't currently (re-)invoke ContextualCheckBlock() or |
| 2315 | // ContextualCheckBlockHeader() here. This means that if we add a new |
| 2316 | // consensus rule that is enforced in one of those two functions, then we |
| 2317 | // may have let in a block that violates the rule prior to updating the |
| 2318 | // software, and we would NOT be enforcing the rule here. Fully solving |
| 2319 | // upgrade from one software version to the next after a consensus rule |
| 2320 | // change is potentially tricky and issue-specific (see NeedsRedownload() |
| 2321 | // for one approach that was used for BIP 141 deployment). |
| 2322 | // Also, currently the rule against blocks more than 2 hours in the future |
| 2323 | // is enforced in ContextualCheckBlockHeader(); we wouldn't want to |
| 2324 | // re-enforce that rule here (at least until we make it impossible for |
| 2325 | // the clock to go backward). |
| 2326 | if (!CheckBlock(block, state, params.GetConsensus(), !fJustCheck, !fJustCheck)) { |
| 2327 | if (state.GetResult() == BlockValidationResult::BLOCK_MUTATED) { |
| 2328 | // We don't write down blocks to disk if they may have been |
| 2329 | // corrupted, so this should be impossible unless we're having hardware |
| 2330 | // problems. |
| 2331 | return FatalError(m_chainman.GetNotifications(), state, _("Corrupt block found indicating potential hardware failure.")); |
| 2332 | } |
| 2333 | LogError("%s: Consensus::CheckBlock: %s\n", __func__, state.ToString()); |
| 2334 | return false; |
| 2335 | } |
| 2336 | |
| 2337 | // verify that the view's current state corresponds to the previous block |
| 2338 | uint256 hashPrevBlock = pindex->pprev == nullptr ? uint256() : pindex->pprev->GetBlockHash(); |
| 2339 | assert(hashPrevBlock == view.GetBestBlock()); |
| 2340 | |
| 2341 | m_chainman.num_blocks_total++; |
| 2342 | |
| 2343 | // Special case for the genesis block, skipping connection of its transactions |
| 2344 | // (its coinbase is unspendable) |
| 2345 | if (block_hash == params.GetConsensus().hashGenesisBlock) { |
| 2346 | if (!fJustCheck) |
| 2347 | view.SetBestBlock(pindex->GetBlockHash()); |
| 2348 | return true; |
| 2349 | } |
| 2350 | |
| 2351 | const char* script_check_reason; |
| 2352 | if (m_chainman.AssumedValidBlock().IsNull()) { |
| 2353 | script_check_reason = "assumevalid=0 (always verify)"; |
| 2354 | } else { |
| 2355 | constexpr int64_t TWO_WEEKS_IN_SECONDS{60 * 60 * 24 * 7 * 2}; |
| 2356 | // We've been configured with the hash of a block which has been externally verified to have a valid history. |
| 2357 | // A suitable default value is included with the software and updated from time to time. Because validity |
| 2358 | // relative to a piece of software is an objective fact these defaults can be easily reviewed. |