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