MCPcopy Create free account
hub / github.com/ElementsProject/elements / ConnectBlock

Method ConnectBlock

src/validation.cpp:2090–2457  ·  view source on GitHub ↗

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). */

Source from the content-addressed store, hash-verified

2088 * Validity checks that depend on the UTXO set are also done; ConnectBlock()
2089 * can fail if those validity checks fail (among other reasons). */
2090bool CChainState::ConnectBlock(const CBlock& block, BlockValidationState& state, CBlockIndex* pindex,
2091 CCoinsViewCache& view, std::set<std::pair<uint256, COutPoint>>* setPeginsSpent, bool fJustCheck)
2092{
2093 AssertLockHeld(cs_main);
2094 assert(pindex);
2095
2096 uint256 block_hash{block.GetHash()};
2097 assert(*pindex->phashBlock == block_hash);
2098
2099 int64_t nTimeStart = GetTimeMicros();
2100
2101 // verify that the view's current state corresponds to the previous block
2102 uint256 hashPrevBlock = pindex->pprev == nullptr ? uint256() : pindex->pprev->GetBlockHash();
2103 assert(hashPrevBlock == view.GetBestBlock());
2104
2105 const Consensus::Params& consensusParams = m_params.GetConsensus();
2106 // Add genesis outputs but don't validate.
2107 if (block_hash == consensusParams.hashGenesisBlock) {
2108 if (!fJustCheck) {
2109 if (consensusParams.connect_genesis_outputs) {
2110 for (const auto& tx : block.vtx) {
2111 // Directly add new coins to DB
2112 AddCoins(view, *tx, 0);
2113 }
2114 }
2115 view.SetBestBlock(pindex->GetBlockHash());
2116 }
2117 nBlocksTotal++;
2118 return true;
2119 }
2120
2121 // Check it again in case a previous version let a bad block in
2122 // NOTE: We don't currently (re-)invoke ContextualCheckBlock() or
2123 // ContextualCheckBlockHeader() here. This means that if we add a new
2124 // consensus rule that is enforced in one of those two functions, then we
2125 // may have let in a block that violates the rule prior to updating the
2126 // software, and we would NOT be enforcing the rule here. Fully solving
2127 // upgrade from one software version to the next after a consensus rule
2128 // change is potentially tricky and issue-specific (see NeedsRedownload()
2129 // for one approach that was used for BIP 141 deployment).
2130 // Also, currently the rule against blocks more than 2 hours in the future
2131 // is enforced in ContextualCheckBlockHeader(); we wouldn't want to
2132 // re-enforce that rule here (at least until we make it impossible for
2133 // GetAdjustedTime() to go backward).
2134 if (!CheckBlock(block, state, m_params.GetConsensus(), !fJustCheck, !fJustCheck)) {
2135 if (state.GetResult() == BlockValidationResult::BLOCK_MUTATED) {
2136 // We don't write down blocks to disk if they may have been
2137 // corrupted, so this should be impossible unless we're having hardware
2138 // problems.
2139 return AbortNode(state, "Corrupt block found indicating potential hardware failure; shutting down");
2140 }
2141 return error("%s: Consensus::CheckBlock: %s", __func__, state.ToString());
2142 }
2143
2144 nBlocksTotal++;
2145
2146 // Check that all non-zero policyAsset coinbase outputs pay to the required destination
2147 const CScript& mandatory_coinbase_destination = m_params.GetConsensus().mandatory_coinbase_destination;

Callers 2

TestBlockValidityFunction · 0.80
VerifyDBMethod · 0.80

Calls 15

GetTimeMicrosFunction · 0.85
AddCoinsFunction · 0.85
CheckBlockFunction · 0.85
errorFunction · 0.85
uint256SFunction · 0.85
DeploymentActiveAtFunction · 0.85
GetBlockScriptFlagsFunction · 0.85
GetActivePAKListFunction · 0.85
IsPAKValidTxFunction · 0.85
GetValidFedpegScriptsFunction · 0.85
SequenceLocksFunction · 0.85

Tested by 1

TestBlockValidityFunction · 0.64