[trivial] Fix recently introduced typos in comments
[bitcoinplatinum.git] / src / validation.cpp
blob144b5d5c0cf5daba451ed4bec0fadb453c693619
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 "chainparams.h"
10 #include "checkpoints.h"
11 #include "checkqueue.h"
12 #include "consensus/consensus.h"
13 #include "consensus/merkle.h"
14 #include "consensus/validation.h"
15 #include "hash.h"
16 #include "init.h"
17 #include "policy/fees.h"
18 #include "policy/policy.h"
19 #include "pow.h"
20 #include "primitives/block.h"
21 #include "primitives/transaction.h"
22 #include "random.h"
23 #include "script/script.h"
24 #include "script/sigcache.h"
25 #include "script/standard.h"
26 #include "timedata.h"
27 #include "tinyformat.h"
28 #include "txdb.h"
29 #include "txmempool.h"
30 #include "ui_interface.h"
31 #include "undo.h"
32 #include "util.h"
33 #include "utilmoneystr.h"
34 #include "utilstrencodings.h"
35 #include "validationinterface.h"
36 #include "versionbits.h"
37 #include "warnings.h"
39 #include <atomic>
40 #include <sstream>
42 #include <boost/algorithm/string/replace.hpp>
43 #include <boost/algorithm/string/join.hpp>
44 #include <boost/filesystem.hpp>
45 #include <boost/filesystem/fstream.hpp>
46 #include <boost/math/distributions/poisson.hpp>
47 #include <boost/thread.hpp>
49 #if defined(NDEBUG)
50 # error "Bitcoin cannot be compiled without assertions."
51 #endif
53 /**
54 * Global state
57 CCriticalSection cs_main;
59 BlockMap mapBlockIndex;
60 CChain chainActive;
61 CBlockIndex *pindexBestHeader = NULL;
62 CWaitableCriticalSection csBestBlock;
63 CConditionVariable cvBlockChange;
64 int nScriptCheckThreads = 0;
65 std::atomic_bool fImporting(false);
66 bool fReindex = false;
67 bool fTxIndex = false;
68 bool fHavePruned = false;
69 bool fPruneMode = false;
70 bool fIsBareMultisigStd = DEFAULT_PERMIT_BAREMULTISIG;
71 bool fRequireStandard = true;
72 bool fCheckBlockIndex = false;
73 bool fCheckpointsEnabled = DEFAULT_CHECKPOINTS_ENABLED;
74 size_t nCoinCacheUsage = 5000 * 300;
75 uint64_t nPruneTarget = 0;
76 int64_t nMaxTipAge = DEFAULT_MAX_TIP_AGE;
77 bool fEnableReplacement = DEFAULT_ENABLE_REPLACEMENT;
79 uint256 hashAssumeValid;
81 CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
82 CAmount maxTxFee = DEFAULT_TRANSACTION_MAXFEE;
84 CTxMemPool mempool(::minRelayTxFee);
86 static void CheckBlockIndex(const Consensus::Params& consensusParams);
88 /** Constant stuff for coinbase transactions we create: */
89 CScript COINBASE_FLAGS;
91 const std::string strMessageMagic = "Bitcoin Signed Message:\n";
93 // Internal stuff
94 namespace {
96 struct CBlockIndexWorkComparator
98 bool operator()(CBlockIndex *pa, CBlockIndex *pb) const {
99 // First sort by most total work, ...
100 if (pa->nChainWork > pb->nChainWork) return false;
101 if (pa->nChainWork < pb->nChainWork) return true;
103 // ... then by earliest time received, ...
104 if (pa->nSequenceId < pb->nSequenceId) return false;
105 if (pa->nSequenceId > pb->nSequenceId) return true;
107 // Use pointer address as tie breaker (should only happen with blocks
108 // loaded from disk, as those all have id 0).
109 if (pa < pb) return false;
110 if (pa > pb) return true;
112 // Identical blocks.
113 return false;
117 CBlockIndex *pindexBestInvalid;
120 * The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS (for itself and all ancestors) and
121 * as good as our current tip or better. Entries may be failed, though, and pruning nodes may be
122 * missing the data for the block.
124 std::set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexCandidates;
125 /** All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
126 * Pruned nodes may have entries where B is missing data.
128 std::multimap<CBlockIndex*, CBlockIndex*> mapBlocksUnlinked;
130 CCriticalSection cs_LastBlockFile;
131 std::vector<CBlockFileInfo> vinfoBlockFile;
132 int nLastBlockFile = 0;
133 /** Global flag to indicate we should check to see if there are
134 * block/undo files that should be deleted. Set on startup
135 * or if we allocate more file space when we're in prune mode
137 bool fCheckForPruning = false;
140 * Every received block is assigned a unique and increasing identifier, so we
141 * know which one to give priority in case of a fork.
143 CCriticalSection cs_nBlockSequenceId;
144 /** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
145 int32_t nBlockSequenceId = 1;
146 /** Decreasing counter (used by subsequent preciousblock calls). */
147 int32_t nBlockReverseSequenceId = -1;
148 /** chainwork for the last block that preciousblock has been applied to. */
149 arith_uint256 nLastPreciousChainwork = 0;
151 /** Dirty block index entries. */
152 std::set<CBlockIndex*> setDirtyBlockIndex;
154 /** Dirty block file entries. */
155 std::set<int> setDirtyFileInfo;
156 } // anon namespace
158 /* Use this class to start tracking transactions that are removed from the
159 * mempool and pass all those transactions through SyncTransaction when the
160 * object goes out of scope. This is currently only used to call SyncTransaction
161 * on conflicts removed from the mempool during block connection. Applied in
162 * ActivateBestChain around ActivateBestStep which in turn calls:
163 * ConnectTip->removeForBlock->removeConflicts
165 class MemPoolConflictRemovalTracker
167 private:
168 std::vector<CTransactionRef> conflictedTxs;
169 CTxMemPool &pool;
171 public:
172 MemPoolConflictRemovalTracker(CTxMemPool &_pool) : pool(_pool) {
173 pool.NotifyEntryRemoved.connect(boost::bind(&MemPoolConflictRemovalTracker::NotifyEntryRemoved, this, _1, _2));
176 void NotifyEntryRemoved(CTransactionRef txRemoved, MemPoolRemovalReason reason) {
177 if (reason == MemPoolRemovalReason::CONFLICT) {
178 conflictedTxs.push_back(txRemoved);
182 ~MemPoolConflictRemovalTracker() {
183 pool.NotifyEntryRemoved.disconnect(boost::bind(&MemPoolConflictRemovalTracker::NotifyEntryRemoved, this, _1, _2));
184 for (const auto& tx : conflictedTxs) {
185 GetMainSignals().SyncTransaction(*tx, NULL, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK);
187 conflictedTxs.clear();
191 CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator)
193 // Find the first block the caller has in the main chain
194 BOOST_FOREACH(const uint256& hash, locator.vHave) {
195 BlockMap::iterator mi = mapBlockIndex.find(hash);
196 if (mi != mapBlockIndex.end())
198 CBlockIndex* pindex = (*mi).second;
199 if (chain.Contains(pindex))
200 return pindex;
201 if (pindex->GetAncestor(chain.Height()) == chain.Tip()) {
202 return chain.Tip();
206 return chain.Genesis();
209 CCoinsViewCache *pcoinsTip = NULL;
210 CBlockTreeDB *pblocktree = NULL;
212 enum FlushStateMode {
213 FLUSH_STATE_NONE,
214 FLUSH_STATE_IF_NEEDED,
215 FLUSH_STATE_PERIODIC,
216 FLUSH_STATE_ALWAYS
219 // See definition for documentation
220 bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode, int nManualPruneHeight=0);
221 void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight);
223 bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
225 if (tx.nLockTime == 0)
226 return true;
227 if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
228 return true;
229 for (const auto& txin : tx.vin) {
230 if (!(txin.nSequence == CTxIn::SEQUENCE_FINAL))
231 return false;
233 return true;
236 bool CheckFinalTx(const CTransaction &tx, int flags)
238 AssertLockHeld(cs_main);
240 // By convention a negative value for flags indicates that the
241 // current network-enforced consensus rules should be used. In
242 // a future soft-fork scenario that would mean checking which
243 // rules would be enforced for the next block and setting the
244 // appropriate flags. At the present time no soft-forks are
245 // scheduled, so no flags are set.
246 flags = std::max(flags, 0);
248 // CheckFinalTx() uses chainActive.Height()+1 to evaluate
249 // nLockTime because when IsFinalTx() is called within
250 // CBlock::AcceptBlock(), the height of the block *being*
251 // evaluated is what is used. Thus if we want to know if a
252 // transaction can be part of the *next* block, we need to call
253 // IsFinalTx() with one more than chainActive.Height().
254 const int nBlockHeight = chainActive.Height() + 1;
256 // BIP113 will require that time-locked transactions have nLockTime set to
257 // less than the median time of the previous block they're contained in.
258 // When the next block is created its previous block will be the current
259 // chain tip, so we use that to calculate the median time passed to
260 // IsFinalTx() if LOCKTIME_MEDIAN_TIME_PAST is set.
261 const int64_t nBlockTime = (flags & LOCKTIME_MEDIAN_TIME_PAST)
262 ? chainActive.Tip()->GetMedianTimePast()
263 : GetAdjustedTime();
265 return IsFinalTx(tx, nBlockHeight, nBlockTime);
269 * Calculates the block height and previous block's median time past at
270 * which the transaction will be considered final in the context of BIP 68.
271 * Also removes from the vector of input heights any entries which did not
272 * correspond to sequence locked inputs as they do not affect the calculation.
274 static std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector<int>* prevHeights, const CBlockIndex& block)
276 assert(prevHeights->size() == tx.vin.size());
278 // Will be set to the equivalent height- and time-based nLockTime
279 // values that would be necessary to satisfy all relative lock-
280 // time constraints given our view of block chain history.
281 // The semantics of nLockTime are the last invalid height/time, so
282 // use -1 to have the effect of any height or time being valid.
283 int nMinHeight = -1;
284 int64_t nMinTime = -1;
286 // tx.nVersion is signed integer so requires cast to unsigned otherwise
287 // we would be doing a signed comparison and half the range of nVersion
288 // wouldn't support BIP 68.
289 bool fEnforceBIP68 = static_cast<uint32_t>(tx.nVersion) >= 2
290 && flags & LOCKTIME_VERIFY_SEQUENCE;
292 // Do not enforce sequence numbers as a relative lock time
293 // unless we have been instructed to
294 if (!fEnforceBIP68) {
295 return std::make_pair(nMinHeight, nMinTime);
298 for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
299 const CTxIn& txin = tx.vin[txinIndex];
301 // Sequence numbers with the most significant bit set are not
302 // treated as relative lock-times, nor are they given any
303 // consensus-enforced meaning at this point.
304 if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) {
305 // The height of this input is not relevant for sequence locks
306 (*prevHeights)[txinIndex] = 0;
307 continue;
310 int nCoinHeight = (*prevHeights)[txinIndex];
312 if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) {
313 int64_t nCoinTime = block.GetAncestor(std::max(nCoinHeight-1, 0))->GetMedianTimePast();
314 // NOTE: Subtract 1 to maintain nLockTime semantics
315 // BIP 68 relative lock times have the semantics of calculating
316 // the first block or time at which the transaction would be
317 // valid. When calculating the effective block time or height
318 // for the entire transaction, we switch to using the
319 // semantics of nLockTime which is the last invalid block
320 // time or height. Thus we subtract 1 from the calculated
321 // time or height.
323 // Time-based relative lock-times are measured from the
324 // smallest allowed timestamp of the block containing the
325 // txout being spent, which is the median time past of the
326 // block prior.
327 nMinTime = std::max(nMinTime, nCoinTime + (int64_t)((txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) << CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) - 1);
328 } else {
329 nMinHeight = std::max(nMinHeight, nCoinHeight + (int)(txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) - 1);
333 return std::make_pair(nMinHeight, nMinTime);
336 static bool EvaluateSequenceLocks(const CBlockIndex& block, std::pair<int, int64_t> lockPair)
338 assert(block.pprev);
339 int64_t nBlockTime = block.pprev->GetMedianTimePast();
340 if (lockPair.first >= block.nHeight || lockPair.second >= nBlockTime)
341 return false;
343 return true;
346 bool SequenceLocks(const CTransaction &tx, int flags, std::vector<int>* prevHeights, const CBlockIndex& block)
348 return EvaluateSequenceLocks(block, CalculateSequenceLocks(tx, flags, prevHeights, block));
351 bool TestLockPointValidity(const LockPoints* lp)
353 AssertLockHeld(cs_main);
354 assert(lp);
355 // If there are relative lock times then the maxInputBlock will be set
356 // If there are no relative lock times, the LockPoints don't depend on the chain
357 if (lp->maxInputBlock) {
358 // Check whether chainActive is an extension of the block at which the LockPoints
359 // calculation was valid. If not LockPoints are no longer valid
360 if (!chainActive.Contains(lp->maxInputBlock)) {
361 return false;
365 // LockPoints still valid
366 return true;
369 bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool useExistingLockPoints)
371 AssertLockHeld(cs_main);
372 AssertLockHeld(mempool.cs);
374 CBlockIndex* tip = chainActive.Tip();
375 CBlockIndex index;
376 index.pprev = tip;
377 // CheckSequenceLocks() uses chainActive.Height()+1 to evaluate
378 // height based locks because when SequenceLocks() is called within
379 // ConnectBlock(), the height of the block *being*
380 // evaluated is what is used.
381 // Thus if we want to know if a transaction can be part of the
382 // *next* block, we need to use one more than chainActive.Height()
383 index.nHeight = tip->nHeight + 1;
385 std::pair<int, int64_t> lockPair;
386 if (useExistingLockPoints) {
387 assert(lp);
388 lockPair.first = lp->height;
389 lockPair.second = lp->time;
391 else {
392 // pcoinsTip contains the UTXO set for chainActive.Tip()
393 CCoinsViewMemPool viewMemPool(pcoinsTip, mempool);
394 std::vector<int> prevheights;
395 prevheights.resize(tx.vin.size());
396 for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
397 const CTxIn& txin = tx.vin[txinIndex];
398 CCoins coins;
399 if (!viewMemPool.GetCoins(txin.prevout.hash, coins)) {
400 return error("%s: Missing input", __func__);
402 if (coins.nHeight == MEMPOOL_HEIGHT) {
403 // Assume all mempool transaction confirm in the next block
404 prevheights[txinIndex] = tip->nHeight + 1;
405 } else {
406 prevheights[txinIndex] = coins.nHeight;
409 lockPair = CalculateSequenceLocks(tx, flags, &prevheights, index);
410 if (lp) {
411 lp->height = lockPair.first;
412 lp->time = lockPair.second;
413 // Also store the hash of the block with the highest height of
414 // all the blocks which have sequence locked prevouts.
415 // This hash needs to still be on the chain
416 // for these LockPoint calculations to be valid
417 // Note: It is impossible to correctly calculate a maxInputBlock
418 // if any of the sequence locked inputs depend on unconfirmed txs,
419 // except in the special case where the relative lock time/height
420 // is 0, which is equivalent to no sequence lock. Since we assume
421 // input height of tip+1 for mempool txs and test the resulting
422 // lockPair from CalculateSequenceLocks against tip+1. We know
423 // EvaluateSequenceLocks will fail if there was a non-zero sequence
424 // lock on a mempool input, so we can use the return value of
425 // CheckSequenceLocks to indicate the LockPoints validity
426 int maxInputHeight = 0;
427 BOOST_FOREACH(int height, prevheights) {
428 // Can ignore mempool inputs since we'll fail if they had non-zero locks
429 if (height != tip->nHeight+1) {
430 maxInputHeight = std::max(maxInputHeight, height);
433 lp->maxInputBlock = tip->GetAncestor(maxInputHeight);
436 return EvaluateSequenceLocks(index, lockPair);
440 unsigned int GetLegacySigOpCount(const CTransaction& tx)
442 unsigned int nSigOps = 0;
443 for (const auto& txin : tx.vin)
445 nSigOps += txin.scriptSig.GetSigOpCount(false);
447 for (const auto& txout : tx.vout)
449 nSigOps += txout.scriptPubKey.GetSigOpCount(false);
451 return nSigOps;
454 unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs)
456 if (tx.IsCoinBase())
457 return 0;
459 unsigned int nSigOps = 0;
460 for (unsigned int i = 0; i < tx.vin.size(); i++)
462 const CTxOut &prevout = inputs.GetOutputFor(tx.vin[i]);
463 if (prevout.scriptPubKey.IsPayToScriptHash())
464 nSigOps += prevout.scriptPubKey.GetSigOpCount(tx.vin[i].scriptSig);
466 return nSigOps;
469 int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& inputs, int flags)
471 int64_t nSigOps = GetLegacySigOpCount(tx) * WITNESS_SCALE_FACTOR;
473 if (tx.IsCoinBase())
474 return nSigOps;
476 if (flags & SCRIPT_VERIFY_P2SH) {
477 nSigOps += GetP2SHSigOpCount(tx, inputs) * WITNESS_SCALE_FACTOR;
480 for (unsigned int i = 0; i < tx.vin.size(); i++)
482 const CTxOut &prevout = inputs.GetOutputFor(tx.vin[i]);
483 nSigOps += CountWitnessSigOps(tx.vin[i].scriptSig, prevout.scriptPubKey, &tx.vin[i].scriptWitness, flags);
485 return nSigOps;
492 bool CheckTransaction(const CTransaction& tx, CValidationState &state, bool fCheckDuplicateInputs)
494 // Basic checks that don't depend on any context
495 if (tx.vin.empty())
496 return state.DoS(10, false, REJECT_INVALID, "bad-txns-vin-empty");
497 if (tx.vout.empty())
498 return state.DoS(10, false, REJECT_INVALID, "bad-txns-vout-empty");
499 // Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)
500 if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) > MAX_BLOCK_BASE_SIZE)
501 return state.DoS(100, false, REJECT_INVALID, "bad-txns-oversize");
503 // Check for negative or overflow output values
504 CAmount nValueOut = 0;
505 for (const auto& txout : tx.vout)
507 if (txout.nValue < 0)
508 return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-negative");
509 if (txout.nValue > MAX_MONEY)
510 return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-toolarge");
511 nValueOut += txout.nValue;
512 if (!MoneyRange(nValueOut))
513 return state.DoS(100, false, REJECT_INVALID, "bad-txns-txouttotal-toolarge");
516 // Check for duplicate inputs - note that this check is slow so we skip it in CheckBlock
517 if (fCheckDuplicateInputs) {
518 std::set<COutPoint> vInOutPoints;
519 for (const auto& txin : tx.vin)
521 if (!vInOutPoints.insert(txin.prevout).second)
522 return state.DoS(100, false, REJECT_INVALID, "bad-txns-inputs-duplicate");
526 if (tx.IsCoinBase())
528 if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
529 return state.DoS(100, false, REJECT_INVALID, "bad-cb-length");
531 else
533 for (const auto& txin : tx.vin)
534 if (txin.prevout.IsNull())
535 return state.DoS(10, false, REJECT_INVALID, "bad-txns-prevout-null");
538 return true;
541 void LimitMempoolSize(CTxMemPool& pool, size_t limit, unsigned long age) {
542 int expired = pool.Expire(GetTime() - age);
543 if (expired != 0)
544 LogPrint("mempool", "Expired %i transactions from the memory pool\n", expired);
546 std::vector<uint256> vNoSpendsRemaining;
547 pool.TrimToSize(limit, &vNoSpendsRemaining);
548 BOOST_FOREACH(const uint256& removed, vNoSpendsRemaining)
549 pcoinsTip->Uncache(removed);
552 /** Convert CValidationState to a human-readable message for logging */
553 std::string FormatStateMessage(const CValidationState &state)
555 return strprintf("%s%s (code %i)",
556 state.GetRejectReason(),
557 state.GetDebugMessage().empty() ? "" : ", "+state.GetDebugMessage(),
558 state.GetRejectCode());
561 static bool IsCurrentForFeeEstimation()
563 AssertLockHeld(cs_main);
564 if (IsInitialBlockDownload())
565 return false;
566 if (chainActive.Tip()->GetBlockTime() < (GetTime() - MAX_FEE_ESTIMATION_TIP_AGE))
567 return false;
568 if (chainActive.Height() < pindexBestHeader->nHeight - 1)
569 return false;
570 return true;
573 bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const CTransactionRef& ptx, bool fLimitFree,
574 bool* pfMissingInputs, int64_t nAcceptTime, std::list<CTransactionRef>* plTxnReplaced,
575 bool fOverrideMempoolLimit, const CAmount& nAbsurdFee, std::vector<uint256>& vHashTxnToUncache)
577 const CTransaction& tx = *ptx;
578 const uint256 hash = tx.GetHash();
579 AssertLockHeld(cs_main);
580 if (pfMissingInputs)
581 *pfMissingInputs = false;
583 if (!CheckTransaction(tx, state))
584 return false; // state filled in by CheckTransaction
586 // Coinbase is only valid in a block, not as a loose transaction
587 if (tx.IsCoinBase())
588 return state.DoS(100, false, REJECT_INVALID, "coinbase");
590 // Reject transactions with witness before segregated witness activates (override with -prematurewitness)
591 bool witnessEnabled = IsWitnessEnabled(chainActive.Tip(), Params().GetConsensus());
592 if (!GetBoolArg("-prematurewitness",false) && tx.HasWitness() && !witnessEnabled) {
593 return state.DoS(0, false, REJECT_NONSTANDARD, "no-witness-yet", true);
596 // Rather not work on nonstandard transactions (unless -testnet/-regtest)
597 std::string reason;
598 if (fRequireStandard && !IsStandardTx(tx, reason, witnessEnabled))
599 return state.DoS(0, false, REJECT_NONSTANDARD, reason);
601 // Only accept nLockTime-using transactions that can be mined in the next
602 // block; we don't want our mempool filled up with transactions that can't
603 // be mined yet.
604 if (!CheckFinalTx(tx, STANDARD_LOCKTIME_VERIFY_FLAGS))
605 return state.DoS(0, false, REJECT_NONSTANDARD, "non-final");
607 // is it already in the memory pool?
608 if (pool.exists(hash))
609 return state.Invalid(false, REJECT_ALREADY_KNOWN, "txn-already-in-mempool");
611 // Check for conflicts with in-memory transactions
612 std::set<uint256> setConflicts;
614 LOCK(pool.cs); // protect pool.mapNextTx
615 BOOST_FOREACH(const CTxIn &txin, tx.vin)
617 auto itConflicting = pool.mapNextTx.find(txin.prevout);
618 if (itConflicting != pool.mapNextTx.end())
620 const CTransaction *ptxConflicting = itConflicting->second;
621 if (!setConflicts.count(ptxConflicting->GetHash()))
623 // Allow opt-out of transaction replacement by setting
624 // nSequence >= maxint-1 on all inputs.
626 // maxint-1 is picked to still allow use of nLockTime by
627 // non-replaceable transactions. All inputs rather than just one
628 // is for the sake of multi-party protocols, where we don't
629 // want a single party to be able to disable replacement.
631 // The opt-out ignores descendants as anyone relying on
632 // first-seen mempool behavior should be checking all
633 // unconfirmed ancestors anyway; doing otherwise is hopelessly
634 // insecure.
635 bool fReplacementOptOut = true;
636 if (fEnableReplacement)
638 BOOST_FOREACH(const CTxIn &_txin, ptxConflicting->vin)
640 if (_txin.nSequence < std::numeric_limits<unsigned int>::max()-1)
642 fReplacementOptOut = false;
643 break;
647 if (fReplacementOptOut)
648 return state.Invalid(false, REJECT_CONFLICT, "txn-mempool-conflict");
650 setConflicts.insert(ptxConflicting->GetHash());
657 CCoinsView dummy;
658 CCoinsViewCache view(&dummy);
660 CAmount nValueIn = 0;
661 LockPoints lp;
663 LOCK(pool.cs);
664 CCoinsViewMemPool viewMemPool(pcoinsTip, pool);
665 view.SetBackend(viewMemPool);
667 // do we already have it?
668 bool fHadTxInCache = pcoinsTip->HaveCoinsInCache(hash);
669 if (view.HaveCoins(hash)) {
670 if (!fHadTxInCache)
671 vHashTxnToUncache.push_back(hash);
672 return state.Invalid(false, REJECT_ALREADY_KNOWN, "txn-already-known");
675 // do all inputs exist?
676 // Note that this does not check for the presence of actual outputs (see the next check for that),
677 // and only helps with filling in pfMissingInputs (to determine missing vs spent).
678 BOOST_FOREACH(const CTxIn txin, tx.vin) {
679 if (!pcoinsTip->HaveCoinsInCache(txin.prevout.hash))
680 vHashTxnToUncache.push_back(txin.prevout.hash);
681 if (!view.HaveCoins(txin.prevout.hash)) {
682 if (pfMissingInputs)
683 *pfMissingInputs = true;
684 return false; // fMissingInputs and !state.IsInvalid() is used to detect this condition, don't set state.Invalid()
688 // are the actual inputs available?
689 if (!view.HaveInputs(tx))
690 return state.Invalid(false, REJECT_DUPLICATE, "bad-txns-inputs-spent");
692 // Bring the best block into scope
693 view.GetBestBlock();
695 nValueIn = view.GetValueIn(tx);
697 // we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool
698 view.SetBackend(dummy);
700 // Only accept BIP68 sequence locked transactions that can be mined in the next
701 // block; we don't want our mempool filled up with transactions that can't
702 // be mined yet.
703 // Must keep pool.cs for this unless we change CheckSequenceLocks to take a
704 // CoinsViewCache instead of create its own
705 if (!CheckSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, &lp))
706 return state.DoS(0, false, REJECT_NONSTANDARD, "non-BIP68-final");
709 // Check for non-standard pay-to-script-hash in inputs
710 if (fRequireStandard && !AreInputsStandard(tx, view))
711 return state.Invalid(false, REJECT_NONSTANDARD, "bad-txns-nonstandard-inputs");
713 // Check for non-standard witness in P2WSH
714 if (tx.HasWitness() && fRequireStandard && !IsWitnessStandard(tx, view))
715 return state.DoS(0, false, REJECT_NONSTANDARD, "bad-witness-nonstandard", true);
717 int64_t nSigOpsCost = GetTransactionSigOpCost(tx, view, STANDARD_SCRIPT_VERIFY_FLAGS);
719 CAmount nValueOut = tx.GetValueOut();
720 CAmount nFees = nValueIn-nValueOut;
721 // nModifiedFees includes any fee deltas from PrioritiseTransaction
722 CAmount nModifiedFees = nFees;
723 double nPriorityDummy = 0;
724 pool.ApplyDeltas(hash, nPriorityDummy, nModifiedFees);
726 CAmount inChainInputValue;
727 double dPriority = view.GetPriority(tx, chainActive.Height(), inChainInputValue);
729 // Keep track of transactions that spend a coinbase, which we re-scan
730 // during reorgs to ensure COINBASE_MATURITY is still met.
731 bool fSpendsCoinbase = false;
732 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
733 const CCoins *coins = view.AccessCoins(txin.prevout.hash);
734 if (coins->IsCoinBase()) {
735 fSpendsCoinbase = true;
736 break;
740 CTxMemPoolEntry entry(ptx, nFees, nAcceptTime, dPriority, chainActive.Height(),
741 inChainInputValue, fSpendsCoinbase, nSigOpsCost, lp);
742 unsigned int nSize = entry.GetTxSize();
744 // Check that the transaction doesn't have an excessive number of
745 // sigops, making it impossible to mine. Since the coinbase transaction
746 // itself can contain sigops MAX_STANDARD_TX_SIGOPS is less than
747 // MAX_BLOCK_SIGOPS; we still consider this an invalid rather than
748 // merely non-standard transaction.
749 if (nSigOpsCost > MAX_STANDARD_TX_SIGOPS_COST)
750 return state.DoS(0, false, REJECT_NONSTANDARD, "bad-txns-too-many-sigops", false,
751 strprintf("%d", nSigOpsCost));
753 CAmount mempoolRejectFee = pool.GetMinFee(GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFee(nSize);
754 if (mempoolRejectFee > 0 && nModifiedFees < mempoolRejectFee) {
755 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "mempool min fee not met", false, strprintf("%d < %d", nFees, mempoolRejectFee));
756 } else if (GetBoolArg("-relaypriority", DEFAULT_RELAYPRIORITY) && nModifiedFees < ::minRelayTxFee.GetFee(nSize) && !AllowFree(entry.GetPriority(chainActive.Height() + 1))) {
757 // Require that free transactions have sufficient priority to be mined in the next block.
758 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "insufficient priority");
761 // Continuously rate-limit free (really, very-low-fee) transactions
762 // This mitigates 'penny-flooding' -- sending thousands of free transactions just to
763 // be annoying or make others' transactions take longer to confirm.
764 if (fLimitFree && nModifiedFees < ::minRelayTxFee.GetFee(nSize))
766 static CCriticalSection csFreeLimiter;
767 static double dFreeCount;
768 static int64_t nLastTime;
769 int64_t nNow = GetTime();
771 LOCK(csFreeLimiter);
773 // Use an exponentially decaying ~10-minute window:
774 dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime));
775 nLastTime = nNow;
776 // -limitfreerelay unit is thousand-bytes-per-minute
777 // At default rate it would take over a month to fill 1GB
778 if (dFreeCount + nSize >= GetArg("-limitfreerelay", DEFAULT_LIMITFREERELAY) * 10 * 1000)
779 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "rate limited free transaction");
780 LogPrint("mempool", "Rate limit dFreeCount: %g => %g\n", dFreeCount, dFreeCount+nSize);
781 dFreeCount += nSize;
784 if (nAbsurdFee && nFees > nAbsurdFee)
785 return state.Invalid(false,
786 REJECT_HIGHFEE, "absurdly-high-fee",
787 strprintf("%d > %d", nFees, nAbsurdFee));
789 // Calculate in-mempool ancestors, up to a limit.
790 CTxMemPool::setEntries setAncestors;
791 size_t nLimitAncestors = GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT);
792 size_t nLimitAncestorSize = GetArg("-limitancestorsize", DEFAULT_ANCESTOR_SIZE_LIMIT)*1000;
793 size_t nLimitDescendants = GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT);
794 size_t nLimitDescendantSize = GetArg("-limitdescendantsize", DEFAULT_DESCENDANT_SIZE_LIMIT)*1000;
795 std::string errString;
796 if (!pool.CalculateMemPoolAncestors(entry, setAncestors, nLimitAncestors, nLimitAncestorSize, nLimitDescendants, nLimitDescendantSize, errString)) {
797 return state.DoS(0, false, REJECT_NONSTANDARD, "too-long-mempool-chain", false, errString);
800 // A transaction that spends outputs that would be replaced by it is invalid. Now
801 // that we have the set of all ancestors we can detect this
802 // pathological case by making sure setConflicts and setAncestors don't
803 // intersect.
804 BOOST_FOREACH(CTxMemPool::txiter ancestorIt, setAncestors)
806 const uint256 &hashAncestor = ancestorIt->GetTx().GetHash();
807 if (setConflicts.count(hashAncestor))
809 return state.DoS(10, false,
810 REJECT_INVALID, "bad-txns-spends-conflicting-tx", false,
811 strprintf("%s spends conflicting transaction %s",
812 hash.ToString(),
813 hashAncestor.ToString()));
817 // Check if it's economically rational to mine this transaction rather
818 // than the ones it replaces.
819 CAmount nConflictingFees = 0;
820 size_t nConflictingSize = 0;
821 uint64_t nConflictingCount = 0;
822 CTxMemPool::setEntries allConflicting;
824 // If we don't hold the lock allConflicting might be incomplete; the
825 // subsequent RemoveStaged() and addUnchecked() calls don't guarantee
826 // mempool consistency for us.
827 LOCK(pool.cs);
828 const bool fReplacementTransaction = setConflicts.size();
829 if (fReplacementTransaction)
831 CFeeRate newFeeRate(nModifiedFees, nSize);
832 std::set<uint256> setConflictsParents;
833 const int maxDescendantsToVisit = 100;
834 CTxMemPool::setEntries setIterConflicting;
835 BOOST_FOREACH(const uint256 &hashConflicting, setConflicts)
837 CTxMemPool::txiter mi = pool.mapTx.find(hashConflicting);
838 if (mi == pool.mapTx.end())
839 continue;
841 // Save these to avoid repeated lookups
842 setIterConflicting.insert(mi);
844 // Don't allow the replacement to reduce the feerate of the
845 // mempool.
847 // We usually don't want to accept replacements with lower
848 // feerates than what they replaced as that would lower the
849 // feerate of the next block. Requiring that the feerate always
850 // be increased is also an easy-to-reason about way to prevent
851 // DoS attacks via replacements.
853 // The mining code doesn't (currently) take children into
854 // account (CPFP) so we only consider the feerates of
855 // transactions being directly replaced, not their indirect
856 // descendants. While that does mean high feerate children are
857 // ignored when deciding whether or not to replace, we do
858 // require the replacement to pay more overall fees too,
859 // mitigating most cases.
860 CFeeRate oldFeeRate(mi->GetModifiedFee(), mi->GetTxSize());
861 if (newFeeRate <= oldFeeRate)
863 return state.DoS(0, false,
864 REJECT_INSUFFICIENTFEE, "insufficient fee", false,
865 strprintf("rejecting replacement %s; new feerate %s <= old feerate %s",
866 hash.ToString(),
867 newFeeRate.ToString(),
868 oldFeeRate.ToString()));
871 BOOST_FOREACH(const CTxIn &txin, mi->GetTx().vin)
873 setConflictsParents.insert(txin.prevout.hash);
876 nConflictingCount += mi->GetCountWithDescendants();
878 // This potentially overestimates the number of actual descendants
879 // but we just want to be conservative to avoid doing too much
880 // work.
881 if (nConflictingCount <= maxDescendantsToVisit) {
882 // If not too many to replace, then calculate the set of
883 // transactions that would have to be evicted
884 BOOST_FOREACH(CTxMemPool::txiter it, setIterConflicting) {
885 pool.CalculateDescendants(it, allConflicting);
887 BOOST_FOREACH(CTxMemPool::txiter it, allConflicting) {
888 nConflictingFees += it->GetModifiedFee();
889 nConflictingSize += it->GetTxSize();
891 } else {
892 return state.DoS(0, false,
893 REJECT_NONSTANDARD, "too many potential replacements", false,
894 strprintf("rejecting replacement %s; too many potential replacements (%d > %d)\n",
895 hash.ToString(),
896 nConflictingCount,
897 maxDescendantsToVisit));
900 for (unsigned int j = 0; j < tx.vin.size(); j++)
902 // We don't want to accept replacements that require low
903 // feerate junk to be mined first. Ideally we'd keep track of
904 // the ancestor feerates and make the decision based on that,
905 // but for now requiring all new inputs to be confirmed works.
906 if (!setConflictsParents.count(tx.vin[j].prevout.hash))
908 // Rather than check the UTXO set - potentially expensive -
909 // it's cheaper to just check if the new input refers to a
910 // tx that's in the mempool.
911 if (pool.mapTx.find(tx.vin[j].prevout.hash) != pool.mapTx.end())
912 return state.DoS(0, false,
913 REJECT_NONSTANDARD, "replacement-adds-unconfirmed", false,
914 strprintf("replacement %s adds unconfirmed input, idx %d",
915 hash.ToString(), j));
919 // The replacement must pay greater fees than the transactions it
920 // replaces - if we did the bandwidth used by those conflicting
921 // transactions would not be paid for.
922 if (nModifiedFees < nConflictingFees)
924 return state.DoS(0, false,
925 REJECT_INSUFFICIENTFEE, "insufficient fee", false,
926 strprintf("rejecting replacement %s, less fees than conflicting txs; %s < %s",
927 hash.ToString(), FormatMoney(nModifiedFees), FormatMoney(nConflictingFees)));
930 // Finally in addition to paying more fees than the conflicts the
931 // new transaction must pay for its own bandwidth.
932 CAmount nDeltaFees = nModifiedFees - nConflictingFees;
933 if (nDeltaFees < ::incrementalRelayFee.GetFee(nSize))
935 return state.DoS(0, false,
936 REJECT_INSUFFICIENTFEE, "insufficient fee", false,
937 strprintf("rejecting replacement %s, not enough additional fees to relay; %s < %s",
938 hash.ToString(),
939 FormatMoney(nDeltaFees),
940 FormatMoney(::incrementalRelayFee.GetFee(nSize))));
944 unsigned int scriptVerifyFlags = STANDARD_SCRIPT_VERIFY_FLAGS;
945 if (!Params().RequireStandard()) {
946 scriptVerifyFlags = GetArg("-promiscuousmempoolflags", scriptVerifyFlags);
949 // Check against previous transactions
950 // This is done last to help prevent CPU exhaustion denial-of-service attacks.
951 PrecomputedTransactionData txdata(tx);
952 if (!CheckInputs(tx, state, view, true, scriptVerifyFlags, true, txdata)) {
953 // SCRIPT_VERIFY_CLEANSTACK requires SCRIPT_VERIFY_WITNESS, so we
954 // need to turn both off, and compare against just turning off CLEANSTACK
955 // to see if the failure is specifically due to witness validation.
956 CValidationState stateDummy; // Want reported failures to be from first CheckInputs
957 if (!tx.HasWitness() && CheckInputs(tx, stateDummy, view, true, scriptVerifyFlags & ~(SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_CLEANSTACK), true, txdata) &&
958 !CheckInputs(tx, stateDummy, view, true, scriptVerifyFlags & ~SCRIPT_VERIFY_CLEANSTACK, true, txdata)) {
959 // Only the witness is missing, so the transaction itself may be fine.
960 state.SetCorruptionPossible();
962 return false; // state filled in by CheckInputs
965 // Check again against just the consensus-critical mandatory script
966 // verification flags, in case of bugs in the standard flags that cause
967 // transactions to pass as valid when they're actually invalid. For
968 // instance the STRICTENC flag was incorrectly allowing certain
969 // CHECKSIG NOT scripts to pass, even though they were invalid.
971 // There is a similar check in CreateNewBlock() to prevent creating
972 // invalid blocks, however allowing such transactions into the mempool
973 // can be exploited as a DoS attack.
974 if (!CheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true, txdata))
976 return error("%s: BUG! PLEASE REPORT THIS! ConnectInputs failed against MANDATORY but not STANDARD flags %s, %s",
977 __func__, hash.ToString(), FormatStateMessage(state));
980 // Remove conflicting transactions from the mempool
981 BOOST_FOREACH(const CTxMemPool::txiter it, allConflicting)
983 LogPrint("mempool", "replacing tx %s with %s for %s BTC additional fees, %d delta bytes\n",
984 it->GetTx().GetHash().ToString(),
985 hash.ToString(),
986 FormatMoney(nModifiedFees - nConflictingFees),
987 (int)nSize - (int)nConflictingSize);
988 if (plTxnReplaced)
989 plTxnReplaced->push_back(it->GetSharedTx());
991 pool.RemoveStaged(allConflicting, false, MemPoolRemovalReason::REPLACED);
993 // This transaction should only count for fee estimation if it isn't a
994 // BIP 125 replacement transaction (may not be widely supported), the
995 // node is not behind, and the transaction is not dependent on any other
996 // transactions in the mempool.
997 bool validForFeeEstimation = !fReplacementTransaction && IsCurrentForFeeEstimation() && pool.HasNoInputsOf(tx);
999 // Store transaction in memory
1000 pool.addUnchecked(hash, entry, setAncestors, validForFeeEstimation);
1002 // trim mempool and check if tx was trimmed
1003 if (!fOverrideMempoolLimit) {
1004 LimitMempoolSize(pool, GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
1005 if (!pool.exists(hash))
1006 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "mempool full");
1010 GetMainSignals().SyncTransaction(tx, NULL, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK);
1012 return true;
1015 bool AcceptToMemoryPoolWithTime(CTxMemPool& pool, CValidationState &state, const CTransactionRef &tx, bool fLimitFree,
1016 bool* pfMissingInputs, int64_t nAcceptTime, std::list<CTransactionRef>* plTxnReplaced,
1017 bool fOverrideMempoolLimit, const CAmount nAbsurdFee)
1019 std::vector<uint256> vHashTxToUncache;
1020 bool res = AcceptToMemoryPoolWorker(pool, state, tx, fLimitFree, pfMissingInputs, nAcceptTime, plTxnReplaced, fOverrideMempoolLimit, nAbsurdFee, vHashTxToUncache);
1021 if (!res) {
1022 BOOST_FOREACH(const uint256& hashTx, vHashTxToUncache)
1023 pcoinsTip->Uncache(hashTx);
1025 // After we've (potentially) uncached entries, ensure our coins cache is still within its size limits
1026 CValidationState stateDummy;
1027 FlushStateToDisk(stateDummy, FLUSH_STATE_PERIODIC);
1028 return res;
1031 bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransactionRef &tx, bool fLimitFree,
1032 bool* pfMissingInputs, std::list<CTransactionRef>* plTxnReplaced,
1033 bool fOverrideMempoolLimit, const CAmount nAbsurdFee)
1035 return AcceptToMemoryPoolWithTime(pool, state, tx, fLimitFree, pfMissingInputs, GetTime(), plTxnReplaced, fOverrideMempoolLimit, nAbsurdFee);
1038 /** Return transaction in txOut, and if it was found inside a block, its hash is placed in hashBlock */
1039 bool GetTransaction(const uint256 &hash, CTransactionRef &txOut, const Consensus::Params& consensusParams, uint256 &hashBlock, bool fAllowSlow)
1041 CBlockIndex *pindexSlow = NULL;
1043 LOCK(cs_main);
1045 CTransactionRef ptx = mempool.get(hash);
1046 if (ptx)
1048 txOut = ptx;
1049 return true;
1052 if (fTxIndex) {
1053 CDiskTxPos postx;
1054 if (pblocktree->ReadTxIndex(hash, postx)) {
1055 CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
1056 if (file.IsNull())
1057 return error("%s: OpenBlockFile failed", __func__);
1058 CBlockHeader header;
1059 try {
1060 file >> header;
1061 fseek(file.Get(), postx.nTxOffset, SEEK_CUR);
1062 file >> txOut;
1063 } catch (const std::exception& e) {
1064 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
1066 hashBlock = header.GetHash();
1067 if (txOut->GetHash() != hash)
1068 return error("%s: txid mismatch", __func__);
1069 return true;
1073 if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
1074 int nHeight = -1;
1076 const CCoinsViewCache& view = *pcoinsTip;
1077 const CCoins* coins = view.AccessCoins(hash);
1078 if (coins)
1079 nHeight = coins->nHeight;
1081 if (nHeight > 0)
1082 pindexSlow = chainActive[nHeight];
1085 if (pindexSlow) {
1086 CBlock block;
1087 if (ReadBlockFromDisk(block, pindexSlow, consensusParams)) {
1088 for (const auto& tx : block.vtx) {
1089 if (tx->GetHash() == hash) {
1090 txOut = tx;
1091 hashBlock = pindexSlow->GetBlockHash();
1092 return true;
1098 return false;
1106 //////////////////////////////////////////////////////////////////////////////
1108 // CBlock and CBlockIndex
1111 bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart)
1113 // Open history file to append
1114 CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
1115 if (fileout.IsNull())
1116 return error("WriteBlockToDisk: OpenBlockFile failed");
1118 // Write index header
1119 unsigned int nSize = GetSerializeSize(fileout, block);
1120 fileout << FLATDATA(messageStart) << nSize;
1122 // Write block
1123 long fileOutPos = ftell(fileout.Get());
1124 if (fileOutPos < 0)
1125 return error("WriteBlockToDisk: ftell failed");
1126 pos.nPos = (unsigned int)fileOutPos;
1127 fileout << block;
1129 return true;
1132 bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams)
1134 block.SetNull();
1136 // Open history file to read
1137 CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
1138 if (filein.IsNull())
1139 return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
1141 // Read block
1142 try {
1143 filein >> block;
1145 catch (const std::exception& e) {
1146 return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
1149 // Check the header
1150 if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
1151 return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
1153 return true;
1156 bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
1158 if (!ReadBlockFromDisk(block, pindex->GetBlockPos(), consensusParams))
1159 return false;
1160 if (block.GetHash() != pindex->GetBlockHash())
1161 return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
1162 pindex->ToString(), pindex->GetBlockPos().ToString());
1163 return true;
1166 CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
1168 int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
1169 // Force block reward to zero when right shift is undefined.
1170 if (halvings >= 64)
1171 return 0;
1173 CAmount nSubsidy = 50 * COIN;
1174 // Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
1175 nSubsidy >>= halvings;
1176 return nSubsidy;
1179 bool IsInitialBlockDownload()
1181 const CChainParams& chainParams = Params();
1183 // Once this function has returned false, it must remain false.
1184 static std::atomic<bool> latchToFalse{false};
1185 // Optimization: pre-test latch before taking the lock.
1186 if (latchToFalse.load(std::memory_order_relaxed))
1187 return false;
1189 LOCK(cs_main);
1190 if (latchToFalse.load(std::memory_order_relaxed))
1191 return false;
1192 if (fImporting || fReindex)
1193 return true;
1194 if (chainActive.Tip() == NULL)
1195 return true;
1196 if (chainActive.Tip()->nChainWork < UintToArith256(chainParams.GetConsensus().nMinimumChainWork))
1197 return true;
1198 if (chainActive.Tip()->GetBlockTime() < (GetTime() - nMaxTipAge))
1199 return true;
1200 latchToFalse.store(true, std::memory_order_relaxed);
1201 return false;
1204 CBlockIndex *pindexBestForkTip = NULL, *pindexBestForkBase = NULL;
1206 static void AlertNotify(const std::string& strMessage)
1208 uiInterface.NotifyAlertChanged();
1209 std::string strCmd = GetArg("-alertnotify", "");
1210 if (strCmd.empty()) return;
1212 // Alert text should be plain ascii coming from a trusted source, but to
1213 // be safe we first strip anything not in safeChars, then add single quotes around
1214 // the whole string before passing it to the shell:
1215 std::string singleQuote("'");
1216 std::string safeStatus = SanitizeString(strMessage);
1217 safeStatus = singleQuote+safeStatus+singleQuote;
1218 boost::replace_all(strCmd, "%s", safeStatus);
1220 boost::thread t(runCommand, strCmd); // thread runs free
1223 void CheckForkWarningConditions()
1225 AssertLockHeld(cs_main);
1226 // Before we get past initial download, we cannot reliably alert about forks
1227 // (we assume we don't get stuck on a fork before finishing our initial sync)
1228 if (IsInitialBlockDownload())
1229 return;
1231 // If our best fork is no longer within 72 blocks (+/- 12 hours if no one mines it)
1232 // of our head, drop it
1233 if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
1234 pindexBestForkTip = NULL;
1236 if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6)))
1238 if (!GetfLargeWorkForkFound() && pindexBestForkBase)
1240 std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
1241 pindexBestForkBase->phashBlock->ToString() + std::string("'");
1242 AlertNotify(warning);
1244 if (pindexBestForkTip && pindexBestForkBase)
1246 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__,
1247 pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
1248 pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
1249 SetfLargeWorkForkFound(true);
1251 else
1253 LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
1254 SetfLargeWorkInvalidChainFound(true);
1257 else
1259 SetfLargeWorkForkFound(false);
1260 SetfLargeWorkInvalidChainFound(false);
1264 void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
1266 AssertLockHeld(cs_main);
1267 // If we are on a fork that is sufficiently large, set a warning flag
1268 CBlockIndex* pfork = pindexNewForkTip;
1269 CBlockIndex* plonger = chainActive.Tip();
1270 while (pfork && pfork != plonger)
1272 while (plonger && plonger->nHeight > pfork->nHeight)
1273 plonger = plonger->pprev;
1274 if (pfork == plonger)
1275 break;
1276 pfork = pfork->pprev;
1279 // We define a condition where we should warn the user about as a fork of at least 7 blocks
1280 // with a tip within 72 blocks (+/- 12 hours if no one mines it) of ours
1281 // We use 7 blocks rather arbitrarily as it represents just under 10% of sustained network
1282 // hash rate operating on the fork.
1283 // or a chain that is entirely longer than ours and invalid (note that this should be detected by both)
1284 // We define it this way because it allows us to only store the highest fork tip (+ base) which meets
1285 // the 7-block condition and from this always have the most-likely-to-cause-warning fork
1286 if (pfork && (!pindexBestForkTip || (pindexBestForkTip && pindexNewForkTip->nHeight > pindexBestForkTip->nHeight)) &&
1287 pindexNewForkTip->nChainWork - pfork->nChainWork > (GetBlockProof(*pfork) * 7) &&
1288 chainActive.Height() - pindexNewForkTip->nHeight < 72)
1290 pindexBestForkTip = pindexNewForkTip;
1291 pindexBestForkBase = pfork;
1294 CheckForkWarningConditions();
1297 void static InvalidChainFound(CBlockIndex* pindexNew)
1299 if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
1300 pindexBestInvalid = pindexNew;
1302 LogPrintf("%s: invalid block=%s height=%d log2_work=%.8g date=%s\n", __func__,
1303 pindexNew->GetBlockHash().ToString(), pindexNew->nHeight,
1304 log(pindexNew->nChainWork.getdouble())/log(2.0), DateTimeStrFormat("%Y-%m-%d %H:%M:%S",
1305 pindexNew->GetBlockTime()));
1306 CBlockIndex *tip = chainActive.Tip();
1307 assert (tip);
1308 LogPrintf("%s: current best=%s height=%d log2_work=%.8g date=%s\n", __func__,
1309 tip->GetBlockHash().ToString(), chainActive.Height(), log(tip->nChainWork.getdouble())/log(2.0),
1310 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", tip->GetBlockTime()));
1311 CheckForkWarningConditions();
1314 void static InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state) {
1315 if (!state.CorruptionPossible()) {
1316 pindex->nStatus |= BLOCK_FAILED_VALID;
1317 setDirtyBlockIndex.insert(pindex);
1318 setBlockIndexCandidates.erase(pindex);
1319 InvalidChainFound(pindex);
1323 void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight)
1325 // mark inputs spent
1326 if (!tx.IsCoinBase()) {
1327 txundo.vprevout.reserve(tx.vin.size());
1328 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
1329 CCoinsModifier coins = inputs.ModifyCoins(txin.prevout.hash);
1330 unsigned nPos = txin.prevout.n;
1332 if (nPos >= coins->vout.size() || coins->vout[nPos].IsNull())
1333 assert(false);
1334 // mark an outpoint spent, and construct undo information
1335 txundo.vprevout.push_back(CTxInUndo(coins->vout[nPos]));
1336 coins->Spend(nPos);
1337 if (coins->vout.size() == 0) {
1338 CTxInUndo& undo = txundo.vprevout.back();
1339 undo.nHeight = coins->nHeight;
1340 undo.fCoinBase = coins->fCoinBase;
1341 undo.nVersion = coins->nVersion;
1345 // add outputs
1346 inputs.ModifyNewCoins(tx.GetHash(), tx.IsCoinBase())->FromTx(tx, nHeight);
1349 void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, int nHeight)
1351 CTxUndo txundo;
1352 UpdateCoins(tx, inputs, txundo, nHeight);
1355 bool CScriptCheck::operator()() {
1356 const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
1357 const CScriptWitness *witness = &ptxTo->vin[nIn].scriptWitness;
1358 if (!VerifyScript(scriptSig, scriptPubKey, witness, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, amount, cacheStore, *txdata), &error)) {
1359 return false;
1361 return true;
1364 int GetSpendHeight(const CCoinsViewCache& inputs)
1366 LOCK(cs_main);
1367 CBlockIndex* pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
1368 return pindexPrev->nHeight + 1;
1371 namespace Consensus {
1372 bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight)
1374 // This doesn't trigger the DoS code on purpose; if it did, it would make it easier
1375 // for an attacker to attempt to split the network.
1376 if (!inputs.HaveInputs(tx))
1377 return state.Invalid(false, 0, "", "Inputs unavailable");
1379 CAmount nValueIn = 0;
1380 CAmount nFees = 0;
1381 for (unsigned int i = 0; i < tx.vin.size(); i++)
1383 const COutPoint &prevout = tx.vin[i].prevout;
1384 const CCoins *coins = inputs.AccessCoins(prevout.hash);
1385 assert(coins);
1387 // If prev is coinbase, check that it's matured
1388 if (coins->IsCoinBase()) {
1389 if (nSpendHeight - coins->nHeight < COINBASE_MATURITY)
1390 return state.Invalid(false,
1391 REJECT_INVALID, "bad-txns-premature-spend-of-coinbase",
1392 strprintf("tried to spend coinbase at depth %d", nSpendHeight - coins->nHeight));
1395 // Check for negative or overflow input values
1396 nValueIn += coins->vout[prevout.n].nValue;
1397 if (!MoneyRange(coins->vout[prevout.n].nValue) || !MoneyRange(nValueIn))
1398 return state.DoS(100, false, REJECT_INVALID, "bad-txns-inputvalues-outofrange");
1402 if (nValueIn < tx.GetValueOut())
1403 return state.DoS(100, false, REJECT_INVALID, "bad-txns-in-belowout", false,
1404 strprintf("value in (%s) < value out (%s)", FormatMoney(nValueIn), FormatMoney(tx.GetValueOut())));
1406 // Tally transaction fees
1407 CAmount nTxFee = nValueIn - tx.GetValueOut();
1408 if (nTxFee < 0)
1409 return state.DoS(100, false, REJECT_INVALID, "bad-txns-fee-negative");
1410 nFees += nTxFee;
1411 if (!MoneyRange(nFees))
1412 return state.DoS(100, false, REJECT_INVALID, "bad-txns-fee-outofrange");
1413 return true;
1415 }// namespace Consensus
1417 bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks)
1419 if (!tx.IsCoinBase())
1421 if (!Consensus::CheckTxInputs(tx, state, inputs, GetSpendHeight(inputs)))
1422 return false;
1424 if (pvChecks)
1425 pvChecks->reserve(tx.vin.size());
1427 // The first loop above does all the inexpensive checks.
1428 // Only if ALL inputs pass do we perform expensive ECDSA signature checks.
1429 // Helps prevent CPU exhaustion attacks.
1431 // Skip script verification when connecting blocks under the
1432 // assumevalid block. Assuming the assumevalid block is valid this
1433 // is safe because block merkle hashes are still computed and checked,
1434 // Of course, if an assumed valid block is invalid due to false scriptSigs
1435 // this optimization would allow an invalid chain to be accepted.
1436 if (fScriptChecks) {
1437 for (unsigned int i = 0; i < tx.vin.size(); i++) {
1438 const COutPoint &prevout = tx.vin[i].prevout;
1439 const CCoins* coins = inputs.AccessCoins(prevout.hash);
1440 assert(coins);
1442 // Verify signature
1443 CScriptCheck check(*coins, tx, i, flags, cacheStore, &txdata);
1444 if (pvChecks) {
1445 pvChecks->push_back(CScriptCheck());
1446 check.swap(pvChecks->back());
1447 } else if (!check()) {
1448 if (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) {
1449 // Check whether the failure was caused by a
1450 // non-mandatory script verification check, such as
1451 // non-standard DER encodings or non-null dummy
1452 // arguments; if so, don't trigger DoS protection to
1453 // avoid splitting the network between upgraded and
1454 // non-upgraded nodes.
1455 CScriptCheck check2(*coins, tx, i,
1456 flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, cacheStore, &txdata);
1457 if (check2())
1458 return state.Invalid(false, REJECT_NONSTANDARD, strprintf("non-mandatory-script-verify-flag (%s)", ScriptErrorString(check.GetScriptError())));
1460 // Failures of other flags indicate a transaction that is
1461 // invalid in new blocks, e.g. a invalid P2SH. We DoS ban
1462 // such nodes as they are not following the protocol. That
1463 // said during an upgrade careful thought should be taken
1464 // as to the correct behavior - we may want to continue
1465 // peering with non-upgraded nodes even after soft-fork
1466 // super-majority signaling has occurred.
1467 return state.DoS(100,false, REJECT_INVALID, strprintf("mandatory-script-verify-flag-failed (%s)", ScriptErrorString(check.GetScriptError())));
1473 return true;
1476 namespace {
1478 bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
1480 // Open history file to append
1481 CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
1482 if (fileout.IsNull())
1483 return error("%s: OpenUndoFile failed", __func__);
1485 // Write index header
1486 unsigned int nSize = GetSerializeSize(fileout, blockundo);
1487 fileout << FLATDATA(messageStart) << nSize;
1489 // Write undo data
1490 long fileOutPos = ftell(fileout.Get());
1491 if (fileOutPos < 0)
1492 return error("%s: ftell failed", __func__);
1493 pos.nPos = (unsigned int)fileOutPos;
1494 fileout << blockundo;
1496 // calculate & write checksum
1497 CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
1498 hasher << hashBlock;
1499 hasher << blockundo;
1500 fileout << hasher.GetHash();
1502 return true;
1505 bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock)
1507 // Open history file to read
1508 CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
1509 if (filein.IsNull())
1510 return error("%s: OpenUndoFile failed", __func__);
1512 // Read block
1513 uint256 hashChecksum;
1514 try {
1515 filein >> blockundo;
1516 filein >> hashChecksum;
1518 catch (const std::exception& e) {
1519 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
1522 // Verify checksum
1523 CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
1524 hasher << hashBlock;
1525 hasher << blockundo;
1526 if (hashChecksum != hasher.GetHash())
1527 return error("%s: Checksum mismatch", __func__);
1529 return true;
1532 /** Abort with a message */
1533 bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
1535 SetMiscWarning(strMessage);
1536 LogPrintf("*** %s\n", strMessage);
1537 uiInterface.ThreadSafeMessageBox(
1538 userMessage.empty() ? _("Error: A fatal internal error occurred, see debug.log for details") : userMessage,
1539 "", CClientUIInterface::MSG_ERROR);
1540 StartShutdown();
1541 return false;
1544 bool AbortNode(CValidationState& state, const std::string& strMessage, const std::string& userMessage="")
1546 AbortNode(strMessage, userMessage);
1547 return state.Error(strMessage);
1550 } // anon namespace
1553 * Apply the undo operation of a CTxInUndo to the given chain state.
1554 * @param undo The undo object.
1555 * @param view The coins view to which to apply the changes.
1556 * @param out The out point that corresponds to the tx input.
1557 * @return True on success.
1559 bool ApplyTxInUndo(const CTxInUndo& undo, CCoinsViewCache& view, const COutPoint& out)
1561 bool fClean = true;
1563 CCoinsModifier coins = view.ModifyCoins(out.hash);
1564 if (undo.nHeight != 0) {
1565 // undo data contains height: this is the last output of the prevout tx being spent
1566 if (!coins->IsPruned())
1567 fClean = fClean && error("%s: undo data overwriting existing transaction", __func__);
1568 coins->Clear();
1569 coins->fCoinBase = undo.fCoinBase;
1570 coins->nHeight = undo.nHeight;
1571 coins->nVersion = undo.nVersion;
1572 } else {
1573 if (coins->IsPruned())
1574 fClean = fClean && error("%s: undo data adding output to missing transaction", __func__);
1576 if (coins->IsAvailable(out.n))
1577 fClean = fClean && error("%s: undo data overwriting existing output", __func__);
1578 if (coins->vout.size() < out.n+1)
1579 coins->vout.resize(out.n+1);
1580 coins->vout[out.n] = undo.txout;
1582 return fClean;
1585 bool DisconnectBlock(const CBlock& block, CValidationState& state, const CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean)
1587 assert(pindex->GetBlockHash() == view.GetBestBlock());
1589 if (pfClean)
1590 *pfClean = false;
1592 bool fClean = true;
1594 CBlockUndo blockUndo;
1595 CDiskBlockPos pos = pindex->GetUndoPos();
1596 if (pos.IsNull())
1597 return error("DisconnectBlock(): no undo data available");
1598 if (!UndoReadFromDisk(blockUndo, pos, pindex->pprev->GetBlockHash()))
1599 return error("DisconnectBlock(): failure reading undo data");
1601 if (blockUndo.vtxundo.size() + 1 != block.vtx.size())
1602 return error("DisconnectBlock(): block and undo data inconsistent");
1604 // undo transactions in reverse order
1605 for (int i = block.vtx.size() - 1; i >= 0; i--) {
1606 const CTransaction &tx = *(block.vtx[i]);
1607 uint256 hash = tx.GetHash();
1609 // Check that all outputs are available and match the outputs in the block itself
1610 // exactly.
1612 CCoinsModifier outs = view.ModifyCoins(hash);
1613 outs->ClearUnspendable();
1615 CCoins outsBlock(tx, pindex->nHeight);
1616 // The CCoins serialization does not serialize negative numbers.
1617 // No network rules currently depend on the version here, so an inconsistency is harmless
1618 // but it must be corrected before txout nversion ever influences a network rule.
1619 if (outsBlock.nVersion < 0)
1620 outs->nVersion = outsBlock.nVersion;
1621 if (*outs != outsBlock)
1622 fClean = fClean && error("DisconnectBlock(): added transaction mismatch? database corrupted");
1624 // remove outputs
1625 outs->Clear();
1628 // restore inputs
1629 if (i > 0) { // not coinbases
1630 const CTxUndo &txundo = blockUndo.vtxundo[i-1];
1631 if (txundo.vprevout.size() != tx.vin.size())
1632 return error("DisconnectBlock(): transaction and undo data inconsistent");
1633 for (unsigned int j = tx.vin.size(); j-- > 0;) {
1634 const COutPoint &out = tx.vin[j].prevout;
1635 const CTxInUndo &undo = txundo.vprevout[j];
1636 if (!ApplyTxInUndo(undo, view, out))
1637 fClean = false;
1642 // move best block pointer to prevout block
1643 view.SetBestBlock(pindex->pprev->GetBlockHash());
1645 if (pfClean) {
1646 *pfClean = fClean;
1647 return true;
1650 return fClean;
1653 void static FlushBlockFile(bool fFinalize = false)
1655 LOCK(cs_LastBlockFile);
1657 CDiskBlockPos posOld(nLastBlockFile, 0);
1659 FILE *fileOld = OpenBlockFile(posOld);
1660 if (fileOld) {
1661 if (fFinalize)
1662 TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nSize);
1663 FileCommit(fileOld);
1664 fclose(fileOld);
1667 fileOld = OpenUndoFile(posOld);
1668 if (fileOld) {
1669 if (fFinalize)
1670 TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nUndoSize);
1671 FileCommit(fileOld);
1672 fclose(fileOld);
1676 bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
1678 static CCheckQueue<CScriptCheck> scriptcheckqueue(128);
1680 void ThreadScriptCheck() {
1681 RenameThread("bitcoin-scriptch");
1682 scriptcheckqueue.Thread();
1685 // Protected by cs_main
1686 VersionBitsCache versionbitscache;
1688 int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
1690 LOCK(cs_main);
1691 int32_t nVersion = VERSIONBITS_TOP_BITS;
1693 for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) {
1694 ThresholdState state = VersionBitsState(pindexPrev, params, (Consensus::DeploymentPos)i, versionbitscache);
1695 if (state == THRESHOLD_LOCKED_IN || state == THRESHOLD_STARTED) {
1696 nVersion |= VersionBitsMask(params, (Consensus::DeploymentPos)i);
1700 return nVersion;
1704 * Threshold condition checker that triggers when unknown versionbits are seen on the network.
1706 class WarningBitsConditionChecker : public AbstractThresholdConditionChecker
1708 private:
1709 int bit;
1711 public:
1712 WarningBitsConditionChecker(int bitIn) : bit(bitIn) {}
1714 int64_t BeginTime(const Consensus::Params& params) const { return 0; }
1715 int64_t EndTime(const Consensus::Params& params) const { return std::numeric_limits<int64_t>::max(); }
1716 int Period(const Consensus::Params& params) const { return params.nMinerConfirmationWindow; }
1717 int Threshold(const Consensus::Params& params) const { return params.nRuleChangeActivationThreshold; }
1719 bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const
1721 return ((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) &&
1722 ((pindex->nVersion >> bit) & 1) != 0 &&
1723 ((ComputeBlockVersion(pindex->pprev, params) >> bit) & 1) == 0;
1727 // Protected by cs_main
1728 static ThresholdConditionCache warningcache[VERSIONBITS_NUM_BITS];
1730 static int64_t nTimeCheck = 0;
1731 static int64_t nTimeForks = 0;
1732 static int64_t nTimeVerify = 0;
1733 static int64_t nTimeConnect = 0;
1734 static int64_t nTimeIndex = 0;
1735 static int64_t nTimeCallbacks = 0;
1736 static int64_t nTimeTotal = 0;
1738 bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex,
1739 CCoinsViewCache& view, const CChainParams& chainparams, bool fJustCheck)
1741 AssertLockHeld(cs_main);
1743 int64_t nTimeStart = GetTimeMicros();
1745 // Check it again in case a previous version let a bad block in
1746 if (!CheckBlock(block, state, chainparams.GetConsensus(), !fJustCheck, !fJustCheck))
1747 return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
1749 // verify that the view's current state corresponds to the previous block
1750 uint256 hashPrevBlock = pindex->pprev == NULL ? uint256() : pindex->pprev->GetBlockHash();
1751 assert(hashPrevBlock == view.GetBestBlock());
1753 // Special case for the genesis block, skipping connection of its transactions
1754 // (its coinbase is unspendable)
1755 if (block.GetHash() == chainparams.GetConsensus().hashGenesisBlock) {
1756 if (!fJustCheck)
1757 view.SetBestBlock(pindex->GetBlockHash());
1758 return true;
1761 bool fScriptChecks = true;
1762 if (!hashAssumeValid.IsNull()) {
1763 // We've been configured with the hash of a block which has been externally verified to have a valid history.
1764 // A suitable default value is included with the software and updated from time to time. Because validity
1765 // relative to a piece of software is an objective fact these defaults can be easily reviewed.
1766 // This setting doesn't force the selection of any particular chain but makes validating some faster by
1767 // effectively caching the result of part of the verification.
1768 BlockMap::const_iterator it = mapBlockIndex.find(hashAssumeValid);
1769 if (it != mapBlockIndex.end()) {
1770 if (it->second->GetAncestor(pindex->nHeight) == pindex &&
1771 pindexBestHeader->GetAncestor(pindex->nHeight) == pindex &&
1772 pindexBestHeader->nChainWork >= UintToArith256(chainparams.GetConsensus().nMinimumChainWork)) {
1773 // This block is a member of the assumed verified chain and an ancestor of the best header.
1774 // The equivalent time check discourages hash power from extorting the network via DOS attack
1775 // into accepting an invalid block through telling users they must manually set assumevalid.
1776 // Requiring a software change or burying the invalid block, regardless of the setting, makes
1777 // it hard to hide the implication of the demand. This also avoids having release candidates
1778 // that are hardly doing any signature verification at all in testing without having to
1779 // artificially set the default assumed verified block further back.
1780 // The test against nMinimumChainWork prevents the skipping when denied access to any chain at
1781 // least as good as the expected chain.
1782 fScriptChecks = (GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, chainparams.GetConsensus()) <= 60 * 60 * 24 * 7 * 2);
1787 int64_t nTime1 = GetTimeMicros(); nTimeCheck += nTime1 - nTimeStart;
1788 LogPrint("bench", " - Sanity checks: %.2fms [%.2fs]\n", 0.001 * (nTime1 - nTimeStart), nTimeCheck * 0.000001);
1790 // Do not allow blocks that contain transactions which 'overwrite' older transactions,
1791 // unless those are already completely spent.
1792 // If such overwrites are allowed, coinbases and transactions depending upon those
1793 // can be duplicated to remove the ability to spend the first instance -- even after
1794 // being sent to another address.
1795 // See BIP30 and http://r6.ca/blog/20120206T005236Z.html for more information.
1796 // This logic is not necessary for memory pool transactions, as AcceptToMemoryPool
1797 // already refuses previously-known transaction ids entirely.
1798 // This rule was originally applied to all blocks with a timestamp after March 15, 2012, 0:00 UTC.
1799 // Now that the whole chain is irreversibly beyond that time it is applied to all blocks except the
1800 // two in the chain that violate it. This prevents exploiting the issue against nodes during their
1801 // initial block download.
1802 bool fEnforceBIP30 = (!pindex->phashBlock) || // Enforce on CreateNewBlock invocations which don't have a hash.
1803 !((pindex->nHeight==91842 && pindex->GetBlockHash() == uint256S("0x00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec")) ||
1804 (pindex->nHeight==91880 && pindex->GetBlockHash() == uint256S("0x00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721")));
1806 // Once BIP34 activated it was not possible to create new duplicate coinbases and thus other than starting
1807 // with the 2 existing duplicate coinbase pairs, not possible to create overwriting txs. But by the
1808 // time BIP34 activated, in each of the existing pairs the duplicate coinbase had overwritten the first
1809 // before the first had been spent. Since those coinbases are sufficiently buried its no longer possible to create further
1810 // duplicate transactions descending from the known pairs either.
1811 // 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.
1812 CBlockIndex *pindexBIP34height = pindex->pprev->GetAncestor(chainparams.GetConsensus().BIP34Height);
1813 //Only continue to enforce if we're below BIP34 activation height or the block hash at that height doesn't correspond.
1814 fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->GetBlockHash() == chainparams.GetConsensus().BIP34Hash));
1816 if (fEnforceBIP30) {
1817 for (const auto& tx : block.vtx) {
1818 const CCoins* coins = view.AccessCoins(tx->GetHash());
1819 if (coins && !coins->IsPruned())
1820 return state.DoS(100, error("ConnectBlock(): tried to overwrite transaction"),
1821 REJECT_INVALID, "bad-txns-BIP30");
1825 // BIP16 didn't become active until Apr 1 2012
1826 int64_t nBIP16SwitchTime = 1333238400;
1827 bool fStrictPayToScriptHash = (pindex->GetBlockTime() >= nBIP16SwitchTime);
1829 unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE;
1831 // Start enforcing the DERSIG (BIP66) rule
1832 if (pindex->nHeight >= chainparams.GetConsensus().BIP66Height) {
1833 flags |= SCRIPT_VERIFY_DERSIG;
1836 // Start enforcing CHECKLOCKTIMEVERIFY (BIP65) rule
1837 if (pindex->nHeight >= chainparams.GetConsensus().BIP65Height) {
1838 flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
1841 // Start enforcing BIP68 (sequence locks) and BIP112 (CHECKSEQUENCEVERIFY) using versionbits logic.
1842 int nLockTimeFlags = 0;
1843 if (VersionBitsState(pindex->pprev, chainparams.GetConsensus(), Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
1844 flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
1845 nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE;
1848 // Start enforcing WITNESS rules using versionbits logic.
1849 if (IsWitnessEnabled(pindex->pprev, chainparams.GetConsensus())) {
1850 flags |= SCRIPT_VERIFY_WITNESS;
1851 flags |= SCRIPT_VERIFY_NULLDUMMY;
1854 int64_t nTime2 = GetTimeMicros(); nTimeForks += nTime2 - nTime1;
1855 LogPrint("bench", " - Fork checks: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1), nTimeForks * 0.000001);
1857 CBlockUndo blockundo;
1859 CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL);
1861 std::vector<int> prevheights;
1862 CAmount nFees = 0;
1863 int nInputs = 0;
1864 int64_t nSigOpsCost = 0;
1865 CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
1866 std::vector<std::pair<uint256, CDiskTxPos> > vPos;
1867 vPos.reserve(block.vtx.size());
1868 blockundo.vtxundo.reserve(block.vtx.size() - 1);
1869 std::vector<PrecomputedTransactionData> txdata;
1870 txdata.reserve(block.vtx.size()); // Required so that pointers to individual PrecomputedTransactionData don't get invalidated
1871 for (unsigned int i = 0; i < block.vtx.size(); i++)
1873 const CTransaction &tx = *(block.vtx[i]);
1875 nInputs += tx.vin.size();
1877 if (!tx.IsCoinBase())
1879 if (!view.HaveInputs(tx))
1880 return state.DoS(100, error("ConnectBlock(): inputs missing/spent"),
1881 REJECT_INVALID, "bad-txns-inputs-missingorspent");
1883 // Check that transaction is BIP68 final
1884 // BIP68 lock checks (as opposed to nLockTime checks) must
1885 // be in ConnectBlock because they require the UTXO set
1886 prevheights.resize(tx.vin.size());
1887 for (size_t j = 0; j < tx.vin.size(); j++) {
1888 prevheights[j] = view.AccessCoins(tx.vin[j].prevout.hash)->nHeight;
1891 if (!SequenceLocks(tx, nLockTimeFlags, &prevheights, *pindex)) {
1892 return state.DoS(100, error("%s: contains a non-BIP68-final transaction", __func__),
1893 REJECT_INVALID, "bad-txns-nonfinal");
1897 // GetTransactionSigOpCost counts 3 types of sigops:
1898 // * legacy (always)
1899 // * p2sh (when P2SH enabled in flags and excludes coinbase)
1900 // * witness (when witness enabled in flags and excludes coinbase)
1901 nSigOpsCost += GetTransactionSigOpCost(tx, view, flags);
1902 if (nSigOpsCost > MAX_BLOCK_SIGOPS_COST)
1903 return state.DoS(100, error("ConnectBlock(): too many sigops"),
1904 REJECT_INVALID, "bad-blk-sigops");
1906 txdata.emplace_back(tx);
1907 if (!tx.IsCoinBase())
1909 nFees += view.GetValueIn(tx)-tx.GetValueOut();
1911 std::vector<CScriptCheck> vChecks;
1912 bool fCacheResults = fJustCheck; /* Don't cache results if we're actually connecting blocks (still consult the cache, though) */
1913 if (!CheckInputs(tx, state, view, fScriptChecks, flags, fCacheResults, txdata[i], nScriptCheckThreads ? &vChecks : NULL))
1914 return error("ConnectBlock(): CheckInputs on %s failed with %s",
1915 tx.GetHash().ToString(), FormatStateMessage(state));
1916 control.Add(vChecks);
1919 CTxUndo undoDummy;
1920 if (i > 0) {
1921 blockundo.vtxundo.push_back(CTxUndo());
1923 UpdateCoins(tx, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight);
1925 vPos.push_back(std::make_pair(tx.GetHash(), pos));
1926 pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
1928 int64_t nTime3 = GetTimeMicros(); nTimeConnect += nTime3 - nTime2;
1929 LogPrint("bench", " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs]\n", (unsigned)block.vtx.size(), 0.001 * (nTime3 - nTime2), 0.001 * (nTime3 - nTime2) / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * (nTime3 - nTime2) / (nInputs-1), nTimeConnect * 0.000001);
1931 CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, chainparams.GetConsensus());
1932 if (block.vtx[0]->GetValueOut() > blockReward)
1933 return state.DoS(100,
1934 error("ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)",
1935 block.vtx[0]->GetValueOut(), blockReward),
1936 REJECT_INVALID, "bad-cb-amount");
1938 if (!control.Wait())
1939 return state.DoS(100, false);
1940 int64_t nTime4 = GetTimeMicros(); nTimeVerify += nTime4 - nTime2;
1941 LogPrint("bench", " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs]\n", nInputs - 1, 0.001 * (nTime4 - nTime2), nInputs <= 1 ? 0 : 0.001 * (nTime4 - nTime2) / (nInputs-1), nTimeVerify * 0.000001);
1943 if (fJustCheck)
1944 return true;
1946 // Write undo information to disk
1947 if (pindex->GetUndoPos().IsNull() || !pindex->IsValid(BLOCK_VALID_SCRIPTS))
1949 if (pindex->GetUndoPos().IsNull()) {
1950 CDiskBlockPos _pos;
1951 if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
1952 return error("ConnectBlock(): FindUndoPos failed");
1953 if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart()))
1954 return AbortNode(state, "Failed to write undo data");
1956 // update nUndoPos in block index
1957 pindex->nUndoPos = _pos.nPos;
1958 pindex->nStatus |= BLOCK_HAVE_UNDO;
1961 pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
1962 setDirtyBlockIndex.insert(pindex);
1965 if (fTxIndex)
1966 if (!pblocktree->WriteTxIndex(vPos))
1967 return AbortNode(state, "Failed to write transaction index");
1969 // add this block to the view's block chain
1970 view.SetBestBlock(pindex->GetBlockHash());
1972 int64_t nTime5 = GetTimeMicros(); nTimeIndex += nTime5 - nTime4;
1973 LogPrint("bench", " - Index writing: %.2fms [%.2fs]\n", 0.001 * (nTime5 - nTime4), nTimeIndex * 0.000001);
1975 // Watch for changes to the previous coinbase transaction.
1976 static uint256 hashPrevBestCoinBase;
1977 GetMainSignals().UpdatedTransaction(hashPrevBestCoinBase);
1978 hashPrevBestCoinBase = block.vtx[0]->GetHash();
1981 int64_t nTime6 = GetTimeMicros(); nTimeCallbacks += nTime6 - nTime5;
1982 LogPrint("bench", " - Callbacks: %.2fms [%.2fs]\n", 0.001 * (nTime6 - nTime5), nTimeCallbacks * 0.000001);
1984 return true;
1988 * Update the on-disk chain state.
1989 * The caches and indexes are flushed depending on the mode we're called with
1990 * if they're too large, if it's been a while since the last write,
1991 * or always and in all cases if we're in prune mode and are deleting files.
1993 bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode, int nManualPruneHeight) {
1994 int64_t nMempoolUsage = mempool.DynamicMemoryUsage();
1995 const CChainParams& chainparams = Params();
1996 LOCK2(cs_main, cs_LastBlockFile);
1997 static int64_t nLastWrite = 0;
1998 static int64_t nLastFlush = 0;
1999 static int64_t nLastSetChain = 0;
2000 std::set<int> setFilesToPrune;
2001 bool fFlushForPrune = false;
2002 try {
2003 if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
2004 if (nManualPruneHeight > 0) {
2005 FindFilesToPruneManual(setFilesToPrune, nManualPruneHeight);
2006 } else {
2007 FindFilesToPrune(setFilesToPrune, chainparams.PruneAfterHeight());
2008 fCheckForPruning = false;
2010 if (!setFilesToPrune.empty()) {
2011 fFlushForPrune = true;
2012 if (!fHavePruned) {
2013 pblocktree->WriteFlag("prunedblockfiles", true);
2014 fHavePruned = true;
2018 int64_t nNow = GetTimeMicros();
2019 // Avoid writing/flushing immediately after startup.
2020 if (nLastWrite == 0) {
2021 nLastWrite = nNow;
2023 if (nLastFlush == 0) {
2024 nLastFlush = nNow;
2026 if (nLastSetChain == 0) {
2027 nLastSetChain = nNow;
2029 int64_t nMempoolSizeMax = GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
2030 int64_t cacheSize = pcoinsTip->DynamicMemoryUsage();
2031 int64_t nTotalSpace = nCoinCacheUsage + std::max<int64_t>(nMempoolSizeMax - nMempoolUsage, 0);
2032 // The cache is large and we're within 10% and 100 MiB of the limit, but we have time now (not in the middle of a block processing).
2033 bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize > std::max((9 * nTotalSpace) / 10, nTotalSpace - 100 * 1024 * 1024);
2034 // The cache is over the limit, we have to write now.
2035 bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && cacheSize > nTotalSpace;
2036 // 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.
2037 bool fPeriodicWrite = mode == FLUSH_STATE_PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
2038 // It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
2039 bool fPeriodicFlush = mode == FLUSH_STATE_PERIODIC && nNow > nLastFlush + (int64_t)DATABASE_FLUSH_INTERVAL * 1000000;
2040 // Combine all conditions that result in a full cache flush.
2041 bool fDoFullFlush = (mode == FLUSH_STATE_ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
2042 // Write blocks and block index to disk.
2043 if (fDoFullFlush || fPeriodicWrite) {
2044 // Depend on nMinDiskSpace to ensure we can write block index
2045 if (!CheckDiskSpace(0))
2046 return state.Error("out of disk space");
2047 // First make sure all block and undo data is flushed to disk.
2048 FlushBlockFile();
2049 // Then update all block file information (which may refer to block and undo files).
2051 std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
2052 vFiles.reserve(setDirtyFileInfo.size());
2053 for (std::set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
2054 vFiles.push_back(std::make_pair(*it, &vinfoBlockFile[*it]));
2055 setDirtyFileInfo.erase(it++);
2057 std::vector<const CBlockIndex*> vBlocks;
2058 vBlocks.reserve(setDirtyBlockIndex.size());
2059 for (std::set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
2060 vBlocks.push_back(*it);
2061 setDirtyBlockIndex.erase(it++);
2063 if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
2064 return AbortNode(state, "Failed to write to block index database");
2067 // Finally remove any pruned files
2068 if (fFlushForPrune)
2069 UnlinkPrunedFiles(setFilesToPrune);
2070 nLastWrite = nNow;
2072 // Flush best chain related state. This can only be done if the blocks / block index write was also done.
2073 if (fDoFullFlush) {
2074 // Typical CCoins structures on disk are around 128 bytes in size.
2075 // Pushing a new one to the database can cause it to be written
2076 // twice (once in the log, and once in the tables). This is already
2077 // an overestimation, as most will delete an existing entry or
2078 // overwrite one. Still, use a conservative safety factor of 2.
2079 if (!CheckDiskSpace(128 * 2 * 2 * pcoinsTip->GetCacheSize()))
2080 return state.Error("out of disk space");
2081 // Flush the chainstate (which may refer to block index entries).
2082 if (!pcoinsTip->Flush())
2083 return AbortNode(state, "Failed to write to coin database");
2084 nLastFlush = nNow;
2086 if (fDoFullFlush || ((mode == FLUSH_STATE_ALWAYS || mode == FLUSH_STATE_PERIODIC) && nNow > nLastSetChain + (int64_t)DATABASE_WRITE_INTERVAL * 1000000)) {
2087 // Update best block in wallet (so we can detect restored wallets).
2088 GetMainSignals().SetBestChain(chainActive.GetLocator());
2089 nLastSetChain = nNow;
2091 } catch (const std::runtime_error& e) {
2092 return AbortNode(state, std::string("System error while flushing: ") + e.what());
2094 return true;
2097 void FlushStateToDisk() {
2098 CValidationState state;
2099 FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
2102 void PruneAndFlush() {
2103 CValidationState state;
2104 fCheckForPruning = true;
2105 FlushStateToDisk(state, FLUSH_STATE_NONE);
2108 /** Update chainActive and related internal data structures. */
2109 void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
2110 chainActive.SetTip(pindexNew);
2112 // New best block
2113 mempool.AddTransactionsUpdated(1);
2115 cvBlockChange.notify_all();
2117 static bool fWarned = false;
2118 std::vector<std::string> warningMessages;
2119 if (!IsInitialBlockDownload())
2121 int nUpgraded = 0;
2122 const CBlockIndex* pindex = chainActive.Tip();
2123 for (int bit = 0; bit < VERSIONBITS_NUM_BITS; bit++) {
2124 WarningBitsConditionChecker checker(bit);
2125 ThresholdState state = checker.GetStateFor(pindex, chainParams.GetConsensus(), warningcache[bit]);
2126 if (state == THRESHOLD_ACTIVE || state == THRESHOLD_LOCKED_IN) {
2127 if (state == THRESHOLD_ACTIVE) {
2128 std::string strWarning = strprintf(_("Warning: unknown new rules activated (versionbit %i)"), bit);
2129 SetMiscWarning(strWarning);
2130 if (!fWarned) {
2131 AlertNotify(strWarning);
2132 fWarned = true;
2134 } else {
2135 warningMessages.push_back(strprintf("unknown new rules are about to activate (versionbit %i)", bit));
2139 // Check the version of the last 100 blocks to see if we need to upgrade:
2140 for (int i = 0; i < 100 && pindex != NULL; i++)
2142 int32_t nExpectedVersion = ComputeBlockVersion(pindex->pprev, chainParams.GetConsensus());
2143 if (pindex->nVersion > VERSIONBITS_LAST_OLD_BLOCK_VERSION && (pindex->nVersion & ~nExpectedVersion) != 0)
2144 ++nUpgraded;
2145 pindex = pindex->pprev;
2147 if (nUpgraded > 0)
2148 warningMessages.push_back(strprintf("%d of last 100 blocks have unexpected version", nUpgraded));
2149 if (nUpgraded > 100/2)
2151 std::string strWarning = _("Warning: Unknown block versions being mined! It's possible unknown rules are in effect");
2152 // notify GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
2153 SetMiscWarning(strWarning);
2154 if (!fWarned) {
2155 AlertNotify(strWarning);
2156 fWarned = true;
2160 LogPrintf("%s: new best=%s height=%d version=0x%08x log2_work=%.8g tx=%lu date='%s' progress=%f cache=%.1fMiB(%utx)", __func__,
2161 chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), chainActive.Tip()->nVersion,
2162 log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
2163 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
2164 GuessVerificationProgress(chainParams.TxData(), chainActive.Tip()), pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
2165 if (!warningMessages.empty())
2166 LogPrintf(" warning='%s'", boost::algorithm::join(warningMessages, ", "));
2167 LogPrintf("\n");
2171 /** Disconnect chainActive's tip. You probably want to call mempool.removeForReorg and manually re-limit mempool size after this, with cs_main held. */
2172 bool static DisconnectTip(CValidationState& state, const CChainParams& chainparams, bool fBare = false)
2174 CBlockIndex *pindexDelete = chainActive.Tip();
2175 assert(pindexDelete);
2176 // Read block from disk.
2177 CBlock block;
2178 if (!ReadBlockFromDisk(block, pindexDelete, chainparams.GetConsensus()))
2179 return AbortNode(state, "Failed to read block");
2180 // Apply the block atomically to the chain state.
2181 int64_t nStart = GetTimeMicros();
2183 CCoinsViewCache view(pcoinsTip);
2184 if (!DisconnectBlock(block, state, pindexDelete, view))
2185 return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
2186 bool flushed = view.Flush();
2187 assert(flushed);
2189 LogPrint("bench", "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
2190 // Write the chain state to disk, if necessary.
2191 if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED))
2192 return false;
2194 if (!fBare) {
2195 // Resurrect mempool transactions from the disconnected block.
2196 std::vector<uint256> vHashUpdate;
2197 for (const auto& it : block.vtx) {
2198 const CTransaction& tx = *it;
2199 // ignore validation errors in resurrected transactions
2200 CValidationState stateDummy;
2201 if (tx.IsCoinBase() || !AcceptToMemoryPool(mempool, stateDummy, it, false, NULL, NULL, true)) {
2202 mempool.removeRecursive(tx, MemPoolRemovalReason::REORG);
2203 } else if (mempool.exists(tx.GetHash())) {
2204 vHashUpdate.push_back(tx.GetHash());
2207 // AcceptToMemoryPool/addUnchecked all assume that new mempool entries have
2208 // no in-mempool children, which is generally not true when adding
2209 // previously-confirmed transactions back to the mempool.
2210 // UpdateTransactionsFromBlock finds descendants of any transactions in this
2211 // block that were added back and cleans up the mempool state.
2212 mempool.UpdateTransactionsFromBlock(vHashUpdate);
2215 // Update chainActive and related variables.
2216 UpdateTip(pindexDelete->pprev, chainparams);
2217 // Let wallets know transactions went from 1-confirmed to
2218 // 0-confirmed or conflicted:
2219 for (const auto& tx : block.vtx) {
2220 GetMainSignals().SyncTransaction(*tx, pindexDelete->pprev, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK);
2222 return true;
2225 static int64_t nTimeReadFromDisk = 0;
2226 static int64_t nTimeConnectTotal = 0;
2227 static int64_t nTimeFlush = 0;
2228 static int64_t nTimeChainState = 0;
2229 static int64_t nTimePostConnect = 0;
2232 * Used to track blocks whose transactions were applied to the UTXO state as a
2233 * part of a single ActivateBestChainStep call.
2235 struct ConnectTrace {
2236 std::vector<std::pair<CBlockIndex*, std::shared_ptr<const CBlock> > > blocksConnected;
2240 * Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
2241 * corresponding to pindexNew, to bypass loading it again from disk.
2243 * The block is always added to connectTrace (either after loading from disk or by copying
2244 * pblock) - if that is not intended, care must be taken to remove the last entry in
2245 * blocksConnected in case of failure.
2247 bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace)
2249 assert(pindexNew->pprev == chainActive.Tip());
2250 // Read block from disk.
2251 int64_t nTime1 = GetTimeMicros();
2252 if (!pblock) {
2253 std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
2254 connectTrace.blocksConnected.emplace_back(pindexNew, pblockNew);
2255 if (!ReadBlockFromDisk(*pblockNew, pindexNew, chainparams.GetConsensus()))
2256 return AbortNode(state, "Failed to read block");
2257 } else {
2258 connectTrace.blocksConnected.emplace_back(pindexNew, pblock);
2260 const CBlock& blockConnecting = *connectTrace.blocksConnected.back().second;
2261 // Apply the block atomically to the chain state.
2262 int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1;
2263 int64_t nTime3;
2264 LogPrint("bench", " - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * 0.001, nTimeReadFromDisk * 0.000001);
2266 CCoinsViewCache view(pcoinsTip);
2267 bool rv = ConnectBlock(blockConnecting, state, pindexNew, view, chainparams);
2268 GetMainSignals().BlockChecked(blockConnecting, state);
2269 if (!rv) {
2270 if (state.IsInvalid())
2271 InvalidBlockFound(pindexNew, state);
2272 return error("ConnectTip(): ConnectBlock %s failed", pindexNew->GetBlockHash().ToString());
2274 nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2;
2275 LogPrint("bench", " - Connect total: %.2fms [%.2fs]\n", (nTime3 - nTime2) * 0.001, nTimeConnectTotal * 0.000001);
2276 bool flushed = view.Flush();
2277 assert(flushed);
2279 int64_t nTime4 = GetTimeMicros(); nTimeFlush += nTime4 - nTime3;
2280 LogPrint("bench", " - Flush: %.2fms [%.2fs]\n", (nTime4 - nTime3) * 0.001, nTimeFlush * 0.000001);
2281 // Write the chain state to disk, if necessary.
2282 if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED))
2283 return false;
2284 int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
2285 LogPrint("bench", " - Writing chainstate: %.2fms [%.2fs]\n", (nTime5 - nTime4) * 0.001, nTimeChainState * 0.000001);
2286 // Remove conflicting transactions from the mempool.;
2287 mempool.removeForBlock(blockConnecting.vtx, pindexNew->nHeight);
2288 // Update chainActive & related variables.
2289 UpdateTip(pindexNew, chainparams);
2291 int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
2292 LogPrint("bench", " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001);
2293 LogPrint("bench", "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001);
2294 return true;
2298 * Return the tip of the chain with the most work in it, that isn't
2299 * known to be invalid (it's however far from certain to be valid).
2301 static CBlockIndex* FindMostWorkChain() {
2302 do {
2303 CBlockIndex *pindexNew = NULL;
2305 // Find the best candidate header.
2307 std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexCandidates.rbegin();
2308 if (it == setBlockIndexCandidates.rend())
2309 return NULL;
2310 pindexNew = *it;
2313 // Check whether all blocks on the path between the currently active chain and the candidate are valid.
2314 // Just going until the active chain is an optimization, as we know all blocks in it are valid already.
2315 CBlockIndex *pindexTest = pindexNew;
2316 bool fInvalidAncestor = false;
2317 while (pindexTest && !chainActive.Contains(pindexTest)) {
2318 assert(pindexTest->nChainTx || pindexTest->nHeight == 0);
2320 // Pruned nodes may have entries in setBlockIndexCandidates for
2321 // which block files have been deleted. Remove those as candidates
2322 // for the most work chain if we come across them; we can't switch
2323 // to a chain unless we have all the non-active-chain parent blocks.
2324 bool fFailedChain = pindexTest->nStatus & BLOCK_FAILED_MASK;
2325 bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
2326 if (fFailedChain || fMissingData) {
2327 // Candidate chain is not usable (either invalid or missing data)
2328 if (fFailedChain && (pindexBestInvalid == NULL || pindexNew->nChainWork > pindexBestInvalid->nChainWork))
2329 pindexBestInvalid = pindexNew;
2330 CBlockIndex *pindexFailed = pindexNew;
2331 // Remove the entire chain from the set.
2332 while (pindexTest != pindexFailed) {
2333 if (fFailedChain) {
2334 pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
2335 } else if (fMissingData) {
2336 // If we're missing data, then add back to mapBlocksUnlinked,
2337 // so that if the block arrives in the future we can try adding
2338 // to setBlockIndexCandidates again.
2339 mapBlocksUnlinked.insert(std::make_pair(pindexFailed->pprev, pindexFailed));
2341 setBlockIndexCandidates.erase(pindexFailed);
2342 pindexFailed = pindexFailed->pprev;
2344 setBlockIndexCandidates.erase(pindexTest);
2345 fInvalidAncestor = true;
2346 break;
2348 pindexTest = pindexTest->pprev;
2350 if (!fInvalidAncestor)
2351 return pindexNew;
2352 } while(true);
2355 /** Delete all entries in setBlockIndexCandidates that are worse than the current tip. */
2356 static void PruneBlockIndexCandidates() {
2357 // Note that we can't delete the current block itself, as we may need to return to it later in case a
2358 // reorganization to a better block fails.
2359 std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
2360 while (it != setBlockIndexCandidates.end() && setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) {
2361 setBlockIndexCandidates.erase(it++);
2363 // Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates.
2364 assert(!setBlockIndexCandidates.empty());
2368 * Try to make some progress towards making pindexMostWork the active block.
2369 * pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
2371 static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace)
2373 AssertLockHeld(cs_main);
2374 const CBlockIndex *pindexOldTip = chainActive.Tip();
2375 const CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork);
2377 // Disconnect active blocks which are no longer in the best chain.
2378 bool fBlocksDisconnected = false;
2379 while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
2380 if (!DisconnectTip(state, chainparams))
2381 return false;
2382 fBlocksDisconnected = true;
2385 // Build list of new blocks to connect.
2386 std::vector<CBlockIndex*> vpindexToConnect;
2387 bool fContinue = true;
2388 int nHeight = pindexFork ? pindexFork->nHeight : -1;
2389 while (fContinue && nHeight != pindexMostWork->nHeight) {
2390 // Don't iterate the entire list of potential improvements toward the best tip, as we likely only need
2391 // a few blocks along the way.
2392 int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight);
2393 vpindexToConnect.clear();
2394 vpindexToConnect.reserve(nTargetHeight - nHeight);
2395 CBlockIndex *pindexIter = pindexMostWork->GetAncestor(nTargetHeight);
2396 while (pindexIter && pindexIter->nHeight != nHeight) {
2397 vpindexToConnect.push_back(pindexIter);
2398 pindexIter = pindexIter->pprev;
2400 nHeight = nTargetHeight;
2402 // Connect new blocks.
2403 BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) {
2404 if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr<const CBlock>(), connectTrace)) {
2405 if (state.IsInvalid()) {
2406 // The block violates a consensus rule.
2407 if (!state.CorruptionPossible())
2408 InvalidChainFound(vpindexToConnect.back());
2409 state = CValidationState();
2410 fInvalidFound = true;
2411 fContinue = false;
2412 // If we didn't actually connect the block, don't notify listeners about it
2413 connectTrace.blocksConnected.pop_back();
2414 break;
2415 } else {
2416 // A system error occurred (disk space, database error, ...).
2417 return false;
2419 } else {
2420 PruneBlockIndexCandidates();
2421 if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
2422 // We're in a better position than we were. Return temporarily to release the lock.
2423 fContinue = false;
2424 break;
2430 if (fBlocksDisconnected) {
2431 mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
2432 LimitMempoolSize(mempool, GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
2434 mempool.check(pcoinsTip);
2436 // Callbacks/notifications for a new best chain.
2437 if (fInvalidFound)
2438 CheckForkWarningConditionsOnNewFork(vpindexToConnect.back());
2439 else
2440 CheckForkWarningConditions();
2442 return true;
2445 static void NotifyHeaderTip() {
2446 bool fNotify = false;
2447 bool fInitialBlockDownload = false;
2448 static CBlockIndex* pindexHeaderOld = NULL;
2449 CBlockIndex* pindexHeader = NULL;
2451 LOCK(cs_main);
2452 pindexHeader = pindexBestHeader;
2454 if (pindexHeader != pindexHeaderOld) {
2455 fNotify = true;
2456 fInitialBlockDownload = IsInitialBlockDownload();
2457 pindexHeaderOld = pindexHeader;
2460 // Send block tip changed notifications without cs_main
2461 if (fNotify) {
2462 uiInterface.NotifyHeaderTip(fInitialBlockDownload, pindexHeader);
2467 * Make the best chain active, in multiple steps. The result is either failure
2468 * or an activated best chain. pblock is either NULL or a pointer to a block
2469 * that is already loaded (to avoid loading it again from disk).
2471 bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) {
2472 // Note that while we're often called here from ProcessNewBlock, this is
2473 // far from a guarantee. Things in the P2P/RPC will often end up calling
2474 // us in the middle of ProcessNewBlock - do not assume pblock is set
2475 // sanely for performance or correctness!
2477 CBlockIndex *pindexMostWork = NULL;
2478 CBlockIndex *pindexNewTip = NULL;
2479 do {
2480 boost::this_thread::interruption_point();
2481 if (ShutdownRequested())
2482 break;
2484 const CBlockIndex *pindexFork;
2485 ConnectTrace connectTrace;
2486 bool fInitialDownload;
2488 LOCK(cs_main);
2489 { // TODO: Temporarily ensure that mempool removals are notified before
2490 // connected transactions. This shouldn't matter, but the abandoned
2491 // state of transactions in our wallet is currently cleared when we
2492 // receive another notification and there is a race condition where
2493 // notification of a connected conflict might cause an outside process
2494 // to abandon a transaction and then have it inadvertently cleared by
2495 // the notification that the conflicted transaction was evicted.
2496 MemPoolConflictRemovalTracker mrt(mempool);
2497 CBlockIndex *pindexOldTip = chainActive.Tip();
2498 if (pindexMostWork == NULL) {
2499 pindexMostWork = FindMostWorkChain();
2502 // Whether we have anything to do at all.
2503 if (pindexMostWork == NULL || pindexMostWork == chainActive.Tip())
2504 return true;
2506 bool fInvalidFound = false;
2507 std::shared_ptr<const CBlock> nullBlockPtr;
2508 if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace))
2509 return false;
2511 if (fInvalidFound) {
2512 // Wipe cache, we may need another branch now.
2513 pindexMostWork = NULL;
2515 pindexNewTip = chainActive.Tip();
2516 pindexFork = chainActive.FindFork(pindexOldTip);
2517 fInitialDownload = IsInitialBlockDownload();
2519 // throw all transactions though the signal-interface
2521 } // MemPoolConflictRemovalTracker destroyed and conflict evictions are notified
2523 // Transactions in the connected block are notified
2524 for (const auto& pair : connectTrace.blocksConnected) {
2525 assert(pair.second);
2526 const CBlock& block = *(pair.second);
2527 for (unsigned int i = 0; i < block.vtx.size(); i++)
2528 GetMainSignals().SyncTransaction(*block.vtx[i], pair.first, i);
2531 // When we reach this point, we switched to a new tip (stored in pindexNewTip).
2533 // Notifications/callbacks that can run without cs_main
2535 // Notify external listeners about the new tip.
2536 GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);
2538 // Always notify the UI if a new block tip was connected
2539 if (pindexFork != pindexNewTip) {
2540 uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
2542 } while (pindexNewTip != pindexMostWork);
2543 CheckBlockIndex(chainparams.GetConsensus());
2545 // Write changes periodically to disk, after relay.
2546 if (!FlushStateToDisk(state, FLUSH_STATE_PERIODIC)) {
2547 return false;
2550 return true;
2554 bool PreciousBlock(CValidationState& state, const CChainParams& params, CBlockIndex *pindex)
2557 LOCK(cs_main);
2558 if (pindex->nChainWork < chainActive.Tip()->nChainWork) {
2559 // Nothing to do, this block is not at the tip.
2560 return true;
2562 if (chainActive.Tip()->nChainWork > nLastPreciousChainwork) {
2563 // The chain has been extended since the last call, reset the counter.
2564 nBlockReverseSequenceId = -1;
2566 nLastPreciousChainwork = chainActive.Tip()->nChainWork;
2567 setBlockIndexCandidates.erase(pindex);
2568 pindex->nSequenceId = nBlockReverseSequenceId;
2569 if (nBlockReverseSequenceId > std::numeric_limits<int32_t>::min()) {
2570 // We can't keep reducing the counter if somebody really wants to
2571 // call preciousblock 2**31-1 times on the same set of tips...
2572 nBlockReverseSequenceId--;
2574 if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && pindex->nChainTx) {
2575 setBlockIndexCandidates.insert(pindex);
2576 PruneBlockIndexCandidates();
2580 return ActivateBestChain(state, params);
2583 bool InvalidateBlock(CValidationState& state, const CChainParams& chainparams, CBlockIndex *pindex)
2585 AssertLockHeld(cs_main);
2587 // Mark the block itself as invalid.
2588 pindex->nStatus |= BLOCK_FAILED_VALID;
2589 setDirtyBlockIndex.insert(pindex);
2590 setBlockIndexCandidates.erase(pindex);
2592 while (chainActive.Contains(pindex)) {
2593 CBlockIndex *pindexWalk = chainActive.Tip();
2594 pindexWalk->nStatus |= BLOCK_FAILED_CHILD;
2595 setDirtyBlockIndex.insert(pindexWalk);
2596 setBlockIndexCandidates.erase(pindexWalk);
2597 // ActivateBestChain considers blocks already in chainActive
2598 // unconditionally valid already, so force disconnect away from it.
2599 if (!DisconnectTip(state, chainparams)) {
2600 mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
2601 return false;
2605 LimitMempoolSize(mempool, GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
2607 // The resulting new best tip may not be in setBlockIndexCandidates anymore, so
2608 // add it again.
2609 BlockMap::iterator it = mapBlockIndex.begin();
2610 while (it != mapBlockIndex.end()) {
2611 if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && !setBlockIndexCandidates.value_comp()(it->second, chainActive.Tip())) {
2612 setBlockIndexCandidates.insert(it->second);
2614 it++;
2617 InvalidChainFound(pindex);
2618 mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
2619 uiInterface.NotifyBlockTip(IsInitialBlockDownload(), pindex->pprev);
2620 return true;
2623 bool ResetBlockFailureFlags(CBlockIndex *pindex) {
2624 AssertLockHeld(cs_main);
2626 int nHeight = pindex->nHeight;
2628 // Remove the invalidity flag from this block and all its descendants.
2629 BlockMap::iterator it = mapBlockIndex.begin();
2630 while (it != mapBlockIndex.end()) {
2631 if (!it->second->IsValid() && it->second->GetAncestor(nHeight) == pindex) {
2632 it->second->nStatus &= ~BLOCK_FAILED_MASK;
2633 setDirtyBlockIndex.insert(it->second);
2634 if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && setBlockIndexCandidates.value_comp()(chainActive.Tip(), it->second)) {
2635 setBlockIndexCandidates.insert(it->second);
2637 if (it->second == pindexBestInvalid) {
2638 // Reset invalid block marker if it was pointing to one of those.
2639 pindexBestInvalid = NULL;
2642 it++;
2645 // Remove the invalidity flag from all ancestors too.
2646 while (pindex != NULL) {
2647 if (pindex->nStatus & BLOCK_FAILED_MASK) {
2648 pindex->nStatus &= ~BLOCK_FAILED_MASK;
2649 setDirtyBlockIndex.insert(pindex);
2651 pindex = pindex->pprev;
2653 return true;
2656 CBlockIndex* AddToBlockIndex(const CBlockHeader& block)
2658 // Check for duplicate
2659 uint256 hash = block.GetHash();
2660 BlockMap::iterator it = mapBlockIndex.find(hash);
2661 if (it != mapBlockIndex.end())
2662 return it->second;
2664 // Construct new block index object
2665 CBlockIndex* pindexNew = new CBlockIndex(block);
2666 assert(pindexNew);
2667 // We assign the sequence id to blocks only when the full data is available,
2668 // to avoid miners withholding blocks but broadcasting headers, to get a
2669 // competitive advantage.
2670 pindexNew->nSequenceId = 0;
2671 BlockMap::iterator mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first;
2672 pindexNew->phashBlock = &((*mi).first);
2673 BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock);
2674 if (miPrev != mapBlockIndex.end())
2676 pindexNew->pprev = (*miPrev).second;
2677 pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
2678 pindexNew->BuildSkip();
2680 pindexNew->nTimeMax = (pindexNew->pprev ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime) : pindexNew->nTime);
2681 pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew);
2682 pindexNew->RaiseValidity(BLOCK_VALID_TREE);
2683 if (pindexBestHeader == NULL || pindexBestHeader->nChainWork < pindexNew->nChainWork)
2684 pindexBestHeader = pindexNew;
2686 setDirtyBlockIndex.insert(pindexNew);
2688 return pindexNew;
2691 /** Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */
2692 bool ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBlockIndex *pindexNew, const CDiskBlockPos& pos)
2694 pindexNew->nTx = block.vtx.size();
2695 pindexNew->nChainTx = 0;
2696 pindexNew->nFile = pos.nFile;
2697 pindexNew->nDataPos = pos.nPos;
2698 pindexNew->nUndoPos = 0;
2699 pindexNew->nStatus |= BLOCK_HAVE_DATA;
2700 if (IsWitnessEnabled(pindexNew->pprev, Params().GetConsensus())) {
2701 pindexNew->nStatus |= BLOCK_OPT_WITNESS;
2703 pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
2704 setDirtyBlockIndex.insert(pindexNew);
2706 if (pindexNew->pprev == NULL || pindexNew->pprev->nChainTx) {
2707 // If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
2708 std::deque<CBlockIndex*> queue;
2709 queue.push_back(pindexNew);
2711 // Recursively process any descendant blocks that now may be eligible to be connected.
2712 while (!queue.empty()) {
2713 CBlockIndex *pindex = queue.front();
2714 queue.pop_front();
2715 pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
2717 LOCK(cs_nBlockSequenceId);
2718 pindex->nSequenceId = nBlockSequenceId++;
2720 if (chainActive.Tip() == NULL || !setBlockIndexCandidates.value_comp()(pindex, chainActive.Tip())) {
2721 setBlockIndexCandidates.insert(pindex);
2723 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex);
2724 while (range.first != range.second) {
2725 std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
2726 queue.push_back(it->second);
2727 range.first++;
2728 mapBlocksUnlinked.erase(it);
2731 } else {
2732 if (pindexNew->pprev && pindexNew->pprev->IsValid(BLOCK_VALID_TREE)) {
2733 mapBlocksUnlinked.insert(std::make_pair(pindexNew->pprev, pindexNew));
2737 return true;
2740 bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
2742 LOCK(cs_LastBlockFile);
2744 unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile;
2745 if (vinfoBlockFile.size() <= nFile) {
2746 vinfoBlockFile.resize(nFile + 1);
2749 if (!fKnown) {
2750 while (vinfoBlockFile[nFile].nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
2751 nFile++;
2752 if (vinfoBlockFile.size() <= nFile) {
2753 vinfoBlockFile.resize(nFile + 1);
2756 pos.nFile = nFile;
2757 pos.nPos = vinfoBlockFile[nFile].nSize;
2760 if ((int)nFile != nLastBlockFile) {
2761 if (!fKnown) {
2762 LogPrintf("Leaving block file %i: %s\n", nLastBlockFile, vinfoBlockFile[nLastBlockFile].ToString());
2764 FlushBlockFile(!fKnown);
2765 nLastBlockFile = nFile;
2768 vinfoBlockFile[nFile].AddBlock(nHeight, nTime);
2769 if (fKnown)
2770 vinfoBlockFile[nFile].nSize = std::max(pos.nPos + nAddSize, vinfoBlockFile[nFile].nSize);
2771 else
2772 vinfoBlockFile[nFile].nSize += nAddSize;
2774 if (!fKnown) {
2775 unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
2776 unsigned int nNewChunks = (vinfoBlockFile[nFile].nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
2777 if (nNewChunks > nOldChunks) {
2778 if (fPruneMode)
2779 fCheckForPruning = true;
2780 if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
2781 FILE *file = OpenBlockFile(pos);
2782 if (file) {
2783 LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
2784 AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
2785 fclose(file);
2788 else
2789 return state.Error("out of disk space");
2793 setDirtyFileInfo.insert(nFile);
2794 return true;
2797 bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
2799 pos.nFile = nFile;
2801 LOCK(cs_LastBlockFile);
2803 unsigned int nNewSize;
2804 pos.nPos = vinfoBlockFile[nFile].nUndoSize;
2805 nNewSize = vinfoBlockFile[nFile].nUndoSize += nAddSize;
2806 setDirtyFileInfo.insert(nFile);
2808 unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
2809 unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
2810 if (nNewChunks > nOldChunks) {
2811 if (fPruneMode)
2812 fCheckForPruning = true;
2813 if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
2814 FILE *file = OpenUndoFile(pos);
2815 if (file) {
2816 LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
2817 AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
2818 fclose(file);
2821 else
2822 return state.Error("out of disk space");
2825 return true;
2828 bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW)
2830 // Check proof of work matches claimed amount
2831 if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
2832 return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");
2834 return true;
2837 bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
2839 // These are checks that are independent of context.
2841 if (block.fChecked)
2842 return true;
2844 // Check that the header is valid (particularly PoW). This is mostly
2845 // redundant with the call in AcceptBlockHeader.
2846 if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW))
2847 return false;
2849 // Check the merkle root.
2850 if (fCheckMerkleRoot) {
2851 bool mutated;
2852 uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated);
2853 if (block.hashMerkleRoot != hashMerkleRoot2)
2854 return state.DoS(100, false, REJECT_INVALID, "bad-txnmrklroot", true, "hashMerkleRoot mismatch");
2856 // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
2857 // of transactions in a block without affecting the merkle root of a block,
2858 // while still invalidating it.
2859 if (mutated)
2860 return state.DoS(100, false, REJECT_INVALID, "bad-txns-duplicate", true, "duplicate transaction");
2863 // All potential-corruption validation must be done before we do any
2864 // transaction validation, as otherwise we may mark the header as invalid
2865 // because we receive the wrong transactions for it.
2866 // Note that witness malleability is checked in ContextualCheckBlock, so no
2867 // checks that use witness data may be performed here.
2869 // Size limits
2870 if (block.vtx.empty() || block.vtx.size() > MAX_BLOCK_BASE_SIZE || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) > MAX_BLOCK_BASE_SIZE)
2871 return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed");
2873 // First transaction must be coinbase, the rest must not be
2874 if (block.vtx.empty() || !block.vtx[0]->IsCoinBase())
2875 return state.DoS(100, false, REJECT_INVALID, "bad-cb-missing", false, "first tx is not coinbase");
2876 for (unsigned int i = 1; i < block.vtx.size(); i++)
2877 if (block.vtx[i]->IsCoinBase())
2878 return state.DoS(100, false, REJECT_INVALID, "bad-cb-multiple", false, "more than one coinbase");
2880 // Check transactions
2881 for (const auto& tx : block.vtx)
2882 if (!CheckTransaction(*tx, state, false))
2883 return state.Invalid(false, state.GetRejectCode(), state.GetRejectReason(),
2884 strprintf("Transaction check failed (tx hash %s) %s", tx->GetHash().ToString(), state.GetDebugMessage()));
2886 unsigned int nSigOps = 0;
2887 for (const auto& tx : block.vtx)
2889 nSigOps += GetLegacySigOpCount(*tx);
2891 if (nSigOps * WITNESS_SCALE_FACTOR > MAX_BLOCK_SIGOPS_COST)
2892 return state.DoS(100, false, REJECT_INVALID, "bad-blk-sigops", false, "out-of-bounds SigOpCount");
2894 if (fCheckPOW && fCheckMerkleRoot)
2895 block.fChecked = true;
2897 return true;
2900 static bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidationState& state, const CChainParams& chainparams, const uint256& hash)
2902 if (*pindexPrev->phashBlock == chainparams.GetConsensus().hashGenesisBlock)
2903 return true;
2905 int nHeight = pindexPrev->nHeight+1;
2906 // Don't accept any forks from the main chain prior to last checkpoint
2907 CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainparams.Checkpoints());
2908 if (pcheckpoint && nHeight < pcheckpoint->nHeight)
2909 return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight));
2911 return true;
2914 bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params)
2916 LOCK(cs_main);
2917 return (VersionBitsState(pindexPrev, params, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_ACTIVE);
2920 // Compute at which vout of the block's coinbase transaction the witness
2921 // commitment occurs, or -1 if not found.
2922 static int GetWitnessCommitmentIndex(const CBlock& block)
2924 int commitpos = -1;
2925 for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
2926 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) {
2927 commitpos = o;
2930 return commitpos;
2933 void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
2935 int commitpos = GetWitnessCommitmentIndex(block);
2936 static const std::vector<unsigned char> nonce(32, 0x00);
2937 if (commitpos != -1 && IsWitnessEnabled(pindexPrev, consensusParams) && !block.vtx[0]->HasWitness()) {
2938 CMutableTransaction tx(*block.vtx[0]);
2939 tx.vin[0].scriptWitness.stack.resize(1);
2940 tx.vin[0].scriptWitness.stack[0] = nonce;
2941 block.vtx[0] = MakeTransactionRef(std::move(tx));
2945 std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
2947 std::vector<unsigned char> commitment;
2948 int commitpos = GetWitnessCommitmentIndex(block);
2949 std::vector<unsigned char> ret(32, 0x00);
2950 if (consensusParams.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nTimeout != 0) {
2951 if (commitpos == -1) {
2952 uint256 witnessroot = BlockWitnessMerkleRoot(block, NULL);
2953 CHash256().Write(witnessroot.begin(), 32).Write(&ret[0], 32).Finalize(witnessroot.begin());
2954 CTxOut out;
2955 out.nValue = 0;
2956 out.scriptPubKey.resize(38);
2957 out.scriptPubKey[0] = OP_RETURN;
2958 out.scriptPubKey[1] = 0x24;
2959 out.scriptPubKey[2] = 0xaa;
2960 out.scriptPubKey[3] = 0x21;
2961 out.scriptPubKey[4] = 0xa9;
2962 out.scriptPubKey[5] = 0xed;
2963 memcpy(&out.scriptPubKey[6], witnessroot.begin(), 32);
2964 commitment = std::vector<unsigned char>(out.scriptPubKey.begin(), out.scriptPubKey.end());
2965 CMutableTransaction tx(*block.vtx[0]);
2966 tx.vout.push_back(out);
2967 block.vtx[0] = MakeTransactionRef(std::move(tx));
2970 UpdateUncommittedBlockStructures(block, pindexPrev, consensusParams);
2971 return commitment;
2974 bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
2976 const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
2977 // Check proof of work
2978 if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
2979 return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");
2981 // Check timestamp against prev
2982 if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
2983 return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
2985 // Check timestamp
2986 if (block.GetBlockTime() > nAdjustedTime + 2 * 60 * 60)
2987 return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
2989 // Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded:
2990 // check for version 2, 3 and 4 upgrades
2991 if((block.nVersion < 2 && nHeight >= consensusParams.BIP34Height) ||
2992 (block.nVersion < 3 && nHeight >= consensusParams.BIP66Height) ||
2993 (block.nVersion < 4 && nHeight >= consensusParams.BIP65Height))
2994 return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", block.nVersion),
2995 strprintf("rejected nVersion=0x%08x block", block.nVersion));
2997 return true;
3000 bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
3002 const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
3004 // Start enforcing BIP113 (Median Time Past) using versionbits logic.
3005 int nLockTimeFlags = 0;
3006 if (VersionBitsState(pindexPrev, consensusParams, Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
3007 nLockTimeFlags |= LOCKTIME_MEDIAN_TIME_PAST;
3010 int64_t nLockTimeCutoff = (nLockTimeFlags & LOCKTIME_MEDIAN_TIME_PAST)
3011 ? pindexPrev->GetMedianTimePast()
3012 : block.GetBlockTime();
3014 // Check that all transactions are finalized
3015 for (const auto& tx : block.vtx) {
3016 if (!IsFinalTx(*tx, nHeight, nLockTimeCutoff)) {
3017 return state.DoS(10, false, REJECT_INVALID, "bad-txns-nonfinal", false, "non-final transaction");
3021 // Enforce rule that the coinbase starts with serialized block height
3022 if (nHeight >= consensusParams.BIP34Height)
3024 CScript expect = CScript() << nHeight;
3025 if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() ||
3026 !std::equal(expect.begin(), expect.end(), block.vtx[0]->vin[0].scriptSig.begin())) {
3027 return state.DoS(100, false, REJECT_INVALID, "bad-cb-height", false, "block height mismatch in coinbase");
3031 // Validation for witness commitments.
3032 // * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the
3033 // coinbase (where 0x0000....0000 is used instead).
3034 // * The coinbase scriptWitness is a stack of a single 32-byte vector, containing a witness nonce (unconstrained).
3035 // * We build a merkle tree with all those witness hashes as leaves (similar to the hashMerkleRoot in the block header).
3036 // * There must be at least one output whose scriptPubKey is a single 36-byte push, the first 4 bytes of which are
3037 // {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness nonce). In case there are
3038 // multiple, the last one is used.
3039 bool fHaveWitness = false;
3040 if (VersionBitsState(pindexPrev, consensusParams, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_ACTIVE) {
3041 int commitpos = GetWitnessCommitmentIndex(block);
3042 if (commitpos != -1) {
3043 bool malleated = false;
3044 uint256 hashWitness = BlockWitnessMerkleRoot(block, &malleated);
3045 // The malleation check is ignored; as the transaction tree itself
3046 // already does not permit it, it is impossible to trigger in the
3047 // witness tree.
3048 if (block.vtx[0]->vin[0].scriptWitness.stack.size() != 1 || block.vtx[0]->vin[0].scriptWitness.stack[0].size() != 32) {
3049 return state.DoS(100, false, REJECT_INVALID, "bad-witness-nonce-size", true, strprintf("%s : invalid witness nonce size", __func__));
3051 CHash256().Write(hashWitness.begin(), 32).Write(&block.vtx[0]->vin[0].scriptWitness.stack[0][0], 32).Finalize(hashWitness.begin());
3052 if (memcmp(hashWitness.begin(), &block.vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
3053 return state.DoS(100, false, REJECT_INVALID, "bad-witness-merkle-match", true, strprintf("%s : witness merkle commitment mismatch", __func__));
3055 fHaveWitness = true;
3059 // No witness data is allowed in blocks that don't commit to witness data, as this would otherwise leave room for spam
3060 if (!fHaveWitness) {
3061 for (size_t i = 0; i < block.vtx.size(); i++) {
3062 if (block.vtx[i]->HasWitness()) {
3063 return state.DoS(100, false, REJECT_INVALID, "unexpected-witness", true, strprintf("%s : unexpected witness data found", __func__));
3068 // After the coinbase witness nonce and commitment are verified,
3069 // we can check if the block weight passes (before we've checked the
3070 // coinbase witness, it would be possible for the weight to be too
3071 // large by filling up the coinbase witness, which doesn't change
3072 // the block hash, so we couldn't mark the block as permanently
3073 // failed).
3074 if (GetBlockWeight(block) > MAX_BLOCK_WEIGHT) {
3075 return state.DoS(100, false, REJECT_INVALID, "bad-blk-weight", false, strprintf("%s : weight limit failed", __func__));
3078 return true;
3081 static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
3083 AssertLockHeld(cs_main);
3084 // Check for duplicate
3085 uint256 hash = block.GetHash();
3086 BlockMap::iterator miSelf = mapBlockIndex.find(hash);
3087 CBlockIndex *pindex = NULL;
3088 if (hash != chainparams.GetConsensus().hashGenesisBlock) {
3090 if (miSelf != mapBlockIndex.end()) {
3091 // Block header is already known.
3092 pindex = miSelf->second;
3093 if (ppindex)
3094 *ppindex = pindex;
3095 if (pindex->nStatus & BLOCK_FAILED_MASK)
3096 return state.Invalid(error("%s: block %s is marked invalid", __func__, hash.ToString()), 0, "duplicate");
3097 return true;
3100 if (!CheckBlockHeader(block, state, chainparams.GetConsensus()))
3101 return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
3103 // Get prev block index
3104 CBlockIndex* pindexPrev = NULL;
3105 BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
3106 if (mi == mapBlockIndex.end())
3107 return state.DoS(10, error("%s: prev block not found", __func__), 0, "bad-prevblk");
3108 pindexPrev = (*mi).second;
3109 if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
3110 return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
3112 assert(pindexPrev);
3113 if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, hash))
3114 return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
3116 if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev, GetAdjustedTime()))
3117 return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
3119 if (pindex == NULL)
3120 pindex = AddToBlockIndex(block);
3122 if (ppindex)
3123 *ppindex = pindex;
3125 CheckBlockIndex(chainparams.GetConsensus());
3127 return true;
3130 // Exposed wrapper for AcceptBlockHeader
3131 bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, CValidationState& state, const CChainParams& chainparams, const CBlockIndex** ppindex)
3134 LOCK(cs_main);
3135 for (const CBlockHeader& header : headers) {
3136 CBlockIndex *pindex = NULL; // Use a temp pindex instead of ppindex to avoid a const_cast
3137 if (!AcceptBlockHeader(header, state, chainparams, &pindex)) {
3138 return false;
3140 if (ppindex) {
3141 *ppindex = pindex;
3145 NotifyHeaderTip();
3146 return true;
3149 /** Store block on disk. If dbp is non-NULL, the file is known to already reside on disk */
3150 static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const CDiskBlockPos* dbp, bool* fNewBlock)
3152 const CBlock& block = *pblock;
3154 if (fNewBlock) *fNewBlock = false;
3155 AssertLockHeld(cs_main);
3157 CBlockIndex *pindexDummy = NULL;
3158 CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
3160 if (!AcceptBlockHeader(block, state, chainparams, &pindex))
3161 return false;
3163 // Try to process all requested blocks that we don't have, but only
3164 // process an unrequested block if it's new and has enough work to
3165 // advance our tip, and isn't too many blocks ahead.
3166 bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA;
3167 bool fHasMoreWork = (chainActive.Tip() ? pindex->nChainWork > chainActive.Tip()->nChainWork : true);
3168 // Blocks that are too out-of-order needlessly limit the effectiveness of
3169 // pruning, because pruning will not delete block files that contain any
3170 // blocks which are too close in height to the tip. Apply this test
3171 // regardless of whether pruning is enabled; it should generally be safe to
3172 // not process unrequested blocks.
3173 bool fTooFarAhead = (pindex->nHeight > int(chainActive.Height() + MIN_BLOCKS_TO_KEEP));
3175 // TODO: Decouple this function from the block download logic by removing fRequested
3176 // This requires some new chain datastructure to efficiently look up if a
3177 // block is in a chain leading to a candidate for best tip, despite not
3178 // being such a candidate itself.
3180 // TODO: deal better with return value and error conditions for duplicate
3181 // and unrequested blocks.
3182 if (fAlreadyHave) return true;
3183 if (!fRequested) { // If we didn't ask for it:
3184 if (pindex->nTx != 0) return true; // This is a previously-processed block that was pruned
3185 if (!fHasMoreWork) return true; // Don't process less-work chains
3186 if (fTooFarAhead) return true; // Block height is too high
3188 if (fNewBlock) *fNewBlock = true;
3190 if (!CheckBlock(block, state, chainparams.GetConsensus(), GetAdjustedTime()) ||
3191 !ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindex->pprev)) {
3192 if (state.IsInvalid() && !state.CorruptionPossible()) {
3193 pindex->nStatus |= BLOCK_FAILED_VALID;
3194 setDirtyBlockIndex.insert(pindex);
3196 return error("%s: %s", __func__, FormatStateMessage(state));
3199 // Header is valid/has work, merkle tree and segwit merkle tree are good...RELAY NOW
3200 // (but if it does not build on our best tip, let the SendMessages loop relay it)
3201 if (!IsInitialBlockDownload() && chainActive.Tip() == pindex->pprev)
3202 GetMainSignals().NewPoWValidBlock(pindex, pblock);
3204 int nHeight = pindex->nHeight;
3206 // Write block to history file
3207 try {
3208 unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
3209 CDiskBlockPos blockPos;
3210 if (dbp != NULL)
3211 blockPos = *dbp;
3212 if (!FindBlockPos(state, blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != NULL))
3213 return error("AcceptBlock(): FindBlockPos failed");
3214 if (dbp == NULL)
3215 if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
3216 AbortNode(state, "Failed to write block");
3217 if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
3218 return error("AcceptBlock(): ReceivedBlockTransactions failed");
3219 } catch (const std::runtime_error& e) {
3220 return AbortNode(state, std::string("System error: ") + e.what());
3223 if (fCheckForPruning)
3224 FlushStateToDisk(state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
3226 return true;
3229 bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<const CBlock> pblock, bool fForceProcessing, bool *fNewBlock)
3232 LOCK(cs_main);
3234 // Store to disk
3235 CBlockIndex *pindex = NULL;
3236 if (fNewBlock) *fNewBlock = false;
3237 CValidationState state;
3238 bool ret = AcceptBlock(pblock, state, chainparams, &pindex, fForceProcessing, NULL, fNewBlock);
3239 CheckBlockIndex(chainparams.GetConsensus());
3240 if (!ret) {
3241 GetMainSignals().BlockChecked(*pblock, state);
3242 return error("%s: AcceptBlock FAILED", __func__);
3246 NotifyHeaderTip();
3248 CValidationState state; // Only used to report errors, not invalidity - ignore it
3249 if (!ActivateBestChain(state, chainparams, pblock))
3250 return error("%s: ActivateBestChain failed", __func__);
3252 return true;
3255 bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
3257 AssertLockHeld(cs_main);
3258 assert(pindexPrev && pindexPrev == chainActive.Tip());
3259 if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, block.GetHash()))
3260 return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
3262 CCoinsViewCache viewNew(pcoinsTip);
3263 CBlockIndex indexDummy(block);
3264 indexDummy.pprev = pindexPrev;
3265 indexDummy.nHeight = pindexPrev->nHeight + 1;
3267 // NOTE: CheckBlockHeader is called by CheckBlock
3268 if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev, GetAdjustedTime()))
3269 return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, FormatStateMessage(state));
3270 if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot))
3271 return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
3272 if (!ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindexPrev))
3273 return error("%s: Consensus::ContextualCheckBlock: %s", __func__, FormatStateMessage(state));
3274 if (!ConnectBlock(block, state, &indexDummy, viewNew, chainparams, true))
3275 return false;
3276 assert(state.IsValid());
3278 return true;
3282 * BLOCK PRUNING CODE
3285 /* Calculate the amount of disk space the block & undo files currently use */
3286 uint64_t CalculateCurrentUsage()
3288 uint64_t retval = 0;
3289 BOOST_FOREACH(const CBlockFileInfo &file, vinfoBlockFile) {
3290 retval += file.nSize + file.nUndoSize;
3292 return retval;
3295 /* Prune a block file (modify associated database entries)*/
3296 void PruneOneBlockFile(const int fileNumber)
3298 for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) {
3299 CBlockIndex* pindex = it->second;
3300 if (pindex->nFile == fileNumber) {
3301 pindex->nStatus &= ~BLOCK_HAVE_DATA;
3302 pindex->nStatus &= ~BLOCK_HAVE_UNDO;
3303 pindex->nFile = 0;
3304 pindex->nDataPos = 0;
3305 pindex->nUndoPos = 0;
3306 setDirtyBlockIndex.insert(pindex);
3308 // Prune from mapBlocksUnlinked -- any block we prune would have
3309 // to be downloaded again in order to consider its chain, at which
3310 // point it would be considered as a candidate for
3311 // mapBlocksUnlinked or setBlockIndexCandidates.
3312 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex->pprev);
3313 while (range.first != range.second) {
3314 std::multimap<CBlockIndex *, CBlockIndex *>::iterator _it = range.first;
3315 range.first++;
3316 if (_it->second == pindex) {
3317 mapBlocksUnlinked.erase(_it);
3323 vinfoBlockFile[fileNumber].SetNull();
3324 setDirtyFileInfo.insert(fileNumber);
3328 void UnlinkPrunedFiles(std::set<int>& setFilesToPrune)
3330 for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
3331 CDiskBlockPos pos(*it, 0);
3332 boost::filesystem::remove(GetBlockPosFilename(pos, "blk"));
3333 boost::filesystem::remove(GetBlockPosFilename(pos, "rev"));
3334 LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
3338 /* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
3339 void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight)
3341 assert(fPruneMode && nManualPruneHeight > 0);
3343 LOCK2(cs_main, cs_LastBlockFile);
3344 if (chainActive.Tip() == NULL)
3345 return;
3347 // last block to prune is the lesser of (user-specified height, MIN_BLOCKS_TO_KEEP from the tip)
3348 unsigned int nLastBlockWeCanPrune = std::min((unsigned)nManualPruneHeight, chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP);
3349 int count=0;
3350 for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
3351 if (vinfoBlockFile[fileNumber].nSize == 0 || vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
3352 continue;
3353 PruneOneBlockFile(fileNumber);
3354 setFilesToPrune.insert(fileNumber);
3355 count++;
3357 LogPrintf("Prune (Manual): prune_height=%d removed %d blk/rev pairs\n", nLastBlockWeCanPrune, count);
3360 /* This function is called from the RPC code for pruneblockchain */
3361 void PruneBlockFilesManual(int nManualPruneHeight)
3363 CValidationState state;
3364 FlushStateToDisk(state, FLUSH_STATE_NONE, nManualPruneHeight);
3367 /* Calculate the block/rev files that should be deleted to remain under target*/
3368 void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight)
3370 LOCK2(cs_main, cs_LastBlockFile);
3371 if (chainActive.Tip() == NULL || nPruneTarget == 0) {
3372 return;
3374 if ((uint64_t)chainActive.Tip()->nHeight <= nPruneAfterHeight) {
3375 return;
3378 unsigned int nLastBlockWeCanPrune = chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP;
3379 uint64_t nCurrentUsage = CalculateCurrentUsage();
3380 // We don't check to prune until after we've allocated new space for files
3381 // So we should leave a buffer under our target to account for another allocation
3382 // before the next pruning.
3383 uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
3384 uint64_t nBytesToPrune;
3385 int count=0;
3387 if (nCurrentUsage + nBuffer >= nPruneTarget) {
3388 for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
3389 nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize;
3391 if (vinfoBlockFile[fileNumber].nSize == 0)
3392 continue;
3394 if (nCurrentUsage + nBuffer < nPruneTarget) // are we below our target?
3395 break;
3397 // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
3398 if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
3399 continue;
3401 PruneOneBlockFile(fileNumber);
3402 // Queue up the files for removal
3403 setFilesToPrune.insert(fileNumber);
3404 nCurrentUsage -= nBytesToPrune;
3405 count++;
3409 LogPrint("prune", "Prune: target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n",
3410 nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
3411 ((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
3412 nLastBlockWeCanPrune, count);
3415 bool CheckDiskSpace(uint64_t nAdditionalBytes)
3417 uint64_t nFreeBytesAvailable = boost::filesystem::space(GetDataDir()).available;
3419 // Check for nMinDiskSpace bytes (currently 50MB)
3420 if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
3421 return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
3423 return true;
3426 FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
3428 if (pos.IsNull())
3429 return NULL;
3430 boost::filesystem::path path = GetBlockPosFilename(pos, prefix);
3431 boost::filesystem::create_directories(path.parent_path());
3432 FILE* file = fopen(path.string().c_str(), "rb+");
3433 if (!file && !fReadOnly)
3434 file = fopen(path.string().c_str(), "wb+");
3435 if (!file) {
3436 LogPrintf("Unable to open file %s\n", path.string());
3437 return NULL;
3439 if (pos.nPos) {
3440 if (fseek(file, pos.nPos, SEEK_SET)) {
3441 LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
3442 fclose(file);
3443 return NULL;
3446 return file;
3449 FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
3450 return OpenDiskFile(pos, "blk", fReadOnly);
3453 FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
3454 return OpenDiskFile(pos, "rev", fReadOnly);
3457 boost::filesystem::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
3459 return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
3462 CBlockIndex * InsertBlockIndex(uint256 hash)
3464 if (hash.IsNull())
3465 return NULL;
3467 // Return existing
3468 BlockMap::iterator mi = mapBlockIndex.find(hash);
3469 if (mi != mapBlockIndex.end())
3470 return (*mi).second;
3472 // Create new
3473 CBlockIndex* pindexNew = new CBlockIndex();
3474 if (!pindexNew)
3475 throw std::runtime_error(std::string(__func__) + ": new CBlockIndex failed");
3476 mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first;
3477 pindexNew->phashBlock = &((*mi).first);
3479 return pindexNew;
3482 bool static LoadBlockIndexDB(const CChainParams& chainparams)
3484 if (!pblocktree->LoadBlockIndexGuts(InsertBlockIndex))
3485 return false;
3487 boost::this_thread::interruption_point();
3489 // Calculate nChainWork
3490 std::vector<std::pair<int, CBlockIndex*> > vSortedByHeight;
3491 vSortedByHeight.reserve(mapBlockIndex.size());
3492 BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
3494 CBlockIndex* pindex = item.second;
3495 vSortedByHeight.push_back(std::make_pair(pindex->nHeight, pindex));
3497 sort(vSortedByHeight.begin(), vSortedByHeight.end());
3498 BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
3500 CBlockIndex* pindex = item.second;
3501 pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
3502 pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime);
3503 // We can link the chain of blocks for which we've received transactions at some point.
3504 // Pruned nodes may have deleted the block.
3505 if (pindex->nTx > 0) {
3506 if (pindex->pprev) {
3507 if (pindex->pprev->nChainTx) {
3508 pindex->nChainTx = pindex->pprev->nChainTx + pindex->nTx;
3509 } else {
3510 pindex->nChainTx = 0;
3511 mapBlocksUnlinked.insert(std::make_pair(pindex->pprev, pindex));
3513 } else {
3514 pindex->nChainTx = pindex->nTx;
3517 if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->nChainTx || pindex->pprev == NULL))
3518 setBlockIndexCandidates.insert(pindex);
3519 if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
3520 pindexBestInvalid = pindex;
3521 if (pindex->pprev)
3522 pindex->BuildSkip();
3523 if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == NULL || CBlockIndexWorkComparator()(pindexBestHeader, pindex)))
3524 pindexBestHeader = pindex;
3527 // Load block file info
3528 pblocktree->ReadLastBlockFile(nLastBlockFile);
3529 vinfoBlockFile.resize(nLastBlockFile + 1);
3530 LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile);
3531 for (int nFile = 0; nFile <= nLastBlockFile; nFile++) {
3532 pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
3534 LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString());
3535 for (int nFile = nLastBlockFile + 1; true; nFile++) {
3536 CBlockFileInfo info;
3537 if (pblocktree->ReadBlockFileInfo(nFile, info)) {
3538 vinfoBlockFile.push_back(info);
3539 } else {
3540 break;
3544 // Check presence of blk files
3545 LogPrintf("Checking all blk files are present...\n");
3546 std::set<int> setBlkDataFiles;
3547 BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
3549 CBlockIndex* pindex = item.second;
3550 if (pindex->nStatus & BLOCK_HAVE_DATA) {
3551 setBlkDataFiles.insert(pindex->nFile);
3554 for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++)
3556 CDiskBlockPos pos(*it, 0);
3557 if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) {
3558 return false;
3562 // Check whether we have ever pruned block & undo files
3563 pblocktree->ReadFlag("prunedblockfiles", fHavePruned);
3564 if (fHavePruned)
3565 LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n");
3567 // Check whether we need to continue reindexing
3568 bool fReindexing = false;
3569 pblocktree->ReadReindexing(fReindexing);
3570 fReindex |= fReindexing;
3572 // Check whether we have a transaction index
3573 pblocktree->ReadFlag("txindex", fTxIndex);
3574 LogPrintf("%s: transaction index %s\n", __func__, fTxIndex ? "enabled" : "disabled");
3576 // Load pointer to end of best chain
3577 BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
3578 if (it == mapBlockIndex.end())
3579 return true;
3580 chainActive.SetTip(it->second);
3582 PruneBlockIndexCandidates();
3584 LogPrintf("%s: hashBestChain=%s height=%d date=%s progress=%f\n", __func__,
3585 chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
3586 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
3587 GuessVerificationProgress(chainparams.TxData(), chainActive.Tip()));
3589 return true;
3592 CVerifyDB::CVerifyDB()
3594 uiInterface.ShowProgress(_("Verifying blocks..."), 0);
3597 CVerifyDB::~CVerifyDB()
3599 uiInterface.ShowProgress("", 100);
3602 bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
3604 LOCK(cs_main);
3605 if (chainActive.Tip() == NULL || chainActive.Tip()->pprev == NULL)
3606 return true;
3608 // Verify blocks in the best chain
3609 if (nCheckDepth <= 0)
3610 nCheckDepth = 1000000000; // suffices until the year 19000
3611 if (nCheckDepth > chainActive.Height())
3612 nCheckDepth = chainActive.Height();
3613 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
3614 LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
3615 CCoinsViewCache coins(coinsview);
3616 CBlockIndex* pindexState = chainActive.Tip();
3617 CBlockIndex* pindexFailure = NULL;
3618 int nGoodTransactions = 0;
3619 CValidationState state;
3620 int reportDone = 0;
3621 LogPrintf("[0%%]...");
3622 for (CBlockIndex* pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev)
3624 boost::this_thread::interruption_point();
3625 int percentageDone = std::max(1, std::min(99, (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
3626 if (reportDone < percentageDone/10) {
3627 // report every 10% step
3628 LogPrintf("[%d%%]...", percentageDone);
3629 reportDone = percentageDone/10;
3631 uiInterface.ShowProgress(_("Verifying blocks..."), percentageDone);
3632 if (pindex->nHeight < chainActive.Height()-nCheckDepth)
3633 break;
3634 if (fPruneMode && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
3635 // If pruning, only go back as far as we have data.
3636 LogPrintf("VerifyDB(): block verification stopping at height %d (pruning, no data)\n", pindex->nHeight);
3637 break;
3639 CBlock block;
3640 // check level 0: read from disk
3641 if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
3642 return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3643 // check level 1: verify block validity
3644 if (nCheckLevel >= 1 && !CheckBlock(block, state, chainparams.GetConsensus()))
3645 return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
3646 pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
3647 // check level 2: verify undo validity
3648 if (nCheckLevel >= 2 && pindex) {
3649 CBlockUndo undo;
3650 CDiskBlockPos pos = pindex->GetUndoPos();
3651 if (!pos.IsNull()) {
3652 if (!UndoReadFromDisk(undo, pos, pindex->pprev->GetBlockHash()))
3653 return error("VerifyDB(): *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
3656 // check level 3: check for inconsistencies during memory-only disconnect of tip blocks
3657 if (nCheckLevel >= 3 && pindex == pindexState && (coins.DynamicMemoryUsage() + pcoinsTip->DynamicMemoryUsage()) <= nCoinCacheUsage) {
3658 bool fClean = true;
3659 if (!DisconnectBlock(block, state, pindex, coins, &fClean))
3660 return error("VerifyDB(): *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3661 pindexState = pindex->pprev;
3662 if (!fClean) {
3663 nGoodTransactions = 0;
3664 pindexFailure = pindex;
3665 } else
3666 nGoodTransactions += block.vtx.size();
3668 if (ShutdownRequested())
3669 return true;
3671 if (pindexFailure)
3672 return error("VerifyDB(): *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainActive.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
3674 // check level 4: try reconnecting blocks
3675 if (nCheckLevel >= 4) {
3676 CBlockIndex *pindex = pindexState;
3677 while (pindex != chainActive.Tip()) {
3678 boost::this_thread::interruption_point();
3679 uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, 100 - (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * 50))));
3680 pindex = chainActive.Next(pindex);
3681 CBlock block;
3682 if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
3683 return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3684 if (!ConnectBlock(block, state, pindex, coins, chainparams))
3685 return error("VerifyDB(): *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3689 LogPrintf("[DONE].\n");
3690 LogPrintf("No coin database inconsistencies in last %i blocks (%i transactions)\n", chainActive.Height() - pindexState->nHeight, nGoodTransactions);
3692 return true;
3695 bool RewindBlockIndex(const CChainParams& params)
3697 LOCK(cs_main);
3699 int nHeight = 1;
3700 while (nHeight <= chainActive.Height()) {
3701 if (IsWitnessEnabled(chainActive[nHeight - 1], params.GetConsensus()) && !(chainActive[nHeight]->nStatus & BLOCK_OPT_WITNESS)) {
3702 break;
3704 nHeight++;
3707 // nHeight is now the height of the first insufficiently-validated block, or tipheight + 1
3708 CValidationState state;
3709 CBlockIndex* pindex = chainActive.Tip();
3710 while (chainActive.Height() >= nHeight) {
3711 if (fPruneMode && !(chainActive.Tip()->nStatus & BLOCK_HAVE_DATA)) {
3712 // If pruning, don't try rewinding past the HAVE_DATA point;
3713 // since older blocks can't be served anyway, there's
3714 // no need to walk further, and trying to DisconnectTip()
3715 // will fail (and require a needless reindex/redownload
3716 // of the blockchain).
3717 break;
3719 if (!DisconnectTip(state, params, true)) {
3720 return error("RewindBlockIndex: unable to disconnect block at height %i", pindex->nHeight);
3722 // Occasionally flush state to disk.
3723 if (!FlushStateToDisk(state, FLUSH_STATE_PERIODIC))
3724 return false;
3727 // Reduce validity flag and have-data flags.
3728 // We do this after actual disconnecting, otherwise we'll end up writing the lack of data
3729 // to disk before writing the chainstate, resulting in a failure to continue if interrupted.
3730 for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
3731 CBlockIndex* pindexIter = it->second;
3733 // Note: If we encounter an insufficiently validated block that
3734 // is on chainActive, it must be because we are a pruning node, and
3735 // this block or some successor doesn't HAVE_DATA, so we were unable to
3736 // rewind all the way. Blocks remaining on chainActive at this point
3737 // must not have their validity reduced.
3738 if (IsWitnessEnabled(pindexIter->pprev, params.GetConsensus()) && !(pindexIter->nStatus & BLOCK_OPT_WITNESS) && !chainActive.Contains(pindexIter)) {
3739 // Reduce validity
3740 pindexIter->nStatus = std::min<unsigned int>(pindexIter->nStatus & BLOCK_VALID_MASK, BLOCK_VALID_TREE) | (pindexIter->nStatus & ~BLOCK_VALID_MASK);
3741 // Remove have-data flags.
3742 pindexIter->nStatus &= ~(BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO);
3743 // Remove storage location.
3744 pindexIter->nFile = 0;
3745 pindexIter->nDataPos = 0;
3746 pindexIter->nUndoPos = 0;
3747 // Remove various other things
3748 pindexIter->nTx = 0;
3749 pindexIter->nChainTx = 0;
3750 pindexIter->nSequenceId = 0;
3751 // Make sure it gets written.
3752 setDirtyBlockIndex.insert(pindexIter);
3753 // Update indexes
3754 setBlockIndexCandidates.erase(pindexIter);
3755 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> ret = mapBlocksUnlinked.equal_range(pindexIter->pprev);
3756 while (ret.first != ret.second) {
3757 if (ret.first->second == pindexIter) {
3758 mapBlocksUnlinked.erase(ret.first++);
3759 } else {
3760 ++ret.first;
3763 } else if (pindexIter->IsValid(BLOCK_VALID_TRANSACTIONS) && pindexIter->nChainTx) {
3764 setBlockIndexCandidates.insert(pindexIter);
3768 PruneBlockIndexCandidates();
3770 CheckBlockIndex(params.GetConsensus());
3772 if (!FlushStateToDisk(state, FLUSH_STATE_ALWAYS)) {
3773 return false;
3776 return true;
3779 // May NOT be used after any connections are up as much
3780 // of the peer-processing logic assumes a consistent
3781 // block index state
3782 void UnloadBlockIndex()
3784 LOCK(cs_main);
3785 setBlockIndexCandidates.clear();
3786 chainActive.SetTip(NULL);
3787 pindexBestInvalid = NULL;
3788 pindexBestHeader = NULL;
3789 mempool.clear();
3790 mapBlocksUnlinked.clear();
3791 vinfoBlockFile.clear();
3792 nLastBlockFile = 0;
3793 nBlockSequenceId = 1;
3794 setDirtyBlockIndex.clear();
3795 setDirtyFileInfo.clear();
3796 versionbitscache.Clear();
3797 for (int b = 0; b < VERSIONBITS_NUM_BITS; b++) {
3798 warningcache[b].clear();
3801 BOOST_FOREACH(BlockMap::value_type& entry, mapBlockIndex) {
3802 delete entry.second;
3804 mapBlockIndex.clear();
3805 fHavePruned = false;
3808 bool LoadBlockIndex(const CChainParams& chainparams)
3810 // Load block index from databases
3811 if (!fReindex && !LoadBlockIndexDB(chainparams))
3812 return false;
3813 return true;
3816 bool InitBlockIndex(const CChainParams& chainparams)
3818 LOCK(cs_main);
3820 // Check whether we're already initialized
3821 if (chainActive.Genesis() != NULL)
3822 return true;
3824 // Use the provided setting for -txindex in the new database
3825 fTxIndex = GetBoolArg("-txindex", DEFAULT_TXINDEX);
3826 pblocktree->WriteFlag("txindex", fTxIndex);
3827 LogPrintf("Initializing databases...\n");
3829 // Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
3830 if (!fReindex) {
3831 try {
3832 CBlock &block = const_cast<CBlock&>(chainparams.GenesisBlock());
3833 // Start new block file
3834 unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
3835 CDiskBlockPos blockPos;
3836 CValidationState state;
3837 if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.GetBlockTime()))
3838 return error("LoadBlockIndex(): FindBlockPos failed");
3839 if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
3840 return error("LoadBlockIndex(): writing genesis block to disk failed");
3841 CBlockIndex *pindex = AddToBlockIndex(block);
3842 if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
3843 return error("LoadBlockIndex(): genesis block not accepted");
3844 // Force a chainstate write so that when we VerifyDB in a moment, it doesn't check stale data
3845 return FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
3846 } catch (const std::runtime_error& e) {
3847 return error("LoadBlockIndex(): failed to initialize block database: %s", e.what());
3851 return true;
3854 bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp)
3856 // Map of disk positions for blocks with unknown parent (only used for reindex)
3857 static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
3858 int64_t nStart = GetTimeMillis();
3860 int nLoaded = 0;
3861 try {
3862 // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
3863 CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE+8, SER_DISK, CLIENT_VERSION);
3864 uint64_t nRewind = blkdat.GetPos();
3865 while (!blkdat.eof()) {
3866 boost::this_thread::interruption_point();
3868 blkdat.SetPos(nRewind);
3869 nRewind++; // start one byte further next time, in case of failure
3870 blkdat.SetLimit(); // remove former limit
3871 unsigned int nSize = 0;
3872 try {
3873 // locate a header
3874 unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
3875 blkdat.FindByte(chainparams.MessageStart()[0]);
3876 nRewind = blkdat.GetPos()+1;
3877 blkdat >> FLATDATA(buf);
3878 if (memcmp(buf, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE))
3879 continue;
3880 // read size
3881 blkdat >> nSize;
3882 if (nSize < 80 || nSize > MAX_BLOCK_SERIALIZED_SIZE)
3883 continue;
3884 } catch (const std::exception&) {
3885 // no valid block header found; don't complain
3886 break;
3888 try {
3889 // read block
3890 uint64_t nBlockPos = blkdat.GetPos();
3891 if (dbp)
3892 dbp->nPos = nBlockPos;
3893 blkdat.SetLimit(nBlockPos + nSize);
3894 blkdat.SetPos(nBlockPos);
3895 std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
3896 CBlock& block = *pblock;
3897 blkdat >> block;
3898 nRewind = blkdat.GetPos();
3900 // detect out of order blocks, and store them for later
3901 uint256 hash = block.GetHash();
3902 if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex.find(block.hashPrevBlock) == mapBlockIndex.end()) {
3903 LogPrint("reindex", "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
3904 block.hashPrevBlock.ToString());
3905 if (dbp)
3906 mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
3907 continue;
3910 // process in case the block isn't known yet
3911 if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
3912 LOCK(cs_main);
3913 CValidationState state;
3914 if (AcceptBlock(pblock, state, chainparams, NULL, true, dbp, NULL))
3915 nLoaded++;
3916 if (state.IsError())
3917 break;
3918 } else if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex[hash]->nHeight % 1000 == 0) {
3919 LogPrint("reindex", "Block Import: already had block %s at height %d\n", hash.ToString(), mapBlockIndex[hash]->nHeight);
3922 // Activate the genesis block so normal node progress can continue
3923 if (hash == chainparams.GetConsensus().hashGenesisBlock) {
3924 CValidationState state;
3925 if (!ActivateBestChain(state, chainparams)) {
3926 break;
3930 NotifyHeaderTip();
3932 // Recursively process earlier encountered successors of this block
3933 std::deque<uint256> queue;
3934 queue.push_back(hash);
3935 while (!queue.empty()) {
3936 uint256 head = queue.front();
3937 queue.pop_front();
3938 std::pair<std::multimap<uint256, CDiskBlockPos>::iterator, std::multimap<uint256, CDiskBlockPos>::iterator> range = mapBlocksUnknownParent.equal_range(head);
3939 while (range.first != range.second) {
3940 std::multimap<uint256, CDiskBlockPos>::iterator it = range.first;
3941 std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
3942 if (ReadBlockFromDisk(*pblockrecursive, it->second, chainparams.GetConsensus()))
3944 LogPrint("reindex", "%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(),
3945 head.ToString());
3946 LOCK(cs_main);
3947 CValidationState dummy;
3948 if (AcceptBlock(pblockrecursive, dummy, chainparams, NULL, true, &it->second, NULL))
3950 nLoaded++;
3951 queue.push_back(pblockrecursive->GetHash());
3954 range.first++;
3955 mapBlocksUnknownParent.erase(it);
3956 NotifyHeaderTip();
3959 } catch (const std::exception& e) {
3960 LogPrintf("%s: Deserialize or I/O error - %s\n", __func__, e.what());
3963 } catch (const std::runtime_error& e) {
3964 AbortNode(std::string("System error: ") + e.what());
3966 if (nLoaded > 0)
3967 LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded, GetTimeMillis() - nStart);
3968 return nLoaded > 0;
3971 void static CheckBlockIndex(const Consensus::Params& consensusParams)
3973 if (!fCheckBlockIndex) {
3974 return;
3977 LOCK(cs_main);
3979 // During a reindex, we read the genesis block and call CheckBlockIndex before ActivateBestChain,
3980 // so we have the genesis block in mapBlockIndex but no active chain. (A few of the tests when
3981 // iterating the block tree require that chainActive has been initialized.)
3982 if (chainActive.Height() < 0) {
3983 assert(mapBlockIndex.size() <= 1);
3984 return;
3987 // Build forward-pointing map of the entire block tree.
3988 std::multimap<CBlockIndex*,CBlockIndex*> forward;
3989 for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
3990 forward.insert(std::make_pair(it->second->pprev, it->second));
3993 assert(forward.size() == mapBlockIndex.size());
3995 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeGenesis = forward.equal_range(NULL);
3996 CBlockIndex *pindex = rangeGenesis.first->second;
3997 rangeGenesis.first++;
3998 assert(rangeGenesis.first == rangeGenesis.second); // There is only one index entry with parent NULL.
4000 // Iterate over the entire block tree, using depth-first search.
4001 // Along the way, remember whether there are blocks on the path from genesis
4002 // block being explored which are the first to have certain properties.
4003 size_t nNodes = 0;
4004 int nHeight = 0;
4005 CBlockIndex* pindexFirstInvalid = NULL; // Oldest ancestor of pindex which is invalid.
4006 CBlockIndex* pindexFirstMissing = NULL; // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA.
4007 CBlockIndex* pindexFirstNeverProcessed = NULL; // Oldest ancestor of pindex for which nTx == 0.
4008 CBlockIndex* pindexFirstNotTreeValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
4009 CBlockIndex* pindexFirstNotTransactionsValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not).
4010 CBlockIndex* pindexFirstNotChainValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not).
4011 CBlockIndex* pindexFirstNotScriptsValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not).
4012 while (pindex != NULL) {
4013 nNodes++;
4014 if (pindexFirstInvalid == NULL && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
4015 if (pindexFirstMissing == NULL && !(pindex->nStatus & BLOCK_HAVE_DATA)) pindexFirstMissing = pindex;
4016 if (pindexFirstNeverProcessed == NULL && pindex->nTx == 0) pindexFirstNeverProcessed = pindex;
4017 if (pindex->pprev != NULL && pindexFirstNotTreeValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TREE) pindexFirstNotTreeValid = pindex;
4018 if (pindex->pprev != NULL && pindexFirstNotTransactionsValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TRANSACTIONS) pindexFirstNotTransactionsValid = pindex;
4019 if (pindex->pprev != NULL && pindexFirstNotChainValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_CHAIN) pindexFirstNotChainValid = pindex;
4020 if (pindex->pprev != NULL && pindexFirstNotScriptsValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS) pindexFirstNotScriptsValid = pindex;
4022 // Begin: actual consistency checks.
4023 if (pindex->pprev == NULL) {
4024 // Genesis block checks.
4025 assert(pindex->GetBlockHash() == consensusParams.hashGenesisBlock); // Genesis block's hash must match.
4026 assert(pindex == chainActive.Genesis()); // The current active chain's genesis block must be this block.
4028 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)
4029 // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred).
4030 // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
4031 if (!fHavePruned) {
4032 // If we've never pruned, then HAVE_DATA should be equivalent to nTx > 0
4033 assert(!(pindex->nStatus & BLOCK_HAVE_DATA) == (pindex->nTx == 0));
4034 assert(pindexFirstMissing == pindexFirstNeverProcessed);
4035 } else {
4036 // If we have pruned, then we can only say that HAVE_DATA implies nTx > 0
4037 if (pindex->nStatus & BLOCK_HAVE_DATA) assert(pindex->nTx > 0);
4039 if (pindex->nStatus & BLOCK_HAVE_UNDO) assert(pindex->nStatus & BLOCK_HAVE_DATA);
4040 assert(((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS) == (pindex->nTx > 0)); // This is pruning-independent.
4041 // All parents having had data (at some point) is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to nChainTx being set.
4042 assert((pindexFirstNeverProcessed != NULL) == (pindex->nChainTx == 0)); // nChainTx != 0 is used to signal that all parent blocks have been processed (but may have been pruned).
4043 assert((pindexFirstNotTransactionsValid != NULL) == (pindex->nChainTx == 0));
4044 assert(pindex->nHeight == nHeight); // nHeight must be consistent.
4045 assert(pindex->pprev == NULL || pindex->nChainWork >= pindex->pprev->nChainWork); // For every block except the genesis block, the chainwork must be larger than the parent's.
4046 assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); // The pskip pointer must point back for all but the first 2 blocks.
4047 assert(pindexFirstNotTreeValid == NULL); // All mapBlockIndex entries must at least be TREE valid
4048 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TREE) assert(pindexFirstNotTreeValid == NULL); // TREE valid implies all parents are TREE valid
4049 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_CHAIN) assert(pindexFirstNotChainValid == NULL); // CHAIN valid implies all parents are CHAIN valid
4050 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_SCRIPTS) assert(pindexFirstNotScriptsValid == NULL); // SCRIPTS valid implies all parents are SCRIPTS valid
4051 if (pindexFirstInvalid == NULL) {
4052 // Checks for not-invalid blocks.
4053 assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
4055 if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && pindexFirstNeverProcessed == NULL) {
4056 if (pindexFirstInvalid == NULL) {
4057 // If this block sorts at least as good as the current tip and
4058 // is valid and we have all data for its parents, it must be in
4059 // setBlockIndexCandidates. chainActive.Tip() must also be there
4060 // even if some data has been pruned.
4061 if (pindexFirstMissing == NULL || pindex == chainActive.Tip()) {
4062 assert(setBlockIndexCandidates.count(pindex));
4064 // If some parent is missing, then it could be that this block was in
4065 // setBlockIndexCandidates but had to be removed because of the missing data.
4066 // In this case it must be in mapBlocksUnlinked -- see test below.
4068 } else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates.
4069 assert(setBlockIndexCandidates.count(pindex) == 0);
4071 // Check whether this block is in mapBlocksUnlinked.
4072 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked = mapBlocksUnlinked.equal_range(pindex->pprev);
4073 bool foundInUnlinked = false;
4074 while (rangeUnlinked.first != rangeUnlinked.second) {
4075 assert(rangeUnlinked.first->first == pindex->pprev);
4076 if (rangeUnlinked.first->second == pindex) {
4077 foundInUnlinked = true;
4078 break;
4080 rangeUnlinked.first++;
4082 if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed != NULL && pindexFirstInvalid == NULL) {
4083 // If this block has block data available, some parent was never received, and has no invalid parents, it must be in mapBlocksUnlinked.
4084 assert(foundInUnlinked);
4086 if (!(pindex->nStatus & BLOCK_HAVE_DATA)) assert(!foundInUnlinked); // Can't be in mapBlocksUnlinked if we don't HAVE_DATA
4087 if (pindexFirstMissing == NULL) assert(!foundInUnlinked); // We aren't missing data for any parent -- cannot be in mapBlocksUnlinked.
4088 if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed == NULL && pindexFirstMissing != NULL) {
4089 // We HAVE_DATA for this block, have received data for all parents at some point, but we're currently missing data for some parent.
4090 assert(fHavePruned); // We must have pruned.
4091 // This block may have entered mapBlocksUnlinked if:
4092 // - it has a descendant that at some point had more work than the
4093 // tip, and
4094 // - we tried switching to that descendant but were missing
4095 // data for some intermediate block between chainActive and the
4096 // tip.
4097 // So if this block is itself better than chainActive.Tip() and it wasn't in
4098 // setBlockIndexCandidates, then it must be in mapBlocksUnlinked.
4099 if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && setBlockIndexCandidates.count(pindex) == 0) {
4100 if (pindexFirstInvalid == NULL) {
4101 assert(foundInUnlinked);
4105 // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow
4106 // End: actual consistency checks.
4108 // Try descending into the first subnode.
4109 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
4110 if (range.first != range.second) {
4111 // A subnode was found.
4112 pindex = range.first->second;
4113 nHeight++;
4114 continue;
4116 // This is a leaf node.
4117 // Move upwards until we reach a node of which we have not yet visited the last child.
4118 while (pindex) {
4119 // We are going to either move to a parent or a sibling of pindex.
4120 // If pindex was the first with a certain property, unset the corresponding variable.
4121 if (pindex == pindexFirstInvalid) pindexFirstInvalid = NULL;
4122 if (pindex == pindexFirstMissing) pindexFirstMissing = NULL;
4123 if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed = NULL;
4124 if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid = NULL;
4125 if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid = NULL;
4126 if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid = NULL;
4127 if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid = NULL;
4128 // Find our parent.
4129 CBlockIndex* pindexPar = pindex->pprev;
4130 // Find which child we just visited.
4131 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
4132 while (rangePar.first->second != pindex) {
4133 assert(rangePar.first != rangePar.second); // Our parent must have at least the node we're coming from as child.
4134 rangePar.first++;
4136 // Proceed to the next one.
4137 rangePar.first++;
4138 if (rangePar.first != rangePar.second) {
4139 // Move to the sibling.
4140 pindex = rangePar.first->second;
4141 break;
4142 } else {
4143 // Move up further.
4144 pindex = pindexPar;
4145 nHeight--;
4146 continue;
4151 // Check that we actually traversed the entire map.
4152 assert(nNodes == forward.size());
4155 std::string CBlockFileInfo::ToString() const
4157 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));
4160 ThresholdState VersionBitsTipState(const Consensus::Params& params, Consensus::DeploymentPos pos)
4162 LOCK(cs_main);
4163 return VersionBitsState(chainActive.Tip(), params, pos, versionbitscache);
4166 int VersionBitsTipStateSinceHeight(const Consensus::Params& params, Consensus::DeploymentPos pos)
4168 LOCK(cs_main);
4169 return VersionBitsStateSinceHeight(chainActive.Tip(), params, pos, versionbitscache);
4172 static const uint64_t MEMPOOL_DUMP_VERSION = 1;
4174 bool LoadMempool(void)
4176 int64_t nExpiryTimeout = GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60;
4177 FILE* filestr = fopen((GetDataDir() / "mempool.dat").string().c_str(), "r");
4178 CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
4179 if (file.IsNull()) {
4180 LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
4181 return false;
4184 int64_t count = 0;
4185 int64_t skipped = 0;
4186 int64_t failed = 0;
4187 int64_t nNow = GetTime();
4189 try {
4190 uint64_t version;
4191 file >> version;
4192 if (version != MEMPOOL_DUMP_VERSION) {
4193 return false;
4195 uint64_t num;
4196 file >> num;
4197 double prioritydummy = 0;
4198 while (num--) {
4199 CTransactionRef tx;
4200 int64_t nTime;
4201 int64_t nFeeDelta;
4202 file >> tx;
4203 file >> nTime;
4204 file >> nFeeDelta;
4206 CAmount amountdelta = nFeeDelta;
4207 if (amountdelta) {
4208 mempool.PrioritiseTransaction(tx->GetHash(), tx->GetHash().ToString(), prioritydummy, amountdelta);
4210 CValidationState state;
4211 if (nTime + nExpiryTimeout > nNow) {
4212 LOCK(cs_main);
4213 AcceptToMemoryPoolWithTime(mempool, state, tx, true, NULL, nTime);
4214 if (state.IsValid()) {
4215 ++count;
4216 } else {
4217 ++failed;
4219 } else {
4220 ++skipped;
4222 if (ShutdownRequested())
4223 return false;
4225 std::map<uint256, CAmount> mapDeltas;
4226 file >> mapDeltas;
4228 for (const auto& i : mapDeltas) {
4229 mempool.PrioritiseTransaction(i.first, i.first.ToString(), prioritydummy, i.second);
4231 } catch (const std::exception& e) {
4232 LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
4233 return false;
4236 LogPrintf("Imported mempool transactions from disk: %i successes, %i failed, %i expired\n", count, failed, skipped);
4237 return true;
4240 void DumpMempool(void)
4242 int64_t start = GetTimeMicros();
4244 std::map<uint256, CAmount> mapDeltas;
4245 std::vector<TxMempoolInfo> vinfo;
4248 LOCK(mempool.cs);
4249 for (const auto &i : mempool.mapDeltas) {
4250 mapDeltas[i.first] = i.second.second;
4252 vinfo = mempool.infoAll();
4255 int64_t mid = GetTimeMicros();
4257 try {
4258 FILE* filestr = fopen((GetDataDir() / "mempool.dat.new").string().c_str(), "w");
4259 if (!filestr) {
4260 return;
4263 CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
4265 uint64_t version = MEMPOOL_DUMP_VERSION;
4266 file << version;
4268 file << (uint64_t)vinfo.size();
4269 for (const auto& i : vinfo) {
4270 file << *(i.tx);
4271 file << (int64_t)i.nTime;
4272 file << (int64_t)i.nFeeDelta;
4273 mapDeltas.erase(i.tx->GetHash());
4276 file << mapDeltas;
4277 FileCommit(file.Get());
4278 file.fclose();
4279 RenameOver(GetDataDir() / "mempool.dat.new", GetDataDir() / "mempool.dat");
4280 int64_t last = GetTimeMicros();
4281 LogPrintf("Dumped mempool: %gs to copy, %gs to dump\n", (mid-start)*0.000001, (last-mid)*0.000001);
4282 } catch (const std::exception& e) {
4283 LogPrintf("Failed to dump mempool: %s. Continuing anyway.\n", e.what());
4287 //! Guess how far we are in the verification process at the given block index
4288 double GuessVerificationProgress(const ChainTxData& data, CBlockIndex *pindex) {
4289 if (pindex == NULL)
4290 return 0.0;
4292 int64_t nNow = time(NULL);
4294 double fTxTotal;
4296 if (pindex->nChainTx <= data.nTxCount) {
4297 fTxTotal = data.nTxCount + (nNow - data.nTime) * data.dTxRate;
4298 } else {
4299 fTxTotal = pindex->nChainTx + (nNow - pindex->GetBlockTime()) * data.dTxRate;
4302 return pindex->nChainTx / fTxTotal;
4305 class CMainCleanup
4307 public:
4308 CMainCleanup() {}
4309 ~CMainCleanup() {
4310 // block headers
4311 BlockMap::iterator it1 = mapBlockIndex.begin();
4312 for (; it1 != mapBlockIndex.end(); it1++)
4313 delete (*it1).second;
4314 mapBlockIndex.clear();
4316 } instance_of_cmaincleanup;