LCOV - code coverage report
Current view: top level - src - main.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 1872 2417 77.5 %
Date: 2015-10-12 22:39:14 Functions: 101 118 85.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2014 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.
       5             : 
       6             : #include "main.h"
       7             : 
       8             : #include "addrman.h"
       9             : #include "alert.h"
      10             : #include "arith_uint256.h"
      11             : #include "chainparams.h"
      12             : #include "checkpoints.h"
      13             : #include "checkqueue.h"
      14             : #include "consensus/consensus.h"
      15             : #include "consensus/validation.h"
      16             : #include "hash.h"
      17             : #include "init.h"
      18             : #include "merkleblock.h"
      19             : #include "net.h"
      20             : #include "policy/policy.h"
      21             : #include "pow.h"
      22             : #include "primitives/block.h"
      23             : #include "primitives/transaction.h"
      24             : #include "script/script.h"
      25             : #include "script/sigcache.h"
      26             : #include "script/standard.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             : 
      37             : #include <sstream>
      38             : 
      39             : #include <boost/algorithm/string/replace.hpp>
      40             : #include <boost/filesystem.hpp>
      41             : #include <boost/filesystem/fstream.hpp>
      42             : #include <boost/math/distributions/poisson.hpp>
      43             : #include <boost/thread.hpp>
      44             : 
      45             : using namespace std;
      46             : 
      47             : #if defined(NDEBUG)
      48             : # error "Bitcoin cannot be compiled without assertions."
      49             : #endif
      50             : 
      51             : /**
      52             :  * Global state
      53             :  */
      54             : 
      55          96 : CCriticalSection cs_main;
      56             : 
      57          96 : BlockMap mapBlockIndex;
      58          96 : CChain chainActive;
      59             : CBlockIndex *pindexBestHeader = NULL;
      60             : int64_t nTimeBestReceived = 0;
      61          96 : CWaitableCriticalSection csBestBlock;
      62          96 : CConditionVariable cvBlockChange;
      63             : int nScriptCheckThreads = 0;
      64             : bool fImporting = false;
      65             : bool fReindex = false;
      66             : bool fTxIndex = false;
      67             : bool fHavePruned = false;
      68             : bool fPruneMode = false;
      69             : bool fIsBareMultisigStd = true;
      70             : bool fRequireStandard = true;
      71             : bool fCheckBlockIndex = false;
      72             : bool fCheckpointsEnabled = true;
      73             : size_t nCoinCacheUsage = 5000 * 300;
      74             : uint64_t nPruneTarget = 0;
      75             : bool fAlerts = DEFAULT_ALERTS;
      76             : 
      77             : /** Fees smaller than this (in satoshi) are considered zero fee (for relaying and mining) */
      78          96 : CFeeRate minRelayTxFee = CFeeRate(5000);
      79             : 
      80          96 : CTxMemPool mempool(::minRelayTxFee);
      81             : 
      82         756 : struct COrphanTx {
      83             :     CTransaction tx;
      84             :     NodeId fromPeer;
      85             : };
      86          96 : map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(cs_main);;
      87          96 : map<uint256, set<uint256> > mapOrphanTransactionsByPrev GUARDED_BY(cs_main);;
      88             : void EraseOrphansFor(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
      89             : 
      90             : /**
      91             :  * Returns true if there are nRequired or more blocks of minVersion or above
      92             :  * in the last Consensus::Params::nMajorityWindow blocks, starting at pstart and going backwards.
      93             :  */
      94             : static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams);
      95             : static void CheckBlockIndex();
      96             : 
      97             : /** Constant stuff for coinbase transactions we create: */
      98          96 : CScript COINBASE_FLAGS;
      99             : 
     100         192 : const string strMessageMagic = "Bitcoin Signed Message:\n";
     101             : 
     102             : // Internal stuff
     103             : namespace {
     104             : 
     105             :     struct CBlockIndexWorkComparator
     106             :     {
     107     4595210 :         bool operator()(CBlockIndex *pa, CBlockIndex *pb) const {
     108             :             // First sort by most total work, ...
     109     9190420 :             if (pa->nChainWork > pb->nChainWork) return false;
     110     5690928 :             if (pa->nChainWork < pb->nChainWork) return true;
     111             : 
     112             :             // ... then by earliest time received, ...
     113       60781 :             if (pa->nSequenceId < pb->nSequenceId) return false;
     114       60509 :             if (pa->nSequenceId > pb->nSequenceId) return true;
     115             : 
     116             :             // Use pointer address as tie breaker (should only happen with blocks
     117             :             // loaded from disk, as those all have id 0).
     118       60213 :             if (pa < pb) return false;
     119       60188 :             if (pa > pb) return true;
     120             : 
     121             :             // Identical blocks.
     122           0 :             return false;
     123             :         }
     124             :     };
     125             : 
     126             :     CBlockIndex *pindexBestInvalid;
     127             : 
     128             :     /**
     129             :      * The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS (for itself and all ancestors) and
     130             :      * as good as our current tip or better. Entries may be failed, though, and pruning nodes may be
     131             :      * missing the data for the block.
     132             :      */
     133          96 :     set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexCandidates;
     134             :     /** Number of nodes with fSyncStarted. */
     135             :     int nSyncStarted = 0;
     136             :     /** All pairs A->B, where A (or one if its ancestors) misses transactions, but B has transactions.
     137             :       * Pruned nodes may have entries where B is missing data.
     138             :       */
     139          96 :     multimap<CBlockIndex*, CBlockIndex*> mapBlocksUnlinked;
     140             : 
     141          96 :     CCriticalSection cs_LastBlockFile;
     142          96 :     std::vector<CBlockFileInfo> vinfoBlockFile;
     143             :     int nLastBlockFile = 0;
     144             :     /** Global flag to indicate we should check to see if there are
     145             :      *  block/undo files that should be deleted.  Set on startup
     146             :      *  or if we allocate more file space when we're in prune mode
     147             :      */
     148             :     bool fCheckForPruning = false;
     149             : 
     150             :     /**
     151             :      * Every received block is assigned a unique and increasing identifier, so we
     152             :      * know which one to give priority in case of a fork.
     153             :      */
     154          96 :     CCriticalSection cs_nBlockSequenceId;
     155             :     /** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
     156             :     uint32_t nBlockSequenceId = 1;
     157             : 
     158             :     /**
     159             :      * Sources of received blocks, saved to be able to send them reject
     160             :      * messages or ban them when processing happens afterwards. Protected by
     161             :      * cs_main.
     162             :      */
     163          96 :     map<uint256, NodeId> mapBlockSource;
     164             : 
     165             :     /**
     166             :      * Filter for transactions that were recently rejected by
     167             :      * AcceptToMemoryPool. These are not rerequested until the chain tip
     168             :      * changes, at which point the entire filter is reset. Protected by
     169             :      * cs_main.
     170             :      *
     171             :      * Without this filter we'd be re-requesting txs from each of our peers,
     172             :      * increasing bandwidth consumption considerably. For instance, with 100
     173             :      * peers, half of which relay a tx we don't accept, that might be a 50x
     174             :      * bandwidth increase. A flooding attacker attempting to roll-over the
     175             :      * filter using minimum-sized, 60byte, transactions might manage to send
     176             :      * 1000/sec if we have fast peers, so we pick 120,000 to give our peers a
     177             :      * two minute window to send invs to us.
     178             :      *
     179             :      * Decreasing the false positive rate is fairly cheap, so we pick one in a
     180             :      * million to make it highly unlikely for users to have issues with this
     181             :      * filter.
     182             :      *
     183             :      * Memory used: 1.7MB
     184             :      */
     185          96 :     boost::scoped_ptr<CRollingBloomFilter> recentRejects;
     186             :     uint256 hashRecentRejectsChainTip;
     187             : 
     188             :     /** Blocks that are in flight, and that are in the queue to be downloaded. Protected by cs_main. */
     189             :     struct QueuedBlock {
     190             :         uint256 hash;
     191             :         CBlockIndex *pindex;  //! Optional.
     192             :         int64_t nTime;  //! Time of "getdata" request in microseconds.
     193             :         bool fValidatedHeaders;  //! Whether this block has validated headers at the time of request.
     194             :         int64_t nTimeDisconnect; //! The timeout for this block request (for disconnecting a slow peer)
     195             :     };
     196          96 :     map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
     197             : 
     198             :     /** Number of blocks in flight with validated headers. */
     199             :     int nQueuedValidatedHeaders = 0;
     200             : 
     201             :     /** Number of preferable block download peers. */
     202             :     int nPreferredDownload = 0;
     203             : 
     204             :     /** Dirty block index entries. */
     205          96 :     set<CBlockIndex*> setDirtyBlockIndex;
     206             : 
     207             :     /** Dirty block file entries. */
     208          96 :     set<int> setDirtyFileInfo;
     209             : } // anon namespace
     210             : 
     211             : //////////////////////////////////////////////////////////////////////////////
     212             : //
     213             : // Registration of network node signals.
     214             : //
     215             : 
     216             : namespace {
     217             : 
     218          70 : struct CBlockReject {
     219             :     unsigned char chRejectCode;
     220             :     string strRejectReason;
     221             :     uint256 hashBlock;
     222             : };
     223             : 
     224             : /**
     225             :  * Maintain validation-specific state about nodes, protected by cs_main, instead
     226             :  * by CNode's own locks. This simplifies asynchronous operation, where
     227             :  * processing of incoming data is done after the ProcessMessage call returns,
     228             :  * and we're no longer holding the node's locks.
     229             :  */
     230        5445 : struct CNodeState {
     231             :     //! The peer's address
     232             :     CService address;
     233             :     //! Whether we have a fully established connection.
     234             :     bool fCurrentlyConnected;
     235             :     //! Accumulated misbehaviour score for this peer.
     236             :     int nMisbehavior;
     237             :     //! Whether this peer should be disconnected and banned (unless whitelisted).
     238             :     bool fShouldBan;
     239             :     //! String name of this peer (debugging/logging purposes).
     240             :     std::string name;
     241             :     //! List of asynchronously-determined block rejections to notify this peer about.
     242             :     std::vector<CBlockReject> rejects;
     243             :     //! The best known block we know this peer has announced.
     244             :     CBlockIndex *pindexBestKnownBlock;
     245             :     //! The hash of the last unknown block this peer has announced.
     246             :     uint256 hashLastUnknownBlock;
     247             :     //! The last full block we both have.
     248             :     CBlockIndex *pindexLastCommonBlock;
     249             :     //! Whether we've started headers synchronization with this peer.
     250             :     bool fSyncStarted;
     251             :     //! Since when we're stalling block download progress (in microseconds), or 0.
     252             :     int64_t nStallingSince;
     253             :     list<QueuedBlock> vBlocksInFlight;
     254             :     int nBlocksInFlight;
     255             :     int nBlocksInFlightValidHeaders;
     256             :     //! Whether we consider this a preferred download peer.
     257             :     bool fPreferredDownload;
     258             : 
     259        1452 :     CNodeState() {
     260         363 :         fCurrentlyConnected = false;
     261         363 :         nMisbehavior = 0;
     262         363 :         fShouldBan = false;
     263         363 :         pindexBestKnownBlock = NULL;
     264         363 :         hashLastUnknownBlock.SetNull();
     265         363 :         pindexLastCommonBlock = NULL;
     266         363 :         fSyncStarted = false;
     267         363 :         nStallingSince = 0;
     268         363 :         nBlocksInFlight = 0;
     269         363 :         nBlocksInFlightValidHeaders = 0;
     270         363 :         fPreferredDownload = false;
     271         363 :     }
     272             : };
     273             : 
     274             : /** Map maintaining per-node state. Requires cs_main. */
     275          96 : map<NodeId, CNodeState> mapNodeState;
     276             : 
     277             : // Requires cs_main.
     278             : CNodeState *State(NodeId pnode) {
     279      202275 :     map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
     280      202275 :     if (it == mapNodeState.end())
     281             :         return NULL;
     282      202275 :     return &it->second;
     283             : }
     284             : 
     285         265 : int GetHeight()
     286             : {
     287         265 :     LOCK(cs_main);
     288         530 :     return chainActive.Height();
     289             : }
     290             : 
     291           0 : void UpdatePreferredDownload(CNode* node, CNodeState* state)
     292             : {
     293         250 :     nPreferredDownload -= state->fPreferredDownload;
     294             : 
     295             :     // Whether this node should be marked as a preferred download node.
     296         250 :     state->fPreferredDownload = (!node->fInbound || node->fWhitelisted) && !node->fOneShot && !node->fClient;
     297             : 
     298         250 :     nPreferredDownload += state->fPreferredDownload;
     299           0 : }
     300             : 
     301             : // Returns time at which to timeout block request (nTime in microseconds)
     302           0 : int64_t GetBlockTimeout(int64_t nTime, int nValidatedQueuedBefore, const Consensus::Params &consensusParams)
     303             : {
     304       14901 :     return nTime + 500000 * consensusParams.nPowTargetSpacing * (4 + nValidatedQueuedBefore);
     305             : }
     306             : 
     307         363 : void InitializeNode(NodeId nodeid, const CNode *pnode) {
     308         363 :     LOCK(cs_main);
     309        1815 :     CNodeState &state = mapNodeState.insert(std::make_pair(nodeid, CNodeState())).first->second;
     310         363 :     state.name = pnode->addrName;
     311         363 :     state.address = pnode->addr;
     312         363 : }
     313             : 
     314          43 : void FinalizeNode(NodeId nodeid) {
     315          43 :     LOCK(cs_main);
     316          86 :     CNodeState *state = State(nodeid);
     317             : 
     318          43 :     if (state->fSyncStarted)
     319          24 :         nSyncStarted--;
     320             : 
     321          43 :     if (state->nMisbehavior == 0 && state->fCurrentlyConnected) {
     322           9 :         AddressCurrentlyConnected(state->address);
     323             :     }
     324             : 
     325         264 :     BOOST_FOREACH(const QueuedBlock& entry, state->vBlocksInFlight)
     326           1 :         mapBlocksInFlight.erase(entry.hash);
     327          43 :     EraseOrphansFor(nodeid);
     328          43 :     nPreferredDownload -= state->fPreferredDownload;
     329             : 
     330             :     mapNodeState.erase(nodeid);
     331          43 : }
     332             : 
     333             : // Requires cs_main.
     334             : // Returns a bool indicating whether we requested this block.
     335        9539 : bool MarkBlockAsReceived(const uint256& hash) {
     336        9539 :     map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
     337        9539 :     if (itInFlight != mapBlocksInFlight.end()) {
     338        8084 :         CNodeState *state = State(itInFlight->second.first);
     339        8084 :         nQueuedValidatedHeaders -= itInFlight->second.second->fValidatedHeaders;
     340        8084 :         state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders;
     341        4042 :         state->vBlocksInFlight.erase(itInFlight->second.second);
     342        4042 :         state->nBlocksInFlight--;
     343        4042 :         state->nStallingSince = 0;
     344             :         mapBlocksInFlight.erase(itInFlight);
     345        4042 :         return true;
     346             :     }
     347             :     return false;
     348             : }
     349             : 
     350             : // Requires cs_main.
     351        4043 : void MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const Consensus::Params& consensusParams, CBlockIndex *pindex = NULL) {
     352        4043 :     CNodeState *state = State(nodeid);
     353        4043 :     assert(state != NULL);
     354             : 
     355             :     // Make sure it's not listed somewhere already.
     356        4043 :     MarkBlockAsReceived(hash);
     357             : 
     358        4043 :     int64_t nNow = GetTimeMicros();
     359        8086 :     QueuedBlock newentry = {hash, pindex, nNow, pindex != NULL, GetBlockTimeout(nNow, nQueuedValidatedHeaders, consensusParams)};
     360        4043 :     nQueuedValidatedHeaders += newentry.fValidatedHeaders;
     361       12129 :     list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(), newentry);
     362        4043 :     state->nBlocksInFlight++;
     363        4043 :     state->nBlocksInFlightValidHeaders += newentry.fValidatedHeaders;
     364        8086 :     mapBlocksInFlight[hash] = std::make_pair(nodeid, it);
     365        4043 : }
     366             : 
     367             : /** Check whether the last unknown block a peer advertized is not yet known. */
     368       64857 : void ProcessBlockAvailability(NodeId nodeid) {
     369       64857 :     CNodeState *state = State(nodeid);
     370       64857 :     assert(state != NULL);
     371             : 
     372       64857 :     if (!state->hashLastUnknownBlock.IsNull()) {
     373       21428 :         BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
     374       12448 :         if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
     375        1636 :             if (state->pindexBestKnownBlock == NULL || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
     376         572 :                 state->pindexBestKnownBlock = itOld->second;
     377         578 :             state->hashLastUnknownBlock.SetNull();
     378             :         }
     379             :     }
     380       64857 : }
     381             : 
     382             : /** Update tracking information about which blocks a peer is assumed to have. */
     383       12168 : void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
     384       12168 :     CNodeState *state = State(nodeid);
     385       12168 :     assert(state != NULL);
     386             : 
     387       12168 :     ProcessBlockAvailability(nodeid);
     388             : 
     389       12168 :     BlockMap::iterator it = mapBlockIndex.find(hash);
     390       34140 :     if (it != mapBlockIndex.end() && it->second->nChainWork > 0) {
     391             :         // An actually better block was announced.
     392       21714 :         if (state->pindexBestKnownBlock == NULL || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
     393        6902 :             state->pindexBestKnownBlock = it->second;
     394             :     } else {
     395             :         // An unknown block was announced; just assume that the latest one is the best one.
     396        4844 :         state->hashLastUnknownBlock = hash;
     397             :     }
     398       12168 : }
     399             : 
     400             : /** Find the last common ancestor two blocks have.
     401             :  *  Both pa and pb must be non-NULL. */
     402       23063 : CBlockIndex* LastCommonAncestor(CBlockIndex* pa, CBlockIndex* pb) {
     403       23063 :     if (pa->nHeight > pb->nHeight) {
     404           0 :         pa = pa->GetAncestor(pb->nHeight);
     405       23063 :     } else if (pb->nHeight > pa->nHeight) {
     406        7679 :         pb = pb->GetAncestor(pa->nHeight);
     407             :     }
     408             : 
     409       23116 :     while (pa != pb && pa && pb) {
     410          53 :         pa = pa->pprev;
     411          53 :         pb = pb->pprev;
     412             :     }
     413             : 
     414             :     // Eventually all chain branches meet at the genesis block.
     415       23063 :     assert(pa == pb);
     416       23063 :     return pa;
     417             : }
     418             : 
     419             : /** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
     420             :  *  at most count entries. */
     421       52689 : void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<CBlockIndex*>& vBlocks, NodeId& nodeStaller) {
     422       52689 :     if (count == 0)
     423       47279 :         return;
     424             : 
     425      105378 :     vBlocks.reserve(vBlocks.size() + count);
     426       52689 :     CNodeState *state = State(nodeid);
     427       52689 :     assert(state != NULL);
     428             : 
     429             :     // Make sure pindexBestKnownBlock is up to date, we'll need it.
     430       52689 :     ProcessBlockAvailability(nodeid);
     431             : 
     432      135081 :     if (state->pindexBestKnownBlock == NULL || state->pindexBestKnownBlock->nChainWork < chainActive.Tip()->nChainWork) {
     433             :         // This peer has nothing interesting.
     434             :         return;
     435             :     }
     436             : 
     437       23063 :     if (state->pindexLastCommonBlock == NULL) {
     438             :         // Bootstrap quickly by guessing a parent of our best tip is the forking point.
     439             :         // Guessing wrong in either direction is not a problem.
     440         519 :         state->pindexLastCommonBlock = chainActive[std::min(state->pindexBestKnownBlock->nHeight, chainActive.Height())];
     441             :     }
     442             : 
     443             :     // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
     444             :     // of its current tip anymore. Go back enough to fix that.
     445       23063 :     state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
     446       23063 :     if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
     447             :         return;
     448             : 
     449             :     std::vector<CBlockIndex*> vToFetch;
     450        7688 :     CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
     451             :     // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
     452             :     // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
     453             :     // download that next block if the window were 1 larger.
     454        7688 :     int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
     455       15376 :     int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
     456        7688 :     NodeId waitingfor = -1;
     457       20788 :     while (pindexWalk->nHeight < nMaxHeight) {
     458             :         // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
     459             :         // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
     460             :         // as iterating over ~100 CBlockIndex* entries anyway.
     461       30760 :         int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
     462        7690 :         vToFetch.resize(nToFetch);
     463        7690 :         pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
     464       15380 :         vToFetch[nToFetch - 1] = pindexWalk;
     465      240730 :         for (unsigned int i = nToFetch - 1; i > 0; i--) {
     466      699120 :             vToFetch[i - 1] = vToFetch[i]->pprev;
     467             :         }
     468             : 
     469             :         // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
     470             :         // are not yet downloaded and not in flight to vBlocks. In the mean time, update
     471             :         // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
     472             :         // already part of our chain (and therefore don't need it even if pruned).
     473     1030503 :         BOOST_FOREACH(CBlockIndex* pindex, vToFetch) {
     474      141925 :             if (!pindex->IsValid(BLOCK_VALID_TREE)) {
     475             :                 // We consider the chain that this peer is on invalid.
     476        2278 :                 return;
     477             :             }
     478      228996 :             if (pindex->nStatus & BLOCK_HAVE_DATA || chainActive.Contains(pindex)) {
     479       54664 :                 if (pindex->nChainTx)
     480       10250 :                     state->pindexLastCommonBlock = pindex;
     481      174332 :             } else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
     482             :                 // The block is not already downloaded, and not yet in flight.
     483        2560 :                 if (pindex->nHeight > nWindowEnd) {
     484             :                     // We reached the end of the window.
     485           0 :                     if (vBlocks.size() == 0 && waitingfor != nodeid) {
     486             :                         // We aren't able to fetch anything, but we would be if the download window was one larger.
     487           0 :                         nodeStaller = waitingfor;
     488             :                     }
     489             :                     return;
     490             :                 }
     491        2560 :                 vBlocks.push_back(pindex);
     492        5120 :                 if (vBlocks.size() == count) {
     493             :                     return;
     494             :                 }
     495       84606 :             } else if (waitingfor == -1) {
     496             :                 // This is the first already-in-flight block.
     497       13008 :                 waitingfor = mapBlocksInFlight[pindex->GetBlockHash()].first;
     498             :             }
     499             :         }
     500             :     }
     501             : }
     502             : 
     503             : } // anon namespace
     504             : 
     505         538 : bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
     506         538 :     LOCK(cs_main);
     507         538 :     CNodeState *state = State(nodeid);
     508         538 :     if (state == NULL)
     509             :         return false;
     510         538 :     stats.nMisbehavior = state->nMisbehavior;
     511         538 :     stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
     512         538 :     stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
     513        3894 :     BOOST_FOREACH(const QueuedBlock& queue, state->vBlocksInFlight) {
     514         111 :         if (queue.pindex)
     515         111 :             stats.vHeightInFlight.push_back(queue.pindex->nHeight);
     516             :     }
     517         538 :     return true;
     518             : }
     519             : 
     520         119 : void RegisterNodeSignals(CNodeSignals& nodeSignals)
     521             : {
     522         238 :     nodeSignals.GetHeight.connect(&GetHeight);
     523         238 :     nodeSignals.ProcessMessages.connect(&ProcessMessages);
     524         238 :     nodeSignals.SendMessages.connect(&SendMessages);
     525         238 :     nodeSignals.InitializeNode.connect(&InitializeNode);
     526         238 :     nodeSignals.FinalizeNode.connect(&FinalizeNode);
     527         119 : }
     528             : 
     529         119 : void UnregisterNodeSignals(CNodeSignals& nodeSignals)
     530             : {
     531         119 :     nodeSignals.GetHeight.disconnect(&GetHeight);
     532         119 :     nodeSignals.ProcessMessages.disconnect(&ProcessMessages);
     533         119 :     nodeSignals.SendMessages.disconnect(&SendMessages);
     534         119 :     nodeSignals.InitializeNode.disconnect(&InitializeNode);
     535         119 :     nodeSignals.FinalizeNode.disconnect(&FinalizeNode);
     536         119 : }
     537             : 
     538        4636 : CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator)
     539             : {
     540             :     // Find the first block the caller has in the main chain
     541       37616 :     BOOST_FOREACH(const uint256& hash, locator.vHave) {
     542        4722 :         BlockMap::iterator mi = mapBlockIndex.find(hash);
     543        4722 :         if (mi != mapBlockIndex.end())
     544             :         {
     545        4675 :             CBlockIndex* pindex = (*mi).second;
     546        4675 :             if (chain.Contains(pindex))
     547        4632 :                 return pindex;
     548             :         }
     549             :     }
     550           4 :     return chain.Genesis();
     551             : }
     552             : 
     553             : CCoinsViewCache *pcoinsTip = NULL;
     554             : CBlockTreeDB *pblocktree = NULL;
     555             : 
     556             : //////////////////////////////////////////////////////////////////////////////
     557             : //
     558             : // mapOrphanTransactions
     559             : //
     560             : 
     561         112 : bool AddOrphanTx(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     562             : {
     563         112 :     uint256 hash = tx.GetHash();
     564         112 :     if (mapOrphanTransactions.count(hash))
     565             :         return false;
     566             : 
     567             :     // Ignore big transactions, to avoid a
     568             :     // send-big-orphans memory exhaustion attack. If a peer has a legitimate
     569             :     // large transaction with a missing parent then we assume
     570             :     // it will rebroadcast it later, after the parent transaction(s)
     571             :     // have been mined or received.
     572             :     // 10,000 orphans, each of which is at most 5,000 bytes big is
     573             :     // at most 500 megabytes of orphans:
     574          94 :     unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
     575          94 :     if (sz > 5000)
     576             :     {
     577          20 :         LogPrint("mempool", "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
     578          10 :         return false;
     579             :     }
     580             : 
     581          84 :     mapOrphanTransactions[hash].tx = tx;
     582          84 :     mapOrphanTransactions[hash].fromPeer = peer;
     583         924 :     BOOST_FOREACH(const CTxIn& txin, tx.vin)
     584          84 :         mapOrphanTransactionsByPrev[txin.prevout.hash].insert(hash);
     585             : 
     586             :     LogPrint("mempool", "stored orphan tx %s (mapsz %u prevsz %u)\n", hash.ToString(),
     587         252 :              mapOrphanTransactions.size(), mapOrphanTransactionsByPrev.size());
     588          84 :     return true;
     589             : }
     590             : 
     591          83 : void static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     592             : {
     593          83 :     map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
     594          83 :     if (it == mapOrphanTransactions.end())
     595          83 :         return;
     596         996 :     BOOST_FOREACH(const CTxIn& txin, it->second.tx.vin)
     597             :     {
     598         166 :         map<uint256, set<uint256> >::iterator itPrev = mapOrphanTransactionsByPrev.find(txin.prevout.hash);
     599          83 :         if (itPrev == mapOrphanTransactionsByPrev.end())
     600             :             continue;
     601          83 :         itPrev->second.erase(hash);
     602         166 :         if (itPrev->second.empty())
     603             :             mapOrphanTransactionsByPrev.erase(itPrev);
     604             :     }
     605             :     mapOrphanTransactions.erase(it);
     606             : }
     607             : 
     608          46 : void EraseOrphansFor(NodeId peer)
     609             : {
     610          46 :     int nErased = 0;
     611          46 :     map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
     612         286 :     while (iter != mapOrphanTransactions.end())
     613             :     {
     614         480 :         map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
     615         240 :         if (maybeErase->second.fromPeer == peer)
     616             :         {
     617          12 :             EraseOrphanTx(maybeErase->second.tx.GetHash());
     618           6 :             ++nErased;
     619             :         }
     620             :     }
     621          46 :     if (nErased > 0) LogPrint("mempool", "Erased %d orphan tx from peer %d\n", nErased, peer);
     622          46 : }
     623             : 
     624             : 
     625           5 : unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
     626             : {
     627           5 :     unsigned int nEvicted = 0;
     628          86 :     while (mapOrphanTransactions.size() > nMaxOrphans)
     629             :     {
     630             :         // Evict a random orphan:
     631          76 :         uint256 randomhash = GetRandHash();
     632          76 :         map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.lower_bound(randomhash);
     633          76 :         if (it == mapOrphanTransactions.end())
     634           6 :             it = mapOrphanTransactions.begin();
     635          76 :         EraseOrphanTx(it->first);
     636          76 :         ++nEvicted;
     637             :     }
     638           5 :     return nEvicted;
     639             : }
     640             : 
     641       70289 : bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
     642             : {
     643       70289 :     if (tx.nLockTime == 0)
     644             :         return true;
     645       12587 :     if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
     646             :         return true;
     647         120 :     BOOST_FOREACH(const CTxIn& txin, tx.vin)
     648          15 :         if (!txin.IsFinal())
     649          15 :             return false;
     650           0 :     return true;
     651             : }
     652             : 
     653       60307 : bool CheckFinalTx(const CTransaction &tx)
     654             : {
     655       60307 :     AssertLockHeld(cs_main);
     656      120614 :     return IsFinalTx(tx, chainActive.Height() + 1, GetAdjustedTime());
     657             : }
     658             : 
     659       35766 : unsigned int GetLegacySigOpCount(const CTransaction& tx)
     660             : {
     661       35766 :     unsigned int nSigOps = 0;
     662      368120 :     BOOST_FOREACH(const CTxIn& txin, tx.vin)
     663             :     {
     664       37858 :         nSigOps += txin.scriptSig.GetSigOpCount(false);
     665             :     }
     666      371470 :     BOOST_FOREACH(const CTxOut& txout, tx.vout)
     667             :     {
     668       38528 :         nSigOps += txout.scriptPubKey.GetSigOpCount(false);
     669             :     }
     670       35766 :     return nSigOps;
     671             : }
     672             : 
     673        3635 : unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs)
     674             : {
     675        3635 :     if (tx.IsCoinBase())
     676             :         return 0;
     677             : 
     678             :     unsigned int nSigOps = 0;
     679       13353 :     for (unsigned int i = 0; i < tx.vin.size(); i++)
     680             :     {
     681        9718 :         const CTxOut &prevout = inputs.GetOutputFor(tx.vin[i]);
     682        4859 :         if (prevout.scriptPubKey.IsPayToScriptHash())
     683          68 :             nSigOps += prevout.scriptPubKey.GetSigOpCount(tx.vin[i].scriptSig);
     684             :     }
     685        3635 :     return nSigOps;
     686             : }
     687             : 
     688             : 
     689             : 
     690             : 
     691             : 
     692             : 
     693             : 
     694             : 
     695       27855 : bool CheckTransaction(const CTransaction& tx, CValidationState &state)
     696             : {
     697             :     // Basic checks that don't depend on any context
     698       55710 :     if (tx.vin.empty())
     699           6 :         return state.DoS(10, false, REJECT_INVALID, "bad-txns-vin-empty");
     700       55708 :     if (tx.vout.empty())
     701           6 :         return state.DoS(10, false, REJECT_INVALID, "bad-txns-vout-empty");
     702             :     // Size limits
     703       27853 :     if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) > MAX_BLOCK_SIZE)
     704           0 :         return state.DoS(100, false, REJECT_INVALID, "bad-txns-oversize");
     705             : 
     706             :     // Check for negative or overflow output values
     707       27853 :     CAmount nValueOut = 0;
     708      322538 :     BOOST_FOREACH(const CTxOut& txout, tx.vout)
     709             :     {
     710       30547 :         if (txout.nValue < 0)
     711           6 :             return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-negative");
     712       30546 :         if (txout.nValue > MAX_MONEY)
     713           6 :             return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-toolarge");
     714       30545 :         nValueOut += txout.nValue;
     715       61090 :         if (!MoneyRange(nValueOut))
     716           6 :             return state.DoS(100, false, REJECT_INVALID, "bad-txns-txouttotal-toolarge");
     717             :     }
     718             : 
     719             :     // Check for duplicate inputs
     720             :     set<COutPoint> vInOutPoints;
     721      319112 :     BOOST_FOREACH(const CTxIn& txin, tx.vin)
     722             :     {
     723       59956 :         if (vInOutPoints.count(txin.prevout))
     724          12 :             return state.DoS(100, false, REJECT_INVALID, "bad-txns-inputs-duplicate");
     725       29976 :         vInOutPoints.insert(txin.prevout);
     726             :     }
     727             : 
     728       27848 :     if (tx.IsCoinBase())
     729             :     {
     730       46228 :         if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
     731          78 :             return state.DoS(100, false, REJECT_INVALID, "bad-cb-length");
     732             :     }
     733             :     else
     734             :     {
     735       64830 :         BOOST_FOREACH(const CTxIn& txin, tx.vin)
     736        6860 :             if (txin.prevout.IsNull())
     737           0 :                 return state.DoS(10, false, REJECT_INVALID, "bad-txns-prevout-null");
     738             :     }
     739             : 
     740             :     return true;
     741             : }
     742             : 
     743         530 : CAmount GetMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree)
     744             : {
     745             :     {
     746         530 :         LOCK(mempool.cs);
     747         530 :         uint256 hash = tx.GetHash();
     748         530 :         double dPriorityDelta = 0;
     749         530 :         CAmount nFeeDelta = 0;
     750         530 :         mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
     751         530 :         if (dPriorityDelta > 0 || nFeeDelta > 0)
     752           0 :             return 0;
     753             :     }
     754             : 
     755         530 :     CAmount nMinFee = ::minRelayTxFee.GetFee(nBytes);
     756             : 
     757         530 :     if (fAllowFree)
     758             :     {
     759             :         // There is a free transaction area in blocks created by most miners,
     760             :         // * If we are relaying we allow transactions up to DEFAULT_BLOCK_PRIORITY_SIZE - 1000
     761             :         //   to be considered to fall into this category. We don't want to encourage sending
     762             :         //   multiple transactions instead of one big transaction to avoid fees.
     763         530 :         if (nBytes < (DEFAULT_BLOCK_PRIORITY_SIZE - 1000))
     764         530 :             nMinFee = 0;
     765             :     }
     766             : 
     767        1060 :     if (!MoneyRange(nMinFee))
     768           0 :         nMinFee = MAX_MONEY;
     769         530 :     return nMinFee;
     770             : }
     771             : 
     772             : /** Convert CValidationState to a human-readable message for logging */
     773          13 : static std::string FormatStateMessage(const CValidationState &state)
     774             : {
     775             :     return strprintf("%s%s (code %i)",
     776             :         state.GetRejectReason(),
     777          13 :         state.GetDebugMessage().empty() ? "" : ", "+state.GetDebugMessage(),
     778          93 :         state.GetRejectCode());
     779             : }
     780             : 
     781         547 : bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,
     782             :                         bool* pfMissingInputs, bool fRejectAbsurdFee)
     783             : {
     784         547 :     AssertLockHeld(cs_main);
     785         547 :     if (pfMissingInputs)
     786         370 :         *pfMissingInputs = false;
     787             : 
     788         547 :     if (!CheckTransaction(tx, state))
     789             :         return false;
     790             : 
     791             :     // Coinbase is only valid in a block, not as a loose transaction
     792         547 :     if (tx.IsCoinBase())
     793           0 :         return state.DoS(100, false, REJECT_INVALID, "coinbase");
     794             : 
     795             :     // Rather not work on nonstandard transactions (unless -testnet/-regtest)
     796             :     string reason;
     797         547 :     if (fRequireStandard && !IsStandardTx(tx, reason))
     798           0 :         return state.DoS(0, false, REJECT_NONSTANDARD, reason);
     799             : 
     800             :     // Only accept nLockTime-using transactions that can be mined in the next
     801             :     // block; we don't want our mempool filled up with transactions that can't
     802             :     // be mined yet.
     803         547 :     if (!CheckFinalTx(tx))
     804          66 :         return state.DoS(0, false, REJECT_NONSTANDARD, "non-final");
     805             : 
     806             :     // is it already in the memory pool?
     807         536 :     uint256 hash = tx.GetHash();
     808         536 :     if (pool.exists(hash))
     809           0 :         return state.Invalid(false, REJECT_ALREADY_KNOWN, "txn-already-in-mempool");
     810             : 
     811             :     // Check for conflicts with in-memory transactions
     812             :     {
     813         536 :     LOCK(pool.cs); // protect pool.mapNextTx
     814        2556 :     for (unsigned int i = 0; i < tx.vin.size(); i++)
     815             :     {
     816        2020 :         COutPoint outpoint = tx.vin[i].prevout;
     817        2020 :         if (pool.mapNextTx.count(outpoint))
     818             :         {
     819             :             // Disable replacement feature for now
     820           0 :             return state.Invalid(false, REJECT_CONFLICT, "txn-mempool-conflict");
     821             :         }
     822             :     }
     823             :     }
     824             : 
     825             :     {
     826             :         CCoinsView dummy;
     827        1060 :         CCoinsViewCache view(&dummy);
     828             : 
     829         536 :         CAmount nValueIn = 0;
     830             :         {
     831         536 :         LOCK(pool.cs);
     832         536 :         CCoinsViewMemPool viewMemPool(pcoinsTip, pool);
     833         536 :         view.SetBackend(viewMemPool);
     834             : 
     835             :         // do we already have it?
     836         536 :         if (view.HaveCoins(hash))
     837           0 :             return state.Invalid(false, REJECT_ALREADY_KNOWN, "txn-already-known");
     838             : 
     839             :         // do all inputs exist?
     840             :         // Note that this does not check for the presence of actual outputs (see the next check for that),
     841             :         // and only helps with filling in pfMissingInputs (to determine missing vs spent).
     842        9738 :         BOOST_FOREACH(const CTxIn txin, tx.vin) {
     843        1010 :             if (!view.HaveCoins(txin.prevout.hash)) {
     844           3 :                 if (pfMissingInputs)
     845           3 :                     *pfMissingInputs = true;
     846           3 :                 return false; // fMissingInputs and !state.IsInvalid() is used to detect this condition, don't set state.Invalid()
     847             :             }
     848             :         }
     849             : 
     850             :         // are the actual inputs available?
     851         533 :         if (!view.HaveInputs(tx))
     852           6 :             return state.Invalid(false, REJECT_DUPLICATE, "bad-txns-inputs-spent");
     853             : 
     854             :         // Bring the best block into scope
     855         532 :         view.GetBestBlock();
     856             : 
     857         532 :         nValueIn = view.GetValueIn(tx);
     858             : 
     859             :         // we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool
     860         532 :         view.SetBackend(dummy);
     861             :         }
     862             : 
     863             :         // Check for non-standard pay-to-script-hash in inputs
     864         535 :         if (fRequireStandard && !AreInputsStandard(tx, view))
     865           0 :             return state.Invalid(false, REJECT_NONSTANDARD, "bad-txns-nonstandard-inputs");
     866             : 
     867             :         // Check that the transaction doesn't have an excessive number of
     868             :         // sigops, making it impossible to mine. Since the coinbase transaction
     869             :         // itself can contain sigops MAX_STANDARD_TX_SIGOPS is less than
     870             :         // MAX_BLOCK_SIGOPS; we still consider this an invalid rather than
     871             :         // merely non-standard transaction.
     872         532 :         unsigned int nSigOps = GetLegacySigOpCount(tx);
     873         532 :         nSigOps += GetP2SHSigOpCount(tx, view);
     874         532 :         if (nSigOps > MAX_STANDARD_TX_SIGOPS)
     875             :             return state.DoS(0, false, REJECT_NONSTANDARD, "bad-txns-too-many-sigops", false,
     876          10 :                 strprintf("%d > %d", nSigOps, MAX_STANDARD_TX_SIGOPS));
     877             : 
     878         530 :         CAmount nValueOut = tx.GetValueOut();
     879         530 :         CAmount nFees = nValueIn-nValueOut;
     880         530 :         double dPriority = view.GetPriority(tx, chainActive.Height());
     881             : 
     882        1060 :         CTxMemPoolEntry entry(tx, nFees, GetTime(), dPriority, chainActive.Height(), mempool.HasNoInputsOf(tx));
     883         530 :         unsigned int nSize = entry.GetTxSize();
     884             : 
     885             :         // Don't accept it if it can't get into a block
     886         530 :         CAmount txMinFee = GetMinRelayFee(tx, nSize, true);
     887         530 :         if (fLimitFree && nFees < txMinFee)
     888             :             return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "insufficient fee", false,
     889           0 :                 strprintf("%d < %d", nFees, txMinFee));
     890             : 
     891             :         // Require that free transactions have sufficient priority to be mined in the next block.
     892        1617 :         if (GetBoolArg("-relaypriority", true) && nFees < ::minRelayTxFee.GetFee(nSize) && !AllowFree(view.GetPriority(tx, chainActive.Height() + 1))) {
     893           0 :             return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "insufficient priority");
     894             :         }
     895             : 
     896             :         // Continuously rate-limit free (really, very-low-fee) transactions
     897             :         // This mitigates 'penny-flooding' -- sending thousands of free transactions just to
     898             :         // be annoying or make others' transactions take longer to confirm.
     899         530 :         if (fLimitFree && nFees < ::minRelayTxFee.GetFee(nSize))
     900             :         {
     901           3 :             static CCriticalSection csFreeLimiter;
     902             :             static double dFreeCount;
     903             :             static int64_t nLastTime;
     904           2 :             int64_t nNow = GetTime();
     905             : 
     906           2 :             LOCK(csFreeLimiter);
     907             : 
     908             :             // Use an exponentially decaying ~10-minute window:
     909           2 :             dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime));
     910           2 :             nLastTime = nNow;
     911             :             // -limitfreerelay unit is thousand-bytes-per-minute
     912             :             // At default rate it would take over a month to fill 1GB
     913           6 :             if (dFreeCount >= GetArg("-limitfreerelay", 15)*10*1000)
     914           0 :                 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "rate limited free transaction");
     915           2 :             LogPrint("mempool", "Rate limit dFreeCount: %g => %g\n", dFreeCount, dFreeCount+nSize);
     916           2 :             dFreeCount += nSize;
     917             :         }
     918             : 
     919         530 :         if (fRejectAbsurdFee && nFees > ::minRelayTxFee.GetFee(nSize) * 10000)
     920             :             return state.Invalid(false,
     921             :                 REJECT_HIGHFEE, "absurdly-high-fee",
     922           0 :                 strprintf("%d > %d", nFees, ::minRelayTxFee.GetFee(nSize) * 10000));
     923             : 
     924             :         // Calculate in-mempool ancestors, up to a limit.
     925             :         CTxMemPool::setEntries setAncestors;
     926        1590 :         size_t nLimitAncestors = GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT);
     927        1590 :         size_t nLimitAncestorSize = GetArg("-limitancestorsize", DEFAULT_ANCESTOR_SIZE_LIMIT)*1000;
     928        1590 :         size_t nLimitDescendants = GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT);
     929        1590 :         size_t nLimitDescendantSize = GetArg("-limitdescendantsize", DEFAULT_DESCENDANT_SIZE_LIMIT)*1000;
     930             :         std::string errString;
     931         530 :         if (!pool.CalculateMemPoolAncestors(entry, setAncestors, nLimitAncestors, nLimitAncestorSize, nLimitDescendants, nLimitDescendantSize, errString)) {
     932           0 :             return state.DoS(0, false, REJECT_NONSTANDARD, "too-long-mempool-chain", false, errString);
     933             :         }
     934             : 
     935             :         // Check against previous transactions
     936             :         // This is done last to help prevent CPU exhaustion denial-of-service attacks.
     937         530 :         if (!CheckInputs(tx, state, view, true, STANDARD_SCRIPT_VERIFY_FLAGS, true))
     938             :             return false;
     939             : 
     940             :         // Check again against just the consensus-critical mandatory script
     941             :         // verification flags, in case of bugs in the standard flags that cause
     942             :         // transactions to pass as valid when they're actually invalid. For
     943             :         // instance the STRICTENC flag was incorrectly allowing certain
     944             :         // CHECKSIG NOT scripts to pass, even though they were invalid.
     945             :         //
     946             :         // There is a similar check in CreateNewBlock() to prevent creating
     947             :         // invalid blocks, however allowing such transactions into the mempool
     948             :         // can be exploited as a DoS attack.
     949         524 :         if (!CheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true))
     950             :         {
     951             :             return error("%s: BUG! PLEASE REPORT THIS! ConnectInputs failed against MANDATORY but not STANDARD flags %s, %s",
     952           0 :                 __func__, hash.ToString(), FormatStateMessage(state));
     953             :         }
     954             : 
     955             :         // Store transaction in memory
     956         524 :         pool.addUnchecked(hash, entry, setAncestors, !IsInitialBlockDownload());
     957             :     }
     958             : 
     959         524 :     SyncWithWallets(tx, NULL);
     960             : 
     961             :     return true;
     962             : }
     963             : 
     964             : /** Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock */
     965          10 : bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow)
     966             : {
     967          10 :     CBlockIndex *pindexSlow = NULL;
     968             : 
     969          10 :     LOCK(cs_main);
     970             : 
     971          10 :     if (mempool.lookup(hash, txOut))
     972             :     {
     973             :         return true;
     974             :     }
     975             : 
     976           5 :     if (fTxIndex) {
     977             :         CDiskTxPos postx;
     978           1 :         if (pblocktree->ReadTxIndex(hash, postx)) {
     979           1 :             CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
     980           1 :             if (file.IsNull())
     981           0 :                 return error("%s: OpenBlockFile failed", __func__);
     982           1 :             CBlockHeader header;
     983             :             try {
     984           1 :                 file >> header;
     985           1 :                 fseek(file.Get(), postx.nTxOffset, SEEK_CUR);
     986           1 :                 file >> txOut;
     987           0 :             } catch (const std::exception& e) {
     988           0 :                 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
     989             :             }
     990           1 :             hashBlock = header.GetHash();
     991           2 :             if (txOut.GetHash() != hash)
     992           0 :                 return error("%s: txid mismatch", __func__);
     993             :             return true;
     994             :         }
     995             :     }
     996             : 
     997           4 :     if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
     998           3 :         int nHeight = -1;
     999             :         {
    1000           3 :             CCoinsViewCache &view = *pcoinsTip;
    1001           3 :             const CCoins* coins = view.AccessCoins(hash);
    1002           3 :             if (coins)
    1003           3 :                 nHeight = coins->nHeight;
    1004             :         }
    1005           3 :         if (nHeight > 0)
    1006           3 :             pindexSlow = chainActive[nHeight];
    1007             :     }
    1008             : 
    1009           4 :     if (pindexSlow) {
    1010           3 :         CBlock block;
    1011           3 :         if (ReadBlockFromDisk(block, pindexSlow)) {
    1012          33 :             BOOST_FOREACH(const CTransaction &tx, block.vtx) {
    1013           8 :                 if (tx.GetHash() == hash) {
    1014           3 :                     txOut = tx;
    1015           6 :                     hashBlock = pindexSlow->GetBlockHash();
    1016             :                     return true;
    1017             :                 }
    1018             :             }
    1019             :         }
    1020             :     }
    1021             : 
    1022             :     return false;
    1023             : }
    1024             : 
    1025             : 
    1026             : 
    1027             : 
    1028             : 
    1029             : 
    1030             : //////////////////////////////////////////////////////////////////////////////
    1031             : //
    1032             : // CBlock and CBlockIndex
    1033             : //
    1034             : 
    1035        5514 : bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart)
    1036             : {
    1037             :     // Open history file to append
    1038        5514 :     CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
    1039        5514 :     if (fileout.IsNull())
    1040           0 :         return error("WriteBlockToDisk: OpenBlockFile failed");
    1041             : 
    1042             :     // Write index header
    1043       11028 :     unsigned int nSize = fileout.GetSerializeSize(block);
    1044       11028 :     fileout << FLATDATA(messageStart) << nSize;
    1045             : 
    1046             :     // Write block
    1047        5514 :     long fileOutPos = ftell(fileout.Get());
    1048        5514 :     if (fileOutPos < 0)
    1049           0 :         return error("WriteBlockToDisk: ftell failed");
    1050        5514 :     pos.nPos = (unsigned int)fileOutPos;
    1051        5514 :     fileout << block;
    1052             : 
    1053             :     return true;
    1054             : }
    1055             : 
    1056       24377 : bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos)
    1057             : {
    1058       24377 :     block.SetNull();
    1059             : 
    1060             :     // Open history file to read
    1061       24377 :     CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
    1062       24377 :     if (filein.IsNull())
    1063           0 :         return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
    1064             : 
    1065             :     // Read block
    1066             :     try {
    1067       24377 :         filein >> block;
    1068             :     }
    1069           0 :     catch (const std::exception& e) {
    1070           0 :         return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
    1071             :     }
    1072             : 
    1073             :     // Check the header
    1074       48754 :     if (!CheckProofOfWork(block.GetHash(), block.nBits, Params().GetConsensus()))
    1075           0 :         return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
    1076             : 
    1077             :     return true;
    1078             : }
    1079             : 
    1080       24377 : bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex)
    1081             : {
    1082       48754 :     if (!ReadBlockFromDisk(block, pindex->GetBlockPos()))
    1083             :         return false;
    1084       73131 :     if (block.GetHash() != pindex->GetBlockHash())
    1085             :         return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
    1086           0 :                 pindex->ToString(), pindex->GetBlockPos().ToString());
    1087             :     return true;
    1088             : }
    1089             : 
    1090       15548 : CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
    1091             : {
    1092       22347 :     int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
    1093             :     // Force block reward to zero when right shift is undefined.
    1094       22347 :     if (halvings >= 64)
    1095             :         return 0;
    1096             : 
    1097       21784 :     CAmount nSubsidy = 50 * COIN;
    1098             :     // Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
    1099       21784 :     nSubsidy >>= halvings;
    1100       14985 :     return nSubsidy;
    1101             : }
    1102             : 
    1103      165940 : bool IsInitialBlockDownload()
    1104             : {
    1105      165940 :     const CChainParams& chainParams = Params();
    1106      165940 :     LOCK(cs_main);
    1107      165940 :     if (fImporting || fReindex)
    1108             :         return true;
    1109      496892 :     if (fCheckpointsEnabled && chainActive.Height() < Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints()))
    1110             :         return true;
    1111             :     static bool lockIBDState = false;
    1112      165777 :     if (lockIBDState)
    1113             :         return false;
    1114       19882 :     bool state = (chainActive.Height() < pindexBestHeader->nHeight - 24 * 6 ||
    1115       29062 :             pindexBestHeader->GetBlockTime() < GetTime() - chainParams.MaxTipAge());
    1116       10702 :     if (!state)
    1117          74 :         lockIBDState = true;
    1118       10702 :     return state;
    1119             : }
    1120             : 
    1121             : bool fLargeWorkForkFound = false;
    1122             : bool fLargeWorkInvalidChainFound = false;
    1123             : CBlockIndex *pindexBestForkTip = NULL, *pindexBestForkBase = NULL;
    1124             : 
    1125        5521 : void CheckForkWarningConditions()
    1126             : {
    1127        5521 :     AssertLockHeld(cs_main);
    1128             :     // Before we get past initial download, we cannot reliably alert about forks
    1129             :     // (we assume we don't get stuck on a fork before the last checkpoint)
    1130        5521 :     if (IsInitialBlockDownload())
    1131        5521 :         return;
    1132             : 
    1133             :     // If our best fork is no longer within 72 blocks (+/- 12 hours if no one mines it)
    1134             :     // of our head, drop it
    1135        5145 :     if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
    1136           0 :         pindexBestForkTip = NULL;
    1137             : 
    1138        5417 :     if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6)))
    1139             :     {
    1140           0 :         if (!fLargeWorkForkFound && pindexBestForkBase)
    1141             :         {
    1142           0 :             std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
    1143           0 :                 pindexBestForkBase->phashBlock->ToString() + std::string("'");
    1144           0 :             CAlert::Notify(warning, true);
    1145             :         }
    1146           0 :         if (pindexBestForkTip && pindexBestForkBase)
    1147             :         {
    1148           0 :             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__,
    1149             :                    pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
    1150           0 :                    pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
    1151           0 :             fLargeWorkForkFound = true;
    1152             :         }
    1153             :         else
    1154             :         {
    1155           0 :             LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
    1156           0 :             fLargeWorkInvalidChainFound = true;
    1157             :         }
    1158             :     }
    1159             :     else
    1160             :     {
    1161        5145 :         fLargeWorkForkFound = false;
    1162        5145 :         fLargeWorkInvalidChainFound = false;
    1163             :     }
    1164             : }
    1165             : 
    1166          16 : void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
    1167             : {
    1168          16 :     AssertLockHeld(cs_main);
    1169             :     // If we are on a fork that is sufficiently large, set a warning flag
    1170          16 :     CBlockIndex* pfork = pindexNewForkTip;
    1171          16 :     CBlockIndex* plonger = chainActive.Tip();
    1172          46 :     while (pfork && pfork != plonger)
    1173             :     {
    1174          14 :         while (plonger && plonger->nHeight > pfork->nHeight)
    1175           0 :             plonger = plonger->pprev;
    1176          14 :         if (pfork == plonger)
    1177             :             break;
    1178          14 :         pfork = pfork->pprev;
    1179             :     }
    1180             : 
    1181             :     // We define a condition where we should warn the user about as a fork of at least 7 blocks
    1182             :     // with a tip within 72 blocks (+/- 12 hours if no one mines it) of ours
    1183             :     // We use 7 blocks rather arbitrarily as it represents just under 10% of sustained network
    1184             :     // hash rate operating on the fork.
    1185             :     // or a chain that is entirely longer than ours and invalid (note that this should be detected by both)
    1186             :     // We define it this way because it allows us to only store the highest fork tip (+ base) which meets
    1187             :     // the 7-block condition and from this always have the most-likely-to-cause-warning fork
    1188          64 :     if (pfork && (!pindexBestForkTip || (pindexBestForkTip && pindexNewForkTip->nHeight > pindexBestForkTip->nHeight)) &&
    1189          48 :             pindexNewForkTip->nChainWork - pfork->nChainWork > (GetBlockProof(*pfork) * 7) &&
    1190           0 :             chainActive.Height() - pindexNewForkTip->nHeight < 72)
    1191             :     {
    1192           0 :         pindexBestForkTip = pindexNewForkTip;
    1193           0 :         pindexBestForkBase = pfork;
    1194             :     }
    1195             : 
    1196          16 :     CheckForkWarningConditions();
    1197          16 : }
    1198             : 
    1199             : // Requires cs_main.
    1200          57 : void Misbehaving(NodeId pnode, int howmuch)
    1201             : {
    1202          57 :     if (howmuch == 0)
    1203             :         return;
    1204             : 
    1205          57 :     CNodeState *state = State(pnode);
    1206          57 :     if (state == NULL)
    1207             :         return;
    1208             : 
    1209          57 :     state->nMisbehavior += howmuch;
    1210         171 :     int banscore = GetArg("-banscore", 100);
    1211          57 :     if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
    1212             :     {
    1213           6 :         LogPrintf("%s: %s (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__, state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
    1214           6 :         state->fShouldBan = true;
    1215             :     } else
    1216          51 :         LogPrintf("%s: %s (%d -> %d)\n", __func__, state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
    1217             : }
    1218             : 
    1219          35 : void static InvalidChainFound(CBlockIndex* pindexNew)
    1220             : {
    1221          64 :     if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
    1222          10 :         pindexBestInvalid = pindexNew;
    1223             : 
    1224         175 :     LogPrintf("%s: invalid block=%s  height=%d  log2_work=%.8g  date=%s\n", __func__,
    1225             :       pindexNew->GetBlockHash().ToString(), pindexNew->nHeight,
    1226             :       log(pindexNew->nChainWork.getdouble())/log(2.0), DateTimeStrFormat("%Y-%m-%d %H:%M:%S",
    1227          35 :       pindexNew->GetBlockTime()));
    1228          35 :     CBlockIndex *tip = chainActive.Tip();
    1229          35 :     assert (tip);
    1230         210 :     LogPrintf("%s:  current best=%s  height=%d  log2_work=%.8g  date=%s\n", __func__,
    1231             :       tip->GetBlockHash().ToString(), chainActive.Height(), log(tip->nChainWork.getdouble())/log(2.0),
    1232          35 :       DateTimeStrFormat("%Y-%m-%d %H:%M:%S", tip->GetBlockTime()));
    1233          35 :     CheckForkWarningConditions();
    1234          35 : }
    1235             : 
    1236          16 : void static InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state) {
    1237          16 :     int nDoS = 0;
    1238          16 :     if (state.IsInvalid(nDoS)) {
    1239          32 :         std::map<uint256, NodeId>::iterator it = mapBlockSource.find(pindex->GetBlockHash());
    1240          60 :         if (it != mapBlockSource.end() && State(it->second)) {
    1241          14 :             assert (state.GetRejectCode() < REJECT_INTERNAL); // Blocks are never rejected with internal reject codes
    1242          42 :             CBlockReject reject = {(unsigned char)state.GetRejectCode(), state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), pindex->GetBlockHash()};
    1243          28 :             State(it->second)->rejects.push_back(reject);
    1244          14 :             if (nDoS > 0)
    1245          24 :                 Misbehaving(it->second, nDoS);
    1246             :         }
    1247             :     }
    1248          16 :     if (!state.CorruptionPossible()) {
    1249          16 :         pindex->nStatus |= BLOCK_FAILED_VALID;
    1250             :         setDirtyBlockIndex.insert(pindex);
    1251             :         setBlockIndexCandidates.erase(pindex);
    1252          16 :         InvalidChainFound(pindex);
    1253             :     }
    1254          16 : }
    1255             : 
    1256       12271 : void UpdateCoins(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight)
    1257             : {
    1258             :     // mark inputs spent
    1259       12271 :     if (!tx.IsCoinBase()) {
    1260       10924 :         txundo.vprevout.reserve(tx.vin.size());
    1261       63865 :         BOOST_FOREACH(const CTxIn &txin, tx.vin) {
    1262        7311 :             CCoinsModifier coins = inputs.ModifyCoins(txin.prevout.hash);
    1263        7311 :             unsigned nPos = txin.prevout.n;
    1264             : 
    1265       36555 :             if (nPos >= coins->vout.size() || coins->vout[nPos].IsNull())
    1266           0 :                 assert(false);
    1267             :             // mark an outpoint spent, and construct undo information
    1268       36555 :             txundo.vprevout.push_back(CTxInUndo(coins->vout[nPos]));
    1269        7311 :             coins->Spend(nPos);
    1270       14622 :             if (coins->vout.size() == 0) {
    1271       11454 :                 CTxInUndo& undo = txundo.vprevout.back();
    1272        5727 :                 undo.nHeight = coins->nHeight;
    1273        5727 :                 undo.fCoinBase = coins->fCoinBase;
    1274        5727 :                 undo.nVersion = coins->nVersion;
    1275             :             }
    1276        7311 :         }
    1277             :     }
    1278             : 
    1279             :     // add outputs
    1280       24542 :     inputs.ModifyCoins(tx.GetHash())->FromTx(tx, nHeight);
    1281       12271 : }
    1282             : 
    1283        3592 : void UpdateCoins(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, int nHeight)
    1284             : {
    1285             :     CTxUndo txundo;
    1286        3592 :     UpdateCoins(tx, state, inputs, txundo, nHeight);
    1287        3592 : }
    1288             : 
    1289        5916 : bool CScriptCheck::operator()() {
    1290       11832 :     const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
    1291       17748 :     if (!VerifyScript(scriptSig, scriptPubKey, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, cacheStore), &error)) {
    1292             :         return false;
    1293             :     }
    1294        5854 :     return true;
    1295             : }
    1296             : 
    1297        6519 : int GetSpendHeight(const CCoinsViewCache& inputs)
    1298             : {
    1299        6519 :     LOCK(cs_main);
    1300       19557 :     CBlockIndex* pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
    1301       13038 :     return pindexPrev->nHeight + 1;
    1302             : }
    1303             : 
    1304             : namespace Consensus {
    1305        6519 : bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight)
    1306             : {
    1307             :         // This doesn't trigger the DoS code on purpose; if it did, it would make it easier
    1308             :         // for an attacker to attempt to split the network.
    1309        6519 :         if (!inputs.HaveInputs(tx))
    1310           0 :             return state.Invalid(false, 0, "", "Inputs unavailable");
    1311             : 
    1312        6519 :         CAmount nValueIn = 0;
    1313        6519 :         CAmount nFees = 0;
    1314       31664 :         for (unsigned int i = 0; i < tx.vin.size(); i++)
    1315             :         {
    1316       18632 :             const COutPoint &prevout = tx.vin[i].prevout;
    1317        9316 :             const CCoins *coins = inputs.AccessCoins(prevout.hash);
    1318        9316 :             assert(coins);
    1319             : 
    1320             :             // If prev is coinbase, check that it's matured
    1321        9316 :             if (coins->IsCoinBase()) {
    1322        2929 :                 if (nSpendHeight - coins->nHeight < COINBASE_MATURITY)
    1323             :                     return state.Invalid(false,
    1324             :                         REJECT_INVALID, "bad-txns-premature-spend-of-coinbase",
    1325          15 :                         strprintf("tried to spend coinbase at depth %d", nSpendHeight - coins->nHeight));
    1326             :             }
    1327             : 
    1328             :             // Check for negative or overflow input values
    1329       18626 :             nValueIn += coins->vout[prevout.n].nValue;
    1330       27939 :             if (!MoneyRange(coins->vout[prevout.n].nValue) || !MoneyRange(nValueIn))
    1331           0 :                 return state.DoS(100, false, REJECT_INVALID, "bad-txns-inputvalues-outofrange");
    1332             : 
    1333             :         }
    1334             : 
    1335        6516 :         if (nValueIn < tx.GetValueOut())
    1336             :             return state.DoS(100, false, REJECT_INVALID, "bad-txns-in-belowout", false,
    1337           0 :                 strprintf("value in (%s) < value out (%s)", FormatMoney(nValueIn), FormatMoney(tx.GetValueOut())));
    1338             : 
    1339             :         // Tally transaction fees
    1340        6516 :         CAmount nTxFee = nValueIn - tx.GetValueOut();
    1341        6516 :         if (nTxFee < 0)
    1342           0 :             return state.DoS(100, false, REJECT_INVALID, "bad-txns-fee-negative");
    1343        6516 :         nFees += nTxFee;
    1344       13032 :         if (!MoneyRange(nFees))
    1345           0 :             return state.DoS(100, false, REJECT_INVALID, "bad-txns-fee-outofrange");
    1346             :     return true;
    1347             : }
    1348             : }// namespace Consensus
    1349             : 
    1350        6519 : bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, std::vector<CScriptCheck> *pvChecks)
    1351             : {
    1352        6519 :     if (!tx.IsCoinBase())
    1353             :     {
    1354        6519 :         if (!Consensus::CheckTxInputs(tx, state, inputs, GetSpendHeight(inputs)))
    1355             :             return false;
    1356             : 
    1357        6516 :         if (pvChecks)
    1358        3740 :             pvChecks->reserve(tx.vin.size());
    1359             : 
    1360             :         // The first loop above does all the inexpensive checks.
    1361             :         // Only if ALL inputs pass do we perform expensive ECDSA signature checks.
    1362             :         // Helps prevent CPU exhaustion attacks.
    1363             : 
    1364             :         // Skip ECDSA signature verification when connecting blocks
    1365             :         // before the last block chain checkpoint. This is safe because block merkle hashes are
    1366             :         // still computed and checked, and any change will be caught at the next checkpoint.
    1367        6516 :         if (fScriptChecks) {
    1368       15835 :             for (unsigned int i = 0; i < tx.vin.size(); i++) {
    1369       11694 :                 const COutPoint &prevout = tx.vin[i].prevout;
    1370        5847 :                 const CCoins* coins = inputs.AccessCoins(prevout.hash);
    1371        5847 :                 assert(coins);
    1372             : 
    1373             :                 // Verify signature
    1374        5847 :                 CScriptCheck check(*coins, tx, i, flags, cacheStore);
    1375        5847 :                 if (pvChecks) {
    1376        4970 :                     pvChecks->push_back(CScriptCheck());
    1377        2485 :                     check.swap(pvChecks->back());
    1378        3362 :                 } else if (!check()) {
    1379           6 :                     if (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) {
    1380             :                         // Check whether the failure was caused by a
    1381             :                         // non-mandatory script verification check, such as
    1382             :                         // non-standard DER encodings or non-null dummy
    1383             :                         // arguments; if so, don't trigger DoS protection to
    1384             :                         // avoid splitting the network between upgraded and
    1385             :                         // non-upgraded nodes.
    1386             :                         CScriptCheck check(*coins, tx, i,
    1387           5 :                                 flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, cacheStore);
    1388           5 :                         if (check())
    1389          25 :                             return state.Invalid(false, REJECT_NONSTANDARD, strprintf("non-mandatory-script-verify-flag (%s)", ScriptErrorString(check.GetScriptError())));
    1390             :                     }
    1391             :                     // Failures of other flags indicate a transaction that is
    1392             :                     // invalid in new blocks, e.g. a invalid P2SH. We DoS ban
    1393             :                     // such nodes as they are not following the protocol. That
    1394             :                     // said during an upgrade careful thought should be taken
    1395             :                     // as to the correct behavior - we may want to continue
    1396             :                     // peering with non-upgraded nodes even after a soft-fork
    1397             :                     // super-majority vote has passed.
    1398           5 :                     return state.DoS(100,false, REJECT_INVALID, strprintf("mandatory-script-verify-flag-failed (%s)", ScriptErrorString(check.GetScriptError())));
    1399             :                 }
    1400             :             }
    1401             :         }
    1402             :     }
    1403             : 
    1404             :     return true;
    1405             : }
    1406             : 
    1407             : namespace {
    1408             : 
    1409        5432 : bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
    1410             : {
    1411             :     // Open history file to append
    1412        5432 :     CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
    1413        5432 :     if (fileout.IsNull())
    1414           0 :         return error("%s: OpenUndoFile failed", __func__);
    1415             : 
    1416             :     // Write index header
    1417       10864 :     unsigned int nSize = fileout.GetSerializeSize(blockundo);
    1418       10864 :     fileout << FLATDATA(messageStart) << nSize;
    1419             : 
    1420             :     // Write undo data
    1421        5432 :     long fileOutPos = ftell(fileout.Get());
    1422        5432 :     if (fileOutPos < 0)
    1423           0 :         return error("%s: ftell failed", __func__);
    1424        5432 :     pos.nPos = (unsigned int)fileOutPos;
    1425        5432 :     fileout << blockundo;
    1426             : 
    1427             :     // calculate & write checksum
    1428             :     CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
    1429             :     hasher << hashBlock;
    1430             :     hasher << blockundo;
    1431        5432 :     fileout << hasher.GetHash();
    1432             : 
    1433        5432 :     return true;
    1434             : }
    1435             : 
    1436       22216 : bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock)
    1437             : {
    1438             :     // Open history file to read
    1439       22216 :     CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
    1440       22216 :     if (filein.IsNull())
    1441           0 :         return error("%s: OpenBlockFile failed", __func__);
    1442             : 
    1443             :     // Read block
    1444             :     uint256 hashChecksum;
    1445             :     try {
    1446       22216 :         filein >> blockundo;
    1447       22216 :         filein >> hashChecksum;
    1448             :     }
    1449           0 :     catch (const std::exception& e) {
    1450           0 :         return error("%s: Deserialize or I/O error - %s", __func__, e.what());
    1451             :     }
    1452             : 
    1453             :     // Verify checksum
    1454             :     CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
    1455             :     hasher << hashBlock;
    1456             :     hasher << blockundo;
    1457       22216 :     if (hashChecksum != hasher.GetHash())
    1458           0 :         return error("%s: Checksum mismatch", __func__);
    1459             : 
    1460             :     return true;
    1461             : }
    1462             : 
    1463             : /** Abort with a message */
    1464           0 : bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
    1465             : {
    1466             :     strMiscWarning = strMessage;
    1467           0 :     LogPrintf("*** %s\n", strMessage);
    1468             :     uiInterface.ThreadSafeMessageBox(
    1469             :         userMessage.empty() ? _("Error: A fatal internal error occurred, see debug.log for details") : userMessage,
    1470           0 :         "", CClientUIInterface::MSG_ERROR);
    1471           0 :     StartShutdown();
    1472           0 :     return false;
    1473             : }
    1474             : 
    1475           0 : bool AbortNode(CValidationState& state, const std::string& strMessage, const std::string& userMessage="")
    1476             : {
    1477           0 :     AbortNode(strMessage, userMessage);
    1478           0 :     return state.Error(strMessage);
    1479             : }
    1480             : 
    1481             : } // anon namespace
    1482             : 
    1483             : /**
    1484             :  * Apply the undo operation of a CTxInUndo to the given chain state.
    1485             :  * @param undo The undo object.
    1486             :  * @param view The coins view to which to apply the changes.
    1487             :  * @param out The out point that corresponds to the tx input.
    1488             :  * @return True on success.
    1489             :  */
    1490         371 : static bool ApplyTxInUndo(const CTxInUndo& undo, CCoinsViewCache& view, const COutPoint& out)
    1491             : {
    1492         371 :     bool fClean = true;
    1493             : 
    1494         371 :     CCoinsModifier coins = view.ModifyCoins(out.hash);
    1495         371 :     if (undo.nHeight != 0) {
    1496             :         // undo data contains height: this is the last output of the prevout tx being spent
    1497         230 :         if (!coins->IsPruned())
    1498           0 :             fClean = fClean && error("%s: undo data overwriting existing transaction", __func__);
    1499         230 :         coins->Clear();
    1500         230 :         coins->fCoinBase = undo.fCoinBase;
    1501         230 :         coins->nHeight = undo.nHeight;
    1502         230 :         coins->nVersion = undo.nVersion;
    1503             :     } else {
    1504         141 :         if (coins->IsPruned())
    1505           0 :             fClean = fClean && error("%s: undo data adding output to missing transaction", __func__);
    1506             :     }
    1507        1113 :     if (coins->IsAvailable(out.n))
    1508           0 :         fClean = fClean && error("%s: undo data overwriting existing output", __func__);
    1509         742 :     if (coins->vout.size() < out.n+1)
    1510         900 :         coins->vout.resize(out.n+1);
    1511        1113 :     coins->vout[out.n] = undo.txout;
    1512             : 
    1513         371 :     return fClean;
    1514             : }
    1515             : 
    1516       11134 : bool DisconnectBlock(const CBlock& block, CValidationState& state, const CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean)
    1517             : {
    1518       22268 :     assert(pindex->GetBlockHash() == view.GetBestBlock());
    1519             : 
    1520       11134 :     if (pfClean)
    1521       11082 :         *pfClean = false;
    1522             : 
    1523       11134 :     bool fClean = true;
    1524             : 
    1525             :     CBlockUndo blockUndo;
    1526       22268 :     CDiskBlockPos pos = pindex->GetUndoPos();
    1527       11134 :     if (pos.IsNull())
    1528           0 :         return error("DisconnectBlock(): no undo data available");
    1529       22268 :     if (!UndoReadFromDisk(blockUndo, pos, pindex->pprev->GetBlockHash()))
    1530           0 :         return error("DisconnectBlock(): failure reading undo data");
    1531             : 
    1532       33402 :     if (blockUndo.vtxundo.size() + 1 != block.vtx.size())
    1533           0 :         return error("DisconnectBlock(): block and undo data inconsistent");
    1534             : 
    1535             :     // undo transactions in reverse order
    1536       22551 :     for (int i = block.vtx.size() - 1; i >= 0; i--) {
    1537       22834 :         const CTransaction &tx = block.vtx[i];
    1538       11417 :         uint256 hash = tx.GetHash();
    1539             : 
    1540             :         // Check that all outputs are available and match the outputs in the block itself
    1541             :         // exactly.
    1542             :         {
    1543       11417 :         CCoinsModifier outs = view.ModifyCoins(hash);
    1544       11417 :         outs->ClearUnspendable();
    1545             : 
    1546       11417 :         CCoins outsBlock(tx, pindex->nHeight);
    1547             :         // The CCoins serialization does not serialize negative numbers.
    1548             :         // No network rules currently depend on the version here, so an inconsistency is harmless
    1549             :         // but it must be corrected before txout nversion ever influences a network rule.
    1550       11417 :         if (outsBlock.nVersion < 0)
    1551           0 :             outs->nVersion = outsBlock.nVersion;
    1552       22834 :         if (*outs != outsBlock)
    1553           0 :             fClean = fClean && error("DisconnectBlock(): added transaction mismatch? database corrupted");
    1554             : 
    1555             :         // remove outputs
    1556       22834 :         outs->Clear();
    1557             :         }
    1558             : 
    1559             :         // restore inputs
    1560       11417 :         if (i > 0) { // not coinbases
    1561         566 :             const CTxUndo &txundo = blockUndo.vtxundo[i-1];
    1562         849 :             if (txundo.vprevout.size() != tx.vin.size())
    1563           0 :                 return error("DisconnectBlock(): transaction and undo data inconsistent");
    1564        1220 :             for (unsigned int j = tx.vin.size(); j-- > 0;) {
    1565         742 :                 const COutPoint &out = tx.vin[j].prevout;
    1566         742 :                 const CTxInUndo &undo = txundo.vprevout[j];
    1567         371 :                 if (!ApplyTxInUndo(undo, view, out))
    1568           0 :                     fClean = false;
    1569             :             }
    1570             :         }
    1571             :     }
    1572             : 
    1573             :     // move best block pointer to prevout block
    1574       22268 :     view.SetBestBlock(pindex->pprev->GetBlockHash());
    1575             : 
    1576       11134 :     if (pfClean) {
    1577       11082 :         *pfClean = fClean;
    1578       11082 :         return true;
    1579             :     }
    1580             : 
    1581          52 :     return fClean;
    1582             : }
    1583             : 
    1584         155 : void static FlushBlockFile(bool fFinalize = false)
    1585             : {
    1586         155 :     LOCK(cs_LastBlockFile);
    1587             : 
    1588         155 :     CDiskBlockPos posOld(nLastBlockFile, 0);
    1589             : 
    1590         155 :     FILE *fileOld = OpenBlockFile(posOld);
    1591         155 :     if (fileOld) {
    1592         155 :         if (fFinalize)
    1593           0 :             TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nSize);
    1594         155 :         FileCommit(fileOld);
    1595         155 :         fclose(fileOld);
    1596             :     }
    1597             : 
    1598         155 :     fileOld = OpenUndoFile(posOld);
    1599         155 :     if (fileOld) {
    1600         155 :         if (fFinalize)
    1601           0 :             TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nUndoSize);
    1602         155 :         FileCommit(fileOld);
    1603         155 :         fclose(fileOld);
    1604             :     }
    1605         155 : }
    1606             : 
    1607             : bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
    1608             : 
    1609          96 : static CCheckQueue<CScriptCheck> scriptcheckqueue(128);
    1610             : 
    1611         332 : void ThreadScriptCheck() {
    1612         332 :     RenameThread("bitcoin-scriptch");
    1613             :     scriptcheckqueue.Thread();
    1614           0 : }
    1615             : 
    1616             : //
    1617             : // Called periodically asynchronously; alerts if it smells like
    1618             : // we're being fed a bad chain (blocks being generated much
    1619             : // too slowly or too quickly).
    1620             : //
    1621           4 : void PartitionCheck(bool (*initialDownloadCheck)(), CCriticalSection& cs, const CBlockIndex *const &bestHeader,
    1622             :                     int64_t nPowTargetSpacing)
    1623             : {
    1624           5 :     if (bestHeader == NULL || initialDownloadCheck()) return;
    1625             : 
    1626             :     static int64_t lastAlertTime = 0;
    1627           4 :     int64_t now = GetAdjustedTime();
    1628           4 :     if (lastAlertTime > now-60*60*24) return; // Alert at most once per day
    1629             : 
    1630           3 :     const int SPAN_HOURS=4;
    1631           3 :     const int SPAN_SECONDS=SPAN_HOURS*60*60;
    1632           3 :     int BLOCKS_EXPECTED = SPAN_SECONDS / nPowTargetSpacing;
    1633             : 
    1634           3 :     boost::math::poisson_distribution<double> poisson(BLOCKS_EXPECTED);
    1635             : 
    1636             :     std::string strWarning;
    1637           3 :     int64_t startTime = GetAdjustedTime()-SPAN_SECONDS;
    1638             : 
    1639           3 :     LOCK(cs);
    1640           3 :     const CBlockIndex* i = bestHeader;
    1641           3 :     int nBlocks = 0;
    1642         183 :     while (i->GetBlockTime() >= startTime) {
    1643          87 :         ++nBlocks;
    1644          87 :         i = i->pprev;
    1645          87 :         if (i == NULL) return; // Ran out of chain, we must not be fully sync'ed
    1646             :     }
    1647             : 
    1648             :     // How likely is it to find that many by chance?
    1649           6 :     double p = boost::math::pdf(poisson, nBlocks);
    1650             : 
    1651           3 :     LogPrint("partitioncheck", "%s : Found %d blocks in the last %d hours\n", __func__, nBlocks, SPAN_HOURS);
    1652           3 :     LogPrint("partitioncheck", "%s : likelihood: %g\n", __func__, p);
    1653             : 
    1654             :     // Aim for one false-positive about every fifty years of normal running:
    1655           3 :     const int FIFTY_YEARS = 50*365*24*60*60;
    1656           3 :     double alertThreshold = 1.0 / (FIFTY_YEARS / SPAN_SECONDS);
    1657             : 
    1658           3 :     if (p <= alertThreshold && nBlocks < BLOCKS_EXPECTED)
    1659             :     {
    1660             :         // Many fewer blocks than expected: alert!
    1661           3 :         strWarning = strprintf(_("WARNING: check your network connection, %d blocks received in the last %d hours (%d expected)"),
    1662           1 :                                nBlocks, SPAN_HOURS, BLOCKS_EXPECTED);
    1663             :     }
    1664           2 :     else if (p <= alertThreshold && nBlocks > BLOCKS_EXPECTED)
    1665             :     {
    1666             :         // Many more blocks than expected: alert!
    1667           3 :         strWarning = strprintf(_("WARNING: abnormally high number of blocks generated, %d blocks received in the last %d hours (%d expected)"),
    1668             :                                nBlocks, SPAN_HOURS, BLOCKS_EXPECTED);
    1669             :     }
    1670           3 :     if (!strWarning.empty())
    1671             :     {
    1672             :         strMiscWarning = strWarning;
    1673           2 :         CAlert::Notify(strWarning, true);
    1674           2 :         lastAlertTime = now;
    1675             :     }
    1676             : }
    1677             : 
    1678             : static int64_t nTimeVerify = 0;
    1679             : static int64_t nTimeConnect = 0;
    1680             : static int64_t nTimeIndex = 0;
    1681             : static int64_t nTimeCallbacks = 0;
    1682             : static int64_t nTimeTotal = 0;
    1683             : 
    1684        6871 : bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool fJustCheck)
    1685             : {
    1686        6871 :     const CChainParams& chainparams = Params();
    1687        6871 :     AssertLockHeld(cs_main);
    1688             :     // Check it again in case a previous version let a bad block in
    1689        6871 :     if (!CheckBlock(block, state, !fJustCheck, !fJustCheck))
    1690             :         return false;
    1691             : 
    1692             :     // verify that the view's current state corresponds to the previous block
    1693       13680 :     uint256 hashPrevBlock = pindex->pprev == NULL ? uint256() : pindex->pprev->GetBlockHash();
    1694       13742 :     assert(hashPrevBlock == view.GetBestBlock());
    1695             : 
    1696             :     // Special case for the genesis block, skipping connection of its transactions
    1697             :     // (its coinbase is unspendable)
    1698       13742 :     if (block.GetHash() == chainparams.GetConsensus().hashGenesisBlock) {
    1699          62 :         if (!fJustCheck)
    1700         124 :             view.SetBestBlock(pindex->GetBlockHash());
    1701             :         return true;
    1702             :     }
    1703             : 
    1704        6809 :     bool fScriptChecks = true;
    1705        6809 :     if (fCheckpointsEnabled) {
    1706        6686 :         CBlockIndex *pindexLastCheckpoint = Checkpoints::GetLastCheckpoint(chainparams.Checkpoints());
    1707        6686 :         if (pindexLastCheckpoint && pindexLastCheckpoint->GetAncestor(pindex->nHeight) == pindex) {
    1708             :             // This block is an ancestor of a checkpoint: disable script checks
    1709           0 :             fScriptChecks = false;
    1710             :         }
    1711             :     }
    1712             : 
    1713             :     // Do not allow blocks that contain transactions which 'overwrite' older transactions,
    1714             :     // unless those are already completely spent.
    1715             :     // If such overwrites are allowed, coinbases and transactions depending upon those
    1716             :     // can be duplicated to remove the ability to spend the first instance -- even after
    1717             :     // being sent to another address.
    1718             :     // See BIP30 and http://r6.ca/blog/20120206T005236Z.html for more information.
    1719             :     // This logic is not necessary for memory pool transactions, as AcceptToMemoryPool
    1720             :     // already refuses previously-known transaction ids entirely.
    1721             :     // This rule was originally applied to all blocks with a timestamp after March 15, 2012, 0:00 UTC.
    1722             :     // Now that the whole chain is irreversibly beyond that time it is applied to all blocks except the
    1723             :     // two in the chain that violate it. This prevents exploiting the issue against nodes during their
    1724             :     // initial block download.
    1725       12265 :     bool fEnforceBIP30 = (!pindex->phashBlock) || // Enforce on CreateNewBlock invocations which don't have a hash.
    1726       12265 :                           !((pindex->nHeight==91842 && pindex->GetBlockHash() == uint256S("0x00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec")) ||
    1727       12265 :                            (pindex->nHeight==91880 && pindex->GetBlockHash() == uint256S("0x00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721")));
    1728        6809 :     if (fEnforceBIP30) {
    1729       86179 :         BOOST_FOREACH(const CTransaction& tx, block.vtx) {
    1730        8689 :             const CCoins* coins = view.AccessCoins(tx.GetHash());
    1731        8689 :             if (coins && !coins->IsPruned())
    1732           0 :                 return state.DoS(100, error("ConnectBlock(): tried to overwrite transaction"),
    1733           0 :                                  REJECT_INVALID, "bad-txns-BIP30");
    1734             :         }
    1735             :     }
    1736             : 
    1737             :     // BIP16 didn't become active until Apr 1 2012
    1738        6809 :     int64_t nBIP16SwitchTime = 1333238400;
    1739       13618 :     bool fStrictPayToScriptHash = (pindex->GetBlockTime() >= nBIP16SwitchTime);
    1740             : 
    1741        6809 :     unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE;
    1742             : 
    1743             :     // Start enforcing the DERSIG (BIP66) rules, for block.nVersion=3 blocks, when 75% of the network has upgraded:
    1744       13260 :     if (block.nVersion >= 3 && IsSuperMajority(3, pindex->pprev, chainparams.GetConsensus().nMajorityEnforceBlockUpgrade, chainparams.GetConsensus())) {
    1745           0 :         flags |= SCRIPT_VERIFY_DERSIG;
    1746             :     }
    1747             : 
    1748             :     CBlockUndo blockundo;
    1749             : 
    1750       13618 :     CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL);
    1751             : 
    1752        6809 :     int64_t nTimeStart = GetTimeMicros();
    1753        6809 :     CAmount nFees = 0;
    1754        6809 :     int nInputs = 0;
    1755        6809 :     unsigned int nSigOps = 0;
    1756       34045 :     CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
    1757             :     std::vector<std::pair<uint256, CDiskTxPos> > vPos;
    1758        6809 :     vPos.reserve(block.vtx.size());
    1759       13618 :     blockundo.vtxundo.reserve(block.vtx.size() - 1);
    1760       24167 :     for (unsigned int i = 0; i < block.vtx.size(); i++)
    1761             :     {
    1762       17378 :         const CTransaction &tx = block.vtx[i];
    1763             : 
    1764       17378 :         nInputs += tx.vin.size();
    1765        8689 :         nSigOps += GetLegacySigOpCount(tx);
    1766        8689 :         if (nSigOps > MAX_BLOCK_SIGOPS)
    1767           0 :             return state.DoS(100, error("ConnectBlock(): too many sigops"),
    1768           0 :                              REJECT_INVALID, "bad-blk-sigops");
    1769             : 
    1770        8689 :         if (!tx.IsCoinBase())
    1771             :         {
    1772        1880 :             if (!view.HaveInputs(tx))
    1773           8 :                 return state.DoS(100, error("ConnectBlock(): inputs missing/spent"),
    1774          56 :                                  REJECT_INVALID, "bad-txns-inputs-missingorspent");
    1775             : 
    1776        1872 :             if (fStrictPayToScriptHash)
    1777             :             {
    1778             :                 // Add in sigops done by pay-to-script-hash inputs;
    1779             :                 // this is to prevent a "rogue miner" from creating
    1780             :                 // an incredibly-expensive-to-validate block.
    1781        1870 :                 nSigOps += GetP2SHSigOpCount(tx, view);
    1782        1870 :                 if (nSigOps > MAX_BLOCK_SIGOPS)
    1783           0 :                     return state.DoS(100, error("ConnectBlock(): too many sigops"),
    1784           0 :                                      REJECT_INVALID, "bad-blk-sigops");
    1785             :             }
    1786             : 
    1787        1872 :             nFees += view.GetValueIn(tx)-tx.GetValueOut();
    1788             : 
    1789             :             std::vector<CScriptCheck> vChecks;
    1790        1872 :             if (!CheckInputs(tx, state, view, fScriptChecks, flags, false, nScriptCheckThreads ? &vChecks : NULL))
    1791             :                 return error("ConnectBlock(): CheckInputs on %s failed with %s",
    1792           6 :                     tx.GetHash().ToString(), FormatStateMessage(state));
    1793        3740 :             control.Add(vChecks);
    1794             :         }
    1795             : 
    1796             :         CTxUndo undoDummy;
    1797        8679 :         if (i > 0) {
    1798        5610 :             blockundo.vtxundo.push_back(CTxUndo());
    1799             :         }
    1800       17358 :         UpdateCoins(tx, state, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight);
    1801             : 
    1802        8679 :         vPos.push_back(std::make_pair(tx.GetHash(), pos));
    1803        8679 :         pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
    1804             :     }
    1805        6799 :     int64_t nTime1 = GetTimeMicros(); nTimeConnect += nTime1 - nTimeStart;
    1806       13598 :     LogPrint("bench", "      - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs]\n", (unsigned)block.vtx.size(), 0.001 * (nTime1 - nTimeStart), 0.001 * (nTime1 - nTimeStart) / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * (nTime1 - nTimeStart) / (nInputs-1), nTimeConnect * 0.000001);
    1807             : 
    1808       13598 :     CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, chainparams.GetConsensus());
    1809        6799 :     if (block.vtx[0].GetValueOut() > blockReward)
    1810             :         return state.DoS(100,
    1811             :                          error("ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)",
    1812          12 :                                block.vtx[0].GetValueOut(), blockReward),
    1813          36 :                                REJECT_INVALID, "bad-cb-amount");
    1814             : 
    1815        6793 :     if (!control.Wait())
    1816           0 :         return state.DoS(100, false);
    1817        6793 :     int64_t nTime2 = GetTimeMicros(); nTimeVerify += nTime2 - nTimeStart;
    1818        6793 :     LogPrint("bench", "    - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs]\n", nInputs - 1, 0.001 * (nTime2 - nTimeStart), nInputs <= 1 ? 0 : 0.001 * (nTime2 - nTimeStart) / (nInputs-1), nTimeVerify * 0.000001);
    1819             : 
    1820        6793 :     if (fJustCheck)
    1821             :         return true;
    1822             : 
    1823             :     // Write undo information to disk
    1824       10880 :     if (pindex->GetUndoPos().IsNull() || !pindex->IsValid(BLOCK_VALID_SCRIPTS))
    1825             :     {
    1826       10864 :         if (pindex->GetUndoPos().IsNull()) {
    1827             :             CDiskBlockPos pos;
    1828        5432 :             if (!FindUndoPos(state, pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
    1829           0 :                 return error("ConnectBlock(): FindUndoPos failed");
    1830       10864 :             if (!UndoWriteToDisk(blockundo, pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart()))
    1831           0 :                 return AbortNode(state, "Failed to write undo data");
    1832             : 
    1833             :             // update nUndoPos in block index
    1834        5432 :             pindex->nUndoPos = pos.nPos;
    1835        5432 :             pindex->nStatus |= BLOCK_HAVE_UNDO;
    1836             :         }
    1837             : 
    1838        5432 :         pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
    1839             :         setDirtyBlockIndex.insert(pindex);
    1840             :     }
    1841             : 
    1842        5440 :     if (fTxIndex)
    1843         107 :         if (!pblocktree->WriteTxIndex(vPos))
    1844           0 :             return AbortNode(state, "Failed to write transaction index");
    1845             : 
    1846             :     // add this block to the view's block chain
    1847       10880 :     view.SetBestBlock(pindex->GetBlockHash());
    1848             : 
    1849        5440 :     int64_t nTime3 = GetTimeMicros(); nTimeIndex += nTime3 - nTime2;
    1850        5440 :     LogPrint("bench", "    - Index writing: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeIndex * 0.000001);
    1851             : 
    1852             :     // Watch for changes to the previous coinbase transaction.
    1853        5508 :     static uint256 hashPrevBestCoinBase;
    1854        5440 :     GetMainSignals().UpdatedTransaction(hashPrevBestCoinBase);
    1855        5440 :     hashPrevBestCoinBase = block.vtx[0].GetHash();
    1856             : 
    1857        5440 :     int64_t nTime4 = GetTimeMicros(); nTimeCallbacks += nTime4 - nTime3;
    1858        5440 :     LogPrint("bench", "    - Callbacks: %.2fms [%.2fs]\n", 0.001 * (nTime4 - nTime3), nTimeCallbacks * 0.000001);
    1859             : 
    1860        5440 :     return true;
    1861             : }
    1862             : 
    1863             : enum FlushStateMode {
    1864             :     FLUSH_STATE_NONE,
    1865             :     FLUSH_STATE_IF_NEEDED,
    1866             :     FLUSH_STATE_PERIODIC,
    1867             :     FLUSH_STATE_ALWAYS
    1868             : };
    1869             : 
    1870             : /**
    1871             :  * Update the on-disk chain state.
    1872             :  * The caches and indexes are flushed depending on the mode we're called with
    1873             :  * if they're too large, if it's been a while since the last write,
    1874             :  * or always and in all cases if we're in prune mode and are deleting files.
    1875             :  */
    1876       10199 : bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
    1877       10199 :     LOCK2(cs_main, cs_LastBlockFile);
    1878             :     static int64_t nLastWrite = 0;
    1879             :     static int64_t nLastFlush = 0;
    1880             :     static int64_t nLastSetChain = 0;
    1881             :     std::set<int> setFilesToPrune;
    1882       10199 :     bool fFlushForPrune = false;
    1883             :     try {
    1884       10199 :     if (fPruneMode && fCheckForPruning) {
    1885           0 :         FindFilesToPrune(setFilesToPrune);
    1886           0 :         fCheckForPruning = false;
    1887           0 :         if (!setFilesToPrune.empty()) {
    1888           0 :             fFlushForPrune = true;
    1889           0 :             if (!fHavePruned) {
    1890           0 :                 pblocktree->WriteFlag("prunedblockfiles", true);
    1891           0 :                 fHavePruned = true;
    1892             :             }
    1893             :         }
    1894             :     }
    1895       10199 :     int64_t nNow = GetTimeMicros();
    1896             :     // Avoid writing/flushing immediately after startup.
    1897       10199 :     if (nLastWrite == 0) {
    1898          95 :         nLastWrite = nNow;
    1899             :     }
    1900       10199 :     if (nLastFlush == 0) {
    1901          95 :         nLastFlush = nNow;
    1902             :     }
    1903       10199 :     if (nLastSetChain == 0) {
    1904          95 :         nLastSetChain = nNow;
    1905             :     }
    1906       10199 :     size_t cacheSize = pcoinsTip->DynamicMemoryUsage();
    1907             :     // The cache is large and close to the limit, but we have time now (not in the middle of a block processing).
    1908       10199 :     bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize * (10.0/9) > nCoinCacheUsage;
    1909             :     // The cache is over the limit, we have to write now.
    1910       10199 :     bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && cacheSize > nCoinCacheUsage;
    1911             :     // 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.
    1912       10199 :     bool fPeriodicWrite = mode == FLUSH_STATE_PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
    1913             :     // It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
    1914       10199 :     bool fPeriodicFlush = mode == FLUSH_STATE_PERIODIC && nNow > nLastFlush + (int64_t)DATABASE_FLUSH_INTERVAL * 1000000;
    1915             :     // Combine all conditions that result in a full cache flush.
    1916       10199 :     bool fDoFullFlush = (mode == FLUSH_STATE_ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
    1917             :     // Write blocks and block index to disk.
    1918       10199 :     if (fDoFullFlush || fPeriodicWrite) {
    1919             :         // Depend on nMinDiskSpace to ensure we can write block index
    1920         155 :         if (!CheckDiskSpace(0))
    1921           0 :             return state.Error("out of disk space");
    1922             :         // First make sure all block and undo data is flushed to disk.
    1923         155 :         FlushBlockFile();
    1924             :         // Then update all block file information (which may refer to block and undo files).
    1925             :         {
    1926             :             std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
    1927         155 :             vFiles.reserve(setDirtyFileInfo.size());
    1928         438 :             for (set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
    1929         512 :                 vFiles.push_back(make_pair(*it, &vinfoBlockFile[*it]));
    1930         128 :                 setDirtyFileInfo.erase(it++);
    1931             :             }
    1932             :             std::vector<const CBlockIndex*> vBlocks;
    1933         155 :             vBlocks.reserve(setDirtyBlockIndex.size());
    1934        5633 :             for (set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
    1935        5323 :                 vBlocks.push_back(*it);
    1936        5323 :                 setDirtyBlockIndex.erase(it++);
    1937             :             }
    1938         155 :             if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
    1939           0 :                 return AbortNode(state, "Files to write to block index database");
    1940             :             }
    1941             :         }
    1942             :         // Finally remove any pruned files
    1943         155 :         if (fFlushForPrune)
    1944           0 :             UnlinkPrunedFiles(setFilesToPrune);
    1945         155 :         nLastWrite = nNow;
    1946             :     }
    1947             :     // Flush best chain related state. This can only be done if the blocks / block index write was also done.
    1948       10199 :     if (fDoFullFlush) {
    1949             :         // Typical CCoins structures on disk are around 128 bytes in size.
    1950             :         // Pushing a new one to the database can cause it to be written
    1951             :         // twice (once in the log, and once in the tables). This is already
    1952             :         // an overestimation, as most will delete an existing entry or
    1953             :         // overwrite one. Still, use a conservative safety factor of 2.
    1954         155 :         if (!CheckDiskSpace(128 * 2 * 2 * pcoinsTip->GetCacheSize()))
    1955           0 :             return state.Error("out of disk space");
    1956             :         // Flush the chainstate (which may refer to block index entries).
    1957         155 :         if (!pcoinsTip->Flush())
    1958           0 :             return AbortNode(state, "Failed to write to coin database");
    1959         155 :         nLastFlush = nNow;
    1960             :     }
    1961       10199 :     if ((mode == FLUSH_STATE_ALWAYS || mode == FLUSH_STATE_PERIODIC) && nNow > nLastSetChain + (int64_t)DATABASE_WRITE_INTERVAL * 1000000) {
    1962             :         // Update best block in wallet (so we can detect restored wallets).
    1963           0 :         GetMainSignals().SetBestChain(chainActive.GetLocator());
    1964           0 :         nLastSetChain = nNow;
    1965             :     }
    1966           0 :     } catch (const std::runtime_error& e) {
    1967           0 :         return AbortNode(state, std::string("System error while flushing: ") + e.what());
    1968             :     }
    1969             :     return true;
    1970             : }
    1971             : 
    1972          94 : void FlushStateToDisk() {
    1973             :     CValidationState state;
    1974          94 :     FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
    1975          94 : }
    1976             : 
    1977           0 : void PruneAndFlush() {
    1978             :     CValidationState state;
    1979           0 :     fCheckForPruning = true;
    1980           0 :     FlushStateToDisk(state, FLUSH_STATE_NONE);
    1981           0 : }
    1982             : 
    1983             : /** Update chainActive and related internal data structures. */
    1984        5554 : void static UpdateTip(CBlockIndex *pindexNew) {
    1985        5554 :     const CChainParams& chainParams = Params();
    1986        5554 :     chainActive.SetTip(pindexNew);
    1987             : 
    1988             :     // New best block
    1989        5554 :     nTimeBestReceived = GetTime();
    1990        5554 :     mempool.AddTransactionsUpdated(1);
    1991             : 
    1992       66648 :     LogPrintf("%s: new best=%s  height=%d  log2_work=%.8g  tx=%lu  date=%s progress=%f  cache=%.1fMiB(%utx)\n", __func__,
    1993             :       chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
    1994             :       DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
    1995        5554 :       Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip()), pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
    1996             : 
    1997        5554 :     cvBlockChange.notify_all();
    1998             : 
    1999             :     // Check the version of the last 100 blocks to see if we need to upgrade:
    2000             :     static bool fWarned = false;
    2001        5554 :     if (!IsInitialBlockDownload() && !fWarned)
    2002             :     {
    2003        5178 :         int nUpgraded = 0;
    2004        5178 :         const CBlockIndex* pindex = chainActive.Tip();
    2005      370806 :         for (int i = 0; i < 100 && pindex != NULL; i++)
    2006             :         {
    2007      365628 :             if (pindex->nVersion > CBlock::CURRENT_VERSION)
    2008           0 :                 ++nUpgraded;
    2009      365628 :             pindex = pindex->pprev;
    2010             :         }
    2011        5178 :         if (nUpgraded > 0)
    2012           0 :             LogPrintf("%s: %d of last 100 blocks above version %d\n", __func__, nUpgraded, (int)CBlock::CURRENT_VERSION);
    2013        5178 :         if (nUpgraded > 100/2)
    2014             :         {
    2015             :             // strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
    2016           0 :             strMiscWarning = _("Warning: This version is obsolete; upgrade required!");
    2017           0 :             CAlert::Notify(strMiscWarning, true);
    2018           0 :             fWarned = true;
    2019             :         }
    2020             :     }
    2021        5554 : }
    2022             : 
    2023             : /** Disconnect chainActive's tip. */
    2024          52 : bool static DisconnectTip(CValidationState &state) {
    2025          52 :     CBlockIndex *pindexDelete = chainActive.Tip();
    2026          52 :     assert(pindexDelete);
    2027          52 :     mempool.check(pcoinsTip);
    2028             :     // Read block from disk.
    2029          52 :     CBlock block;
    2030          52 :     if (!ReadBlockFromDisk(block, pindexDelete))
    2031           0 :         return AbortNode(state, "Failed to read block");
    2032             :     // Apply the block atomically to the chain state.
    2033          52 :     int64_t nStart = GetTimeMicros();
    2034             :     {
    2035          52 :         CCoinsViewCache view(pcoinsTip);
    2036          52 :         if (!DisconnectBlock(block, state, pindexDelete, view))
    2037           0 :             return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
    2038          52 :         assert(view.Flush());
    2039             :     }
    2040          52 :     LogPrint("bench", "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
    2041             :     // Write the chain state to disk, if necessary.
    2042          52 :     if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED))
    2043             :         return false;
    2044             :     // Resurrect mempool transactions from the disconnected block.
    2045             :     std::vector<uint256> vHashUpdate;
    2046         762 :     BOOST_FOREACH(const CTransaction &tx, block.vtx) {
    2047             :         // ignore validation errors in resurrected transactions
    2048             :         list<CTransaction> removed;
    2049          90 :         CValidationState stateDummy;
    2050          90 :         if (tx.IsCoinBase() || !AcceptToMemoryPool(mempool, stateDummy, tx, false, NULL)) {
    2051          59 :             mempool.remove(tx, removed, true);
    2052          31 :         } else if (mempool.exists(tx.GetHash())) {
    2053          31 :             vHashUpdate.push_back(tx.GetHash());
    2054             :         }
    2055             :     }
    2056             :     // AcceptToMemoryPool/addUnchecked all assume that new mempool entries have
    2057             :     // no in-mempool children, which is generally not true when adding
    2058             :     // previously-confirmed transactions back to the mempool.
    2059             :     // UpdateTransactionsFromBlock finds descendants of any transactions in this
    2060             :     // block that were added back and cleans up the mempool state.
    2061          52 :     mempool.UpdateTransactionsFromBlock(vHashUpdate);
    2062          52 :     mempool.removeCoinbaseSpends(pcoinsTip, pindexDelete->nHeight);
    2063          52 :     mempool.check(pcoinsTip);
    2064             :     // Update chainActive and related variables.
    2065          52 :     UpdateTip(pindexDelete->pprev);
    2066             :     // Let wallets know transactions went from 1-confirmed to
    2067             :     // 0-confirmed or conflicted:
    2068         852 :     BOOST_FOREACH(const CTransaction &tx, block.vtx) {
    2069          90 :         SyncWithWallets(tx, NULL);
    2070             :     }
    2071          52 :     return true;
    2072             : }
    2073             : 
    2074             : static int64_t nTimeReadFromDisk = 0;
    2075             : static int64_t nTimeConnectTotal = 0;
    2076             : static int64_t nTimeFlush = 0;
    2077             : static int64_t nTimeChainState = 0;
    2078             : static int64_t nTimePostConnect = 0;
    2079             : 
    2080             : /**
    2081             :  * Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
    2082             :  * corresponding to pindexNew, to bypass loading it again from disk.
    2083             :  */
    2084        5518 : bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, const CBlock *pblock) {
    2085       11036 :     assert(pindexNew->pprev == chainActive.Tip());
    2086        5518 :     mempool.check(pcoinsTip);
    2087             :     // Read block from disk.
    2088        5518 :     int64_t nTime1 = GetTimeMicros();
    2089        5518 :     CBlock block;
    2090        5518 :     if (!pblock) {
    2091        1272 :         if (!ReadBlockFromDisk(block, pindexNew))
    2092           0 :             return AbortNode(state, "Failed to read block");
    2093             :         pblock = &block;
    2094             :     }
    2095             :     // Apply the block atomically to the chain state.
    2096        5518 :     int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1;
    2097             :     int64_t nTime3;
    2098        5518 :     LogPrint("bench", "  - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * 0.001, nTimeReadFromDisk * 0.000001);
    2099             :     {
    2100        5518 :         CCoinsViewCache view(pcoinsTip);
    2101        5518 :         bool rv = ConnectBlock(*pblock, state, pindexNew, view);
    2102        5518 :         GetMainSignals().BlockChecked(*pblock, state);
    2103        5518 :         if (!rv) {
    2104          16 :             if (state.IsInvalid())
    2105          16 :                 InvalidBlockFound(pindexNew, state);
    2106          48 :             return error("ConnectTip(): ConnectBlock %s failed", pindexNew->GetBlockHash().ToString());
    2107             :         }
    2108       11004 :         mapBlockSource.erase(pindexNew->GetBlockHash());
    2109        5502 :         nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2;
    2110        5502 :         LogPrint("bench", "  - Connect total: %.2fms [%.2fs]\n", (nTime3 - nTime2) * 0.001, nTimeConnectTotal * 0.000001);
    2111        5502 :         assert(view.Flush());
    2112             :     }
    2113        5502 :     int64_t nTime4 = GetTimeMicros(); nTimeFlush += nTime4 - nTime3;
    2114        5502 :     LogPrint("bench", "  - Flush: %.2fms [%.2fs]\n", (nTime4 - nTime3) * 0.001, nTimeFlush * 0.000001);
    2115             :     // Write the chain state to disk, if necessary.
    2116        5502 :     if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED))
    2117             :         return false;
    2118        5502 :     int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
    2119        5502 :     LogPrint("bench", "  - Writing chainstate: %.2fms [%.2fs]\n", (nTime5 - nTime4) * 0.001, nTimeChainState * 0.000001);
    2120             :     // Remove conflicting transactions from the mempool.
    2121             :     list<CTransaction> txConflicted;
    2122        5502 :     mempool.removeForBlock(pblock->vtx, pindexNew->nHeight, txConflicted, !IsInitialBlockDownload());
    2123        5502 :     mempool.check(pcoinsTip);
    2124             :     // Update chainActive & related variables.
    2125        5502 :     UpdateTip(pindexNew);
    2126             :     // Tell wallet about transactions that went from mempool
    2127             :     // to conflicted:
    2128       33090 :     BOOST_FOREACH(const CTransaction &tx, txConflicted) {
    2129          13 :         SyncWithWallets(tx, NULL);
    2130             :     }
    2131             :     // ... and about transactions that got confirmed:
    2132       64296 :     BOOST_FOREACH(const CTransaction &tx, pblock->vtx) {
    2133        6131 :         SyncWithWallets(tx, pblock);
    2134             :     }
    2135             : 
    2136        5502 :     int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
    2137        5502 :     LogPrint("bench", "  - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001);
    2138        5502 :     LogPrint("bench", "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001);
    2139        5502 :     return true;
    2140             : }
    2141             : 
    2142             : /**
    2143             :  * Return the tip of the chain with the most work in it, that isn't
    2144             :  * known to be invalid (it's however far from certain to be valid).
    2145             :  */
    2146        6611 : static CBlockIndex* FindMostWorkChain() {
    2147             :     do {
    2148        6615 :         CBlockIndex *pindexNew = NULL;
    2149             : 
    2150             :         // Find the best candidate header.
    2151             :         {
    2152             :             std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexCandidates.rbegin();
    2153        6615 :             if (it == setBlockIndexCandidates.rend())
    2154             :                 return NULL;
    2155        6614 :             pindexNew = *it;
    2156             :         }
    2157             : 
    2158             :         // Check whether all blocks on the path between the currently active chain and the candidate are valid.
    2159             :         // Just going until the active chain is an optimization, as we know all blocks in it are valid already.
    2160        6614 :         CBlockIndex *pindexTest = pindexNew;
    2161        6614 :         bool fInvalidAncestor = false;
    2162       65408 :         while (pindexTest && !chainActive.Contains(pindexTest)) {
    2163       22818 :             assert(pindexTest->nChainTx || pindexTest->nHeight == 0);
    2164             : 
    2165             :             // Pruned nodes may have entries in setBlockIndexCandidates for
    2166             :             // which block files have been deleted.  Remove those as candidates
    2167             :             // for the most work chain if we come across them; we can't switch
    2168             :             // to a chain unless we have all the non-active-chain parent blocks.
    2169       22818 :             bool fFailedChain = pindexTest->nStatus & BLOCK_FAILED_MASK;
    2170       22818 :             bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
    2171       22818 :             if (fFailedChain || fMissingData) {
    2172             :                 // Candidate chain is not usable (either invalid or missing data)
    2173           8 :                 if (fFailedChain && (pindexBestInvalid == NULL || pindexNew->nChainWork > pindexBestInvalid->nChainWork))
    2174           2 :                     pindexBestInvalid = pindexNew;
    2175           4 :                 CBlockIndex *pindexFailed = pindexNew;
    2176             :                 // Remove the entire chain from the set.
    2177          12 :                 while (pindexTest != pindexFailed) {
    2178           4 :                     if (fFailedChain) {
    2179           4 :                         pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
    2180           0 :                     } else if (fMissingData) {
    2181             :                         // If we're missing data, then add back to mapBlocksUnlinked,
    2182             :                         // so that if the block arrives in the future we can try adding
    2183             :                         // to setBlockIndexCandidates again.
    2184           0 :                         mapBlocksUnlinked.insert(std::make_pair(pindexFailed->pprev, pindexFailed));
    2185             :                     }
    2186             :                     setBlockIndexCandidates.erase(pindexFailed);
    2187           4 :                     pindexFailed = pindexFailed->pprev;
    2188             :                 }
    2189             :                 setBlockIndexCandidates.erase(pindexTest);
    2190           4 :                 fInvalidAncestor = true;
    2191             :                 break;
    2192             :             }
    2193       22814 :             pindexTest = pindexTest->pprev;
    2194             :         }
    2195        6614 :         if (!fInvalidAncestor)
    2196        6610 :             return pindexNew;
    2197           4 :     } while(true);
    2198             : }
    2199             : 
    2200             : /** Delete all entries in setBlockIndexCandidates that are worse than the current tip. */
    2201        5559 : static void PruneBlockIndexCandidates() {
    2202             :     // Note that we can't delete the current block itself, as we may need to return to it later in case a
    2203             :     // reorganization to a better block fails.
    2204        5559 :     std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
    2205      110215 :     while (it != setBlockIndexCandidates.end() && setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) {
    2206       16484 :         setBlockIndexCandidates.erase(it++);
    2207             :     }
    2208             :     // Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates.
    2209        5559 :     assert(!setBlockIndexCandidates.empty());
    2210        5559 : }
    2211             : 
    2212             : /**
    2213             :  * Try to make some progress towards making pindexMostWork the active block.
    2214             :  * pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
    2215             :  */
    2216        5486 : static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork, const CBlock *pblock) {
    2217        5486 :     AssertLockHeld(cs_main);
    2218        5486 :     bool fInvalidFound = false;
    2219        5486 :     const CBlockIndex *pindexOldTip = chainActive.Tip();
    2220        5486 :     const CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork);
    2221             : 
    2222             :     // Disconnect active blocks which are no longer in the best chain.
    2223       10986 :     while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
    2224          38 :         if (!DisconnectTip(state))
    2225             :             return false;
    2226             :     }
    2227             : 
    2228             :     // Build list of new blocks to connect.
    2229             :     std::vector<CBlockIndex*> vpindexToConnect;
    2230        5486 :     bool fContinue = true;
    2231        5486 :     int nHeight = pindexFork ? pindexFork->nHeight : -1;
    2232       16458 :     while (fContinue && nHeight != pindexMostWork->nHeight) {
    2233             :     // Don't iterate the entire list of potential improvements toward the best tip, as we likely only need
    2234             :     // a few blocks along the way.
    2235       10972 :     int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight);
    2236             :     vpindexToConnect.clear();
    2237        5486 :     vpindexToConnect.reserve(nTargetHeight - nHeight);
    2238        5486 :     CBlockIndex *pindexIter = pindexMostWork->GetAncestor(nTargetHeight);
    2239       30183 :     while (pindexIter && pindexIter->nHeight != nHeight) {
    2240       19211 :         vpindexToConnect.push_back(pindexIter);
    2241       19211 :         pindexIter = pindexIter->pprev;
    2242             :     }
    2243        5486 :     nHeight = nTargetHeight;
    2244             : 
    2245             :     // Connect new blocks.
    2246       55056 :     BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) {
    2247        5518 :         if (!ConnectTip(state, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL)) {
    2248          16 :             if (state.IsInvalid()) {
    2249             :                 // The block violates a consensus rule.
    2250          16 :                 if (!state.CorruptionPossible())
    2251          16 :                     InvalidChainFound(vpindexToConnect.back());
    2252          16 :                 state = CValidationState();
    2253          16 :                 fInvalidFound = true;
    2254          16 :                 fContinue = false;
    2255          16 :                 break;
    2256             :             } else {
    2257             :                 // A system error occurred (disk space, database error, ...).
    2258             :                 return false;
    2259             :             }
    2260             :         } else {
    2261        5502 :             PruneBlockIndexCandidates();
    2262       16382 :             if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
    2263             :                 // We're in a better position than we were. Return temporarily to release the lock.
    2264             :                 fContinue = false;
    2265             :                 break;
    2266             :             }
    2267             :         }
    2268             :     }
    2269             :     }
    2270             : 
    2271             :     // Callbacks/notifications for a new best chain.
    2272        5486 :     if (fInvalidFound)
    2273          16 :         CheckForkWarningConditionsOnNewFork(vpindexToConnect.back());
    2274             :     else
    2275        5470 :         CheckForkWarningConditions();
    2276             : 
    2277             :     return true;
    2278             : }
    2279             : 
    2280             : /**
    2281             :  * Make the best chain active, in multiple steps. The result is either failure
    2282             :  * or an activated best chain. pblock is either NULL or a pointer to a block
    2283             :  * that is already loaded (to avoid loading it again from disk).
    2284             :  */
    2285        5615 : bool ActivateBestChain(CValidationState &state, const CBlock *pblock) {
    2286        5615 :     CBlockIndex *pindexNewTip = NULL;
    2287        5615 :     CBlockIndex *pindexMostWork = NULL;
    2288        5615 :     const CChainParams& chainParams = Params();
    2289        5486 :     do {
    2290        6611 :         boost::this_thread::interruption_point();
    2291             : 
    2292             :         bool fInitialDownload;
    2293             :         {
    2294        6611 :             LOCK(cs_main);
    2295        6611 :             pindexMostWork = FindMostWorkChain();
    2296             : 
    2297             :             // Whether we have anything to do at all.
    2298       13221 :             if (pindexMostWork == NULL || pindexMostWork == chainActive.Tip())
    2299             :                 return true;
    2300             : 
    2301       16458 :             if (!ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL))
    2302             :                 return false;
    2303             : 
    2304        5486 :             pindexNewTip = chainActive.Tip();
    2305        5486 :             fInitialDownload = IsInitialBlockDownload();
    2306             :         }
    2307             :         // When we reach this point, we switched to a new tip (stored in pindexNewTip).
    2308             : 
    2309             :         // Notifications/callbacks that can run without cs_main
    2310        5486 :         if (!fInitialDownload) {
    2311        5110 :             uint256 hashNewTip = pindexNewTip->GetBlockHash();
    2312             :             // Relay inventory, but don't relay old inventory during initial block download.
    2313        5110 :             int nBlockEstimate = 0;
    2314        5110 :             if (fCheckpointsEnabled)
    2315        5110 :                 nBlockEstimate = Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints());
    2316             :             {
    2317        5110 :                 LOCK(cs_vNodes);
    2318      111078 :                 BOOST_FOREACH(CNode* pnode, vNodes)
    2319       13403 :                     if (chainActive.Height() > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate))
    2320       13403 :                         pnode->PushInventory(CInv(MSG_BLOCK, hashNewTip));
    2321             :             }
    2322             :             // Notify external listeners about the new tip.
    2323        5110 :             GetMainSignals().UpdatedBlockTip(pindexNewTip);
    2324        5110 :             uiInterface.NotifyBlockTip(hashNewTip);
    2325             :         }
    2326             :     } while(pindexMostWork != chainActive.Tip());
    2327        4490 :     CheckBlockIndex();
    2328             : 
    2329             :     // Write changes periodically to disk, after relay.
    2330        4490 :     if (!FlushStateToDisk(state, FLUSH_STATE_PERIODIC)) {
    2331             :         return false;
    2332             :     }
    2333             : 
    2334        4490 :     return true;
    2335             : }
    2336             : 
    2337           3 : bool InvalidateBlock(CValidationState& state, CBlockIndex *pindex) {
    2338           3 :     AssertLockHeld(cs_main);
    2339             : 
    2340             :     // Mark the block itself as invalid.
    2341           3 :     pindex->nStatus |= BLOCK_FAILED_VALID;
    2342             :     setDirtyBlockIndex.insert(pindex);
    2343             :     setBlockIndexCandidates.erase(pindex);
    2344             : 
    2345          34 :     while (chainActive.Contains(pindex)) {
    2346          14 :         CBlockIndex *pindexWalk = chainActive.Tip();
    2347          14 :         pindexWalk->nStatus |= BLOCK_FAILED_CHILD;
    2348             :         setDirtyBlockIndex.insert(pindexWalk);
    2349             :         setBlockIndexCandidates.erase(pindexWalk);
    2350             :         // ActivateBestChain considers blocks already in chainActive
    2351             :         // unconditionally valid already, so force disconnect away from it.
    2352          14 :         if (!DisconnectTip(state)) {
    2353           0 :             return false;
    2354             :         }
    2355             :     }
    2356             : 
    2357             :     // The resulting new best tip may not be in setBlockIndexCandidates anymore, so
    2358             :     // add it again.
    2359           3 :     BlockMap::iterator it = mapBlockIndex.begin();
    2360         620 :     while (it != mapBlockIndex.end()) {
    2361        3646 :         if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && !setBlockIndexCandidates.value_comp()(it->second, chainActive.Tip())) {
    2362           3 :             setBlockIndexCandidates.insert(it->second);
    2363             :         }
    2364         617 :         it++;
    2365             :     }
    2366             : 
    2367           3 :     InvalidChainFound(pindex);
    2368             :     return true;
    2369             : }
    2370             : 
    2371           0 : bool ReconsiderBlock(CValidationState& state, CBlockIndex *pindex) {
    2372           0 :     AssertLockHeld(cs_main);
    2373             : 
    2374           0 :     int nHeight = pindex->nHeight;
    2375             : 
    2376             :     // Remove the invalidity flag from this block and all its descendants.
    2377           0 :     BlockMap::iterator it = mapBlockIndex.begin();
    2378           0 :     while (it != mapBlockIndex.end()) {
    2379           0 :         if (!it->second->IsValid() && it->second->GetAncestor(nHeight) == pindex) {
    2380           0 :             it->second->nStatus &= ~BLOCK_FAILED_MASK;
    2381           0 :             setDirtyBlockIndex.insert(it->second);
    2382           0 :             if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && setBlockIndexCandidates.value_comp()(chainActive.Tip(), it->second)) {
    2383           0 :                 setBlockIndexCandidates.insert(it->second);
    2384             :             }
    2385           0 :             if (it->second == pindexBestInvalid) {
    2386             :                 // Reset invalid block marker if it was pointing to one of those.
    2387           0 :                 pindexBestInvalid = NULL;
    2388             :             }
    2389             :         }
    2390           0 :         it++;
    2391             :     }
    2392             : 
    2393             :     // Remove the invalidity flag from all ancestors too.
    2394           0 :     while (pindex != NULL) {
    2395           0 :         if (pindex->nStatus & BLOCK_FAILED_MASK) {
    2396           0 :             pindex->nStatus &= ~BLOCK_FAILED_MASK;
    2397             :             setDirtyBlockIndex.insert(pindex);
    2398             :         }
    2399           0 :         pindex = pindex->pprev;
    2400             :     }
    2401           0 :     return true;
    2402             : }
    2403             : 
    2404        5536 : CBlockIndex* AddToBlockIndex(const CBlockHeader& block)
    2405             : {
    2406             :     // Check for duplicate
    2407        5536 :     uint256 hash = block.GetHash();
    2408        5536 :     BlockMap::iterator it = mapBlockIndex.find(hash);
    2409        5536 :     if (it != mapBlockIndex.end())
    2410           0 :         return it->second;
    2411             : 
    2412             :     // Construct new block index object
    2413        5536 :     CBlockIndex* pindexNew = new CBlockIndex(block);
    2414        5536 :     assert(pindexNew);
    2415             :     // We assign the sequence id to blocks only when the full data is available,
    2416             :     // to avoid miners withholding blocks but broadcasting headers, to get a
    2417             :     // competitive advantage.
    2418        5536 :     pindexNew->nSequenceId = 0;
    2419        5536 :     BlockMap::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
    2420       11072 :     pindexNew->phashBlock = &((*mi).first);
    2421       11072 :     BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock);
    2422        5536 :     if (miPrev != mapBlockIndex.end())
    2423             :     {
    2424       10948 :         pindexNew->pprev = (*miPrev).second;
    2425        5474 :         pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
    2426        5474 :         pindexNew->BuildSkip();
    2427             :     }
    2428       16608 :     pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew);
    2429        5536 :     pindexNew->RaiseValidity(BLOCK_VALID_TREE);
    2430       11010 :     if (pindexBestHeader == NULL || pindexBestHeader->nChainWork < pindexNew->nChainWork)
    2431        5465 :         pindexBestHeader = pindexNew;
    2432             : 
    2433             :     setDirtyBlockIndex.insert(pindexNew);
    2434             : 
    2435        5536 :     return pindexNew;
    2436             : }
    2437             : 
    2438             : /** Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */
    2439        5518 : bool ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBlockIndex *pindexNew, const CDiskBlockPos& pos)
    2440             : {
    2441       11036 :     pindexNew->nTx = block.vtx.size();
    2442        5518 :     pindexNew->nChainTx = 0;
    2443        5518 :     pindexNew->nFile = pos.nFile;
    2444        5518 :     pindexNew->nDataPos = pos.nPos;
    2445        5518 :     pindexNew->nUndoPos = 0;
    2446        5518 :     pindexNew->nStatus |= BLOCK_HAVE_DATA;
    2447        5518 :     pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
    2448             :     setDirtyBlockIndex.insert(pindexNew);
    2449             : 
    2450        5518 :     if (pindexNew->pprev == NULL || pindexNew->pprev->nChainTx) {
    2451             :         // If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
    2452             :         deque<CBlockIndex*> queue;
    2453        4532 :         queue.push_back(pindexNew);
    2454             : 
    2455             :         // Recursively process any descendant blocks that now may be eligible to be connected.
    2456       10047 :         while (!queue.empty()) {
    2457        5515 :             CBlockIndex *pindex = queue.front();
    2458        5515 :             queue.pop_front();
    2459        5515 :             pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
    2460             :             {
    2461        5515 :                 LOCK(cs_nBlockSequenceId);
    2462        5515 :                 pindex->nSequenceId = nBlockSequenceId++;
    2463             :             }
    2464       21936 :             if (chainActive.Tip() == NULL || !setBlockIndexCandidates.value_comp()(pindex, chainActive.Tip())) {
    2465             :                 setBlockIndexCandidates.insert(pindex);
    2466             :             }
    2467        5515 :             std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex);
    2468        6498 :             while (range.first != range.second) {
    2469         983 :                 std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
    2470         983 :                 queue.push_back(it->second);
    2471         983 :                 range.first++;
    2472             :                 mapBlocksUnlinked.erase(it);
    2473             :             }
    2474        4532 :         }
    2475             :     } else {
    2476         986 :         if (pindexNew->pprev && pindexNew->pprev->IsValid(BLOCK_VALID_TREE)) {
    2477        1972 :             mapBlocksUnlinked.insert(std::make_pair(pindexNew->pprev, pindexNew));
    2478             :         }
    2479             :     }
    2480             : 
    2481        5518 :     return true;
    2482             : }
    2483             : 
    2484        5518 : bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
    2485             : {
    2486        5518 :     LOCK(cs_LastBlockFile);
    2487             : 
    2488        5518 :     unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile;
    2489       11036 :     if (vinfoBlockFile.size() <= nFile) {
    2490          26 :         vinfoBlockFile.resize(nFile + 1);
    2491             :     }
    2492             : 
    2493        5518 :     if (!fKnown) {
    2494       11028 :         while (vinfoBlockFile[nFile].nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
    2495           0 :             LogPrintf("Leaving block file %i: %s\n", nFile, vinfoBlockFile[nFile].ToString());
    2496           0 :             FlushBlockFile(true);
    2497           0 :             nFile++;
    2498           0 :             if (vinfoBlockFile.size() <= nFile) {
    2499           0 :                 vinfoBlockFile.resize(nFile + 1);
    2500             :             }
    2501             :         }
    2502        5514 :         pos.nFile = nFile;
    2503        5514 :         pos.nPos = vinfoBlockFile[nFile].nSize;
    2504             :     }
    2505             : 
    2506        5518 :     nLastBlockFile = nFile;
    2507       11036 :     vinfoBlockFile[nFile].AddBlock(nHeight, nTime);
    2508        5518 :     if (fKnown)
    2509          12 :         vinfoBlockFile[nFile].nSize = std::max(pos.nPos + nAddSize, vinfoBlockFile[nFile].nSize);
    2510             :     else
    2511       11028 :         vinfoBlockFile[nFile].nSize += nAddSize;
    2512             : 
    2513        5518 :     if (!fKnown) {
    2514        5514 :         unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
    2515       11028 :         unsigned int nNewChunks = (vinfoBlockFile[nFile].nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
    2516        5514 :         if (nNewChunks > nOldChunks) {
    2517          61 :             if (fPruneMode)
    2518           0 :                 fCheckForPruning = true;
    2519          61 :             if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
    2520          61 :                 FILE *file = OpenBlockFile(pos);
    2521          61 :                 if (file) {
    2522          61 :                     LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
    2523          61 :                     AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
    2524          61 :                     fclose(file);
    2525             :                 }
    2526             :             }
    2527             :             else
    2528           0 :                 return state.Error("out of disk space");
    2529             :         }
    2530             :     }
    2531             : 
    2532       11036 :     setDirtyFileInfo.insert(nFile);
    2533        5518 :     return true;
    2534             : }
    2535             : 
    2536        5432 : bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
    2537             : {
    2538        5432 :     pos.nFile = nFile;
    2539             : 
    2540        5432 :     LOCK(cs_LastBlockFile);
    2541             : 
    2542             :     unsigned int nNewSize;
    2543       10864 :     pos.nPos = vinfoBlockFile[nFile].nUndoSize;
    2544        5432 :     nNewSize = vinfoBlockFile[nFile].nUndoSize += nAddSize;
    2545             :     setDirtyFileInfo.insert(nFile);
    2546             : 
    2547        5432 :     unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
    2548        5432 :     unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
    2549        5432 :     if (nNewChunks > nOldChunks) {
    2550          37 :         if (fPruneMode)
    2551           0 :             fCheckForPruning = true;
    2552          37 :         if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
    2553          37 :             FILE *file = OpenUndoFile(pos);
    2554          37 :             if (file) {
    2555          37 :                 LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
    2556          37 :                 AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
    2557          37 :                 fclose(file);
    2558             :             }
    2559             :         }
    2560             :         else
    2561           0 :             return state.Error("out of disk space");
    2562             :     }
    2563             : 
    2564             :     return true;
    2565             : }
    2566             : 
    2567       25959 : bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, bool fCheckPOW)
    2568             : {
    2569             :     // Check proof of work matches claimed amount
    2570       49212 :     if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, Params().GetConsensus()))
    2571           0 :         return state.DoS(50, error("CheckBlockHeader(): proof of work failed"),
    2572           0 :                          REJECT_INVALID, "high-hash");
    2573             : 
    2574             :     // Check timestamp
    2575       51918 :     if (block.GetBlockTime() > GetAdjustedTime() + 2 * 60 * 60)
    2576           0 :         return state.Invalid(error("CheckBlockHeader(): block timestamp too far in the future"),
    2577           0 :                              REJECT_INVALID, "time-too-new");
    2578             : 
    2579             :     return true;
    2580             : }
    2581             : 
    2582       30259 : bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bool fCheckMerkleRoot)
    2583             : {
    2584             :     // These are checks that are independent of context.
    2585             : 
    2586       30259 :     if (block.fChecked)
    2587             :         return true;
    2588             : 
    2589             :     // Check that the header is valid (particularly PoW).  This is mostly
    2590             :     // redundant with the call in AcceptBlockHeader.
    2591       20485 :     if (!CheckBlockHeader(block, state, fCheckPOW))
    2592             :         return false;
    2593             : 
    2594             :     // Check the merkle root.
    2595       20485 :     if (fCheckMerkleRoot) {
    2596             :         bool mutated;
    2597       17779 :         uint256 hashMerkleRoot2 = block.ComputeMerkleRoot(&mutated);
    2598       35558 :         if (block.hashMerkleRoot != hashMerkleRoot2)
    2599           0 :             return state.DoS(100, error("CheckBlock(): hashMerkleRoot mismatch"),
    2600           0 :                              REJECT_INVALID, "bad-txnmrklroot", true);
    2601             : 
    2602             :         // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
    2603             :         // of transactions in a block without affecting the merkle root of a block,
    2604             :         // while still invalidating it.
    2605       17779 :         if (mutated)
    2606           0 :             return state.DoS(100, error("CheckBlock(): duplicate transaction"),
    2607           0 :                              REJECT_INVALID, "bad-txns-duplicate", true);
    2608             :     }
    2609             : 
    2610             :     // All potential-corruption validation must be done before we do any
    2611             :     // transaction validation, as otherwise we may mark the header as invalid
    2612             :     // because we receive the wrong transactions for it.
    2613             : 
    2614             :     // Size limits
    2615       81940 :     if (block.vtx.empty() || block.vtx.size() > MAX_BLOCK_SIZE || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION) > MAX_BLOCK_SIZE)
    2616           9 :         return state.DoS(100, error("CheckBlock(): size limits failed"),
    2617          63 :                          REJECT_INVALID, "bad-blk-length");
    2618             : 
    2619             :     // First transaction must be coinbase, the rest must not be
    2620       40952 :     if (block.vtx.empty() || !block.vtx[0].IsCoinBase())
    2621           0 :         return state.DoS(100, error("CheckBlock(): first tx is not coinbase"),
    2622           0 :                          REJECT_INVALID, "bad-cb-missing");
    2623       27484 :     for (unsigned int i = 1; i < block.vtx.size(); i++)
    2624        7008 :         if (block.vtx[i].IsCoinBase())
    2625           0 :             return state.DoS(100, error("CheckBlock(): more than one coinbase"),
    2626           0 :                              REJECT_INVALID, "bad-cb-multiple");
    2627             : 
    2628             :     // Check transactions
    2629      246170 :     BOOST_FOREACH(const CTransaction& tx, block.vtx)
    2630       23970 :         if (!CheckTransaction(tx, state))
    2631             :             return error("CheckBlock(): CheckTransaction of %s failed with %s",
    2632             :                 tx.GetHash().ToString(),
    2633          30 :                 FormatStateMessage(state));
    2634             : 
    2635       20466 :     unsigned int nSigOps = 0;
    2636      222130 :     BOOST_FOREACH(const CTransaction& tx, block.vtx)
    2637             :     {
    2638       23960 :         nSigOps += GetLegacySigOpCount(tx);
    2639             :     }
    2640       20466 :     if (nSigOps > MAX_BLOCK_SIGOPS)
    2641          19 :         return state.DoS(100, error("CheckBlock(): out-of-bounds SigOpCount"),
    2642         133 :                          REJECT_INVALID, "bad-blk-sigops", true);
    2643             : 
    2644       20447 :     if (fCheckPOW && fCheckMerkleRoot)
    2645       17741 :         block.fChecked = true;
    2646             : 
    2647             :     return true;
    2648             : }
    2649             : 
    2650        6704 : static bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidationState& state, const CChainParams& chainparams, const uint256& hash)
    2651             : {
    2652       13408 :     if (*pindexPrev->phashBlock == chainparams.GetConsensus().hashGenesisBlock)
    2653             :         return true;
    2654             : 
    2655        6658 :     int nHeight = pindexPrev->nHeight+1;
    2656             :     // Don't accept any forks from the main chain prior to last checkpoint
    2657        6658 :     CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainparams.Checkpoints());
    2658        6658 :     if (pcheckpoint && nHeight < pcheckpoint->nHeight)
    2659           0 :         return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight));
    2660             : 
    2661             :     return true;
    2662             : }
    2663             : 
    2664        6827 : bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex * const pindexPrev)
    2665             : {
    2666       13654 :     const Consensus::Params& consensusParams = Params().GetConsensus();
    2667             :     // Check proof of work
    2668        6827 :     if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
    2669           0 :         return state.DoS(100, error("%s: incorrect proof of work", __func__),
    2670           0 :                          REJECT_INVALID, "bad-diffbits");
    2671             : 
    2672             :     // Check timestamp against prev
    2673       13654 :     if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
    2674           0 :         return state.Invalid(error("%s: block's timestamp is too early", __func__),
    2675           0 :                              REJECT_INVALID, "time-too-old");
    2676             : 
    2677             :     // Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
    2678        7192 :     if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
    2679           0 :         return state.Invalid(error("%s: rejected nVersion=1 block", __func__),
    2680           0 :                              REJECT_OBSOLETE, "bad-version");
    2681             : 
    2682             :     // Reject block.nVersion=2 blocks when 95% (75% on testnet) of the network has upgraded:
    2683        7192 :     if (block.nVersion < 3 && IsSuperMajority(3, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
    2684           0 :         return state.Invalid(error("%s : rejected nVersion=2 block", __func__),
    2685           0 :                              REJECT_OBSOLETE, "bad-version");
    2686             : 
    2687             :     return true;
    2688             : }
    2689             : 
    2690        6810 : bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex * const pindexPrev)
    2691             : {
    2692        6810 :     const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
    2693        6810 :     const Consensus::Params& consensusParams = Params().GetConsensus();
    2694             : 
    2695             :     // Check that all transactions are finalized
    2696       86196 :     BOOST_FOREACH(const CTransaction& tx, block.vtx)
    2697       17382 :         if (!IsFinalTx(tx, nHeight, block.GetBlockTime())) {
    2698           0 :             return state.DoS(10, error("%s: contains a non-final transaction", __func__), REJECT_INVALID, "bad-txns-nonfinal");
    2699             :         }
    2700             : 
    2701             :     // Enforce block.nVersion=2 rule that the coinbase starts with serialized block height
    2702             :     // if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet):
    2703       13262 :     if (block.nVersion >= 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityEnforceBlockUpgrade, consensusParams))
    2704             :     {
    2705           0 :         CScript expect = CScript() << nHeight;
    2706           0 :         if (block.vtx[0].vin[0].scriptSig.size() < expect.size() ||
    2707           0 :             !std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin())) {
    2708           0 :             return state.DoS(100, error("%s: block height mismatch in coinbase", __func__), REJECT_INVALID, "bad-cb-height");
    2709             :         }
    2710             :     }
    2711             : 
    2712             :     return true;
    2713             : }
    2714             : 
    2715      198363 : bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex** ppindex)
    2716             : {
    2717      198363 :     const CChainParams& chainparams = Params();
    2718      198363 :     AssertLockHeld(cs_main);
    2719             :     // Check for duplicate
    2720      198363 :     uint256 hash = block.GetHash();
    2721      198363 :     BlockMap::iterator miSelf = mapBlockIndex.find(hash);
    2722      198363 :     CBlockIndex *pindex = NULL;
    2723      396726 :     if (hash != chainparams.GetConsensus().hashGenesisBlock) {
    2724             : 
    2725      198362 :         if (miSelf != mapBlockIndex.end()) {
    2726             :             // Block header is already known.
    2727      192888 :             pindex = miSelf->second;
    2728      192888 :             if (ppindex)
    2729      192888 :                 *ppindex = pindex;
    2730      192888 :             if (pindex->nStatus & BLOCK_FAILED_MASK)
    2731           6 :                 return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate");
    2732             :             return true;
    2733             :         }
    2734             : 
    2735        5474 :         if (!CheckBlockHeader(block, state))
    2736             :             return false;
    2737             : 
    2738             :         // Get prev block index
    2739        5474 :         CBlockIndex* pindexPrev = NULL;
    2740       10948 :         BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
    2741        5474 :         if (mi == mapBlockIndex.end())
    2742           0 :             return state.DoS(10, error("%s: prev block not found", __func__), 0, "bad-prevblk");
    2743        5474 :         pindexPrev = (*mi).second;
    2744        5474 :         if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
    2745           0 :             return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
    2746             : 
    2747        5474 :         assert(pindexPrev);
    2748        5474 :         if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, hash))
    2749           0 :             return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
    2750             : 
    2751        5474 :         if (!ContextualCheckBlockHeader(block, state, pindexPrev))
    2752             :             return false;
    2753             :     }
    2754             :     if (pindex == NULL)
    2755        5475 :         pindex = AddToBlockIndex(block);
    2756             : 
    2757        5475 :     if (ppindex)
    2758        5475 :         *ppindex = pindex;
    2759             : 
    2760             :     return true;
    2761             : }
    2762             : 
    2763        5458 : bool AcceptBlock(const CBlock& block, CValidationState& state, CBlockIndex** ppindex, bool fRequested, CDiskBlockPos* dbp)
    2764             : {
    2765        5458 :     const CChainParams& chainparams = Params();
    2766        5458 :     AssertLockHeld(cs_main);
    2767             : 
    2768        5458 :     CBlockIndex *&pindex = *ppindex;
    2769             : 
    2770        5458 :     if (!AcceptBlockHeader(block, state, &pindex))
    2771             :         return false;
    2772             : 
    2773             :     // Try to process all requested blocks that we don't have, but only
    2774             :     // process an unrequested block if it's new and has enough work to
    2775             :     // advance our tip, and isn't too many blocks ahead.
    2776        5457 :     bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA;
    2777       16370 :     bool fHasMoreWork = (chainActive.Tip() ? pindex->nChainWork > chainActive.Tip()->nChainWork : true);
    2778             :     // Blocks that are too out-of-order needlessly limit the effectiveness of
    2779             :     // pruning, because pruning will not delete block files that contain any
    2780             :     // blocks which are too close in height to the tip.  Apply this test
    2781             :     // regardless of whether pruning is enabled; it should generally be safe to
    2782             :     // not process unrequested blocks.
    2783       10914 :     bool fTooFarAhead = (pindex->nHeight > int(chainActive.Height() + MIN_BLOCKS_TO_KEEP));
    2784             : 
    2785             :     // TODO: deal better with return value and error conditions for duplicate
    2786             :     // and unrequested blocks.
    2787        5457 :     if (fAlreadyHave) return true;
    2788        5457 :     if (!fRequested) {  // If we didn't ask for it:
    2789           0 :         if (pindex->nTx != 0) return true;  // This is a previously-processed block that was pruned
    2790           0 :         if (!fHasMoreWork) return true;     // Don't process less-work chains
    2791           0 :         if (fTooFarAhead) return true;      // Block height is too high
    2792             :     }
    2793             : 
    2794        5457 :     if ((!CheckBlock(block, state)) || !ContextualCheckBlock(block, state, pindex->pprev)) {
    2795           0 :         if (state.IsInvalid() && !state.CorruptionPossible()) {
    2796           0 :             pindex->nStatus |= BLOCK_FAILED_VALID;
    2797             :             setDirtyBlockIndex.insert(pindex);
    2798             :         }
    2799             :         return false;
    2800             :     }
    2801             : 
    2802        5457 :     int nHeight = pindex->nHeight;
    2803             : 
    2804             :     // Write block to history file
    2805             :     try {
    2806        5457 :         unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
    2807             :         CDiskBlockPos blockPos;
    2808        5457 :         if (dbp != NULL)
    2809           4 :             blockPos = *dbp;
    2810       10914 :         if (!FindBlockPos(state, blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != NULL))
    2811           0 :             return error("AcceptBlock(): FindBlockPos failed");
    2812        5457 :         if (dbp == NULL)
    2813        5453 :             if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
    2814           0 :                 AbortNode(state, "Failed to write block");
    2815        5457 :         if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
    2816           0 :             return error("AcceptBlock(): ReceivedBlockTransactions failed");
    2817           0 :     } catch (const std::runtime_error& e) {
    2818           0 :         return AbortNode(state, std::string("System error: ") + e.what());
    2819             :     }
    2820             : 
    2821        5457 :     if (fCheckForPruning)
    2822           0 :         FlushStateToDisk(state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
    2823             : 
    2824             :     return true;
    2825             : }
    2826             : 
    2827           0 : static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams)
    2828             : {
    2829           0 :     unsigned int nFound = 0;
    2830     1178343 :     for (int i = 0; i < consensusParams.nMajorityWindow && nFound < nRequired && pstart != NULL; i++)
    2831             :     {
    2832     1178343 :         if (pstart->nVersion >= minVersion)
    2833     1118718 :             ++nFound;
    2834     1178343 :         pstart = pstart->pprev;
    2835             :     }
    2836           0 :     return (nFound >= nRequired);
    2837             : }
    2838             : 
    2839             : 
    2840        5496 : bool ProcessNewBlock(CValidationState &state, const CNode* pfrom, const CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp)
    2841             : {
    2842             :     // Preliminary checks
    2843        5496 :     bool checked = CheckBlock(*pblock, state);
    2844             : 
    2845             :     {
    2846        5496 :         LOCK(cs_main);
    2847        5496 :         bool fRequested = MarkBlockAsReceived(pblock->GetHash());
    2848        5496 :         fRequested |= fForceProcessing;
    2849        5496 :         if (!checked) {
    2850          38 :             return error("%s: CheckBlock FAILED", __func__);
    2851             :         }
    2852             : 
    2853             :         // Store to disk
    2854        5458 :         CBlockIndex *pindex = NULL;
    2855        5458 :         bool ret = AcceptBlock(*pblock, state, &pindex, fRequested, dbp);
    2856        5458 :         if (pindex && pfrom) {
    2857        8008 :             mapBlockSource[pindex->GetBlockHash()] = pfrom->GetId();
    2858             :         }
    2859        5458 :         CheckBlockIndex();
    2860        5458 :         if (!ret)
    2861           1 :             return error("%s: AcceptBlock FAILED", __func__);
    2862             :     }
    2863             : 
    2864        5457 :     if (!ActivateBestChain(state, pblock))
    2865           0 :         return error("%s: ActivateBestChain failed", __func__);
    2866             : 
    2867             :     return true;
    2868             : }
    2869             : 
    2870        1353 : bool TestBlockValidity(CValidationState &state, const CBlock& block, CBlockIndex * const pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
    2871             : {
    2872        1353 :     const CChainParams& chainparams = Params();
    2873        1353 :     AssertLockHeld(cs_main);
    2874        2706 :     assert(pindexPrev && pindexPrev == chainActive.Tip());
    2875        1353 :     if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, block.GetHash()))
    2876           0 :         return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
    2877             : 
    2878        1353 :     CCoinsViewCache viewNew(pcoinsTip);
    2879        1353 :     CBlockIndex indexDummy(block);
    2880        1353 :     indexDummy.pprev = pindexPrev;
    2881        1353 :     indexDummy.nHeight = pindexPrev->nHeight + 1;
    2882             : 
    2883             :     // NOTE: CheckBlockHeader is called by CheckBlock
    2884        1353 :     if (!ContextualCheckBlockHeader(block, state, pindexPrev))
    2885             :         return false;
    2886        1353 :     if (!CheckBlock(block, state, fCheckPOW, fCheckMerkleRoot))
    2887             :         return false;
    2888        1353 :     if (!ContextualCheckBlock(block, state, pindexPrev))
    2889             :         return false;
    2890        1353 :     if (!ConnectBlock(block, state, &indexDummy, viewNew, true))
    2891             :         return false;
    2892        1353 :     assert(state.IsValid());
    2893             : 
    2894        1353 :     return true;
    2895             : }
    2896             : 
    2897             : /**
    2898             :  * BLOCK PRUNING CODE
    2899             :  */
    2900             : 
    2901             : /* Calculate the amount of disk space the block & undo files currently use */
    2902           0 : uint64_t CalculateCurrentUsage()
    2903             : {
    2904           0 :     uint64_t retval = 0;
    2905           0 :     BOOST_FOREACH(const CBlockFileInfo &file, vinfoBlockFile) {
    2906           0 :         retval += file.nSize + file.nUndoSize;
    2907             :     }
    2908           0 :     return retval;
    2909             : }
    2910             : 
    2911             : /* Prune a block file (modify associated database entries)*/
    2912           0 : void PruneOneBlockFile(const int fileNumber)
    2913             : {
    2914           0 :     for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) {
    2915           0 :         CBlockIndex* pindex = it->second;
    2916           0 :         if (pindex->nFile == fileNumber) {
    2917           0 :             pindex->nStatus &= ~BLOCK_HAVE_DATA;
    2918           0 :             pindex->nStatus &= ~BLOCK_HAVE_UNDO;
    2919           0 :             pindex->nFile = 0;
    2920           0 :             pindex->nDataPos = 0;
    2921           0 :             pindex->nUndoPos = 0;
    2922             :             setDirtyBlockIndex.insert(pindex);
    2923             : 
    2924             :             // Prune from mapBlocksUnlinked -- any block we prune would have
    2925             :             // to be downloaded again in order to consider its chain, at which
    2926             :             // point it would be considered as a candidate for
    2927             :             // mapBlocksUnlinked or setBlockIndexCandidates.
    2928           0 :             std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex->pprev);
    2929           0 :             while (range.first != range.second) {
    2930           0 :                 std::multimap<CBlockIndex *, CBlockIndex *>::iterator it = range.first;
    2931           0 :                 range.first++;
    2932           0 :                 if (it->second == pindex) {
    2933             :                     mapBlocksUnlinked.erase(it);
    2934             :                 }
    2935             :             }
    2936             :         }
    2937             :     }
    2938             : 
    2939           0 :     vinfoBlockFile[fileNumber].SetNull();
    2940             :     setDirtyFileInfo.insert(fileNumber);
    2941           0 : }
    2942             : 
    2943             : 
    2944           0 : void UnlinkPrunedFiles(std::set<int>& setFilesToPrune)
    2945             : {
    2946           0 :     for (set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
    2947           0 :         CDiskBlockPos pos(*it, 0);
    2948           0 :         boost::filesystem::remove(GetBlockPosFilename(pos, "blk"));
    2949           0 :         boost::filesystem::remove(GetBlockPosFilename(pos, "rev"));
    2950           0 :         LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
    2951             :     }
    2952           0 : }
    2953             : 
    2954             : /* Calculate the block/rev files that should be deleted to remain under target*/
    2955           0 : void FindFilesToPrune(std::set<int>& setFilesToPrune)
    2956             : {
    2957           0 :     LOCK2(cs_main, cs_LastBlockFile);
    2958           0 :     if (chainActive.Tip() == NULL || nPruneTarget == 0) {
    2959             :         return;
    2960             :     }
    2961           0 :     if (chainActive.Tip()->nHeight <= Params().PruneAfterHeight()) {
    2962             :         return;
    2963             :     }
    2964             : 
    2965           0 :     unsigned int nLastBlockWeCanPrune = chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP;
    2966           0 :     uint64_t nCurrentUsage = CalculateCurrentUsage();
    2967             :     // We don't check to prune until after we've allocated new space for files
    2968             :     // So we should leave a buffer under our target to account for another allocation
    2969             :     // before the next pruning.
    2970           0 :     uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
    2971             :     uint64_t nBytesToPrune;
    2972           0 :     int count=0;
    2973             : 
    2974           0 :     if (nCurrentUsage + nBuffer >= nPruneTarget) {
    2975           0 :         for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
    2976           0 :             nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize;
    2977             : 
    2978           0 :             if (vinfoBlockFile[fileNumber].nSize == 0)
    2979             :                 continue;
    2980             : 
    2981           0 :             if (nCurrentUsage + nBuffer < nPruneTarget)  // are we below our target?
    2982             :                 break;
    2983             : 
    2984             :             // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
    2985           0 :             if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
    2986             :                 continue;
    2987             : 
    2988           0 :             PruneOneBlockFile(fileNumber);
    2989             :             // Queue up the files for removal
    2990             :             setFilesToPrune.insert(fileNumber);
    2991           0 :             nCurrentUsage -= nBytesToPrune;
    2992           0 :             count++;
    2993             :         }
    2994             :     }
    2995             : 
    2996             :     LogPrint("prune", "Prune: target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n",
    2997           0 :            nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
    2998           0 :            ((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
    2999           0 :            nLastBlockWeCanPrune, count);
    3000             : }
    3001             : 
    3002         502 : bool CheckDiskSpace(uint64_t nAdditionalBytes)
    3003             : {
    3004        1004 :     uint64_t nFreeBytesAvailable = boost::filesystem::space(GetDataDir()).available;
    3005             : 
    3006             :     // Check for nMinDiskSpace bytes (currently 50MB)
    3007         502 :     if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
    3008           0 :         return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
    3009             : 
    3010             :     return true;
    3011             : }
    3012             : 
    3013       58006 : FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
    3014             : {
    3015       58006 :     if (pos.IsNull())
    3016             :         return NULL;
    3017       58006 :     boost::filesystem::path path = GetBlockPosFilename(pos, prefix);
    3018      116012 :     boost::filesystem::create_directories(path.parent_path());
    3019      116012 :     FILE* file = fopen(path.string().c_str(), "rb+");
    3020       58006 :     if (!file && !fReadOnly)
    3021         244 :         file = fopen(path.string().c_str(), "wb+");
    3022       58006 :     if (!file) {
    3023           0 :         LogPrintf("Unable to open file %s\n", path.string());
    3024             :         return NULL;
    3025             :     }
    3026       58006 :     if (pos.nPos) {
    3027       57442 :         if (fseek(file, pos.nPos, SEEK_SET)) {
    3028           0 :             LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
    3029           0 :             fclose(file);
    3030             :             return NULL;
    3031             :         }
    3032             :     }
    3033       58006 :     return file;
    3034             : }
    3035             : 
    3036           1 : FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
    3037       30166 :     return OpenDiskFile(pos, "blk", fReadOnly);
    3038             : }
    3039             : 
    3040           0 : FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
    3041       27840 :     return OpenDiskFile(pos, "rev", fReadOnly);
    3042             : }
    3043             : 
    3044       58008 : boost::filesystem::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
    3045             : {
    3046      348048 :     return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
    3047             : }
    3048             : 
    3049       22278 : CBlockIndex * InsertBlockIndex(uint256 hash)
    3050             : {
    3051       22278 :     if (hash.IsNull())
    3052             :         return NULL;
    3053             : 
    3054             :     // Return existing
    3055       22221 :     BlockMap::iterator mi = mapBlockIndex.find(hash);
    3056       22221 :     if (mi != mapBlockIndex.end())
    3057       11082 :         return (*mi).second;
    3058             : 
    3059             :     // Create new
    3060       11139 :     CBlockIndex* pindexNew = new CBlockIndex();
    3061       11139 :     if (!pindexNew)
    3062           0 :         throw runtime_error("LoadBlockIndex(): new CBlockIndex failed");
    3063       11139 :     mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
    3064       11139 :     pindexNew->phashBlock = &((*mi).first);
    3065             : 
    3066       11139 :     return pindexNew;
    3067             : }
    3068             : 
    3069          93 : bool static LoadBlockIndexDB()
    3070             : {
    3071          93 :     const CChainParams& chainparams = Params();
    3072          93 :     if (!pblocktree->LoadBlockIndexGuts())
    3073             :         return false;
    3074             : 
    3075          93 :     boost::this_thread::interruption_point();
    3076             : 
    3077             :     // Calculate nChainWork
    3078             :     vector<pair<int, CBlockIndex*> > vSortedByHeight;
    3079          93 :     vSortedByHeight.reserve(mapBlockIndex.size());
    3080       67392 :     BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
    3081             :     {
    3082       11139 :         CBlockIndex* pindex = item.second;
    3083       11139 :         vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
    3084             :     }
    3085             :     sort(vSortedByHeight.begin(), vSortedByHeight.end());
    3086       56253 :     BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
    3087             :     {
    3088       11139 :         CBlockIndex* pindex = item.second;
    3089       33417 :         pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
    3090             :         // We can link the chain of blocks for which we've received transactions at some point.
    3091             :         // Pruned nodes may have deleted the block.
    3092       11139 :         if (pindex->nTx > 0) {
    3093       11139 :             if (pindex->pprev) {
    3094       11082 :                 if (pindex->pprev->nChainTx) {
    3095       11082 :                     pindex->nChainTx = pindex->pprev->nChainTx + pindex->nTx;
    3096             :                 } else {
    3097           0 :                     pindex->nChainTx = 0;
    3098           0 :                     mapBlocksUnlinked.insert(std::make_pair(pindex->pprev, pindex));
    3099             :                 }
    3100             :             } else {
    3101          57 :                 pindex->nChainTx = pindex->nTx;
    3102             :             }
    3103             :         }
    3104       11139 :         if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->nChainTx || pindex->pprev == NULL))
    3105             :             setBlockIndexCandidates.insert(pindex);
    3106       11139 :         if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
    3107           0 :             pindexBestInvalid = pindex;
    3108       11139 :         if (pindex->pprev)
    3109       11082 :             pindex->BuildSkip();
    3110       11139 :         if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == NULL || CBlockIndexWorkComparator()(pindexBestHeader, pindex)))
    3111       11139 :             pindexBestHeader = pindex;
    3112             :     }
    3113             : 
    3114             :     // Load block file info
    3115          93 :     pblocktree->ReadLastBlockFile(nLastBlockFile);
    3116          93 :     vinfoBlockFile.resize(nLastBlockFile + 1);
    3117          93 :     LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile);
    3118          93 :     for (int nFile = 0; nFile <= nLastBlockFile; nFile++) {
    3119         186 :         pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
    3120             :     }
    3121         279 :     LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString());
    3122          93 :     for (int nFile = nLastBlockFile + 1; true; nFile++) {
    3123             :         CBlockFileInfo info;
    3124          93 :         if (pblocktree->ReadBlockFileInfo(nFile, info)) {
    3125           0 :             vinfoBlockFile.push_back(info);
    3126             :         } else {
    3127             :             break;
    3128             :         }
    3129           0 :     }
    3130             : 
    3131             :     // Check presence of blk files
    3132          93 :     LogPrintf("Checking all blk files are present...\n");
    3133             :     set<int> setBlkDataFiles;
    3134       78531 :     BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
    3135             :     {
    3136       11139 :         CBlockIndex* pindex = item.second;
    3137       11139 :         if (pindex->nStatus & BLOCK_HAVE_DATA) {
    3138       11139 :             setBlkDataFiles.insert(pindex->nFile);
    3139             :         }
    3140             :     }
    3141         300 :     for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++)
    3142             :     {
    3143          57 :         CDiskBlockPos pos(*it, 0);
    3144         171 :         if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) {
    3145           0 :             return false;
    3146             :         }
    3147             :     }
    3148             : 
    3149             :     // Check whether we have ever pruned block & undo files
    3150         279 :     pblocktree->ReadFlag("prunedblockfiles", fHavePruned);
    3151          93 :     if (fHavePruned)
    3152           0 :         LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n");
    3153             : 
    3154             :     // Check whether we need to continue reindexing
    3155          93 :     bool fReindexing = false;
    3156          93 :     pblocktree->ReadReindexing(fReindexing);
    3157          93 :     fReindex |= fReindexing;
    3158             : 
    3159             :     // Check whether we have a transaction index
    3160         279 :     pblocktree->ReadFlag("txindex", fTxIndex);
    3161          93 :     LogPrintf("%s: transaction index %s\n", __func__, fTxIndex ? "enabled" : "disabled");
    3162             : 
    3163             :     // Load pointer to end of best chain
    3164         186 :     BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
    3165          93 :     if (it == mapBlockIndex.end())
    3166             :         return true;
    3167          57 :     chainActive.SetTip(it->second);
    3168             : 
    3169          57 :     PruneBlockIndexCandidates();
    3170             : 
    3171         513 :     LogPrintf("%s: hashBestChain=%s height=%d date=%s progress=%f\n", __func__,
    3172             :         chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
    3173             :         DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
    3174          57 :         Checkpoints::GuessVerificationProgress(chainparams.Checkpoints(), chainActive.Tip()));
    3175             : 
    3176          57 :     return true;
    3177             : }
    3178             : 
    3179          94 : CVerifyDB::CVerifyDB()
    3180             : {
    3181         188 :     uiInterface.ShowProgress(_("Verifying blocks..."), 0);
    3182          94 : }
    3183             : 
    3184          94 : CVerifyDB::~CVerifyDB()
    3185             : {
    3186         282 :     uiInterface.ShowProgress("", 100);
    3187          94 : }
    3188             : 
    3189          94 : bool CVerifyDB::VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
    3190             : {
    3191          94 :     LOCK(cs_main);
    3192         187 :     if (chainActive.Tip() == NULL || chainActive.Tip()->pprev == NULL)
    3193             :         return true;
    3194             : 
    3195             :     // Verify blocks in the best chain
    3196          57 :     if (nCheckDepth <= 0)
    3197           0 :         nCheckDepth = 1000000000; // suffices until the year 19000
    3198          57 :     if (nCheckDepth > chainActive.Height())
    3199          57 :         nCheckDepth = chainActive.Height();
    3200         171 :     nCheckLevel = std::max(0, std::min(4, nCheckLevel));
    3201          57 :     LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
    3202         114 :     CCoinsViewCache coins(coinsview);
    3203          57 :     CBlockIndex* pindexState = chainActive.Tip();
    3204          57 :     CBlockIndex* pindexFailure = NULL;
    3205          57 :     int nGoodTransactions = 0;
    3206          57 :     CValidationState state;
    3207       11139 :     for (CBlockIndex* pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev)
    3208             :     {
    3209       11082 :         boost::this_thread::interruption_point();
    3210       44328 :         uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100)))));
    3211       22164 :         if (pindex->nHeight < chainActive.Height()-nCheckDepth)
    3212             :             break;
    3213       11082 :         CBlock block;
    3214             :         // check level 0: read from disk
    3215       11082 :         if (!ReadBlockFromDisk(block, pindex))
    3216           0 :             return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
    3217             :         // check level 1: verify block validity
    3218       11082 :         if (nCheckLevel >= 1 && !CheckBlock(block, state))
    3219           0 :             return error("VerifyDB(): *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
    3220             :         // check level 2: verify undo validity
    3221       11082 :         if (nCheckLevel >= 2 && pindex) {
    3222             :             CBlockUndo undo;
    3223       22164 :             CDiskBlockPos pos = pindex->GetUndoPos();
    3224       11082 :             if (!pos.IsNull()) {
    3225       22164 :                 if (!UndoReadFromDisk(undo, pos, pindex->pprev->GetBlockHash()))
    3226           0 :                     return error("VerifyDB(): *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
    3227             :             }
    3228             :         }
    3229             :         // check level 3: check for inconsistencies during memory-only disconnect of tip blocks
    3230       11082 :         if (nCheckLevel >= 3 && pindex == pindexState && (coins.DynamicMemoryUsage() + pcoinsTip->DynamicMemoryUsage()) <= nCoinCacheUsage) {
    3231       11082 :             bool fClean = true;
    3232       11082 :             if (!DisconnectBlock(block, state, pindex, coins, &fClean))
    3233           0 :                 return error("VerifyDB(): *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
    3234       11082 :             pindexState = pindex->pprev;
    3235       11082 :             if (!fClean) {
    3236           0 :                 nGoodTransactions = 0;
    3237           0 :                 pindexFailure = pindex;
    3238             :             } else
    3239       22164 :                 nGoodTransactions += block.vtx.size();
    3240             :         }
    3241       11082 :         if (ShutdownRequested())
    3242             :             return true;
    3243             :     }
    3244          57 :     if (pindexFailure)
    3245           0 :         return error("VerifyDB(): *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainActive.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
    3246             : 
    3247             :     // check level 4: try reconnecting blocks
    3248          57 :     if (nCheckLevel >= 4) {
    3249           0 :         CBlockIndex *pindex = pindexState;
    3250           0 :         while (pindex != chainActive.Tip()) {
    3251           0 :             boost::this_thread::interruption_point();
    3252           0 :             uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, 100 - (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * 50))));
    3253           0 :             pindex = chainActive.Next(pindex);
    3254           0 :             CBlock block;
    3255           0 :             if (!ReadBlockFromDisk(block, pindex))
    3256           0 :                 return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
    3257           0 :             if (!ConnectBlock(block, state, pindex, coins))
    3258           0 :                 return error("VerifyDB(): *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
    3259             :         }
    3260             :     }
    3261             : 
    3262          57 :     LogPrintf("No coin database inconsistencies in last %i blocks (%i transactions)\n", chainActive.Height() - pindexState->nHeight, nGoodTransactions);
    3263             : 
    3264          57 :     return true;
    3265             : }
    3266             : 
    3267         119 : void UnloadBlockIndex()
    3268             : {
    3269         119 :     LOCK(cs_main);
    3270             :     setBlockIndexCandidates.clear();
    3271         119 :     chainActive.SetTip(NULL);
    3272         119 :     pindexBestInvalid = NULL;
    3273         119 :     pindexBestHeader = NULL;
    3274         119 :     mempool.clear();
    3275             :     mapOrphanTransactions.clear();
    3276             :     mapOrphanTransactionsByPrev.clear();
    3277         119 :     nSyncStarted = 0;
    3278             :     mapBlocksUnlinked.clear();
    3279             :     vinfoBlockFile.clear();
    3280         119 :     nLastBlockFile = 0;
    3281         119 :     nBlockSequenceId = 1;
    3282             :     mapBlockSource.clear();
    3283             :     mapBlocksInFlight.clear();
    3284         119 :     nQueuedValidatedHeaders = 0;
    3285         119 :     nPreferredDownload = 0;
    3286             :     setDirtyBlockIndex.clear();
    3287             :     setDirtyFileInfo.clear();
    3288             :     mapNodeState.clear();
    3289         119 :     recentRejects.reset(NULL);
    3290             : 
    3291        1904 :     BOOST_FOREACH(BlockMap::value_type& entry, mapBlockIndex) {
    3292         238 :         delete entry.second;
    3293             :     }
    3294             :     mapBlockIndex.clear();
    3295         119 :     fHavePruned = false;
    3296         119 : }
    3297             : 
    3298          94 : bool LoadBlockIndex()
    3299             : {
    3300             :     // Load block index from databases
    3301          94 :     if (!fReindex && !LoadBlockIndexDB())
    3302             :         return false;
    3303          94 :     return true;
    3304             : }
    3305             : 
    3306             : 
    3307         120 : bool InitBlockIndex() {
    3308         120 :     const CChainParams& chainparams = Params();
    3309         120 :     LOCK(cs_main);
    3310             : 
    3311             :     // Initialize global variables that cannot be constructed at startup.
    3312         120 :     recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
    3313             : 
    3314             :     // Check whether we're already initialized
    3315         120 :     if (chainActive.Genesis() != NULL)
    3316             :         return true;
    3317             : 
    3318             :     // Use the provided setting for -txindex in the new database
    3319         186 :     fTxIndex = GetBoolArg("-txindex", false);
    3320         186 :     pblocktree->WriteFlag("txindex", fTxIndex);
    3321          62 :     LogPrintf("Initializing databases...\n");
    3322             : 
    3323             :     // Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
    3324          62 :     if (!fReindex) {
    3325             :         try {
    3326         122 :             CBlock &block = const_cast<CBlock&>(Params().GenesisBlock());
    3327             :             // Start new block file
    3328          61 :             unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
    3329             :             CDiskBlockPos blockPos;
    3330             :             CValidationState state;
    3331         122 :             if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.GetBlockTime()))
    3332           0 :                 return error("LoadBlockIndex(): FindBlockPos failed");
    3333          61 :             if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
    3334           0 :                 return error("LoadBlockIndex(): writing genesis block to disk failed");
    3335          61 :             CBlockIndex *pindex = AddToBlockIndex(block);
    3336          61 :             if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
    3337           0 :                 return error("LoadBlockIndex(): genesis block not accepted");
    3338          61 :             if (!ActivateBestChain(state, &block))
    3339           0 :                 return error("LoadBlockIndex(): genesis block cannot be activated");
    3340             :             // Force a chainstate write so that when we VerifyDB in a moment, it doesn't check stale data
    3341          61 :             return FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
    3342           0 :         } catch (const std::runtime_error& e) {
    3343           0 :             return error("LoadBlockIndex(): failed to initialize block database: %s", e.what());
    3344             :         }
    3345             :     }
    3346             : 
    3347             :     return true;
    3348             : }
    3349             : 
    3350             : 
    3351             : 
    3352           1 : bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
    3353             : {
    3354           1 :     const CChainParams& chainparams = Params();
    3355             :     // Map of disk positions for blocks with unknown parent (only used for reindex)
    3356           2 :     static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
    3357           1 :     int64_t nStart = GetTimeMillis();
    3358             : 
    3359           1 :     int nLoaded = 0;
    3360             :     try {
    3361             :         // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
    3362             :         CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SIZE, MAX_BLOCK_SIZE+8, SER_DISK, CLIENT_VERSION);
    3363           1 :         uint64_t nRewind = blkdat.GetPos();
    3364           6 :         while (!blkdat.eof()) {
    3365           5 :             boost::this_thread::interruption_point();
    3366             : 
    3367             :             blkdat.SetPos(nRewind);
    3368           5 :             nRewind++; // start one byte further next time, in case of failure
    3369           5 :             blkdat.SetLimit(); // remove former limit
    3370           5 :             unsigned int nSize = 0;
    3371             :             try {
    3372             :                 // locate a header
    3373             :                 unsigned char buf[MESSAGE_START_SIZE];
    3374           5 :                 blkdat.FindByte(Params().MessageStart()[0]);
    3375           4 :                 nRewind = blkdat.GetPos()+1;
    3376           4 :                 blkdat >> FLATDATA(buf);
    3377           8 :                 if (memcmp(buf, Params().MessageStart(), MESSAGE_START_SIZE))
    3378           0 :                     continue;
    3379             :                 // read size
    3380             :                 blkdat >> nSize;
    3381           4 :                 if (nSize < 80 || nSize > MAX_BLOCK_SIZE)
    3382             :                     continue;
    3383           1 :             } catch (const std::exception&) {
    3384             :                 // no valid block header found; don't complain
    3385             :                 break;
    3386             :             }
    3387             :             try {
    3388             :                 // read block
    3389           4 :                 uint64_t nBlockPos = blkdat.GetPos();
    3390           4 :                 if (dbp)
    3391           4 :                     dbp->nPos = nBlockPos;
    3392           4 :                 blkdat.SetLimit(nBlockPos + nSize);
    3393             :                 blkdat.SetPos(nBlockPos);
    3394           4 :                 CBlock block;
    3395           4 :                 blkdat >> block;
    3396           4 :                 nRewind = blkdat.GetPos();
    3397             : 
    3398             :                 // detect out of order blocks, and store them for later
    3399           4 :                 uint256 hash = block.GetHash();
    3400          11 :                 if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex.find(block.hashPrevBlock) == mapBlockIndex.end()) {
    3401             :                     LogPrint("reindex", "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
    3402           0 :                             block.hashPrevBlock.ToString());
    3403           0 :                     if (dbp)
    3404           0 :                         mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
    3405             :                     continue;
    3406             :                 }
    3407             : 
    3408             :                 // process in case the block isn't known yet
    3409           4 :                 if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
    3410             :                     CValidationState state;
    3411           4 :                     if (ProcessNewBlock(state, NULL, &block, true, dbp))
    3412           4 :                         nLoaded++;
    3413           4 :                     if (state.IsError())
    3414           4 :                         break;
    3415           0 :                 } else if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex[hash]->nHeight % 1000 == 0) {
    3416           0 :                     LogPrintf("Block Import: already had block %s at height %d\n", hash.ToString(), mapBlockIndex[hash]->nHeight);
    3417             :                 }
    3418             : 
    3419             :                 // Recursively process earlier encountered successors of this block
    3420             :                 deque<uint256> queue;
    3421           4 :                 queue.push_back(hash);
    3422           8 :                 while (!queue.empty()) {
    3423           4 :                     uint256 head = queue.front();
    3424           4 :                     queue.pop_front();
    3425           4 :                     std::pair<std::multimap<uint256, CDiskBlockPos>::iterator, std::multimap<uint256, CDiskBlockPos>::iterator> range = mapBlocksUnknownParent.equal_range(head);
    3426           4 :                     while (range.first != range.second) {
    3427           0 :                         std::multimap<uint256, CDiskBlockPos>::iterator it = range.first;
    3428           0 :                         if (ReadBlockFromDisk(block, it->second))
    3429             :                         {
    3430           0 :                             LogPrintf("%s: Processing out of order child %s of %s\n", __func__, block.GetHash().ToString(),
    3431           0 :                                     head.ToString());
    3432             :                             CValidationState dummy;
    3433           0 :                             if (ProcessNewBlock(dummy, NULL, &block, true, &it->second))
    3434             :                             {
    3435           0 :                                 nLoaded++;
    3436           0 :                                 queue.push_back(block.GetHash());
    3437           0 :                             }
    3438             :                         }
    3439           0 :                         range.first++;
    3440             :                         mapBlocksUnknownParent.erase(it);
    3441             :                     }
    3442             :                 }
    3443           0 :             } catch (const std::exception& e) {
    3444           0 :                 LogPrintf("%s: Deserialize or I/O error - %s\n", __func__, e.what());
    3445             :             }
    3446           1 :         }
    3447           0 :     } catch (const std::runtime_error& e) {
    3448           0 :         AbortNode(std::string("System error: ") + e.what());
    3449             :     }
    3450           1 :     if (nLoaded > 0)
    3451           1 :         LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded, GetTimeMillis() - nStart);
    3452           1 :     return nLoaded > 0;
    3453             : }
    3454             : 
    3455       14579 : void static CheckBlockIndex()
    3456             : {
    3457       14579 :     const Consensus::Params& consensusParams = Params().GetConsensus();
    3458       14579 :     if (!fCheckBlockIndex) {
    3459           1 :         return;
    3460             :     }
    3461             : 
    3462       14579 :     LOCK(cs_main);
    3463             : 
    3464             :     // During a reindex, we read the genesis block and call CheckBlockIndex before ActivateBestChain,
    3465             :     // so we have the genesis block in mapBlockIndex but no active chain.  (A few of the tests when
    3466             :     // iterating the block tree require that chainActive has been initialized.)
    3467       14579 :     if (chainActive.Height() < 0) {
    3468           1 :         assert(mapBlockIndex.size() <= 1);
    3469             :         return;
    3470             :     }
    3471             : 
    3472             :     // Build forward-pointing map of the entire block tree.
    3473             :     std::multimap<CBlockIndex*,CBlockIndex*> forward;
    3474     3217128 :     for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
    3475     4781958 :         forward.insert(std::make_pair(it->second->pprev, it->second));
    3476             :     }
    3477             : 
    3478       14578 :     assert(forward.size() == mapBlockIndex.size());
    3479             : 
    3480       29156 :     std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeGenesis = forward.equal_range(NULL);
    3481       14578 :     CBlockIndex *pindex = rangeGenesis.first->second;
    3482       14578 :     rangeGenesis.first++;
    3483       14578 :     assert(rangeGenesis.first == rangeGenesis.second); // There is only one index entry with parent NULL.
    3484             : 
    3485             :     // Iterate over the entire block tree, using depth-first search.
    3486             :     // Along the way, remember whether there are blocks on the path from genesis
    3487             :     // block being explored which are the first to have certain properties.
    3488             :     size_t nNodes = 0;
    3489             :     int nHeight = 0;
    3490             :     CBlockIndex* pindexFirstInvalid = NULL; // Oldest ancestor of pindex which is invalid.
    3491             :     CBlockIndex* pindexFirstMissing = NULL; // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA.
    3492             :     CBlockIndex* pindexFirstNeverProcessed = NULL; // Oldest ancestor of pindex for which nTx == 0.
    3493             :     CBlockIndex* pindexFirstNotTreeValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
    3494             :     CBlockIndex* pindexFirstNotTransactionsValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not).
    3495             :     CBlockIndex* pindexFirstNotChainValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not).
    3496             :     CBlockIndex* pindexFirstNotScriptsValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not).
    3497     1608564 :     while (pindex != NULL) {
    3498     1593986 :         nNodes++;
    3499     1593986 :         if (pindexFirstInvalid == NULL && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
    3500     1593986 :         if (pindexFirstMissing == NULL && !(pindex->nStatus & BLOCK_HAVE_DATA)) pindexFirstMissing = pindex;
    3501     1593986 :         if (pindexFirstNeverProcessed == NULL && pindex->nTx == 0) pindexFirstNeverProcessed = pindex;
    3502     1593986 :         if (pindex->pprev != NULL && pindexFirstNotTreeValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TREE) pindexFirstNotTreeValid = pindex;
    3503     1593986 :         if (pindex->pprev != NULL && pindexFirstNotTransactionsValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TRANSACTIONS) pindexFirstNotTransactionsValid = pindex;
    3504     1593986 :         if (pindex->pprev != NULL && pindexFirstNotChainValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_CHAIN) pindexFirstNotChainValid = pindex;
    3505     1593986 :         if (pindex->pprev != NULL && pindexFirstNotScriptsValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS) pindexFirstNotScriptsValid = pindex;
    3506             : 
    3507             :         // Begin: actual consistency checks.
    3508     1593986 :         if (pindex->pprev == NULL) {
    3509             :             // Genesis block checks.
    3510       29156 :             assert(pindex->GetBlockHash() == consensusParams.hashGenesisBlock); // Genesis block's hash must match.
    3511       14578 :             assert(pindex == chainActive.Genesis()); // The current active chain's genesis block must be this block.
    3512             :         }
    3513     1593986 :         if (pindex->nChainTx == 0) assert(pindex->nSequenceId == 0);  // nSequenceId can't be set for blocks that aren't linked
    3514             :         // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred).
    3515             :         // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
    3516     1593986 :         if (!fHavePruned) {
    3517             :             // If we've never pruned, then HAVE_DATA should be equivalent to nTx > 0
    3518     1593986 :             assert(!(pindex->nStatus & BLOCK_HAVE_DATA) == (pindex->nTx == 0));
    3519     1593986 :             assert(pindexFirstMissing == pindexFirstNeverProcessed);
    3520             :         } else {
    3521             :             // If we have pruned, then we can only say that HAVE_DATA implies nTx > 0
    3522           0 :             if (pindex->nStatus & BLOCK_HAVE_DATA) assert(pindex->nTx > 0);
    3523             :         }
    3524     1593986 :         if (pindex->nStatus & BLOCK_HAVE_UNDO) assert(pindex->nStatus & BLOCK_HAVE_DATA);
    3525     1593986 :         assert(((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS) == (pindex->nTx > 0)); // This is pruning-independent.
    3526             :         // All parents having had data (at some point) is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to nChainTx being set.
    3527     1593986 :         assert((pindexFirstNeverProcessed != NULL) == (pindex->nChainTx == 0)); // nChainTx != 0 is used to signal that all parent blocks have been processed (but may have been pruned).
    3528     1593986 :         assert((pindexFirstNotTransactionsValid != NULL) == (pindex->nChainTx == 0));
    3529     1593986 :         assert(pindex->nHeight == nHeight); // nHeight must be consistent.
    3530     3173394 :         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.
    3531     1593986 :         assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); // The pskip pointer must point back for all but the first 2 blocks.
    3532     1593986 :         assert(pindexFirstNotTreeValid == NULL); // All mapBlockIndex entries must at least be TREE valid
    3533     1593986 :         if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TREE) assert(pindexFirstNotTreeValid == NULL); // TREE valid implies all parents are TREE valid
    3534     1593986 :         if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_CHAIN) assert(pindexFirstNotChainValid == NULL); // CHAIN valid implies all parents are CHAIN valid
    3535     1593986 :         if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_SCRIPTS) assert(pindexFirstNotScriptsValid == NULL); // SCRIPTS valid implies all parents are SCRIPTS valid
    3536     1593986 :         if (pindexFirstInvalid == NULL) {
    3537             :             // Checks for not-invalid blocks.
    3538     1593403 :             assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
    3539             :         }
    3540     3187972 :         if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && pindexFirstNeverProcessed == NULL) {
    3541       20185 :             if (pindexFirstInvalid == NULL) {
    3542             :                 // If this block sorts at least as good as the current tip and
    3543             :                 // is valid and we have all data for its parents, it must be in
    3544             :                 // setBlockIndexCandidates.  chainActive.Tip() must also be there
    3545             :                 // even if some data has been pruned.
    3546       19996 :                 if (pindexFirstMissing == NULL || pindex == chainActive.Tip()) {
    3547       19996 :                     assert(setBlockIndexCandidates.count(pindex));
    3548             :                 }
    3549             :                 // If some parent is missing, then it could be that this block was in
    3550             :                 // setBlockIndexCandidates but had to be removed because of the missing data.
    3551             :                 // In this case it must be in mapBlocksUnlinked -- see test below.
    3552             :             }
    3553             :         } else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates.
    3554     1573801 :             assert(setBlockIndexCandidates.count(pindex) == 0);
    3555             :         }
    3556             :         // Check whether this block is in mapBlocksUnlinked.
    3557     3187972 :         std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked = mapBlocksUnlinked.equal_range(pindex->pprev);
    3558     1593986 :         bool foundInUnlinked = false;
    3559     3187972 :         while (rangeUnlinked.first != rangeUnlinked.second) {
    3560       44362 :             assert(rangeUnlinked.first->first == pindex->pprev);
    3561       44362 :             if (rangeUnlinked.first->second == pindex) {
    3562             :                 foundInUnlinked = true;
    3563             :                 break;
    3564             :             }
    3565           0 :             rangeUnlinked.first++;
    3566             :         }
    3567     1593986 :         if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed != NULL && pindexFirstInvalid == NULL) {
    3568             :             // If this block has block data available, some parent was never received, and has no invalid parents, it must be in mapBlocksUnlinked.
    3569       44362 :             assert(foundInUnlinked);
    3570             :         }
    3571     1593986 :         if (!(pindex->nStatus & BLOCK_HAVE_DATA)) assert(!foundInUnlinked); // Can't be in mapBlocksUnlinked if we don't HAVE_DATA
    3572     1593986 :         if (pindexFirstMissing == NULL) assert(!foundInUnlinked); // We aren't missing data for any parent -- cannot be in mapBlocksUnlinked.
    3573     1593986 :         if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed == NULL && pindexFirstMissing != NULL) {
    3574             :             // We HAVE_DATA for this block, have received data for all parents at some point, but we're currently missing data for some parent.
    3575           0 :             assert(fHavePruned); // We must have pruned.
    3576             :             // This block may have entered mapBlocksUnlinked if:
    3577             :             //  - it has a descendant that at some point had more work than the
    3578             :             //    tip, and
    3579             :             //  - we tried switching to that descendant but were missing
    3580             :             //    data for some intermediate block between chainActive and the
    3581             :             //    tip.
    3582             :             // So if this block is itself better than chainActive.Tip() and it wasn't in
    3583             :             // setBlockIndexCandidates, then it must be in mapBlocksUnlinked.
    3584           0 :             if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && setBlockIndexCandidates.count(pindex) == 0) {
    3585           0 :                 if (pindexFirstInvalid == NULL) {
    3586           0 :                     assert(foundInUnlinked);
    3587             :                 }
    3588             :             }
    3589             :         }
    3590             :         // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow
    3591             :         // End: actual consistency checks.
    3592             : 
    3593             :         // Try descending into the first subnode.
    3594     1593986 :         std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
    3595     1593986 :         if (range.first != range.second) {
    3596             :             // A subnode was found.
    3597     1578645 :             pindex = range.first->second;
    3598     1578645 :             nHeight++;
    3599             :             continue;
    3600             :         }
    3601             :         // This is a leaf node.
    3602             :         // Move upwards until we reach a node of which we have not yet visited the last child.
    3603     1608564 :         while (pindex) {
    3604             :             // We are going to either move to a parent or a sibling of pindex.
    3605             :             // If pindex was the first with a certain property, unset the corresponding variable.
    3606     1593986 :             if (pindex == pindexFirstInvalid) pindexFirstInvalid = NULL;
    3607     1593986 :             if (pindex == pindexFirstMissing) pindexFirstMissing = NULL;
    3608     1593986 :             if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed = NULL;
    3609     1593986 :             if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid = NULL;
    3610     1593986 :             if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid = NULL;
    3611     1593986 :             if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid = NULL;
    3612     1593986 :             if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid = NULL;
    3613             :             // Find our parent.
    3614     1593986 :             CBlockIndex* pindexPar = pindex->pprev;
    3615             :             // Find which child we just visited.
    3616     1593986 :             std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
    3617     1595461 :             while (rangePar.first->second != pindex) {
    3618        1475 :                 assert(rangePar.first != rangePar.second); // Our parent must have at least the node we're coming from as child.
    3619        1475 :                 rangePar.first++;
    3620             :             }
    3621             :             // Proceed to the next one.
    3622     1593986 :             rangePar.first++;
    3623     1593986 :             if (rangePar.first != rangePar.second) {
    3624             :                 // Move to the sibling.
    3625         763 :                 pindex = rangePar.first->second;
    3626         763 :                 break;
    3627             :             } else {
    3628             :                 // Move up further.
    3629     1593223 :                 pindex = pindexPar;
    3630     1593223 :                 nHeight--;
    3631     1593223 :                 continue;
    3632             :             }
    3633             :         }
    3634             :     }
    3635             : 
    3636             :     // Check that we actually traversed the entire map.
    3637       14578 :     assert(nNodes == forward.size());
    3638             : }
    3639             : 
    3640             : //////////////////////////////////////////////////////////////////////////////
    3641             : //
    3642             : // CAlert
    3643             : //
    3644             : 
    3645        3522 : std::string GetWarnings(const std::string& strFor)
    3646             : {
    3647        3522 :     int nPriority = 0;
    3648             :     string strStatusBar;
    3649             :     string strRPC;
    3650             : 
    3651             :     if (!CLIENT_VERSION_IS_RELEASE)
    3652        7044 :         strStatusBar = _("This is a pre-release test build - use at your own risk - do not use for mining or merchant applications");
    3653             : 
    3654       10566 :     if (GetBoolArg("-testsafemode", false))
    3655           0 :         strStatusBar = strRPC = "testsafemode enabled";
    3656             : 
    3657             :     // Misc warnings like out of disk space and clock is wrong
    3658        3522 :     if (strMiscWarning != "")
    3659             :     {
    3660           0 :         nPriority = 1000;
    3661             :         strStatusBar = strMiscWarning;
    3662             :     }
    3663             : 
    3664        3522 :     if (fLargeWorkForkFound)
    3665             :     {
    3666           0 :         nPriority = 2000;
    3667           0 :         strStatusBar = strRPC = _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.");
    3668             :     }
    3669        3522 :     else if (fLargeWorkInvalidChainFound)
    3670             :     {
    3671           0 :         nPriority = 2000;
    3672           0 :         strStatusBar = strRPC = _("Warning: We do not appear to fully agree with our peers! You may need to upgrade, or other nodes may need to upgrade.");
    3673             :     }
    3674             : 
    3675             :     // Alerts
    3676             :     {
    3677        3522 :         LOCK(cs_mapAlerts);
    3678       21132 :         BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
    3679             :         {
    3680           0 :             const CAlert& alert = item.second;
    3681           0 :             if (alert.AppliesToMe() && alert.nPriority > nPriority)
    3682             :             {
    3683           0 :                 nPriority = alert.nPriority;
    3684           0 :                 strStatusBar = alert.strStatusBar;
    3685             :             }
    3686             :         }
    3687             :     }
    3688             : 
    3689        3522 :     if (strFor == "statusbar")
    3690           4 :         return strStatusBar;
    3691        3518 :     else if (strFor == "rpc")
    3692        3518 :         return strRPC;
    3693           0 :     assert(!"GetWarnings(): invalid parameter");
    3694             :     return "error";
    3695             : }
    3696             : 
    3697             : 
    3698             : 
    3699             : 
    3700             : 
    3701             : 
    3702             : 
    3703             : 
    3704             : //////////////////////////////////////////////////////////////////////////////
    3705             : //
    3706             : // Messages
    3707             : //
    3708             : 
    3709             : 
    3710        9090 : bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
    3711             : {
    3712        9090 :     switch (inv.type)
    3713             :     {
    3714             :     case MSG_TX:
    3715             :         {
    3716        1553 :             assert(recentRejects);
    3717        3106 :             if (chainActive.Tip()->GetBlockHash() != hashRecentRejectsChainTip)
    3718             :             {
    3719             :                 // If the chain tip has changed previously rejected transactions
    3720             :                 // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
    3721             :                 // or a double-spend. Reset the rejects filter and give those
    3722             :                 // txs a second chance.
    3723         302 :                 hashRecentRejectsChainTip = chainActive.Tip()->GetBlockHash();
    3724         151 :                 recentRejects->reset();
    3725             :             }
    3726             : 
    3727        4659 :             return recentRejects->contains(inv.hash) ||
    3728        2824 :                    mempool.exists(inv.hash) ||
    3729        4095 :                    mapOrphanTransactions.count(inv.hash) ||
    3730        2824 :                    pcoinsTip->HaveCoins(inv.hash);
    3731             :         }
    3732             :     case MSG_BLOCK:
    3733       15074 :         return mapBlockIndex.count(inv.hash);
    3734             :     }
    3735             :     // Don't know what it is, just say we already got one
    3736             :     return true;
    3737             : }
    3738             : 
    3739        4007 : void static ProcessGetData(CNode* pfrom)
    3740             : {
    3741        4007 :     std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();
    3742             : 
    3743             :     vector<CInv> vNotFound;
    3744             : 
    3745        4007 :     LOCK(cs_main);
    3746             : 
    3747        8740 :     while (it != pfrom->vRecvGetData.end()) {
    3748             :         // Don't bother if send buffer is too full to respond anyway
    3749        4120 :         if (pfrom->nSendSize >= SendBufferSize())
    3750             :             break;
    3751             : 
    3752        4120 :         const CInv &inv = *it;
    3753             :         {
    3754        4120 :             boost::this_thread::interruption_point();
    3755        4120 :             it++;
    3756             : 
    3757        4120 :             if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK)
    3758             :             {
    3759        3757 :                 bool send = false;
    3760        7514 :                 BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
    3761        3757 :                 if (mi != mapBlockIndex.end())
    3762             :                 {
    3763        7514 :                     if (chainActive.Contains(mi->second)) {
    3764             :                         send = true;
    3765             :                     } else {
    3766             :                         static const int nOneMonth = 30 * 24 * 60 * 60;
    3767             :                         // To prevent fingerprinting attacks, only send blocks outside of the active
    3768             :                         // chain if they are valid, and no more than a month older (both in time, and in
    3769             :                         // best equivalent proof of work) than the best header chain we know about.
    3770           0 :                         send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != NULL) &&
    3771           0 :                             (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() < nOneMonth) &&
    3772           0 :                             (GetBlockProofEquivalentTime(*pindexBestHeader, *mi->second, *pindexBestHeader, Params().GetConsensus()) < nOneMonth);
    3773           0 :                         if (!send) {
    3774           0 :                             LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
    3775             :                         }
    3776             :                     }
    3777             :                 }
    3778             :                 // Pruned nodes may have deleted the block, so check whether
    3779             :                 // it's available before trying to send.
    3780        7514 :                 if (send && (mi->second->nStatus & BLOCK_HAVE_DATA))
    3781             :                 {
    3782             :                     // Send block from disk
    3783        3757 :                     CBlock block;
    3784        3757 :                     if (!ReadBlockFromDisk(block, (*mi).second))
    3785           0 :                         assert(!"cannot load block from disk");
    3786        3757 :                     if (inv.type == MSG_BLOCK)
    3787        3757 :                         pfrom->PushMessage("block", block);
    3788             :                     else // MSG_FILTERED_BLOCK)
    3789             :                     {
    3790           0 :                         LOCK(pfrom->cs_filter);
    3791           0 :                         if (pfrom->pfilter)
    3792             :                         {
    3793           0 :                             CMerkleBlock merkleBlock(block, *pfrom->pfilter);
    3794           0 :                             pfrom->PushMessage("merkleblock", merkleBlock);
    3795             :                             // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
    3796             :                             // This avoids hurting performance by pointlessly requiring a round-trip
    3797             :                             // Note that there is currently no way for a node to request any single transactions we didn't send here -
    3798             :                             // they must either disconnect and retry or request the full block.
    3799             :                             // Thus, the protocol spec specified allows for us to provide duplicate txn here,
    3800             :                             // however we MUST always provide at least what the remote peer needs
    3801             :                             typedef std::pair<unsigned int, uint256> PairType;
    3802           0 :                             BOOST_FOREACH(PairType& pair, merkleBlock.vMatchedTxn)
    3803           0 :                                 if (!pfrom->setInventoryKnown.count(CInv(MSG_TX, pair.second)))
    3804           0 :                                     pfrom->PushMessage("tx", block.vtx[pair.first]);
    3805             :                         }
    3806             :                         // else
    3807             :                             // no response
    3808             :                     }
    3809             : 
    3810             :                     // Trigger the peer node to send a getblocks request for the next batch of inventory
    3811        7514 :                     if (inv.hash == pfrom->hashContinue)
    3812             :                     {
    3813             :                         // Bypass PushInventory, this must send even if redundant,
    3814             :                         // and we want it right after the last block so they don't
    3815             :                         // wait for other stuff first.
    3816             :                         vector<CInv> vInv;
    3817           0 :                         vInv.push_back(CInv(MSG_BLOCK, chainActive.Tip()->GetBlockHash()));
    3818           0 :                         pfrom->PushMessage("inv", vInv);
    3819           0 :                         pfrom->hashContinue.SetNull();
    3820             :                     }
    3821             :                 }
    3822             :             }
    3823         363 :             else if (inv.IsKnownType())
    3824             :             {
    3825             :                 // Send stream from relay memory
    3826         363 :                 bool pushed = false;
    3827             :                 {
    3828         363 :                     LOCK(cs_mapRelay);
    3829         363 :                     map<CInv, CDataStream>::iterator mi = mapRelay.find(inv);
    3830         363 :                     if (mi != mapRelay.end()) {
    3831         363 :                         pfrom->PushMessage(inv.GetCommand(), (*mi).second);
    3832             :                         pushed = true;
    3833             :                     }
    3834             :                 }
    3835         363 :                 if (!pushed && inv.type == MSG_TX) {
    3836           0 :                     CTransaction tx;
    3837           0 :                     if (mempool.lookup(inv.hash, tx)) {
    3838             :                         CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
    3839             :                         ss.reserve(1000);
    3840             :                         ss << tx;
    3841           0 :                         pfrom->PushMessage("tx", ss);
    3842           0 :                         pushed = true;
    3843             :                     }
    3844             :                 }
    3845         363 :                 if (!pushed) {
    3846           0 :                     vNotFound.push_back(inv);
    3847             :                 }
    3848             :             }
    3849             : 
    3850             :             // Track requests for our stuff.
    3851        4120 :             GetMainSignals().Inventory(inv.hash);
    3852             : 
    3853        4120 :             if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK)
    3854             :                 break;
    3855             :         }
    3856             :     }
    3857             : 
    3858        8014 :     pfrom->vRecvGetData.erase(pfrom->vRecvGetData.begin(), it);
    3859             : 
    3860        4007 :     if (!vNotFound.empty()) {
    3861             :         // Let the peer know that we didn't find what it asked for, so it doesn't
    3862             :         // have to wait around forever. Currently only SPV clients actually care
    3863             :         // about this message: it's needed when they are recursively walking the
    3864             :         // dependencies of relevant unconfirmed transactions. SPV clients want to
    3865             :         // do that because they want to know about (and store and rebroadcast and
    3866             :         // risk analyze) the dependencies of transactions relevant to them, without
    3867             :         // having to download the entire memory pool.
    3868           0 :         pfrom->PushMessage("notfound", vNotFound);
    3869             :     }
    3870        4007 : }
    3871             : 
    3872       21203 : bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived)
    3873             : {
    3874       21203 :     const CChainParams& chainparams = Params();
    3875       21203 :     RandAddSeedPerfmon();
    3876       63609 :     LogPrint("net", "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->id);
    3877       84812 :     if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
    3878             :     {
    3879           0 :         LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
    3880           0 :         return true;
    3881             :     }
    3882             : 
    3883             : 
    3884             : 
    3885             : 
    3886       21203 :     if (strCommand == "version")
    3887             :     {
    3888             :         // Each connection can only send one version message
    3889         250 :         if (pfrom->nVersion != 0)
    3890             :         {
    3891           0 :             pfrom->PushMessage("reject", strCommand, REJECT_DUPLICATE, string("Duplicate version message"));
    3892           0 :             Misbehaving(pfrom->GetId(), 1);
    3893           0 :             return false;
    3894             :         }
    3895             : 
    3896             :         int64_t nTime;
    3897         250 :         CAddress addrMe;
    3898         250 :         CAddress addrFrom;
    3899         250 :         uint64_t nNonce = 1;
    3900        1000 :         vRecv >> pfrom->nVersion >> pfrom->nServices >> nTime >> addrMe;
    3901         250 :         if (pfrom->nVersion < MIN_PEER_PROTO_VERSION)
    3902             :         {
    3903             :             // disconnect from peers older than this proto version
    3904           0 :             LogPrintf("peer=%d using obsolete version %i; disconnecting\n", pfrom->id, pfrom->nVersion);
    3905             :             pfrom->PushMessage("reject", strCommand, REJECT_OBSOLETE,
    3906           0 :                                strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION));
    3907           0 :             pfrom->fDisconnect = true;
    3908           0 :             return false;
    3909             :         }
    3910             : 
    3911         250 :         if (pfrom->nVersion == 10300)
    3912           0 :             pfrom->nVersion = 300;
    3913         250 :         if (!vRecv.empty())
    3914         250 :             vRecv >> addrFrom >> nNonce;
    3915         250 :         if (!vRecv.empty()) {
    3916         750 :             vRecv >> LIMITED_STRING(pfrom->strSubVer, MAX_SUBVERSION_LENGTH);
    3917         500 :             pfrom->cleanSubVer = SanitizeString(pfrom->strSubVer);
    3918             :         }
    3919         250 :         if (!vRecv.empty())
    3920         250 :             vRecv >> pfrom->nStartingHeight;
    3921         250 :         if (!vRecv.empty())
    3922         249 :             vRecv >> pfrom->fRelayTxes; // set to true after we get the first filter* message
    3923             :         else
    3924           1 :             pfrom->fRelayTxes = true;
    3925             : 
    3926             :         // Disconnect if we connected to ourself
    3927         250 :         if (nNonce == nLocalHostNonce && nNonce > 1)
    3928             :         {
    3929           0 :             LogPrintf("connected to self at %s, disconnecting\n", pfrom->addr.ToString());
    3930           0 :             pfrom->fDisconnect = true;
    3931           0 :             return true;
    3932             :         }
    3933             : 
    3934         250 :         pfrom->addrLocal = addrMe;
    3935         250 :         if (pfrom->fInbound && addrMe.IsRoutable())
    3936             :         {
    3937           0 :             SeenLocal(addrMe);
    3938             :         }
    3939             : 
    3940             :         // Be shy and don't send version until we hear
    3941         250 :         if (pfrom->fInbound)
    3942         126 :             pfrom->PushVersion();
    3943             : 
    3944         250 :         pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
    3945             : 
    3946             :         // Potentially mark this peer as a preferred download peer.
    3947         500 :         UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
    3948             : 
    3949             :         // Change version
    3950         250 :         pfrom->PushMessage("verack");
    3951         500 :         pfrom->ssSend.SetVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
    3952             : 
    3953         250 :         if (!pfrom->fInbound)
    3954             :         {
    3955             :             // Advertise our address
    3956         124 :             if (fListen && !IsInitialBlockDownload())
    3957             :             {
    3958          32 :                 CAddress addr = GetLocalAddress(&pfrom->addr);
    3959          32 :                 if (addr.IsRoutable())
    3960             :                 {
    3961           0 :                     pfrom->PushAddress(addr);
    3962          32 :                 } else if (IsPeerAddrLocalGood(pfrom)) {
    3963           0 :                     addr.SetIP(pfrom->addrLocal);
    3964           0 :                     pfrom->PushAddress(addr);
    3965             :                 }
    3966             :             }
    3967             : 
    3968             :             // Get recent addresses
    3969         124 :             if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || addrman.size() < 1000)
    3970             :             {
    3971         124 :                 pfrom->PushMessage("getaddr");
    3972         124 :                 pfrom->fGetAddr = true;
    3973             :             }
    3974         124 :             addrman.Good(pfrom->addr);
    3975             :         } else {
    3976         126 :             if (((CNetAddr)pfrom->addr) == (CNetAddr)addrFrom)
    3977             :             {
    3978           1 :                 addrman.Add(addrFrom, addrFrom);
    3979           1 :                 addrman.Good(addrFrom);
    3980             :             }
    3981             :         }
    3982             : 
    3983             :         // Relay alerts
    3984             :         {
    3985         250 :             LOCK(cs_mapAlerts);
    3986        1500 :             BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
    3987           0 :                 item.second.RelayTo(pfrom);
    3988             :         }
    3989             : 
    3990         250 :         pfrom->fSuccessfullyConnected = true;
    3991             : 
    3992             :         string remoteAddr;
    3993         250 :         if (fLogIPs)
    3994           0 :             remoteAddr = ", peeraddr=" + pfrom->addr.ToString();
    3995             : 
    3996         500 :         LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
    3997             :                   pfrom->cleanSubVer, pfrom->nVersion,
    3998             :                   pfrom->nStartingHeight, addrMe.ToString(), pfrom->id,
    3999         250 :                   remoteAddr);
    4000             : 
    4001         250 :         int64_t nTimeOffset = nTime - GetTime();
    4002         250 :         pfrom->nTimeOffset = nTimeOffset;
    4003         250 :         AddTimeData(pfrom->addr, nTimeOffset);
    4004             :     }
    4005             : 
    4006             : 
    4007       20953 :     else if (pfrom->nVersion == 0)
    4008             :     {
    4009             :         // Must have a version message before anything else
    4010           0 :         Misbehaving(pfrom->GetId(), 1);
    4011           0 :         return false;
    4012             :     }
    4013             : 
    4014             : 
    4015       20953 :     else if (strCommand == "verack")
    4016             :     {
    4017         500 :         pfrom->SetRecvVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
    4018             : 
    4019             :         // Mark this node as currently connected, so we update its timestamp later.
    4020         250 :         if (pfrom->fNetworkNode) {
    4021         124 :             LOCK(cs_main);
    4022         248 :             State(pfrom->GetId())->fCurrentlyConnected = true;
    4023             :         }
    4024             :     }
    4025             : 
    4026             : 
    4027       20703 :     else if (strCommand == "addr")
    4028             :     {
    4029             :         vector<CAddress> vAddr;
    4030           0 :         vRecv >> vAddr;
    4031             : 
    4032             :         // Don't want addr from older versions unless seeding
    4033           0 :         if (pfrom->nVersion < CADDR_TIME_VERSION && addrman.size() > 1000)
    4034             :             return true;
    4035           0 :         if (vAddr.size() > 1000)
    4036             :         {
    4037           0 :             Misbehaving(pfrom->GetId(), 20);
    4038           0 :             return error("message addr size() = %u", vAddr.size());
    4039             :         }
    4040             : 
    4041             :         // Store the new addresses
    4042             :         vector<CAddress> vAddrOk;
    4043           0 :         int64_t nNow = GetAdjustedTime();
    4044           0 :         int64_t nSince = nNow - 10 * 60;
    4045           0 :         BOOST_FOREACH(CAddress& addr, vAddr)
    4046             :         {
    4047           0 :             boost::this_thread::interruption_point();
    4048             : 
    4049           0 :             if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
    4050           0 :                 addr.nTime = nNow - 5 * 24 * 60 * 60;
    4051           0 :             pfrom->AddAddressKnown(addr);
    4052           0 :             bool fReachable = IsReachable(addr);
    4053           0 :             if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
    4054             :             {
    4055             :                 // Relay to a limited number of other nodes
    4056             :                 {
    4057           0 :                     LOCK(cs_vNodes);
    4058             :                     // Use deterministic randomness to send to the same nodes for 24 hours
    4059             :                     // at a time so the addrKnowns of the chosen nodes prevent repeats
    4060           0 :                     static uint256 hashSalt;
    4061           0 :                     if (hashSalt.IsNull())
    4062           0 :                         hashSalt = GetRandHash();
    4063           0 :                     uint64_t hashAddr = addr.GetHash();
    4064           0 :                     uint256 hashRand = ArithToUint256(UintToArith256(hashSalt) ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60)));
    4065           0 :                     hashRand = Hash(BEGIN(hashRand), END(hashRand));
    4066             :                     multimap<uint256, CNode*> mapMix;
    4067           0 :                     BOOST_FOREACH(CNode* pnode, vNodes)
    4068             :                     {
    4069           0 :                         if (pnode->nVersion < CADDR_TIME_VERSION)
    4070           0 :                             continue;
    4071             :                         unsigned int nPointer;
    4072             :                         memcpy(&nPointer, &pnode, sizeof(nPointer));
    4073           0 :                         uint256 hashKey = ArithToUint256(UintToArith256(hashRand) ^ nPointer);
    4074           0 :                         hashKey = Hash(BEGIN(hashKey), END(hashKey));
    4075           0 :                         mapMix.insert(make_pair(hashKey, pnode));
    4076             :                     }
    4077           0 :                     int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
    4078           0 :                     for (multimap<uint256, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
    4079           0 :                         ((*mi).second)->PushAddress(addr);
    4080             :                 }
    4081             :             }
    4082             :             // Do not store addresses outside our network
    4083           0 :             if (fReachable)
    4084           0 :                 vAddrOk.push_back(addr);
    4085             :         }
    4086           0 :         addrman.Add(vAddrOk, pfrom->addr, 2 * 60 * 60);
    4087           0 :         if (vAddr.size() < 1000)
    4088           0 :             pfrom->fGetAddr = false;
    4089           0 :         if (pfrom->fOneShot)
    4090           0 :             pfrom->fDisconnect = true;
    4091             :     }
    4092             : 
    4093             : 
    4094       20703 :     else if (strCommand == "inv")
    4095             :     {
    4096             :         vector<CInv> vInv;
    4097             :         vRecv >> vInv;
    4098        6144 :         if (vInv.size() > MAX_INV_SZ)
    4099             :         {
    4100           0 :             Misbehaving(pfrom->GetId(), 20);
    4101           0 :             return error("message inv size() = %u", vInv.size());
    4102             :         }
    4103             : 
    4104        3072 :         LOCK(cs_main);
    4105             : 
    4106             :         std::vector<CInv> vToFetch;
    4107             : 
    4108       22872 :         for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
    4109             :         {
    4110       16728 :             const CInv &inv = vInv[nInv];
    4111             : 
    4112        8364 :             boost::this_thread::interruption_point();
    4113        8364 :             pfrom->AddInventoryKnown(inv);
    4114             : 
    4115        8364 :             bool fAlreadyHave = AlreadyHave(inv);
    4116       16728 :             LogPrint("net", "got inv: %s  %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom->id);
    4117             : 
    4118        8364 :             if (!fAlreadyHave && !fImporting && !fReindex && inv.type != MSG_BLOCK)
    4119         518 :                 pfrom->AskFor(inv);
    4120             : 
    4121        8364 :             if (inv.type == MSG_BLOCK) {
    4122        7537 :                 UpdateBlockAvailability(pfrom->GetId(), inv.hash);
    4123       12381 :                 if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
    4124             :                     // First request the headers preceding the announced block. In the normal fully-synced
    4125             :                     // case where a new block is announced that succeeds the current tip (no reorganization),
    4126             :                     // there are no such headers.
    4127             :                     // Secondly, and only when we are close to being synced, we request the announced block directly,
    4128             :                     // to avoid an extra round-trip. Note that we must *first* ask for the headers, so by the
    4129             :                     // time the block arrives, the header chain leading up to it is already validated. Not
    4130             :                     // doing this will result in the received block being rejected as an orphan in case it is
    4131             :                     // not a direct successor.
    4132        9056 :                     pfrom->PushMessage("getheaders", chainActive.GetLocator(pindexBestHeader), inv.hash);
    4133        9056 :                     CNodeState *nodestate = State(pfrom->GetId());
    4134       13144 :                     if (chainActive.Tip()->GetBlockTime() > GetAdjustedTime() - chainparams.GetConsensus().nPowTargetSpacing * 20 &&
    4135        4088 :                         nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
    4136        1483 :                         vToFetch.push_back(inv);
    4137             :                         // Mark block as in flight already, even though the actual "getdata" message only goes out
    4138             :                         // later (within the same cs_main lock, though).
    4139        1483 :                         MarkBlockAsInFlight(pfrom->GetId(), inv.hash, chainparams.GetConsensus());
    4140             :                     }
    4141        9056 :                     LogPrint("net", "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, inv.hash.ToString(), pfrom->id);
    4142             :                 }
    4143             :             }
    4144             : 
    4145             :             // Track requests for our stuff
    4146        8364 :             GetMainSignals().Inventory(inv.hash);
    4147             : 
    4148        8364 :             if (pfrom->nSendSize > (SendBufferSize() * 2)) {
    4149           0 :                 Misbehaving(pfrom->GetId(), 50);
    4150           0 :                 return error("send buffer size() = %u", pfrom->nSendSize);
    4151             :             }
    4152             :         }
    4153             : 
    4154        3072 :         if (!vToFetch.empty())
    4155         764 :             pfrom->PushMessage("getdata", vToFetch);
    4156             :     }
    4157             : 
    4158             : 
    4159       17631 :     else if (strCommand == "getdata")
    4160             :     {
    4161             :         vector<CInv> vInv;
    4162             :         vRecv >> vInv;
    4163        6306 :         if (vInv.size() > MAX_INV_SZ)
    4164             :         {
    4165           0 :             Misbehaving(pfrom->GetId(), 20);
    4166           0 :             return error("message getdata size() = %u", vInv.size());
    4167             :         }
    4168             : 
    4169        3153 :         if (fDebug || (vInv.size() != 1))
    4170         525 :             LogPrint("net", "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom->id);
    4171             : 
    4172        6306 :         if ((fDebug && vInv.size() > 0) || (vInv.size() == 1))
    4173        5922 :             LogPrint("net", "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom->id);
    4174             : 
    4175        6306 :         pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
    4176        3153 :         ProcessGetData(pfrom);
    4177             :     }
    4178             : 
    4179             : 
    4180       14478 :     else if (strCommand == "getblocks")
    4181             :     {
    4182             :         CBlockLocator locator;
    4183             :         uint256 hashStop;
    4184           1 :         vRecv >> locator >> hashStop;
    4185             : 
    4186           1 :         LOCK(cs_main);
    4187             : 
    4188             :         // Find the last block the caller has in the main chain
    4189           1 :         CBlockIndex* pindex = FindForkInGlobalIndex(chainActive, locator);
    4190             : 
    4191             :         // Send the rest of the chain
    4192           1 :         if (pindex)
    4193           1 :             pindex = chainActive.Next(pindex);
    4194           1 :         int nLimit = 500;
    4195           4 :         LogPrint("net", "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), nLimit, pfrom->id);
    4196           0 :         for (; pindex; pindex = chainActive.Next(pindex))
    4197             :         {
    4198           0 :             if (pindex->GetBlockHash() == hashStop)
    4199             :             {
    4200           0 :                 LogPrint("net", "  getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
    4201           0 :                 break;
    4202             :             }
    4203             :             // If pruning, don't inv blocks unless we have on disk and are likely to still have
    4204             :             // for some reasonable time window (1 hour) that block relay might require.
    4205           0 :             const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / chainparams.GetConsensus().nPowTargetSpacing;
    4206           0 :             if (fPruneMode && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= chainActive.Tip()->nHeight - nPrunedBlocksLikelyToHave))
    4207             :             {
    4208           0 :                 LogPrint("net", " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
    4209           0 :                 break;
    4210             :             }
    4211           0 :             pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
    4212           0 :             if (--nLimit <= 0)
    4213             :             {
    4214             :                 // When this block is requested, we'll send an inv that'll
    4215             :                 // trigger the peer to getblocks the next batch of inventory.
    4216           0 :                 LogPrint("net", "  getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
    4217           0 :                 pfrom->hashContinue = pindex->GetBlockHash();
    4218           0 :                 break;
    4219             :             }
    4220             :         }
    4221             :     }
    4222             : 
    4223             : 
    4224       14477 :     else if (strCommand == "getheaders")
    4225             :     {
    4226             :         CBlockLocator locator;
    4227             :         uint256 hashStop;
    4228        4653 :         vRecv >> locator >> hashStop;
    4229             : 
    4230        4653 :         LOCK(cs_main);
    4231             : 
    4232        4653 :         if (IsInitialBlockDownload())
    4233             :             return true;
    4234             : 
    4235        4542 :         CBlockIndex* pindex = NULL;
    4236        4542 :         if (locator.IsNull())
    4237             :         {
    4238             :             // If locator is null, return the hashStop block
    4239           0 :             BlockMap::iterator mi = mapBlockIndex.find(hashStop);
    4240           0 :             if (mi == mapBlockIndex.end())
    4241             :                 return true;
    4242           0 :             pindex = (*mi).second;
    4243             :         }
    4244             :         else
    4245             :         {
    4246             :             // Find the last block the caller has in the main chain
    4247        4542 :             pindex = FindForkInGlobalIndex(chainActive, locator);
    4248        4542 :             if (pindex)
    4249        4542 :                 pindex = chainActive.Next(pindex);
    4250             :         }
    4251             : 
    4252             :         // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
    4253        4542 :         vector<CBlock> vHeaders;
    4254        4542 :         int nLimit = MAX_HEADERS_RESULTS;
    4255        9084 :         LogPrint("net", "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.ToString(), pfrom->id);
    4256      188366 :         for (; pindex; pindex = chainActive.Next(pindex))
    4257             :         {
    4258      376198 :             vHeaders.push_back(pindex->GetBlockHeader());
    4259      564297 :             if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
    4260             :                 break;
    4261             :         }
    4262        4542 :         pfrom->PushMessage("headers", vHeaders);
    4263             :     }
    4264             : 
    4265             : 
    4266        9824 :     else if (strCommand == "tx")
    4267             :     {
    4268             :         vector<uint256> vWorkQueue;
    4269             :         vector<uint256> vEraseQueue;
    4270         363 :         CTransaction tx;
    4271             :         vRecv >> tx;
    4272             : 
    4273         363 :         CInv inv(MSG_TX, tx.GetHash());
    4274         363 :         pfrom->AddInventoryKnown(inv);
    4275             : 
    4276         363 :         LOCK(cs_main);
    4277             : 
    4278         363 :         bool fMissingInputs = false;
    4279         337 :         CValidationState state;
    4280             : 
    4281         363 :         mapAlreadyAskedFor.erase(inv);
    4282             : 
    4283             :         // Check for recently rejected (and do other quick existence checks)
    4284         363 :         if (AlreadyHave(inv))
    4285          26 :             return true;
    4286             : 
    4287         337 :         if (AcceptToMemoryPool(mempool, state, tx, true, &fMissingInputs))
    4288             :         {
    4289         334 :             mempool.check(pcoinsTip);
    4290         334 :             RelayTransaction(tx);
    4291         334 :             vWorkQueue.push_back(inv.hash);
    4292             : 
    4293             :             LogPrint("mempool", "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u)\n",
    4294             :                 pfrom->id,
    4295             :                 tx.GetHash().ToString(),
    4296         668 :                 mempool.size());
    4297             : 
    4298             :             // Recursively process any orphan transactions that depended on this one
    4299             :             set<NodeId> setMisbehaving;
    4300        1338 :             for (unsigned int i = 0; i < vWorkQueue.size(); i++)
    4301             :             {
    4302        1005 :                 map<uint256, set<uint256> >::iterator itByPrev = mapOrphanTransactionsByPrev.find(vWorkQueue[i]);
    4303         335 :                 if (itByPrev == mapOrphanTransactionsByPrev.end())
    4304             :                     continue;
    4305           4 :                 for (set<uint256>::iterator mi = itByPrev->second.begin();
    4306           4 :                      mi != itByPrev->second.end();
    4307             :                      ++mi)
    4308             :                 {
    4309           1 :                     const uint256& orphanHash = *mi;
    4310           1 :                     const CTransaction& orphanTx = mapOrphanTransactions[orphanHash].tx;
    4311           1 :                     NodeId fromPeer = mapOrphanTransactions[orphanHash].fromPeer;
    4312           1 :                     bool fMissingInputs2 = false;
    4313             :                     // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
    4314             :                     // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
    4315             :                     // anyone relaying LegitTxX banned)
    4316             :                     CValidationState stateDummy;
    4317             : 
    4318             : 
    4319           1 :                     if (setMisbehaving.count(fromPeer))
    4320           0 :                         continue;
    4321           1 :                     if (AcceptToMemoryPool(mempool, stateDummy, orphanTx, true, &fMissingInputs2))
    4322             :                     {
    4323           2 :                         LogPrint("mempool", "   accepted orphan tx %s\n", orphanHash.ToString());
    4324           1 :                         RelayTransaction(orphanTx);
    4325           1 :                         vWorkQueue.push_back(orphanHash);
    4326           1 :                         vEraseQueue.push_back(orphanHash);
    4327             :                     }
    4328           0 :                     else if (!fMissingInputs2)
    4329             :                     {
    4330           0 :                         int nDos = 0;
    4331           0 :                         if (stateDummy.IsInvalid(nDos) && nDos > 0)
    4332             :                         {
    4333             :                             // Punish peer that gave us an invalid orphan tx
    4334           0 :                             Misbehaving(fromPeer, nDos);
    4335             :                             setMisbehaving.insert(fromPeer);
    4336           0 :                             LogPrint("mempool", "   invalid orphan tx %s\n", orphanHash.ToString());
    4337             :                         }
    4338             :                         // Has inputs but not accepted to mempool
    4339             :                         // Probably non-standard or insufficient fee/priority
    4340           0 :                         LogPrint("mempool", "   removed orphan tx %s\n", orphanHash.ToString());
    4341           0 :                         vEraseQueue.push_back(orphanHash);
    4342           0 :                         assert(recentRejects);
    4343           0 :                         recentRejects->insert(orphanHash);
    4344             :                     }
    4345           1 :                     mempool.check(pcoinsTip);
    4346           1 :                 }
    4347             :             }
    4348             : 
    4349        2010 :             BOOST_FOREACH(uint256 hash, vEraseQueue)
    4350           1 :                 EraseOrphanTx(hash);
    4351             :         }
    4352           3 :         else if (fMissingInputs)
    4353             :         {
    4354           2 :             AddOrphanTx(tx, pfrom->GetId());
    4355             : 
    4356             :             // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
    4357           8 :             unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));
    4358           2 :             unsigned int nEvicted = LimitOrphanTxSize(nMaxOrphanTx);
    4359           2 :             if (nEvicted > 0)
    4360           0 :                 LogPrint("mempool", "mapOrphan overflow, removed %u tx\n", nEvicted);
    4361             :         } else {
    4362           1 :             assert(recentRejects);
    4363           2 :             recentRejects->insert(tx.GetHash());
    4364             : 
    4365           1 :             if (pfrom->fWhitelisted) {
    4366             :                 // Always relay transactions received from whitelisted peers, even
    4367             :                 // if they were rejected from the mempool, allowing the node to
    4368             :                 // function as a gateway for nodes hidden behind it.
    4369             :                 //
    4370             :                 // FIXME: This includes invalid transactions, which means a
    4371             :                 // whitelisted peer could get us banned! We may want to change
    4372             :                 // that.
    4373           0 :                 RelayTransaction(tx);
    4374             :             }
    4375             :         }
    4376         337 :         int nDoS = 0;
    4377         337 :         if (state.IsInvalid(nDoS))
    4378             :         {
    4379             :             LogPrint("mempoolrej", "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
    4380             :                 pfrom->id,
    4381           3 :                 FormatStateMessage(state));
    4382           1 :             if (state.GetRejectCode() < REJECT_INTERNAL) // Never send AcceptToMemoryPool's internal codes over P2P
    4383           1 :                 pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),
    4384           4 :                                    state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash);
    4385           1 :             if (nDoS > 0)
    4386           0 :                 Misbehaving(pfrom->GetId(), nDoS);
    4387             :         }
    4388             :     }
    4389             : 
    4390             : 
    4391        9461 :     else if (strCommand == "headers" && !fImporting && !fReindex) // Ignore headers received while importing
    4392             :     {
    4393             :         std::vector<CBlockHeader> headers;
    4394             : 
    4395             :         // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
    4396        4636 :         unsigned int nCount = ReadCompactSize(vRecv);
    4397        4636 :         if (nCount > MAX_HEADERS_RESULTS) {
    4398           0 :             Misbehaving(pfrom->GetId(), 20);
    4399           0 :             return error("headers message size = %u", nCount);
    4400             :         }
    4401        9272 :         headers.resize(nCount);
    4402      197541 :         for (unsigned int n = 0; n < nCount; n++) {
    4403      385810 :             vRecv >> headers[n];
    4404      192905 :             ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
    4405             :         }
    4406             : 
    4407        4636 :         LOCK(cs_main);
    4408             : 
    4409        4636 :         if (nCount == 0) {
    4410             :             // Nothing interesting. Stop asking this peers for more headers.
    4411             :             return true;
    4412             :         }
    4413             : 
    4414        4631 :         CBlockIndex *pindexLast = NULL;
    4415      992311 :         BOOST_FOREACH(const CBlockHeader& header, headers) {
    4416             :             CValidationState state;
    4417      569453 :             if (pindexLast != NULL && header.hashPrevBlock != pindexLast->GetBlockHash()) {
    4418           0 :                 Misbehaving(pfrom->GetId(), 20);
    4419           0 :                 return error("non-continuous headers sequence");
    4420             :             }
    4421      192905 :             if (!AcceptBlockHeader(header, state, &pindexLast)) {
    4422             :                 int nDoS;
    4423           0 :                 if (state.IsInvalid(nDoS)) {
    4424           0 :                     if (nDoS > 0)
    4425           0 :                         Misbehaving(pfrom->GetId(), nDoS);
    4426           0 :                     return error("invalid header received");
    4427             :                 }
    4428             :             }
    4429      192905 :         }
    4430             : 
    4431        4631 :         if (pindexLast)
    4432        9262 :             UpdateBlockAvailability(pfrom->GetId(), pindexLast->GetBlockHash());
    4433             : 
    4434        4631 :         if (nCount == MAX_HEADERS_RESULTS && pindexLast) {
    4435             :             // Headers message had its maximum size; the peer may have more headers.
    4436             :             // TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
    4437             :             // from there instead.
    4438           0 :             LogPrint("net", "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom->id, pfrom->nStartingHeight);
    4439           0 :             pfrom->PushMessage("getheaders", chainActive.GetLocator(pindexLast), uint256());
    4440             :         }
    4441             : 
    4442        4631 :         CheckBlockIndex();
    4443             :     }
    4444             : 
    4445        4825 :     else if (strCommand == "block" && !fImporting && !fReindex) // Ignore blocks received while importing
    4446             :     {
    4447        4042 :         CBlock block;
    4448        4042 :         vRecv >> block;
    4449             : 
    4450        4042 :         CInv inv(MSG_BLOCK, block.GetHash());
    4451        8084 :         LogPrint("net", "received block %s peer=%d\n", inv.hash.ToString(), pfrom->id);
    4452             : 
    4453        4042 :         pfrom->AddInventoryKnown(inv);
    4454             : 
    4455        4042 :         CValidationState state;
    4456             :         // Process all blocks from whitelisted peers, even if not requested,
    4457             :         // unless we're still syncing with the network.
    4458             :         // Such an unrequested block may still be processed, subject to the
    4459             :         // conditions in AcceptBlock().
    4460        4042 :         bool forceProcessing = pfrom->fWhitelisted && !IsInitialBlockDownload();
    4461        4042 :         ProcessNewBlock(state, pfrom, &block, forceProcessing, NULL);
    4462             :         int nDoS;
    4463        4042 :         if (state.IsInvalid(nDoS)) {
    4464          38 :             assert (state.GetRejectCode() < REJECT_INTERNAL); // Blocks are never rejected with internal reject codes
    4465          38 :             pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),
    4466         152 :                                state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash);
    4467          38 :             if (nDoS > 0) {
    4468          38 :                 LOCK(cs_main);
    4469          38 :                 Misbehaving(pfrom->GetId(), nDoS);
    4470             :             }
    4471             :         }
    4472             : 
    4473             :     }
    4474             : 
    4475             : 
    4476             :     // This asymmetric behavior for inbound and outbound connections was introduced
    4477             :     // to prevent a fingerprinting attack: an attacker can send specific fake addresses
    4478             :     // to users' AddrMan and later request them by sending getaddr messages.
    4479             :     // Making nodes which are behind NAT and can only make outgoing connections ignore
    4480             :     // the getaddr message mitigates the attack.
    4481         783 :     else if ((strCommand == "getaddr") && (pfrom->fInbound))
    4482             :     {
    4483         124 :         pfrom->vAddrToSend.clear();
    4484         124 :         vector<CAddress> vAddr = addrman.GetAddr();
    4485         744 :         BOOST_FOREACH(const CAddress &addr, vAddr)
    4486           0 :             pfrom->PushAddress(addr);
    4487             :     }
    4488             : 
    4489             : 
    4490         659 :     else if (strCommand == "mempool")
    4491             :     {
    4492           0 :         LOCK2(cs_main, pfrom->cs_filter);
    4493             : 
    4494             :         std::vector<uint256> vtxid;
    4495           0 :         mempool.queryHashes(vtxid);
    4496             :         vector<CInv> vInv;
    4497           0 :         BOOST_FOREACH(uint256& hash, vtxid) {
    4498           0 :             CInv inv(MSG_TX, hash);
    4499           0 :             CTransaction tx;
    4500           0 :             bool fInMemPool = mempool.lookup(hash, tx);
    4501           0 :             if (!fInMemPool) continue; // another thread removed since queryHashes, maybe...
    4502           0 :             if ((pfrom->pfilter && pfrom->pfilter->IsRelevantAndUpdate(tx)) ||
    4503           0 :                (!pfrom->pfilter))
    4504           0 :                 vInv.push_back(inv);
    4505           0 :             if (vInv.size() == MAX_INV_SZ) {
    4506           0 :                 pfrom->PushMessage("inv", vInv);
    4507             :                 vInv.clear();
    4508             :             }
    4509             :         }
    4510           0 :         if (vInv.size() > 0)
    4511           0 :             pfrom->PushMessage("inv", vInv);
    4512             :     }
    4513             : 
    4514             : 
    4515         659 :     else if (strCommand == "ping")
    4516             :     {
    4517         408 :         if (pfrom->nVersion > BIP0031_VERSION)
    4518             :         {
    4519         408 :             uint64_t nonce = 0;
    4520             :             vRecv >> nonce;
    4521             :             // Echo the message back with the nonce. This allows for two useful features:
    4522             :             //
    4523             :             // 1) A remote node can quickly check if the connection is operational
    4524             :             // 2) Remote nodes can measure the latency of the network thread. If this node
    4525             :             //    is overloaded it won't respond to pings quickly and the remote node can
    4526             :             //    avoid sending us more work, like chain download requests.
    4527             :             //
    4528             :             // The nonce stops the remote getting confused between different pings: without
    4529             :             // it, if the remote node sends a ping once per second and this node takes 5
    4530             :             // seconds to respond to each, the 5th ping the remote sends would appear to
    4531             :             // return very quickly.
    4532         408 :             pfrom->PushMessage("pong", nonce);
    4533             :         }
    4534             :     }
    4535             : 
    4536             : 
    4537         251 :     else if (strCommand == "pong")
    4538             :     {
    4539         250 :         int64_t pingUsecEnd = nTimeReceived;
    4540         250 :         uint64_t nonce = 0;
    4541         250 :         size_t nAvail = vRecv.in_avail();
    4542         250 :         bool bPingFinished = false;
    4543             :         std::string sProblem;
    4544             : 
    4545         250 :         if (nAvail >= sizeof(nonce)) {
    4546             :             vRecv >> nonce;
    4547             : 
    4548             :             // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
    4549         250 :             if (pfrom->nPingNonceSent != 0) {
    4550         250 :                 if (nonce == pfrom->nPingNonceSent) {
    4551             :                     // Matching pong received, this ping is no longer outstanding
    4552         250 :                     bPingFinished = true;
    4553         250 :                     int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart;
    4554         250 :                     if (pingUsecTime > 0) {
    4555             :                         // Successful ping time measurement, replace previous
    4556         250 :                         pfrom->nPingUsecTime = pingUsecTime;
    4557         500 :                         pfrom->nMinPingUsecTime = std::min(pfrom->nMinPingUsecTime, pingUsecTime);
    4558             :                     } else {
    4559             :                         // This should never happen
    4560             :                         sProblem = "Timing mishap";
    4561             :                     }
    4562             :                 } else {
    4563             :                     // Nonce mismatches are normal when pings are overlapping
    4564             :                     sProblem = "Nonce mismatch";
    4565           0 :                     if (nonce == 0) {
    4566             :                         // This is most likely a bug in another implementation somewhere; cancel this ping
    4567           0 :                         bPingFinished = true;
    4568             :                         sProblem = "Nonce zero";
    4569             :                     }
    4570             :                 }
    4571             :             } else {
    4572             :                 sProblem = "Unsolicited pong without ping";
    4573             :             }
    4574             :         } else {
    4575             :             // This is most likely a bug in another implementation somewhere; cancel this ping
    4576           0 :             bPingFinished = true;
    4577             :             sProblem = "Short payload";
    4578             :         }
    4579             : 
    4580         250 :         if (!(sProblem.empty())) {
    4581             :             LogPrint("net", "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
    4582             :                 pfrom->id,
    4583             :                 sProblem,
    4584             :                 pfrom->nPingNonceSent,
    4585             :                 nonce,
    4586           0 :                 nAvail);
    4587             :         }
    4588         250 :         if (bPingFinished) {
    4589         250 :             pfrom->nPingNonceSent = 0;
    4590             :         }
    4591             :     }
    4592             : 
    4593             : 
    4594           2 :     else if (fAlerts && strCommand == "alert")
    4595             :     {
    4596           0 :         CAlert alert;
    4597             :         vRecv >> alert;
    4598             : 
    4599           0 :         uint256 alertHash = alert.GetHash();
    4600           0 :         if (pfrom->setKnown.count(alertHash) == 0)
    4601             :         {
    4602           0 :             if (alert.ProcessAlert(Params().AlertKey()))
    4603             :             {
    4604             :                 // Relay
    4605           0 :                 pfrom->setKnown.insert(alertHash);
    4606             :                 {
    4607           0 :                     LOCK(cs_vNodes);
    4608           0 :                     BOOST_FOREACH(CNode* pnode, vNodes)
    4609           0 :                         alert.RelayTo(pnode);
    4610             :                 }
    4611             :             }
    4612             :             else {
    4613             :                 // Small DoS penalty so peers that send us lots of
    4614             :                 // duplicate/expired/invalid-signature/whatever alerts
    4615             :                 // eventually get banned.
    4616             :                 // This isn't a Misbehaving(100) (immediate ban) because the
    4617             :                 // peer might be an older or different implementation with
    4618             :                 // a different signature key, etc.
    4619           0 :                 Misbehaving(pfrom->GetId(), 10);
    4620             :             }
    4621           0 :         }
    4622             :     }
    4623             : 
    4624             : 
    4625           2 :     else if (!(nLocalServices & NODE_BLOOM) &&
    4626           0 :               (strCommand == "filterload" ||
    4627           0 :                strCommand == "filteradd" ||
    4628           1 :                strCommand == "filterclear") &&
    4629             :               //TODO: Remove this line after reasonable network upgrade
    4630           0 :               pfrom->nVersion >= NO_BLOOM_VERSION)
    4631             :     {
    4632           0 :         if (pfrom->nVersion >= NO_BLOOM_VERSION)
    4633           0 :             Misbehaving(pfrom->GetId(), 100);
    4634             :         //TODO: Enable this after reasonable network upgrade
    4635             :         //else
    4636             :         //    pfrom->fDisconnect = true;
    4637             :     }
    4638             : 
    4639             : 
    4640           1 :     else if (strCommand == "filterload")
    4641             :     {
    4642             :         CBloomFilter filter;
    4643             :         vRecv >> filter;
    4644             : 
    4645           0 :         if (!filter.IsWithinSizeConstraints())
    4646             :             // There is no excuse for sending a too-large filter
    4647           0 :             Misbehaving(pfrom->GetId(), 100);
    4648             :         else
    4649             :         {
    4650           0 :             LOCK(pfrom->cs_filter);
    4651           0 :             delete pfrom->pfilter;
    4652           0 :             pfrom->pfilter = new CBloomFilter(filter);
    4653           0 :             pfrom->pfilter->UpdateEmptyFull();
    4654             :         }
    4655           0 :         pfrom->fRelayTxes = true;
    4656             :     }
    4657             : 
    4658             : 
    4659           1 :     else if (strCommand == "filteradd")
    4660             :     {
    4661             :         vector<unsigned char> vData;
    4662             :         vRecv >> vData;
    4663             : 
    4664             :         // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
    4665             :         // and thus, the maximum size any matched object can have) in a filteradd message
    4666           0 :         if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE)
    4667             :         {
    4668           0 :             Misbehaving(pfrom->GetId(), 100);
    4669             :         } else {
    4670           0 :             LOCK(pfrom->cs_filter);
    4671           0 :             if (pfrom->pfilter)
    4672           0 :                 pfrom->pfilter->insert(vData);
    4673             :             else
    4674           0 :                 Misbehaving(pfrom->GetId(), 100);
    4675             :         }
    4676             :     }
    4677             : 
    4678             : 
    4679           1 :     else if (strCommand == "filterclear")
    4680             :     {
    4681           0 :         LOCK(pfrom->cs_filter);
    4682           0 :         delete pfrom->pfilter;
    4683           0 :         pfrom->pfilter = new CBloomFilter();
    4684           0 :         pfrom->fRelayTxes = true;
    4685             :     }
    4686             : 
    4687             : 
    4688           1 :     else if (strCommand == "reject")
    4689             :     {
    4690           1 :         if (fDebug) {
    4691             :             try {
    4692             :                 string strMsg; unsigned char ccode; string strReason;
    4693           0 :                 vRecv >> LIMITED_STRING(strMsg, CMessageHeader::COMMAND_SIZE) >> ccode >> LIMITED_STRING(strReason, MAX_REJECT_MESSAGE_LENGTH);
    4694             : 
    4695           0 :                 ostringstream ss;
    4696           0 :                 ss << strMsg << " code " << itostr(ccode) << ": " << strReason;
    4697             : 
    4698           0 :                 if (strMsg == "block" || strMsg == "tx")
    4699             :                 {
    4700             :                     uint256 hash;
    4701             :                     vRecv >> hash;
    4702           0 :                     ss << ": hash " << hash.ToString();
    4703             :                 }
    4704           0 :                 LogPrint("net", "Reject %s\n", SanitizeString(ss.str()));
    4705           0 :             } catch (const std::ios_base::failure&) {
    4706             :                 // Avoid feedback loops by preventing reject messages from triggering a new reject message.
    4707           0 :                 LogPrint("net", "Unparseable reject message received\n");
    4708             :             }
    4709             :         }
    4710             :     }
    4711             : 
    4712             :     else
    4713             :     {
    4714             :         // Ignore unknown commands for extensibility
    4715           0 :         LogPrint("net", "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->id);
    4716             :     }
    4717             : 
    4718             : 
    4719             : 
    4720             :     return true;
    4721             : }
    4722             : 
    4723             : // requires LOCK(cs_vRecvMsg)
    4724       59067 : bool ProcessMessages(CNode* pfrom)
    4725             : {
    4726             :     //if (fDebug)
    4727             :     //    LogPrintf("%s(%u messages)\n", __func__, pfrom->vRecvMsg.size());
    4728             : 
    4729             :     //
    4730             :     // Message format
    4731             :     //  (4) message start
    4732             :     //  (12) command
    4733             :     //  (4) size
    4734             :     //  (4) checksum
    4735             :     //  (x) data
    4736             :     //
    4737       59067 :     bool fOk = true;
    4738             : 
    4739      118134 :     if (!pfrom->vRecvGetData.empty())
    4740         854 :         ProcessGetData(pfrom);
    4741             : 
    4742             :     // this maintains the order of responses
    4743      118134 :     if (!pfrom->vRecvGetData.empty()) return fOk;
    4744             : 
    4745       58342 :     std::deque<CNetMessage>::iterator it = pfrom->vRecvMsg.begin();
    4746      175026 :     while (!pfrom->fDisconnect && it != pfrom->vRecvMsg.end()) {
    4747             :         // Don't bother if send buffer is too full to respond anyway
    4748       21210 :         if (pfrom->nSendSize >= SendBufferSize())
    4749             :             break;
    4750             : 
    4751             :         // get next message
    4752       21210 :         CNetMessage& msg = *it;
    4753             : 
    4754             :         //if (fDebug)
    4755             :         //    LogPrintf("%s(message %u msgsz, %u bytes, complete:%s)\n", __func__,
    4756             :         //            msg.hdr.nMessageSize, msg.vRecv.size(),
    4757             :         //            msg.complete() ? "Y" : "N");
    4758             : 
    4759             :         // end, if an incomplete message is found
    4760       21210 :         if (!msg.complete())
    4761             :             break;
    4762             : 
    4763             :         // at this point, any failure means we can delete the current message
    4764       21203 :         it++;
    4765             : 
    4766             :         // Scan for message start
    4767       42406 :         if (memcmp(msg.hdr.pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0) {
    4768           0 :             LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->id);
    4769           0 :             fOk = false;
    4770           0 :             break;
    4771             :         }
    4772             : 
    4773             :         // Read header
    4774       21203 :         CMessageHeader& hdr = msg.hdr;
    4775       42406 :         if (!hdr.IsValid(Params().MessageStart()))
    4776             :         {
    4777           0 :             LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->id);
    4778           0 :             continue;
    4779             :         }
    4780       21203 :         string strCommand = hdr.GetCommand();
    4781             : 
    4782             :         // Message size
    4783       21203 :         unsigned int nMessageSize = hdr.nMessageSize;
    4784             : 
    4785             :         // Checksum
    4786       21203 :         CDataStream& vRecv = msg.vRecv;
    4787       63609 :         uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize);
    4788       21203 :         unsigned int nChecksum = ReadLE32((unsigned char*)&hash);
    4789       21203 :         if (nChecksum != hdr.nChecksum)
    4790             :         {
    4791           0 :             LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR nChecksum=%08x hdr.nChecksum=%08x\n", __func__,
    4792           0 :                SanitizeString(strCommand), nMessageSize, nChecksum, hdr.nChecksum);
    4793             :             continue;
    4794             :         }
    4795             : 
    4796             :         // Process message
    4797       21203 :         bool fRet = false;
    4798             :         try
    4799             :         {
    4800       42406 :             fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime);
    4801       21203 :             boost::this_thread::interruption_point();
    4802             :         }
    4803           0 :         catch (const std::ios_base::failure& e)
    4804             :         {
    4805           0 :             pfrom->PushMessage("reject", strCommand, REJECT_MALFORMED, string("error parsing message"));
    4806           0 :             if (strstr(e.what(), "end of data"))
    4807             :             {
    4808             :                 // Allow exceptions from under-length message on vRecv
    4809           0 :                 LogPrintf("%s(%s, %u bytes): Exception '%s' caught, normally caused by a message being shorter than its stated length\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
    4810             :             }
    4811           0 :             else if (strstr(e.what(), "size too large"))
    4812             :             {
    4813             :                 // Allow exceptions from over-long size
    4814           0 :                 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
    4815             :             }
    4816             :             else
    4817             :             {
    4818           0 :                 PrintExceptionContinue(&e, "ProcessMessages()");
    4819             :             }
    4820             :         }
    4821           0 :         catch (const boost::thread_interrupted&) {
    4822           0 :             throw;
    4823             :         }
    4824           0 :         catch (const std::exception& e) {
    4825           0 :             PrintExceptionContinue(&e, "ProcessMessages()");
    4826           0 :         } catch (...) {
    4827           0 :             PrintExceptionContinue(NULL, "ProcessMessages()");
    4828             :         }
    4829             : 
    4830       21203 :         if (!fRet)
    4831           0 :             LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->id);
    4832             : 
    4833             :         break;
    4834             :     }
    4835             : 
    4836             :     // In case the connection got shut down, its receive buffer was wiped
    4837       58342 :     if (!pfrom->fDisconnect)
    4838      116684 :         pfrom->vRecvMsg.erase(pfrom->vRecvMsg.begin(), it);
    4839             : 
    4840       58342 :     return fOk;
    4841             : }
    4842             : 
    4843             : 
    4844       59366 : bool SendMessages(CNode* pto, bool fSendTrickle)
    4845             : {
    4846      118732 :     const Consensus::Params& consensusParams = Params().GetConsensus();
    4847             :     {
    4848             :         // Don't send anything until we get its version message
    4849       59366 :         if (pto->nVersion == 0)
    4850         458 :             return true;
    4851             : 
    4852             :         //
    4853             :         // Message: ping
    4854             :         //
    4855       59317 :         bool pingSend = false;
    4856       59317 :         if (pto->fPingQueued) {
    4857             :             // RPC ping request by user
    4858           0 :             pingSend = true;
    4859             :         }
    4860       59317 :         if (pto->nPingNonceSent == 0 && pto->nPingUsecStart + PING_INTERVAL * 1000000 < GetTimeMicros()) {
    4861             :             // Ping automatically sent as a latency probe & keepalive.
    4862         254 :             pingSend = true;
    4863             :         }
    4864       59317 :         if (pingSend) {
    4865         254 :             uint64_t nonce = 0;
    4866         762 :             while (nonce == 0) {
    4867         254 :                 GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
    4868             :             }
    4869         254 :             pto->fPingQueued = false;
    4870         254 :             pto->nPingUsecStart = GetTimeMicros();
    4871         254 :             if (pto->nVersion > BIP0031_VERSION) {
    4872         250 :                 pto->nPingNonceSent = nonce;
    4873         250 :                 pto->PushMessage("ping", nonce);
    4874             :             } else {
    4875             :                 // Peer is too old to support ping command with nonce, pong will never arrive.
    4876           4 :                 pto->nPingNonceSent = 0;
    4877           4 :                 pto->PushMessage("ping");
    4878             :             }
    4879             :         }
    4880             : 
    4881       59317 :         TRY_LOCK(cs_main, lockMain); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
    4882       59317 :         if (!lockMain)
    4883         409 :             return true;
    4884             : 
    4885             :         // Address refresh broadcast
    4886             :         static int64_t nLastRebroadcast;
    4887       58908 :         if (!IsInitialBlockDownload() && (GetTime() - nLastRebroadcast > 24 * 60 * 60))
    4888             :         {
    4889          73 :             LOCK(cs_vNodes);
    4890        1380 :             BOOST_FOREACH(CNode* pnode, vNodes)
    4891             :             {
    4892             :                 // Periodically clear addrKnown to allow refresh broadcasts
    4893         157 :                 if (nLastRebroadcast)
    4894           6 :                     pnode->addrKnown.reset();
    4895             : 
    4896             :                 // Rebroadcast our address
    4897         157 :                 AdvertizeLocal(pnode);
    4898             :             }
    4899          73 :             if (!vNodes.empty())
    4900          73 :                 nLastRebroadcast = GetTime();
    4901             :         }
    4902             : 
    4903             :         //
    4904             :         // Message: addr
    4905             :         //
    4906       58908 :         if (fSendTrickle)
    4907             :         {
    4908             :             vector<CAddress> vAddr;
    4909       43586 :             vAddr.reserve(pto->vAddrToSend.size());
    4910      130758 :             BOOST_FOREACH(const CAddress& addr, pto->vAddrToSend)
    4911             :             {
    4912           0 :                 if (!pto->addrKnown.contains(addr.GetKey()))
    4913             :                 {
    4914           0 :                     pto->addrKnown.insert(addr.GetKey());
    4915           0 :                     vAddr.push_back(addr);
    4916             :                     // receiver rejects addr messages larger than 1000
    4917           0 :                     if (vAddr.size() >= 1000)
    4918             :                     {
    4919           0 :                         pto->PushMessage("addr", vAddr);
    4920             :                         vAddr.clear();
    4921             :                     }
    4922             :                 }
    4923             :             }
    4924       21793 :             pto->vAddrToSend.clear();
    4925       21793 :             if (!vAddr.empty())
    4926           0 :                 pto->PushMessage("addr", vAddr);
    4927             :         }
    4928             : 
    4929      117816 :         CNodeState &state = *State(pto->GetId());
    4930       58908 :         if (state.fShouldBan) {
    4931           6 :             if (pto->fWhitelisted)
    4932           4 :                 LogPrintf("Warning: not punishing whitelisted peer %s!\n", pto->addr.ToString());
    4933             :             else {
    4934           4 :                 pto->fDisconnect = true;
    4935           4 :                 if (pto->addr.IsLocal())
    4936           0 :                     LogPrintf("Warning: not banning local peer %s!\n", pto->addr.ToString());
    4937             :                 else
    4938             :                 {
    4939           4 :                     CNode::Ban(pto->addr, BanReasonNodeMisbehaving);
    4940             :                 }
    4941             :             }
    4942           6 :             state.fShouldBan = false;
    4943             :         }
    4944             : 
    4945      353518 :         BOOST_FOREACH(const CBlockReject& reject, state.rejects)
    4946          42 :             pto->PushMessage("reject", (string)"block", reject.chRejectCode, reject.strRejectReason, reject.hashBlock);
    4947       58908 :         state.rejects.clear();
    4948             : 
    4949             :         // Start block sync
    4950       58908 :         if (pindexBestHeader == NULL)
    4951           0 :             pindexBestHeader = chainActive.Tip();
    4952       58908 :         bool fFetch = state.fPreferredDownload || (nPreferredDownload == 0 && !pto->fClient && !pto->fOneShot); // Download if this is a nice peer, or we have no nice peers and this one might do.
    4953       58908 :         if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) {
    4954             :             // Only actively request headers from a single peer, unless we're close to today.
    4955        3927 :             if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
    4956         224 :                 state.fSyncStarted = true;
    4957         224 :                 nSyncStarted++;
    4958         224 :                 CBlockIndex *pindexStart = pindexBestHeader->pprev ? pindexBestHeader->pprev : pindexBestHeader;
    4959         224 :                 LogPrint("net", "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->id, pto->nStartingHeight);
    4960         448 :                 pto->PushMessage("getheaders", chainActive.GetLocator(pindexStart), uint256());
    4961             :             }
    4962             :         }
    4963             : 
    4964             :         // Resend wallet transactions that haven't gotten in a block yet
    4965             :         // Except during reindex, importing and IBD, when old wallet
    4966             :         // transactions become unconfirmed and spams other nodes.
    4967       58908 :         if (!fReindex && !fImporting && !IsInitialBlockDownload())
    4968             :         {
    4969       55094 :             GetMainSignals().Broadcast(nTimeBestReceived);
    4970             :         }
    4971             : 
    4972             :         //
    4973             :         // Message: inventory
    4974             :         //
    4975             :         vector<CInv> vInv;
    4976             :         vector<CInv> vInvWait;
    4977             :         {
    4978       58908 :             LOCK(pto->cs_inventory);
    4979      117816 :             vInv.reserve(pto->vInventoryToSend.size());
    4980      117816 :             vInvWait.reserve(pto->vInventoryToSend.size());
    4981      415254 :             BOOST_FOREACH(const CInv& inv, pto->vInventoryToSend)
    4982             :             {
    4983       20602 :                 if (pto->setInventoryKnown.count(inv))
    4984             :                     continue;
    4985             : 
    4986             :                 // trickle out tx inv to protect privacy
    4987        9929 :                 if (inv.type == MSG_TX && !fSendTrickle)
    4988             :                 {
    4989             :                     // 1/4 of tx invs blast to all immediately
    4990        2210 :                     static uint256 hashSalt;
    4991        2173 :                     if (hashSalt.IsNull())
    4992          37 :                         hashSalt = GetRandHash();
    4993        4346 :                     uint256 hashRand = ArithToUint256(UintToArith256(inv.hash) ^ UintToArith256(hashSalt));
    4994        2173 :                     hashRand = Hash(BEGIN(hashRand), END(hashRand));
    4995        4346 :                     bool fTrickleWait = ((UintToArith256(hashRand) & 3) != 0);
    4996             : 
    4997        2173 :                     if (fTrickleWait)
    4998             :                     {
    4999        1972 :                         vInvWait.push_back(inv);
    5000        1972 :                         continue;
    5001             :                     }
    5002             :                 }
    5003             : 
    5004             :                 // returns true if wasn't already contained in the set
    5005        7957 :                 if (pto->setInventoryKnown.insert(inv).second)
    5006             :                 {
    5007        7957 :                     vInv.push_back(inv);
    5008       15914 :                     if (vInv.size() >= 1000)
    5009             :                     {
    5010           0 :                         pto->PushMessage("inv", vInv);
    5011             :                         vInv.clear();
    5012             :                     }
    5013             :                 }
    5014             :             }
    5015       58908 :             pto->vInventoryToSend = vInvWait;
    5016             :         }
    5017       58908 :         if (!vInv.empty())
    5018        2782 :             pto->PushMessage("inv", vInv);
    5019             : 
    5020             :         // Detect whether we're stalling
    5021       58908 :         int64_t nNow = GetTimeMicros();
    5022       58908 :         if (!pto->fDisconnect && state.nStallingSince && state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) {
    5023             :             // Stalling only triggers when the block download window cannot move. During normal steady state,
    5024             :             // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
    5025             :             // should only happen during initial block download.
    5026           0 :             LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->id);
    5027           0 :             pto->fDisconnect = true;
    5028             :         }
    5029             :         // In case there is a block that has been in flight from this peer for (2 + 0.5 * N) times the block interval
    5030             :         // (with N the number of validated blocks that were in flight at the time it was requested), disconnect due to
    5031             :         // timeout. We compensate for in-flight blocks to prevent killing off peers due to our own downstream link
    5032             :         // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
    5033             :         // to unreasonably increase our timeout.
    5034             :         // We also compare the block download timeout originally calculated against the time at which we'd disconnect
    5035             :         // if we assumed the block were being requested now (ignoring blocks we've requested from this peer, since we're
    5036             :         // only looking at this peer's oldest request).  This way a large queue in the past doesn't result in a
    5037             :         // permanently large window for this block to be delivered (ie if the number of blocks in flight is decreasing
    5038             :         // more quickly than once every 5 minutes, then we'll shorten the download window for this block).
    5039      117808 :         if (!pto->fDisconnect && state.vBlocksInFlight.size() > 0) {
    5040       21716 :             QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
    5041       21716 :             int64_t nTimeoutIfRequestedNow = GetBlockTimeout(nNow, nQueuedValidatedHeaders - state.nBlocksInFlightValidHeaders, consensusParams);
    5042       10858 :             if (queuedBlock.nTimeDisconnect > nTimeoutIfRequestedNow) {
    5043        4992 :                 LogPrint("net", "Reducing block download timeout for peer=%d block=%s, orig=%d new=%d\n", pto->id, queuedBlock.hash.ToString(), queuedBlock.nTimeDisconnect, nTimeoutIfRequestedNow);
    5044        2496 :                 queuedBlock.nTimeDisconnect = nTimeoutIfRequestedNow;
    5045             :             }
    5046       10858 :             if (queuedBlock.nTimeDisconnect < nNow) {
    5047           0 :                 LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.hash.ToString(), pto->id);
    5048           0 :                 pto->fDisconnect = true;
    5049             :             }
    5050             :         }
    5051             : 
    5052             :         //
    5053             :         // Message: getdata (blocks)
    5054             :         //
    5055             :         vector<CInv> vGetData;
    5056       58908 :         if (!pto->fDisconnect && !pto->fClient && (fFetch || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
    5057             :             vector<CBlockIndex*> vToDownload;
    5058       52689 :             NodeId staller = -1;
    5059       52689 :             FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller);
    5060      328934 :             BOOST_FOREACH(CBlockIndex *pindex, vToDownload) {
    5061        5120 :                 vGetData.push_back(CInv(MSG_BLOCK, pindex->GetBlockHash()));
    5062        5120 :                 MarkBlockAsInFlight(pto->GetId(), pindex->GetBlockHash(), consensusParams, pindex);
    5063             :                 LogPrint("net", "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
    5064        7680 :                     pindex->nHeight, pto->id);
    5065             :             }
    5066       52689 :             if (state.nBlocksInFlight == 0 && staller != -1) {
    5067           0 :                 if (State(staller)->nStallingSince == 0) {
    5068           0 :                     State(staller)->nStallingSince = nNow;
    5069           0 :                     LogPrint("net", "Stall started peer=%d\n", staller);
    5070             :                 }
    5071             :             }
    5072             :         }
    5073             : 
    5074             :         //
    5075             :         // Message: getdata (non-blocks)
    5076             :         //
    5077      137500 :         while (!pto->fDisconnect && !pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
    5078             :         {
    5079        1089 :             const CInv& inv = (*pto->mapAskFor.begin()).second;
    5080         363 :             if (!AlreadyHave(inv))
    5081             :             {
    5082         363 :                 if (fDebug)
    5083          24 :                     LogPrint("net", "Requesting %s peer=%d\n", inv.ToString(), pto->id);
    5084         363 :                 vGetData.push_back(inv);
    5085         726 :                 if (vGetData.size() >= 1000)
    5086             :                 {
    5087           0 :                     pto->PushMessage("getdata", vGetData);
    5088             :                     vGetData.clear();
    5089             :                 }
    5090             :             }
    5091         726 :             pto->mapAskFor.erase(pto->mapAskFor.begin());
    5092             :         }
    5093       58908 :         if (!vGetData.empty())
    5094        2660 :             pto->PushMessage("getdata", vGetData);
    5095             : 
    5096             :     }
    5097       58908 :     return true;
    5098             : }
    5099             : 
    5100          93 :  std::string CBlockFileInfo::ToString() const {
    5101         279 :      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));
    5102             :  }
    5103             : 
    5104             : 
    5105             : 
    5106             : class CMainCleanup
    5107             : {
    5108             : public:
    5109           0 :     CMainCleanup() {}
    5110          96 :     ~CMainCleanup() {
    5111             :         // block headers
    5112          96 :         BlockMap::iterator it1 = mapBlockIndex.begin();
    5113       32970 :         for (; it1 != mapBlockIndex.end(); it1++)
    5114       16437 :             delete (*it1).second;
    5115             :         mapBlockIndex.clear();
    5116             : 
    5117             :         // orphan transactions
    5118             :         mapOrphanTransactions.clear();
    5119             :         mapOrphanTransactionsByPrev.clear();
    5120          96 :     }
    5121         384 : } instance_of_cmaincleanup;

Generated by: LCOV version 1.11