]> Git Repo - VerusCoin.git/blame - src/main.cpp
Test
[VerusCoin.git] / src / main.cpp
CommitLineData
0a61b0df 1// Copyright (c) 2009-2010 Satoshi Nakamoto
f914f1a7 2// Copyright (c) 2009-2014 The Bitcoin Core developers
c5b390b6 3// Distributed under the MIT software license, see the accompanying
3a25a2b9
F
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
51ed9ec9 6#include "main.h"
319b1160 7
320f2cc7
SB
8#include "sodium.h"
9
51ed9ec9 10#include "addrman.h"
f35c6c4f 11#include "alert.h"
26c16d9d 12#include "arith_uint256.h"
319b1160 13#include "chainparams.h"
eb5fff9e 14#include "checkpoints.h"
319b1160 15#include "checkqueue.h"
da29ecbc 16#include "consensus/validation.h"
edd309e5 17#include "init.h"
afd4b94b 18#include "merkleblock.h"
a6df7ab5 19#include "metrics.h"
319b1160 20#include "net.h"
df852d2b 21#include "pow.h"
319b1160
GA
22#include "txdb.h"
23#include "txmempool.h"
ed6d0b5f 24#include "ui_interface.h"
937ba572 25#include "undo.h"
51ed9ec9 26#include "util.h"
217a5c92 27#include "utilmoneystr.h"
26c16d9d 28#include "validationinterface.h"
9ddb6ad0 29#include "wallet/asyncrpcoperation_sendmany.h"
51ed9ec9 30
358ce266 31#include <sstream>
51ed9ec9
BD
32
33#include <boost/algorithm/string/replace.hpp>
34#include <boost/filesystem.hpp>
35#include <boost/filesystem/fstream.hpp>
36cba8f1 36#include <boost/math/distributions/poisson.hpp>
ad49c256 37#include <boost/thread.hpp>
7c68cc07 38#include <boost/static_assert.hpp>
0a61b0df 39
4dc5eb05 40using namespace std;
0a61b0df 41
9b59e3bd 42#if defined(NDEBUG)
7662d72b 43# error "Zcash cannot be compiled without assertions."
9b59e3bd
GM
44#endif
45
d27afb07 46
c5b390b6
MF
47/**
48 * Global state
49 */
0a61b0df 50
51CCriticalSection cs_main;
52
145d5be8 53BlockMap mapBlockIndex;
4c6d41b8 54CChain chainActive;
ad6e6017 55CBlockIndex *pindexBestHeader = NULL;
51ed9ec9 56int64_t nTimeBestReceived = 0;
ff6a7af1
LD
57CWaitableCriticalSection csBestBlock;
58CConditionVariable cvBlockChange;
f9cae832 59int nScriptCheckThreads = 0;
66b02c93 60bool fImporting = false;
7fea4846 61bool fReindex = false;
2d1fa42e 62bool fTxIndex = false;
f9ec3f0f 63bool fHavePruned = false;
64bool fPruneMode = false;
3da434a2 65bool fIsBareMultisigStd = true;
3fcfbc8a 66bool fCheckBlockIndex = false;
a8cdaf5c 67bool fCheckpointsEnabled = true;
d212ba32 68bool fCoinbaseEnforcedProtectionEnabled = true;
fc684ad8 69size_t nCoinCacheUsage = 5000 * 300;
f9ec3f0f 70uint64_t nPruneTarget = 0;
4d9c7fe6 71bool fAlerts = DEFAULT_ALERTS;
0a61b0df 72
037b4f14 73/** Fees smaller than this (in satoshi) are considered zero fee (for relaying and mining) */
ba0625f2 74CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
13fc83c7
GA
75
76CTxMemPool mempool(::minRelayTxFee);
000dc551 77
c74332c6
GA
78struct COrphanTx {
79 CTransaction tx;
80 NodeId fromPeer;
81};
82map<uint256, COrphanTx> mapOrphanTransactions;
159bc481 83map<uint256, set<uint256> > mapOrphanTransactionsByPrev;
c74332c6 84void EraseOrphansFor(NodeId peer);
0a61b0df 85
9dcd524f
PW
86/**
87 * Returns true if there are nRequired or more blocks of minVersion or above
51aa2492 88 * in the last Consensus::Params::nMajorityWindow blocks, starting at pstart and going backwards.
9dcd524f 89 */
51aa2492 90static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams);
3fcfbc8a 91static void CheckBlockIndex();
9dcd524f 92
c5b390b6 93/** Constant stuff for coinbase transactions we create: */
7bf8b7c2 94CScript COINBASE_FLAGS;
0a61b0df 95
1a321777 96const string strMessageMagic = "Komodo Signed Message:\n";
2bc4fd60 97
caca6aa4
PW
98// Internal stuff
99namespace {
e10dcf27 100
6b29ccc9
B
101 struct CBlockIndexWorkComparator
102 {
3fcfbc8a 103 bool operator()(CBlockIndex *pa, CBlockIndex *pb) const {
6b29ccc9
B
104 // First sort by most total work, ...
105 if (pa->nChainWork > pb->nChainWork) return false;
106 if (pa->nChainWork < pb->nChainWork) return true;
107
108 // ... then by earliest time received, ...
109 if (pa->nSequenceId < pb->nSequenceId) return false;
110 if (pa->nSequenceId > pb->nSequenceId) return true;
111
112 // Use pointer address as tie breaker (should only happen with blocks
113 // loaded from disk, as those all have id 0).
114 if (pa < pb) return false;
115 if (pa > pb) return true;
116
117 // Identical blocks.
118 return false;
119 }
120 };
121
122 CBlockIndex *pindexBestInvalid;
714a3e65 123
c5b390b6 124 /**
3fcfbc8a 125 * The set of all CBlockIndex entries with BLOCK_VALID_TRANSACTIONS (for itself and all ancestors) and
f9ec3f0f 126 * as good as our current tip or better. Entries may be failed, though, and pruning nodes may be
127 * missing the data for the block.
c5b390b6 128 */
e17bd583 129 set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexCandidates;
c5b390b6 130 /** Number of nodes with fSyncStarted. */
341735eb 131 int nSyncStarted = 0;
f9ec3f0f 132 /** All pairs A->B, where A (or one if its ancestors) misses transactions, but B has transactions.
133 * Pruned nodes may have entries where B is missing data.
134 */
341735eb 135 multimap<CBlockIndex*, CBlockIndex*> mapBlocksUnlinked;
6b29ccc9
B
136
137 CCriticalSection cs_LastBlockFile;
ed6d1a2c 138 std::vector<CBlockFileInfo> vinfoBlockFile;
6b29ccc9 139 int nLastBlockFile = 0;
f9ec3f0f 140 /** Global flag to indicate we should check to see if there are
141 * block/undo files that should be deleted. Set on startup
142 * or if we allocate more file space when we're in prune mode
143 */
144 bool fCheckForPruning = false;
6b29ccc9 145
c5b390b6
MF
146 /**
147 * Every received block is assigned a unique and increasing identifier, so we
148 * know which one to give priority in case of a fork.
149 */
6b29ccc9 150 CCriticalSection cs_nBlockSequenceId;
c5b390b6 151 /** Blocks loaded from disk are assigned id 0, so start the counter at 1. */
6b29ccc9
B
152 uint32_t nBlockSequenceId = 1;
153
c5b390b6 154 /**
b05a89b2
LD
155 * Sources of received blocks, saved to be able to send them reject
156 * messages or ban them when processing happens afterwards. Protected by
157 * cs_main.
c5b390b6 158 */
6b29ccc9
B
159 map<uint256, NodeId> mapBlockSource;
160
ec9b6c33
PT
161 /**
162 * Filter for transactions that were recently rejected by
163 * AcceptToMemoryPool. These are not rerequested until the chain tip
164 * changes, at which point the entire filter is reset. Protected by
165 * cs_main.
166 *
167 * Without this filter we'd be re-requesting txs from each of our peers,
168 * increasing bandwidth consumption considerably. For instance, with 100
169 * peers, half of which relay a tx we don't accept, that might be a 50x
170 * bandwidth increase. A flooding attacker attempting to roll-over the
171 * filter using minimum-sized, 60byte, transactions might manage to send
172 * 1000/sec if we have fast peers, so we pick 120,000 to give our peers a
173 * two minute window to send invs to us.
174 *
175 * Decreasing the false positive rate is fairly cheap, so we pick one in a
176 * million to make it highly unlikely for users to have issues with this
177 * filter.
178 *
179 * Memory used: 1.7MB
180 */
181 boost::scoped_ptr<CRollingBloomFilter> recentRejects;
182 uint256 hashRecentRejectsChainTip;
183
c5b390b6 184 /** Blocks that are in flight, and that are in the queue to be downloaded. Protected by cs_main. */
6b29ccc9
B
185 struct QueuedBlock {
186 uint256 hash;
c5b390b6
MF
187 CBlockIndex *pindex; //! Optional.
188 int64_t nTime; //! Time of "getdata" request in microseconds.
91613034 189 bool fValidatedHeaders; //! Whether this block has validated headers at the time of request.
8ba7f842 190 int64_t nTimeDisconnect; //! The timeout for this block request (for disconnecting a slow peer)
6b29ccc9
B
191 };
192 map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
e10dcf27 193
91613034
PW
194 /** Number of blocks in flight with validated headers. */
195 int nQueuedValidatedHeaders = 0;
196
c5b390b6 197 /** Number of preferable block download peers. */
b4ee0bdd 198 int nPreferredDownload = 0;
51ce901a 199
c5b390b6 200 /** Dirty block index entries. */
51ce901a
PW
201 set<CBlockIndex*> setDirtyBlockIndex;
202
c5b390b6 203 /** Dirty block file entries. */
51ce901a 204 set<int> setDirtyFileInfo;
e10dcf27 205} // anon namespace
0a61b0df 206
501da250
EL
207//////////////////////////////////////////////////////////////////////////////
208//
209// Registration of network node signals.
210//
211
b2864d2f 212namespace {
75f51f2a
PW
213
214struct CBlockReject {
215 unsigned char chRejectCode;
216 string strRejectReason;
217 uint256 hashBlock;
218};
219
c5b390b6
MF
220/**
221 * Maintain validation-specific state about nodes, protected by cs_main, instead
222 * by CNode's own locks. This simplifies asynchronous operation, where
223 * processing of incoming data is done after the ProcessMessage call returns,
224 * and we're no longer holding the node's locks.
225 */
b2864d2f 226struct CNodeState {
9c273790
PW
227 //! The peer's address
228 CService address;
229 //! Whether we have a fully established connection.
230 bool fCurrentlyConnected;
c5b390b6 231 //! Accumulated misbehaviour score for this peer.
b2864d2f 232 int nMisbehavior;
c5b390b6 233 //! Whether this peer should be disconnected and banned (unless whitelisted).
b2864d2f 234 bool fShouldBan;
c5b390b6 235 //! String name of this peer (debugging/logging purposes).
b2864d2f 236 std::string name;
c5b390b6 237 //! List of asynchronously-determined block rejections to notify this peer about.
75f51f2a 238 std::vector<CBlockReject> rejects;
c5b390b6 239 //! The best known block we know this peer has announced.
aa815647 240 CBlockIndex *pindexBestKnownBlock;
c5b390b6 241 //! The hash of the last unknown block this peer has announced.
aa815647 242 uint256 hashLastUnknownBlock;
c5b390b6 243 //! The last full block we both have.
341735eb 244 CBlockIndex *pindexLastCommonBlock;
c5b390b6 245 //! Whether we've started headers synchronization with this peer.
341735eb 246 bool fSyncStarted;
c5b390b6 247 //! Since when we're stalling block download progress (in microseconds), or 0.
341735eb 248 int64_t nStallingSince;
f59d8f0b
PW
249 list<QueuedBlock> vBlocksInFlight;
250 int nBlocksInFlight;
8ba7f842 251 int nBlocksInFlightValidHeaders;
c5b390b6 252 //! Whether we consider this a preferred download peer.
b4ee0bdd 253 bool fPreferredDownload;
b2864d2f
PW
254
255 CNodeState() {
9c273790 256 fCurrentlyConnected = false;
b2864d2f
PW
257 nMisbehavior = 0;
258 fShouldBan = false;
aa815647 259 pindexBestKnownBlock = NULL;
4f152496 260 hashLastUnknownBlock.SetNull();
341735eb
PW
261 pindexLastCommonBlock = NULL;
262 fSyncStarted = false;
263 nStallingSince = 0;
f59d8f0b 264 nBlocksInFlight = 0;
8ba7f842 265 nBlocksInFlightValidHeaders = 0;
b4ee0bdd 266 fPreferredDownload = false;
b2864d2f
PW
267 }
268};
269
c5b390b6 270/** Map maintaining per-node state. Requires cs_main. */
b2864d2f
PW
271map<NodeId, CNodeState> mapNodeState;
272
273// Requires cs_main.
274CNodeState *State(NodeId pnode) {
275 map<NodeId, CNodeState>::iterator it = mapNodeState.find(pnode);
276 if (it == mapNodeState.end())
277 return NULL;
278 return &it->second;
279}
280
281int GetHeight()
4c6d41b8
PW
282{
283 LOCK(cs_main);
284 return chainActive.Height();
285}
286
b4ee0bdd
PW
287void UpdatePreferredDownload(CNode* node, CNodeState* state)
288{
289 nPreferredDownload -= state->fPreferredDownload;
290
291 // Whether this node should be marked as a preferred download node.
292 state->fPreferredDownload = (!node->fInbound || node->fWhitelisted) && !node->fOneShot && !node->fClient;
293
294 nPreferredDownload += state->fPreferredDownload;
295}
296
8ba7f842 297// Returns time at which to timeout block request (nTime in microseconds)
82737933 298int64_t GetBlockTimeout(int64_t nTime, int nValidatedQueuedBefore, const Consensus::Params &consensusParams)
8ba7f842 299{
82737933 300 return nTime + 500000 * consensusParams.nPowTargetSpacing * (4 + nValidatedQueuedBefore);
8ba7f842
SD
301}
302
b2864d2f
PW
303void InitializeNode(NodeId nodeid, const CNode *pnode) {
304 LOCK(cs_main);
305 CNodeState &state = mapNodeState.insert(std::make_pair(nodeid, CNodeState())).first->second;
306 state.name = pnode->addrName;
9c273790 307 state.address = pnode->addr;
b2864d2f
PW
308}
309
310void FinalizeNode(NodeId nodeid) {
311 LOCK(cs_main);
f59d8f0b
PW
312 CNodeState *state = State(nodeid);
313
341735eb
PW
314 if (state->fSyncStarted)
315 nSyncStarted--;
316
9c273790
PW
317 if (state->nMisbehavior == 0 && state->fCurrentlyConnected) {
318 AddressCurrentlyConnected(state->address);
319 }
320
f59d8f0b
PW
321 BOOST_FOREACH(const QueuedBlock& entry, state->vBlocksInFlight)
322 mapBlocksInFlight.erase(entry.hash);
c74332c6 323 EraseOrphansFor(nodeid);
b4ee0bdd 324 nPreferredDownload -= state->fPreferredDownload;
f59d8f0b 325
b2864d2f
PW
326 mapNodeState.erase(nodeid);
327}
f59d8f0b
PW
328
329// Requires cs_main.
304892fc
SD
330// Returns a bool indicating whether we requested this block.
331bool MarkBlockAsReceived(const uint256& hash) {
f59d8f0b
PW
332 map<uint256, pair<NodeId, list<QueuedBlock>::iterator> >::iterator itInFlight = mapBlocksInFlight.find(hash);
333 if (itInFlight != mapBlocksInFlight.end()) {
334 CNodeState *state = State(itInFlight->second.first);
91613034 335 nQueuedValidatedHeaders -= itInFlight->second.second->fValidatedHeaders;
8ba7f842 336 state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders;
f59d8f0b
PW
337 state->vBlocksInFlight.erase(itInFlight->second.second);
338 state->nBlocksInFlight--;
341735eb 339 state->nStallingSince = 0;
f59d8f0b 340 mapBlocksInFlight.erase(itInFlight);
304892fc 341 return true;
f59d8f0b 342 }
304892fc 343 return false;
f59d8f0b
PW
344}
345
346// Requires cs_main.
82737933 347void MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, const Consensus::Params& consensusParams, CBlockIndex *pindex = NULL) {
f59d8f0b
PW
348 CNodeState *state = State(nodeid);
349 assert(state != NULL);
350
351 // Make sure it's not listed somewhere already.
352 MarkBlockAsReceived(hash);
353
8ba7f842 354 int64_t nNow = GetTimeMicros();
82737933 355 QueuedBlock newentry = {hash, pindex, nNow, pindex != NULL, GetBlockTimeout(nNow, nQueuedValidatedHeaders, consensusParams)};
91613034 356 nQueuedValidatedHeaders += newentry.fValidatedHeaders;
f59d8f0b
PW
357 list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(), newentry);
358 state->nBlocksInFlight++;
8ba7f842 359 state->nBlocksInFlightValidHeaders += newentry.fValidatedHeaders;
f59d8f0b
PW
360 mapBlocksInFlight[hash] = std::make_pair(nodeid, it);
361}
362
aa815647
PW
363/** Check whether the last unknown block a peer advertized is not yet known. */
364void ProcessBlockAvailability(NodeId nodeid) {
365 CNodeState *state = State(nodeid);
366 assert(state != NULL);
367
4f152496 368 if (!state->hashLastUnknownBlock.IsNull()) {
145d5be8 369 BlockMap::iterator itOld = mapBlockIndex.find(state->hashLastUnknownBlock);
110696f9 370 if (itOld != mapBlockIndex.end() && itOld->second->nChainWork > 0)
944bf4b0 371 {
aa815647
PW
372 if (state->pindexBestKnownBlock == NULL || itOld->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
373 state->pindexBestKnownBlock = itOld->second;
4f152496 374 state->hashLastUnknownBlock.SetNull();
aa815647
PW
375 }
376 }
377}
378
379/** Update tracking information about which blocks a peer is assumed to have. */
380void UpdateBlockAvailability(NodeId nodeid, const uint256 &hash) {
381 CNodeState *state = State(nodeid);
382 assert(state != NULL);
383
110696f9 384 ProcessBlockAvailability(nodeid);
aa815647 385
110696f9 386 BlockMap::iterator it = mapBlockIndex.find(hash);
a13fb56e 387 if (it->second != 0 && it != mapBlockIndex.end() && it->second->nChainWork > 0) {
aa815647
PW
388 // An actually better block was announced.
389 if (state->pindexBestKnownBlock == NULL || it->second->nChainWork >= state->pindexBestKnownBlock->nChainWork)
390 state->pindexBestKnownBlock = it->second;
110696f9 391 } else
0a0ec219 392 {
aa815647
PW
393 // An unknown block was announced; just assume that the latest one is the best one.
394 state->hashLastUnknownBlock = hash;
395 }
396}
397
341735eb
PW
398/** Find the last common ancestor two blocks have.
399 * Both pa and pb must be non-NULL. */
400CBlockIndex* LastCommonAncestor(CBlockIndex* pa, CBlockIndex* pb) {
401 if (pa->nHeight > pb->nHeight) {
402 pa = pa->GetAncestor(pb->nHeight);
403 } else if (pb->nHeight > pa->nHeight) {
404 pb = pb->GetAncestor(pa->nHeight);
405 }
406
407 while (pa != pb && pa && pb) {
408 pa = pa->pprev;
409 pb = pb->pprev;
410 }
411
412 // Eventually all chain branches meet at the genesis block.
413 assert(pa == pb);
414 return pa;
415}
416
417/** Update pindexLastCommonBlock and add not-in-flight missing successors to vBlocks, until it has
418 * at most count entries. */
419void FindNextBlocksToDownload(NodeId nodeid, unsigned int count, std::vector<CBlockIndex*>& vBlocks, NodeId& nodeStaller) {
420 if (count == 0)
421 return;
422
423 vBlocks.reserve(vBlocks.size() + count);
424 CNodeState *state = State(nodeid);
425 assert(state != NULL);
426
427 // Make sure pindexBestKnownBlock is up to date, we'll need it.
428 ProcessBlockAvailability(nodeid);
429
430 if (state->pindexBestKnownBlock == NULL || state->pindexBestKnownBlock->nChainWork < chainActive.Tip()->nChainWork) {
431 // This peer has nothing interesting.
432 return;
433 }
434
435 if (state->pindexLastCommonBlock == NULL) {
436 // Bootstrap quickly by guessing a parent of our best tip is the forking point.
437 // Guessing wrong in either direction is not a problem.
438 state->pindexLastCommonBlock = chainActive[std::min(state->pindexBestKnownBlock->nHeight, chainActive.Height())];
439 }
440
441 // If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
b05a89b2 442 // of its current tip anymore. Go back enough to fix that.
341735eb
PW
443 state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
444 if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
445 return;
446
447 std::vector<CBlockIndex*> vToFetch;
448 CBlockIndex *pindexWalk = state->pindexLastCommonBlock;
e11b2ce4
PW
449 // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last
450 // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to
451 // download that next block if the window were 1 larger.
452 int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW;
453 int nMaxHeight = std::min<int>(state->pindexBestKnownBlock->nHeight, nWindowEnd + 1);
341735eb
PW
454 NodeId waitingfor = -1;
455 while (pindexWalk->nHeight < nMaxHeight) {
456 // Read up to 128 (or more, if more blocks than that are needed) successors of pindexWalk (towards
457 // pindexBestKnownBlock) into vToFetch. We fetch 128, because CBlockIndex::GetAncestor may be as expensive
458 // as iterating over ~100 CBlockIndex* entries anyway.
459 int nToFetch = std::min(nMaxHeight - pindexWalk->nHeight, std::max<int>(count - vBlocks.size(), 128));
460 vToFetch.resize(nToFetch);
461 pindexWalk = state->pindexBestKnownBlock->GetAncestor(pindexWalk->nHeight + nToFetch);
462 vToFetch[nToFetch - 1] = pindexWalk;
463 for (unsigned int i = nToFetch - 1; i > 0; i--) {
464 vToFetch[i - 1] = vToFetch[i]->pprev;
465 }
466
467 // Iterate over those blocks in vToFetch (in forward direction), adding the ones that
468 // are not yet downloaded and not in flight to vBlocks. In the mean time, update
a5876065
SD
469 // pindexLastCommonBlock as long as all ancestors are already downloaded, or if it's
470 // already part of our chain (and therefore don't need it even if pruned).
341735eb 471 BOOST_FOREACH(CBlockIndex* pindex, vToFetch) {
34970223
PW
472 if (!pindex->IsValid(BLOCK_VALID_TREE)) {
473 // We consider the chain that this peer is on invalid.
474 return;
475 }
a5876065 476 if (pindex->nStatus & BLOCK_HAVE_DATA || chainActive.Contains(pindex)) {
341735eb
PW
477 if (pindex->nChainTx)
478 state->pindexLastCommonBlock = pindex;
479 } else if (mapBlocksInFlight.count(pindex->GetBlockHash()) == 0) {
480 // The block is not already downloaded, and not yet in flight.
e11b2ce4 481 if (pindex->nHeight > nWindowEnd) {
341735eb
PW
482 // We reached the end of the window.
483 if (vBlocks.size() == 0 && waitingfor != nodeid) {
484 // We aren't able to fetch anything, but we would be if the download window was one larger.
485 nodeStaller = waitingfor;
486 }
487 return;
488 }
489 vBlocks.push_back(pindex);
490 if (vBlocks.size() == count) {
491 return;
492 }
493 } else if (waitingfor == -1) {
494 // This is the first already-in-flight block.
495 waitingfor = mapBlocksInFlight[pindex->GetBlockHash()].first;
496 }
497 }
498 }
499}
500
e10dcf27 501} // anon namespace
b2864d2f
PW
502
503bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats) {
504 LOCK(cs_main);
505 CNodeState *state = State(nodeid);
506 if (state == NULL)
507 return false;
508 stats.nMisbehavior = state->nMisbehavior;
aa815647 509 stats.nSyncHeight = state->pindexBestKnownBlock ? state->pindexBestKnownBlock->nHeight : -1;
ad6e6017
PW
510 stats.nCommonHeight = state->pindexLastCommonBlock ? state->pindexLastCommonBlock->nHeight : -1;
511 BOOST_FOREACH(const QueuedBlock& queue, state->vBlocksInFlight) {
512 if (queue.pindex)
513 stats.vHeightInFlight.push_back(queue.pindex->nHeight);
514 }
b2864d2f
PW
515 return true;
516}
517
501da250
EL
518void RegisterNodeSignals(CNodeSignals& nodeSignals)
519{
4c6d41b8 520 nodeSignals.GetHeight.connect(&GetHeight);
501da250
EL
521 nodeSignals.ProcessMessages.connect(&ProcessMessages);
522 nodeSignals.SendMessages.connect(&SendMessages);
b2864d2f
PW
523 nodeSignals.InitializeNode.connect(&InitializeNode);
524 nodeSignals.FinalizeNode.connect(&FinalizeNode);
501da250 525}
64c7ee7e 526
501da250
EL
527void UnregisterNodeSignals(CNodeSignals& nodeSignals)
528{
4c6d41b8 529 nodeSignals.GetHeight.disconnect(&GetHeight);
501da250
EL
530 nodeSignals.ProcessMessages.disconnect(&ProcessMessages);
531 nodeSignals.SendMessages.disconnect(&SendMessages);
b2864d2f
PW
532 nodeSignals.InitializeNode.disconnect(&InitializeNode);
533 nodeSignals.FinalizeNode.disconnect(&FinalizeNode);
501da250 534}
64c7ee7e 535
6db83db3 536CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator)
537{
70e7fba0 538 // Find the first block the caller has in the main chain
e4daecda 539 BOOST_FOREACH(const uint256& hash, locator.vHave) {
145d5be8 540 BlockMap::iterator mi = mapBlockIndex.find(hash);
e4d89f43 541 if (mi != mapBlockIndex.end())
70e7fba0
MH
542 {
543 CBlockIndex* pindex = (*mi).second;
43450135 544 if (pindex != 0 && chain.Contains(pindex))
70e7fba0
MH
545 return pindex;
546 }
547 }
6db83db3 548 return chain.Genesis();
77339e5a
PW
549}
550
ae8bfd12 551CCoinsViewCache *pcoinsTip = NULL;
d979e6e3 552CBlockTreeDB *pblocktree = NULL;
b62d7030 553
554// Komodo globals
b62d7030 555
7637aa7f 556#define KOMODO_ZCASH
eab0d89c 557#include "komodo.h"
450cbb09 558
0a61b0df 559//////////////////////////////////////////////////////////////////////////////
560//
561// mapOrphanTransactions
562//
563
c74332c6 564bool AddOrphanTx(const CTransaction& tx, NodeId peer)
0a61b0df 565{
805344dc 566 uint256 hash = tx.GetHash();
0a61b0df 567 if (mapOrphanTransactions.count(hash))
77b99cf7
GA
568 return false;
569
77b99cf7
GA
570 // Ignore big transactions, to avoid a
571 // send-big-orphans memory exhaustion attack. If a peer has a legitimate
572 // large transaction with a missing parent then we assume
573 // it will rebroadcast it later, after the parent transaction(s)
574 // have been mined or received.
575 // 10,000 orphans, each of which is at most 5,000 bytes big is
576 // at most 500 megabytes of orphans:
e923e3ae 577 unsigned int sz = tx.GetSerializeSize(SER_NETWORK, tx.nVersion);
159bc481 578 if (sz > 5000)
77b99cf7 579 {
7d9d134b 580 LogPrint("mempool", "ignoring large orphan tx (size: %u, hash: %s)\n", sz, hash.ToString());
77b99cf7
GA
581 return false;
582 }
142e6041 583
c74332c6
GA
584 mapOrphanTransactions[hash].tx = tx;
585 mapOrphanTransactions[hash].fromPeer = peer;
223b6f1b 586 BOOST_FOREACH(const CTxIn& txin, tx.vin)
159bc481 587 mapOrphanTransactionsByPrev[txin.prevout.hash].insert(hash);
77b99cf7 588
c74332c6
GA
589 LogPrint("mempool", "stored orphan tx %s (mapsz %u prevsz %u)\n", hash.ToString(),
590 mapOrphanTransactions.size(), mapOrphanTransactionsByPrev.size());
77b99cf7 591 return true;
0a61b0df 592}
593
64c7ee7e 594void static EraseOrphanTx(uint256 hash)
0a61b0df 595{
c74332c6 596 map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.find(hash);
89d91f6a 597 if (it == mapOrphanTransactions.end())
0a61b0df 598 return;
c74332c6 599 BOOST_FOREACH(const CTxIn& txin, it->second.tx.vin)
0a61b0df 600 {
89d91f6a 601 map<uint256, set<uint256> >::iterator itPrev = mapOrphanTransactionsByPrev.find(txin.prevout.hash);
def2fdb4
GA
602 if (itPrev == mapOrphanTransactionsByPrev.end())
603 continue;
89d91f6a
WL
604 itPrev->second.erase(hash);
605 if (itPrev->second.empty())
606 mapOrphanTransactionsByPrev.erase(itPrev);
0a61b0df 607 }
89d91f6a 608 mapOrphanTransactions.erase(it);
0a61b0df 609}
610
c74332c6
GA
611void EraseOrphansFor(NodeId peer)
612{
613 int nErased = 0;
614 map<uint256, COrphanTx>::iterator iter = mapOrphanTransactions.begin();
615 while (iter != mapOrphanTransactions.end())
616 {
617 map<uint256, COrphanTx>::iterator maybeErase = iter++; // increment to avoid iterator becoming invalid
618 if (maybeErase->second.fromPeer == peer)
619 {
805344dc 620 EraseOrphanTx(maybeErase->second.tx.GetHash());
c74332c6
GA
621 ++nErased;
622 }
623 }
624 if (nErased > 0) LogPrint("mempool", "Erased %d orphan tx from peer %d\n", nErased, peer);
625}
626
627
7bd9c3a3 628unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
142e6041 629{
7bd9c3a3 630 unsigned int nEvicted = 0;
142e6041
GA
631 while (mapOrphanTransactions.size() > nMaxOrphans)
632 {
633 // Evict a random orphan:
f718aedd 634 uint256 randomhash = GetRandHash();
c74332c6 635 map<uint256, COrphanTx>::iterator it = mapOrphanTransactions.lower_bound(randomhash);
142e6041
GA
636 if (it == mapOrphanTransactions.end())
637 it = mapOrphanTransactions.begin();
638 EraseOrphanTx(it->first);
639 ++nEvicted;
640 }
641 return nEvicted;
642}
0a61b0df 643
644
645
646
647
648
649
980bfe6e 650bool IsStandardTx(const CTransaction& tx, string& reason)
000dc551 651{
e923e3ae 652 if (tx.nVersion > CTransaction::MAX_CURRENT_VERSION || tx.nVersion < CTransaction::MIN_CURRENT_VERSION) {
980bfe6e 653 reason = "version";
dae3e10a 654 return false;
980bfe6e 655 }
dae3e10a 656
05df3fc6 657 BOOST_FOREACH(const CTxIn& txin, tx.vin)
e679ec96 658 {
4d79098a
PT
659 // Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed
660 // keys. (remember the 520 byte limit on redeemScript size) That works
675bcd58 661 // out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627
4d79098a
PT
662 // bytes of scriptSig, which we round off to 1650 bytes for some minor
663 // future-proofing. That's also enough to spend a 20-of-20
664 // CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not
665 // considered standard)
666 if (txin.scriptSig.size() > 1650) {
980bfe6e 667 reason = "scriptsig-size";
922e8e29 668 return false;
980bfe6e
JG
669 }
670 if (!txin.scriptSig.IsPushOnly()) {
671 reason = "scriptsig-not-pushonly";
922e8e29 672 return false;
87fe71e1 673 }
e679ec96 674 }
a7934247 675
9632af31 676 unsigned int v=0,nDataOut = 0;
a7934247 677 txnouttype whichType;
9632af31 678 BOOST_FOREACH(const CTxOut& txout, tx.vout)
679 {
cc0f93da 680 if (!::IsStandard(txout.scriptPubKey, whichType))
681 {
7a82f2fc 682 reason = "scriptpubkey";
cc0f93da 683 fprintf(stderr,">>>>>>>>>>>>>>> vout.%d nDataout.%d\n",v,nDataOut);
922e8e29 684 return false;
980bfe6e 685 }
9632af31 686
a7934247 687 if (whichType == TX_NULL_DATA)
cc0f93da 688 {
a7934247 689 nDataOut++;
7a82f2fc 690 //fprintf(stderr,"is OP_RETURN\n");
cc0f93da 691 }
3da434a2
JG
692 else if ((whichType == TX_MULTISIG) && (!fIsBareMultisigStd)) {
693 reason = "bare-multisig";
694 return false;
695 } else if (txout.IsDust(::minRelayTxFee)) {
980bfe6e 696 reason = "dust";
65ce2156 697 return false;
980bfe6e 698 }
9632af31 699 v++;
65ce2156 700 }
980bfe6e 701
a7934247
JG
702 // only one OP_RETURN txout is permitted
703 if (nDataOut > 1) {
b34e88a8 704 reason = "multi-op-return";
a7934247
JG
705 return false;
706 }
707
e679ec96
GA
708 return true;
709}
710
7d44d3dd 711uint32_t komodo_heightstamp(int32_t height);
712
ccd177d9 713int32_t komodo_validate_interest(uint32_t *expiredp,const CTransaction& tx,int32_t txheight,uint32_t prevblocktime)
886a58ba 714{
c1004caa 715 int32_t i; uint32_t cmptime=0,tiptime=0,locktime=0; uint64_t value=0; CBlockIndex *tip=0;
4f7a5425 716 if ( KOMODO_REWIND == 0 && ASSETCHAINS_SYMBOL[0] == 0 && (int64_t)tx.nLockTime >= LOCKTIME_THRESHOLD )//1473793441 )
886a58ba 717 {
044a7b96 718 //locktime = komodo_interest_args(&prevblocktime,&txheight,&tiptime,&value,tx.GetHash(),0);
719 LOCK(cs_main);
e8950aca 720 CTransaction gettx; uint256 hashBlock; CBlockIndex *pindex,*previndex;
721 if ( GetTransaction(tx.GetHash(),gettx,hashBlock,true) )
044a7b96 722 {
723 if ( (pindex= mapBlockIndex[hashBlock]) != 0 )
724 {
725 txheight = pindex->nHeight;
4ba649d6 726 if ( (previndex= pindex->pprev) != 0 )
044a7b96 727 prevblocktime = previndex->nTime;
728 }
729 }
730 if ( prevblocktime != 0 )
731 cmptime = prevblocktime + 600;
732 else
733 {
129d39b0 734 //fprintf(stderr,"couldnt get cmptime for [%d] t%u\n",txheight,prevblocktime);
044a7b96 735 return(-1);
736 }
9f539d88 737 /* uint32_t prevblocktime=0,cmptime=0,txheighttime=0,tiptime=0,locktime=0;
738 prevblocktime = 0;
52e5ebfd 739 locktime = komodo_interest_args(&txheighttime,&txheight,&tiptime,&value,tx.GetHash(),0);
c2123afe 740 if ( (txheight= txheightarg) == 0 )
741 txheight = chainActive.Tip()->nHeight + 1;
52e5ebfd 742 if ( (prev= komodo_chainactive(txheight-1)) != 0 )
743 prevblocktime = prev->nTime;
a6e10f40 744 if ( tiptime == 0 )
745 tiptime = (int32_t)chainActive.Tip()->nTime;
83da1e16 746 if ( txheighttime == 0 )
886a58ba 747 {
af484891 748 static uint32_t counter0;
36f4d2cd 749 txheighttime = chainActive.Tip()->nTime;
e7fa1876 750 if ( counter0++ < 3 )
36f4d2cd 751 fprintf(stderr,"error getting txheighttime, set to tiptime.%u\n",txheighttime);
886a58ba 752 }
de463b10 753 if ( txheighttime != 0 && txblocktime != 0 )
886a58ba 754 {
de463b10 755 cmptime = txheighttime;
756 if ( txblocktime < cmptime )
757 cmptime = txblocktime;
758 }
759 else if ( (cmptime= txheighttime) == 0 )
7133babe 760 cmptime = txblocktime;
241a6ce5 761 if ( tiptime != 0 && tiptime < cmptime )
762 cmptime = tiptime;
b2d09a90 763 if ( locktime != 0 && prevblocktime != 0 && prevblocktime < cmptime )
9f539d88 764 cmptime = prevblocktime;
b575c4fb 765 if ( (prev= komodo_chainactive(txheight-1)) != 0 )
766 cmptime = prev->nTime + 600;
67cb4250 767 if ( cmptime >= 1490159171 - 24*3600 )
de463b10 768 {
67cb4250 769 if ( cmptime != 0 && (int64_t)tx.nLockTime < cmptime-3600 )
886a58ba 770 {
67cb4250 771 if ( txheighttime > 1490159171 || (locktime == 0 && txheighttime >= 1490159171) ) // 246748
772 {
773 if ( tx.nLockTime != 1477258935 )
b2d09a90 774 {
4110a59a 775 fprintf(stderr,"komodo_validate_interest reject.%d [%d] locktime %u/%u vs nBlockTime %u txheighttime.%u tiptime.%u txb.%u cmp.%u\n",txheight,(int32_t)(tx.nLockTime - (cmptime-3600)),(uint32_t)tx.nLockTime,locktime,(uint32_t)chainActive.Tip()->nTime,txheighttime,tiptime,txblocktime,cmptime);
b2d09a90 776 }
2d14730e 777 if ( expiredp != 0 )
778 *expiredp = cmptime-3600;
67cb4250 779 return(-1);
780 } else fprintf(stderr,"validateinterest grandfather.%d locktime %u vs txheighttime.%u tiptime.%u txb.%u cmp.%u\n",(int32_t)txheight,tx.nLockTime,txheighttime,tiptime,txblocktime,cmptime);
781 }
044a7b96 782 }
0cb1d2da 783 if ( (cmptime= prevblocktime) == 0 )
5150cb47 784 {
ccd177d9 785 if ( (cmptime= komodo_heightstamp(txheight-1)) == 0 )
5150cb47 786 {
ccd177d9 787 fprintf(stderr,"couldnt get prev.[%d] use tiptime.%u\n",txheight-1,cmptime);
5150cb47 788 return(-1);
789 }
044a7b96 790 }*/
ccd177d9 791 if ( cmptime > 0 && txheight > 246748 )
9f539d88 792 {
ccd177d9 793 if ( txheight < 247205 )
044a7b96 794 cmptime -= 600;
9f539d88 795 if ( (int64_t)tx.nLockTime < cmptime-3600 )
796 {
4f7a5425 797 if ( tx.nLockTime != 1477258935 )
9f539d88 798 {
044a7b96 799 fprintf(stderr,"komodo_validate_interest reject.%d [%d] locktime %u tiptime.%u cmp.%u\n",txheight,(int32_t)(tx.nLockTime - (cmptime-3600)),(uint32_t)tx.nLockTime,(uint32_t)(tip != 0 ? tip->nTime : 0),cmptime);
9f539d88 800 }
801 if ( expiredp != 0 )
802 *expiredp = cmptime-3600;
803 return(-1);
804 }
886a58ba 805 }
044a7b96 806 fprintf(stderr,"validateinterest accept.%d [%d] tip.%d locktime %u cmp.%u\n",(int32_t)txheight,(int32_t)(tx.nLockTime - (cmptime-3600)),(int32_t)(tip != 0 ? tip->nHeight : 0),(int32_t)tx.nLockTime,cmptime);
886a58ba 807 }
808 return(0);
809}
810
0cb1d2da 811bool IsFinalTx(uint32_t *expiredp,const CTransaction &tx, int nBlockHeight, int64_t nBlockTime,int flags,uint32_t prevblocktime)
912af0a6 812{
b5284a65 813 int32_t i;
6ff77181 814 if ( expiredp != 0 )
815 *expiredp = 0;
05df3fc6
EL
816 if (tx.nLockTime == 0)
817 return true;
fc901e2f 818 if ( ASSETCHAINS_SYMBOL[0] == 0 && flags == STANDARD_LOCKTIME_VERIFY_FLAGS && (int64_t)tx.nLockTime >= LOCKTIME_THRESHOLD )//&& nBlockTime >= 1473793441 ) //&& (int64_t)tx.nLockTime < nBlockTime-3600
a9980a36 819 {
aa8eaec4 820 if ( komodo_validate_interest(expiredp,tx,nBlockHeight,prevblocktime < nBlockTime ? nBlockTime : prevblocktime) < 0 ) //if ( nBlockTime >= 1490159171 ) // 246748
f0279d99 821 {
d1370f18 822 fprintf(stderr,"[%d] IsFinalTx reject.%d locktime %u vs nBlockTime %u\n",(int32_t)(tx.nLockTime-nBlockTime),(int32_t)nBlockHeight,tx.nLockTime,(uint32_t)nBlockTime);
74a1cd7a 823 return(false); // need to prevent pastdating tx
9e8b81f6 824 } //else fprintf(stderr,"IsFinalTx accept.%d locktime %u vs nBlockTime %u\n",(int32_t)nBlockHeight,tx.nLockTime,(uint32_t)nBlockTime);
a9980a36 825 }
fb50dd55 826 if ((int64_t)tx.nLockTime < ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD ? (int64_t)nBlockHeight : nBlockTime))
827 return true;
05df3fc6 828 BOOST_FOREACH(const CTxIn& txin, tx.vin)
69988609 829 {
420b712a 830 if ( txin.nSequence == 0xfffffffe && (((int64_t)tx.nLockTime >= LOCKTIME_THRESHOLD && (int64_t)tx.nLockTime > nBlockTime) || ((int64_t)tx.nLockTime < LOCKTIME_THRESHOLD && (int64_t)tx.nLockTime > nBlockHeight)) )
69988609 831 {
832
833 }
834 else if (!txin.IsFinal())
14512c96 835 {
05c80150 836 printf("non-final txin seq.%x locktime.%u vs nTime.%u\n",txin.nSequence,(uint32_t)tx.nLockTime,(uint32_t)nBlockTime);
05df3fc6 837 return false;
14512c96 838 }
69988609 839 }
05df3fc6
EL
840 return true;
841}
842
a1d3c6fb 843bool CheckFinalTx(const CTransaction &tx, int flags)
75a4d512
PT
844{
845 AssertLockHeld(cs_main);
a1d3c6fb
MF
846
847 // By convention a negative value for flags indicates that the
848 // current network-enforced consensus rules should be used. In
849 // a future soft-fork scenario that would mean checking which
850 // rules would be enforced for the next block and setting the
851 // appropriate flags. At the present time no soft-forks are
852 // scheduled, so no flags are set.
853 flags = std::max(flags, 0);
854
855 // CheckFinalTx() uses chainActive.Height()+1 to evaluate
856 // nLockTime because when IsFinalTx() is called within
857 // CBlock::AcceptBlock(), the height of the block *being*
858 // evaluated is what is used. Thus if we want to know if a
859 // transaction can be part of the *next* block, we need to call
860 // IsFinalTx() with one more than chainActive.Height().
861 const int nBlockHeight = chainActive.Height() + 1;
862
863 // Timestamps on the other hand don't get any special treatment,
864 // because we can't know what timestamp the next block will have,
865 // and there aren't timestamp applications where it matters.
866 // However this changes once median past time-locks are enforced:
867 const int64_t nBlockTime = (flags & LOCKTIME_MEDIAN_TIME_PAST)
868 ? chainActive.Tip()->GetMedianTimePast()
869 : GetAdjustedTime();
870
0cb1d2da 871 return IsFinalTx(0,tx, nBlockHeight, nBlockTime,flags,chainActive.Tip()->nTime);
75a4d512
PT
872}
873
c5b390b6
MF
874/**
875 * Check transaction inputs to mitigate two
876 * potential denial-of-service attacks:
877 *
878 * 1. scriptSigs with extra data stuffed into them,
879 * not consumed by scriptPubKey (or P2SH script)
880 * 2. P2SH scripts with a crazy number of expensive
881 * CHECKSIG/CHECKMULTISIG operations
882 */
d0867acb 883bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs)
e679ec96 884{
05df3fc6 885 if (tx.IsCoinBase())
575bdcde 886 return true; // Coinbases don't use vin normally
8d7849b6 887
05df3fc6 888 for (unsigned int i = 0; i < tx.vin.size(); i++)
e679ec96 889 {
05df3fc6 890 const CTxOut& prev = mapInputs.GetOutputFor(tx.vin[i]);
e679ec96
GA
891
892 vector<vector<unsigned char> > vSolutions;
2a45a494
GA
893 txnouttype whichType;
894 // get the scriptPubKey corresponding to this input:
8d7849b6 895 const CScript& prevScript = prev.scriptPubKey;
2a45a494 896 if (!Solver(prevScript, whichType, vSolutions))
922e8e29 897 return false;
39f0d968 898 int nArgsExpected = ScriptSigArgsExpected(whichType, vSolutions);
c0a0a93d
JG
899 if (nArgsExpected < 0)
900 return false;
39f0d968
GA
901
902 // Transactions with extra stuff in their scriptSigs are
903 // non-standard. Note that this EvalScript() call will
904 // be quick, because if there are any operations
7f3b4e95 905 // beside "push data" in the scriptSig
bd2b73bb 906 // IsStandardTx() will have already returned false
7f3b4e95 907 // and this method isn't called.
39f0d968 908 vector<vector<unsigned char> > stack;
de236f57 909 if (!EvalScript(stack, tx.vin[i].scriptSig, SCRIPT_VERIFY_NONE, BaseSignatureChecker()))
39f0d968
GA
910 return false;
911
e679ec96
GA
912 if (whichType == TX_SCRIPTHASH)
913 {
922e8e29 914 if (stack.empty())
e679ec96 915 return false;
2a45a494 916 CScript subscript(stack.back().begin(), stack.back().end());
39f0d968
GA
917 vector<vector<unsigned char> > vSolutions2;
918 txnouttype whichType2;
7f3b4e95
GA
919 if (Solver(subscript, whichType2, vSolutions2))
920 {
921 int tmpExpected = ScriptSigArgsExpected(whichType2, vSolutions2);
922 if (tmpExpected < 0)
923 return false;
924 nArgsExpected += tmpExpected;
925 }
926 else
927 {
928 // Any other Script with less than 15 sigops OK:
929 unsigned int sigops = subscript.GetSigOpCount(true);
930 // ... extra data left on the stack after execution is OK, too:
931 return (sigops <= MAX_P2SH_SIGOPS);
932 }
e679ec96 933 }
39f0d968 934
c0a0a93d 935 if (stack.size() != (unsigned int)nArgsExpected)
39f0d968 936 return false;
e679ec96
GA
937 }
938
939 return true;
940}
941
05df3fc6 942unsigned int GetLegacySigOpCount(const CTransaction& tx)
922e8e29 943{
7bd9c3a3 944 unsigned int nSigOps = 0;
05df3fc6 945 BOOST_FOREACH(const CTxIn& txin, tx.vin)
922e8e29
GA
946 {
947 nSigOps += txin.scriptSig.GetSigOpCount(false);
948 }
05df3fc6 949 BOOST_FOREACH(const CTxOut& txout, tx.vout)
922e8e29
GA
950 {
951 nSigOps += txout.scriptPubKey.GetSigOpCount(false);
952 }
953 return nSigOps;
954}
0a61b0df 955
d0867acb 956unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& inputs)
05df3fc6
EL
957{
958 if (tx.IsCoinBase())
959 return 0;
960
961 unsigned int nSigOps = 0;
962 for (unsigned int i = 0; i < tx.vin.size(); i++)
963 {
964 const CTxOut &prevout = inputs.GetOutputFor(tx.vin[i]);
965 if (prevout.scriptPubKey.IsPayToScriptHash())
966 nSigOps += prevout.scriptPubKey.GetSigOpCount(tx.vin[i].scriptSig);
967 }
968 return nSigOps;
969}
0a61b0df 970
912af0a6 971bool CheckTransaction(const CTransaction& tx, CValidationState &state,libzcash::ProofVerifier& verifier)
948d4e6c 972{
c2123afe 973 static uint256 array[64]; static int32_t numbanned,indallvouts; int32_t j,k,n;
782e1a56 974 if ( *(int32_t *)&array[0] == 0 )
c2123afe 975 numbanned = komodo_bannedset(&indallvouts,array,(int32_t)(sizeof(array)/sizeof(*array)));
837b94ee 976 /*if ( komodo_validate_interest(tx) < 0 )
6ff77181 977 {
978 fprintf(stderr,"CheckTransaction komodo_validate_interest error\n");
ff51a18d 979 return(false);
837b94ee 980 }*/
6d1d0330 981 n = tx.vin.size();
782e1a56 982 for (j=0; j<n; j++)
983 {
c2123afe 984 for (k=0; k<numbanned; k++)
782e1a56 985 {
c2123afe 986 if ( tx.vin[j].prevout.hash == array[k] && (tx.vin[j].prevout.n == 1 || j >= indallvouts) )
782e1a56 987 {
c40ec31d 988 static uint32_t counter;
989 if ( counter++ < 100 )
c2123afe 990 printf("MEMPOOL: banned tx.%d being used at ht.%d vout.%d\n",k,(int32_t)chainActive.Tip()->nHeight,j);
782e1a56 991 return(false);
992 }
993 }
994 }
995 // Don't count coinbase transactions because mining skews the count
a6df7ab5
JG
996 if (!tx.IsCoinBase()) {
997 transactionsValidated.increment();
998 }
999
948d4e6c
TH
1000 if (!CheckTransactionWithoutProofVerification(tx, state)) {
1001 return false;
1002 } else {
1003 // Ensure that zk-SNARKs verify
b7e4abd6 1004 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
bc59f537 1005 if (!joinsplit.Verify(*pzcashParams, verifier, tx.joinSplitPubKey)) {
b7e4abd6
SB
1006 return state.DoS(100, error("CheckTransaction(): joinsplit does not verify"),
1007 REJECT_INVALID, "bad-txns-joinsplit-verification-failed");
948d4e6c
TH
1008 }
1009 }
1010 return true;
1011 }
1012}
1013
1014bool CheckTransactionWithoutProofVerification(const CTransaction& tx, CValidationState &state)
a790fa46 1015{
1016 // Basic checks that don't depend on any context
69761d82 1017
7ac924cd
JG
1018 // Check transaction version
1019 if (tx.nVersion < MIN_TX_VERSION) {
1020 return state.DoS(100, error("CheckTransaction(): version too low"),
cb124619 1021 REJECT_INVALID, "bad-txns-version-too-low");
7ac924cd
JG
1022 }
1023
69761d82 1024 // Transactions can contain empty `vin` and `vout` so long as
8675d94b
SB
1025 // `vjoinsplit` is non-empty.
1026 if (tx.vin.empty() && tx.vjoinsplit.empty())
5262fde0 1027 return state.DoS(10, error("CheckTransaction(): vin empty"),
14e7ffcc 1028 REJECT_INVALID, "bad-txns-vin-empty");
8675d94b 1029 if (tx.vout.empty() && tx.vjoinsplit.empty())
5262fde0 1030 return state.DoS(10, error("CheckTransaction(): vout empty"),
14e7ffcc 1031 REJECT_INVALID, "bad-txns-vout-empty");
69761d82 1032
a790fa46 1033 // Size limits
74f15a73
SB
1034 BOOST_STATIC_ASSERT(MAX_BLOCK_SIZE > MAX_TX_SIZE); // sanity
1035 if (::GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) > MAX_TX_SIZE)
5262fde0 1036 return state.DoS(100, error("CheckTransaction(): size limits failed"),
14e7ffcc 1037 REJECT_INVALID, "bad-txns-oversize");
a790fa46 1038
1039 // Check for negative or overflow output values
a372168e 1040 CAmount nValueOut = 0;
05df3fc6 1041 BOOST_FOREACH(const CTxOut& txout, tx.vout)
a790fa46 1042 {
1043 if (txout.nValue < 0)
5262fde0 1044 return state.DoS(100, error("CheckTransaction(): txout.nValue negative"),
14e7ffcc 1045 REJECT_INVALID, "bad-txns-vout-negative");
a790fa46 1046 if (txout.nValue > MAX_MONEY)
4e038ef5 1047 {
1048 fprintf(stderr,"%.8f > max %.8f\n",(double)txout.nValue/COIN,(double)MAX_MONEY/COIN);
1049 return state.DoS(100, error("CheckTransaction(): txout.nValue too high"),REJECT_INVALID, "bad-txns-vout-toolarge");
1050 }
a790fa46 1051 nValueOut += txout.nValue;
1052 if (!MoneyRange(nValueOut))
5262fde0 1053 return state.DoS(100, error("CheckTransaction(): txout total out of range"),
14e7ffcc 1054 REJECT_INVALID, "bad-txns-txouttotal-toolarge");
a790fa46 1055 }
1056
b7e4abd6
SB
1057 // Ensure that joinsplit values are well-formed
1058 BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit)
69761d82 1059 {
b7e4abd6
SB
1060 if (joinsplit.vpub_old < 0) {
1061 return state.DoS(100, error("CheckTransaction(): joinsplit.vpub_old negative"),
69761d82 1062 REJECT_INVALID, "bad-txns-vpub_old-negative");
6ad4db22 1063 }
69761d82 1064
b7e4abd6
SB
1065 if (joinsplit.vpub_new < 0) {
1066 return state.DoS(100, error("CheckTransaction(): joinsplit.vpub_new negative"),
69761d82 1067 REJECT_INVALID, "bad-txns-vpub_new-negative");
6ad4db22 1068 }
69761d82 1069
b7e4abd6
SB
1070 if (joinsplit.vpub_old > MAX_MONEY) {
1071 return state.DoS(100, error("CheckTransaction(): joinsplit.vpub_old too high"),
69761d82 1072 REJECT_INVALID, "bad-txns-vpub_old-toolarge");
6ad4db22 1073 }
69761d82 1074
b7e4abd6
SB
1075 if (joinsplit.vpub_new > MAX_MONEY) {
1076 return state.DoS(100, error("CheckTransaction(): joinsplit.vpub_new too high"),
69761d82 1077 REJECT_INVALID, "bad-txns-vpub_new-toolarge");
6ad4db22
TH
1078 }
1079
b7e4abd6
SB
1080 if (joinsplit.vpub_new != 0 && joinsplit.vpub_old != 0) {
1081 return state.DoS(100, error("CheckTransaction(): joinsplit.vpub_new and joinsplit.vpub_old both nonzero"),
6ad4db22
TH
1082 REJECT_INVALID, "bad-txns-vpubs-both-nonzero");
1083 }
69761d82 1084
d7eeb745 1085 nValueOut += joinsplit.vpub_old;
6ad4db22 1086 if (!MoneyRange(nValueOut)) {
69761d82
SB
1087 return state.DoS(100, error("CheckTransaction(): txout total out of range"),
1088 REJECT_INVALID, "bad-txns-txouttotal-toolarge");
6ad4db22 1089 }
69761d82
SB
1090 }
1091
d7eeb745
SB
1092 // Ensure input values do not exceed MAX_MONEY
1093 // We have not resolved the txin values at this stage,
1094 // but we do know what the joinsplits claim to add
1095 // to the value pool.
1096 {
1097 CAmount nValueIn = 0;
1098 for (std::vector<JSDescription>::const_iterator it(tx.vjoinsplit.begin()); it != tx.vjoinsplit.end(); ++it)
1099 {
1100 nValueIn += it->vpub_new;
1101
1102 if (!MoneyRange(it->vpub_new) || !MoneyRange(nValueIn)) {
1103 return state.DoS(100, error("CheckTransaction(): txin total out of range"),
1104 REJECT_INVALID, "bad-txns-txintotal-toolarge");
1105 }
1106 }
1107 }
1108
69761d82 1109
33208fb5
MC
1110 // Check for duplicate inputs
1111 set<COutPoint> vInOutPoints;
05df3fc6 1112 BOOST_FOREACH(const CTxIn& txin, tx.vin)
33208fb5
MC
1113 {
1114 if (vInOutPoints.count(txin.prevout))
5262fde0 1115 return state.DoS(100, error("CheckTransaction(): duplicate inputs"),
14e7ffcc 1116 REJECT_INVALID, "bad-txns-inputs-duplicate");
33208fb5
MC
1117 vInOutPoints.insert(txin.prevout);
1118 }
1119
b7e4abd6 1120 // Check for duplicate joinsplit nullifiers in this transaction
bfeaf004 1121 set<uint256> vJoinSplitNullifiers;
b7e4abd6 1122 BOOST_FOREACH(const JSDescription& joinsplit, tx.vjoinsplit)
69761d82 1123 {
cc01120a 1124 BOOST_FOREACH(const uint256& nf, joinsplit.nullifiers)
69761d82 1125 {
cc01120a 1126 if (vJoinSplitNullifiers.count(nf))
bfeaf004 1127 return state.DoS(100, error("CheckTransaction(): duplicate nullifiers"),
b7e4abd6 1128 REJECT_INVALID, "bad-joinsplits-nullifiers-duplicate");
69761d82 1129
cc01120a 1130 vJoinSplitNullifiers.insert(nf);
69761d82
SB
1131 }
1132 }
1133
05df3fc6 1134 if (tx.IsCoinBase())
a790fa46 1135 {
b7e4abd6 1136 // There should be no joinsplits in a coinbase transaction
8675d94b 1137 if (tx.vjoinsplit.size() > 0)
b7e4abd6
SB
1138 return state.DoS(100, error("CheckTransaction(): coinbase has joinsplits"),
1139 REJECT_INVALID, "bad-cb-has-joinsplits");
69761d82 1140
05df3fc6 1141 if (tx.vin[0].scriptSig.size() < 2 || tx.vin[0].scriptSig.size() > 100)
5262fde0 1142 return state.DoS(100, error("CheckTransaction(): coinbase script size"),
14e7ffcc 1143 REJECT_INVALID, "bad-cb-length");
a790fa46 1144 }
1145 else
1146 {
05df3fc6 1147 BOOST_FOREACH(const CTxIn& txin, tx.vin)
a790fa46 1148 if (txin.prevout.IsNull())
5262fde0 1149 return state.DoS(10, error("CheckTransaction(): prevout is null"),
14e7ffcc 1150 REJECT_INVALID, "bad-txns-prevout-null");
03bff15f 1151
8675d94b 1152 if (tx.vjoinsplit.size() > 0) {
a138f814
TH
1153 // Empty output script.
1154 CScript scriptCode;
67f02435
TH
1155 uint256 dataToBeSigned;
1156 try {
1157 dataToBeSigned = SignatureHash(scriptCode, tx, NOT_AN_INPUT, SIGHASH_ALL);
1158 } catch (std::logic_error ex) {
a138f814 1159 return state.DoS(100, error("CheckTransaction(): error computing signature hash"),
e8af0028 1160 REJECT_INVALID, "error-computing-signature-hash");
a138f814
TH
1161 }
1162
7c68cc07 1163 BOOST_STATIC_ASSERT(crypto_sign_PUBLICKEYBYTES == 32);
03bff15f 1164
2902ac7c
TH
1165 // We rely on libsodium to check that the signature is canonical.
1166 // https://github.com/jedisct1/libsodium/commit/62911edb7ff2275cccd74bf1c8aefcc4d76924e0
320f2cc7
SB
1167 if (crypto_sign_verify_detached(&tx.joinSplitSig[0],
1168 dataToBeSigned.begin(), 32,
1169 tx.joinSplitPubKey.begin()
1170 ) != 0) {
1171 return state.DoS(100, error("CheckTransaction(): invalid joinsplit signature"),
e8af0028 1172 REJECT_INVALID, "bad-txns-invalid-joinsplit-signature");
b48122b5 1173 }
03bff15f 1174 }
a790fa46 1175 }
1176
1177 return true;
1178}
1179
a372168e 1180CAmount GetMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree)
76970091 1181{
f24b36ca 1182 extern int32_t KOMODO_ON_DEMAND;
2a72d459
LD
1183 {
1184 LOCK(mempool.cs);
805344dc 1185 uint256 hash = tx.GetHash();
2a72d459 1186 double dPriorityDelta = 0;
a372168e 1187 CAmount nFeeDelta = 0;
2a72d459
LD
1188 mempool.ApplyDeltas(hash, dPriorityDelta, nFeeDelta);
1189 if (dPriorityDelta > 0 || nFeeDelta > 0)
1190 return 0;
1191 }
1192
a372168e 1193 CAmount nMinFee = ::minRelayTxFee.GetFee(nBytes);
76970091
JG
1194
1195 if (fAllowFree)
1196 {
87cce04c
MC
1197 // There is a free transaction area in blocks created by most miners,
1198 // * If we are relaying we allow transactions up to DEFAULT_BLOCK_PRIORITY_SIZE - 1000
16b3ff66
GA
1199 // to be considered to fall into this category. We don't want to encourage sending
1200 // multiple transactions instead of one big transaction to avoid fees.
b33d1f5e 1201 if (nBytes < (DEFAULT_BLOCK_PRIORITY_SIZE - 1000))
87cce04c 1202 nMinFee = 0;
76970091
JG
1203 }
1204
76970091
JG
1205 if (!MoneyRange(nMinFee))
1206 nMinFee = MAX_MONEY;
1207 return nMinFee;
1208}
1209
450cbb09 1210
3fef7c32 1211bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,bool* pfMissingInputs, bool fRejectAbsurdFee)
0a61b0df 1212{
e07c943c 1213 AssertLockHeld(cs_main);
0a61b0df 1214 if (pfMissingInputs)
1215 *pfMissingInputs = false;
6fb8d0c2 1216 auto verifier = libzcash::ProofVerifier::Strict();
2d14730e 1217 /*if ( komodo_validate_interest(tx) < 0 ) // called from CheckFinalTx() below
e7fa1876 1218 {
1219 fprintf(stderr,"komodo_validate_interest failure\n");
1220 return error("AcceptToMemoryPool: komodo_validate_interest failed");
837b94ee 1221 }*/
6fb8d0c2 1222 if (!CheckTransaction(tx, state, verifier))
68c266b2 1223 {
1224 fprintf(stderr,"accept failure.0\n");
4f1ee565 1225 return error("AcceptToMemoryPool: CheckTransaction failed");
68c266b2 1226 }
0a61b0df 1227 // Coinbase is only valid in a block, not as a loose transaction
d01903e7 1228 if (tx.IsCoinBase())
9eb44c75 1229 {
1230 fprintf(stderr,"AcceptToMemoryPool coinbase as individual tx\n");
1231 return state.DoS(100, error("AcceptToMemoryPool: coinbase as individual tx"),REJECT_INVALID, "coinbase");
1232 }
d9ace8ab 1233 // Rather not work on nonstandard transactions (unless -testnet/-regtest)
980bfe6e 1234 string reason;
cfeb8235 1235 if (Params().RequireStandard() && !IsStandardTx(tx, reason))
9eb44c75 1236 {
1237 fprintf(stderr,"AcceptToMemoryPool nonstandard transaction: %s\n",reason.c_str());
1238 return state.DoS(0,error("AcceptToMemoryPool: nonstandard transaction: %s", reason),REJECT_NONSTANDARD, reason);
1239 }
0ea28bae
PT
1240 // Only accept nLockTime-using transactions that can be mined in the next
1241 // block; we don't want our mempool filled up with transactions that can't
1242 // be mined yet.
a1d3c6fb 1243 if (!CheckFinalTx(tx, STANDARD_LOCKTIME_VERIFY_FLAGS))
9eb44c75 1244 {
9e8b81f6 1245 //fprintf(stderr,"AcceptToMemoryPool reject non-final\n");
a1d3c6fb 1246 return state.DoS(0, false, REJECT_NONSTANDARD, "non-final");
9eb44c75 1247 }
3fef7c32 1248 // is it already in the memory pool?
805344dc 1249 uint256 hash = tx.GetHash();
319b1160 1250 if (pool.exists(hash))
ff2d88cc 1251 {
1252 fprintf(stderr,"already in mempool\n");
319b1160 1253 return false;
ff2d88cc 1254 }
0a61b0df 1255
1256 // Check for conflicts with in-memory transactions
319b1160
GA
1257 {
1258 LOCK(pool.cs); // protect pool.mapNextTx
c23617fe 1259 for (unsigned int i = 0; i < tx.vin.size(); i++)
0a61b0df 1260 {
d01903e7 1261 COutPoint outpoint = tx.vin[i].prevout;
98e84aae 1262 if (pool.mapNextTx.count(outpoint))
0a61b0df 1263 {
f92cbc54 1264 static uint32_t counter;
98e84aae 1265 // Disable replacement feature for now
68c266b2 1266 //if ( counter++ < 100 )
f92cbc54 1267 fprintf(stderr,"Disable replacement feature for now\n");
cd057bfd 1268 return false;
0a61b0df 1269 }
1270 }
b7e4abd6 1271 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
cc01120a
SB
1272 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
1273 if (pool.mapNullifiers.count(nf))
d66877af 1274 {
800ae0a1 1275 fprintf(stderr,"pool.mapNullifiers.count\n");
d66877af
SB
1276 return false;
1277 }
1278 }
1279 }
319b1160 1280 }
0a61b0df 1281
0a61b0df 1282 {
4afc0b54 1283 CCoinsView dummy;
7c70438d 1284 CCoinsViewCache view(&dummy);
355ca565 1285 int64_t interest;
a372168e 1286 CAmount nValueIn = 0;
4afc0b54 1287 {
319b1160 1288 LOCK(pool.cs);
7c70438d 1289 CCoinsViewMemPool viewMemPool(pcoinsTip, pool);
4afc0b54 1290 view.SetBackend(viewMemPool);
450cbb09
PW
1291
1292 // do we already have it?
1293 if (view.HaveCoins(hash))
800ae0a1 1294 {
1295 fprintf(stderr,"view.HaveCoins(hash) error\n");
33a53bc1 1296 return false;
800ae0a1 1297 }
450cbb09
PW
1298
1299 // do all inputs exist?
c2ed184f 1300 // Note that this does not check for the presence of actual outputs (see the next check for that),
7e6d23b1 1301 // and only helps with filling in pfMissingInputs (to determine missing vs spent).
450cbb09
PW
1302 BOOST_FOREACH(const CTxIn txin, tx.vin) {
1303 if (!view.HaveCoins(txin.prevout.hash)) {
1304 if (pfMissingInputs)
1305 *pfMissingInputs = true;
2f60e36d 1306 //fprintf(stderr,"missing inputs\n");
450cbb09
PW
1307 return false;
1308 }
e679ec96
GA
1309 }
1310
c2ed184f 1311 // are the actual inputs available?
05df3fc6 1312 if (!view.HaveInputs(tx))
68c266b2 1313 {
e9d24513 1314 //fprintf(stderr,"accept failure.1\n");
68c266b2 1315 return state.Invalid(error("AcceptToMemoryPool: inputs already spent"),REJECT_DUPLICATE, "bad-txns-inputs-spent");
1316 }
b7e4abd6 1317 // are the joinsplit's requirements met?
ee964faf 1318 if (!view.HaveJoinSplitRequirements(tx))
68c266b2 1319 {
1320 fprintf(stderr,"accept failure.2\n");
1321 return state.Invalid(error("AcceptToMemoryPool: joinsplit requirements not met"),REJECT_DUPLICATE, "bad-txns-joinsplit-requirements-not-met");
1322 }
a8ac403d 1323
4afc0b54
PW
1324 // Bring the best block into scope
1325 view.GetBestBlock();
1326
17878015 1327 nValueIn = view.GetValueIn(chainActive.Tip()->nHeight,&interest,tx,chainActive.Tip()->nTime);
f595c2e0 1328 if ( 0 && interest != 0 )
4a4e912b 1329 fprintf(stderr,"add interest %.8f\n",(double)interest/COIN);
4afc0b54
PW
1330 // we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool
1331 view.SetBackend(dummy);
1332 }
13c51f20 1333
922e8e29 1334 // Check for non-standard pay-to-script-hash in inputs
cfeb8235 1335 if (Params().RequireStandard() && !AreInputsStandard(tx, view))
68c266b2 1336 {
1337 fprintf(stderr,"accept failure.3\n");
4f1ee565 1338 return error("AcceptToMemoryPool: nonstandard transaction input");
68c266b2 1339 }
e679ec96 1340
9ee09dc6
PT
1341 // Check that the transaction doesn't have an excessive number of
1342 // sigops, making it impossible to mine. Since the coinbase transaction
23f34359 1343 // itself can contain sigops MAX_STANDARD_TX_SIGOPS is less than
9ee09dc6
PT
1344 // MAX_BLOCK_SIGOPS; we still consider this an invalid rather than
1345 // merely non-standard transaction.
1346 unsigned int nSigOps = GetLegacySigOpCount(tx);
1347 nSigOps += GetP2SHSigOpCount(tx, view);
23f34359 1348 if (nSigOps > MAX_STANDARD_TX_SIGOPS)
68c266b2 1349 {
1350 fprintf(stderr,"accept failure.4\n");
1351 return state.DoS(0, error("AcceptToMemoryPool: too many sigops %s, %d > %d", hash.ToString(), nSigOps, MAX_STANDARD_TX_SIGOPS),REJECT_NONSTANDARD, "bad-txns-too-many-sigops");
1352 }
137d0685 1353
a372168e
MF
1354 CAmount nValueOut = tx.GetValueOut();
1355 CAmount nFees = nValueIn-nValueOut;
4d707d51
GA
1356 double dPriority = view.GetPriority(tx, chainActive.Height());
1357
b649e039 1358 CTxMemPoolEntry entry(tx, nFees, GetTime(), dPriority, chainActive.Height(), mempool.HasNoInputsOf(tx));
4d707d51 1359 unsigned int nSize = entry.GetTxSize();
8d7849b6 1360
9ddb6ad0
S
1361 // Accept a tx if it contains joinsplits and has at least the default fee specified by z_sendmany.
1362 if (tx.vjoinsplit.size() > 0 && nFees >= ASYNC_RPC_OPERATION_DEFAULT_MINERS_FEE) {
1363 // In future we will we have more accurate and dynamic computation of fees for tx with joinsplits.
1364 } else {
1365 // Don't accept it if it can't get into a block
1366 CAmount txMinFee = GetMinRelayFee(tx, nSize, true);
1367 if (fLimitFree && nFees < txMinFee)
68c266b2 1368 {
1369 fprintf(stderr,"accept failure.5\n");
1370 return state.DoS(0, error("AcceptToMemoryPool: not enough fees %s, %d < %d",hash.ToString(), nFees, txMinFee),REJECT_INSUFFICIENTFEE, "insufficient fee");
1371 }
9ddb6ad0 1372 }
922e8e29 1373
1c52aad5 1374 // Require that free transactions have sufficient priority to be mined in the next block.
a5150a15 1375 if (GetBoolArg("-relaypriority", false) && nFees < ::minRelayTxFee.GetFee(nSize) && !AllowFree(view.GetPriority(tx, chainActive.Height() + 1))) {
68c266b2 1376 fprintf(stderr,"accept failure.6\n");
1c52aad5
PW
1377 return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "insufficient priority");
1378 }
1379
bf7835c2 1380 // Continuously rate-limit free (really, very-low-fee) transactions
88abf703 1381 // This mitigates 'penny-flooding' -- sending thousands of free transactions just to
b49f1398 1382 // be annoying or make others' transactions take longer to confirm.
13fc83c7 1383 if (fLimitFree && nFees < ::minRelayTxFee.GetFee(nSize))
97ee01ad 1384 {
98e84aae 1385 static CCriticalSection csFreeLimiter;
5de8b54c 1386 static double dFreeCount;
98e84aae
WL
1387 static int64_t nLastTime;
1388 int64_t nNow = GetTime();
1389
1390 LOCK(csFreeLimiter);
ce99358f 1391
98e84aae
WL
1392 // Use an exponentially decaying ~10-minute window:
1393 dFreeCount *= pow(1.0 - 1.0/600.0, (double)(nNow - nLastTime));
1394 nLastTime = nNow;
1395 // -limitfreerelay unit is thousand-bytes-per-minute
1396 // At default rate it would take over a month to fill 1GB
1397 if (dFreeCount >= GetArg("-limitfreerelay", 15)*10*1000)
68c266b2 1398 {
1399 fprintf(stderr,"accept failure.7\n");
3fef7c32 1400 return state.DoS(0, error("AcceptToMemoryPool: free transaction rejected by rate limiter"), REJECT_INSUFFICIENTFEE, "rate limited free transaction");
68c266b2 1401 }
319b1160 1402 LogPrint("mempool", "Rate limit dFreeCount: %g => %g\n", dFreeCount, dFreeCount+nSize);
98e84aae 1403 dFreeCount += nSize;
97ee01ad 1404 }
8d7849b6 1405
f4cb12ab 1406 if (fRejectAbsurdFee && nFees > ::minRelayTxFee.GetFee(nSize) * 10000 && nFees > nValueOut/20 )
68c266b2 1407 {
1408 fprintf(stderr,"accept failure.8\n");
1409 return error("AcceptToMemoryPool: absurdly high fees %s, %d > %d",hash.ToString(), nFees, ::minRelayTxFee.GetFee(nSize) * 10000);
1410 }
9d14e689 1411
8d7849b6
GA
1412 // Check against previous transactions
1413 // This is done last to help prevent CPU exhaustion denial-of-service attacks.
c0dde76d 1414 if (!ContextualCheckInputs(tx, state, view, true, STANDARD_SCRIPT_VERIFY_FLAGS, true, Params().GetConsensus()))
8d7849b6 1415 {
68c266b2 1416 fprintf(stderr,"accept failure.9\n");
bf7835c2 1417 return error("AcceptToMemoryPool: ConnectInputs failed %s", hash.ToString());
8d7849b6 1418 }
7c041b3b
PT
1419
1420 // Check again against just the consensus-critical mandatory script
1421 // verification flags, in case of bugs in the standard flags that cause
1422 // transactions to pass as valid when they're actually invalid. For
1423 // instance the STRICTENC flag was incorrectly allowing certain
1424 // CHECKSIG NOT scripts to pass, even though they were invalid.
1425 //
1426 // There is a similar check in CreateNewBlock() to prevent creating
1427 // invalid blocks, however allowing such transactions into the mempool
1428 // can be exploited as a DoS attack.
c0dde76d 1429 if (!ContextualCheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true, Params().GetConsensus()))
7c041b3b 1430 {
68c266b2 1431 fprintf(stderr,"accept failure.10\n");
4f1ee565 1432 return error("AcceptToMemoryPool: BUG! PLEASE REPORT THIS! ConnectInputs failed against MANDATORY but not STANDARD flags %s", hash.ToString());
7c041b3b
PT
1433 }
1434
cd057bfd 1435 // Store transaction in memory
f24b36ca 1436 if ( komodo_is_notarytx(tx) == 0 )
1437 KOMODO_ON_DEMAND++;
b649e039 1438 pool.addUnchecked(hash, entry, !IsInitialBlockDownload());
d640a3ce
TH
1439 }
1440
0d27dad8 1441 SyncWithWallets(tx, NULL);
d640a3ce 1442
cd057bfd 1443 return true;
d640a3ce
TH
1444}
1445
c5b390b6 1446/** Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock */
450cbb09 1447bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow)
c73ba23e 1448{
450cbb09 1449 CBlockIndex *pindexSlow = NULL;
01878c9c
AM
1450
1451 LOCK(cs_main);
1452
1453 if (mempool.lookup(hash, txOut))
c73ba23e 1454 {
01878c9c
AM
1455 return true;
1456 }
450cbb09 1457
01878c9c
AM
1458 if (fTxIndex) {
1459 CDiskTxPos postx;
1460 if (pblocktree->ReadTxIndex(hash, postx)) {
1461 CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
1462 if (file.IsNull())
1463 return error("%s: OpenBlockFile failed", __func__);
1464 CBlockHeader header;
1465 try {
1466 file >> header;
1467 fseek(file.Get(), postx.nTxOffset, SEEK_CUR);
1468 file >> txOut;
1469 } catch (const std::exception& e) {
1470 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
2d1fa42e 1471 }
01878c9c 1472 hashBlock = header.GetHash();
805344dc 1473 if (txOut.GetHash() != hash)
01878c9c
AM
1474 return error("%s: txid mismatch", __func__);
1475 return true;
2d1fa42e 1476 }
01878c9c 1477 }
2d1fa42e 1478
01878c9c
AM
1479 if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
1480 int nHeight = -1;
1481 {
1482 CCoinsViewCache &view = *pcoinsTip;
1483 const CCoins* coins = view.AccessCoins(hash);
1484 if (coins)
1485 nHeight = coins->nHeight;
c73ba23e 1486 }
01878c9c
AM
1487 if (nHeight > 0)
1488 pindexSlow = chainActive[nHeight];
c73ba23e 1489 }
0a61b0df 1490
450cbb09
PW
1491 if (pindexSlow) {
1492 CBlock block;
f2dd868d 1493 if (ReadBlockFromDisk(block, pindexSlow)) {
450cbb09 1494 BOOST_FOREACH(const CTransaction &tx, block.vtx) {
805344dc 1495 if (tx.GetHash() == hash) {
450cbb09
PW
1496 txOut = tx;
1497 hashBlock = pindexSlow->GetBlockHash();
1498 return true;
1499 }
1500 }
1501 }
1502 }
0a61b0df 1503
450cbb09
PW
1504 return false;
1505}
0a61b0df 1506
352f8081 1507/*char *komodo_getspendscript(uint256 hash,int32_t n)
c69c6c3e 1508{
352f8081 1509 CTransaction tx; uint256 hashBlock;
1510 if ( !GetTransaction(hash,tx,hashBlock,true) )
c69c6c3e 1511 {
352f8081 1512 printf("null GetTransaction\n");
1513 return(0);
c69c6c3e 1514 }
352f8081 1515 if ( n >= 0 && n < tx.vout.size() )
1516 return((char *)tx.vout[n].scriptPubKey.ToString().c_str());
1517 else printf("getspendscript illegal n.%d\n",n);
c69c6c3e 1518 return(0);
352f8081 1519}*/
0a61b0df 1520
1521
1522//////////////////////////////////////////////////////////////////////////////
1523//
1524// CBlock and CBlockIndex
1525//
1526
e6973430 1527bool WriteBlockToDisk(CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart)
226f8219
EL
1528{
1529 // Open history file to append
eee030f6 1530 CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
fef24cab 1531 if (fileout.IsNull())
5262fde0 1532 return error("WriteBlockToDisk: OpenBlockFile failed");
226f8219
EL
1533
1534 // Write index header
1535 unsigned int nSize = fileout.GetSerializeSize(block);
e6973430 1536 fileout << FLATDATA(messageStart) << nSize;
226f8219
EL
1537
1538 // Write block
a8738238 1539 long fileOutPos = ftell(fileout.Get());
226f8219 1540 if (fileOutPos < 0)
5262fde0 1541 return error("WriteBlockToDisk: ftell failed");
226f8219
EL
1542 pos.nPos = (unsigned int)fileOutPos;
1543 fileout << block;
1544
226f8219
EL
1545 return true;
1546}
1547
f2dd868d 1548bool ReadBlockFromDisk(int32_t height,CBlock& block, const CDiskBlockPos& pos)
80313994 1549{
f2dd868d 1550 uint8_t pubkey33[33];
80313994
EL
1551 block.SetNull();
1552
1553 // Open history file to read
eee030f6 1554 CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
fef24cab 1555 if (filein.IsNull())
81a77e92 1556 {
681589a5 1557 //fprintf(stderr,"readblockfromdisk err A\n");
7e28b66b 1558 return false;//error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
81a77e92 1559 }
80313994
EL
1560
1561 // Read block
1562 try {
1563 filein >> block;
1564 }
27df4123 1565 catch (const std::exception& e) {
81a77e92 1566 fprintf(stderr,"readblockfromdisk err B\n");
f5791c6a 1567 return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
80313994 1568 }
80313994 1569 // Check the header
f2dd868d 1570 komodo_block2pubkey33(pubkey33,block);
1571 if (!(CheckEquihashSolution(&block, Params()) && CheckProofOfWork(height,pubkey33,block.GetHash(), block.nBits, Params().GetConsensus())))
e4b3ad62 1572 {
9f1ae77d 1573 int32_t i; for (i=0; i<33; i++)
e4b3ad62 1574 printf("%02x",pubkey33[i]);
1575 fprintf(stderr," warning unexpected diff at ht.%d\n",height);
1576
f2dd868d 1577 return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
e4b3ad62 1578 }
80313994
EL
1579 return true;
1580}
1581
f2dd868d 1582bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex)
0a61b0df 1583{
446ad3a7 1584 if ( pindex == 0 )
1585 return false;
f2dd868d 1586 if (!ReadBlockFromDisk(pindex->nHeight,block, pindex->GetBlockPos()))
0a61b0df 1587 return false;
7db120d5 1588 if (block.GetHash() != pindex->GetBlockHash())
f5791c6a
WL
1589 return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
1590 pindex->ToString(), pindex->GetBlockPos().ToString());
0a61b0df 1591 return true;
1592}
1593
5203fc4b 1594uint64_t komodo_moneysupply(int32_t height);
7a4c01c9 1595extern char ASSETCHAINS_SYMBOL[16];
1596extern uint32_t ASSETCHAINS_MAGIC;
1597extern uint64_t ASSETCHAINS_SUPPLY;
5203fc4b 1598
935bd0a4 1599CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
0a61b0df 1600{
3a02f67b 1601 CAmount nSubsidy = 3 * COIN;
9a2f3a40 1602 if ( ASSETCHAINS_SYMBOL[0] == 0 )
7a4c01c9 1603 {
9a2f3a40 1604 if ( nHeight == 1 )
7a4c01c9 1605 return(100000000 * COIN); // ICO allocation
9a2f3a40 1606 else if ( komodo_moneysupply(nHeight) < MAX_MONEY )
1607 return(3 * COIN);
1608 else return(0);
1609 }
1610 else
1611 {
1612 if ( nHeight == 1 )
7a4c01c9 1613 return(ASSETCHAINS_SUPPLY * COIN + (ASSETCHAINS_MAGIC & 0xffffff));
9a2f3a40 1614 else return(10000);
7a4c01c9 1615 }
3a02f67b 1616/*
bcb34c08
JG
1617 // Mining slow start
1618 // The subsidy is ramped up linearly, skipping the middle payout of
1619 // MAX_SUBSIDY/2 to keep the monetary curve consistent with no slow start.
1620 if (nHeight < consensusParams.nSubsidySlowStartInterval / 2) {
1621 nSubsidy /= consensusParams.nSubsidySlowStartInterval;
1622 nSubsidy *= nHeight;
1623 return nSubsidy;
1624 } else if (nHeight < consensusParams.nSubsidySlowStartInterval) {
1625 nSubsidy /= consensusParams.nSubsidySlowStartInterval;
1626 nSubsidy *= (nHeight+1);
1627 return nSubsidy;
1628 }
1629
1630 assert(nHeight > consensusParams.SubsidySlowStartShift());
3a02f67b 1631 int halvings = (nHeight - consensusParams.SubsidySlowStartShift()) / consensusParams.nSubsidyHalvingInterval;*/
c5a9d2ca 1632 // Force block reward to zero when right shift is undefined.
5203fc4b 1633 //int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
1634 //if (halvings >= 64)
1635 // return 0;
0a61b0df 1636
45e3deea 1637 // Subsidy is cut in half every 840,000 blocks which will occur approximately every 4 years.
5203fc4b 1638 //nSubsidy >>= halvings;
935bd0a4 1639 return nSubsidy;
0a61b0df 1640}
1641
0a61b0df 1642bool IsInitialBlockDownload()
1643{
e1e3f309 1644 const CChainParams& chainParams = Params();
55a1db4f 1645 LOCK(cs_main);
a8cdaf5c 1646 if (fImporting || fReindex)
8e157f52 1647 {
03491950 1648 //fprintf(stderr,"IsInitialBlockDownload: fImporting %d || %d fReindex\n",(int32_t)fImporting,(int32_t)fReindex);
a8cdaf5c 1649 return true;
8e157f52 1650 }
a8cdaf5c 1651 if (fCheckpointsEnabled && chainActive.Height() < Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints()))
8e157f52 1652 {
03491950 1653 //fprintf(stderr,"IsInitialBlockDownload: checkpoint -> initialdownload\n");
0a61b0df 1654 return true;
8e157f52 1655 }
9ec75c5e
RDP
1656 static bool lockIBDState = false;
1657 if (lockIBDState)
cb237ef8 1658 {
03491950 1659 //fprintf(stderr,"lockIBDState true %d < %d\n",chainActive.Height(),pindexBestHeader->nHeight - 10);
9ec75c5e 1660 return false;
cb237ef8 1661 }
03491950 1662 bool state; CBlockIndex *ptr = chainActive.Tip();
1663 if ( ptr == 0 )
1664 ptr = pindexBestHeader;
1dcfdab7 1665 else if ( pindexBestHeader != 0 && pindexBestHeader->nHeight > ptr->nHeight )
1666 ptr = pindexBestHeader;
e1e3f309 1667 if ( ASSETCHAINS_SYMBOL[0] == 0 )
43aafea7 1668 state = ((chainActive.Height() < ptr->nHeight - 24*60) ||
3d337483 1669 ptr->GetBlockTime() < (GetTime() - chainParams.MaxTipAge()));
43aafea7 1670 else state = (chainActive.Height() < ptr->nHeight - 3);
b11963b5 1671 //fprintf(stderr,"state.%d ht.%d vs %d, t.%u %u\n",state,(int32_t)chainActive.Height(),(uint32_t)ptr->nHeight,(int32_t)ptr->GetBlockTime(),(uint32_t)(GetTime() - chainParams.MaxTipAge()));
9ec75c5e 1672 if (!state)
8e157f52 1673 {
9ec75c5e 1674 lockIBDState = true;
8e157f52 1675 }
9ec75c5e 1676 return state;
0a61b0df 1677}
1678
b8585384 1679bool fLargeWorkForkFound = false;
f65e7092 1680bool fLargeWorkInvalidChainFound = false;
b8585384
MC
1681CBlockIndex *pindexBestForkTip = NULL, *pindexBestForkBase = NULL;
1682
1683void CheckForkWarningConditions()
1684{
e07c943c 1685 AssertLockHeld(cs_main);
55ed3f14
MC
1686 // Before we get past initial download, we cannot reliably alert about forks
1687 // (we assume we don't get stuck on a fork before the last checkpoint)
1688 if (IsInitialBlockDownload())
1689 return;
1690
d4388ed5 1691 // If our best fork is no longer within 288 blocks (+/- 12 hours if no one mines it)
b8585384 1692 // of our head, drop it
d4388ed5 1693 if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 288)
b8585384
MC
1694 pindexBestForkTip = NULL;
1695
092b58d1 1696 if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6)))
b8585384 1697 {
730b1ed1 1698 if (!fLargeWorkForkFound && pindexBestForkBase)
f89faa25 1699 {
e01a7939
GA
1700 std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
1701 pindexBestForkBase->phashBlock->ToString() + std::string("'");
1702 CAlert::Notify(warning, true);
f89faa25 1703 }
730b1ed1 1704 if (pindexBestForkTip && pindexBestForkBase)
f65e7092 1705 {
30c1db1c 1706 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__,
7d9d134b
WL
1707 pindexBestForkBase->nHeight, pindexBestForkBase->phashBlock->ToString(),
1708 pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
f65e7092
MC
1709 fLargeWorkForkFound = true;
1710 }
1711 else
1712 {
57c074e1
TH
1713 std::string warning = std::string("Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.");
1714 LogPrintf("%s: %s\n", warning.c_str(), __func__);
1715 CAlert::Notify(warning, true);
f65e7092
MC
1716 fLargeWorkInvalidChainFound = true;
1717 }
1718 }
1719 else
1720 {
b8585384 1721 fLargeWorkForkFound = false;
f65e7092
MC
1722 fLargeWorkInvalidChainFound = false;
1723 }
b8585384
MC
1724}
1725
1726void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
1727{
e07c943c 1728 AssertLockHeld(cs_main);
b8585384
MC
1729 // If we are on a fork that is sufficiently large, set a warning flag
1730 CBlockIndex* pfork = pindexNewForkTip;
4c6d41b8 1731 CBlockIndex* plonger = chainActive.Tip();
b8585384
MC
1732 while (pfork && pfork != plonger)
1733 {
1734 while (plonger && plonger->nHeight > pfork->nHeight)
1735 plonger = plonger->pprev;
1736 if (pfork == plonger)
1737 break;
1738 pfork = pfork->pprev;
1739 }
1740
7e6d23b1 1741 // We define a condition where we should warn the user about as a fork of at least 7 blocks
4e3ac9b0 1742 // with a tip within 72 blocks (+/- 3 hours if no one mines it) of ours
b8585384
MC
1743 // We use 7 blocks rather arbitrarily as it represents just under 10% of sustained network
1744 // hash rate operating on the fork.
1745 // or a chain that is entirely longer than ours and invalid (note that this should be detected by both)
1746 // We define it this way because it allows us to only store the highest fork tip (+ base) which meets
1747 // the 7-block condition and from this always have the most-likely-to-cause-warning fork
1748 if (pfork && (!pindexBestForkTip || (pindexBestForkTip && pindexNewForkTip->nHeight > pindexBestForkTip->nHeight)) &&
092b58d1 1749 pindexNewForkTip->nChainWork - pfork->nChainWork > (GetBlockProof(*pfork) * 7) &&
4c6d41b8 1750 chainActive.Height() - pindexNewForkTip->nHeight < 72)
b8585384
MC
1751 {
1752 pindexBestForkTip = pindexNewForkTip;
1753 pindexBestForkBase = pfork;
1754 }
1755
1756 CheckForkWarningConditions();
1757}
1758
f59d8f0b 1759// Requires cs_main.
75f51f2a
PW
1760void Misbehaving(NodeId pnode, int howmuch)
1761{
1762 if (howmuch == 0)
1763 return;
1764
1765 CNodeState *state = State(pnode);
1766 if (state == NULL)
1767 return;
1768
1769 state->nMisbehavior += howmuch;
dc942e6f
PW
1770 int banscore = GetArg("-banscore", 100);
1771 if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
75f51f2a 1772 {
30c1db1c 1773 LogPrintf("%s: %s (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__, state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
75f51f2a
PW
1774 state->fShouldBan = true;
1775 } else
30c1db1c 1776 LogPrintf("%s: %s (%d -> %d)\n", __func__, state->name, state->nMisbehavior-howmuch, state->nMisbehavior);
75f51f2a
PW
1777}
1778
64c7ee7e 1779void static InvalidChainFound(CBlockIndex* pindexNew)
0a61b0df 1780{
85eb2cef 1781 if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
85eb2cef 1782 pindexBestInvalid = pindexNew;
beb36e80 1783
30c1db1c 1784 LogPrintf("%s: invalid block=%s height=%d log2_work=%.8g date=%s\n", __func__,
7d9d134b 1785 pindexNew->GetBlockHash().ToString(), pindexNew->nHeight,
1657c4bc 1786 log(pindexNew->nChainWork.getdouble())/log(2.0), DateTimeStrFormat("%Y-%m-%d %H:%M:%S",
7d9d134b 1787 pindexNew->GetBlockTime()));
4a374102
RN
1788 CBlockIndex *tip = chainActive.Tip();
1789 assert (tip);
30c1db1c 1790 LogPrintf("%s: current best=%s height=%d log2_work=%.8g date=%s\n", __func__,
4a374102
RN
1791 tip->GetBlockHash().ToString(), chainActive.Height(), log(tip->nChainWork.getdouble())/log(2.0),
1792 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", tip->GetBlockTime()));
b8585384 1793 CheckForkWarningConditions();
0a61b0df 1794}
1795
75f51f2a
PW
1796void static InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state) {
1797 int nDoS = 0;
1798 if (state.IsInvalid(nDoS)) {
1799 std::map<uint256, NodeId>::iterator it = mapBlockSource.find(pindex->GetBlockHash());
1800 if (it != mapBlockSource.end() && State(it->second)) {
307f7d48 1801 CBlockReject reject = {state.GetRejectCode(), state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), pindex->GetBlockHash()};
75f51f2a
PW
1802 State(it->second)->rejects.push_back(reject);
1803 if (nDoS > 0)
1804 Misbehaving(it->second, nDoS);
857c61df 1805 }
75f51f2a
PW
1806 }
1807 if (!state.CorruptionPossible()) {
1808 pindex->nStatus |= BLOCK_FAILED_VALID;
51ce901a 1809 setDirtyBlockIndex.insert(pindex);
e17bd583 1810 setBlockIndexCandidates.erase(pindex);
75f51f2a
PW
1811 InvalidChainFound(pindex);
1812 }
857c61df
PW
1813}
1814
d38da59b 1815void UpdateCoins(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight)
450cbb09 1816{
021f8bc6 1817 if (!tx.IsCoinBase()) // mark inputs spent
1818 {
ab15b2ec 1819 txundo.vprevout.reserve(tx.vin.size());
f28aec01 1820 BOOST_FOREACH(const CTxIn &txin, tx.vin) {
c444c620 1821 CCoinsModifier coins = inputs.ModifyCoins(txin.prevout.hash);
1822 unsigned nPos = txin.prevout.n;
1823
1824 if (nPos >= coins->vout.size() || coins->vout[nPos].IsNull())
1825 assert(false);
1826 // mark an outpoint spent, and construct undo information
1827 txundo.vprevout.push_back(CTxInUndo(coins->vout[nPos]));
1828 coins->Spend(nPos);
1829 if (coins->vout.size() == 0) {
1830 CTxInUndo& undo = txundo.vprevout.back();
1831 undo.nHeight = coins->nHeight;
1832 undo.fCoinBase = coins->fCoinBase;
1833 undo.nVersion = coins->nVersion;
1834 }
450cbb09
PW
1835 }
1836 }
021f8bc6 1837 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) { // spend nullifiers
cc01120a
SB
1838 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
1839 inputs.SetNullifier(nf, true);
d66877af
SB
1840 }
1841 }
021f8bc6 1842 inputs.ModifyCoins(tx.GetHash())->FromTx(tx, nHeight); // add outputs
450cbb09
PW
1843}
1844
d7621ccf 1845void UpdateCoins(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, int nHeight)
1846{
1847 CTxUndo txundo;
1848 UpdateCoins(tx, state, inputs, txundo, nHeight);
1849}
1850
307f7d48 1851bool CScriptCheck::operator()() {
2800ce73 1852 const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
9fddceda 1853 if (!VerifyScript(scriptSig, scriptPubKey, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, cacheStore), &error)) {
805344dc 1854 return ::error("CScriptCheck(): %s:%d VerifySignature failed: %s", ptxTo->GetHash().ToString(), nIn, ScriptErrorString(error));
307f7d48 1855 }
2800ce73
PW
1856 return true;
1857}
1858
c0dde76d 1859bool NonContextualCheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, const Consensus::Params& consensusParams, std::vector<CScriptCheck> *pvChecks)
0a61b0df 1860{
05df3fc6 1861 if (!tx.IsCoinBase())
0a61b0df 1862 {
f9cae832 1863 if (pvChecks)
05df3fc6 1864 pvChecks->reserve(tx.vin.size());
f9cae832 1865
13c51f20
PW
1866 // This doesn't trigger the DoS code on purpose; if it did, it would make it easier
1867 // for an attacker to attempt to split the network.
05df3fc6 1868 if (!inputs.HaveInputs(tx))
805344dc 1869 return state.Invalid(error("CheckInputs(): %s inputs unavailable", tx.GetHash().ToString()));
13c51f20 1870
b7e4abd6 1871 // are the JoinSplit's requirements met?
ee964faf 1872 if (!inputs.HaveJoinSplitRequirements(tx))
805344dc 1873 return state.Invalid(error("CheckInputs(): %s JoinSplit requirements not met", tx.GetHash().ToString()));
a8ac403d 1874
a372168e
MF
1875 CAmount nValueIn = 0;
1876 CAmount nFees = 0;
05df3fc6 1877 for (unsigned int i = 0; i < tx.vin.size(); i++)
0a61b0df 1878 {
05df3fc6 1879 const COutPoint &prevout = tx.vin[i].prevout;
629d75fa
PW
1880 const CCoins *coins = inputs.AccessCoins(prevout.hash);
1881 assert(coins);
0a61b0df 1882
629d75fa 1883 if (coins->IsCoinBase()) {
1d38795f 1884 // Ensure that coinbases cannot be spent to transparent outputs
c0dde76d 1885 // Disabled on regtest
d212ba32
SB
1886 if (fCoinbaseEnforcedProtectionEnabled &&
1887 consensusParams.fCoinbaseMustBeProtected &&
1888 !tx.vout.empty()) {
358ce266 1889 return state.Invalid(
1d38795f
SB
1890 error("CheckInputs(): tried to spend coinbase with transparent outputs"),
1891 REJECT_INVALID, "bad-txns-coinbase-spend-has-transparent-outputs");
1892 }
450cbb09 1893 }
0a61b0df 1894
4add41a2 1895 // Check for negative or overflow input values
629d75fa 1896 nValueIn += coins->vout[prevout.n].nValue;
782120ca 1897#ifdef KOMODO_ENABLE_INTEREST
9fdf5974 1898 if ( ASSETCHAINS_SYMBOL[0] == 0 && chainActive.Tip() != 0 && chainActive.Tip()->nHeight >= 60000 )
782120ca 1899 {
98f68225 1900 if ( coins->vout[prevout.n].nValue >= 10*COIN )
782120ca 1901 {
1902 int64_t interest; int32_t txheight; uint32_t locktime;
407de87c 1903 if ( (interest= komodo_accrued_interest(&txheight,&locktime,prevout.hash,prevout.n,0,coins->vout[prevout.n].nValue)) != 0 )
1904 {
f6356642 1905 //printf("checkResult %.8f += val %.8f interest %.8f ht.%d lock.%u tip.%u\n",(double)nValueIn/COIN,(double)coins->vout[prevout.n].nValue/COIN,(double)interest/COIN,txheight,locktime,chainActive.Tip()->nTime);
1906 //fprintf(stderr,"checkResult %.8f += val %.8f interest %.8f ht.%d lock.%u tip.%u\n",(double)nValueIn/COIN,(double)coins->vout[prevout.n].nValue/COIN,(double)interest/COIN,txheight,locktime,chainActive.Tip()->nTime);
407de87c 1907 nValueIn += interest;
1908 }
782120ca 1909 }
1910 }
1911#endif
629d75fa 1912 if (!MoneyRange(coins->vout[prevout.n].nValue) || !MoneyRange(nValueIn))
5262fde0 1913 return state.DoS(100, error("CheckInputs(): txin values out of range"),
14e7ffcc 1914 REJECT_INVALID, "bad-txns-inputvalues-outofrange");
4add41a2
GA
1915
1916 }
450cbb09 1917
942bc467 1918 nValueIn += tx.GetJoinSplitValueIn();
f512cf7c
SB
1919 if (!MoneyRange(nValueIn))
1920 return state.DoS(100, error("CheckInputs(): vpub_old values out of range"),
1921 REJECT_INVALID, "bad-txns-inputvalues-outofrange");
1922
0733c1bd 1923 if (nValueIn < tx.GetValueOut())
407de87c 1924 return state.DoS(100, error("CheckInputs(): %s value in (%s) < value out (%s) diff %.8f",
1925 tx.GetHash().ToString(), FormatMoney(nValueIn), FormatMoney(tx.GetValueOut()),((double)nValueIn - tx.GetValueOut())/COIN),REJECT_INVALID, "bad-txns-in-belowout");
450cbb09
PW
1926
1927 // Tally transaction fees
a372168e 1928 CAmount nTxFee = nValueIn - tx.GetValueOut();
450cbb09 1929 if (nTxFee < 0)
805344dc 1930 return state.DoS(100, error("CheckInputs(): %s nTxFee < 0", tx.GetHash().ToString()),
14e7ffcc 1931 REJECT_INVALID, "bad-txns-fee-negative");
450cbb09
PW
1932 nFees += nTxFee;
1933 if (!MoneyRange(nFees))
5262fde0 1934 return state.DoS(100, error("CheckInputs(): nFees out of range"),
14e7ffcc 1935 REJECT_INVALID, "bad-txns-fee-outofrange");
38dfbe15 1936//fprintf(stderr,"nFees %.8f\n",(double)nFees/COIN);
4add41a2
GA
1937 // The first loop above does all the inexpensive checks.
1938 // Only if ALL inputs pass do we perform expensive ECDSA signature checks.
1939 // Helps prevent CPU exhaustion attacks.
4add41a2 1940
450cbb09 1941 // Skip ECDSA signature verification when connecting blocks
729b1806 1942 // before the last block chain checkpoint. This is safe because block merkle hashes are
450cbb09 1943 // still computed and checked, and any change will be caught at the next checkpoint.
1d70f4bd 1944 if (fScriptChecks) {
05df3fc6
EL
1945 for (unsigned int i = 0; i < tx.vin.size(); i++) {
1946 const COutPoint &prevout = tx.vin[i].prevout;
629d75fa
PW
1947 const CCoins* coins = inputs.AccessCoins(prevout.hash);
1948 assert(coins);
8d7849b6 1949
b14bd4df 1950 // Verify signature
e790c370 1951 CScriptCheck check(*coins, tx, i, flags, cacheStore);
f9cae832
PW
1952 if (pvChecks) {
1953 pvChecks->push_back(CScriptCheck());
1954 check.swap(pvChecks->back());
97e7901a 1955 } else if (!check()) {
f80cffa2
PT
1956 if (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) {
1957 // Check whether the failure was caused by a
1958 // non-mandatory script verification check, such as
1959 // non-standard DER encodings or non-null dummy
1960 // arguments; if so, don't trigger DoS protection to
1961 // avoid splitting the network between upgraded and
1962 // non-upgraded nodes.
629d75fa 1963 CScriptCheck check(*coins, tx, i,
e790c370 1964 flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, cacheStore);
97e7901a 1965 if (check())
307f7d48 1966 return state.Invalid(false, REJECT_NONSTANDARD, strprintf("non-mandatory-script-verify-flag (%s)", ScriptErrorString(check.GetScriptError())));
97e7901a 1967 }
f80cffa2
PT
1968 // Failures of other flags indicate a transaction that is
1969 // invalid in new blocks, e.g. a invalid P2SH. We DoS ban
1970 // such nodes as they are not following the protocol. That
1971 // said during an upgrade careful thought should be taken
1972 // as to the correct behavior - we may want to continue
1973 // peering with non-upgraded nodes even after a soft-fork
1974 // super-majority vote has passed.
307f7d48 1975 return state.DoS(100,false, REJECT_INVALID, strprintf("mandatory-script-verify-flag-failed (%s)", ScriptErrorString(check.GetScriptError())));
97e7901a 1976 }
2a45a494 1977 }
0a61b0df 1978 }
0a61b0df 1979 }
1980
0a61b0df 1981 return true;
1982}
1983
c0dde76d 1984bool ContextualCheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheStore, const Consensus::Params& consensusParams, std::vector<CScriptCheck> *pvChecks)
2c901fd8 1985{
89f3cd11 1986 if (!NonContextualCheckInputs(tx, state, inputs, fScriptChecks, flags, cacheStore, consensusParams, pvChecks)) {
9d92c93d 1987 fprintf(stderr,"ContextualCheckInputs failure.0\n");
89f3cd11
SB
1988 return false;
1989 }
1990
2c901fd8
SB
1991 if (!tx.IsCoinBase())
1992 {
1993 // While checking, GetBestBlock() refers to the parent block.
1994 // This is also true for mempool checks.
1995 CBlockIndex *pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
1996 int nSpendHeight = pindexPrev->nHeight + 1;
1997 for (unsigned int i = 0; i < tx.vin.size(); i++)
1998 {
1999 const COutPoint &prevout = tx.vin[i].prevout;
2000 const CCoins *coins = inputs.AccessCoins(prevout.hash);
89f3cd11
SB
2001 // Assertion is okay because NonContextualCheckInputs ensures the inputs
2002 // are available.
2c901fd8
SB
2003 assert(coins);
2004
2005 // If prev is coinbase, check that it's matured
2006 if (coins->IsCoinBase()) {
7a90b9dd 2007 if ( ASSETCHAINS_SYMBOL[0] == 0 )
2008 COINBASE_MATURITY = _COINBASE_MATURITY;
89f3cd11 2009 if (nSpendHeight - coins->nHeight < COINBASE_MATURITY) {
6bc7804a 2010 fprintf(stderr,"ContextualCheckInputs failure.1 i.%d of %d\n",i,(int32_t)tx.vin.size());
9d92c93d 2011
2c901fd8 2012 return state.Invalid(
9d92c93d 2013 error("CheckInputs(): tried to spend coinbase at depth %d", nSpendHeight - coins->nHeight),REJECT_INVALID, "bad-txns-premature-spend-of-coinbase");
89f3cd11 2014 }
2c901fd8
SB
2015 }
2016 }
2017 }
2018
89f3cd11 2019 return true;
2c901fd8
SB
2020}
2021
87fb3108 2022namespace {
2023
e6973430 2024bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
87fb3108 2025{
2026 // Open history file to append
2027 CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
2028 if (fileout.IsNull())
5262fde0 2029 return error("%s: OpenUndoFile failed", __func__);
87fb3108 2030
2031 // Write index header
2032 unsigned int nSize = fileout.GetSerializeSize(blockundo);
e6973430 2033 fileout << FLATDATA(messageStart) << nSize;
87fb3108 2034
2035 // Write undo data
2036 long fileOutPos = ftell(fileout.Get());
2037 if (fileOutPos < 0)
5262fde0 2038 return error("%s: ftell failed", __func__);
87fb3108 2039 pos.nPos = (unsigned int)fileOutPos;
2040 fileout << blockundo;
2041
2042 // calculate & write checksum
2043 CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
2044 hasher << hashBlock;
2045 hasher << blockundo;
2046 fileout << hasher.GetHash();
2047
2048 return true;
2049}
2050
2051bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock)
2052{
2053 // Open history file to read
2054 CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
2055 if (filein.IsNull())
5262fde0 2056 return error("%s: OpenBlockFile failed", __func__);
87fb3108 2057
2058 // Read block
2059 uint256 hashChecksum;
2060 try {
2061 filein >> blockundo;
2062 filein >> hashChecksum;
2063 }
2064 catch (const std::exception& e) {
5262fde0 2065 return error("%s: Deserialize or I/O error - %s", __func__, e.what());
87fb3108 2066 }
2067
2068 // Verify checksum
2069 CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
2070 hasher << hashBlock;
2071 hasher << blockundo;
2072 if (hashChecksum != hasher.GetHash())
5262fde0 2073 return error("%s: Checksum mismatch", __func__);
87fb3108 2074
2075 return true;
2076}
2077
27afcd89
CF
2078/** Abort with a message */
2079bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
2080{
2081 strMiscWarning = strMessage;
2082 LogPrintf("*** %s\n", strMessage);
2083 uiInterface.ThreadSafeMessageBox(
45bfa137 2084 userMessage.empty() ? _("Error: A fatal internal error occurred, see debug.log for details") : userMessage,
27afcd89
CF
2085 "", CClientUIInterface::MSG_ERROR);
2086 StartShutdown();
2087 return false;
2088}
2089
2090bool AbortNode(CValidationState& state, const std::string& strMessage, const std::string& userMessage="")
2091{
2092 AbortNode(strMessage, userMessage);
2093 return state.Error(strMessage);
2094}
2095
87fb3108 2096} // anon namespace
0a61b0df 2097
eb1c2cd3
DK
2098/**
2099 * Apply the undo operation of a CTxInUndo to the given chain state.
2100 * @param undo The undo object.
2101 * @param view The coins view to which to apply the changes.
2102 * @param out The out point that corresponds to the tx input.
2103 * @return True on success.
2104 */
2105static bool ApplyTxInUndo(const CTxInUndo& undo, CCoinsViewCache& view, const COutPoint& out)
2106{
2107 bool fClean = true;
2108
2109 CCoinsModifier coins = view.ModifyCoins(out.hash);
2110 if (undo.nHeight != 0) {
2111 // undo data contains height: this is the last output of the prevout tx being spent
2112 if (!coins->IsPruned())
2113 fClean = fClean && error("%s: undo data overwriting existing transaction", __func__);
2114 coins->Clear();
2115 coins->fCoinBase = undo.fCoinBase;
2116 coins->nHeight = undo.nHeight;
2117 coins->nVersion = undo.nVersion;
2118 } else {
2119 if (coins->IsPruned())
2120 fClean = fClean && error("%s: undo data adding output to missing transaction", __func__);
2121 }
2122 if (coins->IsAvailable(out.n))
2123 fClean = fClean && error("%s: undo data overwriting existing output", __func__);
2124 if (coins->vout.size() < out.n+1)
2125 coins->vout.resize(out.n+1);
2126 coins->vout[out.n] = undo.txout;
2127
2128 return fClean;
2129}
2130
5c363ed6 2131bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean)
0a61b0df 2132{
84674082 2133 assert(pindex->GetBlockHash() == view.GetBestBlock());
0a61b0df 2134
2cbd71da
PW
2135 if (pfClean)
2136 *pfClean = false;
2137
2138 bool fClean = true;
4355e769 2139 komodo_disconnect(pindex,block);
450cbb09 2140 CBlockUndo blockUndo;
8539361e
PW
2141 CDiskBlockPos pos = pindex->GetUndoPos();
2142 if (pos.IsNull())
5262fde0 2143 return error("DisconnectBlock(): no undo data available");
e035c6a7 2144 if (!UndoReadFromDisk(blockUndo, pos, pindex->pprev->GetBlockHash()))
5262fde0 2145 return error("DisconnectBlock(): failure reading undo data");
0a61b0df 2146
5c363ed6 2147 if (blockUndo.vtxundo.size() + 1 != block.vtx.size())
5262fde0 2148 return error("DisconnectBlock(): block and undo data inconsistent");
450cbb09
PW
2149
2150 // undo transactions in reverse order
5c363ed6
EL
2151 for (int i = block.vtx.size() - 1; i >= 0; i--) {
2152 const CTransaction &tx = block.vtx[i];
805344dc 2153 uint256 hash = tx.GetHash();
450cbb09 2154
170e02de 2155 // Check that all outputs are available and match the outputs in the block itself
eb1c2cd3 2156 // exactly.
f28aec01 2157 {
f28aec01
PW
2158 CCoinsModifier outs = view.ModifyCoins(hash);
2159 outs->ClearUnspendable();
450cbb09 2160
f28aec01 2161 CCoins outsBlock(tx, pindex->nHeight);
f8b7aa86
GM
2162 // The CCoins serialization does not serialize negative numbers.
2163 // No network rules currently depend on the version here, so an inconsistency is harmless
2164 // but it must be corrected before txout nversion ever influences a network rule.
2165 if (outsBlock.nVersion < 0)
f28aec01 2166 outs->nVersion = outsBlock.nVersion;
c7a3aca6 2167 if (*outs != outsBlock)
2168 fClean = fClean && error("DisconnectBlock(): added transaction mismatch? database corrupted");
2169
450cbb09 2170 // remove outputs
f28aec01
PW
2171 outs->Clear();
2172 }
450cbb09 2173
bfeaf004 2174 // unspend nullifiers
b7e4abd6 2175 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
cc01120a
SB
2176 BOOST_FOREACH(const uint256 &nf, joinsplit.nullifiers) {
2177 view.SetNullifier(nf, false);
d66877af
SB
2178 }
2179 }
2180
450cbb09
PW
2181 // restore inputs
2182 if (i > 0) { // not coinbases
2183 const CTxUndo &txundo = blockUndo.vtxundo[i-1];
2cbd71da 2184 if (txundo.vprevout.size() != tx.vin.size())
5262fde0 2185 return error("DisconnectBlock(): transaction and undo data inconsistent");
450cbb09
PW
2186 for (unsigned int j = tx.vin.size(); j-- > 0;) {
2187 const COutPoint &out = tx.vin[j].prevout;
2188 const CTxInUndo &undo = txundo.vprevout[j];
eb1c2cd3
DK
2189 if (!ApplyTxInUndo(undo, view, out))
2190 fClean = false;
450cbb09
PW
2191 }
2192 }
2193 }
2194
a8ac403d
SB
2195 // set the old best anchor back
2196 view.PopAnchor(blockUndo.old_tree_root);
2197
450cbb09 2198 // move best block pointer to prevout block
84674082 2199 view.SetBestBlock(pindex->pprev->GetBlockHash());
450cbb09 2200
2cbd71da
PW
2201 if (pfClean) {
2202 *pfClean = fClean;
2203 return true;
2cbd71da 2204 }
eb1c2cd3
DK
2205
2206 return fClean;
0a61b0df 2207}
2208
1eb57879 2209void static FlushBlockFile(bool fFinalize = false)
44d40f26
PW
2210{
2211 LOCK(cs_LastBlockFile);
2212
a8a4b967 2213 CDiskBlockPos posOld(nLastBlockFile, 0);
44d40f26
PW
2214
2215 FILE *fileOld = OpenBlockFile(posOld);
b19388dd 2216 if (fileOld) {
1eb57879 2217 if (fFinalize)
ed6d1a2c 2218 TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nSize);
b19388dd
PK
2219 FileCommit(fileOld);
2220 fclose(fileOld);
2221 }
44d40f26
PW
2222
2223 fileOld = OpenUndoFile(posOld);
b19388dd 2224 if (fileOld) {
1eb57879 2225 if (fFinalize)
ed6d1a2c 2226 TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nUndoSize);
b19388dd
PK
2227 FileCommit(fileOld);
2228 fclose(fileOld);
2229 }
44d40f26
PW
2230}
2231
ef3988ca 2232bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
5382bcf8 2233
f9cae832
PW
2234static CCheckQueue<CScriptCheck> scriptcheckqueue(128);
2235
21eb5ada 2236void ThreadScriptCheck() {
7662d72b 2237 RenameThread("zcash-scriptch");
f9cae832 2238 scriptcheckqueue.Thread();
f9cae832
PW
2239}
2240
36cba8f1
GA
2241//
2242// Called periodically asynchronously; alerts if it smells like
2243// we're being fed a bad chain (blocks being generated much
2244// too slowly or too quickly).
2245//
fce474c9
GA
2246void PartitionCheck(bool (*initialDownloadCheck)(), CCriticalSection& cs, const CBlockIndex *const &bestHeader,
2247 int64_t nPowTargetSpacing)
36cba8f1 2248{
fce474c9 2249 if (bestHeader == NULL || initialDownloadCheck()) return;
36cba8f1
GA
2250
2251 static int64_t lastAlertTime = 0;
2252 int64_t now = GetAdjustedTime();
2253 if (lastAlertTime > now-60*60*24) return; // Alert at most once per day
2254
2255 const int SPAN_HOURS=4;
2256 const int SPAN_SECONDS=SPAN_HOURS*60*60;
2257 int BLOCKS_EXPECTED = SPAN_SECONDS / nPowTargetSpacing;
2258
2259 boost::math::poisson_distribution<double> poisson(BLOCKS_EXPECTED);
2260
2261 std::string strWarning;
2262 int64_t startTime = GetAdjustedTime()-SPAN_SECONDS;
2263
2264 LOCK(cs);
fce474c9
GA
2265 const CBlockIndex* i = bestHeader;
2266 int nBlocks = 0;
2267 while (i->GetBlockTime() >= startTime) {
2268 ++nBlocks;
2269 i = i->pprev;
2270 if (i == NULL) return; // Ran out of chain, we must not be fully sync'ed
2271 }
36cba8f1
GA
2272
2273 // How likely is it to find that many by chance?
2274 double p = boost::math::pdf(poisson, nBlocks);
2275
2276 LogPrint("partitioncheck", "%s : Found %d blocks in the last %d hours\n", __func__, nBlocks, SPAN_HOURS);
2277 LogPrint("partitioncheck", "%s : likelihood: %g\n", __func__, p);
2278
2279 // Aim for one false-positive about every fifty years of normal running:
2280 const int FIFTY_YEARS = 50*365*24*60*60;
2281 double alertThreshold = 1.0 / (FIFTY_YEARS / SPAN_SECONDS);
2282
2283 if (p <= alertThreshold && nBlocks < BLOCKS_EXPECTED)
2284 {
2285 // Many fewer blocks than expected: alert!
2286 strWarning = strprintf(_("WARNING: check your network connection, %d blocks received in the last %d hours (%d expected)"),
2287 nBlocks, SPAN_HOURS, BLOCKS_EXPECTED);
2288 }
2289 else if (p <= alertThreshold && nBlocks > BLOCKS_EXPECTED)
2290 {
2291 // Many more blocks than expected: alert!
2292 strWarning = strprintf(_("WARNING: abnormally high number of blocks generated, %d blocks received in the last %d hours (%d expected)"),
2293 nBlocks, SPAN_HOURS, BLOCKS_EXPECTED);
2294 }
2295 if (!strWarning.empty())
2296 {
2297 strMiscWarning = strWarning;
2298 CAlert::Notify(strWarning, true);
2299 lastAlertTime = now;
2300 }
2301}
2302
d70bc52e
PW
2303static int64_t nTimeVerify = 0;
2304static int64_t nTimeConnect = 0;
2305static int64_t nTimeIndex = 0;
2306static int64_t nTimeCallbacks = 0;
2307static int64_t nTimeTotal = 0;
2308
df08a626 2309bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool fJustCheck)
0a61b0df 2310{
4e382177 2311 const CChainParams& chainparams = Params();
b39a07dc 2312 AssertLockHeld(cs_main);
0a61b0df 2313 // Check it again in case a previous version let a bad block in
6fb8d0c2
JG
2314 bool fExpensiveChecks = (!fCheckpointsEnabled || pindex->nHeight >= Checkpoints::GetTotalBlocksEstimate(chainparams.Checkpoints()));
2315 auto verifier = libzcash::ProofVerifier::Strict();
2316 auto disabledVerifier = libzcash::ProofVerifier::Disabled();
2317
2318 // Check it again to verify JoinSplit proofs, and in case a previous version let a bad block in
3ced9364 2319 if (!CheckBlock(pindex->nHeight,pindex,block, state, fExpensiveChecks ? verifier : disabledVerifier, !fJustCheck, !fJustCheck))
0a61b0df 2320 return false;
2321
450cbb09 2322 // verify that the view's current state corresponds to the previous block
4f152496 2323 uint256 hashPrevBlock = pindex->pprev == NULL ? uint256() : pindex->pprev->GetBlockHash();
84674082 2324 assert(hashPrevBlock == view.GetBestBlock());
450cbb09 2325
8301ff50
PW
2326 // Special case for the genesis block, skipping connection of its transactions
2327 // (its coinbase is unspendable)
4e382177 2328 if (block.GetHash() == chainparams.GetConsensus().hashGenesisBlock) {
6a7acc29 2329 if (!fJustCheck) {
1b178a7f 2330 view.SetBestBlock(pindex->GetBlockHash());
6a7acc29
JG
2331 // Before the genesis block, there was an empty tree
2332 ZCIncrementalMerkleTree tree;
2333 pindex->hashAnchor = tree.root();
2334 }
8301ff50
PW
2335 return true;
2336 }
2337
a8cdaf5c 2338 bool fScriptChecks = (!fCheckpointsEnabled || pindex->nHeight >= Checkpoints::GetTotalBlocksEstimate(chainparams.Checkpoints()));
f8b5c164 2339 //if ( KOMODO_TESTNET_EXPIRATION != 0 && pindex->nHeight > KOMODO_TESTNET_EXPIRATION ) // "testnet"
2340 // return(false);
a206b0ea
PW
2341 // Do not allow blocks that contain transactions which 'overwrite' older transactions,
2342 // unless those are already completely spent.
b3be1ef5 2343 BOOST_FOREACH(const CTransaction& tx, block.vtx) {
805344dc 2344 const CCoins* coins = view.AccessCoins(tx.GetHash());
b3be1ef5
SB
2345 if (coins && !coins->IsPruned())
2346 return state.DoS(100, error("ConnectBlock(): tried to overwrite transaction"),
2347 REJECT_INVALID, "bad-txns-BIP30");
450cbb09 2348 }
a206b0ea 2349
b3be1ef5 2350 unsigned int flags = SCRIPT_VERIFY_P2SH;
ef0f4225 2351
5e82e1c8
PT
2352 // Start enforcing the DERSIG (BIP66) rules, for block.nVersion=3 blocks,
2353 // when 75% of the network has upgraded:
542da618 2354 if (block.nVersion >= 3) {
5a47811d
PW
2355 flags |= SCRIPT_VERIFY_DERSIG;
2356 }
2357
5e82e1c8
PT
2358 // Start enforcing CHECKLOCKTIMEVERIFY, (BIP65) for block.nVersion=4
2359 // blocks, when 75% of the network has upgraded:
542da618 2360 if (block.nVersion >= 4) {
5e82e1c8
PT
2361 flags |= SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY;
2362 }
2363
8adf48dc
PW
2364 CBlockUndo blockundo;
2365
6fb8d0c2 2366 CCheckQueueControl<CScriptCheck> control(fExpensiveChecks && nScriptCheckThreads ? &scriptcheckqueue : NULL);
f9cae832 2367
d70bc52e 2368 int64_t nTimeStart = GetTimeMicros();
a372168e 2369 CAmount nFees = 0;
8a28bb6d 2370 int nInputs = 0;
355ca565 2371 int64_t interest,sum = 0;
7bd9c3a3 2372 unsigned int nSigOps = 0;
f3ae51dc 2373 CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
2d1fa42e 2374 std::vector<std::pair<uint256, CDiskTxPos> > vPos;
f3ae51dc 2375 vPos.reserve(block.vtx.size());
ab15b2ec 2376 blockundo.vtxundo.reserve(block.vtx.size() - 1);
a8ac403d
SB
2377
2378 // Construct the incremental merkle tree at the current
2379 // block position,
2380 auto old_tree_root = view.GetBestAnchor();
b6961fc1 2381 // saving the top anchor in the block index as we go.
6a7acc29
JG
2382 if (!fJustCheck) {
2383 pindex->hashAnchor = old_tree_root;
2384 }
434f3284 2385 ZCIncrementalMerkleTree tree;
a8ac403d
SB
2386 // This should never fail: we should always be able to get the root
2387 // that is on the tip of our chain
2388 assert(view.GetAnchorAt(old_tree_root, tree));
2389
2390 {
2391 // Consistency check: the root of the tree we're given should
2392 // match what we asked for.
434f3284 2393 assert(tree.root() == old_tree_root);
a8ac403d
SB
2394 }
2395
f3ae51dc 2396 for (unsigned int i = 0; i < block.vtx.size(); i++)
0a61b0df 2397 {
f3ae51dc 2398 const CTransaction &tx = block.vtx[i];
64dd46fd 2399
8a28bb6d 2400 nInputs += tx.vin.size();
05df3fc6 2401 nSigOps += GetLegacySigOpCount(tx);
137d0685 2402 if (nSigOps > MAX_BLOCK_SIGOPS)
5262fde0 2403 return state.DoS(100, error("ConnectBlock(): too many sigops"),
14e7ffcc 2404 REJECT_INVALID, "bad-blk-sigops");
420af8fd 2405//fprintf(stderr,"ht.%d vout0 t%u\n",pindex->nHeight,tx.nLockTime);
8d7849b6
GA
2406 if (!tx.IsCoinBase())
2407 {
05df3fc6 2408 if (!view.HaveInputs(tx))
5262fde0 2409 return state.DoS(100, error("ConnectBlock(): inputs missing/spent"),
14e7ffcc 2410 REJECT_INVALID, "bad-txns-inputs-missingorspent");
922e8e29 2411
b7e4abd6 2412 // are the JoinSplit's requirements met?
ee964faf 2413 if (!view.HaveJoinSplitRequirements(tx))
b7e4abd6
SB
2414 return state.DoS(100, error("ConnectBlock(): JoinSplit requirements not met"),
2415 REJECT_INVALID, "bad-txns-joinsplit-requirements-not-met");
a8ac403d 2416
b3be1ef5
SB
2417 // Add in sigops done by pay-to-script-hash inputs;
2418 // this is to prevent a "rogue miner" from creating
2419 // an incredibly-expensive-to-validate block.
2420 nSigOps += GetP2SHSigOpCount(tx, view);
2421 if (nSigOps > MAX_BLOCK_SIGOPS)
2422 return state.DoS(100, error("ConnectBlock(): too many sigops"),
2423 REJECT_INVALID, "bad-blk-sigops");
922e8e29 2424
17878015 2425 nFees += view.GetValueIn(chainActive.Tip()->nHeight,&interest,tx,chainActive.Tip()->nTime) - tx.GetValueOut();
355ca565 2426 sum += interest;
f9cae832 2427 std::vector<CScriptCheck> vChecks;
6fb8d0c2 2428 if (!ContextualCheckInputs(tx, state, view, fExpensiveChecks, flags, false, chainparams.GetConsensus(), nScriptCheckThreads ? &vChecks : NULL))
40634605 2429 return false;
f9cae832 2430 control.Add(vChecks);
8d7849b6 2431 }
1565e3c3 2432 komodo_earned_interest(pindex->nHeight,sum);
ab15b2ec
PW
2433 CTxUndo undoDummy;
2434 if (i > 0) {
2435 blockundo.vtxundo.push_back(CTxUndo());
2436 }
2437 UpdateCoins(tx, state, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight);
8a28bb6d 2438
b7e4abd6 2439 BOOST_FOREACH(const JSDescription &joinsplit, tx.vjoinsplit) {
4bc00dc1
DH
2440 BOOST_FOREACH(const uint256 &note_commitment, joinsplit.commitments) {
2441 // Insert the note commitments into our temporary tree.
a8ac403d 2442
4bc00dc1 2443 tree.append(note_commitment);
a8ac403d
SB
2444 }
2445 }
2446
805344dc 2447 vPos.push_back(std::make_pair(tx.GetHash(), pos));
2d1fa42e 2448 pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
0a61b0df 2449 }
a8ac403d 2450
a8ac403d
SB
2451 view.PushAnchor(tree);
2452 blockundo.old_tree_root = old_tree_root;
2453
d70bc52e
PW
2454 int64_t nTime1 = GetTimeMicros(); nTimeConnect += nTime1 - nTimeStart;
2455 LogPrint("bench", " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs]\n", (unsigned)block.vtx.size(), 0.001 * (nTime1 - nTimeStart), 0.001 * (nTime1 - nTimeStart) / block.vtx.size(), nInputs <= 1 ? 0 : 0.001 * (nTime1 - nTimeStart) / (nInputs-1), nTimeConnect * 0.000001);
e679ec96 2456
935bd0a4 2457 CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, chainparams.GetConsensus());
87f0ea10 2458 if (block.vtx[0].vout[0].nValue > blockReward)
2459 //if (block.vtx[0].GetValueOut() > blockReward)
358ce266 2460 return state.DoS(100,
5262fde0 2461 error("ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)",
935bd0a4 2462 block.vtx[0].GetValueOut(), blockReward),
2b45345a 2463 REJECT_INVALID, "bad-cb-amount");
9e957fb3 2464
f9cae832 2465 if (!control.Wait())
ef3988ca 2466 return state.DoS(100, false);
d70bc52e
PW
2467 int64_t nTime2 = GetTimeMicros(); nTimeVerify += nTime2 - nTimeStart;
2468 LogPrint("bench", " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs]\n", nInputs - 1, 0.001 * (nTime2 - nTimeStart), nInputs <= 1 ? 0 : 0.001 * (nTime2 - nTimeStart) / (nInputs-1), nTimeVerify * 0.000001);
f9cae832 2469
3cd01fdf
LD
2470 if (fJustCheck)
2471 return true;
2472
5382bcf8 2473 // Write undo information to disk
942b33a1 2474 if (pindex->GetUndoPos().IsNull() || !pindex->IsValid(BLOCK_VALID_SCRIPTS))
5382bcf8 2475 {
857c61df
PW
2476 if (pindex->GetUndoPos().IsNull()) {
2477 CDiskBlockPos pos;
ef3988ca 2478 if (!FindUndoPos(state, pindex->nFile, pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
5262fde0 2479 return error("ConnectBlock(): FindUndoPos failed");
e6973430 2480 if (!UndoWriteToDisk(blockundo, pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart()))
27afcd89 2481 return AbortNode(state, "Failed to write undo data");
857c61df
PW
2482
2483 // update nUndoPos in block index
2484 pindex->nUndoPos = pos.nPos;
2485 pindex->nStatus |= BLOCK_HAVE_UNDO;
2486 }
2487
942b33a1 2488 pindex->RaiseValidity(BLOCK_VALID_SCRIPTS);
51ce901a 2489 setDirtyBlockIndex.insert(pindex);
0a61b0df 2490 }
2491
2d1fa42e 2492 if (fTxIndex)
ef3988ca 2493 if (!pblocktree->WriteTxIndex(vPos))
27afcd89 2494 return AbortNode(state, "Failed to write transaction index");
2d1fa42e 2495
729b1806 2496 // add this block to the view's block chain
c9d1a81c 2497 view.SetBestBlock(pindex->GetBlockHash());
450cbb09 2498
d70bc52e
PW
2499 int64_t nTime3 = GetTimeMicros(); nTimeIndex += nTime3 - nTime2;
2500 LogPrint("bench", " - Index writing: %.2fms [%.2fs]\n", 0.001 * (nTime3 - nTime2), nTimeIndex * 0.000001);
2501
202e0194
PW
2502 // Watch for changes to the previous coinbase transaction.
2503 static uint256 hashPrevBestCoinBase;
26c16d9d 2504 GetMainSignals().UpdatedTransaction(hashPrevBestCoinBase);
805344dc 2505 hashPrevBestCoinBase = block.vtx[0].GetHash();
202e0194 2506
d70bc52e
PW
2507 int64_t nTime4 = GetTimeMicros(); nTimeCallbacks += nTime4 - nTime3;
2508 LogPrint("bench", " - Callbacks: %.2fms [%.2fs]\n", 0.001 * (nTime4 - nTime3), nTimeCallbacks * 0.000001);
a0344f90 2509
0f42af4c 2510 //FlushStateToDisk();
651989c7 2511 komodo_connectblock(pindex,*(CBlock *)&block);
0a61b0df 2512 return true;
2513}
2514
a2069500 2515enum FlushStateMode {
f9ec3f0f 2516 FLUSH_STATE_NONE,
a2069500
PW
2517 FLUSH_STATE_IF_NEEDED,
2518 FLUSH_STATE_PERIODIC,
2519 FLUSH_STATE_ALWAYS
2520};
2521
51ce901a
PW
2522/**
2523 * Update the on-disk chain state.
f9ec3f0f 2524 * The caches and indexes are flushed depending on the mode we're called with
2525 * if they're too large, if it's been a while since the last write,
2526 * or always and in all cases if we're in prune mode and are deleting files.
51ce901a 2527 */
a2069500 2528bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode) {
f9ec3f0f 2529 LOCK2(cs_main, cs_LastBlockFile);
75f51f2a 2530 static int64_t nLastWrite = 0;
67708acf
PW
2531 static int64_t nLastFlush = 0;
2532 static int64_t nLastSetChain = 0;
f9ec3f0f 2533 std::set<int> setFilesToPrune;
2534 bool fFlushForPrune = false;
e4134579 2535 try {
dfe55bdc 2536 if (fPruneMode && fCheckForPruning && !fReindex) {
f9ec3f0f 2537 FindFilesToPrune(setFilesToPrune);
c2080403 2538 fCheckForPruning = false;
f9ec3f0f 2539 if (!setFilesToPrune.empty()) {
2540 fFlushForPrune = true;
2541 if (!fHavePruned) {
2542 pblocktree->WriteFlag("prunedblockfiles", true);
2543 fHavePruned = true;
2544 }
2545 }
2546 }
67708acf
PW
2547 int64_t nNow = GetTimeMicros();
2548 // Avoid writing/flushing immediately after startup.
2549 if (nLastWrite == 0) {
2550 nLastWrite = nNow;
2551 }
2552 if (nLastFlush == 0) {
2553 nLastFlush = nNow;
2554 }
2555 if (nLastSetChain == 0) {
2556 nLastSetChain = nNow;
2557 }
2558 size_t cacheSize = pcoinsTip->DynamicMemoryUsage();
2559 // The cache is large and close to the limit, but we have time now (not in the middle of a block processing).
2560 bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize * (10.0/9) > nCoinCacheUsage;
2561 // The cache is over the limit, we have to write now.
2562 bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && cacheSize > nCoinCacheUsage;
2563 // 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.
2564 bool fPeriodicWrite = mode == FLUSH_STATE_PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
2565 // It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
2566 bool fPeriodicFlush = mode == FLUSH_STATE_PERIODIC && nNow > nLastFlush + (int64_t)DATABASE_FLUSH_INTERVAL * 1000000;
2567 // Combine all conditions that result in a full cache flush.
2568 bool fDoFullFlush = (mode == FLUSH_STATE_ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
2569 // Write blocks and block index to disk.
2570 if (fDoFullFlush || fPeriodicWrite) {
86a5f4b5
AM
2571 // Depend on nMinDiskSpace to ensure we can write block index
2572 if (!CheckDiskSpace(0))
c117d9e9 2573 return state.Error("out of disk space");
51ce901a 2574 // First make sure all block and undo data is flushed to disk.
44d40f26 2575 FlushBlockFile();
51ce901a 2576 // Then update all block file information (which may refer to block and undo files).
63d1ae55
PW
2577 {
2578 std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
2579 vFiles.reserve(setDirtyFileInfo.size());
2580 for (set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
2581 vFiles.push_back(make_pair(*it, &vinfoBlockFile[*it]));
2582 setDirtyFileInfo.erase(it++);
2583 }
2584 std::vector<const CBlockIndex*> vBlocks;
2585 vBlocks.reserve(setDirtyBlockIndex.size());
2586 for (set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
2587 vBlocks.push_back(*it);
2588 setDirtyBlockIndex.erase(it++);
2589 }
2590 if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
27afcd89 2591 return AbortNode(state, "Files to write to block index database");
51ce901a 2592 }
51ce901a 2593 }
f9ec3f0f 2594 // Finally remove any pruned files
c2080403 2595 if (fFlushForPrune)
f9ec3f0f 2596 UnlinkPrunedFiles(setFilesToPrune);
67708acf
PW
2597 nLastWrite = nNow;
2598 }
2599 // Flush best chain related state. This can only be done if the blocks / block index write was also done.
2600 if (fDoFullFlush) {
86a5f4b5
AM
2601 // Typical CCoins structures on disk are around 128 bytes in size.
2602 // Pushing a new one to the database can cause it to be written
2603 // twice (once in the log, and once in the tables). This is already
2604 // an overestimation, as most will delete an existing entry or
2605 // overwrite one. Still, use a conservative safety factor of 2.
2606 if (!CheckDiskSpace(128 * 2 * 2 * pcoinsTip->GetCacheSize()))
2607 return state.Error("out of disk space");
67708acf
PW
2608 // Flush the chainstate (which may refer to block index entries).
2609 if (!pcoinsTip->Flush())
27afcd89 2610 return AbortNode(state, "Failed to write to coin database");
67708acf
PW
2611 nLastFlush = nNow;
2612 }
2613 if ((mode == FLUSH_STATE_ALWAYS || mode == FLUSH_STATE_PERIODIC) && nNow > nLastSetChain + (int64_t)DATABASE_WRITE_INTERVAL * 1000000) {
51ce901a 2614 // Update best block in wallet (so we can detect restored wallets).
67708acf
PW
2615 GetMainSignals().SetBestChain(chainActive.GetLocator());
2616 nLastSetChain = nNow;
44d40f26 2617 }
e4134579 2618 } catch (const std::runtime_error& e) {
27afcd89 2619 return AbortNode(state, std::string("System error while flushing: ") + e.what());
e4134579 2620 }
0ec16f35
PW
2621 return true;
2622}
450cbb09 2623
51ce901a
PW
2624void FlushStateToDisk() {
2625 CValidationState state;
a2069500 2626 FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
51ce901a
PW
2627}
2628
f9ec3f0f 2629void PruneAndFlush() {
2630 CValidationState state;
2631 fCheckForPruning = true;
2632 FlushStateToDisk(state, FLUSH_STATE_NONE);
2633}
2634
c5b390b6 2635/** Update chainActive and related internal data structures. */
0ec16f35 2636void static UpdateTip(CBlockIndex *pindexNew) {
11982d36 2637 const CChainParams& chainParams = Params();
4c6d41b8 2638 chainActive.SetTip(pindexNew);
0a61b0df 2639
0a61b0df 2640 // New best block
0a61b0df 2641 nTimeBestReceived = GetTime();
319b1160 2642 mempool.AddTransactionsUpdated(1);
ff6a7af1 2643
b0ae7941 2644 LogPrintf("%s: new best=%s height=%d log2_work=%.8g tx=%lu date=%s progress=%f cache=%.1fMiB(%utx)\n", __func__,
0ec16f35 2645 chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
7d9d134b 2646 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
b3ed4236 2647 Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip()), pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
0a61b0df 2648
ff6a7af1
LD
2649 cvBlockChange.notify_all();
2650
2a919e39 2651 // Check the version of the last 100 blocks to see if we need to upgrade:
dbca89b7
GA
2652 static bool fWarned = false;
2653 if (!IsInitialBlockDownload() && !fWarned)
2a919e39
GA
2654 {
2655 int nUpgraded = 0;
4c6d41b8 2656 const CBlockIndex* pindex = chainActive.Tip();
2a919e39
GA
2657 for (int i = 0; i < 100 && pindex != NULL; i++)
2658 {
2659 if (pindex->nVersion > CBlock::CURRENT_VERSION)
2660 ++nUpgraded;
2661 pindex = pindex->pprev;
2662 }
2663 if (nUpgraded > 0)
30c1db1c 2664 LogPrintf("%s: %d of last 100 blocks above version %d\n", __func__, nUpgraded, (int)CBlock::CURRENT_VERSION);
2a919e39 2665 if (nUpgraded > 100/2)
dbca89b7 2666 {
2a919e39 2667 // strMiscWarning is read by GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
7e6d23b1 2668 strMiscWarning = _("Warning: This version is obsolete; upgrade required!");
dbca89b7
GA
2669 CAlert::Notify(strMiscWarning, true);
2670 fWarned = true;
2671 }
2a919e39 2672 }
75f51f2a 2673}
2a919e39 2674
c5b390b6 2675/** Disconnect chainActive's tip. */
75f51f2a
PW
2676bool static DisconnectTip(CValidationState &state) {
2677 CBlockIndex *pindexDelete = chainActive.Tip();
2678 assert(pindexDelete);
2679 mempool.check(pcoinsTip);
2680 // Read block from disk.
2681 CBlock block;
f2dd868d 2682 if (!ReadBlockFromDisk(block, pindexDelete))
27afcd89 2683 return AbortNode(state, "Failed to read block");
75f51f2a 2684 // Apply the block atomically to the chain state.
a8ac403d 2685 uint256 anchorBeforeDisconnect = pcoinsTip->GetBestAnchor();
75f51f2a 2686 int64_t nStart = GetTimeMicros();
d237f62c 2687 {
7c70438d 2688 CCoinsViewCache view(pcoinsTip);
75f51f2a 2689 if (!DisconnectBlock(block, state, pindexDelete, view))
5262fde0 2690 return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
75f51f2a 2691 assert(view.Flush());
d237f62c 2692 }
d70bc52e 2693 LogPrint("bench", "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * 0.001);
a8ac403d 2694 uint256 anchorAfterDisconnect = pcoinsTip->GetBestAnchor();
75f51f2a 2695 // Write the chain state to disk, if necessary.
a2069500 2696 if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED))
75f51f2a 2697 return false;
93a18a36 2698 // Resurrect mempool transactions from the disconnected block.
75f51f2a
PW
2699 BOOST_FOREACH(const CTransaction &tx, block.vtx) {
2700 // ignore validation errors in resurrected transactions
93a18a36 2701 list<CTransaction> removed;
ac14bcc1 2702 CValidationState stateDummy;
868d0416
MC
2703 if (tx.IsCoinBase() || !AcceptToMemoryPool(mempool, stateDummy, tx, false, NULL))
2704 mempool.remove(tx, removed, true);
75f51f2a 2705 }
a8ac403d
SB
2706 if (anchorBeforeDisconnect != anchorAfterDisconnect) {
2707 // The anchor may not change between block disconnects,
2708 // in which case we don't want to evict from the mempool yet!
2709 mempool.removeWithAnchor(anchorBeforeDisconnect);
2710 }
723d12c0 2711 mempool.removeCoinbaseSpends(pcoinsTip, pindexDelete->nHeight);
75f51f2a
PW
2712 mempool.check(pcoinsTip);
2713 // Update chainActive and related variables.
2714 UpdateTip(pindexDelete->pprev);
de42390f
JG
2715 // Get the current commitment tree
2716 ZCIncrementalMerkleTree newTree;
2717 assert(pcoinsTip->GetAnchorAt(pcoinsTip->GetBestAnchor(), newTree));
93a18a36
GA
2718 // Let wallets know transactions went from 1-confirmed to
2719 // 0-confirmed or conflicted:
2720 BOOST_FOREACH(const CTransaction &tx, block.vtx) {
d38da59b 2721 SyncWithWallets(tx, NULL);
93a18a36 2722 }
769e031c 2723 // Update cached incremental witnesses
de42390f 2724 GetMainSignals().ChainTip(pindexDelete, &block, newTree, false);
75f51f2a 2725 return true;
0ec16f35 2726}
d237f62c 2727
d70bc52e
PW
2728static int64_t nTimeReadFromDisk = 0;
2729static int64_t nTimeConnectTotal = 0;
2730static int64_t nTimeFlush = 0;
2731static int64_t nTimeChainState = 0;
2732static int64_t nTimePostConnect = 0;
2733
c5b390b6
MF
2734/**
2735 * Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
2736 * corresponding to pindexNew, to bypass loading it again from disk.
2737 */
92bb6f2f 2738bool static ConnectTip(CValidationState &state, CBlockIndex *pindexNew, CBlock *pblock) {
76374710 2739
75f51f2a 2740 assert(pindexNew->pprev == chainActive.Tip());
a0fa20a1 2741 mempool.check(pcoinsTip);
75f51f2a 2742 // Read block from disk.
d70bc52e 2743 int64_t nTime1 = GetTimeMicros();
75f51f2a 2744 CBlock block;
92bb6f2f 2745 if (!pblock) {
f2dd868d 2746 if (!ReadBlockFromDisk(block, pindexNew))
27afcd89 2747 return AbortNode(state, "Failed to read block");
92bb6f2f
PW
2748 pblock = &block;
2749 }
de42390f
JG
2750 // Get the current commitment tree
2751 ZCIncrementalMerkleTree oldTree;
2752 assert(pcoinsTip->GetAnchorAt(pcoinsTip->GetBestAnchor(), oldTree));
75f51f2a 2753 // Apply the block atomically to the chain state.
d70bc52e
PW
2754 int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1;
2755 int64_t nTime3;
2756 LogPrint("bench", " - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * 0.001, nTimeReadFromDisk * 0.000001);
0a61b0df 2757 {
7c70438d 2758 CCoinsViewCache view(pcoinsTip);
75f51f2a 2759 CInv inv(MSG_BLOCK, pindexNew->GetBlockHash());
24e88964 2760 bool rv = ConnectBlock(*pblock, state, pindexNew, view);
26c16d9d 2761 GetMainSignals().BlockChecked(*pblock, state);
24e88964 2762 if (!rv) {
75f51f2a
PW
2763 if (state.IsInvalid())
2764 InvalidBlockFound(pindexNew, state);
5262fde0 2765 return error("ConnectTip(): ConnectBlock %s failed", pindexNew->GetBlockHash().ToString());
7851033d 2766 }
75f51f2a 2767 mapBlockSource.erase(inv.hash);
d70bc52e
PW
2768 nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2;
2769 LogPrint("bench", " - Connect total: %.2fms [%.2fs]\n", (nTime3 - nTime2) * 0.001, nTimeConnectTotal * 0.000001);
75f51f2a 2770 assert(view.Flush());
0a61b0df 2771 }
d70bc52e
PW
2772 int64_t nTime4 = GetTimeMicros(); nTimeFlush += nTime4 - nTime3;
2773 LogPrint("bench", " - Flush: %.2fms [%.2fs]\n", (nTime4 - nTime3) * 0.001, nTimeFlush * 0.000001);
75f51f2a 2774 // Write the chain state to disk, if necessary.
a2069500 2775 if (!FlushStateToDisk(state, FLUSH_STATE_IF_NEEDED))
75f51f2a 2776 return false;
d70bc52e
PW
2777 int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
2778 LogPrint("bench", " - Writing chainstate: %.2fms [%.2fs]\n", (nTime5 - nTime4) * 0.001, nTimeChainState * 0.000001);
75f51f2a 2779 // Remove conflicting transactions from the mempool.
93a18a36 2780 list<CTransaction> txConflicted;
b649e039 2781 mempool.removeForBlock(pblock->vtx, pindexNew->nHeight, txConflicted, !IsInitialBlockDownload());
75f51f2a
PW
2782 mempool.check(pcoinsTip);
2783 // Update chainActive & related variables.
880b2931 2784 UpdateTip(pindexNew);
93a18a36
GA
2785 // Tell wallet about transactions that went from mempool
2786 // to conflicted:
2787 BOOST_FOREACH(const CTransaction &tx, txConflicted) {
d38da59b 2788 SyncWithWallets(tx, NULL);
93a18a36
GA
2789 }
2790 // ... and about transactions that got confirmed:
92bb6f2f
PW
2791 BOOST_FOREACH(const CTransaction &tx, pblock->vtx) {
2792 SyncWithWallets(tx, pblock);
93a18a36 2793 }
769e031c 2794 // Update cached incremental witnesses
de42390f 2795 GetMainSignals().ChainTip(pindexNew, pblock, oldTree, true);
d920f7dc 2796
d70bc52e
PW
2797 int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
2798 LogPrint("bench", " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001);
2799 LogPrint("bench", "- Connect block: %.2fms [%.2fs]\n", (nTime6 - nTime1) * 0.001, nTimeTotal * 0.000001);
0a61b0df 2800 return true;
2801}
2802
c5b390b6
MF
2803/**
2804 * Return the tip of the chain with the most work in it, that isn't
2805 * known to be invalid (it's however far from certain to be valid).
2806 */
77339e5a 2807static CBlockIndex* FindMostWorkChain() {
75f51f2a 2808 do {
77339e5a
PW
2809 CBlockIndex *pindexNew = NULL;
2810
75f51f2a
PW
2811 // Find the best candidate header.
2812 {
e17bd583
PW
2813 std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexCandidates.rbegin();
2814 if (it == setBlockIndexCandidates.rend())
77339e5a 2815 return NULL;
75f51f2a
PW
2816 pindexNew = *it;
2817 }
2818
2819 // Check whether all blocks on the path between the currently active chain and the candidate are valid.
2820 // Just going until the active chain is an optimization, as we know all blocks in it are valid already.
2821 CBlockIndex *pindexTest = pindexNew;
2822 bool fInvalidAncestor = false;
2823 while (pindexTest && !chainActive.Contains(pindexTest)) {
341735eb 2824 assert(pindexTest->nChainTx || pindexTest->nHeight == 0);
f9ec3f0f 2825
2826 // Pruned nodes may have entries in setBlockIndexCandidates for
2827 // which block files have been deleted. Remove those as candidates
2828 // for the most work chain if we come across them; we can't switch
2829 // to a chain unless we have all the non-active-chain parent blocks.
2830 bool fFailedChain = pindexTest->nStatus & BLOCK_FAILED_MASK;
2831 bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
2832 if (fFailedChain || fMissingData) {
2833 // Candidate chain is not usable (either invalid or missing data)
2834 if (fFailedChain && (pindexBestInvalid == NULL || pindexNew->nChainWork > pindexBestInvalid->nChainWork))
942b33a1
PW
2835 pindexBestInvalid = pindexNew;
2836 CBlockIndex *pindexFailed = pindexNew;
f9ec3f0f 2837 // Remove the entire chain from the set.
75f51f2a 2838 while (pindexTest != pindexFailed) {
f9ec3f0f 2839 if (fFailedChain) {
2840 pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
2841 } else if (fMissingData) {
2842 // If we're missing data, then add back to mapBlocksUnlinked,
2843 // so that if the block arrives in the future we can try adding
2844 // to setBlockIndexCandidates again.
2845 mapBlocksUnlinked.insert(std::make_pair(pindexFailed->pprev, pindexFailed));
2846 }
e17bd583 2847 setBlockIndexCandidates.erase(pindexFailed);
75f51f2a
PW
2848 pindexFailed = pindexFailed->pprev;
2849 }
e17bd583 2850 setBlockIndexCandidates.erase(pindexTest);
75f51f2a
PW
2851 fInvalidAncestor = true;
2852 break;
ef3988ca 2853 }
75f51f2a 2854 pindexTest = pindexTest->pprev;
0a61b0df 2855 }
77339e5a
PW
2856 if (!fInvalidAncestor)
2857 return pindexNew;
75f51f2a 2858 } while(true);
75f51f2a 2859}
0a61b0df 2860
c5b390b6 2861/** Delete all entries in setBlockIndexCandidates that are worse than the current tip. */
cca48f69 2862static void PruneBlockIndexCandidates() {
2863 // Note that we can't delete the current block itself, as we may need to return to it later in case a
2864 // reorganization to a better block fails.
2865 std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
34559c7c 2866 while (it != setBlockIndexCandidates.end() && setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) {
cca48f69 2867 setBlockIndexCandidates.erase(it++);
2868 }
34559c7c
PW
2869 // Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates.
2870 assert(!setBlockIndexCandidates.empty());
cca48f69 2871}
2872
c5b390b6
MF
2873/**
2874 * Try to make some progress towards making pindexMostWork the active block.
2875 * pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
2876 */
92bb6f2f 2877static bool ActivateBestChainStep(CValidationState &state, CBlockIndex *pindexMostWork, CBlock *pblock) {
4e0eed88 2878 AssertLockHeld(cs_main);
202e0194 2879 bool fInvalidFound = false;
b33bd7a3
DK
2880 const CBlockIndex *pindexOldTip = chainActive.Tip();
2881 const CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork);
0a61b0df 2882
4e0eed88
PW
2883 // Disconnect active blocks which are no longer in the best chain.
2884 while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
2885 if (!DisconnectTip(state))
2886 return false;
2887 }
8abcd819 2888 if ( KOMODO_REWIND != 0 )
8985164d 2889 {
7d106717 2890 fprintf(stderr,"rewind start ht.%d\n",chainActive.Tip()->nHeight);
6ec8416f 2891 while ( KOMODO_REWIND > 0 && chainActive.Tip()->nHeight > KOMODO_REWIND )
297a4978 2892 {
310bb0a1 2893 if ( !DisconnectTip(state) )
297a4978 2894 {
d8be8b2e 2895 InvalidateBlock(state,chainActive.Tip());
45142781 2896 break;
310bb0a1 2897 }
67d2b8b9 2898 }
d8be8b2e 2899 fprintf(stderr,"reached rewind.%d, best to do: ./komodo-cli stop\n",KOMODO_REWIND);
6ec8416f 2900 sleep(60);
d8be8b2e 2901 KOMODO_REWIND = 0;
2902 return(true);
2903 }
4e0eed88
PW
2904 // Build list of new blocks to connect.
2905 std::vector<CBlockIndex*> vpindexToConnect;
afc32c5e
PW
2906 bool fContinue = true;
2907 int nHeight = pindexFork ? pindexFork->nHeight : -1;
2908 while (fContinue && nHeight != pindexMostWork->nHeight) {
2909 // Don't iterate the entire list of potential improvements toward the best tip, as we likely only need
2910 // a few blocks along the way.
2911 int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight);
2912 vpindexToConnect.clear();
2913 vpindexToConnect.reserve(nTargetHeight - nHeight);
2914 CBlockIndex *pindexIter = pindexMostWork->GetAncestor(nTargetHeight);
2915 while (pindexIter && pindexIter->nHeight != nHeight) {
92bb6f2f
PW
2916 vpindexToConnect.push_back(pindexIter);
2917 pindexIter = pindexIter->pprev;
4e0eed88 2918 }
afc32c5e 2919 nHeight = nTargetHeight;
77339e5a 2920
4e0eed88
PW
2921 // Connect new blocks.
2922 BOOST_REVERSE_FOREACH(CBlockIndex *pindexConnect, vpindexToConnect) {
92bb6f2f 2923 if (!ConnectTip(state, pindexConnect, pindexConnect == pindexMostWork ? pblock : NULL)) {
4e0eed88
PW
2924 if (state.IsInvalid()) {
2925 // The block violates a consensus rule.
2926 if (!state.CorruptionPossible())
2927 InvalidChainFound(vpindexToConnect.back());
2928 state = CValidationState();
202e0194 2929 fInvalidFound = true;
afc32c5e 2930 fContinue = false;
4e0eed88
PW
2931 break;
2932 } else {
2933 // A system error occurred (disk space, database error, ...).
2934 return false;
2935 }
2936 } else {
cca48f69 2937 PruneBlockIndexCandidates();
4e0eed88
PW
2938 if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
2939 // We're in a better position than we were. Return temporarily to release the lock.
afc32c5e 2940 fContinue = false;
4e0eed88 2941 break;
75f51f2a
PW
2942 }
2943 }
231b3999 2944 }
afc32c5e 2945 }
0a61b0df 2946
202e0194
PW
2947 // Callbacks/notifications for a new best chain.
2948 if (fInvalidFound)
2949 CheckForkWarningConditionsOnNewFork(vpindexToConnect.back());
2950 else
2951 CheckForkWarningConditions();
2952
0a61b0df 2953 return true;
2954}
0a61b0df 2955
c5b390b6
MF
2956/**
2957 * Make the best chain active, in multiple steps. The result is either failure
2958 * or an activated best chain. pblock is either NULL or a pointer to a block
2959 * that is already loaded (to avoid loading it again from disk).
2960 */
92bb6f2f 2961bool ActivateBestChain(CValidationState &state, CBlock *pblock) {
202e0194
PW
2962 CBlockIndex *pindexNewTip = NULL;
2963 CBlockIndex *pindexMostWork = NULL;
11982d36 2964 const CChainParams& chainParams = Params();
4e0eed88
PW
2965 do {
2966 boost::this_thread::interruption_point();
2967
202e0194
PW
2968 bool fInitialDownload;
2969 {
2970 LOCK(cs_main);
2971 pindexMostWork = FindMostWorkChain();
4e0eed88 2972
202e0194
PW
2973 // Whether we have anything to do at all.
2974 if (pindexMostWork == NULL || pindexMostWork == chainActive.Tip())
2975 return true;
4e0eed88 2976
92bb6f2f 2977 if (!ActivateBestChainStep(state, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : NULL))
9083591a 2978 return false;
202e0194
PW
2979 pindexNewTip = chainActive.Tip();
2980 fInitialDownload = IsInitialBlockDownload();
2981 }
2982 // When we reach this point, we switched to a new tip (stored in pindexNewTip).
2983
2984 // Notifications/callbacks that can run without cs_main
2985 if (!fInitialDownload) {
2986 uint256 hashNewTip = pindexNewTip->GetBlockHash();
2987 // Relay inventory, but don't relay old inventory during initial block download.
a8cdaf5c
CF
2988 int nBlockEstimate = 0;
2989 if (fCheckpointsEnabled)
2990 nBlockEstimate = Checkpoints::GetTotalBlocksEstimate(chainParams.Checkpoints());
f9ec3f0f 2991 // Don't relay blocks if pruning -- could cause a peer to try to download, resulting
2992 // in a stalled download if the block file is pruned before the request.
2993 if (nLocalServices & NODE_NETWORK) {
4dc5eb05
PK
2994 LOCK(cs_vNodes);
2995 BOOST_FOREACH(CNode* pnode, vNodes)
2996 if (chainActive.Height() > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate))
2997 pnode->PushInventory(CInv(MSG_BLOCK, hashNewTip));
202e0194 2998 }
51ce901a 2999 // Notify external listeners about the new tip.
c7b6117d 3000 uiInterface.NotifyBlockTip(hashNewTip);
b11963b5 3001 } //else fprintf(stderr,"initial download skips propagation\n");
202e0194 3002 } while(pindexMostWork != chainActive.Tip());
3fcfbc8a 3003 CheckBlockIndex();
4e0eed88 3004
51ce901a 3005 // Write changes periodically to disk, after relay.
a2069500 3006 if (!FlushStateToDisk(state, FLUSH_STATE_PERIODIC)) {
51ce901a
PW
3007 return false;
3008 }
3009
4e0eed88
PW
3010 return true;
3011}
942b33a1 3012
9b0a8d31
PW
3013bool InvalidateBlock(CValidationState& state, CBlockIndex *pindex) {
3014 AssertLockHeld(cs_main);
3015
3016 // Mark the block itself as invalid.
3017 pindex->nStatus |= BLOCK_FAILED_VALID;
0dd06b25 3018 setDirtyBlockIndex.insert(pindex);
9b0a8d31
PW
3019 setBlockIndexCandidates.erase(pindex);
3020
3021 while (chainActive.Contains(pindex)) {
3022 CBlockIndex *pindexWalk = chainActive.Tip();
3023 pindexWalk->nStatus |= BLOCK_FAILED_CHILD;
0dd06b25 3024 setDirtyBlockIndex.insert(pindexWalk);
9b0a8d31
PW
3025 setBlockIndexCandidates.erase(pindexWalk);
3026 // ActivateBestChain considers blocks already in chainActive
3027 // unconditionally valid already, so force disconnect away from it.
3028 if (!DisconnectTip(state)) {
3029 return false;
3030 }
3031 }
3032
3033 // The resulting new best tip may not be in setBlockIndexCandidates anymore, so
b05a89b2 3034 // add it again.
9b0a8d31
PW
3035 BlockMap::iterator it = mapBlockIndex.begin();
3036 while (it != mapBlockIndex.end()) {
cd3d67cf 3037 if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && !setBlockIndexCandidates.value_comp()(it->second, chainActive.Tip())) {
a9af4158 3038 setBlockIndexCandidates.insert(it->second);
9b0a8d31
PW
3039 }
3040 it++;
3041 }
3042
3043 InvalidChainFound(pindex);
3044 return true;
3045}
3046
3047bool ReconsiderBlock(CValidationState& state, CBlockIndex *pindex) {
3048 AssertLockHeld(cs_main);
3049
3050 int nHeight = pindex->nHeight;
3051
3052 // Remove the invalidity flag from this block and all its descendants.
3053 BlockMap::iterator it = mapBlockIndex.begin();
3054 while (it != mapBlockIndex.end()) {
3055 if (!it->second->IsValid() && it->second->GetAncestor(nHeight) == pindex) {
3056 it->second->nStatus &= ~BLOCK_FAILED_MASK;
0dd06b25 3057 setDirtyBlockIndex.insert(it->second);
9b0a8d31
PW
3058 if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && setBlockIndexCandidates.value_comp()(chainActive.Tip(), it->second)) {
3059 setBlockIndexCandidates.insert(it->second);
3060 }
3061 if (it->second == pindexBestInvalid) {
3062 // Reset invalid block marker if it was pointing to one of those.
3063 pindexBestInvalid = NULL;
3064 }
3065 }
3066 it++;
3067 }
3068
3069 // Remove the invalidity flag from all ancestors too.
3070 while (pindex != NULL) {
0dd06b25
PW
3071 if (pindex->nStatus & BLOCK_FAILED_MASK) {
3072 pindex->nStatus &= ~BLOCK_FAILED_MASK;
3073 setDirtyBlockIndex.insert(pindex);
9b0a8d31
PW
3074 }
3075 pindex = pindex->pprev;
3076 }
3077 return true;
3078}
3079
341735eb 3080CBlockIndex* AddToBlockIndex(const CBlockHeader& block)
0a61b0df 3081{
3082 // Check for duplicate
1959997a 3083 uint256 hash = block.GetHash();
145d5be8 3084 BlockMap::iterator it = mapBlockIndex.find(hash);
942b33a1
PW
3085 if (it != mapBlockIndex.end())
3086 return it->second;
0a61b0df 3087
3088 // Construct new block index object
1959997a 3089 CBlockIndex* pindexNew = new CBlockIndex(block);
94c8bfb2 3090 assert(pindexNew);
341735eb
PW
3091 // We assign the sequence id to blocks only when the full data is available,
3092 // to avoid miners withholding blocks but broadcasting headers, to get a
3093 // competitive advantage.
3094 pindexNew->nSequenceId = 0;
145d5be8 3095 BlockMap::iterator mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
0a61b0df 3096 pindexNew->phashBlock = &((*mi).first);
145d5be8 3097 BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock);
0a61b0df 3098 if (miPrev != mapBlockIndex.end())
3099 {
3100 pindexNew->pprev = (*miPrev).second;
3101 pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
c9a09183 3102 pindexNew->BuildSkip();
0a61b0df 3103 }
092b58d1 3104 pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew);
942b33a1 3105 pindexNew->RaiseValidity(BLOCK_VALID_TREE);
341735eb
PW
3106 if (pindexBestHeader == NULL || pindexBestHeader->nChainWork < pindexNew->nChainWork)
3107 pindexBestHeader = pindexNew;
3108
51ce901a 3109 setDirtyBlockIndex.insert(pindexNew);
942b33a1
PW
3110
3111 return pindexNew;
3112}
3113
c5b390b6 3114/** Mark a block as having its data received and checked (up to BLOCK_VALID_TRANSACTIONS). */
942b33a1
PW
3115bool ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBlockIndex *pindexNew, const CDiskBlockPos& pos)
3116{
3117 pindexNew->nTx = block.vtx.size();
341735eb 3118 pindexNew->nChainTx = 0;
857c61df
PW
3119 pindexNew->nFile = pos.nFile;
3120 pindexNew->nDataPos = pos.nPos;
5382bcf8 3121 pindexNew->nUndoPos = 0;
942b33a1 3122 pindexNew->nStatus |= BLOCK_HAVE_DATA;
341735eb 3123 pindexNew->RaiseValidity(BLOCK_VALID_TRANSACTIONS);
51ce901a 3124 setDirtyBlockIndex.insert(pindexNew);
942b33a1 3125
341735eb
PW
3126 if (pindexNew->pprev == NULL || pindexNew->pprev->nChainTx) {
3127 // If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
3128 deque<CBlockIndex*> queue;
3129 queue.push_back(pindexNew);
0a61b0df 3130
341735eb
PW
3131 // Recursively process any descendant blocks that now may be eligible to be connected.
3132 while (!queue.empty()) {
3133 CBlockIndex *pindex = queue.front();
3134 queue.pop_front();
3135 pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
c1ecee8f
SD
3136 {
3137 LOCK(cs_nBlockSequenceId);
3138 pindex->nSequenceId = nBlockSequenceId++;
3139 }
3fcfbc8a
PW
3140 if (chainActive.Tip() == NULL || !setBlockIndexCandidates.value_comp()(pindex, chainActive.Tip())) {
3141 setBlockIndexCandidates.insert(pindex);
3142 }
341735eb
PW
3143 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex);
3144 while (range.first != range.second) {
3145 std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
3146 queue.push_back(it->second);
3147 range.first++;
3148 mapBlocksUnlinked.erase(it);
3149 }
341735eb
PW
3150 }
3151 } else {
3152 if (pindexNew->pprev && pindexNew->pprev->IsValid(BLOCK_VALID_TREE)) {
3153 mapBlocksUnlinked.insert(std::make_pair(pindexNew->pprev, pindexNew));
3154 }
341735eb 3155 }
0a61b0df 3156
18e72167 3157 return true;
0a61b0df 3158}
3159
51ed9ec9 3160bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
5382bcf8 3161{
5382bcf8
PW
3162 LOCK(cs_LastBlockFile);
3163
ed6d1a2c
PW
3164 unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile;
3165 if (vinfoBlockFile.size() <= nFile) {
3166 vinfoBlockFile.resize(nFile + 1);
3167 }
3168
3169 if (!fKnown) {
3170 while (vinfoBlockFile[nFile].nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
ed6d1a2c
PW
3171 nFile++;
3172 if (vinfoBlockFile.size() <= nFile) {
3173 vinfoBlockFile.resize(nFile + 1);
3174 }
7fea4846 3175 }
ed6d1a2c
PW
3176 pos.nFile = nFile;
3177 pos.nPos = vinfoBlockFile[nFile].nSize;
5382bcf8
PW
3178 }
3179
4e895b08
PW
3180 if (nFile != nLastBlockFile) {
3181 if (!fKnown) {
3182 LogPrintf("Leaving block file %i: %s\n", nFile, vinfoBlockFile[nFile].ToString());
3183 }
3184 FlushBlockFile(!fKnown);
3185 nLastBlockFile = nFile;
3186 }
3187
ed6d1a2c 3188 vinfoBlockFile[nFile].AddBlock(nHeight, nTime);
bb6acff0
CF
3189 if (fKnown)
3190 vinfoBlockFile[nFile].nSize = std::max(pos.nPos + nAddSize, vinfoBlockFile[nFile].nSize);
3191 else
3192 vinfoBlockFile[nFile].nSize += nAddSize;
5382bcf8 3193
7fea4846
PW
3194 if (!fKnown) {
3195 unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
ed6d1a2c 3196 unsigned int nNewChunks = (vinfoBlockFile[nFile].nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
7fea4846 3197 if (nNewChunks > nOldChunks) {
f9ec3f0f 3198 if (fPruneMode)
3199 fCheckForPruning = true;
fa45c26a
PK
3200 if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
3201 FILE *file = OpenBlockFile(pos);
3202 if (file) {
881a85a2 3203 LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
fa45c26a
PK
3204 AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
3205 fclose(file);
3206 }
7fea4846 3207 }
fa45c26a 3208 else
c117d9e9 3209 return state.Error("out of disk space");
bba89aa8 3210 }
bba89aa8
PW
3211 }
3212
51ce901a 3213 setDirtyFileInfo.insert(nFile);
5382bcf8
PW
3214 return true;
3215}
3216
ef3988ca 3217bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
5382bcf8
PW
3218{
3219 pos.nFile = nFile;
3220
3221 LOCK(cs_LastBlockFile);
3222
bba89aa8 3223 unsigned int nNewSize;
ed6d1a2c
PW
3224 pos.nPos = vinfoBlockFile[nFile].nUndoSize;
3225 nNewSize = vinfoBlockFile[nFile].nUndoSize += nAddSize;
51ce901a 3226 setDirtyFileInfo.insert(nFile);
bba89aa8
PW
3227
3228 unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
3229 unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
3230 if (nNewChunks > nOldChunks) {
f9ec3f0f 3231 if (fPruneMode)
3232 fCheckForPruning = true;
fa45c26a
PK
3233 if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
3234 FILE *file = OpenUndoFile(pos);
3235 if (file) {
881a85a2 3236 LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
fa45c26a
PK
3237 AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
3238 fclose(file);
3239 }
bba89aa8 3240 }
fa45c26a 3241 else
c117d9e9 3242 return state.Error("out of disk space");
5382bcf8
PW
3243 }
3244
5382bcf8
PW
3245 return true;
3246}
3247
eea133f1 3248bool CheckBlockHeader(int32_t height,CBlockIndex *pindex, const CBlockHeader& blockhdr, CValidationState& state, bool fCheckPOW)
0a61b0df 3249{
f2dd868d 3250 uint8_t pubkey33[33];
d7426190 3251 // Check timestamp
e40b78e9 3252 if ( 0 )
c0dbb034 3253 {
3254 uint256 hash; int32_t i;
3255 hash = blockhdr.GetHash();
92266e99 3256 for (i=31; i>=0; i--)
c0dbb034 3257 fprintf(stderr,"%02x",((uint8_t *)&hash)[i]);
3258 fprintf(stderr," <- CheckBlockHeader\n");
807949f4 3259 if ( chainActive.Tip() != 0 )
3260 {
3261 hash = chainActive.Tip()->GetBlockHash();
3262 for (i=31; i>=0; i--)
3263 fprintf(stderr,"%02x",((uint8_t *)&hash)[i]);
3264 fprintf(stderr," <- chainTip\n");
3265 }
c0dbb034 3266 }
5dde7075 3267 if (blockhdr.GetBlockTime() > GetAdjustedTime() + 60)
d7426190 3268 return state.Invalid(error("CheckBlockHeader(): block timestamp too far in the future"),REJECT_INVALID, "time-too-new");
80f4cdcf 3269 // Check block version
30853e4a 3270 //if (block.nVersion < MIN_BLOCK_VERSION)
3271 // return state.DoS(100, error("CheckBlockHeader(): block version too low"),REJECT_INVALID, "version-too-low");
80f4cdcf 3272
f2dd868d 3273 // Check Equihash solution is valid
de6724df 3274 if ( fCheckPOW && !CheckEquihashSolution(&blockhdr, Params()) )
f2dd868d 3275 return state.DoS(100, error("CheckBlockHeader(): Equihash solution invalid"),REJECT_INVALID, "invalid-solution");
3276
3277 // Check proof of work matches claimed amount
0d24f3ed 3278 komodo_index2pubkey33(pubkey33,pindex,height);
5dde7075 3279 if ( fCheckPOW && !CheckProofOfWork(height,pubkey33,blockhdr.GetHash(), blockhdr.nBits, Params().GetConsensus()) )
f2dd868d 3280 return state.DoS(50, error("CheckBlockHeader(): proof of work failed"),REJECT_INVALID, "high-hash");
f4573470
PW
3281 return true;
3282}
3283
c7a3aca6 3284int32_t komodo_check_deposit(int32_t height,const CBlock& block);
3ced9364 3285bool CheckBlock(int32_t height,CBlockIndex *pindex,const CBlock& block, CValidationState& state,
6fb8d0c2
JG
3286 libzcash::ProofVerifier& verifier,
3287 bool fCheckPOW, bool fCheckMerkleRoot)
0a61b0df 3288{
341735eb 3289 // These are checks that are independent of context.
0a61b0df 3290
57425a24
DK
3291 // Check that the header is valid (particularly PoW). This is mostly
3292 // redundant with the call in AcceptBlockHeader.
63ac81f0 3293 if (!CheckBlockHeader(height,pindex,block,state,fCheckPOW))
f4573470
PW
3294 return false;
3295
341735eb
PW
3296 // Check the merkle root.
3297 if (fCheckMerkleRoot) {
3298 bool mutated;
3299 uint256 hashMerkleRoot2 = block.BuildMerkleTree(&mutated);
3300 if (block.hashMerkleRoot != hashMerkleRoot2)
5262fde0 3301 return state.DoS(100, error("CheckBlock(): hashMerkleRoot mismatch"),
341735eb
PW
3302 REJECT_INVALID, "bad-txnmrklroot", true);
3303
3304 // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
3305 // of transactions in a block without affecting the merkle root of a block,
3306 // while still invalidating it.
3307 if (mutated)
5262fde0 3308 return state.DoS(100, error("CheckBlock(): duplicate transaction"),
341735eb
PW
3309 REJECT_INVALID, "bad-txns-duplicate", true);
3310 }
3311
3312 // All potential-corruption validation must be done before we do any
3313 // transaction validation, as otherwise we may mark the header as invalid
3314 // because we receive the wrong transactions for it.
3315
0a61b0df 3316 // Size limits
38991ffa 3317 if (block.vtx.empty() || block.vtx.size() > MAX_BLOCK_SIZE || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION) > MAX_BLOCK_SIZE)
5262fde0 3318 return state.DoS(100, error("CheckBlock(): size limits failed"),
14e7ffcc 3319 REJECT_INVALID, "bad-blk-length");
0a61b0df 3320
0a61b0df 3321 // First transaction must be coinbase, the rest must not be
38991ffa 3322 if (block.vtx.empty() || !block.vtx[0].IsCoinBase())
5262fde0 3323 return state.DoS(100, error("CheckBlock(): first tx is not coinbase"),
14e7ffcc 3324 REJECT_INVALID, "bad-cb-missing");
38991ffa
EL
3325 for (unsigned int i = 1; i < block.vtx.size(); i++)
3326 if (block.vtx[i].IsCoinBase())
5262fde0 3327 return state.DoS(100, error("CheckBlock(): more than one coinbase"),
14e7ffcc 3328 REJECT_INVALID, "bad-cb-multiple");
0a61b0df 3329
3330 // Check transactions
38991ffa 3331 BOOST_FOREACH(const CTransaction& tx, block.vtx)
837b94ee 3332 {
04585092 3333 uint32_t prevtime = 0; CBlockIndex *ptr;
4e4e39ab 3334 if ( chainActive.Tip() != 0 && height == chainActive.Tip()->nHeight+1 )
04585092 3335 prevtime = chainActive.Tip()->nTime;
3336 else if ( pindex != 0 )
837b94ee 3337 {
04585092 3338 if ( (ptr= pindex->pprev) != 0 )
3339 prevtime = ptr->nTime;
3340 }
3341 if ( prevtime == 0 )
3342 {
4e4e39ab 3343 if ( height > 0 && (ptr= chainActive[height-1]) != 0 )
04585092 3344 prevtime = ptr->nTime;
3345 }
3346 if ( komodo_validate_interest(0,tx,height,prevtime) < 0 )
3347 {
4f7a5425 3348 //fprintf(stderr,"CheckBlock(%d:%d) %d, %u: komodo_validate_interest failure blocksize.%d tiptime.%u %u\n",height,komodo_block2height((CBlock *)&block),pindex!=0?(int32_t)pindex->nHeight:0,pindex!=0?(int32_t)pindex->nTime:0,(int32_t)block.vtx.size(),chainActive.Tip()->nTime,prevtime);
837b94ee 3349 return error("CheckBlock: komodo_validate_interest failed");
3350 }
6fb8d0c2 3351 if (!CheckTransaction(tx, state, verifier))
5262fde0 3352 return error("CheckBlock(): CheckTransaction failed");
837b94ee 3353 }
7bd9c3a3 3354 unsigned int nSigOps = 0;
38991ffa 3355 BOOST_FOREACH(const CTransaction& tx, block.vtx)
e679ec96 3356 {
05df3fc6 3357 nSigOps += GetLegacySigOpCount(tx);
e679ec96
GA
3358 }
3359 if (nSigOps > MAX_BLOCK_SIGOPS)
5262fde0 3360 return state.DoS(100, error("CheckBlock(): out-of-bounds SigOpCount"),
14e7ffcc 3361 REJECT_INVALID, "bad-blk-sigops", true);
15a8c930 3362 if ( komodo_check_deposit(ASSETCHAINS_SYMBOL[0] == 0 ? height : pindex != 0 ? (int32_t)pindex->nHeight : chainActive.Tip()->nHeight+1,block) < 0 )
e699e13d 3363 {
541f9019 3364 static uint32_t counter;
3365 if ( counter++ < 100 )
3366 fprintf(stderr,"check deposit rejection\n");
59642d51 3367 return(false);
e699e13d 3368 }
0a61b0df 3369 return true;
3370}
3371
a48f2d6d
LD
3372bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex * const pindexPrev)
3373{
11982d36
CF
3374 const CChainParams& chainParams = Params();
3375 const Consensus::Params& consensusParams = chainParams.GetConsensus();
a48f2d6d 3376 uint256 hash = block.GetHash();
4e382177 3377 if (hash == consensusParams.hashGenesisBlock)
a48f2d6d
LD
3378 return true;
3379
3380 assert(pindexPrev);
3381
3382 int nHeight = pindexPrev->nHeight+1;
3383
3384 // Check proof of work
36f1b84b 3385 if ( (nHeight < 235300 || nHeight > 236000) && block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
5645d111 3386 {
c939721c 3387 cout << block.nBits << " block.nBits vs. calc " << GetNextWorkRequired(pindexPrev, &block, consensusParams) << endl;
5262fde0 3388 return state.DoS(100, error("%s: incorrect proof of work", __func__),
a48f2d6d 3389 REJECT_INVALID, "bad-diffbits");
5645d111 3390 }
a48f2d6d
LD
3391
3392 // Check timestamp against prev
3393 if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
5262fde0 3394 return state.Invalid(error("%s: block's timestamp is too early", __func__),
a48f2d6d
LD
3395 REJECT_INVALID, "time-too-old");
3396
a8cdaf5c
CF
3397 if(fCheckpointsEnabled)
3398 {
3399 // Check that the block chain matches the known block chain up to a checkpoint
3400 if (!Checkpoints::CheckBlock(chainParams.Checkpoints(), nHeight, hash))
5645d111 3401 return state.DoS(100, error("%s: rejected by checkpoint lock-in at %d", __func__, nHeight),REJECT_CHECKPOINT, "checkpoint mismatch");
a48f2d6d 3402
a8cdaf5c
CF
3403 // Don't accept any forks from the main chain prior to last checkpoint
3404 CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainParams.Checkpoints());
b62d7030 3405 int32_t notarized_height;
a8cdaf5c 3406 if (pcheckpoint && nHeight < pcheckpoint->nHeight)
602dc744 3407 return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d) vs %d", __func__, nHeight,pcheckpoint->nHeight));
b62d7030 3408 else if ( komodo_checkpoint(&notarized_height,nHeight,hash) < 0 )
3409 return state.DoS(100, error("%s: forked chain %d older than last notarized (height %d) vs %d", __func__,nHeight, notarized_height));
a8cdaf5c 3410 }
542da618
SB
3411 // Reject block.nVersion < 4 blocks
3412 if (block.nVersion < 4)
3413 return state.Invalid(error("%s : rejected nVersion<4 block", __func__),
5e82e1c8
PT
3414 REJECT_OBSOLETE, "bad-version");
3415
a48f2d6d
LD
3416 return true;
3417}
3418
3419bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex * const pindexPrev)
3420{
3421 const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1;
51aa2492 3422 const Consensus::Params& consensusParams = Params().GetConsensus();
a48f2d6d
LD
3423
3424 // Check that all transactions are finalized
a1d3c6fb
MF
3425 BOOST_FOREACH(const CTransaction& tx, block.vtx) {
3426 int nLockTimeFlags = 0;
3427 int64_t nLockTimeCutoff = (nLockTimeFlags & LOCKTIME_MEDIAN_TIME_PAST)
3428 ? pindexPrev->GetMedianTimePast()
3429 : block.GetBlockTime();
0cb1d2da 3430 if (!IsFinalTx(0,tx, nHeight, nLockTimeCutoff,0*STANDARD_LOCKTIME_VERIFY_FLAGS,pindexPrev->nTime)) {
5262fde0 3431 return state.DoS(10, error("%s: contains a non-final transaction", __func__), REJECT_INVALID, "bad-txns-nonfinal");
a48f2d6d 3432 }
a1d3c6fb 3433 }
a48f2d6d
LD
3434
3435 // Enforce block.nVersion=2 rule that the coinbase starts with serialized block height
3436 // if 750 of the last 1,000 blocks are version 2 or greater (51/100 if testnet):
548bbd95
JG
3437 // Since MIN_BLOCK_VERSION = 4 all blocks with nHeight > 0 should satisfy this.
3438 // This rule is not applied to the genesis block, which didn't include the height
3439 // in the coinbase.
3440 if (nHeight > 0)
a48f2d6d
LD
3441 {
3442 CScript expect = CScript() << nHeight;
3443 if (block.vtx[0].vin[0].scriptSig.size() < expect.size() ||
3444 !std::equal(expect.begin(), expect.end(), block.vtx[0].vin[0].scriptSig.begin())) {
16cd9f2d 3445 return state.DoS(100, error("%s: block height mismatch in coinbase", __func__), REJECT_INVALID, "bad-cb-height");
a48f2d6d
LD
3446 }
3447 }
3448
3449 return true;
3450}
3451
341735eb 3452bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex** ppindex)
0a61b0df 3453{
4e382177 3454 const CChainParams& chainparams = Params();
e07c943c 3455 AssertLockHeld(cs_main);
0a61b0df 3456 // Check for duplicate
2a4d3464 3457 uint256 hash = block.GetHash();
145d5be8 3458 BlockMap::iterator miSelf = mapBlockIndex.find(hash);
942b33a1
PW
3459 CBlockIndex *pindex = NULL;
3460 if (miSelf != mapBlockIndex.end()) {
341735eb 3461 // Block header is already known.
942b33a1 3462 pindex = miSelf->second;
341735eb
PW
3463 if (ppindex)
3464 *ppindex = pindex;
34ad681a 3465 if (pindex != 0 && pindex->nStatus & BLOCK_FAILED_MASK)
5262fde0 3466 return state.Invalid(error("%s: block is marked invalid", __func__), 0, "duplicate");
341735eb 3467 return true;
942b33a1 3468 }
0a61b0df 3469
5f197aee 3470 if (!CheckBlockHeader(*ppindex!=0?(*ppindex)->nHeight:0,*ppindex, block, state))
3471 return false;
57425a24 3472
0a61b0df 3473 // Get prev block index
7fea4846 3474 CBlockIndex* pindexPrev = NULL;
4e382177 3475 if (hash != chainparams.GetConsensus().hashGenesisBlock) {
145d5be8 3476 BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
b56585d0 3477 if (mi == mapBlockIndex.end())
5262fde0 3478 return state.DoS(10, error("%s: prev block not found", __func__), 0, "bad-prevblk");
b56585d0 3479 pindexPrev = (*mi).second;
16514911 3480 if (pindexPrev == 0 || (pindexPrev->nStatus & BLOCK_FAILED_MASK) )
5262fde0 3481 return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
942b33a1 3482 }
a48f2d6d
LD
3483 if (!ContextualCheckBlockHeader(block, state, pindexPrev))
3484 return false;
942b33a1
PW
3485 if (pindex == NULL)
3486 pindex = AddToBlockIndex(block);
942b33a1
PW
3487 if (ppindex)
3488 *ppindex = pindex;
942b33a1
PW
3489 return true;
3490}
3491
304892fc 3492bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex** ppindex, bool fRequested, CDiskBlockPos* dbp)
942b33a1 3493{
e6973430 3494 const CChainParams& chainparams = Params();
942b33a1
PW
3495 AssertLockHeld(cs_main);
3496
3497 CBlockIndex *&pindex = *ppindex;
942b33a1
PW
3498 if (!AcceptBlockHeader(block, state, &pindex))
3499 return false;
1e9dc6a8 3500 if ( pindex == 0 )
3501 {
3502 fprintf(stderr,"AcceptBlock error null pindex\n");
3503 return false;
3504 }
304892fc
SD
3505 // Try to process all requested blocks that we don't have, but only
3506 // process an unrequested block if it's new and has enough work to
93b606ae 3507 // advance our tip, and isn't too many blocks ahead.
304892fc
SD
3508 bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA;
3509 bool fHasMoreWork = (chainActive.Tip() ? pindex->nChainWork > chainActive.Tip()->nChainWork : true);
93b606ae
SD
3510 // Blocks that are too out-of-order needlessly limit the effectiveness of
3511 // pruning, because pruning will not delete block files that contain any
3512 // blocks which are too close in height to the tip. Apply this test
3513 // regardless of whether pruning is enabled; it should generally be safe to
3514 // not process unrequested blocks.
3515 bool fTooFarAhead = (pindex->nHeight > int(chainActive.Height() + MIN_BLOCKS_TO_KEEP));
304892fc
SD
3516
3517 // TODO: deal better with return value and error conditions for duplicate
3518 // and unrequested blocks.
3519 if (fAlreadyHave) return true;
3520 if (!fRequested) { // If we didn't ask for it:
3521 if (pindex->nTx != 0) return true; // This is a previously-processed block that was pruned
3522 if (!fHasMoreWork) return true; // Don't process less-work chains
93b606ae 3523 if (fTooFarAhead) return true; // Block height is too high
341735eb
PW
3524 }
3525
6fb8d0c2
JG
3526 // See method docstring for why this is always disabled
3527 auto verifier = libzcash::ProofVerifier::Disabled();
f10bf3ab 3528 if ((!CheckBlock(pindex->nHeight,pindex,block, state, verifier)) || !ContextualCheckBlock(block, state, pindex->pprev)) {
43005cff 3529 if (state.IsInvalid() && !state.CorruptionPossible()) {
942b33a1 3530 pindex->nStatus |= BLOCK_FAILED_VALID;
51ce901a 3531 setDirtyBlockIndex.insert(pindex);
942b33a1
PW
3532 }
3533 return false;
3534 }
3535
3536 int nHeight = pindex->nHeight;
942b33a1 3537
0a61b0df 3538 // Write block to history file
421218d3 3539 try {
2a4d3464 3540 unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
421218d3
PW
3541 CDiskBlockPos blockPos;
3542 if (dbp != NULL)
3543 blockPos = *dbp;
209377a7 3544 if (!FindBlockPos(state, blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != NULL))
5262fde0 3545 return error("AcceptBlock(): FindBlockPos failed");
421218d3 3546 if (dbp == NULL)
e6973430 3547 if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
27afcd89 3548 AbortNode(state, "Failed to write block");
942b33a1 3549 if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
5262fde0 3550 return error("AcceptBlock(): ReceivedBlockTransactions failed");
27df4123 3551 } catch (const std::runtime_error& e) {
27afcd89 3552 return AbortNode(state, std::string("System error: ") + e.what());
421218d3 3553 }
0a61b0df 3554
f9ec3f0f 3555 if (fCheckForPruning)
3556 FlushStateToDisk(state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
3557
0a61b0df 3558 return true;
3559}
3560
51aa2492 3561static bool IsSuperMajority(int minVersion, const CBlockIndex* pstart, unsigned nRequired, const Consensus::Params& consensusParams)
de237cbf
GA
3562{
3563 unsigned int nFound = 0;
51aa2492 3564 for (int i = 0; i < consensusParams.nMajorityWindow && nFound < nRequired && pstart != NULL; i++)
de237cbf
GA
3565 {
3566 if (pstart->nVersion >= minVersion)
3567 ++nFound;
3568 pstart = pstart->pprev;
3569 }
3570 return (nFound >= nRequired);
3571}
3572
c75c18fc 3573void komodo_currentheight_set(int32_t height);
c9a09183 3574
35915149 3575bool ProcessNewBlock(int32_t height,CValidationState &state, CNode* pfrom, CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp)
0a61b0df 3576{
0a61b0df 3577 // Preliminary checks
a5355664 3578 bool checked;
6fb8d0c2 3579 auto verifier = libzcash::ProofVerifier::Disabled();
6ae728c7 3580 if ( chainActive.Tip() != 0 )
c75c18fc 3581 komodo_currentheight_set(chainActive.Tip()->nHeight);
a5355664 3582 if ( ASSETCHAINS_SYMBOL[0] == 0 )
3ced9364 3583 checked = CheckBlock(height!=0?height:komodo_block2height(pblock),0,*pblock, state, verifier);
84e0ca8e 3584 else checked = CheckBlock(height!=0?height:komodo_block2height(pblock),0,*pblock, state, verifier);
0a61b0df 3585 {
341735eb 3586 LOCK(cs_main);
304892fc
SD
3587 bool fRequested = MarkBlockAsReceived(pblock->GetHash());
3588 fRequested |= fForceProcessing;
341735eb 3589 if (!checked) {
50c490cb 3590 if ( pfrom != 0 )
fe43f943 3591 Misbehaving(pfrom->GetId(), 1);
5262fde0 3592 return error("%s: CheckBlock FAILED", __func__);
5c88e3c1 3593 }
0a61b0df 3594
341735eb
PW
3595 // Store to disk
3596 CBlockIndex *pindex = NULL;
304892fc 3597 bool ret = AcceptBlock(*pblock, state, &pindex, fRequested, dbp);
341735eb
PW
3598 if (pindex && pfrom) {
3599 mapBlockSource[pindex->GetBlockHash()] = pfrom->GetId();
0a61b0df 3600 }
3fcfbc8a 3601 CheckBlockIndex();
341735eb 3602 if (!ret)
5262fde0 3603 return error("%s: AcceptBlock FAILED", __func__);
18e72167
PW
3604 }
3605
92bb6f2f 3606 if (!ActivateBestChain(state, pblock))
5262fde0 3607 return error("%s: ActivateBestChain failed", __func__);
18e72167 3608
0a61b0df 3609 return true;
3610}
3611
df08a626
LD
3612bool TestBlockValidity(CValidationState &state, const CBlock& block, CBlockIndex * const pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
3613{
3614 AssertLockHeld(cs_main);
3615 assert(pindexPrev == chainActive.Tip());
3616
3617 CCoinsViewCache viewNew(pcoinsTip);
3618 CBlockIndex indexDummy(block);
3619 indexDummy.pprev = pindexPrev;
3620 indexDummy.nHeight = pindexPrev->nHeight + 1;
6fb8d0c2
JG
3621 // JoinSplit proofs are verified in ConnectBlock
3622 auto verifier = libzcash::ProofVerifier::Disabled();
df08a626
LD
3623
3624 // NOTE: CheckBlockHeader is called by CheckBlock
3625 if (!ContextualCheckBlockHeader(block, state, pindexPrev))
d4190a2a 3626 {
3627 fprintf(stderr,"TestBlockValidity failure A\n");
df08a626 3628 return false;
d4190a2a 3629 }
3ced9364 3630 if (!CheckBlock(indexDummy.nHeight,0,block, state, verifier, fCheckPOW, fCheckMerkleRoot))
d4190a2a 3631 {
3632 fprintf(stderr,"TestBlockValidity failure B\n");
df08a626 3633 return false;
d4190a2a 3634 }
df08a626 3635 if (!ContextualCheckBlock(block, state, pindexPrev))
d4190a2a 3636 {
3637 fprintf(stderr,"TestBlockValidity failure C\n");
df08a626 3638 return false;
d4190a2a 3639 }
df08a626 3640 if (!ConnectBlock(block, state, &indexDummy, viewNew, true))
d4190a2a 3641 {
3642 fprintf(stderr,"TestBlockValidity failure D\n");
df08a626 3643 return false;
d4190a2a 3644 }
df08a626
LD
3645 assert(state.IsValid());
3646
3647 return true;
3648}
3649
f9ec3f0f 3650/**
3651 * BLOCK PRUNING CODE
3652 */
3653
3654/* Calculate the amount of disk space the block & undo files currently use */
3655uint64_t CalculateCurrentUsage()
3656{
3657 uint64_t retval = 0;
3658 BOOST_FOREACH(const CBlockFileInfo &file, vinfoBlockFile) {
3659 retval += file.nSize + file.nUndoSize;
3660 }
3661 return retval;
3662}
3663
3664/* Prune a block file (modify associated database entries)*/
3665void PruneOneBlockFile(const int fileNumber)
3666{
3667 for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) {
3668 CBlockIndex* pindex = it->second;
3669 if (pindex->nFile == fileNumber) {
3670 pindex->nStatus &= ~BLOCK_HAVE_DATA;
3671 pindex->nStatus &= ~BLOCK_HAVE_UNDO;
3672 pindex->nFile = 0;
3673 pindex->nDataPos = 0;
3674 pindex->nUndoPos = 0;
3675 setDirtyBlockIndex.insert(pindex);
3676
3677 // Prune from mapBlocksUnlinked -- any block we prune would have
3678 // to be downloaded again in order to consider its chain, at which
3679 // point it would be considered as a candidate for
3680 // mapBlocksUnlinked or setBlockIndexCandidates.
3681 std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex->pprev);
3682 while (range.first != range.second) {
3683 std::multimap<CBlockIndex *, CBlockIndex *>::iterator it = range.first;
3684 range.first++;
3685 if (it->second == pindex) {
3686 mapBlocksUnlinked.erase(it);
3687 }
3688 }
3689 }
3690 }
3691
3692 vinfoBlockFile[fileNumber].SetNull();
3693 setDirtyFileInfo.insert(fileNumber);
3694}
3695
3696
3697void UnlinkPrunedFiles(std::set<int>& setFilesToPrune)
3698{
3699 for (set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
3700 CDiskBlockPos pos(*it, 0);
3701 boost::filesystem::remove(GetBlockPosFilename(pos, "blk"));
3702 boost::filesystem::remove(GetBlockPosFilename(pos, "rev"));
3703 LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
3704 }
3705}
3706
3707/* Calculate the block/rev files that should be deleted to remain under target*/
3708void FindFilesToPrune(std::set<int>& setFilesToPrune)
3709{
3710 LOCK2(cs_main, cs_LastBlockFile);
3711 if (chainActive.Tip() == NULL || nPruneTarget == 0) {
3712 return;
3713 }
3714 if (chainActive.Tip()->nHeight <= Params().PruneAfterHeight()) {
3715 return;
3716 }
3717
b89f3077 3718 unsigned int nLastBlockWeCanPrune = chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP;
f9ec3f0f 3719 uint64_t nCurrentUsage = CalculateCurrentUsage();
3720 // We don't check to prune until after we've allocated new space for files
3721 // So we should leave a buffer under our target to account for another allocation
3722 // before the next pruning.
3723 uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
3724 uint64_t nBytesToPrune;
3725 int count=0;
3726
3727 if (nCurrentUsage + nBuffer >= nPruneTarget) {
3728 for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
3729 nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize;
3730
3731 if (vinfoBlockFile[fileNumber].nSize == 0)
3732 continue;
3733
3734 if (nCurrentUsage + nBuffer < nPruneTarget) // are we below our target?
3735 break;
3736
6cb70ca4 3737 // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
b89f3077 3738 if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
6cb70ca4 3739 continue;
f9ec3f0f 3740
3741 PruneOneBlockFile(fileNumber);
3742 // Queue up the files for removal
3743 setFilesToPrune.insert(fileNumber);
3744 nCurrentUsage -= nBytesToPrune;
3745 count++;
3746 }
3747 }
3748
b89f3077 3749 LogPrint("prune", "Prune: target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n",
f9ec3f0f 3750 nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
3751 ((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
b89f3077 3752 nLastBlockWeCanPrune, count);
f9ec3f0f 3753}
3754
51ed9ec9 3755bool CheckDiskSpace(uint64_t nAdditionalBytes)
0a61b0df 3756{
a3241998 3757 uint64_t nFreeBytesAvailable = boost::filesystem::space(GetDataDir()).available;
0a61b0df 3758
966ae00f
PK
3759 // Check for nMinDiskSpace bytes (currently 50MB)
3760 if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
b9b2e3fa 3761 return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
7851033d 3762
0a61b0df 3763 return true;
3764}
3765
5382bcf8 3766FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
42613c97 3767{
450cbb09 3768 if (pos.IsNull())
0a61b0df 3769 return NULL;
ec7eb0fa 3770 boost::filesystem::path path = GetBlockPosFilename(pos, prefix);
5382bcf8
PW
3771 boost::filesystem::create_directories(path.parent_path());
3772 FILE* file = fopen(path.string().c_str(), "rb+");
3773 if (!file && !fReadOnly)
3774 file = fopen(path.string().c_str(), "wb+");
450cbb09 3775 if (!file) {
7d9d134b 3776 LogPrintf("Unable to open file %s\n", path.string());
0a61b0df 3777 return NULL;
450cbb09 3778 }
5382bcf8
PW
3779 if (pos.nPos) {
3780 if (fseek(file, pos.nPos, SEEK_SET)) {
7d9d134b 3781 LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
5382bcf8
PW
3782 fclose(file);
3783 return NULL;
3784 }
3785 }
0a61b0df 3786 return file;
3787}
3788
5382bcf8
PW
3789FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
3790 return OpenDiskFile(pos, "blk", fReadOnly);
3791}
3792
69e07747 3793FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
5382bcf8
PW
3794 return OpenDiskFile(pos, "rev", fReadOnly);
3795}
3796
ec7eb0fa
SD
3797boost::filesystem::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
3798{
f7e36370 3799 return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
ec7eb0fa
SD
3800}
3801
2d8a4829
PW
3802CBlockIndex * InsertBlockIndex(uint256 hash)
3803{
4f152496 3804 if (hash.IsNull())
2d8a4829
PW
3805 return NULL;
3806
3807 // Return existing
145d5be8 3808 BlockMap::iterator mi = mapBlockIndex.find(hash);
2d8a4829
PW
3809 if (mi != mapBlockIndex.end())
3810 return (*mi).second;
3811
3812 // Create new
3813 CBlockIndex* pindexNew = new CBlockIndex();
3814 if (!pindexNew)
5262fde0 3815 throw runtime_error("LoadBlockIndex(): new CBlockIndex failed");
2d8a4829
PW
3816 mi = mapBlockIndex.insert(make_pair(hash, pindexNew)).first;
3817 pindexNew->phashBlock = &((*mi).first);
3818
3819 return pindexNew;
3820}
3821
3822bool static LoadBlockIndexDB()
3823{
11982d36 3824 const CChainParams& chainparams = Params();
2d8a4829
PW
3825 if (!pblocktree->LoadBlockIndexGuts())
3826 return false;
3827
b31499ec 3828 boost::this_thread::interruption_point();
2d8a4829 3829
1657c4bc 3830 // Calculate nChainWork
2d8a4829
PW
3831 vector<pair<int, CBlockIndex*> > vSortedByHeight;
3832 vSortedByHeight.reserve(mapBlockIndex.size());
3833 BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
3834 {
3835 CBlockIndex* pindex = item.second;
3836 vSortedByHeight.push_back(make_pair(pindex->nHeight, pindex));
3837 }
3838 sort(vSortedByHeight.begin(), vSortedByHeight.end());
3839 BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
3840 {
3841 CBlockIndex* pindex = item.second;
092b58d1 3842 pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
f9ec3f0f 3843 // We can link the chain of blocks for which we've received transactions at some point.
3844 // Pruned nodes may have deleted the block.
3845 if (pindex->nTx > 0) {
341735eb
PW
3846 if (pindex->pprev) {
3847 if (pindex->pprev->nChainTx) {
3848 pindex->nChainTx = pindex->pprev->nChainTx + pindex->nTx;
3849 } else {
3850 pindex->nChainTx = 0;
3851 mapBlocksUnlinked.insert(std::make_pair(pindex->pprev, pindex));
3852 }
3853 } else {
3854 pindex->nChainTx = pindex->nTx;
3855 }
3856 }
3857 if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->nChainTx || pindex->pprev == NULL))
e17bd583 3858 setBlockIndexCandidates.insert(pindex);
85eb2cef
PW
3859 if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
3860 pindexBestInvalid = pindex;
c9a09183
PW
3861 if (pindex->pprev)
3862 pindex->BuildSkip();
341735eb
PW
3863 if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == NULL || CBlockIndexWorkComparator()(pindexBestHeader, pindex)))
3864 pindexBestHeader = pindex;
2d8a4829
PW
3865 }
3866
3867 // Load block file info
3868 pblocktree->ReadLastBlockFile(nLastBlockFile);
ed6d1a2c 3869 vinfoBlockFile.resize(nLastBlockFile + 1);
7b2bb962 3870 LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile);
ed6d1a2c
PW
3871 for (int nFile = 0; nFile <= nLastBlockFile; nFile++) {
3872 pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
3873 }
7b2bb962 3874 LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString());
ed6d1a2c
PW
3875 for (int nFile = nLastBlockFile + 1; true; nFile++) {
3876 CBlockFileInfo info;
3877 if (pblocktree->ReadBlockFileInfo(nFile, info)) {
3878 vinfoBlockFile.push_back(info);
3879 } else {
3880 break;
3881 }
3882 }
729b1806 3883
8c93bf4c
AH
3884 // Check presence of blk files
3885 LogPrintf("Checking all blk files are present...\n");
3886 set<int> setBlkDataFiles;
3887 BOOST_FOREACH(const PAIRTYPE(uint256, CBlockIndex*)& item, mapBlockIndex)
3888 {
3889 CBlockIndex* pindex = item.second;
3890 if (pindex->nStatus & BLOCK_HAVE_DATA) {
3891 setBlkDataFiles.insert(pindex->nFile);
3892 }
3893 }
3894 for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++)
3895 {
3896 CDiskBlockPos pos(*it, 0);
a8738238 3897 if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) {
8c93bf4c
AH
3898 return false;
3899 }
3900 }
3901
f9ec3f0f 3902 // Check whether we have ever pruned block & undo files
3903 pblocktree->ReadFlag("prunedblockfiles", fHavePruned);
3904 if (fHavePruned)
3905 LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n");
3906
89b7019b
PW
3907 // Check whether we need to continue reindexing
3908 bool fReindexing = false;
3909 pblocktree->ReadReindexing(fReindexing);
3910 fReindex |= fReindexing;
3911
2d1fa42e
PW
3912 // Check whether we have a transaction index
3913 pblocktree->ReadFlag("txindex", fTxIndex);
52070c87 3914 LogPrintf("%s: transaction index %s\n", __func__, fTxIndex ? "enabled" : "disabled");
2d1fa42e 3915
85eb2cef 3916 // Load pointer to end of best chain
145d5be8 3917 BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
84674082 3918 if (it == mapBlockIndex.end())
89b7019b 3919 return true;
84674082 3920 chainActive.SetTip(it->second);
cca48f69 3921
3922 PruneBlockIndexCandidates();
3923
52070c87 3924 LogPrintf("%s: hashBestChain=%s height=%d date=%s progress=%f\n", __func__,
7d9d134b 3925 chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
c4656e0d 3926 DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
11982d36 3927 Checkpoints::GuessVerificationProgress(chainparams.Checkpoints(), chainActive.Tip()));
2d8a4829 3928
1f355b66
PW
3929 return true;
3930}
3931
06a91d96
CL
3932CVerifyDB::CVerifyDB()
3933{
3934 uiInterface.ShowProgress(_("Verifying blocks..."), 0);
3935}
3936
3937CVerifyDB::~CVerifyDB()
3938{
3939 uiInterface.ShowProgress("", 100);
3940}
3941
2e280311 3942bool CVerifyDB::VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
168ba993 3943{
a475285a 3944 LOCK(cs_main);
4c6d41b8 3945 if (chainActive.Tip() == NULL || chainActive.Tip()->pprev == NULL)
1f355b66
PW
3946 return true;
3947
2d8a4829 3948 // Verify blocks in the best chain
f5906533 3949 if (nCheckDepth <= 0)
2d8a4829 3950 nCheckDepth = 1000000000; // suffices until the year 19000
4c6d41b8
PW
3951 if (nCheckDepth > chainActive.Height())
3952 nCheckDepth = chainActive.Height();
1f355b66 3953 nCheckLevel = std::max(0, std::min(4, nCheckLevel));
881a85a2 3954 LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
7c70438d 3955 CCoinsViewCache coins(coinsview);
4c6d41b8 3956 CBlockIndex* pindexState = chainActive.Tip();
1f355b66
PW
3957 CBlockIndex* pindexFailure = NULL;
3958 int nGoodTransactions = 0;
ef3988ca 3959 CValidationState state;
6fb8d0c2
JG
3960 // No need to verify JoinSplits twice
3961 auto verifier = libzcash::ProofVerifier::Disabled();
4c6d41b8 3962 for (CBlockIndex* pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev)
2d8a4829 3963 {
b31499ec 3964 boost::this_thread::interruption_point();
06a91d96 3965 uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100)))));
4c6d41b8 3966 if (pindex->nHeight < chainActive.Height()-nCheckDepth)
2d8a4829
PW
3967 break;
3968 CBlock block;
1f355b66 3969 // check level 0: read from disk
f2dd868d 3970 if (!ReadBlockFromDisk(block, pindex))
5262fde0 3971 return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
2d8a4829 3972 // check level 1: verify block validity
3ced9364 3973 if (nCheckLevel >= 1 && !CheckBlock(pindex->nHeight,pindex,block, state, verifier))
5262fde0 3974 return error("VerifyDB(): *** found bad block at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
1f355b66
PW
3975 // check level 2: verify undo validity
3976 if (nCheckLevel >= 2 && pindex) {
3977 CBlockUndo undo;
3978 CDiskBlockPos pos = pindex->GetUndoPos();
3979 if (!pos.IsNull()) {
e035c6a7 3980 if (!UndoReadFromDisk(undo, pos, pindex->pprev->GetBlockHash()))
5262fde0 3981 return error("VerifyDB(): *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
1f355b66
PW
3982 }
3983 }
3984 // check level 3: check for inconsistencies during memory-only disconnect of tip blocks
fc684ad8 3985 if (nCheckLevel >= 3 && pindex == pindexState && (coins.DynamicMemoryUsage() + pcoinsTip->DynamicMemoryUsage()) <= nCoinCacheUsage) {
1f355b66 3986 bool fClean = true;
5c363ed6 3987 if (!DisconnectBlock(block, state, pindex, coins, &fClean))
5262fde0 3988 return error("VerifyDB(): *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
1f355b66
PW
3989 pindexState = pindex->pprev;
3990 if (!fClean) {
3991 nGoodTransactions = 0;
3992 pindexFailure = pindex;
3993 } else
3994 nGoodTransactions += block.vtx.size();
2d8a4829 3995 }
70477a0b
TZ
3996 if (ShutdownRequested())
3997 return true;
2d8a4829 3998 }
1f355b66 3999 if (pindexFailure)
5262fde0 4000 return error("VerifyDB(): *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainActive.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
1f355b66
PW
4001
4002 // check level 4: try reconnecting blocks
4003 if (nCheckLevel >= 4) {
4004 CBlockIndex *pindex = pindexState;
4c6d41b8 4005 while (pindex != chainActive.Tip()) {
b31499ec 4006 boost::this_thread::interruption_point();
06a91d96 4007 uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, 100 - (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * 50))));
4c6d41b8 4008 pindex = chainActive.Next(pindex);
b001c871 4009 CBlock block;
f2dd868d 4010 if (!ReadBlockFromDisk(block, pindex))
5262fde0 4011 return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
f3ae51dc 4012 if (!ConnectBlock(block, state, pindex, coins))
5262fde0 4013 return error("VerifyDB(): *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
1f355b66 4014 }
2d8a4829
PW
4015 }
4016
4c6d41b8 4017 LogPrintf("No coin database inconsistencies in last %i blocks (%i transactions)\n", chainActive.Height() - pindexState->nHeight, nGoodTransactions);
1f355b66 4018
2d8a4829
PW
4019 return true;
4020}
4021
f7f3a96b
PW
4022void UnloadBlockIndex()
4023{
51598b26 4024 LOCK(cs_main);
e17bd583 4025 setBlockIndexCandidates.clear();
4c6d41b8 4026 chainActive.SetTip(NULL);
85eb2cef 4027 pindexBestInvalid = NULL;
51598b26
PW
4028 pindexBestHeader = NULL;
4029 mempool.clear();
4030 mapOrphanTransactions.clear();
4031 mapOrphanTransactionsByPrev.clear();
4032 nSyncStarted = 0;
4033 mapBlocksUnlinked.clear();
4034 vinfoBlockFile.clear();
4035 nLastBlockFile = 0;
4036 nBlockSequenceId = 1;
4037 mapBlockSource.clear();
4038 mapBlocksInFlight.clear();
4039 nQueuedValidatedHeaders = 0;
4040 nPreferredDownload = 0;
4041 setDirtyBlockIndex.clear();
4042 setDirtyFileInfo.clear();
4043 mapNodeState.clear();
ec9b6c33 4044 recentRejects.reset(NULL);
51598b26
PW
4045
4046 BOOST_FOREACH(BlockMap::value_type& entry, mapBlockIndex) {
4047 delete entry.second;
4048 }
4049 mapBlockIndex.clear();
f9ec3f0f 4050 fHavePruned = false;
f7f3a96b
PW
4051}
4052
7fea4846 4053bool LoadBlockIndex()
0a61b0df 4054{
5603bd7a 4055 extern int32_t KOMODO_LOADINGBLOCKS;
d979e6e3 4056 // Load block index from databases
d042777b 4057 KOMODO_LOADINGBLOCKS = 1;
2d1fa42e 4058 if (!fReindex && !LoadBlockIndexDB())
d042777b 4059 {
4060 KOMODO_LOADINGBLOCKS = 0;
0a61b0df 4061 return false;
d042777b 4062 }
4063 KOMODO_LOADINGBLOCKS = 0;
25f7ef8c 4064 fprintf(stderr,"finished loading blocks %s\n",ASSETCHAINS_SYMBOL);
38603761
PW
4065 return true;
4066}
2d1fa42e 4067
2d1fa42e 4068
38603761 4069bool InitBlockIndex() {
e6973430 4070 const CChainParams& chainparams = Params();
55a1db4f 4071 LOCK(cs_main);
5094a81d
WL
4072
4073 // Initialize global variables that cannot be constructed at startup.
4074 recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
4075
38603761 4076 // Check whether we're already initialized
4c6d41b8 4077 if (chainActive.Genesis() != NULL)
38603761
PW
4078 return true;
4079
4080 // Use the provided setting for -txindex in the new database
4081 fTxIndex = GetBoolArg("-txindex", false);
4082 pblocktree->WriteFlag("txindex", fTxIndex);
881a85a2 4083 LogPrintf("Initializing databases...\n");
38603761
PW
4084
4085 // Only add the genesis block if not reindexing (in which case we reuse the one already on disk)
4086 if (!fReindex) {
38603761 4087 try {
0e4b3175
MH
4088 CBlock &block = const_cast<CBlock&>(Params().GenesisBlock());
4089 // Start new block file
38603761
PW
4090 unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
4091 CDiskBlockPos blockPos;
4092 CValidationState state;
209377a7 4093 if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.GetBlockTime()))
5262fde0 4094 return error("LoadBlockIndex(): FindBlockPos failed");
e6973430 4095 if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
5262fde0 4096 return error("LoadBlockIndex(): writing genesis block to disk failed");
942b33a1
PW
4097 CBlockIndex *pindex = AddToBlockIndex(block);
4098 if (!ReceivedBlockTransactions(block, state, pindex, blockPos))
5262fde0 4099 return error("LoadBlockIndex(): genesis block not accepted");
92bb6f2f 4100 if (!ActivateBestChain(state, &block))
5262fde0 4101 return error("LoadBlockIndex(): genesis block cannot be activated");
bf7835c2 4102 // Force a chainstate write so that when we VerifyDB in a moment, it doesn't check stale data
a2069500 4103 return FlushStateToDisk(state, FLUSH_STATE_ALWAYS);
27df4123 4104 } catch (const std::runtime_error& e) {
5262fde0 4105 return error("LoadBlockIndex(): failed to initialize block database: %s", e.what());
38603761 4106 }
0a61b0df 4107 }
4108
4109 return true;
4110}
4111
4112
4113
7fea4846 4114bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp)
1d740055 4115{
4e382177 4116 const CChainParams& chainparams = Params();
ad96e7cc
WL
4117 // Map of disk positions for blocks with unknown parent (only used for reindex)
4118 static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
51ed9ec9 4119 int64_t nStart = GetTimeMillis();
746f502a 4120
1d740055 4121 int nLoaded = 0;
421218d3 4122 try {
c9fb27da 4123 // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
05d97268 4124 CBufferedFile blkdat(fileIn, 2*MAX_BLOCK_SIZE, MAX_BLOCK_SIZE+8, SER_DISK, CLIENT_VERSION);
51ed9ec9 4125 uint64_t nRewind = blkdat.GetPos();
eb0b56b1 4126 while (!blkdat.eof()) {
21eb5ada
GA
4127 boost::this_thread::interruption_point();
4128
05d97268
PW
4129 blkdat.SetPos(nRewind);
4130 nRewind++; // start one byte further next time, in case of failure
4131 blkdat.SetLimit(); // remove former limit
7fea4846 4132 unsigned int nSize = 0;
05d97268
PW
4133 try {
4134 // locate a header
0caf2b18 4135 unsigned char buf[MESSAGE_START_SIZE];
0e4b3175 4136 blkdat.FindByte(Params().MessageStart()[0]);
05d97268
PW
4137 nRewind = blkdat.GetPos()+1;
4138 blkdat >> FLATDATA(buf);
0caf2b18 4139 if (memcmp(buf, Params().MessageStart(), MESSAGE_START_SIZE))
05d97268
PW
4140 continue;
4141 // read size
1d740055 4142 blkdat >> nSize;
05d97268
PW
4143 if (nSize < 80 || nSize > MAX_BLOCK_SIZE)
4144 continue;
27df4123 4145 } catch (const std::exception&) {
7fea4846
PW
4146 // no valid block header found; don't complain
4147 break;
4148 }
4149 try {
05d97268 4150 // read block
51ed9ec9 4151 uint64_t nBlockPos = blkdat.GetPos();
ad96e7cc
WL
4152 if (dbp)
4153 dbp->nPos = nBlockPos;
7fea4846 4154 blkdat.SetLimit(nBlockPos + nSize);
16d51941
PW
4155 blkdat.SetPos(nBlockPos);
4156 CBlock block;
4157 blkdat >> block;
ad96e7cc
WL
4158 nRewind = blkdat.GetPos();
4159
16d51941
PW
4160 // detect out of order blocks, and store them for later
4161 uint256 hash = block.GetHash();
4e382177 4162 if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex.find(block.hashPrevBlock) == mapBlockIndex.end()) {
ad96e7cc 4163 LogPrint("reindex", "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
16d51941 4164 block.hashPrevBlock.ToString());
ad96e7cc 4165 if (dbp)
16d51941 4166 mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
ad96e7cc
WL
4167 continue;
4168 }
4169
16d51941 4170 // process in case the block isn't known yet
8375e221 4171 if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
16d51941 4172 CValidationState state;
35915149 4173 if (ProcessNewBlock(0,state, NULL, &block, true, dbp))
16d51941
PW
4174 nLoaded++;
4175 if (state.IsError())
4176 break;
4e382177 4177 } else if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex[hash]->nHeight % 1000 == 0) {
50b43fda 4178 LogPrintf("Block Import: already had block %s at height %d\n", hash.ToString(), mapBlockIndex[hash]->nHeight);
16d51941 4179 }
ad96e7cc
WL
4180
4181 // Recursively process earlier encountered successors of this block
4182 deque<uint256> queue;
4183 queue.push_back(hash);
4184 while (!queue.empty()) {
4185 uint256 head = queue.front();
4186 queue.pop_front();
4187 std::pair<std::multimap<uint256, CDiskBlockPos>::iterator, std::multimap<uint256, CDiskBlockPos>::iterator> range = mapBlocksUnknownParent.equal_range(head);
4188 while (range.first != range.second) {
4189 std::multimap<uint256, CDiskBlockPos>::iterator it = range.first;
fa9e5205 4190 if (ReadBlockFromDisk(mapBlockIndex[hash]!=0?mapBlockIndex[hash]->nHeight:0,block, it->second))
ad96e7cc
WL
4191 {
4192 LogPrintf("%s: Processing out of order child %s of %s\n", __func__, block.GetHash().ToString(),
4193 head.ToString());
4194 CValidationState dummy;
35915149 4195 if (ProcessNewBlock(0,dummy, NULL, &block, true, &it->second))
ad96e7cc
WL
4196 {
4197 nLoaded++;
4198 queue.push_back(block.GetHash());
4199 }
4200 }
4201 range.first++;
4202 mapBlocksUnknownParent.erase(it);
4203 }
1d740055 4204 }
27df4123 4205 } catch (const std::exception& e) {
7ff9d122 4206 LogPrintf("%s: Deserialize or I/O error - %s\n", __func__, e.what());
1d740055
PW
4207 }
4208 }
27df4123 4209 } catch (const std::runtime_error& e) {
b9b2e3fa 4210 AbortNode(std::string("System error: ") + e.what());
1d740055 4211 }
7fea4846 4212 if (nLoaded > 0)
f48742c2 4213 LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded, GetTimeMillis() - nStart);
1d740055
PW
4214 return nLoaded > 0;
4215}
0a61b0df 4216
3fcfbc8a
PW
4217void static CheckBlockIndex()
4218{
4e382177 4219 const Consensus::Params& consensusParams = Params().GetConsensus();
3fcfbc8a
PW
4220 if (!fCheckBlockIndex) {
4221 return;
4222 }
4223
4224 LOCK(cs_main);
4225
0421c18f 4226 // During a reindex, we read the genesis block and call CheckBlockIndex before ActivateBestChain,
4227 // so we have the genesis block in mapBlockIndex but no active chain. (A few of the tests when
4228 // iterating the block tree require that chainActive has been initialized.)
4229 if (chainActive.Height() < 0) {
4230 assert(mapBlockIndex.size() <= 1);
4231 return;
4232 }
4233
3fcfbc8a
PW
4234 // Build forward-pointing map of the entire block tree.
4235 std::multimap<CBlockIndex*,CBlockIndex*> forward;
4236 for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
4237 forward.insert(std::make_pair(it->second->pprev, it->second));
4238 }
4239
4240 assert(forward.size() == mapBlockIndex.size());
4241
4242 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeGenesis = forward.equal_range(NULL);
4243 CBlockIndex *pindex = rangeGenesis.first->second;
4244 rangeGenesis.first++;
4245 assert(rangeGenesis.first == rangeGenesis.second); // There is only one index entry with parent NULL.
4246
4247 // Iterate over the entire block tree, using depth-first search.
4248 // Along the way, remember whether there are blocks on the path from genesis
4249 // block being explored which are the first to have certain properties.
4250 size_t nNodes = 0;
4251 int nHeight = 0;
4252 CBlockIndex* pindexFirstInvalid = NULL; // Oldest ancestor of pindex which is invalid.
4253 CBlockIndex* pindexFirstMissing = NULL; // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA.
f9ec3f0f 4254 CBlockIndex* pindexFirstNeverProcessed = NULL; // Oldest ancestor of pindex for which nTx == 0.
3fcfbc8a 4255 CBlockIndex* pindexFirstNotTreeValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
ede379f7 4256 CBlockIndex* pindexFirstNotTransactionsValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not).
3fcfbc8a
PW
4257 CBlockIndex* pindexFirstNotChainValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not).
4258 CBlockIndex* pindexFirstNotScriptsValid = NULL; // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not).
4259 while (pindex != NULL) {
4260 nNodes++;
4261 if (pindexFirstInvalid == NULL && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
4262 if (pindexFirstMissing == NULL && !(pindex->nStatus & BLOCK_HAVE_DATA)) pindexFirstMissing = pindex;
f9ec3f0f 4263 if (pindexFirstNeverProcessed == NULL && pindex->nTx == 0) pindexFirstNeverProcessed = pindex;
3fcfbc8a 4264 if (pindex->pprev != NULL && pindexFirstNotTreeValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TREE) pindexFirstNotTreeValid = pindex;
ede379f7 4265 if (pindex->pprev != NULL && pindexFirstNotTransactionsValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TRANSACTIONS) pindexFirstNotTransactionsValid = pindex;
3fcfbc8a
PW
4266 if (pindex->pprev != NULL && pindexFirstNotChainValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_CHAIN) pindexFirstNotChainValid = pindex;
4267 if (pindex->pprev != NULL && pindexFirstNotScriptsValid == NULL && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS) pindexFirstNotScriptsValid = pindex;
4268
4269 // Begin: actual consistency checks.
4270 if (pindex->pprev == NULL) {
4271 // Genesis block checks.
4e382177 4272 assert(pindex->GetBlockHash() == consensusParams.hashGenesisBlock); // Genesis block's hash must match.
3fcfbc8a
PW
4273 assert(pindex == chainActive.Genesis()); // The current active chain's genesis block must be this block.
4274 }
c1ecee8f 4275 if (pindex->nChainTx == 0) assert(pindex->nSequenceId == 0); // nSequenceId can't be set for blocks that aren't linked
f9ec3f0f 4276 // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred).
4277 // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
4278 if (!fHavePruned) {
4279 // If we've never pruned, then HAVE_DATA should be equivalent to nTx > 0
4280 assert(!(pindex->nStatus & BLOCK_HAVE_DATA) == (pindex->nTx == 0));
4281 assert(pindexFirstMissing == pindexFirstNeverProcessed);
4282 } else {
4283 // If we have pruned, then we can only say that HAVE_DATA implies nTx > 0
4284 if (pindex->nStatus & BLOCK_HAVE_DATA) assert(pindex->nTx > 0);
4285 }
4286 if (pindex->nStatus & BLOCK_HAVE_UNDO) assert(pindex->nStatus & BLOCK_HAVE_DATA);
4287 assert(((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS) == (pindex->nTx > 0)); // This is pruning-independent.
4288 // All parents having had data (at some point) is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to nChainTx being set.
4289 assert((pindexFirstNeverProcessed != NULL) == (pindex->nChainTx == 0)); // nChainTx != 0 is used to signal that all parent blocks have been processed (but may have been pruned).
ede379f7 4290 assert((pindexFirstNotTransactionsValid != NULL) == (pindex->nChainTx == 0));
3fcfbc8a
PW
4291 assert(pindex->nHeight == nHeight); // nHeight must be consistent.
4292 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.
4293 assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); // The pskip pointer must point back for all but the first 2 blocks.
4294 assert(pindexFirstNotTreeValid == NULL); // All mapBlockIndex entries must at least be TREE valid
4295 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TREE) assert(pindexFirstNotTreeValid == NULL); // TREE valid implies all parents are TREE valid
4296 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_CHAIN) assert(pindexFirstNotChainValid == NULL); // CHAIN valid implies all parents are CHAIN valid
4297 if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_SCRIPTS) assert(pindexFirstNotScriptsValid == NULL); // SCRIPTS valid implies all parents are SCRIPTS valid
4298 if (pindexFirstInvalid == NULL) {
4299 // Checks for not-invalid blocks.
4300 assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
4301 }
f9ec3f0f 4302 if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && pindexFirstNeverProcessed == NULL) {
4303 if (pindexFirstInvalid == NULL) {
4304 // If this block sorts at least as good as the current tip and
4305 // is valid and we have all data for its parents, it must be in
4306 // setBlockIndexCandidates. chainActive.Tip() must also be there
4307 // even if some data has been pruned.
4308 if (pindexFirstMissing == NULL || pindex == chainActive.Tip()) {
4309 assert(setBlockIndexCandidates.count(pindex));
4310 }
4311 // If some parent is missing, then it could be that this block was in
4312 // setBlockIndexCandidates but had to be removed because of the missing data.
4313 // In this case it must be in mapBlocksUnlinked -- see test below.
3fcfbc8a 4314 }
f9ec3f0f 4315 } else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates.
3fcfbc8a
PW
4316 assert(setBlockIndexCandidates.count(pindex) == 0);
4317 }
4318 // Check whether this block is in mapBlocksUnlinked.
4319 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked = mapBlocksUnlinked.equal_range(pindex->pprev);
4320 bool foundInUnlinked = false;
4321 while (rangeUnlinked.first != rangeUnlinked.second) {
4322 assert(rangeUnlinked.first->first == pindex->pprev);
4323 if (rangeUnlinked.first->second == pindex) {
4324 foundInUnlinked = true;
4325 break;
4326 }
4327 rangeUnlinked.first++;
4328 }
f9ec3f0f 4329 if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed != NULL && pindexFirstInvalid == NULL) {
4330 // If this block has block data available, some parent was never received, and has no invalid parents, it must be in mapBlocksUnlinked.
4331 assert(foundInUnlinked);
4332 }
4333 if (!(pindex->nStatus & BLOCK_HAVE_DATA)) assert(!foundInUnlinked); // Can't be in mapBlocksUnlinked if we don't HAVE_DATA
4334 if (pindexFirstMissing == NULL) assert(!foundInUnlinked); // We aren't missing data for any parent -- cannot be in mapBlocksUnlinked.
4335 if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed == NULL && pindexFirstMissing != NULL) {
4336 // We HAVE_DATA for this block, have received data for all parents at some point, but we're currently missing data for some parent.
4337 assert(fHavePruned); // We must have pruned.
4338 // This block may have entered mapBlocksUnlinked if:
4339 // - it has a descendant that at some point had more work than the
4340 // tip, and
4341 // - we tried switching to that descendant but were missing
4342 // data for some intermediate block between chainActive and the
4343 // tip.
4344 // So if this block is itself better than chainActive.Tip() and it wasn't in
4345 // setBlockIndexCandidates, then it must be in mapBlocksUnlinked.
4346 if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && setBlockIndexCandidates.count(pindex) == 0) {
4347 if (pindexFirstInvalid == NULL) {
4348 assert(foundInUnlinked);
4349 }
3fcfbc8a 4350 }
3fcfbc8a
PW
4351 }
4352 // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow
4353 // End: actual consistency checks.
4354
4355 // Try descending into the first subnode.
4356 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
4357 if (range.first != range.second) {
4358 // A subnode was found.
4359 pindex = range.first->second;
4360 nHeight++;
4361 continue;
4362 }
4363 // This is a leaf node.
4364 // Move upwards until we reach a node of which we have not yet visited the last child.
4365 while (pindex) {
4366 // We are going to either move to a parent or a sibling of pindex.
4367 // If pindex was the first with a certain property, unset the corresponding variable.
4368 if (pindex == pindexFirstInvalid) pindexFirstInvalid = NULL;
4369 if (pindex == pindexFirstMissing) pindexFirstMissing = NULL;
f9ec3f0f 4370 if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed = NULL;
3fcfbc8a 4371 if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid = NULL;
ede379f7 4372 if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid = NULL;
3fcfbc8a
PW
4373 if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid = NULL;
4374 if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid = NULL;
4375 // Find our parent.
4376 CBlockIndex* pindexPar = pindex->pprev;
4377 // Find which child we just visited.
4378 std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
4379 while (rangePar.first->second != pindex) {
4380 assert(rangePar.first != rangePar.second); // Our parent must have at least the node we're coming from as child.
4381 rangePar.first++;
4382 }
4383 // Proceed to the next one.
4384 rangePar.first++;
4385 if (rangePar.first != rangePar.second) {
4386 // Move to the sibling.
4387 pindex = rangePar.first->second;
4388 break;
4389 } else {
4390 // Move up further.
4391 pindex = pindexPar;
4392 nHeight--;
4393 continue;
4394 }
4395 }
4396 }
4397
4398 // Check that we actually traversed the entire map.
4399 assert(nNodes == forward.size());
4400}
4401
0a61b0df 4402//////////////////////////////////////////////////////////////////////////////
4403//
4404// CAlert
4405//
4406
0a61b0df 4407string GetWarnings(string strFor)
4408{
4409 int nPriority = 0;
4410 string strStatusBar;
4411 string strRPC;
62e21fb5 4412
62e21fb5
WL
4413 if (!CLIENT_VERSION_IS_RELEASE)
4414 strStatusBar = _("This is a pre-release test build - use at your own risk - do not use for mining or merchant applications");
4415
73578933 4416 if (GetBoolArg("-testsafemode", false))
4417 strStatusBar = strRPC = "testsafemode enabled";
4418
0a61b0df 4419 // Misc warnings like out of disk space and clock is wrong
4420 if (strMiscWarning != "")
4421 {
4422 nPriority = 1000;
4423 strStatusBar = strMiscWarning;
4424 }
4425
b8585384 4426 if (fLargeWorkForkFound)
0a61b0df 4427 {
4428 nPriority = 2000;
f65e7092
MC
4429 strStatusBar = strRPC = _("Warning: The network does not appear to fully agree! Some miners appear to be experiencing issues.");
4430 }
4431 else if (fLargeWorkInvalidChainFound)
0a61b0df 4432 {
4433 nPriority = 2000;
f65e7092 4434 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.");
0a61b0df 4435 }
4436
4437 // Alerts
0a61b0df 4438 {
f8dcd5ca 4439 LOCK(cs_mapAlerts);
223b6f1b 4440 BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
0a61b0df 4441 {
4442 const CAlert& alert = item.second;
4443 if (alert.AppliesToMe() && alert.nPriority > nPriority)
4444 {
4445 nPriority = alert.nPriority;
4446 strStatusBar = alert.strStatusBar;
a40034f7
JG
4447 if (alert.nPriority >= ALERT_PRIORITY_SAFE_MODE) {
4448 strRPC = alert.strRPCError;
4449 }
0a61b0df 4450 }
4451 }
4452 }
4453
4454 if (strFor == "statusbar")
4455 return strStatusBar;
4456 else if (strFor == "rpc")
4457 return strRPC;
5262fde0 4458 assert(!"GetWarnings(): invalid parameter");
0a61b0df 4459 return "error";
4460}
4461
0a61b0df 4462
4463
4464
4465
4466
4467
4468
4469//////////////////////////////////////////////////////////////////////////////
4470//
4471// Messages
4472//
4473
4474
ae8bfd12 4475bool static AlreadyHave(const CInv& inv)
0a61b0df 4476{
4477 switch (inv.type)
4478 {
8deb9822
JG
4479 case MSG_TX:
4480 {
5094a81d 4481 assert(recentRejects);
ec9b6c33
PT
4482 if (chainActive.Tip()->GetBlockHash() != hashRecentRejectsChainTip)
4483 {
4484 // If the chain tip has changed previously rejected transactions
4485 // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
4486 // or a double-spend. Reset the rejects filter and give those
4487 // txs a second chance.
4488 hashRecentRejectsChainTip = chainActive.Tip()->GetBlockHash();
4489 recentRejects->reset();
4490 }
4491
4492 return recentRejects->contains(inv.hash) ||
4493 mempool.exists(inv.hash) ||
4494 mapOrphanTransactions.count(inv.hash) ||
4495 pcoinsTip->HaveCoins(inv.hash);
8deb9822 4496 }
8deb9822 4497 case MSG_BLOCK:
341735eb 4498 return mapBlockIndex.count(inv.hash);
0a61b0df 4499 }
4500 // Don't know what it is, just say we already got one
4501 return true;
4502}
4503
c7f039b6
PW
4504void static ProcessGetData(CNode* pfrom)
4505{
4506 std::deque<CInv>::iterator it = pfrom->vRecvGetData.begin();
4507
4508 vector<CInv> vNotFound;
4509
7d38af3c
PW
4510 LOCK(cs_main);
4511
c7f039b6
PW
4512 while (it != pfrom->vRecvGetData.end()) {
4513 // Don't bother if send buffer is too full to respond anyway
4514 if (pfrom->nSendSize >= SendBufferSize())
4515 break;
4516
4517 const CInv &inv = *it;
4518 {
b31499ec 4519 boost::this_thread::interruption_point();
c7f039b6
PW
4520 it++;
4521
4522 if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK)
4523 {
d8b4b496 4524 bool send = false;
145d5be8 4525 BlockMap::iterator mi = mapBlockIndex.find(inv.hash);
c7f039b6
PW
4526 if (mi != mapBlockIndex.end())
4527 {
85da07a5 4528 if (chainActive.Contains(mi->second)) {
2b45345a 4529 send = true;
85da07a5 4530 } else {
f7303f97 4531 static const int nOneMonth = 30 * 24 * 60 * 60;
85da07a5 4532 // To prevent fingerprinting attacks, only send blocks outside of the active
f7303f97
PW
4533 // chain if they are valid, and no more than a month older (both in time, and in
4534 // best equivalent proof of work) than the best header chain we know about.
85da07a5 4535 send = mi->second->IsValid(BLOCK_VALID_SCRIPTS) && (pindexBestHeader != NULL) &&
f7303f97
PW
4536 (pindexBestHeader->GetBlockTime() - mi->second->GetBlockTime() < nOneMonth) &&
4537 (GetBlockProofEquivalentTime(*pindexBestHeader, *mi->second, *pindexBestHeader, Params().GetConsensus()) < nOneMonth);
85da07a5 4538 if (!send) {
30c1db1c 4539 LogPrintf("%s: ignoring request from peer=%i for old block that isn't in the main chain\n", __func__, pfrom->GetId());
85da07a5 4540 }
d8b4b496
AH
4541 }
4542 }
f9ec3f0f 4543 // Pruned nodes may have deleted the block, so check whether
4544 // it's available before trying to send.
4545 if (send && (mi->second->nStatus & BLOCK_HAVE_DATA))
d8b4b496
AH
4546 {
4547 // Send block from disk
c7f039b6 4548 CBlock block;
f2dd868d 4549 if (!ReadBlockFromDisk(block, (*mi).second))
13931733 4550 {
b34b7b31 4551 assert(!"cannot load block from disk");
13931733 4552 }
4553 else
c7f039b6 4554 {
13931733 4555 if (inv.type == MSG_BLOCK)
4556 pfrom->PushMessage("block", block);
4557 else // MSG_FILTERED_BLOCK)
c7f039b6 4558 {
13931733 4559 LOCK(pfrom->cs_filter);
4560 if (pfrom->pfilter)
4561 {
4562 CMerkleBlock merkleBlock(block, *pfrom->pfilter);
4563 pfrom->PushMessage("merkleblock", merkleBlock);
4564 // CMerkleBlock just contains hashes, so also push any transactions in the block the client did not see
4565 // This avoids hurting performance by pointlessly requiring a round-trip
4566 // Note that there is currently no way for a node to request any single transactions we didn't send here -
4567 // they must either disconnect and retry or request the full block.
4568 // Thus, the protocol spec specified allows for us to provide duplicate txn here,
4569 // however we MUST always provide at least what the remote peer needs
4570 typedef std::pair<unsigned int, uint256> PairType;
4571 BOOST_FOREACH(PairType& pair, merkleBlock.vMatchedTxn)
c7f039b6
PW
4572 if (!pfrom->setInventoryKnown.count(CInv(MSG_TX, pair.second)))
4573 pfrom->PushMessage("tx", block.vtx[pair.first]);
13931733 4574 }
4575 // else
c7f039b6 4576 // no response
13931733 4577 }
c7f039b6 4578 }
b05a89b2 4579 // Trigger the peer node to send a getblocks request for the next batch of inventory
c7f039b6
PW
4580 if (inv.hash == pfrom->hashContinue)
4581 {
4582 // Bypass PushInventory, this must send even if redundant,
4583 // and we want it right after the last block so they don't
4584 // wait for other stuff first.
4585 vector<CInv> vInv;
4c6d41b8 4586 vInv.push_back(CInv(MSG_BLOCK, chainActive.Tip()->GetBlockHash()));
c7f039b6 4587 pfrom->PushMessage("inv", vInv);
4f152496 4588 pfrom->hashContinue.SetNull();
c7f039b6
PW
4589 }
4590 }
4591 }
4592 else if (inv.IsKnownType())
4593 {
4594 // Send stream from relay memory
4595 bool pushed = false;
4596 {
4597 LOCK(cs_mapRelay);
4598 map<CInv, CDataStream>::iterator mi = mapRelay.find(inv);
4599 if (mi != mapRelay.end()) {
4600 pfrom->PushMessage(inv.GetCommand(), (*mi).second);
4601 pushed = true;
4602 }
4603 }
4604 if (!pushed && inv.type == MSG_TX) {
319b1160
GA
4605 CTransaction tx;
4606 if (mempool.lookup(inv.hash, tx)) {
c7f039b6
PW
4607 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
4608 ss.reserve(1000);
4609 ss << tx;
4610 pfrom->PushMessage("tx", ss);
4611 pushed = true;
4612 }
4613 }
4614 if (!pushed) {
4615 vNotFound.push_back(inv);
4616 }
4617 }
4618
4619 // Track requests for our stuff.
26c16d9d 4620 GetMainSignals().Inventory(inv.hash);
cd696e64 4621
75ef87dd
PS
4622 if (inv.type == MSG_BLOCK || inv.type == MSG_FILTERED_BLOCK)
4623 break;
c7f039b6
PW
4624 }
4625 }
4626
4627 pfrom->vRecvGetData.erase(pfrom->vRecvGetData.begin(), it);
4628
4629 if (!vNotFound.empty()) {
4630 // Let the peer know that we didn't find what it asked for, so it doesn't
4631 // have to wait around forever. Currently only SPV clients actually care
4632 // about this message: it's needed when they are recursively walking the
4633 // dependencies of relevant unconfirmed transactions. SPV clients want to
4634 // do that because they want to know about (and store and rebroadcast and
4635 // risk analyze) the dependencies of transactions relevant to them, without
4636 // having to download the entire memory pool.
4637 pfrom->PushMessage("notfound", vNotFound);
4638 }
4639}
4640
9f4da19b 4641bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, int64_t nTimeReceived)
0a61b0df 4642{
e8e8904d 4643 const CChainParams& chainparams = Params();
0a61b0df 4644 RandAddSeedPerfmon();
28d4cff0 4645 LogPrint("net", "received: %s (%u bytes) peer=%d\n", SanitizeString(strCommand), vRecv.size(), pfrom->id);
0a61b0df 4646 if (mapArgs.count("-dropmessagestest") && GetRand(atoi(mapArgs["-dropmessagestest"])) == 0)
4647 {
881a85a2 4648 LogPrintf("dropmessagestest DROPPING RECV MESSAGE\n");
0a61b0df 4649 return true;
4650 }
4651
0a61b0df 4652
4653
4654
4655 if (strCommand == "version")
4656 {
4657 // Each connection can only send one version message
4658 if (pfrom->nVersion != 0)
806704c2 4659 {
358ce266 4660 pfrom->PushMessage("reject", strCommand, REJECT_DUPLICATE, string("Duplicate version message"));
b2864d2f 4661 Misbehaving(pfrom->GetId(), 1);
0a61b0df 4662 return false;
806704c2 4663 }
0a61b0df 4664
51ed9ec9 4665 int64_t nTime;
0a61b0df 4666 CAddress addrMe;
4667 CAddress addrFrom;
51ed9ec9 4668 uint64_t nNonce = 1;
0a61b0df 4669 vRecv >> pfrom->nVersion >> pfrom->nServices >> nTime >> addrMe;
1ce41892 4670 if (pfrom->nVersion < MIN_PEER_PROTO_VERSION)
18c0fa97 4671 {
1ce41892 4672 // disconnect from peers older than this proto version
2e36866f 4673 LogPrintf("peer=%d using obsolete version %i; disconnecting\n", pfrom->id, pfrom->nVersion);
358ce266
GA
4674 pfrom->PushMessage("reject", strCommand, REJECT_OBSOLETE,
4675 strprintf("Version must be %d or greater", MIN_PEER_PROTO_VERSION));
18c0fa97
PW
4676 pfrom->fDisconnect = true;
4677 return false;
4678 }
4679
0a61b0df 4680 if (pfrom->nVersion == 10300)
4681 pfrom->nVersion = 300;
18c0fa97 4682 if (!vRecv.empty())
0a61b0df 4683 vRecv >> addrFrom >> nNonce;
a946aa8d 4684 if (!vRecv.empty()) {
216e9a44 4685 vRecv >> LIMITED_STRING(pfrom->strSubVer, 256);
a946aa8d
MH
4686 pfrom->cleanSubVer = SanitizeString(pfrom->strSubVer);
4687 }
18c0fa97 4688 if (!vRecv.empty())
0a61b0df 4689 vRecv >> pfrom->nStartingHeight;
4c8fc1a5
MC
4690 if (!vRecv.empty())
4691 vRecv >> pfrom->fRelayTxes; // set to true after we get the first filter* message
4692 else
4693 pfrom->fRelayTxes = true;
0a61b0df 4694
0a61b0df 4695 // Disconnect if we connected to ourself
4696 if (nNonce == nLocalHostNonce && nNonce > 1)
4697 {
7d9d134b 4698 LogPrintf("connected to self at %s, disconnecting\n", pfrom->addr.ToString());
0a61b0df 4699 pfrom->fDisconnect = true;
4700 return true;
4701 }
4702
845c86d1
GM
4703 pfrom->addrLocal = addrMe;
4704 if (pfrom->fInbound && addrMe.IsRoutable())
4705 {
4706 SeenLocal(addrMe);
4707 }
4708
cbc920d4
GA
4709 // Be shy and don't send version until we hear
4710 if (pfrom->fInbound)
4711 pfrom->PushVersion();
4712
0a61b0df 4713 pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
0a61b0df 4714
b4ee0bdd
PW
4715 // Potentially mark this peer as a preferred download peer.
4716 UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
0a61b0df 4717
4718 // Change version
18c0fa97 4719 pfrom->PushMessage("verack");
41b052ad 4720 pfrom->ssSend.SetVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
0a61b0df 4721
c891967b 4722 if (!pfrom->fInbound)
4723 {
4724 // Advertise our address
53a08815 4725 if (fListen && !IsInitialBlockDownload())
c891967b 4726 {
39857190
PW
4727 CAddress addr = GetLocalAddress(&pfrom->addr);
4728 if (addr.IsRoutable())
845c86d1
GM
4729 {
4730 pfrom->PushAddress(addr);
4731 } else if (IsPeerAddrLocalGood(pfrom)) {
4732 addr.SetIP(pfrom->addrLocal);
39857190 4733 pfrom->PushAddress(addr);
845c86d1 4734 }
c891967b 4735 }
4736
4737 // Get recent addresses
478b01d9 4738 if (pfrom->fOneShot || pfrom->nVersion >= CADDR_TIME_VERSION || addrman.size() < 1000)
c891967b 4739 {
4740 pfrom->PushMessage("getaddr");
4741 pfrom->fGetAddr = true;
4742 }
5fee401f
PW
4743 addrman.Good(pfrom->addr);
4744 } else {
4745 if (((CNetAddr)pfrom->addr) == (CNetAddr)addrFrom)
4746 {
4747 addrman.Add(addrFrom, addrFrom);
4748 addrman.Good(addrFrom);
4749 }
c891967b 4750 }
4751
0a61b0df 4752 // Relay alerts
f8dcd5ca
PW
4753 {
4754 LOCK(cs_mapAlerts);
223b6f1b 4755 BOOST_FOREACH(PAIRTYPE(const uint256, CAlert)& item, mapAlerts)
0a61b0df 4756 item.second.RelayTo(pfrom);
f8dcd5ca 4757 }
0a61b0df 4758
4759 pfrom->fSuccessfullyConnected = true;
4760
70b9d36a
JG
4761 string remoteAddr;
4762 if (fLogIPs)
4763 remoteAddr = ", peeraddr=" + pfrom->addr.ToString();
4764
4765 LogPrintf("receive version message: %s: version %d, blocks=%d, us=%s, peer=%d%s\n",
4766 pfrom->cleanSubVer, pfrom->nVersion,
4767 pfrom->nStartingHeight, addrMe.ToString(), pfrom->id,
4768 remoteAddr);
a8b95ce6 4769
26a6bae7
PJ
4770 int64_t nTimeOffset = nTime - GetTime();
4771 pfrom->nTimeOffset = nTimeOffset;
4772 AddTimeData(pfrom->addr, nTimeOffset);
0a61b0df 4773 }
4774
4775
4776 else if (pfrom->nVersion == 0)
4777 {
4778 // Must have a version message before anything else
b2864d2f 4779 Misbehaving(pfrom->GetId(), 1);
0a61b0df 4780 return false;
4781 }
4782
4783
4784 else if (strCommand == "verack")
4785 {
607dbfde 4786 pfrom->SetRecvVersion(min(pfrom->nVersion, PROTOCOL_VERSION));
9c273790
PW
4787
4788 // Mark this node as currently connected, so we update its timestamp later.
4789 if (pfrom->fNetworkNode) {
4790 LOCK(cs_main);
4791 State(pfrom->GetId())->fCurrentlyConnected = true;
4792 }
0a61b0df 4793 }
4794
4795
4796 else if (strCommand == "addr")
4797 {
4798 vector<CAddress> vAddr;
4799 vRecv >> vAddr;
c891967b 4800
4801 // Don't want addr from older versions unless seeding
8b09cd3a 4802 if (pfrom->nVersion < CADDR_TIME_VERSION && addrman.size() > 1000)
0a61b0df 4803 return true;
4804 if (vAddr.size() > 1000)
806704c2 4805 {
b2864d2f 4806 Misbehaving(pfrom->GetId(), 20);
783b182c 4807 return error("message addr size() = %u", vAddr.size());
806704c2 4808 }
0a61b0df 4809
4810 // Store the new addresses
090e5b40 4811 vector<CAddress> vAddrOk;
51ed9ec9
BD
4812 int64_t nNow = GetAdjustedTime();
4813 int64_t nSince = nNow - 10 * 60;
223b6f1b 4814 BOOST_FOREACH(CAddress& addr, vAddr)
0a61b0df 4815 {
b31499ec
GA
4816 boost::this_thread::interruption_point();
4817
c891967b 4818 if (addr.nTime <= 100000000 || addr.nTime > nNow + 10 * 60)
4819 addr.nTime = nNow - 5 * 24 * 60 * 60;
0a61b0df 4820 pfrom->AddAddressKnown(addr);
090e5b40 4821 bool fReachable = IsReachable(addr);
c891967b 4822 if (addr.nTime > nSince && !pfrom->fGetAddr && vAddr.size() <= 10 && addr.IsRoutable())
0a61b0df 4823 {
4824 // Relay to a limited number of other nodes
0a61b0df 4825 {
f8dcd5ca 4826 LOCK(cs_vNodes);
5cbf7532 4827 // Use deterministic randomness to send to the same nodes for 24 hours
d81cff32 4828 // at a time so the addrKnowns of the chosen nodes prevent repeats
0a61b0df 4829 static uint256 hashSalt;
4f152496 4830 if (hashSalt.IsNull())
f718aedd 4831 hashSalt = GetRandHash();
51ed9ec9 4832 uint64_t hashAddr = addr.GetHash();
734f85c4 4833 uint256 hashRand = ArithToUint256(UintToArith256(hashSalt) ^ (hashAddr<<32) ^ ((GetTime()+hashAddr)/(24*60*60)));
5cbf7532 4834 hashRand = Hash(BEGIN(hashRand), END(hashRand));
0a61b0df 4835 multimap<uint256, CNode*> mapMix;
223b6f1b 4836 BOOST_FOREACH(CNode* pnode, vNodes)
5cbf7532 4837 {
8b09cd3a 4838 if (pnode->nVersion < CADDR_TIME_VERSION)
c891967b 4839 continue;
5cbf7532 4840 unsigned int nPointer;
4841 memcpy(&nPointer, &pnode, sizeof(nPointer));
734f85c4 4842 uint256 hashKey = ArithToUint256(UintToArith256(hashRand) ^ nPointer);
5cbf7532 4843 hashKey = Hash(BEGIN(hashKey), END(hashKey));
4844 mapMix.insert(make_pair(hashKey, pnode));
4845 }
090e5b40 4846 int nRelayNodes = fReachable ? 2 : 1; // limited relaying of addresses outside our network(s)
0a61b0df 4847 for (multimap<uint256, CNode*>::iterator mi = mapMix.begin(); mi != mapMix.end() && nRelayNodes-- > 0; ++mi)
4848 ((*mi).second)->PushAddress(addr);
4849 }
4850 }
090e5b40
PW
4851 // Do not store addresses outside our network
4852 if (fReachable)
4853 vAddrOk.push_back(addr);
0a61b0df 4854 }
090e5b40 4855 addrman.Add(vAddrOk, pfrom->addr, 2 * 60 * 60);
0a61b0df 4856 if (vAddr.size() < 1000)
4857 pfrom->fGetAddr = false;
478b01d9
PW
4858 if (pfrom->fOneShot)
4859 pfrom->fDisconnect = true;
0a61b0df 4860 }
4861
4862
4863 else if (strCommand == "inv")
4864 {
4865 vector<CInv> vInv;
4866 vRecv >> vInv;
05a85b2b 4867 if (vInv.size() > MAX_INV_SZ)
806704c2 4868 {
b2864d2f 4869 Misbehaving(pfrom->GetId(), 20);
783b182c 4870 return error("message inv size() = %u", vInv.size());
806704c2 4871 }
0a61b0df 4872
7d38af3c
PW
4873 LOCK(cs_main);
4874
341735eb
PW
4875 std::vector<CInv> vToFetch;
4876
c376ac35 4877 for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
0a61b0df 4878 {
0aa89c08
PW
4879 const CInv &inv = vInv[nInv];
4880
b31499ec 4881 boost::this_thread::interruption_point();
0a61b0df 4882 pfrom->AddInventoryKnown(inv);
4883
ae8bfd12 4884 bool fAlreadyHave = AlreadyHave(inv);
2e36866f 4885 LogPrint("net", "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom->id);
0a61b0df 4886
341735eb
PW
4887 if (!fAlreadyHave && !fImporting && !fReindex && inv.type != MSG_BLOCK)
4888 pfrom->AskFor(inv);
0a61b0df 4889
341735eb 4890 if (inv.type == MSG_BLOCK) {
aa815647 4891 UpdateBlockAvailability(pfrom->GetId(), inv.hash);
341735eb 4892 if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
7e6d23b1 4893 // First request the headers preceding the announced block. In the normal fully-synced
341735eb
PW
4894 // case where a new block is announced that succeeds the current tip (no reorganization),
4895 // there are no such headers.
4896 // Secondly, and only when we are close to being synced, we request the announced block directly,
4897 // to avoid an extra round-trip. Note that we must *first* ask for the headers, so by the
4898 // time the block arrives, the header chain leading up to it is already validated. Not
4899 // doing this will result in the received block being rejected as an orphan in case it is
4900 // not a direct successor.
4901 pfrom->PushMessage("getheaders", chainActive.GetLocator(pindexBestHeader), inv.hash);
c9077043 4902 CNodeState *nodestate = State(pfrom->GetId());
e8e8904d 4903 if (chainActive.Tip()->GetBlockTime() > GetAdjustedTime() - chainparams.GetConsensus().nPowTargetSpacing * 20 &&
c9077043 4904 nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
341735eb
PW
4905 vToFetch.push_back(inv);
4906 // Mark block as in flight already, even though the actual "getdata" message only goes out
4907 // later (within the same cs_main lock, though).
82737933 4908 MarkBlockAsInFlight(pfrom->GetId(), inv.hash, chainparams.GetConsensus());
341735eb 4909 }
4c933229 4910 LogPrint("net", "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, inv.hash.ToString(), pfrom->id);
341735eb
PW
4911 }
4912 }
aa815647 4913
0a61b0df 4914 // Track requests for our stuff
26c16d9d 4915 GetMainSignals().Inventory(inv.hash);
540ac451
JG
4916
4917 if (pfrom->nSendSize > (SendBufferSize() * 2)) {
4918 Misbehaving(pfrom->GetId(), 50);
4919 return error("send buffer size() = %u", pfrom->nSendSize);
4920 }
0a61b0df 4921 }
341735eb
PW
4922
4923 if (!vToFetch.empty())
4924 pfrom->PushMessage("getdata", vToFetch);
0a61b0df 4925 }
4926
4927
4928 else if (strCommand == "getdata")
4929 {
4930 vector<CInv> vInv;
4931 vRecv >> vInv;
05a85b2b 4932 if (vInv.size() > MAX_INV_SZ)
806704c2 4933 {
b2864d2f 4934 Misbehaving(pfrom->GetId(), 20);
783b182c 4935 return error("message getdata size() = %u", vInv.size());
806704c2 4936 }
0a61b0df 4937
3b570559 4938 if (fDebug || (vInv.size() != 1))
2e36866f 4939 LogPrint("net", "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom->id);
983e4bde 4940
3b570559 4941 if ((fDebug && vInv.size() > 0) || (vInv.size() == 1))
2e36866f 4942 LogPrint("net", "received getdata for: %s peer=%d\n", vInv[0].ToString(), pfrom->id);
0a61b0df 4943
c7f039b6
PW
4944 pfrom->vRecvGetData.insert(pfrom->vRecvGetData.end(), vInv.begin(), vInv.end());
4945 ProcessGetData(pfrom);
0a61b0df 4946 }
4947
4948
4949 else if (strCommand == "getblocks")
4950 {
4951 CBlockLocator locator;
4952 uint256 hashStop;
4953 vRecv >> locator >> hashStop;
4954
7d38af3c
PW
4955 LOCK(cs_main);
4956
f03304a9 4957 // Find the last block the caller has in the main chain
6db83db3 4958 CBlockIndex* pindex = FindForkInGlobalIndex(chainActive, locator);
0a61b0df 4959
4960 // Send the rest of the chain
4961 if (pindex)
4c6d41b8 4962 pindex = chainActive.Next(pindex);
9d6cd04b 4963 int nLimit = 500;
4f152496 4964 LogPrint("net", "getblocks %d to %s limit %d from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.IsNull() ? "end" : hashStop.ToString(), nLimit, pfrom->id);
4c6d41b8 4965 for (; pindex; pindex = chainActive.Next(pindex))
0a61b0df 4966 {
4967 if (pindex->GetBlockHash() == hashStop)
4968 {
7d9d134b 4969 LogPrint("net", " getblocks stopping at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
0a61b0df 4970 break;
4971 }
4972 pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
9d6cd04b 4973 if (--nLimit <= 0)
0a61b0df 4974 {
b05a89b2
LD
4975 // When this block is requested, we'll send an inv that'll
4976 // trigger the peer to getblocks the next batch of inventory.
7d9d134b 4977 LogPrint("net", " getblocks stopping at limit %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
0a61b0df 4978 pfrom->hashContinue = pindex->GetBlockHash();
4979 break;
4980 }
4981 }
4982 }
4983
4984
f03304a9 4985 else if (strCommand == "getheaders")
4986 {
4987 CBlockLocator locator;
4988 uint256 hashStop;
4989 vRecv >> locator >> hashStop;
4990
7d38af3c
PW
4991 LOCK(cs_main);
4992
b4bbad18
SD
4993 if (IsInitialBlockDownload())
4994 return true;
4995
f03304a9 4996 CBlockIndex* pindex = NULL;
4997 if (locator.IsNull())
4998 {
4999 // If locator is null, return the hashStop block
145d5be8 5000 BlockMap::iterator mi = mapBlockIndex.find(hashStop);
f03304a9 5001 if (mi == mapBlockIndex.end())
5002 return true;
5003 pindex = (*mi).second;
5004 }
5005 else
5006 {
5007 // Find the last block the caller has in the main chain
6db83db3 5008 pindex = FindForkInGlobalIndex(chainActive, locator);
f03304a9 5009 if (pindex)
4c6d41b8 5010 pindex = chainActive.Next(pindex);
f03304a9 5011 }
5012
e754cf41 5013 // we must use CBlocks, as CBlockHeaders won't include the 0x00 nTx count at the end
f03304a9 5014 vector<CBlock> vHeaders;
341735eb 5015 int nLimit = MAX_HEADERS_RESULTS;
4c933229 5016 LogPrint("net", "getheaders %d to %s from peer=%d\n", (pindex ? pindex->nHeight : -1), hashStop.ToString(), pfrom->id);
4c6d41b8 5017 for (; pindex; pindex = chainActive.Next(pindex))
f03304a9 5018 {
5019 vHeaders.push_back(pindex->GetBlockHeader());
5020 if (--nLimit <= 0 || pindex->GetBlockHash() == hashStop)
5021 break;
5022 }
5023 pfrom->PushMessage("headers", vHeaders);
5024 }
5025
5026
0a61b0df 5027 else if (strCommand == "tx")
5028 {
5029 vector<uint256> vWorkQueue;
7a15109c 5030 vector<uint256> vEraseQueue;
0a61b0df 5031 CTransaction tx;
5032 vRecv >> tx;
5033
805344dc 5034 CInv inv(MSG_TX, tx.GetHash());
0a61b0df 5035 pfrom->AddInventoryKnown(inv);
5036
7d38af3c
PW
5037 LOCK(cs_main);
5038
0a61b0df 5039 bool fMissingInputs = false;
ef3988ca 5040 CValidationState state;
604ee2aa 5041
e2190f80 5042 pfrom->setAskFor.erase(inv.hash);
604ee2aa
B
5043 mapAlreadyAskedFor.erase(inv);
5044
60aed954 5045 if (!AlreadyHave(inv) && AcceptToMemoryPool(mempool, state, tx, true, &fMissingInputs))
0a61b0df 5046 {
a0fa20a1 5047 mempool.check(pcoinsTip);
d38da59b 5048 RelayTransaction(tx);
0a61b0df 5049 vWorkQueue.push_back(inv.hash);
5050
5262fde0 5051 LogPrint("mempool", "AcceptToMemoryPool: peer=%d %s: accepted %s (poolsz %u)\n",
2e36866f 5052 pfrom->id, pfrom->cleanSubVer,
805344dc 5053 tx.GetHash().ToString(),
ba6a4ea3
MH
5054 mempool.mapTx.size());
5055
0a61b0df 5056 // Recursively process any orphan transactions that depended on this one
c74332c6 5057 set<NodeId> setMisbehaving;
c376ac35 5058 for (unsigned int i = 0; i < vWorkQueue.size(); i++)
0a61b0df 5059 {
89d91f6a
WL
5060 map<uint256, set<uint256> >::iterator itByPrev = mapOrphanTransactionsByPrev.find(vWorkQueue[i]);
5061 if (itByPrev == mapOrphanTransactionsByPrev.end())
5062 continue;
5063 for (set<uint256>::iterator mi = itByPrev->second.begin();
5064 mi != itByPrev->second.end();
0a61b0df 5065 ++mi)
5066 {
159bc481 5067 const uint256& orphanHash = *mi;
c74332c6
GA
5068 const CTransaction& orphanTx = mapOrphanTransactions[orphanHash].tx;
5069 NodeId fromPeer = mapOrphanTransactions[orphanHash].fromPeer;
7a15109c 5070 bool fMissingInputs2 = false;
159bc481
GA
5071 // Use a dummy CValidationState so someone can't setup nodes to counter-DoS based on orphan
5072 // resolution (that is, feeding people an invalid transaction based on LegitTxX in order to get
5073 // anyone relaying LegitTxX banned)
8c4e4313 5074 CValidationState stateDummy;
0a61b0df 5075
c74332c6
GA
5076
5077 if (setMisbehaving.count(fromPeer))
5078 continue;
319b1160 5079 if (AcceptToMemoryPool(mempool, stateDummy, orphanTx, true, &fMissingInputs2))
0a61b0df 5080 {
7d9d134b 5081 LogPrint("mempool", " accepted orphan tx %s\n", orphanHash.ToString());
d38da59b 5082 RelayTransaction(orphanTx);
159bc481 5083 vWorkQueue.push_back(orphanHash);
37b4e425 5084 vEraseQueue.push_back(orphanHash);
7a15109c
GA
5085 }
5086 else if (!fMissingInputs2)
5087 {
c74332c6
GA
5088 int nDos = 0;
5089 if (stateDummy.IsInvalid(nDos) && nDos > 0)
5090 {
5091 // Punish peer that gave us an invalid orphan tx
5092 Misbehaving(fromPeer, nDos);
5093 setMisbehaving.insert(fromPeer);
5094 LogPrint("mempool", " invalid orphan tx %s\n", orphanHash.ToString());
5095 }
37b4e425
AM
5096 // Has inputs but not accepted to mempool
5097 // Probably non-standard or insufficient fee/priority
7d9d134b 5098 LogPrint("mempool", " removed orphan tx %s\n", orphanHash.ToString());
37b4e425 5099 vEraseQueue.push_back(orphanHash);
5094a81d 5100 assert(recentRejects);
ec9b6c33 5101 recentRejects->insert(orphanHash);
0a61b0df 5102 }
a0fa20a1 5103 mempool.check(pcoinsTip);
0a61b0df 5104 }
5105 }
5106
7a15109c 5107 BOOST_FOREACH(uint256 hash, vEraseQueue)
0a61b0df 5108 EraseOrphanTx(hash);
5109 }
b7e4abd6 5110 // TODO: currently, prohibit joinsplits from entering mapOrphans
8675d94b 5111 else if (fMissingInputs && tx.vjoinsplit.size() == 0)
0a61b0df 5112 {
c74332c6 5113 AddOrphanTx(tx, pfrom->GetId());
142e6041
GA
5114
5115 // DoS prevention: do not allow mapOrphanTransactions to grow unbounded
aa3c697e
GA
5116 unsigned int nMaxOrphanTx = (unsigned int)std::max((int64_t)0, GetArg("-maxorphantx", DEFAULT_MAX_ORPHAN_TRANSACTIONS));
5117 unsigned int nEvicted = LimitOrphanTxSize(nMaxOrphanTx);
142e6041 5118 if (nEvicted > 0)
881a85a2 5119 LogPrint("mempool", "mapOrphan overflow, removed %u tx\n", nEvicted);
ec9b6c33 5120 } else {
36f14bf2 5121 assert(recentRejects);
805344dc 5122 recentRejects->insert(tx.GetHash());
36f14bf2 5123
ec9b6c33
PT
5124 if (pfrom->fWhitelisted) {
5125 // Always relay transactions received from whitelisted peers, even
60aed954
PW
5126 // if they were already in the mempool or rejected from it due
5127 // to policy, allowing the node to function as a gateway for
5128 // nodes hidden behind it.
ec9b6c33 5129 //
60aed954
PW
5130 // Never relay transactions that we would assign a non-zero DoS
5131 // score for, as we expect peers to do the same with us in that
5132 // case.
5133 int nDoS = 0;
5134 if (!state.IsInvalid(nDoS) || nDoS == 0) {
5135 LogPrintf("Force relaying tx %s from whitelisted peer=%d\n", tx.GetHash().ToString(), pfrom->id);
5136 RelayTransaction(tx);
5137 } else {
e63d14fd 5138 LogPrintf("Not relaying invalid transaction %s from whitelisted peer=%d (%s (code %d))\n",
de3dd8a0 5139 tx.GetHash().ToString(), pfrom->id, state.GetRejectReason(), state.GetRejectCode());
60aed954 5140 }
ec9b6c33 5141 }
0a61b0df 5142 }
fbed9c9d 5143 int nDoS = 0;
5ea66c54 5144 if (state.IsInvalid(nDoS))
2b45345a 5145 {
805344dc 5146 LogPrint("mempool", "%s from peer=%d %s was not accepted into the memory pool: %s\n", tx.GetHash().ToString(),
2e36866f 5147 pfrom->id, pfrom->cleanSubVer,
7d9d134b 5148 state.GetRejectReason());
358ce266 5149 pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),
307f7d48 5150 state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash);
5ea66c54 5151 if (nDoS > 0)
b2864d2f 5152 Misbehaving(pfrom->GetId(), nDoS);
358ce266 5153 }
0a61b0df 5154 }
5155
5156
341735eb
PW
5157 else if (strCommand == "headers" && !fImporting && !fReindex) // Ignore headers received while importing
5158 {
5159 std::vector<CBlockHeader> headers;
5160
5161 // Bypass the normal CBlock deserialization, as we don't want to risk deserializing 2000 full blocks.
5162 unsigned int nCount = ReadCompactSize(vRecv);
5163 if (nCount > MAX_HEADERS_RESULTS) {
5164 Misbehaving(pfrom->GetId(), 20);
5165 return error("headers message size = %u", nCount);
5166 }
5167 headers.resize(nCount);
5168 for (unsigned int n = 0; n < nCount; n++) {
5169 vRecv >> headers[n];
5170 ReadCompactSize(vRecv); // ignore tx count; assume it is 0.
5171 }
5172
5173 LOCK(cs_main);
5174
5175 if (nCount == 0) {
5176 // Nothing interesting. Stop asking this peers for more headers.
5177 return true;
5178 }
5179
5180 CBlockIndex *pindexLast = NULL;
5181 BOOST_FOREACH(const CBlockHeader& header, headers) {
5182 CValidationState state;
5183 if (pindexLast != NULL && header.hashPrevBlock != pindexLast->GetBlockHash()) {
5184 Misbehaving(pfrom->GetId(), 20);
5185 return error("non-continuous headers sequence");
5186 }
5187 if (!AcceptBlockHeader(header, state, &pindexLast)) {
5188 int nDoS;
5189 if (state.IsInvalid(nDoS)) {
5190 if (nDoS > 0)
0d2cefb0 5191 Misbehaving(pfrom->GetId(), nDoS/nDoS);
341735eb
PW
5192 return error("invalid header received");
5193 }
5194 }
5195 }
5196
5197 if (pindexLast)
5198 UpdateBlockAvailability(pfrom->GetId(), pindexLast->GetBlockHash());
5199
5200 if (nCount == MAX_HEADERS_RESULTS && pindexLast) {
5201 // Headers message had its maximum size; the peer may have more headers.
5202 // TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
5203 // from there instead.
4c933229 5204 LogPrint("net", "more getheaders (%d) to end to peer=%d (startheight:%d)\n", pindexLast->nHeight, pfrom->id, pfrom->nStartingHeight);
4f152496 5205 pfrom->PushMessage("getheaders", chainActive.GetLocator(pindexLast), uint256());
341735eb 5206 }
3fcfbc8a
PW
5207
5208 CheckBlockIndex();
341735eb
PW
5209 }
5210
7fea4846 5211 else if (strCommand == "block" && !fImporting && !fReindex) // Ignore blocks received while importing
0a61b0df 5212 {
f03304a9 5213 CBlock block;
5214 vRecv >> block;
0a61b0df 5215
f03304a9 5216 CInv inv(MSG_BLOCK, block.GetHash());
341735eb 5217 LogPrint("net", "received block %s peer=%d\n", inv.hash.ToString(), pfrom->id);
0a61b0df 5218
341735eb 5219 pfrom->AddInventoryKnown(inv);
7d38af3c 5220
ef3988ca 5221 CValidationState state;
93b606ae
SD
5222 // Process all blocks from whitelisted peers, even if not requested,
5223 // unless we're still syncing with the network.
5224 // Such an unrequested block may still be processed, subject to the
5225 // conditions in AcceptBlock().
5226 bool forceProcessing = pfrom->fWhitelisted && !IsInitialBlockDownload();
35915149 5227 ProcessNewBlock(0,state, pfrom, &block, forceProcessing, NULL);
40f5cb87
PW
5228 int nDoS;
5229 if (state.IsInvalid(nDoS)) {
5230 pfrom->PushMessage("reject", strCommand, state.GetRejectCode(),
307f7d48 5231 state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash);
40f5cb87
PW
5232 if (nDoS > 0) {
5233 LOCK(cs_main);
5234 Misbehaving(pfrom->GetId(), nDoS);
5235 }
5236 }
5237
0a61b0df 5238 }
5239
5240
dca799e1
IP
5241 // This asymmetric behavior for inbound and outbound connections was introduced
5242 // to prevent a fingerprinting attack: an attacker can send specific fake addresses
b05a89b2
LD
5243 // to users' AddrMan and later request them by sending getaddr messages.
5244 // Making nodes which are behind NAT and can only make outgoing connections ignore
5245 // the getaddr message mitigates the attack.
dca799e1 5246 else if ((strCommand == "getaddr") && (pfrom->fInbound))
0a61b0df 5247 {
a514cb29
GM
5248 // Only send one GetAddr response per connection to reduce resource waste
5249 // and discourage addr stamping of INV announcements.
5250 if (pfrom->fSentAddr) {
5251 LogPrint("net", "Ignoring repeated \"getaddr\". peer=%d\n", pfrom->id);
5252 return true;
5253 }
5254 pfrom->fSentAddr = true;
5255
0a61b0df 5256 pfrom->vAddrToSend.clear();
5fee401f
PW
5257 vector<CAddress> vAddr = addrman.GetAddr();
5258 BOOST_FOREACH(const CAddress &addr, vAddr)
5259 pfrom->PushAddress(addr);
0a61b0df 5260 }
5261
5262
05a85b2b
JG
5263 else if (strCommand == "mempool")
5264 {
319b1160 5265 LOCK2(cs_main, pfrom->cs_filter);
7d38af3c 5266
05a85b2b
JG
5267 std::vector<uint256> vtxid;
5268 mempool.queryHashes(vtxid);
5269 vector<CInv> vInv;
c51694eb
MC
5270 BOOST_FOREACH(uint256& hash, vtxid) {
5271 CInv inv(MSG_TX, hash);
319b1160
GA
5272 CTransaction tx;
5273 bool fInMemPool = mempool.lookup(hash, tx);
5274 if (!fInMemPool) continue; // another thread removed since queryHashes, maybe...
d38da59b 5275 if ((pfrom->pfilter && pfrom->pfilter->IsRelevantAndUpdate(tx)) ||
c51694eb
MC
5276 (!pfrom->pfilter))
5277 vInv.push_back(inv);
1f3d3647
GA
5278 if (vInv.size() == MAX_INV_SZ) {
5279 pfrom->PushMessage("inv", vInv);
5280 vInv.clear();
5281 }
05a85b2b
JG
5282 }
5283 if (vInv.size() > 0)
5284 pfrom->PushMessage("inv", vInv);
5285 }
5286
5287
0a61b0df 5288 else if (strCommand == "ping")
5289 {
93e447b6
JG
5290 if (pfrom->nVersion > BIP0031_VERSION)
5291 {
51ed9ec9 5292 uint64_t nonce = 0;
93e447b6
JG
5293 vRecv >> nonce;
5294 // Echo the message back with the nonce. This allows for two useful features:
5295 //
5296 // 1) A remote node can quickly check if the connection is operational
5297 // 2) Remote nodes can measure the latency of the network thread. If this node
5298 // is overloaded it won't respond to pings quickly and the remote node can
5299 // avoid sending us more work, like chain download requests.
5300 //
5301 // The nonce stops the remote getting confused between different pings: without
5302 // it, if the remote node sends a ping once per second and this node takes 5
5303 // seconds to respond to each, the 5th ping the remote sends would appear to
5304 // return very quickly.
5305 pfrom->PushMessage("pong", nonce);
5306 }
0a61b0df 5307 }
5308
5309
971bb3e9
JL
5310 else if (strCommand == "pong")
5311 {
9f4da19b 5312 int64_t pingUsecEnd = nTimeReceived;
51ed9ec9 5313 uint64_t nonce = 0;
971bb3e9
JL
5314 size_t nAvail = vRecv.in_avail();
5315 bool bPingFinished = false;
5316 std::string sProblem;
cd696e64 5317
971bb3e9
JL
5318 if (nAvail >= sizeof(nonce)) {
5319 vRecv >> nonce;
cd696e64 5320
971bb3e9
JL
5321 // Only process pong message if there is an outstanding ping (old ping without nonce should never pong)
5322 if (pfrom->nPingNonceSent != 0) {
5323 if (nonce == pfrom->nPingNonceSent) {
5324 // Matching pong received, this ping is no longer outstanding
5325 bPingFinished = true;
51ed9ec9 5326 int64_t pingUsecTime = pingUsecEnd - pfrom->nPingUsecStart;
971bb3e9
JL
5327 if (pingUsecTime > 0) {
5328 // Successful ping time measurement, replace previous
5329 pfrom->nPingUsecTime = pingUsecTime;
e279e5f9 5330 pfrom->nMinPingUsecTime = std::min(pfrom->nMinPingUsecTime, pingUsecTime);
971bb3e9
JL
5331 } else {
5332 // This should never happen
5333 sProblem = "Timing mishap";
5334 }
5335 } else {
5336 // Nonce mismatches are normal when pings are overlapping
5337 sProblem = "Nonce mismatch";
5338 if (nonce == 0) {
7e6d23b1 5339 // This is most likely a bug in another implementation somewhere; cancel this ping
971bb3e9
JL
5340 bPingFinished = true;
5341 sProblem = "Nonce zero";
5342 }
5343 }
5344 } else {
5345 sProblem = "Unsolicited pong without ping";
5346 }
5347 } else {
7e6d23b1 5348 // This is most likely a bug in another implementation somewhere; cancel this ping
971bb3e9
JL
5349 bPingFinished = true;
5350 sProblem = "Short payload";
5351 }
cd696e64 5352
971bb3e9 5353 if (!(sProblem.empty())) {
2e36866f
B
5354 LogPrint("net", "pong peer=%d %s: %s, %x expected, %x received, %u bytes\n",
5355 pfrom->id,
7d9d134b
WL
5356 pfrom->cleanSubVer,
5357 sProblem,
7dea6345
PK
5358 pfrom->nPingNonceSent,
5359 nonce,
5360 nAvail);
971bb3e9
JL
5361 }
5362 if (bPingFinished) {
5363 pfrom->nPingNonceSent = 0;
5364 }
5365 }
cd696e64
PK
5366
5367
4d9c7fe6 5368 else if (fAlerts && strCommand == "alert")
0a61b0df 5369 {
5370 CAlert alert;
5371 vRecv >> alert;
5372
d5a52d9b
GA
5373 uint256 alertHash = alert.GetHash();
5374 if (pfrom->setKnown.count(alertHash) == 0)
0a61b0df 5375 {
f14e687f 5376 if (alert.ProcessAlert(Params().AlertKey()))
f8dcd5ca 5377 {
d5a52d9b
GA
5378 // Relay
5379 pfrom->setKnown.insert(alertHash);
5380 {
5381 LOCK(cs_vNodes);
5382 BOOST_FOREACH(CNode* pnode, vNodes)
5383 alert.RelayTo(pnode);
5384 }
5385 }
5386 else {
5387 // Small DoS penalty so peers that send us lots of
5388 // duplicate/expired/invalid-signature/whatever alerts
5389 // eventually get banned.
5390 // This isn't a Misbehaving(100) (immediate ban) because the
5391 // peer might be an older or different implementation with
5392 // a different signature key, etc.
b2864d2f 5393 Misbehaving(pfrom->GetId(), 10);
f8dcd5ca 5394 }
0a61b0df 5395 }
5396 }
5397
5398
422d1225
MC
5399 else if (strCommand == "filterload")
5400 {
5401 CBloomFilter filter;
5402 vRecv >> filter;
5403
5404 if (!filter.IsWithinSizeConstraints())
5405 // There is no excuse for sending a too-large filter
b2864d2f 5406 Misbehaving(pfrom->GetId(), 100);
422d1225
MC
5407 else
5408 {
5409 LOCK(pfrom->cs_filter);
5410 delete pfrom->pfilter;
5411 pfrom->pfilter = new CBloomFilter(filter);
a7f533a9 5412 pfrom->pfilter->UpdateEmptyFull();
422d1225 5413 }
4c8fc1a5 5414 pfrom->fRelayTxes = true;
422d1225
MC
5415 }
5416
5417
5418 else if (strCommand == "filteradd")
5419 {
5420 vector<unsigned char> vData;
5421 vRecv >> vData;
5422
5423 // Nodes must NEVER send a data item > 520 bytes (the max size for a script data object,
5424 // and thus, the maximum size any matched object can have) in a filteradd message
192cc910 5425 if (vData.size() > MAX_SCRIPT_ELEMENT_SIZE)
422d1225 5426 {
b2864d2f 5427 Misbehaving(pfrom->GetId(), 100);
422d1225
MC
5428 } else {
5429 LOCK(pfrom->cs_filter);
5430 if (pfrom->pfilter)
5431 pfrom->pfilter->insert(vData);
5432 else
b2864d2f 5433 Misbehaving(pfrom->GetId(), 100);
422d1225
MC
5434 }
5435 }
5436
5437
5438 else if (strCommand == "filterclear")
5439 {
5440 LOCK(pfrom->cs_filter);
5441 delete pfrom->pfilter;
37c6389c 5442 pfrom->pfilter = new CBloomFilter();
4c8fc1a5 5443 pfrom->fRelayTxes = true;
422d1225
MC
5444 }
5445
5446
358ce266
GA
5447 else if (strCommand == "reject")
5448 {
efad808a
PW
5449 if (fDebug) {
5450 try {
5451 string strMsg; unsigned char ccode; string strReason;
307f7d48 5452 vRecv >> LIMITED_STRING(strMsg, CMessageHeader::COMMAND_SIZE) >> ccode >> LIMITED_STRING(strReason, MAX_REJECT_MESSAGE_LENGTH);
358ce266 5453
efad808a
PW
5454 ostringstream ss;
5455 ss << strMsg << " code " << itostr(ccode) << ": " << strReason;
358ce266 5456
efad808a
PW
5457 if (strMsg == "block" || strMsg == "tx")
5458 {
5459 uint256 hash;
5460 vRecv >> hash;
5461 ss << ": hash " << hash.ToString();
5462 }
5463 LogPrint("net", "Reject %s\n", SanitizeString(ss.str()));
27df4123 5464 } catch (const std::ios_base::failure&) {
efad808a
PW
5465 // Avoid feedback loops by preventing reject messages from triggering a new reject message.
5466 LogPrint("net", "Unparseable reject message received\n");
358ce266 5467 }
358ce266
GA
5468 }
5469 }
432bc22a 5470 else if (strCommand == "notfound") {
e496b2e3
WL
5471 // We do not care about the NOTFOUND message, but logging an Unknown Command
5472 // message would be undesirable as we transmit it ourselves.
5473 }
5474
5475 else {
0a61b0df 5476 // Ignore unknown commands for extensibility
6ecf3edf 5477 LogPrint("net", "Unknown command \"%s\" from peer=%d\n", SanitizeString(strCommand), pfrom->id);
0a61b0df 5478 }
5479
5480
0a61b0df 5481
5482 return true;
5483}
5484
607dbfde 5485// requires LOCK(cs_vRecvMsg)
e89b9f6a
PW
5486bool ProcessMessages(CNode* pfrom)
5487{
e89b9f6a 5488 //if (fDebug)
30c1db1c 5489 // LogPrintf("%s(%u messages)\n", __func__, pfrom->vRecvMsg.size());
0a61b0df 5490
e89b9f6a
PW
5491 //
5492 // Message format
5493 // (4) message start
5494 // (12) command
5495 // (4) size
5496 // (4) checksum
5497 // (x) data
5498 //
967f2459 5499 bool fOk = true;
0a61b0df 5500
c7f039b6
PW
5501 if (!pfrom->vRecvGetData.empty())
5502 ProcessGetData(pfrom);
cd696e64 5503
75ef87dd
PS
5504 // this maintains the order of responses
5505 if (!pfrom->vRecvGetData.empty()) return fOk;
cd696e64 5506
967f2459 5507 std::deque<CNetMessage>::iterator it = pfrom->vRecvMsg.begin();
41b052ad 5508 while (!pfrom->fDisconnect && it != pfrom->vRecvMsg.end()) {
9d6cd04b 5509 // Don't bother if send buffer is too full to respond anyway
41b052ad 5510 if (pfrom->nSendSize >= SendBufferSize())
9d6cd04b
MC
5511 break;
5512
967f2459
PW
5513 // get next message
5514 CNetMessage& msg = *it;
607dbfde
JG
5515
5516 //if (fDebug)
30c1db1c 5517 // LogPrintf("%s(message %u msgsz, %u bytes, complete:%s)\n", __func__,
607dbfde
JG
5518 // msg.hdr.nMessageSize, msg.vRecv.size(),
5519 // msg.complete() ? "Y" : "N");
5520
967f2459 5521 // end, if an incomplete message is found
607dbfde 5522 if (!msg.complete())
e89b9f6a 5523 break;
607dbfde 5524
967f2459
PW
5525 // at this point, any failure means we can delete the current message
5526 it++;
5527
607dbfde 5528 // Scan for message start
0e4b3175 5529 if (memcmp(msg.hdr.pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0) {
28d4cff0 5530 LogPrintf("PROCESSMESSAGE: INVALID MESSAGESTART %s peer=%d\n", SanitizeString(msg.hdr.GetCommand()), pfrom->id);
967f2459
PW
5531 fOk = false;
5532 break;
e89b9f6a 5533 }
0a61b0df 5534
e89b9f6a 5535 // Read header
607dbfde 5536 CMessageHeader& hdr = msg.hdr;
eec37136 5537 if (!hdr.IsValid(Params().MessageStart()))
e89b9f6a 5538 {
28d4cff0 5539 LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->id);
e89b9f6a
PW
5540 continue;
5541 }
5542 string strCommand = hdr.GetCommand();
5543
5544 // Message size
5545 unsigned int nMessageSize = hdr.nMessageSize;
e89b9f6a
PW
5546
5547 // Checksum
607dbfde 5548 CDataStream& vRecv = msg.vRecv;
18c0fa97 5549 uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize);
556814ec 5550 unsigned int nChecksum = ReadLE32((unsigned char*)&hash);
18c0fa97 5551 if (nChecksum != hdr.nChecksum)
e89b9f6a 5552 {
30c1db1c 5553 LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR nChecksum=%08x hdr.nChecksum=%08x\n", __func__,
28d4cff0 5554 SanitizeString(strCommand), nMessageSize, nChecksum, hdr.nChecksum);
18c0fa97 5555 continue;
e89b9f6a
PW
5556 }
5557
e89b9f6a
PW
5558 // Process message
5559 bool fRet = false;
5560 try
5561 {
9f4da19b 5562 fRet = ProcessMessage(pfrom, strCommand, vRecv, msg.nTime);
b31499ec 5563 boost::this_thread::interruption_point();
e89b9f6a 5564 }
27df4123 5565 catch (const std::ios_base::failure& e)
e89b9f6a 5566 {
358ce266 5567 pfrom->PushMessage("reject", strCommand, REJECT_MALFORMED, string("error parsing message"));
e89b9f6a
PW
5568 if (strstr(e.what(), "end of data"))
5569 {
814efd6f 5570 // Allow exceptions from under-length message on vRecv
30c1db1c 5571 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());
e89b9f6a
PW
5572 }
5573 else if (strstr(e.what(), "size too large"))
5574 {
814efd6f 5575 // Allow exceptions from over-long size
30c1db1c 5576 LogPrintf("%s(%s, %u bytes): Exception '%s' caught\n", __func__, SanitizeString(strCommand), nMessageSize, e.what());
e89b9f6a
PW
5577 }
5578 else
5579 {
5970a0d7 5580 //PrintExceptionContinue(&e, "ProcessMessages()");
e89b9f6a
PW
5581 }
5582 }
27df4123 5583 catch (const boost::thread_interrupted&) {
b31499ec
GA
5584 throw;
5585 }
27df4123 5586 catch (const std::exception& e) {
ea591ead 5587 PrintExceptionContinue(&e, "ProcessMessages()");
e89b9f6a 5588 } catch (...) {
ea591ead 5589 PrintExceptionContinue(NULL, "ProcessMessages()");
e89b9f6a
PW
5590 }
5591
5592 if (!fRet)
30c1db1c 5593 LogPrintf("%s(%s, %u bytes) FAILED peer=%d\n", __func__, SanitizeString(strCommand), nMessageSize, pfrom->id);
cd696e64 5594
75ef87dd 5595 break;
e89b9f6a
PW
5596 }
5597
41b052ad
PW
5598 // In case the connection got shut down, its receive buffer was wiped
5599 if (!pfrom->fDisconnect)
5600 pfrom->vRecvMsg.erase(pfrom->vRecvMsg.begin(), it);
5601
967f2459 5602 return fOk;
e89b9f6a 5603}
0a61b0df 5604
5605
0a61b0df 5606bool SendMessages(CNode* pto, bool fSendTrickle)
5607{
e8e8904d 5608 const Consensus::Params& consensusParams = Params().GetConsensus();
6055b910 5609 {
b05a89b2 5610 // Don't send anything until we get its version message
0a61b0df 5611 if (pto->nVersion == 0)
5612 return true;
5613
971bb3e9
JL
5614 //
5615 // Message: ping
5616 //
5617 bool pingSend = false;
5618 if (pto->fPingQueued) {
5619 // RPC ping request by user
5620 pingSend = true;
5621 }
f1920e86
PW
5622 if (pto->nPingNonceSent == 0 && pto->nPingUsecStart + PING_INTERVAL * 1000000 < GetTimeMicros()) {
5623 // Ping automatically sent as a latency probe & keepalive.
971bb3e9
JL
5624 pingSend = true;
5625 }
5626 if (pingSend) {
51ed9ec9 5627 uint64_t nonce = 0;
971bb3e9 5628 while (nonce == 0) {
001a53d7 5629 GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
971bb3e9 5630 }
971bb3e9 5631 pto->fPingQueued = false;
f1920e86 5632 pto->nPingUsecStart = GetTimeMicros();
971bb3e9 5633 if (pto->nVersion > BIP0031_VERSION) {
f1920e86 5634 pto->nPingNonceSent = nonce;
c971112d 5635 pto->PushMessage("ping", nonce);
971bb3e9 5636 } else {
f1920e86
PW
5637 // Peer is too old to support ping command with nonce, pong will never arrive.
5638 pto->nPingNonceSent = 0;
93e447b6 5639 pto->PushMessage("ping");
971bb3e9 5640 }
93e447b6 5641 }
0a61b0df 5642
55a1db4f
WL
5643 TRY_LOCK(cs_main, lockMain); // Acquire cs_main for IsInitialBlockDownload() and CNodeState()
5644 if (!lockMain)
5645 return true;
5646
0a61b0df 5647 // Address refresh broadcast
51ed9ec9 5648 static int64_t nLastRebroadcast;
5d1b8f17 5649 if (!IsInitialBlockDownload() && (GetTime() - nLastRebroadcast > 24 * 60 * 60))
0a61b0df 5650 {
845c86d1
GM
5651 LOCK(cs_vNodes);
5652 BOOST_FOREACH(CNode* pnode, vNodes)
0a61b0df 5653 {
d81cff32 5654 // Periodically clear addrKnown to allow refresh broadcasts
845c86d1 5655 if (nLastRebroadcast)
83671efe 5656 pnode->addrKnown.reset();
0a61b0df 5657
845c86d1
GM
5658 // Rebroadcast our address
5659 AdvertizeLocal(pnode);
0a61b0df 5660 }
845c86d1
GM
5661 if (!vNodes.empty())
5662 nLastRebroadcast = GetTime();
0a61b0df 5663 }
5664
0a61b0df 5665 //
5666 // Message: addr
5667 //
5668 if (fSendTrickle)
5669 {
5670 vector<CAddress> vAddr;
5671 vAddr.reserve(pto->vAddrToSend.size());
223b6f1b 5672 BOOST_FOREACH(const CAddress& addr, pto->vAddrToSend)
0a61b0df 5673 {
d81cff32 5674 if (!pto->addrKnown.contains(addr.GetKey()))
0a61b0df 5675 {
d81cff32 5676 pto->addrKnown.insert(addr.GetKey());
0a61b0df 5677 vAddr.push_back(addr);
5678 // receiver rejects addr messages larger than 1000
5679 if (vAddr.size() >= 1000)
5680 {
5681 pto->PushMessage("addr", vAddr);
5682 vAddr.clear();
5683 }
5684 }
5685 }
5686 pto->vAddrToSend.clear();
5687 if (!vAddr.empty())
5688 pto->PushMessage("addr", vAddr);
5689 }
5690
75f51f2a
PW
5691 CNodeState &state = *State(pto->GetId());
5692 if (state.fShouldBan) {
dc942e6f
PW
5693 if (pto->fWhitelisted)
5694 LogPrintf("Warning: not punishing whitelisted peer %s!\n", pto->addr.ToString());
b2864d2f
PW
5695 else {
5696 pto->fDisconnect = true;
dc942e6f
PW
5697 if (pto->addr.IsLocal())
5698 LogPrintf("Warning: not banning local peer %s!\n", pto->addr.ToString());
5699 else
c74332c6 5700 {
dc942e6f 5701 CNode::Ban(pto->addr);
c74332c6 5702 }
b2864d2f 5703 }
75f51f2a 5704 state.fShouldBan = false;
b2864d2f
PW
5705 }
5706
75f51f2a
PW
5707 BOOST_FOREACH(const CBlockReject& reject, state.rejects)
5708 pto->PushMessage("reject", (string)"block", reject.chRejectCode, reject.strRejectReason, reject.hashBlock);
5709 state.rejects.clear();
5710
6055b910 5711 // Start block sync
341735eb
PW
5712 if (pindexBestHeader == NULL)
5713 pindexBestHeader = chainActive.Tip();
b4ee0bdd 5714 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.
00dcaf4b 5715 if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) {
341735eb 5716 // Only actively request headers from a single peer, unless we're close to today.
00dcaf4b 5717 if ((nSyncStarted == 0 && fFetch) || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
341735eb
PW
5718 state.fSyncStarted = true;
5719 nSyncStarted++;
5720 CBlockIndex *pindexStart = pindexBestHeader->pprev ? pindexBestHeader->pprev : pindexBestHeader;
4c933229 5721 LogPrint("net", "initial getheaders (%d) to peer=%d (startheight:%d)\n", pindexStart->nHeight, pto->id, pto->nStartingHeight);
4f152496 5722 pto->PushMessage("getheaders", chainActive.GetLocator(pindexStart), uint256());
341735eb 5723 }
6055b910
PW
5724 }
5725
5726 // Resend wallet transactions that haven't gotten in a block yet
5727 // Except during reindex, importing and IBD, when old wallet
5728 // transactions become unconfirmed and spams other nodes.
5729 if (!fReindex && !fImporting && !IsInitialBlockDownload())
5730 {
0f5954c4 5731 GetMainSignals().Broadcast(nTimeBestReceived);
6055b910 5732 }
0a61b0df 5733
5734 //
5735 // Message: inventory
5736 //
5737 vector<CInv> vInv;
5738 vector<CInv> vInvWait;
0a61b0df 5739 {
f8dcd5ca 5740 LOCK(pto->cs_inventory);
0a61b0df 5741 vInv.reserve(pto->vInventoryToSend.size());
5742 vInvWait.reserve(pto->vInventoryToSend.size());
223b6f1b 5743 BOOST_FOREACH(const CInv& inv, pto->vInventoryToSend)
0a61b0df 5744 {
5745 if (pto->setInventoryKnown.count(inv))
5746 continue;
5747
5748 // trickle out tx inv to protect privacy
5749 if (inv.type == MSG_TX && !fSendTrickle)
5750 {
5751 // 1/4 of tx invs blast to all immediately
5752 static uint256 hashSalt;
4f152496 5753 if (hashSalt.IsNull())
f718aedd 5754 hashSalt = GetRandHash();
734f85c4 5755 uint256 hashRand = ArithToUint256(UintToArith256(inv.hash) ^ UintToArith256(hashSalt));
0a61b0df 5756 hashRand = Hash(BEGIN(hashRand), END(hashRand));
734f85c4 5757 bool fTrickleWait = ((UintToArith256(hashRand) & 3) != 0);
0a61b0df 5758
0a61b0df 5759 if (fTrickleWait)
5760 {
5761 vInvWait.push_back(inv);
5762 continue;
5763 }
5764 }
5765
5766 // returns true if wasn't already contained in the set
5767 if (pto->setInventoryKnown.insert(inv).second)
5768 {
5769 vInv.push_back(inv);
5770 if (vInv.size() >= 1000)
5771 {
5772 pto->PushMessage("inv", vInv);
5773 vInv.clear();
5774 }
5775 }
5776 }
5777 pto->vInventoryToSend = vInvWait;
5778 }
5779 if (!vInv.empty())
5780 pto->PushMessage("inv", vInv);
5781
341735eb 5782 // Detect whether we're stalling
f59d8f0b 5783 int64_t nNow = GetTimeMicros();
341735eb
PW
5784 if (!pto->fDisconnect && state.nStallingSince && state.nStallingSince < nNow - 1000000 * BLOCK_STALLING_TIMEOUT) {
5785 // Stalling only triggers when the block download window cannot move. During normal steady state,
5786 // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
5787 // should only happen during initial block download.
5788 LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->id);
f59d8f0b
PW
5789 pto->fDisconnect = true;
5790 }
3ff735c9 5791 // In case there is a block that has been in flight from this peer for (2 + 0.5 * N) times the block interval
91613034
PW
5792 // (with N the number of validated blocks that were in flight at the time it was requested), disconnect due to
5793 // timeout. We compensate for in-flight blocks to prevent killing off peers due to our own downstream link
7e6d23b1 5794 // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
91613034 5795 // to unreasonably increase our timeout.
8ba7f842
SD
5796 // We also compare the block download timeout originally calculated against the time at which we'd disconnect
5797 // if we assumed the block were being requested now (ignoring blocks we've requested from this peer, since we're
5798 // only looking at this peer's oldest request). This way a large queue in the past doesn't result in a
5799 // permanently large window for this block to be delivered (ie if the number of blocks in flight is decreasing
5800 // more quickly than once every 5 minutes, then we'll shorten the download window for this block).
5801 if (!pto->fDisconnect && state.vBlocksInFlight.size() > 0) {
5802 QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
82737933 5803 int64_t nTimeoutIfRequestedNow = GetBlockTimeout(nNow, nQueuedValidatedHeaders - state.nBlocksInFlightValidHeaders, consensusParams);
8ba7f842
SD
5804 if (queuedBlock.nTimeDisconnect > nTimeoutIfRequestedNow) {
5805 LogPrint("net", "Reducing block download timeout for peer=%d block=%s, orig=%d new=%d\n", pto->id, queuedBlock.hash.ToString(), queuedBlock.nTimeDisconnect, nTimeoutIfRequestedNow);
5806 queuedBlock.nTimeDisconnect = nTimeoutIfRequestedNow;
5807 }
5808 if (queuedBlock.nTimeDisconnect < nNow) {
5809 LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.hash.ToString(), pto->id);
5810 pto->fDisconnect = true;
5811 }
91613034 5812 }
f59d8f0b 5813
0a61b0df 5814 //
f59d8f0b 5815 // Message: getdata (blocks)
0a61b0df 5816 //
5817 vector<CInv> vGetData;
00dcaf4b 5818 if (!pto->fDisconnect && !pto->fClient && (fFetch || !IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
341735eb
PW
5819 vector<CBlockIndex*> vToDownload;
5820 NodeId staller = -1;
5821 FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller);
5822 BOOST_FOREACH(CBlockIndex *pindex, vToDownload) {
5823 vGetData.push_back(CInv(MSG_BLOCK, pindex->GetBlockHash()));
82737933 5824 MarkBlockAsInFlight(pto->GetId(), pindex->GetBlockHash(), consensusParams, pindex);
1af838b3
B
5825 LogPrint("net", "Requesting block %s (%d) peer=%d\n", pindex->GetBlockHash().ToString(),
5826 pindex->nHeight, pto->id);
341735eb
PW
5827 }
5828 if (state.nBlocksInFlight == 0 && staller != -1) {
1bcee67e 5829 if (State(staller)->nStallingSince == 0) {
341735eb 5830 State(staller)->nStallingSince = nNow;
1bcee67e
B
5831 LogPrint("net", "Stall started peer=%d\n", staller);
5832 }
f59d8f0b
PW
5833 }
5834 }
5835
5836 //
5837 // Message: getdata (non-blocks)
5838 //
5839 while (!pto->fDisconnect && !pto->mapAskFor.empty() && (*pto->mapAskFor.begin()).first <= nNow)
0a61b0df 5840 {
5841 const CInv& inv = (*pto->mapAskFor.begin()).second;
ae8bfd12 5842 if (!AlreadyHave(inv))
0a61b0df 5843 {
3b570559 5844 if (fDebug)
2e36866f 5845 LogPrint("net", "Requesting %s peer=%d\n", inv.ToString(), pto->id);
0a61b0df 5846 vGetData.push_back(inv);
5847 if (vGetData.size() >= 1000)
5848 {
5849 pto->PushMessage("getdata", vGetData);
5850 vGetData.clear();
5851 }
e2190f80
GM
5852 } else {
5853 //If we're not going to ask, don't expect a response.
5854 pto->setAskFor.erase(inv.hash);
0a61b0df 5855 }
5856 pto->mapAskFor.erase(pto->mapAskFor.begin());
5857 }
5858 if (!vGetData.empty())
5859 pto->PushMessage("getdata", vGetData);
5860
5861 }
5862 return true;
5863}
5864
651480c8 5865 std::string CBlockFileInfo::ToString() const {
2c2cc5da 5866 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));
651480c8 5867 }
0a61b0df 5868
5869
5870
3427517d
PW
5871class CMainCleanup
5872{
5873public:
5874 CMainCleanup() {}
5875 ~CMainCleanup() {
5876 // block headers
145d5be8 5877 BlockMap::iterator it1 = mapBlockIndex.begin();
3427517d
PW
5878 for (; it1 != mapBlockIndex.end(); it1++)
5879 delete (*it1).second;
5880 mapBlockIndex.clear();
5881
3427517d 5882 // orphan transactions
3427517d 5883 mapOrphanTransactions.clear();
c74332c6 5884 mapOrphanTransactionsByPrev.clear();
3427517d
PW
5885 }
5886} instance_of_cmaincleanup;
431cce98 5887
431cce98 5888extern "C" const char* getDataDir()
5889{
5890 return GetDataDir().string().c_str();
5891}
5892
This page took 2.159388 seconds and 4 git commands to generate.