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.
5 
6 #include "main.h"
7 
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 "policy/fees.h"
22 #include "policy/policy.h"
23 #include "pow.h"
24 #include "primitives/block.h"
25 #include "primitives/transaction.h"
26 #include "random.h"
27 #include "script/script.h"
28 #include "script/sigcache.h"
29 #include "script/standard.h"
30 #include "tinyformat.h"
31 #include "txdb.h"
32 #include "txmempool.h"
33 #include "ui_interface.h"
34 #include "undo.h"
35 #include "util.h"
36 #include "utilmoneystr.h"
37 #include "utilstrencodings.h"
38 #include "validationinterface.h"
39 #include "versionbits.h"
40 
41 #include <atomic>
42 #include <sstream>
43 
44 #include <boost/algorithm/string/replace.hpp>
45 #include <boost/algorithm/string/join.hpp>
46 #include <boost/filesystem.hpp>
47 #include <boost/filesystem/fstream.hpp>
48 #include <boost/math/distributions/poisson.hpp>
49 #include <boost/thread.hpp>
50 
51 using namespace std;
52 
53 #if defined(NDEBUG)
54 # error "Zetacoin cannot be compiled without assertions."
55 #endif
56 
57 /**
58  * Global state
59  */
60 
61 CCriticalSection cs_main;
62 
63 BlockMap mapBlockIndex;
64 CChain chainActive;
65 CBlockIndex *pindexBestHeader = NULL;
66 int64_t nTimeBestReceived = 0;
67 CWaitableCriticalSection csBestBlock;
68 CConditionVariable cvBlockChange;
69 int nScriptCheckThreads = 0;
70 bool fImporting = false;
71 bool fReindex = false;
72 bool fTxIndex = false;
73 bool fHavePruned = false;
74 bool fPruneMode = false;
75 bool fIsBareMultisigStd = DEFAULT_PERMIT_BAREMULTISIG;
76 bool fRequireStandard = true;
77 bool fCheckBlockIndex = false;
78 bool fCheckpointsEnabled = DEFAULT_CHECKPOINTS_ENABLED;
79 size_t nCoinCacheUsage = 5000 * 300;
80 uint64_t nPruneTarget = 0;
81 int64_t nMaxTipAge = DEFAULT_MAX_TIP_AGE;
82 bool fEnableReplacement = DEFAULT_ENABLE_REPLACEMENT;
83 
84 
85 CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
86 CAmount maxTxFee = DEFAULT_TRANSACTION_MAXFEE;
87 
88 CTxMemPool mempool(::minRelayTxFee);
89 FeeFilterRounder filterRounder(::minRelayTxFee);
90 
91 struct IteratorComparator
92 {
93     template<typename I>
operator ()IteratorComparator94     bool operator()(const I& a, const I& b)
95     {
96         return &(*a) < &(*b);
97     }
98 };
99 
100 struct COrphanTx {
101     CTransaction tx;
102     NodeId fromPeer;
103     int64_t nTimeExpire;
104 };
105 map<uint256, COrphanTx> mapOrphanTransactions GUARDED_BY(cs_main);
106 map<COutPoint, set<map<uint256, COrphanTx>::iterator, IteratorComparator>> mapOrphanTransactionsByPrev GUARDED_BY(cs_main);
107 void EraseOrphansFor(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
108 
109 /**
110  * Returns true if there are nRequired or more blocks of minVersion or above
111  * in the last Consensus::Params::nMajorityWindow blocks, starting at pstart and going backwards.
112  */
113 static void CheckBlockIndex(const Consensus::Params& consensusParams);
114 
115 /** Constant stuff for coinbase transactions we create: */
116 CScript COINBASE_FLAGS;
117 
118 const string strMessageMagic = "Zetacoin Signed Message:\n";
119 
120 // Internal stuff
121 namespace {
122 
123     struct CBlockIndexWorkComparator
124     {
operator ()__anonac908fb30111::CBlockIndexWorkComparator125         bool operator()(CBlockIndex *pa, CBlockIndex *pb) const {
126             // First sort by most total work, ...
127             if (pa->nChainWork > pb->nChainWork) return false;
128             if (pa->nChainWork < pb->nChainWork) return true;
129 
130             // ... then by earliest time received, ...
131             if (pa->nSequenceId < pb->nSequenceId) return false;
132             if (pa->nSequenceId > pb->nSequenceId) return true;
133 
134             // Use pointer address as tie breaker (should only happen with blocks
135             // loaded from disk, as those all have id 0).
136             if (pa < pb) return false;
137             if (pa > pb) return true;
138 
139             // Identical blocks.
140             return false;
141         }
142     };
143 
144     CBlockIndex *pindexBestInvalid;
145 
146     /**
147      * The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS (for itself and all ancestors) and
148      * as good as our current tip or better. Entries may be failed, though, and pruning nodes may be
149      * missing the data for the block.
150      */
151     set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexCandidates;
152     /** Number of nodes with fSyncStarted. */
153     int nSyncStarted = 0;
154     /** All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
155      * Pruned nodes may have entries where B is missing data.
156      */
157     multimap<CBlockIndex*, CBlockIndex*> mapBlocksUnlinked;
158 
159     CCriticalSection cs_LastBlockFile;
160     std::vector<CBlockFileInfo> vinfoBlockFile;
161     int nLastBlockFile = 0;
162     /** Global flag to indicate we should check to see if there are
163      *  block/undo files that should be deleted.  Set on startup
164      *  or if we allocate more file space when we're in prune mode
165      */
166     bool fCheckForPruning = false;
167 
168     /**
169      * Every received block is assigned a unique and increasing identifier, so we
170      * know which one to give priority in case of a fork.
171      */
172     CCriticalSection cs_nBlockSequenceId;
173     /** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
174     uint32_t nBlockSequenceId = 1;
175 
176     /**
177      * Sources of received blocks, saved to be able to send them reject
178      * messages or ban them when processing happens afterwards. Protected by
179      * cs_main.
180      * Set mapBlockSource[hash].second to false if the node should not be
181      * punished if the block is invalid.
182      */
183     map<uint256, std::pair<NodeId, bool>> mapBlockSource;
184 
185     /**
186      * Filter for transactions that were recently rejected by
187      * AcceptToMemoryPool. These are not rerequested until the chain tip
188      * changes, at which point the entire filter is reset. Protected by
189      * cs_main.
190      *
191      * Without this filter we'd be re-requesting txs from each of our peers,
192      * increasing bandwidth consumption considerably. For instance, with 100
193      * peers, half of which relay a tx we don't accept, that might be a 50x
194      * bandwidth increase. A flooding attacker attempting to roll-over the
195      * filter using minimum-sized, 60byte, transactions might manage to send
196      * 1000/sec if we have fast peers, so we pick 120,000 to give our peers a
197      * two minute window to send invs to us.
198      *
199      * Decreasing the false positive rate is fairly cheap, so we pick one in a
200      * million to make it highly unlikely for users to have issues with this
201      * filter.
202      *
203      * Memory used: 1.3 MB
204      */
205     boost::scoped_ptr<CRollingBloomFilter> recentRejects;
206     uint256 hashRecentRejectsChainTip;
207 
208     /** Blocks that are in flight, and that are in the queue to be downloaded. Protected by cs_main. */
209     struct QueuedBlock {
210         uint256 hash;
211         CBlockIndex* pindex;                                     //!< Optional.
212         bool fValidatedHeaders;                                  //!< Whether this block has validated headers at the time of request.
213         std::unique_ptr<PartiallyDownloadedBlock> partialBlock;  //!< Optional, used for CMPCTBLOCK downloads
214     };
215     map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
216 
217     /** Stack of nodes which we have set to announce using compact blocks */
218     list<NodeId> lNodesAnnouncingHeaderAndIDs;
219 
220     /** Number of preferable block download peers. */
221     int nPreferredDownload = 0;
222 
223     /** Dirty block index entries. */
224     set<CBlockIndex*> setDirtyBlockIndex;
225 
226     /** Dirty block file entries. */
227     set<int> setDirtyFileInfo;
228 
229     /** Number of peers from which we're downloading blocks. */
230     int nPeersWithValidatedDownloads = 0;
231 
232     /** Relay map, protected by cs_main. */
233     typedef std::map<uint256, std::shared_ptr<const CTransaction>> MapRelay;
234     MapRelay mapRelay;
235     /** Expiration-time ordered list of (expire time, relay map entry) pairs, protected by cs_main). */
236     std::deque<std::pair<int64_t, MapRelay::iterator>> vRelayExpiration;
237 } // anon namespace
238 
239 //////////////////////////////////////////////////////////////////////////////
240 //
241 // Registration of network node signals.
242 //
243 
244 namespace {
245 
246 struct CBlockReject {
247     unsigned char chRejectCode;
248     string strRejectReason;
249     uint256 hashBlock;
250 };
251 
252 /**
253  * Maintain validation-specific state about nodes, protected by cs_main, instead
254  * by CNode's own locks. This simplifies asynchronous operation, where
255  * processing of incoming data is done after the ProcessMessage call returns,
256  * and we're no longer holding the node's locks.
257  */
258 struct CNodeState {
259     //! The peer's address
260     CService address;
261     //! Whether we have a fully established connection.
262     bool fCurrentlyConnected;
263     //! Accumulated misbehaviour score for this peer.
264     int nMisbehavior;
265     //! Whether this peer should be disconnected and banned (unless whitelisted).
266     bool fShouldBan;
267     //! String name of this peer (debugging/logging purposes).
268     std::string name;
269     //! List of asynchronously-determined block rejections to notify this peer about.
270     std::vector<CBlockReject> rejects;
271     //! The best known block we know this peer has announced.
272     CBlockIndex *pindexBestKnownBlock;
273     //! The hash of the last unknown block this peer has announced.
274     uint256 hashLastUnknownBlock;
275     //! The last full block we both have.
276     CBlockIndex *pindexLastCommonBlock;
277     //! The best header we have sent our peer.
278     CBlockIndex *pindexBestHeaderSent;
279     //! Length of current-streak of unconnecting headers announcements
280     int nUnconnectingHeaders;
281     //! Whether we've started headers synchronization with this peer.
282     bool fSyncStarted;
283     //! Since when we're stalling block download progress (in microseconds), or 0.
284     int64_t nStallingSince;
285     list<QueuedBlock> vBlocksInFlight;
286     //! When the first entry in vBlocksInFlight started downloading. Don't care when vBlocksInFlight is empty.
287     int64_t nDownloadingSince;
288     int nBlocksInFlight;
289     int nBlocksInFlightValidHeaders;
290     //! Whether we consider this a preferred download peer.
291     bool fPreferredDownload;
292     //! Whether this peer wants invs or headers (when possible) for block announcements.
293     bool fPreferHeaders;
294     //! Whether this peer wants invs or cmpctblocks (when possible) for block announcements.
295     bool fPreferHeaderAndIDs;
296     /**
297       * Whether this peer will send us cmpctblocks if we request them.
298       * This is not used to gate request logic, as we really only care about fSupportsDesiredCmpctVersion,
299       * but is used as a flag to "lock in" the version of compact blocks (fWantsCmpctWitness) we send.
300       */
301     bool fProvidesHeaderAndIDs;
302     //! Whether this peer can give us witnesses
303     bool fHaveWitness;
304     //! Whether this peer wants witnesses in cmpctblocks/blocktxns
305     bool fWantsCmpctWitness;
306     /**
307      * If we've announced NODE_WITNESS to this peer: whether the peer sends witnesses in cmpctblocks/blocktxns,
308      * otherwise: whether this peer sends non-witnesses in cmpctblocks/blocktxns.
309      */
310     bool fSupportsDesiredCmpctVersion;
311 
CNodeState__anonac908fb30211::CNodeState312     CNodeState() {
313         fCurrentlyConnected = false;
314         nMisbehavior = 0;
315         fShouldBan = false;
316         pindexBestKnownBlock = NULL;
317         hashLastUnknownBlock.SetNull();
318         pindexLastCommonBlock = NULL;
319         pindexBestHeaderSent = NULL;
320         nUnconnectingHeaders = 0;
321         fSyncStarted = false;
322         nStallingSince = 0;
323         nDownloadingSince = 0;
324         nBlocksInFlight = 0;
325         nBlocksInFlightValidHeaders = 0;
326         fPreferredDownload = false;
327         fPreferHeaders = false;
328         fPreferHeaderAndIDs = false;
329         fProvidesHeaderAndIDs = false;
330         fHaveWitness = false;
331         fWantsCmpctWitness = false;
332         fSupportsDesiredCmpctVersion = false;
333     }
334 };
335 
336 /** Map maintaining per-node state. Requires cs_main. */
337 map<NodeId, CNodeState> mapNodeState;
338 
339 // Requires cs_main.
State(NodeId pnode)340 CNodeState *State(NodeId pnode) {
341     map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
342     if (it == mapNodeState.end())
343         return NULL;
344     return &it->second;
345 }
346 
GetHeight()347 int GetHeight()
348 {
349     LOCK(cs_main);
350     return chainActive.Height();
351 }
352 
UpdatePreferredDownload(CNode * node,CNodeState * state)353 void UpdatePreferredDownload(CNode* node, CNodeState* state)
354 {
355     nPreferredDownload -= state->fPreferredDownload;
356 
357     // Whether this node should be marked as a preferred download node.
358     state->fPreferredDownload = (!node->fInbound || node->fWhitelisted) && !node->fOneShot && !node->fClient;
359 
360     nPreferredDownload += state->fPreferredDownload;
361 }
362 
InitializeNode(NodeId nodeid,const CNode * pnode)363 void InitializeNode(NodeId nodeid, const CNode *pnode) {
364     LOCK(cs_main);
365     CNodeState &state = mapNodeState.insert(std::make_pair(nodeid, CNodeState())).first->second;
366     state.name = pnode->addrName;
367     state.address = pnode->addr;
368 }
369 
FinalizeNode(NodeId nodeid)370 void FinalizeNode(NodeId nodeid) {
371     LOCK(cs_main);
372     CNodeState *state = State(nodeid);
373 
374     if (state->fSyncStarted)
375         nSyncStarted--;
376 
377     if (state->nMisbehavior == 0 && state->fCurrentlyConnected) {
378         AddressCurrentlyConnected(state->address);
379     }
380 
381     BOOST_FOREACH(const QueuedBlock& entry, state->vBlocksInFlight) {
382         mapBlocksInFlight.erase(entry.hash);
383     }
384     EraseOrphansFor(nodeid);
385     nPreferredDownload -= state->fPreferredDownload;
386     nPeersWithValidatedDownloads -= (state->nBlocksInFlightValidHeaders != 0);
387     assert(nPeersWithValidatedDownloads >= 0);
388 
389     mapNodeState.erase(nodeid);
390 
391     if (mapNodeState.empty()) {
392         // Do a consistency check after the last peer is removed.
393         assert(mapBlocksInFlight.empty());
394         assert(nPreferredDownload == 0);
395         assert(nPeersWithValidatedDownloads == 0);
396     }
397 }
398 
399 // Requires cs_main.
400 // Returns a bool indicating whether we requested this block.
401 // Also used if a block was /not/ received and timed out or started with another peer
MarkBlockAsReceived(const uint256 & hash)402 bool MarkBlockAsReceived(const uint256& hash) {
403     map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
404     if (itInFlight != mapBlocksInFlight.end()) {
405         CNodeState *state = State(itInFlight->second.first);
406         state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders;
407         if (state->nBlocksInFlightValidHeaders == 0 && itInFlight->second.second->fValidatedHeaders) {
408             // Last validated block on the queue was received.
409             nPeersWithValidatedDownloads--;
410         }
411         if (state->vBlocksInFlight.begin() == itInFlight->second.second) {
412             // First block on the queue was received, update the start download time for the next one
413             state->nDownloadingSince = std::max(state->nDownloadingSince, GetTimeMicros());
414         }
415         state->vBlocksInFlight.erase(itInFlight->second.second);
416         state->nBlocksInFlight--;
417         state->nStallingSince = 0;
418         mapBlocksInFlight.erase(itInFlight);
419         return true;
420     }
421     return false;
422 }
423 
424 // Requires cs_main.
425 // returns false, still setting pit, if the block was already in flight from the same peer
426 // pit will only be valid as long as the same cs_main lock is being held
MarkBlockAsInFlight(NodeId nodeid,const uint256 & hash,const Consensus::Params & consensusParams,CBlockIndex * pindex=NULL,list<QueuedBlock>::iterator ** pit=NULL)427 bool MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const Consensus::Params& consensusParams, CBlockIndex *pindex = NULL, list<QueuedBlock>::iterator **pit = NULL) {
428     CNodeState *state = State(nodeid);
429     assert(state != NULL);
430 
431     // Short-circuit most stuff in case its from the same node
432     map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
433     if (itInFlight != mapBlocksInFlight.end() && itInFlight->second.first == nodeid) {
434         *pit = &itInFlight->second.second;
435         return false;
436     }
437 
438     // Make sure it's not listed somewhere already.
439     MarkBlockAsReceived(hash);
440 
441     list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(),
442             {hash, pindex, pindex != NULL, std::unique_ptr<PartiallyDownloadedBlock>(pit ? new PartiallyDownloadedBlock(&mempool) : NULL)});
443     state->nBlocksInFlight++;
444     state->nBlocksInFlightValidHeaders += it->fValidatedHeaders;
445     if (state->nBlocksInFlight == 1) {
446         // We're starting a block download (batch) from this peer.
447         state->nDownloadingSince = GetTimeMicros();
448     }
449     if (state->nBlocksInFlightValidHeaders == 1 && pindex != NULL) {
450         nPeersWithValidatedDownloads++;
451     }
452     itInFlight = mapBlocksInFlight.insert(std::make_pair(hash, std::make_pair(nodeid, it))).first;
453     if (pit)
454         *pit = &itInFlight->second.second;
455     return true;
456 }
457 
458 /** Check whether the last unknown block a peer advertised is not yet known. */
ProcessBlockAvailability(NodeId nodeid)459 void ProcessBlockAvailability(NodeId nodeid) {
460     CNodeState *state = State(nodeid);
461     assert(state != NULL);
462 
463     if (!state->hashLastUnknownBlock.IsNull()) {
464         BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
465         if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0) {
466             if (state->pindexBestKnownBlock == NULL || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
467                 state->pindexBestKnownBlock = itOld->second;
468             state->hashLastUnknownBlock.SetNull();
469         }
470     }
471 }
472 
473 /** Update tracking information about which blocks a peer is assumed to have. */
UpdateBlockAvailability(NodeId nodeid,const uint256 & hash)474 void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
475     CNodeState *state = State(nodeid);
476     assert(state != NULL);
477 
478     ProcessBlockAvailability(nodeid);
479 
480     BlockMap::iterator it = mapBlockIndex.find(hash);
481     if (it != mapBlockIndex.end() && it->second->nChainWork > 0) {
482         // An actually better block was announced.
483         if (state->pindexBestKnownBlock == NULL || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
484             state->pindexBestKnownBlock = it->second;
485     } else {
486         // An unknown block was announced; just assume that the latest one is the best one.
487         state->hashLastUnknownBlock = hash;
488     }
489 }
490 
MaybeSetPeerAsAnnouncingHeaderAndIDs(const CNodeState * nodestate,CNode * pfrom)491 void MaybeSetPeerAsAnnouncingHeaderAndIDs(const CNodeState* nodestate, CNode* pfrom) {
492     if (!nodestate->fSupportsDesiredCmpctVersion) {
493         // Never ask from peers who can't provide witnesses.
494         return;
495     }
496     if (nodestate->fProvidesHeaderAndIDs) {
497         for (std::list<NodeId>::iterator it = lNodesAnnouncingHeaderAndIDs.begin(); it != lNodesAnnouncingHeaderAndIDs.end(); it++) {
498             if (*it == pfrom->GetId()) {
499                 lNodesAnnouncingHeaderAndIDs.erase(it);
500                 lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
501                 return;
502             }
503         }
504         bool fAnnounceUsingCMPCTBLOCK = false;
505         uint64_t nCMPCTBLOCKVersion = (nLocalServices & NODE_WITNESS) ? 2 : 1;
506         if (lNodesAnnouncingHeaderAndIDs.size() >= 3) {
507             // As per BIP152, we only get 3 of our peers to announce
508             // blocks using compact encodings.
509             CNode* pnodeStop = FindNode(lNodesAnnouncingHeaderAndIDs.front());
510             if (pnodeStop) {
511                 pnodeStop->PushMessage(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion);
512             }
513             lNodesAnnouncingHeaderAndIDs.pop_front();
514         }
515         fAnnounceUsingCMPCTBLOCK = true;
516         pfrom->PushMessage(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion);
517         lNodesAnnouncingHeaderAndIDs.push_back(pfrom->GetId());
518     }
519 }
520 
521 // Requires cs_main
CanDirectFetch(const Consensus::Params & consensusParams)522 bool CanDirectFetch(const Consensus::Params &consensusParams)
523 {
524     return chainActive.Tip()->GetBlockTime() > GetAdjustedTime() - consensusParams.nPowTargetSpacing * 20;
525 }
526 
527 // Requires cs_main
PeerHasHeader(CNodeState * state,CBlockIndex * pindex)528 bool PeerHasHeader(CNodeState *state, CBlockIndex *pindex)
529 {
530     if (state->pindexBestKnownBlock && pindex == state->pindexBestKnownBlock->GetAncestor(pindex->nHeight))
531         return true;
532     if (state->pindexBestHeaderSent && pindex == state->pindexBestHeaderSent->GetAncestor(pindex->nHeight))
533         return true;
534     return false;
535 }
536 
537 /** Find the last common ancestor two blocks have.
538  *  Both pa and pb must be non-NULL. */
LastCommonAncestor(CBlockIndex * pa,CBlockIndex * pb)539 CBlockIndex* LastCommonAncestor(CBlockIndex* pa, CBlockIndex* pb) {
540     if (pa->nHeight > pb->nHeight) {
541         pa = pa->GetAncestor(pb->nHeight);
542     } else if (pb->nHeight > pa->nHeight) {
543         pb = pb->GetAncestor(pa->nHeight);
544     }
545 
546     while (pa != pb && pa && pb) {
547         pa = pa->pprev;
548         pb = pb->pprev;
549     }
550 
551     // Eventually all chain branches meet at the genesis block.
552     assert(pa == pb);
553     return pa;
554 }
555 
556 /** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
557  *  at most count entries. */
FindNextBlocksToDownload(NodeId nodeid,unsigned int count,std::vector<CBlockIndex * > & vBlocks,NodeId & nodeStaller,const Consensus::Params & consensusParams)558 void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<CBlockIndex*>& vBlocks, NodeId& nodeStaller, const Consensus::Params& consensusParams) {
559     if (count == 0)
560         return;
561 
562     vBlocks.reserve(vBlocks.size() + count);
563     CNodeState *state = State(nodeid);
564     assert(state != NULL);
565 
566     // Make sure pindexBestKnownBlock is up to date, we'll need it.
567     ProcessBlockAvailability(nodeid);
568 
569     if (state->pindexBestKnownBlock == NULL || state->pindexBestKnownBlock->nChainWork < chainActive.Tip()->nChainWork) {
570         // This peer has nothing interesting.
571         return;
572     }
573 
574     if (state->pindexLastCommonBlock == NULL) {
575         // Bootstrap quickly by guessing a parent of our best tip is the forking point.
576         // Guessing wrong in either direction is not a problem.
577         state->pindexLastCommonBlock = chainActive[std::min(state->pindexBestKnownBlock->nHeight, chainActive.Height())];
578     }
579 
580     // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
581     // of its current tip anymore. Go back enough to fix that.
582     state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
583     if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
584         return;
585 
586     std::vector<CBlockIndex*> vToFetch;
587     CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
588     // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
589     // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
590     // download that next block if the window were 1 larger.
591     int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
592     int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
593     NodeId waitingfor = -1;
594     while (pindexWalk->nHeight < nMaxHeight) {
595         // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
596         // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
597         // as iterating over ~100 CBlockIndex* entries anyway.
598         int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
599         vToFetch.resize(nToFetch);
600         pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
601         vToFetch[nToFetch - 1] = pindexWalk;
602         for (unsigned int i = nToFetch - 1; i > 0; i--) {
603             vToFetch[i - 1] = vToFetch[i]->pprev;
604         }
605 
606         // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
607         // are not yet downloaded and not in flight to vBlocks. In the mean time, update
608         // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
609         // already part of our chain (and therefore don't need it even if pruned).
610         BOOST_FOREACH(CBlockIndex* pindex, vToFetch) {
611             if (!pindex->IsValid(BLOCK_VALID_TREE)) {
612                 // We consider the chain that this peer is on invalid.
613                 return;
614             }
615             if (!State(nodeid)->fHaveWitness && IsWitnessEnabled(pindex->pprev, consensusParams)) {
616                 // We wouldn't download this block or its descendants from this peer.
617                 return;
618             }
619             if (pindex->nStatus & BLOCK_HAVE_DATA || chainActive.Contains(pindex)) {
620                 if (pindex->nChainTx)
621                     state->pindexLastCommonBlock = pindex;
622             } else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
623                 // The block is not already downloaded, and not yet in flight.
624                 if (pindex->nHeight > nWindowEnd) {
625                     // We reached the end of the window.
626                     if (vBlocks.size() == 0 && waitingfor != nodeid) {
627                         // We aren't able to fetch anything, but we would be if the download window was one larger.
628                         nodeStaller = waitingfor;
629                     }
630                     return;
631                 }
632                 vBlocks.push_back(pindex);
633                 if (vBlocks.size() == count) {
634                     return;
635                 }
636             } else if (waitingfor == -1) {
637                 // This is the first already-in-flight block.
638                 waitingfor = mapBlocksInFlight[pindex->GetBlockHash()].first;
639             }
640         }
641     }
642 }
643 
644 } // anon namespace
645 
GetNodeStateStats(NodeId nodeid,CNodeStateStats & stats)646 bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
647     LOCK(cs_main);
648     CNodeState *state = State(nodeid);
649     if (state == NULL)
650         return false;
651     stats.nMisbehavior = state->nMisbehavior;
652     stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
653     stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
654     BOOST_FOREACH(const QueuedBlock& queue, state->vBlocksInFlight) {
655         if (queue.pindex)
656             stats.vHeightInFlight.push_back(queue.pindex->nHeight);
657     }
658     return true;
659 }
660 
RegisterNodeSignals(CNodeSignals & nodeSignals)661 void RegisterNodeSignals(CNodeSignals& nodeSignals)
662 {
663     nodeSignals.GetHeight.connect(&GetHeight);
664     nodeSignals.ProcessMessages.connect(&ProcessMessages);
665     nodeSignals.SendMessages.connect(&SendMessages);
666     nodeSignals.InitializeNode.connect(&InitializeNode);
667     nodeSignals.FinalizeNode.connect(&FinalizeNode);
668 }
669 
UnregisterNodeSignals(CNodeSignals & nodeSignals)670 void UnregisterNodeSignals(CNodeSignals& nodeSignals)
671 {
672     nodeSignals.GetHeight.disconnect(&GetHeight);
673     nodeSignals.ProcessMessages.disconnect(&ProcessMessages);
674     nodeSignals.SendMessages.disconnect(&SendMessages);
675     nodeSignals.InitializeNode.disconnect(&InitializeNode);
676     nodeSignals.FinalizeNode.disconnect(&FinalizeNode);
677 }
678 
FindForkInGlobalIndex(const CChain & chain,const CBlockLocator & locator)679 CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator)
680 {
681     // Find the first block the caller has in the main chain
682     BOOST_FOREACH(const uint256& hash, locator.vHave) {
683         BlockMap::iterator mi = mapBlockIndex.find(hash);
684         if (mi != mapBlockIndex.end())
685         {
686             CBlockIndex* pindex = (*mi).second;
687             if (chain.Contains(pindex))
688                 return pindex;
689             if (pindex->GetAncestor(chain.Height()) == chain.Tip()) {
690                 return chain.Tip();
691             }
692         }
693     }
694     return chain.Genesis();
695 }
696 
697 CCoinsViewCache *pcoinsTip = NULL;
698 CBlockTreeDB *pblocktree = NULL;
699 
700 //////////////////////////////////////////////////////////////////////////////
701 //
702 // mapOrphanTransactions
703 //
704 
AddOrphanTx(const CTransaction & tx,NodeId peer)705 bool AddOrphanTx(const CTransaction& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
706 {
707     uint256 hash = tx.GetHash();
708     if (mapOrphanTransactions.count(hash))
709         return false;
710 
711     // Ignore big transactions, to avoid a
712     // send-big-orphans memory exhaustion attack. If a peer has a legitimate
713     // large transaction with a missing parent then we assume
714     // it will rebroadcast it later, after the parent transaction(s)
715     // have been mined or received.
716     // 100 orphans, each of which is at most 99,999 bytes big is
717     // at most 10 megabytes of orphans and somewhat more byprev index (in the worst case):
718     unsigned int sz = GetTransactionWeight(tx);
719     if (sz >= MAX_STANDARD_TX_WEIGHT)
720     {
721         LogPrint("mempool", "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
722         return false;
723     }
724 
725     auto ret = mapOrphanTransactions.emplace(hash, COrphanTx{tx, peer, GetTime() + ORPHAN_TX_EXPIRE_TIME});
726     assert(ret.second);
727     BOOST_FOREACH(const CTxIn& txin, tx.vin) {
728         mapOrphanTransactionsByPrev[txin.prevout].insert(ret.first);
729     }
730 
731     LogPrint("mempool", "stored orphan tx %s (mapsz %u outsz %u)\n", hash.ToString(),
732              mapOrphanTransactions.size(), mapOrphanTransactionsByPrev.size());
733     return true;
734 }
735 
EraseOrphanTx(uint256 hash)736 int static EraseOrphanTx(uint256 hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
737 {
738     map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
739     if (it == mapOrphanTransactions.end())
740         return 0;
741     BOOST_FOREACH(const CTxIn& txin, it->second.tx.vin)
742     {
743         auto itPrev = mapOrphanTransactionsByPrev.find(txin.prevout);
744         if (itPrev == mapOrphanTransactionsByPrev.end())
745             continue;
746         itPrev->second.erase(it);
747         if (itPrev->second.empty())
748             mapOrphanTransactionsByPrev.erase(itPrev);
749     }
750     mapOrphanTransactions.erase(it);
751     return 1;
752 }
753 
EraseOrphansFor(NodeId peer)754 void EraseOrphansFor(NodeId peer)
755 {
756     int nErased = 0;
757     map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
758     while (iter != mapOrphanTransactions.end())
759     {
760         map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
761         if (maybeErase->second.fromPeer == peer)
762         {
763             nErased += EraseOrphanTx(maybeErase->second.tx.GetHash());
764         }
765     }
766     if (nErased > 0) LogPrint("mempool", "Erased %d orphan tx from peer %d\n", nErased, peer);
767 }
768 
769 
LimitOrphanTxSize(unsigned int nMaxOrphans)770 unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
771 {
772     unsigned int nEvicted = 0;
773     static int64_t nNextSweep;
774     int64_t nNow = GetTime();
775     if (nNextSweep <= nNow) {
776         // Sweep out expired orphan pool entries:
777         int nErased = 0;
778         int64_t nMinExpTime = nNow + ORPHAN_TX_EXPIRE_TIME - ORPHAN_TX_EXPIRE_INTERVAL;
779         map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
780         while (iter != mapOrphanTransactions.end())
781         {
782             map<uint256, COrphanTx>::iterator maybeErase = iter++;
783             if (maybeErase->second.nTimeExpire <= nNow) {
784                 nErased += EraseOrphanTx(maybeErase->second.tx.GetHash());
785             } else {
786                 nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
787             }
788         }
789         // Sweep again 5 minutes after the next entry that expires in order to batch the linear scan.
790         nNextSweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
791         if (nErased > 0) LogPrint("mempool", "Erased %d orphan tx due to expiration\n", nErased);
792     }
793     while (mapOrphanTransactions.size() > nMaxOrphans)
794     {
795         // Evict a random orphan:
796         uint256 randomhash = GetRandHash();
797         map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.lower_bound(randomhash);
798         if (it == mapOrphanTransactions.end())
799             it = mapOrphanTransactions.begin();
800         EraseOrphanTx(it->first);
801         ++nEvicted;
802     }
803     return nEvicted;
804 }
805 
IsFinalTx(const CTransaction & tx,int nBlockHeight,int64_t nBlockTime)806 bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
807 {
808     if (tx.nLockTime == 0)
809         return true;
810     if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
811         return true;
812     BOOST_FOREACH(const CTxIn& txin, tx.vin) {
813         if (!(txin.nSequence == CTxIn::SEQUENCE_FINAL))
814             return false;
815     }
816     return true;
817 }
818 
CheckFinalTx(const CTransaction & tx,int flags)819 bool CheckFinalTx(const CTransaction &tx, int flags)
820 {
821     AssertLockHeld(cs_main);
822 
823     // By convention a negative value for flags indicates that the
824     // current network-enforced consensus rules should be used. In
825     // a future soft-fork scenario that would mean checking which
826     // rules would be enforced for the next block and setting the
827     // appropriate flags. At the present time no soft-forks are
828     // scheduled, so no flags are set.
829     flags = std::max(flags, 0);
830 
831     // CheckFinalTx() uses chainActive.Height()+1 to evaluate
832     // nLockTime because when IsFinalTx() is called within
833     // CBlock::AcceptBlock(), the height of the block *being*
834     // evaluated is what is used. Thus if we want to know if a
835     // transaction can be part of the *next* block, we need to call
836     // IsFinalTx() with one more than chainActive.Height().
837     const int nBlockHeight = chainActive.Height() + 1;
838 
839     // BIP113 will require that time-locked transactions have nLockTime set to
840     // less than the median time of the previous block they're contained in.
841     // When the next block is created its previous block will be the current
842     // chain tip, so we use that to calculate the median time passed to
843     // IsFinalTx() if LOCKTIME_MEDIAN_TIME_PAST is set.
844     const int64_t nBlockTime = (flags & LOCKTIME_MEDIAN_TIME_PAST)
845                              ? chainActive.Tip()->GetMedianTimePast()
846                              : GetAdjustedTime();
847 
848     return IsFinalTx(tx, nBlockHeight, nBlockTime);
849 }
850 
851 /**
852  * Calculates the block height and previous block's median time past at
853  * which the transaction will be considered final in the context of BIP 68.
854  * Also removes from the vector of input heights any entries which did not
855  * correspond to sequence locked inputs as they do not affect the calculation.
856  */
CalculateSequenceLocks(const CTransaction & tx,int flags,std::vector<int> * prevHeights,const CBlockIndex & block)857 static std::pair<int, int64_t> CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector<int>* prevHeights, const CBlockIndex& block)
858 {
859     assert(prevHeights->size() == tx.vin.size());
860 
861     // Will be set to the equivalent height- and time-based nLockTime
862     // values that would be necessary to satisfy all relative lock-
863     // time constraints given our view of block chain history.
864     // The semantics of nLockTime are the last invalid height/time, so
865     // use -1 to have the effect of any height or time being valid.
866     int nMinHeight = -1;
867     int64_t nMinTime = -1;
868 
869     // tx.nVersion is signed integer so requires cast to unsigned otherwise
870     // we would be doing a signed comparison and half the range of nVersion
871     // wouldn't support BIP 68.
872     bool fEnforceBIP68 = static_cast<uint32_t>(tx.nVersion) >= 2
873                       && flags & LOCKTIME_VERIFY_SEQUENCE;
874 
875     // Do not enforce sequence numbers as a relative lock time
876     // unless we have been instructed to
877     if (!fEnforceBIP68) {
878         return std::make_pair(nMinHeight, nMinTime);
879     }
880 
881     for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
882         const CTxIn& txin = tx.vin[txinIndex];
883 
884         // Sequence numbers with the most significant bit set are not
885         // treated as relative lock-times, nor are they given any
886         // consensus-enforced meaning at this point.
887         if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) {
888             // The height of this input is not relevant for sequence locks
889             (*prevHeights)[txinIndex] = 0;
890             continue;
891         }
892 
893         int nCoinHeight = (*prevHeights)[txinIndex];
894 
895         if (txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) {
896             int64_t nCoinTime = block.GetAncestor(std::max(nCoinHeight-1, 0))->GetMedianTimePast();
897             // NOTE: Subtract 1 to maintain nLockTime semantics
898             // BIP 68 relative lock times have the semantics of calculating
899             // the first block or time at which the transaction would be
900             // valid. When calculating the effective block time or height
901             // for the entire transaction, we switch to using the
902             // semantics of nLockTime which is the last invalid block
903             // time or height.  Thus we subtract 1 from the calculated
904             // time or height.
905 
906             // Time-based relative lock-times are measured from the
907             // smallest allowed timestamp of the block containing the
908             // txout being spent, which is the median time past of the
909             // block prior.
910             nMinTime = std::max(nMinTime, nCoinTime + (int64_t)((txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) << CTxIn::SEQUENCE_LOCKTIME_GRANULARITY) - 1);
911         } else {
912             nMinHeight = std::max(nMinHeight, nCoinHeight + (int)(txin.nSequence & CTxIn::SEQUENCE_LOCKTIME_MASK) - 1);
913         }
914     }
915 
916     return std::make_pair(nMinHeight, nMinTime);
917 }
918 
EvaluateSequenceLocks(const CBlockIndex & block,std::pair<int,int64_t> lockPair)919 static bool EvaluateSequenceLocks(const CBlockIndex& block, std::pair<int, int64_t> lockPair)
920 {
921     assert(block.pprev);
922     int64_t nBlockTime = block.pprev->GetMedianTimePast();
923     if (lockPair.first >= block.nHeight || lockPair.second >= nBlockTime)
924         return false;
925 
926     return true;
927 }
928 
SequenceLocks(const CTransaction & tx,int flags,std::vector<int> * prevHeights,const CBlockIndex & block)929 bool SequenceLocks(const CTransaction &tx, int flags, std::vector<int>* prevHeights, const CBlockIndex& block)
930 {
931     return EvaluateSequenceLocks(block, CalculateSequenceLocks(tx, flags, prevHeights, block));
932 }
933 
TestLockPointValidity(const LockPoints * lp)934 bool TestLockPointValidity(const LockPoints* lp)
935 {
936     AssertLockHeld(cs_main);
937     assert(lp);
938     // If there are relative lock times then the maxInputBlock will be set
939     // If there are no relative lock times, the LockPoints don't depend on the chain
940     if (lp->maxInputBlock) {
941         // Check whether chainActive is an extension of the block at which the LockPoints
942         // calculation was valid.  If not LockPoints are no longer valid
943         if (!chainActive.Contains(lp->maxInputBlock)) {
944             return false;
945         }
946     }
947 
948     // LockPoints still valid
949     return true;
950 }
951 
CheckSequenceLocks(const CTransaction & tx,int flags,LockPoints * lp,bool useExistingLockPoints)952 bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool useExistingLockPoints)
953 {
954     AssertLockHeld(cs_main);
955     AssertLockHeld(mempool.cs);
956 
957     CBlockIndex* tip = chainActive.Tip();
958     CBlockIndex index;
959     index.pprev = tip;
960     // CheckSequenceLocks() uses chainActive.Height()+1 to evaluate
961     // height based locks because when SequenceLocks() is called within
962     // ConnectBlock(), the height of the block *being*
963     // evaluated is what is used.
964     // Thus if we want to know if a transaction can be part of the
965     // *next* block, we need to use one more than chainActive.Height()
966     index.nHeight = tip->nHeight + 1;
967 
968     std::pair<int, int64_t> lockPair;
969     if (useExistingLockPoints) {
970         assert(lp);
971         lockPair.first = lp->height;
972         lockPair.second = lp->time;
973     }
974     else {
975         // pcoinsTip contains the UTXO set for chainActive.Tip()
976         CCoinsViewMemPool viewMemPool(pcoinsTip, mempool);
977         std::vector<int> prevheights;
978         prevheights.resize(tx.vin.size());
979         for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
980             const CTxIn& txin = tx.vin[txinIndex];
981             CCoins coins;
982             if (!viewMemPool.GetCoins(txin.prevout.hash, coins)) {
983                 return error("%s: Missing input", __func__);
984             }
985             if (coins.nHeight == MEMPOOL_HEIGHT) {
986                 // Assume all mempool transaction confirm in the next block
987                 prevheights[txinIndex] = tip->nHeight + 1;
988             } else {
989                 prevheights[txinIndex] = coins.nHeight;
990             }
991         }
992         lockPair = CalculateSequenceLocks(tx, flags, &prevheights, index);
993         if (lp) {
994             lp->height = lockPair.first;
995             lp->time = lockPair.second;
996             // Also store the hash of the block with the highest height of
997             // all the blocks which have sequence locked prevouts.
998             // This hash needs to still be on the chain
999             // for these LockPoint calculations to be valid
1000             // Note: It is impossible to correctly calculate a maxInputBlock
1001             // if any of the sequence locked inputs depend on unconfirmed txs,
1002             // except in the special case where the relative lock time/height
1003             // is 0, which is equivalent to no sequence lock. Since we assume
1004             // input height of tip+1 for mempool txs and test the resulting
1005             // lockPair from CalculateSequenceLocks against tip+1.  We know
1006             // EvaluateSequenceLocks will fail if there was a non-zero sequence
1007             // lock on a mempool input, so we can use the return value of
1008             // CheckSequenceLocks to indicate the LockPoints validity
1009             int maxInputHeight = 0;
1010             BOOST_FOREACH(int height, prevheights) {
1011                 // Can ignore mempool inputs since we'll fail if they had non-zero locks
1012                 if (height != tip->nHeight+1) {
1013                     maxInputHeight = std::max(maxInputHeight, height);
1014                 }
1015             }
1016             lp->maxInputBlock = tip->GetAncestor(maxInputHeight);
1017         }
1018     }
1019     return EvaluateSequenceLocks(index, lockPair);
1020 }
1021 
1022 
GetLegacySigOpCount(const CTransaction & tx)1023 unsigned int GetLegacySigOpCount(const CTransaction& tx)
1024 {
1025     unsigned int nSigOps = 0;
1026     BOOST_FOREACH(const CTxIn& txin, tx.vin)
1027     {
1028         nSigOps += txin.scriptSig.GetSigOpCount(false);
1029     }
1030     BOOST_FOREACH(const CTxOut& txout, tx.vout)
1031     {
1032         nSigOps += txout.scriptPubKey.GetSigOpCount(false);
1033     }
1034     return nSigOps;
1035 }
1036 
GetP2SHSigOpCount(const CTransaction & tx,const CCoinsViewCache & inputs)1037 unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs)
1038 {
1039     if (tx.IsCoinBase())
1040         return 0;
1041 
1042     unsigned int nSigOps = 0;
1043     for (unsigned int i = 0; i < tx.vin.size(); i++)
1044     {
1045         const CTxOut &prevout = inputs.GetOutputFor(tx.vin[i]);
1046         if (prevout.scriptPubKey.IsPayToScriptHash())
1047             nSigOps += prevout.scriptPubKey.GetSigOpCount(tx.vin[i].scriptSig);
1048     }
1049     return nSigOps;
1050 }
1051 
GetTransactionSigOpCost(const CTransaction & tx,const CCoinsViewCache & inputs,int flags)1052 int64_t GetTransactionSigOpCost(const CTransaction& tx, const CCoinsViewCache& inputs, int flags)
1053 {
1054     int64_t nSigOps = GetLegacySigOpCount(tx) * WITNESS_SCALE_FACTOR;
1055 
1056     if (tx.IsCoinBase())
1057         return nSigOps;
1058 
1059     if (flags & SCRIPT_VERIFY_P2SH) {
1060         nSigOps += GetP2SHSigOpCount(tx, inputs) * WITNESS_SCALE_FACTOR;
1061     }
1062 
1063     for (unsigned int i = 0; i < tx.vin.size(); i++)
1064     {
1065         const CTxOut &prevout = inputs.GetOutputFor(tx.vin[i]);
1066         nSigOps += CountWitnessSigOps(tx.vin[i].scriptSig, prevout.scriptPubKey, i < tx.wit.vtxinwit.size() ? &tx.wit.vtxinwit[i].scriptWitness : NULL, flags);
1067     }
1068     return nSigOps;
1069 }
1070 
1071 
1072 
1073 
1074 
CheckTransaction(const CTransaction & tx,CValidationState & state)1075 bool CheckTransaction(const CTransaction& tx, CValidationState &state)
1076 {
1077     // Basic checks that don't depend on any context
1078     if (tx.vin.empty())
1079         return state.DoS(10, false, REJECT_INVALID, "bad-txns-vin-empty");
1080     if (tx.vout.empty())
1081         return state.DoS(10, false, REJECT_INVALID, "bad-txns-vout-empty");
1082     // Size limits (this doesn't take the witness into account, as that hasn't been checked for malleability)
1083     if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) > MAX_BLOCK_BASE_SIZE)
1084         return state.DoS(100, false, REJECT_INVALID, "bad-txns-oversize");
1085 
1086     // Check for negative or overflow output values
1087     CAmount nValueOut = 0;
1088     BOOST_FOREACH(const CTxOut& txout, tx.vout)
1089     {
1090         if (txout.nValue < 0)
1091             return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-negative");
1092         if (txout.nValue > MAX_MONEY)
1093             return state.DoS(100, false, REJECT_INVALID, "bad-txns-vout-toolarge");
1094         nValueOut += txout.nValue;
1095         if (!MoneyRange(nValueOut))
1096             return state.DoS(100, false, REJECT_INVALID, "bad-txns-txouttotal-toolarge");
1097     }
1098 
1099     // Check for duplicate inputs
1100     set<COutPoint> vInOutPoints;
1101     BOOST_FOREACH(const CTxIn& txin, tx.vin)
1102     {
1103         if (vInOutPoints.count(txin.prevout))
1104             return state.DoS(100, false, REJECT_INVALID, "bad-txns-inputs-duplicate");
1105         vInOutPoints.insert(txin.prevout);
1106     }
1107 
1108     if (tx.IsCoinBase())
1109     {
1110         if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
1111             return state.DoS(100, false, REJECT_INVALID, "bad-cb-length");
1112     }
1113     else
1114     {
1115         BOOST_FOREACH(const CTxIn& txin, tx.vin)
1116             if (txin.prevout.IsNull())
1117                 return state.DoS(10, false, REJECT_INVALID, "bad-txns-prevout-null");
1118     }
1119 
1120     return true;
1121 }
1122 
LimitMempoolSize(CTxMemPool & pool,size_t limit,unsigned long age)1123 void LimitMempoolSize(CTxMemPool& pool, size_t limit, unsigned long age) {
1124     int expired = pool.Expire(GetTime() - age);
1125     if (expired != 0)
1126         LogPrint("mempool", "Expired %i transactions from the memory pool\n", expired);
1127 
1128     std::vector<uint256> vNoSpendsRemaining;
1129     pool.TrimToSize(limit, &vNoSpendsRemaining);
1130     BOOST_FOREACH(const uint256& removed, vNoSpendsRemaining)
1131         pcoinsTip->Uncache(removed);
1132 }
1133 
1134 /** Convert CValidationState to a human-readable message for logging */
FormatStateMessage(const CValidationState & state)1135 std::string FormatStateMessage(const CValidationState &state)
1136 {
1137     return strprintf("%s%s (code %i)",
1138         state.GetRejectReason(),
1139         state.GetDebugMessage().empty() ? "" : ", "+state.GetDebugMessage(),
1140         state.GetRejectCode());
1141 }
1142 
AcceptToMemoryPoolWorker(CTxMemPool & pool,CValidationState & state,const CTransaction & tx,bool fLimitFree,bool * pfMissingInputs,bool fOverrideMempoolLimit,const CAmount & nAbsurdFee,std::vector<uint256> & vHashTxnToUncache)1143 bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const CTransaction& tx, bool fLimitFree,
1144                               bool* pfMissingInputs, bool fOverrideMempoolLimit, const CAmount& nAbsurdFee,
1145                               std::vector<uint256>& vHashTxnToUncache)
1146 {
1147     const uint256 hash = tx.GetHash();
1148     AssertLockHeld(cs_main);
1149     if (pfMissingInputs)
1150         *pfMissingInputs = false;
1151 
1152     if (!CheckTransaction(tx, state))
1153         return false; // state filled in by CheckTransaction
1154 
1155     // Coinbase is only valid in a block, not as a loose transaction
1156     if (tx.IsCoinBase())
1157         return state.DoS(100, false, REJECT_INVALID, "coinbase");
1158 
1159     // Don't relay version 2 transactions until CSV is active, and we can be
1160     // sure that such transactions will be mined (unless we're on
1161     // -testnet/-regtest).
1162     const CChainParams& chainparams = Params();
1163     if (fRequireStandard && tx.nVersion >= 2 && VersionBitsTipState(chainparams.GetConsensus(), Consensus::DEPLOYMENT_CSV) != THRESHOLD_ACTIVE) {
1164         return state.DoS(0, false, REJECT_NONSTANDARD, "premature-version2-tx");
1165     }
1166 
1167     // Reject transactions with witness before segregated witness activates (override with -prematurewitness)
1168     bool witnessEnabled = IsWitnessEnabled(chainActive.Tip(), Params().GetConsensus());
1169     if (!GetBoolArg("-prematurewitness",false) && !tx.wit.IsNull() && !witnessEnabled) {
1170         return state.DoS(0, false, REJECT_NONSTANDARD, "no-witness-yet", true);
1171     }
1172 
1173     // Rather not work on nonstandard transactions (unless -testnet/-regtest)
1174     string reason;
1175     if (fRequireStandard && !IsStandardTx(tx, reason, witnessEnabled))
1176         return state.DoS(0, false, REJECT_NONSTANDARD, reason);
1177 
1178     // Only accept nLockTime-using transactions that can be mined in the next
1179     // block; we don't want our mempool filled up with transactions that can't
1180     // be mined yet.
1181     if (!CheckFinalTx(tx, STANDARD_LOCKTIME_VERIFY_FLAGS))
1182         return state.DoS(0, false, REJECT_NONSTANDARD, "non-final");
1183 
1184     // is it already in the memory pool?
1185     if (pool.exists(hash))
1186         return state.Invalid(false, REJECT_ALREADY_KNOWN, "txn-already-in-mempool");
1187 
1188     // Check for conflicts with in-memory transactions
1189     set<uint256> setConflicts;
1190     {
1191     LOCK(pool.cs); // protect pool.mapNextTx
1192     BOOST_FOREACH(const CTxIn &txin, tx.vin)
1193     {
1194         auto itConflicting = pool.mapNextTx.find(txin.prevout);
1195         if (itConflicting != pool.mapNextTx.end())
1196         {
1197             const CTransaction *ptxConflicting = itConflicting->second;
1198             if (!setConflicts.count(ptxConflicting->GetHash()))
1199             {
1200                 // Allow opt-out of transaction replacement by setting
1201                 // nSequence >= maxint-1 on all inputs.
1202                 //
1203                 // maxint-1 is picked to still allow use of nLockTime by
1204                 // non-replaceable transactions. All inputs rather than just one
1205                 // is for the sake of multi-party protocols, where we don't
1206                 // want a single party to be able to disable replacement.
1207                 //
1208                 // The opt-out ignores descendants as anyone relying on
1209                 // first-seen mempool behavior should be checking all
1210                 // unconfirmed ancestors anyway; doing otherwise is hopelessly
1211                 // insecure.
1212                 bool fReplacementOptOut = true;
1213                 if (fEnableReplacement)
1214                 {
1215                     BOOST_FOREACH(const CTxIn &txin, ptxConflicting->vin)
1216                     {
1217                         if (txin.nSequence < std::numeric_limits<unsigned int>::max()-1)
1218                         {
1219                             fReplacementOptOut = false;
1220                             break;
1221                         }
1222                     }
1223                 }
1224                 if (fReplacementOptOut)
1225                     return state.Invalid(false, REJECT_CONFLICT, "txn-mempool-conflict");
1226 
1227                 setConflicts.insert(ptxConflicting->GetHash());
1228             }
1229         }
1230     }
1231     }
1232 
1233     {
1234         CCoinsView dummy;
1235         CCoinsViewCache view(&dummy);
1236 
1237         CAmount nValueIn = 0;
1238         LockPoints lp;
1239         {
1240         LOCK(pool.cs);
1241         CCoinsViewMemPool viewMemPool(pcoinsTip, pool);
1242         view.SetBackend(viewMemPool);
1243 
1244         // do we already have it?
1245         bool fHadTxInCache = pcoinsTip->HaveCoinsInCache(hash);
1246         if (view.HaveCoins(hash)) {
1247             if (!fHadTxInCache)
1248                 vHashTxnToUncache.push_back(hash);
1249             return state.Invalid(false, REJECT_ALREADY_KNOWN, "txn-already-known");
1250         }
1251 
1252         // do all inputs exist?
1253         // Note that this does not check for the presence of actual outputs (see the next check for that),
1254         // and only helps with filling in pfMissingInputs (to determine missing vs spent).
1255         BOOST_FOREACH(const CTxIn txin, tx.vin) {
1256             if (!pcoinsTip->HaveCoinsInCache(txin.prevout.hash))
1257                 vHashTxnToUncache.push_back(txin.prevout.hash);
1258             if (!view.HaveCoins(txin.prevout.hash)) {
1259                 if (pfMissingInputs)
1260                     *pfMissingInputs = true;
1261                 return false; // fMissingInputs and !state.IsInvalid() is used to detect this condition, don't set state.Invalid()
1262             }
1263         }
1264 
1265         // are the actual inputs available?
1266         if (!view.HaveInputs(tx))
1267             return state.Invalid(false, REJECT_DUPLICATE, "bad-txns-inputs-spent");
1268 
1269         // Bring the best block into scope
1270         view.GetBestBlock();
1271 
1272         nValueIn = view.GetValueIn(tx);
1273 
1274         // we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool
1275         view.SetBackend(dummy);
1276 
1277         // Only accept BIP68 sequence locked transactions that can be mined in the next
1278         // block; we don't want our mempool filled up with transactions that can't
1279         // be mined yet.
1280         // Must keep pool.cs for this unless we change CheckSequenceLocks to take a
1281         // CoinsViewCache instead of create its own
1282         if (!CheckSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, &lp))
1283             return state.DoS(0, false, REJECT_NONSTANDARD, "non-BIP68-final");
1284         }
1285 
1286         // Check for non-standard pay-to-script-hash in inputs
1287         if (fRequireStandard && !AreInputsStandard(tx, view))
1288             return state.Invalid(false, REJECT_NONSTANDARD, "bad-txns-nonstandard-inputs");
1289 
1290         // Check for non-standard witness in P2WSH
1291         if (!tx.wit.IsNull() && fRequireStandard && !IsWitnessStandard(tx, view))
1292             return state.DoS(0, false, REJECT_NONSTANDARD, "bad-witness-nonstandard", true);
1293 
1294         int64_t nSigOpsCost = GetTransactionSigOpCost(tx, view, STANDARD_SCRIPT_VERIFY_FLAGS);
1295 
1296         CAmount nValueOut = tx.GetValueOut();
1297         CAmount nFees = nValueIn-nValueOut;
1298         // nModifiedFees includes any fee deltas from PrioritiseTransaction
1299         CAmount nModifiedFees = nFees;
1300         double nPriorityDummy = 0;
1301         pool.ApplyDeltas(hash, nPriorityDummy, nModifiedFees);
1302 
1303         CAmount inChainInputValue;
1304         double dPriority = view.GetPriority(tx, chainActive.Height(), inChainInputValue);
1305 
1306         // Keep track of transactions that spend a coinbase, which we re-scan
1307         // during reorgs to ensure COINBASE_MATURITY is still met.
1308         bool fSpendsCoinbase = false;
1309         BOOST_FOREACH(const CTxIn &txin, tx.vin) {
1310             const CCoins *coins = view.AccessCoins(txin.prevout.hash);
1311             if (coins->IsCoinBase()) {
1312                 fSpendsCoinbase = true;
1313                 break;
1314             }
1315         }
1316 
1317         CTxMemPoolEntry entry(tx, nFees, GetTime(), dPriority, chainActive.Height(), pool.HasNoInputsOf(tx), inChainInputValue, fSpendsCoinbase, nSigOpsCost, lp);
1318         unsigned int nSize = entry.GetTxSize();
1319 
1320         // Check that the transaction doesn't have an excessive number of
1321         // sigops, making it impossible to mine. Since the coinbase transaction
1322         // itself can contain sigops MAX_STANDARD_TX_SIGOPS is less than
1323         // MAX_BLOCK_SIGOPS; we still consider this an invalid rather than
1324         // merely non-standard transaction.
1325         if (nSigOpsCost > MAX_STANDARD_TX_SIGOPS_COST)
1326             return state.DoS(0, false, REJECT_NONSTANDARD, "bad-txns-too-many-sigops", false,
1327                 strprintf("%d", nSigOpsCost));
1328 
1329         CAmount mempoolRejectFee = pool.GetMinFee(GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFee(nSize);
1330         if (mempoolRejectFee > 0 && nModifiedFees < mempoolRejectFee) {
1331             return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "mempool min fee not met", false, strprintf("%d < %d", nFees, mempoolRejectFee));
1332         } else if (GetBoolArg("-relaypriority", DEFAULT_RELAYPRIORITY) && nModifiedFees < ::minRelayTxFee.GetFee(nSize) && !AllowFree(entry.GetPriority(chainActive.Height() + 1))) {
1333             // Require that free transactions have sufficient priority to be mined in the next block.
1334             return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "insufficient priority");
1335         }
1336 
1337         // Continuously rate-limit free (really, very-low-fee) transactions
1338         // This mitigates 'penny-flooding' -- sending thousands of free transactions just to
1339         // be annoying or make others' transactions take longer to confirm.
1340         if (fLimitFree && nModifiedFees < ::minRelayTxFee.GetFee(nSize))
1341         {
1342             static CCriticalSection csFreeLimiter;
1343             static double dFreeCount;
1344             static int64_t nLastTime;
1345             int64_t nNow = GetTime();
1346 
1347             LOCK(csFreeLimiter);
1348 
1349             // Use an exponentially decaying ~10-minute window:
1350             dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime));
1351             nLastTime = nNow;
1352             // -limitfreerelay unit is thousand-bytes-per-minute
1353             // At default rate it would take over a month to fill 1GB
1354             if (dFreeCount + nSize >= GetArg("-limitfreerelay", DEFAULT_LIMITFREERELAY) * 10 * 1000)
1355                 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "rate limited free transaction");
1356             LogPrint("mempool", "Rate limit dFreeCount: %g => %g\n", dFreeCount, dFreeCount+nSize);
1357             dFreeCount += nSize;
1358         }
1359 
1360         if (nAbsurdFee && nFees > nAbsurdFee)
1361             return state.Invalid(false,
1362                 REJECT_HIGHFEE, "absurdly-high-fee",
1363                 strprintf("%d > %d", nFees, nAbsurdFee));
1364 
1365         // Calculate in-mempool ancestors, up to a limit.
1366         CTxMemPool::setEntries setAncestors;
1367         size_t nLimitAncestors = GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT);
1368         size_t nLimitAncestorSize = GetArg("-limitancestorsize", DEFAULT_ANCESTOR_SIZE_LIMIT)*1000;
1369         size_t nLimitDescendants = GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT);
1370         size_t nLimitDescendantSize = GetArg("-limitdescendantsize", DEFAULT_DESCENDANT_SIZE_LIMIT)*1000;
1371         std::string errString;
1372         if (!pool.CalculateMemPoolAncestors(entry, setAncestors, nLimitAncestors, nLimitAncestorSize, nLimitDescendants, nLimitDescendantSize, errString)) {
1373             return state.DoS(0, false, REJECT_NONSTANDARD, "too-long-mempool-chain", false, errString);
1374         }
1375 
1376         // A transaction that spends outputs that would be replaced by it is invalid. Now
1377         // that we have the set of all ancestors we can detect this
1378         // pathological case by making sure setConflicts and setAncestors don't
1379         // intersect.
1380         BOOST_FOREACH(CTxMemPool::txiter ancestorIt, setAncestors)
1381         {
1382             const uint256 &hashAncestor = ancestorIt->GetTx().GetHash();
1383             if (setConflicts.count(hashAncestor))
1384             {
1385                 return state.DoS(10, false,
1386                                  REJECT_INVALID, "bad-txns-spends-conflicting-tx", false,
1387                                  strprintf("%s spends conflicting transaction %s",
1388                                            hash.ToString(),
1389                                            hashAncestor.ToString()));
1390             }
1391         }
1392 
1393         // Check if it's economically rational to mine this transaction rather
1394         // than the ones it replaces.
1395         CAmount nConflictingFees = 0;
1396         size_t nConflictingSize = 0;
1397         uint64_t nConflictingCount = 0;
1398         CTxMemPool::setEntries allConflicting;
1399 
1400         // If we don't hold the lock allConflicting might be incomplete; the
1401         // subsequent RemoveStaged() and addUnchecked() calls don't guarantee
1402         // mempool consistency for us.
1403         LOCK(pool.cs);
1404         if (setConflicts.size())
1405         {
1406             CFeeRate newFeeRate(nModifiedFees, nSize);
1407             set<uint256> setConflictsParents;
1408             const int maxDescendantsToVisit = 100;
1409             CTxMemPool::setEntries setIterConflicting;
1410             BOOST_FOREACH(const uint256 &hashConflicting, setConflicts)
1411             {
1412                 CTxMemPool::txiter mi = pool.mapTx.find(hashConflicting);
1413                 if (mi == pool.mapTx.end())
1414                     continue;
1415 
1416                 // Save these to avoid repeated lookups
1417                 setIterConflicting.insert(mi);
1418 
1419                 // Don't allow the replacement to reduce the feerate of the
1420                 // mempool.
1421                 //
1422                 // We usually don't want to accept replacements with lower
1423                 // feerates than what they replaced as that would lower the
1424                 // feerate of the next block. Requiring that the feerate always
1425                 // be increased is also an easy-to-reason about way to prevent
1426                 // DoS attacks via replacements.
1427                 //
1428                 // The mining code doesn't (currently) take children into
1429                 // account (CPFP) so we only consider the feerates of
1430                 // transactions being directly replaced, not their indirect
1431                 // descendants. While that does mean high feerate children are
1432                 // ignored when deciding whether or not to replace, we do
1433                 // require the replacement to pay more overall fees too,
1434                 // mitigating most cases.
1435                 CFeeRate oldFeeRate(mi->GetModifiedFee(), mi->GetTxSize());
1436                 if (newFeeRate <= oldFeeRate)
1437                 {
1438                     return state.DoS(0, false,
1439                             REJECT_INSUFFICIENTFEE, "insufficient fee", false,
1440                             strprintf("rejecting replacement %s; new feerate %s <= old feerate %s",
1441                                   hash.ToString(),
1442                                   newFeeRate.ToString(),
1443                                   oldFeeRate.ToString()));
1444                 }
1445 
1446                 BOOST_FOREACH(const CTxIn &txin, mi->GetTx().vin)
1447                 {
1448                     setConflictsParents.insert(txin.prevout.hash);
1449                 }
1450 
1451                 nConflictingCount += mi->GetCountWithDescendants();
1452             }
1453             // This potentially overestimates the number of actual descendants
1454             // but we just want to be conservative to avoid doing too much
1455             // work.
1456             if (nConflictingCount <= maxDescendantsToVisit) {
1457                 // If not too many to replace, then calculate the set of
1458                 // transactions that would have to be evicted
1459                 BOOST_FOREACH(CTxMemPool::txiter it, setIterConflicting) {
1460                     pool.CalculateDescendants(it, allConflicting);
1461                 }
1462                 BOOST_FOREACH(CTxMemPool::txiter it, allConflicting) {
1463                     nConflictingFees += it->GetModifiedFee();
1464                     nConflictingSize += it->GetTxSize();
1465                 }
1466             } else {
1467                 return state.DoS(0, false,
1468                         REJECT_NONSTANDARD, "too many potential replacements", false,
1469                         strprintf("rejecting replacement %s; too many potential replacements (%d > %d)\n",
1470                             hash.ToString(),
1471                             nConflictingCount,
1472                             maxDescendantsToVisit));
1473             }
1474 
1475             for (unsigned int j = 0; j < tx.vin.size(); j++)
1476             {
1477                 // We don't want to accept replacements that require low
1478                 // feerate junk to be mined first. Ideally we'd keep track of
1479                 // the ancestor feerates and make the decision based on that,
1480                 // but for now requiring all new inputs to be confirmed works.
1481                 if (!setConflictsParents.count(tx.vin[j].prevout.hash))
1482                 {
1483                     // Rather than check the UTXO set - potentially expensive -
1484                     // it's cheaper to just check if the new input refers to a
1485                     // tx that's in the mempool.
1486                     if (pool.mapTx.find(tx.vin[j].prevout.hash) != pool.mapTx.end())
1487                         return state.DoS(0, false,
1488                                          REJECT_NONSTANDARD, "replacement-adds-unconfirmed", false,
1489                                          strprintf("replacement %s adds unconfirmed input, idx %d",
1490                                                   hash.ToString(), j));
1491                 }
1492             }
1493 
1494             // The replacement must pay greater fees than the transactions it
1495             // replaces - if we did the bandwidth used by those conflicting
1496             // transactions would not be paid for.
1497             if (nModifiedFees < nConflictingFees)
1498             {
1499                 return state.DoS(0, false,
1500                                  REJECT_INSUFFICIENTFEE, "insufficient fee", false,
1501                                  strprintf("rejecting replacement %s, less fees than conflicting txs; %s < %s",
1502                                           hash.ToString(), FormatMoney(nModifiedFees), FormatMoney(nConflictingFees)));
1503             }
1504 
1505             // Finally in addition to paying more fees than the conflicts the
1506             // new transaction must pay for its own bandwidth.
1507             CAmount nDeltaFees = nModifiedFees - nConflictingFees;
1508             if (nDeltaFees < ::minRelayTxFee.GetFee(nSize))
1509             {
1510                 return state.DoS(0, false,
1511                         REJECT_INSUFFICIENTFEE, "insufficient fee", false,
1512                         strprintf("rejecting replacement %s, not enough additional fees to relay; %s < %s",
1513                               hash.ToString(),
1514                               FormatMoney(nDeltaFees),
1515                               FormatMoney(::minRelayTxFee.GetFee(nSize))));
1516             }
1517         }
1518 
1519         unsigned int scriptVerifyFlags = STANDARD_SCRIPT_VERIFY_FLAGS;
1520         if (!Params().RequireStandard()) {
1521             scriptVerifyFlags = GetArg("-promiscuousmempoolflags", scriptVerifyFlags);
1522         }
1523 
1524         // Check against previous transactions
1525         // This is done last to help prevent CPU exhaustion denial-of-service attacks.
1526         PrecomputedTransactionData txdata(tx);
1527         if (!CheckInputs(tx, state, view, true, scriptVerifyFlags, true, txdata)) {
1528             // SCRIPT_VERIFY_CLEANSTACK requires SCRIPT_VERIFY_WITNESS, so we
1529             // need to turn both off, and compare against just turning off CLEANSTACK
1530             // to see if the failure is specifically due to witness validation.
1531             if (tx.wit.IsNull() && CheckInputs(tx, state, view, true, scriptVerifyFlags & ~(SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_CLEANSTACK), true, txdata) &&
1532                 !CheckInputs(tx, state, view, true, scriptVerifyFlags & ~SCRIPT_VERIFY_CLEANSTACK, true, txdata)) {
1533                 // Only the witness is missing, so the transaction itself may be fine.
1534                 state.SetCorruptionPossible();
1535             }
1536             return false;
1537         }
1538 
1539         // Check again against just the consensus-critical mandatory script
1540         // verification flags, in case of bugs in the standard flags that cause
1541         // transactions to pass as valid when they're actually invalid. For
1542         // instance the STRICTENC flag was incorrectly allowing certain
1543         // CHECKSIG NOT scripts to pass, even though they were invalid.
1544         //
1545         // There is a similar check in CreateNewBlock() to prevent creating
1546         // invalid blocks, however allowing such transactions into the mempool
1547         // can be exploited as a DoS attack.
1548         if (!CheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true, txdata))
1549         {
1550             return error("%s: BUG! PLEASE REPORT THIS! ConnectInputs failed against MANDATORY but not STANDARD flags %s, %s",
1551                 __func__, hash.ToString(), FormatStateMessage(state));
1552         }
1553 
1554         // Remove conflicting transactions from the mempool
1555         BOOST_FOREACH(const CTxMemPool::txiter it, allConflicting)
1556         {
1557             LogPrint("mempool", "replacing tx %s with %s for %s BTC additional fees, %d delta bytes\n",
1558                     it->GetTx().GetHash().ToString(),
1559                     hash.ToString(),
1560                     FormatMoney(nModifiedFees - nConflictingFees),
1561                     (int)nSize - (int)nConflictingSize);
1562         }
1563         pool.RemoveStaged(allConflicting, false);
1564 
1565         // Store transaction in memory
1566         pool.addUnchecked(hash, entry, setAncestors, !IsInitialBlockDownload());
1567 
1568         // trim mempool and check if tx was trimmed
1569         if (!fOverrideMempoolLimit) {
1570             LimitMempoolSize(pool, GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
1571             if (!pool.exists(hash))
1572                 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "mempool full");
1573         }
1574     }
1575 
1576     SyncWithWallets(tx, NULL, NULL);
1577 
1578     return true;
1579 }
1580 
AcceptToMemoryPool(CTxMemPool & pool,CValidationState & state,const CTransaction & tx,bool fLimitFree,bool * pfMissingInputs,bool fOverrideMempoolLimit,const CAmount nAbsurdFee)1581 bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,
1582                         bool* pfMissingInputs, bool fOverrideMempoolLimit, const CAmount nAbsurdFee)
1583 {
1584     std::vector<uint256> vHashTxToUncache;
1585     bool res = AcceptToMemoryPoolWorker(pool, state, tx, fLimitFree, pfMissingInputs, fOverrideMempoolLimit, nAbsurdFee, vHashTxToUncache);
1586     if (!res) {
1587         BOOST_FOREACH(const uint256& hashTx, vHashTxToUncache)
1588             pcoinsTip->Uncache(hashTx);
1589     }
1590     return res;
1591 }
1592 
1593 /** Return transaction in txOut, and if it was found inside a block, its hash is placed in hashBlock */
GetTransaction(const uint256 & hash,CTransaction & txOut,const Consensus::Params & consensusParams,uint256 & hashBlock,bool fAllowSlow)1594 bool GetTransaction(const uint256 &hash, CTransaction &txOut, const Consensus::Params& consensusParams, uint256 &hashBlock, bool fAllowSlow)
1595 {
1596     CBlockIndex *pindexSlow = NULL;
1597 
1598     LOCK(cs_main);
1599 
1600     std::shared_ptr<const CTransaction> ptx = mempool.get(hash);
1601     if (ptx)
1602     {
1603         txOut = *ptx;
1604         return true;
1605     }
1606 
1607     if (fTxIndex) {
1608         CDiskTxPos postx;
1609         if (pblocktree->ReadTxIndex(hash, postx)) {
1610             CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
1611             if (file.IsNull())
1612                 return error("%s: OpenBlockFile failed", __func__);
1613             CBlockHeader header;
1614             try {
1615                 file >> header;
1616                 fseek(file.Get(), postx.nTxOffset, SEEK_CUR);
1617                 file >> txOut;
1618             } catch (const std::exception& e) {
1619                 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
1620             }
1621             hashBlock = header.GetHash();
1622             if (txOut.GetHash() != hash)
1623                 return error("%s: txid mismatch", __func__);
1624             return true;
1625         }
1626     }
1627 
1628     if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
1629         int nHeight = -1;
1630         {
1631             const CCoinsViewCache& view = *pcoinsTip;
1632             const CCoins* coins = view.AccessCoins(hash);
1633             if (coins)
1634                 nHeight = coins->nHeight;
1635         }
1636         if (nHeight > 0)
1637             pindexSlow = chainActive[nHeight];
1638     }
1639 
1640     if (pindexSlow) {
1641         CBlock block;
1642         if (ReadBlockFromDisk(block, pindexSlow, consensusParams)) {
1643             BOOST_FOREACH(const CTransaction &tx, block.vtx) {
1644                 if (tx.GetHash() == hash) {
1645                     txOut = tx;
1646                     hashBlock = pindexSlow->GetBlockHash();
1647                     return true;
1648                 }
1649             }
1650         }
1651     }
1652 
1653     return false;
1654 }
1655 
1656 
1657 
1658 
1659 
1660 
1661 //////////////////////////////////////////////////////////////////////////////
1662 //
1663 // CBlock and CBlockIndex
1664 //
1665 
WriteBlockToDisk(const CBlock & block,CDiskBlockPos & pos,const CMessageHeader::MessageStartChars & messageStart)1666 bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart)
1667 {
1668     // Open history file to append
1669     CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
1670     if (fileout.IsNull())
1671         return error("WriteBlockToDisk: OpenBlockFile failed");
1672 
1673     // Write index header
1674     unsigned int nSize = fileout.GetSerializeSize(block);
1675     fileout << FLATDATA(messageStart) << nSize;
1676 
1677     // Write block
1678     long fileOutPos = ftell(fileout.Get());
1679     if (fileOutPos < 0)
1680         return error("WriteBlockToDisk: ftell failed");
1681     pos.nPos = (unsigned int)fileOutPos;
1682     fileout << block;
1683 
1684     return true;
1685 }
1686 
ReadBlockFromDisk(CBlock & block,const CDiskBlockPos & pos,const Consensus::Params & consensusParams)1687 bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams)
1688 {
1689     block.SetNull();
1690 
1691     // Open history file to read
1692     CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
1693     if (filein.IsNull())
1694         return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
1695 
1696     // Read block
1697     try {
1698         filein >> block;
1699     }
1700     catch (const std::exception& e) {
1701         return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
1702     }
1703 
1704     // Check the header
1705     if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
1706         return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
1707 
1708     return true;
1709 }
1710 
ReadBlockFromDisk(CBlock & block,const CBlockIndex * pindex,const Consensus::Params & consensusParams)1711 bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
1712 {
1713     if (!ReadBlockFromDisk(block, pindex->GetBlockPos(), consensusParams))
1714         return false;
1715     if (block.GetHash() != pindex->GetBlockHash())
1716         return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
1717                 pindex->ToString(), pindex->GetBlockPos().ToString());
1718     return true;
1719 }
1720 
1721 static const CAmount nStartSubsidy = 1000 * COIN;
1722 static const CAmount nMinSubsidy = 1 * COIN;
1723 
GetBlockSubsidy(int nHeight,const Consensus::Params & consensusParams)1724 CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
1725 {
1726     int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
1727     // Force block reward to minimum when right shift is undefined.
1728     if (halvings >= 64)
1729         return nMinSubsidy;
1730 
1731     CAmount nSubsidy = nStartSubsidy;
1732     nSubsidy >>= halvings;
1733     if (nSubsidy < nMinSubsidy)
1734         return nMinSubsidy;
1735     return nSubsidy;
1736 }
1737 
IsInitialBlockDownload()1738 bool IsInitialBlockDownload()
1739 {
1740     //const CChainParams& chainParams = Params();
1741 
1742     // Once this function has returned false, it must remain false.
1743     static std::atomic<bool> latchToFalse{false};
1744     // Optimization: pre-test latch before taking the lock.
1745     if (latchToFalse.load(std::memory_order_relaxed))
1746         return false;
1747 
1748     LOCK(cs_main);
1749     if (latchToFalse.load(std::memory_order_relaxed))
1750         return false;
1751     if (fImporting || fReindex)
1752         return true;
1753     if (chainActive.Tip() == NULL)
1754         return true;
1755     //if (chainActive.Tip()->nChainWork < UintToArith256(chainParams.GetConsensus().nMinimumChainWork))
1756     //    return true;
1757     if (chainActive.Tip()->GetBlockTime() < (GetTime() - nMaxTipAge))
1758         return true;
1759     latchToFalse.store(true, std::memory_order_relaxed);
1760     return false;
1761 }
1762 
1763 bool fLargeWorkForkFound = false;
1764 bool fLargeWorkInvalidChainFound = false;
1765 CBlockIndex *pindexBestForkTip = NULL, *pindexBestForkBase = NULL;
1766 
AlertNotify(const std::string & strMessage)1767 static void AlertNotify(const std::string& strMessage)
1768 {
1769     uiInterface.NotifyAlertChanged();
1770     std::string strCmd = GetArg("-alertnotify", "");
1771     if (strCmd.empty()) return;
1772 
1773     // Alert text should be plain ascii coming from a trusted source, but to
1774     // be safe we first strip anything not in safeChars, then add single quotes around
1775     // the whole string before passing it to the shell:
1776     std::string singleQuote("'");
1777     std::string safeStatus = SanitizeString(strMessage);
1778     safeStatus = singleQuote+safeStatus+singleQuote;
1779     boost::replace_all(strCmd, "%s", safeStatus);
1780 
1781     boost::thread t(runCommand, strCmd); // thread runs free
1782 }
1783 
CheckForkWarningConditions()1784 void CheckForkWarningConditions()
1785 {
1786     AssertLockHeld(cs_main);
1787     // Before we get past initial download, we cannot reliably alert about forks
1788     // (we assume we don't get stuck on a fork before finishing our initial sync)
1789     if (IsInitialBlockDownload())
1790         return;
1791 
1792     // If our best fork is no longer within 72 blocks (+/- 12 hours if no one mines it)
1793     // of our head, drop it
1794     if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
1795         pindexBestForkTip = NULL;
1796 
1797     if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6)))
1798     {
1799         if (!fLargeWorkForkFound && pindexBestForkBase)
1800         {
1801             std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
1802                 pindexBestForkBase->phashBlock->ToString() + std::string("'");
1803             AlertNotify(warning);
1804         }
1805         if (pindexBestForkTip && pindexBestForkBase)
1806         {
1807             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__,
1808                    pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
1809                    pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
1810             fLargeWorkForkFound = true;
1811         }
1812         else
1813         {
1814             LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
1815             fLargeWorkInvalidChainFound = true;
1816         }
1817     }
1818     else
1819     {
1820         fLargeWorkForkFound = false;
1821         fLargeWorkInvalidChainFound = false;
1822     }
1823 }
1824 
CheckForkWarningConditionsOnNewFork(CBlockIndex * pindexNewForkTip)1825 void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
1826 {
1827     AssertLockHeld(cs_main);
1828     // If we are on a fork that is sufficiently large, set a warning flag
1829     CBlockIndex* pfork = pindexNewForkTip;
1830     CBlockIndex* plonger = chainActive.Tip();
1831     while (pfork && pfork != plonger)
1832     {
1833         while (plonger && plonger->nHeight > pfork->nHeight)
1834             plonger = plonger->pprev;
1835         if (pfork == plonger)
1836             break;
1837         pfork = pfork->pprev;
1838     }
1839 
1840     // We define a condition where we should warn the user about as a fork of at least 7 blocks
1841     // with a tip within 72 blocks (+/- 12 hours if no one mines it) of ours
1842     // We use 7 blocks rather arbitrarily as it represents just under 10% of sustained network
1843     // hash rate operating on the fork.
1844     // or a chain that is entirely longer than ours and invalid (note that this should be detected by both)
1845     // We define it this way because it allows us to only store the highest fork tip (+ base) which meets
1846     // the 7-block condition and from this always have the most-likely-to-cause-warning fork
1847     if (pfork && (!pindexBestForkTip || (pindexBestForkTip && pindexNewForkTip->nHeight > pindexBestForkTip->nHeight)) &&
1848             pindexNewForkTip->nChainWork - pfork->nChainWork > (GetBlockProof(*pfork) * 7) &&
1849             chainActive.Height() - pindexNewForkTip->nHeight < 72)
1850     {
1851         pindexBestForkTip = pindexNewForkTip;
1852         pindexBestForkBase = pfork;
1853     }
1854 
1855     CheckForkWarningConditions();
1856 }
1857 
1858 // Requires cs_main.
Misbehaving(NodeId pnode,int howmuch)1859 void Misbehaving(NodeId pnode, int howmuch)
1860 {
1861     if (howmuch == 0)
1862         return;
1863 
1864     CNodeState *state = State(pnode);
1865     if (state == NULL)
1866         return;
1867 
1868     state->nMisbehavior += howmuch;
1869     int banscore = GetArg("-banscore", DEFAULT_BANSCORE_THRESHOLD);
1870     if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
1871     {
1872         LogPrintf("%s: %s (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__, state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
1873         state->fShouldBan = true;
1874     } else
1875         LogPrintf("%s: %s (%d -> %d)\n", __func__, state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
1876 }
1877 
InvalidChainFound(CBlockIndex * pindexNew)1878 void static InvalidChainFound(CBlockIndex* pindexNew)
1879 {
1880     if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
1881         pindexBestInvalid = pindexNew;
1882 
1883     LogPrintf("%s: invalid block=%s  height=%d  log2_work=%.8g  date=%s\n", __func__,
1884       pindexNew->GetBlockHash().ToString(), pindexNew->nHeight,
1885       log(pindexNew->nChainWork.getdouble())/log(2.0), DateTimeStrFormat("%Y-%m-%d %H:%M:%S",
1886       pindexNew->GetBlockTime()));
1887     CBlockIndex *tip = chainActive.Tip();
1888     assert (tip);
1889     LogPrintf("%s:  current best=%s  height=%d  log2_work=%.8g  date=%s\n", __func__,
1890       tip->GetBlockHash().ToString(), chainActive.Height(), log(tip->nChainWork.getdouble())/log(2.0),
1891       DateTimeStrFormat("%Y-%m-%d %H:%M:%S", tip->GetBlockTime()));
1892     CheckForkWarningConditions();
1893 }
1894 
InvalidBlockFound(CBlockIndex * pindex,const CValidationState & state)1895 void static InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state) {
1896     int nDoS = 0;
1897     if (state.IsInvalid(nDoS)) {
1898         std::map<uint256, std::pair<NodeId, bool>>::iterator it = mapBlockSource.find(pindex->GetBlockHash());
1899         if (it != mapBlockSource.end() && State(it->second.first)) {
1900             assert (state.GetRejectCode() < REJECT_INTERNAL); // Blocks are never rejected with internal reject codes
1901             CBlockReject reject = {(unsigned char)state.GetRejectCode(), state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), pindex->GetBlockHash()};
1902             State(it->second.first)->rejects.push_back(reject);
1903             if (nDoS > 0 && it->second.second)
1904                 Misbehaving(it->second.first, nDoS);
1905         }
1906     }
1907     if (!state.CorruptionPossible()) {
1908         pindex->nStatus |= BLOCK_FAILED_VALID;
1909         setDirtyBlockIndex.insert(pindex);
1910         setBlockIndexCandidates.erase(pindex);
1911         InvalidChainFound(pindex);
1912     }
1913 }
1914 
UpdateCoins(const CTransaction & tx,CCoinsViewCache & inputs,CTxUndo & txundo,int nHeight)1915 void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight)
1916 {
1917     // mark inputs spent
1918     if (!tx.IsCoinBase()) {
1919         txundo.vprevout.reserve(tx.vin.size());
1920         BOOST_FOREACH(const CTxIn &txin, tx.vin) {
1921             CCoinsModifier coins = inputs.ModifyCoins(txin.prevout.hash);
1922             unsigned nPos = txin.prevout.n;
1923 
1924             if (nPos >= coins->vout.size() || coins->vout[nPos].IsNull())
1925                 assert(false);
1926             // mark an outpoint spent, and construct undo information
1927             txundo.vprevout.push_back(CTxInUndo(coins->vout[nPos]));
1928             coins->Spend(nPos);
1929             if (coins->vout.size() == 0) {
1930                 CTxInUndo& undo = txundo.vprevout.back();
1931                 undo.nHeight = coins->nHeight;
1932                 undo.fCoinBase = coins->fCoinBase;
1933                 undo.nVersion = coins->nVersion;
1934             }
1935         }
1936     }
1937     // add outputs
1938     inputs.ModifyNewCoins(tx.GetHash(), tx.IsCoinBase())->FromTx(tx, nHeight);
1939 }
1940 
UpdateCoins(const CTransaction & tx,CCoinsViewCache & inputs,int nHeight)1941 void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, int nHeight)
1942 {
1943     CTxUndo txundo;
1944     UpdateCoins(tx, inputs, txundo, nHeight);
1945 }
1946 
operator ()()1947 bool CScriptCheck::operator()() {
1948     const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
1949     const CScriptWitness *witness = (nIn < ptxTo->wit.vtxinwit.size()) ? &ptxTo->wit.vtxinwit[nIn].scriptWitness : NULL;
1950     if (!VerifyScript(scriptSig, scriptPubKey, witness, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, amount, cacheStore, *txdata), &error)) {
1951         return false;
1952     }
1953     return true;
1954 }
1955 
GetSpendHeight(const CCoinsViewCache & inputs)1956 int GetSpendHeight(const CCoinsViewCache& inputs)
1957 {
1958     LOCK(cs_main);
1959     CBlockIndex* pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
1960     return pindexPrev->nHeight + 1;
1961 }
1962 
1963 namespace Consensus {
CheckTxInputs(const CTransaction & tx,CValidationState & state,const CCoinsViewCache & inputs,int nSpendHeight)1964 bool CheckTxInputs(const CTransaction& tx, CValidationState& state, const CCoinsViewCache& inputs, int nSpendHeight)
1965 {
1966         // This doesn't trigger the DoS code on purpose; if it did, it would make it easier
1967         // for an attacker to attempt to split the network.
1968         if (!inputs.HaveInputs(tx))
1969             return state.Invalid(false, 0, "", "Inputs unavailable");
1970 
1971         CAmount nValueIn = 0;
1972         CAmount nFees = 0;
1973         for (unsigned int i = 0; i < tx.vin.size(); i++)
1974         {
1975             const COutPoint &prevout = tx.vin[i].prevout;
1976             const CCoins *coins = inputs.AccessCoins(prevout.hash);
1977             assert(coins);
1978 
1979             // If prev is coinbase, check that it's matured
1980             if (coins->IsCoinBase()) {
1981                 if (nSpendHeight - coins->nHeight < COINBASE_MATURITY)
1982                     return state.Invalid(false,
1983                         REJECT_INVALID, "bad-txns-premature-spend-of-coinbase",
1984                         strprintf("tried to spend coinbase at depth %d", nSpendHeight - coins->nHeight));
1985             }
1986 
1987             // Check for negative or overflow input values
1988             nValueIn += coins->vout[prevout.n].nValue;
1989             if (!MoneyRange(coins->vout[prevout.n].nValue) || !MoneyRange(nValueIn))
1990                 return state.DoS(100, false, REJECT_INVALID, "bad-txns-inputvalues-outofrange");
1991 
1992         }
1993 
1994         if (nValueIn < tx.GetValueOut())
1995             return state.DoS(100, false, REJECT_INVALID, "bad-txns-in-belowout", false,
1996                 strprintf("value in (%s) < value out (%s)", FormatMoney(nValueIn), FormatMoney(tx.GetValueOut())));
1997 
1998         // Tally transaction fees
1999         CAmount nTxFee = nValueIn - tx.GetValueOut();
2000         if (nTxFee < 0)
2001             return state.DoS(100, false, REJECT_INVALID, "bad-txns-fee-negative");
2002         nFees += nTxFee;
2003         if (!MoneyRange(nFees))
2004             return state.DoS(100, false, REJECT_INVALID, "bad-txns-fee-outofrange");
2005     return true;
2006 }
2007 }// namespace Consensus
2008 
CheckInputs(const CTransaction & tx,CValidationState & state,const CCoinsViewCache & inputs,bool fScriptChecks,unsigned int flags,bool cacheStore,PrecomputedTransactionData & txdata,std::vector<CScriptCheck> * pvChecks)2009 bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks)
2010 {
2011     if (!tx.IsCoinBase())
2012     {
2013         if (!Consensus::CheckTxInputs(tx, state, inputs, GetSpendHeight(inputs)))
2014             return false;
2015 
2016         if (pvChecks)
2017             pvChecks->reserve(tx.vin.size());
2018 
2019         // The first loop above does all the inexpensive checks.
2020         // Only if ALL inputs pass do we perform expensive ECDSA signature checks.
2021         // Helps prevent CPU exhaustion attacks.
2022 
2023         // Skip ECDSA signature verification when connecting blocks before the
2024         // last block chain checkpoint. Assuming the checkpoints are valid this
2025         // is safe because block merkle hashes are still computed and checked,
2026         // and any change will be caught at the next checkpoint. Of course, if
2027         // the checkpoint is for a chain that's invalid due to false scriptSigs
2028         // this optimization would allow an invalid chain to be accepted.
2029         if (fScriptChecks) {
2030             for (unsigned int i = 0; i < tx.vin.size(); i++) {
2031                 const COutPoint &prevout = tx.vin[i].prevout;
2032                 const CCoins* coins = inputs.AccessCoins(prevout.hash);
2033                 assert(coins);
2034 
2035                 // Verify signature
2036                 CScriptCheck check(*coins, tx, i, flags, cacheStore, &txdata);
2037                 if (pvChecks) {
2038                     pvChecks->push_back(CScriptCheck());
2039                     check.swap(pvChecks->back());
2040                 } else if (!check()) {
2041                     if (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) {
2042                         // Check whether the failure was caused by a
2043                         // non-mandatory script verification check, such as
2044                         // non-standard DER encodings or non-null dummy
2045                         // arguments; if so, don't trigger DoS protection to
2046                         // avoid splitting the network between upgraded and
2047                         // non-upgraded nodes.
2048                         CScriptCheck check2(*coins, tx, i,
2049                                 flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, cacheStore, &txdata);
2050                         if (check2())
2051                             return state.Invalid(false, REJECT_NONSTANDARD, strprintf("non-mandatory-script-verify-flag (%s)", ScriptErrorString(check.GetScriptError())));
2052                     }
2053                     // Failures of other flags indicate a transaction that is
2054                     // invalid in new blocks, e.g. a invalid P2SH. We DoS ban
2055                     // such nodes as they are not following the protocol. That
2056                     // said during an upgrade careful thought should be taken
2057                     // as to the correct behavior - we may want to continue
2058                     // peering with non-upgraded nodes even after soft-fork
2059                     // super-majority signaling has occurred.
2060                     return state.DoS(100,false, REJECT_INVALID, strprintf("mandatory-script-verify-flag-failed (%s)", ScriptErrorString(check.GetScriptError())));
2061                 }
2062             }
2063         }
2064     }
2065 
2066     return true;
2067 }
2068 
2069 namespace {
2070 
UndoWriteToDisk(const CBlockUndo & blockundo,CDiskBlockPos & pos,const uint256 & hashBlock,const CMessageHeader::MessageStartChars & messageStart)2071 bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
2072 {
2073     // Open history file to append
2074     CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
2075     if (fileout.IsNull())
2076         return error("%s: OpenUndoFile failed", __func__);
2077 
2078     // Write index header
2079     unsigned int nSize = fileout.GetSerializeSize(blockundo);
2080     fileout << FLATDATA(messageStart) << nSize;
2081 
2082     // Write undo data
2083     long fileOutPos = ftell(fileout.Get());
2084     if (fileOutPos < 0)
2085         return error("%s: ftell failed", __func__);
2086     pos.nPos = (unsigned int)fileOutPos;
2087     fileout << blockundo;
2088 
2089     // calculate & write checksum
2090     CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
2091     hasher << hashBlock;
2092     hasher << blockundo;
2093     fileout << hasher.GetHash();
2094 
2095     return true;
2096 }
2097 
UndoReadFromDisk(CBlockUndo & blockundo,const CDiskBlockPos & pos,const uint256 & hashBlock)2098 bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock)
2099 {
2100     // Open history file to read
2101     CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
2102     if (filein.IsNull())
2103         return error("%s: OpenUndoFile failed", __func__);
2104 
2105     // Read block
2106     uint256 hashChecksum;
2107     try {
2108         filein >> blockundo;
2109         filein >> hashChecksum;
2110     }
2111     catch (const std::exception& e) {
2112         return error("%s: Deserialize or I/O error - %s", __func__, e.what());
2113     }
2114 
2115     // Verify checksum
2116     CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
2117     hasher << hashBlock;
2118     hasher << blockundo;
2119     if (hashChecksum != hasher.GetHash())
2120         return error("%s: Checksum mismatch", __func__);
2121 
2122     return true;
2123 }
2124 
2125 /** Abort with a message */
AbortNode(const std::string & strMessage,const std::string & userMessage="")2126 bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
2127 {
2128     strMiscWarning = strMessage;
2129     LogPrintf("*** %s\n", strMessage);
2130     uiInterface.ThreadSafeMessageBox(
2131         userMessage.empty() ? _("Error: A fatal internal error occurred, see debug.log for details") : userMessage,
2132         "", CClientUIInterface::MSG_ERROR);
2133     StartShutdown();
2134     return false;
2135 }
2136 
AbortNode(CValidationState & state,const std::string & strMessage,const std::string & userMessage="")2137 bool AbortNode(CValidationState& state, const std::string& strMessage, const std::string& userMessage="")
2138 {
2139     AbortNode(strMessage, userMessage);
2140     return state.Error(strMessage);
2141 }
2142 
2143 } // anon namespace
2144 
2145 /**
2146  * Apply the undo operation of a CTxInUndo to the given chain state.
2147  * @param undo The undo object.
2148  * @param view The coins view to which to apply the changes.
2149  * @param out The out point that corresponds to the tx input.
2150  * @return True on success.
2151  */
ApplyTxInUndo(const CTxInUndo & undo,CCoinsViewCache & view,const COutPoint & out)2152 static bool ApplyTxInUndo(const CTxInUndo& undo, CCoinsViewCache& view, const COutPoint& out)
2153 {
2154     bool fClean = true;
2155 
2156     CCoinsModifier coins = view.ModifyCoins(out.hash);
2157     if (undo.nHeight != 0) {
2158         // undo data contains height: this is the last output of the prevout tx being spent
2159         if (!coins->IsPruned())
2160             fClean = fClean && error("%s: undo data overwriting existing transaction", __func__);
2161         coins->Clear();
2162         coins->fCoinBase = undo.fCoinBase;
2163         coins->nHeight = undo.nHeight;
2164         coins->nVersion = undo.nVersion;
2165     } else {
2166         if (coins->IsPruned())
2167             fClean = fClean && error("%s: undo data adding output to missing transaction", __func__);
2168     }
2169     if (coins->IsAvailable(out.n))
2170         fClean = fClean && error("%s: undo data overwriting existing output", __func__);
2171     if (coins->vout.size() < out.n+1)
2172         coins->vout.resize(out.n+1);
2173     coins->vout[out.n] = undo.txout;
2174 
2175     return fClean;
2176 }
2177 
DisconnectBlock(const CBlock & block,CValidationState & state,const CBlockIndex * pindex,CCoinsViewCache & view,bool * pfClean)2178 bool DisconnectBlock(const CBlock& block, CValidationState& state, const CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean)
2179 {
2180     assert(pindex->GetBlockHash() == view.GetBestBlock());
2181 
2182     if (pfClean)
2183         *pfClean = false;
2184 
2185     bool fClean = true;
2186 
2187     CBlockUndo blockUndo;
2188     CDiskBlockPos pos = pindex->GetUndoPos();
2189     if (pos.IsNull())
2190         return error("DisconnectBlock(): no undo data available");
2191     if (!UndoReadFromDisk(blockUndo, pos, pindex->pprev->GetBlockHash()))
2192         return error("DisconnectBlock(): failure reading undo data");
2193 
2194     if (blockUndo.vtxundo.size() + 1 != block.vtx.size())
2195         return error("DisconnectBlock(): block and undo data inconsistent");
2196 
2197     // undo transactions in reverse order
2198     for (int i = block.vtx.size() - 1; i >= 0; i--) {
2199         const CTransaction &tx = block.vtx[i];
2200         uint256 hash = tx.GetHash();
2201 
2202         // Check that all outputs are available and match the outputs in the block itself
2203         // exactly.
2204         {
2205         CCoinsModifier outs = view.ModifyCoins(hash);
2206         outs->ClearUnspendable();
2207 
2208         CCoins outsBlock(tx, pindex->nHeight);
2209         // The CCoins serialization does not serialize negative numbers.
2210         // No network rules currently depend on the version here, so an inconsistency is harmless
2211         // but it must be corrected before txout nversion ever influences a network rule.
2212         if (outsBlock.nVersion < 0)
2213             outs->nVersion = outsBlock.nVersion;
2214         if (*outs != outsBlock)
2215             fClean = fClean && error("DisconnectBlock(): added transaction mismatch? database corrupted");
2216 
2217         // remove outputs
2218         outs->Clear();
2219         }
2220 
2221         // restore inputs
2222         if (i > 0) { // not coinbases
2223             const CTxUndo &txundo = blockUndo.vtxundo[i-1];
2224             if (txundo.vprevout.size() != tx.vin.size())
2225                 return error("DisconnectBlock(): transaction and undo data inconsistent");
2226             for (unsigned int j = tx.vin.size(); j-- > 0;) {
2227                 const COutPoint &out = tx.vin[j].prevout;
2228                 const CTxInUndo &undo = txundo.vprevout[j];
2229                 if (!ApplyTxInUndo(undo, view, out))
2230                     fClean = false;
2231             }
2232         }
2233     }
2234 
2235     // move best block pointer to prevout block
2236     view.SetBestBlock(pindex->pprev->GetBlockHash());
2237 
2238     if (pfClean) {
2239         *pfClean = fClean;
2240         return true;
2241     }
2242 
2243     return fClean;
2244 }
2245 
FlushBlockFile(bool fFinalize=false)2246 void static FlushBlockFile(bool fFinalize = false)
2247 {
2248     LOCK(cs_LastBlockFile);
2249 
2250     CDiskBlockPos posOld(nLastBlockFile, 0);
2251 
2252     FILE *fileOld = OpenBlockFile(posOld);
2253     if (fileOld) {
2254         if (fFinalize)
2255             TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nSize);
2256         FileCommit(fileOld);
2257         fclose(fileOld);
2258     }
2259 
2260     fileOld = OpenUndoFile(posOld);
2261     if (fileOld) {
2262         if (fFinalize)
2263             TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nUndoSize);
2264         FileCommit(fileOld);
2265         fclose(fileOld);
2266     }
2267 }
2268 
2269 bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
2270 
2271 static CCheckQueue<CScriptCheck> scriptcheckqueue(128);
2272 
ThreadScriptCheck()2273 void ThreadScriptCheck() {
2274     RenameThread("zetacoin-scriptch");
2275     scriptcheckqueue.Thread();
2276 }
2277 
2278 // Protected by cs_main
2279 VersionBitsCache versionbitscache;
2280 
ComputeBlockVersion(const CBlockIndex * pindexPrev,const Consensus::Params & params)2281 int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
2282 {
2283     LOCK(cs_main);
2284     int32_t nVersion = VERSIONBITS_TOP_BITS;
2285 
2286     for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) {
2287         ThresholdState state = VersionBitsState(pindexPrev, params, (Consensus::DeploymentPos)i, versionbitscache);
2288         if (state == THRESHOLD_LOCKED_IN || state == THRESHOLD_STARTED) {
2289             nVersion |= VersionBitsMask(params, (Consensus::DeploymentPos)i);
2290         }
2291     }
2292 
2293     return nVersion;
2294 }
2295 
2296 /**
2297  * Threshold condition checker that triggers when unknown versionbits are seen on the network.
2298  */
2299 class WarningBitsConditionChecker : public AbstractThresholdConditionChecker
2300 {
2301 private:
2302     int bit;
2303 
2304 public:
WarningBitsConditionChecker(int bitIn)2305     WarningBitsConditionChecker(int bitIn) : bit(bitIn) {}
2306 
BeginTime(const Consensus::Params & params) const2307     int64_t BeginTime(const Consensus::Params& params) const { return 0; }
EndTime(const Consensus::Params & params) const2308     int64_t EndTime(const Consensus::Params& params) const { return std::numeric_limits<int64_t>::max(); }
Period(const Consensus::Params & params) const2309     int Period(const Consensus::Params& params) const { return params.nMinerConfirmationWindow; }
Threshold(const Consensus::Params & params) const2310     int Threshold(const Consensus::Params& params) const { return params.nRuleChangeActivationThreshold; }
2311 
Condition(const CBlockIndex * pindex,const Consensus::Params & params) const2312     bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const
2313     {
2314         return ((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) &&
2315                ((pindex->nVersion >> bit) & 1) != 0 &&
2316                ((ComputeBlockVersion(pindex->pprev, params) >> bit) & 1) == 0;
2317     }
2318 };
2319 
2320 // Protected by cs_main
2321 static ThresholdConditionCache warningcache[VERSIONBITS_NUM_BITS];
2322 
2323 static int64_t nTimeCheck = 0;
2324 static int64_t nTimeForks = 0;
2325 static int64_t nTimeVerify = 0;
2326 static int64_t nTimeConnect = 0;
2327 static int64_t nTimeIndex = 0;
2328 static int64_t nTimeCallbacks = 0;
2329 static int64_t nTimeTotal = 0;
2330 
ConnectBlock(const CBlock & block,CValidationState & state,CBlockIndex * pindex,CCoinsViewCache & view,const CChainParams & chainparams,bool fJustCheck)2331 bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex,
2332                   CCoinsViewCache& view, const CChainParams& chainparams, bool fJustCheck)
2333 {
2334     AssertLockHeld(cs_main);
2335 
2336     int64_t nTimeStart = GetTimeMicros();
2337 
2338     // Check it again in case a previous version let a bad block in
2339     if (!CheckBlock(block, state, chainparams.GetConsensus(), !fJustCheck, !fJustCheck))
2340         return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
2341 
2342     // verify that the view's current state corresponds to the previous block
2343     uint256 hashPrevBlock = pindex->pprev == NULL ? uint256() : pindex->pprev->GetBlockHash();
2344     assert(hashPrevBlock == view.GetBestBlock());
2345 
2346     // Special case for the genesis block, skipping connection of its transactions
2347     // (its coinbase is unspendable)
2348     if (block.GetHash() == chainparams.GetConsensus().hashGenesisBlock) {
2349         if (!fJustCheck)
2350             view.SetBestBlock(pindex->GetBlockHash());
2351         return true;
2352     }
2353 
2354     bool fScriptChecks = true;
2355     if (fCheckpointsEnabled) {
2356         CBlockIndex *pindexLastCheckpoint = Checkpoints::GetLastCheckpoint(chainparams.Checkpoints());
2357         if (pindexLastCheckpoint && pindexLastCheckpoint->GetAncestor(pindex->nHeight) == pindex) {
2358             // This block is an ancestor of a checkpoint: disable script checks
2359             fScriptChecks = false;
2360         }
2361     }
2362 
2363     int64_t nTime1 = GetTimeMicros(); nTimeCheck += nTime1 - nTimeStart;
2364     LogPrint("bench", "    - Sanity checks: %.2fms [%.2fs]\n", 0.001 * (nTime1 - nTimeStart), nTimeCheck * 0.000001);
2365 
2366     // Do not allow blocks that contain transactions which 'overwrite' older transactions,
2367     // unless those are already completely spent.
2368     // If such overwrites are allowed, coinbases and transactions depending upon those
2369     // can be duplicated to remove the ability to spend the first instance -- even after
2370     // being sent to another address.
2371     // See BIP30 and http://r6.ca/blog/20120206T005236Z.html for more information.
2372     // This logic is not necessary for memory pool transactions, as AcceptToMemoryPool
2373     // already refuses previously-known transaction ids entirely.
2374     // This rule was originally applied to all blocks with a timestamp after March 15, 2012, 0:00 UTC.
2375     // Now that the whole chain is irreversibly beyond that time it is applied to all blocks except the
2376     // two in the chain that violate it. This prevents exploiting the issue against nodes during their
2377     // initial block download.
2378     bool fEnforceBIP30 = (!pindex->phashBlock); // Enforce on CreateNewBlock invocations which don't have a hash.
2379 
2380     // Once BIP34 activated it was not possible to create new duplicate coinbases and thus other than starting
2381     // with the 2 existing duplicate coinbase pairs, not possible to create overwriting txs.  But by the
2382     // time BIP34 activated, in each of the existing pairs the duplicate coinbase had overwritten the first
2383     // before the first had been spent.  Since those coinbases are sufficiently buried its no longer possible to create further
2384     // duplicate transactions descending from the known pairs either.
2385     // 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.
2386     // CBlockIndex *pindexBIP34height = pindex->pprev->GetAncestor(chainparams.GetConsensus().BIP34Height);
2387     //Only continue to enforce if we're below BIP34 activation height or the block hash at that height doesn't correspond.
2388     // fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->GetBlockHash() == chainparams.GetConsensus().BIP34Hash));
2389 
2390     if (fEnforceBIP30) {
2391         BOOST_FOREACH(const CTransaction& tx, block.vtx) {
2392             const CCoins* coins = view.AccessCoins(tx.GetHash());
2393             if (coins && !coins->IsPruned())
2394                 return state.DoS(100, error("ConnectBlock(): tried to overwrite transaction"),
2395                                  REJECT_INVALID, "bad-txns-BIP30");
2396         }
2397     }
2398 
2399     // BIP16 active
2400     bool fStrictPayToScriptHash = true;
2401     unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE;
2402 
2403     if (pindex->nHeight >= BIP65_HEIGHT) {
2404         flags |= SCRIPT_VERIFY_DERSIG;
2405         flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
2406     }
2407 
2408     // Start enforcing BIP68 (sequence locks) and BIP112 (CHECKSEQUENCEVERIFY) using versionbits logic.
2409     int nLockTimeFlags = 0;
2410     if (VersionBitsState(pindex->pprev, chainparams.GetConsensus(), Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
2411         flags |= SCRIPT_VERIFY_CHECKSEQUENCEVERIFY;
2412         nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE;
2413     }
2414 
2415     // Start enforcing WITNESS rules using versionbits logic.
2416     if (IsWitnessEnabled(pindex->pprev, chainparams.GetConsensus())) {
2417         flags |= SCRIPT_VERIFY_WITNESS;
2418         flags |= SCRIPT_VERIFY_NULLDUMMY;
2419     }
2420 
2421     int64_t nTime2 = GetTimeMicros(); nTimeForks += nTime2 - nTime1;
2422     LogPrint("bench", "    - Fork checks: %.2fms [%.2fs]\n", 0.001 * (nTime2 - nTime1), nTimeForks * 0.000001);
2423 
2424     CBlockUndo blockundo;
2425 
2426     CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL);
2427 
2428     std::vector<uint256> vOrphanErase;
2429     std::vector<int> prevheights;
2430     CAmount nFees = 0;
2431     int nInputs = 0;
2432     int64_t nSigOpsCost = 0;
2433     CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
2434     std::vector<std::pair<uint256, CDiskTxPos> > vPos;
2435     vPos.reserve(block.vtx.size());
2436     blockundo.vtxundo.reserve(block.vtx.size() - 1);
2437     std::vector<PrecomputedTransactionData> txdata;
2438     txdata.reserve(block.vtx.size()); // Required so that pointers to individual PrecomputedTransactionData don't get invalidated
2439     for (unsigned int i = 0; i < block.vtx.size(); i++)
2440     {
2441         const CTransaction &tx = block.vtx[i];
2442 
2443         nInputs += tx.vin.size();
2444 
2445         if (!tx.IsCoinBase())
2446         {
2447             if (!view.HaveInputs(tx))
2448                 return state.DoS(100, error("ConnectBlock(): inputs missing/spent"),
2449                                  REJECT_INVALID, "bad-txns-inputs-missingorspent");
2450 
2451             // Check that transaction is BIP68 final
2452             // BIP68 lock checks (as opposed to nLockTime checks) must
2453             // be in ConnectBlock because they require the UTXO set
2454             prevheights.resize(tx.vin.size());
2455             for (size_t j = 0; j < tx.vin.size(); j++) {
2456                 prevheights[j] = view.AccessCoins(tx.vin[j].prevout.hash)->nHeight;
2457             }
2458 
2459             // Which orphan pool entries must we evict?
2460             for (size_t j = 0; j < tx.vin.size(); j++) {
2461                 auto itByPrev = mapOrphanTransactionsByPrev.find(tx.vin[j].prevout);
2462                 if (itByPrev == mapOrphanTransactionsByPrev.end()) continue;
2463                 for (auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
2464                     const CTransaction& orphanTx = (*mi)->second.tx;
2465                     const uint256& orphanHash = orphanTx.GetHash();
2466                     vOrphanErase.push_back(orphanHash);
2467                 }
2468             }
2469 
2470             if (!SequenceLocks(tx, nLockTimeFlags, &prevheights, *pindex)) {
2471                 return state.DoS(100, error("%s: contains a non-BIP68-final transaction", __func__),
2472                                  REJECT_INVALID, "bad-txns-nonfinal");
2473             }
2474         }
2475 
2476         // GetTransactionSigOpCost counts 3 types of sigops:
2477         // * legacy (always)
2478         // * p2sh (when P2SH enabled in flags and excludes coinbase)
2479         // * witness (when witness enabled in flags and excludes coinbase)
2480         nSigOpsCost += GetTransactionSigOpCost(tx, view, flags);
2481         if (nSigOpsCost > MAX_BLOCK_SIGOPS_COST)
2482             return state.DoS(100, error("ConnectBlock(): too many sigops"),
2483                              REJECT_INVALID, "bad-blk-sigops");
2484 
2485         txdata.emplace_back(tx);
2486         if (!tx.IsCoinBase())
2487         {
2488             nFees += view.GetValueIn(tx)-tx.GetValueOut();
2489 
2490             std::vector<CScriptCheck> vChecks;
2491             bool fCacheResults = fJustCheck; /* Don't cache results if we're actually connecting blocks (still consult the cache, though) */
2492             if (!CheckInputs(tx, state, view, fScriptChecks, flags, fCacheResults, txdata[i], nScriptCheckThreads ? &vChecks : NULL))
2493                 return error("ConnectBlock(): CheckInputs on %s failed with %s",
2494                     tx.GetHash().ToString(), FormatStateMessage(state));
2495             control.Add(vChecks);
2496         }
2497 
2498         CTxUndo undoDummy;
2499         if (i > 0) {
2500             blockundo.vtxundo.push_back(CTxUndo());
2501         }
2502         UpdateCoins(tx, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight);
2503 
2504         vPos.push_back(std::make_pair(tx.GetHash(), pos));
2505         pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
2506     }
2507     int64_t nTime3 = GetTimeMicros(); nTimeConnect += nTime3 - nTime2;
2508     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);
2509 
2510     CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, chainparams.GetConsensus());
2511     if (block.vtx[0].GetValueOut() > blockReward)
2512         return state.DoS(100,
2513                          error("ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)",
2514                                block.vtx[0].GetValueOut(), blockReward),
2515                                REJECT_INVALID, "bad-cb-amount");
2516 
2517     if (!control.Wait())
2518         return state.DoS(100, false);
2519     int64_t nTime4 = GetTimeMicros(); nTimeVerify += nTime4 - nTime2;
2520     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);
2521 
2522     if (fJustCheck)
2523         return true;
2524 
2525     // Write undo information to disk
2526     if (pindex->GetUndoPos().IsNull() || !pindex->IsValid(BLOCK_VALID_SCRIPTS))
2527     {
2528         if (pindex->GetUndoPos().IsNull()) {
2529             CDiskBlockPos pos;
2530             if (!FindUndoPos(state, pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
2531                 return error("ConnectBlock(): FindUndoPos failed");
2532             if (!UndoWriteToDisk(blockundo, pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart()))
2533                 return AbortNode(state, "Failed to write undo data");
2534 
2535             // update nUndoPos in block index
2536             pindex->nUndoPos = pos.nPos;
2537             pindex->nStatus |= BLOCK_HAVE_UNDO;
2538         }
2539 
2540         pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
2541         setDirtyBlockIndex.insert(pindex);
2542     }
2543 
2544     if (fTxIndex)
2545         if (!pblocktree->WriteTxIndex(vPos))
2546             return AbortNode(state, "Failed to write transaction index");
2547 
2548     // add this block to the view's block chain
2549     view.SetBestBlock(pindex->GetBlockHash());
2550 
2551     int64_t nTime5 = GetTimeMicros(); nTimeIndex += nTime5 - nTime4;
2552     LogPrint("bench", "    - Index writing: %.2fms [%.2fs]\n", 0.001 * (nTime5 - nTime4), nTimeIndex * 0.000001);
2553 
2554     // Watch for changes to the previous coinbase transaction.
2555     static uint256 hashPrevBestCoinBase;
2556     GetMainSignals().UpdatedTransaction(hashPrevBestCoinBase);
2557     hashPrevBestCoinBase = block.vtx[0].GetHash();
2558 
2559     // Erase orphan transactions include or precluded by this block
2560     if (vOrphanErase.size()) {
2561         int nErased = 0;
2562         BOOST_FOREACH(uint256 &orphanHash, vOrphanErase) {
2563             nErased += EraseOrphanTx(orphanHash);
2564         }
2565         LogPrint("mempool", "Erased %d orphan tx included or conflicted by block\n", nErased);
2566     }
2567 
2568     int64_t nTime6 = GetTimeMicros(); nTimeCallbacks += nTime6 - nTime5;
2569     LogPrint("bench", "    - Callbacks: %.2fms [%.2fs]\n", 0.001 * (nTime6 - nTime5), nTimeCallbacks * 0.000001);
2570 
2571     return true;
2572 }
2573 
2574 enum FlushStateMode {
2575     FLUSH_STATE_NONE,
2576     FLUSH_STATE_IF_NEEDED,
2577     FLUSH_STATE_PERIODIC,
2578     FLUSH_STATE_ALWAYS
2579 };
2580 
2581 /**
2582  * Update the on-disk chain state.
2583  * The caches and indexes are flushed depending on the mode we're called with
2584  * if they're too large, if it's been a while since the last write,
2585  * or always and in all cases if we're in prune mode and are deleting files.
2586  */
FlushStateToDisk(CValidationState & state,FlushStateMode mode)2587 bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
2588     const CChainParams& chainparams = Params();
2589     LOCK2(cs_main, cs_LastBlockFile);
2590     static int64_t nLastWrite = 0;
2591     static int64_t nLastFlush = 0;
2592     static int64_t nLastSetChain = 0;
2593     std::set<int> setFilesToPrune;
2594     bool fFlushForPrune = false;
2595     try {
2596     if (fPruneMode && fCheckForPruning && !fReindex) {
2597         FindFilesToPrune(setFilesToPrune, chainparams.PruneAfterHeight());
2598         fCheckForPruning = false;
2599         if (!setFilesToPrune.empty()) {
2600             fFlushForPrune = true;
2601             if (!fHavePruned) {
2602                 pblocktree->WriteFlag("prunedblockfiles", true);
2603                 fHavePruned = true;
2604             }
2605         }
2606     }
2607     int64_t nNow = GetTimeMicros();
2608     // Avoid writing/flushing immediately after startup.
2609     if (nLastWrite == 0) {
2610         nLastWrite = nNow;
2611     }
2612     if (nLastFlush == 0) {
2613         nLastFlush = nNow;
2614     }
2615     if (nLastSetChain == 0) {
2616         nLastSetChain = nNow;
2617     }
2618     size_t cacheSize = pcoinsTip->DynamicMemoryUsage();
2619     // The cache is large and close to the limit, but we have time now (not in the middle of a block processing).
2620     bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize * (10.0/9) > nCoinCacheUsage;
2621     // The cache is over the limit, we have to write now.
2622     bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && cacheSize > nCoinCacheUsage;
2623     // 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.
2624     bool fPeriodicWrite = mode == FLUSH_STATE_PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
2625     // It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
2626     bool fPeriodicFlush = mode == FLUSH_STATE_PERIODIC && nNow > nLastFlush + (int64_t)DATABASE_FLUSH_INTERVAL * 1000000;
2627     // Combine all conditions that result in a full cache flush.
2628     bool fDoFullFlush = (mode == FLUSH_STATE_ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
2629     // Write blocks and block index to disk.
2630     if (fDoFullFlush || fPeriodicWrite) {
2631         // Depend on nMinDiskSpace to ensure we can write block index
2632         if (!CheckDiskSpace(0))
2633             return state.Error("out of disk space");
2634         // First make sure all block and undo data is flushed to disk.
2635         FlushBlockFile();
2636         // Then update all block file information (which may refer to block and undo files).
2637         {
2638             std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
2639             vFiles.reserve(setDirtyFileInfo.size());
2640             for (set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
2641                 vFiles.push_back(make_pair(*it, &vinfoBlockFile[*it]));
2642                 setDirtyFileInfo.erase(it++);
2643             }
2644             std::vector<const CBlockIndex*> vBlocks;
2645             vBlocks.reserve(setDirtyBlockIndex.size());
2646             for (set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
2647                 vBlocks.push_back(*it);
2648                 setDirtyBlockIndex.erase(it++);
2649             }
2650             if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
2651                 return AbortNode(state, "Files to write to block index database");
2652             }
2653         }
2654         // Finally remove any pruned files
2655         if (fFlushForPrune)
2656             UnlinkPrunedFiles(setFilesToPrune);
2657         nLastWrite = nNow;
2658     }
2659     // Flush best chain related state. This can only be done if the blocks / block index write was also done.
2660     if (fDoFullFlush) {
2661         // Typical CCoins structures on disk are around 128 bytes in size.
2662         // Pushing a new one to the database can cause it to be written
2663         // twice (once in the log, and once in the tables). This is already
2664         // an overestimation, as most will delete an existing entry or
2665         // overwrite one. Still, use a conservative safety factor of 2.
2666         if (!CheckDiskSpace(128 * 2 * 2 * pcoinsTip->GetCacheSize()))
2667             return state.Error("out of disk space");
2668         // Flush the chainstate (which may refer to block index entries).
2669         if (!pcoinsTip->Flush())
2670             return AbortNode(state, "Failed to write to coin database");
2671         nLastFlush = nNow;
2672     }
2673     if (fDoFullFlush || ((mode == FLUSH_STATE_ALWAYS || mode == FLUSH_STATE_PERIODIC) && nNow > nLastSetChain + (int64_t)DATABASE_WRITE_INTERVAL * 1000000)) {
2674         // Update best block in wallet (so we can detect restored wallets).
2675         GetMainSignals().SetBestChain(chainActive.GetLocator());
2676         nLastSetChain = nNow;
2677     }
2678     } catch (const std::runtime_error& e) {
2679         return AbortNode(state, std::string("System error while flushing: ") + e.what());
2680     }
2681     return true;
2682 }
2683 
FlushStateToDisk()2684 void FlushStateToDisk() {
2685     CValidationState state;
2686     FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
2687 }
2688 
PruneAndFlush()2689 void PruneAndFlush() {
2690     CValidationState state;
2691     fCheckForPruning = true;
2692     FlushStateToDisk(state, FLUSH_STATE_NONE);
2693 }
2694 
2695 /** Update chainActive and related internal data structures. */
UpdateTip(CBlockIndex * pindexNew,const CChainParams & chainParams)2696 void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
2697     chainActive.SetTip(pindexNew);
2698 
2699     // New best block
2700     nTimeBestReceived = GetTime();
2701     mempool.AddTransactionsUpdated(1);
2702 
2703     cvBlockChange.notify_all();
2704 
2705     static bool fWarned = false;
2706     std::vector<std::string> warningMessages;
2707     if (!IsInitialBlockDownload())
2708     {
2709         int nUpgraded = 0;
2710         const CBlockIndex* pindex = chainActive.Tip();
2711         for (int bit = 0; bit < VERSIONBITS_NUM_BITS; bit++) {
2712             WarningBitsConditionChecker checker(bit);
2713             ThresholdState state = checker.GetStateFor(pindex, chainParams.GetConsensus(), warningcache[bit]);
2714             if (state == THRESHOLD_ACTIVE || state == THRESHOLD_LOCKED_IN) {
2715                 if (state == THRESHOLD_ACTIVE) {
2716                     strMiscWarning = strprintf(_("Warning: unknown new rules activated (versionbit %i)"), bit);
2717                     if (!fWarned) {
2718                         AlertNotify(strMiscWarning);
2719                         fWarned = true;
2720                     }
2721                 } else {
2722                     warningMessages.push_back(strprintf("unknown new rules are about to activate (versionbit %i)", bit));
2723                 }
2724             }
2725         }
2726         // Check the version of the last 400 blocks to see if we need to upgrade:
2727         for (int i = 0; i < 400 && pindex != NULL; i++)
2728         {
2729             int32_t nExpectedVersion = ComputeBlockVersion(pindex->pprev, chainParams.GetConsensus());
2730             if (pindex->nVersion > VERSIONBITS_LAST_OLD_BLOCK_VERSION && (pindex->nVersion & ~nExpectedVersion) != 0)
2731                 ++nUpgraded;
2732             pindex = pindex->pprev;
2733         }
2734         if (nUpgraded > 300)
2735             warningMessages.push_back(strprintf("%d of last 100 blocks have unexpected version", nUpgraded));
2736         if (nUpgraded > 200)
2737         {
2738             // strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
2739             strMiscWarning = _("Warning: Unknown block versions being mined! It's possible unknown rules are in effect");
2740             if (!fWarned) {
2741                 AlertNotify(strMiscWarning);
2742                 fWarned = true;
2743             }
2744         }
2745     }
2746     LogPrintf("%s: new best=%s height=%d version=0x%08x log2_work=%.8g tx=%lu date='%s' progress=%f cache=%.1fMiB(%utx)", __func__,
2747       chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), chainActive.Tip()->nVersion,
2748       log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
2749       DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
2750       Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip()), pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
2751     if (!warningMessages.empty())
2752         LogPrintf(" warning='%s'", boost::algorithm::join(warningMessages, ", "));
2753     LogPrintf("\n");
2754 
2755 }
2756 
2757 /** Disconnect chainActive's tip. You probably want to call mempool.removeForReorg and manually re-limit mempool size after this, with cs_main held. */
DisconnectTip(CValidationState & state,const CChainParams & chainparams,bool fBare=false)2758 bool static DisconnectTip(CValidationState& state, const CChainParams& chainparams, bool fBare = false)
2759 {
2760     CBlockIndex *pindexDelete = chainActive.Tip();
2761     assert(pindexDelete);
2762     // Read block from disk.
2763     CBlock block;
2764     if (!ReadBlockFromDisk(block, pindexDelete, chainparams.GetConsensus()))
2765         return AbortNode(state, "Failed to read block");
2766     // Apply the block atomically to the chain state.
2767     int64_t nStart = GetTimeMicros();
2768     {
2769         CCoinsViewCache view(pcoinsTip);
2770         if (!DisconnectBlock(block, state, pindexDelete, view))
2771             return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
2772         assert(view.Flush());
2773     }
2774     LogPrint("bench", "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
2775     // Write the chain state to disk, if necessary.
2776     if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED))
2777         return false;
2778 
2779     if (!fBare) {
2780         // Resurrect mempool transactions from the disconnected block.
2781         std::vector<uint256> vHashUpdate;
2782         BOOST_FOREACH(const CTransaction &tx, block.vtx) {
2783             // ignore validation errors in resurrected transactions
2784             list<CTransaction> removed;
2785             CValidationState stateDummy;
2786             if (tx.IsCoinBase() || !AcceptToMemoryPool(mempool, stateDummy, tx, false, NULL, true)) {
2787                 mempool.removeRecursive(tx, removed);
2788             } else if (mempool.exists(tx.GetHash())) {
2789                 vHashUpdate.push_back(tx.GetHash());
2790             }
2791         }
2792         // AcceptToMemoryPool/addUnchecked all assume that new mempool entries have
2793         // no in-mempool children, which is generally not true when adding
2794         // previously-confirmed transactions back to the mempool.
2795         // UpdateTransactionsFromBlock finds descendants of any transactions in this
2796         // block that were added back and cleans up the mempool state.
2797         mempool.UpdateTransactionsFromBlock(vHashUpdate);
2798     }
2799 
2800     // Update chainActive and related variables.
2801     UpdateTip(pindexDelete->pprev, chainparams);
2802     // Let wallets know transactions went from 1-confirmed to
2803     // 0-confirmed or conflicted:
2804     BOOST_FOREACH(const CTransaction &tx, block.vtx) {
2805         SyncWithWallets(tx, pindexDelete->pprev, NULL);
2806     }
2807     return true;
2808 }
2809 
2810 static int64_t nTimeReadFromDisk = 0;
2811 static int64_t nTimeConnectTotal = 0;
2812 static int64_t nTimeFlush = 0;
2813 static int64_t nTimeChainState = 0;
2814 static int64_t nTimePostConnect = 0;
2815 
2816 /**
2817  * Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
2818  * corresponding to pindexNew, to bypass loading it again from disk.
2819  */
ConnectTip(CValidationState & state,const CChainParams & chainparams,CBlockIndex * pindexNew,const CBlock * pblock)2820 bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const CBlock* pblock)
2821 {
2822     assert(pindexNew->pprev == chainActive.Tip());
2823     // Read block from disk.
2824     int64_t nTime1 = GetTimeMicros();
2825     CBlock block;
2826     if (!pblock) {
2827         if (!ReadBlockFromDisk(block, pindexNew, chainparams.GetConsensus()))
2828             return AbortNode(state, "Failed to read block");
2829         pblock = &block;
2830     }
2831     // Apply the block atomically to the chain state.
2832     int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1;
2833     int64_t nTime3;
2834     LogPrint("bench", "  - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * 0.001, nTimeReadFromDisk * 0.000001);
2835     {
2836         CCoinsViewCache view(pcoinsTip);
2837         bool rv = ConnectBlock(*pblock, state, pindexNew, view, chainparams);
2838         GetMainSignals().BlockChecked(*pblock, state);
2839         if (!rv) {
2840             if (state.IsInvalid())
2841                 InvalidBlockFound(pindexNew, state);
2842             return error("ConnectTip(): ConnectBlock %s failed", pindexNew->GetBlockHash().ToString());
2843         }
2844         mapBlockSource.erase(pindexNew->GetBlockHash());
2845         nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2;
2846         LogPrint("bench", "  - Connect total: %.2fms [%.2fs]\n", (nTime3 - nTime2) * 0.001, nTimeConnectTotal * 0.000001);
2847         assert(view.Flush());
2848     }
2849     int64_t nTime4 = GetTimeMicros(); nTimeFlush += nTime4 - nTime3;
2850     LogPrint("bench", "  - Flush: %.2fms [%.2fs]\n", (nTime4 - nTime3) * 0.001, nTimeFlush * 0.000001);
2851     // Write the chain state to disk, if necessary.
2852     if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED))
2853         return false;
2854     int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
2855     LogPrint("bench", "  - Writing chainstate: %.2fms [%.2fs]\n", (nTime5 - nTime4) * 0.001, nTimeChainState * 0.000001);
2856     // Remove conflicting transactions from the mempool.
2857     list<CTransaction> txConflicted;
2858     mempool.removeForBlock(pblock->vtx, pindexNew->nHeight, txConflicted, !IsInitialBlockDownload());
2859     // Update chainActive & related variables.
2860     UpdateTip(pindexNew, chainparams);
2861     // Tell wallet about transactions that went from mempool
2862     // to conflicted:
2863     BOOST_FOREACH(const CTransaction &tx, txConflicted) {
2864         SyncWithWallets(tx, pindexNew, NULL);
2865     }
2866     // ... and about transactions that got confirmed:
2867     BOOST_FOREACH(const CTransaction &tx, pblock->vtx) {
2868         SyncWithWallets(tx, pindexNew, pblock);
2869     }
2870 
2871     int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
2872     LogPrint("bench", "  - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001);
2873     LogPrint("bench", "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001);
2874     return true;
2875 }
2876 
2877 /**
2878  * Return the tip of the chain with the most work in it, that isn't
2879  * known to be invalid (it's however far from certain to be valid).
2880  */
FindMostWorkChain()2881 static CBlockIndex* FindMostWorkChain() {
2882     do {
2883         CBlockIndex *pindexNew = NULL;
2884 
2885         // Find the best candidate header.
2886         {
2887             std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexCandidates.rbegin();
2888             if (it == setBlockIndexCandidates.rend())
2889                 return NULL;
2890             pindexNew = *it;
2891         }
2892 
2893         // Check whether all blocks on the path between the currently active chain and the candidate are valid.
2894         // Just going until the active chain is an optimization, as we know all blocks in it are valid already.
2895         CBlockIndex *pindexTest = pindexNew;
2896         bool fInvalidAncestor = false;
2897         while (pindexTest && !chainActive.Contains(pindexTest)) {
2898             assert(pindexTest->nChainTx || pindexTest->nHeight == 0);
2899 
2900             // Pruned nodes may have entries in setBlockIndexCandidates for
2901             // which block files have been deleted.  Remove those as candidates
2902             // for the most work chain if we come across them; we can't switch
2903             // to a chain unless we have all the non-active-chain parent blocks.
2904             bool fFailedChain = pindexTest->nStatus & BLOCK_FAILED_MASK;
2905             bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
2906             if (fFailedChain || fMissingData) {
2907                 // Candidate chain is not usable (either invalid or missing data)
2908                 if (fFailedChain && (pindexBestInvalid == NULL || pindexNew->nChainWork > pindexBestInvalid->nChainWork))
2909                     pindexBestInvalid = pindexNew;
2910                 CBlockIndex *pindexFailed = pindexNew;
2911                 // Remove the entire chain from the set.
2912                 while (pindexTest != pindexFailed) {
2913                     if (fFailedChain) {
2914                         pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
2915                     } else if (fMissingData) {
2916                         // If we're missing data, then add back to mapBlocksUnlinked,
2917                         // so that if the block arrives in the future we can try adding
2918                         // to setBlockIndexCandidates again.
2919                         mapBlocksUnlinked.insert(std::make_pair(pindexFailed->pprev, pindexFailed));
2920                     }
2921                     setBlockIndexCandidates.erase(pindexFailed);
2922                     pindexFailed = pindexFailed->pprev;
2923                 }
2924                 setBlockIndexCandidates.erase(pindexTest);
2925                 fInvalidAncestor = true;
2926                 break;
2927             }
2928             pindexTest = pindexTest->pprev;
2929         }
2930         if (!fInvalidAncestor)
2931             return pindexNew;
2932     } while(true);
2933 }
2934 
2935 /** Delete all entries in setBlockIndexCandidates that are worse than the current tip. */
PruneBlockIndexCandidates()2936 static void PruneBlockIndexCandidates() {
2937     // Note that we can't delete the current block itself, as we may need to return to it later in case a
2938     // reorganization to a better block fails.
2939     std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
2940     while (it != setBlockIndexCandidates.end() && setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) {
2941         setBlockIndexCandidates.erase(it++);
2942     }
2943     // Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates.
2944 
2945     // Assertion fails
2946     //assert(!setBlockIndexCandidates.empty());
2947 }
2948 
2949 /**
2950  * Try to make some progress towards making pindexMostWork the active block.
2951  * pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
2952  */
ActivateBestChainStep(CValidationState & state,const CChainParams & chainparams,CBlockIndex * pindexMostWork,const CBlock * pblock,bool & fInvalidFound)2953 static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const CBlock* pblock, bool& fInvalidFound)
2954 {
2955     AssertLockHeld(cs_main);
2956     const CBlockIndex *pindexOldTip = chainActive.Tip();
2957     const CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork);
2958 
2959     // Disconnect active blocks which are no longer in the best chain.
2960     bool fBlocksDisconnected = false;
2961     while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
2962         if (!DisconnectTip(state, chainparams))
2963             return false;
2964         fBlocksDisconnected = true;
2965     }
2966 
2967     // Build list of new blocks to connect.
2968     std::vector<CBlockIndex*> vpindexToConnect;
2969     bool fContinue = true;
2970     int nHeight = pindexFork ? pindexFork->nHeight : -1;
2971     while (fContinue && nHeight != pindexMostWork->nHeight) {
2972         // Don't iterate the entire list of potential improvements toward the best tip, as we likely only need
2973         // a few blocks along the way.
2974         int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight);
2975         vpindexToConnect.clear();
2976         vpindexToConnect.reserve(nTargetHeight - nHeight);
2977         CBlockIndex *pindexIter = pindexMostWork->GetAncestor(nTargetHeight);
2978         while (pindexIter && pindexIter->nHeight != nHeight) {
2979             vpindexToConnect.push_back(pindexIter);
2980             pindexIter = pindexIter->pprev;
2981         }
2982         nHeight = nTargetHeight;
2983 
2984         // Connect new blocks.
2985         BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) {
2986             if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL)) {
2987                 if (state.IsInvalid()) {
2988                     // The block violates a consensus rule.
2989                     if (!state.CorruptionPossible())
2990                         InvalidChainFound(vpindexToConnect.back());
2991                     state = CValidationState();
2992                     fInvalidFound = true;
2993                     fContinue = false;
2994                     break;
2995                 } else {
2996                     // A system error occurred (disk space, database error, ...).
2997                     return false;
2998                 }
2999             } else {
3000                 PruneBlockIndexCandidates();
3001                 if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
3002                     // We're in a better position than we were. Return temporarily to release the lock.
3003                     fContinue = false;
3004                     break;
3005                 }
3006             }
3007         }
3008     }
3009 
3010     if (fBlocksDisconnected) {
3011         mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
3012         LimitMempoolSize(mempool, GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
3013     }
3014     mempool.check(pcoinsTip);
3015 
3016     // Callbacks/notifications for a new best chain.
3017     if (fInvalidFound)
3018         CheckForkWarningConditionsOnNewFork(vpindexToConnect.back());
3019     else
3020         CheckForkWarningConditions();
3021 
3022     return true;
3023 }
3024 
NotifyHeaderTip()3025 static void NotifyHeaderTip() {
3026     bool fNotify = false;
3027     bool fInitialBlockDownload = false;
3028     static CBlockIndex* pindexHeaderOld = NULL;
3029     CBlockIndex* pindexHeader = NULL;
3030     {
3031         LOCK(cs_main);
3032         if (!setBlockIndexCandidates.empty()) {
3033             pindexHeader = *setBlockIndexCandidates.rbegin();
3034         }
3035         if (pindexHeader != pindexHeaderOld) {
3036             fNotify = true;
3037             fInitialBlockDownload = IsInitialBlockDownload();
3038             pindexHeaderOld = pindexHeader;
3039         }
3040     }
3041     // Send block tip changed notifications without cs_main
3042     if (fNotify) {
3043         uiInterface.NotifyHeaderTip(fInitialBlockDownload, pindexHeader);
3044     }
3045 }
3046 
3047 /**
3048  * Make the best chain active, in multiple steps. The result is either failure
3049  * or an activated best chain. pblock is either NULL or a pointer to a block
3050  * that is already loaded (to avoid loading it again from disk).
3051  */
ActivateBestChain(CValidationState & state,const CChainParams & chainparams,const CBlock * pblock)3052 bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, const CBlock *pblock) {
3053     CBlockIndex *pindexMostWork = NULL;
3054     CBlockIndex *pindexNewTip = NULL;
3055     do {
3056         boost::this_thread::interruption_point();
3057         if (ShutdownRequested())
3058             break;
3059 
3060         const CBlockIndex *pindexFork;
3061         bool fInitialDownload;
3062         int nNewHeight;
3063         {
3064             LOCK(cs_main);
3065             CBlockIndex *pindexOldTip = chainActive.Tip();
3066             if (pindexMostWork == NULL) {
3067                 pindexMostWork = FindMostWorkChain();
3068             }
3069 
3070             // Whether we have anything to do at all.
3071             if (pindexMostWork == NULL || pindexMostWork == chainActive.Tip())
3072                 return true;
3073 
3074             bool fInvalidFound = false;
3075             if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL, fInvalidFound))
3076                 return false;
3077 
3078             if (fInvalidFound) {
3079                 // Wipe cache, we may need another branch now.
3080                 pindexMostWork = NULL;
3081             }
3082             pindexNewTip = chainActive.Tip();
3083             pindexFork = chainActive.FindFork(pindexOldTip);
3084             fInitialDownload = IsInitialBlockDownload();
3085             nNewHeight = chainActive.Height();
3086         }
3087         // When we reach this point, we switched to a new tip (stored in pindexNewTip).
3088 
3089         // Notifications/callbacks that can run without cs_main
3090         // Always notify the UI if a new block tip was connected
3091         if (pindexFork != pindexNewTip) {
3092             uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
3093 
3094             if (!fInitialDownload) {
3095                 // Find the hashes of all blocks that weren't previously in the best chain.
3096                 std::vector<uint256> vHashes;
3097                 CBlockIndex *pindexToAnnounce = pindexNewTip;
3098                 while (pindexToAnnounce != pindexFork) {
3099                     vHashes.push_back(pindexToAnnounce->GetBlockHash());
3100                     pindexToAnnounce = pindexToAnnounce->pprev;
3101                     if (vHashes.size() == MAX_BLOCKS_TO_ANNOUNCE) {
3102                         // Limit announcements in case of a huge reorganization.
3103                         // Rely on the peer's synchronization mechanism in that case.
3104                         break;
3105                     }
3106                 }
3107                 // Relay inventory, but don't relay old inventory during initial block download.
3108                 {
3109                     LOCK(cs_vNodes);
3110                     BOOST_FOREACH(CNode* pnode, vNodes) {
3111                         if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : 0)) {
3112                             BOOST_REVERSE_FOREACH(const uint256& hash, vHashes) {
3113                                 pnode->PushBlockHash(hash);
3114                             }
3115                         }
3116                     }
3117                 }
3118                 // Notify external listeners about the new tip.
3119                 if (!vHashes.empty()) {
3120                     GetMainSignals().UpdatedBlockTip(pindexNewTip);
3121                 }
3122             }
3123         }
3124     } while (pindexNewTip != pindexMostWork);
3125     CheckBlockIndex(chainparams.GetConsensus());
3126 
3127     // Write changes periodically to disk, after relay.
3128     if (!FlushStateToDisk(state, FLUSH_STATE_PERIODIC)) {
3129         return false;
3130     }
3131 
3132     return true;
3133 }
3134 
InvalidateBlock(CValidationState & state,const CChainParams & chainparams,CBlockIndex * pindex)3135 bool InvalidateBlock(CValidationState& state, const CChainParams& chainparams, CBlockIndex *pindex)
3136 {
3137     AssertLockHeld(cs_main);
3138 
3139     // Mark the block itself as invalid.
3140     pindex->nStatus |= BLOCK_FAILED_VALID;
3141     setDirtyBlockIndex.insert(pindex);
3142     setBlockIndexCandidates.erase(pindex);
3143 
3144     while (chainActive.Contains(pindex)) {
3145         CBlockIndex *pindexWalk = chainActive.Tip();
3146         pindexWalk->nStatus |= BLOCK_FAILED_CHILD;
3147         setDirtyBlockIndex.insert(pindexWalk);
3148         setBlockIndexCandidates.erase(pindexWalk);
3149         // ActivateBestChain considers blocks already in chainActive
3150         // unconditionally valid already, so force disconnect away from it.
3151         if (!DisconnectTip(state, chainparams)) {
3152             mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
3153             return false;
3154         }
3155     }
3156 
3157     LimitMempoolSize(mempool, GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
3158 
3159     // The resulting new best tip may not be in setBlockIndexCandidates anymore, so
3160     // add it again.
3161     BlockMap::iterator it = mapBlockIndex.begin();
3162     while (it != mapBlockIndex.end()) {
3163         if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && !setBlockIndexCandidates.value_comp()(it->second, chainActive.Tip())) {
3164             setBlockIndexCandidates.insert(it->second);
3165         }
3166         it++;
3167     }
3168 
3169     InvalidChainFound(pindex);
3170     mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
3171     uiInterface.NotifyBlockTip(IsInitialBlockDownload(), pindex->pprev);
3172     return true;
3173 }
3174 
ResetBlockFailureFlags(CBlockIndex * pindex)3175 bool ResetBlockFailureFlags(CBlockIndex *pindex) {
3176     AssertLockHeld(cs_main);
3177 
3178     int nHeight = pindex->nHeight;
3179 
3180     // Remove the invalidity flag from this block and all its descendants.
3181     BlockMap::iterator it = mapBlockIndex.begin();
3182     while (it != mapBlockIndex.end()) {
3183         if (!it->second->IsValid() && it->second->GetAncestor(nHeight) == pindex) {
3184             it->second->nStatus &= ~BLOCK_FAILED_MASK;
3185             setDirtyBlockIndex.insert(it->second);
3186             if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && setBlockIndexCandidates.value_comp()(chainActive.Tip(), it->second)) {
3187                 setBlockIndexCandidates.insert(it->second);
3188             }
3189             if (it->second == pindexBestInvalid) {
3190                 // Reset invalid block marker if it was pointing to one of those.
3191                 pindexBestInvalid = NULL;
3192             }
3193         }
3194         it++;
3195     }
3196 
3197     // Remove the invalidity flag from all ancestors too.
3198     while (pindex != NULL) {
3199         if (pindex->nStatus & BLOCK_FAILED_MASK) {
3200             pindex->nStatus &= ~BLOCK_FAILED_MASK;
3201             setDirtyBlockIndex.insert(pindex);
3202         }
3203         pindex = pindex->pprev;
3204     }
3205     return true;
3206 }
3207 
AddToBlockIndex(const CBlockHeader & block)3208 CBlockIndex* AddToBlockIndex(const CBlockHeader& block)
3209 {
3210     // Check for duplicate
3211     uint256 hash = block.GetHash();
3212     BlockMap::iterator it = mapBlockIndex.find(hash);
3213     if (it != mapBlockIndex.end())
3214         return it->second;
3215 
3216     // Construct new block index object
3217     CBlockIndex* pindexNew = new CBlockIndex(block);
3218     assert(pindexNew);
3219     // We assign the sequence id to blocks only when the full data is available,
3220     // to avoid miners withholding blocks but broadcasting headers, to get a
3221     // competitive advantage.
3222     pindexNew->nSequenceId = 0;
3223     BlockMap::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
3224     pindexNew->phashBlock = &((*mi).first);
3225     BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock);
3226     if (miPrev != mapBlockIndex.end())
3227     {
3228         pindexNew->pprev = (*miPrev).second;
3229         pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
3230         pindexNew->BuildSkip();
3231     }
3232     pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew);
3233     pindexNew->RaiseValidity(BLOCK_VALID_TREE);
3234     if (pindexBestHeader == NULL || pindexBestHeader->nChainWork < pindexNew->nChainWork)
3235         pindexBestHeader = pindexNew;
3236 
3237     setDirtyBlockIndex.insert(pindexNew);
3238 
3239     return pindexNew;
3240 }
3241 
3242 /** Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */
ReceivedBlockTransactions(const CBlock & block,CValidationState & state,CBlockIndex * pindexNew,const CDiskBlockPos & pos)3243 bool ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBlockIndex *pindexNew, const CDiskBlockPos& pos)
3244 {
3245     pindexNew->nTx = block.vtx.size();
3246     pindexNew->nChainTx = 0;
3247     pindexNew->nFile = pos.nFile;
3248     pindexNew->nDataPos = pos.nPos;
3249     pindexNew->nUndoPos = 0;
3250     pindexNew->nStatus |= BLOCK_HAVE_DATA;
3251     if (IsWitnessEnabled(pindexNew->pprev, Params().GetConsensus())) {
3252         pindexNew->nStatus |= BLOCK_OPT_WITNESS;
3253     }
3254     pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
3255     setDirtyBlockIndex.insert(pindexNew);
3256 
3257     if (pindexNew->pprev == NULL || pindexNew->pprev->nChainTx) {
3258         // If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
3259         deque<CBlockIndex*> queue;
3260         queue.push_back(pindexNew);
3261 
3262         // Recursively process any descendant blocks that now may be eligible to be connected.
3263         while (!queue.empty()) {
3264             CBlockIndex *pindex = queue.front();
3265             queue.pop_front();
3266             pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
3267             {
3268                 LOCK(cs_nBlockSequenceId);
3269                 pindex->nSequenceId = nBlockSequenceId++;
3270             }
3271             if (chainActive.Tip() == NULL || !setBlockIndexCandidates.value_comp()(pindex, chainActive.Tip())) {
3272                 setBlockIndexCandidates.insert(pindex);
3273             }
3274             std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex);
3275             while (range.first != range.second) {
3276                 std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
3277                 queue.push_back(it->second);
3278                 range.first++;
3279                 mapBlocksUnlinked.erase(it);
3280             }
3281         }
3282     } else {
3283         if (pindexNew->pprev && pindexNew->pprev->IsValid(BLOCK_VALID_TREE)) {
3284             mapBlocksUnlinked.insert(std::make_pair(pindexNew->pprev, pindexNew));
3285         }
3286     }
3287 
3288     return true;
3289 }
3290 
FindBlockPos(CValidationState & state,CDiskBlockPos & pos,unsigned int nAddSize,unsigned int nHeight,uint64_t nTime,bool fKnown=false)3291 bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
3292 {
3293     LOCK(cs_LastBlockFile);
3294 
3295     unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile;
3296     if (vinfoBlockFile.size() <= nFile) {
3297         vinfoBlockFile.resize(nFile + 1);
3298     }
3299 
3300     if (!fKnown) {
3301         while (vinfoBlockFile[nFile].nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
3302             nFile++;
3303             if (vinfoBlockFile.size() <= nFile) {
3304                 vinfoBlockFile.resize(nFile + 1);
3305             }
3306         }
3307         pos.nFile = nFile;
3308         pos.nPos = vinfoBlockFile[nFile].nSize;
3309     }
3310 
3311     if ((int)nFile != nLastBlockFile) {
3312         if (!fKnown) {
3313             LogPrintf("Leaving block file %i: %s\n", nLastBlockFile, vinfoBlockFile[nLastBlockFile].ToString());
3314         }
3315         FlushBlockFile(!fKnown);
3316         nLastBlockFile = nFile;
3317     }
3318 
3319     vinfoBlockFile[nFile].AddBlock(nHeight, nTime);
3320     if (fKnown)
3321         vinfoBlockFile[nFile].nSize = std::max(pos.nPos + nAddSize, vinfoBlockFile[nFile].nSize);
3322     else
3323         vinfoBlockFile[nFile].nSize += nAddSize;
3324 
3325     if (!fKnown) {
3326         unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
3327         unsigned int nNewChunks = (vinfoBlockFile[nFile].nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
3328         if (nNewChunks > nOldChunks) {
3329             if (fPruneMode)
3330                 fCheckForPruning = true;
3331             if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
3332                 FILE *file = OpenBlockFile(pos);
3333                 if (file) {
3334                     LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
3335                     AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
3336                     fclose(file);
3337                 }
3338             }
3339             else
3340                 return state.Error("out of disk space");
3341         }
3342     }
3343 
3344     setDirtyFileInfo.insert(nFile);
3345     return true;
3346 }
3347 
FindUndoPos(CValidationState & state,int nFile,CDiskBlockPos & pos,unsigned int nAddSize)3348 bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
3349 {
3350     pos.nFile = nFile;
3351 
3352     LOCK(cs_LastBlockFile);
3353 
3354     unsigned int nNewSize;
3355     pos.nPos = vinfoBlockFile[nFile].nUndoSize;
3356     nNewSize = vinfoBlockFile[nFile].nUndoSize += nAddSize;
3357     setDirtyFileInfo.insert(nFile);
3358 
3359     unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
3360     unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
3361     if (nNewChunks > nOldChunks) {
3362         if (fPruneMode)
3363             fCheckForPruning = true;
3364         if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
3365             FILE *file = OpenUndoFile(pos);
3366             if (file) {
3367                 LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
3368                 AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
3369                 fclose(file);
3370             }
3371         }
3372         else
3373             return state.Error("out of disk space");
3374     }
3375 
3376     return true;
3377 }
3378 
CheckBlockHeader(const CBlockHeader & block,CValidationState & state,const Consensus::Params & consensusParams,bool fCheckPOW)3379 bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW)
3380 {
3381     // Check proof of work matches claimed amount
3382     if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
3383         return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");
3384 
3385     return true;
3386 }
3387 
CheckBlock(const CBlock & block,CValidationState & state,const Consensus::Params & consensusParams,bool fCheckPOW,bool fCheckMerkleRoot)3388 bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
3389 {
3390     // These are checks that are independent of context.
3391 
3392     if (block.fChecked)
3393         return true;
3394 
3395     // Check that the header is valid (particularly PoW).  This is mostly
3396     // redundant with the call in AcceptBlockHeader.
3397     if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW))
3398         return false;
3399 
3400     // Check the merkle root.
3401     if (fCheckMerkleRoot) {
3402         bool mutated;
3403         uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated);
3404         if (block.hashMerkleRoot != hashMerkleRoot2)
3405             return state.DoS(100, false, REJECT_INVALID, "bad-txnmrklroot", true, "hashMerkleRoot mismatch");
3406 
3407         // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
3408         // of transactions in a block without affecting the merkle root of a block,
3409         // while still invalidating it.
3410         if (mutated)
3411             return state.DoS(100, false, REJECT_INVALID, "bad-txns-duplicate", true, "duplicate transaction");
3412     }
3413 
3414     // All potential-corruption validation must be done before we do any
3415     // transaction validation, as otherwise we may mark the header as invalid
3416     // because we receive the wrong transactions for it.
3417     // Note that witness malleability is checked in ContextualCheckBlock, so no
3418     // checks that use witness data may be performed here.
3419 
3420     // Size limits
3421     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)
3422         return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed");
3423 
3424     // First transaction must be coinbase, the rest must not be
3425     if (block.vtx.empty() || !block.vtx[0].IsCoinBase())
3426         return state.DoS(100, false, REJECT_INVALID, "bad-cb-missing", false, "first tx is not coinbase");
3427     for (unsigned int i = 1; i < block.vtx.size(); i++)
3428         if (block.vtx[i].IsCoinBase())
3429             return state.DoS(100, false, REJECT_INVALID, "bad-cb-multiple", false, "more than one coinbase");
3430 
3431     // Check transactions
3432     BOOST_FOREACH(const CTransaction& tx, block.vtx)
3433         if (!CheckTransaction(tx, state))
3434             return state.Invalid(false, state.GetRejectCode(), state.GetRejectReason(),
3435                                  strprintf("Transaction check failed (tx hash %s) %s", tx.GetHash().ToString(), state.GetDebugMessage()));
3436 
3437     unsigned int nSigOps = 0;
3438     BOOST_FOREACH(const CTransaction& tx, block.vtx)
3439     {
3440         nSigOps += GetLegacySigOpCount(tx);
3441     }
3442     if (nSigOps * WITNESS_SCALE_FACTOR > MAX_BLOCK_SIGOPS_COST)
3443         return state.DoS(100, false, REJECT_INVALID, "bad-blk-sigops", false, "out-of-bounds SigOpCount");
3444 
3445     if (fCheckPOW && fCheckMerkleRoot)
3446         block.fChecked = true;
3447 
3448     return true;
3449 }
3450 
CheckIndexAgainstCheckpoint(const CBlockIndex * pindexPrev,CValidationState & state,const CChainParams & chainparams,const uint256 & hash)3451 static bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidationState& state, const CChainParams& chainparams, const uint256& hash)
3452 {
3453     if (*pindexPrev->phashBlock == chainparams.GetConsensus().hashGenesisBlock)
3454         return true;
3455 
3456     int nHeight = pindexPrev->nHeight+1;
3457     // Don't accept any forks from the main chain prior to last checkpoint
3458     CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainparams.Checkpoints());
3459     if (pcheckpoint && nHeight < pcheckpoint->nHeight)
3460         return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight));
3461 
3462     return true;
3463 }
3464 
IsWitnessEnabled(const CBlockIndex * pindexPrev,const Consensus::Params & params)3465 bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params)
3466 {
3467     LOCK(cs_main);
3468     return (VersionBitsState(pindexPrev, params, Consensus::DEPLOYMENT_SEGWIT, versionbitscache) == THRESHOLD_ACTIVE);
3469 }
3470 
3471 // Compute at which vout of the block's coinbase transaction the witness
3472 // commitment occurs, or -1 if not found.
GetWitnessCommitmentIndex(const CBlock & block)3473 static int GetWitnessCommitmentIndex(const CBlock& block)
3474 {
3475     int commitpos = -1;
3476     for (size_t o = 0; o < block.vtx[0].vout.size(); o++) {
3477         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) {
3478             commitpos = o;
3479         }
3480     }
3481     return commitpos;
3482 }
3483 
UpdateUncommittedBlockStructures(CBlock & block,const CBlockIndex * pindexPrev,const Consensus::Params & consensusParams)3484 void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
3485 {
3486     int commitpos = GetWitnessCommitmentIndex(block);
3487     static const std::vector<unsigned char> nonce(32, 0x00);
3488     if (commitpos != -1 && IsWitnessEnabled(pindexPrev, consensusParams) && block.vtx[0].wit.IsEmpty()) {
3489         block.vtx[0].wit.vtxinwit.resize(1);
3490         block.vtx[0].wit.vtxinwit[0].scriptWitness.stack.resize(1);
3491         block.vtx[0].wit.vtxinwit[0].scriptWitness.stack[0] = nonce;
3492     }
3493 }
3494 
GenerateCoinbaseCommitment(CBlock & block,const CBlockIndex * pindexPrev,const Consensus::Params & consensusParams)3495 std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
3496 {
3497     std::vector<unsigned char> commitment;
3498     int commitpos = GetWitnessCommitmentIndex(block);
3499     std::vector<unsigned char> ret(32, 0x00);
3500     if (consensusParams.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nTimeout != 0) {
3501         if (commitpos == -1) {
3502             uint256 witnessroot = BlockWitnessMerkleRoot(block, NULL);
3503             CHash256().Write(witnessroot.begin(), 32).Write(&ret[0], 32).Finalize(witnessroot.begin());
3504             CTxOut out;
3505             out.nValue = 0;
3506             out.scriptPubKey.resize(38);
3507             out.scriptPubKey[0] = OP_RETURN;
3508             out.scriptPubKey[1] = 0x24;
3509             out.scriptPubKey[2] = 0xaa;
3510             out.scriptPubKey[3] = 0x21;
3511             out.scriptPubKey[4] = 0xa9;
3512             out.scriptPubKey[5] = 0xed;
3513             memcpy(&out.scriptPubKey[6], witnessroot.begin(), 32);
3514             commitment = std::vector<unsigned char>(out.scriptPubKey.begin(), out.scriptPubKey.end());
3515             const_cast<std::vector<CTxOut>*>(&block.vtx[0].vout)->push_back(out);
3516             block.vtx[0].UpdateHash();
3517         }
3518     }
3519     UpdateUncommittedBlockStructures(block, pindexPrev, consensusParams);
3520     return commitment;
3521 }
3522 
ContextualCheckBlockHeader(const CBlockHeader & block,CValidationState & state,const Consensus::Params & consensusParams,CBlockIndex * const pindexPrev,int64_t nAdjustedTime)3523 bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, CBlockIndex * const pindexPrev, int64_t nAdjustedTime)
3524 {
3525     const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
3526     // Check proof of work
3527     if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
3528         return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");
3529 
3530     // Check timestamp against prev
3531     if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
3532         return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
3533 
3534     if (nHeight >= BIP65_HEIGHT)
3535         if (block.nVersion < 4)
3536             return state.Invalid(error("%s : rejected nVersion<4 block", __func__),
3537                                 REJECT_OBSOLETE, "bad-version");
3538 
3539     // Check timestamp
3540     if (block.GetBlockTime() > nAdjustedTime + 2 * 60 * 60)
3541         return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
3542 
3543     if (IsWitnessEnabled(pindexPrev, consensusParams))
3544         if (block.nVersion <= 5)
3545             return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", block.nVersion),
3546                                  strprintf("rejected nVersion=0x%08x block", block.nVersion));
3547 /*
3548     // Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded:
3549     for (int32_t version = 2; version < 6; ++version) // check for version 2, 3 and 4 upgrades
3550         if (block.nVersion < version && IsSuperMajority(version, pindexPrev, consensusParams.nMajorityRejectBlockOutdated, consensusParams))
3551             return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", version - 1),
3552                                  strprintf("rejected nVersion=0x%08x block", version - 1));
3553     */
3554 
3555     return true;
3556 }
3557 
ContextualCheckBlock(const CBlock & block,CValidationState & state,CBlockIndex * const pindexPrev)3558 bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex * const pindexPrev)
3559 {
3560     const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
3561     const Consensus::Params& consensusParams = Params().GetConsensus();
3562 
3563     // Start enforcing BIP113 (Median Time Past) using versionbits logic.
3564     int nLockTimeFlags = 0;
3565     if (VersionBitsState(pindexPrev, consensusParams, Consensus::DEPLOYMENT_CSV, versionbitscache) == THRESHOLD_ACTIVE) {
3566         nLockTimeFlags |= LOCKTIME_MEDIAN_TIME_PAST;
3567     }
3568 
3569     int64_t nLockTimeCutoff = (nLockTimeFlags & LOCKTIME_MEDIAN_TIME_PAST)
3570                               ? pindexPrev->GetMedianTimePast()
3571                               : block.GetBlockTime();
3572 
3573     // Check that all transactions are finalized
3574     BOOST_FOREACH(const CTransaction& tx, block.vtx) {
3575         if (!IsFinalTx(tx, nHeight, nLockTimeCutoff)) {
3576             return state.DoS(10, false, REJECT_INVALID, "bad-txns-nonfinal", false, "non-final transaction");
3577         }
3578     }
3579 
3580     // Enforce block.nVersion=2 rule that the coinbase starts with serialized block height
3581     if (nHeight >= V2_RULE_HEIGHT)
3582     {
3583         CScript expect = CScript() << nHeight;
3584         if (block.vtx[0].vin[0].scriptSig.size() < expect.size() ||
3585             !std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin())) {
3586             return state.DoS(100, false, REJECT_INVALID, "bad-cb-height", false, "block height mismatch in coinbase");
3587         }
3588     }
3589 
3590     // Validation for witness commitments.
3591     // * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the
3592     //   coinbase (where 0x0000....0000 is used instead).
3593     // * The coinbase scriptWitness is a stack of a single 32-byte vector, containing a witness nonce (unconstrained).
3594     // * We build a merkle tree with all those witness hashes as leaves (similar to the hashMerkleRoot in the block header).
3595     // * There must be at least one output whose scriptPubKey is a single 36-byte push, the first 4 bytes of which are
3596     //   {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness nonce). In case there are
3597     //   multiple, the last one is used.
3598     bool fHaveWitness = false;
3599     if (IsWitnessEnabled(pindexPrev, consensusParams)) {
3600         int commitpos = GetWitnessCommitmentIndex(block);
3601         if (commitpos != -1) {
3602             bool malleated = false;
3603             uint256 hashWitness = BlockWitnessMerkleRoot(block, &malleated);
3604             // The malleation check is ignored; as the transaction tree itself
3605             // already does not permit it, it is impossible to trigger in the
3606             // witness tree.
3607             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) {
3608                 return state.DoS(100, error("%s : invalid witness nonce size", __func__), REJECT_INVALID, "bad-witness-nonce-size", true);
3609             }
3610             CHash256().Write(hashWitness.begin(), 32).Write(&block.vtx[0].wit.vtxinwit[0].scriptWitness.stack[0][0], 32).Finalize(hashWitness.begin());
3611             if (memcmp(hashWitness.begin(), &block.vtx[0].vout[commitpos].scriptPubKey[6], 32)) {
3612                 return state.DoS(100, error("%s : witness merkle commitment mismatch", __func__), REJECT_INVALID, "bad-witness-merkle-match", true);
3613             }
3614             fHaveWitness = true;
3615         }
3616     }
3617 
3618     // No witness data is allowed in blocks that don't commit to witness data, as this would otherwise leave room for spam
3619     if (!fHaveWitness) {
3620         for (size_t i = 0; i < block.vtx.size(); i++) {
3621             if (!block.vtx[i].wit.IsNull()) {
3622                 return state.DoS(100, error("%s : unexpected witness data found", __func__), REJECT_INVALID, "unexpected-witness", true);
3623             }
3624         }
3625     }
3626 
3627     // After the coinbase witness nonce and commitment are verified,
3628     // we can check if the block weight passes (before we've checked the
3629     // coinbase witness, it would be possible for the weight to be too
3630     // large by filling up the coinbase witness, which doesn't change
3631     // the block hash, so we couldn't mark the block as permanently
3632     // failed).
3633     if (GetBlockWeight(block) > MAX_BLOCK_WEIGHT) {
3634         return state.DoS(100, error("ContextualCheckBlock(): weight limit failed"), REJECT_INVALID, "bad-blk-weight");
3635     }
3636 
3637     return true;
3638 }
3639 
AcceptBlockHeader(const CBlockHeader & block,CValidationState & state,const CChainParams & chainparams,CBlockIndex ** ppindex=NULL)3640 static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex=NULL)
3641 {
3642     AssertLockHeld(cs_main);
3643     // Check for duplicate
3644     uint256 hash = block.GetHash();
3645     BlockMap::iterator miSelf = mapBlockIndex.find(hash);
3646     CBlockIndex *pindex = NULL;
3647     if (hash != chainparams.GetConsensus().hashGenesisBlock) {
3648 
3649         if (miSelf != mapBlockIndex.end()) {
3650             // Block header is already known.
3651             pindex = miSelf->second;
3652             if (ppindex)
3653                 *ppindex = pindex;
3654             if (pindex->nStatus & BLOCK_FAILED_MASK)
3655                 return state.Invalid(error("%s: block %s is marked invalid", __func__, hash.ToString()), 0, "duplicate");
3656             return true;
3657         }
3658 
3659         if (!CheckBlockHeader(block, state, chainparams.GetConsensus()))
3660             return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
3661 
3662         // Get prev block index
3663         CBlockIndex* pindexPrev = NULL;
3664         BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
3665         if (mi == mapBlockIndex.end())
3666             return state.DoS(10, error("%s: prev block not found", __func__), 0, "bad-prevblk");
3667         pindexPrev = (*mi).second;
3668         if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
3669             return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
3670 
3671         assert(pindexPrev);
3672         if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, hash))
3673             return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
3674 
3675         if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev, GetAdjustedTime()))
3676             return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
3677     }
3678     if (pindex == NULL)
3679         pindex = AddToBlockIndex(block);
3680 
3681     if (ppindex)
3682         *ppindex = pindex;
3683 
3684     return true;
3685 }
3686 
3687 /** Store block on disk. If dbp is non-NULL, the file is known to already reside on disk */
AcceptBlock(const CBlock & block,CValidationState & state,const CChainParams & chainparams,CBlockIndex ** ppindex,bool fRequested,const CDiskBlockPos * dbp,bool * fNewBlock)3688 static bool AcceptBlock(const CBlock& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const CDiskBlockPos* dbp, bool* fNewBlock)
3689 {
3690     if (fNewBlock) *fNewBlock = false;
3691     AssertLockHeld(cs_main);
3692 
3693     CBlockIndex *pindexDummy = NULL;
3694     CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
3695 
3696     if (!AcceptBlockHeader(block, state, chainparams, &pindex))
3697         return false;
3698 
3699     // Try to process all requested blocks that we don't have, but only
3700     // process an unrequested block if it's new and has enough work to
3701     // advance our tip, and isn't too many blocks ahead.
3702     bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA;
3703     bool fHasMoreWork = (chainActive.Tip() ? pindex->nChainWork > chainActive.Tip()->nChainWork : true);
3704     // Blocks that are too out-of-order needlessly limit the effectiveness of
3705     // pruning, because pruning will not delete block files that contain any
3706     // blocks which are too close in height to the tip.  Apply this test
3707     // regardless of whether pruning is enabled; it should generally be safe to
3708     // not process unrequested blocks.
3709     bool fTooFarAhead = (pindex->nHeight > int(chainActive.Height() + MIN_BLOCKS_TO_KEEP));
3710 
3711     // TODO: deal better with return value and error conditions for duplicate
3712     // and unrequested blocks.
3713     if (fAlreadyHave) return true;
3714     if (!fRequested) {  // If we didn't ask for it:
3715         if (pindex->nTx != 0) return true;  // This is a previously-processed block that was pruned
3716         if (!fHasMoreWork) return true;     // Don't process less-work chains
3717         if (fTooFarAhead) return true;      // Block height is too high
3718     }
3719     if (fNewBlock) *fNewBlock = true;
3720 
3721     if ((!CheckBlock(block, state, chainparams.GetConsensus(), GetAdjustedTime())) || !ContextualCheckBlock(block, state, pindex->pprev)) {
3722         if (state.IsInvalid() && !state.CorruptionPossible()) {
3723             pindex->nStatus |= BLOCK_FAILED_VALID;
3724             setDirtyBlockIndex.insert(pindex);
3725         }
3726         return error("%s: %s", __func__, FormatStateMessage(state));
3727     }
3728 
3729     int nHeight = pindex->nHeight;
3730 
3731     // Write block to history file
3732     try {
3733         unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
3734         CDiskBlockPos blockPos;
3735         if (dbp != NULL)
3736             blockPos = *dbp;
3737         if (!FindBlockPos(state, blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != NULL))
3738             return error("AcceptBlock(): FindBlockPos failed");
3739         if (dbp == NULL)
3740             if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
3741                 AbortNode(state, "Failed to write block");
3742         if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
3743             return error("AcceptBlock(): ReceivedBlockTransactions failed");
3744     } catch (const std::runtime_error& e) {
3745         return AbortNode(state, std::string("System error: ") + e.what());
3746     }
3747 
3748     if (fCheckForPruning)
3749         FlushStateToDisk(state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
3750 
3751     return true;
3752 }
3753 
3754 /*
3755 static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams)
3756 {
3757     unsigned int nFound = 0;
3758     for (int i = 0; i < consensusParams.nMajorityWindow && nFound < nRequired && pstart != NULL; i++)
3759     {
3760         if (pstart->nVersion >= minVersion)
3761             ++nFound;
3762         pstart = pstart->pprev;
3763     }
3764     return (nFound >= nRequired);
3765 }
3766 */
3767 
ProcessNewBlock(CValidationState & state,const CChainParams & chainparams,CNode * pfrom,const CBlock * pblock,bool fForceProcessing,const CDiskBlockPos * dbp,bool fMayBanPeerIfInvalid)3768 bool ProcessNewBlock(CValidationState& state, const CChainParams& chainparams, CNode* pfrom, const CBlock* pblock, bool fForceProcessing, const CDiskBlockPos* dbp, bool fMayBanPeerIfInvalid)
3769 {
3770     {
3771         LOCK(cs_main);
3772         bool fRequested = MarkBlockAsReceived(pblock->GetHash());
3773         fRequested |= fForceProcessing;
3774 
3775         // Store to disk
3776         CBlockIndex *pindex = NULL;
3777         bool fNewBlock = false;
3778         bool ret = AcceptBlock(*pblock, state, chainparams, &pindex, fRequested, dbp, &fNewBlock);
3779         if (pindex && pfrom) {
3780             mapBlockSource[pindex->GetBlockHash()] = std::make_pair(pfrom->GetId(), fMayBanPeerIfInvalid);
3781             if (fNewBlock) pfrom->nLastBlockTime = GetTime();
3782         }
3783         CheckBlockIndex(chainparams.GetConsensus());
3784         if (!ret)
3785             return error("%s: AcceptBlock FAILED", __func__);
3786     }
3787 
3788     NotifyHeaderTip();
3789 
3790     if (!ActivateBestChain(state, chainparams, pblock))
3791         return error("%s: ActivateBestChain failed", __func__);
3792 
3793     return true;
3794 }
3795 
TestBlockValidity(CValidationState & state,const CChainParams & chainparams,const CBlock & block,CBlockIndex * pindexPrev,bool fCheckPOW,bool fCheckMerkleRoot)3796 bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
3797 {
3798     AssertLockHeld(cs_main);
3799     assert(pindexPrev && pindexPrev == chainActive.Tip());
3800     if (fCheckpointsEnabled && !CheckIndexAgainstCheckpoint(pindexPrev, state, chainparams, block.GetHash()))
3801         return error("%s: CheckIndexAgainstCheckpoint(): %s", __func__, state.GetRejectReason().c_str());
3802 
3803     CCoinsViewCache viewNew(pcoinsTip);
3804     CBlockIndex indexDummy(block);
3805     indexDummy.pprev = pindexPrev;
3806     indexDummy.nHeight = pindexPrev->nHeight + 1;
3807 
3808     // NOTE: CheckBlockHeader is called by CheckBlock
3809     if (!ContextualCheckBlockHeader(block, state, chainparams.GetConsensus(), pindexPrev, GetAdjustedTime()))
3810         return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, FormatStateMessage(state));
3811     if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot))
3812         return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
3813     if (!ContextualCheckBlock(block, state, pindexPrev))
3814         return error("%s: Consensus::ContextualCheckBlock: %s", __func__, FormatStateMessage(state));
3815     if (!ConnectBlock(block, state, &indexDummy, viewNew, chainparams, true))
3816         return false;
3817     assert(state.IsValid());
3818 
3819     return true;
3820 }
3821 
3822 /**
3823  * BLOCK PRUNING CODE
3824  */
3825 
3826 /* Calculate the amount of disk space the block & undo files currently use */
CalculateCurrentUsage()3827 uint64_t CalculateCurrentUsage()
3828 {
3829     uint64_t retval = 0;
3830     BOOST_FOREACH(const CBlockFileInfo &file, vinfoBlockFile) {
3831         retval += file.nSize + file.nUndoSize;
3832     }
3833     return retval;
3834 }
3835 
3836 /* Prune a block file (modify associated database entries)*/
PruneOneBlockFile(const int fileNumber)3837 void PruneOneBlockFile(const int fileNumber)
3838 {
3839     for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) {
3840         CBlockIndex* pindex = it->second;
3841         if (pindex->nFile == fileNumber) {
3842             pindex->nStatus &= ~BLOCK_HAVE_DATA;
3843             pindex->nStatus &= ~BLOCK_HAVE_UNDO;
3844             pindex->nFile = 0;
3845             pindex->nDataPos = 0;
3846             pindex->nUndoPos = 0;
3847             setDirtyBlockIndex.insert(pindex);
3848 
3849             // Prune from mapBlocksUnlinked -- any block we prune would have
3850             // to be downloaded again in order to consider its chain, at which
3851             // point it would be considered as a candidate for
3852             // mapBlocksUnlinked or setBlockIndexCandidates.
3853             std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex->pprev);
3854             while (range.first != range.second) {
3855                 std::multimap<CBlockIndex *, CBlockIndex *>::iterator it = range.first;
3856                 range.first++;
3857                 if (it->second == pindex) {
3858                     mapBlocksUnlinked.erase(it);
3859                 }
3860             }
3861         }
3862     }
3863 
3864     vinfoBlockFile[fileNumber].SetNull();
3865     setDirtyFileInfo.insert(fileNumber);
3866 }
3867 
3868 
UnlinkPrunedFiles(std::set<int> & setFilesToPrune)3869 void UnlinkPrunedFiles(std::set<int>& setFilesToPrune)
3870 {
3871     for (set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
3872         CDiskBlockPos pos(*it, 0);
3873         boost::filesystem::remove(GetBlockPosFilename(pos, "blk"));
3874         boost::filesystem::remove(GetBlockPosFilename(pos, "rev"));
3875         LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
3876     }
3877 }
3878 
3879 /* Calculate the block/rev files that should be deleted to remain under target*/
FindFilesToPrune(std::set<int> & setFilesToPrune,uint64_t nPruneAfterHeight)3880 void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight)
3881 {
3882     LOCK2(cs_main, cs_LastBlockFile);
3883     if (chainActive.Tip() == NULL || nPruneTarget == 0) {
3884         return;
3885     }
3886     if ((uint64_t)chainActive.Tip()->nHeight <= nPruneAfterHeight) {
3887         return;
3888     }
3889 
3890     unsigned int nLastBlockWeCanPrune = chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP;
3891     uint64_t nCurrentUsage = CalculateCurrentUsage();
3892     // We don't check to prune until after we've allocated new space for files
3893     // So we should leave a buffer under our target to account for another allocation
3894     // before the next pruning.
3895     uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
3896     uint64_t nBytesToPrune;
3897     int count=0;
3898 
3899     if (nCurrentUsage + nBuffer >= nPruneTarget) {
3900         for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
3901             nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize;
3902 
3903             if (vinfoBlockFile[fileNumber].nSize == 0)
3904                 continue;
3905 
3906             if (nCurrentUsage + nBuffer < nPruneTarget)  // are we below our target?
3907                 break;
3908 
3909             // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
3910             if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
3911                 continue;
3912 
3913             PruneOneBlockFile(fileNumber);
3914             // Queue up the files for removal
3915             setFilesToPrune.insert(fileNumber);
3916             nCurrentUsage -= nBytesToPrune;
3917             count++;
3918         }
3919     }
3920 
3921     LogPrint("prune", "Prune: target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n",
3922            nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
3923            ((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
3924            nLastBlockWeCanPrune, count);
3925 }
3926 
CheckDiskSpace(uint64_t nAdditionalBytes)3927 bool CheckDiskSpace(uint64_t nAdditionalBytes)
3928 {
3929     uint64_t nFreeBytesAvailable = boost::filesystem::space(GetDataDir()).available;
3930 
3931     // Check for nMinDiskSpace bytes (currently 50MB)
3932     if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
3933         return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
3934 
3935     return true;
3936 }
3937 
OpenDiskFile(const CDiskBlockPos & pos,const char * prefix,bool fReadOnly)3938 FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
3939 {
3940     if (pos.IsNull())
3941         return NULL;
3942     boost::filesystem::path path = GetBlockPosFilename(pos, prefix);
3943     boost::filesystem::create_directories(path.parent_path());
3944     FILE* file = fopen(path.string().c_str(), "rb+");
3945     if (!file && !fReadOnly)
3946         file = fopen(path.string().c_str(), "wb+");
3947     if (!file) {
3948         LogPrintf("Unable to open file %s\n", path.string());
3949         return NULL;
3950     }
3951     if (pos.nPos) {
3952         if (fseek(file, pos.nPos, SEEK_SET)) {
3953             LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
3954             fclose(file);
3955             return NULL;
3956         }
3957     }
3958     return file;
3959 }
3960 
OpenBlockFile(const CDiskBlockPos & pos,bool fReadOnly)3961 FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
3962     return OpenDiskFile(pos, "blk", fReadOnly);
3963 }
3964 
OpenUndoFile(const CDiskBlockPos & pos,bool fReadOnly)3965 FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
3966     return OpenDiskFile(pos, "rev", fReadOnly);
3967 }
3968 
GetBlockPosFilename(const CDiskBlockPos & pos,const char * prefix)3969 boost::filesystem::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
3970 {
3971     return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
3972 }
3973 
InsertBlockIndex(uint256 hash)3974 CBlockIndex * InsertBlockIndex(uint256 hash)
3975 {
3976     if (hash.IsNull())
3977         return NULL;
3978 
3979     // Return existing
3980     BlockMap::iterator mi = mapBlockIndex.find(hash);
3981     if (mi != mapBlockIndex.end())
3982         return (*mi).second;
3983 
3984     // Create new
3985     CBlockIndex* pindexNew = new CBlockIndex();
3986     if (!pindexNew)
3987         throw runtime_error(std::string(__func__) + ": new CBlockIndex failed");
3988     mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
3989     pindexNew->phashBlock = &((*mi).first);
3990 
3991     return pindexNew;
3992 }
3993 
LoadBlockIndexDB()3994 bool static LoadBlockIndexDB()
3995 {
3996     const CChainParams& chainparams = Params();
3997     if (!pblocktree->LoadBlockIndexGuts(InsertBlockIndex))
3998         return false;
3999 
4000     boost::this_thread::interruption_point();
4001 
4002     // Calculate nChainWork
4003     vector<pair<int, CBlockIndex*> > vSortedByHeight;
4004     vSortedByHeight.reserve(mapBlockIndex.size());
4005     BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
4006     {
4007         CBlockIndex* pindex = item.second;
4008         vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
4009     }
4010     sort(vSortedByHeight.begin(), vSortedByHeight.end());
4011     BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
4012     {
4013         CBlockIndex* pindex = item.second;
4014         pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
4015         // We can link the chain of blocks for which we've received transactions at some point.
4016         // Pruned nodes may have deleted the block.
4017         if (pindex->nTx > 0) {
4018             if (pindex->pprev) {
4019                 if (pindex->pprev->nChainTx) {
4020                     pindex->nChainTx = pindex->pprev->nChainTx + pindex->nTx;
4021                 } else {
4022                     pindex->nChainTx = 0;
4023                     mapBlocksUnlinked.insert(std::make_pair(pindex->pprev, pindex));
4024                 }
4025             } else {
4026                 pindex->nChainTx = pindex->nTx;
4027             }
4028         }
4029         if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->nChainTx || pindex->pprev == NULL))
4030             setBlockIndexCandidates.insert(pindex);
4031         if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
4032             pindexBestInvalid = pindex;
4033         if (pindex->pprev)
4034             pindex->BuildSkip();
4035         if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == NULL || CBlockIndexWorkComparator()(pindexBestHeader, pindex)))
4036             pindexBestHeader = pindex;
4037     }
4038 
4039     // Load block file info
4040     pblocktree->ReadLastBlockFile(nLastBlockFile);
4041     vinfoBlockFile.resize(nLastBlockFile + 1);
4042     LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile);
4043     for (int nFile = 0; nFile <= nLastBlockFile; nFile++) {
4044         pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
4045     }
4046     LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString());
4047     for (int nFile = nLastBlockFile + 1; true; nFile++) {
4048         CBlockFileInfo info;
4049         if (pblocktree->ReadBlockFileInfo(nFile, info)) {
4050             vinfoBlockFile.push_back(info);
4051         } else {
4052             break;
4053         }
4054     }
4055 
4056     // Check presence of blk files
4057     LogPrintf("Checking all blk files are present...\n");
4058     set<int> setBlkDataFiles;
4059     BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
4060     {
4061         CBlockIndex* pindex = item.second;
4062         if (pindex->nStatus & BLOCK_HAVE_DATA) {
4063             setBlkDataFiles.insert(pindex->nFile);
4064         }
4065     }
4066     for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++)
4067     {
4068         CDiskBlockPos pos(*it, 0);
4069         if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) {
4070             return false;
4071         }
4072     }
4073 
4074     // Check whether we have ever pruned block & undo files
4075     pblocktree->ReadFlag("prunedblockfiles", fHavePruned);
4076     if (fHavePruned)
4077         LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n");
4078 
4079     // Check whether we need to continue reindexing
4080     bool fReindexing = false;
4081     pblocktree->ReadReindexing(fReindexing);
4082     fReindex |= fReindexing;
4083 
4084     // Check whether we have a transaction index
4085     pblocktree->ReadFlag("txindex", fTxIndex);
4086     LogPrintf("%s: transaction index %s\n", __func__, fTxIndex ? "enabled" : "disabled");
4087 
4088     // Load pointer to end of best chain
4089     BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
4090     if (it == mapBlockIndex.end())
4091         return true;
4092     chainActive.SetTip(it->second);
4093 
4094     PruneBlockIndexCandidates();
4095 
4096     LogPrintf("%s: hashBestChain=%s height=%d date=%s progress=%f\n", __func__,
4097         chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
4098         DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
4099         Checkpoints::GuessVerificationProgress(chainparams.Checkpoints(), chainActive.Tip()));
4100 
4101     return true;
4102 }
4103 
CVerifyDB()4104 CVerifyDB::CVerifyDB()
4105 {
4106     uiInterface.ShowProgress(_("Verifying blocks..."), 0);
4107 }
4108 
~CVerifyDB()4109 CVerifyDB::~CVerifyDB()
4110 {
4111     uiInterface.ShowProgress("", 100);
4112 }
4113 
VerifyDB(const CChainParams & chainparams,CCoinsView * coinsview,int nCheckLevel,int nCheckDepth)4114 bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
4115 {
4116     LOCK(cs_main);
4117     if (chainActive.Tip() == NULL || chainActive.Tip()->pprev == NULL)
4118         return true;
4119 
4120     // Verify blocks in the best chain
4121     if (nCheckDepth <= 0)
4122         nCheckDepth = 1000000000; // suffices until the year 19000
4123     if (nCheckDepth > chainActive.Height())
4124         nCheckDepth = chainActive.Height();
4125     nCheckLevel = std::max(0, std::min(4, nCheckLevel));
4126     LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
4127     CCoinsViewCache coins(coinsview);
4128     CBlockIndex* pindexState = chainActive.Tip();
4129     CBlockIndex* pindexFailure = NULL;
4130     int nGoodTransactions = 0;
4131     CValidationState state;
4132     int reportDone = 0;
4133     LogPrintf("[0%]...");
4134     for (CBlockIndex* pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev)
4135     {
4136         boost::this_thread::interruption_point();
4137         int percentageDone = std::max(1, std::min(99, (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
4138         if (reportDone < percentageDone/10) {
4139             // report every 10% step
4140             LogPrintf("[%d%%]...", percentageDone);
4141             reportDone = percentageDone/10;
4142         }
4143         uiInterface.ShowProgress(_("Verifying blocks..."), percentageDone);
4144         if (pindex->nHeight < chainActive.Height()-nCheckDepth)
4145             break;
4146         if (fPruneMode && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
4147             // If pruning, only go back as far as we have data.
4148             LogPrintf("VerifyDB(): block verification stopping at height %d (pruning, no data)\n", pindex->nHeight);
4149             break;
4150         }
4151         CBlock block;
4152         // check level 0: read from disk
4153         if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
4154             return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4155         // check level 1: verify block validity
4156         if (nCheckLevel >= 1 && !CheckBlock(block, state, chainparams.GetConsensus()))
4157             return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
4158                          pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
4159         // check level 2: verify undo validity
4160         if (nCheckLevel >= 2 && pindex) {
4161             CBlockUndo undo;
4162             CDiskBlockPos pos = pindex->GetUndoPos();
4163             if (!pos.IsNull()) {
4164                 if (!UndoReadFromDisk(undo, pos, pindex->pprev->GetBlockHash()))
4165                     return error("VerifyDB(): *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4166             }
4167         }
4168         // check level 3: check for inconsistencies during memory-only disconnect of tip blocks
4169         if (nCheckLevel >= 3 && pindex == pindexState && (coins.DynamicMemoryUsage() + pcoinsTip->DynamicMemoryUsage()) <= nCoinCacheUsage) {
4170             bool fClean = true;
4171             if (!DisconnectBlock(block, state, pindex, coins, &fClean))
4172                 return error("VerifyDB(): *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4173             pindexState = pindex->pprev;
4174             if (!fClean) {
4175                 nGoodTransactions = 0;
4176                 pindexFailure = pindex;
4177             } else
4178                 nGoodTransactions += block.vtx.size();
4179         }
4180         if (ShutdownRequested())
4181             return true;
4182     }
4183     if (pindexFailure)
4184         return error("VerifyDB(): *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainActive.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
4185 
4186     // check level 4: try reconnecting blocks
4187     if (nCheckLevel >= 4) {
4188         CBlockIndex *pindex = pindexState;
4189         while (pindex != chainActive.Tip()) {
4190             boost::this_thread::interruption_point();
4191             uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, 100 - (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * 50))));
4192             pindex = chainActive.Next(pindex);
4193             CBlock block;
4194             if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
4195                 return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4196             if (!ConnectBlock(block, state, pindex, coins, chainparams))
4197                 return error("VerifyDB(): *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4198         }
4199     }
4200 
4201     LogPrintf("[DONE].\n");
4202     LogPrintf("No coin database inconsistencies in last %i blocks (%i transactions)\n", chainActive.Height() - pindexState->nHeight, nGoodTransactions);
4203 
4204     return true;
4205 }
4206 
RewindBlockIndex(const CChainParams & params)4207 bool RewindBlockIndex(const CChainParams& params)
4208 {
4209     LOCK(cs_main);
4210 
4211     int nHeight = 1;
4212     while (nHeight <= chainActive.Height()) {
4213         if (IsWitnessEnabled(chainActive[nHeight - 1], params.GetConsensus()) && !(chainActive[nHeight]->nStatus & BLOCK_OPT_WITNESS)) {
4214             break;
4215         }
4216         nHeight++;
4217     }
4218 
4219     // nHeight is now the height of the first insufficiently-validated block, or tipheight + 1
4220     CValidationState state;
4221     CBlockIndex* pindex = chainActive.Tip();
4222     while (chainActive.Height() >= nHeight) {
4223         if (fPruneMode && !(chainActive.Tip()->nStatus & BLOCK_HAVE_DATA)) {
4224             // If pruning, don't try rewinding past the HAVE_DATA point;
4225             // since older blocks can't be served anyway, there's
4226             // no need to walk further, and trying to DisconnectTip()
4227             // will fail (and require a needless reindex/redownload
4228             // of the blockchain).
4229             break;
4230         }
4231         if (!DisconnectTip(state, params, true)) {
4232             return error("RewindBlockIndex: unable to disconnect block at height %i", pindex->nHeight);
4233         }
4234         // Occasionally flush state to disk.
4235         if (!FlushStateToDisk(state, FLUSH_STATE_PERIODIC))
4236             return false;
4237     }
4238 
4239     // Reduce validity flag and have-data flags.
4240     // We do this after actual disconnecting, otherwise we'll end up writing the lack of data
4241     // to disk before writing the chainstate, resulting in a failure to continue if interrupted.
4242     for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
4243         CBlockIndex* pindexIter = it->second;
4244 
4245         // Note: If we encounter an insufficiently validated block that
4246         // is on chainActive, it must be because we are a pruning node, and
4247         // this block or some successor doesn't HAVE_DATA, so we were unable to
4248         // rewind all the way.  Blocks remaining on chainActive at this point
4249         // must not have their validity reduced.
4250         if (IsWitnessEnabled(pindexIter->pprev, params.GetConsensus()) && !(pindexIter->nStatus & BLOCK_OPT_WITNESS) && !chainActive.Contains(pindexIter)) {
4251             // Reduce validity
4252             pindexIter->nStatus = std::min<unsigned int>(pindexIter->nStatus & BLOCK_VALID_MASK, BLOCK_VALID_TREE) | (pindexIter->nStatus & ~BLOCK_VALID_MASK);
4253             // Remove have-data flags.
4254             pindexIter->nStatus &= ~(BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO);
4255             // Remove storage location.
4256             pindexIter->nFile = 0;
4257             pindexIter->nDataPos = 0;
4258             pindexIter->nUndoPos = 0;
4259             // Remove various other things
4260             pindexIter->nTx = 0;
4261             pindexIter->nChainTx = 0;
4262             pindexIter->nSequenceId = 0;
4263             // Make sure it gets written.
4264             setDirtyBlockIndex.insert(pindexIter);
4265             // Update indexes
4266             setBlockIndexCandidates.erase(pindexIter);
4267             std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> ret = mapBlocksUnlinked.equal_range(pindexIter->pprev);
4268             while (ret.first != ret.second) {
4269                 if (ret.first->second == pindexIter) {
4270                     mapBlocksUnlinked.erase(ret.first++);
4271                 } else {
4272                     ++ret.first;
4273                 }
4274             }
4275         } else if (pindexIter->IsValid(BLOCK_VALID_TRANSACTIONS) && pindexIter->nChainTx) {
4276             setBlockIndexCandidates.insert(pindexIter);
4277         }
4278     }
4279 
4280     PruneBlockIndexCandidates();
4281 
4282     CheckBlockIndex(params.GetConsensus());
4283 
4284     if (!FlushStateToDisk(state, FLUSH_STATE_ALWAYS)) {
4285         return false;
4286     }
4287 
4288     return true;
4289 }
4290 
UnloadBlockIndex()4291 void UnloadBlockIndex()
4292 {
4293     LOCK(cs_main);
4294     setBlockIndexCandidates.clear();
4295     chainActive.SetTip(NULL);
4296     pindexBestInvalid = NULL;
4297     pindexBestHeader = NULL;
4298     mempool.clear();
4299     mapOrphanTransactions.clear();
4300     mapOrphanTransactionsByPrev.clear();
4301     nSyncStarted = 0;
4302     mapBlocksUnlinked.clear();
4303     vinfoBlockFile.clear();
4304     nLastBlockFile = 0;
4305     nBlockSequenceId = 1;
4306     mapBlockSource.clear();
4307     mapBlocksInFlight.clear();
4308     nPreferredDownload = 0;
4309     setDirtyBlockIndex.clear();
4310     setDirtyFileInfo.clear();
4311     mapNodeState.clear();
4312     recentRejects.reset(NULL);
4313     versionbitscache.Clear();
4314     for (int b = 0; b < VERSIONBITS_NUM_BITS; b++) {
4315         warningcache[b].clear();
4316     }
4317 
4318     BOOST_FOREACH(BlockMap::value_type& entry, mapBlockIndex) {
4319         delete entry.second;
4320     }
4321     mapBlockIndex.clear();
4322     fHavePruned = false;
4323 }
4324 
LoadBlockIndex()4325 bool LoadBlockIndex()
4326 {
4327     // Load block index from databases
4328     if (!fReindex && !LoadBlockIndexDB())
4329         return false;
4330     return true;
4331 }
4332 
InitBlockIndex(const CChainParams & chainparams)4333 bool InitBlockIndex(const CChainParams& chainparams)
4334 {
4335     LOCK(cs_main);
4336 
4337     // Initialize global variables that cannot be constructed at startup.
4338     recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
4339 
4340     // Check whether we're already initialized
4341     if (chainActive.Genesis() != NULL)
4342         return true;
4343 
4344     // Use the provided setting for -txindex in the new database
4345     fTxIndex = GetBoolArg("-txindex", DEFAULT_TXINDEX);
4346     pblocktree->WriteFlag("txindex", fTxIndex);
4347     LogPrintf("Initializing databases...\n");
4348 
4349     // Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
4350     if (!fReindex) {
4351         try {
4352             CBlock &block = const_cast<CBlock&>(chainparams.GenesisBlock());
4353             // Start new block file
4354             unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
4355             CDiskBlockPos blockPos;
4356             CValidationState state;
4357             if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.GetBlockTime()))
4358                 return error("LoadBlockIndex(): FindBlockPos failed");
4359             if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
4360                 return error("LoadBlockIndex(): writing genesis block to disk failed");
4361             CBlockIndex *pindex = AddToBlockIndex(block);
4362             if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
4363                 return error("LoadBlockIndex(): genesis block not accepted");
4364             // Force a chainstate write so that when we VerifyDB in a moment, it doesn't check stale data
4365             return FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
4366         } catch (const std::runtime_error& e) {
4367             return error("LoadBlockIndex(): failed to initialize block database: %s", e.what());
4368         }
4369     }
4370 
4371     return true;
4372 }
4373 
LoadExternalBlockFile(const CChainParams & chainparams,FILE * fileIn,CDiskBlockPos * dbp)4374 bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp)
4375 {
4376     // Map of disk positions for blocks with unknown parent (only used for reindex)
4377     static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
4378     int64_t nStart = GetTimeMillis();
4379 
4380     int nLoaded = 0;
4381     try {
4382         // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
4383         CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SERIALIZED_SIZE, MAX_BLOCK_SERIALIZED_SIZE+8, SER_DISK, CLIENT_VERSION);
4384         uint64_t nRewind = blkdat.GetPos();
4385         while (!blkdat.eof()) {
4386             boost::this_thread::interruption_point();
4387 
4388             blkdat.SetPos(nRewind);
4389             nRewind++; // start one byte further next time, in case of failure
4390             blkdat.SetLimit(); // remove former limit
4391             unsigned int nSize = 0;
4392             try {
4393                 // locate a header
4394                 unsigned char buf[MESSAGE_START_SIZE];
4395                 blkdat.FindByte(chainparams.MessageStart()[0]);
4396                 nRewind = blkdat.GetPos()+1;
4397                 blkdat >> FLATDATA(buf);
4398                 if (memcmp(buf, chainparams.MessageStart(), MESSAGE_START_SIZE))
4399                     continue;
4400                 // read size
4401                 blkdat >> nSize;
4402                 if (nSize < 80 || nSize > MAX_BLOCK_SERIALIZED_SIZE)
4403                     continue;
4404             } catch (const std::exception&) {
4405                 // no valid block header found; don't complain
4406                 break;
4407             }
4408             try {
4409                 // read block
4410                 uint64_t nBlockPos = blkdat.GetPos();
4411                 if (dbp)
4412                     dbp->nPos = nBlockPos;
4413                 blkdat.SetLimit(nBlockPos + nSize);
4414                 blkdat.SetPos(nBlockPos);
4415                 CBlock block;
4416                 blkdat >> block;
4417                 nRewind = blkdat.GetPos();
4418 
4419                 // detect out of order blocks, and store them for later
4420                 uint256 hash = block.GetHash();
4421                 if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex.find(block.hashPrevBlock) == mapBlockIndex.end()) {
4422                     LogPrint("reindex", "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
4423                             block.hashPrevBlock.ToString());
4424                     if (dbp)
4425                         mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
4426                     continue;
4427                 }
4428 
4429                 // process in case the block isn't known yet
4430                 if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
4431                     LOCK(cs_main);
4432                     CValidationState state;
4433                     if (AcceptBlock(block, state, chainparams, NULL, true, dbp, NULL))
4434                         nLoaded++;
4435                     if (state.IsError())
4436                         break;
4437                 } else if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex[hash]->nHeight % 1000 == 0) {
4438                     LogPrint("reindex", "Block Import: already had block %s at height %d\n", hash.ToString(), mapBlockIndex[hash]->nHeight);
4439                 }
4440 
4441                 // Activate the genesis block so normal node progress can continue
4442                 if (hash == chainparams.GetConsensus().hashGenesisBlock) {
4443                     CValidationState state;
4444                     if (!ActivateBestChain(state, chainparams)) {
4445                         break;
4446                     }
4447                 }
4448 
4449                 NotifyHeaderTip();
4450 
4451                 // Recursively process earlier encountered successors of this block
4452                 deque<uint256> queue;
4453                 queue.push_back(hash);
4454                 while (!queue.empty()) {
4455                     uint256 head = queue.front();
4456                     queue.pop_front();
4457                     std::pair<std::multimap<uint256, CDiskBlockPos>::iterator, std::multimap<uint256, CDiskBlockPos>::iterator> range = mapBlocksUnknownParent.equal_range(head);
4458                     while (range.first != range.second) {
4459                         std::multimap<uint256, CDiskBlockPos>::iterator it = range.first;
4460                         if (ReadBlockFromDisk(block, it->second, chainparams.GetConsensus()))
4461                         {
4462                             LogPrint("reindex", "%s: Processing out of order child %s of %s\n", __func__, block.GetHash().ToString(),
4463                                     head.ToString());
4464                             LOCK(cs_main);
4465                             CValidationState dummy;
4466                             if (AcceptBlock(block, dummy, chainparams, NULL, true, &it->second, NULL))
4467                             {
4468                                 nLoaded++;
4469                                 queue.push_back(block.GetHash());
4470                             }
4471                         }
4472                         range.first++;
4473                         mapBlocksUnknownParent.erase(it);
4474                         NotifyHeaderTip();
4475                     }
4476                 }
4477             } catch (const std::exception& e) {
4478                 LogPrintf("%s: Deserialize or I/O error - %s\n", __func__, e.what());
4479             }
4480         }
4481     } catch (const std::runtime_error& e) {
4482         AbortNode(std::string("System error: ") + e.what());
4483     }
4484     if (nLoaded > 0)
4485         LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded, GetTimeMillis() - nStart);
4486     return nLoaded > 0;
4487 }
4488 
CheckBlockIndex(const Consensus::Params & consensusParams)4489 void static CheckBlockIndex(const Consensus::Params& consensusParams)
4490 {
4491     if (!fCheckBlockIndex) {
4492         return;
4493     }
4494 
4495     LOCK(cs_main);
4496 
4497     // During a reindex, we read the genesis block and call CheckBlockIndex before ActivateBestChain,
4498     // so we have the genesis block in mapBlockIndex but no active chain.  (A few of the tests when
4499     // iterating the block tree require that chainActive has been initialized.)
4500     if (chainActive.Height() < 0) {
4501         assert(mapBlockIndex.size() <= 1);
4502         return;
4503     }
4504 
4505     // Build forward-pointing map of the entire block tree.
4506     std::multimap<CBlockIndex*,CBlockIndex*> forward;
4507     for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
4508         forward.insert(std::make_pair(it->second->pprev, it->second));
4509     }
4510 
4511     assert(forward.size() == mapBlockIndex.size());
4512 
4513     std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeGenesis = forward.equal_range(NULL);
4514     CBlockIndex *pindex = rangeGenesis.first->second;
4515     rangeGenesis.first++;
4516     assert(rangeGenesis.first == rangeGenesis.second); // There is only one index entry with parent NULL.
4517 
4518     // Iterate over the entire block tree, using depth-first search.
4519     // Along the way, remember whether there are blocks on the path from genesis
4520     // block being explored which are the first to have certain properties.
4521     size_t nNodes = 0;
4522     int nHeight = 0;
4523     CBlockIndex* pindexFirstInvalid = NULL; // Oldest ancestor of pindex which is invalid.
4524     CBlockIndex* pindexFirstMissing = NULL; // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA.
4525     CBlockIndex* pindexFirstNeverProcessed = NULL; // Oldest ancestor of pindex for which nTx == 0.
4526     CBlockIndex* pindexFirstNotTreeValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
4527     CBlockIndex* pindexFirstNotTransactionsValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not).
4528     CBlockIndex* pindexFirstNotChainValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not).
4529     CBlockIndex* pindexFirstNotScriptsValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not).
4530     while (pindex != NULL) {
4531         nNodes++;
4532         if (pindexFirstInvalid == NULL && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
4533         if (pindexFirstMissing == NULL && !(pindex->nStatus & BLOCK_HAVE_DATA)) pindexFirstMissing = pindex;
4534         if (pindexFirstNeverProcessed == NULL && pindex->nTx == 0) pindexFirstNeverProcessed = pindex;
4535         if (pindex->pprev != NULL && pindexFirstNotTreeValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TREE) pindexFirstNotTreeValid = pindex;
4536         if (pindex->pprev != NULL && pindexFirstNotTransactionsValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TRANSACTIONS) pindexFirstNotTransactionsValid = pindex;
4537         if (pindex->pprev != NULL && pindexFirstNotChainValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_CHAIN) pindexFirstNotChainValid = pindex;
4538         if (pindex->pprev != NULL && pindexFirstNotScriptsValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS) pindexFirstNotScriptsValid = pindex;
4539 
4540         // Begin: actual consistency checks.
4541         if (pindex->pprev == NULL) {
4542             // Genesis block checks.
4543             assert(pindex->GetBlockHash() == consensusParams.hashGenesisBlock); // Genesis block's hash must match.
4544             assert(pindex == chainActive.Genesis()); // The current active chain's genesis block must be this block.
4545         }
4546         if (pindex->nChainTx == 0) assert(pindex->nSequenceId == 0);  // nSequenceId can't be set for blocks that aren't linked
4547         // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred).
4548         // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
4549         if (!fHavePruned) {
4550             // If we've never pruned, then HAVE_DATA should be equivalent to nTx > 0
4551             assert(!(pindex->nStatus & BLOCK_HAVE_DATA) == (pindex->nTx == 0));
4552             assert(pindexFirstMissing == pindexFirstNeverProcessed);
4553         } else {
4554             // If we have pruned, then we can only say that HAVE_DATA implies nTx > 0
4555             if (pindex->nStatus & BLOCK_HAVE_DATA) assert(pindex->nTx > 0);
4556         }
4557         if (pindex->nStatus & BLOCK_HAVE_UNDO) assert(pindex->nStatus & BLOCK_HAVE_DATA);
4558         assert(((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS) == (pindex->nTx > 0)); // This is pruning-independent.
4559         // All parents having had data (at some point) is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to nChainTx being set.
4560         assert((pindexFirstNeverProcessed != NULL) == (pindex->nChainTx == 0)); // nChainTx != 0 is used to signal that all parent blocks have been processed (but may have been pruned).
4561         assert((pindexFirstNotTransactionsValid != NULL) == (pindex->nChainTx == 0));
4562         assert(pindex->nHeight == nHeight); // nHeight must be consistent.
4563         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.
4564         assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); // The pskip pointer must point back for all but the first 2 blocks.
4565         assert(pindexFirstNotTreeValid == NULL); // All mapBlockIndex entries must at least be TREE valid
4566         if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TREE) assert(pindexFirstNotTreeValid == NULL); // TREE valid implies all parents are TREE valid
4567         if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_CHAIN) assert(pindexFirstNotChainValid == NULL); // CHAIN valid implies all parents are CHAIN valid
4568         if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_SCRIPTS) assert(pindexFirstNotScriptsValid == NULL); // SCRIPTS valid implies all parents are SCRIPTS valid
4569         if (pindexFirstInvalid == NULL) {
4570             // Checks for not-invalid blocks.
4571             assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
4572         }
4573         if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && pindexFirstNeverProcessed == NULL) {
4574             if (pindexFirstInvalid == NULL) {
4575                 // If this block sorts at least as good as the current tip and
4576                 // is valid and we have all data for its parents, it must be in
4577                 // setBlockIndexCandidates.  chainActive.Tip() must also be there
4578                 // even if some data has been pruned.
4579                 if (pindexFirstMissing == NULL || pindex == chainActive.Tip()) {
4580                     assert(setBlockIndexCandidates.count(pindex));
4581                 }
4582                 // If some parent is missing, then it could be that this block was in
4583                 // setBlockIndexCandidates but had to be removed because of the missing data.
4584                 // In this case it must be in mapBlocksUnlinked -- see test below.
4585             }
4586         } else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates.
4587             assert(setBlockIndexCandidates.count(pindex) == 0);
4588         }
4589         // Check whether this block is in mapBlocksUnlinked.
4590         std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked = mapBlocksUnlinked.equal_range(pindex->pprev);
4591         bool foundInUnlinked = false;
4592         while (rangeUnlinked.first != rangeUnlinked.second) {
4593             assert(rangeUnlinked.first->first == pindex->pprev);
4594             if (rangeUnlinked.first->second == pindex) {
4595                 foundInUnlinked = true;
4596                 break;
4597             }
4598             rangeUnlinked.first++;
4599         }
4600         if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed != NULL && pindexFirstInvalid == NULL) {
4601             // If this block has block data available, some parent was never received, and has no invalid parents, it must be in mapBlocksUnlinked.
4602             assert(foundInUnlinked);
4603         }
4604         if (!(pindex->nStatus & BLOCK_HAVE_DATA)) assert(!foundInUnlinked); // Can't be in mapBlocksUnlinked if we don't HAVE_DATA
4605         if (pindexFirstMissing == NULL) assert(!foundInUnlinked); // We aren't missing data for any parent -- cannot be in mapBlocksUnlinked.
4606         if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed == NULL && pindexFirstMissing != NULL) {
4607             // We HAVE_DATA for this block, have received data for all parents at some point, but we're currently missing data for some parent.
4608             assert(fHavePruned); // We must have pruned.
4609             // This block may have entered mapBlocksUnlinked if:
4610             //  - it has a descendant that at some point had more work than the
4611             //    tip, and
4612             //  - we tried switching to that descendant but were missing
4613             //    data for some intermediate block between chainActive and the
4614             //    tip.
4615             // So if this block is itself better than chainActive.Tip() and it wasn't in
4616             // setBlockIndexCandidates, then it must be in mapBlocksUnlinked.
4617             if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && setBlockIndexCandidates.count(pindex) == 0) {
4618                 if (pindexFirstInvalid == NULL) {
4619                     assert(foundInUnlinked);
4620                 }
4621             }
4622         }
4623         // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow
4624         // End: actual consistency checks.
4625 
4626         // Try descending into the first subnode.
4627         std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
4628         if (range.first != range.second) {
4629             // A subnode was found.
4630             pindex = range.first->second;
4631             nHeight++;
4632             continue;
4633         }
4634         // This is a leaf node.
4635         // Move upwards until we reach a node of which we have not yet visited the last child.
4636         while (pindex) {
4637             // We are going to either move to a parent or a sibling of pindex.
4638             // If pindex was the first with a certain property, unset the corresponding variable.
4639             if (pindex == pindexFirstInvalid) pindexFirstInvalid = NULL;
4640             if (pindex == pindexFirstMissing) pindexFirstMissing = NULL;
4641             if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed = NULL;
4642             if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid = NULL;
4643             if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid = NULL;
4644             if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid = NULL;
4645             if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid = NULL;
4646             // Find our parent.
4647             CBlockIndex* pindexPar = pindex->pprev;
4648             // Find which child we just visited.
4649             std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
4650             while (rangePar.first->second != pindex) {
4651                 assert(rangePar.first != rangePar.second); // Our parent must have at least the node we're coming from as child.
4652                 rangePar.first++;
4653             }
4654             // Proceed to the next one.
4655             rangePar.first++;
4656             if (rangePar.first != rangePar.second) {
4657                 // Move to the sibling.
4658                 pindex = rangePar.first->second;
4659                 break;
4660             } else {
4661                 // Move up further.
4662                 pindex = pindexPar;
4663                 nHeight--;
4664                 continue;
4665             }
4666         }
4667     }
4668 
4669     // Check that we actually traversed the entire map.
4670     assert(nNodes == forward.size());
4671 }
4672 
GetWarnings(const std::string & strFor)4673 std::string GetWarnings(const std::string& strFor)
4674 {
4675     string strStatusBar;
4676     string strRPC;
4677     string strGUI;
4678     const string uiAlertSeperator = "<hr />";
4679 
4680     if (!CLIENT_VERSION_IS_RELEASE) {
4681         strStatusBar = "This is a pre-release test build - use at your own risk - do not use for mining or merchant applications";
4682         strGUI = _("This is a pre-release test build - use at your own risk - do not use for mining or merchant applications");
4683     }
4684 
4685     if (GetBoolArg("-testsafemode", DEFAULT_TESTSAFEMODE))
4686         strStatusBar = strRPC = strGUI = "testsafemode enabled";
4687 
4688     // Misc warnings like out of disk space and clock is wrong
4689     if (strMiscWarning != "")
4690     {
4691         strStatusBar = strMiscWarning;
4692         strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + strMiscWarning;
4693     }
4694 
4695     if (fLargeWorkForkFound)
4696     {
4697         strStatusBar = strRPC = "Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.";
4698         strGUI += (strGUI.empty() ? "" : uiAlertSeperator) + _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.");
4699     }
4700     else if (fLargeWorkInvalidChainFound)
4701     {
4702         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.";
4703         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.");
4704     }
4705 
4706     if (strFor == "gui")
4707         return strGUI;
4708     else if (strFor == "statusbar")
4709         return strStatusBar;
4710     else if (strFor == "rpc")
4711         return strRPC;
4712     assert(!"GetWarnings(): invalid parameter");
4713     return "error";
4714 }
4715 
4716 
4717 
4718 
4719 
4720 
4721 
4722 
4723 //////////////////////////////////////////////////////////////////////////////
4724 // Messages
4725 //
4726 
4727 
AlreadyHave(const CInv & inv)4728 bool static AlreadyHave(const CInv& inv) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
4729 {
4730     switch (inv.type)
4731     {
4732     case MSG_TX:
4733     case MSG_WITNESS_TX:
4734         {
4735             assert(recentRejects);
4736             if (chainActive.Tip()->GetBlockHash() != hashRecentRejectsChainTip)
4737             {
4738                 // If the chain tip has changed previously rejected transactions
4739                 // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
4740                 // or a double-spend. Reset the rejects filter and give those
4741                 // txs a second chance.
4742                 hashRecentRejectsChainTip = chainActive.Tip()->GetBlockHash();
4743                 recentRejects->reset();
4744             }
4745 
4746             // Use pcoinsTip->HaveCoinsInCache as a quick approximation to exclude
4747             // requesting or processing some txs which have already been included in a block
4748             return recentRejects->contains(inv.hash) ||
4749                    mempool.exists(inv.hash) ||
4750                    mapOrphanTransactions.count(inv.hash) ||
4751                    pcoinsTip->HaveCoinsInCache(inv.hash);
4752         }
4753     case MSG_BLOCK:
4754     case MSG_WITNESS_BLOCK:
4755         return mapBlockIndex.count(inv.hash);
4756     }
4757     // Don't know what it is, just say we already got one
4758     return true;
4759 }
4760 
ProcessGetData(CNode * pfrom,const Consensus::Params & consensusParams)4761 void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParams)
4762 {
4763     std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();
4764 
4765     vector<CInv> vNotFound;
4766 
4767     LOCK(cs_main);
4768 
4769     while (it != pfrom->vRecvGetData.end()) {
4770         // Don't bother if send buffer is too full to respond anyway
4771         if (pfrom->nSendSize >= SendBufferSize())
4772             break;
4773 
4774         const CInv &inv = *it;
4775         {
4776             boost::this_thread::interruption_point();
4777             it++;
4778 
4779             if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK)
4780             {
4781                 bool send = false;
4782                 BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
4783                 if (mi != mapBlockIndex.end())
4784                 {
4785                     if (chainActive.Contains(mi->second)) {
4786                         send = true;
4787                     } else {
4788                         static const int nOneMonth = 30 * 24 * 60 * 60;
4789                         // To prevent fingerprinting attacks, only send blocks outside of the active
4790                         // chain if they are valid, and no more than a month older (both in time, and in
4791                         // best equivalent proof of work) than the best header chain we know about.
4792                         send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != NULL) &&
4793                             (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() < nOneMonth) &&
4794                             (GetBlockProofEquivalentTime(*pindexBestHeader, *mi->second, *pindexBestHeader, consensusParams) < nOneMonth);
4795                         if (!send) {
4796                             LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
4797                         }
4798                     }
4799                 }
4800                 // disconnect node in case we have reached the outbound limit for serving historical blocks
4801                 // never disconnect whitelisted nodes
4802                 static const int nOneWeek = 7 * 24 * 60 * 60; // assume > 1 week = historical
4803                 if (send && CNode::OutboundTargetReached(true) && ( ((pindexBestHeader != NULL) && (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() > nOneWeek)) || inv.type == MSG_FILTERED_BLOCK) && !pfrom->fWhitelisted)
4804                 {
4805                     LogPrint("net", "historical block serving limit reached, disconnect peer=%d\n", pfrom->GetId());
4806 
4807                     //disconnect node
4808                     pfrom->fDisconnect = true;
4809                     send = false;
4810                 }
4811                 // Pruned nodes may have deleted the block, so check whether
4812                 // it's available before trying to send.
4813                 if (send && (mi->second->nStatus & BLOCK_HAVE_DATA))
4814                 {
4815                     // Send block from disk
4816                     CBlock block;
4817                     if (!ReadBlockFromDisk(block, (*mi).second, consensusParams))
4818                         assert(!"cannot load block from disk");
4819                     if (inv.type == MSG_BLOCK)
4820                         pfrom->PushMessageWithFlag(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, block);
4821                     else if (inv.type == MSG_WITNESS_BLOCK)
4822                         pfrom->PushMessage(NetMsgType::BLOCK, block);
4823                     else if (inv.type == MSG_FILTERED_BLOCK)
4824                     {
4825                         bool send = false;
4826                         CMerkleBlock merkleBlock;
4827                         {
4828                             LOCK(pfrom->cs_filter);
4829                             if (pfrom->pfilter) {
4830                                 send = true;
4831                                 merkleBlock = CMerkleBlock(block, *pfrom->pfilter);
4832                             }
4833                         }
4834                         if (send) {
4835                             pfrom->PushMessage(NetMsgType::MERKLEBLOCK, merkleBlock);
4836                             // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
4837                             // This avoids hurting performance by pointlessly requiring a round-trip
4838                             // Note that there is currently no way for a node to request any single transactions we didn't send here -
4839                             // they must either disconnect and retry or request the full block.
4840                             // Thus, the protocol spec specified allows for us to provide duplicate txn here,
4841                             // however we MUST always provide at least what the remote peer needs
4842                             typedef std::pair<unsigned int, uint256> PairType;
4843                             BOOST_FOREACH(PairType& pair, merkleBlock.vMatchedTxn)
4844                                 pfrom->PushMessageWithFlag(SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::TX, block.vtx[pair.first]);
4845                         }
4846                         // else
4847                             // no response
4848                     }
4849                     else if (inv.type == MSG_CMPCT_BLOCK)
4850                     {
4851                         // If a peer is asking for old blocks, we're almost guaranteed
4852                         // they wont have a useful mempool to match against a compact block,
4853                         // and we don't feel like constructing the object for them, so
4854                         // instead we respond with the full, non-compact block.
4855                         bool fPeerWantsWitness = State(pfrom->GetId())->fWantsCmpctWitness;
4856                         if (CanDirectFetch(consensusParams) && mi->second->nHeight >= chainActive.Height() - MAX_CMPCTBLOCK_DEPTH) {
4857                             CBlockHeaderAndShortTxIDs cmpctblock(block, fPeerWantsWitness);
4858                             pfrom->PushMessageWithFlag(fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::CMPCTBLOCK, cmpctblock);
4859                         } else
4860                             pfrom->PushMessageWithFlag(fPeerWantsWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCK, block);
4861                     }
4862 
4863                     // Trigger the peer node to send a getblocks request for the next batch of inventory
4864                     if (inv.hash == pfrom->hashContinue)
4865                     {
4866                         // Bypass PushInventory, this must send even if redundant,
4867                         // and we want it right after the last block so they don't
4868                         // wait for other stuff first.
4869                         vector<CInv> vInv;
4870                         vInv.push_back(CInv(MSG_BLOCK, chainActive.Tip()->GetBlockHash()));
4871                         pfrom->PushMessage(NetMsgType::INV, vInv);
4872                         pfrom->hashContinue.SetNull();
4873                     }
4874                 }
4875             }
4876             else if (inv.type == MSG_TX || inv.type == MSG_WITNESS_TX)
4877             {
4878                 // Send stream from relay memory
4879                 bool push = false;
4880                 auto mi = mapRelay.find(inv.hash);
4881                 if (mi != mapRelay.end()) {
4882                     pfrom->PushMessageWithFlag(inv.type == MSG_TX ? SERIALIZE_TRANSACTION_NO_WITNESS : 0, NetMsgType::TX, *mi->second);
4883                     push = true;
4884                 } else if (pfrom->timeLastMempoolReq) {
4885                     auto txinfo = mempool.info(inv.hash);
4886                     // To protect privacy, do not answer getdata using the mempool when
4887                     // that TX couldn't have been INVed in reply to a MEMPOOL request.
4888                     if (txinfo.tx && txinfo.nTime <= pfrom->timeLastMempoolReq) {
4889                         pfrom->PushMessageWithFlag(inv.type == MSG_TX ? SERIALIZE_TRANSACTION_NO_WITNESS : 0, NetMsgType::TX, *txinfo.tx);
4890                         push = true;
4891                     }
4892                 }
4893                 if (!push) {
4894                     vNotFound.push_back(inv);
4895                 }
4896             }
4897 
4898             // Track requests for our stuff.
4899             GetMainSignals().Inventory(inv.hash);
4900 
4901             if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK || inv.type == MSG_CMPCT_BLOCK || inv.type == MSG_WITNESS_BLOCK)
4902                 break;
4903         }
4904     }
4905 
4906     pfrom->vRecvGetData.erase(pfrom->vRecvGetData.begin(), it);
4907 
4908     if (!vNotFound.empty()) {
4909         // Let the peer know that we didn't find what it asked for, so it doesn't
4910         // have to wait around forever. Currently only SPV clients actually care
4911         // about this message: it's needed when they are recursively walking the
4912         // dependencies of relevant unconfirmed transactions. SPV clients want to
4913         // do that because they want to know about (and store and rebroadcast and
4914         // risk analyze) the dependencies of transactions relevant to them, without
4915         // having to download the entire memory pool.
4916         pfrom->PushMessage(NetMsgType::NOTFOUND, vNotFound);
4917     }
4918 }
4919 
GetFetchFlags(CNode * pfrom,CBlockIndex * pprev,const Consensus::Params & chainparams)4920 uint32_t GetFetchFlags(CNode* pfrom, CBlockIndex* pprev, const Consensus::Params& chainparams) {
4921     uint32_t nFetchFlags = 0;
4922     if ((nLocalServices & NODE_WITNESS) && State(pfrom->GetId())->fHaveWitness) {
4923         nFetchFlags |= MSG_WITNESS_FLAG;
4924     }
4925     return nFetchFlags;
4926 }
4927 
ProcessMessage(CNode * pfrom,string strCommand,CDataStream & vRecv,int64_t nTimeReceived,const CChainParams & chainparams)4928 bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived, const CChainParams& chainparams)
4929 {
4930     LogPrint("net", "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->id);
4931     if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
4932     {
4933         LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
4934         return true;
4935     }
4936 
4937 
4938     if (!(nLocalServices & NODE_BLOOM) &&
4939               (strCommand == NetMsgType::FILTERLOAD ||
4940                strCommand == NetMsgType::FILTERADD ||
4941                strCommand == NetMsgType::FILTERCLEAR))
4942     {
4943         if (pfrom->nVersion >= NO_BLOOM_VERSION) {
4944             LOCK(cs_main);
4945             Misbehaving(pfrom->GetId(), 100);
4946             return false;
4947         } else {
4948             pfrom->fDisconnect = true;
4949             return false;
4950         }
4951     }
4952 
4953 
4954     if (strCommand == NetMsgType::VERSION)
4955     {
4956         // Feeler connections exist only to verify if address is online.
4957         if (pfrom->fFeeler) {
4958             assert(pfrom->fInbound == false);
4959             pfrom->fDisconnect = true;
4960         }
4961 
4962         // Each connection can only send one version message
4963         if (pfrom->nVersion != 0)
4964         {
4965             pfrom->PushMessage(NetMsgType::REJECT, strCommand, REJECT_DUPLICATE, string("Duplicate version message"));
4966             LOCK(cs_main);
4967             Misbehaving(pfrom->GetId(), 1);
4968             return false;
4969         }
4970 
4971         int64_t nTime;
4972         CAddress addrMe;
4973         CAddress addrFrom;
4974         uint64_t nNonce = 1;
4975         uint64_t nServiceInt;
4976         vRecv >> pfrom->nVersion >> nServiceInt >> nTime >> addrMe;
4977         pfrom->nServices = ServiceFlags(nServiceInt);
4978         if (!pfrom->fInbound)
4979         {
4980             addrman.SetServices(pfrom->addr, pfrom->nServices);
4981         }
4982         if (pfrom->nServicesExpected & ~pfrom->nServices)
4983         {
4984             LogPrint("net", "peer=%d does not offer the expected services (%08x offered, %08x expected); disconnecting\n", pfrom->id, pfrom->nServices, pfrom->nServicesExpected);
4985             pfrom->PushMessage(NetMsgType::REJECT, strCommand, REJECT_NONSTANDARD,
4986                                strprintf("Expected to offer services %08x", pfrom->nServicesExpected));
4987             pfrom->fDisconnect = true;
4988             return false;
4989         }
4990 
4991         if (pfrom->nVersion < MIN_PEER_PROTO_VERSION)
4992         {
4993             // disconnect from peers older than this proto version
4994             LogPrintf("peer=%d using obsolete version %i; disconnecting\n", pfrom->id, pfrom->nVersion);
4995             pfrom->PushMessage(NetMsgType::REJECT, strCommand, REJECT_OBSOLETE,
4996                                strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION));
4997             pfrom->fDisconnect = true;
4998             return false;
4999         }
5000 
5001         if (pfrom->nVersion == 10300)
5002             pfrom->nVersion = 300;
5003         if (!vRecv.empty())
5004             vRecv >> addrFrom >> nNonce;
5005         if (!vRecv.empty()) {
5006             vRecv >> LIMITED_STRING(pfrom->strSubVer, MAX_SUBVERSION_LENGTH);
5007             pfrom->cleanSubVer = SanitizeString(pfrom->strSubVer);
5008         }
5009         if (!vRecv.empty()) {
5010             vRecv >> pfrom->nStartingHeight;
5011         }
5012         {
5013             LOCK(pfrom->cs_filter);
5014             if (!vRecv.empty())
5015                 vRecv >> pfrom->fRelayTxes; // set to true after we get the first filter* message
5016             else
5017                 pfrom->fRelayTxes = true;
5018         }
5019 
5020         // Disconnect if we connected to ourself
5021         if (nNonce == nLocalHostNonce && nNonce > 1)
5022         {
5023             LogPrintf("connected to self at %s, disconnecting\n", pfrom->addr.ToString());
5024             pfrom->fDisconnect = true;
5025             return true;
5026         }
5027 
5028         pfrom->addrLocal = addrMe;
5029         if (pfrom->fInbound && addrMe.IsRoutable())
5030         {
5031             SeenLocal(addrMe);
5032         }
5033 
5034         // Be shy and don't send version until we hear
5035         if (pfrom->fInbound)
5036             pfrom->PushVersion();
5037 
5038         pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
5039 
5040         if((pfrom->nServices & NODE_WITNESS))
5041         {
5042             LOCK(cs_main);
5043             State(pfrom->GetId())->fHaveWitness = true;
5044         }
5045 
5046         // Potentially mark this peer as a preferred download peer.
5047         {
5048         LOCK(cs_main);
5049         UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
5050         }
5051 
5052         // Change version
5053         pfrom->PushMessage(NetMsgType::VERACK);
5054         pfrom->ssSend.SetVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
5055 
5056         if (!pfrom->fInbound)
5057         {
5058             // Advertise our address
5059             if (fListen && !IsInitialBlockDownload())
5060             {
5061                 CAddress addr = GetLocalAddress(&pfrom->addr);
5062                 if (addr.IsRoutable())
5063                 {
5064                     LogPrintf("ProcessMessages: advertising address %s\n", addr.ToString());
5065                     pfrom->PushAddress(addr);
5066                 } else if (IsPeerAddrLocalGood(pfrom)) {
5067                     addr.SetIP(pfrom->addrLocal);
5068                     LogPrintf("ProcessMessages: advertising address %s\n", addr.ToString());
5069                     pfrom->PushAddress(addr);
5070                 }
5071             }
5072 
5073             // Get recent addresses
5074             if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || addrman.size() < 1000)
5075             {
5076                 pfrom->PushMessage(NetMsgType::GETADDR);
5077                 pfrom->fGetAddr = true;
5078             }
5079             addrman.Good(pfrom->addr);
5080         }
5081 
5082         pfrom->fSuccessfullyConnected = true;
5083 
5084         string remoteAddr;
5085         if (fLogIPs)
5086             remoteAddr = ", peeraddr=" + pfrom->addr.ToString();
5087 
5088         LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
5089                   pfrom->cleanSubVer, pfrom->nVersion,
5090                   pfrom->nStartingHeight, addrMe.ToString(), pfrom->id,
5091                   remoteAddr);
5092 
5093         int64_t nTimeOffset = nTime - GetTime();
5094         pfrom->nTimeOffset = nTimeOffset;
5095         AddTimeData(pfrom->addr, nTimeOffset);
5096     }
5097 
5098 
5099     else if (pfrom->nVersion == 0)
5100     {
5101         // Must have a version message before anything else
5102         LOCK(cs_main);
5103         Misbehaving(pfrom->GetId(), 1);
5104         return false;
5105     }
5106 
5107 
5108     else if (strCommand == NetMsgType::VERACK)
5109     {
5110         pfrom->SetRecvVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
5111 
5112         // Mark this node as currently connected, so we update its timestamp later.
5113         if (pfrom->fNetworkNode) {
5114             LOCK(cs_main);
5115             State(pfrom->GetId())->fCurrentlyConnected = true;
5116         }
5117 
5118         if (pfrom->nVersion >= SENDHEADERS_VERSION) {
5119             // Tell our peer we prefer to receive headers rather than inv's
5120             // We send this to non-NODE NETWORK peers as well, because even
5121             // non-NODE NETWORK peers can announce blocks (such as pruning
5122             // nodes)
5123             pfrom->PushMessage(NetMsgType::SENDHEADERS);
5124         }
5125         if (pfrom->nVersion >= SHORT_IDS_BLOCKS_VERSION) {
5126             // Tell our peer we are willing to provide version 1 or 2 cmpctblocks
5127             // However, we do not request new block announcements using
5128             // cmpctblock messages.
5129             // We send this to non-NODE NETWORK peers as well, because
5130             // they may wish to request compact blocks from us
5131             bool fAnnounceUsingCMPCTBLOCK = false;
5132             uint64_t nCMPCTBLOCKVersion = 2;
5133             if (nLocalServices & NODE_WITNESS)
5134                 pfrom->PushMessage(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion);
5135             nCMPCTBLOCKVersion = 1;
5136             pfrom->PushMessage(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion);
5137         }
5138     }
5139 
5140 
5141     else if (strCommand == NetMsgType::ADDR)
5142     {
5143         vector<CAddress> vAddr;
5144         vRecv >> vAddr;
5145 
5146         // Don't want addr from older versions unless seeding
5147         if (pfrom->nVersion < CADDR_TIME_VERSION && addrman.size() > 1000)
5148             return true;
5149         if (vAddr.size() > 1000)
5150         {
5151             LOCK(cs_main);
5152             Misbehaving(pfrom->GetId(), 20);
5153             return error("message addr size() = %u", vAddr.size());
5154         }
5155 
5156         // Store the new addresses
5157         vector<CAddress> vAddrOk;
5158         int64_t nNow = GetAdjustedTime();
5159         int64_t nSince = nNow - 10 * 60;
5160         BOOST_FOREACH(CAddress& addr, vAddr)
5161         {
5162             boost::this_thread::interruption_point();
5163 
5164             if ((addr.nServices & REQUIRED_SERVICES) != REQUIRED_SERVICES)
5165                 continue;
5166 
5167             if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
5168                 addr.nTime = nNow - 5 * 24 * 60 * 60;
5169             pfrom->AddAddressKnown(addr);
5170             bool fReachable = IsReachable(addr);
5171             if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
5172             {
5173                 // Relay to a limited number of other nodes
5174                 {
5175                     LOCK(cs_vNodes);
5176                     // Use deterministic randomness to send to the same nodes for 24 hours
5177                     // at a time so the addrKnowns of the chosen nodes prevent repeats
5178                     static const uint64_t salt0 = GetRand(std::numeric_limits<uint64_t>::max());
5179                     static const uint64_t salt1 = GetRand(std::numeric_limits<uint64_t>::max());
5180                     uint64_t hashAddr = addr.GetHash();
5181                     multimap<uint64_t, CNode*> mapMix;
5182                     const CSipHasher hasher = CSipHasher(salt0, salt1).Write(hashAddr << 32).Write((GetTime() + hashAddr) / (24*60*60));
5183                     BOOST_FOREACH(CNode* pnode, vNodes)
5184                     {
5185                         if (pnode->nVersion < CADDR_TIME_VERSION)
5186                             continue;
5187                         uint64_t hashKey = CSipHasher(hasher).Write(pnode->id).Finalize();
5188                         mapMix.insert(make_pair(hashKey, pnode));
5189                     }
5190                     int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
5191                     for (multimap<uint64_t, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
5192                         ((*mi).second)->PushAddress(addr);
5193                 }
5194             }
5195             // Do not store addresses outside our network
5196             if (fReachable)
5197                 vAddrOk.push_back(addr);
5198         }
5199         addrman.Add(vAddrOk, pfrom->addr, 2 * 60 * 60);
5200         if (vAddr.size() < 1000)
5201             pfrom->fGetAddr = false;
5202         if (pfrom->fOneShot)
5203             pfrom->fDisconnect = true;
5204     }
5205 
5206     else if (strCommand == NetMsgType::SENDHEADERS)
5207     {
5208         LOCK(cs_main);
5209         State(pfrom->GetId())->fPreferHeaders = true;
5210     }
5211 
5212     else if (strCommand == NetMsgType::SENDCMPCT)
5213     {
5214         bool fAnnounceUsingCMPCTBLOCK = false;
5215         uint64_t nCMPCTBLOCKVersion = 0;
5216         vRecv >> fAnnounceUsingCMPCTBLOCK >> nCMPCTBLOCKVersion;
5217         if (nCMPCTBLOCKVersion == 1 || ((nLocalServices & NODE_WITNESS) && nCMPCTBLOCKVersion == 2)) {
5218             LOCK(cs_main);
5219             // fProvidesHeaderAndIDs is used to "lock in" version of compact blocks we send (fWantsCmpctWitness)
5220             if (!State(pfrom->GetId())->fProvidesHeaderAndIDs) {
5221                 State(pfrom->GetId())->fProvidesHeaderAndIDs = true;
5222                 State(pfrom->GetId())->fWantsCmpctWitness = nCMPCTBLOCKVersion == 2;
5223             }
5224             if (State(pfrom->GetId())->fWantsCmpctWitness == (nCMPCTBLOCKVersion == 2)) // ignore later version announces
5225                 State(pfrom->GetId())->fPreferHeaderAndIDs = fAnnounceUsingCMPCTBLOCK;
5226             if (!State(pfrom->GetId())->fSupportsDesiredCmpctVersion) {
5227                 if (nLocalServices & NODE_WITNESS)
5228                     State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 2);
5229                 else
5230                     State(pfrom->GetId())->fSupportsDesiredCmpctVersion = (nCMPCTBLOCKVersion == 1);
5231             }
5232         }
5233     }
5234 
5235 
5236     else if (strCommand == NetMsgType::INV)
5237     {
5238         vector<CInv> vInv;
5239         vRecv >> vInv;
5240         if (vInv.size() > MAX_INV_SZ)
5241         {
5242             LOCK(cs_main);
5243             Misbehaving(pfrom->GetId(), 20);
5244             return error("message inv size() = %u", vInv.size());
5245         }
5246 
5247         bool fBlocksOnly = !fRelayTxes;
5248 
5249         // Allow whitelisted peers to send data other than blocks in blocks only mode if whitelistrelay is true
5250         if (pfrom->fWhitelisted && GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY))
5251             fBlocksOnly = false;
5252 
5253         LOCK(cs_main);
5254 
5255         uint32_t nFetchFlags = GetFetchFlags(pfrom, chainActive.Tip(), chainparams.GetConsensus());
5256 
5257         std::vector<CInv> vToFetch;
5258 
5259         for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
5260         {
5261             CInv &inv = vInv[nInv];
5262 
5263             boost::this_thread::interruption_point();
5264 
5265             bool fAlreadyHave = AlreadyHave(inv);
5266             LogPrint("net", "got inv: %s  %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom->id);
5267 
5268             if (inv.type == MSG_TX) {
5269                 inv.type |= nFetchFlags;
5270             }
5271 
5272             if (inv.type == MSG_BLOCK) {
5273                 UpdateBlockAvailability(pfrom->GetId(), inv.hash);
5274                 if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
5275                     // First request the headers preceding the announced block. In the normal fully-synced
5276                     // case where a new block is announced that succeeds the current tip (no reorganization),
5277                     // there are no such headers.
5278                     // Secondly, and only when we are close to being synced, we request the announced block directly,
5279                     // to avoid an extra round-trip. Note that we must *first* ask for the headers, so by the
5280                     // time the block arrives, the header chain leading up to it is already validated. Not
5281                     // doing this will result in the received block being rejected as an orphan in case it is
5282                     // not a direct successor.
5283                     pfrom->PushMessage(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), inv.hash);
5284                     CNodeState *nodestate = State(pfrom->GetId());
5285                     if (CanDirectFetch(chainparams.GetConsensus()) &&
5286                         nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER &&
5287                         (!IsWitnessEnabled(chainActive.Tip(), chainparams.GetConsensus()) || State(pfrom->GetId())->fHaveWitness)) {
5288                         inv.type |= nFetchFlags;
5289                         if (nodestate->fSupportsDesiredCmpctVersion)
5290                             vToFetch.push_back(CInv(MSG_CMPCT_BLOCK, inv.hash));
5291                         else
5292                             vToFetch.push_back(inv);
5293                         // Mark block as in flight already, even though the actual "getdata" message only goes out
5294                         // later (within the same cs_main lock, though).
5295                         MarkBlockAsInFlight(pfrom->GetId(), inv.hash, chainparams.GetConsensus());
5296                     }
5297                     LogPrint("net", "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, inv.hash.ToString(), pfrom->id);
5298                 }
5299             }
5300             else
5301             {
5302                 pfrom->AddInventoryKnown(inv);
5303                 if (fBlocksOnly)
5304                     LogPrint("net", "transaction (%s) inv sent in violation of protocol peer=%d\n", inv.hash.ToString(), pfrom->id);
5305                 else if (!fAlreadyHave && !fImporting && !fReindex && !IsInitialBlockDownload())
5306                     pfrom->AskFor(inv);
5307             }
5308 
5309             // Track requests for our stuff
5310             GetMainSignals().Inventory(inv.hash);
5311 
5312             if (pfrom->nSendSize > (SendBufferSize() * 2)) {
5313                 Misbehaving(pfrom->GetId(), 50);
5314                 return error("send buffer size() = %u", pfrom->nSendSize);
5315             }
5316         }
5317 
5318         if (!vToFetch.empty())
5319             pfrom->PushMessage(NetMsgType::GETDATA, vToFetch);
5320     }
5321 
5322 
5323     else if (strCommand == NetMsgType::GETDATA)
5324     {
5325         vector<CInv> vInv;
5326         vRecv >> vInv;
5327         if (vInv.size() > MAX_INV_SZ)
5328         {
5329             LOCK(cs_main);
5330             Misbehaving(pfrom->GetId(), 20);
5331             return error("message getdata size() = %u", vInv.size());
5332         }
5333 
5334         if (fDebug || (vInv.size() != 1))
5335             LogPrint("net", "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom->id);
5336 
5337         if ((fDebug && vInv.size() > 0) || (vInv.size() == 1))
5338             LogPrint("net", "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom->id);
5339 
5340         pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
5341         ProcessGetData(pfrom, chainparams.GetConsensus());
5342     }
5343 
5344 
5345     else if (strCommand == NetMsgType::GETBLOCKS)
5346     {
5347         CBlockLocator locator;
5348         uint256 hashStop;
5349         vRecv >> locator >> hashStop;
5350 
5351         LOCK(cs_main);
5352 
5353         // Find the last block the caller has in the main chain
5354         CBlockIndex* pindex = FindForkInGlobalIndex(chainActive, locator);
5355 
5356         // Send the rest of the chain
5357         if (pindex)
5358             pindex = chainActive.Next(pindex);
5359         int nLimit = 500;
5360         LogPrint("net", "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), nLimit, pfrom->id);
5361         for (; pindex; pindex = chainActive.Next(pindex))
5362         {
5363             if (pindex->GetBlockHash() == hashStop)
5364             {
5365                 LogPrint("net", "  getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
5366                 break;
5367             }
5368             // If pruning, don't inv blocks unless we have on disk and are likely to still have
5369             // for some reasonable time window (1 hour) that block relay might require.
5370             const int nPrunedBlocksLikelyToHave = MIN_BLOCKS_TO_KEEP - 3600 / chainparams.GetConsensus().nPowTargetSpacing;
5371             if (fPruneMode && (!(pindex->nStatus & BLOCK_HAVE_DATA) || pindex->nHeight <= chainActive.Tip()->nHeight - nPrunedBlocksLikelyToHave))
5372             {
5373                 LogPrint("net", " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
5374                 break;
5375             }
5376             pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
5377             if (--nLimit <= 0)
5378             {
5379                 // When this block is requested, we'll send an inv that'll
5380                 // trigger the peer to getblocks the next batch of inventory.
5381                 LogPrint("net", "  getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
5382                 pfrom->hashContinue = pindex->GetBlockHash();
5383                 break;
5384             }
5385         }
5386     }
5387 
5388 
5389     else if (strCommand == NetMsgType::GETBLOCKTXN)
5390     {
5391         BlockTransactionsRequest req;
5392         vRecv >> req;
5393 
5394         LOCK(cs_main);
5395 
5396         BlockMap::iterator it = mapBlockIndex.find(req.blockhash);
5397         if (it == mapBlockIndex.end() || !(it->second->nStatus & BLOCK_HAVE_DATA)) {
5398             LogPrintf("Peer %d sent us a getblocktxn for a block we don't have", pfrom->id);
5399             return true;
5400         }
5401 
5402         if (it->second->nHeight < chainActive.Height() - MAX_BLOCKTXN_DEPTH) {
5403             // If an older block is requested (should never happen in practice,
5404             // but can happen in tests) send a block response instead of a
5405             // blocktxn response. Sending a full block response instead of a
5406             // small blocktxn response is preferable in the case where a peer
5407             // might maliciously send lots of getblocktxn requests to trigger
5408             // expensive disk reads, because it will require the peer to
5409             // actually receive all the data read from disk over the network.
5410             LogPrint("net", "Peer %d sent us a getblocktxn for a block > %i deep", pfrom->id, MAX_BLOCKTXN_DEPTH);
5411             CInv inv;
5412             inv.type = State(pfrom->GetId())->fWantsCmpctWitness ? MSG_WITNESS_BLOCK : MSG_BLOCK;
5413             inv.hash = req.blockhash;
5414             pfrom->vRecvGetData.push_back(inv);
5415             ProcessGetData(pfrom, chainparams.GetConsensus());
5416             return true;
5417         }
5418 
5419         CBlock block;
5420         assert(ReadBlockFromDisk(block, it->second, chainparams.GetConsensus()));
5421 
5422         BlockTransactions resp(req);
5423         for (size_t i = 0; i < req.indexes.size(); i++) {
5424             if (req.indexes[i] >= block.vtx.size()) {
5425                 Misbehaving(pfrom->GetId(), 100);
5426                 LogPrintf("Peer %d sent us a getblocktxn with out-of-bounds tx indices", pfrom->id);
5427                 return true;
5428             }
5429             resp.txn[i] = block.vtx[req.indexes[i]];
5430         }
5431         pfrom->PushMessageWithFlag(State(pfrom->GetId())->fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::BLOCKTXN, resp);
5432     }
5433 
5434 
5435     else if (strCommand == NetMsgType::GETHEADERS)
5436     {
5437         CBlockLocator locator;
5438         uint256 hashStop;
5439         vRecv >> locator >> hashStop;
5440 
5441         LOCK(cs_main);
5442         if (IsInitialBlockDownload() && !pfrom->fWhitelisted) {
5443             LogPrint("net", "Ignoring getheaders from peer=%d because node is in initial block download\n", pfrom->id);
5444             return true;
5445         }
5446 
5447         CNodeState *nodestate = State(pfrom->GetId());
5448         CBlockIndex* pindex = NULL;
5449         if (locator.IsNull())
5450         {
5451             // If locator is null, return the hashStop block
5452             BlockMap::iterator mi = mapBlockIndex.find(hashStop);
5453             if (mi == mapBlockIndex.end())
5454                 return true;
5455             pindex = (*mi).second;
5456         }
5457         else
5458         {
5459             // Find the last block the caller has in the main chain
5460             pindex = FindForkInGlobalIndex(chainActive, locator);
5461             if (pindex)
5462                 pindex = chainActive.Next(pindex);
5463         }
5464 
5465         // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
5466         vector<CBlock> vHeaders;
5467         int nLimit = MAX_HEADERS_RESULTS;
5468         LogPrint("net", "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.ToString(), pfrom->id);
5469         for (; pindex; pindex = chainActive.Next(pindex))
5470         {
5471             vHeaders.push_back(pindex->GetBlockHeader());
5472             if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
5473                 break;
5474         }
5475         // pindex can be NULL either if we sent chainActive.Tip() OR
5476         // if our peer has chainActive.Tip() (and thus we are sending an empty
5477         // headers message). In both cases it's safe to update
5478         // pindexBestHeaderSent to be our tip.
5479         nodestate->pindexBestHeaderSent = pindex ? pindex : chainActive.Tip();
5480         pfrom->PushMessage(NetMsgType::HEADERS, vHeaders);
5481     }
5482 
5483 
5484     else if (strCommand == NetMsgType::TX)
5485     {
5486         // Stop processing the transaction early if
5487         // We are in blocks only mode and peer is either not whitelisted or whitelistrelay is off
5488         if (!fRelayTxes && (!pfrom->fWhitelisted || !GetBoolArg("-whitelistrelay", DEFAULT_WHITELISTRELAY)))
5489         {
5490             LogPrint("net", "transaction sent in violation of protocol peer=%d\n", pfrom->id);
5491             return true;
5492         }
5493 
5494         deque<COutPoint> vWorkQueue;
5495         vector<uint256> vEraseQueue;
5496         CTransaction tx;
5497         vRecv >> tx;
5498 
5499         CInv inv(MSG_TX, tx.GetHash());
5500         pfrom->AddInventoryKnown(inv);
5501 
5502         LOCK(cs_main);
5503 
5504         bool fMissingInputs = false;
5505         CValidationState state;
5506 
5507         pfrom->setAskFor.erase(inv.hash);
5508         mapAlreadyAskedFor.erase(inv.hash);
5509 
5510         if (!AlreadyHave(inv) && AcceptToMemoryPool(mempool, state, tx, true, &fMissingInputs)) {
5511             mempool.check(pcoinsTip);
5512             RelayTransaction(tx);
5513             for (unsigned int i = 0; i < tx.vout.size(); i++) {
5514                 vWorkQueue.emplace_back(inv.hash, i);
5515             }
5516 
5517             pfrom->nLastTXTime = GetTime();
5518 
5519             LogPrint("mempool", "AcceptToMemoryPool: peer=%d: accepted %s (poolsz %u txn, %u kB)\n",
5520                 pfrom->id,
5521                 tx.GetHash().ToString(),
5522                 mempool.size(), mempool.DynamicMemoryUsage() / 1000);
5523 
5524             // Recursively process any orphan transactions that depended on this one
5525             set<NodeId> setMisbehaving;
5526             while (!vWorkQueue.empty()) {
5527                 auto itByPrev = mapOrphanTransactionsByPrev.find(vWorkQueue.front());
5528                 vWorkQueue.pop_front();
5529                 if (itByPrev == mapOrphanTransactionsByPrev.end())
5530                     continue;
5531                 for (auto mi = itByPrev->second.begin();
5532                      mi != itByPrev->second.end();
5533                      ++mi)
5534                 {
5535                     const CTransaction& orphanTx = (*mi)->second.tx;
5536                     const uint256& orphanHash = orphanTx.GetHash();
5537                     NodeId fromPeer = (*mi)->second.fromPeer;
5538                     bool fMissingInputs2 = false;
5539                     // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
5540                     // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
5541                     // anyone relaying LegitTxX banned)
5542                     CValidationState stateDummy;
5543 
5544 
5545                     if (setMisbehaving.count(fromPeer))
5546                         continue;
5547                     if (AcceptToMemoryPool(mempool, stateDummy, orphanTx, true, &fMissingInputs2)) {
5548                         LogPrint("mempool", "   accepted orphan tx %s\n", orphanHash.ToString());
5549                         RelayTransaction(orphanTx);
5550                         for (unsigned int i = 0; i < orphanTx.vout.size(); i++) {
5551                             vWorkQueue.emplace_back(orphanHash, i);
5552                         }
5553                         vEraseQueue.push_back(orphanHash);
5554                     }
5555                     else if (!fMissingInputs2)
5556                     {
5557                         int nDos = 0;
5558                         if (stateDummy.IsInvalid(nDos) && nDos > 0)
5559                         {
5560                             // Punish peer that gave us an invalid orphan tx
5561                             Misbehaving(fromPeer, nDos);
5562                             setMisbehaving.insert(fromPeer);
5563                             LogPrint("mempool", "   invalid orphan tx %s\n", orphanHash.ToString());
5564                         }
5565                         // Has inputs but not accepted to mempool
5566                         // Probably non-standard or insufficient fee/priority
5567                         LogPrint("mempool", "   removed orphan tx %s\n", orphanHash.ToString());
5568                         vEraseQueue.push_back(orphanHash);
5569                         if (orphanTx.wit.IsNull() && !stateDummy.CorruptionPossible()) {
5570                             // Do not use rejection cache for witness transactions or
5571                             // witness-stripped transactions, as they can have been malleated.
5572                             // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
5573                             assert(recentRejects);
5574                             recentRejects->insert(orphanHash);
5575                         }
5576                     }
5577                     mempool.check(pcoinsTip);
5578                 }
5579             }
5580 
5581             BOOST_FOREACH(uint256 hash, vEraseQueue)
5582                 EraseOrphanTx(hash);
5583         }
5584         else if (fMissingInputs)
5585         {
5586             bool fRejectedParents = false; // It may be the case that the orphans parents have all been rejected
5587             BOOST_FOREACH(const CTxIn& txin, tx.vin) {
5588                 if (recentRejects->contains(txin.prevout.hash)) {
5589                     fRejectedParents = true;
5590                     break;
5591                 }
5592             }
5593             if (!fRejectedParents) {
5594                 uint32_t nFetchFlags = GetFetchFlags(pfrom, chainActive.Tip(), chainparams.GetConsensus());
5595                 BOOST_FOREACH(const CTxIn& txin, tx.vin) {
5596                     CInv _inv(MSG_TX | nFetchFlags, txin.prevout.hash);
5597                     pfrom->AddInventoryKnown(_inv);
5598                     if (!AlreadyHave(_inv)) pfrom->AskFor(_inv);
5599                 }
5600                 AddOrphanTx(tx, pfrom->GetId());
5601 
5602                 // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
5603                 unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));
5604                 unsigned int nEvicted = LimitOrphanTxSize(nMaxOrphanTx);
5605                 if (nEvicted > 0)
5606                     LogPrint("mempool", "mapOrphan overflow, removed %u tx\n", nEvicted);
5607             } else {
5608                 LogPrint("mempool", "not keeping orphan with rejected parents %s\n",tx.GetHash().ToString());
5609             }
5610         } else {
5611             if (tx.wit.IsNull() && !state.CorruptionPossible()) {
5612                 // Do not use rejection cache for witness transactions or
5613                 // witness-stripped transactions, as they can have been malleated.
5614                 // See https://github.com/bitcoin/bitcoin/issues/8279 for details.
5615                 assert(recentRejects);
5616                 recentRejects->insert(tx.GetHash());
5617             }
5618 
5619             if (pfrom->fWhitelisted && GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY)) {
5620                 // Always relay transactions received from whitelisted peers, even
5621                 // if they were already in the mempool or rejected from it due
5622                 // to policy, allowing the node to function as a gateway for
5623                 // nodes hidden behind it.
5624                 //
5625                 // Never relay transactions that we would assign a non-zero DoS
5626                 // score for, as we expect peers to do the same with us in that
5627                 // case.
5628                 int nDoS = 0;
5629                 if (!state.IsInvalid(nDoS) || nDoS == 0) {
5630                     LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", tx.GetHash().ToString(), pfrom->id);
5631                     RelayTransaction(tx);
5632                 } else {
5633                     LogPrintf("Not relaying invalid transaction %s from whitelisted peer=%d (%s)\n", tx.GetHash().ToString(), pfrom->id, FormatStateMessage(state));
5634                 }
5635             }
5636         }
5637         int nDoS = 0;
5638         if (state.IsInvalid(nDoS))
5639         {
5640             LogPrint("mempoolrej", "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
5641                 pfrom->id,
5642                 FormatStateMessage(state));
5643             if (state.GetRejectCode() < REJECT_INTERNAL) // Never send AcceptToMemoryPool's internal codes over P2P
5644                 pfrom->PushMessage(NetMsgType::REJECT, strCommand, (unsigned char)state.GetRejectCode(),
5645                                    state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash);
5646             if (nDoS > 0) {
5647                 Misbehaving(pfrom->GetId(), nDoS);
5648             }
5649         }
5650         FlushStateToDisk(state, FLUSH_STATE_PERIODIC);
5651     }
5652 
5653 
5654     else if (strCommand == NetMsgType::CMPCTBLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
5655     {
5656         CBlockHeaderAndShortTxIDs cmpctblock;
5657         vRecv >> cmpctblock;
5658 
5659         // Keep a CBlock for "optimistic" compactblock reconstructions (see
5660         // below)
5661         CBlock block;
5662         bool fBlockReconstructed = false;
5663 
5664         LOCK(cs_main);
5665 
5666         if (mapBlockIndex.find(cmpctblock.header.hashPrevBlock) == mapBlockIndex.end()) {
5667             // Doesn't connect (or is genesis), instead of DoSing in AcceptBlockHeader, request deeper headers
5668             if (!IsInitialBlockDownload())
5669                 pfrom->PushMessage(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256());
5670             return true;
5671         }
5672 
5673         CBlockIndex *pindex = NULL;
5674         CValidationState state;
5675         if (!AcceptBlockHeader(cmpctblock.header, state, chainparams, &pindex)) {
5676             int nDoS;
5677             if (state.IsInvalid(nDoS)) {
5678                 if (nDoS > 0)
5679                     Misbehaving(pfrom->GetId(), nDoS);
5680                 LogPrintf("Peer %d sent us invalid header via cmpctblock\n", pfrom->id);
5681                 return true;
5682             }
5683         }
5684 
5685         // If AcceptBlockHeader returned true, it set pindex
5686         assert(pindex);
5687         UpdateBlockAvailability(pfrom->GetId(), pindex->GetBlockHash());
5688 
5689         std::map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator blockInFlightIt = mapBlocksInFlight.find(pindex->GetBlockHash());
5690         bool fAlreadyInFlight = blockInFlightIt != mapBlocksInFlight.end();
5691 
5692         if (pindex->nStatus & BLOCK_HAVE_DATA) // Nothing to do here
5693             return true;
5694 
5695         if (pindex->nChainWork <= chainActive.Tip()->nChainWork || // We know something better
5696                 pindex->nTx != 0) { // We had this block at some point, but pruned it
5697             if (fAlreadyInFlight) {
5698                 // We requested this block for some reason, but our mempool will probably be useless
5699                 // so we just grab the block via normal getdata
5700                 std::vector<CInv> vInv(1);
5701                 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom, pindex->pprev, chainparams.GetConsensus()), cmpctblock.header.GetHash());
5702                 pfrom->PushMessage(NetMsgType::GETDATA, vInv);
5703             }
5704             return true;
5705         }
5706 
5707         // If we're not close to tip yet, give up and let parallel block fetch work its magic
5708         if (!fAlreadyInFlight && !CanDirectFetch(chainparams.GetConsensus()))
5709             return true;
5710 
5711         CNodeState *nodestate = State(pfrom->GetId());
5712 
5713         if (IsWitnessEnabled(pindex->pprev, chainparams.GetConsensus()) && !nodestate->fSupportsDesiredCmpctVersion) {
5714             // Don't bother trying to process compact blocks from v1 peers
5715             // after segwit activates.
5716             return true;
5717         }
5718 
5719         // We want to be a bit conservative just to be extra careful about DoS
5720         // possibilities in compact block processing...
5721         if (pindex->nHeight <= chainActive.Height() + 2) {
5722             if ((!fAlreadyInFlight && nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) ||
5723                  (fAlreadyInFlight && blockInFlightIt->second.first == pfrom->GetId())) {
5724                 list<QueuedBlock>::iterator *queuedBlockIt = NULL;
5725                 if (!MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), chainparams.GetConsensus(), pindex, &queuedBlockIt)) {
5726                     if (!(*queuedBlockIt)->partialBlock)
5727                         (*queuedBlockIt)->partialBlock.reset(new PartiallyDownloadedBlock(&mempool));
5728                     else {
5729                         // The block was already in flight using compact blocks from the same peer
5730                         LogPrint("net", "Peer sent us compact block we were already syncing!\n");
5731                         return true;
5732                     }
5733                 }
5734 
5735                 PartiallyDownloadedBlock& partialBlock = *(*queuedBlockIt)->partialBlock;
5736                 ReadStatus status = partialBlock.InitData(cmpctblock);
5737                 if (status == READ_STATUS_INVALID) {
5738                     MarkBlockAsReceived(pindex->GetBlockHash()); // Reset in-flight state in case of whitelist
5739                     Misbehaving(pfrom->GetId(), 100);
5740                     LogPrintf("Peer %d sent us invalid compact block\n", pfrom->id);
5741                     return true;
5742                 } else if (status == READ_STATUS_FAILED) {
5743                     // Duplicate txindexes, the block is now in-flight, so just request it
5744                     std::vector<CInv> vInv(1);
5745                     vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom, pindex->pprev, chainparams.GetConsensus()), cmpctblock.header.GetHash());
5746                     pfrom->PushMessage(NetMsgType::GETDATA, vInv);
5747                     return true;
5748                 }
5749 
5750                 if (!fAlreadyInFlight && mapBlocksInFlight.size() == 1 && pindex->pprev->IsValid(BLOCK_VALID_CHAIN)) {
5751                     // We seem to be rather well-synced, so it appears pfrom was the first to provide us
5752                     // with this block! Let's get them to announce using compact blocks in the future.
5753                     MaybeSetPeerAsAnnouncingHeaderAndIDs(nodestate, pfrom);
5754                 }
5755 
5756                 BlockTransactionsRequest req;
5757                 for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
5758                     if (!partialBlock.IsTxAvailable(i))
5759                         req.indexes.push_back(i);
5760                 }
5761                 if (req.indexes.empty()) {
5762                     // Dirty hack to jump to BLOCKTXN code (TODO: move message handling into their own functions)
5763                     BlockTransactions txn;
5764                     txn.blockhash = cmpctblock.header.GetHash();
5765                     CDataStream blockTxnMsg(SER_NETWORK, PROTOCOL_VERSION);
5766                     blockTxnMsg << txn;
5767                     return ProcessMessage(pfrom, NetMsgType::BLOCKTXN, blockTxnMsg, nTimeReceived, chainparams);
5768                 } else {
5769                     req.blockhash = pindex->GetBlockHash();
5770                     pfrom->PushMessage(NetMsgType::GETBLOCKTXN, req);
5771                 }
5772             } else {
5773                 // This block is either already in flight from a different
5774                 // peer, or this peer has too many blocks outstanding to
5775                 // download from.
5776                 // Optimistically try to reconstruct anyway since we might be
5777                 // able to without any round trips.
5778                 PartiallyDownloadedBlock tempBlock(&mempool);
5779                 ReadStatus status = tempBlock.InitData(cmpctblock);
5780                 if (status != READ_STATUS_OK) {
5781                     // TODO: don't ignore failures
5782                     return true;
5783                 }
5784                 std::vector<CTransaction> dummy;
5785                 status = tempBlock.FillBlock(block, dummy);
5786                 if (status == READ_STATUS_OK) {
5787                     fBlockReconstructed = true;
5788                 }
5789             }
5790         } else {
5791             if (fAlreadyInFlight) {
5792                 // We requested this block, but its far into the future, so our
5793                 // mempool will probably be useless - request the block normally
5794                 std::vector<CInv> vInv(1);
5795                 vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(pfrom, pindex->pprev, chainparams.GetConsensus()), cmpctblock.header.GetHash());
5796                 pfrom->PushMessage(NetMsgType::GETDATA, vInv);
5797                 return true;
5798             } else {
5799                 // If this was an announce-cmpctblock, we want the same treatment as a header message
5800                 // Dirty hack to process as if it were just a headers message (TODO: move message handling into their own functions)
5801                 std::vector<CBlock> headers;
5802                 headers.push_back(cmpctblock.header);
5803                 CDataStream vHeadersMsg(SER_NETWORK, PROTOCOL_VERSION);
5804                 vHeadersMsg << headers;
5805                 return ProcessMessage(pfrom, NetMsgType::HEADERS, vHeadersMsg, nTimeReceived, chainparams);
5806             }
5807         }
5808 
5809         if (fBlockReconstructed) {
5810             // If we got here, we were able to optimistically reconstruct a
5811             // block that is in flight from some other peer.  However, this
5812             // cmpctblock may be invalid.  In particular, while we've checked
5813             // that the block merkle root commits to the transaction ids, we
5814             // haven't yet checked that tx witnesses are properly committed to
5815             // in the coinbase witness commitment.
5816             //
5817             // ProcessNewBlock will call MarkBlockAsReceived(), which will
5818             // clear any in-flight compact block state that might be present
5819             // from some other peer.  We don't want a malleated compact block
5820             // request to interfere with block relay, so we don't want to call
5821             // ProcessNewBlock until we've already checked that the witness
5822             // commitment is correct.
5823             {
5824                 LOCK(cs_main);
5825                 CValidationState dummy;
5826                 if (!ContextualCheckBlock(block, dummy, pindex->pprev)) {
5827                     // TODO: could send reject message to peer?
5828                     return true;
5829                 }
5830             }
5831             CValidationState state;
5832             ProcessNewBlock(state, chainparams, pfrom, &block, true, NULL, false);
5833             // TODO: could send reject message if block is invalid?
5834         }
5835 
5836         CheckBlockIndex(chainparams.GetConsensus());
5837     }
5838 
5839     else if (strCommand == NetMsgType::BLOCKTXN && !fImporting && !fReindex) // Ignore blocks received while importing
5840     {
5841         BlockTransactions resp;
5842         vRecv >> resp;
5843 
5844         LOCK(cs_main);
5845 
5846         map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator it = mapBlocksInFlight.find(resp.blockhash);
5847         if (it == mapBlocksInFlight.end() || !it->second.second->partialBlock ||
5848                 it->second.first != pfrom->GetId()) {
5849             LogPrint("net", "Peer %d sent us block transactions for block we weren't expecting\n", pfrom->id);
5850             return true;
5851         }
5852 
5853         PartiallyDownloadedBlock& partialBlock = *it->second.second->partialBlock;
5854         CBlock block;
5855         ReadStatus status = partialBlock.FillBlock(block, resp.txn);
5856         if (status == READ_STATUS_INVALID) {
5857             MarkBlockAsReceived(resp.blockhash); // Reset in-flight state in case of whitelist
5858             Misbehaving(pfrom->GetId(), 100);
5859             LogPrintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom->id);
5860             return true;
5861         } else if (status == READ_STATUS_FAILED) {
5862             // Might have collided, fall back to getdata now :(
5863             std::vector<CInv> invs;
5864             invs.push_back(CInv(MSG_BLOCK | GetFetchFlags(pfrom, chainActive.Tip(), chainparams.GetConsensus()), resp.blockhash));
5865             pfrom->PushMessage(NetMsgType::GETDATA, invs);
5866         } else {
5867             // Block is either okay, or possibly we received
5868             // READ_STATUS_CHECKBLOCK_FAILED.
5869             // Note that CheckBlock can only fail for one of a few reasons:
5870             // 1. bad-proof-of-work (impossible here, because we've already
5871             //    accepted the header)
5872             // 2. merkleroot doesn't match the transactions given (already
5873             //    caught in FillBlock with READ_STATUS_FAILED, so
5874             //    impossible here)
5875             // 3. the block is otherwise invalid (eg invalid coinbase,
5876             //    block is too big, too many legacy sigops, etc).
5877             // So if CheckBlock failed, #3 is the only possibility.
5878             // Under BIP 152, we don't DoS-ban unless proof of work is
5879             // invalid (we don't require all the stateless checks to have
5880             // been run).  This is handled below, so just treat this as
5881             // though the block was successfully read, and rely on the
5882             // handling in ProcessNewBlock to ensure the block index is
5883             // updated, reject messages go out, etc.
5884             CValidationState state;
5885             // BIP 152 permits peers to relay compact blocks after validating
5886             // the header only; we should not punish peers if the block turns
5887             // out to be invalid.
5888             ProcessNewBlock(state, chainparams, pfrom, &block, false, NULL, false);
5889             int nDoS;
5890             if (state.IsInvalid(nDoS)) {
5891                 assert (state.GetRejectCode() < REJECT_INTERNAL); // Blocks are never rejected with internal reject codes
5892                 pfrom->PushMessage(NetMsgType::REJECT, strCommand, (unsigned char)state.GetRejectCode(),
5893                                    state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), block.GetHash());
5894             }
5895         }
5896     }
5897 
5898 
5899     else if (strCommand == NetMsgType::HEADERS && !fImporting && !fReindex) // Ignore headers received while importing
5900     {
5901         std::vector<CBlockHeader> headers;
5902 
5903         // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
5904         unsigned int nCount = ReadCompactSize(vRecv);
5905         if (nCount > MAX_HEADERS_RESULTS) {
5906             LOCK(cs_main);
5907             Misbehaving(pfrom->GetId(), 20);
5908             return error("headers message size = %u", nCount);
5909         }
5910         headers.resize(nCount);
5911         for (unsigned int n = 0; n < nCount; n++) {
5912             vRecv >> headers[n];
5913             ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
5914         }
5915 
5916         {
5917         LOCK(cs_main);
5918 
5919         if (nCount == 0) {
5920             // Nothing interesting. Stop asking this peers for more headers.
5921             return true;
5922         }
5923 
5924         CNodeState *nodestate = State(pfrom->GetId());
5925 
5926         // If this looks like it could be a block announcement (nCount <
5927         // MAX_BLOCKS_TO_ANNOUNCE), use special logic for handling headers that
5928         // don't connect:
5929         // - Send a getheaders message in response to try to connect the chain.
5930         // - The peer can send up to MAX_UNCONNECTING_HEADERS in a row that
5931         //   don't connect before giving DoS points
5932         // - Once a headers message is received that is valid and does connect,
5933         //   nUnconnectingHeaders gets reset back to 0.
5934         if (mapBlockIndex.find(headers[0].hashPrevBlock) == mapBlockIndex.end() && nCount < MAX_BLOCKS_TO_ANNOUNCE) {
5935             nodestate->nUnconnectingHeaders++;
5936             pfrom->PushMessage(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexBestHeader), uint256());
5937             LogPrint("net", "received header %s: missing prev block %s, sending getheaders (%d) to end (peer=%d, nUnconnectingHeaders=%d)\n",
5938                     headers[0].GetHash().ToString(),
5939                     headers[0].hashPrevBlock.ToString(),
5940                     pindexBestHeader->nHeight,
5941                     pfrom->id, nodestate->nUnconnectingHeaders);
5942             // Set hashLastUnknownBlock for this peer, so that if we
5943             // eventually get the headers - even from a different peer -
5944             // we can use this peer to download.
5945             UpdateBlockAvailability(pfrom->GetId(), headers.back().GetHash());
5946 
5947             if (nodestate->nUnconnectingHeaders % MAX_UNCONNECTING_HEADERS == 0) {
5948                 Misbehaving(pfrom->GetId(), 20);
5949             }
5950             return true;
5951         }
5952 
5953         CBlockIndex *pindexLast = NULL;
5954         BOOST_FOREACH(const CBlockHeader& header, headers) {
5955             CValidationState state;
5956             if (pindexLast != NULL && header.hashPrevBlock != pindexLast->GetBlockHash()) {
5957                 Misbehaving(pfrom->GetId(), 20);
5958                 return error("non-continuous headers sequence");
5959             }
5960             if (!AcceptBlockHeader(header, state, chainparams, &pindexLast)) {
5961                 int nDoS;
5962                 if (state.IsInvalid(nDoS)) {
5963                     if (nDoS > 0)
5964                         Misbehaving(pfrom->GetId(), nDoS);
5965                     return error("invalid header received");
5966                 }
5967             }
5968         }
5969 
5970         if (nodestate->nUnconnectingHeaders > 0) {
5971             LogPrint("net", "peer=%d: resetting nUnconnectingHeaders (%d -> 0)\n", pfrom->id, nodestate->nUnconnectingHeaders);
5972         }
5973         nodestate->nUnconnectingHeaders = 0;
5974 
5975         assert(pindexLast);
5976         UpdateBlockAvailability(pfrom->GetId(), pindexLast->GetBlockHash());
5977 
5978         if (nCount == MAX_HEADERS_RESULTS) {
5979             // Headers message had its maximum size; the peer may have more headers.
5980             // TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
5981             // from there instead.
5982             LogPrint("net", "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom->id, pfrom->nStartingHeight);
5983             pfrom->PushMessage(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexLast), uint256());
5984         }
5985 
5986         bool fCanDirectFetch = CanDirectFetch(chainparams.GetConsensus());
5987         // If this set of headers is valid and ends in a block with at least as
5988         // much work as our tip, download as much as possible.
5989         if (fCanDirectFetch && pindexLast->IsValid(BLOCK_VALID_TREE) && chainActive.Tip()->nChainWork <= pindexLast->nChainWork) {
5990             vector<CBlockIndex *> vToFetch;
5991             CBlockIndex *pindexWalk = pindexLast;
5992             // Calculate all the blocks we'd need to switch to pindexLast, up to a limit.
5993             while (pindexWalk && !chainActive.Contains(pindexWalk) && vToFetch.size() <= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
5994                 if (!(pindexWalk->nStatus & BLOCK_HAVE_DATA) &&
5995                         !mapBlocksInFlight.count(pindexWalk->GetBlockHash()) &&
5996                         (!IsWitnessEnabled(pindexWalk->pprev, chainparams.GetConsensus()) || State(pfrom->GetId())->fHaveWitness)) {
5997                     // We don't have this block, and it's not yet in flight.
5998                     vToFetch.push_back(pindexWalk);
5999                 }
6000                 pindexWalk = pindexWalk->pprev;
6001             }
6002             // If pindexWalk still isn't on our main chain, we're looking at a
6003             // very large reorg at a time we think we're close to caught up to
6004             // the main chain -- this shouldn't really happen.  Bail out on the
6005             // direct fetch and rely on parallel download instead.
6006             if (!chainActive.Contains(pindexWalk)) {
6007                 LogPrint("net", "Large reorg, won't direct fetch to %s (%d)\n",
6008                         pindexLast->GetBlockHash().ToString(),
6009                         pindexLast->nHeight);
6010             } else {
6011                 vector<CInv> vGetData;
6012                 // Download as much as possible, from earliest to latest.
6013                 BOOST_REVERSE_FOREACH(CBlockIndex *pindex, vToFetch) {
6014                     if (nodestate->nBlocksInFlight >= MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
6015                         // Can't download any more from this peer
6016                         break;
6017                     }
6018                     uint32_t nFetchFlags = GetFetchFlags(pfrom, pindex->pprev, chainparams.GetConsensus());
6019                     vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
6020                     MarkBlockAsInFlight(pfrom->GetId(), pindex->GetBlockHash(), chainparams.GetConsensus(), pindex);
6021                     LogPrint("net", "Requesting block %s from  peer=%d\n",
6022                             pindex->GetBlockHash().ToString(), pfrom->id);
6023                 }
6024                 if (vGetData.size() > 1) {
6025                     LogPrint("net", "Downloading blocks toward %s (%d) via headers direct fetch\n",
6026                             pindexLast->GetBlockHash().ToString(), pindexLast->nHeight);
6027                 }
6028                 if (vGetData.size() > 0) {
6029                     if (nodestate->fSupportsDesiredCmpctVersion && vGetData.size() == 1 && mapBlocksInFlight.size() == 1 && pindexLast->pprev->IsValid(BLOCK_VALID_CHAIN)) {
6030                         // We seem to be rather well-synced, so it appears pfrom was the first to provide us
6031                         // with this block! Let's get them to announce using compact blocks in the future.
6032                         MaybeSetPeerAsAnnouncingHeaderAndIDs(nodestate, pfrom);
6033                         // In any case, we want to download using a compact block, not a regular one
6034                         vGetData[0] = CInv(MSG_CMPCT_BLOCK, vGetData[0].hash);
6035                     }
6036                     pfrom->PushMessage(NetMsgType::GETDATA, vGetData);
6037                 }
6038             }
6039         }
6040 
6041         CheckBlockIndex(chainparams.GetConsensus());
6042         }
6043 
6044         NotifyHeaderTip();
6045     }
6046 
6047     else if (strCommand == NetMsgType::BLOCK && !fImporting && !fReindex) // Ignore blocks received while importing
6048     {
6049         CBlock block;
6050         vRecv >> block;
6051 
6052         LogPrint("net", "received block %s peer=%d\n", block.GetHash().ToString(), pfrom->id);
6053 
6054         CValidationState state;
6055         // Process all blocks from whitelisted peers, even if not requested,
6056         // unless we're still syncing with the network.
6057         // Such an unrequested block may still be processed, subject to the
6058         // conditions in AcceptBlock().
6059         bool forceProcessing = pfrom->fWhitelisted && !IsInitialBlockDownload();
6060         ProcessNewBlock(state, chainparams, pfrom, &block, forceProcessing, NULL, true);
6061         int nDoS;
6062         if (state.IsInvalid(nDoS)) {
6063             assert (state.GetRejectCode() < REJECT_INTERNAL); // Blocks are never rejected with internal reject codes
6064             pfrom->PushMessage(NetMsgType::REJECT, strCommand, (unsigned char)state.GetRejectCode(),
6065                                state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), block.GetHash());
6066             if (nDoS > 0) {
6067                 LOCK(cs_main);
6068                 Misbehaving(pfrom->GetId(), nDoS);
6069             }
6070         }
6071 
6072     }
6073 
6074 
6075     else if (strCommand == NetMsgType::GETADDR)
6076     {
6077         // This asymmetric behavior for inbound and outbound connections was introduced
6078         // to prevent a fingerprinting attack: an attacker can send specific fake addresses
6079         // to users' AddrMan and later request them by sending getaddr messages.
6080         // Making nodes which are behind NAT and can only make outgoing connections ignore
6081         // the getaddr message mitigates the attack.
6082         if (!pfrom->fInbound) {
6083             LogPrint("net", "Ignoring \"getaddr\" from outbound connection. peer=%d\n", pfrom->id);
6084             return true;
6085         }
6086 
6087         // Only send one GetAddr response per connection to reduce resource waste
6088         //  and discourage addr stamping of INV announcements.
6089         if (pfrom->fSentAddr) {
6090             LogPrint("net", "Ignoring repeated \"getaddr\". peer=%d\n", pfrom->id);
6091             return true;
6092         }
6093         pfrom->fSentAddr = true;
6094 
6095         pfrom->vAddrToSend.clear();
6096         vector<CAddress> vAddr = addrman.GetAddr();
6097         BOOST_FOREACH(const CAddress &addr, vAddr)
6098             pfrom->PushAddress(addr);
6099     }
6100 
6101 
6102     else if (strCommand == NetMsgType::MEMPOOL)
6103     {
6104         if (!(nLocalServices & NODE_BLOOM) && !pfrom->fWhitelisted)
6105         {
6106             LogPrint("net", "mempool request with bloom filters disabled, disconnect peer=%d\n", pfrom->GetId());
6107             pfrom->fDisconnect = true;
6108             return true;
6109         }
6110 
6111         if (CNode::OutboundTargetReached(false) && !pfrom->fWhitelisted)
6112         {
6113             LogPrint("net", "mempool request with bandwidth limit reached, disconnect peer=%d\n", pfrom->GetId());
6114             pfrom->fDisconnect = true;
6115             return true;
6116         }
6117 
6118         LOCK(pfrom->cs_inventory);
6119         pfrom->fSendMempool = true;
6120     }
6121 
6122 
6123     else if (strCommand == NetMsgType::PING)
6124     {
6125         if (pfrom->nVersion > BIP0031_VERSION)
6126         {
6127             uint64_t nonce = 0;
6128             vRecv >> nonce;
6129             // Echo the message back with the nonce. This allows for two useful features:
6130             //
6131             // 1) A remote node can quickly check if the connection is operational
6132             // 2) Remote nodes can measure the latency of the network thread. If this node
6133             //    is overloaded it won't respond to pings quickly and the remote node can
6134             //    avoid sending us more work, like chain download requests.
6135             //
6136             // The nonce stops the remote getting confused between different pings: without
6137             // it, if the remote node sends a ping once per second and this node takes 5
6138             // seconds to respond to each, the 5th ping the remote sends would appear to
6139             // return very quickly.
6140             pfrom->PushMessage(NetMsgType::PONG, nonce);
6141         }
6142     }
6143 
6144 
6145     else if (strCommand == NetMsgType::PONG)
6146     {
6147         int64_t pingUsecEnd = nTimeReceived;
6148         uint64_t nonce = 0;
6149         size_t nAvail = vRecv.in_avail();
6150         bool bPingFinished = false;
6151         std::string sProblem;
6152 
6153         if (nAvail >= sizeof(nonce)) {
6154             vRecv >> nonce;
6155 
6156             // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
6157             if (pfrom->nPingNonceSent != 0) {
6158                 if (nonce == pfrom->nPingNonceSent) {
6159                     // Matching pong received, this ping is no longer outstanding
6160                     bPingFinished = true;
6161                     int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart;
6162                     if (pingUsecTime > 0) {
6163                         // Successful ping time measurement, replace previous
6164                         pfrom->nPingUsecTime = pingUsecTime;
6165                         pfrom->nMinPingUsecTime = std::min(pfrom->nMinPingUsecTime, pingUsecTime);
6166                     } else {
6167                         // This should never happen
6168                         sProblem = "Timing mishap";
6169                     }
6170                 } else {
6171                     // Nonce mismatches are normal when pings are overlapping
6172                     sProblem = "Nonce mismatch";
6173                     if (nonce == 0) {
6174                         // This is most likely a bug in another implementation somewhere; cancel this ping
6175                         bPingFinished = true;
6176                         sProblem = "Nonce zero";
6177                     }
6178                 }
6179             } else {
6180                 sProblem = "Unsolicited pong without ping";
6181             }
6182         } else {
6183             // This is most likely a bug in another implementation somewhere; cancel this ping
6184             bPingFinished = true;
6185             sProblem = "Short payload";
6186         }
6187 
6188         if (!(sProblem.empty())) {
6189             LogPrint("net", "pong peer=%d: %s, %x expected, %x received, %u bytes\n",
6190                 pfrom->id,
6191                 sProblem,
6192                 pfrom->nPingNonceSent,
6193                 nonce,
6194                 nAvail);
6195         }
6196         if (bPingFinished) {
6197             pfrom->nPingNonceSent = 0;
6198         }
6199     }
6200 
6201 
6202     else if (strCommand == NetMsgType::FILTERLOAD)
6203     {
6204         CBloomFilter filter;
6205         vRecv >> filter;
6206 
6207         if (!filter.IsWithinSizeConstraints())
6208         {
6209             // There is no excuse for sending a too-large filter
6210             LOCK(cs_main);
6211             Misbehaving(pfrom->GetId(), 100);
6212         }
6213         else
6214         {
6215             LOCK(pfrom->cs_filter);
6216             delete pfrom->pfilter;
6217             pfrom->pfilter = new CBloomFilter(filter);
6218             pfrom->pfilter->UpdateEmptyFull();
6219             pfrom->fRelayTxes = true;
6220         }
6221     }
6222 
6223 
6224     else if (strCommand == NetMsgType::FILTERADD)
6225     {
6226         vector<unsigned char> vData;
6227         vRecv >> vData;
6228 
6229         // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
6230         // and thus, the maximum size any matched object can have) in a filteradd message
6231         bool bad = false;
6232         if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE) {
6233             bad = true;
6234         } else {
6235             LOCK(pfrom->cs_filter);
6236             if (pfrom->pfilter) {
6237                 pfrom->pfilter->insert(vData);
6238             } else {
6239                 bad = true;
6240             }
6241         }
6242         if (bad) {
6243             LOCK(cs_main);
6244             Misbehaving(pfrom->GetId(), 100);
6245         }
6246     }
6247 
6248 
6249     else if (strCommand == NetMsgType::FILTERCLEAR)
6250     {
6251         LOCK(pfrom->cs_filter);
6252         delete pfrom->pfilter;
6253         pfrom->pfilter = new CBloomFilter();
6254         pfrom->fRelayTxes = true;
6255     }
6256 
6257 
6258     else if (strCommand == NetMsgType::REJECT)
6259     {
6260         if (fDebug) {
6261             try {
6262                 string strMsg; unsigned char ccode; string strReason;
6263                 vRecv >> LIMITED_STRING(strMsg, CMessageHeader::COMMAND_SIZE) >> ccode >> LIMITED_STRING(strReason, MAX_REJECT_MESSAGE_LENGTH);
6264 
6265                 ostringstream ss;
6266                 ss << strMsg << " code " << itostr(ccode) << ": " << strReason;
6267 
6268                 if (strMsg == NetMsgType::BLOCK || strMsg == NetMsgType::TX)
6269                 {
6270                     uint256 hash;
6271                     vRecv >> hash;
6272                     ss << ": hash " << hash.ToString();
6273                 }
6274                 LogPrint("net", "Reject %s\n", SanitizeString(ss.str()));
6275             } catch (const std::ios_base::failure&) {
6276                 // Avoid feedback loops by preventing reject messages from triggering a new reject message.
6277                 LogPrint("net", "Unparseable reject message received\n");
6278             }
6279         }
6280     }
6281 
6282     else if (strCommand == NetMsgType::FEEFILTER) {
6283         CAmount newFeeFilter = 0;
6284         vRecv >> newFeeFilter;
6285         if (MoneyRange(newFeeFilter)) {
6286             {
6287                 LOCK(pfrom->cs_feeFilter);
6288                 pfrom->minFeeFilter = newFeeFilter;
6289             }
6290             LogPrint("net", "received: feefilter of %s from peer=%d\n", CFeeRate(newFeeFilter).ToString(), pfrom->id);
6291         }
6292     }
6293 
6294     else if (strCommand == NetMsgType::NOTFOUND) {
6295         // We do not care about the NOTFOUND message, but logging an Unknown Command
6296         // message would be undesirable as we transmit it ourselves.
6297     }
6298 
6299     else {
6300         // Ignore unknown commands for extensibility
6301         LogPrint("net", "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->id);
6302     }
6303 
6304 
6305 
6306     return true;
6307 }
6308 
6309 // requires LOCK(cs_vRecvMsg)
ProcessMessages(CNode * pfrom)6310 bool ProcessMessages(CNode* pfrom)
6311 {
6312     const CChainParams& chainparams = Params();
6313     //if (fDebug)
6314     //    LogPrintf("%s(%u messages)\n", __func__, pfrom->vRecvMsg.size());
6315 
6316     //
6317     // Message format
6318     //  (4) message start
6319     //  (12) command
6320     //  (4) size
6321     //  (4) checksum
6322     //  (x) data
6323     //
6324     bool fOk = true;
6325 
6326     if (!pfrom->vRecvGetData.empty())
6327         ProcessGetData(pfrom, chainparams.GetConsensus());
6328 
6329     // this maintains the order of responses
6330     if (!pfrom->vRecvGetData.empty()) return fOk;
6331 
6332     std::deque<CNetMessage>::iterator it = pfrom->vRecvMsg.begin();
6333     while (!pfrom->fDisconnect && it != pfrom->vRecvMsg.end()) {
6334         // Don't bother if send buffer is too full to respond anyway
6335         if (pfrom->nSendSize >= SendBufferSize())
6336             break;
6337 
6338         // get next message
6339         CNetMessage& msg = *it;
6340 
6341         //if (fDebug)
6342         //    LogPrintf("%s(message %u msgsz, %u bytes, complete:%s)\n", __func__,
6343         //            msg.hdr.nMessageSize, msg.vRecv.size(),
6344         //            msg.complete() ? "Y" : "N");
6345 
6346         // end, if an incomplete message is found
6347         if (!msg.complete())
6348             break;
6349 
6350         // at this point, any failure means we can delete the current message
6351         it++;
6352 
6353         // Scan for message start
6354         if (memcmp(msg.hdr.pchMessageStart, chainparams.MessageStart(), MESSAGE_START_SIZE) != 0) {
6355             LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->id);
6356             fOk = false;
6357             break;
6358         }
6359 
6360         // Read header
6361         CMessageHeader& hdr = msg.hdr;
6362         if (!hdr.IsValid(chainparams.MessageStart()))
6363         {
6364             LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->id);
6365             continue;
6366         }
6367         string strCommand = hdr.GetCommand();
6368 
6369         // Message size
6370         unsigned int nMessageSize = hdr.nMessageSize;
6371 
6372         // Checksum
6373         CDataStream& vRecv = msg.vRecv;
6374         uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize);
6375         unsigned int nChecksum = ReadLE32((unsigned char*)&hash);
6376         if (nChecksum != hdr.nChecksum)
6377         {
6378             LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR nChecksum=%08x hdr.nChecksum=%08x\n", __func__,
6379                SanitizeString(strCommand), nMessageSize, nChecksum, hdr.nChecksum);
6380             continue;
6381         }
6382 
6383         // Process message
6384         bool fRet = false;
6385         try
6386         {
6387             fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime, chainparams);
6388             boost::this_thread::interruption_point();
6389         }
6390         catch (const std::ios_base::failure& e)
6391         {
6392             pfrom->PushMessage(NetMsgType::REJECT, strCommand, REJECT_MALFORMED, string("error parsing message"));
6393             if (strstr(e.what(), "end of data"))
6394             {
6395                 // Allow exceptions from under-length message on vRecv
6396                 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());
6397             }
6398             else if (strstr(e.what(), "size too large"))
6399             {
6400                 // Allow exceptions from over-long size
6401                 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
6402             }
6403             else if (strstr(e.what(), "non-canonical ReadCompactSize()"))
6404             {
6405                 // Allow exceptions from non-canonical encoding
6406                 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
6407             }
6408             else
6409             {
6410                 PrintExceptionContinue(&e, "ProcessMessages()");
6411             }
6412         }
6413         catch (const boost::thread_interrupted&) {
6414             throw;
6415         }
6416         catch (const std::exception& e) {
6417             PrintExceptionContinue(&e, "ProcessMessages()");
6418         } catch (...) {
6419             PrintExceptionContinue(NULL, "ProcessMessages()");
6420         }
6421 
6422         if (!fRet)
6423             LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->id);
6424 
6425         break;
6426     }
6427 
6428     // In case the connection got shut down, its receive buffer was wiped
6429     if (!pfrom->fDisconnect)
6430         pfrom->vRecvMsg.erase(pfrom->vRecvMsg.begin(), it);
6431 
6432     return fOk;
6433 }
6434 
6435 class CompareInvMempoolOrder
6436 {
6437     CTxMemPool *mp;
6438 public:
CompareInvMempoolOrder(CTxMemPool * mempool)6439     CompareInvMempoolOrder(CTxMemPool *mempool)
6440     {
6441         mp = mempool;
6442     }
6443 
operator ()(std::set<uint256>::iterator a,std::set<uint256>::iterator b)6444     bool operator()(std::set<uint256>::iterator a, std::set<uint256>::iterator b)
6445     {
6446         /* As std::make_heap produces a max-heap, we want the entries with the
6447          * fewest ancestors/highest fee to sort later. */
6448         return mp->CompareDepthAndScore(*b, *a);
6449     }
6450 };
6451 
SendMessages(CNode * pto)6452 bool SendMessages(CNode* pto)
6453 {
6454     const Consensus::Params& consensusParams = Params().GetConsensus();
6455     {
6456         // Don't send anything until we get its version message
6457         if (pto->nVersion == 0)
6458             return true;
6459 
6460         //
6461         // Message: ping
6462         //
6463         bool pingSend = false;
6464         if (pto->fPingQueued) {
6465             // RPC ping request by user
6466             pingSend = true;
6467         }
6468         if (pto->nPingNonceSent == 0 && pto->nPingUsecStart + PING_INTERVAL * 1000000 < GetTimeMicros()) {
6469             // Ping automatically sent as a latency probe & keepalive.
6470             pingSend = true;
6471         }
6472         if (pingSend && !pto->fDisconnect) {
6473             uint64_t nonce = 0;
6474             while (nonce == 0) {
6475                 GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
6476             }
6477             pto->fPingQueued = false;
6478             pto->nPingUsecStart = GetTimeMicros();
6479             if (pto->nVersion > BIP0031_VERSION) {
6480                 pto->nPingNonceSent = nonce;
6481                 pto->PushMessage(NetMsgType::PING, nonce);
6482             } else {
6483                 // Peer is too old to support ping command with nonce, pong will never arrive.
6484                 pto->nPingNonceSent = 0;
6485                 pto->PushMessage(NetMsgType::PING);
6486             }
6487         }
6488 
6489         TRY_LOCK(cs_main, lockMain); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
6490         if (!lockMain)
6491             return true;
6492 
6493         // Address refresh broadcast
6494         int64_t nNow = GetTimeMicros();
6495         if (!IsInitialBlockDownload() && pto->nNextLocalAddrSend < nNow) {
6496             AdvertiseLocal(pto);
6497             pto->nNextLocalAddrSend = PoissonNextSend(nNow, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
6498         }
6499 
6500         //
6501         // Message: addr
6502         //
6503         if (pto->nNextAddrSend < nNow) {
6504             pto->nNextAddrSend = PoissonNextSend(nNow, AVG_ADDRESS_BROADCAST_INTERVAL);
6505             vector<CAddress> vAddr;
6506             vAddr.reserve(pto->vAddrToSend.size());
6507             BOOST_FOREACH(const CAddress& addr, pto->vAddrToSend)
6508             {
6509                 if (!pto->addrKnown.contains(addr.GetKey()))
6510                 {
6511                     pto->addrKnown.insert(addr.GetKey());
6512                     vAddr.push_back(addr);
6513                     // receiver rejects addr messages larger than 1000
6514                     if (vAddr.size() >= 1000)
6515                     {
6516                         pto->PushMessage(NetMsgType::ADDR, vAddr);
6517                         vAddr.clear();
6518                     }
6519                 }
6520             }
6521             pto->vAddrToSend.clear();
6522             if (!vAddr.empty())
6523                 pto->PushMessage(NetMsgType::ADDR, vAddr);
6524             // we only send the big addr message once
6525             if (pto->vAddrToSend.capacity() > 40)
6526                 pto->vAddrToSend.shrink_to_fit();
6527         }
6528 
6529         CNodeState &state = *State(pto->GetId());
6530         if (state.fShouldBan) {
6531             if (pto->fWhitelisted)
6532                 LogPrintf("Warning: not punishing whitelisted peer %s!\n", pto->addr.ToString());
6533             else {
6534                 pto->fDisconnect = true;
6535                 if (pto->addr.IsLocal())
6536                     LogPrintf("Warning: not banning local peer %s!\n", pto->addr.ToString());
6537                 else
6538                 {
6539                     CNode::Ban(pto->addr, BanReasonNodeMisbehaving);
6540                 }
6541             }
6542             state.fShouldBan = false;
6543         }
6544 
6545         BOOST_FOREACH(const CBlockReject& reject, state.rejects)
6546             pto->PushMessage(NetMsgType::REJECT, (string)NetMsgType::BLOCK, reject.chRejectCode, reject.strRejectReason, reject.hashBlock);
6547         state.rejects.clear();
6548 
6549         // Start block sync
6550         if (pindexBestHeader == NULL)
6551             pindexBestHeader = chainActive.Tip();
6552         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.
6553         if (!state.fSyncStarted && !pto->fClient && !pto->fDisconnect && !fImporting && !fReindex) {
6554             // Only actively request headers from a single peer, unless we're close to today.
6555             if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
6556                 state.fSyncStarted = true;
6557                 nSyncStarted++;
6558                 const CBlockIndex *pindexStart = pindexBestHeader;
6559                 /* If possible, start at the block preceding the currently
6560                    best known header.  This ensures that we always get a
6561                    non-empty list of headers back as long as the peer
6562                    is up-to-date.  With a non-empty response, we can initialise
6563                    the peer's known best block.  This wouldn't be possible
6564                    if we requested starting at pindexBestHeader and
6565                    got back an empty response.  */
6566                 if (pindexStart->pprev)
6567                     pindexStart = pindexStart->pprev;
6568                 LogPrint("net", "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->id, pto->nStartingHeight);
6569                 pto->PushMessage(NetMsgType::GETHEADERS, chainActive.GetLocator(pindexStart), uint256());
6570             }
6571         }
6572 
6573         // Resend wallet transactions that haven't gotten in a block yet
6574         // Except during reindex, importing and IBD, when old wallet
6575         // transactions become unconfirmed and spams other nodes.
6576         if (!fReindex && !fImporting && !IsInitialBlockDownload())
6577         {
6578             GetMainSignals().Broadcast(nTimeBestReceived);
6579         }
6580 
6581         //
6582         // Try sending block announcements via headers
6583         //
6584         {
6585             // If we have less than MAX_BLOCKS_TO_ANNOUNCE in our
6586             // list of block hashes we're relaying, and our peer wants
6587             // headers announcements, then find the first header
6588             // not yet known to our peer but would connect, and send.
6589             // If no header would connect, or if we have too many
6590             // blocks, or if the peer doesn't want headers, just
6591             // add all to the inv queue.
6592             LOCK(pto->cs_inventory);
6593             vector<CBlock> vHeaders;
6594             bool fRevertToInv = ((!state.fPreferHeaders &&
6595                                  (!state.fPreferHeaderAndIDs || pto->vBlockHashesToAnnounce.size() > 1)) ||
6596                                 pto->vBlockHashesToAnnounce.size() > MAX_BLOCKS_TO_ANNOUNCE);
6597             CBlockIndex *pBestIndex = NULL; // last header queued for delivery
6598             ProcessBlockAvailability(pto->id); // ensure pindexBestKnownBlock is up-to-date
6599 
6600             if (!fRevertToInv) {
6601                 bool fFoundStartingHeader = false;
6602                 // Try to find first header that our peer doesn't have, and
6603                 // then send all headers past that one.  If we come across any
6604                 // headers that aren't on chainActive, give up.
6605                 BOOST_FOREACH(const uint256 &hash, pto->vBlockHashesToAnnounce) {
6606                     BlockMap::iterator mi = mapBlockIndex.find(hash);
6607                     assert(mi != mapBlockIndex.end());
6608                     CBlockIndex *pindex = mi->second;
6609                     if (chainActive[pindex->nHeight] != pindex) {
6610                         // Bail out if we reorged away from this block
6611                         fRevertToInv = true;
6612                         break;
6613                     }
6614                     if (pBestIndex != NULL && pindex->pprev != pBestIndex) {
6615                         // This means that the list of blocks to announce don't
6616                         // connect to each other.
6617                         // This shouldn't really be possible to hit during
6618                         // regular operation (because reorgs should take us to
6619                         // a chain that has some block not on the prior chain,
6620                         // which should be caught by the prior check), but one
6621                         // way this could happen is by using invalidateblock /
6622                         // reconsiderblock repeatedly on the tip, causing it to
6623                         // be added multiple times to vBlockHashesToAnnounce.
6624                         // Robustly deal with this rare situation by reverting
6625                         // to an inv.
6626                         fRevertToInv = true;
6627                         break;
6628                     }
6629                     pBestIndex = pindex;
6630                     if (fFoundStartingHeader) {
6631                         // add this to the headers message
6632                         vHeaders.push_back(pindex->GetBlockHeader());
6633                     } else if (PeerHasHeader(&state, pindex)) {
6634                         continue; // keep looking for the first new block
6635                     } else if (pindex->pprev == NULL || PeerHasHeader(&state, pindex->pprev)) {
6636                         // Peer doesn't have this header but they do have the prior one.
6637                         // Start sending headers.
6638                         fFoundStartingHeader = true;
6639                         vHeaders.push_back(pindex->GetBlockHeader());
6640                     } else {
6641                         // Peer doesn't have this header or the prior one -- nothing will
6642                         // connect, so bail out.
6643                         fRevertToInv = true;
6644                         break;
6645                     }
6646                 }
6647             }
6648             if (!fRevertToInv && !vHeaders.empty()) {
6649                 if (vHeaders.size() == 1 && state.fPreferHeaderAndIDs) {
6650                     // We only send up to 1 block as header-and-ids, as otherwise
6651                     // probably means we're doing an initial-ish-sync or they're slow
6652                     LogPrint("net", "%s sending header-and-ids %s to peer %d\n", __func__,
6653                             vHeaders.front().GetHash().ToString(), pto->id);
6654                     //TODO: Shouldn't need to reload block from disk, but requires refactor
6655                     CBlock block;
6656                     assert(ReadBlockFromDisk(block, pBestIndex, consensusParams));
6657                     CBlockHeaderAndShortTxIDs cmpctblock(block, state.fWantsCmpctWitness);
6658                     pto->PushMessageWithFlag(state.fWantsCmpctWitness ? 0 : SERIALIZE_TRANSACTION_NO_WITNESS, NetMsgType::CMPCTBLOCK, cmpctblock);
6659                     state.pindexBestHeaderSent = pBestIndex;
6660                 } else if (state.fPreferHeaders) {
6661                     if (vHeaders.size() > 1) {
6662                         LogPrint("net", "%s: %u headers, range (%s, %s), to peer=%d\n", __func__,
6663                                 vHeaders.size(),
6664                                 vHeaders.front().GetHash().ToString(),
6665                                 vHeaders.back().GetHash().ToString(), pto->id);
6666                     } else {
6667                         LogPrint("net", "%s: sending header %s to peer=%d\n", __func__,
6668                                 vHeaders.front().GetHash().ToString(), pto->id);
6669                     }
6670                     pto->PushMessage(NetMsgType::HEADERS, vHeaders);
6671                     state.pindexBestHeaderSent = pBestIndex;
6672                 } else
6673                     fRevertToInv = true;
6674             }
6675             if (fRevertToInv) {
6676                 // If falling back to using an inv, just try to inv the tip.
6677                 // The last entry in vBlockHashesToAnnounce was our tip at some point
6678                 // in the past.
6679                 if (!pto->vBlockHashesToAnnounce.empty()) {
6680                     const uint256 &hashToAnnounce = pto->vBlockHashesToAnnounce.back();
6681                     BlockMap::iterator mi = mapBlockIndex.find(hashToAnnounce);
6682                     assert(mi != mapBlockIndex.end());
6683                     CBlockIndex *pindex = mi->second;
6684 
6685                     // Warn if we're announcing a block that is not on the main chain.
6686                     // This should be very rare and could be optimized out.
6687                     // Just log for now.
6688                     if (chainActive[pindex->nHeight] != pindex) {
6689                         LogPrint("net", "Announcing block %s not on main chain (tip=%s)\n",
6690                             hashToAnnounce.ToString(), chainActive.Tip()->GetBlockHash().ToString());
6691                     }
6692 
6693                     // If the peer's chain has this block, don't inv it back.
6694                     if (!PeerHasHeader(&state, pindex)) {
6695                         pto->PushInventory(CInv(MSG_BLOCK, hashToAnnounce));
6696                         LogPrint("net", "%s: sending inv peer=%d hash=%s\n", __func__,
6697                             pto->id, hashToAnnounce.ToString());
6698                     }
6699                 }
6700             }
6701             pto->vBlockHashesToAnnounce.clear();
6702         }
6703 
6704         //
6705         // Message: inventory
6706         //
6707         vector<CInv> vInv;
6708         {
6709             LOCK(pto->cs_inventory);
6710             vInv.reserve(std::max<size_t>(pto->vInventoryBlockToSend.size(), INVENTORY_BROADCAST_MAX));
6711 
6712             // Add blocks
6713             BOOST_FOREACH(const uint256& hash, pto->vInventoryBlockToSend) {
6714                 vInv.push_back(CInv(MSG_BLOCK, hash));
6715                 if (vInv.size() == MAX_INV_SZ) {
6716                     pto->PushMessage(NetMsgType::INV, vInv);
6717                     vInv.clear();
6718                 }
6719             }
6720             pto->vInventoryBlockToSend.clear();
6721 
6722             // Check whether periodic sends should happen
6723             bool fSendTrickle = pto->fWhitelisted;
6724             if (pto->nNextInvSend < nNow) {
6725                 fSendTrickle = true;
6726                 // Use half the delay for outbound peers, as there is less privacy concern for them.
6727                 pto->nNextInvSend = PoissonNextSend(nNow, INVENTORY_BROADCAST_INTERVAL >> !pto->fInbound);
6728             }
6729 
6730             // Time to send but the peer has requested we not relay transactions.
6731             if (fSendTrickle) {
6732                 LOCK(pto->cs_filter);
6733                 if (!pto->fRelayTxes) pto->setInventoryTxToSend.clear();
6734             }
6735 
6736             // Respond to BIP35 mempool requests
6737             if (fSendTrickle && pto->fSendMempool) {
6738                 auto vtxinfo = mempool.infoAll();
6739                 pto->fSendMempool = false;
6740                 CAmount filterrate = 0;
6741                 {
6742                     LOCK(pto->cs_feeFilter);
6743                     filterrate = pto->minFeeFilter;
6744                 }
6745 
6746                 LOCK(pto->cs_filter);
6747 
6748                 for (const auto& txinfo : vtxinfo) {
6749                     const uint256& hash = txinfo.tx->GetHash();
6750                     CInv inv(MSG_TX, hash);
6751                     pto->setInventoryTxToSend.erase(hash);
6752                     if (filterrate) {
6753                         if (txinfo.feeRate.GetFeePerK() < filterrate)
6754                             continue;
6755                     }
6756                     if (pto->pfilter) {
6757                         if (!pto->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
6758                     }
6759                     pto->filterInventoryKnown.insert(hash);
6760                     vInv.push_back(inv);
6761                     if (vInv.size() == MAX_INV_SZ) {
6762                         pto->PushMessage(NetMsgType::INV, vInv);
6763                         vInv.clear();
6764                     }
6765                 }
6766                 pto->timeLastMempoolReq = GetTime();
6767             }
6768 
6769             // Determine transactions to relay
6770             if (fSendTrickle) {
6771                 // Produce a vector with all candidates for sending
6772                 vector<std::set<uint256>::iterator> vInvTx;
6773                 vInvTx.reserve(pto->setInventoryTxToSend.size());
6774                 for (std::set<uint256>::iterator it = pto->setInventoryTxToSend.begin(); it != pto->setInventoryTxToSend.end(); it++) {
6775                     vInvTx.push_back(it);
6776                 }
6777                 CAmount filterrate = 0;
6778                 {
6779                     LOCK(pto->cs_feeFilter);
6780                     filterrate = pto->minFeeFilter;
6781                 }
6782                 // Topologically and fee-rate sort the inventory we send for privacy and priority reasons.
6783                 // A heap is used so that not all items need sorting if only a few are being sent.
6784                 CompareInvMempoolOrder compareInvMempoolOrder(&mempool);
6785                 std::make_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
6786                 // No reason to drain out at many times the network's capacity,
6787                 // especially since we have many peers and some will draw much shorter delays.
6788                 unsigned int nRelayedTransactions = 0;
6789                 LOCK(pto->cs_filter);
6790                 while (!vInvTx.empty() && nRelayedTransactions < INVENTORY_BROADCAST_MAX) {
6791                     // Fetch the top element from the heap
6792                     std::pop_heap(vInvTx.begin(), vInvTx.end(), compareInvMempoolOrder);
6793                     std::set<uint256>::iterator it = vInvTx.back();
6794                     vInvTx.pop_back();
6795                     uint256 hash = *it;
6796                     // Remove it from the to-be-sent set
6797                     pto->setInventoryTxToSend.erase(it);
6798                     // Check if not in the filter already
6799                     if (pto->filterInventoryKnown.contains(hash)) {
6800                         continue;
6801                     }
6802                     // Not in the mempool anymore? don't bother sending it.
6803                     auto txinfo = mempool.info(hash);
6804                     if (!txinfo.tx) {
6805                         continue;
6806                     }
6807                     if (filterrate && txinfo.feeRate.GetFeePerK() < filterrate) {
6808                         continue;
6809                     }
6810                     if (pto->pfilter && !pto->pfilter->IsRelevantAndUpdate(*txinfo.tx)) continue;
6811                     // Send
6812                     vInv.push_back(CInv(MSG_TX, hash));
6813                     nRelayedTransactions++;
6814                     {
6815                         // Expire old relay messages
6816                         while (!vRelayExpiration.empty() && vRelayExpiration.front().first < nNow)
6817                         {
6818                             mapRelay.erase(vRelayExpiration.front().second);
6819                             vRelayExpiration.pop_front();
6820                         }
6821 
6822                         auto ret = mapRelay.insert(std::make_pair(hash, std::move(txinfo.tx)));
6823                         if (ret.second) {
6824                             vRelayExpiration.push_back(std::make_pair(nNow + 15 * 60 * 1000000, ret.first));
6825                         }
6826                     }
6827                     if (vInv.size() == MAX_INV_SZ) {
6828                         pto->PushMessage(NetMsgType::INV, vInv);
6829                         vInv.clear();
6830                     }
6831                     pto->filterInventoryKnown.insert(hash);
6832                 }
6833             }
6834         }
6835         if (!vInv.empty())
6836             pto->PushMessage(NetMsgType::INV, vInv);
6837 
6838         // Detect whether we're stalling
6839         nNow = GetTimeMicros();
6840         if (!pto->fDisconnect && state.nStallingSince && state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) {
6841             // Stalling only triggers when the block download window cannot move. During normal steady state,
6842             // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
6843             // should only happen during initial block download.
6844             LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->id);
6845             pto->fDisconnect = true;
6846         }
6847         // In case there is a block that has been in flight from this peer for 2 + 0.5 * N times the block interval
6848         // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
6849         // We compensate for other peers to prevent killing off peers due to our own downstream link
6850         // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
6851         // to unreasonably increase our timeout.
6852         if (!pto->fDisconnect && state.vBlocksInFlight.size() > 0) {
6853             QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
6854             int nOtherPeersWithValidatedDownloads = nPeersWithValidatedDownloads - (state.nBlocksInFlightValidHeaders > 0);
6855             if (nNow > state.nDownloadingSince + consensusParams.nPowTargetSpacing * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
6856                 LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.hash.ToString(), pto->id);
6857                 pto->fDisconnect = true;
6858             }
6859         }
6860 
6861         //
6862         // Message: getdata (blocks)
6863         //
6864         vector<CInv> vGetData;
6865         if (!pto->fDisconnect && !pto->fClient && (fFetch || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
6866             vector<CBlockIndex*> vToDownload;
6867             NodeId staller = -1;
6868             FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller, consensusParams);
6869             BOOST_FOREACH(CBlockIndex *pindex, vToDownload) {
6870                 uint32_t nFetchFlags = GetFetchFlags(pto, pindex->pprev, consensusParams);
6871                 vGetData.push_back(CInv(MSG_BLOCK | nFetchFlags, pindex->GetBlockHash()));
6872                 MarkBlockAsInFlight(pto->GetId(), pindex->GetBlockHash(), consensusParams, pindex);
6873                 LogPrint("net", "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
6874                     pindex->nHeight, pto->id);
6875             }
6876             if (state.nBlocksInFlight == 0 && staller != -1) {
6877                 if (State(staller)->nStallingSince == 0) {
6878                     State(staller)->nStallingSince = nNow;
6879                     LogPrint("net", "Stall started peer=%d\n", staller);
6880                 }
6881             }
6882         }
6883 
6884         //
6885         // Message: getdata (non-blocks)
6886         //
6887         while (!pto->fDisconnect && !pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
6888         {
6889             const CInv& inv = (*pto->mapAskFor.begin()).second;
6890             if (!AlreadyHave(inv))
6891             {
6892                 if (fDebug)
6893                     LogPrint("net", "Requesting %s peer=%d\n", inv.ToString(), pto->id);
6894                 vGetData.push_back(inv);
6895                 if (vGetData.size() >= 1000)
6896                 {
6897                     pto->PushMessage(NetMsgType::GETDATA, vGetData);
6898                     vGetData.clear();
6899                 }
6900             } else {
6901                 //If we're not going to ask, don't expect a response.
6902                 pto->setAskFor.erase(inv.hash);
6903             }
6904             pto->mapAskFor.erase(pto->mapAskFor.begin());
6905         }
6906         if (!vGetData.empty())
6907             pto->PushMessage(NetMsgType::GETDATA, vGetData);
6908 
6909         //
6910         // Message: feefilter
6911         //
6912         // We don't want white listed peers to filter txs to us if we have -whitelistforcerelay
6913         if (!pto->fDisconnect && pto->nVersion >= FEEFILTER_VERSION && GetBoolArg("-feefilter", DEFAULT_FEEFILTER) &&
6914             !(pto->fWhitelisted && GetBoolArg("-whitelistforcerelay", DEFAULT_WHITELISTFORCERELAY))) {
6915             CAmount currentFilter = mempool.GetMinFee(GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFeePerK();
6916             int64_t timeNow = GetTimeMicros();
6917             if (timeNow > pto->nextSendTimeFeeFilter) {
6918                 CAmount filterToSend = filterRounder.round(currentFilter);
6919                 if (filterToSend != pto->lastSentFeeFilter) {
6920                     pto->PushMessage(NetMsgType::FEEFILTER, filterToSend);
6921                     pto->lastSentFeeFilter = filterToSend;
6922                 }
6923                 pto->nextSendTimeFeeFilter = PoissonNextSend(timeNow, AVG_FEEFILTER_BROADCAST_INTERVAL);
6924             }
6925             // If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
6926             // until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
6927             else if (timeNow + MAX_FEEFILTER_CHANGE_DELAY * 1000000 < pto->nextSendTimeFeeFilter &&
6928                      (currentFilter < 3 * pto->lastSentFeeFilter / 4 || currentFilter > 4 * pto->lastSentFeeFilter / 3)) {
6929                 pto->nextSendTimeFeeFilter = timeNow + (insecure_rand() % MAX_FEEFILTER_CHANGE_DELAY) * 1000000;
6930             }
6931         }
6932     }
6933     return true;
6934 }
6935 
ToString() const6936  std::string CBlockFileInfo::ToString() const {
6937      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));
6938  }
6939 
VersionBitsTipState(const Consensus::Params & params,Consensus::DeploymentPos pos)6940 ThresholdState VersionBitsTipState(const Consensus::Params& params, Consensus::DeploymentPos pos)
6941 {
6942     LOCK(cs_main);
6943     return VersionBitsState(chainActive.Tip(), params, pos, versionbitscache);
6944 }
6945 
6946 class CMainCleanup
6947 {
6948 public:
CMainCleanup()6949     CMainCleanup() {}
~CMainCleanup()6950     ~CMainCleanup() {
6951         // block headers
6952         BlockMap::iterator it1 = mapBlockIndex.begin();
6953         for (; it1 != mapBlockIndex.end(); it1++)
6954             delete (*it1).second;
6955         mapBlockIndex.clear();
6956 
6957         // orphan transactions
6958         mapOrphanTransactions.clear();
6959         mapOrphanTransactionsByPrev.clear();
6960     }
6961 } instance_of_cmaincleanup;
6962