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). */
| 1850 | * Validity checks that depend on the UTXO set are also done; ConnectBlock() |
| 1851 | * can fail if those validity checks fail (among other reasons). */ |
| 1852 | bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex, |
| 1853 | CCoinsViewCache& view, const CChainParams& chainparams, bool fJustCheck) |
| 1854 | { |
| 1855 | AssertLockHeld(cs_main); |
| 1856 | assert(pindex); |
| 1857 | assert(*pindex->phashBlock == block.GetHash()); |
| 1858 | int64_t nTimeStart = GetTimeMicros(); |
| 1859 | |
| 1860 | // Check it again in case a previous version let a bad block in |
| 1861 | // NOTE: We don't currently (re-)invoke ContextualCheckBlock() or |
| 1862 | // ContextualCheckBlockHeader() here. This means that if we add a new |
| 1863 | // consensus rule that is enforced in one of those two functions, then we |
| 1864 | // may have let in a block that violates the rule prior to updating the |
| 1865 | // software, and we would NOT be enforcing the rule here. Fully solving |
| 1866 | // upgrade from one software version to the next after a consensus rule |
| 1867 | // change is potentially tricky and issue-specific (see RewindBlockIndex() |
| 1868 | // for one general approach that was used for BIP 141 deployment). |
| 1869 | // Also, currently the rule against blocks more than 2 hours in the future |
| 1870 | // is enforced in ContextualCheckBlockHeader(); we wouldn't want to |
| 1871 | // re-enforce that rule here (at least until we make it impossible for |
| 1872 | // GetAdjustedTime() to go backward). |
| 1873 | if (!CheckBlock(block, state, chainparams.GetConsensus(), !fJustCheck, !fJustCheck)) { |
| 1874 | if (state.CorruptionPossible()) { |
| 1875 | // We don't write down blocks to disk if they may have been |
| 1876 | // corrupted, so this should be impossible unless we're having hardware |
| 1877 | // problems. |
| 1878 | return AbortNode(state, "Corrupt block found indicating potential hardware failure; shutting down"); |
| 1879 | } |
| 1880 | return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state)); |
| 1881 | } |
| 1882 | |
| 1883 | // verify that the view's current state corresponds to the previous block |
| 1884 | uint256 hashPrevBlock = pindex->pprev == nullptr ? uint256() : pindex->pprev->GetBlockHash(); |
| 1885 | assert(hashPrevBlock == view.GetBestBlock()); |
| 1886 | |
| 1887 | // Special case for the genesis block, skipping connection of its transactions |
| 1888 | // (its coinbase is unspendable) |
| 1889 | if (block.GetHash() == chainparams.GetConsensus().hashGenesisBlock) { |
| 1890 | if (!fJustCheck) |
| 1891 | view.SetBestBlock(pindex->GetBlockHash()); |
| 1892 | return true; |
| 1893 | } |
| 1894 | |
| 1895 | nBlocksTotal++; |
| 1896 | |
| 1897 | bool fScriptChecks = true; |
| 1898 | if (!hashAssumeValid.IsNull()) { |
| 1899 | // We've been configured with the hash of a block which has been externally verified to have a valid history. |
| 1900 | // A suitable default value is included with the software and updated from time to time. Because validity |
| 1901 | // relative to a piece of software is an objective fact these defaults can be easily reviewed. |
| 1902 | // This setting doesn't force the selection of any particular chain but makes validating some faster by |
| 1903 | // effectively caching the result of part of the verification. |
| 1904 | BlockMap::const_iterator it = mapBlockIndex.find(hashAssumeValid); |
| 1905 | if (it != mapBlockIndex.end()) { |
| 1906 | if (it->second->GetAncestor(pindex->nHeight) == pindex && |
| 1907 | pindexBestHeader->GetAncestor(pindex->nHeight) == pindex && |
| 1908 | pindexBestHeader->nChainWork >= nMinimumChainWork) { |
| 1909 | // This block is a member of the assumed verified chain and an ancestor of the best header. |