Document assumptions that are being made to avoid NULL pointer dereferences
[bitcoinplatinum.git] / src / validation.cpp
blob939921d677e54575960361adfc0df3b9c8510bb9
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "validation.h"
8 #include "arith_uint256.h"
9 #include "chain.h"
10 #include "chainparams.h"
11 #include "checkpoints.h"
12 #include "checkqueue.h"
13 #include "consensus/consensus.h"
14 #include "consensus/merkle.h"
15 #include "consensus/tx_verify.h"
16 #include "consensus/validation.h"
17 #include "cuckoocache.h"
18 #include "fs.h"
19 #include "hash.h"
20 #include "init.h"
21 #include "policy/fees.h"
22 #include "policy/policy.h"
23 #include "policy/rbf.h"
24 #include "pow.h"
25 #include "primitives/block.h"
26 #include "primitives/transaction.h"
27 #include "random.h"
28 #include "reverse_iterator.h"
29 #include "script/script.h"
30 #include "script/sigcache.h"
31 #include "script/standard.h"
32 #include "timedata.h"
33 #include "tinyformat.h"
34 #include "txdb.h"
35 #include "txmempool.h"
36 #include "ui_interface.h"
37 #include "undo.h"
38 #include "util.h"
39 #include "utilmoneystr.h"
40 #include "utilstrencodings.h"
41 #include "validationinterface.h"
42 #include "versionbits.h"
43 #include "warnings.h"
45 #include <atomic>
46 #include <sstream>
48 #include <boost/algorithm/string/replace.hpp>
49 #include <boost/algorithm/string/join.hpp>
50 #include <boost/thread.hpp>
52 #if defined(NDEBUG)
53 # error "Bitcoin cannot be compiled without assertions."
54 #endif
56 #define MICRO 0.000001
57 #define MILLI 0.001
59 /**
60 * Global state
63 CCriticalSection cs_main;
65 BlockMap mapBlockIndex;
66 CChain chainActive;
67 CBlockIndex *pindexBestHeader = nullptr;
68 CWaitableCriticalSection csBestBlock;
69 CConditionVariable cvBlockChange;
70 int nScriptCheckThreads = 0;
71 std::atomic_bool fImporting(false);
72 bool fReindex = false;
73 bool fTxIndex = false;
74 bool fHavePruned = false;
75 bool fPruneMode = false;
76 bool fIsBareMultisigStd = DEFAULT_PERMIT_BAREMULTISIG;
77 bool fRequireStandard = true;
78 bool fCheckBlockIndex = false;
79 bool fCheckpointsEnabled = DEFAULT_CHECKPOINTS_ENABLED;
80 size_t nCoinCacheUsage = 5000 * 300;
81 uint64_t nPruneTarget = 0;
82 int64_t nMaxTipAge = DEFAULT_MAX_TIP_AGE;
83 bool fEnableReplacement = DEFAULT_ENABLE_REPLACEMENT;
85 uint256 hashAssumeValid;
87 CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
88 CAmount maxTxFee = DEFAULT_TRANSACTION_MAXFEE;
90 CBlockPolicyEstimator feeEstimator;
91 CTxMemPool mempool(&feeEstimator);
93 static void CheckBlockIndex(const Consensus::Params& consensusParams);
95 /** Constant stuff for coinbase transactions we create: */
96 CScript COINBASE_FLAGS;
98 const std::string strMessageMagic = "Bitcoin Signed Message:\n";
100 // Internal stuff
101 namespace {
103 struct CBlockIndexWorkComparator
105 bool operator()(const CBlockIndex *pa, const CBlockIndex *pb) const {
106 // First sort by most total work, ...
107 if (pa->nChainWork > pb->nChainWork) return false;
108 if (pa->nChainWork < pb->nChainWork) return true;
110 // ... then by earliest time received, ...
111 if (pa->nSequenceId < pb->nSequenceId) return false;
112 if (pa->nSequenceId > pb->nSequenceId) return true;
114 // Use pointer address as tie breaker (should only happen with blocks
115 // loaded from disk, as those all have id 0).
116 if (pa < pb) return false;
117 if (pa > pb) return true;
119 // Identical blocks.
120 return false;
124 CBlockIndex *pindexBestInvalid;
127 * The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS (for itself and all ancestors) and
128 * as good as our current tip or better. Entries may be failed, though, and pruning nodes may be
129 * missing the data for the block.
131 std::set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexCandidates;
132 /** All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
133 * Pruned nodes may have entries where B is missing data.
135 std::multimap<CBlockIndex*, CBlockIndex*> mapBlocksUnlinked;
137 CCriticalSection cs_LastBlockFile;
138 std::vector<CBlockFileInfo> vinfoBlockFile;
139 int nLastBlockFile = 0;
140 /** Global flag to indicate we should check to see if there are
141 * block/undo files that should be deleted. Set on startup
142 * or if we allocate more file space when we're in prune mode
144 bool fCheckForPruning = false;
147 * Every received block is assigned a unique and increasing identifier, so we
148 * know which one to give priority in case of a fork.
150 CCriticalSection cs_nBlockSequenceId;
151 /** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
152 int32_t nBlockSequenceId = 1;
153 /** Decreasing counter (used by subsequent preciousblock calls). */
154 int32_t nBlockReverseSequenceId = -1;
155 /** chainwork for the last block that preciousblock has been applied to. */
156 arith_uint256 nLastPreciousChainwork = 0;
158 /** Dirty block index entries. */
159 std::set<CBlockIndex*> setDirtyBlockIndex;
161 /** Dirty block file entries. */
162 std::set<int> setDirtyFileInfo;
163 } // anon namespace
165 CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator)
167 // Find the first block the caller has in the main chain
168 for (const uint256& hash : locator.vHave) {
169 BlockMap::iterator mi = mapBlockIndex.find(hash);
170 if (mi != mapBlockIndex.end())
172 CBlockIndex* pindex = (*mi).second;
173 if (chain.Contains(pindex))
174 return pindex;
175 if (pindex->GetAncestor(chain.Height()) == chain.Tip()) {
176 return chain.Tip();
180 return chain.Genesis();
183 CCoinsViewDB *pcoinsdbview = nullptr;
184 CCoinsViewCache *pcoinsTip = nullptr;
185 CBlockTreeDB *pblocktree = nullptr;
187 enum FlushStateMode {
188 FLUSH_STATE_NONE,
189 FLUSH_STATE_IF_NEEDED,
190 FLUSH_STATE_PERIODIC,
191 FLUSH_STATE_ALWAYS
194 // See definition for documentation
195 static bool FlushStateToDisk(const CChainParams& chainParams, CValidationState &state, FlushStateMode mode, int nManualPruneHeight=0);
196 static void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight);
197 static void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight);
198 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 = nullptr);
199 static FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false);
201 bool CheckFinalTx(const CTransaction &tx, int flags)
203 AssertLockHeld(cs_main);
205 // By convention a negative value for flags indicates that the
206 // current network-enforced consensus rules should be used. In
207 // a future soft-fork scenario that would mean checking which
208 // rules would be enforced for the next block and setting the
209 // appropriate flags. At the present time no soft-forks are
210 // scheduled, so no flags are set.
211 flags = std::max(flags, 0);
213 // CheckFinalTx() uses chainActive.Height()+1 to evaluate
214 // nLockTime because when IsFinalTx() is called within
215 // CBlock::AcceptBlock(), the height of the block *being*
216 // evaluated is what is used. Thus if we want to know if a
217 // transaction can be part of the *next* block, we need to call
218 // IsFinalTx() with one more than chainActive.Height().
219 const int nBlockHeight = chainActive.Height() + 1;
221 // BIP113 will require that time-locked transactions have nLockTime set to
222 // less than the median time of the previous block they're contained in.
223 // When the next block is created its previous block will be the current
224 // chain tip, so we use that to calculate the median time passed to
225 // IsFinalTx() if LOCKTIME_MEDIAN_TIME_PAST is set.
226 const int64_t nBlockTime = (flags & LOCKTIME_MEDIAN_TIME_PAST)
227 ? chainActive.Tip()->GetMedianTimePast()
228 : GetAdjustedTime();
230 return IsFinalTx(tx, nBlockHeight, nBlockTime);
233 bool TestLockPointValidity(const LockPoints* lp)
235 AssertLockHeld(cs_main);
236 assert(lp);
237 // If there are relative lock times then the maxInputBlock will be set
238 // If there are no relative lock times, the LockPoints don't depend on the chain
239 if (lp->maxInputBlock) {
240 // Check whether chainActive is an extension of the block at which the LockPoints
241 // calculation was valid. If not LockPoints are no longer valid
242 if (!chainActive.Contains(lp->maxInputBlock)) {
243 return false;
247 // LockPoints still valid
248 return true;
251 bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool useExistingLockPoints)
253 AssertLockHeld(cs_main);
254 AssertLockHeld(mempool.cs);
256 CBlockIndex* tip = chainActive.Tip();
257 CBlockIndex index;
258 index.pprev = tip;
259 // CheckSequenceLocks() uses chainActive.Height()+1 to evaluate
260 // height based locks because when SequenceLocks() is called within
261 // ConnectBlock(), the height of the block *being*
262 // evaluated is what is used.
263 // Thus if we want to know if a transaction can be part of the
264 // *next* block, we need to use one more than chainActive.Height()
265 index.nHeight = tip->nHeight + 1;
267 std::pair<int, int64_t> lockPair;
268 if (useExistingLockPoints) {
269 assert(lp);
270 lockPair.first = lp->height;
271 lockPair.second = lp->time;
273 else {
274 // pcoinsTip contains the UTXO set for chainActive.Tip()
275 CCoinsViewMemPool viewMemPool(pcoinsTip, mempool);
276 std::vector<int> prevheights;
277 prevheights.resize(tx.vin.size());
278 for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
279 const CTxIn& txin = tx.vin[txinIndex];
280 Coin coin;
281 if (!viewMemPool.GetCoin(txin.prevout, coin)) {
282 return error("%s: Missing input", __func__);
284 if (coin.nHeight == MEMPOOL_HEIGHT) {
285 // Assume all mempool transaction confirm in the next block
286 prevheights[txinIndex] = tip->nHeight + 1;
287 } else {
288 prevheights[txinIndex] = coin.nHeight;
291 lockPair = CalculateSequenceLocks(tx, flags, &prevheights, index);
292 if (lp) {
293 lp->height = lockPair.first;
294 lp->time = lockPair.second;
295 // Also store the hash of the block with the highest height of
296 // all the blocks which have sequence locked prevouts.
297 // This hash needs to still be on the chain
298 // for these LockPoint calculations to be valid
299 // Note: It is impossible to correctly calculate a maxInputBlock
300 // if any of the sequence locked inputs depend on unconfirmed txs,
301 // except in the special case where the relative lock time/height
302 // is 0, which is equivalent to no sequence lock. Since we assume
303 // input height of tip+1 for mempool txs and test the resulting
304 // lockPair from CalculateSequenceLocks against tip+1. We know
305 // EvaluateSequenceLocks will fail if there was a non-zero sequence
306 // lock on a mempool input, so we can use the return value of
307 // CheckSequenceLocks to indicate the LockPoints validity
308 int maxInputHeight = 0;
309 for (int height : prevheights) {
310 // Can ignore mempool inputs since we'll fail if they had non-zero locks
311 if (height != tip->nHeight+1) {
312 maxInputHeight = std::max(maxInputHeight, height);
315 lp->maxInputBlock = tip->GetAncestor(maxInputHeight);
318 return EvaluateSequenceLocks(index, lockPair);
321 // Returns the script flags which should be checked for a given block
322 static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consensus::Params& chainparams);
324 static void LimitMempoolSize(CTxMemPool& pool, size_t limit, unsigned long age) {
325 int expired = pool.Expire(GetTime() - age);
326 if (expired != 0) {
327 LogPrint(BCLog::MEMPOOL, "Expired %i transactions from the memory pool\n", expired);
330 std::vector<COutPoint> vNoSpendsRemaining;
331 pool.TrimToSize(limit, &vNoSpendsRemaining);
332 for (const COutPoint& removed : vNoSpendsRemaining)
333 pcoinsTip->Uncache(removed);
336 /** Convert CValidationState to a human-readable message for logging */
337 std::string FormatStateMessage(const CValidationState &state)
339 return strprintf("%s%s (code %i)",
340 state.GetRejectReason(),
341 state.GetDebugMessage().empty() ? "" : ", "+state.GetDebugMessage(),
342 state.GetRejectCode());
345 static bool IsCurrentForFeeEstimation()
347 AssertLockHeld(cs_main);
348 if (IsInitialBlockDownload())
349 return false;
350 if (chainActive.Tip()->GetBlockTime() < (GetTime() - MAX_FEE_ESTIMATION_TIP_AGE))
351 return false;
352 if (chainActive.Height() < pindexBestHeader->nHeight - 1)
353 return false;
354 return true;
357 /* Make mempool consistent after a reorg, by re-adding or recursively erasing
358 * disconnected block transactions from the mempool, and also removing any
359 * other transactions from the mempool that are no longer valid given the new
360 * tip/height.
362 * Note: we assume that disconnectpool only contains transactions that are NOT
363 * confirmed in the current chain nor already in the mempool (otherwise,
364 * in-mempool descendants of such transactions would be removed).
366 * Passing fAddToMempool=false will skip trying to add the transactions back,
367 * and instead just erase from the mempool as needed.
370 void UpdateMempoolForReorg(DisconnectedBlockTransactions &disconnectpool, bool fAddToMempool)
372 AssertLockHeld(cs_main);
373 std::vector<uint256> vHashUpdate;
374 // disconnectpool's insertion_order index sorts the entries from
375 // oldest to newest, but the oldest entry will be the last tx from the
376 // latest mined block that was disconnected.
377 // Iterate disconnectpool in reverse, so that we add transactions
378 // back to the mempool starting with the earliest transaction that had
379 // been previously seen in a block.
380 auto it = disconnectpool.queuedTx.get<insertion_order>().rbegin();
381 while (it != disconnectpool.queuedTx.get<insertion_order>().rend()) {
382 // ignore validation errors in resurrected transactions
383 CValidationState stateDummy;
384 if (!fAddToMempool || (*it)->IsCoinBase() || !AcceptToMemoryPool(mempool, stateDummy, *it, false, nullptr, nullptr, true)) {
385 // If the transaction doesn't make it in to the mempool, remove any
386 // transactions that depend on it (which would now be orphans).
387 mempool.removeRecursive(**it, MemPoolRemovalReason::REORG);
388 } else if (mempool.exists((*it)->GetHash())) {
389 vHashUpdate.push_back((*it)->GetHash());
391 ++it;
393 disconnectpool.queuedTx.clear();
394 // AcceptToMemoryPool/addUnchecked all assume that new mempool entries have
395 // no in-mempool children, which is generally not true when adding
396 // previously-confirmed transactions back to the mempool.
397 // UpdateTransactionsFromBlock finds descendants of any transactions in
398 // the disconnectpool that were added back and cleans up the mempool state.
399 mempool.UpdateTransactionsFromBlock(vHashUpdate);
401 // We also need to remove any now-immature transactions
402 mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
403 // Re-limit mempool size, in case we added any transactions
404 LimitMempoolSize(mempool, gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
407 // Used to avoid mempool polluting consensus critical paths if CCoinsViewMempool
408 // were somehow broken and returning the wrong scriptPubKeys
409 static bool CheckInputsFromMempoolAndCache(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &view, CTxMemPool& pool,
410 unsigned int flags, bool cacheSigStore, PrecomputedTransactionData& txdata) {
411 AssertLockHeld(cs_main);
413 // pool.cs should be locked already, but go ahead and re-take the lock here
414 // to enforce that mempool doesn't change between when we check the view
415 // and when we actually call through to CheckInputs
416 LOCK(pool.cs);
418 assert(!tx.IsCoinBase());
419 for (const CTxIn& txin : tx.vin) {
420 const Coin& coin = view.AccessCoin(txin.prevout);
422 // At this point we haven't actually checked if the coins are all
423 // available (or shouldn't assume we have, since CheckInputs does).
424 // So we just return failure if the inputs are not available here,
425 // and then only have to check equivalence for available inputs.
426 if (coin.IsSpent()) return false;
428 const CTransactionRef& txFrom = pool.get(txin.prevout.hash);
429 if (txFrom) {
430 assert(txFrom->GetHash() == txin.prevout.hash);
431 assert(txFrom->vout.size() > txin.prevout.n);
432 assert(txFrom->vout[txin.prevout.n] == coin.out);
433 } else {
434 const Coin& coinFromDisk = pcoinsTip->AccessCoin(txin.prevout);
435 assert(!coinFromDisk.IsSpent());
436 assert(coinFromDisk.out == coin.out);
440 return CheckInputs(tx, state, view, true, flags, cacheSigStore, true, txdata);
443 static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool& pool, CValidationState& state, const CTransactionRef& ptx, bool fLimitFree,
444 bool* pfMissingInputs, int64_t nAcceptTime, std::list<CTransactionRef>* plTxnReplaced,
445 bool fOverrideMempoolLimit, const CAmount& nAbsurdFee, std::vector<COutPoint>& coins_to_uncache)
447 const CTransaction& tx = *ptx;
448 const uint256 hash = tx.GetHash();
449 AssertLockHeld(cs_main);
450 if (pfMissingInputs)
451 *pfMissingInputs = false;
453 if (!CheckTransaction(tx, state))
454 return false; // state filled in by CheckTransaction
456 // Coinbase is only valid in a block, not as a loose transaction
457 if (tx.IsCoinBase())
458 return state.DoS(100, false, REJECT_INVALID, "coinbase");
460 // Reject transactions with witness before segregated witness activates (override with -prematurewitness)
461 bool witnessEnabled = IsWitnessEnabled(chainActive.Tip(), chainparams.GetConsensus());
462 if (!gArgs.GetBoolArg("-prematurewitness", false) && tx.HasWitness() && !witnessEnabled) {
463 return state.DoS(0, false, REJECT_NONSTANDARD, "no-witness-yet", true);
466 // Rather not work on nonstandard transactions (unless -testnet/-regtest)
467 std::string reason;
468 if (fRequireStandard && !IsStandardTx(tx, reason, witnessEnabled))
469 return state.DoS(0, false, REJECT_NONSTANDARD, reason);
471 // Only accept nLockTime-using transactions that can be mined in the next
472 // block; we don't want our mempool filled up with transactions that can't
473 // be mined yet.
474 if (!CheckFinalTx(tx, STANDARD_LOCKTIME_VERIFY_FLAGS))
475 return state.DoS(0, false, REJECT_NONSTANDARD, "non-final");
477 // is it already in the memory pool?
478 if (pool.exists(hash)) {
479 return state.Invalid(false, REJECT_DUPLICATE, "txn-already-in-mempool");
482 // Check for conflicts with in-memory transactions
483 std::set<uint256> setConflicts;
485 LOCK(pool.cs); // protect pool.mapNextTx
486 for (const CTxIn &txin : tx.vin)
488 auto itConflicting = pool.mapNextTx.find(txin.prevout);
489 if (itConflicting != pool.mapNextTx.end())
491 const CTransaction *ptxConflicting = itConflicting->second;
492 if (!setConflicts.count(ptxConflicting->GetHash()))
494 // Allow opt-out of transaction replacement by setting
495 // nSequence > MAX_BIP125_RBF_SEQUENCE (SEQUENCE_FINAL-2) on all inputs.
497 // SEQUENCE_FINAL-1 is picked to still allow use of nLockTime by
498 // non-replaceable transactions. All inputs rather than just one
499 // is for the sake of multi-party protocols, where we don't
500 // want a single party to be able to disable replacement.
502 // The opt-out ignores descendants as anyone relying on
503 // first-seen mempool behavior should be checking all
504 // unconfirmed ancestors anyway; doing otherwise is hopelessly
505 // insecure.
506 bool fReplacementOptOut = true;
507 if (fEnableReplacement)
509 for (const CTxIn &_txin : ptxConflicting->vin)
511 if (_txin.nSequence <= MAX_BIP125_RBF_SEQUENCE)
513 fReplacementOptOut = false;
514 break;
518 if (fReplacementOptOut) {
519 return state.Invalid(false, REJECT_DUPLICATE, "txn-mempool-conflict");
522 setConflicts.insert(ptxConflicting->GetHash());
529 CCoinsView dummy;
530 CCoinsViewCache view(&dummy);
532 CAmount nValueIn = 0;
533 LockPoints lp;
535 LOCK(pool.cs);
536 CCoinsViewMemPool viewMemPool(pcoinsTip, pool);
537 view.SetBackend(viewMemPool);
539 // do all inputs exist?
540 for (const CTxIn txin : tx.vin) {
541 if (!pcoinsTip->HaveCoinInCache(txin.prevout)) {
542 coins_to_uncache.push_back(txin.prevout);
544 if (!view.HaveCoin(txin.prevout)) {
545 // Are inputs missing because we already have the tx?
546 for (size_t out = 0; out < tx.vout.size(); out++) {
547 // Optimistically just do efficient check of cache for outputs
548 if (pcoinsTip->HaveCoinInCache(COutPoint(hash, out))) {
549 return state.Invalid(false, REJECT_DUPLICATE, "txn-already-known");
552 // Otherwise assume this might be an orphan tx for which we just haven't seen parents yet
553 if (pfMissingInputs) {
554 *pfMissingInputs = true;
556 return false; // fMissingInputs and !state.IsInvalid() is used to detect this condition, don't set state.Invalid()
560 // Bring the best block into scope
561 view.GetBestBlock();
563 nValueIn = view.GetValueIn(tx);
565 // we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool
566 view.SetBackend(dummy);
568 // Only accept BIP68 sequence locked transactions that can be mined in the next
569 // block; we don't want our mempool filled up with transactions that can't
570 // be mined yet.
571 // Must keep pool.cs for this unless we change CheckSequenceLocks to take a
572 // CoinsViewCache instead of create its own
573 if (!CheckSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, &lp))
574 return state.DoS(0, false, REJECT_NONSTANDARD, "non-BIP68-final");
577 // Check for non-standard pay-to-script-hash in inputs
578 if (fRequireStandard && !AreInputsStandard(tx, view))
579 return state.Invalid(false, REJECT_NONSTANDARD, "bad-txns-nonstandard-inputs");
581 // Check for non-standard witness in P2WSH
582 if (tx.HasWitness() && fRequireStandard && !IsWitnessStandard(tx, view))
583 return state.DoS(0, false, REJECT_NONSTANDARD, "bad-witness-nonstandard", true);
585 int64_t nSigOpsCost = GetTransactionSigOpCost(tx, view, STANDARD_SCRIPT_VERIFY_FLAGS);
587 CAmount nValueOut = tx.GetValueOut();
588 CAmount nFees = nValueIn-nValueOut;
589 // nModifiedFees includes any fee deltas from PrioritiseTransaction
590 CAmount nModifiedFees = nFees;
591 pool.ApplyDelta(hash, nModifiedFees);
593 // Keep track of transactions that spend a coinbase, which we re-scan
594 // during reorgs to ensure COINBASE_MATURITY is still met.
595 bool fSpendsCoinbase = false;
596 for (const CTxIn &txin : tx.vin) {
597 const Coin &coin = view.AccessCoin(txin.prevout);
598 if (coin.IsCoinBase()) {
599 fSpendsCoinbase = true;
600 break;
604 CTxMemPoolEntry entry(ptx, nFees, nAcceptTime, chainActive.Height(),
605 fSpendsCoinbase, nSigOpsCost, lp);
606 unsigned int nSize = entry.GetTxSize();
608 // Check that the transaction doesn't have an excessive number of
609 // sigops, making it impossible to mine. Since the coinbase transaction
610 // itself can contain sigops MAX_STANDARD_TX_SIGOPS is less than
611 // MAX_BLOCK_SIGOPS; we still consider this an invalid rather than
612 // merely non-standard transaction.
613 if (nSigOpsCost > MAX_STANDARD_TX_SIGOPS_COST)
614 return state.DoS(0, false, REJECT_NONSTANDARD, "bad-txns-too-many-sigops", false,
615 strprintf("%d", nSigOpsCost));
617 CAmount mempoolRejectFee = pool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFee(nSize);
618 if (mempoolRejectFee > 0 && nModifiedFees < mempoolRejectFee) {
619 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "mempool min fee not met", false, strprintf("%d < %d", nFees, mempoolRejectFee));
622 // No transactions are allowed below minRelayTxFee except from disconnected blocks
623 if (fLimitFree && nModifiedFees < ::minRelayTxFee.GetFee(nSize)) {
624 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "min relay fee not met");
627 if (nAbsurdFee && nFees > nAbsurdFee)
628 return state.Invalid(false,
629 REJECT_HIGHFEE, "absurdly-high-fee",
630 strprintf("%d > %d", nFees, nAbsurdFee));
632 // Calculate in-mempool ancestors, up to a limit.
633 CTxMemPool::setEntries setAncestors;
634 size_t nLimitAncestors = gArgs.GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT);
635 size_t nLimitAncestorSize = gArgs.GetArg("-limitancestorsize", DEFAULT_ANCESTOR_SIZE_LIMIT)*1000;
636 size_t nLimitDescendants = gArgs.GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT);
637 size_t nLimitDescendantSize = gArgs.GetArg("-limitdescendantsize", DEFAULT_DESCENDANT_SIZE_LIMIT)*1000;
638 std::string errString;
639 if (!pool.CalculateMemPoolAncestors(entry, setAncestors, nLimitAncestors, nLimitAncestorSize, nLimitDescendants, nLimitDescendantSize, errString)) {
640 return state.DoS(0, false, REJECT_NONSTANDARD, "too-long-mempool-chain", false, errString);
643 // A transaction that spends outputs that would be replaced by it is invalid. Now
644 // that we have the set of all ancestors we can detect this
645 // pathological case by making sure setConflicts and setAncestors don't
646 // intersect.
647 for (CTxMemPool::txiter ancestorIt : setAncestors)
649 const uint256 &hashAncestor = ancestorIt->GetTx().GetHash();
650 if (setConflicts.count(hashAncestor))
652 return state.DoS(10, false,
653 REJECT_INVALID, "bad-txns-spends-conflicting-tx", false,
654 strprintf("%s spends conflicting transaction %s",
655 hash.ToString(),
656 hashAncestor.ToString()));
660 // Check if it's economically rational to mine this transaction rather
661 // than the ones it replaces.
662 CAmount nConflictingFees = 0;
663 size_t nConflictingSize = 0;
664 uint64_t nConflictingCount = 0;
665 CTxMemPool::setEntries allConflicting;
667 // If we don't hold the lock allConflicting might be incomplete; the
668 // subsequent RemoveStaged() and addUnchecked() calls don't guarantee
669 // mempool consistency for us.
670 LOCK(pool.cs);
671 const bool fReplacementTransaction = setConflicts.size();
672 if (fReplacementTransaction)
674 CFeeRate newFeeRate(nModifiedFees, nSize);
675 std::set<uint256> setConflictsParents;
676 const int maxDescendantsToVisit = 100;
677 CTxMemPool::setEntries setIterConflicting;
678 for (const uint256 &hashConflicting : setConflicts)
680 CTxMemPool::txiter mi = pool.mapTx.find(hashConflicting);
681 if (mi == pool.mapTx.end())
682 continue;
684 // Save these to avoid repeated lookups
685 setIterConflicting.insert(mi);
687 // Don't allow the replacement to reduce the feerate of the
688 // mempool.
690 // We usually don't want to accept replacements with lower
691 // feerates than what they replaced as that would lower the
692 // feerate of the next block. Requiring that the feerate always
693 // be increased is also an easy-to-reason about way to prevent
694 // DoS attacks via replacements.
696 // The mining code doesn't (currently) take children into
697 // account (CPFP) so we only consider the feerates of
698 // transactions being directly replaced, not their indirect
699 // descendants. While that does mean high feerate children are
700 // ignored when deciding whether or not to replace, we do
701 // require the replacement to pay more overall fees too,
702 // mitigating most cases.
703 CFeeRate oldFeeRate(mi->GetModifiedFee(), mi->GetTxSize());
704 if (newFeeRate <= oldFeeRate)
706 return state.DoS(0, false,
707 REJECT_INSUFFICIENTFEE, "insufficient fee", false,
708 strprintf("rejecting replacement %s; new feerate %s <= old feerate %s",
709 hash.ToString(),
710 newFeeRate.ToString(),
711 oldFeeRate.ToString()));
714 for (const CTxIn &txin : mi->GetTx().vin)
716 setConflictsParents.insert(txin.prevout.hash);
719 nConflictingCount += mi->GetCountWithDescendants();
721 // This potentially overestimates the number of actual descendants
722 // but we just want to be conservative to avoid doing too much
723 // work.
724 if (nConflictingCount <= maxDescendantsToVisit) {
725 // If not too many to replace, then calculate the set of
726 // transactions that would have to be evicted
727 for (CTxMemPool::txiter it : setIterConflicting) {
728 pool.CalculateDescendants(it, allConflicting);
730 for (CTxMemPool::txiter it : allConflicting) {
731 nConflictingFees += it->GetModifiedFee();
732 nConflictingSize += it->GetTxSize();
734 } else {
735 return state.DoS(0, false,
736 REJECT_NONSTANDARD, "too many potential replacements", false,
737 strprintf("rejecting replacement %s; too many potential replacements (%d > %d)\n",
738 hash.ToString(),
739 nConflictingCount,
740 maxDescendantsToVisit));
743 for (unsigned int j = 0; j < tx.vin.size(); j++)
745 // We don't want to accept replacements that require low
746 // feerate junk to be mined first. Ideally we'd keep track of
747 // the ancestor feerates and make the decision based on that,
748 // but for now requiring all new inputs to be confirmed works.
749 if (!setConflictsParents.count(tx.vin[j].prevout.hash))
751 // Rather than check the UTXO set - potentially expensive -
752 // it's cheaper to just check if the new input refers to a
753 // tx that's in the mempool.
754 if (pool.mapTx.find(tx.vin[j].prevout.hash) != pool.mapTx.end())
755 return state.DoS(0, false,
756 REJECT_NONSTANDARD, "replacement-adds-unconfirmed", false,
757 strprintf("replacement %s adds unconfirmed input, idx %d",
758 hash.ToString(), j));
762 // The replacement must pay greater fees than the transactions it
763 // replaces - if we did the bandwidth used by those conflicting
764 // transactions would not be paid for.
765 if (nModifiedFees < nConflictingFees)
767 return state.DoS(0, false,
768 REJECT_INSUFFICIENTFEE, "insufficient fee", false,
769 strprintf("rejecting replacement %s, less fees than conflicting txs; %s < %s",
770 hash.ToString(), FormatMoney(nModifiedFees), FormatMoney(nConflictingFees)));
773 // Finally in addition to paying more fees than the conflicts the
774 // new transaction must pay for its own bandwidth.
775 CAmount nDeltaFees = nModifiedFees - nConflictingFees;
776 if (nDeltaFees < ::incrementalRelayFee.GetFee(nSize))
778 return state.DoS(0, false,
779 REJECT_INSUFFICIENTFEE, "insufficient fee", false,
780 strprintf("rejecting replacement %s, not enough additional fees to relay; %s < %s",
781 hash.ToString(),
782 FormatMoney(nDeltaFees),
783 FormatMoney(::incrementalRelayFee.GetFee(nSize))));
787 unsigned int scriptVerifyFlags = STANDARD_SCRIPT_VERIFY_FLAGS;
788 if (!chainparams.RequireStandard()) {
789 scriptVerifyFlags = gArgs.GetArg("-promiscuousmempoolflags", scriptVerifyFlags);
792 // Check against previous transactions
793 // This is done last to help prevent CPU exhaustion denial-of-service attacks.
794 PrecomputedTransactionData txdata(tx);
795 if (!CheckInputs(tx, state, view, true, scriptVerifyFlags, true, false, txdata)) {
796 // SCRIPT_VERIFY_CLEANSTACK requires SCRIPT_VERIFY_WITNESS, so we
797 // need to turn both off, and compare against just turning off CLEANSTACK
798 // to see if the failure is specifically due to witness validation.
799 CValidationState stateDummy; // Want reported failures to be from first CheckInputs
800 if (!tx.HasWitness() && CheckInputs(tx, stateDummy, view, true, scriptVerifyFlags & ~(SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_CLEANSTACK), true, false, txdata) &&
801 !CheckInputs(tx, stateDummy, view, true, scriptVerifyFlags & ~SCRIPT_VERIFY_CLEANSTACK, true, false, txdata)) {
802 // Only the witness is missing, so the transaction itself may be fine.
803 state.SetCorruptionPossible();
805 return false; // state filled in by CheckInputs
808 // Check again against the current block tip's script verification
809 // flags to cache our script execution flags. This is, of course,
810 // useless if the next block has different script flags from the
811 // previous one, but because the cache tracks script flags for us it
812 // will auto-invalidate and we'll just have a few blocks of extra
813 // misses on soft-fork activation.
815 // This is also useful in case of bugs in the standard flags that cause
816 // transactions to pass as valid when they're actually invalid. For
817 // instance the STRICTENC flag was incorrectly allowing certain
818 // CHECKSIG NOT scripts to pass, even though they were invalid.
820 // There is a similar check in CreateNewBlock() to prevent creating
821 // invalid blocks (using TestBlockValidity), however allowing such
822 // transactions into the mempool can be exploited as a DoS attack.
823 unsigned int currentBlockScriptVerifyFlags = GetBlockScriptFlags(chainActive.Tip(), Params().GetConsensus());
824 if (!CheckInputsFromMempoolAndCache(tx, state, view, pool, currentBlockScriptVerifyFlags, true, txdata))
826 // If we're using promiscuousmempoolflags, we may hit this normally
827 // Check if current block has some flags that scriptVerifyFlags
828 // does not before printing an ominous warning
829 if (!(~scriptVerifyFlags & currentBlockScriptVerifyFlags)) {
830 return error("%s: BUG! PLEASE REPORT THIS! ConnectInputs failed against latest-block but not STANDARD flags %s, %s",
831 __func__, hash.ToString(), FormatStateMessage(state));
832 } else {
833 if (!CheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true, false, txdata)) {
834 return error("%s: ConnectInputs failed against MANDATORY but not STANDARD flags due to promiscuous mempool %s, %s",
835 __func__, hash.ToString(), FormatStateMessage(state));
836 } else {
837 LogPrintf("Warning: -promiscuousmempool flags set to not include currently enforced soft forks, this may break mining or otherwise cause instability!\n");
842 // Remove conflicting transactions from the mempool
843 for (const CTxMemPool::txiter it : allConflicting)
845 LogPrint(BCLog::MEMPOOL, "replacing tx %s with %s for %s BTC additional fees, %d delta bytes\n",
846 it->GetTx().GetHash().ToString(),
847 hash.ToString(),
848 FormatMoney(nModifiedFees - nConflictingFees),
849 (int)nSize - (int)nConflictingSize);
850 if (plTxnReplaced)
851 plTxnReplaced->push_back(it->GetSharedTx());
853 pool.RemoveStaged(allConflicting, false, MemPoolRemovalReason::REPLACED);
855 // This transaction should only count for fee estimation if it isn't a
856 // BIP 125 replacement transaction (may not be widely supported), the
857 // node is not behind, and the transaction is not dependent on any other
858 // transactions in the mempool.
859 bool validForFeeEstimation = !fReplacementTransaction && IsCurrentForFeeEstimation() && pool.HasNoInputsOf(tx);
861 // Store transaction in memory
862 pool.addUnchecked(hash, entry, setAncestors, validForFeeEstimation);
864 // trim mempool and check if tx was trimmed
865 if (!fOverrideMempoolLimit) {
866 LimitMempoolSize(pool, gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
867 if (!pool.exists(hash))
868 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "mempool full");
872 GetMainSignals().TransactionAddedToMempool(ptx);
874 return true;
877 /** (try to) add transaction to memory pool with a specified acceptance time **/
878 static bool AcceptToMemoryPoolWithTime(const CChainParams& chainparams, CTxMemPool& pool, CValidationState &state, const CTransactionRef &tx, bool fLimitFree,
879 bool* pfMissingInputs, int64_t nAcceptTime, std::list<CTransactionRef>* plTxnReplaced,
880 bool fOverrideMempoolLimit, const CAmount nAbsurdFee)
882 std::vector<COutPoint> coins_to_uncache;
883 bool res = AcceptToMemoryPoolWorker(chainparams, pool, state, tx, fLimitFree, pfMissingInputs, nAcceptTime, plTxnReplaced, fOverrideMempoolLimit, nAbsurdFee, coins_to_uncache);
884 if (!res) {
885 for (const COutPoint& hashTx : coins_to_uncache)
886 pcoinsTip->Uncache(hashTx);
888 // After we've (potentially) uncached entries, ensure our coins cache is still within its size limits
889 CValidationState stateDummy;
890 FlushStateToDisk(chainparams, stateDummy, FLUSH_STATE_PERIODIC);
891 return res;
894 bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransactionRef &tx, bool fLimitFree,
895 bool* pfMissingInputs, std::list<CTransactionRef>* plTxnReplaced,
896 bool fOverrideMempoolLimit, const CAmount nAbsurdFee)
898 const CChainParams& chainparams = Params();
899 return AcceptToMemoryPoolWithTime(chainparams, pool, state, tx, fLimitFree, pfMissingInputs, GetTime(), plTxnReplaced, fOverrideMempoolLimit, nAbsurdFee);
902 /** Return transaction in txOut, and if it was found inside a block, its hash is placed in hashBlock */
903 bool GetTransaction(const uint256 &hash, CTransactionRef &txOut, const Consensus::Params& consensusParams, uint256 &hashBlock, bool fAllowSlow)
905 CBlockIndex *pindexSlow = nullptr;
907 LOCK(cs_main);
909 CTransactionRef ptx = mempool.get(hash);
910 if (ptx)
912 txOut = ptx;
913 return true;
916 if (fTxIndex) {
917 CDiskTxPos postx;
918 if (pblocktree->ReadTxIndex(hash, postx)) {
919 CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
920 if (file.IsNull())
921 return error("%s: OpenBlockFile failed", __func__);
922 CBlockHeader header;
923 try {
924 file >> header;
925 fseek(file.Get(), postx.nTxOffset, SEEK_CUR);
926 file >> txOut;
927 } catch (const std::exception& e) {
928 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
930 hashBlock = header.GetHash();
931 if (txOut->GetHash() != hash)
932 return error("%s: txid mismatch", __func__);
933 return true;
937 if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
938 const Coin& coin = AccessByTxid(*pcoinsTip, hash);
939 if (!coin.IsSpent()) pindexSlow = chainActive[coin.nHeight];
942 if (pindexSlow) {
943 CBlock block;
944 if (ReadBlockFromDisk(block, pindexSlow, consensusParams)) {
945 for (const auto& tx : block.vtx) {
946 if (tx->GetHash() == hash) {
947 txOut = tx;
948 hashBlock = pindexSlow->GetBlockHash();
949 return true;
955 return false;
963 //////////////////////////////////////////////////////////////////////////////
965 // CBlock and CBlockIndex
968 static bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart)
970 // Open history file to append
971 CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
972 if (fileout.IsNull())
973 return error("WriteBlockToDisk: OpenBlockFile failed");
975 // Write index header
976 unsigned int nSize = GetSerializeSize(fileout, block);
977 fileout << FLATDATA(messageStart) << nSize;
979 // Write block
980 long fileOutPos = ftell(fileout.Get());
981 if (fileOutPos < 0)
982 return error("WriteBlockToDisk: ftell failed");
983 pos.nPos = (unsigned int)fileOutPos;
984 fileout << block;
986 return true;
989 bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams)
991 block.SetNull();
993 // Open history file to read
994 CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
995 if (filein.IsNull())
996 return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
998 // Read block
999 try {
1000 filein >> block;
1002 catch (const std::exception& e) {
1003 return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
1006 // Check the header
1007 if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
1008 return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
1010 return true;
1013 bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
1015 if (!ReadBlockFromDisk(block, pindex->GetBlockPos(), consensusParams))
1016 return false;
1017 if (block.GetHash() != pindex->GetBlockHash())
1018 return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
1019 pindex->ToString(), pindex->GetBlockPos().ToString());
1020 return true;
1023 CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
1025 int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
1026 // Force block reward to zero when right shift is undefined.
1027 if (halvings >= 64)
1028 return 0;
1030 CAmount nSubsidy = 50 * COIN;
1031 // Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
1032 nSubsidy >>= halvings;
1033 return nSubsidy;
1036 bool IsInitialBlockDownload()
1038 const CChainParams& chainParams = Params();
1040 // Once this function has returned false, it must remain false.
1041 static std::atomic<bool> latchToFalse{false};
1042 // Optimization: pre-test latch before taking the lock.
1043 if (latchToFalse.load(std::memory_order_relaxed))
1044 return false;
1046 LOCK(cs_main);
1047 if (latchToFalse.load(std::memory_order_relaxed))
1048 return false;
1049 if (fImporting || fReindex)
1050 return true;
1051 if (chainActive.Tip() == nullptr)
1052 return true;
1053 if (chainActive.Tip()->nChainWork < UintToArith256(chainParams.GetConsensus().nMinimumChainWork))
1054 return true;
1055 if (chainActive.Tip()->GetBlockTime() < (GetTime() - nMaxTipAge))
1056 return true;
1057 LogPrintf("Leaving InitialBlockDownload (latching to false)\n");
1058 latchToFalse.store(true, std::memory_order_relaxed);
1059 return false;
1062 CBlockIndex *pindexBestForkTip = nullptr, *pindexBestForkBase = nullptr;
1064 static void AlertNotify(const std::string& strMessage)
1066 uiInterface.NotifyAlertChanged();
1067 std::string strCmd = gArgs.GetArg("-alertnotify", "");
1068 if (strCmd.empty()) return;
1070 // Alert text should be plain ascii coming from a trusted source, but to
1071 // be safe we first strip anything not in safeChars, then add single quotes around
1072 // the whole string before passing it to the shell:
1073 std::string singleQuote("'");
1074 std::string safeStatus = SanitizeString(strMessage);
1075 safeStatus = singleQuote+safeStatus+singleQuote;
1076 boost::replace_all(strCmd, "%s", safeStatus);
1078 boost::thread t(runCommand, strCmd); // thread runs free
1081 static void CheckForkWarningConditions()
1083 AssertLockHeld(cs_main);
1084 // Before we get past initial download, we cannot reliably alert about forks
1085 // (we assume we don't get stuck on a fork before finishing our initial sync)
1086 if (IsInitialBlockDownload())
1087 return;
1089 // If our best fork is no longer within 72 blocks (+/- 12 hours if no one mines it)
1090 // of our head, drop it
1091 if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
1092 pindexBestForkTip = nullptr;
1094 if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6)))
1096 if (!GetfLargeWorkForkFound() && pindexBestForkBase)
1098 std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
1099 pindexBestForkBase->phashBlock->ToString() + std::string("'");
1100 AlertNotify(warning);
1102 if (pindexBestForkTip && pindexBestForkBase)
1104 LogPrintf("%s: Warning: Large valid fork found\n forking the chain at height %d (%s)\n lasting to height %d (%s).\nChain state database corruption likely.\n", __func__,
1105 pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
1106 pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
1107 SetfLargeWorkForkFound(true);
1109 else
1111 LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
1112 SetfLargeWorkInvalidChainFound(true);
1115 else
1117 SetfLargeWorkForkFound(false);
1118 SetfLargeWorkInvalidChainFound(false);
1122 static void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
1124 AssertLockHeld(cs_main);
1125 // If we are on a fork that is sufficiently large, set a warning flag
1126 CBlockIndex* pfork = pindexNewForkTip;
1127 CBlockIndex* plonger = chainActive.Tip();
1128 while (pfork && pfork != plonger)
1130 while (plonger && plonger->nHeight > pfork->nHeight)
1131 plonger = plonger->pprev;
1132 if (pfork == plonger)
1133 break;
1134 pfork = pfork->pprev;
1137 // We define a condition where we should warn the user about as a fork of at least 7 blocks
1138 // with a tip within 72 blocks (+/- 12 hours if no one mines it) of ours
1139 // We use 7 blocks rather arbitrarily as it represents just under 10% of sustained network
1140 // hash rate operating on the fork.
1141 // or a chain that is entirely longer than ours and invalid (note that this should be detected by both)
1142 // We define it this way because it allows us to only store the highest fork tip (+ base) which meets
1143 // the 7-block condition and from this always have the most-likely-to-cause-warning fork
1144 if (pfork && (!pindexBestForkTip || pindexNewForkTip->nHeight > pindexBestForkTip->nHeight) &&
1145 pindexNewForkTip->nChainWork - pfork->nChainWork > (GetBlockProof(*pfork) * 7) &&
1146 chainActive.Height() - pindexNewForkTip->nHeight < 72)
1148 pindexBestForkTip = pindexNewForkTip;
1149 pindexBestForkBase = pfork;
1152 CheckForkWarningConditions();
1155 void static InvalidChainFound(CBlockIndex* pindexNew)
1157 if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
1158 pindexBestInvalid = pindexNew;
1160 LogPrintf("%s: invalid block=%s height=%d log2_work=%.8g date=%s\n", __func__,
1161 pindexNew->GetBlockHash().ToString(), pindexNew->nHeight,
1162 log(pindexNew->nChainWork.getdouble())/log(2.0), DateTimeStrFormat("%Y-%m-%d %H:%M:%S",
1163 pindexNew->GetBlockTime()));
1164 CBlockIndex *tip = chainActive.Tip();
1165 assert (tip);
1166 LogPrintf("%s: current best=%s height=%d log2_work=%.8g date=%s\n", __func__,
1167 tip->GetBlockHash().ToString(), chainActive.Height(), log(tip->nChainWork.getdouble())/log(2.0),
1168 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", tip->GetBlockTime()));
1169 CheckForkWarningConditions();
1172 void static InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state) {
1173 if (!state.CorruptionPossible()) {
1174 pindex->nStatus |= BLOCK_FAILED_VALID;
1175 setDirtyBlockIndex.insert(pindex);
1176 setBlockIndexCandidates.erase(pindex);
1177 InvalidChainFound(pindex);
1181 void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight)
1183 // mark inputs spent
1184 if (!tx.IsCoinBase()) {
1185 txundo.vprevout.reserve(tx.vin.size());
1186 for (const CTxIn &txin : tx.vin) {
1187 txundo.vprevout.emplace_back();
1188 bool is_spent = inputs.SpendCoin(txin.prevout, &txundo.vprevout.back());
1189 assert(is_spent);
1192 // add outputs
1193 AddCoins(inputs, tx, nHeight);
1196 void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, int nHeight)
1198 CTxUndo txundo;
1199 UpdateCoins(tx, inputs, txundo, nHeight);
1202 bool CScriptCheck::operator()() {
1203 const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
1204 const CScriptWitness *witness = &ptxTo->vin[nIn].scriptWitness;
1205 return VerifyScript(scriptSig, scriptPubKey, witness, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, amount, cacheStore, *txdata), &error);
1208 int GetSpendHeight(const CCoinsViewCache& inputs)
1210 LOCK(cs_main);
1211 CBlockIndex* pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
1212 return pindexPrev->nHeight + 1;
1216 static CuckooCache::cache<uint256, SignatureCacheHasher> scriptExecutionCache;
1217 static uint256 scriptExecutionCacheNonce(GetRandHash());
1219 void InitScriptExecutionCache() {
1220 // nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
1221 // setup_bytes creates the minimum possible cache (2 elements).
1222 size_t nMaxCacheSize = std::min(std::max((int64_t)0, gArgs.GetArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) / 2), MAX_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
1223 size_t nElems = scriptExecutionCache.setup_bytes(nMaxCacheSize);
1224 LogPrintf("Using %zu MiB out of %zu/2 requested for script execution cache, able to store %zu elements\n",
1225 (nElems*sizeof(uint256)) >>20, (nMaxCacheSize*2)>>20, nElems);
1229 * Check whether all inputs of this transaction are valid (no double spends, scripts & sigs, amounts)
1230 * This does not modify the UTXO set.
1232 * If pvChecks is not nullptr, script checks are pushed onto it instead of being performed inline. Any
1233 * script checks which are not necessary (eg due to script execution cache hits) are, obviously,
1234 * not pushed onto pvChecks/run.
1236 * Setting cacheSigStore/cacheFullScriptStore to false will remove elements from the corresponding cache
1237 * which are matched. This is useful for checking blocks where we will likely never need the cache
1238 * entry again.
1240 * Non-static (and re-declared) in src/test/txvalidationcache_tests.cpp
1242 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)
1244 if (!tx.IsCoinBase())
1246 if (!Consensus::CheckTxInputs(tx, state, inputs, GetSpendHeight(inputs)))
1247 return false;
1249 if (pvChecks)
1250 pvChecks->reserve(tx.vin.size());
1252 // The first loop above does all the inexpensive checks.
1253 // Only if ALL inputs pass do we perform expensive ECDSA signature checks.
1254 // Helps prevent CPU exhaustion attacks.
1256 // Skip script verification when connecting blocks under the
1257 // assumevalid block. Assuming the assumevalid block is valid this
1258 // is safe because block merkle hashes are still computed and checked,
1259 // Of course, if an assumed valid block is invalid due to false scriptSigs
1260 // this optimization would allow an invalid chain to be accepted.
1261 if (fScriptChecks) {
1262 // First check if script executions have been cached with the same
1263 // flags. Note that this assumes that the inputs provided are
1264 // correct (ie that the transaction hash which is in tx's prevouts
1265 // properly commits to the scriptPubKey in the inputs view of that
1266 // transaction).
1267 uint256 hashCacheEntry;
1268 // We only use the first 19 bytes of nonce to avoid a second SHA
1269 // round - giving us 19 + 32 + 4 = 55 bytes (+ 8 + 1 = 64)
1270 static_assert(55 - sizeof(flags) - 32 >= 128/8, "Want at least 128 bits of nonce for script execution cache");
1271 CSHA256().Write(scriptExecutionCacheNonce.begin(), 55 - sizeof(flags) - 32).Write(tx.GetWitnessHash().begin(), 32).Write((unsigned char*)&flags, sizeof(flags)).Finalize(hashCacheEntry.begin());
1272 AssertLockHeld(cs_main); //TODO: Remove this requirement by making CuckooCache not require external locks
1273 if (scriptExecutionCache.contains(hashCacheEntry, !cacheFullScriptStore)) {
1274 return true;
1277 for (unsigned int i = 0; i < tx.vin.size(); i++) {
1278 const COutPoint &prevout = tx.vin[i].prevout;
1279 const Coin& coin = inputs.AccessCoin(prevout);
1280 assert(!coin.IsSpent());
1282 // We very carefully only pass in things to CScriptCheck which
1283 // are clearly committed to by tx' witness hash. This provides
1284 // a sanity check that our caching is not introducing consensus
1285 // failures through additional data in, eg, the coins being
1286 // spent being checked as a part of CScriptCheck.
1287 const CScript& scriptPubKey = coin.out.scriptPubKey;
1288 const CAmount amount = coin.out.nValue;
1290 // Verify signature
1291 CScriptCheck check(scriptPubKey, amount, tx, i, flags, cacheSigStore, &txdata);
1292 if (pvChecks) {
1293 pvChecks->push_back(CScriptCheck());
1294 check.swap(pvChecks->back());
1295 } else if (!check()) {
1296 if (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) {
1297 // Check whether the failure was caused by a
1298 // non-mandatory script verification check, such as
1299 // non-standard DER encodings or non-null dummy
1300 // arguments; if so, don't trigger DoS protection to
1301 // avoid splitting the network between upgraded and
1302 // non-upgraded nodes.
1303 CScriptCheck check2(scriptPubKey, amount, tx, i,
1304 flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, cacheSigStore, &txdata);
1305 if (check2())
1306 return state.Invalid(false, REJECT_NONSTANDARD, strprintf("non-mandatory-script-verify-flag (%s)", ScriptErrorString(check.GetScriptError())));
1308 // Failures of other flags indicate a transaction that is
1309 // invalid in new blocks, e.g. an invalid P2SH. We DoS ban
1310 // such nodes as they are not following the protocol. That
1311 // said during an upgrade careful thought should be taken
1312 // as to the correct behavior - we may want to continue
1313 // peering with non-upgraded nodes even after soft-fork
1314 // super-majority signaling has occurred.
1315 return state.DoS(100,false, REJECT_INVALID, strprintf("mandatory-script-verify-flag-failed (%s)", ScriptErrorString(check.GetScriptError())));
1319 if (cacheFullScriptStore && !pvChecks) {
1320 // We executed all of the provided scripts, and were told to
1321 // cache the result. Do so now.
1322 scriptExecutionCache.insert(hashCacheEntry);
1327 return true;
1330 namespace {
1332 bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
1334 // Open history file to append
1335 CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
1336 if (fileout.IsNull())
1337 return error("%s: OpenUndoFile failed", __func__);
1339 // Write index header
1340 unsigned int nSize = GetSerializeSize(fileout, blockundo);
1341 fileout << FLATDATA(messageStart) << nSize;
1343 // Write undo data
1344 long fileOutPos = ftell(fileout.Get());
1345 if (fileOutPos < 0)
1346 return error("%s: ftell failed", __func__);
1347 pos.nPos = (unsigned int)fileOutPos;
1348 fileout << blockundo;
1350 // calculate & write checksum
1351 CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
1352 hasher << hashBlock;
1353 hasher << blockundo;
1354 fileout << hasher.GetHash();
1356 return true;
1359 bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock)
1361 // Open history file to read
1362 CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
1363 if (filein.IsNull())
1364 return error("%s: OpenUndoFile failed", __func__);
1366 // Read block
1367 uint256 hashChecksum;
1368 CHashVerifier<CAutoFile> verifier(&filein); // We need a CHashVerifier as reserializing may lose data
1369 try {
1370 verifier << hashBlock;
1371 verifier >> blockundo;
1372 filein >> hashChecksum;
1374 catch (const std::exception& e) {
1375 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
1378 // Verify checksum
1379 if (hashChecksum != verifier.GetHash())
1380 return error("%s: Checksum mismatch", __func__);
1382 return true;
1385 /** Abort with a message */
1386 bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
1388 SetMiscWarning(strMessage);
1389 LogPrintf("*** %s\n", strMessage);
1390 uiInterface.ThreadSafeMessageBox(
1391 userMessage.empty() ? _("Error: A fatal internal error occurred, see debug.log for details") : userMessage,
1392 "", CClientUIInterface::MSG_ERROR);
1393 StartShutdown();
1394 return false;
1397 bool AbortNode(CValidationState& state, const std::string& strMessage, const std::string& userMessage="")
1399 AbortNode(strMessage, userMessage);
1400 return state.Error(strMessage);
1403 } // namespace
1405 enum DisconnectResult
1407 DISCONNECT_OK, // All good.
1408 DISCONNECT_UNCLEAN, // Rolled back, but UTXO set was inconsistent with block.
1409 DISCONNECT_FAILED // Something else went wrong.
1413 * Restore the UTXO in a Coin at a given COutPoint
1414 * @param undo The Coin to be restored.
1415 * @param view The coins view to which to apply the changes.
1416 * @param out The out point that corresponds to the tx input.
1417 * @return A DisconnectResult as an int
1419 int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out)
1421 bool fClean = true;
1423 if (view.HaveCoin(out)) fClean = false; // overwriting transaction output
1425 if (undo.nHeight == 0) {
1426 // Missing undo metadata (height and coinbase). Older versions included this
1427 // information only in undo records for the last spend of a transactions'
1428 // outputs. This implies that it must be present for some other output of the same tx.
1429 const Coin& alternate = AccessByTxid(view, out.hash);
1430 if (!alternate.IsSpent()) {
1431 undo.nHeight = alternate.nHeight;
1432 undo.fCoinBase = alternate.fCoinBase;
1433 } else {
1434 return DISCONNECT_FAILED; // adding output for transaction without known metadata
1437 // The potential_overwrite parameter to AddCoin is only allowed to be false if we know for
1438 // sure that the coin did not already exist in the cache. As we have queried for that above
1439 // using HaveCoin, we don't need to guess. When fClean is false, a coin already existed and
1440 // it is an overwrite.
1441 view.AddCoin(out, std::move(undo), !fClean);
1443 return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
1446 /** Undo the effects of this block (with given index) on the UTXO set represented by coins.
1447 * When FAILED is returned, view is left in an indeterminate state. */
1448 static DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view)
1450 bool fClean = true;
1452 CBlockUndo blockUndo;
1453 CDiskBlockPos pos = pindex->GetUndoPos();
1454 if (pos.IsNull()) {
1455 error("DisconnectBlock(): no undo data available");
1456 return DISCONNECT_FAILED;
1458 if (!UndoReadFromDisk(blockUndo, pos, pindex->pprev->GetBlockHash())) {
1459 error("DisconnectBlock(): failure reading undo data");
1460 return DISCONNECT_FAILED;
1463 if (blockUndo.vtxundo.size() + 1 != block.vtx.size()) {
1464 error("DisconnectBlock(): block and undo data inconsistent");
1465 return DISCONNECT_FAILED;
1468 // undo transactions in reverse order
1469 for (int i = block.vtx.size() - 1; i >= 0; i--) {
1470 const CTransaction &tx = *(block.vtx[i]);
1471 uint256 hash = tx.GetHash();
1472 bool is_coinbase = tx.IsCoinBase();
1474 // Check that all outputs are available and match the outputs in the block itself
1475 // exactly.
1476 for (size_t o = 0; o < tx.vout.size(); o++) {
1477 if (!tx.vout[o].scriptPubKey.IsUnspendable()) {
1478 COutPoint out(hash, o);
1479 Coin coin;
1480 bool is_spent = view.SpendCoin(out, &coin);
1481 if (!is_spent || tx.vout[o] != coin.out || pindex->nHeight != coin.nHeight || is_coinbase != coin.fCoinBase) {
1482 fClean = false; // transaction output mismatch
1487 // restore inputs
1488 if (i > 0) { // not coinbases
1489 CTxUndo &txundo = blockUndo.vtxundo[i-1];
1490 if (txundo.vprevout.size() != tx.vin.size()) {
1491 error("DisconnectBlock(): transaction and undo data inconsistent");
1492 return DISCONNECT_FAILED;
1494 for (unsigned int j = tx.vin.size(); j-- > 0;) {
1495 const COutPoint &out = tx.vin[j].prevout;
1496 int res = ApplyTxInUndo(std::move(txundo.vprevout[j]), view, out);
1497 if (res == DISCONNECT_FAILED) return DISCONNECT_FAILED;
1498 fClean = fClean && res != DISCONNECT_UNCLEAN;
1500 // At this point, all of txundo.vprevout should have been moved out.
1504 // move best block pointer to prevout block
1505 view.SetBestBlock(pindex->pprev->GetBlockHash());
1507 return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
1510 void static FlushBlockFile(bool fFinalize = false)
1512 LOCK(cs_LastBlockFile);
1514 CDiskBlockPos posOld(nLastBlockFile, 0);
1516 FILE *fileOld = OpenBlockFile(posOld);
1517 if (fileOld) {
1518 if (fFinalize)
1519 TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nSize);
1520 FileCommit(fileOld);
1521 fclose(fileOld);
1524 fileOld = OpenUndoFile(posOld);
1525 if (fileOld) {
1526 if (fFinalize)
1527 TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nUndoSize);
1528 FileCommit(fileOld);
1529 fclose(fileOld);
1533 static bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
1535 static CCheckQueue<CScriptCheck> scriptcheckqueue(128);
1537 void ThreadScriptCheck() {
1538 RenameThread("bitcoin-scriptch");
1539 scriptcheckqueue.Thread();
1542 // Protected by cs_main
1543 VersionBitsCache versionbitscache;
1545 int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
1547 LOCK(cs_main);
1548 int32_t nVersion = VERSIONBITS_TOP_BITS;
1550 for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) {
1551 ThresholdState state = VersionBitsState(pindexPrev, params, (Consensus::DeploymentPos)i, versionbitscache);
1552 if (state == THRESHOLD_LOCKED_IN || state == THRESHOLD_STARTED) {
1553 nVersion |= VersionBitsMask(params, (Consensus::DeploymentPos)i);
1557 return nVersion;
1561 * Threshold condition checker that triggers when unknown versionbits are seen on the network.
1563 class WarningBitsConditionChecker : public AbstractThresholdConditionChecker
1565 private:
1566 int bit;
1568 public:
1569 explicit WarningBitsConditionChecker(int bitIn) : bit(bitIn) {}
1571 int64_t BeginTime(const Consensus::Params& params) const override { return 0; }
1572 int64_t EndTime(const Consensus::Params& params) const override { return std::numeric_limits<int64_t>::max(); }
1573 int Period(const Consensus::Params& params) const override { return params.nMinerConfirmationWindow; }
1574 int Threshold(const Consensus::Params& params) const override { return params.nRuleChangeActivationThreshold; }
1576 bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const override
1578 return ((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) &&
1579 ((pindex->nVersion >> bit) & 1) != 0 &&
1580 ((ComputeBlockVersion(pindex->pprev, params) >> bit) & 1) == 0;
1584 // Protected by cs_main
1585 static ThresholdConditionCache warningcache[VERSIONBITS_NUM_BITS];
1587 static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consensus::Params& consensusparams) {
1588 AssertLockHeld(cs_main);
1590 // BIP16 didn't become active until Apr 1 2012
1591 int64_t nBIP16SwitchTime = 1333238400;
1592 bool fStrictPayToScriptHash = (pindex->GetBlockTime() >= nBIP16SwitchTime);
1594 unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE;
1596 // Start enforcing the DERSIG (BIP66) rule
1597 if (pindex->nHeight >= consensusparams.BIP66Height) {
1598 flags |= SCRIPT_VERIFY_DERSIG;
1601 // Start enforcing CHECKLOCKTIMEVERIFY (BIP65) rule
1602 if (pindex->nHeight >= consensusparams.BIP65Height) {
1603 flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
1606 // Start enforcing BIP68 (sequence locks) and BIP112 (CHECKSEQUENCEVERIFY) using versionbits logic.
1607 if (VersionBitsState(pindex->pprev, consensusparams, Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
1608 flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
1611 // Start enforcing WITNESS rules using versionbits logic.
1612 if (IsWitnessEnabled(pindex->pprev, consensusparams)) {
1613 flags |= SCRIPT_VERIFY_WITNESS;
1614 flags |= SCRIPT_VERIFY_NULLDUMMY;
1617 return flags;
1622 static int64_t nTimeCheck = 0;
1623 static int64_t nTimeForks = 0;
1624 static int64_t nTimeVerify = 0;
1625 static int64_t nTimeConnect = 0;
1626 static int64_t nTimeIndex = 0;
1627 static int64_t nTimeCallbacks = 0;
1628 static int64_t nTimeTotal = 0;
1629 static int64_t nBlocksTotal = 0;
1631 /** Apply the effects of this block (with given index) on the UTXO set represented by coins.
1632 * Validity checks that depend on the UTXO set are also done; ConnectBlock()
1633 * can fail if those validity checks fail (among other reasons). */
1634 static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex,
1635 CCoinsViewCache& view, const CChainParams& chainparams, bool fJustCheck = false)
1637 AssertLockHeld(cs_main);
1638 assert(pindex);
1639 // pindex->phashBlock can be null if called by CreateNewBlock/TestBlockValidity
1640 assert((pindex->phashBlock == nullptr) ||
1641 (*pindex->phashBlock == block.GetHash()));
1642 int64_t nTimeStart = GetTimeMicros();
1644 // Check it again in case a previous version let a bad block in
1645 if (!CheckBlock(block, state, chainparams.GetConsensus(), !fJustCheck, !fJustCheck))
1646 return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
1648 // verify that the view's current state corresponds to the previous block
1649 uint256 hashPrevBlock = pindex->pprev == nullptr ? uint256() : pindex->pprev->GetBlockHash();
1650 assert(hashPrevBlock == view.GetBestBlock());
1652 // Special case for the genesis block, skipping connection of its transactions
1653 // (its coinbase is unspendable)
1654 if (block.GetHash() == chainparams.GetConsensus().hashGenesisBlock) {
1655 if (!fJustCheck)
1656 view.SetBestBlock(pindex->GetBlockHash());
1657 return true;
1660 nBlocksTotal++;
1662 bool fScriptChecks = true;
1663 if (!hashAssumeValid.IsNull()) {
1664 // We've been configured with the hash of a block which has been externally verified to have a valid history.
1665 // A suitable default value is included with the software and updated from time to time. Because validity
1666 // relative to a piece of software is an objective fact these defaults can be easily reviewed.
1667 // This setting doesn't force the selection of any particular chain but makes validating some faster by
1668 // effectively caching the result of part of the verification.
1669 BlockMap::const_iterator it = mapBlockIndex.find(hashAssumeValid);
1670 if (it != mapBlockIndex.end()) {
1671 if (it->second->GetAncestor(pindex->nHeight) == pindex &&
1672 pindexBestHeader->GetAncestor(pindex->nHeight) == pindex &&
1673 pindexBestHeader->nChainWork >= UintToArith256(chainparams.GetConsensus().nMinimumChainWork)) {
1674 // This block is a member of the assumed verified chain and an ancestor of the best header.
1675 // The equivalent time check discourages hash power from extorting the network via DOS attack
1676 // into accepting an invalid block through telling users they must manually set assumevalid.
1677 // Requiring a software change or burying the invalid block, regardless of the setting, makes
1678 // it hard to hide the implication of the demand. This also avoids having release candidates
1679 // that are hardly doing any signature verification at all in testing without having to
1680 // artificially set the default assumed verified block further back.
1681 // The test against nMinimumChainWork prevents the skipping when denied access to any chain at
1682 // least as good as the expected chain.
1683 fScriptChecks = (GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, chainparams.GetConsensus()) <= 60 * 60 * 24 * 7 * 2);
1688 int64_t nTime1 = GetTimeMicros(); nTimeCheck += nTime1 - nTimeStart;
1689 LogPrint(BCLog::BENCH, " - Sanity checks: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime1 - nTimeStart), nTimeCheck * MICRO, nTimeCheck * MILLI / nBlocksTotal);
1691 // Do not allow blocks that contain transactions which 'overwrite' older transactions,
1692 // unless those are already completely spent.
1693 // If such overwrites are allowed, coinbases and transactions depending upon those
1694 // can be duplicated to remove the ability to spend the first instance -- even after
1695 // being sent to another address.
1696 // See BIP30 and http://r6.ca/blog/20120206T005236Z.html for more information.
1697 // This logic is not necessary for memory pool transactions, as AcceptToMemoryPool
1698 // already refuses previously-known transaction ids entirely.
1699 // This rule was originally applied to all blocks with a timestamp after March 15, 2012, 0:00 UTC.
1700 // Now that the whole chain is irreversibly beyond that time it is applied to all blocks except the
1701 // two in the chain that violate it. This prevents exploiting the issue against nodes during their
1702 // initial block download.
1703 bool fEnforceBIP30 = (!pindex->phashBlock) || // Enforce on CreateNewBlock invocations which don't have a hash.
1704 !((pindex->nHeight==91842 && pindex->GetBlockHash() == uint256S("0x00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec")) ||
1705 (pindex->nHeight==91880 && pindex->GetBlockHash() == uint256S("0x00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721")));
1707 // Once BIP34 activated it was not possible to create new duplicate coinbases and thus other than starting
1708 // with the 2 existing duplicate coinbase pairs, not possible to create overwriting txs. But by the
1709 // time BIP34 activated, in each of the existing pairs the duplicate coinbase had overwritten the first
1710 // before the first had been spent. Since those coinbases are sufficiently buried its no longer possible to create further
1711 // duplicate transactions descending from the known pairs either.
1712 // If we're on the known chain at height greater than where BIP34 activated, we can save the db accesses needed for the BIP30 check.
1713 assert(pindex->pprev);
1714 CBlockIndex *pindexBIP34height = pindex->pprev->GetAncestor(chainparams.GetConsensus().BIP34Height);
1715 //Only continue to enforce if we're below BIP34 activation height or the block hash at that height doesn't correspond.
1716 fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->GetBlockHash() == chainparams.GetConsensus().BIP34Hash));
1718 if (fEnforceBIP30) {
1719 for (const auto& tx : block.vtx) {
1720 for (size_t o = 0; o < tx->vout.size(); o++) {
1721 if (view.HaveCoin(COutPoint(tx->GetHash(), o))) {
1722 return state.DoS(100, error("ConnectBlock(): tried to overwrite transaction"),
1723 REJECT_INVALID, "bad-txns-BIP30");
1729 // Start enforcing BIP68 (sequence locks) and BIP112 (CHECKSEQUENCEVERIFY) using versionbits logic.
1730 int nLockTimeFlags = 0;
1731 if (VersionBitsState(pindex->pprev, chainparams.GetConsensus(), Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
1732 nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE;
1735 // Get the script flags for this block
1736 unsigned int flags = GetBlockScriptFlags(pindex, chainparams.GetConsensus());
1738 int64_t nTime2 = GetTimeMicros(); nTimeForks += nTime2 - nTime1;
1739 LogPrint(BCLog::BENCH, " - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime2 - nTime1), nTimeForks * MICRO, nTimeForks * MILLI / nBlocksTotal);
1741 CBlockUndo blockundo;
1743 CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : nullptr);
1745 std::vector<int> prevheights;
1746 CAmount nFees = 0;
1747 int nInputs = 0;
1748 int64_t nSigOpsCost = 0;
1749 CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
1750 std::vector<std::pair<uint256, CDiskTxPos> > vPos;
1751 vPos.reserve(block.vtx.size());
1752 blockundo.vtxundo.reserve(block.vtx.size() - 1);
1753 std::vector<PrecomputedTransactionData> txdata;
1754 txdata.reserve(block.vtx.size()); // Required so that pointers to individual PrecomputedTransactionData don't get invalidated
1755 for (unsigned int i = 0; i < block.vtx.size(); i++)
1757 const CTransaction &tx = *(block.vtx[i]);
1759 nInputs += tx.vin.size();
1761 if (!tx.IsCoinBase())
1763 if (!view.HaveInputs(tx))
1764 return state.DoS(100, error("ConnectBlock(): inputs missing/spent"),
1765 REJECT_INVALID, "bad-txns-inputs-missingorspent");
1767 // Check that transaction is BIP68 final
1768 // BIP68 lock checks (as opposed to nLockTime checks) must
1769 // be in ConnectBlock because they require the UTXO set
1770 prevheights.resize(tx.vin.size());
1771 for (size_t j = 0; j < tx.vin.size(); j++) {
1772 prevheights[j] = view.AccessCoin(tx.vin[j].prevout).nHeight;
1775 if (!SequenceLocks(tx, nLockTimeFlags, &prevheights, *pindex)) {
1776 return state.DoS(100, error("%s: contains a non-BIP68-final transaction", __func__),
1777 REJECT_INVALID, "bad-txns-nonfinal");
1781 // GetTransactionSigOpCost counts 3 types of sigops:
1782 // * legacy (always)
1783 // * p2sh (when P2SH enabled in flags and excludes coinbase)
1784 // * witness (when witness enabled in flags and excludes coinbase)
1785 nSigOpsCost += GetTransactionSigOpCost(tx, view, flags);
1786 if (nSigOpsCost > MAX_BLOCK_SIGOPS_COST)
1787 return state.DoS(100, error("ConnectBlock(): too many sigops"),
1788 REJECT_INVALID, "bad-blk-sigops");
1790 txdata.emplace_back(tx);
1791 if (!tx.IsCoinBase())
1793 nFees += view.GetValueIn(tx)-tx.GetValueOut();
1795 std::vector<CScriptCheck> vChecks;
1796 bool fCacheResults = fJustCheck; /* Don't cache results if we're actually connecting blocks (still consult the cache, though) */
1797 if (!CheckInputs(tx, state, view, fScriptChecks, flags, fCacheResults, fCacheResults, txdata[i], nScriptCheckThreads ? &vChecks : nullptr))
1798 return error("ConnectBlock(): CheckInputs on %s failed with %s",
1799 tx.GetHash().ToString(), FormatStateMessage(state));
1800 control.Add(vChecks);
1803 CTxUndo undoDummy;
1804 if (i > 0) {
1805 blockundo.vtxundo.push_back(CTxUndo());
1807 UpdateCoins(tx, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight);
1809 vPos.push_back(std::make_pair(tx.GetHash(), pos));
1810 pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
1812 int64_t nTime3 = GetTimeMicros(); nTimeConnect += nTime3 - nTime2;
1813 LogPrint(BCLog::BENCH, " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n", (unsigned)block.vtx.size(), MILLI * (nTime3 - nTime2), MILLI * (nTime3 - nTime2) / block.vtx.size(), nInputs <= 1 ? 0 : MILLI * (nTime3 - nTime2) / (nInputs-1), nTimeConnect * MICRO, nTimeConnect * MILLI / nBlocksTotal);
1815 CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, chainparams.GetConsensus());
1816 if (block.vtx[0]->GetValueOut() > blockReward)
1817 return state.DoS(100,
1818 error("ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)",
1819 block.vtx[0]->GetValueOut(), blockReward),
1820 REJECT_INVALID, "bad-cb-amount");
1822 if (!control.Wait())
1823 return state.DoS(100, error("%s: CheckQueue failed", __func__), REJECT_INVALID, "block-validation-failed");
1824 int64_t nTime4 = GetTimeMicros(); nTimeVerify += nTime4 - nTime2;
1825 LogPrint(BCLog::BENCH, " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n", nInputs - 1, MILLI * (nTime4 - nTime2), nInputs <= 1 ? 0 : MILLI * (nTime4 - nTime2) / (nInputs-1), nTimeVerify * MICRO, nTimeVerify * MILLI / nBlocksTotal);
1827 if (fJustCheck)
1828 return true;
1830 // Write undo information to disk
1831 if (pindex->GetUndoPos().IsNull() || !pindex->IsValid(BLOCK_VALID_SCRIPTS))
1833 if (pindex->GetUndoPos().IsNull()) {
1834 CDiskBlockPos _pos;
1835 if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
1836 return error("ConnectBlock(): FindUndoPos failed");
1837 if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart()))
1838 return AbortNode(state, "Failed to write undo data");
1840 // update nUndoPos in block index
1841 pindex->nUndoPos = _pos.nPos;
1842 pindex->nStatus |= BLOCK_HAVE_UNDO;
1845 pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
1846 setDirtyBlockIndex.insert(pindex);
1849 if (fTxIndex)
1850 if (!pblocktree->WriteTxIndex(vPos))
1851 return AbortNode(state, "Failed to write transaction index");
1853 assert(pindex->phashBlock);
1854 // add this block to the view's block chain
1855 view.SetBestBlock(pindex->GetBlockHash());
1857 int64_t nTime5 = GetTimeMicros(); nTimeIndex += nTime5 - nTime4;
1858 LogPrint(BCLog::BENCH, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime5 - nTime4), nTimeIndex * MICRO, nTimeIndex * MILLI / nBlocksTotal);
1860 int64_t nTime6 = GetTimeMicros(); nTimeCallbacks += nTime6 - nTime5;
1861 LogPrint(BCLog::BENCH, " - Callbacks: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime6 - nTime5), nTimeCallbacks * MICRO, nTimeCallbacks * MILLI / nBlocksTotal);
1863 return true;
1867 * Update the on-disk chain state.
1868 * The caches and indexes are flushed depending on the mode we're called with
1869 * if they're too large, if it's been a while since the last write,
1870 * or always and in all cases if we're in prune mode and are deleting files.
1872 bool static FlushStateToDisk(const CChainParams& chainparams, CValidationState &state, FlushStateMode mode, int nManualPruneHeight) {
1873 int64_t nMempoolUsage = mempool.DynamicMemoryUsage();
1874 LOCK(cs_main);
1875 static int64_t nLastWrite = 0;
1876 static int64_t nLastFlush = 0;
1877 static int64_t nLastSetChain = 0;
1878 std::set<int> setFilesToPrune;
1879 bool fFlushForPrune = false;
1880 bool fDoFullFlush = false;
1881 int64_t nNow = 0;
1882 try {
1884 LOCK(cs_LastBlockFile);
1885 if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
1886 if (nManualPruneHeight > 0) {
1887 FindFilesToPruneManual(setFilesToPrune, nManualPruneHeight);
1888 } else {
1889 FindFilesToPrune(setFilesToPrune, chainparams.PruneAfterHeight());
1890 fCheckForPruning = false;
1892 if (!setFilesToPrune.empty()) {
1893 fFlushForPrune = true;
1894 if (!fHavePruned) {
1895 pblocktree->WriteFlag("prunedblockfiles", true);
1896 fHavePruned = true;
1900 nNow = GetTimeMicros();
1901 // Avoid writing/flushing immediately after startup.
1902 if (nLastWrite == 0) {
1903 nLastWrite = nNow;
1905 if (nLastFlush == 0) {
1906 nLastFlush = nNow;
1908 if (nLastSetChain == 0) {
1909 nLastSetChain = nNow;
1911 int64_t nMempoolSizeMax = gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
1912 int64_t cacheSize = pcoinsTip->DynamicMemoryUsage();
1913 int64_t nTotalSpace = nCoinCacheUsage + std::max<int64_t>(nMempoolSizeMax - nMempoolUsage, 0);
1914 // The cache is large and we're within 10% and 10 MiB of the limit, but we have time now (not in the middle of a block processing).
1915 bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize > std::max((9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE * 1024 * 1024);
1916 // The cache is over the limit, we have to write now.
1917 bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && cacheSize > nTotalSpace;
1918 // It's been a while since we wrote the block index to disk. Do this frequently, so we don't need to redownload after a crash.
1919 bool fPeriodicWrite = mode == FLUSH_STATE_PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
1920 // It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
1921 bool fPeriodicFlush = mode == FLUSH_STATE_PERIODIC && nNow > nLastFlush + (int64_t)DATABASE_FLUSH_INTERVAL * 1000000;
1922 // Combine all conditions that result in a full cache flush.
1923 fDoFullFlush = (mode == FLUSH_STATE_ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
1924 // Write blocks and block index to disk.
1925 if (fDoFullFlush || fPeriodicWrite) {
1926 // Depend on nMinDiskSpace to ensure we can write block index
1927 if (!CheckDiskSpace(0))
1928 return state.Error("out of disk space");
1929 // First make sure all block and undo data is flushed to disk.
1930 FlushBlockFile();
1931 // Then update all block file information (which may refer to block and undo files).
1933 std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
1934 vFiles.reserve(setDirtyFileInfo.size());
1935 for (std::set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
1936 vFiles.push_back(std::make_pair(*it, &vinfoBlockFile[*it]));
1937 setDirtyFileInfo.erase(it++);
1939 std::vector<const CBlockIndex*> vBlocks;
1940 vBlocks.reserve(setDirtyBlockIndex.size());
1941 for (std::set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
1942 vBlocks.push_back(*it);
1943 setDirtyBlockIndex.erase(it++);
1945 if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
1946 return AbortNode(state, "Failed to write to block index database");
1949 // Finally remove any pruned files
1950 if (fFlushForPrune)
1951 UnlinkPrunedFiles(setFilesToPrune);
1952 nLastWrite = nNow;
1954 // Flush best chain related state. This can only be done if the blocks / block index write was also done.
1955 if (fDoFullFlush) {
1956 // Typical Coin structures on disk are around 48 bytes in size.
1957 // Pushing a new one to the database can cause it to be written
1958 // twice (once in the log, and once in the tables). This is already
1959 // an overestimation, as most will delete an existing entry or
1960 // overwrite one. Still, use a conservative safety factor of 2.
1961 if (!CheckDiskSpace(48 * 2 * 2 * pcoinsTip->GetCacheSize()))
1962 return state.Error("out of disk space");
1963 // Flush the chainstate (which may refer to block index entries).
1964 if (!pcoinsTip->Flush())
1965 return AbortNode(state, "Failed to write to coin database");
1966 nLastFlush = nNow;
1969 if (fDoFullFlush || ((mode == FLUSH_STATE_ALWAYS || mode == FLUSH_STATE_PERIODIC) && nNow > nLastSetChain + (int64_t)DATABASE_WRITE_INTERVAL * 1000000)) {
1970 // Update best block in wallet (so we can detect restored wallets).
1971 GetMainSignals().SetBestChain(chainActive.GetLocator());
1972 nLastSetChain = nNow;
1974 } catch (const std::runtime_error& e) {
1975 return AbortNode(state, std::string("System error while flushing: ") + e.what());
1977 return true;
1980 void FlushStateToDisk() {
1981 CValidationState state;
1982 const CChainParams& chainparams = Params();
1983 FlushStateToDisk(chainparams, state, FLUSH_STATE_ALWAYS);
1986 void PruneAndFlush() {
1987 CValidationState state;
1988 fCheckForPruning = true;
1989 const CChainParams& chainparams = Params();
1990 FlushStateToDisk(chainparams, state, FLUSH_STATE_NONE);
1993 static void DoWarning(const std::string& strWarning)
1995 static bool fWarned = false;
1996 SetMiscWarning(strWarning);
1997 if (!fWarned) {
1998 AlertNotify(strWarning);
1999 fWarned = true;
2003 /** Update chainActive and related internal data structures. */
2004 void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
2005 chainActive.SetTip(pindexNew);
2007 // New best block
2008 mempool.AddTransactionsUpdated(1);
2010 cvBlockChange.notify_all();
2012 std::vector<std::string> warningMessages;
2013 if (!IsInitialBlockDownload())
2015 int nUpgraded = 0;
2016 const CBlockIndex* pindex = chainActive.Tip();
2017 for (int bit = 0; bit < VERSIONBITS_NUM_BITS; bit++) {
2018 WarningBitsConditionChecker checker(bit);
2019 ThresholdState state = checker.GetStateFor(pindex, chainParams.GetConsensus(), warningcache[bit]);
2020 if (state == THRESHOLD_ACTIVE || state == THRESHOLD_LOCKED_IN) {
2021 const std::string strWarning = strprintf(_("Warning: unknown new rules activated (versionbit %i)"), bit);
2022 if (state == THRESHOLD_ACTIVE) {
2023 DoWarning(strWarning);
2024 } else {
2025 warningMessages.push_back(strWarning);
2029 // Check the version of the last 100 blocks to see if we need to upgrade:
2030 for (int i = 0; i < 100 && pindex != nullptr; i++)
2032 int32_t nExpectedVersion = ComputeBlockVersion(pindex->pprev, chainParams.GetConsensus());
2033 if (pindex->nVersion > VERSIONBITS_LAST_OLD_BLOCK_VERSION && (pindex->nVersion & ~nExpectedVersion) != 0)
2034 ++nUpgraded;
2035 pindex = pindex->pprev;
2037 if (nUpgraded > 0)
2038 warningMessages.push_back(strprintf(_("%d of last 100 blocks have unexpected version"), nUpgraded));
2039 if (nUpgraded > 100/2)
2041 std::string strWarning = _("Warning: Unknown block versions being mined! It's possible unknown rules are in effect");
2042 // notify GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
2043 DoWarning(strWarning);
2046 LogPrintf("%s: new best=%s height=%d version=0x%08x log2_work=%.8g tx=%lu date='%s' progress=%f cache=%.1fMiB(%utxo)", __func__,
2047 chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), chainActive.Tip()->nVersion,
2048 log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
2049 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
2050 GuessVerificationProgress(chainParams.TxData(), chainActive.Tip()), pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
2051 if (!warningMessages.empty())
2052 LogPrintf(" warning='%s'", boost::algorithm::join(warningMessages, ", "));
2053 LogPrintf("\n");
2057 /** Disconnect chainActive's tip.
2058 * After calling, the mempool will be in an inconsistent state, with
2059 * transactions from disconnected blocks being added to disconnectpool. You
2060 * should make the mempool consistent again by calling UpdateMempoolForReorg.
2061 * with cs_main held.
2063 * If disconnectpool is nullptr, then no disconnected transactions are added to
2064 * disconnectpool (note that the caller is responsible for mempool consistency
2065 * in any case).
2067 bool static DisconnectTip(CValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions *disconnectpool)
2069 CBlockIndex *pindexDelete = chainActive.Tip();
2070 assert(pindexDelete);
2071 // Read block from disk.
2072 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
2073 CBlock& block = *pblock;
2074 if (!ReadBlockFromDisk(block, pindexDelete, chainparams.GetConsensus()))
2075 return AbortNode(state, "Failed to read block");
2076 // Apply the block atomically to the chain state.
2077 int64_t nStart = GetTimeMicros();
2079 CCoinsViewCache view(pcoinsTip);
2080 assert(view.GetBestBlock() == pindexDelete->GetBlockHash());
2081 if (DisconnectBlock(block, pindexDelete, view) != DISCONNECT_OK)
2082 return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
2083 bool flushed = view.Flush();
2084 assert(flushed);
2086 LogPrint(BCLog::BENCH, "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * MILLI);
2087 // Write the chain state to disk, if necessary.
2088 if (!FlushStateToDisk(chainparams, state, FLUSH_STATE_IF_NEEDED))
2089 return false;
2091 if (disconnectpool) {
2092 // Save transactions to re-add to mempool at end of reorg
2093 for (auto it = block.vtx.rbegin(); it != block.vtx.rend(); ++it) {
2094 disconnectpool->addTransaction(*it);
2096 while (disconnectpool->DynamicMemoryUsage() > MAX_DISCONNECTED_TX_POOL_SIZE * 1000) {
2097 // Drop the earliest entry, and remove its children from the mempool.
2098 auto it = disconnectpool->queuedTx.get<insertion_order>().begin();
2099 mempool.removeRecursive(**it, MemPoolRemovalReason::REORG);
2100 disconnectpool->removeEntry(it);
2104 // Update chainActive and related variables.
2105 UpdateTip(pindexDelete->pprev, chainparams);
2106 // Let wallets know transactions went from 1-confirmed to
2107 // 0-confirmed or conflicted:
2108 GetMainSignals().BlockDisconnected(pblock);
2109 return true;
2112 static int64_t nTimeReadFromDisk = 0;
2113 static int64_t nTimeConnectTotal = 0;
2114 static int64_t nTimeFlush = 0;
2115 static int64_t nTimeChainState = 0;
2116 static int64_t nTimePostConnect = 0;
2118 struct PerBlockConnectTrace {
2119 CBlockIndex* pindex = nullptr;
2120 std::shared_ptr<const CBlock> pblock;
2121 std::shared_ptr<std::vector<CTransactionRef>> conflictedTxs;
2122 PerBlockConnectTrace() : conflictedTxs(std::make_shared<std::vector<CTransactionRef>>()) {}
2125 * Used to track blocks whose transactions were applied to the UTXO state as a
2126 * part of a single ActivateBestChainStep call.
2128 * This class also tracks transactions that are removed from the mempool as
2129 * conflicts (per block) and can be used to pass all those transactions
2130 * through SyncTransaction.
2132 * This class assumes (and asserts) that the conflicted transactions for a given
2133 * block are added via mempool callbacks prior to the BlockConnected() associated
2134 * with those transactions. If any transactions are marked conflicted, it is
2135 * assumed that an associated block will always be added.
2137 * This class is single-use, once you call GetBlocksConnected() you have to throw
2138 * it away and make a new one.
2140 class ConnectTrace {
2141 private:
2142 std::vector<PerBlockConnectTrace> blocksConnected;
2143 CTxMemPool &pool;
2145 public:
2146 explicit ConnectTrace(CTxMemPool &_pool) : blocksConnected(1), pool(_pool) {
2147 pool.NotifyEntryRemoved.connect(boost::bind(&ConnectTrace::NotifyEntryRemoved, this, _1, _2));
2150 ~ConnectTrace() {
2151 pool.NotifyEntryRemoved.disconnect(boost::bind(&ConnectTrace::NotifyEntryRemoved, this, _1, _2));
2154 void BlockConnected(CBlockIndex* pindex, std::shared_ptr<const CBlock> pblock) {
2155 assert(!blocksConnected.back().pindex);
2156 assert(pindex);
2157 assert(pblock);
2158 blocksConnected.back().pindex = pindex;
2159 blocksConnected.back().pblock = std::move(pblock);
2160 blocksConnected.emplace_back();
2163 std::vector<PerBlockConnectTrace>& GetBlocksConnected() {
2164 // We always keep one extra block at the end of our list because
2165 // blocks are added after all the conflicted transactions have
2166 // been filled in. Thus, the last entry should always be an empty
2167 // one waiting for the transactions from the next block. We pop
2168 // the last entry here to make sure the list we return is sane.
2169 assert(!blocksConnected.back().pindex);
2170 assert(blocksConnected.back().conflictedTxs->empty());
2171 blocksConnected.pop_back();
2172 return blocksConnected;
2175 void NotifyEntryRemoved(CTransactionRef txRemoved, MemPoolRemovalReason reason) {
2176 assert(!blocksConnected.back().pindex);
2177 if (reason == MemPoolRemovalReason::CONFLICT) {
2178 blocksConnected.back().conflictedTxs->emplace_back(std::move(txRemoved));
2184 * Connect a new block to chainActive. pblock is either nullptr or a pointer to a CBlock
2185 * corresponding to pindexNew, to bypass loading it again from disk.
2187 * The block is added to connectTrace if connection succeeds.
2189 bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions &disconnectpool)
2191 assert(pindexNew->pprev == chainActive.Tip());
2192 // Read block from disk.
2193 int64_t nTime1 = GetTimeMicros();
2194 std::shared_ptr<const CBlock> pthisBlock;
2195 if (!pblock) {
2196 std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
2197 if (!ReadBlockFromDisk(*pblockNew, pindexNew, chainparams.GetConsensus()))
2198 return AbortNode(state, "Failed to read block");
2199 pthisBlock = pblockNew;
2200 } else {
2201 pthisBlock = pblock;
2203 const CBlock& blockConnecting = *pthisBlock;
2204 // Apply the block atomically to the chain state.
2205 int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1;
2206 int64_t nTime3;
2207 LogPrint(BCLog::BENCH, " - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * MILLI, nTimeReadFromDisk * MICRO);
2209 CCoinsViewCache view(pcoinsTip);
2210 bool rv = ConnectBlock(blockConnecting, state, pindexNew, view, chainparams);
2211 GetMainSignals().BlockChecked(blockConnecting, state);
2212 if (!rv) {
2213 if (state.IsInvalid())
2214 InvalidBlockFound(pindexNew, state);
2215 return error("ConnectTip(): ConnectBlock %s failed", pindexNew->GetBlockHash().ToString());
2217 nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2;
2218 LogPrint(BCLog::BENCH, " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime3 - nTime2) * MILLI, nTimeConnectTotal * MICRO, nTimeConnectTotal * MILLI / nBlocksTotal);
2219 bool flushed = view.Flush();
2220 assert(flushed);
2222 int64_t nTime4 = GetTimeMicros(); nTimeFlush += nTime4 - nTime3;
2223 LogPrint(BCLog::BENCH, " - Flush: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime4 - nTime3) * MILLI, nTimeFlush * MICRO, nTimeFlush * MILLI / nBlocksTotal);
2224 // Write the chain state to disk, if necessary.
2225 if (!FlushStateToDisk(chainparams, state, FLUSH_STATE_IF_NEEDED))
2226 return false;
2227 int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
2228 LogPrint(BCLog::BENCH, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime5 - nTime4) * MILLI, nTimeChainState * MICRO, nTimeChainState * MILLI / nBlocksTotal);
2229 // Remove conflicting transactions from the mempool.;
2230 mempool.removeForBlock(blockConnecting.vtx, pindexNew->nHeight);
2231 disconnectpool.removeForBlock(blockConnecting.vtx);
2232 // Update chainActive & related variables.
2233 UpdateTip(pindexNew, chainparams);
2235 int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
2236 LogPrint(BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime6 - nTime5) * MILLI, nTimePostConnect * MICRO, nTimePostConnect * MILLI / nBlocksTotal);
2237 LogPrint(BCLog::BENCH, "- Connect block: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime6 - nTime1) * MILLI, nTimeTotal * MICRO, nTimeTotal * MILLI / nBlocksTotal);
2239 connectTrace.BlockConnected(pindexNew, std::move(pthisBlock));
2240 return true;
2244 * Return the tip of the chain with the most work in it, that isn't
2245 * known to be invalid (it's however far from certain to be valid).
2247 static CBlockIndex* FindMostWorkChain() {
2248 do {
2249 CBlockIndex *pindexNew = nullptr;
2251 // Find the best candidate header.
2253 std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexCandidates.rbegin();
2254 if (it == setBlockIndexCandidates.rend())
2255 return nullptr;
2256 pindexNew = *it;
2259 // Check whether all blocks on the path between the currently active chain and the candidate are valid.
2260 // Just going until the active chain is an optimization, as we know all blocks in it are valid already.
2261 CBlockIndex *pindexTest = pindexNew;
2262 bool fInvalidAncestor = false;
2263 while (pindexTest && !chainActive.Contains(pindexTest)) {
2264 assert(pindexTest->nChainTx || pindexTest->nHeight == 0);
2266 // Pruned nodes may have entries in setBlockIndexCandidates for
2267 // which block files have been deleted. Remove those as candidates
2268 // for the most work chain if we come across them; we can't switch
2269 // to a chain unless we have all the non-active-chain parent blocks.
2270 bool fFailedChain = pindexTest->nStatus & BLOCK_FAILED_MASK;
2271 bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
2272 if (fFailedChain || fMissingData) {
2273 // Candidate chain is not usable (either invalid or missing data)
2274 if (fFailedChain && (pindexBestInvalid == nullptr || pindexNew->nChainWork > pindexBestInvalid->nChainWork))
2275 pindexBestInvalid = pindexNew;
2276 CBlockIndex *pindexFailed = pindexNew;
2277 // Remove the entire chain from the set.
2278 while (pindexTest != pindexFailed) {
2279 if (fFailedChain) {
2280 pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
2281 } else if (fMissingData) {
2282 // If we're missing data, then add back to mapBlocksUnlinked,
2283 // so that if the block arrives in the future we can try adding
2284 // to setBlockIndexCandidates again.
2285 mapBlocksUnlinked.insert(std::make_pair(pindexFailed->pprev, pindexFailed));
2287 setBlockIndexCandidates.erase(pindexFailed);
2288 pindexFailed = pindexFailed->pprev;
2290 setBlockIndexCandidates.erase(pindexTest);
2291 fInvalidAncestor = true;
2292 break;
2294 pindexTest = pindexTest->pprev;
2296 if (!fInvalidAncestor)
2297 return pindexNew;
2298 } while(true);
2301 /** Delete all entries in setBlockIndexCandidates that are worse than the current tip. */
2302 static void PruneBlockIndexCandidates() {
2303 // Note that we can't delete the current block itself, as we may need to return to it later in case a
2304 // reorganization to a better block fails.
2305 std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
2306 while (it != setBlockIndexCandidates.end() && setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) {
2307 setBlockIndexCandidates.erase(it++);
2309 // Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates.
2310 assert(!setBlockIndexCandidates.empty());
2314 * Try to make some progress towards making pindexMostWork the active block.
2315 * pblock is either nullptr or a pointer to a CBlock corresponding to pindexMostWork.
2317 static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace)
2319 AssertLockHeld(cs_main);
2320 const CBlockIndex *pindexOldTip = chainActive.Tip();
2321 const CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork);
2323 // Disconnect active blocks which are no longer in the best chain.
2324 bool fBlocksDisconnected = false;
2325 DisconnectedBlockTransactions disconnectpool;
2326 while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
2327 if (!DisconnectTip(state, chainparams, &disconnectpool)) {
2328 // This is likely a fatal error, but keep the mempool consistent,
2329 // just in case. Only remove from the mempool in this case.
2330 UpdateMempoolForReorg(disconnectpool, false);
2331 return false;
2333 fBlocksDisconnected = true;
2336 // Build list of new blocks to connect.
2337 std::vector<CBlockIndex*> vpindexToConnect;
2338 bool fContinue = true;
2339 int nHeight = pindexFork ? pindexFork->nHeight : -1;
2340 while (fContinue && nHeight != pindexMostWork->nHeight) {
2341 // Don't iterate the entire list of potential improvements toward the best tip, as we likely only need
2342 // a few blocks along the way.
2343 int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight);
2344 vpindexToConnect.clear();
2345 vpindexToConnect.reserve(nTargetHeight - nHeight);
2346 CBlockIndex *pindexIter = pindexMostWork->GetAncestor(nTargetHeight);
2347 while (pindexIter && pindexIter->nHeight != nHeight) {
2348 vpindexToConnect.push_back(pindexIter);
2349 pindexIter = pindexIter->pprev;
2351 nHeight = nTargetHeight;
2353 // Connect new blocks.
2354 for (CBlockIndex *pindexConnect : reverse_iterate(vpindexToConnect)) {
2355 if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr<const CBlock>(), connectTrace, disconnectpool)) {
2356 if (state.IsInvalid()) {
2357 // The block violates a consensus rule.
2358 if (!state.CorruptionPossible())
2359 InvalidChainFound(vpindexToConnect.back());
2360 state = CValidationState();
2361 fInvalidFound = true;
2362 fContinue = false;
2363 break;
2364 } else {
2365 // A system error occurred (disk space, database error, ...).
2366 // Make the mempool consistent with the current tip, just in case
2367 // any observers try to use it before shutdown.
2368 UpdateMempoolForReorg(disconnectpool, false);
2369 return false;
2371 } else {
2372 PruneBlockIndexCandidates();
2373 if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
2374 // We're in a better position than we were. Return temporarily to release the lock.
2375 fContinue = false;
2376 break;
2382 if (fBlocksDisconnected) {
2383 // If any blocks were disconnected, disconnectpool may be non empty. Add
2384 // any disconnected transactions back to the mempool.
2385 UpdateMempoolForReorg(disconnectpool, true);
2387 mempool.check(pcoinsTip);
2389 // Callbacks/notifications for a new best chain.
2390 if (fInvalidFound)
2391 CheckForkWarningConditionsOnNewFork(vpindexToConnect.back());
2392 else
2393 CheckForkWarningConditions();
2395 return true;
2398 static void NotifyHeaderTip() {
2399 bool fNotify = false;
2400 bool fInitialBlockDownload = false;
2401 static CBlockIndex* pindexHeaderOld = nullptr;
2402 CBlockIndex* pindexHeader = nullptr;
2404 LOCK(cs_main);
2405 pindexHeader = pindexBestHeader;
2407 if (pindexHeader != pindexHeaderOld) {
2408 fNotify = true;
2409 fInitialBlockDownload = IsInitialBlockDownload();
2410 pindexHeaderOld = pindexHeader;
2413 // Send block tip changed notifications without cs_main
2414 if (fNotify) {
2415 uiInterface.NotifyHeaderTip(fInitialBlockDownload, pindexHeader);
2420 * Make the best chain active, in multiple steps. The result is either failure
2421 * or an activated best chain. pblock is either nullptr or a pointer to a block
2422 * that is already loaded (to avoid loading it again from disk).
2424 bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) {
2425 // Note that while we're often called here from ProcessNewBlock, this is
2426 // far from a guarantee. Things in the P2P/RPC will often end up calling
2427 // us in the middle of ProcessNewBlock - do not assume pblock is set
2428 // sanely for performance or correctness!
2430 CBlockIndex *pindexMostWork = nullptr;
2431 CBlockIndex *pindexNewTip = nullptr;
2432 int nStopAtHeight = gArgs.GetArg("-stopatheight", DEFAULT_STOPATHEIGHT);
2433 do {
2434 boost::this_thread::interruption_point();
2435 if (ShutdownRequested())
2436 break;
2438 const CBlockIndex *pindexFork;
2439 bool fInitialDownload;
2441 LOCK(cs_main);
2442 ConnectTrace connectTrace(mempool); // Destructed before cs_main is unlocked
2444 CBlockIndex *pindexOldTip = chainActive.Tip();
2445 if (pindexMostWork == nullptr) {
2446 pindexMostWork = FindMostWorkChain();
2449 // Whether we have anything to do at all.
2450 if (pindexMostWork == nullptr || pindexMostWork == chainActive.Tip())
2451 return true;
2453 bool fInvalidFound = false;
2454 std::shared_ptr<const CBlock> nullBlockPtr;
2455 if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace))
2456 return false;
2458 if (fInvalidFound) {
2459 // Wipe cache, we may need another branch now.
2460 pindexMostWork = nullptr;
2462 pindexNewTip = chainActive.Tip();
2463 pindexFork = chainActive.FindFork(pindexOldTip);
2464 fInitialDownload = IsInitialBlockDownload();
2466 for (const PerBlockConnectTrace& trace : connectTrace.GetBlocksConnected()) {
2467 assert(trace.pblock && trace.pindex);
2468 GetMainSignals().BlockConnected(trace.pblock, trace.pindex, *trace.conflictedTxs);
2471 // When we reach this point, we switched to a new tip (stored in pindexNewTip).
2473 // Notifications/callbacks that can run without cs_main
2475 // Notify external listeners about the new tip.
2476 GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);
2478 // Always notify the UI if a new block tip was connected
2479 if (pindexFork != pindexNewTip) {
2480 uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
2483 if (nStopAtHeight && pindexNewTip && pindexNewTip->nHeight >= nStopAtHeight) StartShutdown();
2484 } while (pindexNewTip != pindexMostWork);
2485 CheckBlockIndex(chainparams.GetConsensus());
2487 // Write changes periodically to disk, after relay.
2488 if (!FlushStateToDisk(chainparams, state, FLUSH_STATE_PERIODIC)) {
2489 return false;
2492 return true;
2496 bool PreciousBlock(CValidationState& state, const CChainParams& params, CBlockIndex *pindex)
2499 LOCK(cs_main);
2500 if (pindex->nChainWork < chainActive.Tip()->nChainWork) {
2501 // Nothing to do, this block is not at the tip.
2502 return true;
2504 if (chainActive.Tip()->nChainWork > nLastPreciousChainwork) {
2505 // The chain has been extended since the last call, reset the counter.
2506 nBlockReverseSequenceId = -1;
2508 nLastPreciousChainwork = chainActive.Tip()->nChainWork;
2509 setBlockIndexCandidates.erase(pindex);
2510 pindex->nSequenceId = nBlockReverseSequenceId;
2511 if (nBlockReverseSequenceId > std::numeric_limits<int32_t>::min()) {
2512 // We can't keep reducing the counter if somebody really wants to
2513 // call preciousblock 2**31-1 times on the same set of tips...
2514 nBlockReverseSequenceId--;
2516 if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && pindex->nChainTx) {
2517 setBlockIndexCandidates.insert(pindex);
2518 PruneBlockIndexCandidates();
2522 return ActivateBestChain(state, params);
2525 bool InvalidateBlock(CValidationState& state, const CChainParams& chainparams, CBlockIndex *pindex)
2527 AssertLockHeld(cs_main);
2529 // Mark the block itself as invalid.
2530 pindex->nStatus |= BLOCK_FAILED_VALID;
2531 setDirtyBlockIndex.insert(pindex);
2532 setBlockIndexCandidates.erase(pindex);
2534 DisconnectedBlockTransactions disconnectpool;
2535 while (chainActive.Contains(pindex)) {
2536 CBlockIndex *pindexWalk = chainActive.Tip();
2537 pindexWalk->nStatus |= BLOCK_FAILED_CHILD;
2538 setDirtyBlockIndex.insert(pindexWalk);
2539 setBlockIndexCandidates.erase(pindexWalk);
2540 // ActivateBestChain considers blocks already in chainActive
2541 // unconditionally valid already, so force disconnect away from it.
2542 if (!DisconnectTip(state, chainparams, &disconnectpool)) {
2543 // It's probably hopeless to try to make the mempool consistent
2544 // here if DisconnectTip failed, but we can try.
2545 UpdateMempoolForReorg(disconnectpool, false);
2546 return false;
2550 // DisconnectTip will add transactions to disconnectpool; try to add these
2551 // back to the mempool.
2552 UpdateMempoolForReorg(disconnectpool, true);
2554 // The resulting new best tip may not be in setBlockIndexCandidates anymore, so
2555 // add it again.
2556 BlockMap::iterator it = mapBlockIndex.begin();
2557 while (it != mapBlockIndex.end()) {
2558 if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && !setBlockIndexCandidates.value_comp()(it->second, chainActive.Tip())) {
2559 setBlockIndexCandidates.insert(it->second);
2561 it++;
2564 InvalidChainFound(pindex);
2565 uiInterface.NotifyBlockTip(IsInitialBlockDownload(), pindex->pprev);
2566 return true;
2569 bool ResetBlockFailureFlags(CBlockIndex *pindex) {
2570 AssertLockHeld(cs_main);
2572 int nHeight = pindex->nHeight;
2574 // Remove the invalidity flag from this block and all its descendants.
2575 BlockMap::iterator it = mapBlockIndex.begin();
2576 while (it != mapBlockIndex.end()) {
2577 if (!it->second->IsValid() && it->second->GetAncestor(nHeight) == pindex) {
2578 it->second->nStatus &= ~BLOCK_FAILED_MASK;
2579 setDirtyBlockIndex.insert(it->second);
2580 if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && setBlockIndexCandidates.value_comp()(chainActive.Tip(), it->second)) {
2581 setBlockIndexCandidates.insert(it->second);
2583 if (it->second == pindexBestInvalid) {
2584 // Reset invalid block marker if it was pointing to one of those.
2585 pindexBestInvalid = nullptr;
2588 it++;
2591 // Remove the invalidity flag from all ancestors too.
2592 while (pindex != nullptr) {
2593 if (pindex->nStatus & BLOCK_FAILED_MASK) {
2594 pindex->nStatus &= ~BLOCK_FAILED_MASK;
2595 setDirtyBlockIndex.insert(pindex);
2597 pindex = pindex->pprev;
2599 return true;
2602 static CBlockIndex* AddToBlockIndex(const CBlockHeader& block)
2604 // Check for duplicate
2605 uint256 hash = block.GetHash();
2606 BlockMap::iterator it = mapBlockIndex.find(hash);
2607 if (it != mapBlockIndex.end())
2608 return it->second;
2610 // Construct new block index object
2611 CBlockIndex* pindexNew = new CBlockIndex(block);
2612 assert(pindexNew);
2613 // We assign the sequence id to blocks only when the full data is available,
2614 // to avoid miners withholding blocks but broadcasting headers, to get a
2615 // competitive advantage.
2616 pindexNew->nSequenceId = 0;
2617 BlockMap::iterator mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first;
2618 pindexNew->phashBlock = &((*mi).first);
2619 BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock);
2620 if (miPrev != mapBlockIndex.end())
2622 pindexNew->pprev = (*miPrev).second;
2623 pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
2624 pindexNew->BuildSkip();
2626 pindexNew->nTimeMax = (pindexNew->pprev ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime) : pindexNew->nTime);
2627 pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew);
2628 pindexNew->RaiseValidity(BLOCK_VALID_TREE);
2629 if (pindexBestHeader == nullptr || pindexBestHeader->nChainWork < pindexNew->nChainWork)
2630 pindexBestHeader = pindexNew;
2632 setDirtyBlockIndex.insert(pindexNew);
2634 return pindexNew;
2637 /** Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */
2638 static bool ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBlockIndex *pindexNew, const CDiskBlockPos& pos, const Consensus::Params& consensusParams)
2640 pindexNew->nTx = block.vtx.size();
2641 pindexNew->nChainTx = 0;
2642 pindexNew->nFile = pos.nFile;
2643 pindexNew->nDataPos = pos.nPos;
2644 pindexNew->nUndoPos = 0;
2645 pindexNew->nStatus |= BLOCK_HAVE_DATA;
2646 if (IsWitnessEnabled(pindexNew->pprev, consensusParams)) {
2647 pindexNew->nStatus |= BLOCK_OPT_WITNESS;
2649 pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
2650 setDirtyBlockIndex.insert(pindexNew);
2652 if (pindexNew->pprev == nullptr || pindexNew->pprev->nChainTx) {
2653 // If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
2654 std::deque<CBlockIndex*> queue;
2655 queue.push_back(pindexNew);
2657 // Recursively process any descendant blocks that now may be eligible to be connected.
2658 while (!queue.empty()) {
2659 CBlockIndex *pindex = queue.front();
2660 queue.pop_front();
2661 pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
2663 LOCK(cs_nBlockSequenceId);
2664 pindex->nSequenceId = nBlockSequenceId++;
2666 if (chainActive.Tip() == nullptr || !setBlockIndexCandidates.value_comp()(pindex, chainActive.Tip())) {
2667 setBlockIndexCandidates.insert(pindex);
2669 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex);
2670 while (range.first != range.second) {
2671 std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
2672 queue.push_back(it->second);
2673 range.first++;
2674 mapBlocksUnlinked.erase(it);
2677 } else {
2678 if (pindexNew->pprev && pindexNew->pprev->IsValid(BLOCK_VALID_TREE)) {
2679 mapBlocksUnlinked.insert(std::make_pair(pindexNew->pprev, pindexNew));
2683 return true;
2686 static bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
2688 LOCK(cs_LastBlockFile);
2690 unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile;
2691 if (vinfoBlockFile.size() <= nFile) {
2692 vinfoBlockFile.resize(nFile + 1);
2695 if (!fKnown) {
2696 while (vinfoBlockFile[nFile].nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
2697 nFile++;
2698 if (vinfoBlockFile.size() <= nFile) {
2699 vinfoBlockFile.resize(nFile + 1);
2702 pos.nFile = nFile;
2703 pos.nPos = vinfoBlockFile[nFile].nSize;
2706 if ((int)nFile != nLastBlockFile) {
2707 if (!fKnown) {
2708 LogPrintf("Leaving block file %i: %s\n", nLastBlockFile, vinfoBlockFile[nLastBlockFile].ToString());
2710 FlushBlockFile(!fKnown);
2711 nLastBlockFile = nFile;
2714 vinfoBlockFile[nFile].AddBlock(nHeight, nTime);
2715 if (fKnown)
2716 vinfoBlockFile[nFile].nSize = std::max(pos.nPos + nAddSize, vinfoBlockFile[nFile].nSize);
2717 else
2718 vinfoBlockFile[nFile].nSize += nAddSize;
2720 if (!fKnown) {
2721 unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
2722 unsigned int nNewChunks = (vinfoBlockFile[nFile].nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
2723 if (nNewChunks > nOldChunks) {
2724 if (fPruneMode)
2725 fCheckForPruning = true;
2726 if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
2727 FILE *file = OpenBlockFile(pos);
2728 if (file) {
2729 LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
2730 AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
2731 fclose(file);
2734 else
2735 return state.Error("out of disk space");
2739 setDirtyFileInfo.insert(nFile);
2740 return true;
2743 static bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
2745 pos.nFile = nFile;
2747 LOCK(cs_LastBlockFile);
2749 unsigned int nNewSize;
2750 pos.nPos = vinfoBlockFile[nFile].nUndoSize;
2751 nNewSize = vinfoBlockFile[nFile].nUndoSize += nAddSize;
2752 setDirtyFileInfo.insert(nFile);
2754 unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
2755 unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
2756 if (nNewChunks > nOldChunks) {
2757 if (fPruneMode)
2758 fCheckForPruning = true;
2759 if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
2760 FILE *file = OpenUndoFile(pos);
2761 if (file) {
2762 LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
2763 AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
2764 fclose(file);
2767 else
2768 return state.Error("out of disk space");
2771 return true;
2774 static bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true)
2776 // Check proof of work matches claimed amount
2777 if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
2778 return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");
2780 return true;
2783 bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
2785 // These are checks that are independent of context.
2787 if (block.fChecked)
2788 return true;
2790 // Check that the header is valid (particularly PoW). This is mostly
2791 // redundant with the call in AcceptBlockHeader.
2792 if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW))
2793 return false;
2795 // Check the merkle root.
2796 if (fCheckMerkleRoot) {
2797 bool mutated;
2798 uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated);
2799 if (block.hashMerkleRoot != hashMerkleRoot2)
2800 return state.DoS(100, false, REJECT_INVALID, "bad-txnmrklroot", true, "hashMerkleRoot mismatch");
2802 // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
2803 // of transactions in a block without affecting the merkle root of a block,
2804 // while still invalidating it.
2805 if (mutated)
2806 return state.DoS(100, false, REJECT_INVALID, "bad-txns-duplicate", true, "duplicate transaction");
2809 // All potential-corruption validation must be done before we do any
2810 // transaction validation, as otherwise we may mark the header as invalid
2811 // because we receive the wrong transactions for it.
2812 // Note that witness malleability is checked in ContextualCheckBlock, so no
2813 // checks that use witness data may be performed here.
2815 // Size limits
2816 if (block.vtx.empty() || block.vtx.size() * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > MAX_BLOCK_WEIGHT)
2817 return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed");
2819 // First transaction must be coinbase, the rest must not be
2820 if (block.vtx.empty() || !block.vtx[0]->IsCoinBase())
2821 return state.DoS(100, false, REJECT_INVALID, "bad-cb-missing", false, "first tx is not coinbase");
2822 for (unsigned int i = 1; i < block.vtx.size(); i++)
2823 if (block.vtx[i]->IsCoinBase())
2824 return state.DoS(100, false, REJECT_INVALID, "bad-cb-multiple", false, "more than one coinbase");
2826 // Check transactions
2827 for (const auto& tx : block.vtx)
2828 if (!CheckTransaction(*tx, state, false))
2829 return state.Invalid(false, state.GetRejectCode(), state.GetRejectReason(),
2830 strprintf("Transaction check failed (tx hash %s) %s", tx->GetHash().ToString(), state.GetDebugMessage()));
2832 unsigned int nSigOps = 0;
2833 for (const auto& tx : block.vtx)
2835 nSigOps += GetLegacySigOpCount(*tx);
2837 if (nSigOps * WITNESS_SCALE_FACTOR > MAX_BLOCK_SIGOPS_COST)
2838 return state.DoS(100, false, REJECT_INVALID, "bad-blk-sigops", false, "out-of-bounds SigOpCount");
2840 if (fCheckPOW && fCheckMerkleRoot)
2841 block.fChecked = true;
2843 return true;
2846 bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params)
2848 LOCK(cs_main);
2849 return (VersionBitsState(pindexPrev, params, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_ACTIVE);
2852 // Compute at which vout of the block's coinbase transaction the witness
2853 // commitment occurs, or -1 if not found.
2854 static int GetWitnessCommitmentIndex(const CBlock& block)
2856 int commitpos = -1;
2857 if (!block.vtx.empty()) {
2858 for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
2859 if (block.vtx[0]->vout[o].scriptPubKey.size() >= 38 && block.vtx[0]->vout[o].scriptPubKey[0] == OP_RETURN && block.vtx[0]->vout[o].scriptPubKey[1] == 0x24 && block.vtx[0]->vout[o].scriptPubKey[2] == 0xaa && block.vtx[0]->vout[o].scriptPubKey[3] == 0x21 && block.vtx[0]->vout[o].scriptPubKey[4] == 0xa9 && block.vtx[0]->vout[o].scriptPubKey[5] == 0xed) {
2860 commitpos = o;
2864 return commitpos;
2867 void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
2869 int commitpos = GetWitnessCommitmentIndex(block);
2870 static const std::vector<unsigned char> nonce(32, 0x00);
2871 if (commitpos != -1 && IsWitnessEnabled(pindexPrev, consensusParams) && !block.vtx[0]->HasWitness()) {
2872 CMutableTransaction tx(*block.vtx[0]);
2873 tx.vin[0].scriptWitness.stack.resize(1);
2874 tx.vin[0].scriptWitness.stack[0] = nonce;
2875 block.vtx[0] = MakeTransactionRef(std::move(tx));
2879 std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
2881 std::vector<unsigned char> commitment;
2882 int commitpos = GetWitnessCommitmentIndex(block);
2883 std::vector<unsigned char> ret(32, 0x00);
2884 if (consensusParams.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nTimeout != 0) {
2885 if (commitpos == -1) {
2886 uint256 witnessroot = BlockWitnessMerkleRoot(block, nullptr);
2887 CHash256().Write(witnessroot.begin(), 32).Write(ret.data(), 32).Finalize(witnessroot.begin());
2888 CTxOut out;
2889 out.nValue = 0;
2890 out.scriptPubKey.resize(38);
2891 out.scriptPubKey[0] = OP_RETURN;
2892 out.scriptPubKey[1] = 0x24;
2893 out.scriptPubKey[2] = 0xaa;
2894 out.scriptPubKey[3] = 0x21;
2895 out.scriptPubKey[4] = 0xa9;
2896 out.scriptPubKey[5] = 0xed;
2897 memcpy(&out.scriptPubKey[6], witnessroot.begin(), 32);
2898 commitment = std::vector<unsigned char>(out.scriptPubKey.begin(), out.scriptPubKey.end());
2899 CMutableTransaction tx(*block.vtx[0]);
2900 tx.vout.push_back(out);
2901 block.vtx[0] = MakeTransactionRef(std::move(tx));
2904 UpdateUncommittedBlockStructures(block, pindexPrev, consensusParams);
2905 return commitment;
2908 /** Context-dependent validity checks.
2909 * By "context", we mean only the previous block headers, but not the UTXO
2910 * set; UTXO-related validity checks are done in ConnectBlock(). */
2911 static bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& params, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
2913 assert(pindexPrev != nullptr);
2914 const int nHeight = pindexPrev->nHeight + 1;
2916 // Check proof of work
2917 const Consensus::Params& consensusParams = params.GetConsensus();
2918 if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
2919 return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");
2921 // Check against checkpoints
2922 if (fCheckpointsEnabled) {
2923 // Don't accept any forks from the main chain prior to last checkpoint.
2924 // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
2925 // MapBlockIndex.
2926 CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(params.Checkpoints());
2927 if (pcheckpoint && nHeight < pcheckpoint->nHeight)
2928 return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight), REJECT_CHECKPOINT, "bad-fork-prior-to-checkpoint");
2931 // Check timestamp against prev
2932 if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
2933 return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
2935 // Check timestamp
2936 if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME)
2937 return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
2939 // Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded:
2940 // check for version 2, 3 and 4 upgrades
2941 if((block.nVersion < 2 && nHeight >= consensusParams.BIP34Height) ||
2942 (block.nVersion < 3 && nHeight >= consensusParams.BIP66Height) ||
2943 (block.nVersion < 4 && nHeight >= consensusParams.BIP65Height))
2944 return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", block.nVersion),
2945 strprintf("rejected nVersion=0x%08x block", block.nVersion));
2947 return true;
2950 static bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
2952 const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
2954 // Start enforcing BIP113 (Median Time Past) using versionbits logic.
2955 int nLockTimeFlags = 0;
2956 if (VersionBitsState(pindexPrev, consensusParams, Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
2957 nLockTimeFlags |= LOCKTIME_MEDIAN_TIME_PAST;
2960 int64_t nLockTimeCutoff = (nLockTimeFlags & LOCKTIME_MEDIAN_TIME_PAST)
2961 ? pindexPrev->GetMedianTimePast()
2962 : block.GetBlockTime();
2964 // Check that all transactions are finalized
2965 for (const auto& tx : block.vtx) {
2966 if (!IsFinalTx(*tx, nHeight, nLockTimeCutoff)) {
2967 return state.DoS(10, false, REJECT_INVALID, "bad-txns-nonfinal", false, "non-final transaction");
2971 // Enforce rule that the coinbase starts with serialized block height
2972 if (nHeight >= consensusParams.BIP34Height)
2974 CScript expect = CScript() << nHeight;
2975 if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() ||
2976 !std::equal(expect.begin(), expect.end(), block.vtx[0]->vin[0].scriptSig.begin())) {
2977 return state.DoS(100, false, REJECT_INVALID, "bad-cb-height", false, "block height mismatch in coinbase");
2981 // Validation for witness commitments.
2982 // * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the
2983 // coinbase (where 0x0000....0000 is used instead).
2984 // * The coinbase scriptWitness is a stack of a single 32-byte vector, containing a witness nonce (unconstrained).
2985 // * We build a merkle tree with all those witness hashes as leaves (similar to the hashMerkleRoot in the block header).
2986 // * There must be at least one output whose scriptPubKey is a single 36-byte push, the first 4 bytes of which are
2987 // {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness nonce). In case there are
2988 // multiple, the last one is used.
2989 bool fHaveWitness = false;
2990 if (VersionBitsState(pindexPrev, consensusParams, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_ACTIVE) {
2991 int commitpos = GetWitnessCommitmentIndex(block);
2992 if (commitpos != -1) {
2993 bool malleated = false;
2994 uint256 hashWitness = BlockWitnessMerkleRoot(block, &malleated);
2995 // The malleation check is ignored; as the transaction tree itself
2996 // already does not permit it, it is impossible to trigger in the
2997 // witness tree.
2998 if (block.vtx[0]->vin[0].scriptWitness.stack.size() != 1 || block.vtx[0]->vin[0].scriptWitness.stack[0].size() != 32) {
2999 return state.DoS(100, false, REJECT_INVALID, "bad-witness-nonce-size", true, strprintf("%s : invalid witness nonce size", __func__));
3001 CHash256().Write(hashWitness.begin(), 32).Write(&block.vtx[0]->vin[0].scriptWitness.stack[0][0], 32).Finalize(hashWitness.begin());
3002 if (memcmp(hashWitness.begin(), &block.vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
3003 return state.DoS(100, false, REJECT_INVALID, "bad-witness-merkle-match", true, strprintf("%s : witness merkle commitment mismatch", __func__));
3005 fHaveWitness = true;
3009 // No witness data is allowed in blocks that don't commit to witness data, as this would otherwise leave room for spam
3010 if (!fHaveWitness) {
3011 for (const auto& tx : block.vtx) {
3012 if (tx->HasWitness()) {
3013 return state.DoS(100, false, REJECT_INVALID, "unexpected-witness", true, strprintf("%s : unexpected witness data found", __func__));
3018 // After the coinbase witness nonce and commitment are verified,
3019 // we can check if the block weight passes (before we've checked the
3020 // coinbase witness, it would be possible for the weight to be too
3021 // large by filling up the coinbase witness, which doesn't change
3022 // the block hash, so we couldn't mark the block as permanently
3023 // failed).
3024 if (GetBlockWeight(block) > MAX_BLOCK_WEIGHT) {
3025 return state.DoS(100, false, REJECT_INVALID, "bad-blk-weight", false, strprintf("%s : weight limit failed", __func__));
3028 return true;
3031 static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
3033 AssertLockHeld(cs_main);
3034 // Check for duplicate
3035 uint256 hash = block.GetHash();
3036 BlockMap::iterator miSelf = mapBlockIndex.find(hash);
3037 CBlockIndex *pindex = nullptr;
3038 if (hash != chainparams.GetConsensus().hashGenesisBlock) {
3040 if (miSelf != mapBlockIndex.end()) {
3041 // Block header is already known.
3042 pindex = miSelf->second;
3043 if (ppindex)
3044 *ppindex = pindex;
3045 if (pindex->nStatus & BLOCK_FAILED_MASK)
3046 return state.Invalid(error("%s: block %s is marked invalid", __func__, hash.ToString()), 0, "duplicate");
3047 return true;
3050 if (!CheckBlockHeader(block, state, chainparams.GetConsensus()))
3051 return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
3053 // Get prev block index
3054 CBlockIndex* pindexPrev = nullptr;
3055 BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
3056 if (mi == mapBlockIndex.end())
3057 return state.DoS(10, error("%s: prev block not found", __func__), 0, "prev-blk-not-found");
3058 pindexPrev = (*mi).second;
3059 if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
3060 return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
3061 if (!ContextualCheckBlockHeader(block, state, chainparams, pindexPrev, GetAdjustedTime()))
3062 return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
3064 if (pindex == nullptr)
3065 pindex = AddToBlockIndex(block);
3067 if (ppindex)
3068 *ppindex = pindex;
3070 CheckBlockIndex(chainparams.GetConsensus());
3072 return true;
3075 // Exposed wrapper for AcceptBlockHeader
3076 bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, CValidationState& state, const CChainParams& chainparams, const CBlockIndex** ppindex)
3079 LOCK(cs_main);
3080 for (const CBlockHeader& header : headers) {
3081 CBlockIndex *pindex = nullptr; // Use a temp pindex instead of ppindex to avoid a const_cast
3082 if (!AcceptBlockHeader(header, state, chainparams, &pindex)) {
3083 return false;
3085 if (ppindex) {
3086 *ppindex = pindex;
3090 NotifyHeaderTip();
3091 return true;
3094 /** Store block on disk. If dbp is non-nullptr, the file is known to already reside on disk */
3095 static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const CDiskBlockPos* dbp, bool* fNewBlock)
3097 const CBlock& block = *pblock;
3099 if (fNewBlock) *fNewBlock = false;
3100 AssertLockHeld(cs_main);
3102 CBlockIndex *pindexDummy = nullptr;
3103 CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
3105 if (!AcceptBlockHeader(block, state, chainparams, &pindex))
3106 return false;
3108 // Try to process all requested blocks that we don't have, but only
3109 // process an unrequested block if it's new and has enough work to
3110 // advance our tip, and isn't too many blocks ahead.
3111 bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA;
3112 bool fHasMoreWork = (chainActive.Tip() ? pindex->nChainWork > chainActive.Tip()->nChainWork : true);
3113 // Blocks that are too out-of-order needlessly limit the effectiveness of
3114 // pruning, because pruning will not delete block files that contain any
3115 // blocks which are too close in height to the tip. Apply this test
3116 // regardless of whether pruning is enabled; it should generally be safe to
3117 // not process unrequested blocks.
3118 bool fTooFarAhead = (pindex->nHeight > int(chainActive.Height() + MIN_BLOCKS_TO_KEEP));
3120 // TODO: Decouple this function from the block download logic by removing fRequested
3121 // This requires some new chain data structure to efficiently look up if a
3122 // block is in a chain leading to a candidate for best tip, despite not
3123 // being such a candidate itself.
3125 // TODO: deal better with return value and error conditions for duplicate
3126 // and unrequested blocks.
3127 if (fAlreadyHave) return true;
3128 if (!fRequested) { // If we didn't ask for it:
3129 if (pindex->nTx != 0) return true; // This is a previously-processed block that was pruned
3130 if (!fHasMoreWork) return true; // Don't process less-work chains
3131 if (fTooFarAhead) return true; // Block height is too high
3133 if (fNewBlock) *fNewBlock = true;
3135 if (!CheckBlock(block, state, chainparams.GetConsensus()) ||
3136 !ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindex->pprev)) {
3137 if (state.IsInvalid() && !state.CorruptionPossible()) {
3138 pindex->nStatus |= BLOCK_FAILED_VALID;
3139 setDirtyBlockIndex.insert(pindex);
3141 return error("%s: %s", __func__, FormatStateMessage(state));
3144 // Header is valid/has work, merkle tree and segwit merkle tree are good...RELAY NOW
3145 // (but if it does not build on our best tip, let the SendMessages loop relay it)
3146 if (!IsInitialBlockDownload() && chainActive.Tip() == pindex->pprev)
3147 GetMainSignals().NewPoWValidBlock(pindex, pblock);
3149 int nHeight = pindex->nHeight;
3151 // Write block to history file
3152 try {
3153 unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
3154 CDiskBlockPos blockPos;
3155 if (dbp != nullptr)
3156 blockPos = *dbp;
3157 if (!FindBlockPos(state, blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != nullptr))
3158 return error("AcceptBlock(): FindBlockPos failed");
3159 if (dbp == nullptr)
3160 if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
3161 AbortNode(state, "Failed to write block");
3162 if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
3163 return error("AcceptBlock(): ReceivedBlockTransactions failed");
3164 } catch (const std::runtime_error& e) {
3165 return AbortNode(state, std::string("System error: ") + e.what());
3168 if (fCheckForPruning)
3169 FlushStateToDisk(chainparams, state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
3171 return true;
3174 bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<const CBlock> pblock, bool fForceProcessing, bool *fNewBlock)
3177 CBlockIndex *pindex = nullptr;
3178 if (fNewBlock) *fNewBlock = false;
3179 CValidationState state;
3180 // Ensure that CheckBlock() passes before calling AcceptBlock, as
3181 // belt-and-suspenders.
3182 bool ret = CheckBlock(*pblock, state, chainparams.GetConsensus());
3184 LOCK(cs_main);
3186 if (ret) {
3187 // Store to disk
3188 ret = AcceptBlock(pblock, state, chainparams, &pindex, fForceProcessing, nullptr, fNewBlock);
3190 CheckBlockIndex(chainparams.GetConsensus());
3191 if (!ret) {
3192 GetMainSignals().BlockChecked(*pblock, state);
3193 return error("%s: AcceptBlock FAILED", __func__);
3197 NotifyHeaderTip();
3199 CValidationState state; // Only used to report errors, not invalidity - ignore it
3200 if (!ActivateBestChain(state, chainparams, pblock))
3201 return error("%s: ActivateBestChain failed", __func__);
3203 return true;
3206 bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
3208 AssertLockHeld(cs_main);
3209 assert(pindexPrev && pindexPrev == chainActive.Tip());
3210 CCoinsViewCache viewNew(pcoinsTip);
3211 CBlockIndex indexDummy(block);
3212 indexDummy.pprev = pindexPrev;
3213 indexDummy.nHeight = pindexPrev->nHeight + 1;
3215 // NOTE: CheckBlockHeader is called by CheckBlock
3216 if (!ContextualCheckBlockHeader(block, state, chainparams, pindexPrev, GetAdjustedTime()))
3217 return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, FormatStateMessage(state));
3218 if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot))
3219 return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
3220 if (!ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindexPrev))
3221 return error("%s: Consensus::ContextualCheckBlock: %s", __func__, FormatStateMessage(state));
3222 if (!ConnectBlock(block, state, &indexDummy, viewNew, chainparams, true))
3223 return false;
3224 assert(state.IsValid());
3226 return true;
3230 * BLOCK PRUNING CODE
3233 /* Calculate the amount of disk space the block & undo files currently use */
3234 static uint64_t CalculateCurrentUsage()
3236 uint64_t retval = 0;
3237 for (const CBlockFileInfo &file : vinfoBlockFile) {
3238 retval += file.nSize + file.nUndoSize;
3240 return retval;
3243 /* Prune a block file (modify associated database entries)*/
3244 void PruneOneBlockFile(const int fileNumber)
3246 for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) {
3247 CBlockIndex* pindex = it->second;
3248 if (pindex->nFile == fileNumber) {
3249 pindex->nStatus &= ~BLOCK_HAVE_DATA;
3250 pindex->nStatus &= ~BLOCK_HAVE_UNDO;
3251 pindex->nFile = 0;
3252 pindex->nDataPos = 0;
3253 pindex->nUndoPos = 0;
3254 setDirtyBlockIndex.insert(pindex);
3256 // Prune from mapBlocksUnlinked -- any block we prune would have
3257 // to be downloaded again in order to consider its chain, at which
3258 // point it would be considered as a candidate for
3259 // mapBlocksUnlinked or setBlockIndexCandidates.
3260 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex->pprev);
3261 while (range.first != range.second) {
3262 std::multimap<CBlockIndex *, CBlockIndex *>::iterator _it = range.first;
3263 range.first++;
3264 if (_it->second == pindex) {
3265 mapBlocksUnlinked.erase(_it);
3271 vinfoBlockFile[fileNumber].SetNull();
3272 setDirtyFileInfo.insert(fileNumber);
3276 void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
3278 for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
3279 CDiskBlockPos pos(*it, 0);
3280 fs::remove(GetBlockPosFilename(pos, "blk"));
3281 fs::remove(GetBlockPosFilename(pos, "rev"));
3282 LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
3286 /* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
3287 static void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight)
3289 assert(fPruneMode && nManualPruneHeight > 0);
3291 LOCK2(cs_main, cs_LastBlockFile);
3292 if (chainActive.Tip() == nullptr)
3293 return;
3295 // last block to prune is the lesser of (user-specified height, MIN_BLOCKS_TO_KEEP from the tip)
3296 unsigned int nLastBlockWeCanPrune = std::min((unsigned)nManualPruneHeight, chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP);
3297 int count=0;
3298 for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
3299 if (vinfoBlockFile[fileNumber].nSize == 0 || vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
3300 continue;
3301 PruneOneBlockFile(fileNumber);
3302 setFilesToPrune.insert(fileNumber);
3303 count++;
3305 LogPrintf("Prune (Manual): prune_height=%d removed %d blk/rev pairs\n", nLastBlockWeCanPrune, count);
3308 /* This function is called from the RPC code for pruneblockchain */
3309 void PruneBlockFilesManual(int nManualPruneHeight)
3311 CValidationState state;
3312 const CChainParams& chainparams = Params();
3313 FlushStateToDisk(chainparams, state, FLUSH_STATE_NONE, nManualPruneHeight);
3317 * Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a user-defined target.
3318 * The user sets the target (in MB) on the command line or in config file. This will be run on startup and whenever new
3319 * space is allocated in a block or undo file, staying below the target. Changing back to unpruned requires a reindex
3320 * (which in this case means the blockchain must be re-downloaded.)
3322 * Pruning functions are called from FlushStateToDisk when the global fCheckForPruning flag has been set.
3323 * Block and undo files are deleted in lock-step (when blk00003.dat is deleted, so is rev00003.dat.)
3324 * Pruning cannot take place until the longest chain is at least a certain length (100000 on mainnet, 1000 on testnet, 1000 on regtest).
3325 * Pruning will never delete a block within a defined distance (currently 288) from the active chain's tip.
3326 * The block index is updated by unsetting HAVE_DATA and HAVE_UNDO for any blocks that were stored in the deleted files.
3327 * A db flag records the fact that at least some block files have been pruned.
3329 * @param[out] setFilesToPrune The set of file indices that can be unlinked will be returned
3331 static void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight)
3333 LOCK2(cs_main, cs_LastBlockFile);
3334 if (chainActive.Tip() == nullptr || nPruneTarget == 0) {
3335 return;
3337 if ((uint64_t)chainActive.Tip()->nHeight <= nPruneAfterHeight) {
3338 return;
3341 unsigned int nLastBlockWeCanPrune = chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP;
3342 uint64_t nCurrentUsage = CalculateCurrentUsage();
3343 // We don't check to prune until after we've allocated new space for files
3344 // So we should leave a buffer under our target to account for another allocation
3345 // before the next pruning.
3346 uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
3347 uint64_t nBytesToPrune;
3348 int count=0;
3350 if (nCurrentUsage + nBuffer >= nPruneTarget) {
3351 for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
3352 nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize;
3354 if (vinfoBlockFile[fileNumber].nSize == 0)
3355 continue;
3357 if (nCurrentUsage + nBuffer < nPruneTarget) // are we below our target?
3358 break;
3360 // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
3361 if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
3362 continue;
3364 PruneOneBlockFile(fileNumber);
3365 // Queue up the files for removal
3366 setFilesToPrune.insert(fileNumber);
3367 nCurrentUsage -= nBytesToPrune;
3368 count++;
3372 LogPrint(BCLog::PRUNE, "Prune: target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n",
3373 nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
3374 ((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
3375 nLastBlockWeCanPrune, count);
3378 bool CheckDiskSpace(uint64_t nAdditionalBytes)
3380 uint64_t nFreeBytesAvailable = fs::space(GetDataDir()).available;
3382 // Check for nMinDiskSpace bytes (currently 50MB)
3383 if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
3384 return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
3386 return true;
3389 static FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
3391 if (pos.IsNull())
3392 return nullptr;
3393 fs::path path = GetBlockPosFilename(pos, prefix);
3394 fs::create_directories(path.parent_path());
3395 FILE* file = fsbridge::fopen(path, "rb+");
3396 if (!file && !fReadOnly)
3397 file = fsbridge::fopen(path, "wb+");
3398 if (!file) {
3399 LogPrintf("Unable to open file %s\n", path.string());
3400 return nullptr;
3402 if (pos.nPos) {
3403 if (fseek(file, pos.nPos, SEEK_SET)) {
3404 LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
3405 fclose(file);
3406 return nullptr;
3409 return file;
3412 FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
3413 return OpenDiskFile(pos, "blk", fReadOnly);
3416 /** Open an undo file (rev?????.dat) */
3417 static FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
3418 return OpenDiskFile(pos, "rev", fReadOnly);
3421 fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
3423 return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
3426 CBlockIndex * InsertBlockIndex(uint256 hash)
3428 if (hash.IsNull())
3429 return nullptr;
3431 // Return existing
3432 BlockMap::iterator mi = mapBlockIndex.find(hash);
3433 if (mi != mapBlockIndex.end())
3434 return (*mi).second;
3436 // Create new
3437 CBlockIndex* pindexNew = new CBlockIndex();
3438 if (!pindexNew)
3439 throw std::runtime_error(std::string(__func__) + ": new CBlockIndex failed");
3440 mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first;
3441 pindexNew->phashBlock = &((*mi).first);
3443 return pindexNew;
3446 bool static LoadBlockIndexDB(const CChainParams& chainparams)
3448 if (!pblocktree->LoadBlockIndexGuts(chainparams.GetConsensus(), InsertBlockIndex))
3449 return false;
3451 boost::this_thread::interruption_point();
3453 // Calculate nChainWork
3454 std::vector<std::pair<int, CBlockIndex*> > vSortedByHeight;
3455 vSortedByHeight.reserve(mapBlockIndex.size());
3456 for (const std::pair<uint256, CBlockIndex*>& item : mapBlockIndex)
3458 CBlockIndex* pindex = item.second;
3459 vSortedByHeight.push_back(std::make_pair(pindex->nHeight, pindex));
3461 sort(vSortedByHeight.begin(), vSortedByHeight.end());
3462 for (const std::pair<int, CBlockIndex*>& item : vSortedByHeight)
3464 CBlockIndex* pindex = item.second;
3465 pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
3466 pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime);
3467 // We can link the chain of blocks for which we've received transactions at some point.
3468 // Pruned nodes may have deleted the block.
3469 if (pindex->nTx > 0) {
3470 if (pindex->pprev) {
3471 if (pindex->pprev->nChainTx) {
3472 pindex->nChainTx = pindex->pprev->nChainTx + pindex->nTx;
3473 } else {
3474 pindex->nChainTx = 0;
3475 mapBlocksUnlinked.insert(std::make_pair(pindex->pprev, pindex));
3477 } else {
3478 pindex->nChainTx = pindex->nTx;
3481 if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->nChainTx || pindex->pprev == nullptr))
3482 setBlockIndexCandidates.insert(pindex);
3483 if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
3484 pindexBestInvalid = pindex;
3485 if (pindex->pprev)
3486 pindex->BuildSkip();
3487 if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == nullptr || CBlockIndexWorkComparator()(pindexBestHeader, pindex)))
3488 pindexBestHeader = pindex;
3491 // Load block file info
3492 pblocktree->ReadLastBlockFile(nLastBlockFile);
3493 vinfoBlockFile.resize(nLastBlockFile + 1);
3494 LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile);
3495 for (int nFile = 0; nFile <= nLastBlockFile; nFile++) {
3496 pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
3498 LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString());
3499 for (int nFile = nLastBlockFile + 1; true; nFile++) {
3500 CBlockFileInfo info;
3501 if (pblocktree->ReadBlockFileInfo(nFile, info)) {
3502 vinfoBlockFile.push_back(info);
3503 } else {
3504 break;
3508 // Check presence of blk files
3509 LogPrintf("Checking all blk files are present...\n");
3510 std::set<int> setBlkDataFiles;
3511 for (const std::pair<uint256, CBlockIndex*>& item : mapBlockIndex)
3513 CBlockIndex* pindex = item.second;
3514 if (pindex->nStatus & BLOCK_HAVE_DATA) {
3515 setBlkDataFiles.insert(pindex->nFile);
3518 for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++)
3520 CDiskBlockPos pos(*it, 0);
3521 if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) {
3522 return false;
3526 // Check whether we have ever pruned block & undo files
3527 pblocktree->ReadFlag("prunedblockfiles", fHavePruned);
3528 if (fHavePruned)
3529 LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n");
3531 // Check whether we need to continue reindexing
3532 bool fReindexing = false;
3533 pblocktree->ReadReindexing(fReindexing);
3534 fReindex |= fReindexing;
3536 // Check whether we have a transaction index
3537 pblocktree->ReadFlag("txindex", fTxIndex);
3538 LogPrintf("%s: transaction index %s\n", __func__, fTxIndex ? "enabled" : "disabled");
3540 return true;
3543 bool LoadChainTip(const CChainParams& chainparams)
3545 if (chainActive.Tip() && chainActive.Tip()->GetBlockHash() == pcoinsTip->GetBestBlock()) return true;
3547 if (pcoinsTip->GetBestBlock().IsNull() && mapBlockIndex.size() == 1) {
3548 // In case we just added the genesis block, connect it now, so
3549 // that we always have a chainActive.Tip() when we return.
3550 LogPrintf("%s: Connecting genesis block...\n", __func__);
3551 CValidationState state;
3552 if (!ActivateBestChain(state, chainparams)) {
3553 return false;
3557 // Load pointer to end of best chain
3558 BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
3559 if (it == mapBlockIndex.end())
3560 return false;
3561 chainActive.SetTip(it->second);
3563 PruneBlockIndexCandidates();
3565 LogPrintf("Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
3566 chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
3567 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
3568 GuessVerificationProgress(chainparams.TxData(), chainActive.Tip()));
3569 return true;
3572 CVerifyDB::CVerifyDB()
3574 uiInterface.ShowProgress(_("Verifying blocks..."), 0);
3577 CVerifyDB::~CVerifyDB()
3579 uiInterface.ShowProgress("", 100);
3582 bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
3584 LOCK(cs_main);
3585 if (chainActive.Tip() == nullptr || chainActive.Tip()->pprev == nullptr)
3586 return true;
3588 // Verify blocks in the best chain
3589 if (nCheckDepth <= 0 || nCheckDepth > chainActive.Height())
3590 nCheckDepth = chainActive.Height();
3591 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
3592 LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
3593 CCoinsViewCache coins(coinsview);
3594 CBlockIndex* pindexState = chainActive.Tip();
3595 CBlockIndex* pindexFailure = nullptr;
3596 int nGoodTransactions = 0;
3597 CValidationState state;
3598 int reportDone = 0;
3599 LogPrintf("[0%%]...");
3600 for (CBlockIndex* pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev)
3602 boost::this_thread::interruption_point();
3603 int percentageDone = std::max(1, std::min(99, (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
3604 if (reportDone < percentageDone/10) {
3605 // report every 10% step
3606 LogPrintf("[%d%%]...", percentageDone);
3607 reportDone = percentageDone/10;
3609 uiInterface.ShowProgress(_("Verifying blocks..."), percentageDone);
3610 if (pindex->nHeight < chainActive.Height()-nCheckDepth)
3611 break;
3612 if (fPruneMode && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
3613 // If pruning, only go back as far as we have data.
3614 LogPrintf("VerifyDB(): block verification stopping at height %d (pruning, no data)\n", pindex->nHeight);
3615 break;
3617 CBlock block;
3618 // check level 0: read from disk
3619 if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
3620 return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3621 // check level 1: verify block validity
3622 if (nCheckLevel >= 1 && !CheckBlock(block, state, chainparams.GetConsensus()))
3623 return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
3624 pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
3625 // check level 2: verify undo validity
3626 if (nCheckLevel >= 2 && pindex) {
3627 CBlockUndo undo;
3628 CDiskBlockPos pos = pindex->GetUndoPos();
3629 if (!pos.IsNull()) {
3630 if (!UndoReadFromDisk(undo, pos, pindex->pprev->GetBlockHash()))
3631 return error("VerifyDB(): *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
3634 // check level 3: check for inconsistencies during memory-only disconnect of tip blocks
3635 if (nCheckLevel >= 3 && pindex == pindexState && (coins.DynamicMemoryUsage() + pcoinsTip->DynamicMemoryUsage()) <= nCoinCacheUsage) {
3636 assert(coins.GetBestBlock() == pindex->GetBlockHash());
3637 DisconnectResult res = DisconnectBlock(block, pindex, coins);
3638 if (res == DISCONNECT_FAILED) {
3639 return error("VerifyDB(): *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3641 pindexState = pindex->pprev;
3642 if (res == DISCONNECT_UNCLEAN) {
3643 nGoodTransactions = 0;
3644 pindexFailure = pindex;
3645 } else {
3646 nGoodTransactions += block.vtx.size();
3649 if (ShutdownRequested())
3650 return true;
3652 if (pindexFailure)
3653 return error("VerifyDB(): *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainActive.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
3655 // check level 4: try reconnecting blocks
3656 if (nCheckLevel >= 4) {
3657 CBlockIndex *pindex = pindexState;
3658 while (pindex != chainActive.Tip()) {
3659 boost::this_thread::interruption_point();
3660 uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, 100 - (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * 50))));
3661 pindex = chainActive.Next(pindex);
3662 CBlock block;
3663 if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
3664 return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3665 if (!ConnectBlock(block, state, pindex, coins, chainparams))
3666 return error("VerifyDB(): *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3670 LogPrintf("[DONE].\n");
3671 LogPrintf("No coin database inconsistencies in last %i blocks (%i transactions)\n", chainActive.Height() - pindexState->nHeight, nGoodTransactions);
3673 return true;
3676 /** Apply the effects of a block on the utxo cache, ignoring that it may already have been applied. */
3677 static bool RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& inputs, const CChainParams& params)
3679 // TODO: merge with ConnectBlock
3680 CBlock block;
3681 if (!ReadBlockFromDisk(block, pindex, params.GetConsensus())) {
3682 return error("ReplayBlock(): ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3685 for (const CTransactionRef& tx : block.vtx) {
3686 if (!tx->IsCoinBase()) {
3687 for (const CTxIn &txin : tx->vin) {
3688 inputs.SpendCoin(txin.prevout);
3691 // Pass check = true as every addition may be an overwrite.
3692 AddCoins(inputs, *tx, pindex->nHeight, true);
3694 return true;
3697 bool ReplayBlocks(const CChainParams& params, CCoinsView* view)
3699 LOCK(cs_main);
3701 CCoinsViewCache cache(view);
3703 std::vector<uint256> hashHeads = view->GetHeadBlocks();
3704 if (hashHeads.empty()) return true; // We're already in a consistent state.
3705 if (hashHeads.size() != 2) return error("ReplayBlocks(): unknown inconsistent state");
3707 uiInterface.ShowProgress(_("Replaying blocks..."), 0);
3708 LogPrintf("Replaying blocks\n");
3710 const CBlockIndex* pindexOld = nullptr; // Old tip during the interrupted flush.
3711 const CBlockIndex* pindexNew; // New tip during the interrupted flush.
3712 const CBlockIndex* pindexFork = nullptr; // Latest block common to both the old and the new tip.
3714 if (mapBlockIndex.count(hashHeads[0]) == 0) {
3715 return error("ReplayBlocks(): reorganization to unknown block requested");
3717 pindexNew = mapBlockIndex[hashHeads[0]];
3719 if (!hashHeads[1].IsNull()) { // The old tip is allowed to be 0, indicating it's the first flush.
3720 if (mapBlockIndex.count(hashHeads[1]) == 0) {
3721 return error("ReplayBlocks(): reorganization from unknown block requested");
3723 pindexOld = mapBlockIndex[hashHeads[1]];
3724 pindexFork = LastCommonAncestor(pindexOld, pindexNew);
3725 assert(pindexFork != nullptr);
3728 // Rollback along the old branch.
3729 while (pindexOld != pindexFork) {
3730 if (pindexOld->nHeight > 0) { // Never disconnect the genesis block.
3731 CBlock block;
3732 if (!ReadBlockFromDisk(block, pindexOld, params.GetConsensus())) {
3733 return error("RollbackBlock(): ReadBlockFromDisk() failed at %d, hash=%s", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
3735 LogPrintf("Rolling back %s (%i)\n", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
3736 DisconnectResult res = DisconnectBlock(block, pindexOld, cache);
3737 if (res == DISCONNECT_FAILED) {
3738 return error("RollbackBlock(): DisconnectBlock failed at %d, hash=%s", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
3740 // If DISCONNECT_UNCLEAN is returned, it means a non-existing UTXO was deleted, or an existing UTXO was
3741 // overwritten. It corresponds to cases where the block-to-be-disconnect never had all its operations
3742 // applied to the UTXO set. However, as both writing a UTXO and deleting a UTXO are idempotent operations,
3743 // the result is still a version of the UTXO set with the effects of that block undone.
3745 pindexOld = pindexOld->pprev;
3748 // Roll forward from the forking point to the new tip.
3749 int nForkHeight = pindexFork ? pindexFork->nHeight : 0;
3750 for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight; ++nHeight) {
3751 const CBlockIndex* pindex = pindexNew->GetAncestor(nHeight);
3752 LogPrintf("Rolling forward %s (%i)\n", pindex->GetBlockHash().ToString(), nHeight);
3753 if (!RollforwardBlock(pindex, cache, params)) return false;
3756 cache.SetBestBlock(pindexNew->GetBlockHash());
3757 cache.Flush();
3758 uiInterface.ShowProgress("", 100);
3759 return true;
3762 bool RewindBlockIndex(const CChainParams& params)
3764 LOCK(cs_main);
3766 // Note that during -reindex-chainstate we are called with an empty chainActive!
3768 int nHeight = 1;
3769 while (nHeight <= chainActive.Height()) {
3770 if (IsWitnessEnabled(chainActive[nHeight - 1], params.GetConsensus()) && !(chainActive[nHeight]->nStatus & BLOCK_OPT_WITNESS)) {
3771 break;
3773 nHeight++;
3776 // nHeight is now the height of the first insufficiently-validated block, or tipheight + 1
3777 CValidationState state;
3778 CBlockIndex* pindex = chainActive.Tip();
3779 while (chainActive.Height() >= nHeight) {
3780 if (fPruneMode && !(chainActive.Tip()->nStatus & BLOCK_HAVE_DATA)) {
3781 // If pruning, don't try rewinding past the HAVE_DATA point;
3782 // since older blocks can't be served anyway, there's
3783 // no need to walk further, and trying to DisconnectTip()
3784 // will fail (and require a needless reindex/redownload
3785 // of the blockchain).
3786 break;
3788 if (!DisconnectTip(state, params, nullptr)) {
3789 return error("RewindBlockIndex: unable to disconnect block at height %i", pindex->nHeight);
3791 // Occasionally flush state to disk.
3792 if (!FlushStateToDisk(params, state, FLUSH_STATE_PERIODIC))
3793 return false;
3796 // Reduce validity flag and have-data flags.
3797 // We do this after actual disconnecting, otherwise we'll end up writing the lack of data
3798 // to disk before writing the chainstate, resulting in a failure to continue if interrupted.
3799 for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
3800 CBlockIndex* pindexIter = it->second;
3802 // Note: If we encounter an insufficiently validated block that
3803 // is on chainActive, it must be because we are a pruning node, and
3804 // this block or some successor doesn't HAVE_DATA, so we were unable to
3805 // rewind all the way. Blocks remaining on chainActive at this point
3806 // must not have their validity reduced.
3807 if (IsWitnessEnabled(pindexIter->pprev, params.GetConsensus()) && !(pindexIter->nStatus & BLOCK_OPT_WITNESS) && !chainActive.Contains(pindexIter)) {
3808 // Reduce validity
3809 pindexIter->nStatus = std::min<unsigned int>(pindexIter->nStatus & BLOCK_VALID_MASK, BLOCK_VALID_TREE) | (pindexIter->nStatus & ~BLOCK_VALID_MASK);
3810 // Remove have-data flags.
3811 pindexIter->nStatus &= ~(BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO);
3812 // Remove storage location.
3813 pindexIter->nFile = 0;
3814 pindexIter->nDataPos = 0;
3815 pindexIter->nUndoPos = 0;
3816 // Remove various other things
3817 pindexIter->nTx = 0;
3818 pindexIter->nChainTx = 0;
3819 pindexIter->nSequenceId = 0;
3820 // Make sure it gets written.
3821 setDirtyBlockIndex.insert(pindexIter);
3822 // Update indexes
3823 setBlockIndexCandidates.erase(pindexIter);
3824 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> ret = mapBlocksUnlinked.equal_range(pindexIter->pprev);
3825 while (ret.first != ret.second) {
3826 if (ret.first->second == pindexIter) {
3827 mapBlocksUnlinked.erase(ret.first++);
3828 } else {
3829 ++ret.first;
3832 } else if (pindexIter->IsValid(BLOCK_VALID_TRANSACTIONS) && pindexIter->nChainTx) {
3833 setBlockIndexCandidates.insert(pindexIter);
3837 if (chainActive.Tip() != nullptr) {
3838 // We can't prune block index candidates based on our tip if we have
3839 // no tip due to chainActive being empty!
3840 PruneBlockIndexCandidates();
3842 CheckBlockIndex(params.GetConsensus());
3844 // FlushStateToDisk can possibly read chainActive. Be conservative
3845 // and skip it here, we're about to -reindex-chainstate anyway, so
3846 // it'll get called a bunch real soon.
3847 if (!FlushStateToDisk(params, state, FLUSH_STATE_ALWAYS)) {
3848 return false;
3852 return true;
3855 // May NOT be used after any connections are up as much
3856 // of the peer-processing logic assumes a consistent
3857 // block index state
3858 void UnloadBlockIndex()
3860 LOCK(cs_main);
3861 setBlockIndexCandidates.clear();
3862 chainActive.SetTip(nullptr);
3863 pindexBestInvalid = nullptr;
3864 pindexBestHeader = nullptr;
3865 mempool.clear();
3866 mapBlocksUnlinked.clear();
3867 vinfoBlockFile.clear();
3868 nLastBlockFile = 0;
3869 nBlockSequenceId = 1;
3870 setDirtyBlockIndex.clear();
3871 setDirtyFileInfo.clear();
3872 versionbitscache.Clear();
3873 for (int b = 0; b < VERSIONBITS_NUM_BITS; b++) {
3874 warningcache[b].clear();
3877 for (BlockMap::value_type& entry : mapBlockIndex) {
3878 delete entry.second;
3880 mapBlockIndex.clear();
3881 fHavePruned = false;
3884 bool LoadBlockIndex(const CChainParams& chainparams)
3886 // Load block index from databases
3887 bool needs_init = fReindex;
3888 if (!fReindex) {
3889 bool ret = LoadBlockIndexDB(chainparams);
3890 if (!ret) return false;
3891 needs_init = mapBlockIndex.empty();
3894 if (needs_init) {
3895 // Everything here is for *new* reindex/DBs. Thus, though
3896 // LoadBlockIndexDB may have set fReindex if we shut down
3897 // mid-reindex previously, we don't check fReindex and
3898 // instead only check it prior to LoadBlockIndexDB to set
3899 // needs_init.
3901 LogPrintf("Initializing databases...\n");
3902 // Use the provided setting for -txindex in the new database
3903 fTxIndex = gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX);
3904 pblocktree->WriteFlag("txindex", fTxIndex);
3906 return true;
3909 bool LoadGenesisBlock(const CChainParams& chainparams)
3911 LOCK(cs_main);
3913 // Check whether we're already initialized by checking for genesis in
3914 // mapBlockIndex. Note that we can't use chainActive here, since it is
3915 // set based on the coins db, not the block index db, which is the only
3916 // thing loaded at this point.
3917 if (mapBlockIndex.count(chainparams.GenesisBlock().GetHash()))
3918 return true;
3920 try {
3921 CBlock &block = const_cast<CBlock&>(chainparams.GenesisBlock());
3922 // Start new block file
3923 unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
3924 CDiskBlockPos blockPos;
3925 CValidationState state;
3926 if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.GetBlockTime()))
3927 return error("%s: FindBlockPos failed", __func__);
3928 if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
3929 return error("%s: writing genesis block to disk failed", __func__);
3930 CBlockIndex *pindex = AddToBlockIndex(block);
3931 if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
3932 return error("%s: genesis block not accepted", __func__);
3933 } catch (const std::runtime_error& e) {
3934 return error("%s: failed to write genesis block: %s", __func__, e.what());
3937 return true;
3940 bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp)
3942 // Map of disk positions for blocks with unknown parent (only used for reindex)
3943 static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
3944 int64_t nStart = GetTimeMillis();
3946 int nLoaded = 0;
3947 try {
3948 // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
3949 CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE+8, SER_DISK, CLIENT_VERSION);
3950 uint64_t nRewind = blkdat.GetPos();
3951 while (!blkdat.eof()) {
3952 boost::this_thread::interruption_point();
3954 blkdat.SetPos(nRewind);
3955 nRewind++; // start one byte further next time, in case of failure
3956 blkdat.SetLimit(); // remove former limit
3957 unsigned int nSize = 0;
3958 try {
3959 // locate a header
3960 unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
3961 blkdat.FindByte(chainparams.MessageStart()[0]);
3962 nRewind = blkdat.GetPos()+1;
3963 blkdat >> FLATDATA(buf);
3964 if (memcmp(buf, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE))
3965 continue;
3966 // read size
3967 blkdat >> nSize;
3968 if (nSize < 80 || nSize > MAX_BLOCK_SERIALIZED_SIZE)
3969 continue;
3970 } catch (const std::exception&) {
3971 // no valid block header found; don't complain
3972 break;
3974 try {
3975 // read block
3976 uint64_t nBlockPos = blkdat.GetPos();
3977 if (dbp)
3978 dbp->nPos = nBlockPos;
3979 blkdat.SetLimit(nBlockPos + nSize);
3980 blkdat.SetPos(nBlockPos);
3981 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
3982 CBlock& block = *pblock;
3983 blkdat >> block;
3984 nRewind = blkdat.GetPos();
3986 // detect out of order blocks, and store them for later
3987 uint256 hash = block.GetHash();
3988 if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex.find(block.hashPrevBlock) == mapBlockIndex.end()) {
3989 LogPrint(BCLog::REINDEX, "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
3990 block.hashPrevBlock.ToString());
3991 if (dbp)
3992 mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
3993 continue;
3996 // process in case the block isn't known yet
3997 if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
3998 LOCK(cs_main);
3999 CValidationState state;
4000 if (AcceptBlock(pblock, state, chainparams, nullptr, true, dbp, nullptr))
4001 nLoaded++;
4002 if (state.IsError())
4003 break;
4004 } else if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex[hash]->nHeight % 1000 == 0) {
4005 LogPrint(BCLog::REINDEX, "Block Import: already had block %s at height %d\n", hash.ToString(), mapBlockIndex[hash]->nHeight);
4008 // Activate the genesis block so normal node progress can continue
4009 if (hash == chainparams.GetConsensus().hashGenesisBlock) {
4010 CValidationState state;
4011 if (!ActivateBestChain(state, chainparams)) {
4012 break;
4016 NotifyHeaderTip();
4018 // Recursively process earlier encountered successors of this block
4019 std::deque<uint256> queue;
4020 queue.push_back(hash);
4021 while (!queue.empty()) {
4022 uint256 head = queue.front();
4023 queue.pop_front();
4024 std::pair<std::multimap<uint256, CDiskBlockPos>::iterator, std::multimap<uint256, CDiskBlockPos>::iterator> range = mapBlocksUnknownParent.equal_range(head);
4025 while (range.first != range.second) {
4026 std::multimap<uint256, CDiskBlockPos>::iterator it = range.first;
4027 std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
4028 if (ReadBlockFromDisk(*pblockrecursive, it->second, chainparams.GetConsensus()))
4030 LogPrint(BCLog::REINDEX, "%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(),
4031 head.ToString());
4032 LOCK(cs_main);
4033 CValidationState dummy;
4034 if (AcceptBlock(pblockrecursive, dummy, chainparams, nullptr, true, &it->second, nullptr))
4036 nLoaded++;
4037 queue.push_back(pblockrecursive->GetHash());
4040 range.first++;
4041 mapBlocksUnknownParent.erase(it);
4042 NotifyHeaderTip();
4045 } catch (const std::exception& e) {
4046 LogPrintf("%s: Deserialize or I/O error - %s\n", __func__, e.what());
4049 } catch (const std::runtime_error& e) {
4050 AbortNode(std::string("System error: ") + e.what());
4052 if (nLoaded > 0)
4053 LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded, GetTimeMillis() - nStart);
4054 return nLoaded > 0;
4057 void static CheckBlockIndex(const Consensus::Params& consensusParams)
4059 if (!fCheckBlockIndex) {
4060 return;
4063 LOCK(cs_main);
4065 // During a reindex, we read the genesis block and call CheckBlockIndex before ActivateBestChain,
4066 // so we have the genesis block in mapBlockIndex but no active chain. (A few of the tests when
4067 // iterating the block tree require that chainActive has been initialized.)
4068 if (chainActive.Height() < 0) {
4069 assert(mapBlockIndex.size() <= 1);
4070 return;
4073 // Build forward-pointing map of the entire block tree.
4074 std::multimap<CBlockIndex*,CBlockIndex*> forward;
4075 for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
4076 forward.insert(std::make_pair(it->second->pprev, it->second));
4079 assert(forward.size() == mapBlockIndex.size());
4081 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeGenesis = forward.equal_range(nullptr);
4082 CBlockIndex *pindex = rangeGenesis.first->second;
4083 rangeGenesis.first++;
4084 assert(rangeGenesis.first == rangeGenesis.second); // There is only one index entry with parent nullptr.
4086 // Iterate over the entire block tree, using depth-first search.
4087 // Along the way, remember whether there are blocks on the path from genesis
4088 // block being explored which are the first to have certain properties.
4089 size_t nNodes = 0;
4090 int nHeight = 0;
4091 CBlockIndex* pindexFirstInvalid = nullptr; // Oldest ancestor of pindex which is invalid.
4092 CBlockIndex* pindexFirstMissing = nullptr; // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA.
4093 CBlockIndex* pindexFirstNeverProcessed = nullptr; // Oldest ancestor of pindex for which nTx == 0.
4094 CBlockIndex* pindexFirstNotTreeValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
4095 CBlockIndex* pindexFirstNotTransactionsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not).
4096 CBlockIndex* pindexFirstNotChainValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not).
4097 CBlockIndex* pindexFirstNotScriptsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not).
4098 while (pindex != nullptr) {
4099 nNodes++;
4100 if (pindexFirstInvalid == nullptr && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
4101 if (pindexFirstMissing == nullptr && !(pindex->nStatus & BLOCK_HAVE_DATA)) pindexFirstMissing = pindex;
4102 if (pindexFirstNeverProcessed == nullptr && pindex->nTx == 0) pindexFirstNeverProcessed = pindex;
4103 if (pindex->pprev != nullptr && pindexFirstNotTreeValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TREE) pindexFirstNotTreeValid = pindex;
4104 if (pindex->pprev != nullptr && pindexFirstNotTransactionsValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TRANSACTIONS) pindexFirstNotTransactionsValid = pindex;
4105 if (pindex->pprev != nullptr && pindexFirstNotChainValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_CHAIN) pindexFirstNotChainValid = pindex;
4106 if (pindex->pprev != nullptr && pindexFirstNotScriptsValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS) pindexFirstNotScriptsValid = pindex;
4108 // Begin: actual consistency checks.
4109 if (pindex->pprev == nullptr) {
4110 // Genesis block checks.
4111 assert(pindex->GetBlockHash() == consensusParams.hashGenesisBlock); // Genesis block's hash must match.
4112 assert(pindex == chainActive.Genesis()); // The current active chain's genesis block must be this block.
4114 if (pindex->nChainTx == 0) assert(pindex->nSequenceId <= 0); // nSequenceId can't be set positive for blocks that aren't linked (negative is used for preciousblock)
4115 // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred).
4116 // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
4117 if (!fHavePruned) {
4118 // If we've never pruned, then HAVE_DATA should be equivalent to nTx > 0
4119 assert(!(pindex->nStatus & BLOCK_HAVE_DATA) == (pindex->nTx == 0));
4120 assert(pindexFirstMissing == pindexFirstNeverProcessed);
4121 } else {
4122 // If we have pruned, then we can only say that HAVE_DATA implies nTx > 0
4123 if (pindex->nStatus & BLOCK_HAVE_DATA) assert(pindex->nTx > 0);
4125 if (pindex->nStatus & BLOCK_HAVE_UNDO) assert(pindex->nStatus & BLOCK_HAVE_DATA);
4126 assert(((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS) == (pindex->nTx > 0)); // This is pruning-independent.
4127 // All parents having had data (at some point) is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to nChainTx being set.
4128 assert((pindexFirstNeverProcessed != nullptr) == (pindex->nChainTx == 0)); // nChainTx != 0 is used to signal that all parent blocks have been processed (but may have been pruned).
4129 assert((pindexFirstNotTransactionsValid != nullptr) == (pindex->nChainTx == 0));
4130 assert(pindex->nHeight == nHeight); // nHeight must be consistent.
4131 assert(pindex->pprev == nullptr || pindex->nChainWork >= pindex->pprev->nChainWork); // For every block except the genesis block, the chainwork must be larger than the parent's.
4132 assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); // The pskip pointer must point back for all but the first 2 blocks.
4133 assert(pindexFirstNotTreeValid == nullptr); // All mapBlockIndex entries must at least be TREE valid
4134 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TREE) assert(pindexFirstNotTreeValid == nullptr); // TREE valid implies all parents are TREE valid
4135 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_CHAIN) assert(pindexFirstNotChainValid == nullptr); // CHAIN valid implies all parents are CHAIN valid
4136 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_SCRIPTS) assert(pindexFirstNotScriptsValid == nullptr); // SCRIPTS valid implies all parents are SCRIPTS valid
4137 if (pindexFirstInvalid == nullptr) {
4138 // Checks for not-invalid blocks.
4139 assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
4141 if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && pindexFirstNeverProcessed == nullptr) {
4142 if (pindexFirstInvalid == nullptr) {
4143 // If this block sorts at least as good as the current tip and
4144 // is valid and we have all data for its parents, it must be in
4145 // setBlockIndexCandidates. chainActive.Tip() must also be there
4146 // even if some data has been pruned.
4147 if (pindexFirstMissing == nullptr || pindex == chainActive.Tip()) {
4148 assert(setBlockIndexCandidates.count(pindex));
4150 // If some parent is missing, then it could be that this block was in
4151 // setBlockIndexCandidates but had to be removed because of the missing data.
4152 // In this case it must be in mapBlocksUnlinked -- see test below.
4154 } else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates.
4155 assert(setBlockIndexCandidates.count(pindex) == 0);
4157 // Check whether this block is in mapBlocksUnlinked.
4158 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked = mapBlocksUnlinked.equal_range(pindex->pprev);
4159 bool foundInUnlinked = false;
4160 while (rangeUnlinked.first != rangeUnlinked.second) {
4161 assert(rangeUnlinked.first->first == pindex->pprev);
4162 if (rangeUnlinked.first->second == pindex) {
4163 foundInUnlinked = true;
4164 break;
4166 rangeUnlinked.first++;
4168 if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed != nullptr && pindexFirstInvalid == nullptr) {
4169 // If this block has block data available, some parent was never received, and has no invalid parents, it must be in mapBlocksUnlinked.
4170 assert(foundInUnlinked);
4172 if (!(pindex->nStatus & BLOCK_HAVE_DATA)) assert(!foundInUnlinked); // Can't be in mapBlocksUnlinked if we don't HAVE_DATA
4173 if (pindexFirstMissing == nullptr) assert(!foundInUnlinked); // We aren't missing data for any parent -- cannot be in mapBlocksUnlinked.
4174 if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed == nullptr && pindexFirstMissing != nullptr) {
4175 // We HAVE_DATA for this block, have received data for all parents at some point, but we're currently missing data for some parent.
4176 assert(fHavePruned); // We must have pruned.
4177 // This block may have entered mapBlocksUnlinked if:
4178 // - it has a descendant that at some point had more work than the
4179 // tip, and
4180 // - we tried switching to that descendant but were missing
4181 // data for some intermediate block between chainActive and the
4182 // tip.
4183 // So if this block is itself better than chainActive.Tip() and it wasn't in
4184 // setBlockIndexCandidates, then it must be in mapBlocksUnlinked.
4185 if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && setBlockIndexCandidates.count(pindex) == 0) {
4186 if (pindexFirstInvalid == nullptr) {
4187 assert(foundInUnlinked);
4191 // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow
4192 // End: actual consistency checks.
4194 // Try descending into the first subnode.
4195 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
4196 if (range.first != range.second) {
4197 // A subnode was found.
4198 pindex = range.first->second;
4199 nHeight++;
4200 continue;
4202 // This is a leaf node.
4203 // Move upwards until we reach a node of which we have not yet visited the last child.
4204 while (pindex) {
4205 // We are going to either move to a parent or a sibling of pindex.
4206 // If pindex was the first with a certain property, unset the corresponding variable.
4207 if (pindex == pindexFirstInvalid) pindexFirstInvalid = nullptr;
4208 if (pindex == pindexFirstMissing) pindexFirstMissing = nullptr;
4209 if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed = nullptr;
4210 if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid = nullptr;
4211 if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid = nullptr;
4212 if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid = nullptr;
4213 if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid = nullptr;
4214 // Find our parent.
4215 CBlockIndex* pindexPar = pindex->pprev;
4216 // Find which child we just visited.
4217 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
4218 while (rangePar.first->second != pindex) {
4219 assert(rangePar.first != rangePar.second); // Our parent must have at least the node we're coming from as child.
4220 rangePar.first++;
4222 // Proceed to the next one.
4223 rangePar.first++;
4224 if (rangePar.first != rangePar.second) {
4225 // Move to the sibling.
4226 pindex = rangePar.first->second;
4227 break;
4228 } else {
4229 // Move up further.
4230 pindex = pindexPar;
4231 nHeight--;
4232 continue;
4237 // Check that we actually traversed the entire map.
4238 assert(nNodes == forward.size());
4241 std::string CBlockFileInfo::ToString() const
4243 return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, DateTimeStrFormat("%Y-%m-%d", nTimeFirst), DateTimeStrFormat("%Y-%m-%d", nTimeLast));
4246 CBlockFileInfo* GetBlockFileInfo(size_t n)
4248 return &vinfoBlockFile.at(n);
4251 ThresholdState VersionBitsTipState(const Consensus::Params& params, Consensus::DeploymentPos pos)
4253 LOCK(cs_main);
4254 return VersionBitsState(chainActive.Tip(), params, pos, versionbitscache);
4257 BIP9Stats VersionBitsTipStatistics(const Consensus::Params& params, Consensus::DeploymentPos pos)
4259 LOCK(cs_main);
4260 return VersionBitsStatistics(chainActive.Tip(), params, pos);
4263 int VersionBitsTipStateSinceHeight(const Consensus::Params& params, Consensus::DeploymentPos pos)
4265 LOCK(cs_main);
4266 return VersionBitsStateSinceHeight(chainActive.Tip(), params, pos, versionbitscache);
4269 static const uint64_t MEMPOOL_DUMP_VERSION = 1;
4271 bool LoadMempool(void)
4273 const CChainParams& chainparams = Params();
4274 int64_t nExpiryTimeout = gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60;
4275 FILE* filestr = fsbridge::fopen(GetDataDir() / "mempool.dat", "rb");
4276 CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
4277 if (file.IsNull()) {
4278 LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
4279 return false;
4282 int64_t count = 0;
4283 int64_t skipped = 0;
4284 int64_t failed = 0;
4285 int64_t nNow = GetTime();
4287 try {
4288 uint64_t version;
4289 file >> version;
4290 if (version != MEMPOOL_DUMP_VERSION) {
4291 return false;
4293 uint64_t num;
4294 file >> num;
4295 while (num--) {
4296 CTransactionRef tx;
4297 int64_t nTime;
4298 int64_t nFeeDelta;
4299 file >> tx;
4300 file >> nTime;
4301 file >> nFeeDelta;
4303 CAmount amountdelta = nFeeDelta;
4304 if (amountdelta) {
4305 mempool.PrioritiseTransaction(tx->GetHash(), amountdelta);
4307 CValidationState state;
4308 if (nTime + nExpiryTimeout > nNow) {
4309 LOCK(cs_main);
4310 AcceptToMemoryPoolWithTime(chainparams, mempool, state, tx, true, nullptr, nTime, nullptr, false, 0);
4311 if (state.IsValid()) {
4312 ++count;
4313 } else {
4314 ++failed;
4316 } else {
4317 ++skipped;
4319 if (ShutdownRequested())
4320 return false;
4322 std::map<uint256, CAmount> mapDeltas;
4323 file >> mapDeltas;
4325 for (const auto& i : mapDeltas) {
4326 mempool.PrioritiseTransaction(i.first, i.second);
4328 } catch (const std::exception& e) {
4329 LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
4330 return false;
4333 LogPrintf("Imported mempool transactions from disk: %i successes, %i failed, %i expired\n", count, failed, skipped);
4334 return true;
4337 void DumpMempool(void)
4339 int64_t start = GetTimeMicros();
4341 std::map<uint256, CAmount> mapDeltas;
4342 std::vector<TxMempoolInfo> vinfo;
4345 LOCK(mempool.cs);
4346 for (const auto &i : mempool.mapDeltas) {
4347 mapDeltas[i.first] = i.second;
4349 vinfo = mempool.infoAll();
4352 int64_t mid = GetTimeMicros();
4354 try {
4355 FILE* filestr = fsbridge::fopen(GetDataDir() / "mempool.dat.new", "wb");
4356 if (!filestr) {
4357 return;
4360 CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
4362 uint64_t version = MEMPOOL_DUMP_VERSION;
4363 file << version;
4365 file << (uint64_t)vinfo.size();
4366 for (const auto& i : vinfo) {
4367 file << *(i.tx);
4368 file << (int64_t)i.nTime;
4369 file << (int64_t)i.nFeeDelta;
4370 mapDeltas.erase(i.tx->GetHash());
4373 file << mapDeltas;
4374 FileCommit(file.Get());
4375 file.fclose();
4376 RenameOver(GetDataDir() / "mempool.dat.new", GetDataDir() / "mempool.dat");
4377 int64_t last = GetTimeMicros();
4378 LogPrintf("Dumped mempool: %gs to copy, %gs to dump\n", (mid-start)*MICRO, (last-mid)*MICRO);
4379 } catch (const std::exception& e) {
4380 LogPrintf("Failed to dump mempool: %s. Continuing anyway.\n", e.what());
4384 //! Guess how far we are in the verification process at the given block index
4385 double GuessVerificationProgress(const ChainTxData& data, CBlockIndex *pindex) {
4386 if (pindex == nullptr)
4387 return 0.0;
4389 int64_t nNow = time(nullptr);
4391 double fTxTotal;
4393 if (pindex->nChainTx <= data.nTxCount) {
4394 fTxTotal = data.nTxCount + (nNow - data.nTime) * data.dTxRate;
4395 } else {
4396 fTxTotal = pindex->nChainTx + (nNow - pindex->GetBlockTime()) * data.dTxRate;
4399 return pindex->nChainTx / fTxTotal;
4402 class CMainCleanup
4404 public:
4405 CMainCleanup() {}
4406 ~CMainCleanup() {
4407 // block headers
4408 BlockMap::iterator it1 = mapBlockIndex.begin();
4409 for (; it1 != mapBlockIndex.end(); it1++)
4410 delete (*it1).second;
4411 mapBlockIndex.clear();
4413 } instance_of_cmaincleanup;