Cache transaction validation successes
[bitcoinplatinum.git] / src / main.cpp
blob7477d08b18e1e73f88117673a80f2b908eb14b8d
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.
6 #include "main.h"
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"
37 #include <sstream>
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>
45 using namespace std;
47 #if defined(NDEBUG)
48 # error "Bitcoin cannot be compiled without assertions."
49 #endif
51 /**
52 * Global state
55 CCriticalSection cs_main;
57 BlockMap mapBlockIndex;
58 CChain chainActive;
59 CBlockIndex *pindexBestHeader = NULL;
60 int64_t nTimeBestReceived = 0;
61 CWaitableCriticalSection csBestBlock;
62 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;
77 /** Fees smaller than this (in satoshi) are considered zero fee (for relaying and mining) */
78 CFeeRate minRelayTxFee = CFeeRate(1000);
80 CTxMemPool mempool(::minRelayTxFee);
82 struct COrphanTx {
83 CTransaction tx;
84 NodeId fromPeer;
86 map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(cs_main);;
87 map<uint256, set<uint256> > mapOrphanTransactionsByPrev GUARDED_BY(cs_main);;
88 void EraseOrphansFor(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
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.
94 static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams);
95 static void CheckBlockIndex();
97 /** Constant stuff for coinbase transactions we create: */
98 CScript COINBASE_FLAGS;
100 const string strMessageMagic = "Bitcoin Signed Message:\n";
102 // Internal stuff
103 namespace {
105 struct CBlockIndexWorkComparator
107 bool operator()(CBlockIndex *pa, CBlockIndex *pb) const {
108 // First sort by most total work, ...
109 if (pa->nChainWork > pb->nChainWork) return false;
110 if (pa->nChainWork < pb->nChainWork) return true;
112 // ... then by earliest time received, ...
113 if (pa->nSequenceId < pb->nSequenceId) return false;
114 if (pa->nSequenceId > pb->nSequenceId) return true;
116 // Use pointer address as tie breaker (should only happen with blocks
117 // loaded from disk, as those all have id 0).
118 if (pa < pb) return false;
119 if (pa > pb) return true;
121 // Identical blocks.
122 return false;
126 CBlockIndex *pindexBestInvalid;
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.
133 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.
139 multimap<CBlockIndex*, CBlockIndex*> mapBlocksUnlinked;
141 CCriticalSection cs_LastBlockFile;
142 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
148 bool fCheckForPruning = false;
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.
154 CCriticalSection cs_nBlockSequenceId;
155 /** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
156 uint32_t nBlockSequenceId = 1;
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.
163 map<uint256, NodeId> mapBlockSource;
165 /** Blocks that are in flight, and that are in the queue to be downloaded. Protected by cs_main. */
166 struct QueuedBlock {
167 uint256 hash;
168 CBlockIndex *pindex; //! Optional.
169 int64_t nTime; //! Time of "getdata" request in microseconds.
170 bool fValidatedHeaders; //! Whether this block has validated headers at the time of request.
171 int64_t nTimeDisconnect; //! The timeout for this block request (for disconnecting a slow peer)
173 map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
175 /** Number of blocks in flight with validated headers. */
176 int nQueuedValidatedHeaders = 0;
178 /** Number of preferable block download peers. */
179 int nPreferredDownload = 0;
181 /** Dirty block index entries. */
182 set<CBlockIndex*> setDirtyBlockIndex;
184 /** Dirty block file entries. */
185 set<int> setDirtyFileInfo;
186 } // anon namespace
188 //////////////////////////////////////////////////////////////////////////////
190 // Registration of network node signals.
193 namespace {
195 struct CBlockReject {
196 unsigned char chRejectCode;
197 string strRejectReason;
198 uint256 hashBlock;
202 * Maintain validation-specific state about nodes, protected by cs_main, instead
203 * by CNode's own locks. This simplifies asynchronous operation, where
204 * processing of incoming data is done after the ProcessMessage call returns,
205 * and we're no longer holding the node's locks.
207 struct CNodeState {
208 //! The peer's address
209 CService address;
210 //! Whether we have a fully established connection.
211 bool fCurrentlyConnected;
212 //! Accumulated misbehaviour score for this peer.
213 int nMisbehavior;
214 //! Whether this peer should be disconnected and banned (unless whitelisted).
215 bool fShouldBan;
216 //! String name of this peer (debugging/logging purposes).
217 std::string name;
218 //! List of asynchronously-determined block rejections to notify this peer about.
219 std::vector<CBlockReject> rejects;
220 //! The best known block we know this peer has announced.
221 CBlockIndex *pindexBestKnownBlock;
222 //! The hash of the last unknown block this peer has announced.
223 uint256 hashLastUnknownBlock;
224 //! The last full block we both have.
225 CBlockIndex *pindexLastCommonBlock;
226 //! Whether we've started headers synchronization with this peer.
227 bool fSyncStarted;
228 //! Since when we're stalling block download progress (in microseconds), or 0.
229 int64_t nStallingSince;
230 list<QueuedBlock> vBlocksInFlight;
231 int nBlocksInFlight;
232 int nBlocksInFlightValidHeaders;
233 //! Whether we consider this a preferred download peer.
234 bool fPreferredDownload;
236 CNodeState() {
237 fCurrentlyConnected = false;
238 nMisbehavior = 0;
239 fShouldBan = false;
240 pindexBestKnownBlock = NULL;
241 hashLastUnknownBlock.SetNull();
242 pindexLastCommonBlock = NULL;
243 fSyncStarted = false;
244 nStallingSince = 0;
245 nBlocksInFlight = 0;
246 nBlocksInFlightValidHeaders = 0;
247 fPreferredDownload = false;
251 /** Map maintaining per-node state. Requires cs_main. */
252 map<NodeId, CNodeState> mapNodeState;
254 // Requires cs_main.
255 CNodeState *State(NodeId pnode) {
256 map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
257 if (it == mapNodeState.end())
258 return NULL;
259 return &it->second;
262 int GetHeight()
264 LOCK(cs_main);
265 return chainActive.Height();
268 void UpdatePreferredDownload(CNode* node, CNodeState* state)
270 nPreferredDownload -= state->fPreferredDownload;
272 // Whether this node should be marked as a preferred download node.
273 state->fPreferredDownload = (!node->fInbound || node->fWhitelisted) && !node->fOneShot && !node->fClient;
275 nPreferredDownload += state->fPreferredDownload;
278 // Returns time at which to timeout block request (nTime in microseconds)
279 int64_t GetBlockTimeout(int64_t nTime, int nValidatedQueuedBefore, const Consensus::Params &consensusParams)
281 return nTime + 500000 * consensusParams.nPowTargetSpacing * (4 + nValidatedQueuedBefore);
284 void InitializeNode(NodeId nodeid, const CNode *pnode) {
285 LOCK(cs_main);
286 CNodeState &state = mapNodeState.insert(std::make_pair(nodeid, CNodeState())).first->second;
287 state.name = pnode->addrName;
288 state.address = pnode->addr;
291 void FinalizeNode(NodeId nodeid) {
292 LOCK(cs_main);
293 CNodeState *state = State(nodeid);
295 if (state->fSyncStarted)
296 nSyncStarted--;
298 if (state->nMisbehavior == 0 && state->fCurrentlyConnected) {
299 AddressCurrentlyConnected(state->address);
302 BOOST_FOREACH(const QueuedBlock& entry, state->vBlocksInFlight)
303 mapBlocksInFlight.erase(entry.hash);
304 EraseOrphansFor(nodeid);
305 nPreferredDownload -= state->fPreferredDownload;
307 mapNodeState.erase(nodeid);
310 // Requires cs_main.
311 // Returns a bool indicating whether we requested this block.
312 bool MarkBlockAsReceived(const uint256& hash) {
313 map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
314 if (itInFlight != mapBlocksInFlight.end()) {
315 CNodeState *state = State(itInFlight->second.first);
316 nQueuedValidatedHeaders -= itInFlight->second.second->fValidatedHeaders;
317 state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders;
318 state->vBlocksInFlight.erase(itInFlight->second.second);
319 state->nBlocksInFlight--;
320 state->nStallingSince = 0;
321 mapBlocksInFlight.erase(itInFlight);
322 return true;
324 return false;
327 // Requires cs_main.
328 void MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const Consensus::Params& consensusParams, CBlockIndex *pindex = NULL) {
329 CNodeState *state = State(nodeid);
330 assert(state != NULL);
332 // Make sure it's not listed somewhere already.
333 MarkBlockAsReceived(hash);
335 int64_t nNow = GetTimeMicros();
336 QueuedBlock newentry = {hash, pindex, nNow, pindex != NULL, GetBlockTimeout(nNow, nQueuedValidatedHeaders, consensusParams)};
337 nQueuedValidatedHeaders += newentry.fValidatedHeaders;
338 list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(), newentry);
339 state->nBlocksInFlight++;
340 state->nBlocksInFlightValidHeaders += newentry.fValidatedHeaders;
341 mapBlocksInFlight[hash] = std::make_pair(nodeid, it);
344 /** Check whether the last unknown block a peer advertized is not yet known. */
345 void ProcessBlockAvailability(NodeId nodeid) {
346 CNodeState *state = State(nodeid);
347 assert(state != NULL);
349 if (!state->hashLastUnknownBlock.IsNull()) {
350 BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
351 if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
352 if (state->pindexBestKnownBlock == NULL || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
353 state->pindexBestKnownBlock = itOld->second;
354 state->hashLastUnknownBlock.SetNull();
359 /** Update tracking information about which blocks a peer is assumed to have. */
360 void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
361 CNodeState *state = State(nodeid);
362 assert(state != NULL);
364 ProcessBlockAvailability(nodeid);
366 BlockMap::iterator it = mapBlockIndex.find(hash);
367 if (it != mapBlockIndex.end() && it->second->nChainWork > 0) {
368 // An actually better block was announced.
369 if (state->pindexBestKnownBlock == NULL || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
370 state->pindexBestKnownBlock = it->second;
371 } else {
372 // An unknown block was announced; just assume that the latest one is the best one.
373 state->hashLastUnknownBlock = hash;
377 /** Find the last common ancestor two blocks have.
378 * Both pa and pb must be non-NULL. */
379 CBlockIndex* LastCommonAncestor(CBlockIndex* pa, CBlockIndex* pb) {
380 if (pa->nHeight > pb->nHeight) {
381 pa = pa->GetAncestor(pb->nHeight);
382 } else if (pb->nHeight > pa->nHeight) {
383 pb = pb->GetAncestor(pa->nHeight);
386 while (pa != pb && pa && pb) {
387 pa = pa->pprev;
388 pb = pb->pprev;
391 // Eventually all chain branches meet at the genesis block.
392 assert(pa == pb);
393 return pa;
396 /** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
397 * at most count entries. */
398 void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<CBlockIndex*>& vBlocks, NodeId& nodeStaller) {
399 if (count == 0)
400 return;
402 vBlocks.reserve(vBlocks.size() + count);
403 CNodeState *state = State(nodeid);
404 assert(state != NULL);
406 // Make sure pindexBestKnownBlock is up to date, we'll need it.
407 ProcessBlockAvailability(nodeid);
409 if (state->pindexBestKnownBlock == NULL || state->pindexBestKnownBlock->nChainWork < chainActive.Tip()->nChainWork) {
410 // This peer has nothing interesting.
411 return;
414 if (state->pindexLastCommonBlock == NULL) {
415 // Bootstrap quickly by guessing a parent of our best tip is the forking point.
416 // Guessing wrong in either direction is not a problem.
417 state->pindexLastCommonBlock = chainActive[std::min(state->pindexBestKnownBlock->nHeight, chainActive.Height())];
420 // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
421 // of its current tip anymore. Go back enough to fix that.
422 state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
423 if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
424 return;
426 std::vector<CBlockIndex*> vToFetch;
427 CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
428 // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
429 // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
430 // download that next block if the window were 1 larger.
431 int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
432 int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
433 NodeId waitingfor = -1;
434 while (pindexWalk->nHeight < nMaxHeight) {
435 // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
436 // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
437 // as iterating over ~100 CBlockIndex* entries anyway.
438 int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
439 vToFetch.resize(nToFetch);
440 pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
441 vToFetch[nToFetch - 1] = pindexWalk;
442 for (unsigned int i = nToFetch - 1; i > 0; i--) {
443 vToFetch[i - 1] = vToFetch[i]->pprev;
446 // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
447 // are not yet downloaded and not in flight to vBlocks. In the mean time, update
448 // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
449 // already part of our chain (and therefore don't need it even if pruned).
450 BOOST_FOREACH(CBlockIndex* pindex, vToFetch) {
451 if (!pindex->IsValid(BLOCK_VALID_TREE)) {
452 // We consider the chain that this peer is on invalid.
453 return;
455 if (pindex->nStatus & BLOCK_HAVE_DATA || chainActive.Contains(pindex)) {
456 if (pindex->nChainTx)
457 state->pindexLastCommonBlock = pindex;
458 } else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
459 // The block is not already downloaded, and not yet in flight.
460 if (pindex->nHeight > nWindowEnd) {
461 // We reached the end of the window.
462 if (vBlocks.size() == 0 && waitingfor != nodeid) {
463 // We aren't able to fetch anything, but we would be if the download window was one larger.
464 nodeStaller = waitingfor;
466 return;
468 vBlocks.push_back(pindex);
469 if (vBlocks.size() == count) {
470 return;
472 } else if (waitingfor == -1) {
473 // This is the first already-in-flight block.
474 waitingfor = mapBlocksInFlight[pindex->GetBlockHash()].first;
480 } // anon namespace
482 bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
483 LOCK(cs_main);
484 CNodeState *state = State(nodeid);
485 if (state == NULL)
486 return false;
487 stats.nMisbehavior = state->nMisbehavior;
488 stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
489 stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
490 BOOST_FOREACH(const QueuedBlock& queue, state->vBlocksInFlight) {
491 if (queue.pindex)
492 stats.vHeightInFlight.push_back(queue.pindex->nHeight);
494 return true;
497 void RegisterNodeSignals(CNodeSignals& nodeSignals)
499 nodeSignals.GetHeight.connect(&GetHeight);
500 nodeSignals.ProcessMessages.connect(&ProcessMessages);
501 nodeSignals.SendMessages.connect(&SendMessages);
502 nodeSignals.InitializeNode.connect(&InitializeNode);
503 nodeSignals.FinalizeNode.connect(&FinalizeNode);
506 void UnregisterNodeSignals(CNodeSignals& nodeSignals)
508 nodeSignals.GetHeight.disconnect(&GetHeight);
509 nodeSignals.ProcessMessages.disconnect(&ProcessMessages);
510 nodeSignals.SendMessages.disconnect(&SendMessages);
511 nodeSignals.InitializeNode.disconnect(&InitializeNode);
512 nodeSignals.FinalizeNode.disconnect(&FinalizeNode);
515 CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator)
517 // Find the first block the caller has in the main chain
518 BOOST_FOREACH(const uint256& hash, locator.vHave) {
519 BlockMap::iterator mi = mapBlockIndex.find(hash);
520 if (mi != mapBlockIndex.end())
522 CBlockIndex* pindex = (*mi).second;
523 if (chain.Contains(pindex))
524 return pindex;
527 return chain.Genesis();
530 CCoinsViewCache *pcoinsTip = NULL;
531 CBlockTreeDB *pblocktree = NULL;
533 //////////////////////////////////////////////////////////////////////////////
535 // mapOrphanTransactions
538 bool AddOrphanTx(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
540 uint256 hash = tx.GetHash();
541 if (mapOrphanTransactions.count(hash))
542 return false;
544 // Ignore big transactions, to avoid a
545 // send-big-orphans memory exhaustion attack. If a peer has a legitimate
546 // large transaction with a missing parent then we assume
547 // it will rebroadcast it later, after the parent transaction(s)
548 // have been mined or received.
549 // 10,000 orphans, each of which is at most 5,000 bytes big is
550 // at most 500 megabytes of orphans:
551 unsigned int sz = tx.GetSerializeSize(SER_NETWORK, CTransaction::CURRENT_VERSION);
552 if (sz > 5000)
554 LogPrint("mempool", "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
555 return false;
558 mapOrphanTransactions[hash].tx = tx;
559 mapOrphanTransactions[hash].fromPeer = peer;
560 BOOST_FOREACH(const CTxIn& txin, tx.vin)
561 mapOrphanTransactionsByPrev[txin.prevout.hash].insert(hash);
563 LogPrint("mempool", "stored orphan tx %s (mapsz %u prevsz %u)\n", hash.ToString(),
564 mapOrphanTransactions.size(), mapOrphanTransactionsByPrev.size());
565 return true;
568 void static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
570 map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
571 if (it == mapOrphanTransactions.end())
572 return;
573 BOOST_FOREACH(const CTxIn& txin, it->second.tx.vin)
575 map<uint256, set<uint256> >::iterator itPrev = mapOrphanTransactionsByPrev.find(txin.prevout.hash);
576 if (itPrev == mapOrphanTransactionsByPrev.end())
577 continue;
578 itPrev->second.erase(hash);
579 if (itPrev->second.empty())
580 mapOrphanTransactionsByPrev.erase(itPrev);
582 mapOrphanTransactions.erase(it);
585 void EraseOrphansFor(NodeId peer)
587 int nErased = 0;
588 map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
589 while (iter != mapOrphanTransactions.end())
591 map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
592 if (maybeErase->second.fromPeer == peer)
594 EraseOrphanTx(maybeErase->second.tx.GetHash());
595 ++nErased;
598 if (nErased > 0) LogPrint("mempool", "Erased %d orphan tx from peer %d\n", nErased, peer);
602 unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
604 unsigned int nEvicted = 0;
605 while (mapOrphanTransactions.size() > nMaxOrphans)
607 // Evict a random orphan:
608 uint256 randomhash = GetRandHash();
609 map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.lower_bound(randomhash);
610 if (it == mapOrphanTransactions.end())
611 it = mapOrphanTransactions.begin();
612 EraseOrphanTx(it->first);
613 ++nEvicted;
615 return nEvicted;
618 bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
620 if (tx.nLockTime == 0)
621 return true;
622 if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
623 return true;
624 BOOST_FOREACH(const CTxIn& txin, tx.vin)
625 if (!txin.IsFinal())
626 return false;
627 return true;
630 bool CheckFinalTx(const CTransaction &tx)
632 AssertLockHeld(cs_main);
633 return IsFinalTx(tx, chainActive.Height() + 1, GetAdjustedTime());
636 unsigned int GetLegacySigOpCount(const CTransaction& tx)
638 unsigned int nSigOps = 0;
639 BOOST_FOREACH(const CTxIn& txin, tx.vin)
641 nSigOps += txin.scriptSig.GetSigOpCount(false);
643 BOOST_FOREACH(const CTxOut& txout, tx.vout)
645 nSigOps += txout.scriptPubKey.GetSigOpCount(false);
647 return nSigOps;
650 unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs)
652 if (tx.IsCoinBase())
653 return 0;
655 unsigned int nSigOps = 0;
656 for (unsigned int i = 0; i < tx.vin.size(); i++)
658 const CTxOut &prevout = inputs.GetOutputFor(tx.vin[i]);
659 if (prevout.scriptPubKey.IsPayToScriptHash())
660 nSigOps += prevout.scriptPubKey.GetSigOpCount(tx.vin[i].scriptSig);
662 return nSigOps;
672 bool CheckTransaction(const CTransaction& tx, CValidationState &state)
674 // Basic checks that don't depend on any context
675 if (tx.vin.empty())
676 return state.DoS(10, error("CheckTransaction(): vin empty"),
677 REJECT_INVALID, "bad-txns-vin-empty");
678 if (tx.vout.empty())
679 return state.DoS(10, error("CheckTransaction(): vout empty"),
680 REJECT_INVALID, "bad-txns-vout-empty");
681 // Size limits
682 if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) > MAX_BLOCK_SIZE)
683 return state.DoS(100, error("CheckTransaction(): size limits failed"),
684 REJECT_INVALID, "bad-txns-oversize");
686 // Check for negative or overflow output values
687 CAmount nValueOut = 0;
688 BOOST_FOREACH(const CTxOut& txout, tx.vout)
690 if (txout.nValue < 0)
691 return state.DoS(100, error("CheckTransaction(): txout.nValue negative"),
692 REJECT_INVALID, "bad-txns-vout-negative");
693 if (txout.nValue > MAX_MONEY)
694 return state.DoS(100, error("CheckTransaction(): txout.nValue too high"),
695 REJECT_INVALID, "bad-txns-vout-toolarge");
696 nValueOut += txout.nValue;
697 if (!MoneyRange(nValueOut))
698 return state.DoS(100, error("CheckTransaction(): txout total out of range"),
699 REJECT_INVALID, "bad-txns-txouttotal-toolarge");
702 // Check for duplicate inputs
703 set<COutPoint> vInOutPoints;
704 BOOST_FOREACH(const CTxIn& txin, tx.vin)
706 if (vInOutPoints.count(txin.prevout))
707 return state.DoS(100, error("CheckTransaction(): duplicate inputs"),
708 REJECT_INVALID, "bad-txns-inputs-duplicate");
709 vInOutPoints.insert(txin.prevout);
712 if (tx.IsCoinBase())
714 if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
715 return state.DoS(100, error("CheckTransaction(): coinbase script size"),
716 REJECT_INVALID, "bad-cb-length");
718 else
720 BOOST_FOREACH(const CTxIn& txin, tx.vin)
721 if (txin.prevout.IsNull())
722 return state.DoS(10, error("CheckTransaction(): prevout is null"),
723 REJECT_INVALID, "bad-txns-prevout-null");
726 return true;
729 CAmount GetMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree)
732 LOCK(mempool.cs);
733 uint256 hash = tx.GetHash();
734 double dPriorityDelta = 0;
735 CAmount nFeeDelta = 0;
736 mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
737 if (dPriorityDelta > 0 || nFeeDelta > 0)
738 return 0;
741 CAmount nMinFee = ::minRelayTxFee.GetFee(nBytes);
743 if (fAllowFree)
745 // There is a free transaction area in blocks created by most miners,
746 // * If we are relaying we allow transactions up to DEFAULT_BLOCK_PRIORITY_SIZE - 1000
747 // to be considered to fall into this category. We don't want to encourage sending
748 // multiple transactions instead of one big transaction to avoid fees.
749 if (nBytes < (DEFAULT_BLOCK_PRIORITY_SIZE - 1000))
750 nMinFee = 0;
753 if (!MoneyRange(nMinFee))
754 nMinFee = MAX_MONEY;
755 return nMinFee;
759 bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,
760 bool* pfMissingInputs, bool fRejectAbsurdFee)
762 AssertLockHeld(cs_main);
763 if (pfMissingInputs)
764 *pfMissingInputs = false;
766 if (!CheckTransaction(tx, state))
767 return error("AcceptToMemoryPool: CheckTransaction failed");
769 // Coinbase is only valid in a block, not as a loose transaction
770 if (tx.IsCoinBase())
771 return state.DoS(100, error("AcceptToMemoryPool: coinbase as individual tx"),
772 REJECT_INVALID, "coinbase");
774 // Rather not work on nonstandard transactions (unless -testnet/-regtest)
775 string reason;
776 if (fRequireStandard && !IsStandardTx(tx, reason))
777 return state.DoS(0,
778 error("AcceptToMemoryPool: nonstandard transaction: %s", reason),
779 REJECT_NONSTANDARD, reason);
781 // Only accept nLockTime-using transactions that can be mined in the next
782 // block; we don't want our mempool filled up with transactions that can't
783 // be mined yet.
784 if (!CheckFinalTx(tx))
785 return state.DoS(0, error("AcceptToMemoryPool: non-final"),
786 REJECT_NONSTANDARD, "non-final");
788 // is it already in the memory pool?
789 uint256 hash = tx.GetHash();
790 if (pool.exists(hash))
791 return false;
793 // Check for conflicts with in-memory transactions
795 LOCK(pool.cs); // protect pool.mapNextTx
796 for (unsigned int i = 0; i < tx.vin.size(); i++)
798 COutPoint outpoint = tx.vin[i].prevout;
799 if (pool.mapNextTx.count(outpoint))
801 // Disable replacement feature for now
802 return false;
808 CCoinsView dummy;
809 CCoinsViewCache view(&dummy);
811 CAmount nValueIn = 0;
813 LOCK(pool.cs);
814 CCoinsViewMemPool viewMemPool(pcoinsTip, pool);
815 view.SetBackend(viewMemPool);
817 // do we already have it?
818 if (view.HaveCoins(hash))
819 return false;
821 // do all inputs exist?
822 // Note that this does not check for the presence of actual outputs (see the next check for that),
823 // and only helps with filling in pfMissingInputs (to determine missing vs spent).
824 BOOST_FOREACH(const CTxIn txin, tx.vin) {
825 if (!view.HaveCoins(txin.prevout.hash)) {
826 if (pfMissingInputs)
827 *pfMissingInputs = true;
828 return false;
832 // are the actual inputs available?
833 if (!view.HaveInputs(tx))
834 return state.Invalid(error("AcceptToMemoryPool: inputs already spent"),
835 REJECT_DUPLICATE, "bad-txns-inputs-spent");
837 // Bring the best block into scope
838 view.GetBestBlock();
840 nValueIn = view.GetValueIn(tx);
842 // we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool
843 view.SetBackend(dummy);
846 // Check for non-standard pay-to-script-hash in inputs
847 if (fRequireStandard && !AreInputsStandard(tx, view))
848 return error("AcceptToMemoryPool: nonstandard transaction input");
850 // Check that the transaction doesn't have an excessive number of
851 // sigops, making it impossible to mine. Since the coinbase transaction
852 // itself can contain sigops MAX_STANDARD_TX_SIGOPS is less than
853 // MAX_BLOCK_SIGOPS; we still consider this an invalid rather than
854 // merely non-standard transaction.
855 unsigned int nSigOps = GetLegacySigOpCount(tx);
856 nSigOps += GetP2SHSigOpCount(tx, view);
857 if (nSigOps > MAX_STANDARD_TX_SIGOPS)
858 return state.DoS(0,
859 error("AcceptToMemoryPool: too many sigops %s, %d > %d",
860 hash.ToString(), nSigOps, MAX_STANDARD_TX_SIGOPS),
861 REJECT_NONSTANDARD, "bad-txns-too-many-sigops");
863 CAmount nValueOut = tx.GetValueOut();
864 CAmount nFees = nValueIn-nValueOut;
865 double dPriority = view.GetPriority(tx, chainActive.Height());
867 CTxMemPoolEntry entry(tx, nFees, GetTime(), dPriority, chainActive.Height(), mempool.HasNoInputsOf(tx));
868 unsigned int nSize = entry.GetTxSize();
870 // Don't accept it if it can't get into a block
871 CAmount txMinFee = GetMinRelayFee(tx, nSize, true);
872 if (fLimitFree && nFees < txMinFee)
873 return state.DoS(0, error("AcceptToMemoryPool: not enough fees %s, %d < %d",
874 hash.ToString(), nFees, txMinFee),
875 REJECT_INSUFFICIENTFEE, "insufficient fee");
877 // Require that free transactions have sufficient priority to be mined in the next block.
878 if (GetBoolArg("-relaypriority", true) && nFees < ::minRelayTxFee.GetFee(nSize) && !AllowFree(view.GetPriority(tx, chainActive.Height() + 1))) {
879 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "insufficient priority");
882 // Continuously rate-limit free (really, very-low-fee) transactions
883 // This mitigates 'penny-flooding' -- sending thousands of free transactions just to
884 // be annoying or make others' transactions take longer to confirm.
885 if (fLimitFree && nFees < ::minRelayTxFee.GetFee(nSize))
887 static CCriticalSection csFreeLimiter;
888 static double dFreeCount;
889 static int64_t nLastTime;
890 int64_t nNow = GetTime();
892 LOCK(csFreeLimiter);
894 // Use an exponentially decaying ~10-minute window:
895 dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime));
896 nLastTime = nNow;
897 // -limitfreerelay unit is thousand-bytes-per-minute
898 // At default rate it would take over a month to fill 1GB
899 if (dFreeCount >= GetArg("-limitfreerelay", 15)*10*1000)
900 return state.DoS(0, error("AcceptToMemoryPool: free transaction rejected by rate limiter"),
901 REJECT_INSUFFICIENTFEE, "rate limited free transaction");
902 LogPrint("mempool", "Rate limit dFreeCount: %g => %g\n", dFreeCount, dFreeCount+nSize);
903 dFreeCount += nSize;
906 if (fRejectAbsurdFee && nFees > ::minRelayTxFee.GetFee(nSize) * 10000)
907 return error("AcceptToMemoryPool: absurdly high fees %s, %d > %d",
908 hash.ToString(),
909 nFees, ::minRelayTxFee.GetFee(nSize) * 10000);
911 // Check against previous transactions
912 // This is done last to help prevent CPU exhaustion denial-of-service attacks.
913 if (!CheckInputs(tx, state, view, true, STANDARD_SCRIPT_VERIFY_FLAGS, true))
915 return error("AcceptToMemoryPool: ConnectInputs failed %s", hash.ToString());
918 // Check again against just the consensus-critical mandatory script
919 // verification flags, in case of bugs in the standard flags that cause
920 // transactions to pass as valid when they're actually invalid. For
921 // instance the STRICTENC flag was incorrectly allowing certain
922 // CHECKSIG NOT scripts to pass, even though they were invalid.
924 // There is a similar check in CreateNewBlock() to prevent creating
925 // invalid blocks, however allowing such transactions into the mempool
926 // can be exploited as a DoS attack.
927 if (!CheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true))
929 return error("AcceptToMemoryPool: BUG! PLEASE REPORT THIS! ConnectInputs failed against MANDATORY but not STANDARD flags %s", hash.ToString());
932 // Store transaction in memory
933 pool.addUnchecked(hash, entry, !IsInitialBlockDownload());
936 SyncWithWallets(tx, NULL);
938 return true;
941 /** Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock */
942 bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow)
944 CBlockIndex *pindexSlow = NULL;
946 LOCK(cs_main);
948 if (mempool.lookup(hash, txOut))
950 return true;
954 if (fTxIndex) {
955 CDiskTxPos postx;
956 if (pblocktree->ReadTxIndex(hash, postx)) {
957 CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
958 if (file.IsNull())
959 return error("%s: OpenBlockFile failed", __func__);
960 CBlockHeader header;
961 try {
962 file >> header;
963 fseek(file.Get(), postx.nTxOffset, SEEK_CUR);
964 file >> txOut;
965 } catch (const std::exception& e) {
966 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
968 hashBlock = header.GetHash();
969 if (txOut.GetHash() != hash)
970 return error("%s: txid mismatch", __func__);
971 return true;
975 if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
976 int nHeight = -1;
978 CCoinsViewCache &view = *pcoinsTip;
979 const CCoins* coins = view.AccessCoins(hash);
980 if (coins)
981 nHeight = coins->nHeight;
983 if (nHeight > 0)
984 pindexSlow = chainActive[nHeight];
988 if (pindexSlow) {
989 CBlock block;
990 if (ReadBlockFromDisk(block, pindexSlow)) {
991 BOOST_FOREACH(const CTransaction &tx, block.vtx) {
992 if (tx.GetHash() == hash) {
993 txOut = tx;
994 hashBlock = pindexSlow->GetBlockHash();
995 return true;
1001 return false;
1009 //////////////////////////////////////////////////////////////////////////////
1011 // CBlock and CBlockIndex
1014 bool WriteBlockToDisk(CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart)
1016 // Open history file to append
1017 CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
1018 if (fileout.IsNull())
1019 return error("WriteBlockToDisk: OpenBlockFile failed");
1021 // Write index header
1022 unsigned int nSize = fileout.GetSerializeSize(block);
1023 fileout << FLATDATA(messageStart) << nSize;
1025 // Write block
1026 long fileOutPos = ftell(fileout.Get());
1027 if (fileOutPos < 0)
1028 return error("WriteBlockToDisk: ftell failed");
1029 pos.nPos = (unsigned int)fileOutPos;
1030 fileout << block;
1032 return true;
1035 bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos)
1037 block.SetNull();
1039 // Open history file to read
1040 CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
1041 if (filein.IsNull())
1042 return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
1044 // Read block
1045 try {
1046 filein >> block;
1048 catch (const std::exception& e) {
1049 return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
1052 // Check the header
1053 if (!CheckProofOfWork(block.GetHash(), block.nBits, Params().GetConsensus()))
1054 return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
1056 return true;
1059 bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex)
1061 if (!ReadBlockFromDisk(block, pindex->GetBlockPos()))
1062 return false;
1063 if (block.GetHash() != pindex->GetBlockHash())
1064 return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
1065 pindex->ToString(), pindex->GetBlockPos().ToString());
1066 return true;
1069 CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
1071 int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
1072 // Force block reward to zero when right shift is undefined.
1073 if (halvings >= 64)
1074 return 0;
1076 CAmount nSubsidy = 50 * COIN;
1077 // Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
1078 nSubsidy >>= halvings;
1079 return nSubsidy;
1082 bool IsInitialBlockDownload()
1084 const CChainParams& chainParams = Params();
1085 LOCK(cs_main);
1086 if (fImporting || fReindex)
1087 return true;
1088 if (fCheckpointsEnabled && chainActive.Height() < Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints()))
1089 return true;
1090 static bool lockIBDState = false;
1091 if (lockIBDState)
1092 return false;
1093 bool state = (chainActive.Height() < pindexBestHeader->nHeight - 24 * 6 ||
1094 pindexBestHeader->GetBlockTime() < GetTime() - 24 * 60 * 60);
1095 if (!state)
1096 lockIBDState = true;
1097 return state;
1100 bool fLargeWorkForkFound = false;
1101 bool fLargeWorkInvalidChainFound = false;
1102 CBlockIndex *pindexBestForkTip = NULL, *pindexBestForkBase = NULL;
1104 void CheckForkWarningConditions()
1106 AssertLockHeld(cs_main);
1107 // Before we get past initial download, we cannot reliably alert about forks
1108 // (we assume we don't get stuck on a fork before the last checkpoint)
1109 if (IsInitialBlockDownload())
1110 return;
1112 // If our best fork is no longer within 72 blocks (+/- 12 hours if no one mines it)
1113 // of our head, drop it
1114 if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
1115 pindexBestForkTip = NULL;
1117 if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6)))
1119 if (!fLargeWorkForkFound && pindexBestForkBase)
1121 std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
1122 pindexBestForkBase->phashBlock->ToString() + std::string("'");
1123 CAlert::Notify(warning, true);
1125 if (pindexBestForkTip && pindexBestForkBase)
1127 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__,
1128 pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
1129 pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
1130 fLargeWorkForkFound = true;
1132 else
1134 LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
1135 fLargeWorkInvalidChainFound = true;
1138 else
1140 fLargeWorkForkFound = false;
1141 fLargeWorkInvalidChainFound = false;
1145 void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
1147 AssertLockHeld(cs_main);
1148 // If we are on a fork that is sufficiently large, set a warning flag
1149 CBlockIndex* pfork = pindexNewForkTip;
1150 CBlockIndex* plonger = chainActive.Tip();
1151 while (pfork && pfork != plonger)
1153 while (plonger && plonger->nHeight > pfork->nHeight)
1154 plonger = plonger->pprev;
1155 if (pfork == plonger)
1156 break;
1157 pfork = pfork->pprev;
1160 // We define a condition where we should warn the user about as a fork of at least 7 blocks
1161 // with a tip within 72 blocks (+/- 12 hours if no one mines it) of ours
1162 // We use 7 blocks rather arbitrarily as it represents just under 10% of sustained network
1163 // hash rate operating on the fork.
1164 // or a chain that is entirely longer than ours and invalid (note that this should be detected by both)
1165 // We define it this way because it allows us to only store the highest fork tip (+ base) which meets
1166 // the 7-block condition and from this always have the most-likely-to-cause-warning fork
1167 if (pfork && (!pindexBestForkTip || (pindexBestForkTip && pindexNewForkTip->nHeight > pindexBestForkTip->nHeight)) &&
1168 pindexNewForkTip->nChainWork - pfork->nChainWork > (GetBlockProof(*pfork) * 7) &&
1169 chainActive.Height() - pindexNewForkTip->nHeight < 72)
1171 pindexBestForkTip = pindexNewForkTip;
1172 pindexBestForkBase = pfork;
1175 CheckForkWarningConditions();
1178 // Requires cs_main.
1179 void Misbehaving(NodeId pnode, int howmuch)
1181 if (howmuch == 0)
1182 return;
1184 CNodeState *state = State(pnode);
1185 if (state == NULL)
1186 return;
1188 state->nMisbehavior += howmuch;
1189 int banscore = GetArg("-banscore", 100);
1190 if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
1192 LogPrintf("%s: %s (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__, state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
1193 state->fShouldBan = true;
1194 } else
1195 LogPrintf("%s: %s (%d -> %d)\n", __func__, state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
1198 void static InvalidChainFound(CBlockIndex* pindexNew)
1200 if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
1201 pindexBestInvalid = pindexNew;
1203 LogPrintf("%s: invalid block=%s height=%d log2_work=%.8g date=%s\n", __func__,
1204 pindexNew->GetBlockHash().ToString(), pindexNew->nHeight,
1205 log(pindexNew->nChainWork.getdouble())/log(2.0), DateTimeStrFormat("%Y-%m-%d %H:%M:%S",
1206 pindexNew->GetBlockTime()));
1207 LogPrintf("%s: current best=%s height=%d log2_work=%.8g date=%s\n", __func__,
1208 chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0),
1209 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()));
1210 CheckForkWarningConditions();
1213 void static InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state) {
1214 int nDoS = 0;
1215 if (state.IsInvalid(nDoS)) {
1216 std::map<uint256, NodeId>::iterator it = mapBlockSource.find(pindex->GetBlockHash());
1217 if (it != mapBlockSource.end() && State(it->second)) {
1218 CBlockReject reject = {state.GetRejectCode(), state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), pindex->GetBlockHash()};
1219 State(it->second)->rejects.push_back(reject);
1220 if (nDoS > 0)
1221 Misbehaving(it->second, nDoS);
1224 if (!state.CorruptionPossible()) {
1225 pindex->nStatus |= BLOCK_FAILED_VALID;
1226 setDirtyBlockIndex.insert(pindex);
1227 setBlockIndexCandidates.erase(pindex);
1228 InvalidChainFound(pindex);
1232 void UpdateCoins(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight)
1234 // mark inputs spent
1235 if (!tx.IsCoinBase()) {
1236 txundo.vprevout.reserve(tx.vin.size());
1237 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
1238 CCoinsModifier coins = inputs.ModifyCoins(txin.prevout.hash);
1239 unsigned nPos = txin.prevout.n;
1241 if (nPos >= coins->vout.size() || coins->vout[nPos].IsNull())
1242 assert(false);
1243 // mark an outpoint spent, and construct undo information
1244 txundo.vprevout.push_back(CTxInUndo(coins->vout[nPos]));
1245 coins->Spend(nPos);
1246 if (coins->vout.size() == 0) {
1247 CTxInUndo& undo = txundo.vprevout.back();
1248 undo.nHeight = coins->nHeight;
1249 undo.fCoinBase = coins->fCoinBase;
1250 undo.nVersion = coins->nVersion;
1255 // add outputs
1256 inputs.ModifyCoins(tx.GetHash())->FromTx(tx, nHeight);
1259 void UpdateCoins(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, int nHeight)
1261 CTxUndo txundo;
1262 UpdateCoins(tx, state, inputs, txundo, nHeight);
1265 bool CScriptCheck::operator()() {
1266 const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
1267 if (!VerifyScript(scriptSig, scriptPubKey, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, cacheStore), &error)) {
1268 return ::error("CScriptCheck(): %s:%d VerifySignature failed: %s", ptxTo->GetHash().ToString(), nIn, ScriptErrorString(error));
1270 return true;
1273 int GetSpendHeight(const CCoinsViewCache& inputs)
1275 LOCK(cs_main);
1276 CBlockIndex* pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
1277 return pindexPrev->nHeight + 1;
1280 static mrumap<uint256, unsigned int> cacheCheck(2 * MAX_BLOCK_SIZE / CTransaction().GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION));
1281 static boost::mutex cs_cacheCheck;
1283 namespace Consensus {
1284 bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight)
1286 // This doesn't trigger the DoS code on purpose; if it did, it would make it easier
1287 // for an attacker to attempt to split the network.
1288 if (!inputs.HaveInputs(tx))
1289 return state.Invalid(error("CheckInputs(): %s inputs unavailable", tx.GetHash().ToString()));
1291 CAmount nValueIn = 0;
1292 CAmount nFees = 0;
1293 for (unsigned int i = 0; i < tx.vin.size(); i++)
1295 const COutPoint &prevout = tx.vin[i].prevout;
1296 const CCoins *coins = inputs.AccessCoins(prevout.hash);
1297 assert(coins);
1299 // If prev is coinbase, check that it's matured
1300 if (coins->IsCoinBase()) {
1301 if (nSpendHeight - coins->nHeight < COINBASE_MATURITY)
1302 return state.Invalid(
1303 error("CheckInputs(): tried to spend coinbase at depth %d", nSpendHeight - coins->nHeight),
1304 REJECT_INVALID, "bad-txns-premature-spend-of-coinbase");
1307 // Check for negative or overflow input values
1308 nValueIn += coins->vout[prevout.n].nValue;
1309 if (!MoneyRange(coins->vout[prevout.n].nValue) || !MoneyRange(nValueIn))
1310 return state.DoS(100, error("CheckInputs(): txin values out of range"),
1311 REJECT_INVALID, "bad-txns-inputvalues-outofrange");
1315 if (nValueIn < tx.GetValueOut())
1316 return state.DoS(100, error("CheckInputs(): %s value in (%s) < value out (%s)",
1317 tx.GetHash().ToString(), FormatMoney(nValueIn), FormatMoney(tx.GetValueOut())),
1318 REJECT_INVALID, "bad-txns-in-belowout");
1320 // Tally transaction fees
1321 CAmount nTxFee = nValueIn - tx.GetValueOut();
1322 if (nTxFee < 0)
1323 return state.DoS(100, error("CheckInputs(): %s nTxFee < 0", tx.GetHash().ToString()),
1324 REJECT_INVALID, "bad-txns-fee-negative");
1325 nFees += nTxFee;
1326 if (!MoneyRange(nFees))
1327 return state.DoS(100, error("CheckInputs(): nFees out of range"),
1328 REJECT_INVALID, "bad-txns-fee-outofrange");
1329 return true;
1331 }// namespace Consensus
1333 bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, std::vector<CScriptCheck> *pvChecks)
1335 if (!tx.IsCoinBase())
1337 if (fScriptChecks) {
1338 boost::unique_lock<boost::mutex> lock(cs_cacheCheck);
1339 mrumap<uint256, unsigned int>::const_iterator iter = cacheCheck.find(tx.GetHash());
1340 if (iter != cacheCheck.end()) {
1341 // The following test relies on the fact that all script validation flags are softforks (i.e. an extra bit set cannot cause a false result to become true).
1342 if ((iter->second & flags) == flags) {
1343 return true;
1348 if (!Consensus::CheckTxInputs(tx, state, inputs, GetSpendHeight(inputs)))
1349 return false;
1351 if (pvChecks)
1352 pvChecks->reserve(tx.vin.size());
1354 // The first loop above does all the inexpensive checks.
1355 // Only if ALL inputs pass do we perform expensive ECDSA signature checks.
1356 // Helps prevent CPU exhaustion attacks.
1358 // Skip ECDSA signature verification when connecting blocks
1359 // before the last block chain checkpoint. This is safe because block merkle hashes are
1360 // still computed and checked, and any change will be caught at the next checkpoint.
1361 if (fScriptChecks) {
1362 for (unsigned int i = 0; i < tx.vin.size(); i++) {
1363 const COutPoint &prevout = tx.vin[i].prevout;
1364 const CCoins* coins = inputs.AccessCoins(prevout.hash);
1365 assert(coins);
1367 // Verify signature
1368 CScriptCheck check(*coins, tx, i, flags, cacheStore);
1369 if (pvChecks) {
1370 pvChecks->push_back(CScriptCheck());
1371 check.swap(pvChecks->back());
1372 } else if (!check()) {
1373 if (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) {
1374 // Check whether the failure was caused by a
1375 // non-mandatory script verification check, such as
1376 // non-standard DER encodings or non-null dummy
1377 // arguments; if so, don't trigger DoS protection to
1378 // avoid splitting the network between upgraded and
1379 // non-upgraded nodes.
1380 CScriptCheck check(*coins, tx, i,
1381 flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, cacheStore);
1382 if (check())
1383 return state.Invalid(false, REJECT_NONSTANDARD, strprintf("non-mandatory-script-verify-flag (%s)", ScriptErrorString(check.GetScriptError())));
1385 // Failures of other flags indicate a transaction that is
1386 // invalid in new blocks, e.g. a invalid P2SH. We DoS ban
1387 // such nodes as they are not following the protocol. That
1388 // said during an upgrade careful thought should be taken
1389 // as to the correct behavior - we may want to continue
1390 // peering with non-upgraded nodes even after a soft-fork
1391 // super-majority vote has passed.
1392 return state.DoS(100,false, REJECT_INVALID, strprintf("mandatory-script-verify-flag-failed (%s)", ScriptErrorString(check.GetScriptError())));
1398 if (cacheStore && fScriptChecks && pvChecks == NULL) {
1399 boost::unique_lock<boost::mutex> lock(cs_cacheCheck);
1400 cacheCheck.insert(tx.GetHash(), flags);
1403 return true;
1406 namespace {
1408 bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
1410 // Open history file to append
1411 CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
1412 if (fileout.IsNull())
1413 return error("%s: OpenUndoFile failed", __func__);
1415 // Write index header
1416 unsigned int nSize = fileout.GetSerializeSize(blockundo);
1417 fileout << FLATDATA(messageStart) << nSize;
1419 // Write undo data
1420 long fileOutPos = ftell(fileout.Get());
1421 if (fileOutPos < 0)
1422 return error("%s: ftell failed", __func__);
1423 pos.nPos = (unsigned int)fileOutPos;
1424 fileout << blockundo;
1426 // calculate & write checksum
1427 CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
1428 hasher << hashBlock;
1429 hasher << blockundo;
1430 fileout << hasher.GetHash();
1432 return true;
1435 bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock)
1437 // Open history file to read
1438 CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
1439 if (filein.IsNull())
1440 return error("%s: OpenBlockFile failed", __func__);
1442 // Read block
1443 uint256 hashChecksum;
1444 try {
1445 filein >> blockundo;
1446 filein >> hashChecksum;
1448 catch (const std::exception& e) {
1449 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
1452 // Verify checksum
1453 CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
1454 hasher << hashBlock;
1455 hasher << blockundo;
1456 if (hashChecksum != hasher.GetHash())
1457 return error("%s: Checksum mismatch", __func__);
1459 return true;
1462 /** Abort with a message */
1463 bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
1465 strMiscWarning = strMessage;
1466 LogPrintf("*** %s\n", strMessage);
1467 uiInterface.ThreadSafeMessageBox(
1468 userMessage.empty() ? _("Error: A fatal internal error occured, see debug.log for details") : userMessage,
1469 "", CClientUIInterface::MSG_ERROR);
1470 StartShutdown();
1471 return false;
1474 bool AbortNode(CValidationState& state, const std::string& strMessage, const std::string& userMessage="")
1476 AbortNode(strMessage, userMessage);
1477 return state.Error(strMessage);
1480 } // anon namespace
1483 * Apply the undo operation of a CTxInUndo to the given chain state.
1484 * @param undo The undo object.
1485 * @param view The coins view to which to apply the changes.
1486 * @param out The out point that corresponds to the tx input.
1487 * @return True on success.
1489 static bool ApplyTxInUndo(const CTxInUndo& undo, CCoinsViewCache& view, const COutPoint& out)
1491 bool fClean = true;
1493 CCoinsModifier coins = view.ModifyCoins(out.hash);
1494 if (undo.nHeight != 0) {
1495 // undo data contains height: this is the last output of the prevout tx being spent
1496 if (!coins->IsPruned())
1497 fClean = fClean && error("%s: undo data overwriting existing transaction", __func__);
1498 coins->Clear();
1499 coins->fCoinBase = undo.fCoinBase;
1500 coins->nHeight = undo.nHeight;
1501 coins->nVersion = undo.nVersion;
1502 } else {
1503 if (coins->IsPruned())
1504 fClean = fClean && error("%s: undo data adding output to missing transaction", __func__);
1506 if (coins->IsAvailable(out.n))
1507 fClean = fClean && error("%s: undo data overwriting existing output", __func__);
1508 if (coins->vout.size() < out.n+1)
1509 coins->vout.resize(out.n+1);
1510 coins->vout[out.n] = undo.txout;
1512 return fClean;
1515 bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean)
1517 assert(pindex->GetBlockHash() == view.GetBestBlock());
1519 if (pfClean)
1520 *pfClean = false;
1522 bool fClean = true;
1524 CBlockUndo blockUndo;
1525 CDiskBlockPos pos = pindex->GetUndoPos();
1526 if (pos.IsNull())
1527 return error("DisconnectBlock(): no undo data available");
1528 if (!UndoReadFromDisk(blockUndo, pos, pindex->pprev->GetBlockHash()))
1529 return error("DisconnectBlock(): failure reading undo data");
1531 if (blockUndo.vtxundo.size() + 1 != block.vtx.size())
1532 return error("DisconnectBlock(): block and undo data inconsistent");
1534 // undo transactions in reverse order
1535 for (int i = block.vtx.size() - 1; i >= 0; i--) {
1536 const CTransaction &tx = block.vtx[i];
1537 uint256 hash = tx.GetHash();
1539 // Check that all outputs are available and match the outputs in the block itself
1540 // exactly.
1542 CCoinsModifier outs = view.ModifyCoins(hash);
1543 outs->ClearUnspendable();
1545 CCoins outsBlock(tx, pindex->nHeight);
1546 // The CCoins serialization does not serialize negative numbers.
1547 // No network rules currently depend on the version here, so an inconsistency is harmless
1548 // but it must be corrected before txout nversion ever influences a network rule.
1549 if (outsBlock.nVersion < 0)
1550 outs->nVersion = outsBlock.nVersion;
1551 if (*outs != outsBlock)
1552 fClean = fClean && error("DisconnectBlock(): added transaction mismatch? database corrupted");
1554 // remove outputs
1555 outs->Clear();
1558 // restore inputs
1559 if (i > 0) { // not coinbases
1560 const CTxUndo &txundo = blockUndo.vtxundo[i-1];
1561 if (txundo.vprevout.size() != tx.vin.size())
1562 return error("DisconnectBlock(): transaction and undo data inconsistent");
1563 for (unsigned int j = tx.vin.size(); j-- > 0;) {
1564 const COutPoint &out = tx.vin[j].prevout;
1565 const CTxInUndo &undo = txundo.vprevout[j];
1566 if (!ApplyTxInUndo(undo, view, out))
1567 fClean = false;
1572 // move best block pointer to prevout block
1573 view.SetBestBlock(pindex->pprev->GetBlockHash());
1575 if (pfClean) {
1576 *pfClean = fClean;
1577 return true;
1580 return fClean;
1583 void static FlushBlockFile(bool fFinalize = false)
1585 LOCK(cs_LastBlockFile);
1587 CDiskBlockPos posOld(nLastBlockFile, 0);
1589 FILE *fileOld = OpenBlockFile(posOld);
1590 if (fileOld) {
1591 if (fFinalize)
1592 TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nSize);
1593 FileCommit(fileOld);
1594 fclose(fileOld);
1597 fileOld = OpenUndoFile(posOld);
1598 if (fileOld) {
1599 if (fFinalize)
1600 TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nUndoSize);
1601 FileCommit(fileOld);
1602 fclose(fileOld);
1606 bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
1608 static CCheckQueue<CScriptCheck> scriptcheckqueue(128);
1610 void ThreadScriptCheck() {
1611 RenameThread("bitcoin-scriptch");
1612 scriptcheckqueue.Thread();
1616 // Called periodically asynchronously; alerts if it smells like
1617 // we're being fed a bad chain (blocks being generated much
1618 // too slowly or too quickly).
1620 void PartitionCheck(bool (*initialDownloadCheck)(), CCriticalSection& cs, const CBlockIndex *const &bestHeader,
1621 int64_t nPowTargetSpacing)
1623 if (bestHeader == NULL || initialDownloadCheck()) return;
1625 static int64_t lastAlertTime = 0;
1626 int64_t now = GetAdjustedTime();
1627 if (lastAlertTime > now-60*60*24) return; // Alert at most once per day
1629 const int SPAN_HOURS=4;
1630 const int SPAN_SECONDS=SPAN_HOURS*60*60;
1631 int BLOCKS_EXPECTED = SPAN_SECONDS / nPowTargetSpacing;
1633 boost::math::poisson_distribution<double> poisson(BLOCKS_EXPECTED);
1635 std::string strWarning;
1636 int64_t startTime = GetAdjustedTime()-SPAN_SECONDS;
1638 LOCK(cs);
1639 const CBlockIndex* i = bestHeader;
1640 int nBlocks = 0;
1641 while (i->GetBlockTime() >= startTime) {
1642 ++nBlocks;
1643 i = i->pprev;
1644 if (i == NULL) return; // Ran out of chain, we must not be fully sync'ed
1647 // How likely is it to find that many by chance?
1648 double p = boost::math::pdf(poisson, nBlocks);
1650 LogPrint("partitioncheck", "%s : Found %d blocks in the last %d hours\n", __func__, nBlocks, SPAN_HOURS);
1651 LogPrint("partitioncheck", "%s : likelihood: %g\n", __func__, p);
1653 // Aim for one false-positive about every fifty years of normal running:
1654 const int FIFTY_YEARS = 50*365*24*60*60;
1655 double alertThreshold = 1.0 / (FIFTY_YEARS / SPAN_SECONDS);
1657 if (p <= alertThreshold && nBlocks < BLOCKS_EXPECTED)
1659 // Many fewer blocks than expected: alert!
1660 strWarning = strprintf(_("WARNING: check your network connection, %d blocks received in the last %d hours (%d expected)"),
1661 nBlocks, SPAN_HOURS, BLOCKS_EXPECTED);
1663 else if (p <= alertThreshold && nBlocks > BLOCKS_EXPECTED)
1665 // Many more blocks than expected: alert!
1666 strWarning = strprintf(_("WARNING: abnormally high number of blocks generated, %d blocks received in the last %d hours (%d expected)"),
1667 nBlocks, SPAN_HOURS, BLOCKS_EXPECTED);
1669 if (!strWarning.empty())
1671 strMiscWarning = strWarning;
1672 CAlert::Notify(strWarning, true);
1673 lastAlertTime = now;
1677 static int64_t nTimeVerify = 0;
1678 static int64_t nTimeConnect = 0;
1679 static int64_t nTimeIndex = 0;
1680 static int64_t nTimeCallbacks = 0;
1681 static int64_t nTimeTotal = 0;
1683 bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool fJustCheck)
1685 const CChainParams& chainparams = Params();
1686 AssertLockHeld(cs_main);
1687 // Check it again in case a previous version let a bad block in
1688 if (!CheckBlock(block, state, !fJustCheck, !fJustCheck))
1689 return false;
1691 // verify that the view's current state corresponds to the previous block
1692 uint256 hashPrevBlock = pindex->pprev == NULL ? uint256() : pindex->pprev->GetBlockHash();
1693 assert(hashPrevBlock == view.GetBestBlock());
1695 // Special case for the genesis block, skipping connection of its transactions
1696 // (its coinbase is unspendable)
1697 if (block.GetHash() == chainparams.GetConsensus().hashGenesisBlock) {
1698 if (!fJustCheck)
1699 view.SetBestBlock(pindex->GetBlockHash());
1700 return true;
1703 bool fScriptChecks = true;
1704 if (fCheckpointsEnabled) {
1705 CBlockIndex *pindexLastCheckpoint = Checkpoints::GetLastCheckpoint(chainparams.Checkpoints());
1706 if (pindexLastCheckpoint && pindexLastCheckpoint->GetAncestor(pindex->nHeight) == pindex) {
1707 // This block is an ancestor of a checkpoint: disable script checks
1708 fScriptChecks = false;
1712 // Do not allow blocks that contain transactions which 'overwrite' older transactions,
1713 // unless those are already completely spent.
1714 // If such overwrites are allowed, coinbases and transactions depending upon those
1715 // can be duplicated to remove the ability to spend the first instance -- even after
1716 // being sent to another address.
1717 // See BIP30 and http://r6.ca/blog/20120206T005236Z.html for more information.
1718 // This logic is not necessary for memory pool transactions, as AcceptToMemoryPool
1719 // already refuses previously-known transaction ids entirely.
1720 // This rule was originally applied to all blocks with a timestamp after March 15, 2012, 0:00 UTC.
1721 // Now that the whole chain is irreversibly beyond that time it is applied to all blocks except the
1722 // two in the chain that violate it. This prevents exploiting the issue against nodes during their
1723 // initial block download.
1724 bool fEnforceBIP30 = (!pindex->phashBlock) || // Enforce on CreateNewBlock invocations which don't have a hash.
1725 !((pindex->nHeight==91842 && pindex->GetBlockHash() == uint256S("0x00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec")) ||
1726 (pindex->nHeight==91880 && pindex->GetBlockHash() == uint256S("0x00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721")));
1727 if (fEnforceBIP30) {
1728 BOOST_FOREACH(const CTransaction& tx, block.vtx) {
1729 const CCoins* coins = view.AccessCoins(tx.GetHash());
1730 if (coins && !coins->IsPruned())
1731 return state.DoS(100, error("ConnectBlock(): tried to overwrite transaction"),
1732 REJECT_INVALID, "bad-txns-BIP30");
1736 // BIP16 didn't become active until Apr 1 2012
1737 int64_t nBIP16SwitchTime = 1333238400;
1738 bool fStrictPayToScriptHash = (pindex->GetBlockTime() >= nBIP16SwitchTime);
1740 unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE;
1742 // Start enforcing the DERSIG (BIP66) rules, for block.nVersion=3 blocks, when 75% of the network has upgraded:
1743 if (block.nVersion >= 3 && IsSuperMajority(3, pindex->pprev, chainparams.GetConsensus().nMajorityEnforceBlockUpgrade, chainparams.GetConsensus())) {
1744 flags |= SCRIPT_VERIFY_DERSIG;
1747 CBlockUndo blockundo;
1749 CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL);
1751 int64_t nTimeStart = GetTimeMicros();
1752 CAmount nFees = 0;
1753 int nInputs = 0;
1754 unsigned int nSigOps = 0;
1755 CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
1756 std::vector<std::pair<uint256, CDiskTxPos> > vPos;
1757 vPos.reserve(block.vtx.size());
1758 blockundo.vtxundo.reserve(block.vtx.size() - 1);
1759 for (unsigned int i = 0; i < block.vtx.size(); i++)
1761 const CTransaction &tx = block.vtx[i];
1763 nInputs += tx.vin.size();
1764 nSigOps += GetLegacySigOpCount(tx);
1765 if (nSigOps > MAX_BLOCK_SIGOPS)
1766 return state.DoS(100, error("ConnectBlock(): too many sigops"),
1767 REJECT_INVALID, "bad-blk-sigops");
1769 if (!tx.IsCoinBase())
1771 if (!view.HaveInputs(tx))
1772 return state.DoS(100, error("ConnectBlock(): inputs missing/spent"),
1773 REJECT_INVALID, "bad-txns-inputs-missingorspent");
1775 if (fStrictPayToScriptHash)
1777 // Add in sigops done by pay-to-script-hash inputs;
1778 // this is to prevent a "rogue miner" from creating
1779 // an incredibly-expensive-to-validate block.
1780 nSigOps += GetP2SHSigOpCount(tx, view);
1781 if (nSigOps > MAX_BLOCK_SIGOPS)
1782 return state.DoS(100, error("ConnectBlock(): too many sigops"),
1783 REJECT_INVALID, "bad-blk-sigops");
1786 nFees += view.GetValueIn(tx)-tx.GetValueOut();
1788 std::vector<CScriptCheck> vChecks;
1789 if (!CheckInputs(tx, state, view, fScriptChecks, flags, false, nScriptCheckThreads ? &vChecks : NULL))
1790 return false;
1791 control.Add(vChecks);
1794 CTxUndo undoDummy;
1795 if (i > 0) {
1796 blockundo.vtxundo.push_back(CTxUndo());
1798 UpdateCoins(tx, state, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight);
1800 vPos.push_back(std::make_pair(tx.GetHash(), pos));
1801 pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
1803 int64_t nTime1 = GetTimeMicros(); nTimeConnect += nTime1 - nTimeStart;
1804 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);
1806 CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, chainparams.GetConsensus());
1807 if (block.vtx[0].GetValueOut() > blockReward)
1808 return state.DoS(100,
1809 error("ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)",
1810 block.vtx[0].GetValueOut(), blockReward),
1811 REJECT_INVALID, "bad-cb-amount");
1813 if (!control.Wait())
1814 return state.DoS(100, false);
1815 int64_t nTime2 = GetTimeMicros(); nTimeVerify += nTime2 - nTimeStart;
1816 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);
1818 if (fJustCheck)
1819 return true;
1821 // Write undo information to disk
1822 if (pindex->GetUndoPos().IsNull() || !pindex->IsValid(BLOCK_VALID_SCRIPTS))
1824 if (pindex->GetUndoPos().IsNull()) {
1825 CDiskBlockPos pos;
1826 if (!FindUndoPos(state, pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
1827 return error("ConnectBlock(): FindUndoPos failed");
1828 if (!UndoWriteToDisk(blockundo, pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart()))
1829 return AbortNode(state, "Failed to write undo data");
1831 // update nUndoPos in block index
1832 pindex->nUndoPos = pos.nPos;
1833 pindex->nStatus |= BLOCK_HAVE_UNDO;
1836 pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
1837 setDirtyBlockIndex.insert(pindex);
1840 if (fTxIndex)
1841 if (!pblocktree->WriteTxIndex(vPos))
1842 return AbortNode(state, "Failed to write transaction index");
1844 // add this block to the view's block chain
1845 view.SetBestBlock(pindex->GetBlockHash());
1847 int64_t nTime3 = GetTimeMicros(); nTimeIndex += nTime3 - nTime2;
1848 LogPrint("bench", " - Index writing: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeIndex * 0.000001);
1850 // Watch for changes to the previous coinbase transaction.
1851 static uint256 hashPrevBestCoinBase;
1852 GetMainSignals().UpdatedTransaction(hashPrevBestCoinBase);
1853 hashPrevBestCoinBase = block.vtx[0].GetHash();
1855 int64_t nTime4 = GetTimeMicros(); nTimeCallbacks += nTime4 - nTime3;
1856 LogPrint("bench", " - Callbacks: %.2fms [%.2fs]\n", 0.001 * (nTime4 - nTime3), nTimeCallbacks * 0.000001);
1858 return true;
1861 enum FlushStateMode {
1862 FLUSH_STATE_NONE,
1863 FLUSH_STATE_IF_NEEDED,
1864 FLUSH_STATE_PERIODIC,
1865 FLUSH_STATE_ALWAYS
1869 * Update the on-disk chain state.
1870 * The caches and indexes are flushed depending on the mode we're called with
1871 * if they're too large, if it's been a while since the last write,
1872 * or always and in all cases if we're in prune mode and are deleting files.
1874 bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
1875 LOCK2(cs_main, cs_LastBlockFile);
1876 static int64_t nLastWrite = 0;
1877 static int64_t nLastFlush = 0;
1878 static int64_t nLastSetChain = 0;
1879 std::set<int> setFilesToPrune;
1880 bool fFlushForPrune = false;
1881 try {
1882 if (fPruneMode && fCheckForPruning) {
1883 FindFilesToPrune(setFilesToPrune);
1884 fCheckForPruning = false;
1885 if (!setFilesToPrune.empty()) {
1886 fFlushForPrune = true;
1887 if (!fHavePruned) {
1888 pblocktree->WriteFlag("prunedblockfiles", true);
1889 fHavePruned = true;
1893 int64_t nNow = GetTimeMicros();
1894 // Avoid writing/flushing immediately after startup.
1895 if (nLastWrite == 0) {
1896 nLastWrite = nNow;
1898 if (nLastFlush == 0) {
1899 nLastFlush = nNow;
1901 if (nLastSetChain == 0) {
1902 nLastSetChain = nNow;
1904 size_t cacheSize = pcoinsTip->DynamicMemoryUsage();
1905 // The cache is large and close to the limit, but we have time now (not in the middle of a block processing).
1906 bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize * (10.0/9) > nCoinCacheUsage;
1907 // The cache is over the limit, we have to write now.
1908 bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && cacheSize > nCoinCacheUsage;
1909 // 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.
1910 bool fPeriodicWrite = mode == FLUSH_STATE_PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
1911 // It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
1912 bool fPeriodicFlush = mode == FLUSH_STATE_PERIODIC && nNow > nLastFlush + (int64_t)DATABASE_FLUSH_INTERVAL * 1000000;
1913 // Combine all conditions that result in a full cache flush.
1914 bool fDoFullFlush = (mode == FLUSH_STATE_ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
1915 // Write blocks and block index to disk.
1916 if (fDoFullFlush || fPeriodicWrite) {
1917 // Depend on nMinDiskSpace to ensure we can write block index
1918 if (!CheckDiskSpace(0))
1919 return state.Error("out of disk space");
1920 // First make sure all block and undo data is flushed to disk.
1921 FlushBlockFile();
1922 // Then update all block file information (which may refer to block and undo files).
1924 std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
1925 vFiles.reserve(setDirtyFileInfo.size());
1926 for (set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
1927 vFiles.push_back(make_pair(*it, &vinfoBlockFile[*it]));
1928 setDirtyFileInfo.erase(it++);
1930 std::vector<const CBlockIndex*> vBlocks;
1931 vBlocks.reserve(setDirtyBlockIndex.size());
1932 for (set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
1933 vBlocks.push_back(*it);
1934 setDirtyBlockIndex.erase(it++);
1936 if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
1937 return AbortNode(state, "Files to write to block index database");
1940 // Finally remove any pruned files
1941 if (fFlushForPrune)
1942 UnlinkPrunedFiles(setFilesToPrune);
1943 nLastWrite = nNow;
1945 // Flush best chain related state. This can only be done if the blocks / block index write was also done.
1946 if (fDoFullFlush) {
1947 // Typical CCoins structures on disk are around 128 bytes in size.
1948 // Pushing a new one to the database can cause it to be written
1949 // twice (once in the log, and once in the tables). This is already
1950 // an overestimation, as most will delete an existing entry or
1951 // overwrite one. Still, use a conservative safety factor of 2.
1952 if (!CheckDiskSpace(128 * 2 * 2 * pcoinsTip->GetCacheSize()))
1953 return state.Error("out of disk space");
1954 // Flush the chainstate (which may refer to block index entries).
1955 if (!pcoinsTip->Flush())
1956 return AbortNode(state, "Failed to write to coin database");
1957 nLastFlush = nNow;
1959 if ((mode == FLUSH_STATE_ALWAYS || mode == FLUSH_STATE_PERIODIC) && nNow > nLastSetChain + (int64_t)DATABASE_WRITE_INTERVAL * 1000000) {
1960 // Update best block in wallet (so we can detect restored wallets).
1961 GetMainSignals().SetBestChain(chainActive.GetLocator());
1962 nLastSetChain = nNow;
1964 } catch (const std::runtime_error& e) {
1965 return AbortNode(state, std::string("System error while flushing: ") + e.what());
1967 return true;
1970 void FlushStateToDisk() {
1971 CValidationState state;
1972 FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
1975 void PruneAndFlush() {
1976 CValidationState state;
1977 fCheckForPruning = true;
1978 FlushStateToDisk(state, FLUSH_STATE_NONE);
1981 /** Update chainActive and related internal data structures. */
1982 void static UpdateTip(CBlockIndex *pindexNew) {
1983 const CChainParams& chainParams = Params();
1984 chainActive.SetTip(pindexNew);
1986 // New best block
1987 nTimeBestReceived = GetTime();
1988 mempool.AddTransactionsUpdated(1);
1990 LogPrintf("%s: new best=%s height=%d log2_work=%.8g tx=%lu date=%s progress=%f cache=%.1fMiB(%utx)\n", __func__,
1991 chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
1992 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
1993 Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip()), pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
1995 cvBlockChange.notify_all();
1997 // Check the version of the last 100 blocks to see if we need to upgrade:
1998 static bool fWarned = false;
1999 if (!IsInitialBlockDownload() && !fWarned)
2001 int nUpgraded = 0;
2002 const CBlockIndex* pindex = chainActive.Tip();
2003 for (int i = 0; i < 100 && pindex != NULL; i++)
2005 if (pindex->nVersion > CBlock::CURRENT_VERSION)
2006 ++nUpgraded;
2007 pindex = pindex->pprev;
2009 if (nUpgraded > 0)
2010 LogPrintf("%s: %d of last 100 blocks above version %d\n", __func__, nUpgraded, (int)CBlock::CURRENT_VERSION);
2011 if (nUpgraded > 100/2)
2013 // strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
2014 strMiscWarning = _("Warning: This version is obsolete; upgrade required!");
2015 CAlert::Notify(strMiscWarning, true);
2016 fWarned = true;
2021 /** Disconnect chainActive's tip. */
2022 bool static DisconnectTip(CValidationState &state) {
2023 CBlockIndex *pindexDelete = chainActive.Tip();
2024 assert(pindexDelete);
2025 mempool.check(pcoinsTip);
2026 // Read block from disk.
2027 CBlock block;
2028 if (!ReadBlockFromDisk(block, pindexDelete))
2029 return AbortNode(state, "Failed to read block");
2030 // Apply the block atomically to the chain state.
2031 int64_t nStart = GetTimeMicros();
2033 CCoinsViewCache view(pcoinsTip);
2034 if (!DisconnectBlock(block, state, pindexDelete, view))
2035 return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
2036 assert(view.Flush());
2038 LogPrint("bench", "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
2039 // Write the chain state to disk, if necessary.
2040 if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED))
2041 return false;
2042 // Resurrect mempool transactions from the disconnected block.
2043 BOOST_FOREACH(const CTransaction &tx, block.vtx) {
2044 // ignore validation errors in resurrected transactions
2045 list<CTransaction> removed;
2046 CValidationState stateDummy;
2047 if (tx.IsCoinBase() || !AcceptToMemoryPool(mempool, stateDummy, tx, false, NULL))
2048 mempool.remove(tx, removed, true);
2050 mempool.removeCoinbaseSpends(pcoinsTip, pindexDelete->nHeight);
2051 mempool.check(pcoinsTip);
2052 // Update chainActive and related variables.
2053 UpdateTip(pindexDelete->pprev);
2054 // Let wallets know transactions went from 1-confirmed to
2055 // 0-confirmed or conflicted:
2056 BOOST_FOREACH(const CTransaction &tx, block.vtx) {
2057 SyncWithWallets(tx, NULL);
2059 return true;
2062 static int64_t nTimeReadFromDisk = 0;
2063 static int64_t nTimeConnectTotal = 0;
2064 static int64_t nTimeFlush = 0;
2065 static int64_t nTimeChainState = 0;
2066 static int64_t nTimePostConnect = 0;
2069 * Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
2070 * corresponding to pindexNew, to bypass loading it again from disk.
2072 bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock *pblock) {
2073 assert(pindexNew->pprev == chainActive.Tip());
2074 mempool.check(pcoinsTip);
2075 // Read block from disk.
2076 int64_t nTime1 = GetTimeMicros();
2077 CBlock block;
2078 if (!pblock) {
2079 if (!ReadBlockFromDisk(block, pindexNew))
2080 return AbortNode(state, "Failed to read block");
2081 pblock = &block;
2083 // Apply the block atomically to the chain state.
2084 int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1;
2085 int64_t nTime3;
2086 LogPrint("bench", " - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * 0.001, nTimeReadFromDisk * 0.000001);
2088 CCoinsViewCache view(pcoinsTip);
2089 bool rv = ConnectBlock(*pblock, state, pindexNew, view);
2090 GetMainSignals().BlockChecked(*pblock, state);
2091 if (!rv) {
2092 if (state.IsInvalid())
2093 InvalidBlockFound(pindexNew, state);
2094 return error("ConnectTip(): ConnectBlock %s failed", pindexNew->GetBlockHash().ToString());
2096 mapBlockSource.erase(pindexNew->GetBlockHash());
2097 nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2;
2098 LogPrint("bench", " - Connect total: %.2fms [%.2fs]\n", (nTime3 - nTime2) * 0.001, nTimeConnectTotal * 0.000001);
2099 assert(view.Flush());
2101 int64_t nTime4 = GetTimeMicros(); nTimeFlush += nTime4 - nTime3;
2102 LogPrint("bench", " - Flush: %.2fms [%.2fs]\n", (nTime4 - nTime3) * 0.001, nTimeFlush * 0.000001);
2103 // Write the chain state to disk, if necessary.
2104 if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED))
2105 return false;
2106 int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
2107 LogPrint("bench", " - Writing chainstate: %.2fms [%.2fs]\n", (nTime5 - nTime4) * 0.001, nTimeChainState * 0.000001);
2108 // Remove conflicting transactions from the mempool.
2109 list<CTransaction> txConflicted;
2110 mempool.removeForBlock(pblock->vtx, pindexNew->nHeight, txConflicted, !IsInitialBlockDownload());
2111 mempool.check(pcoinsTip);
2112 // Update chainActive & related variables.
2113 UpdateTip(pindexNew);
2114 // Tell wallet about transactions that went from mempool
2115 // to conflicted:
2116 BOOST_FOREACH(const CTransaction &tx, txConflicted) {
2117 SyncWithWallets(tx, NULL);
2119 // ... and about transactions that got confirmed:
2120 BOOST_FOREACH(const CTransaction &tx, pblock->vtx) {
2121 SyncWithWallets(tx, pblock);
2123 // Erase block's transactions from the validation cache
2125 boost::unique_lock<boost::mutex> lock(cs_cacheCheck);
2126 BOOST_FOREACH(const CTransaction &tx, pblock->vtx) {
2127 cacheCheck.erase(tx.GetHash());
2131 int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
2132 LogPrint("bench", " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001);
2133 LogPrint("bench", "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001);
2134 return true;
2138 * Return the tip of the chain with the most work in it, that isn't
2139 * known to be invalid (it's however far from certain to be valid).
2141 static CBlockIndex* FindMostWorkChain() {
2142 do {
2143 CBlockIndex *pindexNew = NULL;
2145 // Find the best candidate header.
2147 std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexCandidates.rbegin();
2148 if (it == setBlockIndexCandidates.rend())
2149 return NULL;
2150 pindexNew = *it;
2153 // Check whether all blocks on the path between the currently active chain and the candidate are valid.
2154 // Just going until the active chain is an optimization, as we know all blocks in it are valid already.
2155 CBlockIndex *pindexTest = pindexNew;
2156 bool fInvalidAncestor = false;
2157 while (pindexTest && !chainActive.Contains(pindexTest)) {
2158 assert(pindexTest->nChainTx || pindexTest->nHeight == 0);
2160 // Pruned nodes may have entries in setBlockIndexCandidates for
2161 // which block files have been deleted. Remove those as candidates
2162 // for the most work chain if we come across them; we can't switch
2163 // to a chain unless we have all the non-active-chain parent blocks.
2164 bool fFailedChain = pindexTest->nStatus & BLOCK_FAILED_MASK;
2165 bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
2166 if (fFailedChain || fMissingData) {
2167 // Candidate chain is not usable (either invalid or missing data)
2168 if (fFailedChain && (pindexBestInvalid == NULL || pindexNew->nChainWork > pindexBestInvalid->nChainWork))
2169 pindexBestInvalid = pindexNew;
2170 CBlockIndex *pindexFailed = pindexNew;
2171 // Remove the entire chain from the set.
2172 while (pindexTest != pindexFailed) {
2173 if (fFailedChain) {
2174 pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
2175 } else if (fMissingData) {
2176 // If we're missing data, then add back to mapBlocksUnlinked,
2177 // so that if the block arrives in the future we can try adding
2178 // to setBlockIndexCandidates again.
2179 mapBlocksUnlinked.insert(std::make_pair(pindexFailed->pprev, pindexFailed));
2181 setBlockIndexCandidates.erase(pindexFailed);
2182 pindexFailed = pindexFailed->pprev;
2184 setBlockIndexCandidates.erase(pindexTest);
2185 fInvalidAncestor = true;
2186 break;
2188 pindexTest = pindexTest->pprev;
2190 if (!fInvalidAncestor)
2191 return pindexNew;
2192 } while(true);
2195 /** Delete all entries in setBlockIndexCandidates that are worse than the current tip. */
2196 static void PruneBlockIndexCandidates() {
2197 // Note that we can't delete the current block itself, as we may need to return to it later in case a
2198 // reorganization to a better block fails.
2199 std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
2200 while (it != setBlockIndexCandidates.end() && setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) {
2201 setBlockIndexCandidates.erase(it++);
2203 // Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates.
2204 assert(!setBlockIndexCandidates.empty());
2208 * Try to make some progress towards making pindexMostWork the active block.
2209 * pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
2211 static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork, CBlock *pblock) {
2212 AssertLockHeld(cs_main);
2213 bool fInvalidFound = false;
2214 const CBlockIndex *pindexOldTip = chainActive.Tip();
2215 const CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork);
2217 // Disconnect active blocks which are no longer in the best chain.
2218 while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
2219 if (!DisconnectTip(state))
2220 return false;
2223 // Build list of new blocks to connect.
2224 std::vector<CBlockIndex*> vpindexToConnect;
2225 bool fContinue = true;
2226 int nHeight = pindexFork ? pindexFork->nHeight : -1;
2227 while (fContinue && nHeight != pindexMostWork->nHeight) {
2228 // Don't iterate the entire list of potential improvements toward the best tip, as we likely only need
2229 // a few blocks along the way.
2230 int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight);
2231 vpindexToConnect.clear();
2232 vpindexToConnect.reserve(nTargetHeight - nHeight);
2233 CBlockIndex *pindexIter = pindexMostWork->GetAncestor(nTargetHeight);
2234 while (pindexIter && pindexIter->nHeight != nHeight) {
2235 vpindexToConnect.push_back(pindexIter);
2236 pindexIter = pindexIter->pprev;
2238 nHeight = nTargetHeight;
2240 // Connect new blocks.
2241 BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) {
2242 if (!ConnectTip(state, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL)) {
2243 if (state.IsInvalid()) {
2244 // The block violates a consensus rule.
2245 if (!state.CorruptionPossible())
2246 InvalidChainFound(vpindexToConnect.back());
2247 state = CValidationState();
2248 fInvalidFound = true;
2249 fContinue = false;
2250 break;
2251 } else {
2252 // A system error occurred (disk space, database error, ...).
2253 return false;
2255 } else {
2256 PruneBlockIndexCandidates();
2257 if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
2258 // We're in a better position than we were. Return temporarily to release the lock.
2259 fContinue = false;
2260 break;
2266 // Callbacks/notifications for a new best chain.
2267 if (fInvalidFound)
2268 CheckForkWarningConditionsOnNewFork(vpindexToConnect.back());
2269 else
2270 CheckForkWarningConditions();
2272 return true;
2276 * Make the best chain active, in multiple steps. The result is either failure
2277 * or an activated best chain. pblock is either NULL or a pointer to a block
2278 * that is already loaded (to avoid loading it again from disk).
2280 bool ActivateBestChain(CValidationState &state, CBlock *pblock) {
2281 CBlockIndex *pindexNewTip = NULL;
2282 CBlockIndex *pindexMostWork = NULL;
2283 const CChainParams& chainParams = Params();
2284 do {
2285 boost::this_thread::interruption_point();
2287 bool fInitialDownload;
2289 LOCK(cs_main);
2290 pindexMostWork = FindMostWorkChain();
2292 // Whether we have anything to do at all.
2293 if (pindexMostWork == NULL || pindexMostWork == chainActive.Tip())
2294 return true;
2296 if (!ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL))
2297 return false;
2299 pindexNewTip = chainActive.Tip();
2300 fInitialDownload = IsInitialBlockDownload();
2302 // When we reach this point, we switched to a new tip (stored in pindexNewTip).
2304 // Notifications/callbacks that can run without cs_main
2305 if (!fInitialDownload) {
2306 uint256 hashNewTip = pindexNewTip->GetBlockHash();
2307 // Relay inventory, but don't relay old inventory during initial block download.
2308 int nBlockEstimate = 0;
2309 if (fCheckpointsEnabled)
2310 nBlockEstimate = Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints());
2311 // Don't relay blocks if pruning -- could cause a peer to try to download, resulting
2312 // in a stalled download if the block file is pruned before the request.
2313 if (nLocalServices & NODE_NETWORK) {
2314 LOCK(cs_vNodes);
2315 BOOST_FOREACH(CNode* pnode, vNodes)
2316 if (chainActive.Height() > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate))
2317 pnode->PushInventory(CInv(MSG_BLOCK, hashNewTip));
2319 // Notify external listeners about the new tip.
2320 uiInterface.NotifyBlockTip(hashNewTip);
2322 } while(pindexMostWork != chainActive.Tip());
2323 CheckBlockIndex();
2325 // Write changes periodically to disk, after relay.
2326 if (!FlushStateToDisk(state, FLUSH_STATE_PERIODIC)) {
2327 return false;
2330 return true;
2333 bool InvalidateBlock(CValidationState& state, CBlockIndex *pindex) {
2334 AssertLockHeld(cs_main);
2336 // Mark the block itself as invalid.
2337 pindex->nStatus |= BLOCK_FAILED_VALID;
2338 setDirtyBlockIndex.insert(pindex);
2339 setBlockIndexCandidates.erase(pindex);
2341 while (chainActive.Contains(pindex)) {
2342 CBlockIndex *pindexWalk = chainActive.Tip();
2343 pindexWalk->nStatus |= BLOCK_FAILED_CHILD;
2344 setDirtyBlockIndex.insert(pindexWalk);
2345 setBlockIndexCandidates.erase(pindexWalk);
2346 // ActivateBestChain considers blocks already in chainActive
2347 // unconditionally valid already, so force disconnect away from it.
2348 if (!DisconnectTip(state)) {
2349 return false;
2353 // The resulting new best tip may not be in setBlockIndexCandidates anymore, so
2354 // add it again.
2355 BlockMap::iterator it = mapBlockIndex.begin();
2356 while (it != mapBlockIndex.end()) {
2357 if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && !setBlockIndexCandidates.value_comp()(it->second, chainActive.Tip())) {
2358 setBlockIndexCandidates.insert(it->second);
2360 it++;
2363 InvalidChainFound(pindex);
2364 return true;
2367 bool ReconsiderBlock(CValidationState& state, CBlockIndex *pindex) {
2368 AssertLockHeld(cs_main);
2370 int nHeight = pindex->nHeight;
2372 // Remove the invalidity flag from this block and all its descendants.
2373 BlockMap::iterator it = mapBlockIndex.begin();
2374 while (it != mapBlockIndex.end()) {
2375 if (!it->second->IsValid() && it->second->GetAncestor(nHeight) == pindex) {
2376 it->second->nStatus &= ~BLOCK_FAILED_MASK;
2377 setDirtyBlockIndex.insert(it->second);
2378 if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && setBlockIndexCandidates.value_comp()(chainActive.Tip(), it->second)) {
2379 setBlockIndexCandidates.insert(it->second);
2381 if (it->second == pindexBestInvalid) {
2382 // Reset invalid block marker if it was pointing to one of those.
2383 pindexBestInvalid = NULL;
2386 it++;
2389 // Remove the invalidity flag from all ancestors too.
2390 while (pindex != NULL) {
2391 if (pindex->nStatus & BLOCK_FAILED_MASK) {
2392 pindex->nStatus &= ~BLOCK_FAILED_MASK;
2393 setDirtyBlockIndex.insert(pindex);
2395 pindex = pindex->pprev;
2397 return true;
2400 CBlockIndex* AddToBlockIndex(const CBlockHeader& block)
2402 // Check for duplicate
2403 uint256 hash = block.GetHash();
2404 BlockMap::iterator it = mapBlockIndex.find(hash);
2405 if (it != mapBlockIndex.end())
2406 return it->second;
2408 // Construct new block index object
2409 CBlockIndex* pindexNew = new CBlockIndex(block);
2410 assert(pindexNew);
2411 // We assign the sequence id to blocks only when the full data is available,
2412 // to avoid miners withholding blocks but broadcasting headers, to get a
2413 // competitive advantage.
2414 pindexNew->nSequenceId = 0;
2415 BlockMap::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
2416 pindexNew->phashBlock = &((*mi).first);
2417 BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock);
2418 if (miPrev != mapBlockIndex.end())
2420 pindexNew->pprev = (*miPrev).second;
2421 pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
2422 pindexNew->BuildSkip();
2424 pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew);
2425 pindexNew->RaiseValidity(BLOCK_VALID_TREE);
2426 if (pindexBestHeader == NULL || pindexBestHeader->nChainWork < pindexNew->nChainWork)
2427 pindexBestHeader = pindexNew;
2429 setDirtyBlockIndex.insert(pindexNew);
2431 return pindexNew;
2434 /** Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */
2435 bool ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBlockIndex *pindexNew, const CDiskBlockPos& pos)
2437 pindexNew->nTx = block.vtx.size();
2438 pindexNew->nChainTx = 0;
2439 pindexNew->nFile = pos.nFile;
2440 pindexNew->nDataPos = pos.nPos;
2441 pindexNew->nUndoPos = 0;
2442 pindexNew->nStatus |= BLOCK_HAVE_DATA;
2443 pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
2444 setDirtyBlockIndex.insert(pindexNew);
2446 if (pindexNew->pprev == NULL || pindexNew->pprev->nChainTx) {
2447 // If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
2448 deque<CBlockIndex*> queue;
2449 queue.push_back(pindexNew);
2451 // Recursively process any descendant blocks that now may be eligible to be connected.
2452 while (!queue.empty()) {
2453 CBlockIndex *pindex = queue.front();
2454 queue.pop_front();
2455 pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
2457 LOCK(cs_nBlockSequenceId);
2458 pindex->nSequenceId = nBlockSequenceId++;
2460 if (chainActive.Tip() == NULL || !setBlockIndexCandidates.value_comp()(pindex, chainActive.Tip())) {
2461 setBlockIndexCandidates.insert(pindex);
2463 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex);
2464 while (range.first != range.second) {
2465 std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
2466 queue.push_back(it->second);
2467 range.first++;
2468 mapBlocksUnlinked.erase(it);
2471 } else {
2472 if (pindexNew->pprev && pindexNew->pprev->IsValid(BLOCK_VALID_TREE)) {
2473 mapBlocksUnlinked.insert(std::make_pair(pindexNew->pprev, pindexNew));
2477 return true;
2480 bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
2482 LOCK(cs_LastBlockFile);
2484 unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile;
2485 if (vinfoBlockFile.size() <= nFile) {
2486 vinfoBlockFile.resize(nFile + 1);
2489 if (!fKnown) {
2490 while (vinfoBlockFile[nFile].nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
2491 LogPrintf("Leaving block file %i: %s\n", nFile, vinfoBlockFile[nFile].ToString());
2492 FlushBlockFile(true);
2493 nFile++;
2494 if (vinfoBlockFile.size() <= nFile) {
2495 vinfoBlockFile.resize(nFile + 1);
2498 pos.nFile = nFile;
2499 pos.nPos = vinfoBlockFile[nFile].nSize;
2502 nLastBlockFile = nFile;
2503 vinfoBlockFile[nFile].AddBlock(nHeight, nTime);
2504 if (fKnown)
2505 vinfoBlockFile[nFile].nSize = std::max(pos.nPos + nAddSize, vinfoBlockFile[nFile].nSize);
2506 else
2507 vinfoBlockFile[nFile].nSize += nAddSize;
2509 if (!fKnown) {
2510 unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
2511 unsigned int nNewChunks = (vinfoBlockFile[nFile].nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
2512 if (nNewChunks > nOldChunks) {
2513 if (fPruneMode)
2514 fCheckForPruning = true;
2515 if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
2516 FILE *file = OpenBlockFile(pos);
2517 if (file) {
2518 LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
2519 AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
2520 fclose(file);
2523 else
2524 return state.Error("out of disk space");
2528 setDirtyFileInfo.insert(nFile);
2529 return true;
2532 bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
2534 pos.nFile = nFile;
2536 LOCK(cs_LastBlockFile);
2538 unsigned int nNewSize;
2539 pos.nPos = vinfoBlockFile[nFile].nUndoSize;
2540 nNewSize = vinfoBlockFile[nFile].nUndoSize += nAddSize;
2541 setDirtyFileInfo.insert(nFile);
2543 unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
2544 unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
2545 if (nNewChunks > nOldChunks) {
2546 if (fPruneMode)
2547 fCheckForPruning = true;
2548 if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
2549 FILE *file = OpenUndoFile(pos);
2550 if (file) {
2551 LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
2552 AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
2553 fclose(file);
2556 else
2557 return state.Error("out of disk space");
2560 return true;
2563 bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, bool fCheckPOW)
2565 // Check proof of work matches claimed amount
2566 if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, Params().GetConsensus()))
2567 return state.DoS(50, error("CheckBlockHeader(): proof of work failed"),
2568 REJECT_INVALID, "high-hash");
2570 // Check timestamp
2571 if (block.GetBlockTime() > GetAdjustedTime() + 2 * 60 * 60)
2572 return state.Invalid(error("CheckBlockHeader(): block timestamp too far in the future"),
2573 REJECT_INVALID, "time-too-new");
2575 return true;
2578 bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bool fCheckMerkleRoot)
2580 // These are checks that are independent of context.
2582 // Check that the header is valid (particularly PoW). This is mostly
2583 // redundant with the call in AcceptBlockHeader.
2584 if (!CheckBlockHeader(block, state, fCheckPOW))
2585 return false;
2587 // Check the merkle root.
2588 if (fCheckMerkleRoot) {
2589 bool mutated;
2590 uint256 hashMerkleRoot2 = block.BuildMerkleTree(&mutated);
2591 if (block.hashMerkleRoot != hashMerkleRoot2)
2592 return state.DoS(100, error("CheckBlock(): hashMerkleRoot mismatch"),
2593 REJECT_INVALID, "bad-txnmrklroot", true);
2595 // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
2596 // of transactions in a block without affecting the merkle root of a block,
2597 // while still invalidating it.
2598 if (mutated)
2599 return state.DoS(100, error("CheckBlock(): duplicate transaction"),
2600 REJECT_INVALID, "bad-txns-duplicate", true);
2603 // All potential-corruption validation must be done before we do any
2604 // transaction validation, as otherwise we may mark the header as invalid
2605 // because we receive the wrong transactions for it.
2607 // Size limits
2608 if (block.vtx.empty() || block.vtx.size() > MAX_BLOCK_SIZE || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION) > MAX_BLOCK_SIZE)
2609 return state.DoS(100, error("CheckBlock(): size limits failed"),
2610 REJECT_INVALID, "bad-blk-length");
2612 // First transaction must be coinbase, the rest must not be
2613 if (block.vtx.empty() || !block.vtx[0].IsCoinBase())
2614 return state.DoS(100, error("CheckBlock(): first tx is not coinbase"),
2615 REJECT_INVALID, "bad-cb-missing");
2616 for (unsigned int i = 1; i < block.vtx.size(); i++)
2617 if (block.vtx[i].IsCoinBase())
2618 return state.DoS(100, error("CheckBlock(): more than one coinbase"),
2619 REJECT_INVALID, "bad-cb-multiple");
2621 // Check transactions
2622 BOOST_FOREACH(const CTransaction& tx, block.vtx)
2623 if (!CheckTransaction(tx, state))
2624 return error("CheckBlock(): CheckTransaction failed");
2626 unsigned int nSigOps = 0;
2627 BOOST_FOREACH(const CTransaction& tx, block.vtx)
2629 nSigOps += GetLegacySigOpCount(tx);
2631 if (nSigOps > MAX_BLOCK_SIGOPS)
2632 return state.DoS(100, error("CheckBlock(): out-of-bounds SigOpCount"),
2633 REJECT_INVALID, "bad-blk-sigops", true);
2635 return true;
2638 static bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidationState& state, const CChainParams& chainparams, const uint256& hash)
2640 if (*pindexPrev->phashBlock == chainparams.GetConsensus().hashGenesisBlock)
2641 return true;
2643 int nHeight = pindexPrev->nHeight+1;
2644 // Don't accept any forks from the main chain prior to last checkpoint
2645 CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainparams.Checkpoints());
2646 if (pcheckpoint && nHeight < pcheckpoint->nHeight)
2647 return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight));
2649 return true;
2652 bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex * const pindexPrev)
2654 const Consensus::Params& consensusParams = Params().GetConsensus();
2655 // Check proof of work
2656 if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
2657 return state.DoS(100, error("%s: incorrect proof of work", __func__),
2658 REJECT_INVALID, "bad-diffbits");
2660 // Check timestamp against prev
2661 if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
2662 return state.Invalid(error("%s: block's timestamp is too early", __func__),
2663 REJECT_INVALID, "time-too-old");
2665 // Reject block.nVersion=1 blocks when 95% (75% on testnet) of the network has upgraded:
2666 if (block.nVersion < 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
2667 return state.Invalid(error("%s: rejected nVersion=1 block", __func__),
2668 REJECT_OBSOLETE, "bad-version");
2670 // Reject block.nVersion=2 blocks when 95% (75% on testnet) of the network has upgraded:
2671 if (block.nVersion < 3 && IsSuperMajority(3, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
2672 return state.Invalid(error("%s : rejected nVersion=2 block", __func__),
2673 REJECT_OBSOLETE, "bad-version");
2675 return true;
2678 bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex * const pindexPrev)
2680 const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
2681 const Consensus::Params& consensusParams = Params().GetConsensus();
2683 // Check that all transactions are finalized
2684 BOOST_FOREACH(const CTransaction& tx, block.vtx)
2685 if (!IsFinalTx(tx, nHeight, block.GetBlockTime())) {
2686 return state.DoS(10, error("%s: contains a non-final transaction", __func__), REJECT_INVALID, "bad-txns-nonfinal");
2689 // Enforce block.nVersion=2 rule that the coinbase starts with serialized block height
2690 // if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet):
2691 if (block.nVersion >= 2 && IsSuperMajority(2, pindexPrev, consensusParams.nMajorityEnforceBlockUpgrade, consensusParams))
2693 CScript expect = CScript() << nHeight;
2694 if (block.vtx[0].vin[0].scriptSig.size() < expect.size() ||
2695 !std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin())) {
2696 return state.DoS(100, error("%s: block height mismatch in coinbase", __func__), REJECT_INVALID, "bad-cb-height");
2700 return true;
2703 bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex** ppindex)
2705 const CChainParams& chainparams = Params();
2706 AssertLockHeld(cs_main);
2707 // Check for duplicate
2708 uint256 hash = block.GetHash();
2709 BlockMap::iterator miSelf = mapBlockIndex.find(hash);
2710 CBlockIndex *pindex = NULL;
2711 if (hash != chainparams.GetConsensus().hashGenesisBlock) {
2713 if (miSelf != mapBlockIndex.end()) {
2714 // Block header is already known.
2715 pindex = miSelf->second;
2716 if (ppindex)
2717 *ppindex = pindex;
2718 if (pindex->nStatus & BLOCK_FAILED_MASK)
2719 return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate");
2720 return true;
2723 if (!CheckBlockHeader(block, state))
2724 return false;
2726 // Get prev block index
2727 CBlockIndex* pindexPrev = NULL;
2728 BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
2729 if (mi == mapBlockIndex.end())
2730 return state.DoS(10, error("%s: prev block not found", __func__), 0, "bad-prevblk");
2731 pindexPrev = (*mi).second;
2732 if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
2733 return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
2735 assert(pindexPrev);
2736 if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, hash))
2737 return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
2739 if (!ContextualCheckBlockHeader(block, state, pindexPrev))
2740 return false;
2742 if (pindex == NULL)
2743 pindex = AddToBlockIndex(block);
2745 if (ppindex)
2746 *ppindex = pindex;
2748 return true;
2751 bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, bool fRequested, CDiskBlockPos* dbp)
2753 const CChainParams& chainparams = Params();
2754 AssertLockHeld(cs_main);
2756 CBlockIndex *&pindex = *ppindex;
2758 if (!AcceptBlockHeader(block, state, &pindex))
2759 return false;
2761 // Try to process all requested blocks that we don't have, but only
2762 // process an unrequested block if it's new and has enough work to
2763 // advance our tip.
2764 bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA;
2765 bool fHasMoreWork = (chainActive.Tip() ? pindex->nChainWork > chainActive.Tip()->nChainWork : true);
2767 // TODO: deal better with return value and error conditions for duplicate
2768 // and unrequested blocks.
2769 if (fAlreadyHave) return true;
2770 if (!fRequested) { // If we didn't ask for it:
2771 if (pindex->nTx != 0) return true; // This is a previously-processed block that was pruned
2772 if (!fHasMoreWork) return true; // Don't process less-work chains
2775 if ((!CheckBlock(block, state)) || !ContextualCheckBlock(block, state, pindex->pprev)) {
2776 if (state.IsInvalid() && !state.CorruptionPossible()) {
2777 pindex->nStatus |= BLOCK_FAILED_VALID;
2778 setDirtyBlockIndex.insert(pindex);
2780 return false;
2783 int nHeight = pindex->nHeight;
2785 // Write block to history file
2786 try {
2787 unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
2788 CDiskBlockPos blockPos;
2789 if (dbp != NULL)
2790 blockPos = *dbp;
2791 if (!FindBlockPos(state, blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != NULL))
2792 return error("AcceptBlock(): FindBlockPos failed");
2793 if (dbp == NULL)
2794 if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
2795 AbortNode(state, "Failed to write block");
2796 if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
2797 return error("AcceptBlock(): ReceivedBlockTransactions failed");
2798 } catch (const std::runtime_error& e) {
2799 return AbortNode(state, std::string("System error: ") + e.what());
2802 if (fCheckForPruning)
2803 FlushStateToDisk(state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
2805 return true;
2808 static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams)
2810 unsigned int nFound = 0;
2811 for (int i = 0; i < consensusParams.nMajorityWindow && nFound < nRequired && pstart != NULL; i++)
2813 if (pstart->nVersion >= minVersion)
2814 ++nFound;
2815 pstart = pstart->pprev;
2817 return (nFound >= nRequired);
2821 bool ProcessNewBlock(CValidationState &state, CNode* pfrom, CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp)
2823 // Preliminary checks
2824 bool checked = CheckBlock(*pblock, state);
2827 LOCK(cs_main);
2828 bool fRequested = MarkBlockAsReceived(pblock->GetHash());
2829 fRequested |= fForceProcessing;
2830 if (!checked) {
2831 return error("%s: CheckBlock FAILED", __func__);
2834 // Store to disk
2835 CBlockIndex *pindex = NULL;
2836 bool ret = AcceptBlock(*pblock, state, &pindex, fRequested, dbp);
2837 if (pindex && pfrom) {
2838 mapBlockSource[pindex->GetBlockHash()] = pfrom->GetId();
2840 CheckBlockIndex();
2841 if (!ret)
2842 return error("%s: AcceptBlock FAILED", __func__);
2845 if (!ActivateBestChain(state, pblock))
2846 return error("%s: ActivateBestChain failed", __func__);
2848 return true;
2851 bool TestBlockValidity(CValidationState &state, const CBlock& block, CBlockIndex * const pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
2853 const CChainParams& chainparams = Params();
2854 AssertLockHeld(cs_main);
2855 assert(pindexPrev && pindexPrev == chainActive.Tip());
2856 if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, block.GetHash()))
2857 return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
2859 CCoinsViewCache viewNew(pcoinsTip);
2860 CBlockIndex indexDummy(block);
2861 indexDummy.pprev = pindexPrev;
2862 indexDummy.nHeight = pindexPrev->nHeight + 1;
2864 // NOTE: CheckBlockHeader is called by CheckBlock
2865 if (!ContextualCheckBlockHeader(block, state, pindexPrev))
2866 return false;
2867 if (!CheckBlock(block, state, fCheckPOW, fCheckMerkleRoot))
2868 return false;
2869 if (!ContextualCheckBlock(block, state, pindexPrev))
2870 return false;
2871 if (!ConnectBlock(block, state, &indexDummy, viewNew, true))
2872 return false;
2873 assert(state.IsValid());
2875 return true;
2879 * BLOCK PRUNING CODE
2882 /* Calculate the amount of disk space the block & undo files currently use */
2883 uint64_t CalculateCurrentUsage()
2885 uint64_t retval = 0;
2886 BOOST_FOREACH(const CBlockFileInfo &file, vinfoBlockFile) {
2887 retval += file.nSize + file.nUndoSize;
2889 return retval;
2892 /* Prune a block file (modify associated database entries)*/
2893 void PruneOneBlockFile(const int fileNumber)
2895 for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) {
2896 CBlockIndex* pindex = it->second;
2897 if (pindex->nFile == fileNumber) {
2898 pindex->nStatus &= ~BLOCK_HAVE_DATA;
2899 pindex->nStatus &= ~BLOCK_HAVE_UNDO;
2900 pindex->nFile = 0;
2901 pindex->nDataPos = 0;
2902 pindex->nUndoPos = 0;
2903 setDirtyBlockIndex.insert(pindex);
2905 // Prune from mapBlocksUnlinked -- any block we prune would have
2906 // to be downloaded again in order to consider its chain, at which
2907 // point it would be considered as a candidate for
2908 // mapBlocksUnlinked or setBlockIndexCandidates.
2909 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex->pprev);
2910 while (range.first != range.second) {
2911 std::multimap<CBlockIndex *, CBlockIndex *>::iterator it = range.first;
2912 range.first++;
2913 if (it->second == pindex) {
2914 mapBlocksUnlinked.erase(it);
2920 vinfoBlockFile[fileNumber].SetNull();
2921 setDirtyFileInfo.insert(fileNumber);
2925 void UnlinkPrunedFiles(std::set<int>& setFilesToPrune)
2927 for (set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
2928 CDiskBlockPos pos(*it, 0);
2929 boost::filesystem::remove(GetBlockPosFilename(pos, "blk"));
2930 boost::filesystem::remove(GetBlockPosFilename(pos, "rev"));
2931 LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
2935 /* Calculate the block/rev files that should be deleted to remain under target*/
2936 void FindFilesToPrune(std::set<int>& setFilesToPrune)
2938 LOCK2(cs_main, cs_LastBlockFile);
2939 if (chainActive.Tip() == NULL || nPruneTarget == 0) {
2940 return;
2942 if (chainActive.Tip()->nHeight <= Params().PruneAfterHeight()) {
2943 return;
2946 unsigned int nLastBlockWeCanPrune = chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP;
2947 uint64_t nCurrentUsage = CalculateCurrentUsage();
2948 // We don't check to prune until after we've allocated new space for files
2949 // So we should leave a buffer under our target to account for another allocation
2950 // before the next pruning.
2951 uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
2952 uint64_t nBytesToPrune;
2953 int count=0;
2955 if (nCurrentUsage + nBuffer >= nPruneTarget) {
2956 for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
2957 nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize;
2959 if (vinfoBlockFile[fileNumber].nSize == 0)
2960 continue;
2962 if (nCurrentUsage + nBuffer < nPruneTarget) // are we below our target?
2963 break;
2965 // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
2966 if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
2967 continue;
2969 PruneOneBlockFile(fileNumber);
2970 // Queue up the files for removal
2971 setFilesToPrune.insert(fileNumber);
2972 nCurrentUsage -= nBytesToPrune;
2973 count++;
2977 LogPrint("prune", "Prune: target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n",
2978 nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
2979 ((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
2980 nLastBlockWeCanPrune, count);
2983 bool CheckDiskSpace(uint64_t nAdditionalBytes)
2985 uint64_t nFreeBytesAvailable = boost::filesystem::space(GetDataDir()).available;
2987 // Check for nMinDiskSpace bytes (currently 50MB)
2988 if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
2989 return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
2991 return true;
2994 FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
2996 if (pos.IsNull())
2997 return NULL;
2998 boost::filesystem::path path = GetBlockPosFilename(pos, prefix);
2999 boost::filesystem::create_directories(path.parent_path());
3000 FILE* file = fopen(path.string().c_str(), "rb+");
3001 if (!file && !fReadOnly)
3002 file = fopen(path.string().c_str(), "wb+");
3003 if (!file) {
3004 LogPrintf("Unable to open file %s\n", path.string());
3005 return NULL;
3007 if (pos.nPos) {
3008 if (fseek(file, pos.nPos, SEEK_SET)) {
3009 LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
3010 fclose(file);
3011 return NULL;
3014 return file;
3017 FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
3018 return OpenDiskFile(pos, "blk", fReadOnly);
3021 FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
3022 return OpenDiskFile(pos, "rev", fReadOnly);
3025 boost::filesystem::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
3027 return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
3030 CBlockIndex * InsertBlockIndex(uint256 hash)
3032 if (hash.IsNull())
3033 return NULL;
3035 // Return existing
3036 BlockMap::iterator mi = mapBlockIndex.find(hash);
3037 if (mi != mapBlockIndex.end())
3038 return (*mi).second;
3040 // Create new
3041 CBlockIndex* pindexNew = new CBlockIndex();
3042 if (!pindexNew)
3043 throw runtime_error("LoadBlockIndex(): new CBlockIndex failed");
3044 mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
3045 pindexNew->phashBlock = &((*mi).first);
3047 return pindexNew;
3050 bool static LoadBlockIndexDB()
3052 const CChainParams& chainparams = Params();
3053 if (!pblocktree->LoadBlockIndexGuts())
3054 return false;
3056 boost::this_thread::interruption_point();
3058 // Calculate nChainWork
3059 vector<pair<int, CBlockIndex*> > vSortedByHeight;
3060 vSortedByHeight.reserve(mapBlockIndex.size());
3061 BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
3063 CBlockIndex* pindex = item.second;
3064 vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
3066 sort(vSortedByHeight.begin(), vSortedByHeight.end());
3067 BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
3069 CBlockIndex* pindex = item.second;
3070 pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
3071 // We can link the chain of blocks for which we've received transactions at some point.
3072 // Pruned nodes may have deleted the block.
3073 if (pindex->nTx > 0) {
3074 if (pindex->pprev) {
3075 if (pindex->pprev->nChainTx) {
3076 pindex->nChainTx = pindex->pprev->nChainTx + pindex->nTx;
3077 } else {
3078 pindex->nChainTx = 0;
3079 mapBlocksUnlinked.insert(std::make_pair(pindex->pprev, pindex));
3081 } else {
3082 pindex->nChainTx = pindex->nTx;
3085 if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->nChainTx || pindex->pprev == NULL))
3086 setBlockIndexCandidates.insert(pindex);
3087 if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
3088 pindexBestInvalid = pindex;
3089 if (pindex->pprev)
3090 pindex->BuildSkip();
3091 if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == NULL || CBlockIndexWorkComparator()(pindexBestHeader, pindex)))
3092 pindexBestHeader = pindex;
3095 // Load block file info
3096 pblocktree->ReadLastBlockFile(nLastBlockFile);
3097 vinfoBlockFile.resize(nLastBlockFile + 1);
3098 LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile);
3099 for (int nFile = 0; nFile <= nLastBlockFile; nFile++) {
3100 pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
3102 LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString());
3103 for (int nFile = nLastBlockFile + 1; true; nFile++) {
3104 CBlockFileInfo info;
3105 if (pblocktree->ReadBlockFileInfo(nFile, info)) {
3106 vinfoBlockFile.push_back(info);
3107 } else {
3108 break;
3112 // Check presence of blk files
3113 LogPrintf("Checking all blk files are present...\n");
3114 set<int> setBlkDataFiles;
3115 BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
3117 CBlockIndex* pindex = item.second;
3118 if (pindex->nStatus & BLOCK_HAVE_DATA) {
3119 setBlkDataFiles.insert(pindex->nFile);
3122 for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++)
3124 CDiskBlockPos pos(*it, 0);
3125 if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) {
3126 return false;
3130 // Check whether we have ever pruned block & undo files
3131 pblocktree->ReadFlag("prunedblockfiles", fHavePruned);
3132 if (fHavePruned)
3133 LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n");
3135 // Check whether we need to continue reindexing
3136 bool fReindexing = false;
3137 pblocktree->ReadReindexing(fReindexing);
3138 fReindex |= fReindexing;
3140 // Check whether we have a transaction index
3141 pblocktree->ReadFlag("txindex", fTxIndex);
3142 LogPrintf("%s: transaction index %s\n", __func__, fTxIndex ? "enabled" : "disabled");
3144 // Load pointer to end of best chain
3145 BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
3146 if (it == mapBlockIndex.end())
3147 return true;
3148 chainActive.SetTip(it->second);
3150 PruneBlockIndexCandidates();
3152 LogPrintf("%s: hashBestChain=%s height=%d date=%s progress=%f\n", __func__,
3153 chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
3154 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
3155 Checkpoints::GuessVerificationProgress(chainparams.Checkpoints(), chainActive.Tip()));
3157 return true;
3160 CVerifyDB::CVerifyDB()
3162 uiInterface.ShowProgress(_("Verifying blocks..."), 0);
3165 CVerifyDB::~CVerifyDB()
3167 uiInterface.ShowProgress("", 100);
3170 bool CVerifyDB::VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
3172 LOCK(cs_main);
3173 if (chainActive.Tip() == NULL || chainActive.Tip()->pprev == NULL)
3174 return true;
3176 // Verify blocks in the best chain
3177 if (nCheckDepth <= 0)
3178 nCheckDepth = 1000000000; // suffices until the year 19000
3179 if (nCheckDepth > chainActive.Height())
3180 nCheckDepth = chainActive.Height();
3181 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
3182 LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
3183 CCoinsViewCache coins(coinsview);
3184 CBlockIndex* pindexState = chainActive.Tip();
3185 CBlockIndex* pindexFailure = NULL;
3186 int nGoodTransactions = 0;
3187 CValidationState state;
3188 for (CBlockIndex* pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev)
3190 boost::this_thread::interruption_point();
3191 uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100)))));
3192 if (pindex->nHeight < chainActive.Height()-nCheckDepth)
3193 break;
3194 CBlock block;
3195 // check level 0: read from disk
3196 if (!ReadBlockFromDisk(block, pindex))
3197 return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3198 // check level 1: verify block validity
3199 if (nCheckLevel >= 1 && !CheckBlock(block, state))
3200 return error("VerifyDB(): *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
3201 // check level 2: verify undo validity
3202 if (nCheckLevel >= 2 && pindex) {
3203 CBlockUndo undo;
3204 CDiskBlockPos pos = pindex->GetUndoPos();
3205 if (!pos.IsNull()) {
3206 if (!UndoReadFromDisk(undo, pos, pindex->pprev->GetBlockHash()))
3207 return error("VerifyDB(): *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
3210 // check level 3: check for inconsistencies during memory-only disconnect of tip blocks
3211 if (nCheckLevel >= 3 && pindex == pindexState && (coins.DynamicMemoryUsage() + pcoinsTip->DynamicMemoryUsage()) <= nCoinCacheUsage) {
3212 bool fClean = true;
3213 if (!DisconnectBlock(block, state, pindex, coins, &fClean))
3214 return error("VerifyDB(): *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3215 pindexState = pindex->pprev;
3216 if (!fClean) {
3217 nGoodTransactions = 0;
3218 pindexFailure = pindex;
3219 } else
3220 nGoodTransactions += block.vtx.size();
3222 if (ShutdownRequested())
3223 return true;
3225 if (pindexFailure)
3226 return error("VerifyDB(): *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainActive.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
3228 // check level 4: try reconnecting blocks
3229 if (nCheckLevel >= 4) {
3230 CBlockIndex *pindex = pindexState;
3231 while (pindex != chainActive.Tip()) {
3232 boost::this_thread::interruption_point();
3233 uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, 100 - (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * 50))));
3234 pindex = chainActive.Next(pindex);
3235 CBlock block;
3236 if (!ReadBlockFromDisk(block, pindex))
3237 return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3238 if (!ConnectBlock(block, state, pindex, coins))
3239 return error("VerifyDB(): *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
3243 LogPrintf("No coin database inconsistencies in last %i blocks (%i transactions)\n", chainActive.Height() - pindexState->nHeight, nGoodTransactions);
3245 return true;
3248 void UnloadBlockIndex()
3250 LOCK(cs_main);
3251 setBlockIndexCandidates.clear();
3252 chainActive.SetTip(NULL);
3253 pindexBestInvalid = NULL;
3254 pindexBestHeader = NULL;
3255 mempool.clear();
3256 mapOrphanTransactions.clear();
3257 mapOrphanTransactionsByPrev.clear();
3258 nSyncStarted = 0;
3259 mapBlocksUnlinked.clear();
3260 vinfoBlockFile.clear();
3261 nLastBlockFile = 0;
3262 nBlockSequenceId = 1;
3263 mapBlockSource.clear();
3264 mapBlocksInFlight.clear();
3265 nQueuedValidatedHeaders = 0;
3266 nPreferredDownload = 0;
3267 setDirtyBlockIndex.clear();
3268 setDirtyFileInfo.clear();
3269 mapNodeState.clear();
3271 BOOST_FOREACH(BlockMap::value_type& entry, mapBlockIndex) {
3272 delete entry.second;
3274 mapBlockIndex.clear();
3275 fHavePruned = false;
3278 bool LoadBlockIndex()
3280 // Load block index from databases
3281 if (!fReindex && !LoadBlockIndexDB())
3282 return false;
3283 return true;
3287 bool InitBlockIndex() {
3288 const CChainParams& chainparams = Params();
3289 LOCK(cs_main);
3290 // Check whether we're already initialized
3291 if (chainActive.Genesis() != NULL)
3292 return true;
3294 // Use the provided setting for -txindex in the new database
3295 fTxIndex = GetBoolArg("-txindex", false);
3296 pblocktree->WriteFlag("txindex", fTxIndex);
3297 LogPrintf("Initializing databases...\n");
3299 // Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
3300 if (!fReindex) {
3301 try {
3302 CBlock &block = const_cast<CBlock&>(Params().GenesisBlock());
3303 // Start new block file
3304 unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
3305 CDiskBlockPos blockPos;
3306 CValidationState state;
3307 if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.GetBlockTime()))
3308 return error("LoadBlockIndex(): FindBlockPos failed");
3309 if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
3310 return error("LoadBlockIndex(): writing genesis block to disk failed");
3311 CBlockIndex *pindex = AddToBlockIndex(block);
3312 if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
3313 return error("LoadBlockIndex(): genesis block not accepted");
3314 if (!ActivateBestChain(state, &block))
3315 return error("LoadBlockIndex(): genesis block cannot be activated");
3316 // Force a chainstate write so that when we VerifyDB in a moment, it doesn't check stale data
3317 return FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
3318 } catch (const std::runtime_error& e) {
3319 return error("LoadBlockIndex(): failed to initialize block database: %s", e.what());
3323 return true;
3328 bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
3330 const CChainParams& chainparams = Params();
3331 // Map of disk positions for blocks with unknown parent (only used for reindex)
3332 static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
3333 int64_t nStart = GetTimeMillis();
3335 int nLoaded = 0;
3336 try {
3337 // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
3338 CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SIZE, MAX_BLOCK_SIZE+8, SER_DISK, CLIENT_VERSION);
3339 uint64_t nRewind = blkdat.GetPos();
3340 while (!blkdat.eof()) {
3341 boost::this_thread::interruption_point();
3343 blkdat.SetPos(nRewind);
3344 nRewind++; // start one byte further next time, in case of failure
3345 blkdat.SetLimit(); // remove former limit
3346 unsigned int nSize = 0;
3347 try {
3348 // locate a header
3349 unsigned char buf[MESSAGE_START_SIZE];
3350 blkdat.FindByte(Params().MessageStart()[0]);
3351 nRewind = blkdat.GetPos()+1;
3352 blkdat >> FLATDATA(buf);
3353 if (memcmp(buf, Params().MessageStart(), MESSAGE_START_SIZE))
3354 continue;
3355 // read size
3356 blkdat >> nSize;
3357 if (nSize < 80 || nSize > MAX_BLOCK_SIZE)
3358 continue;
3359 } catch (const std::exception&) {
3360 // no valid block header found; don't complain
3361 break;
3363 try {
3364 // read block
3365 uint64_t nBlockPos = blkdat.GetPos();
3366 if (dbp)
3367 dbp->nPos = nBlockPos;
3368 blkdat.SetLimit(nBlockPos + nSize);
3369 blkdat.SetPos(nBlockPos);
3370 CBlock block;
3371 blkdat >> block;
3372 nRewind = blkdat.GetPos();
3374 // detect out of order blocks, and store them for later
3375 uint256 hash = block.GetHash();
3376 if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex.find(block.hashPrevBlock) == mapBlockIndex.end()) {
3377 LogPrint("reindex", "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
3378 block.hashPrevBlock.ToString());
3379 if (dbp)
3380 mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
3381 continue;
3384 // process in case the block isn't known yet
3385 if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
3386 CValidationState state;
3387 if (ProcessNewBlock(state, NULL, &block, true, dbp))
3388 nLoaded++;
3389 if (state.IsError())
3390 break;
3391 } else if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex[hash]->nHeight % 1000 == 0) {
3392 LogPrintf("Block Import: already had block %s at height %d\n", hash.ToString(), mapBlockIndex[hash]->nHeight);
3395 // Recursively process earlier encountered successors of this block
3396 deque<uint256> queue;
3397 queue.push_back(hash);
3398 while (!queue.empty()) {
3399 uint256 head = queue.front();
3400 queue.pop_front();
3401 std::pair<std::multimap<uint256, CDiskBlockPos>::iterator, std::multimap<uint256, CDiskBlockPos>::iterator> range = mapBlocksUnknownParent.equal_range(head);
3402 while (range.first != range.second) {
3403 std::multimap<uint256, CDiskBlockPos>::iterator it = range.first;
3404 if (ReadBlockFromDisk(block, it->second))
3406 LogPrintf("%s: Processing out of order child %s of %s\n", __func__, block.GetHash().ToString(),
3407 head.ToString());
3408 CValidationState dummy;
3409 if (ProcessNewBlock(dummy, NULL, &block, true, &it->second))
3411 nLoaded++;
3412 queue.push_back(block.GetHash());
3415 range.first++;
3416 mapBlocksUnknownParent.erase(it);
3419 } catch (const std::exception& e) {
3420 LogPrintf("%s: Deserialize or I/O error - %s", __func__, e.what());
3423 } catch (const std::runtime_error& e) {
3424 AbortNode(std::string("System error: ") + e.what());
3426 if (nLoaded > 0)
3427 LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded, GetTimeMillis() - nStart);
3428 return nLoaded > 0;
3431 void static CheckBlockIndex()
3433 const Consensus::Params& consensusParams = Params().GetConsensus();
3434 if (!fCheckBlockIndex) {
3435 return;
3438 LOCK(cs_main);
3440 // During a reindex, we read the genesis block and call CheckBlockIndex before ActivateBestChain,
3441 // so we have the genesis block in mapBlockIndex but no active chain. (A few of the tests when
3442 // iterating the block tree require that chainActive has been initialized.)
3443 if (chainActive.Height() < 0) {
3444 assert(mapBlockIndex.size() <= 1);
3445 return;
3448 // Build forward-pointing map of the entire block tree.
3449 std::multimap<CBlockIndex*,CBlockIndex*> forward;
3450 for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
3451 forward.insert(std::make_pair(it->second->pprev, it->second));
3454 assert(forward.size() == mapBlockIndex.size());
3456 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeGenesis = forward.equal_range(NULL);
3457 CBlockIndex *pindex = rangeGenesis.first->second;
3458 rangeGenesis.first++;
3459 assert(rangeGenesis.first == rangeGenesis.second); // There is only one index entry with parent NULL.
3461 // Iterate over the entire block tree, using depth-first search.
3462 // Along the way, remember whether there are blocks on the path from genesis
3463 // block being explored which are the first to have certain properties.
3464 size_t nNodes = 0;
3465 int nHeight = 0;
3466 CBlockIndex* pindexFirstInvalid = NULL; // Oldest ancestor of pindex which is invalid.
3467 CBlockIndex* pindexFirstMissing = NULL; // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA.
3468 CBlockIndex* pindexFirstNeverProcessed = NULL; // Oldest ancestor of pindex for which nTx == 0.
3469 CBlockIndex* pindexFirstNotTreeValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
3470 CBlockIndex* pindexFirstNotTransactionsValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not).
3471 CBlockIndex* pindexFirstNotChainValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not).
3472 CBlockIndex* pindexFirstNotScriptsValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not).
3473 while (pindex != NULL) {
3474 nNodes++;
3475 if (pindexFirstInvalid == NULL && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
3476 if (pindexFirstMissing == NULL && !(pindex->nStatus & BLOCK_HAVE_DATA)) pindexFirstMissing = pindex;
3477 if (pindexFirstNeverProcessed == NULL && pindex->nTx == 0) pindexFirstNeverProcessed = pindex;
3478 if (pindex->pprev != NULL && pindexFirstNotTreeValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TREE) pindexFirstNotTreeValid = pindex;
3479 if (pindex->pprev != NULL && pindexFirstNotTransactionsValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TRANSACTIONS) pindexFirstNotTransactionsValid = pindex;
3480 if (pindex->pprev != NULL && pindexFirstNotChainValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_CHAIN) pindexFirstNotChainValid = pindex;
3481 if (pindex->pprev != NULL && pindexFirstNotScriptsValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS) pindexFirstNotScriptsValid = pindex;
3483 // Begin: actual consistency checks.
3484 if (pindex->pprev == NULL) {
3485 // Genesis block checks.
3486 assert(pindex->GetBlockHash() == consensusParams.hashGenesisBlock); // Genesis block's hash must match.
3487 assert(pindex == chainActive.Genesis()); // The current active chain's genesis block must be this block.
3489 if (pindex->nChainTx == 0) assert(pindex->nSequenceId == 0); // nSequenceId can't be set for blocks that aren't linked
3490 // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred).
3491 // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
3492 if (!fHavePruned) {
3493 // If we've never pruned, then HAVE_DATA should be equivalent to nTx > 0
3494 assert(!(pindex->nStatus & BLOCK_HAVE_DATA) == (pindex->nTx == 0));
3495 assert(pindexFirstMissing == pindexFirstNeverProcessed);
3496 } else {
3497 // If we have pruned, then we can only say that HAVE_DATA implies nTx > 0
3498 if (pindex->nStatus & BLOCK_HAVE_DATA) assert(pindex->nTx > 0);
3500 if (pindex->nStatus & BLOCK_HAVE_UNDO) assert(pindex->nStatus & BLOCK_HAVE_DATA);
3501 assert(((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS) == (pindex->nTx > 0)); // This is pruning-independent.
3502 // All parents having had data (at some point) is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to nChainTx being set.
3503 assert((pindexFirstNeverProcessed != NULL) == (pindex->nChainTx == 0)); // nChainTx != 0 is used to signal that all parent blocks have been processed (but may have been pruned).
3504 assert((pindexFirstNotTransactionsValid != NULL) == (pindex->nChainTx == 0));
3505 assert(pindex->nHeight == nHeight); // nHeight must be consistent.
3506 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.
3507 assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); // The pskip pointer must point back for all but the first 2 blocks.
3508 assert(pindexFirstNotTreeValid == NULL); // All mapBlockIndex entries must at least be TREE valid
3509 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TREE) assert(pindexFirstNotTreeValid == NULL); // TREE valid implies all parents are TREE valid
3510 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_CHAIN) assert(pindexFirstNotChainValid == NULL); // CHAIN valid implies all parents are CHAIN valid
3511 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_SCRIPTS) assert(pindexFirstNotScriptsValid == NULL); // SCRIPTS valid implies all parents are SCRIPTS valid
3512 if (pindexFirstInvalid == NULL) {
3513 // Checks for not-invalid blocks.
3514 assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
3516 if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && pindexFirstNeverProcessed == NULL) {
3517 if (pindexFirstInvalid == NULL) {
3518 // If this block sorts at least as good as the current tip and
3519 // is valid and we have all data for its parents, it must be in
3520 // setBlockIndexCandidates. chainActive.Tip() must also be there
3521 // even if some data has been pruned.
3522 if (pindexFirstMissing == NULL || pindex == chainActive.Tip()) {
3523 assert(setBlockIndexCandidates.count(pindex));
3525 // If some parent is missing, then it could be that this block was in
3526 // setBlockIndexCandidates but had to be removed because of the missing data.
3527 // In this case it must be in mapBlocksUnlinked -- see test below.
3529 } else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates.
3530 assert(setBlockIndexCandidates.count(pindex) == 0);
3532 // Check whether this block is in mapBlocksUnlinked.
3533 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked = mapBlocksUnlinked.equal_range(pindex->pprev);
3534 bool foundInUnlinked = false;
3535 while (rangeUnlinked.first != rangeUnlinked.second) {
3536 assert(rangeUnlinked.first->first == pindex->pprev);
3537 if (rangeUnlinked.first->second == pindex) {
3538 foundInUnlinked = true;
3539 break;
3541 rangeUnlinked.first++;
3543 if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed != NULL && pindexFirstInvalid == NULL) {
3544 // If this block has block data available, some parent was never received, and has no invalid parents, it must be in mapBlocksUnlinked.
3545 assert(foundInUnlinked);
3547 if (!(pindex->nStatus & BLOCK_HAVE_DATA)) assert(!foundInUnlinked); // Can't be in mapBlocksUnlinked if we don't HAVE_DATA
3548 if (pindexFirstMissing == NULL) assert(!foundInUnlinked); // We aren't missing data for any parent -- cannot be in mapBlocksUnlinked.
3549 if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed == NULL && pindexFirstMissing != NULL) {
3550 // We HAVE_DATA for this block, have received data for all parents at some point, but we're currently missing data for some parent.
3551 assert(fHavePruned); // We must have pruned.
3552 // This block may have entered mapBlocksUnlinked if:
3553 // - it has a descendant that at some point had more work than the
3554 // tip, and
3555 // - we tried switching to that descendant but were missing
3556 // data for some intermediate block between chainActive and the
3557 // tip.
3558 // So if this block is itself better than chainActive.Tip() and it wasn't in
3559 // setBlockIndexCandidates, then it must be in mapBlocksUnlinked.
3560 if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && setBlockIndexCandidates.count(pindex) == 0) {
3561 if (pindexFirstInvalid == NULL) {
3562 assert(foundInUnlinked);
3566 // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow
3567 // End: actual consistency checks.
3569 // Try descending into the first subnode.
3570 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
3571 if (range.first != range.second) {
3572 // A subnode was found.
3573 pindex = range.first->second;
3574 nHeight++;
3575 continue;
3577 // This is a leaf node.
3578 // Move upwards until we reach a node of which we have not yet visited the last child.
3579 while (pindex) {
3580 // We are going to either move to a parent or a sibling of pindex.
3581 // If pindex was the first with a certain property, unset the corresponding variable.
3582 if (pindex == pindexFirstInvalid) pindexFirstInvalid = NULL;
3583 if (pindex == pindexFirstMissing) pindexFirstMissing = NULL;
3584 if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed = NULL;
3585 if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid = NULL;
3586 if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid = NULL;
3587 if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid = NULL;
3588 if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid = NULL;
3589 // Find our parent.
3590 CBlockIndex* pindexPar = pindex->pprev;
3591 // Find which child we just visited.
3592 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
3593 while (rangePar.first->second != pindex) {
3594 assert(rangePar.first != rangePar.second); // Our parent must have at least the node we're coming from as child.
3595 rangePar.first++;
3597 // Proceed to the next one.
3598 rangePar.first++;
3599 if (rangePar.first != rangePar.second) {
3600 // Move to the sibling.
3601 pindex = rangePar.first->second;
3602 break;
3603 } else {
3604 // Move up further.
3605 pindex = pindexPar;
3606 nHeight--;
3607 continue;
3612 // Check that we actually traversed the entire map.
3613 assert(nNodes == forward.size());
3616 //////////////////////////////////////////////////////////////////////////////
3618 // CAlert
3621 std::string GetWarnings(const std::string& strFor)
3623 int nPriority = 0;
3624 string strStatusBar;
3625 string strRPC;
3627 if (!CLIENT_VERSION_IS_RELEASE)
3628 strStatusBar = _("This is a pre-release test build - use at your own risk - do not use for mining or merchant applications");
3630 if (GetBoolArg("-testsafemode", false))
3631 strStatusBar = strRPC = "testsafemode enabled";
3633 // Misc warnings like out of disk space and clock is wrong
3634 if (strMiscWarning != "")
3636 nPriority = 1000;
3637 strStatusBar = strMiscWarning;
3640 if (fLargeWorkForkFound)
3642 nPriority = 2000;
3643 strStatusBar = strRPC = _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.");
3645 else if (fLargeWorkInvalidChainFound)
3647 nPriority = 2000;
3648 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.");
3651 // Alerts
3653 LOCK(cs_mapAlerts);
3654 BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
3656 const CAlert& alert = item.second;
3657 if (alert.AppliesToMe() && alert.nPriority > nPriority)
3659 nPriority = alert.nPriority;
3660 strStatusBar = alert.strStatusBar;
3665 if (strFor == "statusbar")
3666 return strStatusBar;
3667 else if (strFor == "rpc")
3668 return strRPC;
3669 assert(!"GetWarnings(): invalid parameter");
3670 return "error";
3680 //////////////////////////////////////////////////////////////////////////////
3682 // Messages
3686 bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
3688 switch (inv.type)
3690 case MSG_TX:
3692 bool txInMap = false;
3693 txInMap = mempool.exists(inv.hash);
3694 return txInMap || mapOrphanTransactions.count(inv.hash) ||
3695 pcoinsTip->HaveCoins(inv.hash);
3697 case MSG_BLOCK:
3698 return mapBlockIndex.count(inv.hash);
3700 // Don't know what it is, just say we already got one
3701 return true;
3704 void static ProcessGetData(CNode* pfrom)
3706 std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();
3708 vector<CInv> vNotFound;
3710 LOCK(cs_main);
3712 while (it != pfrom->vRecvGetData.end()) {
3713 // Don't bother if send buffer is too full to respond anyway
3714 if (pfrom->nSendSize >= SendBufferSize())
3715 break;
3717 const CInv &inv = *it;
3719 boost::this_thread::interruption_point();
3720 it++;
3722 if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK)
3724 bool send = false;
3725 BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
3726 if (mi != mapBlockIndex.end())
3728 if (chainActive.Contains(mi->second)) {
3729 send = true;
3730 } else {
3731 static const int nOneMonth = 30 * 24 * 60 * 60;
3732 // To prevent fingerprinting attacks, only send blocks outside of the active
3733 // chain if they are valid, and no more than a month older (both in time, and in
3734 // best equivalent proof of work) than the best header chain we know about.
3735 send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != NULL) &&
3736 (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() < nOneMonth) &&
3737 (GetBlockProofEquivalentTime(*pindexBestHeader, *mi->second, *pindexBestHeader, Params().GetConsensus()) < nOneMonth);
3738 if (!send) {
3739 LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
3743 // Pruned nodes may have deleted the block, so check whether
3744 // it's available before trying to send.
3745 if (send && (mi->second->nStatus & BLOCK_HAVE_DATA))
3747 // Send block from disk
3748 CBlock block;
3749 if (!ReadBlockFromDisk(block, (*mi).second))
3750 assert(!"cannot load block from disk");
3751 if (inv.type == MSG_BLOCK)
3752 pfrom->PushMessage("block", block);
3753 else // MSG_FILTERED_BLOCK)
3755 LOCK(pfrom->cs_filter);
3756 if (pfrom->pfilter)
3758 CMerkleBlock merkleBlock(block, *pfrom->pfilter);
3759 pfrom->PushMessage("merkleblock", merkleBlock);
3760 // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
3761 // This avoids hurting performance by pointlessly requiring a round-trip
3762 // Note that there is currently no way for a node to request any single transactions we didn't send here -
3763 // they must either disconnect and retry or request the full block.
3764 // Thus, the protocol spec specified allows for us to provide duplicate txn here,
3765 // however we MUST always provide at least what the remote peer needs
3766 typedef std::pair<unsigned int, uint256> PairType;
3767 BOOST_FOREACH(PairType& pair, merkleBlock.vMatchedTxn)
3768 if (!pfrom->setInventoryKnown.count(CInv(MSG_TX, pair.second)))
3769 pfrom->PushMessage("tx", block.vtx[pair.first]);
3771 // else
3772 // no response
3775 // Trigger the peer node to send a getblocks request for the next batch of inventory
3776 if (inv.hash == pfrom->hashContinue)
3778 // Bypass PushInventory, this must send even if redundant,
3779 // and we want it right after the last block so they don't
3780 // wait for other stuff first.
3781 vector<CInv> vInv;
3782 vInv.push_back(CInv(MSG_BLOCK, chainActive.Tip()->GetBlockHash()));
3783 pfrom->PushMessage("inv", vInv);
3784 pfrom->hashContinue.SetNull();
3788 else if (inv.IsKnownType())
3790 // Send stream from relay memory
3791 bool pushed = false;
3793 LOCK(cs_mapRelay);
3794 map<CInv, CDataStream>::iterator mi = mapRelay.find(inv);
3795 if (mi != mapRelay.end()) {
3796 pfrom->PushMessage(inv.GetCommand(), (*mi).second);
3797 pushed = true;
3800 if (!pushed && inv.type == MSG_TX) {
3801 CTransaction tx;
3802 if (mempool.lookup(inv.hash, tx)) {
3803 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
3804 ss.reserve(1000);
3805 ss << tx;
3806 pfrom->PushMessage("tx", ss);
3807 pushed = true;
3810 if (!pushed) {
3811 vNotFound.push_back(inv);
3815 // Track requests for our stuff.
3816 GetMainSignals().Inventory(inv.hash);
3818 if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK)
3819 break;
3823 pfrom->vRecvGetData.erase(pfrom->vRecvGetData.begin(), it);
3825 if (!vNotFound.empty()) {
3826 // Let the peer know that we didn't find what it asked for, so it doesn't
3827 // have to wait around forever. Currently only SPV clients actually care
3828 // about this message: it's needed when they are recursively walking the
3829 // dependencies of relevant unconfirmed transactions. SPV clients want to
3830 // do that because they want to know about (and store and rebroadcast and
3831 // risk analyze) the dependencies of transactions relevant to them, without
3832 // having to download the entire memory pool.
3833 pfrom->PushMessage("notfound", vNotFound);
3837 bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived)
3839 const CChainParams& chainparams = Params();
3840 RandAddSeedPerfmon();
3841 LogPrint("net", "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->id);
3842 if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
3844 LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
3845 return true;
3851 if (strCommand == "version")
3853 // Each connection can only send one version message
3854 if (pfrom->nVersion != 0)
3856 pfrom->PushMessage("reject", strCommand, REJECT_DUPLICATE, string("Duplicate version message"));
3857 Misbehaving(pfrom->GetId(), 1);
3858 return false;
3861 int64_t nTime;
3862 CAddress addrMe;
3863 CAddress addrFrom;
3864 uint64_t nNonce = 1;
3865 vRecv >> pfrom->nVersion >> pfrom->nServices >> nTime >> addrMe;
3866 if (pfrom->nVersion < MIN_PEER_PROTO_VERSION)
3868 // disconnect from peers older than this proto version
3869 LogPrintf("peer=%d using obsolete version %i; disconnecting\n", pfrom->id, pfrom->nVersion);
3870 pfrom->PushMessage("reject", strCommand, REJECT_OBSOLETE,
3871 strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION));
3872 pfrom->fDisconnect = true;
3873 return false;
3876 if (pfrom->nVersion == 10300)
3877 pfrom->nVersion = 300;
3878 if (!vRecv.empty())
3879 vRecv >> addrFrom >> nNonce;
3880 if (!vRecv.empty()) {
3881 vRecv >> LIMITED_STRING(pfrom->strSubVer, 256);
3882 pfrom->cleanSubVer = SanitizeString(pfrom->strSubVer);
3884 if (!vRecv.empty())
3885 vRecv >> pfrom->nStartingHeight;
3886 if (!vRecv.empty())
3887 vRecv >> pfrom->fRelayTxes; // set to true after we get the first filter* message
3888 else
3889 pfrom->fRelayTxes = true;
3891 // Disconnect if we connected to ourself
3892 if (nNonce == nLocalHostNonce && nNonce > 1)
3894 LogPrintf("connected to self at %s, disconnecting\n", pfrom->addr.ToString());
3895 pfrom->fDisconnect = true;
3896 return true;
3899 pfrom->addrLocal = addrMe;
3900 if (pfrom->fInbound && addrMe.IsRoutable())
3902 SeenLocal(addrMe);
3905 // Be shy and don't send version until we hear
3906 if (pfrom->fInbound)
3907 pfrom->PushVersion();
3909 pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
3911 // Potentially mark this peer as a preferred download peer.
3912 UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
3914 // Change version
3915 pfrom->PushMessage("verack");
3916 pfrom->ssSend.SetVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
3918 if (!pfrom->fInbound)
3920 // Advertise our address
3921 if (fListen && !IsInitialBlockDownload())
3923 CAddress addr = GetLocalAddress(&pfrom->addr);
3924 if (addr.IsRoutable())
3926 pfrom->PushAddress(addr);
3927 } else if (IsPeerAddrLocalGood(pfrom)) {
3928 addr.SetIP(pfrom->addrLocal);
3929 pfrom->PushAddress(addr);
3933 // Get recent addresses
3934 if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || addrman.size() < 1000)
3936 pfrom->PushMessage("getaddr");
3937 pfrom->fGetAddr = true;
3939 addrman.Good(pfrom->addr);
3940 } else {
3941 if (((CNetAddr)pfrom->addr) == (CNetAddr)addrFrom)
3943 addrman.Add(addrFrom, addrFrom);
3944 addrman.Good(addrFrom);
3948 // Relay alerts
3950 LOCK(cs_mapAlerts);
3951 BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
3952 item.second.RelayTo(pfrom);
3955 pfrom->fSuccessfullyConnected = true;
3957 string remoteAddr;
3958 if (fLogIPs)
3959 remoteAddr = ", peeraddr=" + pfrom->addr.ToString();
3961 LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
3962 pfrom->cleanSubVer, pfrom->nVersion,
3963 pfrom->nStartingHeight, addrMe.ToString(), pfrom->id,
3964 remoteAddr);
3966 int64_t nTimeOffset = nTime - GetTime();
3967 pfrom->nTimeOffset = nTimeOffset;
3968 AddTimeData(pfrom->addr, nTimeOffset);
3972 else if (pfrom->nVersion == 0)
3974 // Must have a version message before anything else
3975 Misbehaving(pfrom->GetId(), 1);
3976 return false;
3980 else if (strCommand == "verack")
3982 pfrom->SetRecvVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
3984 // Mark this node as currently connected, so we update its timestamp later.
3985 if (pfrom->fNetworkNode) {
3986 LOCK(cs_main);
3987 State(pfrom->GetId())->fCurrentlyConnected = true;
3992 else if (strCommand == "addr")
3994 vector<CAddress> vAddr;
3995 vRecv >> vAddr;
3997 // Don't want addr from older versions unless seeding
3998 if (pfrom->nVersion < CADDR_TIME_VERSION && addrman.size() > 1000)
3999 return true;
4000 if (vAddr.size() > 1000)
4002 Misbehaving(pfrom->GetId(), 20);
4003 return error("message addr size() = %u", vAddr.size());
4006 // Store the new addresses
4007 vector<CAddress> vAddrOk;
4008 int64_t nNow = GetAdjustedTime();
4009 int64_t nSince = nNow - 10 * 60;
4010 BOOST_FOREACH(CAddress& addr, vAddr)
4012 boost::this_thread::interruption_point();
4014 if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
4015 addr.nTime = nNow - 5 * 24 * 60 * 60;
4016 pfrom->AddAddressKnown(addr);
4017 bool fReachable = IsReachable(addr);
4018 if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
4020 // Relay to a limited number of other nodes
4022 LOCK(cs_vNodes);
4023 // Use deterministic randomness to send to the same nodes for 24 hours
4024 // at a time so the addrKnowns of the chosen nodes prevent repeats
4025 static uint256 hashSalt;
4026 if (hashSalt.IsNull())
4027 hashSalt = GetRandHash();
4028 uint64_t hashAddr = addr.GetHash();
4029 uint256 hashRand = ArithToUint256(UintToArith256(hashSalt) ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60)));
4030 hashRand = Hash(BEGIN(hashRand), END(hashRand));
4031 multimap<uint256, CNode*> mapMix;
4032 BOOST_FOREACH(CNode* pnode, vNodes)
4034 if (pnode->nVersion < CADDR_TIME_VERSION)
4035 continue;
4036 unsigned int nPointer;
4037 memcpy(&nPointer, &pnode, sizeof(nPointer));
4038 uint256 hashKey = ArithToUint256(UintToArith256(hashRand) ^ nPointer);
4039 hashKey = Hash(BEGIN(hashKey), END(hashKey));
4040 mapMix.insert(make_pair(hashKey, pnode));
4042 int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
4043 for (multimap<uint256, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
4044 ((*mi).second)->PushAddress(addr);
4047 // Do not store addresses outside our network
4048 if (fReachable)
4049 vAddrOk.push_back(addr);
4051 addrman.Add(vAddrOk, pfrom->addr, 2 * 60 * 60);
4052 if (vAddr.size() < 1000)
4053 pfrom->fGetAddr = false;
4054 if (pfrom->fOneShot)
4055 pfrom->fDisconnect = true;
4059 else if (strCommand == "inv")
4061 vector<CInv> vInv;
4062 vRecv >> vInv;
4063 if (vInv.size() > MAX_INV_SZ)
4065 Misbehaving(pfrom->GetId(), 20);
4066 return error("message inv size() = %u", vInv.size());
4069 LOCK(cs_main);
4071 std::vector<CInv> vToFetch;
4073 for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
4075 const CInv &inv = vInv[nInv];
4077 boost::this_thread::interruption_point();
4078 pfrom->AddInventoryKnown(inv);
4080 bool fAlreadyHave = AlreadyHave(inv);
4081 LogPrint("net", "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom->id);
4083 if (!fAlreadyHave && !fImporting && !fReindex && inv.type != MSG_BLOCK)
4084 pfrom->AskFor(inv);
4086 if (inv.type == MSG_BLOCK) {
4087 UpdateBlockAvailability(pfrom->GetId(), inv.hash);
4088 if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
4089 // First request the headers preceding the announced block. In the normal fully-synced
4090 // case where a new block is announced that succeeds the current tip (no reorganization),
4091 // there are no such headers.
4092 // Secondly, and only when we are close to being synced, we request the announced block directly,
4093 // to avoid an extra round-trip. Note that we must *first* ask for the headers, so by the
4094 // time the block arrives, the header chain leading up to it is already validated. Not
4095 // doing this will result in the received block being rejected as an orphan in case it is
4096 // not a direct successor.
4097 pfrom->PushMessage("getheaders", chainActive.GetLocator(pindexBestHeader), inv.hash);
4098 CNodeState *nodestate = State(pfrom->GetId());
4099 if (chainActive.Tip()->GetBlockTime() > GetAdjustedTime() - chainparams.GetConsensus().nPowTargetSpacing * 20 &&
4100 nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
4101 vToFetch.push_back(inv);
4102 // Mark block as in flight already, even though the actual "getdata" message only goes out
4103 // later (within the same cs_main lock, though).
4104 MarkBlockAsInFlight(pfrom->GetId(), inv.hash, chainparams.GetConsensus());
4106 LogPrint("net", "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, inv.hash.ToString(), pfrom->id);
4110 // Track requests for our stuff
4111 GetMainSignals().Inventory(inv.hash);
4113 if (pfrom->nSendSize > (SendBufferSize() * 2)) {
4114 Misbehaving(pfrom->GetId(), 50);
4115 return error("send buffer size() = %u", pfrom->nSendSize);
4119 if (!vToFetch.empty())
4120 pfrom->PushMessage("getdata", vToFetch);
4124 else if (strCommand == "getdata")
4126 vector<CInv> vInv;
4127 vRecv >> vInv;
4128 if (vInv.size() > MAX_INV_SZ)
4130 Misbehaving(pfrom->GetId(), 20);
4131 return error("message getdata size() = %u", vInv.size());
4134 if (fDebug || (vInv.size() != 1))
4135 LogPrint("net", "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom->id);
4137 if ((fDebug && vInv.size() > 0) || (vInv.size() == 1))
4138 LogPrint("net", "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom->id);
4140 pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
4141 ProcessGetData(pfrom);
4145 else if (strCommand == "getblocks")
4147 CBlockLocator locator;
4148 uint256 hashStop;
4149 vRecv >> locator >> hashStop;
4151 LOCK(cs_main);
4153 // Find the last block the caller has in the main chain
4154 CBlockIndex* pindex = FindForkInGlobalIndex(chainActive, locator);
4156 // Send the rest of the chain
4157 if (pindex)
4158 pindex = chainActive.Next(pindex);
4159 int nLimit = 500;
4160 LogPrint("net", "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), nLimit, pfrom->id);
4161 for (; pindex; pindex = chainActive.Next(pindex))
4163 if (pindex->GetBlockHash() == hashStop)
4165 LogPrint("net", " getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4166 break;
4168 pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
4169 if (--nLimit <= 0)
4171 // When this block is requested, we'll send an inv that'll
4172 // trigger the peer to getblocks the next batch of inventory.
4173 LogPrint("net", " getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4174 pfrom->hashContinue = pindex->GetBlockHash();
4175 break;
4181 else if (strCommand == "getheaders")
4183 CBlockLocator locator;
4184 uint256 hashStop;
4185 vRecv >> locator >> hashStop;
4187 LOCK(cs_main);
4189 if (IsInitialBlockDownload())
4190 return true;
4192 CBlockIndex* pindex = NULL;
4193 if (locator.IsNull())
4195 // If locator is null, return the hashStop block
4196 BlockMap::iterator mi = mapBlockIndex.find(hashStop);
4197 if (mi == mapBlockIndex.end())
4198 return true;
4199 pindex = (*mi).second;
4201 else
4203 // Find the last block the caller has in the main chain
4204 pindex = FindForkInGlobalIndex(chainActive, locator);
4205 if (pindex)
4206 pindex = chainActive.Next(pindex);
4209 // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
4210 vector<CBlock> vHeaders;
4211 int nLimit = MAX_HEADERS_RESULTS;
4212 LogPrint("net", "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.ToString(), pfrom->id);
4213 for (; pindex; pindex = chainActive.Next(pindex))
4215 vHeaders.push_back(pindex->GetBlockHeader());
4216 if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
4217 break;
4219 pfrom->PushMessage("headers", vHeaders);
4223 else if (strCommand == "tx")
4225 vector<uint256> vWorkQueue;
4226 vector<uint256> vEraseQueue;
4227 CTransaction tx;
4228 vRecv >> tx;
4230 CInv inv(MSG_TX, tx.GetHash());
4231 pfrom->AddInventoryKnown(inv);
4233 LOCK(cs_main);
4235 bool fMissingInputs = false;
4236 CValidationState state;
4238 mapAlreadyAskedFor.erase(inv);
4240 if (AcceptToMemoryPool(mempool, state, tx, true, &fMissingInputs))
4242 mempool.check(pcoinsTip);
4243 RelayTransaction(tx);
4244 vWorkQueue.push_back(inv.hash);
4246 LogPrint("mempool", "AcceptToMemoryPool: peer=%d %s: accepted %s (poolsz %u)\n",
4247 pfrom->id, pfrom->cleanSubVer,
4248 tx.GetHash().ToString(),
4249 mempool.mapTx.size());
4251 // Recursively process any orphan transactions that depended on this one
4252 set<NodeId> setMisbehaving;
4253 for (unsigned int i = 0; i < vWorkQueue.size(); i++)
4255 map<uint256, set<uint256> >::iterator itByPrev = mapOrphanTransactionsByPrev.find(vWorkQueue[i]);
4256 if (itByPrev == mapOrphanTransactionsByPrev.end())
4257 continue;
4258 for (set<uint256>::iterator mi = itByPrev->second.begin();
4259 mi != itByPrev->second.end();
4260 ++mi)
4262 const uint256& orphanHash = *mi;
4263 const CTransaction& orphanTx = mapOrphanTransactions[orphanHash].tx;
4264 NodeId fromPeer = mapOrphanTransactions[orphanHash].fromPeer;
4265 bool fMissingInputs2 = false;
4266 // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
4267 // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
4268 // anyone relaying LegitTxX banned)
4269 CValidationState stateDummy;
4272 if (setMisbehaving.count(fromPeer))
4273 continue;
4274 if (AcceptToMemoryPool(mempool, stateDummy, orphanTx, true, &fMissingInputs2))
4276 LogPrint("mempool", " accepted orphan tx %s\n", orphanHash.ToString());
4277 RelayTransaction(orphanTx);
4278 vWorkQueue.push_back(orphanHash);
4279 vEraseQueue.push_back(orphanHash);
4281 else if (!fMissingInputs2)
4283 int nDos = 0;
4284 if (stateDummy.IsInvalid(nDos) && nDos > 0)
4286 // Punish peer that gave us an invalid orphan tx
4287 Misbehaving(fromPeer, nDos);
4288 setMisbehaving.insert(fromPeer);
4289 LogPrint("mempool", " invalid orphan tx %s\n", orphanHash.ToString());
4291 // Has inputs but not accepted to mempool
4292 // Probably non-standard or insufficient fee/priority
4293 LogPrint("mempool", " removed orphan tx %s\n", orphanHash.ToString());
4294 vEraseQueue.push_back(orphanHash);
4296 mempool.check(pcoinsTip);
4300 BOOST_FOREACH(uint256 hash, vEraseQueue)
4301 EraseOrphanTx(hash);
4303 else if (fMissingInputs)
4305 AddOrphanTx(tx, pfrom->GetId());
4307 // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
4308 unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));
4309 unsigned int nEvicted = LimitOrphanTxSize(nMaxOrphanTx);
4310 if (nEvicted > 0)
4311 LogPrint("mempool", "mapOrphan overflow, removed %u tx\n", nEvicted);
4312 } else if (pfrom->fWhitelisted) {
4313 // Always relay transactions received from whitelisted peers, even
4314 // if they are already in the mempool (allowing the node to function
4315 // as a gateway for nodes hidden behind it).
4316 RelayTransaction(tx);
4318 int nDoS = 0;
4319 if (state.IsInvalid(nDoS))
4321 LogPrint("mempool", "%s from peer=%d %s was not accepted into the memory pool: %s\n", tx.GetHash().ToString(),
4322 pfrom->id, pfrom->cleanSubVer,
4323 state.GetRejectReason());
4324 pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),
4325 state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash);
4326 if (nDoS > 0)
4327 Misbehaving(pfrom->GetId(), nDoS);
4332 else if (strCommand == "headers" && !fImporting && !fReindex) // Ignore headers received while importing
4334 std::vector<CBlockHeader> headers;
4336 // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
4337 unsigned int nCount = ReadCompactSize(vRecv);
4338 if (nCount > MAX_HEADERS_RESULTS) {
4339 Misbehaving(pfrom->GetId(), 20);
4340 return error("headers message size = %u", nCount);
4342 headers.resize(nCount);
4343 for (unsigned int n = 0; n < nCount; n++) {
4344 vRecv >> headers[n];
4345 ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
4348 LOCK(cs_main);
4350 if (nCount == 0) {
4351 // Nothing interesting. Stop asking this peers for more headers.
4352 return true;
4355 CBlockIndex *pindexLast = NULL;
4356 BOOST_FOREACH(const CBlockHeader& header, headers) {
4357 CValidationState state;
4358 if (pindexLast != NULL && header.hashPrevBlock != pindexLast->GetBlockHash()) {
4359 Misbehaving(pfrom->GetId(), 20);
4360 return error("non-continuous headers sequence");
4362 if (!AcceptBlockHeader(header, state, &pindexLast)) {
4363 int nDoS;
4364 if (state.IsInvalid(nDoS)) {
4365 if (nDoS > 0)
4366 Misbehaving(pfrom->GetId(), nDoS);
4367 return error("invalid header received");
4372 if (pindexLast)
4373 UpdateBlockAvailability(pfrom->GetId(), pindexLast->GetBlockHash());
4375 if (nCount == MAX_HEADERS_RESULTS && pindexLast) {
4376 // Headers message had its maximum size; the peer may have more headers.
4377 // TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
4378 // from there instead.
4379 LogPrint("net", "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom->id, pfrom->nStartingHeight);
4380 pfrom->PushMessage("getheaders", chainActive.GetLocator(pindexLast), uint256());
4383 CheckBlockIndex();
4386 else if (strCommand == "block" && !fImporting && !fReindex) // Ignore blocks received while importing
4388 CBlock block;
4389 vRecv >> block;
4391 CInv inv(MSG_BLOCK, block.GetHash());
4392 LogPrint("net", "received block %s peer=%d\n", inv.hash.ToString(), pfrom->id);
4394 pfrom->AddInventoryKnown(inv);
4396 CValidationState state;
4397 // Process all blocks from whitelisted peers, even if not requested.
4398 ProcessNewBlock(state, pfrom, &block, pfrom->fWhitelisted, NULL);
4399 int nDoS;
4400 if (state.IsInvalid(nDoS)) {
4401 pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),
4402 state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash);
4403 if (nDoS > 0) {
4404 LOCK(cs_main);
4405 Misbehaving(pfrom->GetId(), nDoS);
4412 // This asymmetric behavior for inbound and outbound connections was introduced
4413 // to prevent a fingerprinting attack: an attacker can send specific fake addresses
4414 // to users' AddrMan and later request them by sending getaddr messages.
4415 // Making nodes which are behind NAT and can only make outgoing connections ignore
4416 // the getaddr message mitigates the attack.
4417 else if ((strCommand == "getaddr") && (pfrom->fInbound))
4419 pfrom->vAddrToSend.clear();
4420 vector<CAddress> vAddr = addrman.GetAddr();
4421 BOOST_FOREACH(const CAddress &addr, vAddr)
4422 pfrom->PushAddress(addr);
4426 else if (strCommand == "mempool")
4428 LOCK2(cs_main, pfrom->cs_filter);
4430 std::vector<uint256> vtxid;
4431 mempool.queryHashes(vtxid);
4432 vector<CInv> vInv;
4433 BOOST_FOREACH(uint256& hash, vtxid) {
4434 CInv inv(MSG_TX, hash);
4435 CTransaction tx;
4436 bool fInMemPool = mempool.lookup(hash, tx);
4437 if (!fInMemPool) continue; // another thread removed since queryHashes, maybe...
4438 if ((pfrom->pfilter && pfrom->pfilter->IsRelevantAndUpdate(tx)) ||
4439 (!pfrom->pfilter))
4440 vInv.push_back(inv);
4441 if (vInv.size() == MAX_INV_SZ) {
4442 pfrom->PushMessage("inv", vInv);
4443 vInv.clear();
4446 if (vInv.size() > 0)
4447 pfrom->PushMessage("inv", vInv);
4451 else if (strCommand == "ping")
4453 if (pfrom->nVersion > BIP0031_VERSION)
4455 uint64_t nonce = 0;
4456 vRecv >> nonce;
4457 // Echo the message back with the nonce. This allows for two useful features:
4459 // 1) A remote node can quickly check if the connection is operational
4460 // 2) Remote nodes can measure the latency of the network thread. If this node
4461 // is overloaded it won't respond to pings quickly and the remote node can
4462 // avoid sending us more work, like chain download requests.
4464 // The nonce stops the remote getting confused between different pings: without
4465 // it, if the remote node sends a ping once per second and this node takes 5
4466 // seconds to respond to each, the 5th ping the remote sends would appear to
4467 // return very quickly.
4468 pfrom->PushMessage("pong", nonce);
4473 else if (strCommand == "pong")
4475 int64_t pingUsecEnd = nTimeReceived;
4476 uint64_t nonce = 0;
4477 size_t nAvail = vRecv.in_avail();
4478 bool bPingFinished = false;
4479 std::string sProblem;
4481 if (nAvail >= sizeof(nonce)) {
4482 vRecv >> nonce;
4484 // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
4485 if (pfrom->nPingNonceSent != 0) {
4486 if (nonce == pfrom->nPingNonceSent) {
4487 // Matching pong received, this ping is no longer outstanding
4488 bPingFinished = true;
4489 int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart;
4490 if (pingUsecTime > 0) {
4491 // Successful ping time measurement, replace previous
4492 pfrom->nPingUsecTime = pingUsecTime;
4493 } else {
4494 // This should never happen
4495 sProblem = "Timing mishap";
4497 } else {
4498 // Nonce mismatches are normal when pings are overlapping
4499 sProblem = "Nonce mismatch";
4500 if (nonce == 0) {
4501 // This is most likely a bug in another implementation somewhere; cancel this ping
4502 bPingFinished = true;
4503 sProblem = "Nonce zero";
4506 } else {
4507 sProblem = "Unsolicited pong without ping";
4509 } else {
4510 // This is most likely a bug in another implementation somewhere; cancel this ping
4511 bPingFinished = true;
4512 sProblem = "Short payload";
4515 if (!(sProblem.empty())) {
4516 LogPrint("net", "pong peer=%d %s: %s, %x expected, %x received, %u bytes\n",
4517 pfrom->id,
4518 pfrom->cleanSubVer,
4519 sProblem,
4520 pfrom->nPingNonceSent,
4521 nonce,
4522 nAvail);
4524 if (bPingFinished) {
4525 pfrom->nPingNonceSent = 0;
4530 else if (fAlerts && strCommand == "alert")
4532 CAlert alert;
4533 vRecv >> alert;
4535 uint256 alertHash = alert.GetHash();
4536 if (pfrom->setKnown.count(alertHash) == 0)
4538 if (alert.ProcessAlert(Params().AlertKey()))
4540 // Relay
4541 pfrom->setKnown.insert(alertHash);
4543 LOCK(cs_vNodes);
4544 BOOST_FOREACH(CNode* pnode, vNodes)
4545 alert.RelayTo(pnode);
4548 else {
4549 // Small DoS penalty so peers that send us lots of
4550 // duplicate/expired/invalid-signature/whatever alerts
4551 // eventually get banned.
4552 // This isn't a Misbehaving(100) (immediate ban) because the
4553 // peer might be an older or different implementation with
4554 // a different signature key, etc.
4555 Misbehaving(pfrom->GetId(), 10);
4561 else if (strCommand == "filterload")
4563 CBloomFilter filter;
4564 vRecv >> filter;
4566 if (!filter.IsWithinSizeConstraints())
4567 // There is no excuse for sending a too-large filter
4568 Misbehaving(pfrom->GetId(), 100);
4569 else
4571 LOCK(pfrom->cs_filter);
4572 delete pfrom->pfilter;
4573 pfrom->pfilter = new CBloomFilter(filter);
4574 pfrom->pfilter->UpdateEmptyFull();
4576 pfrom->fRelayTxes = true;
4580 else if (strCommand == "filteradd")
4582 vector<unsigned char> vData;
4583 vRecv >> vData;
4585 // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
4586 // and thus, the maximum size any matched object can have) in a filteradd message
4587 if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE)
4589 Misbehaving(pfrom->GetId(), 100);
4590 } else {
4591 LOCK(pfrom->cs_filter);
4592 if (pfrom->pfilter)
4593 pfrom->pfilter->insert(vData);
4594 else
4595 Misbehaving(pfrom->GetId(), 100);
4600 else if (strCommand == "filterclear")
4602 LOCK(pfrom->cs_filter);
4603 delete pfrom->pfilter;
4604 pfrom->pfilter = new CBloomFilter();
4605 pfrom->fRelayTxes = true;
4609 else if (strCommand == "reject")
4611 if (fDebug) {
4612 try {
4613 string strMsg; unsigned char ccode; string strReason;
4614 vRecv >> LIMITED_STRING(strMsg, CMessageHeader::COMMAND_SIZE) >> ccode >> LIMITED_STRING(strReason, MAX_REJECT_MESSAGE_LENGTH);
4616 ostringstream ss;
4617 ss << strMsg << " code " << itostr(ccode) << ": " << strReason;
4619 if (strMsg == "block" || strMsg == "tx")
4621 uint256 hash;
4622 vRecv >> hash;
4623 ss << ": hash " << hash.ToString();
4625 LogPrint("net", "Reject %s\n", SanitizeString(ss.str()));
4626 } catch (const std::ios_base::failure&) {
4627 // Avoid feedback loops by preventing reject messages from triggering a new reject message.
4628 LogPrint("net", "Unparseable reject message received\n");
4633 else
4635 // Ignore unknown commands for extensibility
4636 LogPrint("net", "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->id);
4641 return true;
4644 // requires LOCK(cs_vRecvMsg)
4645 bool ProcessMessages(CNode* pfrom)
4647 //if (fDebug)
4648 // LogPrintf("%s(%u messages)\n", __func__, pfrom->vRecvMsg.size());
4651 // Message format
4652 // (4) message start
4653 // (12) command
4654 // (4) size
4655 // (4) checksum
4656 // (x) data
4658 bool fOk = true;
4660 if (!pfrom->vRecvGetData.empty())
4661 ProcessGetData(pfrom);
4663 // this maintains the order of responses
4664 if (!pfrom->vRecvGetData.empty()) return fOk;
4666 std::deque<CNetMessage>::iterator it = pfrom->vRecvMsg.begin();
4667 while (!pfrom->fDisconnect && it != pfrom->vRecvMsg.end()) {
4668 // Don't bother if send buffer is too full to respond anyway
4669 if (pfrom->nSendSize >= SendBufferSize())
4670 break;
4672 // get next message
4673 CNetMessage& msg = *it;
4675 //if (fDebug)
4676 // LogPrintf("%s(message %u msgsz, %u bytes, complete:%s)\n", __func__,
4677 // msg.hdr.nMessageSize, msg.vRecv.size(),
4678 // msg.complete() ? "Y" : "N");
4680 // end, if an incomplete message is found
4681 if (!msg.complete())
4682 break;
4684 // at this point, any failure means we can delete the current message
4685 it++;
4687 // Scan for message start
4688 if (memcmp(msg.hdr.pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0) {
4689 LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->id);
4690 fOk = false;
4691 break;
4694 // Read header
4695 CMessageHeader& hdr = msg.hdr;
4696 if (!hdr.IsValid(Params().MessageStart()))
4698 LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->id);
4699 continue;
4701 string strCommand = hdr.GetCommand();
4703 // Message size
4704 unsigned int nMessageSize = hdr.nMessageSize;
4706 // Checksum
4707 CDataStream& vRecv = msg.vRecv;
4708 uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize);
4709 unsigned int nChecksum = ReadLE32((unsigned char*)&hash);
4710 if (nChecksum != hdr.nChecksum)
4712 LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR nChecksum=%08x hdr.nChecksum=%08x\n", __func__,
4713 SanitizeString(strCommand), nMessageSize, nChecksum, hdr.nChecksum);
4714 continue;
4717 // Process message
4718 bool fRet = false;
4721 fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime);
4722 boost::this_thread::interruption_point();
4724 catch (const std::ios_base::failure& e)
4726 pfrom->PushMessage("reject", strCommand, REJECT_MALFORMED, string("error parsing message"));
4727 if (strstr(e.what(), "end of data"))
4729 // Allow exceptions from under-length message on vRecv
4730 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());
4732 else if (strstr(e.what(), "size too large"))
4734 // Allow exceptions from over-long size
4735 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
4737 else
4739 PrintExceptionContinue(&e, "ProcessMessages()");
4742 catch (const boost::thread_interrupted&) {
4743 throw;
4745 catch (const std::exception& e) {
4746 PrintExceptionContinue(&e, "ProcessMessages()");
4747 } catch (...) {
4748 PrintExceptionContinue(NULL, "ProcessMessages()");
4751 if (!fRet)
4752 LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->id);
4754 break;
4757 // In case the connection got shut down, its receive buffer was wiped
4758 if (!pfrom->fDisconnect)
4759 pfrom->vRecvMsg.erase(pfrom->vRecvMsg.begin(), it);
4761 return fOk;
4765 bool SendMessages(CNode* pto, bool fSendTrickle)
4767 const Consensus::Params& consensusParams = Params().GetConsensus();
4769 // Don't send anything until we get its version message
4770 if (pto->nVersion == 0)
4771 return true;
4774 // Message: ping
4776 bool pingSend = false;
4777 if (pto->fPingQueued) {
4778 // RPC ping request by user
4779 pingSend = true;
4781 if (pto->nPingNonceSent == 0 && pto->nPingUsecStart + PING_INTERVAL * 1000000 < GetTimeMicros()) {
4782 // Ping automatically sent as a latency probe & keepalive.
4783 pingSend = true;
4785 if (pingSend) {
4786 uint64_t nonce = 0;
4787 while (nonce == 0) {
4788 GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
4790 pto->fPingQueued = false;
4791 pto->nPingUsecStart = GetTimeMicros();
4792 if (pto->nVersion > BIP0031_VERSION) {
4793 pto->nPingNonceSent = nonce;
4794 pto->PushMessage("ping", nonce);
4795 } else {
4796 // Peer is too old to support ping command with nonce, pong will never arrive.
4797 pto->nPingNonceSent = 0;
4798 pto->PushMessage("ping");
4802 TRY_LOCK(cs_main, lockMain); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
4803 if (!lockMain)
4804 return true;
4806 // Address refresh broadcast
4807 static int64_t nLastRebroadcast;
4808 if (!IsInitialBlockDownload() && (GetTime() - nLastRebroadcast > 24 * 60 * 60))
4810 LOCK(cs_vNodes);
4811 BOOST_FOREACH(CNode* pnode, vNodes)
4813 // Periodically clear addrKnown to allow refresh broadcasts
4814 if (nLastRebroadcast)
4815 pnode->addrKnown.clear();
4817 // Rebroadcast our address
4818 AdvertizeLocal(pnode);
4820 if (!vNodes.empty())
4821 nLastRebroadcast = GetTime();
4825 // Message: addr
4827 if (fSendTrickle)
4829 vector<CAddress> vAddr;
4830 vAddr.reserve(pto->vAddrToSend.size());
4831 BOOST_FOREACH(const CAddress& addr, pto->vAddrToSend)
4833 if (!pto->addrKnown.contains(addr.GetKey()))
4835 pto->addrKnown.insert(addr.GetKey());
4836 vAddr.push_back(addr);
4837 // receiver rejects addr messages larger than 1000
4838 if (vAddr.size() >= 1000)
4840 pto->PushMessage("addr", vAddr);
4841 vAddr.clear();
4845 pto->vAddrToSend.clear();
4846 if (!vAddr.empty())
4847 pto->PushMessage("addr", vAddr);
4850 CNodeState &state = *State(pto->GetId());
4851 if (state.fShouldBan) {
4852 if (pto->fWhitelisted)
4853 LogPrintf("Warning: not punishing whitelisted peer %s!\n", pto->addr.ToString());
4854 else {
4855 pto->fDisconnect = true;
4856 if (pto->addr.IsLocal())
4857 LogPrintf("Warning: not banning local peer %s!\n", pto->addr.ToString());
4858 else
4860 CNode::Ban(pto->addr, BanReasonNodeMisbehaving);
4863 state.fShouldBan = false;
4866 BOOST_FOREACH(const CBlockReject& reject, state.rejects)
4867 pto->PushMessage("reject", (string)"block", reject.chRejectCode, reject.strRejectReason, reject.hashBlock);
4868 state.rejects.clear();
4870 // Start block sync
4871 if (pindexBestHeader == NULL)
4872 pindexBestHeader = chainActive.Tip();
4873 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.
4874 if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) {
4875 // Only actively request headers from a single peer, unless we're close to today.
4876 if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
4877 state.fSyncStarted = true;
4878 nSyncStarted++;
4879 CBlockIndex *pindexStart = pindexBestHeader->pprev ? pindexBestHeader->pprev : pindexBestHeader;
4880 LogPrint("net", "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->id, pto->nStartingHeight);
4881 pto->PushMessage("getheaders", chainActive.GetLocator(pindexStart), uint256());
4885 // Resend wallet transactions that haven't gotten in a block yet
4886 // Except during reindex, importing and IBD, when old wallet
4887 // transactions become unconfirmed and spams other nodes.
4888 if (!fReindex && !fImporting && !IsInitialBlockDownload())
4890 GetMainSignals().Broadcast(nTimeBestReceived);
4894 // Message: inventory
4896 vector<CInv> vInv;
4897 vector<CInv> vInvWait;
4899 LOCK(pto->cs_inventory);
4900 vInv.reserve(pto->vInventoryToSend.size());
4901 vInvWait.reserve(pto->vInventoryToSend.size());
4902 BOOST_FOREACH(const CInv& inv, pto->vInventoryToSend)
4904 if (pto->setInventoryKnown.count(inv))
4905 continue;
4907 // trickle out tx inv to protect privacy
4908 if (inv.type == MSG_TX && !fSendTrickle)
4910 // 1/4 of tx invs blast to all immediately
4911 static uint256 hashSalt;
4912 if (hashSalt.IsNull())
4913 hashSalt = GetRandHash();
4914 uint256 hashRand = ArithToUint256(UintToArith256(inv.hash) ^ UintToArith256(hashSalt));
4915 hashRand = Hash(BEGIN(hashRand), END(hashRand));
4916 bool fTrickleWait = ((UintToArith256(hashRand) & 3) != 0);
4918 if (fTrickleWait)
4920 vInvWait.push_back(inv);
4921 continue;
4925 // returns true if wasn't already contained in the set
4926 if (pto->setInventoryKnown.insert(inv).second)
4928 vInv.push_back(inv);
4929 if (vInv.size() >= 1000)
4931 pto->PushMessage("inv", vInv);
4932 vInv.clear();
4936 pto->vInventoryToSend = vInvWait;
4938 if (!vInv.empty())
4939 pto->PushMessage("inv", vInv);
4941 // Detect whether we're stalling
4942 int64_t nNow = GetTimeMicros();
4943 if (!pto->fDisconnect && state.nStallingSince && state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) {
4944 // Stalling only triggers when the block download window cannot move. During normal steady state,
4945 // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
4946 // should only happen during initial block download.
4947 LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->id);
4948 pto->fDisconnect = true;
4950 // In case there is a block that has been in flight from this peer for (2 + 0.5 * N) times the block interval
4951 // (with N the number of validated blocks that were in flight at the time it was requested), disconnect due to
4952 // timeout. We compensate for in-flight blocks to prevent killing off peers due to our own downstream link
4953 // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
4954 // to unreasonably increase our timeout.
4955 // We also compare the block download timeout originally calculated against the time at which we'd disconnect
4956 // if we assumed the block were being requested now (ignoring blocks we've requested from this peer, since we're
4957 // only looking at this peer's oldest request). This way a large queue in the past doesn't result in a
4958 // permanently large window for this block to be delivered (ie if the number of blocks in flight is decreasing
4959 // more quickly than once every 5 minutes, then we'll shorten the download window for this block).
4960 if (!pto->fDisconnect && state.vBlocksInFlight.size() > 0) {
4961 QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
4962 int64_t nTimeoutIfRequestedNow = GetBlockTimeout(nNow, nQueuedValidatedHeaders - state.nBlocksInFlightValidHeaders, consensusParams);
4963 if (queuedBlock.nTimeDisconnect > nTimeoutIfRequestedNow) {
4964 LogPrint("net", "Reducing block download timeout for peer=%d block=%s, orig=%d new=%d\n", pto->id, queuedBlock.hash.ToString(), queuedBlock.nTimeDisconnect, nTimeoutIfRequestedNow);
4965 queuedBlock.nTimeDisconnect = nTimeoutIfRequestedNow;
4967 if (queuedBlock.nTimeDisconnect < nNow) {
4968 LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.hash.ToString(), pto->id);
4969 pto->fDisconnect = true;
4974 // Message: getdata (blocks)
4976 vector<CInv> vGetData;
4977 if (!pto->fDisconnect && !pto->fClient && (fFetch || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
4978 vector<CBlockIndex*> vToDownload;
4979 NodeId staller = -1;
4980 FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller);
4981 BOOST_FOREACH(CBlockIndex *pindex, vToDownload) {
4982 vGetData.push_back(CInv(MSG_BLOCK, pindex->GetBlockHash()));
4983 MarkBlockAsInFlight(pto->GetId(), pindex->GetBlockHash(), consensusParams, pindex);
4984 LogPrint("net", "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
4985 pindex->nHeight, pto->id);
4987 if (state.nBlocksInFlight == 0 && staller != -1) {
4988 if (State(staller)->nStallingSince == 0) {
4989 State(staller)->nStallingSince = nNow;
4990 LogPrint("net", "Stall started peer=%d\n", staller);
4996 // Message: getdata (non-blocks)
4998 while (!pto->fDisconnect && !pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
5000 const CInv& inv = (*pto->mapAskFor.begin()).second;
5001 if (!AlreadyHave(inv))
5003 if (fDebug)
5004 LogPrint("net", "Requesting %s peer=%d\n", inv.ToString(), pto->id);
5005 vGetData.push_back(inv);
5006 if (vGetData.size() >= 1000)
5008 pto->PushMessage("getdata", vGetData);
5009 vGetData.clear();
5012 pto->mapAskFor.erase(pto->mapAskFor.begin());
5014 if (!vGetData.empty())
5015 pto->PushMessage("getdata", vGetData);
5018 return true;
5021 std::string CBlockFileInfo::ToString() const {
5022 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));
5027 class CMainCleanup
5029 public:
5030 CMainCleanup() {}
5031 ~CMainCleanup() {
5032 // block headers
5033 BlockMap::iterator it1 = mapBlockIndex.begin();
5034 for (; it1 != mapBlockIndex.end(); it1++)
5035 delete (*it1).second;
5036 mapBlockIndex.clear();
5038 // orphan transactions
5039 mapOrphanTransactions.clear();
5040 mapOrphanTransactionsByPrev.clear();
5042 } instance_of_cmaincleanup;