Replace CValidationState param in ProcessNewBlock with BlockChecked
[bitcoinplatinum.git] / src / main.cpp
blob14923163fbf66b3bf64da1ca52fad3cab137909c
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "main.h"
8 #include "addrman.h"
9 #include "arith_uint256.h"
10 #include "blockencodings.h"
11 #include "chainparams.h"
12 #include "checkpoints.h"
13 #include "checkqueue.h"
14 #include "consensus/consensus.h"
15 #include "consensus/merkle.h"
16 #include "consensus/validation.h"
17 #include "hash.h"
18 #include "init.h"
19 #include "merkleblock.h"
20 #include "net.h"
21 #include "netbase.h"
22 #include "policy/fees.h"
23 #include "policy/policy.h"
24 #include "pow.h"
25 #include "primitives/block.h"
26 #include "primitives/transaction.h"
27 #include "random.h"
28 #include "script/script.h"
29 #include "script/sigcache.h"
30 #include "script/standard.h"
31 #include "tinyformat.h"
32 #include "txdb.h"
33 #include "txmempool.h"
34 #include "ui_interface.h"
35 #include "undo.h"
36 #include "util.h"
37 #include "utilmoneystr.h"
38 #include "utilstrencodings.h"
39 #include "validationinterface.h"
40 #include "versionbits.h"
42 #include <atomic>
43 #include <sstream>
45 #include <boost/algorithm/string/replace.hpp>
46 #include <boost/algorithm/string/join.hpp>
47 #include <boost/filesystem.hpp>
48 #include <boost/filesystem/fstream.hpp>
49 #include <boost/math/distributions/poisson.hpp>
50 #include <boost/thread.hpp>
52 using namespace std;
54 #if defined(NDEBUG)
55 # error "Bitcoin cannot be compiled without assertions."
56 #endif
58 /**
59 * Global state
62 CCriticalSection cs_main;
64 BlockMap mapBlockIndex;
65 CChain chainActive;
66 CBlockIndex *pindexBestHeader = NULL;
67 int64_t nTimeBestReceived = 0; // Used only to inform the wallet of when we last received a block
68 CWaitableCriticalSection csBestBlock;
69 CConditionVariable cvBlockChange;
70 int nScriptCheckThreads = 0;
71 bool fImporting = false;
72 bool fReindex = false;
73 bool fTxIndex = false;
74 bool fHavePruned = false;
75 bool fPruneMode = false;
76 bool fIsBareMultisigStd = DEFAULT_PERMIT_BAREMULTISIG;
77 bool fRequireStandard = true;
78 bool fCheckBlockIndex = false;
79 bool fCheckpointsEnabled = DEFAULT_CHECKPOINTS_ENABLED;
80 size_t nCoinCacheUsage = 5000 * 300;
81 uint64_t nPruneTarget = 0;
82 int64_t nMaxTipAge = DEFAULT_MAX_TIP_AGE;
83 bool fEnableReplacement = DEFAULT_ENABLE_REPLACEMENT;
86 CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
87 CAmount maxTxFee = DEFAULT_TRANSACTION_MAXFEE;
89 CTxMemPool mempool(::minRelayTxFee);
90 FeeFilterRounder filterRounder(::minRelayTxFee);
92 struct IteratorComparator
94 template<typename I>
95 bool operator()(const I& a, const I& b)
97 return &(*a) < &(*b);
101 struct COrphanTx {
102 CTransaction tx;
103 NodeId fromPeer;
104 int64_t nTimeExpire;
106 map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(cs_main);
107 map<COutPoint, set<map<uint256, COrphanTx>::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(cs_main);
108 void EraseOrphansFor(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
110 static void CheckBlockIndex(const Consensus::Params& consensusParams);
112 /** Constant stuff for coinbase transactions we create: */
113 CScript COINBASE_FLAGS;
115 const string strMessageMagic = "Bitcoin Signed Message:\n";
117 static const uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL; // SHA256("main address relay")[0:8]
119 // Internal stuff
120 namespace {
122 struct CBlockIndexWorkComparator
124 bool operator()(CBlockIndex *pa, CBlockIndex *pb) const {
125 // First sort by most total work, ...
126 if (pa->nChainWork > pb->nChainWork) return false;
127 if (pa->nChainWork < pb->nChainWork) return true;
129 // ... then by earliest time received, ...
130 if (pa->nSequenceId < pb->nSequenceId) return false;
131 if (pa->nSequenceId > pb->nSequenceId) return true;
133 // Use pointer address as tie breaker (should only happen with blocks
134 // loaded from disk, as those all have id 0).
135 if (pa < pb) return false;
136 if (pa > pb) return true;
138 // Identical blocks.
139 return false;
143 CBlockIndex *pindexBestInvalid;
146 * The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS (for itself and all ancestors) and
147 * as good as our current tip or better. Entries may be failed, though, and pruning nodes may be
148 * missing the data for the block.
150 set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexCandidates;
151 /** Number of nodes with fSyncStarted. */
152 int nSyncStarted = 0;
153 /** All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
154 * Pruned nodes may have entries where B is missing data.
156 multimap<CBlockIndex*, CBlockIndex*> mapBlocksUnlinked;
158 CCriticalSection cs_LastBlockFile;
159 std::vector<CBlockFileInfo> vinfoBlockFile;
160 int nLastBlockFile = 0;
161 /** Global flag to indicate we should check to see if there are
162 * block/undo files that should be deleted. Set on startup
163 * or if we allocate more file space when we're in prune mode
165 bool fCheckForPruning = false;
168 * Every received block is assigned a unique and increasing identifier, so we
169 * know which one to give priority in case of a fork.
171 CCriticalSection cs_nBlockSequenceId;
172 /** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
173 int32_t nBlockSequenceId = 1;
174 /** Decreasing counter (used by subsequent preciousblock calls). */
175 int32_t nBlockReverseSequenceId = -1;
176 /** chainwork for the last block that preciousblock has been applied to. */
177 arith_uint256 nLastPreciousChainwork = 0;
180 * Sources of received blocks, saved to be able to send them reject
181 * messages or ban them when processing happens afterwards. Protected by
182 * cs_main.
183 * Set mapBlockSource[hash].second to false if the node should not be
184 * punished if the block is invalid.
186 map<uint256, std::pair<NodeId, bool>> mapBlockSource;
189 * Filter for transactions that were recently rejected by
190 * AcceptToMemoryPool. These are not rerequested until the chain tip
191 * changes, at which point the entire filter is reset. Protected by
192 * cs_main.
194 * Without this filter we'd be re-requesting txs from each of our peers,
195 * increasing bandwidth consumption considerably. For instance, with 100
196 * peers, half of which relay a tx we don't accept, that might be a 50x
197 * bandwidth increase. A flooding attacker attempting to roll-over the
198 * filter using minimum-sized, 60byte, transactions might manage to send
199 * 1000/sec if we have fast peers, so we pick 120,000 to give our peers a
200 * two minute window to send invs to us.
202 * Decreasing the false positive rate is fairly cheap, so we pick one in a
203 * million to make it highly unlikely for users to have issues with this
204 * filter.
206 * Memory used: 1.3 MB
208 std::unique_ptr<CRollingBloomFilter> recentRejects;
209 uint256 hashRecentRejectsChainTip;
211 /** Blocks that are in flight, and that are in the queue to be downloaded. Protected by cs_main. */
212 struct QueuedBlock {
213 uint256 hash;
214 CBlockIndex* pindex; //!< Optional.
215 bool fValidatedHeaders; //!< Whether this block has validated headers at the time of request.
216 std::unique_ptr<PartiallyDownloadedBlock> partialBlock; //!< Optional, used for CMPCTBLOCK downloads
218 map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
220 /** Stack of nodes which we have set to announce using compact blocks */
221 list<NodeId> lNodesAnnouncingHeaderAndIDs;
223 /** Number of preferable block download peers. */
224 int nPreferredDownload = 0;
226 /** Dirty block index entries. */
227 set<CBlockIndex*> setDirtyBlockIndex;
229 /** Dirty block file entries. */
230 set<int> setDirtyFileInfo;
232 /** Number of peers from which we're downloading blocks. */
233 int nPeersWithValidatedDownloads = 0;
235 /** Relay map, protected by cs_main. */
236 typedef std::map<uint256, std::shared_ptr<const CTransaction>> MapRelay;
237 MapRelay mapRelay;
238 /** Expiration-time ordered list of (expire time, relay map entry) pairs, protected by cs_main). */
239 std::deque<std::pair<int64_t, MapRelay::iterator>> vRelayExpiration;
240 } // anon namespace
242 //////////////////////////////////////////////////////////////////////////////
244 // Registration of network node signals.
247 namespace {
249 struct CBlockReject {
250 unsigned char chRejectCode;
251 string strRejectReason;
252 uint256 hashBlock;
256 * Maintain validation-specific state about nodes, protected by cs_main, instead
257 * by CNode's own locks. This simplifies asynchronous operation, where
258 * processing of incoming data is done after the ProcessMessage call returns,
259 * and we're no longer holding the node's locks.
261 struct CNodeState {
262 //! The peer's address
263 const CService address;
264 //! Whether we have a fully established connection.
265 bool fCurrentlyConnected;
266 //! Accumulated misbehaviour score for this peer.
267 int nMisbehavior;
268 //! Whether this peer should be disconnected and banned (unless whitelisted).
269 bool fShouldBan;
270 //! String name of this peer (debugging/logging purposes).
271 const std::string name;
272 //! List of asynchronously-determined block rejections to notify this peer about.
273 std::vector<CBlockReject> rejects;
274 //! The best known block we know this peer has announced.
275 CBlockIndex *pindexBestKnownBlock;
276 //! The hash of the last unknown block this peer has announced.
277 uint256 hashLastUnknownBlock;
278 //! The last full block we both have.
279 CBlockIndex *pindexLastCommonBlock;
280 //! The best header we have sent our peer.
281 CBlockIndex *pindexBestHeaderSent;
282 //! Length of current-streak of unconnecting headers announcements
283 int nUnconnectingHeaders;
284 //! Whether we've started headers synchronization with this peer.
285 bool fSyncStarted;
286 //! Since when we're stalling block download progress (in microseconds), or 0.
287 int64_t nStallingSince;
288 list<QueuedBlock> vBlocksInFlight;
289 //! When the first entry in vBlocksInFlight started downloading. Don't care when vBlocksInFlight is empty.
290 int64_t nDownloadingSince;
291 int nBlocksInFlight;
292 int nBlocksInFlightValidHeaders;
293 //! Whether we consider this a preferred download peer.
294 bool fPreferredDownload;
295 //! Whether this peer wants invs or headers (when possible) for block announcements.
296 bool fPreferHeaders;
297 //! Whether this peer wants invs or cmpctblocks (when possible) for block announcements.
298 bool fPreferHeaderAndIDs;
300 * Whether this peer will send us cmpctblocks if we request them.
301 * This is not used to gate request logic, as we really only care about fSupportsDesiredCmpctVersion,
302 * but is used as a flag to "lock in" the version of compact blocks (fWantsCmpctWitness) we send.
304 bool fProvidesHeaderAndIDs;
305 //! Whether this peer can give us witnesses
306 bool fHaveWitness;
307 //! Whether this peer wants witnesses in cmpctblocks/blocktxns
308 bool fWantsCmpctWitness;
310 * If we've announced NODE_WITNESS to this peer: whether the peer sends witnesses in cmpctblocks/blocktxns,
311 * otherwise: whether this peer sends non-witnesses in cmpctblocks/blocktxns.
313 bool fSupportsDesiredCmpctVersion;
315 CNodeState(CAddress addrIn, std::string addrNameIn) : address(addrIn), name(addrNameIn) {
316 fCurrentlyConnected = false;
317 nMisbehavior = 0;
318 fShouldBan = false;
319 pindexBestKnownBlock = NULL;
320 hashLastUnknownBlock.SetNull();
321 pindexLastCommonBlock = NULL;
322 pindexBestHeaderSent = NULL;
323 nUnconnectingHeaders = 0;
324 fSyncStarted = false;
325 nStallingSince = 0;
326 nDownloadingSince = 0;
327 nBlocksInFlight = 0;
328 nBlocksInFlightValidHeaders = 0;
329 fPreferredDownload = false;
330 fPreferHeaders = false;
331 fPreferHeaderAndIDs = false;
332 fProvidesHeaderAndIDs = false;
333 fHaveWitness = false;
334 fWantsCmpctWitness = false;
335 fSupportsDesiredCmpctVersion = false;
339 /** Map maintaining per-node state. Requires cs_main. */
340 map<NodeId, CNodeState> mapNodeState;
342 // Requires cs_main.
343 CNodeState *State(NodeId pnode) {
344 map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
345 if (it == mapNodeState.end())
346 return NULL;
347 return &it->second;
350 void UpdatePreferredDownload(CNode* node, CNodeState* state)
352 nPreferredDownload -= state->fPreferredDownload;
354 // Whether this node should be marked as a preferred download node.
355 state->fPreferredDownload = (!node->fInbound || node->fWhitelisted) && !node->fOneShot && !node->fClient;
357 nPreferredDownload += state->fPreferredDownload;
360 void PushNodeVersion(CNode *pnode, CConnman& connman, int64_t nTime)
362 ServiceFlags nLocalNodeServices = pnode->GetLocalServices();
363 uint64_t nonce = pnode->GetLocalNonce();
364 int nNodeStartingHeight = pnode->GetMyStartingHeight();
365 NodeId nodeid = pnode->GetId();
366 CAddress addr = pnode->addr;
368 CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices));
369 CAddress addrMe = CAddress(CService(), nLocalNodeServices);
371 connman.PushMessageWithVersion(pnode, INIT_PROTO_VERSION, NetMsgType::VERSION, PROTOCOL_VERSION, (uint64_t)nLocalNodeServices, nTime, addrYou, addrMe,
372 nonce, strSubVersion, nNodeStartingHeight, ::fRelayTxes);
374 if (fLogIPs)
375 LogPrint("net", "send version message: version %d, blocks=%d, us=%s, them=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), addrYou.ToString(), nodeid);
376 else
377 LogPrint("net", "send version message: version %d, blocks=%d, us=%s, peer=%d\n", PROTOCOL_VERSION, nNodeStartingHeight, addrMe.ToString(), nodeid);
380 void InitializeNode(CNode *pnode, CConnman& connman) {
381 CAddress addr = pnode->addr;
382 std::string addrName = pnode->addrName;
383 NodeId nodeid = pnode->GetId();
385 LOCK(cs_main);
386 mapNodeState.emplace_hint(mapNodeState.end(), std::piecewise_construct, std::forward_as_tuple(nodeid), std::forward_as_tuple(addr, std::move(addrName)));
388 if(!pnode->fInbound)
389 PushNodeVersion(pnode, connman, GetTime());
392 void FinalizeNode(NodeId nodeid, bool& fUpdateConnectionTime) {
393 fUpdateConnectionTime = false;
394 LOCK(cs_main);
395 CNodeState *state = State(nodeid);
397 if (state->fSyncStarted)
398 nSyncStarted--;
400 if (state->nMisbehavior == 0 && state->fCurrentlyConnected) {
401 fUpdateConnectionTime = true;
404 BOOST_FOREACH(const QueuedBlock& entry, state->vBlocksInFlight) {
405 mapBlocksInFlight.erase(entry.hash);
407 EraseOrphansFor(nodeid);
408 nPreferredDownload -= state->fPreferredDownload;
409 nPeersWithValidatedDownloads -= (state->nBlocksInFlightValidHeaders != 0);
410 assert(nPeersWithValidatedDownloads >= 0);
412 mapNodeState.erase(nodeid);
414 if (mapNodeState.empty()) {
415 // Do a consistency check after the last peer is removed.
416 assert(mapBlocksInFlight.empty());
417 assert(nPreferredDownload == 0);
418 assert(nPeersWithValidatedDownloads == 0);
422 // Requires cs_main.
423 // Returns a bool indicating whether we requested this block.
424 // Also used if a block was /not/ received and timed out or started with another peer
425 bool MarkBlockAsReceived(const uint256& hash) {
426 map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
427 if (itInFlight != mapBlocksInFlight.end()) {
428 CNodeState *state = State(itInFlight->second.first);
429 state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders;
430 if (state->nBlocksInFlightValidHeaders == 0 && itInFlight->second.second->fValidatedHeaders) {
431 // Last validated block on the queue was received.
432 nPeersWithValidatedDownloads--;
434 if (state->vBlocksInFlight.begin() == itInFlight->second.second) {
435 // First block on the queue was received, update the start download time for the next one
436 state->nDownloadingSince = std::max(state->nDownloadingSince, GetTimeMicros());
438 state->vBlocksInFlight.erase(itInFlight->second.second);
439 state->nBlocksInFlight--;
440 state->nStallingSince = 0;
441 mapBlocksInFlight.erase(itInFlight);
442 return true;
444 return false;
447 // Requires cs_main.
448 // returns false, still setting pit, if the block was already in flight from the same peer
449 // pit will only be valid as long as the same cs_main lock is being held
450 bool MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const Consensus::Params& consensusParams, CBlockIndex *pindex = NULL, list<QueuedBlock>::iterator **pit = NULL) {
451 CNodeState *state = State(nodeid);
452 assert(state != NULL);
454 // Short-circuit most stuff in case its from the same node
455 map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
456 if (itInFlight != mapBlocksInFlight.end() && itInFlight->second.first == nodeid) {
457 *pit = &itInFlight->second.second;
458 return false;
461 // Make sure it's not listed somewhere already.
462 MarkBlockAsReceived(hash);
464 list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(),
465 {hash, pindex, pindex != NULL, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&mempool) : NULL)});
466 state->nBlocksInFlight++;
467 state->nBlocksInFlightValidHeaders += it->fValidatedHeaders;
468 if (state->nBlocksInFlight == 1) {
469 // We're starting a block download (batch) from this peer.
470 state->nDownloadingSince = GetTimeMicros();
472 if (state->nBlocksInFlightValidHeaders == 1 && pindex != NULL) {
473 nPeersWithValidatedDownloads++;
475 itInFlight = mapBlocksInFlight.insert(std::make_pair(hash, std::make_pair(nodeid, it))).first;
476 if (pit)
477 *pit = &itInFlight->second.second;
478 return true;
481 /** Check whether the last unknown block a peer advertised is not yet known. */
482 void ProcessBlockAvailability(NodeId nodeid) {
483 CNodeState *state = State(nodeid);
484 assert(state != NULL);
486 if (!state->hashLastUnknownBlock.IsNull()) {
487 BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
488 if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
489 if (state->pindexBestKnownBlock == NULL || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
490 state->pindexBestKnownBlock = itOld->second;
491 state->hashLastUnknownBlock.SetNull();
496 /** Update tracking information about which blocks a peer is assumed to have. */
497 void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
498 CNodeState *state = State(nodeid);
499 assert(state != NULL);
501 ProcessBlockAvailability(nodeid);
503 BlockMap::iterator it = mapBlockIndex.find(hash);
504 if (it != mapBlockIndex.end() && it->second->nChainWork > 0) {
505 // An actually better block was announced.
506 if (state->pindexBestKnownBlock == NULL || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
507 state->pindexBestKnownBlock = it->second;
508 } else {
509 // An unknown block was announced; just assume that the latest one is the best one.
510 state->hashLastUnknownBlock = hash;
514 void MaybeSetPeerAsAnnouncingHeaderAndIDs(const CNodeState* nodestate, CNode* pfrom, CConnman& connman) {
515 if (!nodestate->fSupportsDesiredCmpctVersion) {
516 // Never ask from peers who can't provide witnesses.
517 return;
519 if (nodestate->fProvidesHeaderAndIDs) {
520 for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
521 if (*it == pfrom->GetId()) {
522 lNodesAnnouncingHeaderAndIDs.erase(it);
523 lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
524 return;
527 bool fAnnounceUsingCMPCTBLOCK = false;
528 uint64_t nCMPCTBLOCKVersion = (pfrom->GetLocalServices() & NODE_WITNESS) ? 2 : 1;
529 if (lNodesAnnouncingHeaderAndIDs.size() >= 3) {
530 // As per BIP152, we only get 3 of our peers to announce
531 // blocks using compact encodings.
532 bool found = connman.ForNode(lNodesAnnouncingHeaderAndIDs.front(), [&connman, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion](CNode* pnodeStop){
533 connman.PushMessage(pnodeStop, NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion);
534 return true;
536 if(found)
537 lNodesAnnouncingHeaderAndIDs.pop_front();
539 fAnnounceUsingCMPCTBLOCK = true;
540 connman.PushMessage(pfrom, NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion);
541 lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
545 // Requires cs_main
546 bool CanDirectFetch(const Consensus::Params &consensusParams)
548 return chainActive.Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams.nPowTargetSpacing * 20;
551 // Requires cs_main
552 bool PeerHasHeader(CNodeState *state, CBlockIndex *pindex)
554 if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock->GetAncestor(pindex->nHeight))
555 return true;
556 if (state->pindexBestHeaderSent && pindex == state->pindexBestHeaderSent->GetAncestor(pindex->nHeight))
557 return true;
558 return false;
561 /** Find the last common ancestor two blocks have.
562 * Both pa and pb must be non-NULL. */
563 CBlockIndex* LastCommonAncestor(CBlockIndex* pa, CBlockIndex* pb) {
564 if (pa->nHeight > pb->nHeight) {
565 pa = pa->GetAncestor(pb->nHeight);
566 } else if (pb->nHeight > pa->nHeight) {
567 pb = pb->GetAncestor(pa->nHeight);
570 while (pa != pb && pa && pb) {
571 pa = pa->pprev;
572 pb = pb->pprev;
575 // Eventually all chain branches meet at the genesis block.
576 assert(pa == pb);
577 return pa;
580 /** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
581 * at most count entries. */
582 void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) {
583 if (count == 0)
584 return;
586 vBlocks.reserve(vBlocks.size() + count);
587 CNodeState *state = State(nodeid);
588 assert(state != NULL);
590 // Make sure pindexBestKnownBlock is up to date, we'll need it.
591 ProcessBlockAvailability(nodeid);
593 if (state->pindexBestKnownBlock == NULL || state->pindexBestKnownBlock->nChainWork < chainActive.Tip()->nChainWork) {
594 // This peer has nothing interesting.
595 return;
598 if (state->pindexLastCommonBlock == NULL) {
599 // Bootstrap quickly by guessing a parent of our best tip is the forking point.
600 // Guessing wrong in either direction is not a problem.
601 state->pindexLastCommonBlock = chainActive[std::min(state->pindexBestKnownBlock->nHeight, chainActive.Height())];
604 // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
605 // of its current tip anymore. Go back enough to fix that.
606 state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
607 if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
608 return;
610 std::vector<CBlockIndex*> vToFetch;
611 CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
612 // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
613 // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
614 // download that next block if the window were 1 larger.
615 int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
616 int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
617 NodeId waitingfor = -1;
618 while (pindexWalk->nHeight < nMaxHeight) {
619 // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
620 // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
621 // as iterating over ~100 CBlockIndex* entries anyway.
622 int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
623 vToFetch.resize(nToFetch);
624 pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
625 vToFetch[nToFetch - 1] = pindexWalk;
626 for (unsigned int i = nToFetch - 1; i > 0; i--) {
627 vToFetch[i - 1] = vToFetch[i]->pprev;
630 // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
631 // are not yet downloaded and not in flight to vBlocks. In the mean time, update
632 // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
633 // already part of our chain (and therefore don't need it even if pruned).
634 BOOST_FOREACH(CBlockIndex* pindex, vToFetch) {
635 if (!pindex->IsValid(BLOCK_VALID_TREE)) {
636 // We consider the chain that this peer is on invalid.
637 return;
639 if (!State(nodeid)->fHaveWitness && IsWitnessEnabled(pindex->pprev, consensusParams)) {
640 // We wouldn't download this block or its descendants from this peer.
641 return;
643 if (pindex->nStatus & BLOCK_HAVE_DATA || chainActive.Contains(pindex)) {
644 if (pindex->nChainTx)
645 state->pindexLastCommonBlock = pindex;
646 } else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
647 // The block is not already downloaded, and not yet in flight.
648 if (pindex->nHeight > nWindowEnd) {
649 // We reached the end of the window.
650 if (vBlocks.size() == 0 && waitingfor != nodeid) {
651 // We aren't able to fetch anything, but we would be if the download window was one larger.
652 nodeStaller = waitingfor;
654 return;
656 vBlocks.push_back(pindex);
657 if (vBlocks.size() == count) {
658 return;
660 } else if (waitingfor == -1) {
661 // This is the first already-in-flight block.
662 waitingfor = mapBlocksInFlight[pindex->GetBlockHash()].first;
668 } // anon namespace
670 bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
671 LOCK(cs_main);
672 CNodeState *state = State(nodeid);
673 if (state == NULL)
674 return false;
675 stats.nMisbehavior = state->nMisbehavior;
676 stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
677 stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
678 BOOST_FOREACH(const QueuedBlock& queue, state->vBlocksInFlight) {
679 if (queue.pindex)
680 stats.vHeightInFlight.push_back(queue.pindex->nHeight);
682 return true;
685 void RegisterNodeSignals(CNodeSignals& nodeSignals)
687 nodeSignals.ProcessMessages.connect(&ProcessMessages);
688 nodeSignals.SendMessages.connect(&SendMessages);
689 nodeSignals.InitializeNode.connect(&InitializeNode);
690 nodeSignals.FinalizeNode.connect(&FinalizeNode);
693 void UnregisterNodeSignals(CNodeSignals& nodeSignals)
695 nodeSignals.ProcessMessages.disconnect(&ProcessMessages);
696 nodeSignals.SendMessages.disconnect(&SendMessages);
697 nodeSignals.InitializeNode.disconnect(&InitializeNode);
698 nodeSignals.FinalizeNode.disconnect(&FinalizeNode);
701 CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator)
703 // Find the first block the caller has in the main chain
704 BOOST_FOREACH(const uint256& hash, locator.vHave) {
705 BlockMap::iterator mi = mapBlockIndex.find(hash);
706 if (mi != mapBlockIndex.end())
708 CBlockIndex* pindex = (*mi).second;
709 if (chain.Contains(pindex))
710 return pindex;
711 if (pindex->GetAncestor(chain.Height()) == chain.Tip()) {
712 return chain.Tip();
716 return chain.Genesis();
719 CCoinsViewCache *pcoinsTip = NULL;
720 CBlockTreeDB *pblocktree = NULL;
722 enum FlushStateMode {
723 FLUSH_STATE_NONE,
724 FLUSH_STATE_IF_NEEDED,
725 FLUSH_STATE_PERIODIC,
726 FLUSH_STATE_ALWAYS
729 // See definition for documentation
730 bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode);
732 //////////////////////////////////////////////////////////////////////////////
734 // mapOrphanTransactions
737 bool AddOrphanTx(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
739 uint256 hash = tx.GetHash();
740 if (mapOrphanTransactions.count(hash))
741 return false;
743 // Ignore big transactions, to avoid a
744 // send-big-orphans memory exhaustion attack. If a peer has a legitimate
745 // large transaction with a missing parent then we assume
746 // it will rebroadcast it later, after the parent transaction(s)
747 // have been mined or received.
748 // 100 orphans, each of which is at most 99,999 bytes big is
749 // at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
750 unsigned int sz = GetTransactionWeight(tx);
751 if (sz >= MAX_STANDARD_TX_WEIGHT)
753 LogPrint("mempool", "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
754 return false;
757 auto ret = mapOrphanTransactions.emplace(hash, COrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME});
758 assert(ret.second);
759 BOOST_FOREACH(const CTxIn& txin, tx.vin) {
760 mapOrphanTransactionsByPrev[txin.prevout].insert(ret.first);
763 LogPrint("mempool", "stored orphan tx %s (mapsz %u outsz %u)\n", hash.ToString(),
764 mapOrphanTransactions.size(), mapOrphanTransactionsByPrev.size());
765 return true;
768 int static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
770 map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
771 if (it == mapOrphanTransactions.end())
772 return 0;
773 BOOST_FOREACH(const CTxIn& txin, it->second.tx.vin)
775 auto itPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
776 if (itPrev == mapOrphanTransactionsByPrev.end())
777 continue;
778 itPrev->second.erase(it);
779 if (itPrev->second.empty())
780 mapOrphanTransactionsByPrev.erase(itPrev);
782 mapOrphanTransactions.erase(it);
783 return 1;
786 void EraseOrphansFor(NodeId peer)
788 int nErased = 0;
789 map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
790 while (iter != mapOrphanTransactions.end())
792 map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
793 if (maybeErase->second.fromPeer == peer)
795 nErased += EraseOrphanTx(maybeErase->second.tx.GetHash());
798 if (nErased > 0) LogPrint("mempool", "Erased %d orphan tx from peer %d\n", nErased, peer);
802 unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
804 unsigned int nEvicted = 0;
805 static int64_t nNextSweep;
806 int64_t nNow = GetTime();
807 if (nNextSweep <= nNow) {
808 // Sweep out expired orphan pool entries:
809 int nErased = 0;
810 int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
811 map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
812 while (iter != mapOrphanTransactions.end())
814 map<uint256, COrphanTx>::iterator maybeErase = iter++;
815 if (maybeErase->second.nTimeExpire <= nNow) {
816 nErased += EraseOrphanTx(maybeErase->second.tx.GetHash());
817 } else {
818 nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
821 // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
822 nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
823 if (nErased > 0) LogPrint("mempool", "Erased %d orphan tx due to expiration\n", nErased);
825 while (mapOrphanTransactions.size() > nMaxOrphans)
827 // Evict a random orphan:
828 uint256 randomhash = GetRandHash();
829 map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.lower_bound(randomhash);
830 if (it == mapOrphanTransactions.end())
831 it = mapOrphanTransactions.begin();
832 EraseOrphanTx(it->first);
833 ++nEvicted;
835 return nEvicted;
838 bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
840 if (tx.nLockTime == 0)
841 return true;
842 if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
843 return true;
844 for (const auto& txin : tx.vin) {
845 if (!(txin.nSequence == CTxIn::SEQUENCE_FINAL))
846 return false;
848 return true;
851 bool CheckFinalTx(const CTransaction &tx, int flags)
853 AssertLockHeld(cs_main);
855 // By convention a negative value for flags indicates that the
856 // current network-enforced consensus rules should be used. In
857 // a future soft-fork scenario that would mean checking which
858 // rules would be enforced for the next block and setting the
859 // appropriate flags. At the present time no soft-forks are
860 // scheduled, so no flags are set.
861 flags = std::max(flags, 0);
863 // CheckFinalTx() uses chainActive.Height()+1 to evaluate
864 // nLockTime because when IsFinalTx() is called within
865 // CBlock::AcceptBlock(), the height of the block *being*
866 // evaluated is what is used. Thus if we want to know if a
867 // transaction can be part of the *next* block, we need to call
868 // IsFinalTx() with one more than chainActive.Height().
869 const int nBlockHeight = chainActive.Height() + 1;
871 // BIP113 will require that time-locked transactions have nLockTime set to
872 // less than the median time of the previous block they're contained in.
873 // When the next block is created its previous block will be the current
874 // chain tip, so we use that to calculate the median time passed to
875 // IsFinalTx() if LOCKTIME_MEDIAN_TIME_PAST is set.
876 const int64_t nBlockTime = (flags & LOCKTIME_MEDIAN_TIME_PAST)
877 ? chainActive.Tip()->GetMedianTimePast()
878 : GetAdjustedTime();
880 return IsFinalTx(tx, nBlockHeight, nBlockTime);
884 * Calculates the block height and previous block's median time past at
885 * which the transaction will be considered final in the context of BIP 68.
886 * Also removes from the vector of input heights any entries which did not
887 * correspond to sequence locked inputs as they do not affect the calculation.
889 static std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector<int>* prevHeights, const CBlockIndex& block)
891 assert(prevHeights->size() == tx.vin.size());
893 // Will be set to the equivalent height- and time-based nLockTime
894 // values that would be necessary to satisfy all relative lock-
895 // time constraints given our view of block chain history.
896 // The semantics of nLockTime are the last invalid height/time, so
897 // use -1 to have the effect of any height or time being valid.
898 int nMinHeight = -1;
899 int64_t nMinTime = -1;
901 // tx.nVersion is signed integer so requires cast to unsigned otherwise
902 // we would be doing a signed comparison and half the range of nVersion
903 // wouldn't support BIP 68.
904 bool fEnforceBIP68 = static_cast<uint32_t>(tx.nVersion) >= 2
905 && flags & LOCKTIME_VERIFY_SEQUENCE;
907 // Do not enforce sequence numbers as a relative lock time
908 // unless we have been instructed to
909 if (!fEnforceBIP68) {
910 return std::make_pair(nMinHeight, nMinTime);
913 for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
914 const CTxIn& txin = tx.vin[txinIndex];
916 // Sequence numbers with the most significant bit set are not
917 // treated as relative lock-times, nor are they given any
918 // consensus-enforced meaning at this point.
919 if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) {
920 // The height of this input is not relevant for sequence locks
921 (*prevHeights)[txinIndex] = 0;
922 continue;
925 int nCoinHeight = (*prevHeights)[txinIndex];
927 if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) {
928 int64_t nCoinTime = block.GetAncestor(std::max(nCoinHeight-1, 0))->GetMedianTimePast();
929 // NOTE: Subtract 1 to maintain nLockTime semantics
930 // BIP 68 relative lock times have the semantics of calculating
931 // the first block or time at which the transaction would be
932 // valid. When calculating the effective block time or height
933 // for the entire transaction, we switch to using the
934 // semantics of nLockTime which is the last invalid block
935 // time or height. Thus we subtract 1 from the calculated
936 // time or height.
938 // Time-based relative lock-times are measured from the
939 // smallest allowed timestamp of the block containing the
940 // txout being spent, which is the median time past of the
941 // block prior.
942 nMinTime = std::max(nMinTime, nCoinTime + (int64_t)((txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) << CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) - 1);
943 } else {
944 nMinHeight = std::max(nMinHeight, nCoinHeight + (int)(txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) - 1);
948 return std::make_pair(nMinHeight, nMinTime);
951 static bool EvaluateSequenceLocks(const CBlockIndex& block, std::pair<int, int64_t> lockPair)
953 assert(block.pprev);
954 int64_t nBlockTime = block.pprev->GetMedianTimePast();
955 if (lockPair.first >= block.nHeight || lockPair.second >= nBlockTime)
956 return false;
958 return true;
961 bool SequenceLocks(const CTransaction &tx, int flags, std::vector<int>* prevHeights, const CBlockIndex& block)
963 return EvaluateSequenceLocks(block, CalculateSequenceLocks(tx, flags, prevHeights, block));
966 bool TestLockPointValidity(const LockPoints* lp)
968 AssertLockHeld(cs_main);
969 assert(lp);
970 // If there are relative lock times then the maxInputBlock will be set
971 // If there are no relative lock times, the LockPoints don't depend on the chain
972 if (lp->maxInputBlock) {
973 // Check whether chainActive is an extension of the block at which the LockPoints
974 // calculation was valid. If not LockPoints are no longer valid
975 if (!chainActive.Contains(lp->maxInputBlock)) {
976 return false;
980 // LockPoints still valid
981 return true;
984 bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool useExistingLockPoints)
986 AssertLockHeld(cs_main);
987 AssertLockHeld(mempool.cs);
989 CBlockIndex* tip = chainActive.Tip();
990 CBlockIndex index;
991 index.pprev = tip;
992 // CheckSequenceLocks() uses chainActive.Height()+1 to evaluate
993 // height based locks because when SequenceLocks() is called within
994 // ConnectBlock(), the height of the block *being*
995 // evaluated is what is used.
996 // Thus if we want to know if a transaction can be part of the
997 // *next* block, we need to use one more than chainActive.Height()
998 index.nHeight = tip->nHeight + 1;
1000 std::pair<int, int64_t> lockPair;
1001 if (useExistingLockPoints) {
1002 assert(lp);
1003 lockPair.first = lp->height;
1004 lockPair.second = lp->time;
1006 else {
1007 // pcoinsTip contains the UTXO set for chainActive.Tip()
1008 CCoinsViewMemPool viewMemPool(pcoinsTip, mempool);
1009 std::vector<int> prevheights;
1010 prevheights.resize(tx.vin.size());
1011 for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
1012 const CTxIn& txin = tx.vin[txinIndex];
1013 CCoins coins;
1014 if (!viewMemPool.GetCoins(txin.prevout.hash, coins)) {
1015 return error("%s: Missing input", __func__);
1017 if (coins.nHeight == MEMPOOL_HEIGHT) {
1018 // Assume all mempool transaction confirm in the next block
1019 prevheights[txinIndex] = tip->nHeight + 1;
1020 } else {
1021 prevheights[txinIndex] = coins.nHeight;
1024 lockPair = CalculateSequenceLocks(tx, flags, &prevheights, index);
1025 if (lp) {
1026 lp->height = lockPair.first;
1027 lp->time = lockPair.second;
1028 // Also store the hash of the block with the highest height of
1029 // all the blocks which have sequence locked prevouts.
1030 // This hash needs to still be on the chain
1031 // for these LockPoint calculations to be valid
1032 // Note: It is impossible to correctly calculate a maxInputBlock
1033 // if any of the sequence locked inputs depend on unconfirmed txs,
1034 // except in the special case where the relative lock time/height
1035 // is 0, which is equivalent to no sequence lock. Since we assume
1036 // input height of tip+1 for mempool txs and test the resulting
1037 // lockPair from CalculateSequenceLocks against tip+1. We know
1038 // EvaluateSequenceLocks will fail if there was a non-zero sequence
1039 // lock on a mempool input, so we can use the return value of
1040 // CheckSequenceLocks to indicate the LockPoints validity
1041 int maxInputHeight = 0;
1042 BOOST_FOREACH(int height, prevheights) {
1043 // Can ignore mempool inputs since we'll fail if they had non-zero locks
1044 if (height != tip->nHeight+1) {
1045 maxInputHeight = std::max(maxInputHeight, height);
1048 lp->maxInputBlock = tip->GetAncestor(maxInputHeight);
1051 return EvaluateSequenceLocks(index, lockPair);
1055 unsigned int GetLegacySigOpCount(const CTransaction& tx)
1057 unsigned int nSigOps = 0;
1058 for (const auto& txin : tx.vin)
1060 nSigOps += txin.scriptSig.GetSigOpCount(false);
1062 for (const auto& txout : tx.vout)
1064 nSigOps += txout.scriptPubKey.GetSigOpCount(false);
1066 return nSigOps;
1069 unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs)
1071 if (tx.IsCoinBase())
1072 return 0;
1074 unsigned int nSigOps = 0;
1075 for (unsigned int i = 0; i < tx.vin.size(); i++)
1077 const CTxOut &prevout = inputs.GetOutputFor(tx.vin[i]);
1078 if (prevout.scriptPubKey.IsPayToScriptHash())
1079 nSigOps += prevout.scriptPubKey.GetSigOpCount(tx.vin[i].scriptSig);
1081 return nSigOps;
1084 int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& inputs, int flags)
1086 int64_t nSigOps = GetLegacySigOpCount(tx) * WITNESS_SCALE_FACTOR;
1088 if (tx.IsCoinBase())
1089 return nSigOps;
1091 if (flags & SCRIPT_VERIFY_P2SH) {
1092 nSigOps += GetP2SHSigOpCount(tx, inputs) * WITNESS_SCALE_FACTOR;
1095 for (unsigned int i = 0; i < tx.vin.size(); i++)
1097 const CTxOut &prevout = inputs.GetOutputFor(tx.vin[i]);
1098 nSigOps += CountWitnessSigOps(tx.vin[i].scriptSig, prevout.scriptPubKey, i < tx.wit.vtxinwit.size() ? &tx.wit.vtxinwit[i].scriptWitness : NULL, flags);
1100 return nSigOps;
1107 bool CheckTransaction(const CTransaction& tx, CValidationState &state)
1109 // Basic checks that don't depend on any context
1110 if (tx.vin.empty())
1111 return state.DoS(10, false, REJECT_INVALID, "bad-txns-vin-empty");
1112 if (tx.vout.empty())
1113 return state.DoS(10, false, REJECT_INVALID, "bad-txns-vout-empty");
1114 // Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)
1115 if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) > MAX_BLOCK_BASE_SIZE)
1116 return state.DoS(100, false, REJECT_INVALID, "bad-txns-oversize");
1118 // Check for negative or overflow output values
1119 CAmount nValueOut = 0;
1120 for (const auto& txout : tx.vout)
1122 if (txout.nValue < 0)
1123 return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-negative");
1124 if (txout.nValue > MAX_MONEY)
1125 return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-toolarge");
1126 nValueOut += txout.nValue;
1127 if (!MoneyRange(nValueOut))
1128 return state.DoS(100, false, REJECT_INVALID, "bad-txns-txouttotal-toolarge");
1131 // Check for duplicate inputs
1132 set<COutPoint> vInOutPoints;
1133 for (const auto& txin : tx.vin)
1135 if (vInOutPoints.count(txin.prevout))
1136 return state.DoS(100, false, REJECT_INVALID, "bad-txns-inputs-duplicate");
1137 vInOutPoints.insert(txin.prevout);
1140 if (tx.IsCoinBase())
1142 if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
1143 return state.DoS(100, false, REJECT_INVALID, "bad-cb-length");
1145 else
1147 for (const auto& txin : tx.vin)
1148 if (txin.prevout.IsNull())
1149 return state.DoS(10, false, REJECT_INVALID, "bad-txns-prevout-null");
1152 return true;
1155 void LimitMempoolSize(CTxMemPool& pool, size_t limit, unsigned long age) {
1156 int expired = pool.Expire(GetTime() - age);
1157 if (expired != 0)
1158 LogPrint("mempool", "Expired %i transactions from the memory pool\n", expired);
1160 std::vector<uint256> vNoSpendsRemaining;
1161 pool.TrimToSize(limit, &vNoSpendsRemaining);
1162 BOOST_FOREACH(const uint256& removed, vNoSpendsRemaining)
1163 pcoinsTip->Uncache(removed);
1166 /** Convert CValidationState to a human-readable message for logging */
1167 std::string FormatStateMessage(const CValidationState &state)
1169 return strprintf("%s%s (code %i)",
1170 state.GetRejectReason(),
1171 state.GetDebugMessage().empty() ? "" : ", "+state.GetDebugMessage(),
1172 state.GetRejectCode());
1175 bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const CTransaction& tx, bool fLimitFree,
1176 bool* pfMissingInputs, int64_t nAcceptTime, bool fOverrideMempoolLimit, const CAmount& nAbsurdFee,
1177 std::vector<uint256>& vHashTxnToUncache)
1179 const uint256 hash = tx.GetHash();
1180 AssertLockHeld(cs_main);
1181 if (pfMissingInputs)
1182 *pfMissingInputs = false;
1184 if (!CheckTransaction(tx, state))
1185 return false; // state filled in by CheckTransaction
1187 // Coinbase is only valid in a block, not as a loose transaction
1188 if (tx.IsCoinBase())
1189 return state.DoS(100, false, REJECT_INVALID, "coinbase");
1191 // Don't relay version 2 transactions until CSV is active, and we can be
1192 // sure that such transactions will be mined (unless we're on
1193 // -testnet/-regtest).
1194 const CChainParams& chainparams = Params();
1195 if (fRequireStandard && tx.nVersion >= 2 && VersionBitsTipState(chainparams.GetConsensus(), Consensus::DEPLOYMENT_CSV) != THRESHOLD_ACTIVE) {
1196 return state.DoS(0, false, REJECT_NONSTANDARD, "premature-version2-tx");
1199 // Reject transactions with witness before segregated witness activates (override with -prematurewitness)
1200 bool witnessEnabled = IsWitnessEnabled(chainActive.Tip(), Params().GetConsensus());
1201 if (!GetBoolArg("-prematurewitness",false) && !tx.wit.IsNull() && !witnessEnabled) {
1202 return state.DoS(0, false, REJECT_NONSTANDARD, "no-witness-yet", true);
1205 // Rather not work on nonstandard transactions (unless -testnet/-regtest)
1206 string reason;
1207 if (fRequireStandard && !IsStandardTx(tx, reason, witnessEnabled))
1208 return state.DoS(0, false, REJECT_NONSTANDARD, reason);
1210 // Only accept nLockTime-using transactions that can be mined in the next
1211 // block; we don't want our mempool filled up with transactions that can't
1212 // be mined yet.
1213 if (!CheckFinalTx(tx, STANDARD_LOCKTIME_VERIFY_FLAGS))
1214 return state.DoS(0, false, REJECT_NONSTANDARD, "non-final");
1216 // is it already in the memory pool?
1217 if (pool.exists(hash))
1218 return state.Invalid(false, REJECT_ALREADY_KNOWN, "txn-already-in-mempool");
1220 // Check for conflicts with in-memory transactions
1221 set<uint256> setConflicts;
1223 LOCK(pool.cs); // protect pool.mapNextTx
1224 BOOST_FOREACH(const CTxIn &txin, tx.vin)
1226 auto itConflicting = pool.mapNextTx.find(txin.prevout);
1227 if (itConflicting != pool.mapNextTx.end())
1229 const CTransaction *ptxConflicting = itConflicting->second;
1230 if (!setConflicts.count(ptxConflicting->GetHash()))
1232 // Allow opt-out of transaction replacement by setting
1233 // nSequence >= maxint-1 on all inputs.
1235 // maxint-1 is picked to still allow use of nLockTime by
1236 // non-replaceable transactions. All inputs rather than just one
1237 // is for the sake of multi-party protocols, where we don't
1238 // want a single party to be able to disable replacement.
1240 // The opt-out ignores descendants as anyone relying on
1241 // first-seen mempool behavior should be checking all
1242 // unconfirmed ancestors anyway; doing otherwise is hopelessly
1243 // insecure.
1244 bool fReplacementOptOut = true;
1245 if (fEnableReplacement)
1247 BOOST_FOREACH(const CTxIn &_txin, ptxConflicting->vin)
1249 if (_txin.nSequence < std::numeric_limits<unsigned int>::max()-1)
1251 fReplacementOptOut = false;
1252 break;
1256 if (fReplacementOptOut)
1257 return state.Invalid(false, REJECT_CONFLICT, "txn-mempool-conflict");
1259 setConflicts.insert(ptxConflicting->GetHash());
1266 CCoinsView dummy;
1267 CCoinsViewCache view(&dummy);
1269 CAmount nValueIn = 0;
1270 LockPoints lp;
1272 LOCK(pool.cs);
1273 CCoinsViewMemPool viewMemPool(pcoinsTip, pool);
1274 view.SetBackend(viewMemPool);
1276 // do we already have it?
1277 bool fHadTxInCache = pcoinsTip->HaveCoinsInCache(hash);
1278 if (view.HaveCoins(hash)) {
1279 if (!fHadTxInCache)
1280 vHashTxnToUncache.push_back(hash);
1281 return state.Invalid(false, REJECT_ALREADY_KNOWN, "txn-already-known");
1284 // do all inputs exist?
1285 // Note that this does not check for the presence of actual outputs (see the next check for that),
1286 // and only helps with filling in pfMissingInputs (to determine missing vs spent).
1287 BOOST_FOREACH(const CTxIn txin, tx.vin) {
1288 if (!pcoinsTip->HaveCoinsInCache(txin.prevout.hash))
1289 vHashTxnToUncache.push_back(txin.prevout.hash);
1290 if (!view.HaveCoins(txin.prevout.hash)) {
1291 if (pfMissingInputs)
1292 *pfMissingInputs = true;
1293 return false; // fMissingInputs and !state.IsInvalid() is used to detect this condition, don't set state.Invalid()
1297 // are the actual inputs available?
1298 if (!view.HaveInputs(tx))
1299 return state.Invalid(false, REJECT_DUPLICATE, "bad-txns-inputs-spent");
1301 // Bring the best block into scope
1302 view.GetBestBlock();
1304 nValueIn = view.GetValueIn(tx);
1306 // we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool
1307 view.SetBackend(dummy);
1309 // Only accept BIP68 sequence locked transactions that can be mined in the next
1310 // block; we don't want our mempool filled up with transactions that can't
1311 // be mined yet.
1312 // Must keep pool.cs for this unless we change CheckSequenceLocks to take a
1313 // CoinsViewCache instead of create its own
1314 if (!CheckSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, &lp))
1315 return state.DoS(0, false, REJECT_NONSTANDARD, "non-BIP68-final");
1318 // Check for non-standard pay-to-script-hash in inputs
1319 if (fRequireStandard && !AreInputsStandard(tx, view))
1320 return state.Invalid(false, REJECT_NONSTANDARD, "bad-txns-nonstandard-inputs");
1322 // Check for non-standard witness in P2WSH
1323 if (!tx.wit.IsNull() && fRequireStandard && !IsWitnessStandard(tx, view))
1324 return state.DoS(0, false, REJECT_NONSTANDARD, "bad-witness-nonstandard", true);
1326 int64_t nSigOpsCost = GetTransactionSigOpCost(tx, view, STANDARD_SCRIPT_VERIFY_FLAGS);
1328 CAmount nValueOut = tx.GetValueOut();
1329 CAmount nFees = nValueIn-nValueOut;
1330 // nModifiedFees includes any fee deltas from PrioritiseTransaction
1331 CAmount nModifiedFees = nFees;
1332 double nPriorityDummy = 0;
1333 pool.ApplyDeltas(hash, nPriorityDummy, nModifiedFees);
1335 CAmount inChainInputValue;
1336 double dPriority = view.GetPriority(tx, chainActive.Height(), inChainInputValue);
1338 // Keep track of transactions that spend a coinbase, which we re-scan
1339 // during reorgs to ensure COINBASE_MATURITY is still met.
1340 bool fSpendsCoinbase = false;
1341 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
1342 const CCoins *coins = view.AccessCoins(txin.prevout.hash);
1343 if (coins->IsCoinBase()) {
1344 fSpendsCoinbase = true;
1345 break;
1349 CTxMemPoolEntry entry(tx, nFees, nAcceptTime, dPriority, chainActive.Height(), pool.HasNoInputsOf(tx), inChainInputValue, fSpendsCoinbase, nSigOpsCost, lp);
1350 unsigned int nSize = entry.GetTxSize();
1352 // Check that the transaction doesn't have an excessive number of
1353 // sigops, making it impossible to mine. Since the coinbase transaction
1354 // itself can contain sigops MAX_STANDARD_TX_SIGOPS is less than
1355 // MAX_BLOCK_SIGOPS; we still consider this an invalid rather than
1356 // merely non-standard transaction.
1357 if (nSigOpsCost > MAX_STANDARD_TX_SIGOPS_COST)
1358 return state.DoS(0, false, REJECT_NONSTANDARD, "bad-txns-too-many-sigops", false,
1359 strprintf("%d", nSigOpsCost));
1361 CAmount mempoolRejectFee = pool.GetMinFee(GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFee(nSize);
1362 if (mempoolRejectFee > 0 && nModifiedFees < mempoolRejectFee) {
1363 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "mempool min fee not met", false, strprintf("%d < %d", nFees, mempoolRejectFee));
1364 } else if (GetBoolArg("-relaypriority", DEFAULT_RELAYPRIORITY) && nModifiedFees < ::minRelayTxFee.GetFee(nSize) && !AllowFree(entry.GetPriority(chainActive.Height() + 1))) {
1365 // Require that free transactions have sufficient priority to be mined in the next block.
1366 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "insufficient priority");
1369 // Continuously rate-limit free (really, very-low-fee) transactions
1370 // This mitigates 'penny-flooding' -- sending thousands of free transactions just to
1371 // be annoying or make others' transactions take longer to confirm.
1372 if (fLimitFree && nModifiedFees < ::minRelayTxFee.GetFee(nSize))
1374 static CCriticalSection csFreeLimiter;
1375 static double dFreeCount;
1376 static int64_t nLastTime;
1377 int64_t nNow = GetTime();
1379 LOCK(csFreeLimiter);
1381 // Use an exponentially decaying ~10-minute window:
1382 dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime));
1383 nLastTime = nNow;
1384 // -limitfreerelay unit is thousand-bytes-per-minute
1385 // At default rate it would take over a month to fill 1GB
1386 if (dFreeCount + nSize >= GetArg("-limitfreerelay", DEFAULT_LIMITFREERELAY) * 10 * 1000)
1387 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "rate limited free transaction");
1388 LogPrint("mempool", "Rate limit dFreeCount: %g => %g\n", dFreeCount, dFreeCount+nSize);
1389 dFreeCount += nSize;
1392 if (nAbsurdFee && nFees > nAbsurdFee)
1393 return state.Invalid(false,
1394 REJECT_HIGHFEE, "absurdly-high-fee",
1395 strprintf("%d > %d", nFees, nAbsurdFee));
1397 // Calculate in-mempool ancestors, up to a limit.
1398 CTxMemPool::setEntries setAncestors;
1399 size_t nLimitAncestors = GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT);
1400 size_t nLimitAncestorSize = GetArg("-limitancestorsize", DEFAULT_ANCESTOR_SIZE_LIMIT)*1000;
1401 size_t nLimitDescendants = GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT);
1402 size_t nLimitDescendantSize = GetArg("-limitdescendantsize", DEFAULT_DESCENDANT_SIZE_LIMIT)*1000;
1403 std::string errString;
1404 if (!pool.CalculateMemPoolAncestors(entry, setAncestors, nLimitAncestors, nLimitAncestorSize, nLimitDescendants, nLimitDescendantSize, errString)) {
1405 return state.DoS(0, false, REJECT_NONSTANDARD, "too-long-mempool-chain", false, errString);
1408 // A transaction that spends outputs that would be replaced by it is invalid. Now
1409 // that we have the set of all ancestors we can detect this
1410 // pathological case by making sure setConflicts and setAncestors don't
1411 // intersect.
1412 BOOST_FOREACH(CTxMemPool::txiter ancestorIt, setAncestors)
1414 const uint256 &hashAncestor = ancestorIt->GetTx().GetHash();
1415 if (setConflicts.count(hashAncestor))
1417 return state.DoS(10, false,
1418 REJECT_INVALID, "bad-txns-spends-conflicting-tx", false,
1419 strprintf("%s spends conflicting transaction %s",
1420 hash.ToString(),
1421 hashAncestor.ToString()));
1425 // Check if it's economically rational to mine this transaction rather
1426 // than the ones it replaces.
1427 CAmount nConflictingFees = 0;
1428 size_t nConflictingSize = 0;
1429 uint64_t nConflictingCount = 0;
1430 CTxMemPool::setEntries allConflicting;
1432 // If we don't hold the lock allConflicting might be incomplete; the
1433 // subsequent RemoveStaged() and addUnchecked() calls don't guarantee
1434 // mempool consistency for us.
1435 LOCK(pool.cs);
1436 if (setConflicts.size())
1438 CFeeRate newFeeRate(nModifiedFees, nSize);
1439 set<uint256> setConflictsParents;
1440 const int maxDescendantsToVisit = 100;
1441 CTxMemPool::setEntries setIterConflicting;
1442 BOOST_FOREACH(const uint256 &hashConflicting, setConflicts)
1444 CTxMemPool::txiter mi = pool.mapTx.find(hashConflicting);
1445 if (mi == pool.mapTx.end())
1446 continue;
1448 // Save these to avoid repeated lookups
1449 setIterConflicting.insert(mi);
1451 // Don't allow the replacement to reduce the feerate of the
1452 // mempool.
1454 // We usually don't want to accept replacements with lower
1455 // feerates than what they replaced as that would lower the
1456 // feerate of the next block. Requiring that the feerate always
1457 // be increased is also an easy-to-reason about way to prevent
1458 // DoS attacks via replacements.
1460 // The mining code doesn't (currently) take children into
1461 // account (CPFP) so we only consider the feerates of
1462 // transactions being directly replaced, not their indirect
1463 // descendants. While that does mean high feerate children are
1464 // ignored when deciding whether or not to replace, we do
1465 // require the replacement to pay more overall fees too,
1466 // mitigating most cases.
1467 CFeeRate oldFeeRate(mi->GetModifiedFee(), mi->GetTxSize());
1468 if (newFeeRate <= oldFeeRate)
1470 return state.DoS(0, false,
1471 REJECT_INSUFFICIENTFEE, "insufficient fee", false,
1472 strprintf("rejecting replacement %s; new feerate %s <= old feerate %s",
1473 hash.ToString(),
1474 newFeeRate.ToString(),
1475 oldFeeRate.ToString()));
1478 BOOST_FOREACH(const CTxIn &txin, mi->GetTx().vin)
1480 setConflictsParents.insert(txin.prevout.hash);
1483 nConflictingCount += mi->GetCountWithDescendants();
1485 // This potentially overestimates the number of actual descendants
1486 // but we just want to be conservative to avoid doing too much
1487 // work.
1488 if (nConflictingCount <= maxDescendantsToVisit) {
1489 // If not too many to replace, then calculate the set of
1490 // transactions that would have to be evicted
1491 BOOST_FOREACH(CTxMemPool::txiter it, setIterConflicting) {
1492 pool.CalculateDescendants(it, allConflicting);
1494 BOOST_FOREACH(CTxMemPool::txiter it, allConflicting) {
1495 nConflictingFees += it->GetModifiedFee();
1496 nConflictingSize += it->GetTxSize();
1498 } else {
1499 return state.DoS(0, false,
1500 REJECT_NONSTANDARD, "too many potential replacements", false,
1501 strprintf("rejecting replacement %s; too many potential replacements (%d > %d)\n",
1502 hash.ToString(),
1503 nConflictingCount,
1504 maxDescendantsToVisit));
1507 for (unsigned int j = 0; j < tx.vin.size(); j++)
1509 // We don't want to accept replacements that require low
1510 // feerate junk to be mined first. Ideally we'd keep track of
1511 // the ancestor feerates and make the decision based on that,
1512 // but for now requiring all new inputs to be confirmed works.
1513 if (!setConflictsParents.count(tx.vin[j].prevout.hash))
1515 // Rather than check the UTXO set - potentially expensive -
1516 // it's cheaper to just check if the new input refers to a
1517 // tx that's in the mempool.
1518 if (pool.mapTx.find(tx.vin[j].prevout.hash) != pool.mapTx.end())
1519 return state.DoS(0, false,
1520 REJECT_NONSTANDARD, "replacement-adds-unconfirmed", false,
1521 strprintf("replacement %s adds unconfirmed input, idx %d",
1522 hash.ToString(), j));
1526 // The replacement must pay greater fees than the transactions it
1527 // replaces - if we did the bandwidth used by those conflicting
1528 // transactions would not be paid for.
1529 if (nModifiedFees < nConflictingFees)
1531 return state.DoS(0, false,
1532 REJECT_INSUFFICIENTFEE, "insufficient fee", false,
1533 strprintf("rejecting replacement %s, less fees than conflicting txs; %s < %s",
1534 hash.ToString(), FormatMoney(nModifiedFees), FormatMoney(nConflictingFees)));
1537 // Finally in addition to paying more fees than the conflicts the
1538 // new transaction must pay for its own bandwidth.
1539 CAmount nDeltaFees = nModifiedFees - nConflictingFees;
1540 if (nDeltaFees < ::minRelayTxFee.GetFee(nSize))
1542 return state.DoS(0, false,
1543 REJECT_INSUFFICIENTFEE, "insufficient fee", false,
1544 strprintf("rejecting replacement %s, not enough additional fees to relay; %s < %s",
1545 hash.ToString(),
1546 FormatMoney(nDeltaFees),
1547 FormatMoney(::minRelayTxFee.GetFee(nSize))));
1551 unsigned int scriptVerifyFlags = STANDARD_SCRIPT_VERIFY_FLAGS;
1552 if (!Params().RequireStandard()) {
1553 scriptVerifyFlags = GetArg("-promiscuousmempoolflags", scriptVerifyFlags);
1556 // Check against previous transactions
1557 // This is done last to help prevent CPU exhaustion denial-of-service attacks.
1558 PrecomputedTransactionData txdata(tx);
1559 if (!CheckInputs(tx, state, view, true, scriptVerifyFlags, true, txdata)) {
1560 // SCRIPT_VERIFY_CLEANSTACK requires SCRIPT_VERIFY_WITNESS, so we
1561 // need to turn both off, and compare against just turning off CLEANSTACK
1562 // to see if the failure is specifically due to witness validation.
1563 if (tx.wit.IsNull() && CheckInputs(tx, state, view, true, scriptVerifyFlags & ~(SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_CLEANSTACK), true, txdata) &&
1564 !CheckInputs(tx, state, view, true, scriptVerifyFlags & ~SCRIPT_VERIFY_CLEANSTACK, true, txdata)) {
1565 // Only the witness is missing, so the transaction itself may be fine.
1566 state.SetCorruptionPossible();
1568 return false;
1571 // Check again against just the consensus-critical mandatory script
1572 // verification flags, in case of bugs in the standard flags that cause
1573 // transactions to pass as valid when they're actually invalid. For
1574 // instance the STRICTENC flag was incorrectly allowing certain
1575 // CHECKSIG NOT scripts to pass, even though they were invalid.
1577 // There is a similar check in CreateNewBlock() to prevent creating
1578 // invalid blocks, however allowing such transactions into the mempool
1579 // can be exploited as a DoS attack.
1580 if (!CheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true, txdata))
1582 return error("%s: BUG! PLEASE REPORT THIS! ConnectInputs failed against MANDATORY but not STANDARD flags %s, %s",
1583 __func__, hash.ToString(), FormatStateMessage(state));
1586 // Remove conflicting transactions from the mempool
1587 BOOST_FOREACH(const CTxMemPool::txiter it, allConflicting)
1589 LogPrint("mempool", "replacing tx %s with %s for %s BTC additional fees, %d delta bytes\n",
1590 it->GetTx().GetHash().ToString(),
1591 hash.ToString(),
1592 FormatMoney(nModifiedFees - nConflictingFees),
1593 (int)nSize - (int)nConflictingSize);
1595 pool.RemoveStaged(allConflicting, false);
1597 // Store transaction in memory
1598 pool.addUnchecked(hash, entry, setAncestors, !IsInitialBlockDownload());
1600 // trim mempool and check if tx was trimmed
1601 if (!fOverrideMempoolLimit) {
1602 LimitMempoolSize(pool, GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
1603 if (!pool.exists(hash))
1604 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "mempool full");
1608 GetMainSignals().SyncTransaction(tx, NULL, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK);
1610 return true;
1613 bool AcceptToMemoryPoolWithTime(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,
1614 bool* pfMissingInputs, int64_t nAcceptTime, bool fOverrideMempoolLimit, const CAmount nAbsurdFee)
1616 std::vector<uint256> vHashTxToUncache;
1617 bool res = AcceptToMemoryPoolWorker(pool, state, tx, fLimitFree, pfMissingInputs, nAcceptTime, fOverrideMempoolLimit, nAbsurdFee, vHashTxToUncache);
1618 if (!res) {
1619 BOOST_FOREACH(const uint256& hashTx, vHashTxToUncache)
1620 pcoinsTip->Uncache(hashTx);
1622 // After we've (potentially) uncached entries, ensure our coins cache is still within its size limits
1623 CValidationState stateDummy;
1624 FlushStateToDisk(stateDummy, FLUSH_STATE_PERIODIC);
1625 return res;
1628 bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,
1629 bool* pfMissingInputs, bool fOverrideMempoolLimit, const CAmount nAbsurdFee)
1631 return AcceptToMemoryPoolWithTime(pool, state, tx, fLimitFree, pfMissingInputs, GetTime(), fOverrideMempoolLimit, nAbsurdFee);
1634 /** Return transaction in txOut, and if it was found inside a block, its hash is placed in hashBlock */
1635 bool GetTransaction(const uint256 &hash, CTransaction &txOut, const Consensus::Params& consensusParams, uint256 &hashBlock, bool fAllowSlow)
1637 CBlockIndex *pindexSlow = NULL;
1639 LOCK(cs_main);
1641 std::shared_ptr<const CTransaction> ptx = mempool.get(hash);
1642 if (ptx)
1644 txOut = *ptx;
1645 return true;
1648 if (fTxIndex) {
1649 CDiskTxPos postx;
1650 if (pblocktree->ReadTxIndex(hash, postx)) {
1651 CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
1652 if (file.IsNull())
1653 return error("%s: OpenBlockFile failed", __func__);
1654 CBlockHeader header;
1655 try {
1656 file >> header;
1657 fseek(file.Get(), postx.nTxOffset, SEEK_CUR);
1658 file >> txOut;
1659 } catch (const std::exception& e) {
1660 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
1662 hashBlock = header.GetHash();
1663 if (txOut.GetHash() != hash)
1664 return error("%s: txid mismatch", __func__);
1665 return true;
1669 if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
1670 int nHeight = -1;
1672 const CCoinsViewCache& view = *pcoinsTip;
1673 const CCoins* coins = view.AccessCoins(hash);
1674 if (coins)
1675 nHeight = coins->nHeight;
1677 if (nHeight > 0)
1678 pindexSlow = chainActive[nHeight];
1681 if (pindexSlow) {
1682 CBlock block;
1683 if (ReadBlockFromDisk(block, pindexSlow, consensusParams)) {
1684 BOOST_FOREACH(const CTransaction &tx, block.vtx) {
1685 if (tx.GetHash() == hash) {
1686 txOut = tx;
1687 hashBlock = pindexSlow->GetBlockHash();
1688 return true;
1694 return false;
1702 //////////////////////////////////////////////////////////////////////////////
1704 // CBlock and CBlockIndex
1707 bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart)
1709 // Open history file to append
1710 CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
1711 if (fileout.IsNull())
1712 return error("WriteBlockToDisk: OpenBlockFile failed");
1714 // Write index header
1715 unsigned int nSize = fileout.GetSerializeSize(block);
1716 fileout << FLATDATA(messageStart) << nSize;
1718 // Write block
1719 long fileOutPos = ftell(fileout.Get());
1720 if (fileOutPos < 0)
1721 return error("WriteBlockToDisk: ftell failed");
1722 pos.nPos = (unsigned int)fileOutPos;
1723 fileout << block;
1725 return true;
1728 bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams)
1730 block.SetNull();
1732 // Open history file to read
1733 CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
1734 if (filein.IsNull())
1735 return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
1737 // Read block
1738 try {
1739 filein >> block;
1741 catch (const std::exception& e) {
1742 return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
1745 // Check the header
1746 if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
1747 return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
1749 return true;
1752 bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
1754 if (!ReadBlockFromDisk(block, pindex->GetBlockPos(), consensusParams))
1755 return false;
1756 if (block.GetHash() != pindex->GetBlockHash())
1757 return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
1758 pindex->ToString(), pindex->GetBlockPos().ToString());
1759 return true;
1762 CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
1764 int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
1765 // Force block reward to zero when right shift is undefined.
1766 if (halvings >= 64)
1767 return 0;
1769 CAmount nSubsidy = 50 * COIN;
1770 // Subsidy is cut in half every 210,000 blocks which will occur approximately every 4 years.
1771 nSubsidy >>= halvings;
1772 return nSubsidy;
1775 bool IsInitialBlockDownload()
1777 const CChainParams& chainParams = Params();
1779 // Once this function has returned false, it must remain false.
1780 static std::atomic<bool> latchToFalse{false};
1781 // Optimization: pre-test latch before taking the lock.
1782 if (latchToFalse.load(std::memory_order_relaxed))
1783 return false;
1785 LOCK(cs_main);
1786 if (latchToFalse.load(std::memory_order_relaxed))
1787 return false;
1788 if (fImporting || fReindex)
1789 return true;
1790 if (chainActive.Tip() == NULL)
1791 return true;
1792 if (chainActive.Tip()->nChainWork < UintToArith256(chainParams.GetConsensus().nMinimumChainWork))
1793 return true;
1794 if (chainActive.Tip()->GetBlockTime() < (GetTime() - nMaxTipAge))
1795 return true;
1796 latchToFalse.store(true, std::memory_order_relaxed);
1797 return false;
1800 bool fLargeWorkForkFound = false;
1801 bool fLargeWorkInvalidChainFound = false;
1802 CBlockIndex *pindexBestForkTip = NULL, *pindexBestForkBase = NULL;
1804 static void AlertNotify(const std::string& strMessage)
1806 uiInterface.NotifyAlertChanged();
1807 std::string strCmd = GetArg("-alertnotify", "");
1808 if (strCmd.empty()) return;
1810 // Alert text should be plain ascii coming from a trusted source, but to
1811 // be safe we first strip anything not in safeChars, then add single quotes around
1812 // the whole string before passing it to the shell:
1813 std::string singleQuote("'");
1814 std::string safeStatus = SanitizeString(strMessage);
1815 safeStatus = singleQuote+safeStatus+singleQuote;
1816 boost::replace_all(strCmd, "%s", safeStatus);
1818 boost::thread t(runCommand, strCmd); // thread runs free
1821 void CheckForkWarningConditions()
1823 AssertLockHeld(cs_main);
1824 // Before we get past initial download, we cannot reliably alert about forks
1825 // (we assume we don't get stuck on a fork before finishing our initial sync)
1826 if (IsInitialBlockDownload())
1827 return;
1829 // If our best fork is no longer within 72 blocks (+/- 12 hours if no one mines it)
1830 // of our head, drop it
1831 if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
1832 pindexBestForkTip = NULL;
1834 if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6)))
1836 if (!fLargeWorkForkFound && pindexBestForkBase)
1838 std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
1839 pindexBestForkBase->phashBlock->ToString() + std::string("'");
1840 AlertNotify(warning);
1842 if (pindexBestForkTip && pindexBestForkBase)
1844 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__,
1845 pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
1846 pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
1847 fLargeWorkForkFound = true;
1849 else
1851 LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
1852 fLargeWorkInvalidChainFound = true;
1855 else
1857 fLargeWorkForkFound = false;
1858 fLargeWorkInvalidChainFound = false;
1862 void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
1864 AssertLockHeld(cs_main);
1865 // If we are on a fork that is sufficiently large, set a warning flag
1866 CBlockIndex* pfork = pindexNewForkTip;
1867 CBlockIndex* plonger = chainActive.Tip();
1868 while (pfork && pfork != plonger)
1870 while (plonger && plonger->nHeight > pfork->nHeight)
1871 plonger = plonger->pprev;
1872 if (pfork == plonger)
1873 break;
1874 pfork = pfork->pprev;
1877 // We define a condition where we should warn the user about as a fork of at least 7 blocks
1878 // with a tip within 72 blocks (+/- 12 hours if no one mines it) of ours
1879 // We use 7 blocks rather arbitrarily as it represents just under 10% of sustained network
1880 // hash rate operating on the fork.
1881 // or a chain that is entirely longer than ours and invalid (note that this should be detected by both)
1882 // We define it this way because it allows us to only store the highest fork tip (+ base) which meets
1883 // the 7-block condition and from this always have the most-likely-to-cause-warning fork
1884 if (pfork && (!pindexBestForkTip || (pindexBestForkTip && pindexNewForkTip->nHeight > pindexBestForkTip->nHeight)) &&
1885 pindexNewForkTip->nChainWork - pfork->nChainWork > (GetBlockProof(*pfork) * 7) &&
1886 chainActive.Height() - pindexNewForkTip->nHeight < 72)
1888 pindexBestForkTip = pindexNewForkTip;
1889 pindexBestForkBase = pfork;
1892 CheckForkWarningConditions();
1895 // Requires cs_main.
1896 void Misbehaving(NodeId pnode, int howmuch)
1898 if (howmuch == 0)
1899 return;
1901 CNodeState *state = State(pnode);
1902 if (state == NULL)
1903 return;
1905 state->nMisbehavior += howmuch;
1906 int banscore = GetArg("-banscore", DEFAULT_BANSCORE_THRESHOLD);
1907 if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
1909 LogPrintf("%s: %s peer=%d (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior);
1910 state->fShouldBan = true;
1911 } else
1912 LogPrintf("%s: %s peer=%d (%d -> %d)\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior);
1915 void static InvalidChainFound(CBlockIndex* pindexNew)
1917 if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
1918 pindexBestInvalid = pindexNew;
1920 LogPrintf("%s: invalid block=%s height=%d log2_work=%.8g date=%s\n", __func__,
1921 pindexNew->GetBlockHash().ToString(), pindexNew->nHeight,
1922 log(pindexNew->nChainWork.getdouble())/log(2.0), DateTimeStrFormat("%Y-%m-%d %H:%M:%S",
1923 pindexNew->GetBlockTime()));
1924 CBlockIndex *tip = chainActive.Tip();
1925 assert (tip);
1926 LogPrintf("%s: current best=%s height=%d log2_work=%.8g date=%s\n", __func__,
1927 tip->GetBlockHash().ToString(), chainActive.Height(), log(tip->nChainWork.getdouble())/log(2.0),
1928 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", tip->GetBlockTime()));
1929 CheckForkWarningConditions();
1932 void static InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state) {
1933 if (!state.CorruptionPossible()) {
1934 pindex->nStatus |= BLOCK_FAILED_VALID;
1935 setDirtyBlockIndex.insert(pindex);
1936 setBlockIndexCandidates.erase(pindex);
1937 InvalidChainFound(pindex);
1941 void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight)
1943 // mark inputs spent
1944 if (!tx.IsCoinBase()) {
1945 txundo.vprevout.reserve(tx.vin.size());
1946 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
1947 CCoinsModifier coins = inputs.ModifyCoins(txin.prevout.hash);
1948 unsigned nPos = txin.prevout.n;
1950 if (nPos >= coins->vout.size() || coins->vout[nPos].IsNull())
1951 assert(false);
1952 // mark an outpoint spent, and construct undo information
1953 txundo.vprevout.push_back(CTxInUndo(coins->vout[nPos]));
1954 coins->Spend(nPos);
1955 if (coins->vout.size() == 0) {
1956 CTxInUndo& undo = txundo.vprevout.back();
1957 undo.nHeight = coins->nHeight;
1958 undo.fCoinBase = coins->fCoinBase;
1959 undo.nVersion = coins->nVersion;
1963 // add outputs
1964 inputs.ModifyNewCoins(tx.GetHash(), tx.IsCoinBase())->FromTx(tx, nHeight);
1967 void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, int nHeight)
1969 CTxUndo txundo;
1970 UpdateCoins(tx, inputs, txundo, nHeight);
1973 bool CScriptCheck::operator()() {
1974 const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
1975 const CScriptWitness *witness = (nIn < ptxTo->wit.vtxinwit.size()) ? &ptxTo->wit.vtxinwit[nIn].scriptWitness : NULL;
1976 if (!VerifyScript(scriptSig, scriptPubKey, witness, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, amount, cacheStore, *txdata), &error)) {
1977 return false;
1979 return true;
1982 int GetSpendHeight(const CCoinsViewCache& inputs)
1984 LOCK(cs_main);
1985 CBlockIndex* pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
1986 return pindexPrev->nHeight + 1;
1989 namespace Consensus {
1990 bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight)
1992 // This doesn't trigger the DoS code on purpose; if it did, it would make it easier
1993 // for an attacker to attempt to split the network.
1994 if (!inputs.HaveInputs(tx))
1995 return state.Invalid(false, 0, "", "Inputs unavailable");
1997 CAmount nValueIn = 0;
1998 CAmount nFees = 0;
1999 for (unsigned int i = 0; i < tx.vin.size(); i++)
2001 const COutPoint &prevout = tx.vin[i].prevout;
2002 const CCoins *coins = inputs.AccessCoins(prevout.hash);
2003 assert(coins);
2005 // If prev is coinbase, check that it's matured
2006 if (coins->IsCoinBase()) {
2007 if (nSpendHeight - coins->nHeight < COINBASE_MATURITY)
2008 return state.Invalid(false,
2009 REJECT_INVALID, "bad-txns-premature-spend-of-coinbase",
2010 strprintf("tried to spend coinbase at depth %d", nSpendHeight - coins->nHeight));
2013 // Check for negative or overflow input values
2014 nValueIn += coins->vout[prevout.n].nValue;
2015 if (!MoneyRange(coins->vout[prevout.n].nValue) || !MoneyRange(nValueIn))
2016 return state.DoS(100, false, REJECT_INVALID, "bad-txns-inputvalues-outofrange");
2020 if (nValueIn < tx.GetValueOut())
2021 return state.DoS(100, false, REJECT_INVALID, "bad-txns-in-belowout", false,
2022 strprintf("value in (%s) < value out (%s)", FormatMoney(nValueIn), FormatMoney(tx.GetValueOut())));
2024 // Tally transaction fees
2025 CAmount nTxFee = nValueIn - tx.GetValueOut();
2026 if (nTxFee < 0)
2027 return state.DoS(100, false, REJECT_INVALID, "bad-txns-fee-negative");
2028 nFees += nTxFee;
2029 if (!MoneyRange(nFees))
2030 return state.DoS(100, false, REJECT_INVALID, "bad-txns-fee-outofrange");
2031 return true;
2033 }// namespace Consensus
2035 bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks)
2037 if (!tx.IsCoinBase())
2039 if (!Consensus::CheckTxInputs(tx, state, inputs, GetSpendHeight(inputs)))
2040 return false;
2042 if (pvChecks)
2043 pvChecks->reserve(tx.vin.size());
2045 // The first loop above does all the inexpensive checks.
2046 // Only if ALL inputs pass do we perform expensive ECDSA signature checks.
2047 // Helps prevent CPU exhaustion attacks.
2049 // Skip ECDSA signature verification when connecting blocks before the
2050 // last block chain checkpoint. Assuming the checkpoints are valid this
2051 // is safe because block merkle hashes are still computed and checked,
2052 // and any change will be caught at the next checkpoint. Of course, if
2053 // the checkpoint is for a chain that's invalid due to false scriptSigs
2054 // this optimization would allow an invalid chain to be accepted.
2055 if (fScriptChecks) {
2056 for (unsigned int i = 0; i < tx.vin.size(); i++) {
2057 const COutPoint &prevout = tx.vin[i].prevout;
2058 const CCoins* coins = inputs.AccessCoins(prevout.hash);
2059 assert(coins);
2061 // Verify signature
2062 CScriptCheck check(*coins, tx, i, flags, cacheStore, &txdata);
2063 if (pvChecks) {
2064 pvChecks->push_back(CScriptCheck());
2065 check.swap(pvChecks->back());
2066 } else if (!check()) {
2067 if (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) {
2068 // Check whether the failure was caused by a
2069 // non-mandatory script verification check, such as
2070 // non-standard DER encodings or non-null dummy
2071 // arguments; if so, don't trigger DoS protection to
2072 // avoid splitting the network between upgraded and
2073 // non-upgraded nodes.
2074 CScriptCheck check2(*coins, tx, i,
2075 flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, cacheStore, &txdata);
2076 if (check2())
2077 return state.Invalid(false, REJECT_NONSTANDARD, strprintf("non-mandatory-script-verify-flag (%s)", ScriptErrorString(check.GetScriptError())));
2079 // Failures of other flags indicate a transaction that is
2080 // invalid in new blocks, e.g. a invalid P2SH. We DoS ban
2081 // such nodes as they are not following the protocol. That
2082 // said during an upgrade careful thought should be taken
2083 // as to the correct behavior - we may want to continue
2084 // peering with non-upgraded nodes even after soft-fork
2085 // super-majority signaling has occurred.
2086 return state.DoS(100,false, REJECT_INVALID, strprintf("mandatory-script-verify-flag-failed (%s)", ScriptErrorString(check.GetScriptError())));
2092 return true;
2095 namespace {
2097 bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
2099 // Open history file to append
2100 CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
2101 if (fileout.IsNull())
2102 return error("%s: OpenUndoFile failed", __func__);
2104 // Write index header
2105 unsigned int nSize = fileout.GetSerializeSize(blockundo);
2106 fileout << FLATDATA(messageStart) << nSize;
2108 // Write undo data
2109 long fileOutPos = ftell(fileout.Get());
2110 if (fileOutPos < 0)
2111 return error("%s: ftell failed", __func__);
2112 pos.nPos = (unsigned int)fileOutPos;
2113 fileout << blockundo;
2115 // calculate & write checksum
2116 CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
2117 hasher << hashBlock;
2118 hasher << blockundo;
2119 fileout << hasher.GetHash();
2121 return true;
2124 bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock)
2126 // Open history file to read
2127 CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
2128 if (filein.IsNull())
2129 return error("%s: OpenUndoFile failed", __func__);
2131 // Read block
2132 uint256 hashChecksum;
2133 try {
2134 filein >> blockundo;
2135 filein >> hashChecksum;
2137 catch (const std::exception& e) {
2138 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
2141 // Verify checksum
2142 CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
2143 hasher << hashBlock;
2144 hasher << blockundo;
2145 if (hashChecksum != hasher.GetHash())
2146 return error("%s: Checksum mismatch", __func__);
2148 return true;
2151 /** Abort with a message */
2152 bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
2154 strMiscWarning = strMessage;
2155 LogPrintf("*** %s\n", strMessage);
2156 uiInterface.ThreadSafeMessageBox(
2157 userMessage.empty() ? _("Error: A fatal internal error occurred, see debug.log for details") : userMessage,
2158 "", CClientUIInterface::MSG_ERROR);
2159 StartShutdown();
2160 return false;
2163 bool AbortNode(CValidationState& state, const std::string& strMessage, const std::string& userMessage="")
2165 AbortNode(strMessage, userMessage);
2166 return state.Error(strMessage);
2169 } // anon namespace
2172 * Apply the undo operation of a CTxInUndo to the given chain state.
2173 * @param undo The undo object.
2174 * @param view The coins view to which to apply the changes.
2175 * @param out The out point that corresponds to the tx input.
2176 * @return True on success.
2178 static bool ApplyTxInUndo(const CTxInUndo& undo, CCoinsViewCache& view, const COutPoint& out)
2180 bool fClean = true;
2182 CCoinsModifier coins = view.ModifyCoins(out.hash);
2183 if (undo.nHeight != 0) {
2184 // undo data contains height: this is the last output of the prevout tx being spent
2185 if (!coins->IsPruned())
2186 fClean = fClean && error("%s: undo data overwriting existing transaction", __func__);
2187 coins->Clear();
2188 coins->fCoinBase = undo.fCoinBase;
2189 coins->nHeight = undo.nHeight;
2190 coins->nVersion = undo.nVersion;
2191 } else {
2192 if (coins->IsPruned())
2193 fClean = fClean && error("%s: undo data adding output to missing transaction", __func__);
2195 if (coins->IsAvailable(out.n))
2196 fClean = fClean && error("%s: undo data overwriting existing output", __func__);
2197 if (coins->vout.size() < out.n+1)
2198 coins->vout.resize(out.n+1);
2199 coins->vout[out.n] = undo.txout;
2201 return fClean;
2204 bool DisconnectBlock(const CBlock& block, CValidationState& state, const CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean)
2206 assert(pindex->GetBlockHash() == view.GetBestBlock());
2208 if (pfClean)
2209 *pfClean = false;
2211 bool fClean = true;
2213 CBlockUndo blockUndo;
2214 CDiskBlockPos pos = pindex->GetUndoPos();
2215 if (pos.IsNull())
2216 return error("DisconnectBlock(): no undo data available");
2217 if (!UndoReadFromDisk(blockUndo, pos, pindex->pprev->GetBlockHash()))
2218 return error("DisconnectBlock(): failure reading undo data");
2220 if (blockUndo.vtxundo.size() + 1 != block.vtx.size())
2221 return error("DisconnectBlock(): block and undo data inconsistent");
2223 // undo transactions in reverse order
2224 for (int i = block.vtx.size() - 1; i >= 0; i--) {
2225 const CTransaction &tx = block.vtx[i];
2226 uint256 hash = tx.GetHash();
2228 // Check that all outputs are available and match the outputs in the block itself
2229 // exactly.
2231 CCoinsModifier outs = view.ModifyCoins(hash);
2232 outs->ClearUnspendable();
2234 CCoins outsBlock(tx, pindex->nHeight);
2235 // The CCoins serialization does not serialize negative numbers.
2236 // No network rules currently depend on the version here, so an inconsistency is harmless
2237 // but it must be corrected before txout nversion ever influences a network rule.
2238 if (outsBlock.nVersion < 0)
2239 outs->nVersion = outsBlock.nVersion;
2240 if (*outs != outsBlock)
2241 fClean = fClean && error("DisconnectBlock(): added transaction mismatch? database corrupted");
2243 // remove outputs
2244 outs->Clear();
2247 // restore inputs
2248 if (i > 0) { // not coinbases
2249 const CTxUndo &txundo = blockUndo.vtxundo[i-1];
2250 if (txundo.vprevout.size() != tx.vin.size())
2251 return error("DisconnectBlock(): transaction and undo data inconsistent");
2252 for (unsigned int j = tx.vin.size(); j-- > 0;) {
2253 const COutPoint &out = tx.vin[j].prevout;
2254 const CTxInUndo &undo = txundo.vprevout[j];
2255 if (!ApplyTxInUndo(undo, view, out))
2256 fClean = false;
2261 // move best block pointer to prevout block
2262 view.SetBestBlock(pindex->pprev->GetBlockHash());
2264 if (pfClean) {
2265 *pfClean = fClean;
2266 return true;
2269 return fClean;
2272 void static FlushBlockFile(bool fFinalize = false)
2274 LOCK(cs_LastBlockFile);
2276 CDiskBlockPos posOld(nLastBlockFile, 0);
2278 FILE *fileOld = OpenBlockFile(posOld);
2279 if (fileOld) {
2280 if (fFinalize)
2281 TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nSize);
2282 FileCommit(fileOld);
2283 fclose(fileOld);
2286 fileOld = OpenUndoFile(posOld);
2287 if (fileOld) {
2288 if (fFinalize)
2289 TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nUndoSize);
2290 FileCommit(fileOld);
2291 fclose(fileOld);
2295 bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
2297 static CCheckQueue<CScriptCheck> scriptcheckqueue(128);
2299 void ThreadScriptCheck() {
2300 RenameThread("bitcoin-scriptch");
2301 scriptcheckqueue.Thread();
2304 // Protected by cs_main
2305 VersionBitsCache versionbitscache;
2307 int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
2309 LOCK(cs_main);
2310 int32_t nVersion = VERSIONBITS_TOP_BITS;
2312 for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) {
2313 ThresholdState state = VersionBitsState(pindexPrev, params, (Consensus::DeploymentPos)i, versionbitscache);
2314 if (state == THRESHOLD_LOCKED_IN || state == THRESHOLD_STARTED) {
2315 nVersion |= VersionBitsMask(params, (Consensus::DeploymentPos)i);
2319 return nVersion;
2323 * Threshold condition checker that triggers when unknown versionbits are seen on the network.
2325 class WarningBitsConditionChecker : public AbstractThresholdConditionChecker
2327 private:
2328 int bit;
2330 public:
2331 WarningBitsConditionChecker(int bitIn) : bit(bitIn) {}
2333 int64_t BeginTime(const Consensus::Params& params) const { return 0; }
2334 int64_t EndTime(const Consensus::Params& params) const { return std::numeric_limits<int64_t>::max(); }
2335 int Period(const Consensus::Params& params) const { return params.nMinerConfirmationWindow; }
2336 int Threshold(const Consensus::Params& params) const { return params.nRuleChangeActivationThreshold; }
2338 bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const
2340 return ((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) &&
2341 ((pindex->nVersion >> bit) & 1) != 0 &&
2342 ((ComputeBlockVersion(pindex->pprev, params) >> bit) & 1) == 0;
2346 // Protected by cs_main
2347 static ThresholdConditionCache warningcache[VERSIONBITS_NUM_BITS];
2349 static int64_t nTimeCheck = 0;
2350 static int64_t nTimeForks = 0;
2351 static int64_t nTimeVerify = 0;
2352 static int64_t nTimeConnect = 0;
2353 static int64_t nTimeIndex = 0;
2354 static int64_t nTimeCallbacks = 0;
2355 static int64_t nTimeTotal = 0;
2357 bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex,
2358 CCoinsViewCache& view, const CChainParams& chainparams, bool fJustCheck)
2360 AssertLockHeld(cs_main);
2362 int64_t nTimeStart = GetTimeMicros();
2364 // Check it again in case a previous version let a bad block in
2365 if (!CheckBlock(block, state, chainparams.GetConsensus(), !fJustCheck, !fJustCheck))
2366 return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
2368 // verify that the view's current state corresponds to the previous block
2369 uint256 hashPrevBlock = pindex->pprev == NULL ? uint256() : pindex->pprev->GetBlockHash();
2370 assert(hashPrevBlock == view.GetBestBlock());
2372 // Special case for the genesis block, skipping connection of its transactions
2373 // (its coinbase is unspendable)
2374 if (block.GetHash() == chainparams.GetConsensus().hashGenesisBlock) {
2375 if (!fJustCheck)
2376 view.SetBestBlock(pindex->GetBlockHash());
2377 return true;
2380 bool fScriptChecks = true;
2381 if (fCheckpointsEnabled) {
2382 CBlockIndex *pindexLastCheckpoint = Checkpoints::GetLastCheckpoint(chainparams.Checkpoints());
2383 if (pindexLastCheckpoint && pindexLastCheckpoint->GetAncestor(pindex->nHeight) == pindex) {
2384 // This block is an ancestor of a checkpoint: disable script checks
2385 fScriptChecks = false;
2389 int64_t nTime1 = GetTimeMicros(); nTimeCheck += nTime1 - nTimeStart;
2390 LogPrint("bench", " - Sanity checks: %.2fms [%.2fs]\n", 0.001 * (nTime1 - nTimeStart), nTimeCheck * 0.000001);
2392 // Do not allow blocks that contain transactions which 'overwrite' older transactions,
2393 // unless those are already completely spent.
2394 // If such overwrites are allowed, coinbases and transactions depending upon those
2395 // can be duplicated to remove the ability to spend the first instance -- even after
2396 // being sent to another address.
2397 // See BIP30 and http://r6.ca/blog/20120206T005236Z.html for more information.
2398 // This logic is not necessary for memory pool transactions, as AcceptToMemoryPool
2399 // already refuses previously-known transaction ids entirely.
2400 // This rule was originally applied to all blocks with a timestamp after March 15, 2012, 0:00 UTC.
2401 // Now that the whole chain is irreversibly beyond that time it is applied to all blocks except the
2402 // two in the chain that violate it. This prevents exploiting the issue against nodes during their
2403 // initial block download.
2404 bool fEnforceBIP30 = (!pindex->phashBlock) || // Enforce on CreateNewBlock invocations which don't have a hash.
2405 !((pindex->nHeight==91842 && pindex->GetBlockHash() == uint256S("0x00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec")) ||
2406 (pindex->nHeight==91880 && pindex->GetBlockHash() == uint256S("0x00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721")));
2408 // Once BIP34 activated it was not possible to create new duplicate coinbases and thus other than starting
2409 // with the 2 existing duplicate coinbase pairs, not possible to create overwriting txs. But by the
2410 // time BIP34 activated, in each of the existing pairs the duplicate coinbase had overwritten the first
2411 // before the first had been spent. Since those coinbases are sufficiently buried its no longer possible to create further
2412 // duplicate transactions descending from the known pairs either.
2413 // If we're on the known chain at height greater than where BIP34 activated, we can save the db accesses needed for the BIP30 check.
2414 CBlockIndex *pindexBIP34height = pindex->pprev->GetAncestor(chainparams.GetConsensus().BIP34Height);
2415 //Only continue to enforce if we're below BIP34 activation height or the block hash at that height doesn't correspond.
2416 fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->GetBlockHash() == chainparams.GetConsensus().BIP34Hash));
2418 if (fEnforceBIP30) {
2419 BOOST_FOREACH(const CTransaction& tx, block.vtx) {
2420 const CCoins* coins = view.AccessCoins(tx.GetHash());
2421 if (coins && !coins->IsPruned())
2422 return state.DoS(100, error("ConnectBlock(): tried to overwrite transaction"),
2423 REJECT_INVALID, "bad-txns-BIP30");
2427 // BIP16 didn't become active until Apr 1 2012
2428 int64_t nBIP16SwitchTime = 1333238400;
2429 bool fStrictPayToScriptHash = (pindex->GetBlockTime() >= nBIP16SwitchTime);
2431 unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE;
2433 // Start enforcing the DERSIG (BIP66) rule
2434 if (pindex->nHeight >= chainparams.GetConsensus().BIP66Height) {
2435 flags |= SCRIPT_VERIFY_DERSIG;
2438 // Start enforcing CHECKLOCKTIMEVERIFY (BIP65) rule
2439 if (pindex->nHeight >= chainparams.GetConsensus().BIP65Height) {
2440 flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
2443 // Start enforcing BIP68 (sequence locks) and BIP112 (CHECKSEQUENCEVERIFY) using versionbits logic.
2444 int nLockTimeFlags = 0;
2445 if (VersionBitsState(pindex->pprev, chainparams.GetConsensus(), Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
2446 flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
2447 nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE;
2450 // Start enforcing WITNESS rules using versionbits logic.
2451 if (IsWitnessEnabled(pindex->pprev, chainparams.GetConsensus())) {
2452 flags |= SCRIPT_VERIFY_WITNESS;
2453 flags |= SCRIPT_VERIFY_NULLDUMMY;
2456 int64_t nTime2 = GetTimeMicros(); nTimeForks += nTime2 - nTime1;
2457 LogPrint("bench", " - Fork checks: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1), nTimeForks * 0.000001);
2459 CBlockUndo blockundo;
2461 CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL);
2463 std::vector<uint256> vOrphanErase;
2464 std::vector<int> prevheights;
2465 CAmount nFees = 0;
2466 int nInputs = 0;
2467 int64_t nSigOpsCost = 0;
2468 CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
2469 std::vector<std::pair<uint256, CDiskTxPos> > vPos;
2470 vPos.reserve(block.vtx.size());
2471 blockundo.vtxundo.reserve(block.vtx.size() - 1);
2472 std::vector<PrecomputedTransactionData> txdata;
2473 txdata.reserve(block.vtx.size()); // Required so that pointers to individual PrecomputedTransactionData don't get invalidated
2474 for (unsigned int i = 0; i < block.vtx.size(); i++)
2476 const CTransaction &tx = block.vtx[i];
2478 nInputs += tx.vin.size();
2480 if (!tx.IsCoinBase())
2482 if (!view.HaveInputs(tx))
2483 return state.DoS(100, error("ConnectBlock(): inputs missing/spent"),
2484 REJECT_INVALID, "bad-txns-inputs-missingorspent");
2486 // Check that transaction is BIP68 final
2487 // BIP68 lock checks (as opposed to nLockTime checks) must
2488 // be in ConnectBlock because they require the UTXO set
2489 prevheights.resize(tx.vin.size());
2490 for (size_t j = 0; j < tx.vin.size(); j++) {
2491 prevheights[j] = view.AccessCoins(tx.vin[j].prevout.hash)->nHeight;
2494 // Which orphan pool entries must we evict?
2495 for (size_t j = 0; j < tx.vin.size(); j++) {
2496 auto itByPrev = mapOrphanTransactionsByPrev.find(tx.vin[j].prevout);
2497 if (itByPrev == mapOrphanTransactionsByPrev.end()) continue;
2498 for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
2499 const CTransaction& orphanTx = (*mi)->second.tx;
2500 const uint256& orphanHash = orphanTx.GetHash();
2501 vOrphanErase.push_back(orphanHash);
2505 if (!SequenceLocks(tx, nLockTimeFlags, &prevheights, *pindex)) {
2506 return state.DoS(100, error("%s: contains a non-BIP68-final transaction", __func__),
2507 REJECT_INVALID, "bad-txns-nonfinal");
2511 // GetTransactionSigOpCost counts 3 types of sigops:
2512 // * legacy (always)
2513 // * p2sh (when P2SH enabled in flags and excludes coinbase)
2514 // * witness (when witness enabled in flags and excludes coinbase)
2515 nSigOpsCost += GetTransactionSigOpCost(tx, view, flags);
2516 if (nSigOpsCost > MAX_BLOCK_SIGOPS_COST)
2517 return state.DoS(100, error("ConnectBlock(): too many sigops"),
2518 REJECT_INVALID, "bad-blk-sigops");
2520 txdata.emplace_back(tx);
2521 if (!tx.IsCoinBase())
2523 nFees += view.GetValueIn(tx)-tx.GetValueOut();
2525 std::vector<CScriptCheck> vChecks;
2526 bool fCacheResults = fJustCheck; /* Don't cache results if we're actually connecting blocks (still consult the cache, though) */
2527 if (!CheckInputs(tx, state, view, fScriptChecks, flags, fCacheResults, txdata[i], nScriptCheckThreads ? &vChecks : NULL))
2528 return error("ConnectBlock(): CheckInputs on %s failed with %s",
2529 tx.GetHash().ToString(), FormatStateMessage(state));
2530 control.Add(vChecks);
2533 CTxUndo undoDummy;
2534 if (i > 0) {
2535 blockundo.vtxundo.push_back(CTxUndo());
2537 UpdateCoins(tx, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight);
2539 vPos.push_back(std::make_pair(tx.GetHash(), pos));
2540 pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
2542 int64_t nTime3 = GetTimeMicros(); nTimeConnect += nTime3 - nTime2;
2543 LogPrint("bench", " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs]\n", (unsigned)block.vtx.size(), 0.001 * (nTime3 - nTime2), 0.001 * (nTime3 - nTime2) / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * (nTime3 - nTime2) / (nInputs-1), nTimeConnect * 0.000001);
2545 CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, chainparams.GetConsensus());
2546 if (block.vtx[0].GetValueOut() > blockReward)
2547 return state.DoS(100,
2548 error("ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)",
2549 block.vtx[0].GetValueOut(), blockReward),
2550 REJECT_INVALID, "bad-cb-amount");
2552 if (!control.Wait())
2553 return state.DoS(100, false);
2554 int64_t nTime4 = GetTimeMicros(); nTimeVerify += nTime4 - nTime2;
2555 LogPrint("bench", " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs]\n", nInputs - 1, 0.001 * (nTime4 - nTime2), nInputs <= 1 ? 0 : 0.001 * (nTime4 - nTime2) / (nInputs-1), nTimeVerify * 0.000001);
2557 if (fJustCheck)
2558 return true;
2560 // Write undo information to disk
2561 if (pindex->GetUndoPos().IsNull() || !pindex->IsValid(BLOCK_VALID_SCRIPTS))
2563 if (pindex->GetUndoPos().IsNull()) {
2564 CDiskBlockPos _pos;
2565 if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
2566 return error("ConnectBlock(): FindUndoPos failed");
2567 if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart()))
2568 return AbortNode(state, "Failed to write undo data");
2570 // update nUndoPos in block index
2571 pindex->nUndoPos = _pos.nPos;
2572 pindex->nStatus |= BLOCK_HAVE_UNDO;
2575 pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
2576 setDirtyBlockIndex.insert(pindex);
2579 if (fTxIndex)
2580 if (!pblocktree->WriteTxIndex(vPos))
2581 return AbortNode(state, "Failed to write transaction index");
2583 // add this block to the view's block chain
2584 view.SetBestBlock(pindex->GetBlockHash());
2586 int64_t nTime5 = GetTimeMicros(); nTimeIndex += nTime5 - nTime4;
2587 LogPrint("bench", " - Index writing: %.2fms [%.2fs]\n", 0.001 * (nTime5 - nTime4), nTimeIndex * 0.000001);
2589 // Watch for changes to the previous coinbase transaction.
2590 static uint256 hashPrevBestCoinBase;
2591 GetMainSignals().UpdatedTransaction(hashPrevBestCoinBase);
2592 hashPrevBestCoinBase = block.vtx[0].GetHash();
2594 // Erase orphan transactions include or precluded by this block
2595 if (vOrphanErase.size()) {
2596 int nErased = 0;
2597 BOOST_FOREACH(uint256 &orphanHash, vOrphanErase) {
2598 nErased += EraseOrphanTx(orphanHash);
2600 LogPrint("mempool", "Erased %d orphan tx included or conflicted by block\n", nErased);
2603 int64_t nTime6 = GetTimeMicros(); nTimeCallbacks += nTime6 - nTime5;
2604 LogPrint("bench", " - Callbacks: %.2fms [%.2fs]\n", 0.001 * (nTime6 - nTime5), nTimeCallbacks * 0.000001);
2606 return true;
2610 * Update the on-disk chain state.
2611 * The caches and indexes are flushed depending on the mode we're called with
2612 * if they're too large, if it's been a while since the last write,
2613 * or always and in all cases if we're in prune mode and are deleting files.
2615 bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
2616 const CChainParams& chainparams = Params();
2617 LOCK2(cs_main, cs_LastBlockFile);
2618 static int64_t nLastWrite = 0;
2619 static int64_t nLastFlush = 0;
2620 static int64_t nLastSetChain = 0;
2621 std::set<int> setFilesToPrune;
2622 bool fFlushForPrune = false;
2623 try {
2624 if (fPruneMode && fCheckForPruning && !fReindex) {
2625 FindFilesToPrune(setFilesToPrune, chainparams.PruneAfterHeight());
2626 fCheckForPruning = false;
2627 if (!setFilesToPrune.empty()) {
2628 fFlushForPrune = true;
2629 if (!fHavePruned) {
2630 pblocktree->WriteFlag("prunedblockfiles", true);
2631 fHavePruned = true;
2635 int64_t nNow = GetTimeMicros();
2636 // Avoid writing/flushing immediately after startup.
2637 if (nLastWrite == 0) {
2638 nLastWrite = nNow;
2640 if (nLastFlush == 0) {
2641 nLastFlush = nNow;
2643 if (nLastSetChain == 0) {
2644 nLastSetChain = nNow;
2646 size_t cacheSize = pcoinsTip->DynamicMemoryUsage();
2647 // The cache is large and close to the limit, but we have time now (not in the middle of a block processing).
2648 bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize * (10.0/9) > nCoinCacheUsage;
2649 // The cache is over the limit, we have to write now.
2650 bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && cacheSize > nCoinCacheUsage;
2651 // 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.
2652 bool fPeriodicWrite = mode == FLUSH_STATE_PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
2653 // It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
2654 bool fPeriodicFlush = mode == FLUSH_STATE_PERIODIC && nNow > nLastFlush + (int64_t)DATABASE_FLUSH_INTERVAL * 1000000;
2655 // Combine all conditions that result in a full cache flush.
2656 bool fDoFullFlush = (mode == FLUSH_STATE_ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
2657 // Write blocks and block index to disk.
2658 if (fDoFullFlush || fPeriodicWrite) {
2659 // Depend on nMinDiskSpace to ensure we can write block index
2660 if (!CheckDiskSpace(0))
2661 return state.Error("out of disk space");
2662 // First make sure all block and undo data is flushed to disk.
2663 FlushBlockFile();
2664 // Then update all block file information (which may refer to block and undo files).
2666 std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
2667 vFiles.reserve(setDirtyFileInfo.size());
2668 for (set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
2669 vFiles.push_back(make_pair(*it, &vinfoBlockFile[*it]));
2670 setDirtyFileInfo.erase(it++);
2672 std::vector<const CBlockIndex*> vBlocks;
2673 vBlocks.reserve(setDirtyBlockIndex.size());
2674 for (set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
2675 vBlocks.push_back(*it);
2676 setDirtyBlockIndex.erase(it++);
2678 if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
2679 return AbortNode(state, "Files to write to block index database");
2682 // Finally remove any pruned files
2683 if (fFlushForPrune)
2684 UnlinkPrunedFiles(setFilesToPrune);
2685 nLastWrite = nNow;
2687 // Flush best chain related state. This can only be done if the blocks / block index write was also done.
2688 if (fDoFullFlush) {
2689 // Typical CCoins structures on disk are around 128 bytes in size.
2690 // Pushing a new one to the database can cause it to be written
2691 // twice (once in the log, and once in the tables). This is already
2692 // an overestimation, as most will delete an existing entry or
2693 // overwrite one. Still, use a conservative safety factor of 2.
2694 if (!CheckDiskSpace(128 * 2 * 2 * pcoinsTip->GetCacheSize()))
2695 return state.Error("out of disk space");
2696 // Flush the chainstate (which may refer to block index entries).
2697 if (!pcoinsTip->Flush())
2698 return AbortNode(state, "Failed to write to coin database");
2699 nLastFlush = nNow;
2701 if (fDoFullFlush || ((mode == FLUSH_STATE_ALWAYS || mode == FLUSH_STATE_PERIODIC) && nNow > nLastSetChain + (int64_t)DATABASE_WRITE_INTERVAL * 1000000)) {
2702 // Update best block in wallet (so we can detect restored wallets).
2703 GetMainSignals().SetBestChain(chainActive.GetLocator());
2704 nLastSetChain = nNow;
2706 } catch (const std::runtime_error& e) {
2707 return AbortNode(state, std::string("System error while flushing: ") + e.what());
2709 return true;
2712 void FlushStateToDisk() {
2713 CValidationState state;
2714 FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
2717 void PruneAndFlush() {
2718 CValidationState state;
2719 fCheckForPruning = true;
2720 FlushStateToDisk(state, FLUSH_STATE_NONE);
2723 /** Update chainActive and related internal data structures. */
2724 void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
2725 chainActive.SetTip(pindexNew);
2727 // New best block
2728 mempool.AddTransactionsUpdated(1);
2730 cvBlockChange.notify_all();
2732 static bool fWarned = false;
2733 std::vector<std::string> warningMessages;
2734 if (!IsInitialBlockDownload())
2736 int nUpgraded = 0;
2737 const CBlockIndex* pindex = chainActive.Tip();
2738 for (int bit = 0; bit < VERSIONBITS_NUM_BITS; bit++) {
2739 WarningBitsConditionChecker checker(bit);
2740 ThresholdState state = checker.GetStateFor(pindex, chainParams.GetConsensus(), warningcache[bit]);
2741 if (state == THRESHOLD_ACTIVE || state == THRESHOLD_LOCKED_IN) {
2742 if (state == THRESHOLD_ACTIVE) {
2743 strMiscWarning = strprintf(_("Warning: unknown new rules activated (versionbit %i)"), bit);
2744 if (!fWarned) {
2745 AlertNotify(strMiscWarning);
2746 fWarned = true;
2748 } else {
2749 warningMessages.push_back(strprintf("unknown new rules are about to activate (versionbit %i)", bit));
2753 // Check the version of the last 100 blocks to see if we need to upgrade:
2754 for (int i = 0; i < 100 && pindex != NULL; i++)
2756 int32_t nExpectedVersion = ComputeBlockVersion(pindex->pprev, chainParams.GetConsensus());
2757 if (pindex->nVersion > VERSIONBITS_LAST_OLD_BLOCK_VERSION && (pindex->nVersion & ~nExpectedVersion) != 0)
2758 ++nUpgraded;
2759 pindex = pindex->pprev;
2761 if (nUpgraded > 0)
2762 warningMessages.push_back(strprintf("%d of last 100 blocks have unexpected version", nUpgraded));
2763 if (nUpgraded > 100/2)
2765 // strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
2766 strMiscWarning = _("Warning: Unknown block versions being mined! It's possible unknown rules are in effect");
2767 if (!fWarned) {
2768 AlertNotify(strMiscWarning);
2769 fWarned = true;
2773 LogPrintf("%s: new best=%s height=%d version=0x%08x log2_work=%.8g tx=%lu date='%s' progress=%f cache=%.1fMiB(%utx)", __func__,
2774 chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), chainActive.Tip()->nVersion,
2775 log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
2776 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
2777 Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip()), pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
2778 if (!warningMessages.empty())
2779 LogPrintf(" warning='%s'", boost::algorithm::join(warningMessages, ", "));
2780 LogPrintf("\n");
2784 /** Disconnect chainActive's tip. You probably want to call mempool.removeForReorg and manually re-limit mempool size after this, with cs_main held. */
2785 bool static DisconnectTip(CValidationState& state, const CChainParams& chainparams, bool fBare = false)
2787 CBlockIndex *pindexDelete = chainActive.Tip();
2788 assert(pindexDelete);
2789 // Read block from disk.
2790 CBlock block;
2791 if (!ReadBlockFromDisk(block, pindexDelete, chainparams.GetConsensus()))
2792 return AbortNode(state, "Failed to read block");
2793 // Apply the block atomically to the chain state.
2794 int64_t nStart = GetTimeMicros();
2796 CCoinsViewCache view(pcoinsTip);
2797 if (!DisconnectBlock(block, state, pindexDelete, view))
2798 return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
2799 assert(view.Flush());
2801 LogPrint("bench", "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
2802 // Write the chain state to disk, if necessary.
2803 if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED))
2804 return false;
2806 if (!fBare) {
2807 // Resurrect mempool transactions from the disconnected block.
2808 std::vector<uint256> vHashUpdate;
2809 BOOST_FOREACH(const CTransaction &tx, block.vtx) {
2810 // ignore validation errors in resurrected transactions
2811 CValidationState stateDummy;
2812 if (tx.IsCoinBase() || !AcceptToMemoryPool(mempool, stateDummy, tx, false, NULL, true)) {
2813 mempool.removeRecursive(tx);
2814 } else if (mempool.exists(tx.GetHash())) {
2815 vHashUpdate.push_back(tx.GetHash());
2818 // AcceptToMemoryPool/addUnchecked all assume that new mempool entries have
2819 // no in-mempool children, which is generally not true when adding
2820 // previously-confirmed transactions back to the mempool.
2821 // UpdateTransactionsFromBlock finds descendants of any transactions in this
2822 // block that were added back and cleans up the mempool state.
2823 mempool.UpdateTransactionsFromBlock(vHashUpdate);
2826 // Update chainActive and related variables.
2827 UpdateTip(pindexDelete->pprev, chainparams);
2828 // Let wallets know transactions went from 1-confirmed to
2829 // 0-confirmed or conflicted:
2830 BOOST_FOREACH(const CTransaction &tx, block.vtx) {
2831 GetMainSignals().SyncTransaction(tx, pindexDelete->pprev, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK);
2833 return true;
2836 static int64_t nTimeReadFromDisk = 0;
2837 static int64_t nTimeConnectTotal = 0;
2838 static int64_t nTimeFlush = 0;
2839 static int64_t nTimeChainState = 0;
2840 static int64_t nTimePostConnect = 0;
2843 * Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
2844 * corresponding to pindexNew, to bypass loading it again from disk.
2846 bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const CBlock* pblock, std::vector<std::shared_ptr<const CTransaction>> &txConflicted, std::vector<std::tuple<CTransaction,CBlockIndex*,int>> &txChanged)
2848 assert(pindexNew->pprev == chainActive.Tip());
2849 // Read block from disk.
2850 int64_t nTime1 = GetTimeMicros();
2851 CBlock block;
2852 if (!pblock) {
2853 if (!ReadBlockFromDisk(block, pindexNew, chainparams.GetConsensus()))
2854 return AbortNode(state, "Failed to read block");
2855 pblock = &block;
2857 // Apply the block atomically to the chain state.
2858 int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1;
2859 int64_t nTime3;
2860 LogPrint("bench", " - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * 0.001, nTimeReadFromDisk * 0.000001);
2862 CCoinsViewCache view(pcoinsTip);
2863 bool rv = ConnectBlock(*pblock, state, pindexNew, view, chainparams);
2864 GetMainSignals().BlockChecked(*pblock, state);
2865 if (!rv) {
2866 if (state.IsInvalid())
2867 InvalidBlockFound(pindexNew, state);
2868 return error("ConnectTip(): ConnectBlock %s failed", pindexNew->GetBlockHash().ToString());
2870 nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2;
2871 LogPrint("bench", " - Connect total: %.2fms [%.2fs]\n", (nTime3 - nTime2) * 0.001, nTimeConnectTotal * 0.000001);
2872 assert(view.Flush());
2874 int64_t nTime4 = GetTimeMicros(); nTimeFlush += nTime4 - nTime3;
2875 LogPrint("bench", " - Flush: %.2fms [%.2fs]\n", (nTime4 - nTime3) * 0.001, nTimeFlush * 0.000001);
2876 // Write the chain state to disk, if necessary.
2877 if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED))
2878 return false;
2879 int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
2880 LogPrint("bench", " - Writing chainstate: %.2fms [%.2fs]\n", (nTime5 - nTime4) * 0.001, nTimeChainState * 0.000001);
2881 // Remove conflicting transactions from the mempool.;
2882 mempool.removeForBlock(pblock->vtx, pindexNew->nHeight, &txConflicted, !IsInitialBlockDownload());
2883 // Update chainActive & related variables.
2884 UpdateTip(pindexNew, chainparams);
2886 for(unsigned int i=0; i < pblock->vtx.size(); i++)
2887 txChanged.emplace_back(pblock->vtx[i], pindexNew, i);
2889 int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
2890 LogPrint("bench", " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001);
2891 LogPrint("bench", "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001);
2892 return true;
2896 * Return the tip of the chain with the most work in it, that isn't
2897 * known to be invalid (it's however far from certain to be valid).
2899 static CBlockIndex* FindMostWorkChain() {
2900 do {
2901 CBlockIndex *pindexNew = NULL;
2903 // Find the best candidate header.
2905 std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexCandidates.rbegin();
2906 if (it == setBlockIndexCandidates.rend())
2907 return NULL;
2908 pindexNew = *it;
2911 // Check whether all blocks on the path between the currently active chain and the candidate are valid.
2912 // Just going until the active chain is an optimization, as we know all blocks in it are valid already.
2913 CBlockIndex *pindexTest = pindexNew;
2914 bool fInvalidAncestor = false;
2915 while (pindexTest && !chainActive.Contains(pindexTest)) {
2916 assert(pindexTest->nChainTx || pindexTest->nHeight == 0);
2918 // Pruned nodes may have entries in setBlockIndexCandidates for
2919 // which block files have been deleted. Remove those as candidates
2920 // for the most work chain if we come across them; we can't switch
2921 // to a chain unless we have all the non-active-chain parent blocks.
2922 bool fFailedChain = pindexTest->nStatus & BLOCK_FAILED_MASK;
2923 bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
2924 if (fFailedChain || fMissingData) {
2925 // Candidate chain is not usable (either invalid or missing data)
2926 if (fFailedChain && (pindexBestInvalid == NULL || pindexNew->nChainWork > pindexBestInvalid->nChainWork))
2927 pindexBestInvalid = pindexNew;
2928 CBlockIndex *pindexFailed = pindexNew;
2929 // Remove the entire chain from the set.
2930 while (pindexTest != pindexFailed) {
2931 if (fFailedChain) {
2932 pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
2933 } else if (fMissingData) {
2934 // If we're missing data, then add back to mapBlocksUnlinked,
2935 // so that if the block arrives in the future we can try adding
2936 // to setBlockIndexCandidates again.
2937 mapBlocksUnlinked.insert(std::make_pair(pindexFailed->pprev, pindexFailed));
2939 setBlockIndexCandidates.erase(pindexFailed);
2940 pindexFailed = pindexFailed->pprev;
2942 setBlockIndexCandidates.erase(pindexTest);
2943 fInvalidAncestor = true;
2944 break;
2946 pindexTest = pindexTest->pprev;
2948 if (!fInvalidAncestor)
2949 return pindexNew;
2950 } while(true);
2953 /** Delete all entries in setBlockIndexCandidates that are worse than the current tip. */
2954 static void PruneBlockIndexCandidates() {
2955 // Note that we can't delete the current block itself, as we may need to return to it later in case a
2956 // reorganization to a better block fails.
2957 std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
2958 while (it != setBlockIndexCandidates.end() && setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) {
2959 setBlockIndexCandidates.erase(it++);
2961 // Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates.
2962 assert(!setBlockIndexCandidates.empty());
2966 * Try to make some progress towards making pindexMostWork the active block.
2967 * pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
2969 static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const CBlock* pblock, bool& fInvalidFound, std::vector<std::shared_ptr<const CTransaction>>& txConflicted, std::vector<std::tuple<CTransaction,CBlockIndex*,int>>& txChanged)
2971 AssertLockHeld(cs_main);
2972 const CBlockIndex *pindexOldTip = chainActive.Tip();
2973 const CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork);
2975 // Disconnect active blocks which are no longer in the best chain.
2976 bool fBlocksDisconnected = false;
2977 while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
2978 if (!DisconnectTip(state, chainparams))
2979 return false;
2980 fBlocksDisconnected = true;
2983 // Build list of new blocks to connect.
2984 std::vector<CBlockIndex*> vpindexToConnect;
2985 bool fContinue = true;
2986 int nHeight = pindexFork ? pindexFork->nHeight : -1;
2987 while (fContinue && nHeight != pindexMostWork->nHeight) {
2988 // Don't iterate the entire list of potential improvements toward the best tip, as we likely only need
2989 // a few blocks along the way.
2990 int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight);
2991 vpindexToConnect.clear();
2992 vpindexToConnect.reserve(nTargetHeight - nHeight);
2993 CBlockIndex *pindexIter = pindexMostWork->GetAncestor(nTargetHeight);
2994 while (pindexIter && pindexIter->nHeight != nHeight) {
2995 vpindexToConnect.push_back(pindexIter);
2996 pindexIter = pindexIter->pprev;
2998 nHeight = nTargetHeight;
3000 // Connect new blocks.
3001 BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) {
3002 if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL, txConflicted, txChanged)) {
3003 if (state.IsInvalid()) {
3004 // The block violates a consensus rule.
3005 if (!state.CorruptionPossible())
3006 InvalidChainFound(vpindexToConnect.back());
3007 state = CValidationState();
3008 fInvalidFound = true;
3009 fContinue = false;
3010 break;
3011 } else {
3012 // A system error occurred (disk space, database error, ...).
3013 return false;
3015 } else {
3016 PruneBlockIndexCandidates();
3017 if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
3018 // We're in a better position than we were. Return temporarily to release the lock.
3019 fContinue = false;
3020 break;
3026 if (fBlocksDisconnected) {
3027 mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
3028 LimitMempoolSize(mempool, GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
3030 mempool.check(pcoinsTip);
3032 // Callbacks/notifications for a new best chain.
3033 if (fInvalidFound)
3034 CheckForkWarningConditionsOnNewFork(vpindexToConnect.back());
3035 else
3036 CheckForkWarningConditions();
3038 return true;
3041 static void NotifyHeaderTip() {
3042 bool fNotify = false;
3043 bool fInitialBlockDownload = false;
3044 static CBlockIndex* pindexHeaderOld = NULL;
3045 CBlockIndex* pindexHeader = NULL;
3047 LOCK(cs_main);
3048 pindexHeader = pindexBestHeader;
3050 if (pindexHeader != pindexHeaderOld) {
3051 fNotify = true;
3052 fInitialBlockDownload = IsInitialBlockDownload();
3053 pindexHeaderOld = pindexHeader;
3056 // Send block tip changed notifications without cs_main
3057 if (fNotify) {
3058 uiInterface.NotifyHeaderTip(fInitialBlockDownload, pindexHeader);
3063 * Make the best chain active, in multiple steps. The result is either failure
3064 * or an activated best chain. pblock is either NULL or a pointer to a block
3065 * that is already loaded (to avoid loading it again from disk).
3067 bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, const CBlock *pblock) {
3068 CBlockIndex *pindexMostWork = NULL;
3069 CBlockIndex *pindexNewTip = NULL;
3070 std::vector<std::tuple<CTransaction,CBlockIndex*,int>> txChanged;
3071 if (pblock)
3072 txChanged.reserve(pblock->vtx.size());
3073 do {
3074 txChanged.clear();
3075 boost::this_thread::interruption_point();
3076 if (ShutdownRequested())
3077 break;
3079 const CBlockIndex *pindexFork;
3080 std::vector<std::shared_ptr<const CTransaction>> txConflicted;
3081 bool fInitialDownload;
3083 LOCK(cs_main);
3084 CBlockIndex *pindexOldTip = chainActive.Tip();
3085 if (pindexMostWork == NULL) {
3086 pindexMostWork = FindMostWorkChain();
3089 // Whether we have anything to do at all.
3090 if (pindexMostWork == NULL || pindexMostWork == chainActive.Tip())
3091 return true;
3093 bool fInvalidFound = false;
3094 if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL, fInvalidFound, txConflicted, txChanged))
3095 return false;
3097 if (fInvalidFound) {
3098 // Wipe cache, we may need another branch now.
3099 pindexMostWork = NULL;
3101 pindexNewTip = chainActive.Tip();
3102 pindexFork = chainActive.FindFork(pindexOldTip);
3103 fInitialDownload = IsInitialBlockDownload();
3105 // When we reach this point, we switched to a new tip (stored in pindexNewTip).
3107 // Notifications/callbacks that can run without cs_main
3109 // throw all transactions though the signal-interface
3110 // while _not_ holding the cs_main lock
3111 for(std::shared_ptr<const CTransaction> tx : txConflicted)
3113 GetMainSignals().SyncTransaction(*tx, pindexNewTip, CMainSignals::SYNC_TRANSACTION_NOT_IN_BLOCK);
3115 // ... and about transactions that got confirmed:
3116 for(unsigned int i = 0; i < txChanged.size(); i++)
3117 GetMainSignals().SyncTransaction(std::get<0>(txChanged[i]), std::get<1>(txChanged[i]), std::get<2>(txChanged[i]));
3119 // Notify external listeners about the new tip.
3120 GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);
3122 // Always notify the UI if a new block tip was connected
3123 if (pindexFork != pindexNewTip) {
3124 uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
3126 } while (pindexNewTip != pindexMostWork);
3127 CheckBlockIndex(chainparams.GetConsensus());
3129 // Write changes periodically to disk, after relay.
3130 if (!FlushStateToDisk(state, FLUSH_STATE_PERIODIC)) {
3131 return false;
3134 return true;
3138 bool PreciousBlock(CValidationState& state, const CChainParams& params, CBlockIndex *pindex)
3141 LOCK(cs_main);
3142 if (pindex->nChainWork < chainActive.Tip()->nChainWork) {
3143 // Nothing to do, this block is not at the tip.
3144 return true;
3146 if (chainActive.Tip()->nChainWork > nLastPreciousChainwork) {
3147 // The chain has been extended since the last call, reset the counter.
3148 nBlockReverseSequenceId = -1;
3150 nLastPreciousChainwork = chainActive.Tip()->nChainWork;
3151 setBlockIndexCandidates.erase(pindex);
3152 pindex->nSequenceId = nBlockReverseSequenceId;
3153 if (nBlockReverseSequenceId > std::numeric_limits<int32_t>::min()) {
3154 // We can't keep reducing the counter if somebody really wants to
3155 // call preciousblock 2**31-1 times on the same set of tips...
3156 nBlockReverseSequenceId--;
3158 if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && pindex->nChainTx) {
3159 setBlockIndexCandidates.insert(pindex);
3160 PruneBlockIndexCandidates();
3164 return ActivateBestChain(state, params);
3167 bool InvalidateBlock(CValidationState& state, const CChainParams& chainparams, CBlockIndex *pindex)
3169 AssertLockHeld(cs_main);
3171 // Mark the block itself as invalid.
3172 pindex->nStatus |= BLOCK_FAILED_VALID;
3173 setDirtyBlockIndex.insert(pindex);
3174 setBlockIndexCandidates.erase(pindex);
3176 while (chainActive.Contains(pindex)) {
3177 CBlockIndex *pindexWalk = chainActive.Tip();
3178 pindexWalk->nStatus |= BLOCK_FAILED_CHILD;
3179 setDirtyBlockIndex.insert(pindexWalk);
3180 setBlockIndexCandidates.erase(pindexWalk);
3181 // ActivateBestChain considers blocks already in chainActive
3182 // unconditionally valid already, so force disconnect away from it.
3183 if (!DisconnectTip(state, chainparams)) {
3184 mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
3185 return false;
3189 LimitMempoolSize(mempool, GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
3191 // The resulting new best tip may not be in setBlockIndexCandidates anymore, so
3192 // add it again.
3193 BlockMap::iterator it = mapBlockIndex.begin();
3194 while (it != mapBlockIndex.end()) {
3195 if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && !setBlockIndexCandidates.value_comp()(it->second, chainActive.Tip())) {
3196 setBlockIndexCandidates.insert(it->second);
3198 it++;
3201 InvalidChainFound(pindex);
3202 mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
3203 return true;
3206 bool ResetBlockFailureFlags(CBlockIndex *pindex) {
3207 AssertLockHeld(cs_main);
3209 int nHeight = pindex->nHeight;
3211 // Remove the invalidity flag from this block and all its descendants.
3212 BlockMap::iterator it = mapBlockIndex.begin();
3213 while (it != mapBlockIndex.end()) {
3214 if (!it->second->IsValid() && it->second->GetAncestor(nHeight) == pindex) {
3215 it->second->nStatus &= ~BLOCK_FAILED_MASK;
3216 setDirtyBlockIndex.insert(it->second);
3217 if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && setBlockIndexCandidates.value_comp()(chainActive.Tip(), it->second)) {
3218 setBlockIndexCandidates.insert(it->second);
3220 if (it->second == pindexBestInvalid) {
3221 // Reset invalid block marker if it was pointing to one of those.
3222 pindexBestInvalid = NULL;
3225 it++;
3228 // Remove the invalidity flag from all ancestors too.
3229 while (pindex != NULL) {
3230 if (pindex->nStatus & BLOCK_FAILED_MASK) {
3231 pindex->nStatus &= ~BLOCK_FAILED_MASK;
3232 setDirtyBlockIndex.insert(pindex);
3234 pindex = pindex->pprev;
3236 return true;
3239 CBlockIndex* AddToBlockIndex(const CBlockHeader& block)
3241 // Check for duplicate
3242 uint256 hash = block.GetHash();
3243 BlockMap::iterator it = mapBlockIndex.find(hash);
3244 if (it != mapBlockIndex.end())
3245 return it->second;
3247 // Construct new block index object
3248 CBlockIndex* pindexNew = new CBlockIndex(block);
3249 assert(pindexNew);
3250 // We assign the sequence id to blocks only when the full data is available,
3251 // to avoid miners withholding blocks but broadcasting headers, to get a
3252 // competitive advantage.
3253 pindexNew->nSequenceId = 0;
3254 BlockMap::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
3255 pindexNew->phashBlock = &((*mi).first);
3256 BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock);
3257 if (miPrev != mapBlockIndex.end())
3259 pindexNew->pprev = (*miPrev).second;
3260 pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
3261 pindexNew->BuildSkip();
3263 pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew);
3264 pindexNew->RaiseValidity(BLOCK_VALID_TREE);
3265 if (pindexBestHeader == NULL || pindexBestHeader->nChainWork < pindexNew->nChainWork)
3266 pindexBestHeader = pindexNew;
3268 setDirtyBlockIndex.insert(pindexNew);
3270 return pindexNew;
3273 /** Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */
3274 bool ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBlockIndex *pindexNew, const CDiskBlockPos& pos)
3276 pindexNew->nTx = block.vtx.size();
3277 pindexNew->nChainTx = 0;
3278 pindexNew->nFile = pos.nFile;
3279 pindexNew->nDataPos = pos.nPos;
3280 pindexNew->nUndoPos = 0;
3281 pindexNew->nStatus |= BLOCK_HAVE_DATA;
3282 if (IsWitnessEnabled(pindexNew->pprev, Params().GetConsensus())) {
3283 pindexNew->nStatus |= BLOCK_OPT_WITNESS;
3285 pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
3286 setDirtyBlockIndex.insert(pindexNew);
3288 if (pindexNew->pprev == NULL || pindexNew->pprev->nChainTx) {
3289 // If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
3290 deque<CBlockIndex*> queue;
3291 queue.push_back(pindexNew);
3293 // Recursively process any descendant blocks that now may be eligible to be connected.
3294 while (!queue.empty()) {
3295 CBlockIndex *pindex = queue.front();
3296 queue.pop_front();
3297 pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
3299 LOCK(cs_nBlockSequenceId);
3300 pindex->nSequenceId = nBlockSequenceId++;
3302 if (chainActive.Tip() == NULL || !setBlockIndexCandidates.value_comp()(pindex, chainActive.Tip())) {
3303 setBlockIndexCandidates.insert(pindex);
3305 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex);
3306 while (range.first != range.second) {
3307 std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
3308 queue.push_back(it->second);
3309 range.first++;
3310 mapBlocksUnlinked.erase(it);
3313 } else {
3314 if (pindexNew->pprev && pindexNew->pprev->IsValid(BLOCK_VALID_TREE)) {
3315 mapBlocksUnlinked.insert(std::make_pair(pindexNew->pprev, pindexNew));
3319 return true;
3322 bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
3324 LOCK(cs_LastBlockFile);
3326 unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile;
3327 if (vinfoBlockFile.size() <= nFile) {
3328 vinfoBlockFile.resize(nFile + 1);
3331 if (!fKnown) {
3332 while (vinfoBlockFile[nFile].nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
3333 nFile++;
3334 if (vinfoBlockFile.size() <= nFile) {
3335 vinfoBlockFile.resize(nFile + 1);
3338 pos.nFile = nFile;
3339 pos.nPos = vinfoBlockFile[nFile].nSize;
3342 if ((int)nFile != nLastBlockFile) {
3343 if (!fKnown) {
3344 LogPrintf("Leaving block file %i: %s\n", nLastBlockFile, vinfoBlockFile[nLastBlockFile].ToString());
3346 FlushBlockFile(!fKnown);
3347 nLastBlockFile = nFile;
3350 vinfoBlockFile[nFile].AddBlock(nHeight, nTime);
3351 if (fKnown)
3352 vinfoBlockFile[nFile].nSize = std::max(pos.nPos + nAddSize, vinfoBlockFile[nFile].nSize);
3353 else
3354 vinfoBlockFile[nFile].nSize += nAddSize;
3356 if (!fKnown) {
3357 unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
3358 unsigned int nNewChunks = (vinfoBlockFile[nFile].nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
3359 if (nNewChunks > nOldChunks) {
3360 if (fPruneMode)
3361 fCheckForPruning = true;
3362 if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
3363 FILE *file = OpenBlockFile(pos);
3364 if (file) {
3365 LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
3366 AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
3367 fclose(file);
3370 else
3371 return state.Error("out of disk space");
3375 setDirtyFileInfo.insert(nFile);
3376 return true;
3379 bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
3381 pos.nFile = nFile;
3383 LOCK(cs_LastBlockFile);
3385 unsigned int nNewSize;
3386 pos.nPos = vinfoBlockFile[nFile].nUndoSize;
3387 nNewSize = vinfoBlockFile[nFile].nUndoSize += nAddSize;
3388 setDirtyFileInfo.insert(nFile);
3390 unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
3391 unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
3392 if (nNewChunks > nOldChunks) {
3393 if (fPruneMode)
3394 fCheckForPruning = true;
3395 if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
3396 FILE *file = OpenUndoFile(pos);
3397 if (file) {
3398 LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
3399 AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
3400 fclose(file);
3403 else
3404 return state.Error("out of disk space");
3407 return true;
3410 bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW)
3412 // Check proof of work matches claimed amount
3413 if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
3414 return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");
3416 return true;
3419 bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
3421 // These are checks that are independent of context.
3423 if (block.fChecked)
3424 return true;
3426 // Check that the header is valid (particularly PoW). This is mostly
3427 // redundant with the call in AcceptBlockHeader.
3428 if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW))
3429 return false;
3431 // Check the merkle root.
3432 if (fCheckMerkleRoot) {
3433 bool mutated;
3434 uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated);
3435 if (block.hashMerkleRoot != hashMerkleRoot2)
3436 return state.DoS(100, false, REJECT_INVALID, "bad-txnmrklroot", true, "hashMerkleRoot mismatch");
3438 // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
3439 // of transactions in a block without affecting the merkle root of a block,
3440 // while still invalidating it.
3441 if (mutated)
3442 return state.DoS(100, false, REJECT_INVALID, "bad-txns-duplicate", true, "duplicate transaction");
3445 // All potential-corruption validation must be done before we do any
3446 // transaction validation, as otherwise we may mark the header as invalid
3447 // because we receive the wrong transactions for it.
3448 // Note that witness malleability is checked in ContextualCheckBlock, so no
3449 // checks that use witness data may be performed here.
3451 // Size limits
3452 if (block.vtx.empty() || block.vtx.size() > MAX_BLOCK_BASE_SIZE || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) > MAX_BLOCK_BASE_SIZE)
3453 return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed");
3455 // First transaction must be coinbase, the rest must not be
3456 if (block.vtx.empty() || !block.vtx[0].IsCoinBase())
3457 return state.DoS(100, false, REJECT_INVALID, "bad-cb-missing", false, "first tx is not coinbase");
3458 for (unsigned int i = 1; i < block.vtx.size(); i++)
3459 if (block.vtx[i].IsCoinBase())
3460 return state.DoS(100, false, REJECT_INVALID, "bad-cb-multiple", false, "more than one coinbase");
3462 // Check transactions
3463 for (const auto& tx : block.vtx)
3464 if (!CheckTransaction(tx, state))
3465 return state.Invalid(false, state.GetRejectCode(), state.GetRejectReason(),
3466 strprintf("Transaction check failed (tx hash %s) %s", tx.GetHash().ToString(), state.GetDebugMessage()));
3468 unsigned int nSigOps = 0;
3469 for (const auto& tx : block.vtx)
3471 nSigOps += GetLegacySigOpCount(tx);
3473 if (nSigOps * WITNESS_SCALE_FACTOR > MAX_BLOCK_SIGOPS_COST)
3474 return state.DoS(100, false, REJECT_INVALID, "bad-blk-sigops", false, "out-of-bounds SigOpCount");
3476 if (fCheckPOW && fCheckMerkleRoot)
3477 block.fChecked = true;
3479 return true;
3482 static bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidationState& state, const CChainParams& chainparams, const uint256& hash)
3484 if (*pindexPrev->phashBlock == chainparams.GetConsensus().hashGenesisBlock)
3485 return true;
3487 int nHeight = pindexPrev->nHeight+1;
3488 // Don't accept any forks from the main chain prior to last checkpoint
3489 CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainparams.Checkpoints());
3490 if (pcheckpoint && nHeight < pcheckpoint->nHeight)
3491 return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight));
3493 return true;
3496 bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params)
3498 LOCK(cs_main);
3499 return (VersionBitsState(pindexPrev, params, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_ACTIVE);
3502 // Compute at which vout of the block's coinbase transaction the witness
3503 // commitment occurs, or -1 if not found.
3504 static int GetWitnessCommitmentIndex(const CBlock& block)
3506 int commitpos = -1;
3507 for (size_t o = 0; o < block.vtx[0].vout.size(); o++) {
3508 if (block.vtx[0].vout[o].scriptPubKey.size() >= 38 && block.vtx[0].vout[o].scriptPubKey[0] == OP_RETURN && block.vtx[0].vout[o].scriptPubKey[1] == 0x24 && block.vtx[0].vout[o].scriptPubKey[2] == 0xaa && block.vtx[0].vout[o].scriptPubKey[3] == 0x21 && block.vtx[0].vout[o].scriptPubKey[4] == 0xa9 && block.vtx[0].vout[o].scriptPubKey[5] == 0xed) {
3509 commitpos = o;
3512 return commitpos;
3515 void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
3517 int commitpos = GetWitnessCommitmentIndex(block);
3518 static const std::vector<unsigned char> nonce(32, 0x00);
3519 if (commitpos != -1 && IsWitnessEnabled(pindexPrev, consensusParams) && block.vtx[0].wit.IsEmpty()) {
3520 block.vtx[0].wit.vtxinwit.resize(1);
3521 block.vtx[0].wit.vtxinwit[0].scriptWitness.stack.resize(1);
3522 block.vtx[0].wit.vtxinwit[0].scriptWitness.stack[0] = nonce;
3526 std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
3528 std::vector<unsigned char> commitment;
3529 int commitpos = GetWitnessCommitmentIndex(block);
3530 bool fHaveWitness = false;
3531 for (size_t t = 1; t < block.vtx.size(); t++) {
3532 if (!block.vtx[t].wit.IsNull()) {
3533 fHaveWitness = true;
3534 break;
3537 std::vector<unsigned char> ret(32, 0x00);
3538 if (fHaveWitness && IsWitnessEnabled(pindexPrev, consensusParams)) {
3539 if (commitpos == -1) {
3540 uint256 witnessroot = BlockWitnessMerkleRoot(block, NULL);
3541 CHash256().Write(witnessroot.begin(), 32).Write(&ret[0], 32).Finalize(witnessroot.begin());
3542 CTxOut out;
3543 out.nValue = 0;
3544 out.scriptPubKey.resize(38);
3545 out.scriptPubKey[0] = OP_RETURN;
3546 out.scriptPubKey[1] = 0x24;
3547 out.scriptPubKey[2] = 0xaa;
3548 out.scriptPubKey[3] = 0x21;
3549 out.scriptPubKey[4] = 0xa9;
3550 out.scriptPubKey[5] = 0xed;
3551 memcpy(&out.scriptPubKey[6], witnessroot.begin(), 32);
3552 commitment = std::vector<unsigned char>(out.scriptPubKey.begin(), out.scriptPubKey.end());
3553 const_cast<std::vector<CTxOut>*>(&block.vtx[0].vout)->push_back(out);
3554 block.vtx[0].UpdateHash();
3557 UpdateUncommittedBlockStructures(block, pindexPrev, consensusParams);
3558 return commitment;
3561 bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
3563 const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
3564 // Check proof of work
3565 if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
3566 return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");
3568 // Check timestamp against prev
3569 if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
3570 return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
3572 // Check timestamp
3573 if (block.GetBlockTime() > nAdjustedTime + 2 * 60 * 60)
3574 return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
3576 // Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded:
3577 // check for version 2, 3 and 4 upgrades
3578 if((block.nVersion < 2 && nHeight >= consensusParams.BIP34Height) ||
3579 (block.nVersion < 3 && nHeight >= consensusParams.BIP66Height) ||
3580 (block.nVersion < 4 && nHeight >= consensusParams.BIP65Height))
3581 return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", block.nVersion),
3582 strprintf("rejected nVersion=0x%08x block", block.nVersion));
3584 return true;
3587 bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
3589 const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
3591 // Start enforcing BIP113 (Median Time Past) using versionbits logic.
3592 int nLockTimeFlags = 0;
3593 if (VersionBitsState(pindexPrev, consensusParams, Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
3594 nLockTimeFlags |= LOCKTIME_MEDIAN_TIME_PAST;
3597 int64_t nLockTimeCutoff = (nLockTimeFlags & LOCKTIME_MEDIAN_TIME_PAST)
3598 ? pindexPrev->GetMedianTimePast()
3599 : block.GetBlockTime();
3601 // Check that all transactions are finalized
3602 for (const auto& tx : block.vtx) {
3603 if (!IsFinalTx(tx, nHeight, nLockTimeCutoff)) {
3604 return state.DoS(10, false, REJECT_INVALID, "bad-txns-nonfinal", false, "non-final transaction");
3608 // Enforce rule that the coinbase starts with serialized block height
3609 if (nHeight >= consensusParams.BIP34Height)
3611 CScript expect = CScript() << nHeight;
3612 if (block.vtx[0].vin[0].scriptSig.size() < expect.size() ||
3613 !std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin())) {
3614 return state.DoS(100, false, REJECT_INVALID, "bad-cb-height", false, "block height mismatch in coinbase");
3618 // Validation for witness commitments.
3619 // * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the
3620 // coinbase (where 0x0000....0000 is used instead).
3621 // * The coinbase scriptWitness is a stack of a single 32-byte vector, containing a witness nonce (unconstrained).
3622 // * We build a merkle tree with all those witness hashes as leaves (similar to the hashMerkleRoot in the block header).
3623 // * There must be at least one output whose scriptPubKey is a single 36-byte push, the first 4 bytes of which are
3624 // {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness nonce). In case there are
3625 // multiple, the last one is used.
3626 bool fHaveWitness = false;
3627 if (VersionBitsState(pindexPrev, consensusParams, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_ACTIVE) {
3628 int commitpos = GetWitnessCommitmentIndex(block);
3629 if (commitpos != -1) {
3630 bool malleated = false;
3631 uint256 hashWitness = BlockWitnessMerkleRoot(block, &malleated);
3632 // The malleation check is ignored; as the transaction tree itself
3633 // already does not permit it, it is impossible to trigger in the
3634 // witness tree.
3635 if (block.vtx[0].wit.vtxinwit.size() != 1 || block.vtx[0].wit.vtxinwit[0].scriptWitness.stack.size() != 1 || block.vtx[0].wit.vtxinwit[0].scriptWitness.stack[0].size() != 32) {
3636 return state.DoS(100, false, REJECT_INVALID, "bad-witness-nonce-size", true, strprintf("%s : invalid witness nonce size", __func__));
3638 CHash256().Write(hashWitness.begin(), 32).Write(&block.vtx[0].wit.vtxinwit[0].scriptWitness.stack[0][0], 32).Finalize(hashWitness.begin());
3639 if (memcmp(hashWitness.begin(), &block.vtx[0].vout[commitpos].scriptPubKey[6], 32)) {
3640 return state.DoS(100, false, REJECT_INVALID, "bad-witness-merkle-match", true, strprintf("%s : witness merkle commitment mismatch", __func__));
3642 fHaveWitness = true;
3646 // No witness data is allowed in blocks that don't commit to witness data, as this would otherwise leave room for spam
3647 if (!fHaveWitness) {
3648 for (size_t i = 0; i < block.vtx.size(); i++) {
3649 if (!block.vtx[i].wit.IsNull()) {
3650 return state.DoS(100, false, REJECT_INVALID, "unexpected-witness", true, strprintf("%s : unexpected witness data found", __func__));
3655 // After the coinbase witness nonce and commitment are verified,
3656 // we can check if the block weight passes (before we've checked the
3657 // coinbase witness, it would be possible for the weight to be too
3658 // large by filling up the coinbase witness, which doesn't change
3659 // the block hash, so we couldn't mark the block as permanently
3660 // failed).
3661 if (GetBlockWeight(block) > MAX_BLOCK_WEIGHT) {
3662 return state.DoS(100, false, REJECT_INVALID, "bad-blk-weight", false, strprintf("%s : weight limit failed", __func__));
3665 return true;
3668 static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex=NULL)
3670 AssertLockHeld(cs_main);
3671 // Check for duplicate
3672 uint256 hash = block.GetHash();
3673 BlockMap::iterator miSelf = mapBlockIndex.find(hash);
3674 CBlockIndex *pindex = NULL;
3675 if (hash != chainparams.GetConsensus().hashGenesisBlock) {
3677 if (miSelf != mapBlockIndex.end()) {
3678 // Block header is already known.
3679 pindex = miSelf->second;
3680 if (ppindex)
3681 *ppindex = pindex;
3682 if (pindex->nStatus & BLOCK_FAILED_MASK)
3683 return state.Invalid(error("%s: block %s is marked invalid", __func__, hash.ToString()), 0, "duplicate");
3684 return true;
3687 if (!CheckBlockHeader(block, state, chainparams.GetConsensus()))
3688 return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
3690 // Get prev block index
3691 CBlockIndex* pindexPrev = NULL;
3692 BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
3693 if (mi == mapBlockIndex.end())
3694 return state.DoS(10, error("%s: prev block not found", __func__), 0, "bad-prevblk");
3695 pindexPrev = (*mi).second;
3696 if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
3697 return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
3699 assert(pindexPrev);
3700 if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, hash))
3701 return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
3703 if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev, GetAdjustedTime()))
3704 return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
3706 if (pindex == NULL)
3707 pindex = AddToBlockIndex(block);
3709 if (ppindex)
3710 *ppindex = pindex;
3712 CheckBlockIndex(chainparams.GetConsensus());
3714 return true;
3717 /** Store block on disk. If dbp is non-NULL, the file is known to already reside on disk */
3718 static bool AcceptBlock(const CBlock& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const CDiskBlockPos* dbp, bool* fNewBlock)
3720 if (fNewBlock) *fNewBlock = false;
3721 AssertLockHeld(cs_main);
3723 CBlockIndex *pindexDummy = NULL;
3724 CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
3726 if (!AcceptBlockHeader(block, state, chainparams, &pindex))
3727 return false;
3729 // Try to process all requested blocks that we don't have, but only
3730 // process an unrequested block if it's new and has enough work to
3731 // advance our tip, and isn't too many blocks ahead.
3732 bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA;
3733 bool fHasMoreWork = (chainActive.Tip() ? pindex->nChainWork > chainActive.Tip()->nChainWork : true);
3734 // Blocks that are too out-of-order needlessly limit the effectiveness of
3735 // pruning, because pruning will not delete block files that contain any
3736 // blocks which are too close in height to the tip. Apply this test
3737 // regardless of whether pruning is enabled; it should generally be safe to
3738 // not process unrequested blocks.
3739 bool fTooFarAhead = (pindex->nHeight > int(chainActive.Height() + MIN_BLOCKS_TO_KEEP));
3741 // TODO: Decouple this function from the block download logic by removing fRequested
3742 // This requires some new chain datastructure to efficiently look up if a
3743 // block is in a chain leading to a candidate for best tip, despite not
3744 // being such a candidate itself.
3746 // TODO: deal better with return value and error conditions for duplicate
3747 // and unrequested blocks.
3748 if (fAlreadyHave) return true;
3749 if (!fRequested) { // If we didn't ask for it:
3750 if (pindex->nTx != 0) return true; // This is a previously-processed block that was pruned
3751 if (!fHasMoreWork) return true; // Don't process less-work chains
3752 if (fTooFarAhead) return true; // Block height is too high
3754 if (fNewBlock) *fNewBlock = true;
3756 if (!CheckBlock(block, state, chainparams.GetConsensus(), GetAdjustedTime()) ||
3757 !ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindex->pprev)) {
3758 if (state.IsInvalid() && !state.CorruptionPossible()) {
3759 pindex->nStatus |= BLOCK_FAILED_VALID;
3760 setDirtyBlockIndex.insert(pindex);
3762 return error("%s: %s", __func__, FormatStateMessage(state));
3765 int nHeight = pindex->nHeight;
3767 // Write block to history file
3768 try {
3769 unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
3770 CDiskBlockPos blockPos;
3771 if (dbp != NULL)
3772 blockPos = *dbp;
3773 if (!FindBlockPos(state, blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != NULL))
3774 return error("AcceptBlock(): FindBlockPos failed");
3775 if (dbp == NULL)
3776 if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
3777 AbortNode(state, "Failed to write block");
3778 if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
3779 return error("AcceptBlock(): ReceivedBlockTransactions failed");
3780 } catch (const std::runtime_error& e) {
3781 return AbortNode(state, std::string("System error: ") + e.what());
3784 if (fCheckForPruning)
3785 FlushStateToDisk(state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
3787 return true;
3790 bool ProcessNewBlock(const CChainParams& chainparams, const CBlock* pblock, bool fForceProcessing, const CDiskBlockPos* dbp, bool *fNewBlock)
3793 LOCK(cs_main);
3795 // Store to disk
3796 CBlockIndex *pindex = NULL;
3797 if (fNewBlock) *fNewBlock = false;
3798 CValidationState state;
3799 bool ret = AcceptBlock(*pblock, state, chainparams, &pindex, fForceProcessing, dbp, fNewBlock);
3800 CheckBlockIndex(chainparams.GetConsensus());
3801 if (!ret) {
3802 GetMainSignals().BlockChecked(*pblock, state);
3803 return error("%s: AcceptBlock FAILED", __func__);
3807 NotifyHeaderTip();
3809 CValidationState state; // Only used to report errors, not invalidity - ignore it
3810 if (!ActivateBestChain(state, chainparams, pblock))
3811 return error("%s: ActivateBestChain failed", __func__);
3813 return true;
3816 bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
3818 AssertLockHeld(cs_main);
3819 assert(pindexPrev && pindexPrev == chainActive.Tip());
3820 if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, block.GetHash()))
3821 return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
3823 CCoinsViewCache viewNew(pcoinsTip);
3824 CBlockIndex indexDummy(block);
3825 indexDummy.pprev = pindexPrev;
3826 indexDummy.nHeight = pindexPrev->nHeight + 1;
3828 // NOTE: CheckBlockHeader is called by CheckBlock
3829 if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev, GetAdjustedTime()))
3830 return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, FormatStateMessage(state));
3831 if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot))
3832 return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
3833 if (!ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindexPrev))
3834 return error("%s: Consensus::ContextualCheckBlock: %s", __func__, FormatStateMessage(state));
3835 if (!ConnectBlock(block, state, &indexDummy, viewNew, chainparams, true))
3836 return false;
3837 assert(state.IsValid());
3839 return true;
3843 * BLOCK PRUNING CODE
3846 /* Calculate the amount of disk space the block & undo files currently use */
3847 uint64_t CalculateCurrentUsage()
3849 uint64_t retval = 0;
3850 BOOST_FOREACH(const CBlockFileInfo &file, vinfoBlockFile) {
3851 retval += file.nSize + file.nUndoSize;
3853 return retval;
3856 /* Prune a block file (modify associated database entries)*/
3857 void PruneOneBlockFile(const int fileNumber)
3859 for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) {
3860 CBlockIndex* pindex = it->second;
3861 if (pindex->nFile == fileNumber) {
3862 pindex->nStatus &= ~BLOCK_HAVE_DATA;
3863 pindex->nStatus &= ~BLOCK_HAVE_UNDO;
3864 pindex->nFile = 0;
3865 pindex->nDataPos = 0;
3866 pindex->nUndoPos = 0;
3867 setDirtyBlockIndex.insert(pindex);
3869 // Prune from mapBlocksUnlinked -- any block we prune would have
3870 // to be downloaded again in order to consider its chain, at which
3871 // point it would be considered as a candidate for
3872 // mapBlocksUnlinked or setBlockIndexCandidates.
3873 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex->pprev);
3874 while (range.first != range.second) {
3875 std::multimap<CBlockIndex *, CBlockIndex *>::iterator _it = range.first;
3876 range.first++;
3877 if (_it->second == pindex) {
3878 mapBlocksUnlinked.erase(_it);
3884 vinfoBlockFile[fileNumber].SetNull();
3885 setDirtyFileInfo.insert(fileNumber);
3889 void UnlinkPrunedFiles(std::set<int>& setFilesToPrune)
3891 for (set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
3892 CDiskBlockPos pos(*it, 0);
3893 boost::filesystem::remove(GetBlockPosFilename(pos, "blk"));
3894 boost::filesystem::remove(GetBlockPosFilename(pos, "rev"));
3895 LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
3899 /* Calculate the block/rev files that should be deleted to remain under target*/
3900 void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight)
3902 LOCK2(cs_main, cs_LastBlockFile);
3903 if (chainActive.Tip() == NULL || nPruneTarget == 0) {
3904 return;
3906 if ((uint64_t)chainActive.Tip()->nHeight <= nPruneAfterHeight) {
3907 return;
3910 unsigned int nLastBlockWeCanPrune = chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP;
3911 uint64_t nCurrentUsage = CalculateCurrentUsage();
3912 // We don't check to prune until after we've allocated new space for files
3913 // So we should leave a buffer under our target to account for another allocation
3914 // before the next pruning.
3915 uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
3916 uint64_t nBytesToPrune;
3917 int count=0;
3919 if (nCurrentUsage + nBuffer >= nPruneTarget) {
3920 for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
3921 nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize;
3923 if (vinfoBlockFile[fileNumber].nSize == 0)
3924 continue;
3926 if (nCurrentUsage + nBuffer < nPruneTarget) // are we below our target?
3927 break;
3929 // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
3930 if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
3931 continue;
3933 PruneOneBlockFile(fileNumber);
3934 // Queue up the files for removal
3935 setFilesToPrune.insert(fileNumber);
3936 nCurrentUsage -= nBytesToPrune;
3937 count++;
3941 LogPrint("prune", "Prune: target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n",
3942 nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
3943 ((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
3944 nLastBlockWeCanPrune, count);
3947 bool CheckDiskSpace(uint64_t nAdditionalBytes)
3949 uint64_t nFreeBytesAvailable = boost::filesystem::space(GetDataDir()).available;
3951 // Check for nMinDiskSpace bytes (currently 50MB)
3952 if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
3953 return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
3955 return true;
3958 FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
3960 if (pos.IsNull())
3961 return NULL;
3962 boost::filesystem::path path = GetBlockPosFilename(pos, prefix);
3963 boost::filesystem::create_directories(path.parent_path());
3964 FILE* file = fopen(path.string().c_str(), "rb+");
3965 if (!file && !fReadOnly)
3966 file = fopen(path.string().c_str(), "wb+");
3967 if (!file) {
3968 LogPrintf("Unable to open file %s\n", path.string());
3969 return NULL;
3971 if (pos.nPos) {
3972 if (fseek(file, pos.nPos, SEEK_SET)) {
3973 LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
3974 fclose(file);
3975 return NULL;
3978 return file;
3981 FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
3982 return OpenDiskFile(pos, "blk", fReadOnly);
3985 FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
3986 return OpenDiskFile(pos, "rev", fReadOnly);
3989 boost::filesystem::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
3991 return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
3994 CBlockIndex * InsertBlockIndex(uint256 hash)
3996 if (hash.IsNull())
3997 return NULL;
3999 // Return existing
4000 BlockMap::iterator mi = mapBlockIndex.find(hash);
4001 if (mi != mapBlockIndex.end())
4002 return (*mi).second;
4004 // Create new
4005 CBlockIndex* pindexNew = new CBlockIndex();
4006 if (!pindexNew)
4007 throw runtime_error(std::string(__func__) + ": new CBlockIndex failed");
4008 mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
4009 pindexNew->phashBlock = &((*mi).first);
4011 return pindexNew;
4014 bool static LoadBlockIndexDB(const CChainParams& chainparams)
4016 if (!pblocktree->LoadBlockIndexGuts(InsertBlockIndex))
4017 return false;
4019 boost::this_thread::interruption_point();
4021 // Calculate nChainWork
4022 vector<pair<int, CBlockIndex*> > vSortedByHeight;
4023 vSortedByHeight.reserve(mapBlockIndex.size());
4024 BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
4026 CBlockIndex* pindex = item.second;
4027 vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
4029 sort(vSortedByHeight.begin(), vSortedByHeight.end());
4030 BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
4032 CBlockIndex* pindex = item.second;
4033 pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
4034 // We can link the chain of blocks for which we've received transactions at some point.
4035 // Pruned nodes may have deleted the block.
4036 if (pindex->nTx > 0) {
4037 if (pindex->pprev) {
4038 if (pindex->pprev->nChainTx) {
4039 pindex->nChainTx = pindex->pprev->nChainTx + pindex->nTx;
4040 } else {
4041 pindex->nChainTx = 0;
4042 mapBlocksUnlinked.insert(std::make_pair(pindex->pprev, pindex));
4044 } else {
4045 pindex->nChainTx = pindex->nTx;
4048 if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->nChainTx || pindex->pprev == NULL))
4049 setBlockIndexCandidates.insert(pindex);
4050 if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
4051 pindexBestInvalid = pindex;
4052 if (pindex->pprev)
4053 pindex->BuildSkip();
4054 if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == NULL || CBlockIndexWorkComparator()(pindexBestHeader, pindex)))
4055 pindexBestHeader = pindex;
4058 // Load block file info
4059 pblocktree->ReadLastBlockFile(nLastBlockFile);
4060 vinfoBlockFile.resize(nLastBlockFile + 1);
4061 LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile);
4062 for (int nFile = 0; nFile <= nLastBlockFile; nFile++) {
4063 pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
4065 LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString());
4066 for (int nFile = nLastBlockFile + 1; true; nFile++) {
4067 CBlockFileInfo info;
4068 if (pblocktree->ReadBlockFileInfo(nFile, info)) {
4069 vinfoBlockFile.push_back(info);
4070 } else {
4071 break;
4075 // Check presence of blk files
4076 LogPrintf("Checking all blk files are present...\n");
4077 set<int> setBlkDataFiles;
4078 BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
4080 CBlockIndex* pindex = item.second;
4081 if (pindex->nStatus & BLOCK_HAVE_DATA) {
4082 setBlkDataFiles.insert(pindex->nFile);
4085 for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++)
4087 CDiskBlockPos pos(*it, 0);
4088 if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) {
4089 return false;
4093 // Check whether we have ever pruned block & undo files
4094 pblocktree->ReadFlag("prunedblockfiles", fHavePruned);
4095 if (fHavePruned)
4096 LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n");
4098 // Check whether we need to continue reindexing
4099 bool fReindexing = false;
4100 pblocktree->ReadReindexing(fReindexing);
4101 fReindex |= fReindexing;
4103 // Check whether we have a transaction index
4104 pblocktree->ReadFlag("txindex", fTxIndex);
4105 LogPrintf("%s: transaction index %s\n", __func__, fTxIndex ? "enabled" : "disabled");
4107 // Load pointer to end of best chain
4108 BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
4109 if (it == mapBlockIndex.end())
4110 return true;
4111 chainActive.SetTip(it->second);
4113 PruneBlockIndexCandidates();
4115 LogPrintf("%s: hashBestChain=%s height=%d date=%s progress=%f\n", __func__,
4116 chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
4117 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
4118 Checkpoints::GuessVerificationProgress(chainparams.Checkpoints(), chainActive.Tip()));
4120 return true;
4123 CVerifyDB::CVerifyDB()
4125 uiInterface.ShowProgress(_("Verifying blocks..."), 0);
4128 CVerifyDB::~CVerifyDB()
4130 uiInterface.ShowProgress("", 100);
4133 bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
4135 LOCK(cs_main);
4136 if (chainActive.Tip() == NULL || chainActive.Tip()->pprev == NULL)
4137 return true;
4139 // Verify blocks in the best chain
4140 if (nCheckDepth <= 0)
4141 nCheckDepth = 1000000000; // suffices until the year 19000
4142 if (nCheckDepth > chainActive.Height())
4143 nCheckDepth = chainActive.Height();
4144 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
4145 LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
4146 CCoinsViewCache coins(coinsview);
4147 CBlockIndex* pindexState = chainActive.Tip();
4148 CBlockIndex* pindexFailure = NULL;
4149 int nGoodTransactions = 0;
4150 CValidationState state;
4151 int reportDone = 0;
4152 LogPrintf("[0%%]...");
4153 for (CBlockIndex* pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev)
4155 boost::this_thread::interruption_point();
4156 int percentageDone = std::max(1, std::min(99, (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
4157 if (reportDone < percentageDone/10) {
4158 // report every 10% step
4159 LogPrintf("[%d%%]...", percentageDone);
4160 reportDone = percentageDone/10;
4162 uiInterface.ShowProgress(_("Verifying blocks..."), percentageDone);
4163 if (pindex->nHeight < chainActive.Height()-nCheckDepth)
4164 break;
4165 if (fPruneMode && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
4166 // If pruning, only go back as far as we have data.
4167 LogPrintf("VerifyDB(): block verification stopping at height %d (pruning, no data)\n", pindex->nHeight);
4168 break;
4170 CBlock block;
4171 // check level 0: read from disk
4172 if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
4173 return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4174 // check level 1: verify block validity
4175 if (nCheckLevel >= 1 && !CheckBlock(block, state, chainparams.GetConsensus()))
4176 return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
4177 pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
4178 // check level 2: verify undo validity
4179 if (nCheckLevel >= 2 && pindex) {
4180 CBlockUndo undo;
4181 CDiskBlockPos pos = pindex->GetUndoPos();
4182 if (!pos.IsNull()) {
4183 if (!UndoReadFromDisk(undo, pos, pindex->pprev->GetBlockHash()))
4184 return error("VerifyDB(): *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4187 // check level 3: check for inconsistencies during memory-only disconnect of tip blocks
4188 if (nCheckLevel >= 3 && pindex == pindexState && (coins.DynamicMemoryUsage() + pcoinsTip->DynamicMemoryUsage()) <= nCoinCacheUsage) {
4189 bool fClean = true;
4190 if (!DisconnectBlock(block, state, pindex, coins, &fClean))
4191 return error("VerifyDB(): *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4192 pindexState = pindex->pprev;
4193 if (!fClean) {
4194 nGoodTransactions = 0;
4195 pindexFailure = pindex;
4196 } else
4197 nGoodTransactions += block.vtx.size();
4199 if (ShutdownRequested())
4200 return true;
4202 if (pindexFailure)
4203 return error("VerifyDB(): *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainActive.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
4205 // check level 4: try reconnecting blocks
4206 if (nCheckLevel >= 4) {
4207 CBlockIndex *pindex = pindexState;
4208 while (pindex != chainActive.Tip()) {
4209 boost::this_thread::interruption_point();
4210 uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, 100 - (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * 50))));
4211 pindex = chainActive.Next(pindex);
4212 CBlock block;
4213 if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
4214 return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4215 if (!ConnectBlock(block, state, pindex, coins, chainparams))
4216 return error("VerifyDB(): *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4220 LogPrintf("[DONE].\n");
4221 LogPrintf("No coin database inconsistencies in last %i blocks (%i transactions)\n", chainActive.Height() - pindexState->nHeight, nGoodTransactions);
4223 return true;
4226 bool RewindBlockIndex(const CChainParams& params)
4228 LOCK(cs_main);
4230 int nHeight = 1;
4231 while (nHeight <= chainActive.Height()) {
4232 if (IsWitnessEnabled(chainActive[nHeight - 1], params.GetConsensus()) && !(chainActive[nHeight]->nStatus & BLOCK_OPT_WITNESS)) {
4233 break;
4235 nHeight++;
4238 // nHeight is now the height of the first insufficiently-validated block, or tipheight + 1
4239 CValidationState state;
4240 CBlockIndex* pindex = chainActive.Tip();
4241 while (chainActive.Height() >= nHeight) {
4242 if (fPruneMode && !(chainActive.Tip()->nStatus & BLOCK_HAVE_DATA)) {
4243 // If pruning, don't try rewinding past the HAVE_DATA point;
4244 // since older blocks can't be served anyway, there's
4245 // no need to walk further, and trying to DisconnectTip()
4246 // will fail (and require a needless reindex/redownload
4247 // of the blockchain).
4248 break;
4250 if (!DisconnectTip(state, params, true)) {
4251 return error("RewindBlockIndex: unable to disconnect block at height %i", pindex->nHeight);
4253 // Occasionally flush state to disk.
4254 if (!FlushStateToDisk(state, FLUSH_STATE_PERIODIC))
4255 return false;
4258 // Reduce validity flag and have-data flags.
4259 // We do this after actual disconnecting, otherwise we'll end up writing the lack of data
4260 // to disk before writing the chainstate, resulting in a failure to continue if interrupted.
4261 for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
4262 CBlockIndex* pindexIter = it->second;
4264 // Note: If we encounter an insufficiently validated block that
4265 // is on chainActive, it must be because we are a pruning node, and
4266 // this block or some successor doesn't HAVE_DATA, so we were unable to
4267 // rewind all the way. Blocks remaining on chainActive at this point
4268 // must not have their validity reduced.
4269 if (IsWitnessEnabled(pindexIter->pprev, params.GetConsensus()) && !(pindexIter->nStatus & BLOCK_OPT_WITNESS) && !chainActive.Contains(pindexIter)) {
4270 // Reduce validity
4271 pindexIter->nStatus = std::min<unsigned int>(pindexIter->nStatus & BLOCK_VALID_MASK, BLOCK_VALID_TREE) | (pindexIter->nStatus & ~BLOCK_VALID_MASK);
4272 // Remove have-data flags.
4273 pindexIter->nStatus &= ~(BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO);
4274 // Remove storage location.
4275 pindexIter->nFile = 0;
4276 pindexIter->nDataPos = 0;
4277 pindexIter->nUndoPos = 0;
4278 // Remove various other things
4279 pindexIter->nTx = 0;
4280 pindexIter->nChainTx = 0;
4281 pindexIter->nSequenceId = 0;
4282 // Make sure it gets written.
4283 setDirtyBlockIndex.insert(pindexIter);
4284 // Update indexes
4285 setBlockIndexCandidates.erase(pindexIter);
4286 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> ret = mapBlocksUnlinked.equal_range(pindexIter->pprev);
4287 while (ret.first != ret.second) {
4288 if (ret.first->second == pindexIter) {
4289 mapBlocksUnlinked.erase(ret.first++);
4290 } else {
4291 ++ret.first;
4294 } else if (pindexIter->IsValid(BLOCK_VALID_TRANSACTIONS) && pindexIter->nChainTx) {
4295 setBlockIndexCandidates.insert(pindexIter);
4299 PruneBlockIndexCandidates();
4301 CheckBlockIndex(params.GetConsensus());
4303 if (!FlushStateToDisk(state, FLUSH_STATE_ALWAYS)) {
4304 return false;
4307 return true;
4310 // May NOT be used after any connections are up as much
4311 // of the peer-processing logic assumes a consistent
4312 // block index state
4313 void UnloadBlockIndex()
4315 LOCK(cs_main);
4316 setBlockIndexCandidates.clear();
4317 chainActive.SetTip(NULL);
4318 pindexBestInvalid = NULL;
4319 pindexBestHeader = NULL;
4320 mempool.clear();
4321 mapOrphanTransactions.clear();
4322 mapOrphanTransactionsByPrev.clear();
4323 mapBlocksUnlinked.clear();
4324 vinfoBlockFile.clear();
4325 nLastBlockFile = 0;
4326 nBlockSequenceId = 1;
4327 setDirtyBlockIndex.clear();
4328 setDirtyFileInfo.clear();
4329 versionbitscache.Clear();
4330 for (int b = 0; b < VERSIONBITS_NUM_BITS; b++) {
4331 warningcache[b].clear();
4334 BOOST_FOREACH(BlockMap::value_type& entry, mapBlockIndex) {
4335 delete entry.second;
4337 mapBlockIndex.clear();
4338 fHavePruned = false;
4341 bool LoadBlockIndex(const CChainParams& chainparams)
4343 // Load block index from databases
4344 if (!fReindex && !LoadBlockIndexDB(chainparams))
4345 return false;
4346 return true;
4349 bool InitBlockIndex(const CChainParams& chainparams)
4351 LOCK(cs_main);
4353 // Check whether we're already initialized
4354 if (chainActive.Genesis() != NULL)
4355 return true;
4357 // Use the provided setting for -txindex in the new database
4358 fTxIndex = GetBoolArg("-txindex", DEFAULT_TXINDEX);
4359 pblocktree->WriteFlag("txindex", fTxIndex);
4360 LogPrintf("Initializing databases...\n");
4362 // Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
4363 if (!fReindex) {
4364 try {
4365 CBlock &block = const_cast<CBlock&>(chainparams.GenesisBlock());
4366 // Start new block file
4367 unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
4368 CDiskBlockPos blockPos;
4369 CValidationState state;
4370 if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.GetBlockTime()))
4371 return error("LoadBlockIndex(): FindBlockPos failed");
4372 if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
4373 return error("LoadBlockIndex(): writing genesis block to disk failed");
4374 CBlockIndex *pindex = AddToBlockIndex(block);
4375 if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
4376 return error("LoadBlockIndex(): genesis block not accepted");
4377 // Force a chainstate write so that when we VerifyDB in a moment, it doesn't check stale data
4378 return FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
4379 } catch (const std::runtime_error& e) {
4380 return error("LoadBlockIndex(): failed to initialize block database: %s", e.what());
4384 return true;
4387 bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp)
4389 // Map of disk positions for blocks with unknown parent (only used for reindex)
4390 static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
4391 int64_t nStart = GetTimeMillis();
4393 int nLoaded = 0;
4394 try {
4395 // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
4396 CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE+8, SER_DISK, CLIENT_VERSION);
4397 uint64_t nRewind = blkdat.GetPos();
4398 while (!blkdat.eof()) {
4399 boost::this_thread::interruption_point();
4401 blkdat.SetPos(nRewind);
4402 nRewind++; // start one byte further next time, in case of failure
4403 blkdat.SetLimit(); // remove former limit
4404 unsigned int nSize = 0;
4405 try {
4406 // locate a header
4407 unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
4408 blkdat.FindByte(chainparams.MessageStart()[0]);
4409 nRewind = blkdat.GetPos()+1;
4410 blkdat >> FLATDATA(buf);
4411 if (memcmp(buf, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE))
4412 continue;
4413 // read size
4414 blkdat >> nSize;
4415 if (nSize < 80 || nSize > MAX_BLOCK_SERIALIZED_SIZE)
4416 continue;
4417 } catch (const std::exception&) {
4418 // no valid block header found; don't complain
4419 break;
4421 try {
4422 // read block
4423 uint64_t nBlockPos = blkdat.GetPos();
4424 if (dbp)
4425 dbp->nPos = nBlockPos;
4426 blkdat.SetLimit(nBlockPos + nSize);
4427 blkdat.SetPos(nBlockPos);
4428 CBlock block;
4429 blkdat >> block;
4430 nRewind = blkdat.GetPos();
4432 // detect out of order blocks, and store them for later
4433 uint256 hash = block.GetHash();
4434 if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex.find(block.hashPrevBlock) == mapBlockIndex.end()) {
4435 LogPrint("reindex", "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
4436 block.hashPrevBlock.ToString());
4437 if (dbp)
4438 mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
4439 continue;
4442 // process in case the block isn't known yet
4443 if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
4444 LOCK(cs_main);
4445 CValidationState state;
4446 if (AcceptBlock(block, state, chainparams, NULL, true, dbp, NULL))
4447 nLoaded++;
4448 if (state.IsError())
4449 break;
4450 } else if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex[hash]->nHeight % 1000 == 0) {
4451 LogPrint("reindex", "Block Import: already had block %s at height %d\n", hash.ToString(), mapBlockIndex[hash]->nHeight);
4454 // Activate the genesis block so normal node progress can continue
4455 if (hash == chainparams.GetConsensus().hashGenesisBlock) {
4456 CValidationState state;
4457 if (!ActivateBestChain(state, chainparams)) {
4458 break;
4462 NotifyHeaderTip();
4464 // Recursively process earlier encountered successors of this block
4465 deque<uint256> queue;
4466 queue.push_back(hash);
4467 while (!queue.empty()) {
4468 uint256 head = queue.front();
4469 queue.pop_front();
4470 std::pair<std::multimap<uint256, CDiskBlockPos>::iterator, std::multimap<uint256, CDiskBlockPos>::iterator> range = mapBlocksUnknownParent.equal_range(head);
4471 while (range.first != range.second) {
4472 std::multimap<uint256, CDiskBlockPos>::iterator it = range.first;
4473 if (ReadBlockFromDisk(block, it->second, chainparams.GetConsensus()))
4475 LogPrint("reindex", "%s: Processing out of order child %s of %s\n", __func__, block.GetHash().ToString(),
4476 head.ToString());
4477 LOCK(cs_main);
4478 CValidationState dummy;
4479 if (AcceptBlock(block, dummy, chainparams, NULL, true, &it->second, NULL))
4481 nLoaded++;
4482 queue.push_back(block.GetHash());
4485 range.first++;
4486 mapBlocksUnknownParent.erase(it);
4487 NotifyHeaderTip();
4490 } catch (const std::exception& e) {
4491 LogPrintf("%s: Deserialize or I/O error - %s\n", __func__, e.what());
4494 } catch (const std::runtime_error& e) {
4495 AbortNode(std::string("System error: ") + e.what());
4497 if (nLoaded > 0)
4498 LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded, GetTimeMillis() - nStart);
4499 return nLoaded > 0;
4502 void static CheckBlockIndex(const Consensus::Params& consensusParams)
4504 if (!fCheckBlockIndex) {
4505 return;
4508 LOCK(cs_main);
4510 // During a reindex, we read the genesis block and call CheckBlockIndex before ActivateBestChain,
4511 // so we have the genesis block in mapBlockIndex but no active chain. (A few of the tests when
4512 // iterating the block tree require that chainActive has been initialized.)
4513 if (chainActive.Height() < 0) {
4514 assert(mapBlockIndex.size() <= 1);
4515 return;
4518 // Build forward-pointing map of the entire block tree.
4519 std::multimap<CBlockIndex*,CBlockIndex*> forward;
4520 for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
4521 forward.insert(std::make_pair(it->second->pprev, it->second));
4524 assert(forward.size() == mapBlockIndex.size());
4526 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeGenesis = forward.equal_range(NULL);
4527 CBlockIndex *pindex = rangeGenesis.first->second;
4528 rangeGenesis.first++;
4529 assert(rangeGenesis.first == rangeGenesis.second); // There is only one index entry with parent NULL.
4531 // Iterate over the entire block tree, using depth-first search.
4532 // Along the way, remember whether there are blocks on the path from genesis
4533 // block being explored which are the first to have certain properties.
4534 size_t nNodes = 0;
4535 int nHeight = 0;
4536 CBlockIndex* pindexFirstInvalid = NULL; // Oldest ancestor of pindex which is invalid.
4537 CBlockIndex* pindexFirstMissing = NULL; // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA.
4538 CBlockIndex* pindexFirstNeverProcessed = NULL; // Oldest ancestor of pindex for which nTx == 0.
4539 CBlockIndex* pindexFirstNotTreeValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
4540 CBlockIndex* pindexFirstNotTransactionsValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not).
4541 CBlockIndex* pindexFirstNotChainValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not).
4542 CBlockIndex* pindexFirstNotScriptsValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not).
4543 while (pindex != NULL) {
4544 nNodes++;
4545 if (pindexFirstInvalid == NULL && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
4546 if (pindexFirstMissing == NULL && !(pindex->nStatus & BLOCK_HAVE_DATA)) pindexFirstMissing = pindex;
4547 if (pindexFirstNeverProcessed == NULL && pindex->nTx == 0) pindexFirstNeverProcessed = pindex;
4548 if (pindex->pprev != NULL && pindexFirstNotTreeValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TREE) pindexFirstNotTreeValid = pindex;
4549 if (pindex->pprev != NULL && pindexFirstNotTransactionsValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TRANSACTIONS) pindexFirstNotTransactionsValid = pindex;
4550 if (pindex->pprev != NULL && pindexFirstNotChainValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_CHAIN) pindexFirstNotChainValid = pindex;
4551 if (pindex->pprev != NULL && pindexFirstNotScriptsValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS) pindexFirstNotScriptsValid = pindex;
4553 // Begin: actual consistency checks.
4554 if (pindex->pprev == NULL) {
4555 // Genesis block checks.
4556 assert(pindex->GetBlockHash() == consensusParams.hashGenesisBlock); // Genesis block's hash must match.
4557 assert(pindex == chainActive.Genesis()); // The current active chain's genesis block must be this block.
4559 if (pindex->nChainTx == 0) assert(pindex->nSequenceId <= 0); // nSequenceId can't be set positive for blocks that aren't linked (negative is used for preciousblock)
4560 // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred).
4561 // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
4562 if (!fHavePruned) {
4563 // If we've never pruned, then HAVE_DATA should be equivalent to nTx > 0
4564 assert(!(pindex->nStatus & BLOCK_HAVE_DATA) == (pindex->nTx == 0));
4565 assert(pindexFirstMissing == pindexFirstNeverProcessed);
4566 } else {
4567 // If we have pruned, then we can only say that HAVE_DATA implies nTx > 0
4568 if (pindex->nStatus & BLOCK_HAVE_DATA) assert(pindex->nTx > 0);
4570 if (pindex->nStatus & BLOCK_HAVE_UNDO) assert(pindex->nStatus & BLOCK_HAVE_DATA);
4571 assert(((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS) == (pindex->nTx > 0)); // This is pruning-independent.
4572 // All parents having had data (at some point) is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to nChainTx being set.
4573 assert((pindexFirstNeverProcessed != NULL) == (pindex->nChainTx == 0)); // nChainTx != 0 is used to signal that all parent blocks have been processed (but may have been pruned).
4574 assert((pindexFirstNotTransactionsValid != NULL) == (pindex->nChainTx == 0));
4575 assert(pindex->nHeight == nHeight); // nHeight must be consistent.
4576 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.
4577 assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); // The pskip pointer must point back for all but the first 2 blocks.
4578 assert(pindexFirstNotTreeValid == NULL); // All mapBlockIndex entries must at least be TREE valid
4579 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TREE) assert(pindexFirstNotTreeValid == NULL); // TREE valid implies all parents are TREE valid
4580 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_CHAIN) assert(pindexFirstNotChainValid == NULL); // CHAIN valid implies all parents are CHAIN valid
4581 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_SCRIPTS) assert(pindexFirstNotScriptsValid == NULL); // SCRIPTS valid implies all parents are SCRIPTS valid
4582 if (pindexFirstInvalid == NULL) {
4583 // Checks for not-invalid blocks.
4584 assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
4586 if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && pindexFirstNeverProcessed == NULL) {
4587 if (pindexFirstInvalid == NULL) {
4588 // If this block sorts at least as good as the current tip and
4589 // is valid and we have all data for its parents, it must be in
4590 // setBlockIndexCandidates. chainActive.Tip() must also be there
4591 // even if some data has been pruned.
4592 if (pindexFirstMissing == NULL || pindex == chainActive.Tip()) {
4593 assert(setBlockIndexCandidates.count(pindex));
4595 // If some parent is missing, then it could be that this block was in
4596 // setBlockIndexCandidates but had to be removed because of the missing data.
4597 // In this case it must be in mapBlocksUnlinked -- see test below.
4599 } else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates.
4600 assert(setBlockIndexCandidates.count(pindex) == 0);
4602 // Check whether this block is in mapBlocksUnlinked.
4603 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked = mapBlocksUnlinked.equal_range(pindex->pprev);
4604 bool foundInUnlinked = false;
4605 while (rangeUnlinked.first != rangeUnlinked.second) {
4606 assert(rangeUnlinked.first->first == pindex->pprev);
4607 if (rangeUnlinked.first->second == pindex) {
4608 foundInUnlinked = true;
4609 break;
4611 rangeUnlinked.first++;
4613 if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed != NULL && pindexFirstInvalid == NULL) {
4614 // If this block has block data available, some parent was never received, and has no invalid parents, it must be in mapBlocksUnlinked.
4615 assert(foundInUnlinked);
4617 if (!(pindex->nStatus & BLOCK_HAVE_DATA)) assert(!foundInUnlinked); // Can't be in mapBlocksUnlinked if we don't HAVE_DATA
4618 if (pindexFirstMissing == NULL) assert(!foundInUnlinked); // We aren't missing data for any parent -- cannot be in mapBlocksUnlinked.
4619 if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed == NULL && pindexFirstMissing != NULL) {
4620 // We HAVE_DATA for this block, have received data for all parents at some point, but we're currently missing data for some parent.
4621 assert(fHavePruned); // We must have pruned.
4622 // This block may have entered mapBlocksUnlinked if:
4623 // - it has a descendant that at some point had more work than the
4624 // tip, and
4625 // - we tried switching to that descendant but were missing
4626 // data for some intermediate block between chainActive and the
4627 // tip.
4628 // So if this block is itself better than chainActive.Tip() and it wasn't in
4629 // setBlockIndexCandidates, then it must be in mapBlocksUnlinked.
4630 if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && setBlockIndexCandidates.count(pindex) == 0) {
4631 if (pindexFirstInvalid == NULL) {
4632 assert(foundInUnlinked);
4636 // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow
4637 // End: actual consistency checks.
4639 // Try descending into the first subnode.
4640 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
4641 if (range.first != range.second) {
4642 // A subnode was found.
4643 pindex = range.first->second;
4644 nHeight++;
4645 continue;
4647 // This is a leaf node.
4648 // Move upwards until we reach a node of which we have not yet visited the last child.
4649 while (pindex) {
4650 // We are going to either move to a parent or a sibling of pindex.
4651 // If pindex was the first with a certain property, unset the corresponding variable.
4652 if (pindex == pindexFirstInvalid) pindexFirstInvalid = NULL;
4653 if (pindex == pindexFirstMissing) pindexFirstMissing = NULL;
4654 if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed = NULL;
4655 if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid = NULL;
4656 if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid = NULL;
4657 if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid = NULL;
4658 if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid = NULL;
4659 // Find our parent.
4660 CBlockIndex* pindexPar = pindex->pprev;
4661 // Find which child we just visited.
4662 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
4663 while (rangePar.first->second != pindex) {
4664 assert(rangePar.first != rangePar.second); // Our parent must have at least the node we're coming from as child.
4665 rangePar.first++;
4667 // Proceed to the next one.
4668 rangePar.first++;
4669 if (rangePar.first != rangePar.second) {
4670 // Move to the sibling.
4671 pindex = rangePar.first->second;
4672 break;
4673 } else {
4674 // Move up further.
4675 pindex = pindexPar;
4676 nHeight--;
4677 continue;
4682 // Check that we actually traversed the entire map.
4683 assert(nNodes == forward.size());
4686 std::string GetWarnings(const std::string& strFor)
4688 string strStatusBar;
4689 string strRPC;
4690 string strGUI;
4691 const string uiAlertSeperator = "<hr />";
4693 if (!CLIENT_VERSION_IS_RELEASE) {
4694 strStatusBar = "This is a pre-release test build - use at your own risk - do not use for mining or merchant applications";
4695 strGUI = _("This is a pre-release test build - use at your own risk - do not use for mining or merchant applications");
4698 if (GetBoolArg("-testsafemode", DEFAULT_TESTSAFEMODE))
4699 strStatusBar = strRPC = strGUI = "testsafemode enabled";
4701 // Misc warnings like out of disk space and clock is wrong
4702 if (strMiscWarning != "")
4704 strStatusBar = strMiscWarning;
4705 strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + strMiscWarning;
4708 if (fLargeWorkForkFound)
4710 strStatusBar = strRPC = "Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.";
4711 strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.");
4713 else if (fLargeWorkInvalidChainFound)
4715 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.";
4716 strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + _("Warning: We do not appear to fully agree with our peers! You may need to upgrade, or other nodes may need to upgrade.");
4719 if (strFor == "gui")
4720 return strGUI;
4721 else if (strFor == "statusbar")
4722 return strStatusBar;
4723 else if (strFor == "rpc")
4724 return strRPC;
4725 assert(!"GetWarnings(): invalid parameter");
4726 return "error";
4736 //////////////////////////////////////////////////////////////////////////////
4738 // blockchain -> download logic notification
4741 PeerLogicValidation::PeerLogicValidation(CConnman* connmanIn) : connman(connmanIn) {
4742 // Initialize global variables that cannot be constructed at startup.
4743 recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
4746 void PeerLogicValidation::UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) {
4747 const int nNewHeight = pindexNew->nHeight;
4748 connman->SetBestHeight(nNewHeight);
4750 if (!fInitialDownload) {
4751 // Find the hashes of all blocks that weren't previously in the best chain.
4752 std::vector<uint256> vHashes;
4753 const CBlockIndex *pindexToAnnounce = pindexNew;
4754 while (pindexToAnnounce != pindexFork) {
4755 vHashes.push_back(pindexToAnnounce->GetBlockHash());
4756 pindexToAnnounce = pindexToAnnounce->pprev;
4757 if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) {
4758 // Limit announcements in case of a huge reorganization.
4759 // Rely on the peer's synchronization mechanism in that case.
4760 break;
4763 // Relay inventory, but don't relay old inventory during initial block download.
4764 connman->ForEachNode([nNewHeight, &vHashes](CNode* pnode) {
4765 if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 0)) {
4766 BOOST_REVERSE_FOREACH(const uint256& hash, vHashes) {
4767 pnode->PushBlockHash(hash);
4773 nTimeBestReceived = GetTime();
4776 void PeerLogicValidation::BlockChecked(const CBlock& block, const CValidationState& state) {
4777 LOCK(cs_main);
4779 const uint256 hash(block.GetHash());
4780 std::map<uint256, std::pair<NodeId, bool>>::iterator it = mapBlockSource.find(hash);
4782 int nDoS = 0;
4783 if (state.IsInvalid(nDoS)) {
4784 if (it != mapBlockSource.end() && State(it->second.first)) {
4785 assert (state.GetRejectCode() < REJECT_INTERNAL); // Blocks are never rejected with internal reject codes
4786 CBlockReject reject = {(unsigned char)state.GetRejectCode(), state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), hash};
4787 State(it->second.first)->rejects.push_back(reject);
4788 if (nDoS > 0 && it->second.second)
4789 Misbehaving(it->second.first, nDoS);
4792 if (it != mapBlockSource.end())
4793 mapBlockSource.erase(it);
4796 //////////////////////////////////////////////////////////////////////////////
4798 // Messages
4802 bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
4804 switch (inv.type)
4806 case MSG_TX:
4807 case MSG_WITNESS_TX:
4809 assert(recentRejects);
4810 if (chainActive.Tip()->GetBlockHash() != hashRecentRejectsChainTip)
4812 // If the chain tip has changed previously rejected transactions
4813 // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
4814 // or a double-spend. Reset the rejects filter and give those
4815 // txs a second chance.
4816 hashRecentRejectsChainTip = chainActive.Tip()->GetBlockHash();
4817 recentRejects->reset();
4820 // Use pcoinsTip->HaveCoinsInCache as a quick approximation to exclude
4821 // requesting or processing some txs which have already been included in a block
4822 return recentRejects->contains(inv.hash) ||
4823 mempool.exists(inv.hash) ||
4824 mapOrphanTransactions.count(inv.hash) ||
4825 pcoinsTip->HaveCoinsInCache(inv.hash);
4827 case MSG_BLOCK:
4828 case MSG_WITNESS_BLOCK:
4829 return mapBlockIndex.count(inv.hash);
4831 // Don't know what it is, just say we already got one
4832 return true;
4835 static void RelayTransaction(const CTransaction& tx, CConnman& connman)
4837 CInv inv(MSG_TX, tx.GetHash());
4838 connman.ForEachNode([&inv](CNode* pnode)
4840 pnode->PushInventory(inv);
4844 static void RelayAddress(const CAddress& addr, bool fReachable, CConnman& connman)
4846 int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
4848 // Relay to a limited number of other nodes
4849 // Use deterministic randomness to send to the same nodes for 24 hours
4850 // at a time so the addrKnowns of the chosen nodes prevent repeats
4851 uint64_t hashAddr = addr.GetHash();
4852 std::multimap<uint64_t, CNode*> mapMix;
4853 const CSipHasher hasher = connman.GetDeterministicRandomizer(RANDOMIZER_ID_ADDRESS_RELAY).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
4854 FastRandomContext insecure_rand;
4856 auto sortfunc = [&mapMix, &hasher](CNode* pnode) {
4857 if (pnode->nVersion >= CADDR_TIME_VERSION) {
4858 uint64_t hashKey = CSipHasher(hasher).Write(pnode->id).Finalize();
4859 mapMix.emplace(hashKey, pnode);
4863 auto pushfunc = [&addr, &mapMix, &nRelayNodes, &insecure_rand] {
4864 for (auto mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
4865 mi->second->PushAddress(addr, insecure_rand);
4868 connman.ForEachNodeThen(std::move(sortfunc), std::move(pushfunc));
4871 void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParams, CConnman& connman)
4873 std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();
4874 unsigned int nMaxSendBufferSize = connman.GetSendBufferSize();
4876 vector<CInv> vNotFound;
4878 LOCK(cs_main);
4880 while (it != pfrom->vRecvGetData.end()) {
4881 // Don't bother if send buffer is too full to respond anyway
4882 if (pfrom->nSendSize >= nMaxSendBufferSize)
4883 break;
4885 const CInv &inv = *it;
4887 boost::this_thread::interruption_point();
4888 it++;
4890 if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK)
4892 bool send = false;
4893 BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
4894 if (mi != mapBlockIndex.end())
4896 if (chainActive.Contains(mi->second)) {
4897 send = true;
4898 } else {
4899 static const int nOneMonth = 30 * 24 * 60 * 60;
4900 // To prevent fingerprinting attacks, only send blocks outside of the active
4901 // chain if they are valid, and no more than a month older (both in time, and in
4902 // best equivalent proof of work) than the best header chain we know about.
4903 send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != NULL) &&
4904 (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() < nOneMonth) &&
4905 (GetBlockProofEquivalentTime(*pindexBestHeader, *mi->second, *pindexBestHeader, consensusParams) < nOneMonth);
4906 if (!send) {
4907 LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
4911 // disconnect node in case we have reached the outbound limit for serving historical blocks
4912 // never disconnect whitelisted nodes
4913 static const int nOneWeek = 7 * 24 * 60 * 60; // assume > 1 week = historical
4914 if (send && connman.OutboundTargetReached(true) && ( ((pindexBestHeader != NULL) && (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() > nOneWeek)) || inv.type == MSG_FILTERED_BLOCK) && !pfrom->fWhitelisted)
4916 LogPrint("net", "historical block serving limit reached, disconnect peer=%d\n", pfrom->GetId());
4918 //disconnect node
4919 pfrom->fDisconnect = true;
4920 send = false;
4922 // Pruned nodes may have deleted the block, so check whether
4923 // it's available before trying to send.
4924 if (send && (mi->second->nStatus & BLOCK_HAVE_DATA))
4926 // Send block from disk
4927 CBlock block;
4928 if (!ReadBlockFromDisk(block, (*mi).second, consensusParams))
4929 assert(!"cannot load block from disk");
4930 if (inv.type == MSG_BLOCK)
4931 connman.PushMessageWithFlag(pfrom, SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, block);
4932 else if (inv.type == MSG_WITNESS_BLOCK)
4933 connman.PushMessage(pfrom, NetMsgType::BLOCK, block);
4934 else if (inv.type == MSG_FILTERED_BLOCK)
4936 bool sendMerkleBlock = false;
4937 CMerkleBlock merkleBlock;
4939 LOCK(pfrom->cs_filter);
4940 if (pfrom->pfilter) {
4941 sendMerkleBlock = true;
4942 merkleBlock = CMerkleBlock(block, *pfrom->pfilter);
4945 if (sendMerkleBlock) {
4946 connman.PushMessage(pfrom, NetMsgType::MERKLEBLOCK, merkleBlock);
4947 // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
4948 // This avoids hurting performance by pointlessly requiring a round-trip
4949 // Note that there is currently no way for a node to request any single transactions we didn't send here -
4950 // they must either disconnect and retry or request the full block.
4951 // Thus, the protocol spec specified allows for us to provide duplicate txn here,
4952 // however we MUST always provide at least what the remote peer needs
4953 typedef std::pair<unsigned int, uint256> PairType;
4954 BOOST_FOREACH(PairType& pair, merkleBlock.vMatchedTxn)
4955 connman.PushMessageWithFlag(pfrom, SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::TX, block.vtx[pair.first]);
4957 // else
4958 // no response
4960 else if (inv.type == MSG_CMPCT_BLOCK)
4962 // If a peer is asking for old blocks, we're almost guaranteed
4963 // they wont have a useful mempool to match against a compact block,
4964 // and we don't feel like constructing the object for them, so
4965 // instead we respond with the full, non-compact block.
4966 bool fPeerWantsWitness = State(pfrom->GetId())->fWantsCmpctWitness;
4967 if (CanDirectFetch(consensusParams) && mi->second->nHeight >= chainActive.Height() - MAX_CMPCTBLOCK_DEPTH) {
4968 CBlockHeaderAndShortTxIDs cmpctblock(block, fPeerWantsWitness);
4969 connman.PushMessageWithFlag(pfrom, fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::CMPCTBLOCK, cmpctblock);
4970 } else
4971 connman.PushMessageWithFlag(pfrom, fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, block);
4974 // Trigger the peer node to send a getblocks request for the next batch of inventory
4975 if (inv.hash == pfrom->hashContinue)
4977 // Bypass PushInventory, this must send even if redundant,
4978 // and we want it right after the last block so they don't
4979 // wait for other stuff first.
4980 vector<CInv> vInv;
4981 vInv.push_back(CInv(MSG_BLOCK, chainActive.Tip()->GetBlockHash()));
4982 connman.PushMessage(pfrom, NetMsgType::INV, vInv);
4983 pfrom->hashContinue.SetNull();
4987 else if (inv.type == MSG_TX || inv.type == MSG_WITNESS_TX)
4989 // Send stream from relay memory
4990 bool push = false;
4991 auto mi = mapRelay.find(inv.hash);
4992 if (mi != mapRelay.end()) {
4993 connman.PushMessageWithFlag(pfrom, inv.type == MSG_TX ? SERIALIZE_TRANSACTION_NO_WITNESS : 0, NetMsgType::TX, *mi->second);
4994 push = true;
4995 } else if (pfrom->timeLastMempoolReq) {
4996 auto txinfo = mempool.info(inv.hash);
4997 // To protect privacy, do not answer getdata using the mempool when
4998 // that TX couldn't have been INVed in reply to a MEMPOOL request.
4999 if (txinfo.tx && txinfo.nTime <= pfrom->timeLastMempoolReq) {
5000 connman.PushMessageWithFlag(pfrom, inv.type == MSG_TX ? SERIALIZE_TRANSACTION_NO_WITNESS : 0, NetMsgType::TX, *txinfo.tx);
5001 push = true;
5004 if (!push) {
5005 vNotFound.push_back(inv);
5009 // Track requests for our stuff.
5010 GetMainSignals().Inventory(inv.hash);
5012 if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK)
5013 break;
5017 pfrom->vRecvGetData.erase(pfrom->vRecvGetData.begin(), it);
5019 if (!vNotFound.empty()) {
5020 // Let the peer know that we didn't find what it asked for, so it doesn't
5021 // have to wait around forever. Currently only SPV clients actually care
5022 // about this message: it's needed when they are recursively walking the
5023 // dependencies of relevant unconfirmed transactions. SPV clients want to
5024 // do that because they want to know about (and store and rebroadcast and
5025 // risk analyze) the dependencies of transactions relevant to them, without
5026 // having to download the entire memory pool.
5027 connman.PushMessage(pfrom, NetMsgType::NOTFOUND, vNotFound);
5031 uint32_t GetFetchFlags(CNode* pfrom, CBlockIndex* pprev, const Consensus::Params& chainparams) {
5032 uint32_t nFetchFlags = 0;
5033 if ((pfrom->GetLocalServices() & NODE_WITNESS) && State(pfrom->GetId())->fHaveWitness) {
5034 nFetchFlags |= MSG_WITNESS_FLAG;
5036 return nFetchFlags;
5039 bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams, CConnman& connman)
5041 unsigned int nMaxSendBufferSize = connman.GetSendBufferSize();
5043 LogPrint("net", "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->id);
5044 if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
5046 LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
5047 return true;
5051 if (!(pfrom->GetLocalServices() & NODE_BLOOM) &&
5052 (strCommand == NetMsgType::FILTERLOAD ||
5053 strCommand == NetMsgType::FILTERADD))
5055 if (pfrom->nVersion >= NO_BLOOM_VERSION) {
5056 LOCK(cs_main);
5057 Misbehaving(pfrom->GetId(), 100);
5058 return false;
5059 } else {
5060 pfrom->fDisconnect = true;
5061 return false;
5066 if (strCommand == NetMsgType::VERSION)
5068 // Feeler connections exist only to verify if address is online.
5069 if (pfrom->fFeeler) {
5070 assert(pfrom->fInbound == false);
5071 pfrom->fDisconnect = true;
5074 // Each connection can only send one version message
5075 if (pfrom->nVersion != 0)
5077 connman.PushMessageWithVersion(pfrom, INIT_PROTO_VERSION, NetMsgType::REJECT, strCommand, REJECT_DUPLICATE, string("Duplicate version message"));
5078 LOCK(cs_main);
5079 Misbehaving(pfrom->GetId(), 1);
5080 return false;
5083 int64_t nTime;
5084 CAddress addrMe;
5085 CAddress addrFrom;
5086 uint64_t nNonce = 1;
5087 uint64_t nServiceInt;
5088 vRecv >> pfrom->nVersion >> nServiceInt >> nTime >> addrMe;
5089 pfrom->nServices = ServiceFlags(nServiceInt);
5090 if (!pfrom->fInbound)
5092 connman.SetServices(pfrom->addr, pfrom->nServices);
5094 if (pfrom->nServicesExpected & ~pfrom->nServices)
5096 LogPrint("net", "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom->id, pfrom->nServices, pfrom->nServicesExpected);
5097 connman.PushMessageWithVersion(pfrom, INIT_PROTO_VERSION, NetMsgType::REJECT, strCommand, REJECT_NONSTANDARD,
5098 strprintf("Expected to offer services %08x", pfrom->nServicesExpected));
5099 pfrom->fDisconnect = true;
5100 return false;
5103 if (pfrom->nVersion < MIN_PEER_PROTO_VERSION)
5105 // disconnect from peers older than this proto version
5106 LogPrintf("peer=%d using obsolete version %i; disconnecting\n", pfrom->id, pfrom->nVersion);
5107 connman.PushMessageWithVersion(pfrom, INIT_PROTO_VERSION, NetMsgType::REJECT, strCommand, REJECT_OBSOLETE,
5108 strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION));
5109 pfrom->fDisconnect = true;
5110 return false;
5113 if (pfrom->nVersion == 10300)
5114 pfrom->nVersion = 300;
5115 if (!vRecv.empty())
5116 vRecv >> addrFrom >> nNonce;
5117 if (!vRecv.empty()) {
5118 vRecv >> LIMITED_STRING(pfrom->strSubVer, MAX_SUBVERSION_LENGTH);
5119 pfrom->cleanSubVer = SanitizeString(pfrom->strSubVer);
5121 if (!vRecv.empty()) {
5122 vRecv >> pfrom->nStartingHeight;
5125 LOCK(pfrom->cs_filter);
5126 if (!vRecv.empty())
5127 vRecv >> pfrom->fRelayTxes; // set to true after we get the first filter* message
5128 else
5129 pfrom->fRelayTxes = true;
5132 // Disconnect if we connected to ourself
5133 if (pfrom->fInbound && !connman.CheckIncomingNonce(nNonce))
5135 LogPrintf("connected to self at %s, disconnecting\n", pfrom->addr.ToString());
5136 pfrom->fDisconnect = true;
5137 return true;
5140 pfrom->addrLocal = addrMe;
5141 if (pfrom->fInbound && addrMe.IsRoutable())
5143 SeenLocal(addrMe);
5146 // Be shy and don't send version until we hear
5147 if (pfrom->fInbound)
5148 PushNodeVersion(pfrom, connman, GetAdjustedTime());
5150 pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
5152 if((pfrom->nServices & NODE_WITNESS))
5154 LOCK(cs_main);
5155 State(pfrom->GetId())->fHaveWitness = true;
5158 // Potentially mark this peer as a preferred download peer.
5160 LOCK(cs_main);
5161 UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
5164 // Change version
5165 connman.PushMessageWithVersion(pfrom, INIT_PROTO_VERSION, NetMsgType::VERACK);
5166 pfrom->SetSendVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
5168 if (!pfrom->fInbound)
5170 // Advertise our address
5171 if (fListen && !IsInitialBlockDownload())
5173 CAddress addr = GetLocalAddress(&pfrom->addr, pfrom->GetLocalServices());
5174 FastRandomContext insecure_rand;
5175 if (addr.IsRoutable())
5177 LogPrint("net", "ProcessMessages: advertising address %s\n", addr.ToString());
5178 pfrom->PushAddress(addr, insecure_rand);
5179 } else if (IsPeerAddrLocalGood(pfrom)) {
5180 addr.SetIP(pfrom->addrLocal);
5181 LogPrint("net", "ProcessMessages: advertising address %s\n", addr.ToString());
5182 pfrom->PushAddress(addr, insecure_rand);
5186 // Get recent addresses
5187 if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || connman.GetAddressCount() < 1000)
5189 connman.PushMessage(pfrom, NetMsgType::GETADDR);
5190 pfrom->fGetAddr = true;
5192 connman.MarkAddressGood(pfrom->addr);
5195 pfrom->fSuccessfullyConnected = true;
5197 string remoteAddr;
5198 if (fLogIPs)
5199 remoteAddr = ", peeraddr=" + pfrom->addr.ToString();
5201 LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
5202 pfrom->cleanSubVer, pfrom->nVersion,
5203 pfrom->nStartingHeight, addrMe.ToString(), pfrom->id,
5204 remoteAddr);
5206 int64_t nTimeOffset = nTime - GetTime();
5207 pfrom->nTimeOffset = nTimeOffset;
5208 AddTimeData(pfrom->addr, nTimeOffset);
5212 else if (pfrom->nVersion == 0)
5214 // Must have a version message before anything else
5215 LOCK(cs_main);
5216 Misbehaving(pfrom->GetId(), 1);
5217 return false;
5221 else if (strCommand == NetMsgType::VERACK)
5223 pfrom->SetRecvVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
5225 // Mark this node as currently connected, so we update its timestamp later.
5226 if (pfrom->fNetworkNode) {
5227 LOCK(cs_main);
5228 State(pfrom->GetId())->fCurrentlyConnected = true;
5231 if (pfrom->nVersion >= SENDHEADERS_VERSION) {
5232 // Tell our peer we prefer to receive headers rather than inv's
5233 // We send this to non-NODE NETWORK peers as well, because even
5234 // non-NODE NETWORK peers can announce blocks (such as pruning
5235 // nodes)
5236 connman.PushMessage(pfrom, NetMsgType::SENDHEADERS);
5238 if (pfrom->nVersion >= SHORT_IDS_BLOCKS_VERSION) {
5239 // Tell our peer we are willing to provide version 1 or 2 cmpctblocks
5240 // However, we do not request new block announcements using
5241 // cmpctblock messages.
5242 // We send this to non-NODE NETWORK peers as well, because
5243 // they may wish to request compact blocks from us
5244 bool fAnnounceUsingCMPCTBLOCK = false;
5245 uint64_t nCMPCTBLOCKVersion = 2;
5246 if (pfrom->GetLocalServices() & NODE_WITNESS)
5247 connman.PushMessage(pfrom, NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion);
5248 nCMPCTBLOCKVersion = 1;
5249 connman.PushMessage(pfrom, NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion);
5254 else if (strCommand == NetMsgType::ADDR)
5256 vector<CAddress> vAddr;
5257 vRecv >> vAddr;
5259 // Don't want addr from older versions unless seeding
5260 if (pfrom->nVersion < CADDR_TIME_VERSION && connman.GetAddressCount() > 1000)
5261 return true;
5262 if (vAddr.size() > 1000)
5264 LOCK(cs_main);
5265 Misbehaving(pfrom->GetId(), 20);
5266 return error("message addr size() = %u", vAddr.size());
5269 // Store the new addresses
5270 vector<CAddress> vAddrOk;
5271 int64_t nNow = GetAdjustedTime();
5272 int64_t nSince = nNow - 10 * 60;
5273 BOOST_FOREACH(CAddress& addr, vAddr)
5275 boost::this_thread::interruption_point();
5277 if ((addr.nServices & REQUIRED_SERVICES) != REQUIRED_SERVICES)
5278 continue;
5280 if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
5281 addr.nTime = nNow - 5 * 24 * 60 * 60;
5282 pfrom->AddAddressKnown(addr);
5283 bool fReachable = IsReachable(addr);
5284 if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
5286 // Relay to a limited number of other nodes
5287 RelayAddress(addr, fReachable, connman);
5289 // Do not store addresses outside our network
5290 if (fReachable)
5291 vAddrOk.push_back(addr);
5293 connman.AddNewAddresses(vAddrOk, pfrom->addr, 2 * 60 * 60);
5294 if (vAddr.size() < 1000)
5295 pfrom->fGetAddr = false;
5296 if (pfrom->fOneShot)
5297 pfrom->fDisconnect = true;
5300 else if (strCommand == NetMsgType::SENDHEADERS)
5302 LOCK(cs_main);
5303 State(pfrom->GetId())->fPreferHeaders = true;
5306 else if (strCommand == NetMsgType::SENDCMPCT)
5308 bool fAnnounceUsingCMPCTBLOCK = false;
5309 uint64_t nCMPCTBLOCKVersion = 0;
5310 vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
5311 if (nCMPCTBLOCKVersion == 1 || ((pfrom->GetLocalServices() & NODE_WITNESS) && nCMPCTBLOCKVersion == 2)) {
5312 LOCK(cs_main);
5313 // fProvidesHeaderAndIDs is used to "lock in" version of compact blocks we send (fWantsCmpctWitness)
5314 if (!State(pfrom->GetId())->fProvidesHeaderAndIDs) {
5315 State(pfrom->GetId())->fProvidesHeaderAndIDs = true;
5316 State(pfrom->GetId())->fWantsCmpctWitness = nCMPCTBLOCKVersion == 2;
5318 if (State(pfrom->GetId())->fWantsCmpctWitness == (nCMPCTBLOCKVersion == 2)) // ignore later version announces
5319 State(pfrom->GetId())->fPreferHeaderAndIDs = fAnnounceUsingCMPCTBLOCK;
5320 if (!State(pfrom->GetId())->fSupportsDesiredCmpctVersion) {
5321 if (pfrom->GetLocalServices() & NODE_WITNESS)
5322 State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 2);
5323 else
5324 State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 1);
5330 else if (strCommand == NetMsgType::INV)
5332 vector<CInv> vInv;
5333 vRecv >> vInv;
5334 if (vInv.size() > MAX_INV_SZ)
5336 LOCK(cs_main);
5337 Misbehaving(pfrom->GetId(), 20);
5338 return error("message inv size() = %u", vInv.size());
5341 bool fBlocksOnly = !fRelayTxes;
5343 // Allow whitelisted peers to send data other than blocks in blocks only mode if whitelistrelay is true
5344 if (pfrom->fWhitelisted && GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY))
5345 fBlocksOnly = false;
5347 LOCK(cs_main);
5349 uint32_t nFetchFlags = GetFetchFlags(pfrom, chainActive.Tip(), chainparams.GetConsensus());
5351 std::vector<CInv> vToFetch;
5353 for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
5355 CInv &inv = vInv[nInv];
5357 boost::this_thread::interruption_point();
5359 bool fAlreadyHave = AlreadyHave(inv);
5360 LogPrint("net", "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom->id);
5362 if (inv.type == MSG_TX) {
5363 inv.type |= nFetchFlags;
5366 if (inv.type == MSG_BLOCK) {
5367 UpdateBlockAvailability(pfrom->GetId(), inv.hash);
5368 if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
5369 // First request the headers preceding the announced block. In the normal fully-synced
5370 // case where a new block is announced that succeeds the current tip (no reorganization),
5371 // there are no such headers.
5372 // Secondly, and only when we are close to being synced, we request the announced block directly,
5373 // to avoid an extra round-trip. Note that we must *first* ask for the headers, so by the
5374 // time the block arrives, the header chain leading up to it is already validated. Not
5375 // doing this will result in the received block being rejected as an orphan in case it is
5376 // not a direct successor.
5377 connman.PushMessage(pfrom, NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), inv.hash);
5378 CNodeState *nodestate = State(pfrom->GetId());
5379 if (CanDirectFetch(chainparams.GetConsensus()) &&
5380 nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER &&
5381 (!IsWitnessEnabled(chainActive.Tip(), chainparams.GetConsensus()) || State(pfrom->GetId())->fHaveWitness)) {
5382 inv.type |= nFetchFlags;
5383 if (nodestate->fSupportsDesiredCmpctVersion)
5384 vToFetch.push_back(CInv(MSG_CMPCT_BLOCK, inv.hash));
5385 else
5386 vToFetch.push_back(inv);
5387 // Mark block as in flight already, even though the actual "getdata" message only goes out
5388 // later (within the same cs_main lock, though).
5389 MarkBlockAsInFlight(pfrom->GetId(), inv.hash, chainparams.GetConsensus());
5391 LogPrint("net", "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, inv.hash.ToString(), pfrom->id);
5394 else
5396 pfrom->AddInventoryKnown(inv);
5397 if (fBlocksOnly)
5398 LogPrint("net", "transaction (%s) inv sent in violation of protocol peer=%d\n", inv.hash.ToString(), pfrom->id);
5399 else if (!fAlreadyHave && !fImporting && !fReindex && !IsInitialBlockDownload())
5400 pfrom->AskFor(inv);
5403 // Track requests for our stuff
5404 GetMainSignals().Inventory(inv.hash);
5406 if (pfrom->nSendSize > (nMaxSendBufferSize * 2)) {
5407 Misbehaving(pfrom->GetId(), 50);
5408 return error("send buffer size() = %u", pfrom->nSendSize);
5412 if (!vToFetch.empty())
5413 connman.PushMessage(pfrom, NetMsgType::GETDATA, vToFetch);
5417 else if (strCommand == NetMsgType::GETDATA)
5419 vector<CInv> vInv;
5420 vRecv >> vInv;
5421 if (vInv.size() > MAX_INV_SZ)
5423 LOCK(cs_main);
5424 Misbehaving(pfrom->GetId(), 20);
5425 return error("message getdata size() = %u", vInv.size());
5428 if (fDebug || (vInv.size() != 1))
5429 LogPrint("net", "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom->id);
5431 if ((fDebug && vInv.size() > 0) || (vInv.size() == 1))
5432 LogPrint("net", "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom->id);
5434 pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
5435 ProcessGetData(pfrom, chainparams.GetConsensus(), connman);
5439 else if (strCommand == NetMsgType::GETBLOCKS)
5441 CBlockLocator locator;
5442 uint256 hashStop;
5443 vRecv >> locator >> hashStop;
5445 LOCK(cs_main);
5447 // Find the last block the caller has in the main chain
5448 CBlockIndex* pindex = FindForkInGlobalIndex(chainActive, locator);
5450 // Send the rest of the chain
5451 if (pindex)
5452 pindex = chainActive.Next(pindex);
5453 int nLimit = 500;
5454 LogPrint("net", "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), nLimit, pfrom->id);
5455 for (; pindex; pindex = chainActive.Next(pindex))
5457 if (pindex->GetBlockHash() == hashStop)
5459 LogPrint("net", " getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
5460 break;
5462 // If pruning, don't inv blocks unless we have on disk and are likely to still have
5463 // for some reasonable time window (1 hour) that block relay might require.
5464 const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / chainparams.GetConsensus().nPowTargetSpacing;
5465 if (fPruneMode && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= chainActive.Tip()->nHeight - nPrunedBlocksLikelyToHave))
5467 LogPrint("net", " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
5468 break;
5470 pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
5471 if (--nLimit <= 0)
5473 // When this block is requested, we'll send an inv that'll
5474 // trigger the peer to getblocks the next batch of inventory.
5475 LogPrint("net", " getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
5476 pfrom->hashContinue = pindex->GetBlockHash();
5477 break;
5483 else if (strCommand == NetMsgType::GETBLOCKTXN)
5485 BlockTransactionsRequest req;
5486 vRecv >> req;
5488 LOCK(cs_main);
5490 BlockMap::iterator it = mapBlockIndex.find(req.blockhash);
5491 if (it == mapBlockIndex.end() || !(it->second->nStatus & BLOCK_HAVE_DATA)) {
5492 LogPrintf("Peer %d sent us a getblocktxn for a block we don't have", pfrom->id);
5493 return true;
5496 if (it->second->nHeight < chainActive.Height() - MAX_BLOCKTXN_DEPTH) {
5497 LogPrint("net", "Peer %d sent us a getblocktxn for a block > %i deep", pfrom->id, MAX_BLOCKTXN_DEPTH);
5498 return true;
5501 CBlock block;
5502 assert(ReadBlockFromDisk(block, it->second, chainparams.GetConsensus()));
5504 BlockTransactions resp(req);
5505 for (size_t i = 0; i < req.indexes.size(); i++) {
5506 if (req.indexes[i] >= block.vtx.size()) {
5507 Misbehaving(pfrom->GetId(), 100);
5508 LogPrintf("Peer %d sent us a getblocktxn with out-of-bounds tx indices", pfrom->id);
5509 return true;
5511 resp.txn[i] = block.vtx[req.indexes[i]];
5513 connman.PushMessageWithFlag(pfrom, State(pfrom->GetId())->fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCKTXN, resp);
5517 else if (strCommand == NetMsgType::GETHEADERS)
5519 CBlockLocator locator;
5520 uint256 hashStop;
5521 vRecv >> locator >> hashStop;
5523 LOCK(cs_main);
5524 if (IsInitialBlockDownload() && !pfrom->fWhitelisted) {
5525 LogPrint("net", "Ignoring getheaders from peer=%d because node is in initial block download\n", pfrom->id);
5526 return true;
5529 CNodeState *nodestate = State(pfrom->GetId());
5530 CBlockIndex* pindex = NULL;
5531 if (locator.IsNull())
5533 // If locator is null, return the hashStop block
5534 BlockMap::iterator mi = mapBlockIndex.find(hashStop);
5535 if (mi == mapBlockIndex.end())
5536 return true;
5537 pindex = (*mi).second;
5539 else
5541 // Find the last block the caller has in the main chain
5542 pindex = FindForkInGlobalIndex(chainActive, locator);
5543 if (pindex)
5544 pindex = chainActive.Next(pindex);
5547 // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
5548 vector<CBlock> vHeaders;
5549 int nLimit = MAX_HEADERS_RESULTS;
5550 LogPrint("net", "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), pfrom->id);
5551 for (; pindex; pindex = chainActive.Next(pindex))
5553 vHeaders.push_back(pindex->GetBlockHeader());
5554 if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
5555 break;
5557 // pindex can be NULL either if we sent chainActive.Tip() OR
5558 // if our peer has chainActive.Tip() (and thus we are sending an empty
5559 // headers message). In both cases it's safe to update
5560 // pindexBestHeaderSent to be our tip.
5561 nodestate->pindexBestHeaderSent = pindex ? pindex : chainActive.Tip();
5562 connman.PushMessage(pfrom, NetMsgType::HEADERS, vHeaders);
5566 else if (strCommand == NetMsgType::TX)
5568 // Stop processing the transaction early if
5569 // We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
5570 if (!fRelayTxes && (!pfrom->fWhitelisted || !GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)))
5572 LogPrint("net", "transaction sent in violation of protocol peer=%d\n", pfrom->id);
5573 return true;
5576 deque<COutPoint> vWorkQueue;
5577 vector<uint256> vEraseQueue;
5578 CTransaction tx;
5579 vRecv >> tx;
5581 CInv inv(MSG_TX, tx.GetHash());
5582 pfrom->AddInventoryKnown(inv);
5584 LOCK(cs_main);
5586 bool fMissingInputs = false;
5587 CValidationState state;
5589 pfrom->setAskFor.erase(inv.hash);
5590 mapAlreadyAskedFor.erase(inv.hash);
5592 if (!AlreadyHave(inv) && AcceptToMemoryPool(mempool, state, tx, true, &fMissingInputs)) {
5593 mempool.check(pcoinsTip);
5594 RelayTransaction(tx, connman);
5595 for (unsigned int i = 0; i < tx.vout.size(); i++) {
5596 vWorkQueue.emplace_back(inv.hash, i);
5599 pfrom->nLastTXTime = GetTime();
5601 LogPrint("mempool", "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
5602 pfrom->id,
5603 tx.GetHash().ToString(),
5604 mempool.size(), mempool.DynamicMemoryUsage() / 1000);
5606 // Recursively process any orphan transactions that depended on this one
5607 set<NodeId> setMisbehaving;
5608 while (!vWorkQueue.empty()) {
5609 auto itByPrev = mapOrphanTransactionsByPrev.find(vWorkQueue.front());
5610 vWorkQueue.pop_front();
5611 if (itByPrev == mapOrphanTransactionsByPrev.end())
5612 continue;
5613 for (auto mi = itByPrev->second.begin();
5614 mi != itByPrev->second.end();
5615 ++mi)
5617 const CTransaction& orphanTx = (*mi)->second.tx;
5618 const uint256& orphanHash = orphanTx.GetHash();
5619 NodeId fromPeer = (*mi)->second.fromPeer;
5620 bool fMissingInputs2 = false;
5621 // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
5622 // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
5623 // anyone relaying LegitTxX banned)
5624 CValidationState stateDummy;
5627 if (setMisbehaving.count(fromPeer))
5628 continue;
5629 if (AcceptToMemoryPool(mempool, stateDummy, orphanTx, true, &fMissingInputs2)) {
5630 LogPrint("mempool", " accepted orphan tx %s\n", orphanHash.ToString());
5631 RelayTransaction(orphanTx, connman);
5632 for (unsigned int i = 0; i < orphanTx.vout.size(); i++) {
5633 vWorkQueue.emplace_back(orphanHash, i);
5635 vEraseQueue.push_back(orphanHash);
5637 else if (!fMissingInputs2)
5639 int nDos = 0;
5640 if (stateDummy.IsInvalid(nDos) && nDos > 0)
5642 // Punish peer that gave us an invalid orphan tx
5643 Misbehaving(fromPeer, nDos);
5644 setMisbehaving.insert(fromPeer);
5645 LogPrint("mempool", " invalid orphan tx %s\n", orphanHash.ToString());
5647 // Has inputs but not accepted to mempool
5648 // Probably non-standard or insufficient fee/priority
5649 LogPrint("mempool", " removed orphan tx %s\n", orphanHash.ToString());
5650 vEraseQueue.push_back(orphanHash);
5651 if (orphanTx.wit.IsNull() && !stateDummy.CorruptionPossible()) {
5652 // Do not use rejection cache for witness transactions or
5653 // witness-stripped transactions, as they can have been malleated.
5654 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
5655 assert(recentRejects);
5656 recentRejects->insert(orphanHash);
5659 mempool.check(pcoinsTip);
5663 BOOST_FOREACH(uint256 hash, vEraseQueue)
5664 EraseOrphanTx(hash);
5666 else if (fMissingInputs)
5668 bool fRejectedParents = false; // It may be the case that the orphans parents have all been rejected
5669 BOOST_FOREACH(const CTxIn& txin, tx.vin) {
5670 if (recentRejects->contains(txin.prevout.hash)) {
5671 fRejectedParents = true;
5672 break;
5675 if (!fRejectedParents) {
5676 BOOST_FOREACH(const CTxIn& txin, tx.vin) {
5677 CInv _inv(MSG_TX, txin.prevout.hash);
5678 pfrom->AddInventoryKnown(_inv);
5679 if (!AlreadyHave(_inv)) pfrom->AskFor(_inv);
5681 AddOrphanTx(tx, pfrom->GetId());
5683 // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
5684 unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));
5685 unsigned int nEvicted = LimitOrphanTxSize(nMaxOrphanTx);
5686 if (nEvicted > 0)
5687 LogPrint("mempool", "mapOrphan overflow, removed %u tx\n", nEvicted);
5688 } else {
5689 LogPrint("mempool", "not keeping orphan with rejected parents %s\n",tx.GetHash().ToString());
5691 } else {
5692 if (tx.wit.IsNull() && !state.CorruptionPossible()) {
5693 // Do not use rejection cache for witness transactions or
5694 // witness-stripped transactions, as they can have been malleated.
5695 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
5696 assert(recentRejects);
5697 recentRejects->insert(tx.GetHash());
5700 if (pfrom->fWhitelisted && GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY)) {
5701 // Always relay transactions received from whitelisted peers, even
5702 // if they were already in the mempool or rejected from it due
5703 // to policy, allowing the node to function as a gateway for
5704 // nodes hidden behind it.
5706 // Never relay transactions that we would assign a non-zero DoS
5707 // score for, as we expect peers to do the same with us in that
5708 // case.
5709 int nDoS = 0;
5710 if (!state.IsInvalid(nDoS) || nDoS == 0) {
5711 LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", tx.GetHash().ToString(), pfrom->id);
5712 RelayTransaction(tx, connman);
5713 } else {
5714 LogPrintf("Not relaying invalid transaction %s from whitelisted peer=%d (%s)\n", tx.GetHash().ToString(), pfrom->id, FormatStateMessage(state));
5718 int nDoS = 0;
5719 if (state.IsInvalid(nDoS))
5721 LogPrint("mempoolrej", "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
5722 pfrom->id,
5723 FormatStateMessage(state));
5724 if (state.GetRejectCode() < REJECT_INTERNAL) // Never send AcceptToMemoryPool's internal codes over P2P
5725 connman.PushMessage(pfrom, NetMsgType::REJECT, strCommand, (unsigned char)state.GetRejectCode(),
5726 state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash);
5727 if (nDoS > 0) {
5728 Misbehaving(pfrom->GetId(), nDoS);
5734 else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
5736 CBlockHeaderAndShortTxIDs cmpctblock;
5737 vRecv >> cmpctblock;
5739 LOCK(cs_main);
5741 if (mapBlockIndex.find(cmpctblock.header.hashPrevBlock) == mapBlockIndex.end()) {
5742 // Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
5743 if (!IsInitialBlockDownload())
5744 connman.PushMessage(pfrom, NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256());
5745 return true;
5748 CBlockIndex *pindex = NULL;
5749 CValidationState state;
5750 if (!AcceptBlockHeader(cmpctblock.header, state, chainparams, &pindex)) {
5751 int nDoS;
5752 if (state.IsInvalid(nDoS)) {
5753 if (nDoS > 0)
5754 Misbehaving(pfrom->GetId(), nDoS);
5755 LogPrintf("Peer %d sent us invalid header via cmpctblock\n", pfrom->id);
5756 return true;
5760 // If AcceptBlockHeader returned true, it set pindex
5761 assert(pindex);
5762 UpdateBlockAvailability(pfrom->GetId(), pindex->GetBlockHash());
5764 std::map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator blockInFlightIt = mapBlocksInFlight.find(pindex->GetBlockHash());
5765 bool fAlreadyInFlight = blockInFlightIt != mapBlocksInFlight.end();
5767 if (pindex->nStatus & BLOCK_HAVE_DATA) // Nothing to do here
5768 return true;
5770 if (pindex->nChainWork <= chainActive.Tip()->nChainWork || // We know something better
5771 pindex->nTx != 0) { // We had this block at some point, but pruned it
5772 if (fAlreadyInFlight) {
5773 // We requested this block for some reason, but our mempool will probably be useless
5774 // so we just grab the block via normal getdata
5775 std::vector<CInv> vInv(1);
5776 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom, pindex->pprev, chainparams.GetConsensus()), cmpctblock.header.GetHash());
5777 connman.PushMessage(pfrom, NetMsgType::GETDATA, vInv);
5779 return true;
5782 // If we're not close to tip yet, give up and let parallel block fetch work its magic
5783 if (!fAlreadyInFlight && !CanDirectFetch(chainparams.GetConsensus()))
5784 return true;
5786 CNodeState *nodestate = State(pfrom->GetId());
5788 if (IsWitnessEnabled(pindex->pprev, chainparams.GetConsensus()) && !nodestate->fSupportsDesiredCmpctVersion) {
5789 // Don't bother trying to process compact blocks from v1 peers
5790 // after segwit activates.
5791 return true;
5794 // We want to be a bit conservative just to be extra careful about DoS
5795 // possibilities in compact block processing...
5796 if (pindex->nHeight <= chainActive.Height() + 2) {
5797 if ((!fAlreadyInFlight && nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) ||
5798 (fAlreadyInFlight && blockInFlightIt->second.first == pfrom->GetId())) {
5799 list<QueuedBlock>::iterator *queuedBlockIt = NULL;
5800 if (!MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), chainparams.GetConsensus(), pindex, &queuedBlockIt)) {
5801 if (!(*queuedBlockIt)->partialBlock)
5802 (*queuedBlockIt)->partialBlock.reset(new PartiallyDownloadedBlock(&mempool));
5803 else {
5804 // The block was already in flight using compact blocks from the same peer
5805 LogPrint("net", "Peer sent us compact block we were already syncing!\n");
5806 return true;
5810 PartiallyDownloadedBlock& partialBlock = *(*queuedBlockIt)->partialBlock;
5811 ReadStatus status = partialBlock.InitData(cmpctblock);
5812 if (status == READ_STATUS_INVALID) {
5813 MarkBlockAsReceived(pindex->GetBlockHash()); // Reset in-flight state in case of whitelist
5814 Misbehaving(pfrom->GetId(), 100);
5815 LogPrintf("Peer %d sent us invalid compact block\n", pfrom->id);
5816 return true;
5817 } else if (status == READ_STATUS_FAILED) {
5818 // Duplicate txindexes, the block is now in-flight, so just request it
5819 std::vector<CInv> vInv(1);
5820 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom, pindex->pprev, chainparams.GetConsensus()), cmpctblock.header.GetHash());
5821 connman.PushMessage(pfrom, NetMsgType::GETDATA, vInv);
5822 return true;
5825 if (!fAlreadyInFlight && mapBlocksInFlight.size() == 1 && pindex->pprev->IsValid(BLOCK_VALID_CHAIN)) {
5826 // We seem to be rather well-synced, so it appears pfrom was the first to provide us
5827 // with this block! Let's get them to announce using compact blocks in the future.
5828 MaybeSetPeerAsAnnouncingHeaderAndIDs(nodestate, pfrom, connman);
5831 BlockTransactionsRequest req;
5832 for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
5833 if (!partialBlock.IsTxAvailable(i))
5834 req.indexes.push_back(i);
5836 if (req.indexes.empty()) {
5837 // Dirty hack to jump to BLOCKTXN code (TODO: move message handling into their own functions)
5838 BlockTransactions txn;
5839 txn.blockhash = cmpctblock.header.GetHash();
5840 CDataStream blockTxnMsg(SER_NETWORK, PROTOCOL_VERSION);
5841 blockTxnMsg << txn;
5842 return ProcessMessage(pfrom, NetMsgType::BLOCKTXN, blockTxnMsg, nTimeReceived, chainparams, connman);
5843 } else {
5844 req.blockhash = pindex->GetBlockHash();
5845 connman.PushMessage(pfrom, NetMsgType::GETBLOCKTXN, req);
5848 } else {
5849 if (fAlreadyInFlight) {
5850 // We requested this block, but its far into the future, so our
5851 // mempool will probably be useless - request the block normally
5852 std::vector<CInv> vInv(1);
5853 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom, pindex->pprev, chainparams.GetConsensus()), cmpctblock.header.GetHash());
5854 connman.PushMessage(pfrom, NetMsgType::GETDATA, vInv);
5855 return true;
5856 } else {
5857 // If this was an announce-cmpctblock, we want the same treatment as a header message
5858 // Dirty hack to process as if it were just a headers message (TODO: move message handling into their own functions)
5859 std::vector<CBlock> headers;
5860 headers.push_back(cmpctblock.header);
5861 CDataStream vHeadersMsg(SER_NETWORK, PROTOCOL_VERSION);
5862 vHeadersMsg << headers;
5863 return ProcessMessage(pfrom, NetMsgType::HEADERS, vHeadersMsg, nTimeReceived, chainparams, connman);
5868 else if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
5870 BlockTransactions resp;
5871 vRecv >> resp;
5873 CBlock block;
5874 bool fBlockRead = false;
5876 LOCK(cs_main);
5878 map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator it = mapBlocksInFlight.find(resp.blockhash);
5879 if (it == mapBlocksInFlight.end() || !it->second.second->partialBlock ||
5880 it->second.first != pfrom->GetId()) {
5881 LogPrint("net", "Peer %d sent us block transactions for block we weren't expecting\n", pfrom->id);
5882 return true;
5885 PartiallyDownloadedBlock& partialBlock = *it->second.second->partialBlock;
5886 ReadStatus status = partialBlock.FillBlock(block, resp.txn);
5887 if (status == READ_STATUS_INVALID) {
5888 MarkBlockAsReceived(resp.blockhash); // Reset in-flight state in case of whitelist
5889 Misbehaving(pfrom->GetId(), 100);
5890 LogPrintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom->id);
5891 return true;
5892 } else if (status == READ_STATUS_FAILED) {
5893 // Might have collided, fall back to getdata now :(
5894 std::vector<CInv> invs;
5895 invs.push_back(CInv(MSG_BLOCK | GetFetchFlags(pfrom, chainActive.Tip(), chainparams.GetConsensus()), resp.blockhash));
5896 connman.PushMessage(pfrom, NetMsgType::GETDATA, invs);
5897 } else {
5898 // Block is either okay, or possibly we received
5899 // READ_STATUS_CHECKBLOCK_FAILED.
5900 // Note that CheckBlock can only fail for one of a few reasons:
5901 // 1. bad-proof-of-work (impossible here, because we've already
5902 // accepted the header)
5903 // 2. merkleroot doesn't match the transactions given (already
5904 // caught in FillBlock with READ_STATUS_FAILED, so
5905 // impossible here)
5906 // 3. the block is otherwise invalid (eg invalid coinbase,
5907 // block is too big, too many legacy sigops, etc).
5908 // So if CheckBlock failed, #3 is the only possibility.
5909 // Under BIP 152, we don't DoS-ban unless proof of work is
5910 // invalid (we don't require all the stateless checks to have
5911 // been run). This is handled below, so just treat this as
5912 // though the block was successfully read, and rely on the
5913 // handling in ProcessNewBlock to ensure the block index is
5914 // updated, reject messages go out, etc.
5915 MarkBlockAsReceived(resp.blockhash); // it is now an empty pointer
5916 fBlockRead = true;
5917 // mapBlockSource is only used for sending reject messages and DoS scores,
5918 // so the race between here and cs_main in ProcessNewBlock is fine.
5919 // BIP 152 permits peers to relay compact blocks after validating
5920 // the header only; we should not punish peers if the block turns
5921 // out to be invalid.
5922 mapBlockSource.emplace(resp.blockhash, std::make_pair(pfrom->GetId(), false));
5924 } // Don't hold cs_main when we call into ProcessNewBlock
5925 if (fBlockRead) {
5926 bool fNewBlock = false;
5927 // Since we requested this block (it was in mapBlocksInFlight), force it to be processed,
5928 // even if it would not be a candidate for new tip (missing previous block, chain not long enough, etc)
5929 ProcessNewBlock(chainparams, &block, true, NULL, &fNewBlock);
5930 if (fNewBlock)
5931 pfrom->nLastBlockTime = GetTime();
5936 else if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
5938 std::vector<CBlockHeader> headers;
5940 // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
5941 unsigned int nCount = ReadCompactSize(vRecv);
5942 if (nCount > MAX_HEADERS_RESULTS) {
5943 LOCK(cs_main);
5944 Misbehaving(pfrom->GetId(), 20);
5945 return error("headers message size = %u", nCount);
5947 headers.resize(nCount);
5948 for (unsigned int n = 0; n < nCount; n++) {
5949 vRecv >> headers[n];
5950 ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
5954 LOCK(cs_main);
5956 if (nCount == 0) {
5957 // Nothing interesting. Stop asking this peers for more headers.
5958 return true;
5961 CNodeState *nodestate = State(pfrom->GetId());
5963 // If this looks like it could be a block announcement (nCount <
5964 // MAX_BLOCKS_TO_ANNOUNCE), use special logic for handling headers that
5965 // don't connect:
5966 // - Send a getheaders message in response to try to connect the chain.
5967 // - The peer can send up to MAX_UNCONNECTING_HEADERS in a row that
5968 // don't connect before giving DoS points
5969 // - Once a headers message is received that is valid and does connect,
5970 // nUnconnectingHeaders gets reset back to 0.
5971 if (mapBlockIndex.find(headers[0].hashPrevBlock) == mapBlockIndex.end() && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
5972 nodestate->nUnconnectingHeaders++;
5973 connman.PushMessage(pfrom, NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256());
5974 LogPrint("net", "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
5975 headers[0].GetHash().ToString(),
5976 headers[0].hashPrevBlock.ToString(),
5977 pindexBestHeader->nHeight,
5978 pfrom->id, nodestate->nUnconnectingHeaders);
5979 // Set hashLastUnknownBlock for this peer, so that if we
5980 // eventually get the headers - even from a different peer -
5981 // we can use this peer to download.
5982 UpdateBlockAvailability(pfrom->GetId(), headers.back().GetHash());
5984 if (nodestate->nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
5985 Misbehaving(pfrom->GetId(), 20);
5987 return true;
5990 CBlockIndex *pindexLast = NULL;
5991 BOOST_FOREACH(const CBlockHeader& header, headers) {
5992 CValidationState state;
5993 if (pindexLast != NULL && header.hashPrevBlock != pindexLast->GetBlockHash()) {
5994 Misbehaving(pfrom->GetId(), 20);
5995 return error("non-continuous headers sequence");
5997 if (!AcceptBlockHeader(header, state, chainparams, &pindexLast)) {
5998 int nDoS;
5999 if (state.IsInvalid(nDoS)) {
6000 if (nDoS > 0)
6001 Misbehaving(pfrom->GetId(), nDoS);
6002 return error("invalid header received");
6007 if (nodestate->nUnconnectingHeaders > 0) {
6008 LogPrint("net", "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom->id, nodestate->nUnconnectingHeaders);
6010 nodestate->nUnconnectingHeaders = 0;
6012 assert(pindexLast);
6013 UpdateBlockAvailability(pfrom->GetId(), pindexLast->GetBlockHash());
6015 if (nCount == MAX_HEADERS_RESULTS) {
6016 // Headers message had its maximum size; the peer may have more headers.
6017 // TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
6018 // from there instead.
6019 LogPrint("net", "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom->id, pfrom->nStartingHeight);
6020 connman.PushMessage(pfrom, NetMsgType::GETHEADERS, chainActive.GetLocator(pindexLast), uint256());
6023 bool fCanDirectFetch = CanDirectFetch(chainparams.GetConsensus());
6024 // If this set of headers is valid and ends in a block with at least as
6025 // much work as our tip, download as much as possible.
6026 if (fCanDirectFetch && pindexLast->IsValid(BLOCK_VALID_TREE) && chainActive.Tip()->nChainWork <= pindexLast->nChainWork) {
6027 vector<CBlockIndex *> vToFetch;
6028 CBlockIndex *pindexWalk = pindexLast;
6029 // Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
6030 while (pindexWalk && !chainActive.Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
6031 if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
6032 !mapBlocksInFlight.count(pindexWalk->GetBlockHash()) &&
6033 (!IsWitnessEnabled(pindexWalk->pprev, chainparams.GetConsensus()) || State(pfrom->GetId())->fHaveWitness)) {
6034 // We don't have this block, and it's not yet in flight.
6035 vToFetch.push_back(pindexWalk);
6037 pindexWalk = pindexWalk->pprev;
6039 // If pindexWalk still isn't on our main chain, we're looking at a
6040 // very large reorg at a time we think we're close to caught up to
6041 // the main chain -- this shouldn't really happen. Bail out on the
6042 // direct fetch and rely on parallel download instead.
6043 if (!chainActive.Contains(pindexWalk)) {
6044 LogPrint("net", "Large reorg, won't direct fetch to %s (%d)\n",
6045 pindexLast->GetBlockHash().ToString(),
6046 pindexLast->nHeight);
6047 } else {
6048 vector<CInv> vGetData;
6049 // Download as much as possible, from earliest to latest.
6050 BOOST_REVERSE_FOREACH(CBlockIndex *pindex, vToFetch) {
6051 if (nodestate->nBlocksInFlight >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
6052 // Can't download any more from this peer
6053 break;
6055 uint32_t nFetchFlags = GetFetchFlags(pfrom, pindex->pprev, chainparams.GetConsensus());
6056 vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
6057 MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), chainparams.GetConsensus(), pindex);
6058 LogPrint("net", "Requesting block %s from peer=%d\n",
6059 pindex->GetBlockHash().ToString(), pfrom->id);
6061 if (vGetData.size() > 1) {
6062 LogPrint("net", "Downloading blocks toward %s (%d) via headers direct fetch\n",
6063 pindexLast->GetBlockHash().ToString(), pindexLast->nHeight);
6065 if (vGetData.size() > 0) {
6066 if (nodestate->fSupportsDesiredCmpctVersion && vGetData.size() == 1 && mapBlocksInFlight.size() == 1 && pindexLast->pprev->IsValid(BLOCK_VALID_CHAIN)) {
6067 // We seem to be rather well-synced, so it appears pfrom was the first to provide us
6068 // with this block! Let's get them to announce using compact blocks in the future.
6069 MaybeSetPeerAsAnnouncingHeaderAndIDs(nodestate, pfrom, connman);
6070 // In any case, we want to download using a compact block, not a regular one
6071 vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
6073 connman.PushMessage(pfrom, NetMsgType::GETDATA, vGetData);
6079 NotifyHeaderTip();
6082 else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
6084 CBlock block;
6085 vRecv >> block;
6087 LogPrint("net", "received block %s peer=%d\n", block.GetHash().ToString(), pfrom->id);
6089 // Process all blocks from whitelisted peers, even if not requested,
6090 // unless we're still syncing with the network.
6091 // Such an unrequested block may still be processed, subject to the
6092 // conditions in AcceptBlock().
6093 bool forceProcessing = pfrom->fWhitelisted && !IsInitialBlockDownload();
6094 const uint256 hash(block.GetHash());
6096 LOCK(cs_main);
6097 // Also always process if we requested the block explicitly, as we may
6098 // need it even though it is not a candidate for a new best tip.
6099 forceProcessing |= MarkBlockAsReceived(hash);
6100 // mapBlockSource is only used for sending reject messages and DoS scores,
6101 // so the race between here and cs_main in ProcessNewBlock is fine.
6102 mapBlockSource.emplace(hash, std::make_pair(pfrom->GetId(), true));
6104 bool fNewBlock = false;
6105 ProcessNewBlock(chainparams, &block, forceProcessing, NULL, &fNewBlock);
6106 if (fNewBlock)
6107 pfrom->nLastBlockTime = GetTime();
6111 else if (strCommand == NetMsgType::GETADDR)
6113 // This asymmetric behavior for inbound and outbound connections was introduced
6114 // to prevent a fingerprinting attack: an attacker can send specific fake addresses
6115 // to users' AddrMan and later request them by sending getaddr messages.
6116 // Making nodes which are behind NAT and can only make outgoing connections ignore
6117 // the getaddr message mitigates the attack.
6118 if (!pfrom->fInbound) {
6119 LogPrint("net", "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom->id);
6120 return true;
6123 // Only send one GetAddr response per connection to reduce resource waste
6124 // and discourage addr stamping of INV announcements.
6125 if (pfrom->fSentAddr) {
6126 LogPrint("net", "Ignoring repeated \"getaddr\". peer=%d\n", pfrom->id);
6127 return true;
6129 pfrom->fSentAddr = true;
6131 pfrom->vAddrToSend.clear();
6132 vector<CAddress> vAddr = connman.GetAddresses();
6133 FastRandomContext insecure_rand;
6134 BOOST_FOREACH(const CAddress &addr, vAddr)
6135 pfrom->PushAddress(addr, insecure_rand);
6139 else if (strCommand == NetMsgType::MEMPOOL)
6141 if (!(pfrom->GetLocalServices() & NODE_BLOOM) && !pfrom->fWhitelisted)
6143 LogPrint("net", "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom->GetId());
6144 pfrom->fDisconnect = true;
6145 return true;
6148 if (connman.OutboundTargetReached(false) && !pfrom->fWhitelisted)
6150 LogPrint("net", "mempool request with bandwidth limit reached, disconnect peer=%d\n", pfrom->GetId());
6151 pfrom->fDisconnect = true;
6152 return true;
6155 LOCK(pfrom->cs_inventory);
6156 pfrom->fSendMempool = true;
6160 else if (strCommand == NetMsgType::PING)
6162 if (pfrom->nVersion > BIP0031_VERSION)
6164 uint64_t nonce = 0;
6165 vRecv >> nonce;
6166 // Echo the message back with the nonce. This allows for two useful features:
6168 // 1) A remote node can quickly check if the connection is operational
6169 // 2) Remote nodes can measure the latency of the network thread. If this node
6170 // is overloaded it won't respond to pings quickly and the remote node can
6171 // avoid sending us more work, like chain download requests.
6173 // The nonce stops the remote getting confused between different pings: without
6174 // it, if the remote node sends a ping once per second and this node takes 5
6175 // seconds to respond to each, the 5th ping the remote sends would appear to
6176 // return very quickly.
6177 connman.PushMessage(pfrom, NetMsgType::PONG, nonce);
6182 else if (strCommand == NetMsgType::PONG)
6184 int64_t pingUsecEnd = nTimeReceived;
6185 uint64_t nonce = 0;
6186 size_t nAvail = vRecv.in_avail();
6187 bool bPingFinished = false;
6188 std::string sProblem;
6190 if (nAvail >= sizeof(nonce)) {
6191 vRecv >> nonce;
6193 // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
6194 if (pfrom->nPingNonceSent != 0) {
6195 if (nonce == pfrom->nPingNonceSent) {
6196 // Matching pong received, this ping is no longer outstanding
6197 bPingFinished = true;
6198 int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart;
6199 if (pingUsecTime > 0) {
6200 // Successful ping time measurement, replace previous
6201 pfrom->nPingUsecTime = pingUsecTime;
6202 pfrom->nMinPingUsecTime = std::min(pfrom->nMinPingUsecTime, pingUsecTime);
6203 } else {
6204 // This should never happen
6205 sProblem = "Timing mishap";
6207 } else {
6208 // Nonce mismatches are normal when pings are overlapping
6209 sProblem = "Nonce mismatch";
6210 if (nonce == 0) {
6211 // This is most likely a bug in another implementation somewhere; cancel this ping
6212 bPingFinished = true;
6213 sProblem = "Nonce zero";
6216 } else {
6217 sProblem = "Unsolicited pong without ping";
6219 } else {
6220 // This is most likely a bug in another implementation somewhere; cancel this ping
6221 bPingFinished = true;
6222 sProblem = "Short payload";
6225 if (!(sProblem.empty())) {
6226 LogPrint("net", "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
6227 pfrom->id,
6228 sProblem,
6229 pfrom->nPingNonceSent,
6230 nonce,
6231 nAvail);
6233 if (bPingFinished) {
6234 pfrom->nPingNonceSent = 0;
6239 else if (strCommand == NetMsgType::FILTERLOAD)
6241 CBloomFilter filter;
6242 vRecv >> filter;
6244 if (!filter.IsWithinSizeConstraints())
6246 // There is no excuse for sending a too-large filter
6247 LOCK(cs_main);
6248 Misbehaving(pfrom->GetId(), 100);
6250 else
6252 LOCK(pfrom->cs_filter);
6253 delete pfrom->pfilter;
6254 pfrom->pfilter = new CBloomFilter(filter);
6255 pfrom->pfilter->UpdateEmptyFull();
6256 pfrom->fRelayTxes = true;
6261 else if (strCommand == NetMsgType::FILTERADD)
6263 vector<unsigned char> vData;
6264 vRecv >> vData;
6266 // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
6267 // and thus, the maximum size any matched object can have) in a filteradd message
6268 bool bad = false;
6269 if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE) {
6270 bad = true;
6271 } else {
6272 LOCK(pfrom->cs_filter);
6273 if (pfrom->pfilter) {
6274 pfrom->pfilter->insert(vData);
6275 } else {
6276 bad = true;
6279 if (bad) {
6280 LOCK(cs_main);
6281 Misbehaving(pfrom->GetId(), 100);
6286 else if (strCommand == NetMsgType::FILTERCLEAR)
6288 LOCK(pfrom->cs_filter);
6289 if (pfrom->GetLocalServices() & NODE_BLOOM) {
6290 delete pfrom->pfilter;
6291 pfrom->pfilter = new CBloomFilter();
6293 pfrom->fRelayTxes = true;
6297 else if (strCommand == NetMsgType::REJECT)
6299 if (fDebug) {
6300 try {
6301 string strMsg; unsigned char ccode; string strReason;
6302 vRecv >> LIMITED_STRING(strMsg, CMessageHeader::COMMAND_SIZE) >> ccode >> LIMITED_STRING(strReason, MAX_REJECT_MESSAGE_LENGTH);
6304 ostringstream ss;
6305 ss << strMsg << " code " << itostr(ccode) << ": " << strReason;
6307 if (strMsg == NetMsgType::BLOCK || strMsg == NetMsgType::TX)
6309 uint256 hash;
6310 vRecv >> hash;
6311 ss << ": hash " << hash.ToString();
6313 LogPrint("net", "Reject %s\n", SanitizeString(ss.str()));
6314 } catch (const std::ios_base::failure&) {
6315 // Avoid feedback loops by preventing reject messages from triggering a new reject message.
6316 LogPrint("net", "Unparseable reject message received\n");
6321 else if (strCommand == NetMsgType::FEEFILTER) {
6322 CAmount newFeeFilter = 0;
6323 vRecv >> newFeeFilter;
6324 if (MoneyRange(newFeeFilter)) {
6326 LOCK(pfrom->cs_feeFilter);
6327 pfrom->minFeeFilter = newFeeFilter;
6329 LogPrint("net", "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), pfrom->id);
6333 else if (strCommand == NetMsgType::NOTFOUND) {
6334 // We do not care about the NOTFOUND message, but logging an Unknown Command
6335 // message would be undesirable as we transmit it ourselves.
6338 else {
6339 // Ignore unknown commands for extensibility
6340 LogPrint("net", "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->id);
6345 return true;
6348 // requires LOCK(cs_vRecvMsg)
6349 bool ProcessMessages(CNode* pfrom, CConnman& connman)
6351 const CChainParams& chainparams = Params();
6352 unsigned int nMaxSendBufferSize = connman.GetSendBufferSize();
6353 //if (fDebug)
6354 // LogPrintf("%s(%u messages)\n", __func__, pfrom->vRecvMsg.size());
6357 // Message format
6358 // (4) message start
6359 // (12) command
6360 // (4) size
6361 // (4) checksum
6362 // (x) data
6364 bool fOk = true;
6366 if (!pfrom->vRecvGetData.empty())
6367 ProcessGetData(pfrom, chainparams.GetConsensus(), connman);
6369 // this maintains the order of responses
6370 if (!pfrom->vRecvGetData.empty()) return fOk;
6372 std::deque<CNetMessage>::iterator it = pfrom->vRecvMsg.begin();
6373 while (!pfrom->fDisconnect && it != pfrom->vRecvMsg.end()) {
6374 // Don't bother if send buffer is too full to respond anyway
6375 if (pfrom->nSendSize >= nMaxSendBufferSize)
6376 break;
6378 // get next message
6379 CNetMessage& msg = *it;
6381 //if (fDebug)
6382 // LogPrintf("%s(message %u msgsz, %u bytes, complete:%s)\n", __func__,
6383 // msg.hdr.nMessageSize, msg.vRecv.size(),
6384 // msg.complete() ? "Y" : "N");
6386 // end, if an incomplete message is found
6387 if (!msg.complete())
6388 break;
6390 // at this point, any failure means we can delete the current message
6391 it++;
6393 // Scan for message start
6394 if (memcmp(msg.hdr.pchMessageStart, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE) != 0) {
6395 LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->id);
6396 fOk = false;
6397 break;
6400 // Read header
6401 CMessageHeader& hdr = msg.hdr;
6402 if (!hdr.IsValid(chainparams.MessageStart()))
6404 LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->id);
6405 continue;
6407 string strCommand = hdr.GetCommand();
6409 // Message size
6410 unsigned int nMessageSize = hdr.nMessageSize;
6412 // Checksum
6413 CDataStream& vRecv = msg.vRecv;
6414 const uint256& hash = msg.GetMessageHash();
6415 if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0)
6417 LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,
6418 SanitizeString(strCommand), nMessageSize,
6419 HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
6420 HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
6421 continue;
6424 // Process message
6425 bool fRet = false;
6428 fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime, chainparams, connman);
6429 boost::this_thread::interruption_point();
6431 catch (const std::ios_base::failure& e)
6433 connman.PushMessageWithVersion(pfrom, INIT_PROTO_VERSION, NetMsgType::REJECT, strCommand, REJECT_MALFORMED, string("error parsing message"));
6434 if (strstr(e.what(), "end of data"))
6436 // Allow exceptions from under-length message on vRecv
6437 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());
6439 else if (strstr(e.what(), "size too large"))
6441 // Allow exceptions from over-long size
6442 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
6444 else if (strstr(e.what(), "non-canonical ReadCompactSize()"))
6446 // Allow exceptions from non-canonical encoding
6447 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
6449 else
6451 PrintExceptionContinue(&e, "ProcessMessages()");
6454 catch (const boost::thread_interrupted&) {
6455 throw;
6457 catch (const std::exception& e) {
6458 PrintExceptionContinue(&e, "ProcessMessages()");
6459 } catch (...) {
6460 PrintExceptionContinue(NULL, "ProcessMessages()");
6463 if (!fRet)
6464 LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->id);
6466 break;
6469 // In case the connection got shut down, its receive buffer was wiped
6470 if (!pfrom->fDisconnect)
6471 pfrom->vRecvMsg.erase(pfrom->vRecvMsg.begin(), it);
6473 return fOk;
6476 class CompareInvMempoolOrder
6478 CTxMemPool *mp;
6479 public:
6480 CompareInvMempoolOrder(CTxMemPool *_mempool)
6482 mp = _mempool;
6485 bool operator()(std::set<uint256>::iterator a, std::set<uint256>::iterator b)
6487 /* As std::make_heap produces a max-heap, we want the entries with the
6488 * fewest ancestors/highest fee to sort later. */
6489 return mp->CompareDepthAndScore(*b, *a);
6493 bool SendMessages(CNode* pto, CConnman& connman)
6495 const Consensus::Params& consensusParams = Params().GetConsensus();
6497 // Don't send anything until we get its version message
6498 if (pto->nVersion == 0)
6499 return true;
6502 // Message: ping
6504 bool pingSend = false;
6505 if (pto->fPingQueued) {
6506 // RPC ping request by user
6507 pingSend = true;
6509 if (pto->nPingNonceSent == 0 && pto->nPingUsecStart + PING_INTERVAL * 1000000 < GetTimeMicros()) {
6510 // Ping automatically sent as a latency probe & keepalive.
6511 pingSend = true;
6513 if (pingSend && !pto->fDisconnect) {
6514 uint64_t nonce = 0;
6515 while (nonce == 0) {
6516 GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
6518 pto->fPingQueued = false;
6519 pto->nPingUsecStart = GetTimeMicros();
6520 if (pto->nVersion > BIP0031_VERSION) {
6521 pto->nPingNonceSent = nonce;
6522 connman.PushMessage(pto, NetMsgType::PING, nonce);
6523 } else {
6524 // Peer is too old to support ping command with nonce, pong will never arrive.
6525 pto->nPingNonceSent = 0;
6526 connman.PushMessage(pto, NetMsgType::PING);
6530 TRY_LOCK(cs_main, lockMain); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
6531 if (!lockMain)
6532 return true;
6534 // Address refresh broadcast
6535 int64_t nNow = GetTimeMicros();
6536 if (!IsInitialBlockDownload() && pto->nNextLocalAddrSend < nNow) {
6537 AdvertiseLocal(pto);
6538 pto->nNextLocalAddrSend = PoissonNextSend(nNow, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
6542 // Message: addr
6544 if (pto->nNextAddrSend < nNow) {
6545 pto->nNextAddrSend = PoissonNextSend(nNow, AVG_ADDRESS_BROADCAST_INTERVAL);
6546 vector<CAddress> vAddr;
6547 vAddr.reserve(pto->vAddrToSend.size());
6548 BOOST_FOREACH(const CAddress& addr, pto->vAddrToSend)
6550 if (!pto->addrKnown.contains(addr.GetKey()))
6552 pto->addrKnown.insert(addr.GetKey());
6553 vAddr.push_back(addr);
6554 // receiver rejects addr messages larger than 1000
6555 if (vAddr.size() >= 1000)
6557 connman.PushMessage(pto, NetMsgType::ADDR, vAddr);
6558 vAddr.clear();
6562 pto->vAddrToSend.clear();
6563 if (!vAddr.empty())
6564 connman.PushMessage(pto, NetMsgType::ADDR, vAddr);
6565 // we only send the big addr message once
6566 if (pto->vAddrToSend.capacity() > 40)
6567 pto->vAddrToSend.shrink_to_fit();
6570 CNodeState &state = *State(pto->GetId());
6571 if (state.fShouldBan) {
6572 if (pto->fWhitelisted)
6573 LogPrintf("Warning: not punishing whitelisted peer %s!\n", pto->addr.ToString());
6574 else {
6575 pto->fDisconnect = true;
6576 if (pto->addr.IsLocal())
6577 LogPrintf("Warning: not banning local peer %s!\n", pto->addr.ToString());
6578 else
6580 connman.Ban(pto->addr, BanReasonNodeMisbehaving);
6583 state.fShouldBan = false;
6586 BOOST_FOREACH(const CBlockReject& reject, state.rejects)
6587 connman.PushMessage(pto, NetMsgType::REJECT, (string)NetMsgType::BLOCK, reject.chRejectCode, reject.strRejectReason, reject.hashBlock);
6588 state.rejects.clear();
6590 // Start block sync
6591 if (pindexBestHeader == NULL)
6592 pindexBestHeader = chainActive.Tip();
6593 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.
6594 if (!state.fSyncStarted && !pto->fClient && !pto->fDisconnect && !fImporting && !fReindex) {
6595 // Only actively request headers from a single peer, unless we're close to today.
6596 if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
6597 state.fSyncStarted = true;
6598 nSyncStarted++;
6599 const CBlockIndex *pindexStart = pindexBestHeader;
6600 /* If possible, start at the block preceding the currently
6601 best known header. This ensures that we always get a
6602 non-empty list of headers back as long as the peer
6603 is up-to-date. With a non-empty response, we can initialise
6604 the peer's known best block. This wouldn't be possible
6605 if we requested starting at pindexBestHeader and
6606 got back an empty response. */
6607 if (pindexStart->pprev)
6608 pindexStart = pindexStart->pprev;
6609 LogPrint("net", "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->id, pto->nStartingHeight);
6610 connman.PushMessage(pto, NetMsgType::GETHEADERS, chainActive.GetLocator(pindexStart), uint256());
6614 // Resend wallet transactions that haven't gotten in a block yet
6615 // Except during reindex, importing and IBD, when old wallet
6616 // transactions become unconfirmed and spams other nodes.
6617 if (!fReindex && !fImporting && !IsInitialBlockDownload())
6619 GetMainSignals().Broadcast(nTimeBestReceived, &connman);
6623 // Try sending block announcements via headers
6626 // If we have less than MAX_BLOCKS_TO_ANNOUNCE in our
6627 // list of block hashes we're relaying, and our peer wants
6628 // headers announcements, then find the first header
6629 // not yet known to our peer but would connect, and send.
6630 // If no header would connect, or if we have too many
6631 // blocks, or if the peer doesn't want headers, just
6632 // add all to the inv queue.
6633 LOCK(pto->cs_inventory);
6634 vector<CBlock> vHeaders;
6635 bool fRevertToInv = ((!state.fPreferHeaders &&
6636 (!state.fPreferHeaderAndIDs || pto->vBlockHashesToAnnounce.size() > 1)) ||
6637 pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
6638 CBlockIndex *pBestIndex = NULL; // last header queued for delivery
6639 ProcessBlockAvailability(pto->id); // ensure pindexBestKnownBlock is up-to-date
6641 if (!fRevertToInv) {
6642 bool fFoundStartingHeader = false;
6643 // Try to find first header that our peer doesn't have, and
6644 // then send all headers past that one. If we come across any
6645 // headers that aren't on chainActive, give up.
6646 BOOST_FOREACH(const uint256 &hash, pto->vBlockHashesToAnnounce) {
6647 BlockMap::iterator mi = mapBlockIndex.find(hash);
6648 assert(mi != mapBlockIndex.end());
6649 CBlockIndex *pindex = mi->second;
6650 if (chainActive[pindex->nHeight] != pindex) {
6651 // Bail out if we reorged away from this block
6652 fRevertToInv = true;
6653 break;
6655 if (pBestIndex != NULL && pindex->pprev != pBestIndex) {
6656 // This means that the list of blocks to announce don't
6657 // connect to each other.
6658 // This shouldn't really be possible to hit during
6659 // regular operation (because reorgs should take us to
6660 // a chain that has some block not on the prior chain,
6661 // which should be caught by the prior check), but one
6662 // way this could happen is by using invalidateblock /
6663 // reconsiderblock repeatedly on the tip, causing it to
6664 // be added multiple times to vBlockHashesToAnnounce.
6665 // Robustly deal with this rare situation by reverting
6666 // to an inv.
6667 fRevertToInv = true;
6668 break;
6670 pBestIndex = pindex;
6671 if (fFoundStartingHeader) {
6672 // add this to the headers message
6673 vHeaders.push_back(pindex->GetBlockHeader());
6674 } else if (PeerHasHeader(&state, pindex)) {
6675 continue; // keep looking for the first new block
6676 } else if (pindex->pprev == NULL || PeerHasHeader(&state, pindex->pprev)) {
6677 // Peer doesn't have this header but they do have the prior one.
6678 // Start sending headers.
6679 fFoundStartingHeader = true;
6680 vHeaders.push_back(pindex->GetBlockHeader());
6681 } else {
6682 // Peer doesn't have this header or the prior one -- nothing will
6683 // connect, so bail out.
6684 fRevertToInv = true;
6685 break;
6689 if (!fRevertToInv && !vHeaders.empty()) {
6690 if (vHeaders.size() == 1 && state.fPreferHeaderAndIDs) {
6691 // We only send up to 1 block as header-and-ids, as otherwise
6692 // probably means we're doing an initial-ish-sync or they're slow
6693 LogPrint("net", "%s sending header-and-ids %s to peer %d\n", __func__,
6694 vHeaders.front().GetHash().ToString(), pto->id);
6695 //TODO: Shouldn't need to reload block from disk, but requires refactor
6696 CBlock block;
6697 assert(ReadBlockFromDisk(block, pBestIndex, consensusParams));
6698 CBlockHeaderAndShortTxIDs cmpctblock(block, state.fWantsCmpctWitness);
6699 connman.PushMessageWithFlag(pto, state.fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::CMPCTBLOCK, cmpctblock);
6700 state.pindexBestHeaderSent = pBestIndex;
6701 } else if (state.fPreferHeaders) {
6702 if (vHeaders.size() > 1) {
6703 LogPrint("net", "%s: %u headers, range (%s, %s), to peer=%d\n", __func__,
6704 vHeaders.size(),
6705 vHeaders.front().GetHash().ToString(),
6706 vHeaders.back().GetHash().ToString(), pto->id);
6707 } else {
6708 LogPrint("net", "%s: sending header %s to peer=%d\n", __func__,
6709 vHeaders.front().GetHash().ToString(), pto->id);
6711 connman.PushMessage(pto, NetMsgType::HEADERS, vHeaders);
6712 state.pindexBestHeaderSent = pBestIndex;
6713 } else
6714 fRevertToInv = true;
6716 if (fRevertToInv) {
6717 // If falling back to using an inv, just try to inv the tip.
6718 // The last entry in vBlockHashesToAnnounce was our tip at some point
6719 // in the past.
6720 if (!pto->vBlockHashesToAnnounce.empty()) {
6721 const uint256 &hashToAnnounce = pto->vBlockHashesToAnnounce.back();
6722 BlockMap::iterator mi = mapBlockIndex.find(hashToAnnounce);
6723 assert(mi != mapBlockIndex.end());
6724 CBlockIndex *pindex = mi->second;
6726 // Warn if we're announcing a block that is not on the main chain.
6727 // This should be very rare and could be optimized out.
6728 // Just log for now.
6729 if (chainActive[pindex->nHeight] != pindex) {
6730 LogPrint("net", "Announcing block %s not on main chain (tip=%s)\n",
6731 hashToAnnounce.ToString(), chainActive.Tip()->GetBlockHash().ToString());
6734 // If the peer's chain has this block, don't inv it back.
6735 if (!PeerHasHeader(&state, pindex)) {
6736 pto->PushInventory(CInv(MSG_BLOCK, hashToAnnounce));
6737 LogPrint("net", "%s: sending inv peer=%d hash=%s\n", __func__,
6738 pto->id, hashToAnnounce.ToString());
6742 pto->vBlockHashesToAnnounce.clear();
6746 // Message: inventory
6748 vector<CInv> vInv;
6750 LOCK(pto->cs_inventory);
6751 vInv.reserve(std::max<size_t>(pto->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX));
6753 // Add blocks
6754 BOOST_FOREACH(const uint256& hash, pto->vInventoryBlockToSend) {
6755 vInv.push_back(CInv(MSG_BLOCK, hash));
6756 if (vInv.size() == MAX_INV_SZ) {
6757 connman.PushMessage(pto, NetMsgType::INV, vInv);
6758 vInv.clear();
6761 pto->vInventoryBlockToSend.clear();
6763 // Check whether periodic sends should happen
6764 bool fSendTrickle = pto->fWhitelisted;
6765 if (pto->nNextInvSend < nNow) {
6766 fSendTrickle = true;
6767 // Use half the delay for outbound peers, as there is less privacy concern for them.
6768 pto->nNextInvSend = PoissonNextSend(nNow, INVENTORY_BROADCAST_INTERVAL >> !pto->fInbound);
6771 // Time to send but the peer has requested we not relay transactions.
6772 if (fSendTrickle) {
6773 LOCK(pto->cs_filter);
6774 if (!pto->fRelayTxes) pto->setInventoryTxToSend.clear();
6777 // Respond to BIP35 mempool requests
6778 if (fSendTrickle && pto->fSendMempool) {
6779 auto vtxinfo = mempool.infoAll();
6780 pto->fSendMempool = false;
6781 CAmount filterrate = 0;
6783 LOCK(pto->cs_feeFilter);
6784 filterrate = pto->minFeeFilter;
6787 LOCK(pto->cs_filter);
6789 for (const auto& txinfo : vtxinfo) {
6790 const uint256& hash = txinfo.tx->GetHash();
6791 CInv inv(MSG_TX, hash);
6792 pto->setInventoryTxToSend.erase(hash);
6793 if (filterrate) {
6794 if (txinfo.feeRate.GetFeePerK() < filterrate)
6795 continue;
6797 if (pto->pfilter) {
6798 if (!pto->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
6800 pto->filterInventoryKnown.insert(hash);
6801 vInv.push_back(inv);
6802 if (vInv.size() == MAX_INV_SZ) {
6803 connman.PushMessage(pto, NetMsgType::INV, vInv);
6804 vInv.clear();
6807 pto->timeLastMempoolReq = GetTime();
6810 // Determine transactions to relay
6811 if (fSendTrickle) {
6812 // Produce a vector with all candidates for sending
6813 vector<std::set<uint256>::iterator> vInvTx;
6814 vInvTx.reserve(pto->setInventoryTxToSend.size());
6815 for (std::set<uint256>::iterator it = pto->setInventoryTxToSend.begin(); it != pto->setInventoryTxToSend.end(); it++) {
6816 vInvTx.push_back(it);
6818 CAmount filterrate = 0;
6820 LOCK(pto->cs_feeFilter);
6821 filterrate = pto->minFeeFilter;
6823 // Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
6824 // A heap is used so that not all items need sorting if only a few are being sent.
6825 CompareInvMempoolOrder compareInvMempoolOrder(&mempool);
6826 std::make_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
6827 // No reason to drain out at many times the network's capacity,
6828 // especially since we have many peers and some will draw much shorter delays.
6829 unsigned int nRelayedTransactions = 0;
6830 LOCK(pto->cs_filter);
6831 while (!vInvTx.empty() && nRelayedTransactions < INVENTORY_BROADCAST_MAX) {
6832 // Fetch the top element from the heap
6833 std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
6834 std::set<uint256>::iterator it = vInvTx.back();
6835 vInvTx.pop_back();
6836 uint256 hash = *it;
6837 // Remove it from the to-be-sent set
6838 pto->setInventoryTxToSend.erase(it);
6839 // Check if not in the filter already
6840 if (pto->filterInventoryKnown.contains(hash)) {
6841 continue;
6843 // Not in the mempool anymore? don't bother sending it.
6844 auto txinfo = mempool.info(hash);
6845 if (!txinfo.tx) {
6846 continue;
6848 if (filterrate && txinfo.feeRate.GetFeePerK() < filterrate) {
6849 continue;
6851 if (pto->pfilter && !pto->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
6852 // Send
6853 vInv.push_back(CInv(MSG_TX, hash));
6854 nRelayedTransactions++;
6856 // Expire old relay messages
6857 while (!vRelayExpiration.empty() && vRelayExpiration.front().first < nNow)
6859 mapRelay.erase(vRelayExpiration.front().second);
6860 vRelayExpiration.pop_front();
6863 auto ret = mapRelay.insert(std::make_pair(hash, std::move(txinfo.tx)));
6864 if (ret.second) {
6865 vRelayExpiration.push_back(std::make_pair(nNow + 15 * 60 * 1000000, ret.first));
6868 if (vInv.size() == MAX_INV_SZ) {
6869 connman.PushMessage(pto, NetMsgType::INV, vInv);
6870 vInv.clear();
6872 pto->filterInventoryKnown.insert(hash);
6876 if (!vInv.empty())
6877 connman.PushMessage(pto, NetMsgType::INV, vInv);
6879 // Detect whether we're stalling
6880 nNow = GetTimeMicros();
6881 if (!pto->fDisconnect && state.nStallingSince && state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) {
6882 // Stalling only triggers when the block download window cannot move. During normal steady state,
6883 // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
6884 // should only happen during initial block download.
6885 LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->id);
6886 pto->fDisconnect = true;
6888 // In case there is a block that has been in flight from this peer for 2 + 0.5 * N times the block interval
6889 // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
6890 // We compensate for other peers to prevent killing off peers due to our own downstream link
6891 // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
6892 // to unreasonably increase our timeout.
6893 if (!pto->fDisconnect && state.vBlocksInFlight.size() > 0) {
6894 QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
6895 int nOtherPeersWithValidatedDownloads = nPeersWithValidatedDownloads - (state.nBlocksInFlightValidHeaders > 0);
6896 if (nNow > state.nDownloadingSince + consensusParams.nPowTargetSpacing * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
6897 LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.hash.ToString(), pto->id);
6898 pto->fDisconnect = true;
6903 // Message: getdata (blocks)
6905 vector<CInv> vGetData;
6906 if (!pto->fDisconnect && !pto->fClient && (fFetch || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
6907 vector<CBlockIndex*> vToDownload;
6908 NodeId staller = -1;
6909 FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller, consensusParams);
6910 BOOST_FOREACH(CBlockIndex *pindex, vToDownload) {
6911 uint32_t nFetchFlags = GetFetchFlags(pto, pindex->pprev, consensusParams);
6912 vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
6913 MarkBlockAsInFlight(pto->GetId(), pindex->GetBlockHash(), consensusParams, pindex);
6914 LogPrint("net", "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
6915 pindex->nHeight, pto->id);
6917 if (state.nBlocksInFlight == 0 && staller != -1) {
6918 if (State(staller)->nStallingSince == 0) {
6919 State(staller)->nStallingSince = nNow;
6920 LogPrint("net", "Stall started peer=%d\n", staller);
6926 // Message: getdata (non-blocks)
6928 while (!pto->fDisconnect && !pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
6930 const CInv& inv = (*pto->mapAskFor.begin()).second;
6931 if (!AlreadyHave(inv))
6933 if (fDebug)
6934 LogPrint("net", "Requesting %s peer=%d\n", inv.ToString(), pto->id);
6935 vGetData.push_back(inv);
6936 if (vGetData.size() >= 1000)
6938 connman.PushMessage(pto, NetMsgType::GETDATA, vGetData);
6939 vGetData.clear();
6941 } else {
6942 //If we're not going to ask, don't expect a response.
6943 pto->setAskFor.erase(inv.hash);
6945 pto->mapAskFor.erase(pto->mapAskFor.begin());
6947 if (!vGetData.empty())
6948 connman.PushMessage(pto, NetMsgType::GETDATA, vGetData);
6951 // Message: feefilter
6953 // We don't want white listed peers to filter txs to us if we have -whitelistforcerelay
6954 if (pto->nVersion >= FEEFILTER_VERSION && GetBoolArg("-feefilter", DEFAULT_FEEFILTER) &&
6955 !(pto->fWhitelisted && GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY))) {
6956 CAmount currentFilter = mempool.GetMinFee(GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFeePerK();
6957 int64_t timeNow = GetTimeMicros();
6958 if (timeNow > pto->nextSendTimeFeeFilter) {
6959 CAmount filterToSend = filterRounder.round(currentFilter);
6960 if (filterToSend != pto->lastSentFeeFilter) {
6961 connman.PushMessage(pto, NetMsgType::FEEFILTER, filterToSend);
6962 pto->lastSentFeeFilter = filterToSend;
6964 pto->nextSendTimeFeeFilter = PoissonNextSend(timeNow, AVG_FEEFILTER_BROADCAST_INTERVAL);
6966 // If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
6967 // until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
6968 else if (timeNow + MAX_FEEFILTER_CHANGE_DELAY * 1000000 < pto->nextSendTimeFeeFilter &&
6969 (currentFilter < 3 * pto->lastSentFeeFilter / 4 || currentFilter > 4 * pto->lastSentFeeFilter / 3)) {
6970 pto->nextSendTimeFeeFilter = timeNow + GetRandInt(MAX_FEEFILTER_CHANGE_DELAY) * 1000000;
6974 return true;
6977 std::string CBlockFileInfo::ToString() const {
6978 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));
6981 ThresholdState VersionBitsTipState(const Consensus::Params& params, Consensus::DeploymentPos pos)
6983 LOCK(cs_main);
6984 return VersionBitsState(chainActive.Tip(), params, pos, versionbitscache);
6987 int VersionBitsTipStateSinceHeight(const Consensus::Params& params, Consensus::DeploymentPos pos)
6989 LOCK(cs_main);
6990 return VersionBitsStateSinceHeight(chainActive.Tip(), params, pos, versionbitscache);
6993 static const uint64_t MEMPOOL_DUMP_VERSION = 1;
6995 bool LoadMempool(void)
6997 int64_t nExpiryTimeout = GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60;
6998 FILE* filestr = fopen((GetDataDir() / "mempool.dat").string().c_str(), "r");
6999 CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
7000 if (file.IsNull()) {
7001 LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
7002 return false;
7005 int64_t count = 0;
7006 int64_t skipped = 0;
7007 int64_t failed = 0;
7008 int64_t nNow = GetTime();
7010 try {
7011 uint64_t version;
7012 file >> version;
7013 if (version != MEMPOOL_DUMP_VERSION) {
7014 return false;
7016 uint64_t num;
7017 file >> num;
7018 double prioritydummy = 0;
7019 while (num--) {
7020 CTransaction tx;
7021 int64_t nTime;
7022 int64_t nFeeDelta;
7023 file >> tx;
7024 file >> nTime;
7025 file >> nFeeDelta;
7027 CAmount amountdelta = nFeeDelta;
7028 if (amountdelta) {
7029 mempool.PrioritiseTransaction(tx.GetHash(), tx.GetHash().ToString(), prioritydummy, amountdelta);
7031 CValidationState state;
7032 if (nTime + nExpiryTimeout > nNow) {
7033 LOCK(cs_main);
7034 AcceptToMemoryPoolWithTime(mempool, state, tx, true, NULL, nTime);
7035 if (state.IsValid()) {
7036 ++count;
7037 } else {
7038 ++failed;
7040 } else {
7041 ++skipped;
7044 std::map<uint256, CAmount> mapDeltas;
7045 file >> mapDeltas;
7047 for (const auto& i : mapDeltas) {
7048 mempool.PrioritiseTransaction(i.first, i.first.ToString(), prioritydummy, i.second);
7050 } catch (const std::exception& e) {
7051 LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
7052 return false;
7055 LogPrintf("Imported mempool transactions from disk: %i successes, %i failed, %i expired\n", count, failed, skipped);
7056 return true;
7059 void DumpMempool(void)
7061 int64_t start = GetTimeMicros();
7063 std::map<uint256, CAmount> mapDeltas;
7064 std::vector<TxMempoolInfo> vinfo;
7067 LOCK(mempool.cs);
7068 for (const auto &i : mempool.mapDeltas) {
7069 mapDeltas[i.first] = i.second.first;
7071 vinfo = mempool.infoAll();
7074 int64_t mid = GetTimeMicros();
7076 try {
7077 FILE* filestr = fopen((GetDataDir() / "mempool.dat.new").string().c_str(), "w");
7078 if (!filestr) {
7079 return;
7082 CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
7084 uint64_t version = MEMPOOL_DUMP_VERSION;
7085 file << version;
7087 file << (uint64_t)vinfo.size();
7088 for (const auto& i : vinfo) {
7089 file << *(i.tx);
7090 file << (int64_t)i.nTime;
7091 file << (int64_t)i.nFeeDelta;
7092 mapDeltas.erase(i.tx->GetHash());
7095 file << mapDeltas;
7096 FileCommit(file.Get());
7097 file.fclose();
7098 RenameOver(GetDataDir() / "mempool.dat.new", GetDataDir() / "mempool.dat");
7099 int64_t last = GetTimeMicros();
7100 LogPrintf("Dumped mempool: %gs to copy, %gs to dump\n", (mid-start)*0.000001, (last-mid)*0.000001);
7101 } catch (const std::exception& e) {
7102 LogPrintf("Failed to dump mempool: %s. Continuing anyway.\n", e.what());
7106 class CMainCleanup
7108 public:
7109 CMainCleanup() {}
7110 ~CMainCleanup() {
7111 // block headers
7112 BlockMap::iterator it1 = mapBlockIndex.begin();
7113 for (; it1 != mapBlockIndex.end(); it1++)
7114 delete (*it1).second;
7115 mapBlockIndex.clear();
7117 // orphan transactions
7118 mapOrphanTransactions.clear();
7119 mapOrphanTransactionsByPrev.clear();
7121 } instance_of_cmaincleanup;