Raven Core  3.0.0
P2P Digital Currency
validation.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "validation.h"
8 
9 #include "arith_uint256.h"
10 #include "chain.h"
11 #include "chainparams.h"
12 #include "checkpoints.h"
13 #include "checkqueue.h"
14 #include "consensus/consensus.h"
15 #include "consensus/merkle.h"
16 #include "consensus/tx_verify.h"
17 #include "consensus/validation.h"
18 #include "cuckoocache.h"
19 #include "fs.h"
20 #include "hash.h"
21 #include "init.h"
22 #include "policy/fees.h"
23 #include "policy/policy.h"
24 #include "policy/rbf.h"
25 #include "pow.h"
26 #include "primitives/block.h"
27 #include "primitives/transaction.h"
28 #include "random.h"
29 #include "reverse_iterator.h"
30 #include "script/script.h"
31 #include "script/sigcache.h"
32 #include "script/standard.h"
33 #include "timedata.h"
34 #include "tinyformat.h"
35 #include "txdb.h"
36 #include "txmempool.h"
37 #include "ui_interface.h"
38 #include "undo.h"
39 #include "util.h"
40 #include "utilmoneystr.h"
41 #include "utilstrencodings.h"
42 #include "validationinterface.h"
43 #include "versionbits.h"
44 #include "warnings.h"
45 #include "net.h"
46 
47 #include <atomic>
48 #include <sstream>
49 
50 #include <boost/algorithm/string/replace.hpp>
51 #include <boost/algorithm/string/join.hpp>
52 #include <boost/thread.hpp>
53 #include <script/ismine.h>
54 #include <wallet/wallet.h>
55 
56 #include "assets/assets.h"
57 #include "assets/assetdb.h"
58 #include "base58.h"
59 
60 #if defined(NDEBUG)
61 # error "Raven cannot be compiled without assertions."
62 #endif
63 
64 #define MICRO 0.000001
65 #define MILLI 0.001
66 
73 
80 std::atomic_bool fImporting(false);
81 std::atomic_bool fReindex(false);
82 bool fMessaging = true;
83 bool fTxIndex = false;
84 bool fAssetIndex = false;
85 bool fAddressIndex = false;
86 bool fTimestampIndex = false;
87 bool fSpentIndex = false;
88 bool fHavePruned = false;
89 bool fPruneMode = false;
90 bool fIsBareMultisigStd = DEFAULT_PERMIT_BAREMULTISIG;
91 bool fRequireStandard = true;
92 bool fCheckBlockIndex = false;
93 bool fCheckpointsEnabled = DEFAULT_CHECKPOINTS_ENABLED;
94 size_t nCoinCacheUsage = 5000 * 300;
95 uint64_t nPruneTarget = 0;
96 int64_t nMaxTipAge = DEFAULT_MAX_TIP_AGE;
97 bool fEnableReplacement = DEFAULT_ENABLE_REPLACEMENT;
98 
99 bool fUnitTest = false;
100 
103 
104 CFeeRate minRelayTxFee = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE);
105 CAmount maxTxFee = DEFAULT_TRANSACTION_MAXFEE;
106 
107 CFeeRate minRelayTxFeeV2 = CFeeRate(DEFAULT_MIN_RELAY_TX_FEE_V2);
108 
110 CTxMemPool mempool(&feeEstimator);
111 
112 static void CheckBlockIndex(const Consensus::Params& consensusParams);
113 
116 
117 const std::string strMessageMagic = "Raven Signed Message:\n";
118 
119 // Internal stuff
120 namespace {
121 
122  struct CBlockIndexWorkComparator
123  {
124  bool operator()(const CBlockIndex *pa, const CBlockIndex *pb) const {
125  // First sort by most total work, ...
126  if (pa->nChainWork > pb->nChainWork) return false;
127  if (pa->nChainWork < pb->nChainWork) return true;
128 
129  // ... then by earliest time received, ...
130  if (pa->nSequenceId < pb->nSequenceId) return false;
131  if (pa->nSequenceId > pb->nSequenceId) return true;
132 
133  // Use pointer address as tie breaker (should only happen with blocks
134  // loaded from disk, as those all have id 0).
135  if (pa < pb) return false;
136  if (pa > pb) return true;
137 
138  // Identical blocks.
139  return false;
140  }
141  };
142 
143  CBlockIndex *pindexBestInvalid;
144 
150  std::set<CBlockIndex*, CBlockIndexWorkComparator> setBlockIndexCandidates;
154  std::multimap<CBlockIndex*, CBlockIndex*> mapBlocksUnlinked;
155 
156  CCriticalSection cs_LastBlockFile;
157  std::vector<CBlockFileInfo> vinfoBlockFile;
158  int nLastBlockFile = 0;
163  bool fCheckForPruning = false;
164 
169  CCriticalSection cs_nBlockSequenceId;
171  int32_t nBlockSequenceId = 1;
173  int32_t nBlockReverseSequenceId = -1;
175  arith_uint256 nLastPreciousChainwork = 0;
176 
195  std::set<CBlockIndex*> g_failed_blocks;
196 
198  std::set<CBlockIndex*> setDirtyBlockIndex;
199 
201  std::set<int> setDirtyFileInfo;
202 } // anon namespace
203 
205 {
206  // Find the first block the caller has in the main chain
207  for (const uint256& hash : locator.vHave) {
208  BlockMap::iterator mi = mapBlockIndex.find(hash);
209  if (mi != mapBlockIndex.end())
210  {
211  CBlockIndex* pindex = (*mi).second;
212  if (chain.Contains(pindex))
213  return pindex;
214  if (pindex->GetAncestor(chain.Height()) == chain.Tip()) {
215  return chain.Tip();
216  }
217  }
218  }
219  return chain.Genesis();
220 }
221 
225 
226 CAssetsDB *passetsdb = nullptr;
234 
240 
246 };
247 
248 // See definition for documentation
249 static bool FlushStateToDisk(const CChainParams& chainParams, CValidationState &state, FlushStateMode mode, int nManualPruneHeight=0);
250 static void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight);
251 static void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight);
252 bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks = nullptr);
253 static FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false);
254 
255 bool CheckFinalTx(const CTransaction &tx, int flags)
256 {
257  AssertLockHeld(cs_main);
258 
259  // By convention a negative value for flags indicates that the
260  // current network-enforced consensus rules should be used. In
261  // a future soft-fork scenario that would mean checking which
262  // rules would be enforced for the next block and setting the
263  // appropriate flags. At the present time no soft-forks are
264  // scheduled, so no flags are set.
265  flags = std::max(flags, 0);
266 
267  // CheckFinalTx() uses chainActive.Height()+1 to evaluate
268  // nLockTime because when IsFinalTx() is called within
269  // CBlock::AcceptBlock(), the height of the block *being*
270  // evaluated is what is used. Thus if we want to know if a
271  // transaction can be part of the *next* block, we need to call
272  // IsFinalTx() with one more than chainActive.Height().
273  const int nBlockHeight = chainActive.Height() + 1;
274 
275  // BIP113 requires that time-locked transactions have nLockTime set to
276  // less than the median time of the previous block they're contained in.
277  // When the next block is created its previous block will be the current
278  // chain tip, so we use that to calculate the median time passed to
279  // IsFinalTx() if LOCKTIME_MEDIAN_TIME_PAST is set.
280  const int64_t nBlockTime = ((flags & LOCKTIME_MEDIAN_TIME_PAST) && chainActive.Tip())
281  ? chainActive.Tip()->GetMedianTimePast()
282  : GetAdjustedTime();
283 
284  return IsFinalTx(tx, nBlockHeight, nBlockTime);
285 }
286 
288 {
289  AssertLockHeld(cs_main);
290  assert(lp);
291  // If there are relative lock times then the maxInputBlock will be set
292  // If there are no relative lock times, the LockPoints don't depend on the chain
293  if (lp->maxInputBlock) {
294  // Check whether chainActive is an extension of the block at which the LockPoints
295  // calculation was valid. If not LockPoints are no longer valid
296  if (!chainActive.Contains(lp->maxInputBlock)) {
297  return false;
298  }
299  }
300 
301  // LockPoints still valid
302  return true;
303 }
304 
305 bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool useExistingLockPoints)
306 {
307  AssertLockHeld(cs_main);
309 
310  CBlockIndex* tip = chainActive.Tip();
311  assert(tip != nullptr);
312 
313  CBlockIndex index;
314  index.pprev = tip;
315  // CheckSequenceLocks() uses chainActive.Height()+1 to evaluate
316  // height based locks because when SequenceLocks() is called within
317  // ConnectBlock(), the height of the block *being*
318  // evaluated is what is used.
319  // Thus if we want to know if a transaction can be part of the
320  // *next* block, we need to use one more than chainActive.Height()
321  index.nHeight = tip->nHeight + 1;
322 
323  std::pair<int, int64_t> lockPair;
324  if (useExistingLockPoints) {
325  assert(lp);
326  lockPair.first = lp->height;
327  lockPair.second = lp->time;
328  }
329  else {
330  // pcoinsTip contains the UTXO set for chainActive.Tip()
331  CCoinsViewMemPool viewMemPool(pcoinsTip, mempool);
332  std::vector<int> prevheights;
333  prevheights.resize(tx.vin.size());
334  for (size_t txinIndex = 0; txinIndex < tx.vin.size(); txinIndex++) {
335  const CTxIn& txin = tx.vin[txinIndex];
336  Coin coin;
337  if (!viewMemPool.GetCoin(txin.prevout, coin)) {
338  return error("%s: Missing input", __func__);
339  }
340  if (coin.nHeight == MEMPOOL_HEIGHT) {
341  // Assume all mempool transaction confirm in the next block
342  prevheights[txinIndex] = tip->nHeight + 1;
343  } else {
344  prevheights[txinIndex] = coin.nHeight;
345  }
346  }
347  lockPair = CalculateSequenceLocks(tx, flags, &prevheights, index);
348  if (lp) {
349  lp->height = lockPair.first;
350  lp->time = lockPair.second;
351  // Also store the hash of the block with the highest height of
352  // all the blocks which have sequence locked prevouts.
353  // This hash needs to still be on the chain
354  // for these LockPoint calculations to be valid
355  // Note: It is impossible to correctly calculate a maxInputBlock
356  // if any of the sequence locked inputs depend on unconfirmed txs,
357  // except in the special case where the relative lock time/height
358  // is 0, which is equivalent to no sequence lock. Since we assume
359  // input height of tip+1 for mempool txs and test the resulting
360  // lockPair from CalculateSequenceLocks against tip+1. We know
361  // EvaluateSequenceLocks will fail if there was a non-zero sequence
362  // lock on a mempool input, so we can use the return value of
363  // CheckSequenceLocks to indicate the LockPoints validity
364  int maxInputHeight = 0;
365  for (int height : prevheights) {
366  // Can ignore mempool inputs since we'll fail if they had non-zero locks
367  if (height != tip->nHeight+1) {
368  maxInputHeight = std::max(maxInputHeight, height);
369  }
370  }
371  lp->maxInputBlock = tip->GetAncestor(maxInputHeight);
372  }
373  }
374  return EvaluateSequenceLocks(index, lockPair);
375 }
376 
377 // Returns the script flags which should be checked for a given block
378 static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consensus::Params& chainparams);
379 
380 static void LimitMempoolSize(CTxMemPool& pool, size_t limit, unsigned long age) {
381  int expired = pool.Expire(GetTime() - age);
382  if (expired != 0) {
383  LogPrint(BCLog::MEMPOOL, "Expired %i transactions from the memory pool\n", expired);
384  }
385 
386  std::vector<COutPoint> vNoSpendsRemaining;
387  pool.TrimToSize(limit, &vNoSpendsRemaining);
388  for (const COutPoint& removed : vNoSpendsRemaining)
389  pcoinsTip->Uncache(removed);
390 }
391 
393 std::string FormatStateMessage(const CValidationState &state)
394 {
395  return strprintf("%s%s (code %i)",
396  state.GetRejectReason(),
397  state.GetDebugMessage().empty() ? "" : ", "+state.GetDebugMessage(),
398  state.GetRejectCode());
399 }
400 
401 static bool IsCurrentForFeeEstimation()
402 {
403  AssertLockHeld(cs_main);
405  return false;
406  if (chainActive.Tip()->GetBlockTime() < (GetTime() - MAX_FEE_ESTIMATION_TIP_AGE))
407  return false;
408  if (chainActive.Height() < pindexBestHeader->nHeight - 1)
409  return false;
410  return true;
411 }
412 
413 /* Make mempool consistent after a reorg, by re-adding or recursively erasing
414  * disconnected block transactions from the mempool, and also removing any
415  * other transactions from the mempool that are no longer valid given the new
416  * tip/height.
417  *
418  * Note: we assume that disconnectpool only contains transactions that are NOT
419  * confirmed in the current chain nor already in the mempool (otherwise,
420  * in-mempool descendants of such transactions would be removed).
421  *
422  * Passing fAddToMempool=false will skip trying to add the transactions back,
423  * and instead just erase from the mempool as needed.
424  */
425 
426 void UpdateMempoolForReorg(DisconnectedBlockTransactions &disconnectpool, bool fAddToMempool)
427 {
428  AssertLockHeld(cs_main);
429  std::vector<uint256> vHashUpdate;
430  // disconnectpool's insertion_order index sorts the entries from
431  // oldest to newest, but the oldest entry will be the last tx from the
432  // latest mined block that was disconnected.
433  // Iterate disconnectpool in reverse, so that we add transactions
434  // back to the mempool starting with the earliest transaction that had
435  // been previously seen in a block.
436  auto it = disconnectpool.queuedTx.get<insertion_order>().rbegin();
437  while (it != disconnectpool.queuedTx.get<insertion_order>().rend()) {
438  // ignore validation errors in resurrected transactions
439  CValidationState stateDummy;
440  if (!fAddToMempool || (*it)->IsCoinBase() ||
441  !AcceptToMemoryPool(mempool, stateDummy, *it, nullptr /* pfMissingInputs */,
442  nullptr /* plTxnReplaced */, true /* bypass_limits */, 0 /* nAbsurdFee */)) {
443  // If the transaction doesn't make it in to the mempool, remove any
444  // transactions that depend on it (which would now be orphans).
446  } else if (mempool.exists((*it)->GetHash())) {
447  vHashUpdate.push_back((*it)->GetHash());
448  }
449  ++it;
450  }
451  disconnectpool.queuedTx.clear();
452  // AcceptToMemoryPool/addUnchecked all assume that new mempool entries have
453  // no in-mempool children, which is generally not true when adding
454  // previously-confirmed transactions back to the mempool.
455  // UpdateTransactionsFromBlock finds descendants of any transactions in
456  // the disconnectpool that were added back and cleans up the mempool state.
458 
459  // We also need to remove any now-immature transactions
460  mempool.removeForReorg(pcoinsTip, chainActive.Tip()->nHeight + 1, STANDARD_LOCKTIME_VERIFY_FLAGS);
461  // Re-limit mempool size, in case we added any transactions
462  LimitMempoolSize(mempool, gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
463 }
464 
465 // Used to avoid mempool polluting consensus critical paths if CCoinsViewMempool
466 // were somehow broken and returning the wrong scriptPubKeys
467 static bool CheckInputsFromMempoolAndCache(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &view, CTxMemPool& pool,
468  unsigned int flags, bool cacheSigStore, PrecomputedTransactionData& txdata) {
469  AssertLockHeld(cs_main);
470 
471  // pool.cs should be locked already, but go ahead and re-take the lock here
472  // to enforce that mempool doesn't change between when we check the view
473  // and when we actually call through to CheckInputs
474  LOCK(pool.cs);
475 
476  assert(!tx.IsCoinBase());
477  for (const CTxIn& txin : tx.vin) {
478  const Coin& coin = view.AccessCoin(txin.prevout);
479 
480  // At this point we haven't actually checked if the coins are all
481  // available (or shouldn't assume we have, since CheckInputs does).
482  // So we just return failure if the inputs are not available here,
483  // and then only have to check equivalence for available inputs.
484  if (coin.IsSpent()) return false;
485 
486  const CTransactionRef& txFrom = pool.get(txin.prevout.hash);
487  if (txFrom) {
488  assert(txFrom->GetHash() == txin.prevout.hash);
489  assert(txFrom->vout.size() > txin.prevout.n);
490  assert(txFrom->vout[txin.prevout.n] == coin.out);
491  } else {
492  const Coin& coinFromDisk = pcoinsTip->AccessCoin(txin.prevout);
493  assert(!coinFromDisk.IsSpent());
494  assert(coinFromDisk.out == coin.out);
495  }
496  }
497 
498  return CheckInputs(tx, state, view, true, flags, cacheSigStore, true, txdata);
499 }
500 
501 static bool AcceptToMemoryPoolWorker(const CChainParams& chainparams, CTxMemPool& pool, CValidationState& state, const CTransactionRef& ptx,
502  bool* pfMissingInputs, int64_t nAcceptTime, std::list<CTransactionRef>* plTxnReplaced,
503  bool bypass_limits, const CAmount& nAbsurdFee, std::vector<COutPoint>& coins_to_uncache)
504 {
505  const CTransaction& tx = *ptx;
506  const uint256 hash = tx.GetHash();
507 
509  std::vector<std::pair<std::string, uint256>> vReissueAssets;
510  AssertLockHeld(cs_main);
511  if (pfMissingInputs)
512  *pfMissingInputs = false;
513  if (!CheckTransaction(tx, state))
514  return false; // state filled in by CheckTransaction
515 
516  // Coinbase is only valid in a block, not as a loose transaction
517  if (tx.IsCoinBase())
518  return state.DoS(100, false, REJECT_INVALID, "coinbase");
519 
520  // Reject transactions with witness before segregated witness activates (override with -prematurewitness)
521  bool witnessEnabled = IsWitnessEnabled(chainActive.Tip(), chainparams.GetConsensus());
522  if (!gArgs.GetBoolArg("-prematurewitness", false) && tx.HasWitness() && !witnessEnabled) {
523  return state.DoS(0, false, REJECT_NONSTANDARD, "no-witness-yet", true);
524  }
525 
526  // Rather not work on nonstandard transactions (unless -testnet/-regtest)
527  std::string reason;
528  if (fRequireStandard && !IsStandardTx(tx, reason, witnessEnabled))
529  return state.DoS(0, false, REJECT_NONSTANDARD, reason);
530 
531  // Only accept nLockTime-using transactions that can be mined in the next
532  // block; we don't want our mempool filled up with transactions that can't
533  // be mined yet.
534  if (!CheckFinalTx(tx, STANDARD_LOCKTIME_VERIFY_FLAGS))
535  return state.DoS(0, false, REJECT_NONSTANDARD, "non-final");
536 
537  // is it already in the memory pool?
538  if (pool.exists(hash)) {
539  return state.Invalid(false, REJECT_DUPLICATE, "txn-already-in-mempool");
540  }
541 
542  // Check for conflicts with in-memory transactions
543  std::set<uint256> setConflicts;
544  {
545  LOCK(pool.cs); // protect pool.mapNextTx
546  for (const CTxIn &txin : tx.vin)
547  {
548  auto itConflicting = pool.mapNextTx.find(txin.prevout);
549  if (itConflicting != pool.mapNextTx.end())
550  {
551  const CTransaction *ptxConflicting = itConflicting->second;
552  if (!setConflicts.count(ptxConflicting->GetHash()))
553  {
554  // Allow opt-out of transaction replacement by setting
555  // nSequence > MAX_BIP125_RBF_SEQUENCE (SEQUENCE_FINAL-2) on all inputs.
556  //
557  // SEQUENCE_FINAL-1 is picked to still allow use of nLockTime by
558  // non-replaceable transactions. All inputs rather than just one
559  // is for the sake of multi-party protocols, where we don't
560  // want a single party to be able to disable replacement.
561  //
562  // The opt-out ignores descendants as anyone relying on
563  // first-seen mempool behavior should be checking all
564  // unconfirmed ancestors anyway; doing otherwise is hopelessly
565  // insecure.
566  bool fReplacementOptOut = true;
567  if (fEnableReplacement)
568  {
569  for (const CTxIn &_txin : ptxConflicting->vin)
570  {
571  if (_txin.nSequence <= MAX_BIP125_RBF_SEQUENCE)
572  {
573  fReplacementOptOut = false;
574  break;
575  }
576  }
577  }
578  if (fReplacementOptOut) {
579  return state.Invalid(false, REJECT_DUPLICATE, "txn-mempool-conflict");
580  }
581 
582  setConflicts.insert(ptxConflicting->GetHash());
583  }
584  }
585  }
586  }
587 
588  {
589  CCoinsView dummy;
590  CCoinsViewCache view(&dummy);
591 
592  LockPoints lp;
593  {
594  LOCK(pool.cs);
595  CCoinsViewMemPool viewMemPool(pcoinsTip, pool);
596  view.SetBackend(viewMemPool);
597 
598  // do all inputs exist?
599  for (const CTxIn txin : tx.vin) {
600  if (!pcoinsTip->HaveCoinInCache(txin.prevout)) {
601  coins_to_uncache.push_back(txin.prevout);
602  }
603  if (!view.HaveCoin(txin.prevout)) {
604  // Are inputs missing because we already have the tx?
605  for (size_t out = 0; out < tx.vout.size(); out++) {
606  // Optimistically just do efficient check of cache for outputs
607  if (pcoinsTip->HaveCoinInCache(COutPoint(hash, out))) {
608  return state.Invalid(false, REJECT_DUPLICATE, "txn-already-known");
609  }
610  }
611  // Otherwise assume this might be an orphan tx for which we just haven't seen parents yet
612  if (pfMissingInputs) {
613  *pfMissingInputs = true;
614  }
615  return false; // fMissingInputs and !state.IsInvalid() is used to detect this condition, don't set state.Invalid()
616  }
617  }
618 
619  // Bring the best block into scope
620  view.GetBestBlock();
621 
622  // we have all inputs cached now, so switch back to dummy, so we don't need to keep lock on mempool
623  view.SetBackend(dummy);
624 
625  // Only accept BIP68 sequence locked transactions that can be mined in the next
626  // block; we don't want our mempool filled up with transactions that can't
627  // be mined yet.
628  // Must keep pool.cs for this unless we change CheckSequenceLocks to take a
629  // CoinsViewCache instead of create its own
630  if (!CheckSequenceLocks(tx, STANDARD_LOCKTIME_VERIFY_FLAGS, &lp))
631  return state.DoS(0, false, REJECT_NONSTANDARD, "non-BIP68-final");
632 
633  } // end LOCK(pool.cs)
634 
635  CAmount nFees = 0;
636  if (!Consensus::CheckTxInputs(tx, state, view, GetSpendHeight(view), nFees)) {
637  return error("%s: Consensus::CheckTxInputs: %s, %s", __func__, tx.GetHash().ToString(), FormatStateMessage(state));
638  }
639 
641  if (!AreAssetsDeployed()) {
642  for (auto out : tx.vout) {
643  if (out.scriptPubKey.IsAssetScript())
644  return state.DoS(100, false, REJECT_INVALID, "bad-txns-contained-asset-when-not-active");
645  }
646  }
647 
648  if (AreAssetsDeployed()) {
649  if (!Consensus::CheckTxAssets(tx, state, view, GetCurrentAssetCache(), true, vReissueAssets))
650  return error("%s: Consensus::CheckTxAssets: %s, %s", __func__, tx.GetHash().ToString(),
651  FormatStateMessage(state));
652  }
655  // Check for non-standard pay-to-script-hash in inputs
656  if (fRequireStandard && !AreInputsStandard(tx, view))
657  return state.Invalid(false, REJECT_NONSTANDARD, "bad-txns-nonstandard-inputs");
658 
659  // Check for non-standard witness in P2WSH
660  if (tx.HasWitness() && fRequireStandard && !IsWitnessStandard(tx, view))
661  return state.DoS(0, false, REJECT_NONSTANDARD, "bad-witness-nonstandard", true);
662 
663  int64_t nSigOpsCost = GetTransactionSigOpCost(tx, view, STANDARD_SCRIPT_VERIFY_FLAGS);
664 
665  // nModifiedFees includes any fee deltas from PrioritiseTransaction
666  CAmount nModifiedFees = nFees;
667  pool.ApplyDelta(hash, nModifiedFees);
668 
669  // Keep track of transactions that spend a coinbase, which we re-scan
670  // during reorgs to ensure COINBASE_MATURITY is still met.
671  bool fSpendsCoinbase = false;
672  for (const CTxIn &txin : tx.vin) {
673  const Coin &coin = view.AccessCoin(txin.prevout);
674  if (coin.IsCoinBase()) {
675  fSpendsCoinbase = true;
676  break;
677  }
678  }
679 
680  CTxMemPoolEntry entry(ptx, nFees, nAcceptTime, chainActive.Height(),
681  fSpendsCoinbase, nSigOpsCost, lp);
682  unsigned int nSize = entry.GetTxSize();
683 
684  // Check that the transaction doesn't have an excessive number of
685  // sigops, making it impossible to mine. Since the coinbase transaction
686  // itself can contain sigops MAX_STANDARD_TX_SIGOPS is less than
687  // MAX_BLOCK_SIGOPS; we still consider this an invalid rather than
688  // merely non-standard transaction.
689  if (nSigOpsCost > MAX_STANDARD_TX_SIGOPS_COST)
690  return state.DoS(0, false, REJECT_NONSTANDARD, "bad-txns-too-many-sigops", false,
691  strprintf("%d", nSigOpsCost));
692 
693  CAmount mempoolRejectFee = pool.GetMinFee(gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000).GetFee(nSize);
694  if (!bypass_limits && mempoolRejectFee > 0 && nModifiedFees < mempoolRejectFee) {
695  return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "mempool min fee not met", false, strprintf("%d < %d", nFees, mempoolRejectFee));
696  }
697 
698  // No transactions are allowed below minRelayTxFee except from disconnected blocks
699  if (!bypass_limits && nModifiedFees < (AreMessagingDeployed() ? ::minRelayTxFeeV2.GetFee(nSize) : ::minRelayTxFee.GetFee(nSize))) {
700 
701  LogPrintf("Modifed fees: %u, minrelayfee: %u\n", nModifiedFees, (AreMessagingDeployed() ? ::minRelayTxFeeV2.GetFee(nSize) : ::minRelayTxFee.GetFee(nSize)));
702  return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "min relay fee not met");
703  }
704 
705  if (nAbsurdFee && nFees > nAbsurdFee)
706  return state.Invalid(false,
707  REJECT_HIGHFEE, "absurdly-high-fee",
708  strprintf("%d > %d", nFees, nAbsurdFee));
709 
710  // Calculate in-mempool ancestors, up to a limit.
711  CTxMemPool::setEntries setAncestors;
712  size_t nLimitAncestors = gArgs.GetArg("-limitancestorcount", DEFAULT_ANCESTOR_LIMIT);
713  size_t nLimitAncestorSize = gArgs.GetArg("-limitancestorsize", DEFAULT_ANCESTOR_SIZE_LIMIT)*1000;
714  size_t nLimitDescendants = gArgs.GetArg("-limitdescendantcount", DEFAULT_DESCENDANT_LIMIT);
715  size_t nLimitDescendantSize = gArgs.GetArg("-limitdescendantsize", DEFAULT_DESCENDANT_SIZE_LIMIT)*1000;
716  std::string errString;
717  if (!pool.CalculateMemPoolAncestors(entry, setAncestors, nLimitAncestors, nLimitAncestorSize, nLimitDescendants, nLimitDescendantSize, errString)) {
718  return state.DoS(0, false, REJECT_NONSTANDARD, "too-long-mempool-chain", false, errString);
719  }
720 
721  // A transaction that spends outputs that would be replaced by it is invalid. Now
722  // that we have the set of all ancestors we can detect this
723  // pathological case by making sure setConflicts and setAncestors don't
724  // intersect.
725  for (CTxMemPool::txiter ancestorIt : setAncestors)
726  {
727  const uint256 &hashAncestor = ancestorIt->GetTx().GetHash();
728  if (setConflicts.count(hashAncestor))
729  {
730  return state.DoS(10, false,
731  REJECT_INVALID, "bad-txns-spends-conflicting-tx", false,
732  strprintf("%s spends conflicting transaction %s",
733  hash.ToString(),
734  hashAncestor.ToString()));
735  }
736  }
737 
738  // Check if it's economically rational to mine this transaction rather
739  // than the ones it replaces.
740  CAmount nConflictingFees = 0;
741  size_t nConflictingSize = 0;
742  uint64_t nConflictingCount = 0;
743  CTxMemPool::setEntries allConflicting;
744 
745  // If we don't hold the lock allConflicting might be incomplete; the
746  // subsequent RemoveStaged() and addUnchecked() calls don't guarantee
747  // mempool consistency for us.
748  LOCK(pool.cs);
749  const bool fReplacementTransaction = setConflicts.size();
750  if (fReplacementTransaction)
751  {
752  CFeeRate newFeeRate(nModifiedFees, nSize);
753  std::set<uint256> setConflictsParents;
754  const int maxDescendantsToVisit = 100;
755  CTxMemPool::setEntries setIterConflicting;
756  for (const uint256 &hashConflicting : setConflicts)
757  {
758  CTxMemPool::txiter mi = pool.mapTx.find(hashConflicting);
759  if (mi == pool.mapTx.end())
760  continue;
761 
762  // Save these to avoid repeated lookups
763  setIterConflicting.insert(mi);
764 
765  // Don't allow the replacement to reduce the feerate of the
766  // mempool.
767  //
768  // We usually don't want to accept replacements with lower
769  // feerates than what they replaced as that would lower the
770  // feerate of the next block. Requiring that the feerate always
771  // be increased is also an easy-to-reason about way to prevent
772  // DoS attacks via replacements.
773  //
774  // The mining code doesn't (currently) take children into
775  // account (CPFP) so we only consider the feerates of
776  // transactions being directly replaced, not their indirect
777  // descendants. While that does mean high feerate children are
778  // ignored when deciding whether or not to replace, we do
779  // require the replacement to pay more overall fees too,
780  // mitigating most cases.
781  CFeeRate oldFeeRate(mi->GetModifiedFee(), mi->GetTxSize());
782  if (newFeeRate <= oldFeeRate)
783  {
784  return state.DoS(0, false,
785  REJECT_INSUFFICIENTFEE, "insufficient fee", false,
786  strprintf("rejecting replacement %s; new feerate %s <= old feerate %s",
787  hash.ToString(),
788  newFeeRate.ToString(),
789  oldFeeRate.ToString()));
790  }
791 
792  for (const CTxIn &txin : mi->GetTx().vin)
793  {
794  setConflictsParents.insert(txin.prevout.hash);
795  }
796 
797  nConflictingCount += mi->GetCountWithDescendants();
798  }
799  // This potentially overestimates the number of actual descendants
800  // but we just want to be conservative to avoid doing too much
801  // work.
802  if (nConflictingCount <= maxDescendantsToVisit) {
803  // If not too many to replace, then calculate the set of
804  // transactions that would have to be evicted
805  for (CTxMemPool::txiter it : setIterConflicting) {
806  pool.CalculateDescendants(it, allConflicting);
807  }
808  for (CTxMemPool::txiter it : allConflicting) {
809  nConflictingFees += it->GetModifiedFee();
810  nConflictingSize += it->GetTxSize();
811  }
812  } else {
813  return state.DoS(0, false,
814  REJECT_NONSTANDARD, "too many potential replacements", false,
815  strprintf("rejecting replacement %s; too many potential replacements (%d > %d)\n",
816  hash.ToString(),
817  nConflictingCount,
818  maxDescendantsToVisit));
819  }
820 
821  for (unsigned int j = 0; j < tx.vin.size(); j++)
822  {
823  // We don't want to accept replacements that require low
824  // feerate junk to be mined first. Ideally we'd keep track of
825  // the ancestor feerates and make the decision based on that,
826  // but for now requiring all new inputs to be confirmed works.
827  if (!setConflictsParents.count(tx.vin[j].prevout.hash))
828  {
829  // Rather than check the UTXO set - potentially expensive -
830  // it's cheaper to just check if the new input refers to a
831  // tx that's in the mempool.
832  if (pool.mapTx.find(tx.vin[j].prevout.hash) != pool.mapTx.end())
833  return state.DoS(0, false,
834  REJECT_NONSTANDARD, "replacement-adds-unconfirmed", false,
835  strprintf("replacement %s adds unconfirmed input, idx %d",
836  hash.ToString(), j));
837  }
838  }
839 
840  // The replacement must pay greater fees than the transactions it
841  // replaces - if we did the bandwidth used by those conflicting
842  // transactions would not be paid for.
843  if (nModifiedFees < nConflictingFees)
844  {
845  return state.DoS(0, false,
846  REJECT_INSUFFICIENTFEE, "insufficient fee", false,
847  strprintf("rejecting replacement %s, less fees than conflicting txs; %s < %s",
848  hash.ToString(), FormatMoney(nModifiedFees), FormatMoney(nConflictingFees)));
849  }
850 
851  // Finally in addition to paying more fees than the conflicts the
852  // new transaction must pay for its own bandwidth.
853  CAmount nDeltaFees = nModifiedFees - nConflictingFees;
854  if (nDeltaFees < ::incrementalRelayFee.GetFee(nSize))
855  {
856  return state.DoS(0, false,
857  REJECT_INSUFFICIENTFEE, "insufficient fee", false,
858  strprintf("rejecting replacement %s, not enough additional fees to relay; %s < %s",
859  hash.ToString(),
860  FormatMoney(nDeltaFees),
862  }
863  }
864 
865  unsigned int scriptVerifyFlags = STANDARD_SCRIPT_VERIFY_FLAGS;
866  if (!chainparams.RequireStandard()) {
867  scriptVerifyFlags = gArgs.GetArg("-promiscuousmempoolflags", scriptVerifyFlags);
868  }
869 
870  // Check against previous transactions
871  // This is done last to help prevent CPU exhaustion denial-of-service attacks.
872  PrecomputedTransactionData txdata(tx);
873  if (!CheckInputs(tx, state, view, true, scriptVerifyFlags, true, false, txdata)) {
874  // SCRIPT_VERIFY_CLEANSTACK requires SCRIPT_VERIFY_WITNESS, so we
875  // need to turn both off, and compare against just turning off CLEANSTACK
876  // to see if the failure is specifically due to witness validation.
877  CValidationState stateDummy; // Want reported failures to be from first CheckInputs
878  if (!tx.HasWitness() && CheckInputs(tx, stateDummy, view, true, scriptVerifyFlags & ~(SCRIPT_VERIFY_WITNESS | SCRIPT_VERIFY_CLEANSTACK), true, false, txdata) &&
879  !CheckInputs(tx, stateDummy, view, true, scriptVerifyFlags & ~SCRIPT_VERIFY_CLEANSTACK, true, false, txdata)) {
880  // Only the witness is missing, so the transaction itself may be fine.
881  state.SetCorruptionPossible();
882  }
883  return false; // state filled in by CheckInputs
884  }
885 
886  // Check again against the current block tip's script verification
887  // flags to cache our script execution flags. This is, of course,
888  // useless if the next block has different script flags from the
889  // previous one, but because the cache tracks script flags for us it
890  // will auto-invalidate and we'll just have a few blocks of extra
891  // misses on soft-fork activation.
892  //
893  // This is also useful in case of bugs in the standard flags that cause
894  // transactions to pass as valid when they're actually invalid. For
895  // instance the STRICTENC flag was incorrectly allowing certain
896  // CHECKSIG NOT scripts to pass, even though they were invalid.
897  //
898  // There is a similar check in CreateNewBlock() to prevent creating
899  // invalid blocks (using TestBlockValidity), however allowing such
900  // transactions into the mempool can be exploited as a DoS attack.
901  unsigned int currentBlockScriptVerifyFlags = GetBlockScriptFlags(chainActive.Tip(), Params().GetConsensus());
902  if (!CheckInputsFromMempoolAndCache(tx, state, view, pool, currentBlockScriptVerifyFlags, true, txdata))
903  {
904  // If we're using promiscuousmempoolflags, we may hit this normally
905  // Check if current block has some flags that scriptVerifyFlags
906  // does not before printing an ominous warning
907  if (!(~scriptVerifyFlags & currentBlockScriptVerifyFlags)) {
908  return error("%s: BUG! PLEASE REPORT THIS! ConnectInputs failed against latest-block but not STANDARD flags %s, %s",
909  __func__, hash.ToString(), FormatStateMessage(state));
910  } else {
911  if (!CheckInputs(tx, state, view, true, MANDATORY_SCRIPT_VERIFY_FLAGS, true, false, txdata)) {
912  return error("%s: ConnectInputs failed against MANDATORY but not STANDARD flags due to promiscuous mempool %s, %s",
913  __func__, hash.ToString(), FormatStateMessage(state));
914  } else {
915  LogPrintf("Warning: -promiscuousmempool flags set to not include currently enforced soft forks, this may break mining or otherwise cause instability!\n");
916  }
917  }
918  }
919 
920  // Remove conflicting transactions from the mempool
921  for (const CTxMemPool::txiter it : allConflicting)
922  {
923  LogPrint(BCLog::MEMPOOL, "replacing tx %s with %s for %s RVN additional fees, %d delta bytes\n",
924  it->GetTx().GetHash().ToString(),
925  hash.ToString(),
926  FormatMoney(nModifiedFees - nConflictingFees),
927  (int)nSize - (int)nConflictingSize);
928  if (plTxnReplaced)
929  plTxnReplaced->push_back(it->GetSharedTx());
930  }
931  pool.RemoveStaged(allConflicting, false, MemPoolRemovalReason::REPLACED);
932 
933  // This transaction should only count for fee estimation if:
934  // - it isn't a BIP 125 replacement transaction (may not be widely supported)
935  // - it's not being readded during a reorg which bypasses typical mempool fee limits
936  // - the node is not behind
937  // - the transaction is not dependent on any other transactions in the mempool
938  bool validForFeeEstimation = !fReplacementTransaction && !bypass_limits && IsCurrentForFeeEstimation() && pool.HasNoInputsOf(tx);
939 
940  // Store transaction in memory
941  pool.addUnchecked(hash, entry, setAncestors, validForFeeEstimation);
942 
943  // Add memory address index
944  if (fAddressIndex) {
945  pool.addAddressIndex(entry, view);
946  }
947 
948  // Add memory spent index
949  if (fSpentIndex) {
950  pool.addSpentIndex(entry, view);
951  }
952 
953  // trim mempool and check if tx was trimmed
954  if (!bypass_limits) {
955  LimitMempoolSize(pool, gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000, gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60);
956  if (!pool.exists(hash))
957  return state.DoS(0, false, REJECT_INSUFFICIENTFEE, "mempool full");
958  }
959 
960  for (auto out : vReissueAssets) {
961  mapReissuedAssets.insert(out);
962  mapReissuedTx.insert(std::make_pair(out.second, out.first));
963  }
964 
965  if (AreAssetsDeployed()) {
966  for (auto out : tx.vout) {
967  if (out.scriptPubKey.IsAssetScript()) {
968  CAssetOutputEntry data;
969  if (!GetAssetData(out.scriptPubKey, data))
970  continue;
971  if (data.type == TX_NEW_ASSET && !IsAssetNameAnOwner(data.assetName)) {
972  pool.mapAssetToHash[data.assetName] = hash;
973  pool.mapHashToAsset[hash] = data.assetName;
974  }
975 
976  // Keep track of all restricted assets tx that can become invalid if qualifier or verifiers are changed
979  std::string address = EncodeDestination(data.destination);
980  pool.mapAddressesQualifiersChanged[address].insert(hash);
981  pool.mapHashQualifiersChanged[hash].insert(address);
982 
983  pool.mapAssetVerifierChanged[data.assetName].insert(hash);
984  pool.mapHashVerifierChanged[hash].insert(data.assetName);
985  }
986  }
987  }
988  }
989  }
990 
991  // Keep track of all restricted assets tx that can become invalid if address or assets are marked as frozen
993  for (auto in : tx.vin) {
994  const Coin coin = pcoinsTip->AccessCoin(in.prevout);
995 
996  if (!coin.IsAsset())
997  continue;
998 
999  CAssetOutputEntry data;
1000  if (GetAssetData(coin.out.scriptPubKey, data)) {
1001 
1002  if (IsAssetNameAnRestricted(data.assetName)) {
1003  pool.mapAssetMarkedGlobalFrozen[data.assetName].insert(hash);
1004  pool.mapHashMarkedGlobalFrozen[hash].insert(data.assetName);
1005 
1006  auto pair = std::make_pair(EncodeDestination(data.destination), data.assetName);
1007  pool.mapAddressesMarkedFrozen[pair].insert(hash);
1008  pool.mapHashToAddressMarkedFrozen[hash].insert(pair);
1009  }
1010  }
1011  }
1012  }
1013  }
1014 
1016 
1017  return true;
1018 }
1019 
1021 static bool AcceptToMemoryPoolWithTime(const CChainParams& chainparams, CTxMemPool& pool, CValidationState &state, const CTransactionRef &tx,
1022  bool* pfMissingInputs, int64_t nAcceptTime, std::list<CTransactionRef>* plTxnReplaced,
1023  bool bypass_limits, const CAmount nAbsurdFee)
1024 {
1025  std::vector<COutPoint> coins_to_uncache;
1026  bool res = AcceptToMemoryPoolWorker(chainparams, pool, state, tx, pfMissingInputs, nAcceptTime, plTxnReplaced, bypass_limits, nAbsurdFee, coins_to_uncache);
1027  if (!res) {
1028  for (const COutPoint& hashTx : coins_to_uncache)
1029  pcoinsTip->Uncache(hashTx);
1030  }
1031  // After we've (potentially) uncached entries, ensure our coins cache is still within its size limits
1032  CValidationState stateDummy;
1033  FlushStateToDisk(chainparams, stateDummy, FLUSH_STATE_PERIODIC);
1034  return res;
1035 }
1036 
1038  bool* pfMissingInputs, std::list<CTransactionRef>* plTxnReplaced,
1039  bool bypass_limits, const CAmount nAbsurdFee)
1040 {
1041  const CChainParams& chainparams = Params();
1042  return AcceptToMemoryPoolWithTime(chainparams, pool, state, tx, pfMissingInputs, GetTime(), plTxnReplaced, bypass_limits, nAbsurdFee);
1043 }
1044 
1045 bool GetTimestampIndex(const unsigned int &high, const unsigned int &low, const bool fActiveOnly, std::vector<std::pair<uint256, unsigned int> > &hashes)
1046 {
1047  if (!fTimestampIndex)
1048  return error("Timestamp index not enabled");
1049 
1050  if (!pblocktree->ReadTimestampIndex(high, low, fActiveOnly, hashes))
1051  return error("Unable to get hashes for timestamps");
1052 
1053  return true;
1054 }
1055 
1057 {
1058  if (!fSpentIndex)
1059  return false;
1060 
1061  if (mempool.getSpentIndex(key, value))
1062  return true;
1063 
1064  if (!pblocktree->ReadSpentIndex(key, value))
1065  return false;
1066 
1067  return true;
1068 }
1069 
1070 bool HashOnchainActive(const uint256 &hash)
1071 {
1072  CBlockIndex* pblockindex = mapBlockIndex[hash];
1073 
1074  if (!chainActive.Contains(pblockindex)) {
1075  return false;
1076  }
1077 
1078  return true;
1079 }
1080 
1081 bool GetAddressIndex(uint160 addressHash, int type, std::string assetName,
1082  std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex, int start, int end)
1083 {
1084  if (!fAddressIndex)
1085  return error("address index not enabled");
1086 
1087  if (!pblocktree->ReadAddressIndex(addressHash, type, assetName, addressIndex, start, end))
1088  return error("unable to get txids for address");
1089 
1090  return true;
1091 }
1092 
1093 bool GetAddressIndex(uint160 addressHash, int type,
1094  std::vector<std::pair<CAddressIndexKey, CAmount> > &addressIndex, int start, int end)
1095 {
1096  if (!fAddressIndex)
1097  return error("address index not enabled");
1098 
1099  if (!pblocktree->ReadAddressIndex(addressHash, type, addressIndex, start, end))
1100  return error("unable to get txids for address");
1101 
1102  return true;
1103 }
1104 
1105 bool GetAddressUnspent(uint160 addressHash, int type, std::string assetName,
1106  std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &unspentOutputs)
1107 {
1108  if (!fAddressIndex)
1109  return error("address index not enabled");
1110 
1111  if (!pblocktree->ReadAddressUnspentIndex(addressHash, type, assetName, unspentOutputs))
1112  return error("unable to get txids for address");
1113 
1114  return true;
1115 }
1116 
1117 bool GetAddressUnspent(uint160 addressHash, int type,
1118  std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > &unspentOutputs)
1119 {
1120  if (!fAddressIndex)
1121  return error("address index not enabled");
1122 
1123  if (!pblocktree->ReadAddressUnspentIndex(addressHash, type, unspentOutputs))
1124  return error("unable to get txids for address");
1125 
1126  return true;
1127 }
1128 
1130 bool GetTransaction(const uint256 &hash, CTransactionRef &txOut, const Consensus::Params& consensusParams, uint256 &hashBlock, bool fAllowSlow)
1131 {
1132  CBlockIndex *pindexSlow = nullptr;
1133 
1134  LOCK(cs_main);
1135 
1136  CTransactionRef ptx = mempool.get(hash);
1137  if (ptx)
1138  {
1139  txOut = ptx;
1140  return true;
1141  }
1142 
1143  if (fTxIndex) {
1144  CDiskTxPos postx;
1145  if (pblocktree->ReadTxIndex(hash, postx)) {
1146  CAutoFile file(OpenBlockFile(postx, true), SER_DISK, CLIENT_VERSION);
1147  if (file.IsNull())
1148  return error("%s: OpenBlockFile failed", __func__);
1149  CBlockHeader header;
1150  try {
1151  file >> header;
1152  fseek(file.Get(), postx.nTxOffset, SEEK_CUR);
1153  file >> txOut;
1154  } catch (const std::exception& e) {
1155  return error("%s: Deserialize or I/O error - %s", __func__, e.what());
1156  }
1157  hashBlock = header.GetHash();
1158  if (txOut->GetHash() != hash)
1159  return error("%s: txid mismatch", __func__);
1160  return true;
1161  }
1162 
1163  // transaction not found in index, nothing more can be done
1164  return false;
1165  }
1166 
1167  if (fAllowSlow) { // use coin database to locate block that contains transaction, and scan it
1168  const Coin& coin = AccessByTxid(*pcoinsTip, hash);
1169  if (!coin.IsSpent()) pindexSlow = chainActive[coin.nHeight];
1170  }
1171 
1172  if (pindexSlow) {
1173  CBlock block;
1174  if (ReadBlockFromDisk(block, pindexSlow, consensusParams)) {
1175  for (const auto& tx : block.vtx) {
1176  if (tx->GetHash() == hash) {
1177  txOut = tx;
1178  hashBlock = pindexSlow->GetBlockHash();
1179  return true;
1180  }
1181  }
1182  }
1183  }
1184 
1185  return false;
1186 }
1187 
1188 
1189 
1190 
1191 
1192 
1194 //
1195 // CBlock and CBlockIndex
1196 //
1197 
1198 static bool WriteBlockToDisk(const CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart)
1199 {
1200  // Open history file to append
1201  CAutoFile fileout(OpenBlockFile(pos), SER_DISK, CLIENT_VERSION);
1202  if (fileout.IsNull())
1203  return error("WriteBlockToDisk: OpenBlockFile failed");
1204 
1205  // Write index header
1206  unsigned int nSize = GetSerializeSize(fileout, block);
1207  fileout << FLATDATA(messageStart) << nSize;
1208 
1209  // Write block
1210  long fileOutPos = ftell(fileout.Get());
1211  if (fileOutPos < 0)
1212  return error("WriteBlockToDisk: ftell failed");
1213  pos.nPos = (unsigned int)fileOutPos;
1214  fileout << block;
1215 
1216  return true;
1217 }
1218 
1219 bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, const Consensus::Params& consensusParams)
1220 {
1221  block.SetNull();
1222 
1223  // Open history file to read
1224  CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
1225  if (filein.IsNull())
1226  return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
1227 
1228  // Read block
1229  try {
1230  filein >> block;
1231  }
1232  catch (const std::exception& e) {
1233  return error("%s: Deserialize or I/O error - %s at %s", __func__, e.what(), pos.ToString());
1234  }
1235 
1236  // Check the header
1237  if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
1238  return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
1239 
1240  return true;
1241 }
1242 
1243 bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
1244 {
1245  if (!ReadBlockFromDisk(block, pindex->GetBlockPos(), consensusParams))
1246  return false;
1247  if (block.GetHash() != pindex->GetBlockHash())
1248  return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
1249  pindex->ToString(), pindex->GetBlockPos().ToString());
1250  return true;
1251 }
1252 
1253 CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams)
1254 {
1255  int halvings = nHeight / consensusParams.nSubsidyHalvingInterval;
1256  // Force block reward to zero when right shift is undefined.
1257  if (halvings >= 64)
1258  return 0;
1259 
1260  CAmount nSubsidy = 5000 * COIN;
1261  // Subsidy is cut in half every 2,100,000 blocks which will occur approximately every 4 years.
1262  nSubsidy >>= halvings;
1263  return nSubsidy;
1264 }
1265 
1267 {
1268  // Once this function has returned false, it must remain false.
1269  static std::atomic<bool> latchToFalse{false};
1270  // Optimization: pre-test latch before taking the lock.
1271  if (latchToFalse.load(std::memory_order_relaxed))
1272  return false;
1273 
1274  LOCK(cs_main);
1275  if (latchToFalse.load(std::memory_order_relaxed))
1276  return false;
1277  if (fImporting || fReindex)
1278  {
1279 // LogPrintf("IsInitialBlockDownload (importing or reindex)\n");
1280  return true;
1281  }
1282  if (chainActive.Tip() == nullptr)
1283  {
1284 // LogPrintf("IsInitialBlockDownload (tip is null)");
1285  return true;
1286  }
1287  if (chainActive.Tip()->nChainWork < nMinimumChainWork)
1288  {
1289 // LogPrintf("IsInitialBlockDownload (min chain work)");
1290 // LogPrintf("Work found: %s", chainActive.Tip()->nChainWork.GetHex());
1291 // LogPrintf("Work needed: %s", nMinimumChainWork.GetHex());
1292  return true;
1293  }
1294  if (chainActive.Tip()->GetBlockTime() < (GetTime() - nMaxTipAge))
1295  {
1296 // LogPrintf("%s: (tip age): %d\n", __func__, nMaxTipAge);
1297  return true;
1298  }
1299 // LogPrintf("Leaving InitialBlockDownload (latching to false)\n");
1300  latchToFalse.store(true, std::memory_order_relaxed);
1301  return false;
1302 }
1303 
1305 {
1306  // Once this function has returned false, it must remain false.
1307  static std::atomic<bool> syncLatchToFalse{false};
1308  // Optimization: pre-test latch before taking the lock.
1309  if (syncLatchToFalse.load(std::memory_order_relaxed))
1310  return false;
1311 
1312  LOCK(cs_main);
1313  if (syncLatchToFalse.load(std::memory_order_relaxed))
1314  return false;
1315  if (fImporting || fReindex)
1316  {
1317 // LogPrintf("IsInitialBlockDownload (importing or reindex)\n");
1318  return true;
1319  }
1320  if (chainActive.Tip() == nullptr)
1321  {
1322 // LogPrintf("IsInitialBlockDownload (tip is null)");
1323  return true;
1324  }
1325  if (chainActive.Tip()->nChainWork < nMinimumChainWork)
1326  {
1327 // LogPrintf("IsInitialBlockDownload (min chain work)");
1328 // LogPrintf("Work found: %s", chainActive.Tip()->nChainWork.GetHex());
1329 // LogPrintf("Work needed: %s", nMinimumChainWork.GetHex());
1330  return true;
1331  }
1332  if (chainActive.Tip()->GetBlockTime() < (GetTime() - (60 * 60 * 72))) // 3 Days
1333  {
1334 // LogPrintf("%s: (tip age): %d\n", __func__, nMaxTipAge);
1335  return true;
1336  }
1337 // LogPrintf("Leaving InitialBlockDownload (latching to false)\n");
1338  syncLatchToFalse.store(true, std::memory_order_relaxed);
1339  return false;
1340 }
1341 
1343 
1344 static void AlertNotify(const std::string& strMessage)
1345 {
1347  std::string strCmd = gArgs.GetArg("-alertnotify", "");
1348  if (strCmd.empty()) return;
1349 
1350  // Alert text should be plain ascii coming from a trusted source, but to
1351  // be safe we first strip anything not in safeChars, then add single quotes around
1352  // the whole string before passing it to the shell:
1353  std::string singleQuote("'");
1354  std::string safeStatus = SanitizeString(strMessage);
1355  safeStatus = singleQuote+safeStatus+singleQuote;
1356  boost::replace_all(strCmd, "%s", safeStatus);
1357 
1358  boost::thread t(runCommand, strCmd); // thread runs free
1359 }
1360 
1361 static void CheckForkWarningConditions()
1362 {
1363  AssertLockHeld(cs_main);
1364  // Before we get past initial download, we cannot reliably alert about forks
1365  // (we assume we don't get stuck on a fork before finishing our initial sync)
1366  if (IsInitialBlockDownload())
1367  return;
1368 
1369  // If our best fork is no longer within 72 blocks (+/- 12 hours if no one mines it)
1370  // of our head, drop it
1371  if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
1372  pindexBestForkTip = nullptr;
1373 
1374  if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (GetBlockProof(*chainActive.Tip()) * 6)))
1375  {
1377  {
1378  std::string warning = std::string("'Warning: Large-work fork detected, forking after block ") +
1379  pindexBestForkBase->phashBlock->ToString() + std::string("'");
1380  AlertNotify(warning);
1381  }
1382  if (pindexBestForkTip && pindexBestForkBase)
1383  {
1384  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__,
1386  pindexBestForkTip->nHeight, pindexBestForkTip->phashBlock->ToString());
1387  SetfLargeWorkForkFound(true);
1388  }
1389  else
1390  {
1391  LogPrintf("%s: Warning: Found invalid chain at least ~6 blocks longer than our best chain.\nChain state database corruption likely.\n", __func__);
1393  }
1394  }
1395  else
1396  {
1397  SetfLargeWorkForkFound(false);
1399  }
1400 }
1401 
1402 static void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
1403 {
1404  AssertLockHeld(cs_main);
1405  // If we are on a fork that is sufficiently large, set a warning flag
1406  CBlockIndex* pfork = pindexNewForkTip;
1407  CBlockIndex* plonger = chainActive.Tip();
1408  while (pfork && pfork != plonger)
1409  {
1410  while (plonger && plonger->nHeight > pfork->nHeight)
1411  plonger = plonger->pprev;
1412  if (pfork == plonger)
1413  break;
1414  pfork = pfork->pprev;
1415  }
1416 
1417  // We define a condition where we should warn the user about as a fork of at least 7 blocks
1418  // with a tip within 72 blocks (+/- 12 hours if no one mines it) of ours
1419  // We use 7 blocks rather arbitrarily as it represents just under 10% of sustained network
1420  // hash rate operating on the fork.
1421  // or a chain that is entirely longer than ours and invalid (note that this should be detected by both)
1422  // We define it this way because it allows us to only store the highest fork tip (+ base) which meets
1423  // the 7-block condition and from this always have the most-likely-to-cause-warning fork
1424  if (pfork && (!pindexBestForkTip || pindexNewForkTip->nHeight > pindexBestForkTip->nHeight) &&
1425  pindexNewForkTip->nChainWork - pfork->nChainWork > (GetBlockProof(*pfork) * 7) &&
1426  chainActive.Height() - pindexNewForkTip->nHeight < 72)
1427  {
1428  pindexBestForkTip = pindexNewForkTip;
1429  pindexBestForkBase = pfork;
1430  }
1431 
1432  CheckForkWarningConditions();
1433 }
1434 
1435 void static InvalidChainFound(CBlockIndex* pindexNew)
1436 {
1437  if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
1438  pindexBestInvalid = pindexNew;
1439 
1440  LogPrintf("%s: invalid block=%s height=%d log2_work=%.8g date=%s\n", __func__,
1441  pindexNew->GetBlockHash().ToString(), pindexNew->nHeight,
1442  log(pindexNew->nChainWork.getdouble())/log(2.0), DateTimeStrFormat("%Y-%m-%d %H:%M:%S",
1443  pindexNew->GetBlockTime()));
1444  CBlockIndex *tip = chainActive.Tip();
1445  assert (tip);
1446  LogPrintf("%s: current best=%s height=%d log2_work=%.8g date=%s\n", __func__,
1447  tip->GetBlockHash().ToString(), chainActive.Height(), log(tip->nChainWork.getdouble())/log(2.0),
1448  DateTimeStrFormat("%Y-%m-%d %H:%M:%S", tip->GetBlockTime()));
1449  CheckForkWarningConditions();
1450 }
1451 
1452 void static InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state) {
1453  if (!state.CorruptionPossible()) {
1454  pindex->nStatus |= BLOCK_FAILED_VALID;
1455  g_failed_blocks.insert(pindex);
1456  setDirtyBlockIndex.insert(pindex);
1457  setBlockIndexCandidates.erase(pindex);
1458  InvalidChainFound(pindex);
1459  }
1460 }
1461 
1462 void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight, uint256 blockHash, CAssetsCache* assetCache, std::pair<std::string, CBlockAssetUndo>* undoAssetData)
1463 {
1464  // mark inputs spent
1465  if (!tx.IsCoinBase()) {
1466  txundo.vprevout.reserve(tx.vin.size());
1467  for (const CTxIn &txin : tx.vin) {
1468  txundo.vprevout.emplace_back();
1469  bool is_spent = inputs.SpendCoin(txin.prevout, &txundo.vprevout.back(), assetCache); /* Pass assetCache into function */
1470  assert(is_spent);
1471  }
1472  }
1473  // add outputs
1474  AddCoins(inputs, tx, nHeight, blockHash, false, assetCache, undoAssetData); /* Pass assetCache into function */
1475 }
1476 
1477 void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, int nHeight)
1478 {
1479  CTxUndo txundo;
1480  UpdateCoins(tx, inputs, txundo, nHeight, uint256());
1481 }
1482 
1484  const CScript &scriptSig = ptxTo->vin[nIn].scriptSig;
1485  const CScriptWitness *witness = &ptxTo->vin[nIn].scriptWitness;
1486  return VerifyScript(scriptSig, m_tx_out.scriptPubKey, witness, nFlags, CachingTransactionSignatureChecker(ptxTo, nIn, m_tx_out.nValue, cacheStore, *txdata), &error);
1487 }
1488 
1490 {
1491  LOCK(cs_main);
1492  CBlockIndex* pindexPrev = mapBlockIndex.find(inputs.GetBestBlock())->second;
1493  return pindexPrev->nHeight + 1;
1494 }
1495 
1496 
1497 static CuckooCache::cache<uint256, SignatureCacheHasher> scriptExecutionCache;
1498 static uint256 scriptExecutionCacheNonce(GetRandHash());
1499 
1501  // nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
1502  // setup_bytes creates the minimum possible cache (2 elements).
1503  size_t nMaxCacheSize = std::min(std::max((int64_t)0, gArgs.GetArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) / 2), MAX_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
1504  size_t nElems = scriptExecutionCache.setup_bytes(nMaxCacheSize);
1505  LogPrintf("Using %zu MiB out of %zu/2 requested for script execution cache, able to store %zu elements\n",
1506  (nElems*sizeof(uint256)) >>20, (nMaxCacheSize*2)>>20, nElems);
1507 }
1508 
1523 bool CheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks)
1524 {
1525  if (!tx.IsCoinBase())
1526  {
1527  if (pvChecks)
1528  pvChecks->reserve(tx.vin.size());
1529 
1530  // The first loop above does all the inexpensive checks.
1531  // Only if ALL inputs pass do we perform expensive ECDSA signature checks.
1532  // Helps prevent CPU exhaustion attacks.
1533 
1534  // Skip script verification when connecting blocks under the
1535  // assumevalid block. Assuming the assumevalid block is valid this
1536  // is safe because block merkle hashes are still computed and checked,
1537  // Of course, if an assumed valid block is invalid due to false scriptSigs
1538  // this optimization would allow an invalid chain to be accepted.
1539  if (fScriptChecks) {
1540  // First check if script executions have been cached with the same
1541  // flags. Note that this assumes that the inputs provided are
1542  // correct (ie that the transaction hash which is in tx's prevouts
1543  // properly commits to the scriptPubKey in the inputs view of that
1544  // transaction).
1545  uint256 hashCacheEntry;
1546  // We only use the first 19 bytes of nonce to avoid a second SHA
1547  // round - giving us 19 + 32 + 4 = 55 bytes (+ 8 + 1 = 64)
1548  static_assert(55 - sizeof(flags) - 32 >= 128/8, "Want at least 128 bits of nonce for script execution cache");
1549  CSHA256().Write(scriptExecutionCacheNonce.begin(), 55 - sizeof(flags) - 32).Write(tx.GetWitnessHash().begin(), 32).Write((unsigned char*)&flags, sizeof(flags)).Finalize(hashCacheEntry.begin());
1550  AssertLockHeld(cs_main); //TODO: Remove this requirement by making CuckooCache not require external locks
1551  if (scriptExecutionCache.contains(hashCacheEntry, !cacheFullScriptStore)) {
1552  return true;
1553  }
1554 
1555  for (unsigned int i = 0; i < tx.vin.size(); i++) {
1556  const COutPoint &prevout = tx.vin[i].prevout;
1557  const Coin& coin = inputs.AccessCoin(prevout);
1558  assert(!coin.IsSpent());
1559 
1560  // We very carefully only pass in things to CScriptCheck which
1561  // are clearly committed to by tx' witness hash. This provides
1562  // a sanity check that our caching is not introducing consensus
1563  // failures through additional data in, eg, the coins being
1564  // spent being checked as a part of CScriptCheck.
1565 
1566  // Verify signature
1567  CScriptCheck check(coin.out, tx, i, flags, cacheSigStore, &txdata);
1568  if (pvChecks) {
1569  pvChecks->push_back(CScriptCheck());
1570  check.swap(pvChecks->back());
1571  } else if (!check()) {
1572  if (flags & STANDARD_NOT_MANDATORY_VERIFY_FLAGS) {
1573  // Check whether the failure was caused by a
1574  // non-mandatory script verification check, such as
1575  // non-standard DER encodings or non-null dummy
1576  // arguments; if so, don't trigger DoS protection to
1577  // avoid splitting the network between upgraded and
1578  // non-upgraded nodes.
1579  CScriptCheck check2(coin.out, tx, i,
1580  flags & ~STANDARD_NOT_MANDATORY_VERIFY_FLAGS, cacheSigStore, &txdata);
1581  if (check2())
1582  return state.Invalid(false, REJECT_NONSTANDARD, strprintf("non-mandatory-script-verify-flag (%s)", ScriptErrorString(check.GetScriptError())));
1583  }
1584  // Failures of other flags indicate a transaction that is
1585  // invalid in new blocks, e.g. an invalid P2SH. We DoS ban
1586  // such nodes as they are not following the protocol. That
1587  // said during an upgrade careful thought should be taken
1588  // as to the correct behavior - we may want to continue
1589  // peering with non-upgraded nodes even after soft-fork
1590  // super-majority signaling has occurred.
1591 
1592  return state.DoS(100,false, REJECT_INVALID, strprintf("mandatory-script-verify-flag-failed (%s)", ScriptErrorString(check.GetScriptError())));
1593  }
1594  }
1595 
1596  if (cacheFullScriptStore && !pvChecks) {
1597  // We executed all of the provided scripts, and were told to
1598  // cache the result. Do so now.
1599  scriptExecutionCache.insert(hashCacheEntry);
1600  }
1601  }
1602  }
1603 
1604  return true;
1605 }
1606 
1607 namespace {
1608 
1609 bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
1610 {
1611  // Open history file to append
1612  CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
1613  if (fileout.IsNull())
1614  return error("%s: OpenUndoFile failed", __func__);
1615 
1616  // Write index header
1617  unsigned int nSize = GetSerializeSize(fileout, blockundo);
1618  fileout << FLATDATA(messageStart) << nSize;
1619 
1620  // Write undo data
1621  long fileOutPos = ftell(fileout.Get());
1622  if (fileOutPos < 0)
1623  return error("%s: ftell failed", __func__);
1624  pos.nPos = (unsigned int)fileOutPos;
1625  fileout << blockundo;
1626 
1627  // calculate & write checksum
1628  CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
1629  hasher << hashBlock;
1630  hasher << blockundo;
1631  fileout << hasher.GetHash();
1632 
1633  return true;
1634 }
1635 
1636 bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock)
1637 {
1638  // Open history file to read
1639  CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
1640  if (filein.IsNull())
1641  return error("%s: OpenUndoFile failed", __func__);
1642 
1643  // Read block
1644  uint256 hashChecksum;
1645  CHashVerifier<CAutoFile> verifier(&filein); // We need a CHashVerifier as reserializing may lose data
1646  try {
1647  verifier << hashBlock;
1648  verifier >> blockundo;
1649  filein >> hashChecksum;
1650  }
1651  catch (const std::exception& e) {
1652  return error("%s: Deserialize or I/O error - %s", __func__, e.what());
1653  }
1654 
1655  // Verify checksum
1656  if (hashChecksum != verifier.GetHash())
1657  return error("%s: Checksum mismatch", __func__);
1658 
1659  return true;
1660 }
1661 
1663 bool AbortNode(const std::string& strMessage, const std::string& userMessage="")
1664 {
1665  SetMiscWarning(strMessage);
1666  LogPrintf("*** %s\n", strMessage);
1668  userMessage.empty() ? _("Error: A fatal internal error occurred, see debug.log for details") : userMessage,
1670 
1671  StartShutdown();
1672  return false;
1673 }
1674 
1675 bool AbortNode(CValidationState& state, const std::string& strMessage, const std::string& userMessage="")
1676 {
1677  AbortNode(strMessage, userMessage);
1678  return state.Error(strMessage);
1679 }
1680 
1681 } // namespace
1682 
1684 {
1685  DISCONNECT_OK, // All good.
1686  DISCONNECT_UNCLEAN, // Rolled back, but UTXO set was inconsistent with block.
1687  DISCONNECT_FAILED // Something else went wrong.
1688 };
1689 
1697 int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out, CAssetsCache* assetCache = nullptr)
1698 {
1699  bool fClean = true;
1700 
1702  // This is needed because undo, is going to be cleared and moved when AddCoin is called. We need this for undo assets
1703  Coin tempCoin;
1704  bool fIsAsset = false;
1705  if (undo.IsAsset()) {
1706  fIsAsset = true;
1707  tempCoin = undo;
1708  }
1711  if (view.HaveCoin(out)) fClean = false; // overwriting transaction output
1712 
1713  if (undo.nHeight == 0) {
1714  // Missing undo metadata (height and coinbase). Older versions included this
1715  // information only in undo records for the last spend of a transactions'
1716  // outputs. This implies that it must be present for some other output of the same tx.
1717  const Coin& alternate = AccessByTxid(view, out.hash);
1718  if (!alternate.IsSpent()) {
1719  undo.nHeight = alternate.nHeight;
1720  undo.fCoinBase = alternate.fCoinBase;
1721  } else {
1722  return DISCONNECT_FAILED; // adding output for transaction without known metadata
1723  }
1724  }
1725  // The potential_overwrite parameter to AddCoin is only allowed to be false if we know for
1726  // sure that the coin did not already exist in the cache. As we have queried for that above
1727  // using HaveCoin, we don't need to guess. When fClean is false, a coin already existed and
1728  // it is an overwrite.
1729  view.AddCoin(out, std::move(undo), !fClean);
1730 
1732  if (AreAssetsDeployed()) {
1733  if (assetCache && fIsAsset) {
1734  if (!assetCache->UndoAssetCoin(tempCoin, out))
1735  fClean = false;
1736  }
1737  }
1740  return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
1741 }
1742 
1745 static DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex* pindex, CCoinsViewCache& view, CAssetsCache* assetsCache = nullptr, bool ignoreAddressIndex = false, bool databaseMessaging = true)
1746 {
1747  bool fClean = true;
1748 
1749  CBlockUndo blockUndo;
1750  CDiskBlockPos pos = pindex->GetUndoPos();
1751  if (pos.IsNull()) {
1752  error("DisconnectBlock(): no undo data available");
1753  return DISCONNECT_FAILED;
1754  }
1755  if (!UndoReadFromDisk(blockUndo, pos, pindex->pprev->GetBlockHash())) {
1756  error("DisconnectBlock(): failure reading undo data");
1757  return DISCONNECT_FAILED;
1758  }
1759 
1760  if (blockUndo.vtxundo.size() + 1 != block.vtx.size()) {
1761  error("DisconnectBlock(): block and undo data inconsistent");
1762  return DISCONNECT_FAILED;
1763  }
1764 
1765  std::vector<std::pair<std::string, CBlockAssetUndo> > vUndoData;
1766  if (!passetsdb->ReadBlockUndoAssetData(block.GetHash(), vUndoData)) {
1767  error("DisconnectBlock(): block asset undo data inconsistent");
1768  return DISCONNECT_FAILED;
1769  }
1770 
1771  std::vector<std::pair<CAddressIndexKey, CAmount> > addressIndex;
1772  std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > addressUnspentIndex;
1773  std::vector<std::pair<CSpentIndexKey, CSpentIndexValue> > spentIndex;
1774 
1775  // undo transactions in reverse order
1776  CAssetsCache tempCache(*assetsCache);
1777  for (int i = block.vtx.size() - 1; i >= 0; i--) {
1778  const CTransaction &tx = *(block.vtx[i]);
1779  uint256 hash = tx.GetHash();
1780  bool is_coinbase = tx.IsCoinBase();
1781 
1782  std::vector<int> vAssetTxIndex;
1783  std::vector<int> vNullAssetTxIndex;
1784  if (fAddressIndex) {
1785  for (unsigned int k = tx.vout.size(); k-- > 0;) {
1786  const CTxOut &out = tx.vout[k];
1787 
1788  if (out.scriptPubKey.IsPayToScriptHash()) {
1789  std::vector<unsigned char> hashBytes(out.scriptPubKey.begin()+2, out.scriptPubKey.begin()+22);
1790 
1791  // undo receiving activity
1792  addressIndex.push_back(std::make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, hash, k, false), out.nValue));
1793 
1794  // undo unspent index
1795  addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(2, uint160(hashBytes), hash, k), CAddressUnspentValue()));
1796 
1797  } else if (out.scriptPubKey.IsPayToPublicKeyHash()) {
1798  std::vector<unsigned char> hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23);
1799 
1800  // undo receiving activity
1801  addressIndex.push_back(std::make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, hash, k, false), out.nValue));
1802 
1803  // undo unspent index
1804  addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(1, uint160(hashBytes), hash, k), CAddressUnspentValue()));
1805 
1806  } else if (out.scriptPubKey.IsPayToPublicKey()) {
1807  uint160 hashBytes(Hash160(out.scriptPubKey.begin()+1, out.scriptPubKey.end()-1));
1808  addressIndex.push_back(std::make_pair(CAddressIndexKey(1, hashBytes, pindex->nHeight, i, hash, k, false), out.nValue));
1809  addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(1, hashBytes, hash, k), CAddressUnspentValue()));
1810  } else {
1812  if (AreAssetsDeployed()) {
1813  std::string assetName;
1814  CAmount assetAmount;
1815  uint160 hashBytes;
1816 
1817  if (ParseAssetScript(out.scriptPubKey, hashBytes, assetName, assetAmount)) {
1818 // std::cout << "ConnectBlock(): pushing assets onto addressIndex: " << "1" << ", " << hashBytes.GetHex() << ", " << assetName << ", " << pindex->nHeight
1819 // << ", " << i << ", " << hash.GetHex() << ", " << k << ", " << "true" << ", " << assetAmount << std::endl;
1820 
1821  // undo receiving activity
1822  addressIndex.push_back(std::make_pair(
1823  CAddressIndexKey(1, uint160(hashBytes), assetName, pindex->nHeight, i, hash, k,
1824  false), assetAmount));
1825 
1826  // undo unspent index
1827  addressUnspentIndex.push_back(
1828  std::make_pair(CAddressUnspentKey(1, uint160(hashBytes), assetName, hash, k),
1830  } else {
1831  continue;
1832  }
1833  }
1835  }
1836  }
1837  }
1838 
1839  // Check that all outputs are available and match the outputs in the block itself
1840  // exactly.
1841  int indexOfRestrictedAssetVerifierString = -1;
1842  for (size_t o = 0; o < tx.vout.size(); o++) {
1843  if (!tx.vout[o].scriptPubKey.IsUnspendable()) {
1844  COutPoint out(hash, o);
1845  Coin coin;
1846  bool is_spent = view.SpendCoin(out, &coin, &tempCache); /* Pass assetsCache into the SpendCoin function */
1847  if (!is_spent || tx.vout[o] != coin.out || pindex->nHeight != coin.nHeight || is_coinbase != coin.fCoinBase) {
1848  fClean = false; // transaction output mismatch
1849  }
1850 
1852  if (AreAssetsDeployed()) {
1853  if (assetsCache) {
1854  if (IsScriptTransferAsset(tx.vout[o].scriptPubKey))
1855  vAssetTxIndex.emplace_back(o);
1856  }
1857  }
1859  } else {
1861  if (assetsCache) {
1862  if (tx.vout[o].scriptPubKey.IsNullAsset()) {
1863  if (tx.vout[o].scriptPubKey.IsNullAssetVerifierTxDataScript()) {
1864  indexOfRestrictedAssetVerifierString = o;
1865  } else {
1866  vNullAssetTxIndex.emplace_back(o);
1867  }
1868  }
1869  }
1870  }
1871  }
1872  }
1873 
1875  if (AreAssetsDeployed()) {
1876  if (assetsCache) {
1877  if (tx.IsNewAsset()) {
1878  // Remove the newly created asset
1879  CNewAsset asset;
1880  std::string strAddress;
1881  if (!AssetFromTransaction(tx, asset, strAddress)) {
1882  error("%s : Failed to get asset from transaction. TXID : %s", __func__, tx.GetHash().GetHex());
1883  return DISCONNECT_FAILED;
1884  }
1885  if (assetsCache->ContainsAsset(asset)) {
1886  if (!assetsCache->RemoveNewAsset(asset, strAddress)) {
1887  error("%s : Failed to Remove Asset. Asset Name : %s", __func__, asset.strName);
1888  return DISCONNECT_FAILED;
1889  }
1890  }
1891 
1892  // Get the owner from the transaction and remove it
1893  std::string ownerName;
1894  std::string ownerAddress;
1895  if (!OwnerFromTransaction(tx, ownerName, ownerAddress)) {
1896  error("%s : Failed to get owner from transaction. TXID : %s", __func__, tx.GetHash().GetHex());
1897  return DISCONNECT_FAILED;
1898  }
1899 
1900  if (!assetsCache->RemoveOwnerAsset(ownerName, ownerAddress)) {
1901  error("%s : Failed to Remove Owner from transaction. TXID : %s", __func__, tx.GetHash().GetHex());
1902  return DISCONNECT_FAILED;
1903  }
1904  } else if (tx.IsReissueAsset()) {
1906  std::string strAddress;
1907 
1908  if (!ReissueAssetFromTransaction(tx, reissue, strAddress)) {
1909  error("%s : Failed to get reissue asset from transaction. TXID : %s", __func__,
1910  tx.GetHash().GetHex());
1911  return DISCONNECT_FAILED;
1912  }
1913 
1914  if (assetsCache->ContainsAsset(reissue.strName)) {
1915  if (!assetsCache->RemoveReissueAsset(reissue, strAddress,
1916  COutPoint(tx.GetHash(), tx.vout.size() - 1),
1917  vUndoData)) {
1918  error("%s : Failed to Undo Reissue Asset. Asset Name : %s", __func__, reissue.strName);
1919  return DISCONNECT_FAILED;
1920  }
1921  }
1922  } else if (tx.IsNewUniqueAsset()) {
1923  for (int n = 0; n < (int)tx.vout.size(); n++) {
1924  auto out = tx.vout[n];
1925  CNewAsset asset;
1926  std::string strAddress;
1927 
1928  if (IsScriptNewUniqueAsset(out.scriptPubKey)) {
1929  if (!AssetFromScript(out.scriptPubKey, asset, strAddress)) {
1930  error("%s : Failed to get unique asset from transaction. TXID : %s, vout: %s", __func__,
1931  tx.GetHash().GetHex(), n);
1932  return DISCONNECT_FAILED;
1933  }
1934 
1935  if (assetsCache->ContainsAsset(asset.strName)) {
1936  if (!assetsCache->RemoveNewAsset(asset, strAddress)) {
1937  error("%s : Failed to Undo Unique Asset. Asset Name : %s", __func__, asset.strName);
1938  return DISCONNECT_FAILED;
1939  }
1940  }
1941  }
1942  }
1943  } else if (tx.IsNewMsgChannelAsset()) {
1944  CNewAsset asset;
1945  std::string strAddress;
1946 
1947  if (!MsgChannelAssetFromTransaction(tx, asset, strAddress)) {
1948  error("%s : Failed to get msgchannel asset from transaction. TXID : %s", __func__,
1949  tx.GetHash().GetHex());
1950  return DISCONNECT_FAILED;
1951  }
1952 
1953  if (assetsCache->ContainsAsset(asset.strName)) {
1954  if (!assetsCache->RemoveNewAsset(asset, strAddress)) {
1955  error("%s : Failed to Undo Msg Channel Asset. Asset Name : %s", __func__, asset.strName);
1956  return DISCONNECT_FAILED;
1957  }
1958  }
1959  } else if (tx.IsNewQualifierAsset()) {
1960  CNewAsset asset;
1961  std::string strAddress;
1962 
1963  if (!QualifierAssetFromTransaction(tx, asset, strAddress)) {
1964  error("%s : Failed to get qualifier asset from transaction. TXID : %s", __func__,
1965  tx.GetHash().GetHex());
1966  return DISCONNECT_FAILED;
1967  }
1968 
1969  if (assetsCache->ContainsAsset(asset.strName)) {
1970  if (!assetsCache->RemoveNewAsset(asset, strAddress)) {
1971  error("%s : Failed to Undo Qualifier Asset. Asset Name : %s", __func__, asset.strName);
1972  return DISCONNECT_FAILED;
1973  }
1974  }
1975  } else if (tx.IsNewRestrictedAsset()) {
1976  CNewAsset asset;
1977  std::string strAddress;
1978 
1979  if (!RestrictedAssetFromTransaction(tx, asset, strAddress)) {
1980  error("%s : Failed to get restricted asset from transaction. TXID : %s", __func__,
1981  tx.GetHash().GetHex());
1982  return DISCONNECT_FAILED;
1983  }
1984 
1985  if (assetsCache->ContainsAsset(asset.strName)) {
1986  if (!assetsCache->RemoveNewAsset(asset, strAddress)) {
1987  error("%s : Failed to Undo Restricted Asset. Asset Name : %s", __func__, asset.strName);
1988  return DISCONNECT_FAILED;
1989  }
1990  }
1991 
1992  if (indexOfRestrictedAssetVerifierString < 0) {
1993  error("%s : Failed to find the restricted asset verifier string index from trasaction. TxID : %s", __func__, tx.GetHash().GetHex());
1994  return DISCONNECT_FAILED;
1995  }
1996 
1997  CNullAssetTxVerifierString verifier;
1998  if (!AssetNullVerifierDataFromScript(tx.vout[indexOfRestrictedAssetVerifierString].scriptPubKey, verifier)) {
1999  error("%s : Failed to get the restricted asset verifier string from trasaction. TxID : %s", __func__, tx.GetHash().GetHex());
2000  return DISCONNECT_FAILED;
2001  }
2002 
2003  if (!assetsCache->RemoveRestrictedVerifier(asset.strName, verifier.verifier_string)){
2004  error("%s : Failed to Remove Restricted Verifier from transaction. TXID : %s", __func__, tx.GetHash().GetHex());
2005  return DISCONNECT_FAILED;
2006  }
2007  }
2008 
2009  for (auto index : vAssetTxIndex) {
2011  std::string strAddress;
2012  if (!TransferAssetFromScript(tx.vout[index].scriptPubKey, transfer, strAddress)) {
2013  error("%s : Failed to get transfer asset from transaction. CTxOut : %s", __func__,
2014  tx.vout[index].ToString());
2015  return DISCONNECT_FAILED;
2016  }
2017 
2018  COutPoint out(hash, index);
2019  if (!assetsCache->RemoveTransfer(transfer, strAddress, out)) {
2020  error("%s : Failed to Remove the transfer of an asset. Asset Name : %s, COutPoint : %s",
2021  __func__,
2022  transfer.strName, out.ToString());
2023  return DISCONNECT_FAILED;
2024  }
2025 
2026  // Undo messages
2027  if (AreMessagingDeployed() && fMessaging && databaseMessaging && !transfer.message.empty() &&
2028  (IsAssetNameAnOwner(transfer.strName) || IsAssetNameAnMsgChannel(transfer.strName))) {
2029 
2030  LOCK(cs_messaging);
2031  if (IsChannelSubscribed(transfer.strName)) {
2032  OrphanMessage(COutPoint(hash, index));
2033  }
2034  }
2035  }
2036 
2038  // Because of the strict rules for allowing the null asset tx types into a transaction.
2039  // We know that if these are in a transaction, that they are valid null asset tx, and can be reversed
2040  for (auto index: vNullAssetTxIndex) {
2041  CScript script = tx.vout[index].scriptPubKey;
2042 
2043  if (script.IsNullAssetTxDataScript()) {
2044  CNullAssetTxData data;
2045  std::string address;
2046  if (!AssetNullDataFromScript(script, data, address)) {
2047  error("%s : Failed to get null asset data from transaction. CTxOut : %s", __func__,
2048  tx.vout[index].ToString());
2049  return DISCONNECT_FAILED;
2050  }
2051 
2052  AssetType type;
2053  IsAssetNameValid(data.asset_name, type);
2054 
2055  // Handle adding qualifiers to addresses
2056  if (type == AssetType::QUALIFIER || type == AssetType::SUB_QUALIFIER) {
2057  if (!assetsCache->RemoveQualifierAddress(data.asset_name, address, data.flag ? QualifierType::ADD_QUALIFIER : QualifierType::REMOVE_QUALIFIER)) {
2058  error("%s : Failed to remove qualifier from address, Qualifier : %s, Flag Removing : %d, Address : %s",
2059  __func__, data.asset_name, data.flag, address);
2060  return DISCONNECT_FAILED;
2061  }
2062  // Handle adding restrictions to addresses
2063  } else if (type == AssetType::RESTRICTED) {
2064  if (!assetsCache->RemoveRestrictedAddress(data.asset_name, address, data.flag ? RestrictedType::FREEZE_ADDRESS : RestrictedType::UNFREEZE_ADDRESS)) {
2065  error("%s : Failed to remove restriction from address, Restriction : %s, Flag Removing : %d, Address : %s",
2066  __func__, data.asset_name, data.flag, address);
2067  return DISCONNECT_FAILED;
2068  }
2069  }
2070  } else if (script.IsNullGlobalRestrictionAssetTxDataScript()) {
2071  CNullAssetTxData data;
2072  std::string address;
2073  if (!GlobalAssetNullDataFromScript(script, data)) {
2074  error("%s : Failed to get global null asset data from transaction. CTxOut : %s", __func__,
2075  tx.vout[index].ToString());
2076  return DISCONNECT_FAILED;
2077  }
2078 
2079  if (!assetsCache->RemoveGlobalRestricted(data.asset_name, data.flag ? RestrictedType::GLOBAL_FREEZE : RestrictedType::GLOBAL_UNFREEZE)) {
2080  error("%s : Failed to remove global restriction from cache. Asset Name: %s, Flag Removing %d", __func__, data.asset_name, data.flag);
2081  return DISCONNECT_FAILED;
2082  }
2083  } else if (script.IsNullAssetVerifierTxDataScript()) {
2084  // These are handled in the undo restricted asset issuance, and restricted asset reissuance
2085  continue;
2086  }
2087  }
2088  }
2089  }
2090  }
2093  // restore inputs
2094  if (i > 0) { // not coinbases
2095  CTxUndo &txundo = blockUndo.vtxundo[i-1];
2096  if (txundo.vprevout.size() != tx.vin.size()) {
2097  error("DisconnectBlock(): transaction and undo data inconsistent");
2098  return DISCONNECT_FAILED;
2099  }
2100  for (unsigned int j = tx.vin.size(); j-- > 0;) {
2101  const COutPoint &out = tx.vin[j].prevout;
2102  Coin &undo = txundo.vprevout[j];
2103  int res = ApplyTxInUndo(std::move(undo), view, out, assetsCache); /* Pass assetsCache into ApplyTxInUndo function */
2104  if (res == DISCONNECT_FAILED) return DISCONNECT_FAILED;
2105  fClean = fClean && res != DISCONNECT_UNCLEAN;
2106 
2107  const CTxIn input = tx.vin[j];
2108 
2109  if (fSpentIndex) {
2110  // undo and delete the spent index
2111  spentIndex.push_back(std::make_pair(CSpentIndexKey(input.prevout.hash, input.prevout.n), CSpentIndexValue()));
2112  }
2113 
2114  if (fAddressIndex) {
2115  const CTxOut &prevout = view.AccessCoin(tx.vin[j].prevout).out;
2116  if (prevout.scriptPubKey.IsPayToScriptHash()) {
2117  std::vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22);
2118 
2119  // undo spending activity
2120  addressIndex.push_back(std::make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, hash, j, true), prevout.nValue * -1));
2121 
2122  // restore unspent index
2123  addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(2, uint160(hashBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey, undo.nHeight)));
2124 
2125 
2126  } else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
2127  std::vector<unsigned char> hashBytes(prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23);
2128 
2129  // undo spending activity
2130  addressIndex.push_back(std::make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, hash, j, true), prevout.nValue * -1));
2131 
2132  // restore unspent index
2133  addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(1, uint160(hashBytes), input.prevout.hash, input.prevout.n), CAddressUnspentValue(prevout.nValue, prevout.scriptPubKey, undo.nHeight)));
2134 
2135  } else if (prevout.scriptPubKey.IsPayToPublicKey()) {
2136  uint160 hashBytes(Hash160(prevout.scriptPubKey.begin()+1, prevout.scriptPubKey.end()-1));
2137  addressIndex.push_back(std::make_pair(CAddressIndexKey(1, hashBytes, pindex->nHeight, i, hash, j, false), prevout.nValue));
2138  addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(1, hashBytes, hash, j), CAddressUnspentValue()));
2139  } else {
2141  if (AreAssetsDeployed()) {
2142  std::string assetName;
2143  CAmount assetAmount;
2144  uint160 hashBytes;
2145 
2146  if (ParseAssetScript(prevout.scriptPubKey, hashBytes, assetName, assetAmount)) {
2147 // std::cout << "ConnectBlock(): pushing assets onto addressIndex: " << "1" << ", " << hashBytes.GetHex() << ", " << assetName << ", " << pindex->nHeight
2148 // << ", " << i << ", " << hash.GetHex() << ", " << j << ", " << "true" << ", " << assetAmount * -1 << std::endl;
2149 
2150  // undo spending activity
2151  addressIndex.push_back(std::make_pair(
2152  CAddressIndexKey(1, uint160(hashBytes), assetName, pindex->nHeight, i, hash, j,
2153  true), assetAmount * -1));
2154 
2155  // restore unspent index
2156  addressUnspentIndex.push_back(std::make_pair(
2157  CAddressUnspentKey(1, uint160(hashBytes), assetName, input.prevout.hash,
2158  input.prevout.n),
2159  CAddressUnspentValue(assetAmount, prevout.scriptPubKey, undo.nHeight)));
2160  } else {
2161  continue;
2162  }
2163  }
2165  }
2166  }
2167  }
2168  // At this point, all of txundo.vprevout should have been moved out.
2169  }
2170  }
2171 
2172 
2173  // move best block pointer to prevout block
2174  view.SetBestBlock(pindex->pprev->GetBlockHash());
2175 
2176  if (!ignoreAddressIndex && fAddressIndex) {
2177  if (!pblocktree->EraseAddressIndex(addressIndex)) {
2178  error("Failed to delete address index");
2179  return DISCONNECT_FAILED;
2180  }
2181  if (!pblocktree->UpdateAddressUnspentIndex(addressUnspentIndex)) {
2182  error("Failed to write address unspent index");
2183  return DISCONNECT_FAILED;
2184  }
2185  }
2186 
2187  return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
2188 }
2189 
2190 void static FlushBlockFile(bool fFinalize = false)
2191 {
2192  LOCK(cs_LastBlockFile);
2193 
2194  CDiskBlockPos posOld(nLastBlockFile, 0);
2195 
2196  FILE *fileOld = OpenBlockFile(posOld);
2197  if (fileOld) {
2198  if (fFinalize)
2199  TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nSize);
2200  FileCommit(fileOld);
2201  fclose(fileOld);
2202  }
2203 
2204  fileOld = OpenUndoFile(posOld);
2205  if (fileOld) {
2206  if (fFinalize)
2207  TruncateFile(fileOld, vinfoBlockFile[nLastBlockFile].nUndoSize);
2208  FileCommit(fileOld);
2209  fclose(fileOld);
2210  }
2211 }
2212 
2213 static bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
2214 
2215 static CCheckQueue<CScriptCheck> scriptcheckqueue(128);
2216 
2218  RenameThread("raven-scriptch");
2219  scriptcheckqueue.Thread();
2220 }
2221 
2222 // Protected by cs_main
2224 
2225 int32_t ComputeBlockVersion(const CBlockIndex* pindexPrev, const Consensus::Params& params)
2226 {
2227  LOCK(cs_main);
2228  int32_t nVersion = VERSIONBITS_TOP_BITS;
2229 
2231  if (AreAssetsDeployed())
2232  nVersion = VERSIONBITS_TOP_BITS_ASSETS;
2233 
2234  if (AreMessagingDeployed())
2235  nVersion = VERSIONBITS_TOP_BITS_MESSAGING;
2236 
2238  nVersion = VERSIONBITS_TOP_BITS_RESTRICTED;
2239 
2240  for (int i = 0; i < (int)Consensus::MAX_VERSION_BITS_DEPLOYMENTS; i++) {
2241  ThresholdState state = VersionBitsState(pindexPrev, params, (Consensus::DeploymentPos)i, versionbitscache);
2242  if (state == THRESHOLD_LOCKED_IN || state == THRESHOLD_STARTED) {
2243  nVersion |= VersionBitsMask(params, (Consensus::DeploymentPos)i);
2244  }
2245  }
2246 
2247  return nVersion;
2248 }
2249 
2254 {
2255 private:
2256  int bit;
2257 
2258 public:
2259  explicit WarningBitsConditionChecker(int bitIn) : bit(bitIn) {}
2260 
2261  int64_t BeginTime(const Consensus::Params& params) const override { return 0; }
2262  int64_t EndTime(const Consensus::Params& params) const override { return std::numeric_limits<int64_t>::max(); }
2263  int Period(const Consensus::Params& params) const override { return params.nMinerConfirmationWindow; }
2264  int Threshold(const Consensus::Params& params) const override { return params.nRuleChangeActivationThreshold; }
2265 
2266  bool Condition(const CBlockIndex* pindex, const Consensus::Params& params) const override
2267  {
2268  return ((pindex->nVersion & VERSIONBITS_TOP_MASK) == VERSIONBITS_TOP_BITS) &&
2269  ((pindex->nVersion >> bit) & 1) != 0 &&
2270  ((ComputeBlockVersion(pindex->pprev, params) >> bit) & 1) == 0;
2271  }
2272 };
2273 
2274 // Protected by cs_main
2275 static ThresholdConditionCache warningcache[VERSIONBITS_NUM_BITS];
2276 
2277 static unsigned int GetBlockScriptFlags(const CBlockIndex* pindex, const Consensus::Params& consensusparams) {
2278  AssertLockHeld(cs_main);
2279 
2280  // BIP16 didn't become active until Apr 1 2012
2281  int64_t nBIP16SwitchTime = 1333238400;
2282  bool fStrictPayToScriptHash = (pindex->GetBlockTime() >= nBIP16SwitchTime);
2283 
2284  unsigned int flags = fStrictPayToScriptHash ? SCRIPT_VERIFY_P2SH : SCRIPT_VERIFY_NONE;
2285 
2286  if(consensusparams.nBIP66Enabled) {
2287  // Start enforcing the DERSIG (BIP66) rule
2288  flags |= SCRIPT_VERIFY_DERSIG;
2289  }
2290 
2291  if(consensusparams.nBIP65Enabled) {
2292  // Start enforcing CHECKLOCKTIMEVERIFY (BIP65) rule
2294  }
2295 
2296  if(consensusparams.nCSVEnabled) {
2297  // Start enforcing BIP68 (sequence locks) and BIP112 (CHECKSEQUENCEVERIFY) using versionbits logic.
2299  }
2300 
2301  // Start enforcing WITNESS rules using versionbits logic.
2302  if (IsWitnessEnabled(pindex->pprev, consensusparams)) {
2303  flags |= SCRIPT_VERIFY_WITNESS;
2304  flags |= SCRIPT_VERIFY_NULLDUMMY;
2305  }
2306 
2307  return flags;
2308 }
2309 
2310 
2311 
2312 static int64_t nTimeCheck = 0;
2313 static int64_t nTimeForks = 0;
2314 static int64_t nTimeVerify = 0;
2315 static int64_t nTimeConnect = 0;
2316 static int64_t nTimeIndex = 0;
2317 static int64_t nTimeCallbacks = 0;
2318 static int64_t nTimeTotal = 0;
2319 static int64_t nBlocksTotal = 0;
2320 
2324 static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex,
2325  CCoinsViewCache& view, const CChainParams& chainparams, CAssetsCache* assetsCache = nullptr, bool fJustCheck = false, bool ignoreAddressIndex = false, std::set<CMessage>* messageCache = nullptr)
2326 {
2327 
2328  AssertLockHeld(cs_main);
2329  assert(pindex);
2330  // pindex->phashBlock can be null if called by CreateNewBlock/TestBlockValidity
2331  assert((pindex->phashBlock == nullptr) ||
2332  (*pindex->phashBlock == block.GetHash()));
2333  int64_t nTimeStart = GetTimeMicros();
2334 
2335  // Check it again in case a previous version let a bad block in
2336  if (!CheckBlock(block, state, chainparams.GetConsensus(), !fJustCheck, !fJustCheck)) // Force the check of asset duplicates when connecting the block
2337  return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
2338 
2339  // verify that the view's current state corresponds to the previous block
2340  uint256 hashPrevBlock = pindex->pprev == nullptr ? uint256() : pindex->pprev->GetBlockHash();
2341  assert(hashPrevBlock == view.GetBestBlock());
2342 
2343  // Special case for the genesis block, skipping connection of its transactions
2344  // (its coinbase is unspendable)
2345  if (block.GetHash() == chainparams.GetConsensus().hashGenesisBlock) {
2346  if (!fJustCheck)
2347  view.SetBestBlock(pindex->GetBlockHash());
2348  return true;
2349  }
2350 
2351  nBlocksTotal++;
2352 
2353  bool fScriptChecks = true;
2354  if (!hashAssumeValid.IsNull()) {
2355  // We've been configured with the hash of a block which has been externally verified to have a valid history.
2356  // A suitable default value is included with the software and updated from time to time. Because validity
2357  // relative to a piece of software is an objective fact these defaults can be easily reviewed.
2358  // This setting doesn't force the selection of any particular chain but makes validating some faster by
2359  // effectively caching the result of part of the verification.
2360  BlockMap::const_iterator it = mapBlockIndex.find(hashAssumeValid);
2361  if (it != mapBlockIndex.end()) {
2362  if (it->second->GetAncestor(pindex->nHeight) == pindex &&
2363  pindexBestHeader->GetAncestor(pindex->nHeight) == pindex &&
2364  pindexBestHeader->nChainWork >= nMinimumChainWork) {
2365  // This block is a member of the assumed verified chain and an ancestor of the best header.
2366  // The equivalent time check discourages hash power from extorting the network via DOS attack
2367  // into accepting an invalid block through telling users they must manually set assumevalid.
2368  // Requiring a software change or burying the invalid block, regardless of the setting, makes
2369  // it hard to hide the implication of the demand. This also avoids having release candidates
2370  // that are hardly doing any signature verification at all in testing without having to
2371  // artificially set the default assumed verified block further back.
2372  // The test against nMinimumChainWork prevents the skipping when denied access to any chain at
2373  // least as good as the expected chain.
2374  fScriptChecks = (GetBlockProofEquivalentTime(*pindexBestHeader, *pindex, *pindexBestHeader, chainparams.GetConsensus()) <= 60 * 60 * 24 * 7 * 2);
2375  }
2376  }
2377  }
2378 
2379  int64_t nTime1 = GetTimeMicros(); nTimeCheck += nTime1 - nTimeStart;
2380  LogPrint(BCLog::BENCH, " - Sanity checks: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime1 - nTimeStart), nTimeCheck * MICRO, nTimeCheck * MILLI / nBlocksTotal);
2381 
2382  // Do not allow blocks that contain transactions which 'overwrite' older transactions,
2383  // unless those are already completely spent.
2384  // If such overwrites are allowed, coinbases and transactions depending upon those
2385  // can be duplicated to remove the ability to spend the first instance -- even after
2386  // being sent to another address.
2387  // See BIP30 and http://r6.ca/blog/20120206T005236Z.html for more information.
2388  // This logic is not necessary for memory pool transactions, as AcceptToMemoryPool
2389  // already refuses previously-known transaction ids entirely.
2390  // This rule was originally applied to all blocks with a timestamp after March 15, 2012, 0:00 UTC.
2391  // Now that the whole chain is irreversibly beyond that time it is applied to all blocks except the
2392  // two in the chain that violate it. This prevents exploiting the issue against nodes during their
2393  // initial block download.
2394  // bool fEnforceBIP30 = (!pindex->phashBlock) || // Enforce on CreateNewBlock invocations which don't have a hash.
2395  // !((pindex->nHeight==91842 && pindex->GetBlockHash() == uint256S("0x00000000000a4d0a398161ffc163c503763b1f4360639393e0e4c8e300e0caec")) ||
2396  // (pindex->nHeight==91880 && pindex->GetBlockHash() == uint256S("0x00000000000743f190a18c5577a3c2d2a1f610ae9601ac046a38084ccb7cd721")));
2397 
2398  // Once BIP34 activated it was not possible to create new duplicate coinbases and thus other than starting
2399  // with the 2 existing duplicate coinbase pairs, not possible to create overwriting txs. But by the
2400  // time BIP34 activated, in each of the existing pairs the duplicate coinbase had overwritten the first
2401  // before the first had been spent. Since those coinbases are sufficiently buried its no longer possible to create further
2402  // duplicate transactions descending from the known pairs either.
2403  // If we're on the known chain at height greater than where BIP34 activated, we can save the db accesses needed for the BIP30 check.
2404  // assert(pindex->pprev);
2405  // CBlockIndex *pindexBIP34height = pindex->pprev->GetAncestor(chainparams.GetConsensus().BIP34Height);
2406  // //Only continue to enforce if we're below BIP34 activation height or the block hash at that height doesn't correspond.
2407  // fEnforceBIP30 = fEnforceBIP30 && (!pindexBIP34height || !(pindexBIP34height->GetBlockHash() == chainparams.GetConsensus().BIP34Hash));
2408 
2409  // if (fEnforceBIP30) {
2410  // for (const auto& tx : block.vtx) {
2411  // for (size_t o = 0; o < tx->vout.size(); o++) {
2412  // if (view.HaveCoin(COutPoint(tx->GetHash(), o))) {
2413  // return state.DoS(100, error("ConnectBlock(): tried to overwrite transaction"),
2414  // REJECT_INVALID, "bad-txns-BIP30");
2415  // }
2416  // }
2417  // }
2418  // }
2419 
2420  // Start enforcing BIP68 (sequence locks) and BIP112 (CHECKSEQUENCEVERIFY) using versionbits logic.
2421  int nLockTimeFlags = 0;
2422  if(chainparams.CSVEnabled()) {
2423  nLockTimeFlags |= LOCKTIME_VERIFY_SEQUENCE;
2424  }
2425 
2426  // Get the script flags for this block
2427  unsigned int flags = GetBlockScriptFlags(pindex, chainparams.GetConsensus());
2428 
2429  int64_t nTime2 = GetTimeMicros(); nTimeForks += nTime2 - nTime1;
2430  LogPrint(BCLog::BENCH, " - Fork checks: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime2 - nTime1), nTimeForks * MICRO, nTimeForks * MILLI / nBlocksTotal);
2431 
2432  CBlockUndo blockundo;
2433  std::vector<std::pair<std::string, CBlockAssetUndo> > vUndoAssetData;
2434 
2435  CCheckQueueControl<CScriptCheck> control(fScriptChecks && nScriptCheckThreads ? &scriptcheckqueue : nullptr);
2436 
2437  std::vector<int> prevheights;
2438  CAmount nFees = 0;
2439  int nInputs = 0;
2440  int64_t nSigOpsCost = 0;
2441  CDiskTxPos pos(pindex->GetBlockPos(), GetSizeOfCompactSize(block.vtx.size()));
2442  std::vector<std::pair<uint256, CDiskTxPos> > vPos;
2443  vPos.reserve(block.vtx.size());
2444  blockundo.vtxundo.reserve(block.vtx.size() - 1);
2445  std::vector<PrecomputedTransactionData> txdata;
2446  txdata.reserve(block.vtx.size()); // Required so that pointers to individual PrecomputedTransactionData don't get invalidated
2447 
2448  std::vector<std::pair<CAddressIndexKey, CAmount> > addressIndex;
2449  std::vector<std::pair<CAddressUnspentKey, CAddressUnspentValue> > addressUnspentIndex;
2450  std::vector<std::pair<CSpentIndexKey, CSpentIndexValue> > spentIndex;
2451 
2452  std::set<CMessage> setMessages;
2453  for (unsigned int i = 0; i < block.vtx.size(); i++)
2454  {
2455  const CTransaction &tx = *(block.vtx[i]);
2456  const uint256 txhash = tx.GetHash();
2457 
2458  nInputs += tx.vin.size();
2459 
2460  if (!tx.IsCoinBase())
2461  {
2462  CAmount txfee = 0;
2463  if (!Consensus::CheckTxInputs(tx, state, view, pindex->nHeight, txfee)) {
2464  return error("%s: Consensus::CheckTxInputs: %s, %s", __func__, tx.GetHash().ToString(), FormatStateMessage(state));
2465  }
2466  nFees += txfee;
2467  if (!MoneyRange(nFees)) {
2468  return state.DoS(100, error("%s: accumulated fee in the block out of range.", __func__),
2469  REJECT_INVALID, "bad-txns-accumulated-fee-outofrange");
2470  }
2471 
2473  if (!AreAssetsDeployed()) {
2474  for (auto out : tx.vout)
2475  if (out.scriptPubKey.IsAssetScript())
2476  return state.DoS(100, error("%s : Received Block with tx that contained an asset when assets wasn't active", __func__), REJECT_INVALID, "bad-txns-assets-not-active");
2477  else if (out.scriptPubKey.IsNullAsset())
2478  return state.DoS(100, error("%s : Received Block with tx that contained an null asset data tx when assets wasn't active", __func__), REJECT_INVALID, "bad-txns-null-data-assets-not-active");
2479  }
2480 
2481  if (AreAssetsDeployed()) {
2482  std::vector<std::pair<std::string, uint256>> vReissueAssets;
2483  if (!Consensus::CheckTxAssets(tx, state, view, assetsCache, false, vReissueAssets, false, &setMessages, block.nTime)) {
2484  return error("%s: Consensus::CheckTxAssets: %s, %s", __func__, tx.GetHash().ToString(),
2485  FormatStateMessage(state));
2486  }
2487  }
2488 
2491  // Check that transaction is BIP68 final
2492  // BIP68 lock checks (as opposed to nLockTime checks) must
2493  // be in ConnectBlock because they require the UTXO set
2494  prevheights.resize(tx.vin.size());
2495  for (size_t j = 0; j < tx.vin.size(); j++) {
2496  prevheights[j] = view.AccessCoin(tx.vin[j].prevout).nHeight;
2497  }
2498 
2499  if (!SequenceLocks(tx, nLockTimeFlags, &prevheights, *pindex)) {
2500  return state.DoS(100, error("%s: contains a non-BIP68-final transaction", __func__),
2501  REJECT_INVALID, "bad-txns-nonfinal");
2502  }
2503 
2504  if (fAddressIndex || fSpentIndex)
2505  {
2506  for (size_t j = 0; j < tx.vin.size(); j++) {
2507 
2508  const CTxIn input = tx.vin[j];
2509  const CTxOut &prevout = view.AccessCoin(tx.vin[j].prevout).out;
2510  uint160 hashBytes;
2511  int addressType = 0;
2512  bool isAsset = false;
2513  std::string assetName;
2514  CAmount assetAmount;
2515 
2516  if (prevout.scriptPubKey.IsPayToScriptHash()) {
2517  hashBytes = uint160(std::vector <unsigned char>(prevout.scriptPubKey.begin()+2, prevout.scriptPubKey.begin()+22));
2518  addressType = 2;
2519  } else if (prevout.scriptPubKey.IsPayToPublicKeyHash()) {
2520  hashBytes = uint160(std::vector <unsigned char>(prevout.scriptPubKey.begin()+3, prevout.scriptPubKey.begin()+23));
2521  addressType = 1;
2522  } else if (prevout.scriptPubKey.IsPayToPublicKey()) {
2523  hashBytes = Hash160(prevout.scriptPubKey.begin() + 1, prevout.scriptPubKey.end() - 1);
2524  addressType = 1;
2525  } else {
2527  if (AreAssetsDeployed()) {
2528  hashBytes.SetNull();
2529  addressType = 0;
2530 
2531  if (ParseAssetScript(prevout.scriptPubKey, hashBytes, assetName, assetAmount)) {
2532  addressType = 1;
2533  isAsset = true;
2534  }
2535  }
2537  }
2538 
2539  if (fAddressIndex && addressType > 0) {
2541  if (isAsset) {
2542 // std::cout << "ConnectBlock(): pushing assets onto addressIndex: " << "1" << ", " << hashBytes.GetHex() << ", " << assetName << ", " << pindex->nHeight
2543 // << ", " << i << ", " << txhash.GetHex() << ", " << j << ", " << "true" << ", " << assetAmount * -1 << std::endl;
2544 
2545  // record spending activity
2546  addressIndex.push_back(std::make_pair(CAddressIndexKey(addressType, hashBytes, assetName, pindex->nHeight, i, txhash, j, true), assetAmount * -1));
2547 
2548  // remove address from unspent index
2549  addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(addressType, hashBytes, assetName, input.prevout.hash, input.prevout.n), CAddressUnspentValue()));
2551  } else {
2552  // record spending activity
2553  addressIndex.push_back(std::make_pair(CAddressIndexKey(addressType, hashBytes, pindex->nHeight, i, txhash, j, true), prevout.nValue * -1));
2554 
2555  // remove address from unspent index
2556  addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(addressType, hashBytes, input.prevout.hash, input.prevout.n), CAddressUnspentValue()));
2557  }
2558  }
2561  if (fSpentIndex) {
2562  // add the spent index to determine the txid and input that spent an output
2563  // and to find the amount and address from an input
2564  spentIndex.push_back(std::make_pair(CSpentIndexKey(input.prevout.hash, input.prevout.n), CSpentIndexValue(txhash, j, pindex->nHeight, prevout.nValue, addressType, hashBytes)));
2565  }
2566  }
2567 
2568  }
2569  }
2570 
2571  // GetTransactionSigOpCost counts 3 types of sigops:
2572  // * legacy (always)
2573  // * p2sh (when P2SH enabled in flags and excludes coinbase)
2574  // * witness (when witness enabled in flags and excludes coinbase)
2575  nSigOpsCost += GetTransactionSigOpCost(tx, view, flags);
2576  if (nSigOpsCost > MAX_BLOCK_SIGOPS_COST)
2577  return state.DoS(100, error("ConnectBlock(): too many sigops"),
2578  REJECT_INVALID, "bad-blk-sigops");
2579 
2580  txdata.emplace_back(tx);
2581  if (!tx.IsCoinBase())
2582  {
2583  std::vector<CScriptCheck> vChecks;
2584  bool fCacheResults = fJustCheck; /* Don't cache results if we're actually connecting blocks (still consult the cache, though) */
2585  if (!CheckInputs(tx, state, view, fScriptChecks, flags, fCacheResults, fCacheResults, txdata[i], nScriptCheckThreads ? &vChecks : nullptr))
2586  return error("ConnectBlock(): CheckInputs on %s failed with %s",
2587  tx.GetHash().ToString(), FormatStateMessage(state));
2588  control.Add(vChecks);
2589  }
2590 
2591  if (fAddressIndex) {
2592  for (unsigned int k = 0; k < tx.vout.size(); k++) {
2593  const CTxOut &out = tx.vout[k];
2594 
2595  if (out.scriptPubKey.IsPayToScriptHash()) {
2596  std::vector<unsigned char> hashBytes(out.scriptPubKey.begin()+2, out.scriptPubKey.begin()+22);
2597 
2598  // record receiving activity
2599  addressIndex.push_back(std::make_pair(CAddressIndexKey(2, uint160(hashBytes), pindex->nHeight, i, txhash, k, false), out.nValue));
2600 
2601  // record unspent output
2602  addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(2, uint160(hashBytes), txhash, k), CAddressUnspentValue(out.nValue, out.scriptPubKey, pindex->nHeight)));
2603 
2604  } else if (out.scriptPubKey.IsPayToPublicKeyHash()) {
2605  std::vector<unsigned char> hashBytes(out.scriptPubKey.begin()+3, out.scriptPubKey.begin()+23);
2606 
2607  // record receiving activity
2608  addressIndex.push_back(std::make_pair(CAddressIndexKey(1, uint160(hashBytes), pindex->nHeight, i, txhash, k, false), out.nValue));
2609 
2610  // record unspent output
2611  addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(1, uint160(hashBytes), txhash, k), CAddressUnspentValue(out.nValue, out.scriptPubKey, pindex->nHeight)));
2612 
2613  } else if (out.scriptPubKey.IsPayToPublicKey()) {
2614  uint160 hashBytes(Hash160(out.scriptPubKey.begin() + 1, out.scriptPubKey.end() - 1));
2615  addressIndex.push_back(
2616  std::make_pair(CAddressIndexKey(1, hashBytes, pindex->nHeight, i, txhash, k, false),
2617  out.nValue));
2618  addressUnspentIndex.push_back(std::make_pair(CAddressUnspentKey(1, hashBytes, txhash, k),
2620  pindex->nHeight)));
2621  } else {
2623  if (AreAssetsDeployed()) {
2624  std::string assetName;
2625  CAmount assetAmount;
2626  uint160 hashBytes;
2627 
2628  if (ParseAssetScript(out.scriptPubKey, hashBytes, assetName, assetAmount)) {
2629 // std::cout << "ConnectBlock(): pushing assets onto addressIndex: " << "1" << ", " << hashBytes.GetHex() << ", " << assetName << ", " << pindex->nHeight
2630 // << ", " << i << ", " << txhash.GetHex() << ", " << k << ", " << "true" << ", " << assetAmount << std::endl;
2631 
2632  // record receiving activity
2633  addressIndex.push_back(std::make_pair(
2634  CAddressIndexKey(1, hashBytes, assetName, pindex->nHeight, i, txhash, k, false),
2635  assetAmount));
2636 
2637  // record unspent output
2638  addressUnspentIndex.push_back(
2639  std::make_pair(CAddressUnspentKey(1, hashBytes, assetName, txhash, k),
2640  CAddressUnspentValue(assetAmount, out.scriptPubKey,
2641  pindex->nHeight)));
2642  }
2643  } else {
2644  continue;
2645  }
2647  }
2648  }
2649  }
2650 
2651  CTxUndo undoDummy;
2652  if (i > 0) {
2653  blockundo.vtxundo.push_back(CTxUndo());
2654  }
2656  // Create the basic empty string pair for the undoblock
2657  std::pair<std::string, CBlockAssetUndo> undoPair = std::make_pair("", CBlockAssetUndo());
2658  std::pair<std::string, CBlockAssetUndo>* undoAssetData = &undoPair;
2661  UpdateCoins(tx, view, i == 0 ? undoDummy : blockundo.vtxundo.back(), pindex->nHeight, block.GetHash(), assetsCache, undoAssetData);
2662 
2664  if (!undoAssetData->first.empty()) {
2665  vUndoAssetData.emplace_back(*undoAssetData);
2666  }
2669  vPos.push_back(std::make_pair(tx.GetHash(), pos));
2670  pos.nTxOffset += ::GetSerializeSize(tx, SER_DISK, CLIENT_VERSION);
2671  }
2672  int64_t nTime3 = GetTimeMicros(); nTimeConnect += nTime3 - nTime2;
2673  LogPrint(BCLog::BENCH, " - Connect %u transactions: %.2fms (%.3fms/tx, %.3fms/txin) [%.2fs (%.2fms/blk)]\n", (unsigned)block.vtx.size(), MILLI * (nTime3 - nTime2), MILLI * (nTime3 - nTime2) / block.vtx.size(), nInputs <= 1 ? 0 : MILLI * (nTime3 - nTime2) / (nInputs-1), nTimeConnect * MICRO, nTimeConnect * MILLI / nBlocksTotal);
2674 
2675  CAmount blockReward = nFees + GetBlockSubsidy(pindex->nHeight, chainparams.GetConsensus());
2676  if (block.vtx[0]->GetValueOut() > blockReward)
2677  return state.DoS(100,
2678  error("ConnectBlock(): coinbase pays too much (actual=%d vs limit=%d)",
2679  block.vtx[0]->GetValueOut(), blockReward),
2680  REJECT_INVALID, "bad-cb-amount");
2681 
2682  if (!control.Wait())
2683  return state.DoS(100, error("%s: CheckQueue failed", __func__), REJECT_INVALID, "block-validation-failed");
2684  int64_t nTime4 = GetTimeMicros(); nTimeVerify += nTime4 - nTime2;
2685  LogPrint(BCLog::BENCH, " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs (%.2fms/blk)]\n", nInputs - 1, MILLI * (nTime4 - nTime2), nInputs <= 1 ? 0 : MILLI * (nTime4 - nTime2) / (nInputs-1), nTimeVerify * MICRO, nTimeVerify * MILLI / nBlocksTotal);
2686 
2687  if (fJustCheck)
2688  return true;
2689 
2690  // Write undo information to disk
2691  if (pindex->GetUndoPos().IsNull() || !pindex->IsValid(BLOCK_VALID_SCRIPTS))
2692  {
2693  if (pindex->GetUndoPos().IsNull()) {
2694  CDiskBlockPos _pos;
2695  if (!FindUndoPos(state, pindex->nFile, _pos, ::GetSerializeSize(blockundo, SER_DISK, CLIENT_VERSION) + 40))
2696  return error("ConnectBlock(): FindUndoPos failed");
2697  if (!UndoWriteToDisk(blockundo, _pos, pindex->pprev->GetBlockHash(), chainparams.MessageStart()))
2698  return AbortNode(state, "Failed to write undo data");
2699 
2700  // update nUndoPos in block index
2701  pindex->nUndoPos = _pos.nPos;
2702  pindex->nStatus |= BLOCK_HAVE_UNDO;
2703  }
2704 
2705  if (vUndoAssetData.size()) {
2706  if (!passetsdb->WriteBlockUndoAssetData(block.GetHash(), vUndoAssetData))
2707  return AbortNode(state, "Failed to write asset undo data");
2708  }
2709 
2711  setDirtyBlockIndex.insert(pindex);
2712  }
2713 
2714  if (fTxIndex)
2715  if (!pblocktree->WriteTxIndex(vPos))
2716  return AbortNode(state, "Failed to write transaction index");
2717 
2718  if (!ignoreAddressIndex && fAddressIndex) {
2719  if (!pblocktree->WriteAddressIndex(addressIndex)) {
2720  return AbortNode(state, "Failed to write address index");
2721  }
2722 
2723  if (!pblocktree->UpdateAddressUnspentIndex(addressUnspentIndex)) {
2724  return AbortNode(state, "Failed to write address unspent index");
2725  }
2726  }
2727 
2728  if (!ignoreAddressIndex && fSpentIndex)
2729  if (!pblocktree->UpdateSpentIndex(spentIndex))
2730  return AbortNode(state, "Failed to write transaction index");
2731 
2732  if (!ignoreAddressIndex && fTimestampIndex) {
2733  unsigned int logicalTS = pindex->nTime;
2734  unsigned int prevLogicalTS = 0;
2735 
2736  // retrieve logical timestamp of the previous block
2737  if (pindex->pprev)
2738  if (!pblocktree->ReadTimestampBlockIndex(pindex->pprev->GetBlockHash(), prevLogicalTS))
2739  LogPrintf("%s: Failed to read previous block's logical timestamp\n", __func__);
2740 
2741  if (logicalTS <= prevLogicalTS) {
2742  logicalTS = prevLogicalTS + 1;
2743  LogPrintf("%s: Previous logical timestamp is newer Actual[%d] prevLogical[%d] Logical[%d]\n", __func__, pindex->nTime, prevLogicalTS, logicalTS);
2744  }
2745 
2746  if (!pblocktree->WriteTimestampIndex(CTimestampIndexKey(logicalTS, pindex->GetBlockHash())))
2747  return AbortNode(state, "Failed to write timestamp index");
2748 
2750  return AbortNode(state, "Failed to write blockhash index");
2751  }
2752 
2753  if (AreMessagingDeployed() && fMessaging && setMessages.size()) {
2754  LOCK(cs_messaging);
2755  for (auto message : setMessages) {
2756  int nHeight = 0;
2757  if (pindex)
2758  nHeight = pindex->nHeight;
2759  message.nBlockHeight = nHeight;
2760 
2761  if (message.nExpiredTime == 0 || GetTime() < message.nExpiredTime)
2762  GetMainSignals().NewAssetMessage(message);
2763 
2764  if (IsChannelSubscribed(message.strName)) {
2765  AddMessage(message);
2766  }
2767  }
2768  }
2769 
2770  assert(pindex->phashBlock);
2771  // add this block to the view's block chain
2772  view.SetBestBlock(pindex->GetBlockHash());
2773 
2774  int64_t nTime5 = GetTimeMicros(); nTimeIndex += nTime5 - nTime4;
2775  LogPrint(BCLog::BENCH, " - Index writing: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime5 - nTime4), nTimeIndex * MICRO, nTimeIndex * MILLI / nBlocksTotal);
2776 
2777  int64_t nTime6 = GetTimeMicros(); nTimeCallbacks += nTime6 - nTime5;
2778  LogPrint(BCLog::BENCH, " - Callbacks: %.2fms [%.2fs (%.2fms/blk)]\n", MILLI * (nTime6 - nTime5), nTimeCallbacks * MICRO, nTimeCallbacks * MILLI / nBlocksTotal);
2779 
2780  return true;
2781 }
2782 
2789 bool static FlushStateToDisk(const CChainParams& chainparams, CValidationState &state, FlushStateMode mode, int nManualPruneHeight) {
2790  int64_t nMempoolUsage = mempool.DynamicMemoryUsage();
2791  LOCK(cs_main);
2792  static int64_t nLastWrite = 0;
2793  static int64_t nLastFlush = 0;
2794  static int64_t nLastSetChain = 0;
2795  std::set<int> setFilesToPrune;
2796  bool fFlushForPrune = false;
2797  bool fDoFullFlush = false;
2798  int64_t nNow = 0;
2799 
2800  try {
2801  {
2802  LOCK(cs_LastBlockFile);
2803  if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
2804  if (nManualPruneHeight > 0) {
2805  FindFilesToPruneManual(setFilesToPrune, nManualPruneHeight);
2806  } else {
2807  FindFilesToPrune(setFilesToPrune, chainparams.PruneAfterHeight());
2808  fCheckForPruning = false;
2809  }
2810  if (!setFilesToPrune.empty()) {
2811  fFlushForPrune = true;
2812  if (!fHavePruned) {
2813  pblocktree->WriteFlag("prunedblockfiles", true);
2814  fHavePruned = true;
2815  }
2816  }
2817  }
2818  nNow = GetTimeMicros();
2819  // Avoid writing/flushing immediately after startup.
2820  if (nLastWrite == 0) {
2821  nLastWrite = nNow;
2822  }
2823  if (nLastFlush == 0) {
2824  nLastFlush = nNow;
2825  }
2826  if (nLastSetChain == 0) {
2827  nLastSetChain = nNow;
2828  }
2829 
2830  // Get the size of the memory used by the asset cache.
2831  int64_t assetDynamicSize = 0;
2832  int64_t assetDirtyCacheSize = 0;
2833  size_t assetMapAmountSize = 0;
2834  if (AreAssetsDeployed()) {
2835  auto currentActiveAssetCache = GetCurrentAssetCache();
2836  if (currentActiveAssetCache) {
2837  assetDynamicSize = currentActiveAssetCache->DynamicMemoryUsage();
2838  assetDirtyCacheSize = currentActiveAssetCache->GetCacheSizeV2();
2839  assetMapAmountSize = currentActiveAssetCache->mapAssetsAddressAmount.size();
2840  }
2841  }
2842 
2843  int messageCacheSize = 0;
2844 
2845  if (fMessaging) {
2846  messageCacheSize = GetMessageDirtyCacheSize();
2847  }
2848 
2849  int64_t nMempoolSizeMax = gArgs.GetArg("-maxmempool", DEFAULT_MAX_MEMPOOL_SIZE) * 1000000;
2850  int64_t cacheSize = pcoinsTip->DynamicMemoryUsage() + assetDynamicSize + assetDirtyCacheSize + messageCacheSize;
2851  int64_t nTotalSpace = nCoinCacheUsage + std::max<int64_t>(nMempoolSizeMax - nMempoolUsage, 0);
2852  // The cache is large and we're within 10% and 10 MiB of the limit, but we have time now (not in the middle of a block processing).
2853  bool fCacheLarge = mode == FLUSH_STATE_PERIODIC && cacheSize > std::max((9 * nTotalSpace) / 10, nTotalSpace - MAX_BLOCK_COINSDB_USAGE * 1024 * 1024);
2854  // The cache is over the limit, we have to write now.
2855  bool fCacheCritical = mode == FLUSH_STATE_IF_NEEDED && (cacheSize > nTotalSpace || assetMapAmountSize > 1000000);
2856  // 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.
2857  bool fPeriodicWrite = mode == FLUSH_STATE_PERIODIC && nNow > nLastWrite + (int64_t)DATABASE_WRITE_INTERVAL * 1000000;
2858  // It's been very long since we flushed the cache. Do this infrequently, to optimize cache usage.
2859  bool fPeriodicFlush = mode == FLUSH_STATE_PERIODIC && nNow > nLastFlush + (int64_t)DATABASE_FLUSH_INTERVAL * 1000000;
2860 
2861  // Combine all conditions that result in a full cache flush.
2862  fDoFullFlush = (mode == FLUSH_STATE_ALWAYS) || fCacheLarge || fCacheCritical || fPeriodicFlush || fFlushForPrune;
2863 
2864  if (!fDoFullFlush && IsInitialSyncSpeedUp() && nNow > nLastFlush + (int64_t) DATABASE_FLUSH_INTERVAL_SPEEDY * 1000000) {
2865  LogPrintf("Flushing to database sooner for speedy sync\n");
2866  fDoFullFlush = true;
2867  }
2868 
2869  // Write blocks and block index to disk.
2870  if (fDoFullFlush || fPeriodicWrite) {
2871  // Depend on nMinDiskSpace to ensure we can write block index
2872  if (!CheckDiskSpace(0))
2873  return state.Error("out of disk space");
2874  // First make sure all block and undo data is flushed to disk.
2875  FlushBlockFile();
2876  // Then update all block file information (which may refer to block and undo files).
2877  {
2878  std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
2879  vFiles.reserve(setDirtyFileInfo.size());
2880  for (std::set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
2881  vFiles.push_back(std::make_pair(*it, &vinfoBlockFile[*it]));
2882  setDirtyFileInfo.erase(it++);
2883  }
2884  std::vector<const CBlockIndex*> vBlocks;
2885  vBlocks.reserve(setDirtyBlockIndex.size());
2886  for (std::set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
2887  vBlocks.push_back(*it);
2888  setDirtyBlockIndex.erase(it++);
2889  }
2890  if (!pblocktree->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
2891  return AbortNode(state, "Failed to write to block index database");
2892  }
2893  }
2894  // Finally remove any pruned files
2895  if (fFlushForPrune)
2896  UnlinkPrunedFiles(setFilesToPrune);
2897  nLastWrite = nNow;
2898  }
2899  // Flush best chain related state. This can only be done if the blocks / block index write was also done.
2900  if (fDoFullFlush) {
2901 
2902 
2903  // Typical Coin structures on disk are around 48 bytes in size.
2904  // Pushing a new one to the database can cause it to be written
2905  // twice (once in the log, and once in the tables). This is already
2906  // an overestimation, as most will delete an existing entry or
2907  // overwrite one. Still, use a conservative safety factor of 2.
2908  if (!CheckDiskSpace((48 * 2 * 2 * pcoinsTip->GetCacheSize()) + assetDirtyCacheSize * 2))
2909  return state.Error("out of disk space");
2910 
2911  // Flush the chainstate (which may refer to block index entries).
2912  if (!pcoinsTip->Flush())
2913  return AbortNode(state, "Failed to write to coin database");
2914 
2916  // Flush the assetstate
2917  if (AreAssetsDeployed()) {
2918  // Flush the assetstate
2919  auto currentActiveAssetCache = GetCurrentAssetCache();
2920  if (currentActiveAssetCache) {
2921  if (!currentActiveAssetCache->DumpCacheToDatabase())
2922  return AbortNode(state, "Failed to write to asset database");
2923  }
2924  }
2925 
2926  // Write the reissue mempool data to database
2927  if (passetsdb)
2928  passetsdb->WriteReissuedMempoolState();
2929 
2930  if (fMessaging) {
2931  if (pmessagedb) {
2932  LOCK(cs_messaging);
2933  if (!pmessagedb->Flush())
2934  return AbortNode(state, "Failed to Flush the message database");
2935  }
2936 
2937  if (pmessagechanneldb) {
2938  LOCK(cs_messaging);
2939  if (!pmessagechanneldb->Flush())
2940  return AbortNode(state, "Failed to Flush the message channel database");
2941  }
2942  }
2945  nLastFlush = nNow;
2946  }
2947  }
2948  if (fDoFullFlush || ((mode == FLUSH_STATE_ALWAYS || mode == FLUSH_STATE_PERIODIC) && nNow > nLastSetChain + (int64_t)DATABASE_WRITE_INTERVAL * 1000000)) {
2949  // Update best block in wallet (so we can detect restored wallets).
2950  GetMainSignals().SetBestChain(chainActive.GetLocator());
2951  nLastSetChain = nNow;
2952  }
2953  } catch (const std::runtime_error& e) {
2954  return AbortNode(state, std::string("System error while flushing: ") + e.what());
2955  }
2956  return true;
2957 }
2958 
2960  CValidationState state;
2961  const CChainParams& chainparams = Params();
2962  FlushStateToDisk(chainparams, state, FLUSH_STATE_ALWAYS);
2963 }
2964 
2966  CValidationState state;
2967  fCheckForPruning = true;
2968  const CChainParams& chainparams = Params();
2969  FlushStateToDisk(chainparams, state, FLUSH_STATE_NONE);
2970 }
2971 
2972 static void DoWarning(const std::string& strWarning)
2973 {
2974  static bool fWarned = false;
2975  SetMiscWarning(strWarning);
2976  if (!fWarned) {
2977  AlertNotify(strWarning);
2978  fWarned = true;
2979  }
2980 }
2981 
2983 void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
2984  chainActive.SetTip(pindexNew);
2985 
2986  // New best block
2988 
2989  cvBlockChange.notify_all();
2990 
2991  std::vector<std::string> warningMessages;
2992  if (!IsInitialBlockDownload())
2993  {
2994  int nUpgraded = 0;
2995  const CBlockIndex* pindex = chainActive.Tip();
2996  for (int bit = 0; bit < VERSIONBITS_NUM_BITS; bit++) {
2997  WarningBitsConditionChecker checker(bit);
2998  ThresholdState state = checker.GetStateFor(pindex, chainParams.GetConsensus(), warningcache[bit]);
2999  if (state == THRESHOLD_ACTIVE || state == THRESHOLD_LOCKED_IN) {
3000  const std::string strWarning = strprintf(_("Warning: unknown new rules activated (versionbit %i)"), bit);
3001  if (bit == 28) // DUMMY TEST BIT
3002  continue;
3003  if (state == THRESHOLD_ACTIVE) {
3004  DoWarning(strWarning);
3005  } else {
3006  warningMessages.push_back(strWarning);
3007  }
3008  }
3009  }
3010  // Check the version of the last 100 blocks to see if we need to upgrade:
3011  for (int i = 0; i < 100 && pindex != nullptr; i++)
3012  {
3013  int32_t nExpectedVersion = ComputeBlockVersion(pindex->pprev, chainParams.GetConsensus());
3014  if (pindex->nVersion > nExpectedVersion)
3015  ++nUpgraded;
3016  pindex = pindex->pprev;
3017  }
3018  if (nUpgraded > 0)
3019  warningMessages.push_back(strprintf(_("%d of last 100 blocks have unexpected version"), nUpgraded));
3020  if (nUpgraded > 100/2)
3021  {
3022  std::string strWarning = _("Warning: Unknown block versions being mined! It's possible unknown rules are in effect");
3023  // notify GetWarnings(), called by Qt and the JSON-RPC code to warn the user:
3024  DoWarning(strWarning);
3025  }
3026  }
3027  LogPrintf("%s: new best=%s height=%d version=0x%08x log2_work=%.8g tx=%lu date='%s' progress=%f cache=%.1fMiB(%utxo)", __func__,
3028  chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), chainActive.Tip()->nVersion,
3029  log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
3030  DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
3031  GuessVerificationProgress(chainParams.TxData(), chainActive.Tip()), pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
3032  if (!warningMessages.empty())
3033  LogPrintf(" warning='%s'", boost::algorithm::join(warningMessages, ", "));
3034  LogPrintf("\n");
3035 
3036 }
3037 
3048 bool static DisconnectTip(CValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions *disconnectpool)
3049 {
3050  CBlockIndex *pindexDelete = chainActive.Tip();
3051  assert(pindexDelete);
3052  // Read block from disk.
3053  std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
3054  CBlock& block = *pblock;
3055  if (!ReadBlockFromDisk(block, pindexDelete, chainparams.GetConsensus()))
3056  return AbortNode(state, "Failed to read block");
3057  // Apply the block atomically to the chain state.
3058  int64_t nStart = GetTimeMicros();
3059  {
3060  CCoinsViewCache view(pcoinsTip);
3061  CAssetsCache assetCache;
3062 
3063  assert(view.GetBestBlock() == pindexDelete->GetBlockHash());
3064  if (DisconnectBlock(block, pindexDelete, view, &assetCache) != DISCONNECT_OK)
3065  return error("DisconnectTip(): DisconnectBlock %s failed", pindexDelete->GetBlockHash().ToString());
3066  bool flushed = view.Flush();
3067  assert(flushed);
3068 
3069  bool assetsFlushed = assetCache.Flush();
3070  assert(assetsFlushed);
3071  }
3072  LogPrint(BCLog::BENCH, "- Disconnect block: %.2fms\n", (GetTimeMicros() - nStart) * MILLI);
3073  // Write the chain state to disk, if necessary.
3074  if (!FlushStateToDisk(chainparams, state, FLUSH_STATE_IF_NEEDED))
3075  return false;
3076 
3077  if (disconnectpool) {
3078  // Save transactions to re-add to mempool at end of reorg
3079  for (auto it = block.vtx.rbegin(); it != block.vtx.rend(); ++it) {
3080  disconnectpool->addTransaction(*it);
3081  }
3082  while (disconnectpool->DynamicMemoryUsage() > MAX_DISCONNECTED_TX_POOL_SIZE * 1000) {
3083  // Drop the earliest entry, and remove its children from the mempool.
3084  auto it = disconnectpool->queuedTx.get<insertion_order>().begin();
3086  disconnectpool->removeEntry(it);
3087  }
3088  }
3089 
3090  // Update chainActive and related variables.
3091  UpdateTip(pindexDelete->pprev, chainparams);
3092  // Let wallets know transactions went from 1-confirmed to
3093  // 0-confirmed or conflicted:
3095  return true;
3096 }
3097 
3098 static int64_t nTimeReadFromDisk = 0;
3099 static int64_t nTimeConnectTotal = 0;
3100 static int64_t nTimeFlush = 0;
3101 static int64_t nTimeAssetFlush = 0;
3102 static int64_t nTimeAssetTasks = 0;
3103 static int64_t nTimeChainState = 0;
3104 static int64_t nTimePostConnect = 0;
3105 
3107  CBlockIndex* pindex = nullptr;
3108  std::shared_ptr<const CBlock> pblock;
3109  std::shared_ptr<std::vector<CTransactionRef>> conflictedTxs;
3110  PerBlockConnectTrace() : conflictedTxs(std::make_shared<std::vector<CTransactionRef>>()) {}
3111 };
3129 private:
3130  std::vector<PerBlockConnectTrace> blocksConnected;
3132 
3133 public:
3134  explicit ConnectTrace(CTxMemPool &_pool) : blocksConnected(1), pool(_pool) {
3135  pool.NotifyEntryRemoved.connect(boost::bind(&ConnectTrace::NotifyEntryRemoved, this, _1, _2));
3136  }
3137 
3139  pool.NotifyEntryRemoved.disconnect(boost::bind(&ConnectTrace::NotifyEntryRemoved, this, _1, _2));
3140  }
3141 
3142  void BlockConnected(CBlockIndex* pindex, std::shared_ptr<const CBlock> pblock) {
3143  assert(!blocksConnected.back().pindex);
3144  assert(pindex);
3145  assert(pblock);
3146  blocksConnected.back().pindex = pindex;
3147  blocksConnected.back().pblock = std::move(pblock);
3148  blocksConnected.emplace_back();
3149  }
3150 
3151  std::vector<PerBlockConnectTrace>& GetBlocksConnected() {
3152  // We always keep one extra block at the end of our list because
3153  // blocks are added after all the conflicted transactions have
3154  // been filled in. Thus, the last entry should always be an empty
3155  // one waiting for the transactions from the next block. We pop
3156  // the last entry here to make sure the list we return is sane.
3157  assert(!blocksConnected.back().pindex);
3158  assert(blocksConnected.back().conflictedTxs->empty());
3159  blocksConnected.pop_back();
3160  return blocksConnected;
3161  }
3162 
3164  assert(!blocksConnected.back().pindex);
3165  if (reason == MemPoolRemovalReason::CONFLICT) {
3166  blocksConnected.back().conflictedTxs->emplace_back(std::move(txRemoved));
3167  }
3168  }
3169 };
3170 
3177 bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions &disconnectpool)
3178 {
3179  assert(pindexNew->pprev == chainActive.Tip());
3180  // Read block from disk.
3181  int64_t nTime1 = GetTimeMicros();
3182  std::shared_ptr<const CBlock> pthisBlock;
3183  if (!pblock) {
3184  std::shared_ptr<CBlock> pblockNew = std::make_shared<CBlock>();
3185  if (!ReadBlockFromDisk(*pblockNew, pindexNew, chainparams.GetConsensus()))
3186  return AbortNode(state, "Failed to read block");
3187  pthisBlock = pblockNew;
3188  } else {
3189  pthisBlock = pblock;
3190  }
3191  const CBlock& blockConnecting = *pthisBlock;
3192  // Apply the block atomically to the chain state.
3193  int64_t nTime2 = GetTimeMicros(); nTimeReadFromDisk += nTime2 - nTime1;
3194  int64_t nTime3;
3195  int64_t nTime4;
3196  int64_t nTimeAssetsFlush;
3197  LogPrint(BCLog::BENCH, " - Load block from disk: %.2fms [%.2fs]\n", (nTime2 - nTime1) * MILLI, nTimeReadFromDisk * MICRO);
3198 
3200  // Initialize sets used from removing asset entries from the mempool
3201  ConnectedBlockAssetData assetDataFromBlock;
3204  {
3205  CCoinsViewCache view(pcoinsTip);
3207  // Create the empty asset cache, that will be sent into the connect block
3208  // All new data will be added to the cache, and will be flushed back into passets after a successful
3209  // Connect Block cycle
3210  CAssetsCache assetCache;
3213  int64_t nTimeConnectStart = GetTimeMicros();
3214 
3215  bool rv = ConnectBlock(blockConnecting, state, pindexNew, view, chainparams, &assetCache);
3216  GetMainSignals().BlockChecked(blockConnecting, state);
3217  if (!rv) {
3218  if (state.IsInvalid())
3219  InvalidBlockFound(pindexNew, state);
3220  return error("ConnectTip(): ConnectBlock %s failed", pindexNew->GetBlockHash().ToString());
3221  }
3222  int64_t nTimeConnectDone = GetTimeMicros();
3223  LogPrint(BCLog::BENCH, " - Connect Block only time: %.2fms [%.2fs (%.2fms/blk)]\n", (nTimeConnectDone - nTimeConnectStart) * MILLI, nTimeConnectTotal * MICRO, nTimeConnectTotal * MILLI / nBlocksTotal);
3224 
3225  int64_t nTimeAssetsStart = GetTimeMicros();
3227  // Get the newly created assets, from the connectblock assetCache so we can remove the correct assets from the mempool
3228  assetDataFromBlock = {assetCache.setNewAssetsToAdd, assetCache.setNewRestrictedVerifierToAdd, assetCache.setNewRestrictedAddressToAdd, assetCache.setNewRestrictedGlobalToAdd, assetCache.setNewQualifierAddressToAdd};
3229 
3230  // Remove all tx hashes, that were marked as reissued script from the mapReissuedTx.
3231  // Without this check, you wouldn't be able to reissue for those assets again, as this maps block it
3232  for (auto tx : blockConnecting.vtx) {
3233  uint256 txHash = tx->GetHash();
3234  if (mapReissuedTx.count(txHash)) {
3235  mapReissuedAssets.erase(mapReissuedTx.at(txHash));
3236  mapReissuedTx.erase(txHash);
3237  }
3238  }
3239  int64_t nTimeAssetsEnd = GetTimeMicros(); nTimeAssetTasks += nTimeAssetsEnd - nTimeAssetsStart;
3240  LogPrint(BCLog::BENCH, " - Compute Asset Tasks total: %.2fms [%.2fs (%.2fms/blk)]\n", (nTimeAssetsEnd - nTimeAssetsStart) * MILLI, nTimeAssetsEnd * MICRO, nTimeAssetsEnd * MILLI / nBlocksTotal);
3243  nTime3 = GetTimeMicros(); nTimeConnectTotal += nTime3 - nTime2;
3244  LogPrint(BCLog::BENCH, " - Connect total: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime3 - nTime2) * MILLI, nTimeConnectTotal * MICRO, nTimeConnectTotal * MILLI / nBlocksTotal);
3245  bool flushed = view.Flush();
3246  assert(flushed);
3247  nTime4 = GetTimeMicros(); nTimeFlush += nTime4 - nTime3;
3248  LogPrint(BCLog::BENCH, " - Flush RVN: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime4 - nTime3) * MILLI, nTimeFlush * MICRO, nTimeFlush * MILLI / nBlocksTotal);
3249 
3251  nTimeAssetsFlush = GetTimeMicros();
3252  bool assetFlushed = assetCache.Flush();
3253  assert(assetFlushed);
3254  int64_t nTimeAssetFlushFinished = GetTimeMicros(); nTimeAssetFlush += nTimeAssetFlushFinished - nTimeAssetsFlush;
3255  LogPrint(BCLog::BENCH, " - Flush Assets: %.2fms [%.2fs (%.2fms/blk)]\n", (nTimeAssetFlushFinished - nTimeAssetsFlush) * MILLI, nTimeAssetFlush * MICRO, nTimeAssetFlush * MILLI / nBlocksTotal);
3257  }
3258 
3259  // Write the chain state to disk, if necessary.
3260  if (!FlushStateToDisk(chainparams, state, FLUSH_STATE_IF_NEEDED))
3261  return false;
3262  int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
3263  LogPrint(BCLog::BENCH, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime5 - nTime4) * MILLI, nTimeChainState * MICRO, nTimeChainState * MILLI / nBlocksTotal);
3264  // Remove conflicting transactions from the mempool.;
3265  mempool.removeForBlock(blockConnecting.vtx, pindexNew->nHeight, assetDataFromBlock);
3266  disconnectpool.removeForBlock(blockConnecting.vtx);
3267  // Update chainActive & related variables.
3268  UpdateTip(pindexNew, chainparams);
3269 
3270  int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
3271  LogPrint(BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime6 - nTime5) * MILLI, nTimePostConnect * MICRO, nTimePostConnect * MILLI / nBlocksTotal);
3272  LogPrint(BCLog::BENCH, "- Connect block: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime6 - nTime1) * MILLI, nTimeTotal * MICRO, nTimeTotal * MILLI / nBlocksTotal);
3273 
3274  connectTrace.BlockConnected(pindexNew, std::move(pthisBlock));
3275  return true;
3276 }
3277 
3282 static CBlockIndex* FindMostWorkChain() {
3283  do {
3284  CBlockIndex *pindexNew = nullptr;
3285 
3286  // Find the best candidate header.
3287  {
3288  std::set<CBlockIndex*, CBlockIndexWorkComparator>::reverse_iterator it = setBlockIndexCandidates.rbegin();
3289  if (it == setBlockIndexCandidates.rend())
3290  return nullptr;
3291  pindexNew = *it;
3292  }
3293 
3294  // Check whether all blocks on the path between the currently active chain and the candidate are valid.
3295  // Just going until the active chain is an optimization, as we know all blocks in it are valid already.
3296  CBlockIndex *pindexTest = pindexNew;
3297  bool fInvalidAncestor = false;
3298  while (pindexTest && !chainActive.Contains(pindexTest)) {
3299  assert(pindexTest->nChainTx || pindexTest->nHeight == 0);
3300 
3301  // Pruned nodes may have entries in setBlockIndexCandidates for
3302  // which block files have been deleted. Remove those as candidates
3303  // for the most work chain if we come across them; we can't switch
3304  // to a chain unless we have all the non-active-chain parent blocks.
3305  bool fFailedChain = pindexTest->nStatus & BLOCK_FAILED_MASK;
3306  bool fMissingData = !(pindexTest->nStatus & BLOCK_HAVE_DATA);
3307  if (fFailedChain || fMissingData) {
3308  // Candidate chain is not usable (either invalid or missing data)
3309  if (fFailedChain && (pindexBestInvalid == nullptr || pindexNew->nChainWork > pindexBestInvalid->nChainWork))
3310  pindexBestInvalid = pindexNew;
3311  CBlockIndex *pindexFailed = pindexNew;
3312  // Remove the entire chain from the set.
3313  while (pindexTest != pindexFailed) {
3314  if (fFailedChain) {
3315  pindexFailed->nStatus |= BLOCK_FAILED_CHILD;
3316  } else if (fMissingData) {
3317  // If we're missing data, then add back to mapBlocksUnlinked,
3318  // so that if the block arrives in the future we can try adding
3319  // to setBlockIndexCandidates again.
3320  mapBlocksUnlinked.insert(std::make_pair(pindexFailed->pprev, pindexFailed));
3321  }
3322  setBlockIndexCandidates.erase(pindexFailed);
3323  pindexFailed = pindexFailed->pprev;
3324  }
3325  setBlockIndexCandidates.erase(pindexTest);
3326  fInvalidAncestor = true;
3327  break;
3328  }
3329  pindexTest = pindexTest->pprev;
3330  }
3331  if (!fInvalidAncestor)
3332  return pindexNew;
3333  } while(true);
3334 }
3335 
3337 static void PruneBlockIndexCandidates() {
3338  // Note that we can't delete the current block itself, as we may need to return to it later in case a
3339  // reorganization to a better block fails.
3340  std::set<CBlockIndex*, CBlockIndexWorkComparator>::iterator it = setBlockIndexCandidates.begin();
3341  while (it != setBlockIndexCandidates.end() && setBlockIndexCandidates.value_comp()(*it, chainActive.Tip())) {
3342  setBlockIndexCandidates.erase(it++);
3343  }
3344  // Either the current tip or a successor of it we're working towards is left in setBlockIndexCandidates.
3345  assert(!setBlockIndexCandidates.empty());
3346 }
3347 
3352 static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace)
3353 {
3354  AssertLockHeld(cs_main);
3355  const CBlockIndex *pindexOldTip = chainActive.Tip();
3356  const CBlockIndex *pindexFork = chainActive.FindFork(pindexMostWork);
3357 
3358  // Disconnect active blocks which are no longer in the best chain.
3359  bool fBlocksDisconnected = false;
3360  DisconnectedBlockTransactions disconnectpool;
3361  while (chainActive.Tip() && chainActive.Tip() != pindexFork) {
3362  if (!DisconnectTip(state, chainparams, &disconnectpool)) {
3363  // This is likely a fatal error, but keep the mempool consistent,
3364  // just in case. Only remove from the mempool in this case.
3365  UpdateMempoolForReorg(disconnectpool, false);
3366  return false;
3367  }
3368  fBlocksDisconnected = true;
3369  }
3370 
3371  // Build list of new blocks to connect.
3372  std::vector<CBlockIndex*> vpindexToConnect;
3373  bool fContinue = true;
3374  int nHeight = pindexFork ? pindexFork->nHeight : -1;
3375  while (fContinue && nHeight != pindexMostWork->nHeight) {
3376  // Don't iterate the entire list of potential improvements toward the best tip, as we likely only need
3377  // a few blocks along the way.
3378  int nTargetHeight = std::min(nHeight + 32, pindexMostWork->nHeight);
3379  vpindexToConnect.clear();
3380  vpindexToConnect.reserve(nTargetHeight - nHeight);
3381  CBlockIndex *pindexIter = pindexMostWork->GetAncestor(nTargetHeight);
3382  while (pindexIter && pindexIter->nHeight != nHeight) {
3383  vpindexToConnect.push_back(pindexIter);
3384  pindexIter = pindexIter->pprev;
3385  }
3386  nHeight = nTargetHeight;
3387 
3388  // Connect new blocks.
3389  for (CBlockIndex *pindexConnect : reverse_iterate(vpindexToConnect)) {
3390  if (!ConnectTip(state, chainparams, pindexConnect, pindexConnect == pindexMostWork ? pblock : std::shared_ptr<const CBlock>(), connectTrace, disconnectpool)) {
3391  if (state.IsInvalid()) {
3392  // The block violates a consensus rule.
3393  if (!state.CorruptionPossible())
3394  InvalidChainFound(vpindexToConnect.back());
3395  state = CValidationState();
3396  fInvalidFound = true;
3397  fContinue = false;
3398  break;
3399  } else {
3400  // A system error occurred (disk space, database error, ...).
3401  // Make the mempool consistent with the current tip, just in case
3402  // any observers try to use it before shutdown.
3403  UpdateMempoolForReorg(disconnectpool, false);
3404  return false;
3405  }
3406  } else {
3407  PruneBlockIndexCandidates();
3408  if (!pindexOldTip || chainActive.Tip()->nChainWork > pindexOldTip->nChainWork) {
3409  // We're in a better position than we were. Return temporarily to release the lock.
3410  fContinue = false;
3411  break;
3412  }
3413  }
3414  }
3415  }
3416 
3417  if (fBlocksDisconnected) {
3418  // If any blocks were disconnected, disconnectpool may be non empty. Add
3419  // any disconnected transactions back to the mempool.
3420  UpdateMempoolForReorg(disconnectpool, true);
3421  }
3422  mempool.check(pcoinsTip);
3423 
3424  // Callbacks/notifications for a new best chain.
3425  if (fInvalidFound)
3426  CheckForkWarningConditionsOnNewFork(vpindexToConnect.back());
3427  else
3428  CheckForkWarningConditions();
3429 
3430  return true;
3431 }
3432 
3433 static void NotifyHeaderTip() {
3434  bool fNotify = false;
3435  bool fInitialBlockDownload = false;
3436  static CBlockIndex* pindexHeaderOld = nullptr;
3437  CBlockIndex* pindexHeader = nullptr;
3438  {
3439  LOCK(cs_main);
3440  pindexHeader = pindexBestHeader;
3441 
3442  if (pindexHeader != pindexHeaderOld) {
3443  fNotify = true;
3444  fInitialBlockDownload = IsInitialBlockDownload();
3445  pindexHeaderOld = pindexHeader;
3446  }
3447  }
3448  // Send block tip changed notifications without cs_main
3449  if (fNotify) {
3450  uiInterface.NotifyHeaderTip(fInitialBlockDownload, pindexHeader);
3451  }
3452 }
3453 
3459 bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) {
3460  // Note that while we're often called here from ProcessNewBlock, this is
3461  // far from a guarantee. Things in the P2P/RPC will often end up calling
3462  // us in the middle of ProcessNewBlock - do not assume pblock is set
3463  // sanely for performance or correctness!
3464 
3465  CBlockIndex *pindexMostWork = nullptr;
3466  CBlockIndex *pindexNewTip = nullptr;
3467  int nStopAtHeight = gArgs.GetArg("-stopatheight", DEFAULT_STOPATHEIGHT);
3468  do {
3469  boost::this_thread::interruption_point();
3470  if (ShutdownRequested())
3471  break;
3472 
3473  const CBlockIndex *pindexFork;
3474  bool fInitialDownload;
3475  {
3476  LOCK(cs_main);
3477  ConnectTrace connectTrace(mempool); // Destructed before cs_main is unlocked
3478 
3479  CBlockIndex *pindexOldTip = chainActive.Tip();
3480  if (pindexMostWork == nullptr) {
3481  pindexMostWork = FindMostWorkChain();
3482  }
3483 
3484  // Whether we have anything to do at all.
3485  if (pindexMostWork == nullptr || pindexMostWork == chainActive.Tip())
3486  return true;
3487 
3488  bool fInvalidFound = false;
3489  std::shared_ptr<const CBlock> nullBlockPtr;
3490  if (!ActivateBestChainStep(state, chainparams, pindexMostWork, pblock && pblock->GetHash() == pindexMostWork->GetBlockHash() ? pblock : nullBlockPtr, fInvalidFound, connectTrace))
3491  return false;
3492 
3493  if (fInvalidFound) {
3494  // Wipe cache, we may need another branch now.
3495  pindexMostWork = nullptr;
3496  }
3497  pindexNewTip = chainActive.Tip();
3498  pindexFork = chainActive.FindFork(pindexOldTip);
3499  fInitialDownload = IsInitialBlockDownload();
3500 
3501  for (const PerBlockConnectTrace& trace : connectTrace.GetBlocksConnected()) {
3502  assert(trace.pblock && trace.pindex);
3503  GetMainSignals().BlockConnected(trace.pblock, trace.pindex, *trace.conflictedTxs);
3504  }
3505  }
3506  // When we reach this point, we switched to a new tip (stored in pindexNewTip).
3507 
3508  // Notifications/callbacks that can run without cs_main
3509 
3510  // Notify external listeners about the new tip.
3511  GetMainSignals().UpdatedBlockTip(pindexNewTip, pindexFork, fInitialDownload);
3512 
3513  // Always notify the UI if a new block tip was connected
3514  if (pindexFork != pindexNewTip) {
3515  uiInterface.NotifyBlockTip(fInitialDownload, pindexNewTip);
3516  }
3517 
3518  if (nStopAtHeight && pindexNewTip && pindexNewTip->nHeight >= nStopAtHeight) StartShutdown();
3519  } while (pindexNewTip != pindexMostWork);
3520  CheckBlockIndex(chainparams.GetConsensus());
3521 
3522  // Write changes periodically to disk, after relay.
3523  if (!FlushStateToDisk(chainparams, state, FLUSH_STATE_PERIODIC)) {
3524  return false;
3525  }
3526 
3527  return true;
3528 }
3529 
3530 
3531 bool PreciousBlock(CValidationState& state, const CChainParams& params, CBlockIndex *pindex)
3532 {
3533  {
3534  LOCK(cs_main);
3535  if (pindex->nChainWork < chainActive.Tip()->nChainWork) {
3536  // Nothing to do, this block is not at the tip.
3537  return true;
3538  }
3539  if (chainActive.Tip()->nChainWork > nLastPreciousChainwork) {
3540  // The chain has been extended since the last call, reset the counter.
3541  nBlockReverseSequenceId = -1;
3542  }
3543  nLastPreciousChainwork = chainActive.Tip()->nChainWork;
3544  setBlockIndexCandidates.erase(pindex);
3545  pindex->nSequenceId = nBlockReverseSequenceId;
3546  if (nBlockReverseSequenceId > std::numeric_limits<int32_t>::min()) {
3547  // We can't keep reducing the counter if somebody really wants to
3548  // call preciousblock 2**31-1 times on the same set of tips...
3549  nBlockReverseSequenceId--;
3550  }
3551  if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && pindex->nChainTx) {
3552  setBlockIndexCandidates.insert(pindex);
3553  PruneBlockIndexCandidates();
3554  }
3555  }
3556 
3557  return ActivateBestChain(state, params);
3558 }
3559 
3560 bool InvalidateBlock(CValidationState& state, const CChainParams& chainparams, CBlockIndex *pindex)
3561 {
3562  AssertLockHeld(cs_main);
3563 
3564  // We first disconnect backwards and then mark the blocks as invalid.
3565  // This prevents a case where pruned nodes may fail to invalidateblock
3566  // and be left unable to start as they have no tip candidates (as there
3567  // are no blocks that meet the "have data and are not invalid per
3568  // nStatus" criteria for inclusion in setBlockIndexCandidates).
3569 
3570  bool pindex_was_in_chain = false;
3571  CBlockIndex *invalid_walk_tip = chainActive.Tip();
3572 
3573  DisconnectedBlockTransactions disconnectpool;
3574  while (chainActive.Contains(pindex)) {
3575  pindex_was_in_chain = true;
3576  // ActivateBestChain considers blocks already in chainActive
3577  // unconditionally valid already, so force disconnect away from it.
3578  if (!DisconnectTip(state, chainparams, &disconnectpool)) {
3579  // It's probably hopeless to try to make the mempool consistent
3580  // here if DisconnectTip failed, but we can try.
3581  UpdateMempoolForReorg(disconnectpool, false);
3582  return false;
3583  }
3584  }
3585 
3586  // Now mark the blocks we just disconnected as descendants invalid
3587  // (note this may not be all descendants).
3588  while (pindex_was_in_chain && invalid_walk_tip != pindex) {
3589  invalid_walk_tip->nStatus |= BLOCK_FAILED_CHILD;
3590  setDirtyBlockIndex.insert(invalid_walk_tip);
3591  setBlockIndexCandidates.erase(invalid_walk_tip);
3592  invalid_walk_tip = invalid_walk_tip->pprev;
3593  }
3594 
3595  // Mark the block itself as invalid.
3596  pindex->nStatus |= BLOCK_FAILED_VALID;
3597  setDirtyBlockIndex.insert(pindex);
3598  setBlockIndexCandidates.erase(pindex);
3599  g_failed_blocks.insert(pindex);
3600 
3601  // DisconnectTip will add transactions to disconnectpool; try to add these
3602  // back to the mempool.
3603  UpdateMempoolForReorg(disconnectpool, true);
3604 
3605  // The resulting new best tip may not be in setBlockIndexCandidates anymore, so
3606  // add it again.
3607  BlockMap::iterator it = mapBlockIndex.begin();
3608  while (it != mapBlockIndex.end()) {
3609  if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && !setBlockIndexCandidates.value_comp()(it->second, chainActive.Tip())) {
3610  setBlockIndexCandidates.insert(it->second);
3611  }
3612  it++;
3613  }
3614 
3615  InvalidChainFound(pindex);
3617  return true;
3618 }
3619 
3621  AssertLockHeld(cs_main);
3622 
3623  int nHeight = pindex->nHeight;
3624 
3625  // Remove the invalidity flag from this block and all its descendants.
3626  BlockMap::iterator it = mapBlockIndex.begin();
3627  while (it != mapBlockIndex.end()) {
3628  if (!it->second->IsValid() && it->second->GetAncestor(nHeight) == pindex) {
3629  it->second->nStatus &= ~BLOCK_FAILED_MASK;
3630  setDirtyBlockIndex.insert(it->second);
3631  if (it->second->IsValid(BLOCK_VALID_TRANSACTIONS) && it->second->nChainTx && setBlockIndexCandidates.value_comp()(chainActive.Tip(), it->second)) {
3632  setBlockIndexCandidates.insert(it->second);
3633  }
3634  if (it->second == pindexBestInvalid) {
3635  // Reset invalid block marker if it was pointing to one of those.
3636  pindexBestInvalid = nullptr;
3637  }
3638  g_failed_blocks.erase(it->second);
3639  }
3640  it++;
3641  }
3642 
3643  // Remove the invalidity flag from all ancestors too.
3644  while (pindex != nullptr) {
3645  if (pindex->nStatus & BLOCK_FAILED_MASK) {
3646  pindex->nStatus &= ~BLOCK_FAILED_MASK;
3647  setDirtyBlockIndex.insert(pindex);
3648  }
3649  pindex = pindex->pprev;
3650  }
3651  return true;
3652 }
3653 
3654 static CBlockIndex* AddToBlockIndex(const CBlockHeader& block)
3655 {
3656  // Check for duplicate
3657  uint256 hash = block.GetHash();
3658  BlockMap::iterator it = mapBlockIndex.find(hash);
3659  if (it != mapBlockIndex.end())
3660  return it->second;
3661 
3662  // Construct new block index object
3663  CBlockIndex* pindexNew = new CBlockIndex(block);
3664  // We assign the sequence id to blocks only when the full data is available,
3665  // to avoid miners withholding blocks but broadcasting headers, to get a
3666  // competitive advantage.
3667  pindexNew->nSequenceId = 0;
3668  BlockMap::iterator mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first;
3669  pindexNew->phashBlock = &((*mi).first);
3670  BlockMap::iterator miPrev = mapBlockIndex.find(block.hashPrevBlock);
3671  if (miPrev != mapBlockIndex.end())
3672  {
3673  pindexNew->pprev = (*miPrev).second;
3674  pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
3675  pindexNew->BuildSkip();
3676  }
3677  pindexNew->nTimeMax = (pindexNew->pprev ? std::max(pindexNew->pprev->nTimeMax, pindexNew->nTime) : pindexNew->nTime);
3678  pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + GetBlockProof(*pindexNew);
3679  pindexNew->RaiseValidity(BLOCK_VALID_TREE);
3680  if (pindexBestHeader == nullptr || pindexBestHeader->nChainWork < pindexNew->nChainWork)
3681  pindexBestHeader = pindexNew;
3682 
3683  setDirtyBlockIndex.insert(pindexNew);
3684 
3685  return pindexNew;
3686 }
3687 
3689 static bool ReceivedBlockTransactions(const CBlock &block, CValidationState& state, CBlockIndex *pindexNew, const CDiskBlockPos& pos, const Consensus::Params& consensusParams)
3690 {
3691  pindexNew->nTx = block.vtx.size();
3692  pindexNew->nChainTx = 0;
3693  pindexNew->nFile = pos.nFile;
3694  pindexNew->nDataPos = pos.nPos;
3695  pindexNew->nUndoPos = 0;
3696  pindexNew->nStatus |= BLOCK_HAVE_DATA;
3697  if (IsWitnessEnabled(pindexNew->pprev, consensusParams)) {
3698  pindexNew->nStatus |= BLOCK_OPT_WITNESS;
3699  }
3701  setDirtyBlockIndex.insert(pindexNew);
3702 
3703  if (pindexNew->pprev == nullptr || pindexNew->pprev->nChainTx) {
3704  // If pindexNew is the genesis block or all parents are BLOCK_VALID_TRANSACTIONS.
3705  std::deque<CBlockIndex*> queue;
3706  queue.push_back(pindexNew);
3707 
3708  // Recursively process any descendant blocks that now may be eligible to be connected.
3709  while (!queue.empty()) {
3710  CBlockIndex *pindex = queue.front();
3711  queue.pop_front();
3712  pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
3713  {
3714  LOCK(cs_nBlockSequenceId);
3715  pindex->nSequenceId = nBlockSequenceId++;
3716  }
3717  if (chainActive.Tip() == nullptr || !setBlockIndexCandidates.value_comp()(pindex, chainActive.Tip())) {
3718  setBlockIndexCandidates.insert(pindex);
3719  }
3720  std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex);
3721  while (range.first != range.second) {
3722  std::multimap<CBlockIndex*, CBlockIndex*>::iterator it = range.first;
3723  queue.push_back(it->second);
3724  range.first++;
3725  mapBlocksUnlinked.erase(it);
3726  }
3727  }
3728  } else {
3729  if (pindexNew->pprev && pindexNew->pprev->IsValid(BLOCK_VALID_TREE)) {
3730  mapBlocksUnlinked.insert(std::make_pair(pindexNew->pprev, pindexNew));
3731  }
3732  }
3733 
3734  return true;
3735 }
3736 
3737 static bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false)
3738 {
3739  LOCK(cs_LastBlockFile);
3740 
3741  unsigned int nFile = fKnown ? pos.nFile : nLastBlockFile;
3742  if (vinfoBlockFile.size() <= nFile) {
3743  vinfoBlockFile.resize(nFile + 1);
3744  }
3745 
3746  if (!fKnown) {
3747  while (vinfoBlockFile[nFile].nSize + nAddSize >= MAX_BLOCKFILE_SIZE) {
3748  nFile++;
3749  if (vinfoBlockFile.size() <= nFile) {
3750  vinfoBlockFile.resize(nFile + 1);
3751  }
3752  }
3753  pos.nFile = nFile;
3754  pos.nPos = vinfoBlockFile[nFile].nSize;
3755  }
3756 
3757  if ((int)nFile != nLastBlockFile) {
3758  if (!fKnown) {
3759  LogPrintf("Leaving block file %i: %s\n", nLastBlockFile, vinfoBlockFile[nLastBlockFile].ToString());
3760  }
3761  FlushBlockFile(!fKnown);
3762  nLastBlockFile = nFile;
3763  }
3764 
3765  vinfoBlockFile[nFile].AddBlock(nHeight, nTime);
3766  if (fKnown)
3767  vinfoBlockFile[nFile].nSize = std::max(pos.nPos + nAddSize, vinfoBlockFile[nFile].nSize);
3768  else
3769  vinfoBlockFile[nFile].nSize += nAddSize;
3770 
3771  if (!fKnown) {
3772  unsigned int nOldChunks = (pos.nPos + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
3773  unsigned int nNewChunks = (vinfoBlockFile[nFile].nSize + BLOCKFILE_CHUNK_SIZE - 1) / BLOCKFILE_CHUNK_SIZE;
3774  if (nNewChunks > nOldChunks) {
3775  if (fPruneMode)
3776  fCheckForPruning = true;
3777  if (CheckDiskSpace(nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos)) {
3778  FILE *file = OpenBlockFile(pos);
3779  if (file) {
3780  LogPrintf("Pre-allocating up to position 0x%x in blk%05u.dat\n", nNewChunks * BLOCKFILE_CHUNK_SIZE, pos.nFile);
3781  AllocateFileRange(file, pos.nPos, nNewChunks * BLOCKFILE_CHUNK_SIZE - pos.nPos);
3782  fclose(file);
3783  }
3784  }
3785  else
3786  return state.Error("out of disk space");
3787  }
3788  }
3789 
3790  setDirtyFileInfo.insert(nFile);
3791  return true;
3792 }
3793 
3794 static bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
3795 {
3796  pos.nFile = nFile;
3797 
3798  LOCK(cs_LastBlockFile);
3799 
3800  unsigned int nNewSize;
3801  pos.nPos = vinfoBlockFile[nFile].nUndoSize;
3802  nNewSize = vinfoBlockFile[nFile].nUndoSize += nAddSize;
3803  setDirtyFileInfo.insert(nFile);
3804 
3805  unsigned int nOldChunks = (pos.nPos + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
3806  unsigned int nNewChunks = (nNewSize + UNDOFILE_CHUNK_SIZE - 1) / UNDOFILE_CHUNK_SIZE;
3807  if (nNewChunks > nOldChunks) {
3808  if (fPruneMode)
3809  fCheckForPruning = true;
3810  if (CheckDiskSpace(nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos)) {
3811  FILE *file = OpenUndoFile(pos);
3812  if (file) {
3813  LogPrintf("Pre-allocating up to position 0x%x in rev%05u.dat\n", nNewChunks * UNDOFILE_CHUNK_SIZE, pos.nFile);
3814  AllocateFileRange(file, pos.nPos, nNewChunks * UNDOFILE_CHUNK_SIZE - pos.nPos);
3815  fclose(file);
3816  }
3817  }
3818  else
3819  return state.Error("out of disk space");
3820  }
3821 
3822  return true;
3823 }
3824 
3825 static bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true)
3826 {
3827  // Check proof of work matches claimed amount
3828  if (fCheckPOW && !CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
3829  return state.DoS(50, false, REJECT_INVALID, "high-hash", false, "proof of work failed");
3830  return true;
3831 }
3832 
3833 bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
3834 {
3835  // These are checks that are independent of context.
3836 
3837  if (block.fChecked)
3838  return true;
3839 
3840  // Check that the header is valid (particularly PoW). This is mostly
3841  // redundant with the call in AcceptBlockHeader.
3842  if (!CheckBlockHeader(block, state, consensusParams, fCheckPOW))
3843  return false;
3844 
3845  // Check the merkle root.
3846  if (fCheckMerkleRoot) {
3847  bool mutated;
3848  uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated);
3849  if (block.hashMerkleRoot != hashMerkleRoot2)
3850  return state.DoS(100, false, REJECT_INVALID, "bad-txnmrklroot", true, "hashMerkleRoot mismatch");
3851 
3852  // Check for merkle tree malleability (CVE-2012-2459): repeating sequences
3853  // of transactions in a block without affecting the merkle root of a block,
3854  // while still invalidating it.
3855  if (mutated)
3856  return state.DoS(100, false, REJECT_INVALID, "bad-txns-duplicate", true, "duplicate transaction");
3857  }
3858 
3859  // All potential-corruption validation must be done before we do any
3860  // transaction validation, as otherwise we may mark the header as invalid
3861  // because we receive the wrong transactions for it.
3862  // Note that witness malleability is checked in ContextualCheckBlock, so no
3863  // checks that use witness data may be performed here.
3864 
3865  // Size limits
3866  if (block.vtx.empty() || block.vtx.size() * WITNESS_SCALE_FACTOR > GetMaxBlockWeight() || ::GetSerializeSize(block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS) * WITNESS_SCALE_FACTOR > GetMaxBlockWeight())
3867  return state.DoS(100, false, REJECT_INVALID, "bad-blk-length", false, "size limits failed");
3868 
3869  // First transaction must be coinbase, the rest must not be
3870  if (block.vtx.empty() || !block.vtx[0]->IsCoinBase())
3871  return state.DoS(100, false, REJECT_INVALID, "bad-cb-missing", false, "first tx is not coinbase");
3872 
3873  for (unsigned int i = 1; i < block.vtx.size(); i++)
3874  if (block.vtx[i]->IsCoinBase())
3875  return state.DoS(100, false, REJECT_INVALID, "bad-cb-multiple", false, "more than one coinbase");
3876 
3877  // Check transactions
3878  for (const auto& tx : block.vtx)
3879  if (!CheckTransaction(*tx, state))
3880  return state.Invalid(false, state.GetRejectCode(), state.GetRejectReason(),
3881  strprintf("Transaction check failed (tx hash %s) %s %s", tx->GetHash().ToString(), state.GetDebugMessage(), state.GetRejectReason()));
3882 
3883  unsigned int nSigOps = 0;
3884  for (const auto& tx : block.vtx)
3885  {
3886  nSigOps += GetLegacySigOpCount(*tx);
3887  }
3888  if (nSigOps * WITNESS_SCALE_FACTOR > MAX_BLOCK_SIGOPS_COST)
3889  return state.DoS(100, false, REJECT_INVALID, "bad-blk-sigops", false, "out-of-bounds SigOpCount");
3890 
3891  if (fCheckPOW && fCheckMerkleRoot)
3892  block.fChecked = true;
3893 
3894  return true;
3895 }
3896 
3897 bool IsWitnessEnabled(const CBlockIndex* pindexPrev, const Consensus::Params& params)
3898 {
3899  return params.nSegwitEnabled;
3900 }
3901 
3903  return params.nSegwitEnabled;
3904 }
3905 // Compute at which vout of the block's coinbase transaction the witness
3906 // commitment occurs, or -1 if not found.
3907 static int GetWitnessCommitmentIndex(const CBlock& block)
3908 {
3909  int commitpos = -1;
3910  if (!block.vtx.empty()) {
3911  for (size_t o = 0; o < block.vtx[0]->vout.size(); o++) {
3912  if (block.vtx[0]->vout[o].scriptPubKey.size() >= 38 && block.vtx[0]->vout[o].scriptPubKey[0] == OP_RETURN && block.vtx[0]->vout[o].scriptPubKey[1] == 0x24 && block.vtx[0]->vout[o].scriptPubKey[2] == 0xaa && block.vtx[0]->vout[o].scriptPubKey[3] == 0x21 && block.vtx[0]->vout[o].scriptPubKey[4] == 0xa9 && block.vtx[0]->vout[o].scriptPubKey[5] == 0xed) {
3913  commitpos = o;
3914  }
3915  }
3916  }
3917  return commitpos;
3918 }
3919 
3920 void UpdateUncommittedBlockStructures(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
3921 {
3922  int commitpos = GetWitnessCommitmentIndex(block);
3923  static const std::vector<unsigned char> nonce(32, 0x00);
3924  if (commitpos != -1 && IsWitnessEnabled(pindexPrev, consensusParams) && !block.vtx[0]->HasWitness()) {
3925  CMutableTransaction tx(*block.vtx[0]);
3926  tx.vin[0].scriptWitness.stack.resize(1);
3927  tx.vin[0].scriptWitness.stack[0] = nonce;
3928  block.vtx[0] = MakeTransactionRef(std::move(tx));
3929  }
3930 }
3931 
3932 std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBlockIndex* pindexPrev, const Consensus::Params& consensusParams)
3933 {
3934  std::vector<unsigned char> commitment;
3935  int commitpos = GetWitnessCommitmentIndex(block);
3936  std::vector<unsigned char> ret(32, 0x00);
3937  if(consensusParams.nSegwitEnabled) { // if (consensusParams.vDeployments[Consensus::DEPLOYMENT_SEGWIT].nTimeout != 0) {
3938  if (commitpos == -1) {
3939  uint256 witnessroot = BlockWitnessMerkleRoot(block, nullptr);
3940  CHash256().Write(witnessroot.begin(), 32).Write(ret.data(), 32).Finalize(witnessroot.begin());
3941  CTxOut out;
3942  out.nValue = 0;
3943  out.scriptPubKey.resize(38);
3944  out.scriptPubKey[0] = OP_RETURN;
3945  out.scriptPubKey[1] = 0x24;
3946  out.scriptPubKey[2] = 0xaa;
3947  out.scriptPubKey[3] = 0x21;
3948  out.scriptPubKey[4] = 0xa9;
3949  out.scriptPubKey[5] = 0xed;
3950  memcpy(&out.scriptPubKey[6], witnessroot.begin(), 32);
3951  commitment = std::vector<unsigned char>(out.scriptPubKey.begin(), out.scriptPubKey.end());
3952  CMutableTransaction tx(*block.vtx[0]);
3953  tx.vout.push_back(out);
3954  block.vtx[0] = MakeTransactionRef(std::move(tx));
3955  }
3956  }
3957  UpdateUncommittedBlockStructures(block, pindexPrev, consensusParams);
3958  return commitment;
3959 }
3960 
3964 static bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& params, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
3965 {
3966  assert(pindexPrev != nullptr);
3967  const int nHeight = pindexPrev->nHeight + 1;
3968 
3969  //If this is a reorg, check that it is not too deep
3970  int nMaxReorgDepth = gArgs.GetArg("-maxreorg", Params().MaxReorganizationDepth());
3971  int nMinReorgPeers = gArgs.GetArg("-minreorgpeers", Params().MinReorganizationPeers());
3972  int nMinReorgAge = gArgs.GetArg("-minreorgage", Params().MinReorganizationAge());
3973  bool fGreaterThanMaxReorg = (chainActive.Height() - (nHeight - 1)) >= nMaxReorgDepth;
3974  if (fGreaterThanMaxReorg && g_connman) {
3975  int nCurrentNodeCount = g_connman->GetNodeCount(CConnman::CONNECTIONS_ALL);
3976  bool bIsCurrentChainCaughtUp = (GetTime() - chainActive.Tip()->nTime) <= nMinReorgAge;
3977  if ((nCurrentNodeCount >= nMinReorgPeers) && bIsCurrentChainCaughtUp)
3978  return state.DoS(10,
3979  error("%s: forked chain older than max reorganization depth (height %d), with connections (count %d), and caught up with active chain (%s)",
3980  __func__, nHeight, nCurrentNodeCount, bIsCurrentChainCaughtUp ? "true" : "false"),
3981  REJECT_MAXREORGDEPTH, "bad-fork-prior-to-maxreorgdepth");
3982  }
3983 
3984  // Check proof of work
3985  const Consensus::Params& consensusParams = params.GetConsensus();
3986  if (block.nBits != GetNextWorkRequired(pindexPrev, &block, consensusParams))
3987  return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, "incorrect proof of work");
3988 
3989  // Check against checkpoints
3990  if (fCheckpointsEnabled) {
3991  // Don't accept any forks from the main chain prior to last checkpoint.
3992  // GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
3993  // MapBlockIndex.
3994  CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(params.Checkpoints());
3995  if (pcheckpoint && nHeight < pcheckpoint->nHeight)
3996  return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight), REJECT_CHECKPOINT, "bad-fork-prior-to-checkpoint");
3997  }
3998 
3999  // Check timestamp against prev
4000  if (block.GetBlockTime() <= pindexPrev->GetMedianTimePast())
4001  return state.Invalid(false, REJECT_INVALID, "time-too-old", "block's timestamp is too early");
4002 
4003  // Check timestamp
4004  if (IsDGWActive(pindexPrev->nHeight+1))
4005  {
4006  if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME_DGW)
4007  return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
4008  }
4009  else
4010  {
4011  if (block.GetBlockTime() > nAdjustedTime + MAX_FUTURE_BLOCK_TIME)
4012  return state.Invalid(false, REJECT_INVALID, "time-too-new", "block timestamp too far in the future");
4013  }
4014 
4015  // Reject outdated version blocks when 95% (75% on testnet) of the network has upgraded:
4016  // check for version 2, 3 and 4 upgrades
4017  // if((block.nVersion < 2 && nHeight >= consensusParams.BIP34Height) ||
4018  // (block.nVersion < 3 && nHeight >= consensusParams.BIP66Height) ||
4019  // (block.nVersion < 4 && nHeight >= consensusParams.BIP65Height))
4020  // return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", block.nVersion),
4021  // strprintf("rejected nVersion=0x%08x block", block.nVersion));
4022 
4023  // Reject outdated version blocks once assets are active.
4024  if (AreAssetsDeployed() && block.nVersion < VERSIONBITS_TOP_BITS_ASSETS)
4025  return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", block.nVersion), strprintf("rejected nVersion=0x%08x block", block.nVersion));
4026 
4027  // Reject outdated version blocks once messages are active.
4028  if (IsMessagingActive(pindexPrev->nHeight+1)) {
4029  if (block.nVersion < VERSIONBITS_TOP_BITS_MESSAGING) {
4030  return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", block.nVersion),
4031  strprintf("rejected nVersion=0x%08x block", block.nVersion));
4032  }
4033  }
4034 
4035  if (IsRestrictedActive(pindexPrev->nHeight+1)) {
4036  if (block.nVersion < VERSIONBITS_TOP_BITS_RESTRICTED) {
4037  return state.Invalid(false, REJECT_OBSOLETE, strprintf("bad-version(0x%08x)", block.nVersion),
4038  strprintf("rejected nVersion=0x%08x block", block.nVersion));
4039  }
4040  }
4041 
4042  return true;
4043 }
4044 
4045 static bool ContextualCheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev, CAssetsCache* assetCache)
4046 {
4047  const int nHeight = pindexPrev == nullptr ? 0 : pindexPrev->nHeight + 1;
4048 
4049  // Start enforcing BIP113 (Median Time Past) using versionbits logic.
4050  int nLockTimeFlags = 0;
4051  if(consensusParams.nCSVEnabled == true) {
4052  nLockTimeFlags |= LOCKTIME_MEDIAN_TIME_PAST;
4053  }
4054 
4055  int64_t nLockTimeCutoff = ((nLockTimeFlags & LOCKTIME_MEDIAN_TIME_PAST) && pindexPrev)
4056  ? pindexPrev->GetMedianTimePast()
4057  : block.GetBlockTime();
4058 
4059  // Check that all transactions are finalized
4060  for (const auto& tx : block.vtx) {
4061  if (!IsFinalTx(*tx, nHeight, nLockTimeCutoff)) {
4062  return state.DoS(10, false, REJECT_INVALID, "bad-txns-nonfinal", false, "non-final transaction");
4063  }
4064  }
4065 
4066  // Enforce rule that the coinbase starts with serialized block height
4067  CScript expect = CScript() << nHeight;
4068 
4069  if (consensusParams.nBIP34Enabled)
4070  {
4071  if (block.vtx[0]->vin[0].scriptSig.size() < expect.size() ||
4072  !std::equal(expect.begin(), expect.end(), block.vtx[0]->vin[0].scriptSig.begin())) {
4073  return state.DoS(100, false, REJECT_INVALID, "bad-cb-height", false, "block height mismatch in coinbase");
4074  }
4075  }
4076  // Validation for witness commitments.
4077  // * We compute the witness hash (which is the hash including witnesses) of all the block's transactions, except the
4078  // coinbase (where 0x0000....0000 is used instead).
4079  // * The coinbase scriptWitness is a stack of a single 32-byte vector, containing a witness nonce (unconstrained).
4080  // * We build a merkle tree with all those witness hashes as leaves (similar to the hashMerkleRoot in the block header).
4081  // * There must be at least one output whose scriptPubKey is a single 36-byte push, the first 4 bytes of which are
4082  // {0xaa, 0x21, 0xa9, 0xed}, and the following 32 bytes are SHA256^2(witness root, witness nonce). In case there are
4083  // multiple, the last one is used.
4084  bool fHaveWitness = false;
4085  if(IsWitnessEnabled(consensusParams)) {
4086  int commitpos = GetWitnessCommitmentIndex(block);
4087  if (commitpos != -1) {
4088  bool malleated = false;
4089  uint256 hashWitness = BlockWitnessMerkleRoot(block, &malleated);
4090  // The malleation check is ignored; as the transaction tree itself
4091  // already does not permit it, it is impossible to trigger in the
4092  // witness tree.
4093  if (block.vtx[0]->vin[0].scriptWitness.stack.size() != 1 || block.vtx[0]->vin[0].scriptWitness.stack[0].size() != 32) {
4094  return state.DoS(100, false, REJECT_INVALID, "bad-witness-nonce-size", true, strprintf("%s : invalid witness nonce size", __func__));
4095  }
4096  CHash256().Write(hashWitness.begin(), 32).Write(&block.vtx[0]->vin[0].scriptWitness.stack[0][0], 32).Finalize(hashWitness.begin());
4097  if (memcmp(hashWitness.begin(), &block.vtx[0]->vout[commitpos].scriptPubKey[6], 32)) {
4098  return state.DoS(100, false, REJECT_INVALID, "bad-witness-merkle-match", true, strprintf("%s : witness merkle commitment mismatch", __func__));
4099  }
4100  fHaveWitness = true;
4101  }
4102  }
4103  // No witness data is allowed in blocks that don't commit to witness data, as this would otherwise leave room for spam
4104  if (!fHaveWitness) {
4105  for (const auto& tx : block.vtx) {
4106  if (tx->HasWitness()) {
4107  return state.DoS(100, false, REJECT_INVALID, "unexpected-witness", true, strprintf("%s : unexpected witness data found", __func__));
4108  }
4109  }
4110  }
4111 
4112  // After the coinbase witness nonce and commitment are verified,
4113  // we can check if the block weight passes (before we've checked the
4114  // coinbase witness, it would be possible for the weight to be too
4115  // large by filling up the coinbase witness, which doesn't change
4116  // the block hash, so we couldn't mark the block as permanently
4117  // failed).
4118  if (GetBlockWeight(block) > GetMaxBlockWeight()) {
4119  return state.DoS(100, false, REJECT_INVALID, "bad-blk-weight", false, strprintf("%s : weight limit failed", __func__));
4120  }
4121 
4122  return true;
4123 }
4124 
4125 static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex)
4126 {
4127  AssertLockHeld(cs_main);
4128  // Check for duplicate
4129  uint256 hash = block.GetHash();
4130  BlockMap::iterator miSelf = mapBlockIndex.find(hash);
4131  CBlockIndex *pindex = nullptr;
4132  if (hash != chainparams.GetConsensus().hashGenesisBlock) {
4133 
4134  if (miSelf != mapBlockIndex.end()) {
4135  // Block header is already known.
4136  pindex = miSelf->second;
4137  if (ppindex)
4138  *ppindex = pindex;
4139  if (pindex->nStatus & BLOCK_FAILED_MASK)
4140  return state.Invalid(error("%s: block %s is marked invalid", __func__, hash.ToString()), 0, "duplicate");
4141  return true;
4142  }
4143 
4144  if (!CheckBlockHeader(block, state, chainparams.GetConsensus()))
4145  return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
4146 
4147  // Get prev block index
4148  CBlockIndex* pindexPrev = nullptr;
4149  BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
4150  if (mi == mapBlockIndex.end())
4151  return state.DoS(10, error("%s: prev block not found", __func__), 0, "prev-blk-not-found");
4152  pindexPrev = (*mi).second;
4153  if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
4154  return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
4155  if (!ContextualCheckBlockHeader(block, state, chainparams, pindexPrev, GetAdjustedTime()))
4156  return error("%s: Consensus::ContextualCheckBlockHeader: %s, %s", __func__, hash.ToString(), FormatStateMessage(state));
4157 
4158  if (!pindexPrev->IsValid(BLOCK_VALID_SCRIPTS)) {
4159  for (const CBlockIndex* failedit : g_failed_blocks) {
4160  if (pindexPrev->GetAncestor(failedit->nHeight) == failedit) {
4161  assert(failedit->nStatus & BLOCK_FAILED_VALID);
4162  CBlockIndex* invalid_walk = pindexPrev;
4163  while (invalid_walk != failedit) {
4164  invalid_walk->nStatus |= BLOCK_FAILED_CHILD;
4165  setDirtyBlockIndex.insert(invalid_walk);
4166  invalid_walk = invalid_walk->pprev;
4167  }
4168  return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");
4169  }
4170  }
4171  }
4172  }
4173  if (pindex == nullptr)
4174  pindex = AddToBlockIndex(block);
4175 
4176  if (ppindex)
4177  *ppindex = pindex;
4178 
4179  CheckBlockIndex(chainparams.GetConsensus());
4180 
4181  return true;
4182 }
4183 
4184 // Exposed wrapper for AcceptBlockHeader
4185 bool ProcessNewBlockHeaders(const std::vector<CBlockHeader>& headers, CValidationState& state, const CChainParams& chainparams, const CBlockIndex** ppindex, CBlockHeader *first_invalid)
4186 {
4187  if (first_invalid != nullptr) first_invalid->SetNull();
4188  {
4189  LOCK(cs_main);
4190  for (const CBlockHeader& header : headers) {
4191  CBlockIndex *pindex = nullptr; // Use a temp pindex instead of ppindex to avoid a const_cast
4192  if (!AcceptBlockHeader(header, state, chainparams, &pindex)) {
4193  if (first_invalid) *first_invalid = header;
4194  return false;
4195  }
4196  if (ppindex) {
4197  *ppindex = pindex;
4198  }
4199  }
4200  }
4201  NotifyHeaderTip();
4202  return true;
4203 }
4204 
4206 static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested, const CDiskBlockPos* dbp, bool* fNewBlock)
4207 {
4208  const CBlock& block = *pblock;
4209 
4210  if (fNewBlock) *fNewBlock = false;
4211  AssertLockHeld(cs_main);
4212 
4213  CBlockIndex *pindexDummy = nullptr;
4214  CBlockIndex *&pindex = ppindex ? *ppindex : pindexDummy;
4215 
4216  if (!AcceptBlockHeader(block, state, chainparams, &pindex))
4217  return false;
4218 
4219  // Try to process all requested blocks that we don't have, but only
4220  // process an unrequested block if it's new and has enough work to
4221  // advance our tip, and isn't too many blocks ahead.
4222  bool fAlreadyHave = pindex->nStatus & BLOCK_HAVE_DATA;
4223  bool fHasMoreWork = (chainActive.Tip() ? pindex->nChainWork > chainActive.Tip()->nChainWork : true);
4224  // Blocks that are too out-of-order needlessly limit the effectiveness of
4225  // pruning, because pruning will not delete block files that contain any
4226  // blocks which are too close in height to the tip. Apply this test
4227  // regardless of whether pruning is enabled; it should generally be safe to
4228  // not process unrequested blocks.
4229  bool fTooFarAhead = (pindex->nHeight > int(chainActive.Height() + MIN_BLOCKS_TO_KEEP));
4230  // TODO: Decouple this function from the block download logic by removing fRequested
4231  // This requires some new chain data structure to efficiently look up if a
4232  // block is in a chain leading to a candidate for best tip, despite not
4233  // being such a candidate itself.
4234 
4235  // TODO: deal better with return value and error conditions for duplicate
4236  // and unrequested blocks.
4237  if (fAlreadyHave) return true;
4238  if (!fRequested) { // If we didn't ask for it:
4239  if (pindex->nTx != 0) return true; // This is a previously-processed block that was pruned
4240  if (!fHasMoreWork) return true; // Don't process less-work chains
4241  if (fTooFarAhead) return true; // Block height is too high
4242 
4243  // Protect against DoS attacks from low-work chains.
4244  // If our tip is behind, a peer could try to send us
4245  // low-work blocks on a fake chain that we would never
4246  // request; don't process these.
4247  if (pindex->nChainWork < nMinimumChainWork) return true;
4248  }
4249 
4250  if (fNewBlock) *fNewBlock = true;
4251 
4252  auto currentActiveAssetCache = GetCurrentAssetCache();
4253  // Dont force the CheckBlock asset duplciates when checking from this state
4254  if (!CheckBlock(block, state, chainparams.GetConsensus(), true, true) ||
4255  !ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindex->pprev, currentActiveAssetCache)) {
4256  if (state.IsInvalid() && !state.CorruptionPossible()) {
4257  pindex->nStatus |= BLOCK_FAILED_VALID;
4258  setDirtyBlockIndex.insert(pindex);
4259  }
4260  return error("%s: %s", __func__, FormatStateMessage(state));
4261  }
4262 
4263  // Header is valid/has work, merkle tree and segwit merkle tree are good...RELAY NOW
4264  // (but if it does not build on our best tip, let the SendMessages loop relay it)
4265  if (!IsInitialBlockDownload() && chainActive.Tip() == pindex->pprev)
4266  GetMainSignals().NewPoWValidBlock(pindex, pblock);
4267 
4268  int nHeight = pindex->nHeight;
4269 
4270  // Write block to history file
4271  try {
4272  unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
4273  CDiskBlockPos blockPos;
4274  if (dbp != nullptr)
4275  blockPos = *dbp;
4276  if (!FindBlockPos(state, blockPos, nBlockSize+8, nHeight, block.GetBlockTime(), dbp != nullptr))
4277  return error("AcceptBlock(): FindBlockPos failed");
4278  if (dbp == nullptr)
4279  if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
4280  AbortNode(state, "Failed to write block");
4281  if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
4282  return error("AcceptBlock(): ReceivedBlockTransactions failed");
4283  } catch (const std::runtime_error& e) {
4284  return AbortNode(state, std::string("System error: ") + e.what());
4285  }
4286 
4287  if (fCheckForPruning)
4288  FlushStateToDisk(chainparams, state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
4289 
4290  return true;
4291 }
4292 
4293 bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<const CBlock> pblock, bool fForceProcessing, bool *fNewBlock)
4294 {
4295  {
4296  CBlockIndex *pindex = nullptr;
4297  if (fNewBlock) *fNewBlock = false;
4298  CValidationState state;
4299 
4300  // Ensure that CheckBlock() passes before calling AcceptBlock, as
4301  // belt-and-suspenders.
4302  bool ret = CheckBlock(*pblock, state, chainparams.GetConsensus(), true, true);
4303 
4304  LOCK(cs_main);
4305 
4306  if (ret) {
4307  // Store to disk
4308  ret = AcceptBlock(pblock, state, chainparams, &pindex, fForceProcessing, nullptr, fNewBlock);
4309  }
4310 
4311  CheckBlockIndex(chainparams.GetConsensus());
4312  if (!ret) {
4313  GetMainSignals().BlockChecked(*pblock, state);
4314  return error("%s: AcceptBlock FAILED (%s)", __func__, state.GetDebugMessage());
4315  }
4316  }
4317  NotifyHeaderTip();
4318 
4319  CValidationState state; // Only used to report errors, not invalidity - ignore it
4320  if (!ActivateBestChain(state, chainparams, pblock))
4321  return error("%s: ActivateBestChain failed", __func__);
4322 
4323  return true;
4324 }
4325 
4326 bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
4327 {
4328  AssertLockHeld(cs_main);
4329  assert(pindexPrev && pindexPrev == chainActive.Tip());
4330  CCoinsViewCache viewNew(pcoinsTip);
4331  CBlockIndex indexDummy(block);
4332  indexDummy.pprev = pindexPrev;
4333  indexDummy.nHeight = pindexPrev->nHeight + 1;
4334 
4336  CAssetsCache assetCache = *GetCurrentAssetCache();
4339  // NOTE: CheckBlockHeader is called by CheckBlock
4340  if (!ContextualCheckBlockHeader(block, state, chainparams, pindexPrev, GetAdjustedTime()))
4341  return error("%s: Consensus::ContextualCheckBlockHeader: %s", __func__, FormatStateMessage(state));
4342  if (!CheckBlock(block, state, chainparams.GetConsensus(), fCheckPOW, fCheckMerkleRoot))
4343  return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
4344  if (!ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindexPrev, &assetCache))
4345  return error("%s: Consensus::ContextualCheckBlock: %s", __func__, FormatStateMessage(state));
4346  if (!ConnectBlock(block, state, &indexDummy, viewNew, chainparams, &assetCache, true)) /*Add asset to function */
4347  return error("%s: Consensus::ConnectBlock: %s", __func__, FormatStateMessage(state));
4348  assert(state.IsValid());
4349 
4350  return true;
4351 }
4352 
4357 /* Calculate the amount of disk space the block & undo files currently use */
4359 {
4360  LOCK(cs_LastBlockFile);
4361 
4362  uint64_t retval = 0;
4363  for (const CBlockFileInfo &file : vinfoBlockFile) {
4364  retval += file.nSize + file.nUndoSize;
4365  }
4366  return retval;
4367 }
4368 
4369 /* Prune a block file (modify associated database entries)*/
4370 void PruneOneBlockFile(const int fileNumber)
4371 {
4372  LOCK(cs_LastBlockFile);
4373 
4374  for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); ++it) {
4375  CBlockIndex* pindex = it->second;
4376  if (pindex->nFile == fileNumber) {
4377  pindex->nStatus &= ~BLOCK_HAVE_DATA;
4378  pindex->nStatus &= ~BLOCK_HAVE_UNDO;
4379  pindex->nFile = 0;
4380  pindex->nDataPos = 0;
4381  pindex->nUndoPos = 0;
4382  setDirtyBlockIndex.insert(pindex);
4383 
4384  // Prune from mapBlocksUnlinked -- any block we prune would have
4385  // to be downloaded again in order to consider its chain, at which
4386  // point it would be considered as a candidate for
4387  // mapBlocksUnlinked or setBlockIndexCandidates.
4388  std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> range = mapBlocksUnlinked.equal_range(pindex->pprev);
4389  while (range.first != range.second) {
4390  std::multimap<CBlockIndex *, CBlockIndex *>::iterator _it = range.first;
4391  range.first++;
4392  if (_it->second == pindex) {
4393  mapBlocksUnlinked.erase(_it);
4394  }
4395  }
4396  }
4397  }
4398 
4399  vinfoBlockFile[fileNumber].SetNull();
4400  setDirtyFileInfo.insert(fileNumber);
4401 }
4402 
4403 
4404 void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
4405 {
4406  for (std::set<int>::iterator it = setFilesToPrune.begin(); it != setFilesToPrune.end(); ++it) {
4407  CDiskBlockPos pos(*it, 0);
4408  fs::remove(GetBlockPosFilename(pos, "blk"));
4409  fs::remove(GetBlockPosFilename(pos, "rev"));
4410  LogPrintf("Prune: %s deleted blk/rev (%05u)\n", __func__, *it);
4411  }
4412 }
4413 
4414 /* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
4415 static void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeight)
4416 {
4417  assert(fPruneMode && nManualPruneHeight > 0);
4418 
4419  LOCK2(cs_main, cs_LastBlockFile);
4420  if (chainActive.Tip() == nullptr)
4421  return;
4422 
4423  // last block to prune is the lesser of (user-specified height, MIN_BLOCKS_TO_KEEP from the tip)
4424  unsigned int nLastBlockWeCanPrune = std::min((unsigned)nManualPruneHeight, chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP);
4425  int count=0;
4426  for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
4427  if (vinfoBlockFile[fileNumber].nSize == 0 || vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
4428  continue;
4429  PruneOneBlockFile(fileNumber);
4430  setFilesToPrune.insert(fileNumber);
4431  count++;
4432  }
4433  LogPrintf("Prune (Manual): prune_height=%d removed %d blk/rev pairs\n", nLastBlockWeCanPrune, count);
4434 }
4435 
4436 /* This function is called from the RPC code for pruneblockchain */
4437 void PruneBlockFilesManual(int nManualPruneHeight)
4438 {
4439  CValidationState state;
4440  const CChainParams& chainparams = Params();
4441  FlushStateToDisk(chainparams, state, FLUSH_STATE_NONE, nManualPruneHeight);
4442 }
4443 
4459 static void FindFilesToPrune(std::set<int>& setFilesToPrune, uint64_t nPruneAfterHeight)
4460 {
4461  LOCK2(cs_main, cs_LastBlockFile);
4462  if (chainActive.Tip() == nullptr || nPruneTarget == 0) {
4463  return;
4464  }
4465  if ((uint64_t)chainActive.Tip()->nHeight <= nPruneAfterHeight) {
4466  return;
4467  }
4468 
4469  unsigned int nLastBlockWeCanPrune = chainActive.Tip()->nHeight - MIN_BLOCKS_TO_KEEP;
4470  uint64_t nCurrentUsage = CalculateCurrentUsage();
4471  // We don't check to prune until after we've allocated new space for files
4472  // So we should leave a buffer under our target to account for another allocation
4473  // before the next pruning.
4474  uint64_t nBuffer = BLOCKFILE_CHUNK_SIZE + UNDOFILE_CHUNK_SIZE;
4475  uint64_t nBytesToPrune;
4476  int count=0;
4477 
4478  if (nCurrentUsage + nBuffer >= nPruneTarget) {
4479  for (int fileNumber = 0; fileNumber < nLastBlockFile; fileNumber++) {
4480  nBytesToPrune = vinfoBlockFile[fileNumber].nSize + vinfoBlockFile[fileNumber].nUndoSize;
4481 
4482  if (vinfoBlockFile[fileNumber].nSize == 0)
4483  continue;
4484 
4485  if (nCurrentUsage + nBuffer < nPruneTarget) // are we below our target?
4486  break;
4487 
4488  // don't prune files that could have a block within MIN_BLOCKS_TO_KEEP of the main chain's tip but keep scanning
4489  if (vinfoBlockFile[fileNumber].nHeightLast > nLastBlockWeCanPrune)
4490  continue;
4491 
4492  PruneOneBlockFile(fileNumber);
4493  // Queue up the files for removal
4494  setFilesToPrune.insert(fileNumber);
4495  nCurrentUsage -= nBytesToPrune;
4496  count++;
4497  }
4498  }
4499 
4500  LogPrint(BCLog::PRUNE, "Prune: target=%dMiB actual=%dMiB diff=%dMiB max_prune_height=%d removed %d blk/rev pairs\n",
4501  nPruneTarget/1024/1024, nCurrentUsage/1024/1024,
4502  ((int64_t)nPruneTarget - (int64_t)nCurrentUsage)/1024/1024,
4503  nLastBlockWeCanPrune, count);
4504 }
4505 
4506 bool CheckDiskSpace(uint64_t nAdditionalBytes)
4507 {
4508  uint64_t nFreeBytesAvailable = fs::space(GetDataDir()).available;
4509 
4510  // Check for nMinDiskSpace bytes (currently 50MB)
4511  if (nFreeBytesAvailable < nMinDiskSpace + nAdditionalBytes)
4512  return AbortNode("Disk space is low!", _("Error: Disk space is low!"));
4513 
4514  return true;
4515 }
4516 
4517 static FILE* OpenDiskFile(const CDiskBlockPos &pos, const char *prefix, bool fReadOnly)
4518 {
4519  if (pos.IsNull())
4520  return nullptr;
4521  fs::path path = GetBlockPosFilename(pos, prefix);
4522  fs::create_directories(path.parent_path());
4523  FILE* file = fsbridge::fopen(path, "rb+");
4524  if (!file && !fReadOnly)
4525  file = fsbridge::fopen(path, "wb+");
4526  if (!file) {
4527  LogPrintf("Unable to open file %s\n", path.string());
4528  return nullptr;
4529  }
4530  if (pos.nPos) {
4531  if (fseek(file, pos.nPos, SEEK_SET)) {
4532  LogPrintf("Unable to seek to position %u of %s\n", pos.nPos, path.string());
4533  fclose(file);
4534  return nullptr;
4535  }
4536  }
4537  return file;
4538 }
4539 
4540 FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
4541  return OpenDiskFile(pos, "blk", fReadOnly);
4542 }
4543 
4545 static FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly) {
4546  return OpenDiskFile(pos, "rev", fReadOnly);
4547 }
4548 
4549 fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
4550 {
4551  return GetDataDir() / "blocks" / strprintf("%s%05u.dat", prefix, pos.nFile);
4552 }
4553 
4555 {
4556  if (hash.IsNull())
4557  return nullptr;
4558 
4559  // Return existing
4560  BlockMap::iterator mi = mapBlockIndex.find(hash);
4561  if (mi != mapBlockIndex.end())
4562  return (*mi).second;
4563 
4564  // Create new
4565  CBlockIndex* pindexNew = new CBlockIndex();
4566  mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first;
4567  pindexNew->phashBlock = &((*mi).first);
4568 
4569  return pindexNew;
4570 }
4571 
4572 bool static LoadBlockIndexDB(const CChainParams& chainparams)
4573 {
4574  if (!pblocktree->LoadBlockIndexGuts(chainparams.GetConsensus(), InsertBlockIndex))
4575  return false;
4576 
4577  boost::this_thread::interruption_point();
4578 
4579  // Calculate nChainWork
4580  std::vector<std::pair<int, CBlockIndex*> > vSortedByHeight;
4581  vSortedByHeight.reserve(mapBlockIndex.size());
4582  for (const std::pair<uint256, CBlockIndex*>& item : mapBlockIndex)
4583  {
4584  CBlockIndex* pindex = item.second;
4585  vSortedByHeight.push_back(std::make_pair(pindex->nHeight, pindex));
4586  }
4587  sort(vSortedByHeight.begin(), vSortedByHeight.end());
4588  for (const std::pair<int, CBlockIndex*>& item : vSortedByHeight)
4589  {
4590  CBlockIndex* pindex = item.second;
4591  pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + GetBlockProof(*pindex);
4592  pindex->nTimeMax = (pindex->pprev ? std::max(pindex->pprev->nTimeMax, pindex->nTime) : pindex->nTime);
4593  // We can link the chain of blocks for which we've received transactions at some point.
4594  // Pruned nodes may have deleted the block.
4595  if (pindex->nTx > 0) {
4596  if (pindex->pprev) {
4597  if (pindex->pprev->nChainTx) {
4598  pindex->nChainTx = pindex->pprev->nChainTx + pindex->nTx;
4599  } else {
4600  pindex->nChainTx = 0;
4601  mapBlocksUnlinked.insert(std::make_pair(pindex->pprev, pindex));
4602  }
4603  } else {
4604  pindex->nChainTx = pindex->nTx;
4605  }
4606  }
4607  if (!(pindex->nStatus & BLOCK_FAILED_MASK) && pindex->pprev && (pindex->pprev->nStatus & BLOCK_FAILED_MASK)) {
4608  pindex->nStatus |= BLOCK_FAILED_CHILD;
4609  setDirtyBlockIndex.insert(pindex);
4610  }
4611  if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS) && (pindex->nChainTx || pindex->pprev == nullptr))
4612  setBlockIndexCandidates.insert(pindex);
4613  if (pindex->nStatus & BLOCK_FAILED_MASK && (!pindexBestInvalid || pindex->nChainWork > pindexBestInvalid->nChainWork))
4614  pindexBestInvalid = pindex;
4615  if (pindex->pprev)
4616  pindex->BuildSkip();
4617  if (pindex->IsValid(BLOCK_VALID_TREE) && (pindexBestHeader == nullptr || CBlockIndexWorkComparator()(pindexBestHeader, pindex)))
4618  pindexBestHeader = pindex;
4619  }
4620 
4621  // Load block file info
4622  pblocktree->ReadLastBlockFile(nLastBlockFile);
4623  vinfoBlockFile.resize(nLastBlockFile + 1);
4624  LogPrintf("%s: last block file = %i\n", __func__, nLastBlockFile);
4625  for (int nFile = 0; nFile <= nLastBlockFile; nFile++) {
4626  pblocktree->ReadBlockFileInfo(nFile, vinfoBlockFile[nFile]);
4627  }
4628  LogPrintf("%s: last block file info: %s\n", __func__, vinfoBlockFile[nLastBlockFile].ToString());
4629  for (int nFile = nLastBlockFile + 1; true; nFile++) {
4630  CBlockFileInfo info;
4631  if (pblocktree->ReadBlockFileInfo(nFile, info)) {
4632  vinfoBlockFile.push_back(info);
4633  } else {
4634  break;
4635  }
4636  }
4637 
4638  // Check presence of blk files
4639  LogPrintf("Checking all blk files are present...\n");
4640  std::set<int> setBlkDataFiles;
4641  for (const std::pair<uint256, CBlockIndex*>& item : mapBlockIndex)
4642  {
4643  CBlockIndex* pindex = item.second;
4644  if (pindex->nStatus & BLOCK_HAVE_DATA) {
4645  setBlkDataFiles.insert(pindex->nFile);
4646  }
4647  }
4648  for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++)
4649  {
4650  CDiskBlockPos pos(*it, 0);
4651  if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) {
4652  return false;
4653  }
4654  }
4655 
4656  // Check whether we have ever pruned block & undo files
4657  pblocktree->ReadFlag("prunedblockfiles", fHavePruned);
4658  if (fHavePruned)
4659  LogPrintf("LoadBlockIndexDB(): Block files have previously been pruned\n");
4660 
4661  // Check whether we need to continue reindexing
4662  bool fReindexing = false;
4663  pblocktree->ReadReindexing(fReindexing);
4664  if(fReindexing) fReindex = true;
4665 
4666  // Check whether we have a transaction index
4667  pblocktree->ReadFlag("txindex", fTxIndex);
4668  LogPrintf("%s: transaction index %s\n", __func__, fTxIndex ? "enabled" : "disabled");
4669 
4670  pblocktree->ReadFlag("assetindex", fAssetIndex);
4671  LogPrintf("%s: asset index %s\n", __func__, fAssetIndex ? "enabled" : "disabled");
4672 
4673  // Check whether we have an address index
4674  pblocktree->ReadFlag("addressindex", fAddressIndex);
4675  LogPrintf("%s: address index %s\n", __func__, fAddressIndex ? "enabled" : "disabled");
4676 
4677  // Check whether we have a timestamp index
4678  pblocktree->ReadFlag("timestampindex", fTimestampIndex);
4679  LogPrintf("%s: timestamp index %s\n", __func__, fTimestampIndex ? "enabled" : "disabled");
4680 
4681  // Check whether we have a spent index
4682  pblocktree->ReadFlag("spentindex", fSpentIndex);
4683  LogPrintf("%s: spent index %s\n", __func__, fSpentIndex ? "enabled" : "disabled");
4684  return true;
4685 }
4686 
4687 bool LoadChainTip(const CChainParams& chainparams)
4688 {
4689  if (chainActive.Tip() && chainActive.Tip()->GetBlockHash() == pcoinsTip->GetBestBlock()) return true;
4690 
4691  if (pcoinsTip->GetBestBlock().IsNull() && mapBlockIndex.size() == 1) {
4692  // In case we just added the genesis block, connect it now, so
4693  // that we always have a chainActive.Tip() when we return.
4694  LogPrintf("%s: Connecting genesis block...\n", __func__);
4695  CValidationState state;
4696  if (!ActivateBestChain(state, chainparams)) {
4697  return false;
4698  }
4699  }
4700 
4701  // Load pointer to end of best chain
4702  BlockMap::iterator it = mapBlockIndex.find(pcoinsTip->GetBestBlock());
4703  if (it == mapBlockIndex.end())
4704  return false;
4705  chainActive.SetTip(it->second);
4706 
4707  PruneBlockIndexCandidates();
4708 
4709  LogPrintf("Loaded best chain: hashBestChain=%s height=%d date=%s progress=%f\n",
4710  chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
4711  DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
4712  GuessVerificationProgress(chainparams.TxData(), chainActive.Tip()));
4713  return true;
4714 }
4715 
4717 {
4718  uiInterface.ShowProgress(_("Verifying blocks..."), 0, false);
4719 }
4720 
4722 {
4723  uiInterface.ShowProgress("", 100, false);
4724 }
4725 
4726 bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
4727 {
4728  LOCK(cs_main);
4729  if (chainActive.Tip() == nullptr || chainActive.Tip()->pprev == nullptr)
4730  return true;
4731 
4732  // Verify blocks in the best chain
4733  if (nCheckDepth <= 0 || nCheckDepth > chainActive.Height())
4734  nCheckDepth = chainActive.Height();
4735  nCheckLevel = std::max(0, std::min(4, nCheckLevel));
4736  LogPrintf("Verifying last %i blocks at level %i\n", nCheckDepth, nCheckLevel);
4737  CCoinsViewCache coins(coinsview);
4738  CBlockIndex* pindexState = chainActive.Tip();
4739  CBlockIndex* pindexFailure = nullptr;
4740  int nGoodTransactions = 0;
4741  CValidationState state;
4742  int reportDone = 0;
4743 
4744  auto currentActiveAssetCache = GetCurrentAssetCache();
4745  CAssetsCache assetCache(*currentActiveAssetCache);
4746  LogPrintf("[0%%]...");
4747  for (CBlockIndex* pindex = chainActive.Tip(); pindex && pindex->pprev; pindex = pindex->pprev)
4748  {
4749  boost::this_thread::interruption_point();
4750  int percentageDone = std::max(1, std::min(99, (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * (nCheckLevel >= 4 ? 50 : 100))));
4751  if (reportDone < percentageDone/10) {
4752  // report every 10% step
4753  LogPrintf("[%d%%]...", percentageDone);
4754  reportDone = percentageDone/10;
4755  }
4756  uiInterface.ShowProgress(_("Verifying blocks..."), percentageDone, false);
4757  if (pindex->nHeight < chainActive.Height()-nCheckDepth)
4758  break;
4759  if (fPruneMode && !(pindex->nStatus & BLOCK_HAVE_DATA)) {
4760  // If pruning, only go back as far as we have data.
4761  LogPrintf("VerifyDB(): block verification stopping at height %d (pruning, no data)\n", pindex->nHeight);
4762  break;
4763  }
4764  CBlock block;
4765  // check level 0: read from disk
4766  if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
4767  return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4768  // check level 1: verify block validity
4769  if (nCheckLevel >= 1 && !CheckBlock(block, state, chainparams.GetConsensus(), true, true)) // fCheckAssetDuplicate set to false, because we don't want to fail because the asset exists in our database, when loading blocks from our asset databse
4770  return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
4771  pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
4772  // check level 2: verify undo validity
4773  if (nCheckLevel >= 2 && pindex) {
4774  CBlockUndo undo;
4775  CDiskBlockPos pos = pindex->GetUndoPos();
4776  if (!pos.IsNull()) {
4777  if (!UndoReadFromDisk(undo, pos, pindex->pprev->GetBlockHash()))
4778  return error("VerifyDB(): *** found bad undo data at %d, hash=%s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
4779  }
4780  }
4781  // check level 3: check for inconsistencies during memory-only disconnect of tip blocks
4782  if (nCheckLevel >= 3 && pindex == pindexState && (coins.DynamicMemoryUsage() + pcoinsTip->DynamicMemoryUsage()) <= nCoinCacheUsage) {
4783  assert(coins.GetBestBlock() == pindex->GetBlockHash());
4784  DisconnectResult res = DisconnectBlock(block, pindex, coins, &assetCache, true, false);
4785  if (res == DISCONNECT_FAILED) {
4786  return error("VerifyDB(): *** irrecoverable inconsistency in block data at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4787  }
4788  pindexState = pindex->pprev;
4789  if (res == DISCONNECT_UNCLEAN) {
4790  nGoodTransactions = 0;
4791  pindexFailure = pindex;
4792  } else {
4793  nGoodTransactions += block.vtx.size();
4794  }
4795  }
4796  if (ShutdownRequested())
4797  return true;
4798  }
4799  if (pindexFailure)
4800  return error("VerifyDB(): *** coin database inconsistencies found (last %i blocks, %i good transactions before that)\n", chainActive.Height() - pindexFailure->nHeight + 1, nGoodTransactions);
4801 
4802  // check level 4: try reconnecting blocks
4803  if (nCheckLevel >= 4) {
4804  CBlockIndex *pindex = pindexState;
4805  while (pindex != chainActive.Tip()) {
4806  boost::this_thread::interruption_point();
4807  uiInterface.ShowProgress(_("Verifying blocks..."), std::max(1, std::min(99, 100 - (int)(((double)(chainActive.Height() - pindex->nHeight)) / (double)nCheckDepth * 50))), false);
4808  pindex = chainActive.Next(pindex);
4809  CBlock block;
4810  if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
4811  return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4812  if (!ConnectBlock(block, state, pindex, coins, chainparams, &assetCache, false, true))
4813  return error("VerifyDB(): *** found unconnectable block at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4814  }
4815  }
4816 
4817  LogPrintf("[DONE].\n");
4818  LogPrintf("No coin database inconsistencies in last %i blocks (%i transactions)\n", chainActive.Height() - pindexState->nHeight, nGoodTransactions);
4819 
4820  return true;
4821 }
4822 
4824 static bool RollforwardBlock(const CBlockIndex* pindex, CCoinsViewCache& inputs, const CChainParams& params, CAssetsCache* assetsCache = nullptr)
4825 {
4826  // TODO: merge with ConnectBlock
4827  CBlock block;
4828  if (!ReadBlockFromDisk(block, pindex, params.GetConsensus())) {
4829  return error("ReplayBlock(): ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
4830  }
4831 
4832  for (const CTransactionRef& tx : block.vtx) {
4833  if (!tx->IsCoinBase()) {
4834  for (const CTxIn &txin : tx->vin) {
4835  inputs.SpendCoin(txin.prevout, nullptr, assetsCache);
4836  }
4837  }
4838  // Pass check = true as every addition may be an overwrite.
4839  AddCoins(inputs, *tx, pindex->nHeight, pindex->GetBlockHash(), true, assetsCache);
4840  }
4841  return true;
4842 }
4843 
4844 bool ReplayBlocks(const CChainParams& params, CCoinsView* view)
4845 {
4846  LOCK(cs_main);
4847 
4848  CCoinsViewCache cache(view);
4849  auto currentActiveAssetCache = GetCurrentAssetCache();
4850  CAssetsCache assetsCache(*currentActiveAssetCache);
4851 
4852  std::vector<uint256> hashHeads = view->GetHeadBlocks();
4853  if (hashHeads.empty()) return true; // We're already in a consistent state.
4854  if (hashHeads.size() != 2) return error("ReplayBlocks(): unknown inconsistent state");
4855 
4856  uiInterface.ShowProgress(_("Replaying blocks..."), 0, false);
4857  LogPrintf("Replaying blocks\n");
4858 
4859  const CBlockIndex* pindexOld = nullptr; // Old tip during the interrupted flush.
4860  const CBlockIndex* pindexNew; // New tip during the interrupted flush.
4861  const CBlockIndex* pindexFork = nullptr; // Latest block common to both the old and the new tip.
4862 
4863  if (mapBlockIndex.count(hashHeads[0]) == 0) {
4864  return error("ReplayBlocks(): reorganization to unknown block requested");
4865  }
4866  pindexNew = mapBlockIndex[hashHeads[0]];
4867 
4868  if (!hashHeads[1].IsNull()) { // The old tip is allowed to be 0, indicating it's the first flush.
4869  if (mapBlockIndex.count(hashHeads[1]) == 0) {
4870  return error("ReplayBlocks(): reorganization from unknown block requested");
4871  }
4872  pindexOld = mapBlockIndex[hashHeads[1]];
4873  pindexFork = LastCommonAncestor(pindexOld, pindexNew);
4874  assert(pindexFork != nullptr);
4875  }
4876 
4877  // Rollback along the old branch.
4878  while (pindexOld != pindexFork) {
4879  if (pindexOld->nHeight > 0) { // Never disconnect the genesis block.
4880  CBlock block;
4881  if (!ReadBlockFromDisk(block, pindexOld, params.GetConsensus())) {
4882  return error("RollbackBlock(): ReadBlockFromDisk() failed at %d, hash=%s", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
4883  }
4884  LogPrintf("Rolling back %s (%i)\n", pindexOld->GetBlockHash().ToString(), pindexOld->nHeight);
4885  DisconnectResult res = DisconnectBlock(block, pindexOld, cache, &assetsCache);
4886  if (res == DISCONNECT_FAILED) {
4887  return error("RollbackBlock(): DisconnectBlock failed at %d, hash=%s", pindexOld->nHeight, pindexOld->GetBlockHash().ToString());
4888  }
4889  // If DISCONNECT_UNCLEAN is returned, it means a non-existing UTXO was deleted, or an existing UTXO was
4890  // overwritten. It corresponds to cases where the block-to-be-disconnect never had all its operations
4891  // applied to the UTXO set. However, as both writing a UTXO and deleting a UTXO are idempotent operations,
4892  // the result is still a version of the UTXO set with the effects of that block undone.
4893  }
4894  pindexOld = pindexOld->pprev;
4895  }
4896 
4897  // Roll forward from the forking point to the new tip.
4898  int nForkHeight = pindexFork ? pindexFork->nHeight : 0;
4899  for (int nHeight = nForkHeight + 1; nHeight <= pindexNew->nHeight; ++nHeight) {
4900  const CBlockIndex* pindex = pindexNew->GetAncestor(nHeight);
4901  LogPrintf("Rolling forward %s (%i)\n", pindex->GetBlockHash().ToString(), nHeight);
4902  if (!RollforwardBlock(pindex, cache, params)) return false;
4903  }
4904 
4905  cache.SetBestBlock(pindexNew->GetBlockHash());
4906  cache.Flush();
4907  assetsCache.Flush();
4908  uiInterface.ShowProgress("", 100, false);
4909  return true;
4910 }
4911 
4912 bool RewindBlockIndex(const CChainParams& params)
4913 {
4914  LOCK(cs_main);
4915 
4916  // Note that during -reindex-chainstate we are called with an empty chainActive!
4917 
4918  int nHeight = 1;
4919  while (nHeight <= chainActive.Height()) {
4920  if (IsWitnessEnabled(chainActive[nHeight - 1], params.GetConsensus()) && !(chainActive[nHeight]->nStatus & BLOCK_OPT_WITNESS)) {
4921  break;
4922  }
4923  nHeight++;
4924  }
4925 
4926  // nHeight is now the height of the first insufficiently-validated block, or tipheight + 1
4927  CValidationState state;
4928  CBlockIndex* pindex = chainActive.Tip();
4929  while (chainActive.Height() >= nHeight) {
4930  if (fPruneMode && !(chainActive.Tip()->nStatus & BLOCK_HAVE_DATA)) {
4931  // If pruning, don't try rewinding past the HAVE_DATA point;
4932  // since older blocks can't be served anyway, there's
4933  // no need to walk further, and trying to DisconnectTip()
4934  // will fail (and require a needless reindex/redownload
4935  // of the blockchain).
4936  break;
4937  }
4938  if (!DisconnectTip(state, params, nullptr)) {
4939  return error("RewindBlockIndex: unable to disconnect block at height %i", pindex->nHeight);
4940  }
4941  // Occasionally flush state to disk.
4942  if (!FlushStateToDisk(params, state, FLUSH_STATE_PERIODIC))
4943  return false;
4944  }
4945 
4946  // Reduce validity flag and have-data flags.
4947  // We do this after actual disconnecting, otherwise we'll end up writing the lack of data
4948  // to disk before writing the chainstate, resulting in a failure to continue if interrupted.
4949  for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
4950  CBlockIndex* pindexIter = it->second;
4951 
4952  // Note: If we encounter an insufficiently validated block that
4953  // is on chainActive, it must be because we are a pruning node, and
4954  // this block or some successor doesn't HAVE_DATA, so we were unable to
4955  // rewind all the way. Blocks remaining on chainActive at this point
4956  // must not have their validity reduced.
4957  if (IsWitnessEnabled(pindexIter->pprev, params.GetConsensus()) && !(pindexIter->nStatus & BLOCK_OPT_WITNESS) && !chainActive.Contains(pindexIter)) {
4958  // Reduce validity
4959  pindexIter->nStatus = std::min<unsigned int>(pindexIter->nStatus & BLOCK_VALID_MASK, BLOCK_VALID_TREE) | (pindexIter->nStatus & ~BLOCK_VALID_MASK);
4960  // Remove have-data flags.
4961  pindexIter->nStatus &= ~(BLOCK_HAVE_DATA | BLOCK_HAVE_UNDO);
4962  // Remove storage location.
4963  pindexIter->nFile = 0;
4964  pindexIter->nDataPos = 0;
4965  pindexIter->nUndoPos = 0;
4966  // Remove various other things
4967  pindexIter->nTx = 0;
4968  pindexIter->nChainTx = 0;
4969  pindexIter->nSequenceId = 0;
4970  // Make sure it gets written.
4971  setDirtyBlockIndex.insert(pindexIter);
4972  // Update indexes
4973  setBlockIndexCandidates.erase(pindexIter);
4974  std::pair<std::multimap<CBlockIndex*, CBlockIndex*>::iterator, std::multimap<CBlockIndex*, CBlockIndex*>::iterator> ret = mapBlocksUnlinked.equal_range(pindexIter->pprev);
4975  while (ret.first != ret.second) {
4976  if (ret.first->second == pindexIter) {
4977  mapBlocksUnlinked.erase(ret.first++);
4978  } else {
4979  ++ret.first;
4980  }
4981  }
4982  } else if (pindexIter->IsValid(BLOCK_VALID_TRANSACTIONS) && pindexIter->nChainTx) {
4983  setBlockIndexCandidates.insert(pindexIter);
4984  }
4985  }
4986 
4987  if (chainActive.Tip() != nullptr) {
4988  // We can't prune block index candidates based on our tip if we have
4989  // no tip due to chainActive being empty!
4990  PruneBlockIndexCandidates();
4991 
4992  CheckBlockIndex(params.GetConsensus());
4993 
4994  // FlushStateToDisk can possibly read chainActive. Be conservative
4995  // and skip it here, we're about to -reindex-chainstate anyway, so
4996  // it'll get called a bunch real soon.
4997  if (!FlushStateToDisk(params, state, FLUSH_STATE_ALWAYS)) {
4998  return false;
4999  }
5000  }
5001 
5002  return true;
5003 }
5004 
5005 // May NOT be used after any connections are up as much
5006 // of the peer-processing logic assumes a consistent
5007 // block index state
5009 {
5010  LOCK(cs_main);
5011  setBlockIndexCandidates.clear();
5012  chainActive.SetTip(nullptr);
5013  pindexBestInvalid = nullptr;
5014  pindexBestHeader = nullptr;
5015  mempool.clear();
5016  mapBlocksUnlinked.clear();
5017  vinfoBlockFile.clear();
5018  nLastBlockFile = 0;
5019  nBlockSequenceId = 1;
5020  setDirtyBlockIndex.clear();
5021  g_failed_blocks.clear();
5022  setDirtyFileInfo.clear();
5023  versionbitscache.Clear();
5024  for (int b = 0; b < VERSIONBITS_NUM_BITS; b++) {
5025  warningcache[b].clear();
5026  }
5027 
5028  for (BlockMap::value_type& entry : mapBlockIndex) {
5029  delete entry.second;
5030  }
5031  mapBlockIndex.clear();
5032  fHavePruned = false;
5033 }
5034 
5035 bool LoadBlockIndex(const CChainParams& chainparams)
5036 {
5037  // Load block index from databases
5038  bool needs_init = fReindex;
5039  if (!fReindex) {
5040  bool ret = LoadBlockIndexDB(chainparams);
5041  if (!ret) return false;
5042  needs_init = mapBlockIndex.empty();
5043  }
5044 
5045  if (needs_init) {
5046  // Everything here is for *new* reindex/DBs. Thus, though
5047  // LoadBlockIndexDB may have set fReindex if we shut down
5048  // mid-reindex previously, we don't check fReindex and
5049  // instead only check it prior to LoadBlockIndexDB to set
5050  // needs_init.
5051 
5052  LogPrintf("Initializing databases...\n");
5053 
5054  // Use the provided setting for -txindex in the new database
5055  fTxIndex = gArgs.GetBoolArg("-txindex", DEFAULT_TXINDEX);
5056  pblocktree->WriteFlag("txindex", fTxIndex);
5057  LogPrintf("%s: transaction index %s\n", __func__, fTxIndex ? "enabled" : "disabled");
5058 
5059  // Use the provided setting for -assetindex in the new database
5060  fAssetIndex = gArgs.GetBoolArg("-assetindex", DEFAULT_ASSETINDEX);
5061  pblocktree->WriteFlag("assetindex", fAssetIndex);
5062  LogPrintf("%s: asset index %s\n", __func__, fAssetIndex ? "enabled" : "disabled");
5063 
5064  // Use the provided setting for -addressindex in the new database
5065  fAddressIndex = gArgs.GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX);
5066  pblocktree->WriteFlag("addressindex", fAddressIndex);
5067  LogPrintf("%s: address index %s\n", __func__, fAddressIndex ? "enabled" : "disabled");
5068 
5069  // Use the provided setting for -timestampindex in the new database
5070  fTimestampIndex = gArgs.GetBoolArg("-timestampindex", DEFAULT_TIMESTAMPINDEX);
5071  pblocktree->WriteFlag("timestampindex", fTimestampIndex);
5072  LogPrintf("%s: timestamp index %s\n", __func__, fTimestampIndex ? "enabled" : "disabled");
5073 
5074  // Use the provided setting for -spentindex in the new database
5075  fSpentIndex = gArgs.GetBoolArg("-spentindex", DEFAULT_SPENTINDEX);
5076  pblocktree->WriteFlag("spentindex", fSpentIndex);
5077  LogPrintf("%s: spent index %s\n", __func__, fSpentIndex ? "enabled" : "disabled");
5078 
5079  }
5080  return true;
5081 }
5082 
5083 bool LoadGenesisBlock(const CChainParams& chainparams)
5084 {
5085  LOCK(cs_main);
5086 
5087  // Check whether we're already initialized by checking for genesis in
5088  // mapBlockIndex. Note that we can't use chainActive here, since it is
5089  // set based on the coins db, not the block index db, which is the only
5090  // thing loaded at this point.
5091  if (mapBlockIndex.count(chainparams.GenesisBlock().GetHash()))
5092  return true;
5093 
5094  try {
5095  CBlock &block = const_cast<CBlock&>(chainparams.GenesisBlock());
5096  // Start new block file
5097  unsigned int nBlockSize = ::GetSerializeSize(block, SER_DISK, CLIENT_VERSION);
5098  CDiskBlockPos blockPos;
5099  CValidationState state;
5100  if (!FindBlockPos(state, blockPos, nBlockSize+8, 0, block.GetBlockTime()))
5101  return error("%s: FindBlockPos failed", __func__);
5102  if (!WriteBlockToDisk(block, blockPos, chainparams.MessageStart()))
5103  return error("%s: writing genesis block to disk failed", __func__);
5104  CBlockIndex *pindex = AddToBlockIndex(block);
5105  if (!ReceivedBlockTransactions(block, state, pindex, blockPos, chainparams.GetConsensus()))
5106  return error("%s: genesis block not accepted", __func__);
5107  } catch (const std::runtime_error& e) {
5108  return error("%s: failed to write genesis block: %s", __func__, e.what());
5109  }
5110 
5111  return true;
5112 }
5113 
5114 bool LoadExternalBlockFile(const CChainParams& chainparams, FILE* fileIn, CDiskBlockPos *dbp)
5115 {
5116  // Map of disk positions for blocks with unknown parent (only used for reindex)
5117  static std::multimap<uint256, CDiskBlockPos> mapBlocksUnknownParent;
5118  int64_t nStart = GetTimeMillis();
5119 
5120  int nLoaded = 0;
5121  try {
5122  // This takes over fileIn and calls fclose() on it in the CBufferedFile destructor
5123  CBufferedFile blkdat(fileIn, 2*GetMaxBlockSerializedSize(), GetMaxBlockSerializedSize()+8, SER_DISK, CLIENT_VERSION);
5124  uint64_t nRewind = blkdat.GetPos();
5125  while (!blkdat.eof()) {
5126  boost::this_thread::interruption_point();
5127 
5128  blkdat.SetPos(nRewind);
5129  nRewind++; // start one byte further next time, in case of failure
5130  blkdat.SetLimit(); // remove former limit
5131  unsigned int nSize = 0;
5132  try {
5133  // locate a header
5134  unsigned char buf[CMessageHeader::MESSAGE_START_SIZE];
5135  blkdat.FindByte(chainparams.MessageStart()[0]);
5136  nRewind = blkdat.GetPos()+1;
5137  blkdat >> FLATDATA(buf);
5138  if (memcmp(buf, chainparams.MessageStart(), CMessageHeader::MESSAGE_START_SIZE))
5139  continue;
5140  // read size
5141  blkdat >> nSize;
5142  if (nSize < 80 || nSize > GetMaxBlockSerializedSize())
5143  continue;
5144  } catch (const std::exception&) {
5145  // no valid block header found; don't complain
5146  break;
5147  }
5148  try {
5149  // read block
5150  uint64_t nBlockPos = blkdat.GetPos();
5151  if (dbp)
5152  dbp->nPos = nBlockPos;
5153  blkdat.SetLimit(nBlockPos + nSize);
5154  blkdat.SetPos(nBlockPos);
5155  std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
5156  CBlock& block = *pblock;
5157  blkdat >> block;
5158  nRewind = blkdat.GetPos();
5159 
5160  // detect out of order blocks, and store them for later
5161  uint256 hash = block.GetHash();
5162  if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex.find(block.hashPrevBlock) == mapBlockIndex.end()) {
5163  LogPrint(BCLog::REINDEX, "%s: Out of order block %s, parent %s not known\n", __func__, hash.ToString(),
5164  block.hashPrevBlock.ToString());
5165  if (dbp)
5166  mapBlocksUnknownParent.insert(std::make_pair(block.hashPrevBlock, *dbp));
5167  continue;
5168  }
5169 
5170  // process in case the block isn't known yet
5171  if (mapBlockIndex.count(hash) == 0 || (mapBlockIndex[hash]->nStatus & BLOCK_HAVE_DATA) == 0) {
5172  LOCK(cs_main);
5173  CValidationState state;
5174  if (AcceptBlock(pblock, state, chainparams, nullptr, true, dbp, nullptr)) {
5175  nLoaded++;
5176  }
5177  if (state.IsError())
5178  break;
5179  } else if (hash != chainparams.GetConsensus().hashGenesisBlock && mapBlockIndex[hash]->nHeight % 1000 == 0) {
5180  LogPrint(BCLog::REINDEX, "Block Import: already had block %s at height %d\n", hash.ToString(), mapBlockIndex[hash]->nHeight);
5181  }
5182 
5183  // Activate the genesis block so normal node progress can continue
5184  if (hash == chainparams.GetConsensus().hashGenesisBlock) {
5185  CValidationState state;
5186  if (!ActivateBestChain(state, chainparams)) {
5187  break;
5188  }
5189  }
5190 
5191  NotifyHeaderTip();
5192 
5193  // Recursively process earlier encountered successors of this block
5194  std::deque<uint256> queue;
5195  queue.push_back(hash);
5196  while (!queue.empty()) {
5197  uint256 head = queue.front();
5198  queue.pop_front();
5199  std::pair<std::multimap<uint256, CDiskBlockPos>::iterator, std::multimap<uint256, CDiskBlockPos>::iterator> range = mapBlocksUnknownParent.equal_range(head);
5200  while (range.first != range.second) {
5201  std::multimap<uint256, CDiskBlockPos>::iterator it = range.first;
5202  std::shared_ptr<CBlock> pblockrecursive = std::make_shared<CBlock>();
5203  if (ReadBlockFromDisk(*pblockrecursive, it->second, chainparams.GetConsensus()))
5204  {
5205  LogPrint(BCLog::REINDEX, "%s: Processing out of order child %s of %s\n", __func__, pblockrecursive->GetHash().ToString(),
5206  head.ToString());
5207  LOCK(cs_main);
5208  CValidationState dummy;
5209  if (AcceptBlock(pblockrecursive, dummy, chainparams, nullptr, true, &it->second, nullptr))
5210  {
5211  nLoaded++;
5212  queue.push_back(pblockrecursive->GetHash());
5213  }
5214  }
5215  range.first++;
5216  mapBlocksUnknownParent.erase(it);
5217  NotifyHeaderTip();
5218  }
5219  }
5220  } catch (const std::exception& e) {
5221  LogPrintf("%s: Deserialize or I/O error - %s\n", __func__, e.what());
5222  }
5223  }
5224  } catch (const std::runtime_error& e) {
5225  AbortNode(std::string("System error: ") + e.what());
5226  }
5227  if (nLoaded > 0)
5228  LogPrintf("Loaded %i blocks from external file in %dms\n", nLoaded, GetTimeMillis() - nStart);
5229  return nLoaded > 0;
5230 }
5231 
5232 void static CheckBlockIndex(const Consensus::Params& consensusParams)
5233 {
5234  if (!fCheckBlockIndex) {
5235  return;
5236  }
5237 
5238  LOCK(cs_main);
5239 
5240  // During a reindex, we read the genesis block and call CheckBlockIndex before ActivateBestChain,
5241  // so we have the genesis block in mapBlockIndex but no active chain. (A few of the tests when
5242  // iterating the block tree require that chainActive has been initialized.)
5243  if (chainActive.Height() < 0) {
5244  assert(mapBlockIndex.size() <= 1);
5245  return;
5246  }
5247 
5248  // Build forward-pointing map of the entire block tree.
5249  std::multimap<CBlockIndex*,CBlockIndex*> forward;
5250  for (BlockMap::iterator it = mapBlockIndex.begin(); it != mapBlockIndex.end(); it++) {
5251  forward.insert(std::make_pair(it->second->pprev, it->second));
5252  }
5253 
5254  assert(forward.size() == mapBlockIndex.size());
5255 
5256  std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeGenesis = forward.equal_range(nullptr);
5257  CBlockIndex *pindex = rangeGenesis.first->second;
5258  rangeGenesis.first++;
5259  assert(rangeGenesis.first == rangeGenesis.second); // There is only one index entry with parent nullptr.
5260 
5261  // Iterate over the entire block tree, using depth-first search.
5262  // Along the way, remember whether there are blocks on the path from genesis
5263  // block being explored which are the first to have certain properties.
5264  size_t nNodes = 0;
5265  int nHeight = 0;
5266  CBlockIndex* pindexFirstInvalid = nullptr; // Oldest ancestor of pindex which is invalid.
5267  CBlockIndex* pindexFirstMissing = nullptr; // Oldest ancestor of pindex which does not have BLOCK_HAVE_DATA.
5268  CBlockIndex* pindexFirstNeverProcessed = nullptr; // Oldest ancestor of pindex for which nTx == 0.
5269  CBlockIndex* pindexFirstNotTreeValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TREE (regardless of being valid or not).
5270  CBlockIndex* pindexFirstNotTransactionsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_TRANSACTIONS (regardless of being valid or not).
5271  CBlockIndex* pindexFirstNotChainValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_CHAIN (regardless of being valid or not).
5272  CBlockIndex* pindexFirstNotScriptsValid = nullptr; // Oldest ancestor of pindex which does not have BLOCK_VALID_SCRIPTS (regardless of being valid or not).
5273  while (pindex != nullptr) {
5274  nNodes++;
5275  if (pindexFirstInvalid == nullptr && pindex->nStatus & BLOCK_FAILED_VALID) pindexFirstInvalid = pindex;
5276  if (pindexFirstMissing == nullptr && !(pindex->nStatus & BLOCK_HAVE_DATA)) pindexFirstMissing = pindex;
5277  if (pindexFirstNeverProcessed == nullptr && pindex->nTx == 0) pindexFirstNeverProcessed = pindex;
5278  if (pindex->pprev != nullptr && pindexFirstNotTreeValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TREE) pindexFirstNotTreeValid = pindex;
5279  if (pindex->pprev != nullptr && pindexFirstNotTransactionsValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_TRANSACTIONS) pindexFirstNotTransactionsValid = pindex;
5280  if (pindex->pprev != nullptr && pindexFirstNotChainValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_CHAIN) pindexFirstNotChainValid = pindex;
5281  if (pindex->pprev != nullptr && pindexFirstNotScriptsValid == nullptr && (pindex->nStatus & BLOCK_VALID_MASK) < BLOCK_VALID_SCRIPTS) pindexFirstNotScriptsValid = pindex;
5282 
5283  // Begin: actual consistency checks.
5284  if (pindex->pprev == nullptr) {
5285  // Genesis block checks.
5286  assert(pindex->GetBlockHash() == consensusParams.hashGenesisBlock); // Genesis block's hash must match.
5287  assert(pindex == chainActive.Genesis()); // The current active chain's genesis block must be this block.
5288  }
5289  if (pindex->nChainTx == 0) assert(pindex->nSequenceId <= 0); // nSequenceId can't be set positive for blocks that aren't linked (negative is used for preciousblock)
5290  // VALID_TRANSACTIONS is equivalent to nTx > 0 for all nodes (whether or not pruning has occurred).
5291  // HAVE_DATA is only equivalent to nTx > 0 (or VALID_TRANSACTIONS) if no pruning has occurred.
5292  if (!fHavePruned) {
5293  // If we've never pruned, then HAVE_DATA should be equivalent to nTx > 0
5294  assert(!(pindex->nStatus & BLOCK_HAVE_DATA) == (pindex->nTx == 0));
5295  assert(pindexFirstMissing == pindexFirstNeverProcessed);
5296  } else {
5297  // If we have pruned, then we can only say that HAVE_DATA implies nTx > 0
5298  if (pindex->nStatus & BLOCK_HAVE_DATA) assert(pindex->nTx > 0);
5299  }
5300  if (pindex->nStatus & BLOCK_HAVE_UNDO) assert(pindex->nStatus & BLOCK_HAVE_DATA);
5301  assert(((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TRANSACTIONS) == (pindex->nTx > 0)); // This is pruning-independent.
5302  // All parents having had data (at some point) is equivalent to all parents being VALID_TRANSACTIONS, which is equivalent to nChainTx being set.
5303  assert((pindexFirstNeverProcessed != nullptr) == (pindex->nChainTx == 0)); // nChainTx != 0 is used to signal that all parent blocks have been processed (but may have been pruned).
5304  assert((pindexFirstNotTransactionsValid != nullptr) == (pindex->nChainTx == 0));
5305  assert(pindex->nHeight == nHeight); // nHeight must be consistent.
5306  assert(pindex->pprev == nullptr || pindex->nChainWork >= pindex->pprev->nChainWork); // For every block except the genesis block, the chainwork must be larger than the parent's.
5307  assert(nHeight < 2 || (pindex->pskip && (pindex->pskip->nHeight < nHeight))); // The pskip pointer must point back for all but the first 2 blocks.
5308  assert(pindexFirstNotTreeValid == nullptr); // All mapBlockIndex entries must at least be TREE valid
5309  if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_TREE) assert(pindexFirstNotTreeValid == nullptr); // TREE valid implies all parents are TREE valid
5310  if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_CHAIN) assert(pindexFirstNotChainValid == nullptr); // CHAIN valid implies all parents are CHAIN valid
5311  if ((pindex->nStatus & BLOCK_VALID_MASK) >= BLOCK_VALID_SCRIPTS) assert(pindexFirstNotScriptsValid == nullptr); // SCRIPTS valid implies all parents are SCRIPTS valid
5312  if (pindexFirstInvalid == nullptr) {
5313  // Checks for not-invalid blocks.
5314  assert((pindex->nStatus & BLOCK_FAILED_MASK) == 0); // The failed mask cannot be set for blocks without invalid parents.
5315  }
5316  if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && pindexFirstNeverProcessed == nullptr) {
5317  if (pindexFirstInvalid == nullptr) {
5318  // If this block sorts at least as good as the current tip and
5319  // is valid and we have all data for its parents, it must be in
5320  // setBlockIndexCandidates. chainActive.Tip() must also be there
5321  // even if some data has been pruned.
5322  if (pindexFirstMissing == nullptr || pindex == chainActive.Tip()) {
5323  assert(setBlockIndexCandidates.count(pindex));
5324  }
5325  // If some parent is missing, then it could be that this block was in
5326  // setBlockIndexCandidates but had to be removed because of the missing data.
5327  // In this case it must be in mapBlocksUnlinked -- see test below.
5328  }
5329  } else { // If this block sorts worse than the current tip or some ancestor's block has never been seen, it cannot be in setBlockIndexCandidates.
5330  assert(setBlockIndexCandidates.count(pindex) == 0);
5331  }
5332  // Check whether this block is in mapBlocksUnlinked.
5333  std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangeUnlinked = mapBlocksUnlinked.equal_range(pindex->pprev);
5334  bool foundInUnlinked = false;
5335  while (rangeUnlinked.first != rangeUnlinked.second) {
5336  assert(rangeUnlinked.first->first == pindex->pprev);
5337  if (rangeUnlinked.first->second == pindex) {
5338  foundInUnlinked = true;
5339  break;
5340  }
5341  rangeUnlinked.first++;
5342  }
5343  if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed != nullptr && pindexFirstInvalid == nullptr) {
5344  // If this block has block data available, some parent was never received, and has no invalid parents, it must be in mapBlocksUnlinked.
5345  assert(foundInUnlinked);
5346  }
5347  if (!(pindex->nStatus & BLOCK_HAVE_DATA)) assert(!foundInUnlinked); // Can't be in mapBlocksUnlinked if we don't HAVE_DATA
5348  if (pindexFirstMissing == nullptr) assert(!foundInUnlinked); // We aren't missing data for any parent -- cannot be in mapBlocksUnlinked.
5349  if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed == nullptr && pindexFirstMissing != nullptr) {
5350  // We HAVE_DATA for this block, have received data for all parents at some point, but we're currently missing data for some parent.
5351  assert(fHavePruned); // We must have pruned.
5352  // This block may have entered mapBlocksUnlinked if:
5353  // - it has a descendant that at some point had more work than the
5354  // tip, and
5355  // - we tried switching to that descendant but were missing
5356  // data for some intermediate block between chainActive and the
5357  // tip.
5358  // So if this block is itself better than chainActive.Tip() and it wasn't in
5359  // setBlockIndexCandidates, then it must be in mapBlocksUnlinked.
5360  if (!CBlockIndexWorkComparator()(pindex, chainActive.Tip()) && setBlockIndexCandidates.count(pindex) == 0) {
5361  if (pindexFirstInvalid == nullptr) {
5362  assert(foundInUnlinked);
5363  }
5364  }
5365  }
5366  // assert(pindex->GetBlockHash() == pindex->GetBlockHeader().GetHash()); // Perhaps too slow
5367  // End: actual consistency checks.
5368 
5369  // Try descending into the first subnode.
5370  std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> range = forward.equal_range(pindex);
5371  if (range.first != range.second) {
5372  // A subnode was found.
5373  pindex = range.first->second;
5374  nHeight++;
5375  continue;
5376  }
5377  // This is a leaf node.
5378  // Move upwards until we reach a node of which we have not yet visited the last child.
5379  while (pindex) {
5380  // We are going to either move to a parent or a sibling of pindex.
5381  // If pindex was the first with a certain property, unset the corresponding variable.
5382  if (pindex == pindexFirstInvalid) pindexFirstInvalid = nullptr;
5383  if (pindex == pindexFirstMissing) pindexFirstMissing = nullptr;
5384  if (pindex == pindexFirstNeverProcessed) pindexFirstNeverProcessed = nullptr;
5385  if (pindex == pindexFirstNotTreeValid) pindexFirstNotTreeValid = nullptr;
5386  if (pindex == pindexFirstNotTransactionsValid) pindexFirstNotTransactionsValid = nullptr;
5387  if (pindex == pindexFirstNotChainValid) pindexFirstNotChainValid = nullptr;
5388  if (pindex == pindexFirstNotScriptsValid) pindexFirstNotScriptsValid = nullptr;
5389  // Find our parent.
5390  CBlockIndex* pindexPar = pindex->pprev;
5391  // Find which child we just visited.
5392  std::pair<std::multimap<CBlockIndex*,CBlockIndex*>::iterator,std::multimap<CBlockIndex*,CBlockIndex*>::iterator> rangePar = forward.equal_range(pindexPar);
5393  while (rangePar.first->second != pindex) {
5394  assert(rangePar.first != rangePar.second); // Our parent must have at least the node we're coming from as child.
5395  rangePar.first++;
5396  }
5397  // Proceed to the next one.
5398  rangePar.first++;
5399  if (rangePar.first != rangePar.second) {
5400  // Move to the sibling.
5401  pindex = rangePar.first->second;
5402  break;
5403  } else {
5404  // Move up further.
5405  pindex = pindexPar;
5406  nHeight--;
5407  continue;
5408  }
5409  }
5410  }
5411 
5412  // Check that we actually traversed the entire map.
5413  assert(nNodes == forward.size());
5414 }
5415 
5416 std::string CBlockFileInfo::ToString() const
5417 {
5418  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));
5419 }
5420 
5422 {
5423  LOCK(cs_LastBlockFile);
5424 
5425  return &vinfoBlockFile.at(n);
5426 }
5427 
5429 {
5430  LOCK(cs_main);
5431  return VersionBitsState(chainActive.Tip(), params, pos, versionbitscache);
5432 }
5433 
5435 {
5436  LOCK(cs_main);
5437  return VersionBitsStatistics(chainActive.Tip(), params, pos);
5438 }
5439 
5441 {
5442  LOCK(cs_main);
5443  return VersionBitsStateSinceHeight(chainActive.Tip(), params, pos, versionbitscache);
5444 }
5445 
5446 static const uint64_t MEMPOOL_DUMP_VERSION = 1;
5447 
5448 bool LoadMempool(void)
5449 {
5450  const CChainParams& chainparams = Params();
5451  int64_t nExpiryTimeout = gArgs.GetArg("-mempoolexpiry", DEFAULT_MEMPOOL_EXPIRY) * 60 * 60;
5452  FILE* filestr = fsbridge::fopen(GetDataDir() / "mempool.dat", "rb");
5453  CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
5454  if (file.IsNull()) {
5455  LogPrintf("Failed to open mempool file from disk. Continuing anyway.\n");
5456  return false;
5457  }
5458 
5459  int64_t count = 0;
5460  int64_t expired = 0;
5461  int64_t failed = 0;
5462  int64_t already_there = 0;
5463  int64_t nNow = GetTime();
5464 
5465  try {
5466  uint64_t version;
5467  file >> version;
5468  if (version != MEMPOOL_DUMP_VERSION) {
5469  return false;
5470  }
5471  uint64_t num;
5472  file >> num;
5473  while (num--) {
5474  CTransactionRef tx;
5475  int64_t nTime;
5476  int64_t nFeeDelta;
5477  file >> tx;
5478  file >> nTime;
5479  file >> nFeeDelta;
5480 
5481  CAmount amountdelta = nFeeDelta;
5482  if (amountdelta) {
5483  mempool.PrioritiseTransaction(tx->GetHash(), amountdelta);
5484  }
5485  CValidationState state;
5486  if (nTime + nExpiryTimeout > nNow) {
5487  LOCK(cs_main);
5488  AcceptToMemoryPoolWithTime(chainparams, mempool, state, tx, nullptr /* pfMissingInputs */, nTime,
5489  nullptr /* plTxnReplaced */, false /* bypass_limits */, 0 /* nAbsurdFee */);
5490  if (state.IsValid()) {
5491  ++count;
5492  } else {
5493  // mempool may contain the transaction already, e.g. from
5494  // wallet(s) having loaded it while we were processing
5495  // mempool transactions; consider these as valid, instead of
5496  // failed, but mark them as 'already there'
5497  if (mempool.exists(tx->GetHash())) {
5498  ++already_there;
5499  } else {
5500  ++failed;
5501  }
5502  }
5503  } else {
5504  ++expired;
5505  }
5506  if (ShutdownRequested())
5507  return false;
5508  }
5509  std::map<uint256, CAmount> mapDeltas;
5510  file >> mapDeltas;
5511 
5512  for (const auto& i : mapDeltas) {
5513  mempool.PrioritiseTransaction(i.first, i.second);
5514  }
5515  } catch (const std::exception& e) {
5516  LogPrintf("Failed to deserialize mempool data on disk: %s. Continuing anyway.\n", e.what());
5517  return false;
5518  }
5519 
5520  LogPrintf("Imported mempool transactions from disk: %i succeeded, %i failed, %i expired, %i already there\n", count, failed, expired, already_there);
5521  return true;
5522 }
5523 
5524 bool DumpMempool(void)
5525 {
5526  int64_t start = GetTimeMicros();
5527 
5528  std::map<uint256, CAmount> mapDeltas;
5529  std::vector<TxMempoolInfo> vinfo;
5530 
5531  {
5532  LOCK(mempool.cs);
5533  for (const auto &i : mempool.mapDeltas) {
5534  mapDeltas[i.first] = i.second;
5535  }
5536  vinfo = mempool.infoAll();
5537  }
5538 
5539  int64_t mid = GetTimeMicros();
5540 
5541  try {
5542  FILE* filestr = fsbridge::fopen(GetDataDir() / "mempool.dat.new", "wb");
5543  if (!filestr) {
5544  return false;
5545  }
5546 
5547  CAutoFile file(filestr, SER_DISK, CLIENT_VERSION);
5548 
5549  uint64_t version = MEMPOOL_DUMP_VERSION;
5550  file << version;
5551 
5552  file << (uint64_t)vinfo.size();
5553  for (const auto& i : vinfo) {
5554  file << *(i.tx);
5555  file << (int64_t)i.nTime;
5556  file << (int64_t)i.nFeeDelta;
5557  mapDeltas.erase(i.tx->GetHash());
5558  }
5559 
5560  file << mapDeltas;
5561  FileCommit(file.Get());
5562  file.fclose();
5563  RenameOver(GetDataDir() / "mempool.dat.new", GetDataDir() / "mempool.dat");
5564  int64_t last = GetTimeMicros();
5565  LogPrintf("Dumped mempool: %gs to copy, %gs to dump\n", (mid-start)*MICRO, (last-mid)*MICRO);
5566  } catch (const std::exception& e) {
5567  LogPrintf("Failed to dump mempool: %s. Continuing anyway.\n", e.what());
5568  return false;
5569  }
5570  return true;
5571 }
5572 
5574 double GuessVerificationProgress(const ChainTxData& data, CBlockIndex *pindex) {
5575  if (pindex == nullptr)
5576  return 0.0;
5577 
5578  int64_t nNow = time(nullptr);
5579 
5580  double fTxTotal;
5581 
5582  if (pindex->nChainTx <= data.nTxCount) {
5583  fTxTotal = data.nTxCount + (nNow - data.nTime) * data.dTxRate;
5584  } else {
5585  fTxTotal = pindex->nChainTx + (nNow - pindex->GetBlockTime()) * data.dTxRate;
5586  }
5587 
5588  return pindex->nChainTx / fTxTotal;
5589 }
5590 
5593 
5594  if (fAssetsIsActive)
5595  return true;
5596 
5597  const ThresholdState thresholdState = VersionBitsTipState(Params().GetConsensus(), Consensus::DEPLOYMENT_ASSETS);
5598  if (thresholdState == THRESHOLD_ACTIVE)
5599  fAssetsIsActive = true;
5600 
5601  return fAssetsIsActive;
5602 }
5603 
5605 
5606  if (fMessagesIsActive)
5607  return true;
5608 
5609  const ThresholdState thresholdState = VersionBitsTipState(Params().GetConsensus(), Consensus::DEPLOYMENT_MESSAGING);
5610  if (thresholdState == THRESHOLD_ACTIVE)
5611  fMessagesIsActive = true;
5612 
5613  return fMessagesIsActive;
5614 }
5615 
5617 
5618  if (fRestrictedAssetsIsActive)
5619  return true;
5620 
5621  const ThresholdState thresholdState = VersionBitsTipState(Params().GetConsensus(), Consensus::DEPLOYMENT_RESTRICTED_ASSETS);
5622  if (thresholdState == THRESHOLD_ACTIVE)
5623  fRestrictedAssetsIsActive = true;
5624 
5625  return fRestrictedAssetsIsActive;
5626 }
5627 
5628 bool IsDGWActive(unsigned int nBlockNumber) {
5629  return nBlockNumber >= Params().DGWActivationBlock();
5630 }
5631 
5632 bool IsMessagingActive(unsigned int nBlockNumber) {
5633  if (Params().MessagingActivationBlock()) {
5634  return nBlockNumber > Params().MessagingActivationBlock();
5635  } else {
5636  return AreMessagingDeployed();
5637  }
5638 }
5639 
5640 bool IsRestrictedActive(unsigned int nBlockNumber)
5641 {
5642  if (Params().RestrictedActivationBlock()) {
5643  return nBlockNumber > Params().RestrictedActivationBlock();
5644  } else {
5645  return AreRestrictedAssetsDeployed();
5646  }
5647 }
5648 
5650 {
5651  return passets;
5652 }
5656 {
5657 public:
5660  // block headers
5661  BlockMap::iterator it1 = mapBlockIndex.begin();
5662  for (; it1 != mapBlockIndex.end(); it1++)
5663  delete (*it1).second;
5664  mapBlockIndex.clear();
5665  }
bool QualifierAssetFromTransaction(const CTransaction &tx, CNewAsset &asset, std::string &strAddress)
Definition: assets.cpp:549
void UpdatedBlockTip(const CBlockIndex *, const CBlockIndex *, bool fInitialDownload)
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: chain.h:197
std::set< CAssetCacheRestrictedVerifiers > setNewRestrictedVerifierToAdd
Restricted Assets Verifier Caches.
Definition: assets.h:140
CAmount nValue
Definition: transaction.h:140
const Coin & AccessByTxid(const CCoinsViewCache &view, const uint256 &txid)
Utility function to find any unspent output with a given txid.
Definition: coins.cpp:522
bool ReadTimestampBlockIndex(const uint256 &hash, unsigned int &logicalTS)
Definition: txdb.cpp:438
CTxMemPool & pool
CSHA256 & Write(const unsigned char *data, size_t len)
Definition: sha256.cpp:202
CDiskBlockPos GetBlockPos() const
Definition: chain.h:263
CTxMemPool mempool
bool IsSpent() const
Definition: coins.h:78
int Expire(int64_t time)
Expire all transaction (and their dependencies) in the mempool older than time.
Definition: txmempool.cpp:1295
std::vector< Coin > vprevout
Definition: undo.h:76
std::string ToString() const
Definition: chain.h:325
int64_t EndTime(const Consensus::Params &params) const override
void resize(size_type new_size)
Definition: prevector.h:317
int32_t nSequenceId
(memory only) Sequential id assigned to distinguish order in which blocks are received.
Definition: chain.h:219
bool IsCoinBase() const
Definition: coins.h:57
int nScriptCheckThreads
Definition: validation.cpp:79
void AddCoin(const COutPoint &outpoint, Coin &&coin, bool potential_overwrite)
Add a coin.
Definition: coins.cpp:76
void NotifyEntryRemoved(CTransactionRef txRemoved, MemPoolRemovalReason reason)
bool fPruneMode
True if we&#39;re running in -prune mode.
Definition: validation.cpp:89
bool Flush()
Definition: messagedb.cpp:90
CBlockIndex * pskip
pointer to the index of some further predecessor of this block
Definition: chain.h:182
void addSpentIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view)
Definition: txmempool.cpp:551
uint256 GetRandHash()
Definition: random.cpp:373
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params &consensusParams)
unsigned int nTxOffset
Definition: txdb.h:47
boost::condition_variable CConditionVariable
Just a typedef for boost::condition_variable, can be wrapped later if desired.
Definition: sync.h:104
bool DumpMempool(void)
Dump the mempool to disk.
bool WriteReissuedMempoolState()
Definition: assetdb.cpp:101
bool GetAddressIndex(uint160 addressHash, int type, std::string assetName, std::vector< std::pair< CAddressIndexKey, CAmount > > &addressIndex, int start, int end)
std::pair< int, int64_t > CalculateSequenceLocks(const CTransaction &tx, int flags, std::vector< int > *prevHeights, const CBlockIndex &block)
Calculates the block height and previous block&#39;s median time past at which the transaction will be co...
Definition: tx_verify.cpp:39
std::set< CAssetCacheRestrictedAddress > setNewRestrictedAddressToAdd
Restricted Address Asset Caches.
Definition: assets.h:132
void UnloadBlockIndex()
Unload database information.
bool fAddressIndex
Definition: validation.cpp:85
bool GetTimestampIndex(const unsigned int &high, const unsigned int &low, const bool fActiveOnly, std::vector< std::pair< uint256, unsigned int > > &hashes)
uint256 GetWitnessHash() const
Definition: transaction.cpp:79
void SetNull()
Definition: uint256.h:41
void Add(std::vector< T > &vChecks)
Definition: checkqueue.h:202
int64_t GetBlockTime() const
Definition: chain.h:299
FILE * fopen(const fs::path &p, const char *mode)
Definition: fs.cpp:5
std::string strName
Definition: assettypes.h:239
int Threshold(const Consensus::Params &params) const override
Describes a place in the block chain to another node such that if the other node doesn&#39;t have the sam...
Definition: block.h:132
double dTxRate
Definition: chainparams.h:38
CScript scriptPubKey
Definition: transaction.h:141
descends from failed block
Definition: chain.h:161
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:179
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:1177
bool SpendCoin(const COutPoint &outpoint, Coin *moveto=nullptr, CAssetsCache *assetsCache=nullptr)
Spend a coin.
Definition: coins.cpp:354
uint32_t nStatus
Verification status of this block. See enum BlockStatus.
Definition: chain.h:209
std::map< uint256, std::set< std::string > > mapHashQualifiersChanged
Definition: txmempool.h:485
const Coin & AccessCoin(const COutPoint &output) const
Return a reference to Coin in the cache, or a pruned one if not found.
Definition: coins.cpp:390
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN)
Definition: txmempool.cpp:716
bool Flush()
Push the modifications applied to this cache to its base.
Definition: coins.cpp:475
void FileCommit(FILE *file)
Definition: util.cpp:699
bool IsNewRestrictedAsset() const
Make sure to call VerifyNewAsset if this call returns true.
Definition: assets.cpp:1253
void SetBackend(CCoinsView &viewIn)
Definition: coins.cpp:37
int flags
Definition: raven-tx.cpp:500
int ApplyTxInUndo(Coin &&undo, CCoinsViewCache &view, const COutPoint &out, CAssetsCache *assetCache=nullptr)
Restore the UTXO in a Coin at a given COutPoint.
bool SequenceLocks(const CTransaction &tx, int flags, std::vector< int > *prevHeights, const CBlockIndex &block)
Check if transaction is final per BIP 68 sequence numbers and can be included in a block...
Definition: tx_verify.cpp:111
A UTXO entry.
Definition: coins.h:32
CLRUCache< std::string, int8_t > * passetsQualifierCache
Global variable that points to the asset address qualifier LRU Cache (protected by cs_main) ...
Definition: validation.cpp:236
bool ReadTimestampIndex(const unsigned int &high, const unsigned int &low, const bool fActiveOnly, std::vector< std::pair< uint256, unsigned int > > &vect)
Definition: txdb.cpp:405
Definition: block.h:73
CBlockIndex * pindexBestForkBase
int64_t BeginTime(const Consensus::Params &params) const override
size_t GetTxSize() const
Definition: txmempool.cpp:56
bool LoadGenesisBlock(const CChainParams &chainparams)
Ensures we have a genesis block in the block tree, possibly writing one to disk.
bool WriteBlockUndoAssetData(const uint256 &blockhash, const std::vector< std::pair< std::string, CBlockAssetUndo > > &assetUndoData)
Definition: assetdb.cpp:86
bool ShutdownRequested()
Definition: init.cpp:136
#define strprintf
Definition: tinyformat.h:1054
bool IsPayToScriptHash() const
Definition: script.cpp:221
unsigned int MessagingActivationBlock() const
Definition: chainparams.h:115
unsigned int GetMaxBlockWeight()
Definition: consensus.cpp:8
An in-memory indexed chain of blocks.
Definition: chain.h:437
bool VerifyDB(const CChainParams &chainparams, CCoinsView *coinsview, int nCheckLevel, int nCheckDepth)
bool InvalidateBlock(CValidationState &state, const CChainParams &chainparams, CBlockIndex *pindex)
Mark a block as invalid.
CAmount maxTxFee
Absolute maximum transaction fee (in satoshis) used by wallet and mempool (rejects high fee in sendra...
Definition: validation.cpp:105
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1281
bool GetCoin(const COutPoint &outpoint, Coin &coin) const override
Retrieve the Coin (unspent transaction output) for a given outpoint.
Definition: txmempool.cpp:1265
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
bool fHavePruned
Pruning-related variables and constants.
Definition: validation.cpp:88
unsigned int RestrictedActivationBlock() const
Definition: chainparams.h:116
std::atomic_bool fReindex(false)
reverse_range< T > reverse_iterate(T &x)
CHash256 & Write(const unsigned char *data, size_t len)
Definition: hash.h:88
CRestrictedDB * prestricteddb
Global variable that points to the active restricted asset database (protected by cs_main) ...
Definition: validation.cpp:239
int Period(const Consensus::Params &params) const override
bool ReadLastBlockFile(int &nFile)
Definition: txdb.cpp:176
CWaitableCriticalSection csBestBlock
Definition: validation.cpp:77
std::vector< CTxIn > vin
Definition: transaction.h:391
size_t GetMessageDirtyCacheSize()
Definition: messages.cpp:299
size_t GetSerializeSize(const T &t, int nType, int nVersion=0)
Definition: serialize.h:967
bool Condition(const CBlockIndex *pindex, const Consensus::Params &params) const override
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
Definition: utiltime.cpp:79
const CBlockIndex * LastCommonAncestor(const CBlockIndex *pa, const CBlockIndex *pb)
Find the last common ancestor two blocks have.
Definition: chain.cpp:155
bool ReadBlockUndoAssetData(const uint256 &blockhash, std::vector< std::pair< std::string, CBlockAssetUndo > > &assetUndoData)
Definition: assetdb.cpp:91
const char * prefix
Definition: rest.cpp:568
int height
Definition: txmempool.h:44
bool AssetFromScript(const CScript &scriptPubKey, CNewAsset &assetNew, std::string &strAddress)
Definition: assets.cpp:664
txnouttype type
Definition: wallet.h:193
All parent headers found, difficulty matches, timestamp >= median previous, checkpoint.
Definition: chain.h:136
bool MoneyRange(const CAmount &nValue)
Definition: amount.h:28
bool TransferAssetFromScript(const CScript &scriptPubKey, CAssetTransfer &assetTransfer, std::string &strAddress)
Get specific asset type metadata from the given scripts.
Definition: assets.cpp:638
std::map< uint256, std::set< std::string > > mapHashVerifierChanged
Definition: txmempool.h:489
std::map< uint256, std::set< std::string > > mapHashMarkedGlobalFrozen
Definition: txmempool.h:481
int Height() const
Return the maximal height in the chain.
Definition: chain.h:479
void StartShutdown()
Definition: init.cpp:128
CMessageChannelDB * pmessagechanneldb
Global variable that points to the message channel database (protected by cs_main) ...
Definition: validation.cpp:233
CCriticalSection cs_main
Global state.
Definition: validation.cpp:72
size_t DynamicMemoryUsage() const
Calculate the size of the cache (in bytes)
Definition: coins.cpp:46
bool IsValid() const
Definition: validation.h:69
CTxOut out
unspent transaction output
Definition: coins.h:36
void NewAssetMessage(const CMessage &)
unsigned int GetSizeOfCompactSize(uint64_t nSize)
Compact Size size < 253 – 1 byte size <= USHRT_MAX – 3 bytes (253 + 2 bytes) size <= UINT_MAX – 5 ...
Definition: serialize.h:221
bool WriteTimestampBlockIndex(const CTimestampBlockIndexKey &blockhashIndex, const CTimestampBlockIndexValue &logicalts)
Definition: txdb.cpp:432
bool Flush()
Flush all new cache entries into the passets global cache.
Definition: assets.cpp:2750
#define expect(bit)
bool fTimestampIndex
Definition: validation.cpp:86
cache implements a cache with properties similar to a cuckoo-set
Definition: cuckoocache.h:160
stage after last reached validness failed
Definition: chain.h:160
void clear()
Definition: txmempool.cpp:972
unsigned int fCoinBase
whether containing transaction was a coinbase
Definition: coins.h:39
bool AssetFromTransaction(const CTransaction &tx, CNewAsset &asset, std::string &strAddress)
These types of asset tx, have specific metadata at certain indexes in the transaction.
Definition: assets.cpp:525
bool HaveCoinInCache(const COutPoint &outpoint) const
Check if we have the given utxo already loaded in this cache.
Definition: coins.cpp:404
sph_u32 high
Definition: keccak.c:370
Only first tx is coinbase, 2 <= coinbase input script length <= 100, transactions valid...
Definition: chain.h:143
bool HasNoInputsOf(const CTransaction &tx) const
Check that none of this transactions inputs are in the mempool, and thus the tx is not dependent on o...
Definition: txmempool.cpp:1255
BIP9Stats VersionBitsTipStatistics(const Consensus::Params &params, Consensus::DeploymentPos pos)
Get the numerical statistics for the BIP9 state for a given deployment at the current tip...
arith_uint256 nMinimumChainWork
Minimum work we will assume exists on some valid chain.
Definition: validation.cpp:102
uint256 BlockWitnessMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:169
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:499
std::string strName
Definition: assettypes.h:100
void addTransaction(const CTransactionRef &tx)
Definition: txmempool.h:790
CBlockIndex * Genesis() const
Returns the index entry for the genesis block of this chain, or nullptr if none.
Definition: chain.h:443
const CBlock & GenesisBlock() const
Definition: chainparams.h:66
int64_t GetTimeMicros()
Definition: utiltime.cpp:48
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal...
Definition: txmempool.h:320
CChainParams defines various tweakable parameters of a given instance of the Raven system...
Definition: chainparams.h:48
bool DoS(int level, bool ret=false, unsigned int chRejectCodeIn=0, const std::string &strRejectReasonIn="", bool corruptionIn=false, const std::string &strDebugMessageIn="")
Definition: validation.h:44
uint32_t nTime
Definition: chain.h:214
bool contains(const Element &e, const bool erase) const
Definition: cuckoocache.h:467
undo data available in rev*.dat
Definition: chain.h:157
bool GetBoolArg(const std::string &strArg, bool fDefault) const
Return boolean argument or default value.
Definition: util.cpp:470
bool AssetNullDataFromScript(const CScript &scriptPubKey, CNullAssetTxData &assetData, std::string &strAddress)
Definition: assets.cpp:815
A hasher class for Raven&#39;s 256-bit hash (double SHA-256).
Definition: hash.h:76
std::string ToString() const
Definition: transaction.cpp:14
void Thread()
Worker thread.
Definition: checkqueue.h:138
ThresholdState
Definition: versionbits.h:27
uint32_t VersionBitsMask(const Consensus::Params &params, Consensus::DeploymentPos pos)
int nFile
Which # file this block is stored in (blk?????.dat)
Definition: chain.h:188
RAII-style controller object for a CCheckQueue that guarantees the passed queue is finished before co...
Definition: checkqueue.h:18
void UpdateMempoolForReorg(DisconnectedBlockTransactions &disconnectpool, bool fAddToMempool)
Definition: validation.cpp:426
uint160 Hash160(const T1 pbegin, const T1 pend)
Compute the 160-bit hash an object.
Definition: hash.h:208
std::string strName
Definition: assettypes.h:190
int32_t ComputeBlockVersion(const CBlockIndex *pindexPrev, const Consensus::Params &params)
Determine what nVersion a new block should use.
void TrimToSize(size_t sizelimit, std::vector< COutPoint > *pvNoSpendsRemaining=nullptr)
Remove transactions from the mempool until its dynamic size is <= sizelimit.
Definition: txmempool.cpp:1389
bool ProcessNewBlock(const CChainParams &chainparams, const std::shared_ptr< const CBlock > pblock, bool fForceProcessing, bool *fNewBlock)
Process an incoming block.
bool CSVEnabled() const
CAssetsDB * passetsdb
RVN START.
Definition: validation.cpp:226
bool ReadSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value)
Definition: txdb.cpp:257
int64_t nTime
Definition: chainparams.h:36
bool IsNewQualifierAsset() const
Make sure to call VerifyNewQualifierAsset if this call returns true.
Definition: assets.cpp:1164
const CCheckpointData & Checkpoints() const
Definition: chainparams.h:79
void BlockChecked(const CBlock &, const CValidationState &)
uint64_t nPruneTarget
Number of MiB of block files that we&#39;re trying to stay below.
Definition: validation.cpp:95
std::string asset_name
Definition: assettypes.h:278
unsigned char * begin()
Definition: uint256.h:57
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:436
CBlockIndex * pindexBestForkTip
bool fAssetIndex
Definition: validation.cpp:84
void RenameThread(const char *name)
Definition: util.cpp:849
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight, ConnectedBlockAssetData &connectedBlockData)
Called when a block is connected.
Definition: txmempool.cpp:815
#define FLATDATA(obj)
Definition: serialize.h:366
Reads data from an underlying stream, while hashing the read data.
Definition: hash.h:266
double GuessVerificationProgress(const ChainTxData &data, CBlockIndex *pindex)
Guess how far we are in the verification process at the given block index.
CLRUCache< std::string, int > * pMessageSubscribedChannelsCache
Global variable that points to the subscribed channel LRU Cache (protected by cs_main) ...
Definition: validation.cpp:230
bool IsNull() const
Definition: uint256.h:33
const std::string strMessageMagic
Definition: validation.cpp:117
bool IsCoinBase() const
Definition: transaction.h:360
bool IsNewAsset() const
RVN START.
Definition: assets.cpp:881
bool fIsBareMultisigStd
Definition: validation.cpp:90
uint32_t nTime
Definition: block.h:28
unsigned int nChainTx
(memory only) Number of transactions in the chain up to and including this block. ...
Definition: chain.h:206
bool GetAddressUnspent(uint160 addressHash, int type, std::string assetName, std::vector< std::pair< CAddressUnspentKey, CAddressUnspentValue > > &unspentOutputs)
bool IsRestrictedActive(unsigned int nBlockNumber)
bool fUnitTest
Definition: validation.cpp:99
bool ActivateBestChain(CValidationState &state, const CChainParams &chainparams, std::shared_ptr< const CBlock > pblock)
Make the best chain active, in multiple steps.
indirectmap< COutPoint, const CTransaction * > mapNextTx
Definition: txmempool.h:532
const std::vector< CTxIn > vin
Definition: transaction.h:287
bool IsNull() const
Return true if the wrapped FILE* is nullptr, false otherwise.
Definition: streams.h:501
ThresholdState VersionBitsTipState(const Consensus::Params &params, Consensus::DeploymentPos pos)
Get the BIP9 state for a given deployment at the current tip.
RVN END.
bool IsWitnessStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check if the transaction is over standard P2WSH resources limit: 3600bytes witnessScript size...
Definition: policy.cpp:210
void UpdateTransactionsFromBlock(const std::vector< uint256 > &vHashesToUpdate)
When adding transactions from a disconnected block back to the mempool, new mempool entries may have ...
Definition: txmempool.cpp:111
bool ProcessNewBlockHeaders(const std::vector< CBlockHeader > &headers, CValidationState &state, const CChainParams &chainparams, const CBlockIndex **ppindex, CBlockHeader *first_invalid)
Process incoming block headers.
bool ReadReindexing(bool &fReindexing)
Definition: txdb.cpp:171
CLRUCache< std::string, int8_t > * passetsRestrictionCache
Global variable that points to the asset address restriction LRU Cache (protected by cs_main) ...
Definition: validation.cpp:237
RVN START.
Definition: wallet.h:191
bool IsMessagingActive(unsigned int nBlockNumber)
bool GetAssetData(const CScript &script, CAssetOutputEntry &data)
Definition: assets.cpp:3440
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: txmempool.h:68
void check(const CCoinsViewCache *pcoins) const
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
Definition: txmempool.cpp:993
int nSubsidyHalvingInterval
Definition: params.h:49
indexed_transaction_set mapTx
Definition: txmempool.h:469
const char * ScriptErrorString(const ScriptError serror)
Definition: script_error.cpp:9
bool IsAssetNameAnMsgChannel(const std::string &name)
Check if an asset is a message channel.
Definition: assets.cpp:311
bool nBIP34Enabled
Block height and hash at which BIP34 becomes active.
Definition: params.h:51
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: hash.h:82
int64_t nTxCount
Definition: chainparams.h:37
bool nSegwitEnabled
Definition: params.h:76
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
void SetBestBlock(const uint256 &hashBlock)
Definition: coins.cpp:415
bool fSpentIndex
Definition: validation.cpp:87
uint256 GetBlockHash() const
Definition: chain.h:294
bool AreInputsStandard(const CTransaction &tx, const CCoinsViewCache &mapInputs)
Check transaction inputs to mitigate two potential denial-of-service attacks:
Definition: policy.cpp:176
boost::signals2::signal< void(bool, const CBlockIndex *)> NotifyBlockTip
New block has been accepted.
Definition: ui_interface.h:105
CLRUCache< std::string, CMessage > * pMessagesCache
Global variable that points to the subscribed channel LRU Cache (protected by cs_main) ...
Definition: validation.cpp:229
boost::signals2::signal< void(bool, const CBlockIndex *)> NotifyHeaderTip
Best header has changed.
Definition: ui_interface.h:108
bool IsValid(enum BlockStatus nUpTo=BLOCK_VALID_TRANSACTIONS) const
Check whether this block index entry is valid up to the passed validity level.
Definition: chain.h:334
#define AssertLockHeld(cs)
Definition: sync.h:86
CBlockPolicyEstimator feeEstimator
Definition: validation.cpp:109
uint32_t nHeight
at which height this containing transaction was included in the active block chain ...
Definition: coins.h:42
bool OwnerFromTransaction(const CTransaction &tx, std::string &ownerName, std::string &strAddress)
Definition: assets.cpp:626
Removed in size limiting.
indexed_disconnected_transactions queuedTx
Definition: txmempool.h:781
bool SetLimit(uint64_t nPos=(uint64_t)(-1))
Definition: streams.h:684
bool CheckDiskSpace(uint64_t nAdditionalBytes)
Check whether enough disk space is available for an incoming block.
void fclose()
Definition: streams.h:479
bool AreMessagingDeployed()
unsigned int GetCacheSize() const
Calculate the size of the cache (in number of transaction outputs)
Definition: coins.cpp:491
iterator end()
Definition: prevector.h:293
uint64_t PruneAfterHeight() const
Definition: chainparams.h:71
uint64_t GetPos() const
Definition: streams.h:652
CCoinsViewCache * pcoinsTip
Global variable that points to the active CCoinsView (protected by cs_main)
Definition: validation.cpp:223
bool IsPayToPublicKey() const
RVN END.
Definition: script.cpp:384
#define LOCK2(cs1, cs2)
Definition: sync.h:177
void SetCorruptionPossible()
Definition: validation.h:88
void SetfLargeWorkInvalidChainFound(bool flag)
Definition: warnings.cpp:35
iterator end()
Definition: indirectmap.h:50
bool GlobalAssetNullDataFromScript(const CScript &scriptPubKey, CNullAssetTxData &assetData)
Definition: assets.cpp:840
Outputs do not overspend inputs, no double spends, coinbase output ok, no immature coinbase spends...
Definition: chain.h:147
bool IsAssetNameValid(const std::string &name, AssetType &assetType, std::string &error)
Definition: assets.cpp:207
bool fCheckpointsEnabled
Definition: validation.cpp:93
bool ReplayBlocks(const CChainParams &params, CCoinsView *view)
Replay blocks that aren&#39;t fully applied to the database.
bool EraseAddressIndex(const std::vector< std::pair< CAddressIndexKey, CAmount > > &vect)
Definition: txdb.cpp:347
unsigned int nTimeMax
(memory only) Maximum nTime in the chain up to and including this block.
Definition: chain.h:222
bool ReadAddressUnspentIndex(uint160 addressHash, int type, std::string assetName, std::vector< std::pair< CAddressUnspentKey, CAddressUnspentValue > > &vect)
Definition: txdb.cpp:285
bool TruncateFile(FILE *file, unsigned int length)
Definition: util.cpp:716
bool UpdateAddressUnspentIndex(const std::vector< std::pair< CAddressUnspentKey, CAddressUnspentValue > > &vect)
Definition: txdb.cpp:273
bool IsInitialSyncSpeedUp()
#define LogPrintf(...)
Definition: util.h:149
std::map< uint256, std::string > mapHashToAsset
Definition: txmempool.h:472
bool CheckFinalTx(const CTransaction &tx, int flags)
Transaction validation functions.
Definition: validation.cpp:255
FILE * OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly)
Open a block file (blk?????.dat)
void PruneOneBlockFile(const int fileNumber)
Mark one block file as pruned.
CLRUCache< std::string, CNullAssetTxVerifierString > * passetsVerifierCache
Global variable that points to the asset verifier LRU Cache (protected by cs_main) ...
Definition: validation.cpp:235
bool AreAssetsDeployed()
RVN START.
int GetSpendHeight(const CCoinsViewCache &inputs)
RVN END.
Scripts & signatures ok. Implies all parents are also at least SCRIPTS.
Definition: chain.h:150
Access to the block database (blocks/index/)
Definition: txdb.h:113
bool AcceptToMemoryPool(CTxMemPool &pool, CValidationState &state, const CTransactionRef &tx, bool *pfMissingInputs, std::list< CTransactionRef > *plTxnReplaced, bool bypass_limits, const CAmount nAbsurdFee)
(try to) add transaction to memory pool plTxnReplaced will be appended to with all transactions repla...
uint256 hashMerkleRoot
Definition: block.h:27
bool IsInvalid() const
Definition: validation.h:72
UniValue transfer(const JSONRPCRequest &request)
Definition: assets.cpp:1101
Abstract view on the open txout dataset.
Definition: coins.h:152
void ApplyDelta(const uint256 hash, CAmount &nFeeDelta) const
Definition: txmempool.cpp:1239
CFeeRate minRelayTxFee
A fee rate smaller than this is considered zero fee (for relaying, mining and transaction creation) ...
Definition: validation.cpp:104
CBlockIndex * pindexBestHeader
Best header we&#39;ve seen so far (used for getheaders queries&#39; starting points).
Definition: validation.cpp:76
unsigned int nDataPos
Byte offset within blk?????.dat where this block&#39;s data is stored.
Definition: chain.h:191
DeploymentPos
Definition: params.h:16
CAssetsCache * GetCurrentAssetCache()
void removeForBlock(const std::vector< CTransactionRef > &vtx)
Definition: txmempool.h:797
An input of a transaction.
Definition: transaction.h:67
bool MsgChannelAssetFromTransaction(const CTransaction &tx, CNewAsset &asset, std::string &strAddress)
Definition: assets.cpp:537
ConnectTrace(CTxMemPool &_pool)
int VersionBitsStateSinceHeight(const CBlockIndex *pindexPrev, const Consensus::Params &params, Consensus::DeploymentPos pos, VersionBitsCache &cache)
#define LOCK(cs)
Definition: sync.h:176
void BlockConnected(const std::shared_ptr< const CBlock > &, const CBlockIndex *pindex, const std::vector< CTransactionRef > &)
bool CheckBlock(const CBlock &block, CValidationState &state, const Consensus::Params &consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
Functions for validating blocks and updating the block tree.
We want to be able to estimate feerates that are needed on tx&#39;s to be included in a certain number of...
Definition: fees.h:139
bool CheckTxInputs(const CTransaction &tx, CValidationState &state, const CCoinsViewCache &inputs, int nSpendHeight, CAmount &txfee)
Check whether all inputs of this transaction are valid (no double spends and amounts) This does not m...
Definition: tx_verify.cpp:525
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition: pow.cpp:123
const uint256 & GetHash() const
Definition: transaction.h:320
CMessageDB * pmessagedb
Global variable that points to the messages database (protected by cs_main)
Definition: validation.cpp:232
std::string assetName
Definition: wallet.h:194
bool IsDGWActive(unsigned int nBlockNumber)
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
Definition: chain.h:466
std::map< std::string, uint256 > mapReissuedAssets
Definition: assets.cpp:41
std::map< uint256, CAmount > mapDeltas
Definition: txmempool.h:533
void CalculateDescendants(txiter it, setEntries &setDescendants)
Populate setDescendants with all in-mempool descendants of hash.
Definition: txmempool.cpp:693
CBlockIndex * Next(const CBlockIndex *pindex) const
Find the successor of a block in this chain, or nullptr if the given index is not found or is the tip...
Definition: chain.h:471
Abstract class that implements BIP9-style threshold logic, and caches results.
Definition: versionbits.h:60
uint256 hashPrevBlock
Definition: block.h:26
bool WriteTimestampIndex(const CTimestampIndexKey &timestampIndex)
Definition: txdb.cpp:399
std::string GetRejectReason() const
Definition: validation.h:92
bool ParseAssetScript(CScript scriptPubKey, uint160 &hashBytes, std::string &assetName, CAmount &assetAmount)
Helper method for extracting address bytes, asset name and amount from an asset script.
Definition: assets.cpp:4317
uint32_t n
Definition: transaction.h:26
void Finalize(unsigned char hash[OUTPUT_SIZE])
Definition: sha256.cpp:228
const std::vector< CTxOut > vout
Definition: transaction.h:288
void AllocateFileRange(FILE *file, unsigned int offset, unsigned int length)
this function tries to make a particular range of a file allocated (corresponding to disk space) it i...
Definition: util.cpp:755
void UnlinkPrunedFiles(const std::set< int > &setFilesToPrune)
Actually unlink the specified files.
bool IsInitialBlockDownload()
Check whether we are doing an initial block download (synchronizing from disk or network) ...
std::map< std::string, std::set< uint256 > > mapAssetVerifierChanged
Definition: txmempool.h:488
std::map< std::string, std::set< uint256 > > mapAddressesQualifiersChanged
Definition: txmempool.h:484
std::shared_ptr< const CBlock > pblock
bool EvaluateSequenceLocks(const CBlockIndex &block, std::pair< int, int64_t > lockPair)
Definition: tx_verify.cpp:101
CMainSignals & GetMainSignals()
bool IsError() const
Definition: validation.h:75
virtual std::vector< uint256 > GetHeadBlocks() const
Retrieve the range of blocks that may have been only partially written.
Definition: coins.cpp:22
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:159
CCriticalSection cs_messaging
Definition: messages.cpp:26
void BuildSkip()
Build the skiplist pointer for this entry.
Definition: chain.cpp:115
const CMessageHeader::MessageStartChars & MessageStart() const
Definition: chainparams.h:62
int VersionBitsTipStateSinceHeight(const Consensus::Params &params, Consensus::DeploymentPos pos)
Get the block height at which the BIP9 deployment switched into the state for the block building on t...
bool nBIP65Enabled
Definition: params.h:52
bool exists(uint256 hash) const
Definition: txmempool.h:660
bool TestLockPointValidity(const LockPoints *lp)
Test whether the LockPoints height and time are still valid on the current chain. ...
Definition: validation.cpp:287
std::set< CAssetCacheQualifierAddress > setNewQualifierAddressToAdd
Qualfier Address Asset Caches.
Definition: assets.h:128
bool operator()()
std::string ToString() const
Definition: chain.h:120
bool nCSVEnabled
Definition: params.h:77
An output of a transaction.
Definition: transaction.h:137
CChain chainActive
The currently-connected chain of blocks (protected by cs_main).
Definition: validation.cpp:75
void SetNull()
Definition: block.h:49
bool CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, setEntries &setAncestors, uint64_t limitAncestorCount, uint64_t limitAncestorSize, uint64_t limitDescendantCount, uint64_t limitDescendantSize, std::string &errString, bool fSearchForParents=true) const
Try to calculate all in-mempool ancestors of entry.
Definition: txmempool.cpp:154
std::string ToString() const
Definition: uint256.cpp:63
std::vector< uint256 > vHave
Definition: block.h:134
CBlockIndex * GetLastCheckpoint(const CCheckpointData &data)
Returns last CBlockIndex* in mapBlockIndex that is a checkpoint.
Definition: checkpoints.cpp:19
uint32_t nMinerConfirmationWindow
Definition: params.h:65
bool SetPos(uint64_t nPos)
Definition: streams.h:657
class CMainCleanup instance_of_cmaincleanup
bool IsStandardTx(const CTransaction &tx, std::string &reason, const bool witnessEnabled)
Check for standard transaction types.
Definition: policy.cpp:87
bool GetSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value)
Queue for verifications that have to be performed.
Definition: checkqueue.h:31
Parameters that influence chain consensus.
Definition: params.h:47
void ThreadScriptCheck()
Run an instance of the script checking thread.
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:167
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:22
bool fEnableReplacement
Definition: validation.cpp:97
CScript COINBASE_FLAGS
Constant stuff for coinbase transactions we create:
Definition: validation.cpp:115
int64_t GetBlockTime() const
Definition: block.h:66
bool IsNullAssetVerifierTxDataScript() const
Definition: script.cpp:348
bool IsNull() const
Definition: chain.h:118
CBlockFileInfo * GetBlockFileInfo(size_t n)
Get block file info entry for one block file.
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:356
bool fTxIndex
Definition: validation.cpp:83
CFeeRate GetMinFee(size_t sizelimit) const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
Definition: txmempool.cpp:1357
bool RenameOver(fs::path src, fs::path dest)
Definition: util.cpp:668
CLRUCache< std::string, int8_t > * passetsGlobalRestrictionCache
Global variable that points to the global asset restriction LRU Cache (protected by cs_main) ...
Definition: validation.cpp:238
std::string FormatMoney(const CAmount &n)
Money parsing/formatting utilities.
bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints *lp, bool useExistingLockPoints)
Check if transaction will be BIP 68 final in the next block to be created.
Definition: validation.cpp:305
bool WriteAddressIndex(const std::vector< std::pair< CAddressIndexKey, CAmount > > &vect)
Definition: txdb.cpp:340
int64_t nMaxTipAge
If the tip is older than this (in seconds), the node is considered to be in initial block download...
Definition: validation.cpp:96
256-bit unsigned big integer.
int64_t GetMedianTimePast() const
Definition: chain.h:311
indexed_transaction_set::nth_index< 0 >::type::iterator txiter
Definition: txmempool.h:491
block data in blk*.data was received with a witness-enforcing client
Definition: chain.h:164
CCriticalSection cs
Definition: txmempool.h:468
unsigned int nPos
Definition: chain.h:90
VersionBitsCache versionbitscache
void FlushStateToDisk()
Flush all state, indexes and buffers to disk.
void BlockConnected(CBlockIndex *pindex, std::shared_ptr< const CBlock > pblock)
bool ReadFlag(const std::string &name, bool &fValue)
Definition: txdb.cpp:452
bool WriteBatchSync(const std::vector< std::pair< int, const CBlockFileInfo *> > &fileInfo, int nLastFile, const std::vector< const CBlockIndex *> &blockinfo)
Definition: txdb.cpp:234
uint256 hashAssumeValid
Block hash whose ancestors we will assume to have valid scripts without checking them.
Definition: validation.cpp:101
bool ReadBlockFileInfo(int nFile, CBlockFileInfo &info)
Definition: txdb.cpp:160
unsigned char MessageStartChars[MESSAGE_START_SIZE]
Definition: protocol.h:41
std::map< std::pair< std::string, std::string >, std::set< uint256 > > mapAddressesMarkedFrozen
Restricted assets maps.
Definition: txmempool.h:476
bool ReadTxIndex(const uint256 &txid, CDiskTxPos &pos)
Definition: txdb.cpp:246
CFeeRate minRelayTxFeeV2
A fee rate smaller than this is considered zero fee (for relaying, mining and transaction creation) ...
Definition: validation.cpp:107
std::map< const CBlockIndex *, ThresholdState > ThresholdConditionCache
Definition: versionbits.h:38
bool RestrictedAssetFromTransaction(const CTransaction &tx, CNewAsset &asset, std::string &strAddress)
Definition: assets.cpp:560
Closure representing one script verification Note that this stores references to the spending transac...
Definition: validation.h:398
void insert(Element e)
insert loops at most depth_limit times trying to insert a hash at various locations in the table via ...
Definition: cuckoocache.h:392
bool HashOnchainActive(const uint256 &hash)
bool LoadBlockIndex(const CChainParams &chainparams)
Load the block tree and coins database from disk, initializing state if we&#39;re running with -reindex...
bool AreRestrictedAssetsDeployed()
bool Error(const std::string &strRejectReasonIn)
Definition: validation.h:63
AssetType
Definition: assettypes.h:21
uint32_t setup_bytes(size_t bytes)
setup_bytes is a convenience function which accounts for internal memory usage when deciding how many...
Definition: cuckoocache.h:367
uint256 GetBestBlock() const override
Retrieve the block hash whose state this CCoinsView currently represents.
Definition: coins.cpp:409
std::set< CAssetCacheNewAsset > setNewAssetsToAdd
Definition: assets.h:113
unsigned int nUndoPos
Byte offset within rev?????.dat where this block&#39;s undo data is stored.
Definition: chain.h:194
bool IsScriptTransferAsset(const CScript &scriptPubKey)
Check script and see if it matches the transfer asset template.
Definition: assets.cpp:3202
std::shared_ptr< std::vector< CTransactionRef > > conflictedTxs
#define LogPrint(category,...)
Definition: util.h:160
uint256 GetHash()
Definition: hash.h:250
uint256 GetHash() const
Definition: block.cpp:14
int32_t nVersion
block header
Definition: chain.h:212
RVN END.
Definition: validation.h:30
256-bit opaque blob.
Definition: uint256.h:123
bool fCheckBlockIndex
Definition: validation.cpp:92
bool LoadExternalBlockFile(const CChainParams &chainparams, FILE *fileIn, CDiskBlockPos *dbp)
Import blocks from an external file.
CAssetsCache * passets
Global variable that point to the active assets (protected by cs_main)
Definition: validation.cpp:227
bool HasWitness() const
Definition: transaction.h:377
ArgsManager gArgs
Definition: util.cpp:94
bool RequireStandard() const
Policy: Filter transactions that do not match well-defined patterns.
Definition: chainparams.h:70
std::vector< CTransactionRef > vtx
Definition: block.h:77
FILE * Get() const
Get wrapped FILE* without transfer of ownership.
Definition: streams.h:497
std::string FormatStateMessage(const CValidationState &state)
Convert CValidationState to a human-readable message for logging.
Definition: validation.cpp:393
void SetTip(CBlockIndex *pindex)
Set/initialize a chain with a given tip.
Definition: chain.cpp:12
const ChainTxData & TxData() const
Definition: chainparams.h:80
bool GetTransaction(const uint256 &hash, CTransactionRef &txOut, const Consensus::Params &consensusParams, uint256 &hashBlock, bool fAllowSlow)
Return transaction in txOut, and if it was found inside a block, its hash is placed in hashBlock...
ThresholdState GetStateFor(const CBlockIndex *pindexPrev, const Consensus::Params &params, ThresholdConditionCache &cache) const
Definition: versionbits.cpp:32
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:416
std::map< uint256, std::set< std::pair< std::string, std::string > > > mapHashToAddressMarkedFrozen
Definition: txmempool.h:477
bool fRequireStandard
Definition: validation.cpp:91
std::string EncodeDestination(const CTxDestination &dest)
Definition: base58.cpp:326
Template mixin that adds -Wthread-safety locking annotations to a subset of the mutex API...
Definition: sync.h:55
bool CheckInputs(const CTransaction &tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks, unsigned int flags, bool cacheSigStore, bool cacheFullScriptStore, PrecomputedTransactionData &txdata, std::vector< CScriptCheck > *pvChecks=nullptr)
Check whether all inputs of this transaction are valid (no double spends, scripts & sigs...
unsigned int GetLegacySigOpCount(const CTransaction &tx)
Auxiliary functions for transaction validation (ideally should not be exposed)
Definition: tx_verify.cpp:116
void FindByte(char ch)
Definition: streams.h:699
bool ResetBlockFailureFlags(CBlockIndex *pindex)
Remove invalidity status from a block and its descendants.
void SetNull()
Definition: block.h:101
bool ReissueAssetFromTransaction(const CTransaction &tx, CReissueAsset &reissue, std::string &strAddress)
Definition: assets.cpp:572
std::map< std::string, uint256 > mapAssetToHash
Definition: txmempool.h:471
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:172
const CChainParams & Params()
Return the currently selected parameters.
bool IsNewMsgChannelAsset() const
Make sure to call VerifyNewUniqueAsset if this call returns true.
Definition: assets.cpp:1077
bool IsNullAssetTxDataScript() const
Definition: script.cpp:331
Undo information for a CBlock.
Definition: undo.h:110
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:396
bool fMessaging
Definition: validation.cpp:82
uint32_t nSequence
Definition: transaction.h:72
boost::signals2::signal< void()> NotifyAlertChanged
Status bar alerts changed.
Definition: ui_interface.h:93
RVN START.
Definition: undo.h:72
std::atomic_bool fImporting(false)
void * memcpy(void *a, const void *b, size_t c)
std::vector< PerBlockConnectTrace > blocksConnected
std::vector< unsigned char > GenerateCoinbaseCommitment(CBlock &block, const CBlockIndex *pindexPrev, const Consensus::Params &consensusParams)
Produce the necessary coinbase commitment for a block (modifies the hash, don&#39;t call for mined blocks...
bool GetfLargeWorkForkFound()
Definition: warnings.cpp:29
CCoinsView backed by the coin database (chainstate/)
Definition: txdb.h:71
#define MILLI
Definition: validation.cpp:65
int64_t GetTimeMillis()
Definition: utiltime.cpp:40
CDiskBlockPos GetUndoPos() const
Definition: chain.h:272
void AddMessage(const CMessage &message)
Definition: messages.cpp:149
void InitScriptExecutionCache()
Initializes the script-execution cache.
void Uncache(const COutPoint &outpoint)
Removes the UTXO with the given outpoint from the cache, if it is not modified.
Definition: coins.cpp:482
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: util.cpp:454
void PruneBlockFilesManual(int nManualPruneHeight)
Prune block files up to a given height.
CBlockIndex * FindForkInGlobalIndex(const CChain &chain, const CBlockLocator &locator)
Find the last common block between the parameter chain and a locator.
Definition: validation.cpp:204
int64_t GetAdjustedTime()
Definition: timedata.cpp:36
bool TestBlockValidity(CValidationState &state, const CChainParams &chainparams, const CBlock &block, CBlockIndex *pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
Check a block is completely valid from start to finish (only works on top of our current best block...
void runCommand(const std::string &strCommand)
Definition: util.cpp:841
std::map< uint256, std::string > mapReissuedTx
Definition: assets.cpp:40
bool LoadMempool(void)
Load the mempool from disk.
RVN START.
Definition: standard.h:69
bool PreciousBlock(CValidationState &state, const CChainParams &params, CBlockIndex *pindex)
Mark a block as precious and reorganize.
std::set< CAssetCacheRestrictedGlobal > setNewRestrictedGlobalToAdd
Restricted Global Asset Caches.
Definition: assets.h:136
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:448
std::string message
Definition: assettypes.h:192
std::string GetHex() const
Definition: uint256.cpp:22
sph_u32 low
Definition: keccak.c:370
void OrphanMessage(const COutPoint &out)
Definition: messages.cpp:174
void SetMiscWarning(const std::string &strWarning)
Definition: warnings.cpp:17
Fee rate in satoshis per kilobyte: CAmount / kB.
Definition: feerate.h:20
std::unique_ptr< CConnman > g_connman
Definition: init.cpp:81
160-bit opaque blob.
Definition: uint256.h:112
CBlockLocator GetLocator(const CBlockIndex *pindex=nullptr) const
Return a CBlockLocator that refers to a block in this chain (by default the tip). ...
Definition: chain.cpp:24
unsigned int DGWActivationBlock() const
Definition: chainparams.h:114
void UpdateCoins(const CTransaction &tx, CCoinsViewCache &inputs, CTxUndo &txundo, int nHeight, uint256 blockHash, CAssetsCache *assetCache, std::pair< std::string, CBlockAssetUndo > *undoAssetData)
std::string GetDebugMessage() const
Definition: validation.h:93
bool error(const char *fmt, const Args &... args)
Definition: util.h:168
bool RaiseValidity(enum BlockStatus nUpTo)
Raise the validity level of this block index entry.
Definition: chain.h:344
bool CheckTransaction(const CTransaction &tx, CValidationState &state, bool fCheckDuplicateInputs)
Transaction validation functions.
Definition: tx_verify.cpp:168
iterator begin()
Definition: prevector.h:291
bool IsWitnessEnabled(const CBlockIndex *pindexPrev, const Consensus::Params &params)
Check whether witness commitments are required for block.
bool AssetNullVerifierDataFromScript(const CScript &scriptPubKey, CNullAssetTxVerifierString &verifierData)
Definition: assets.cpp:860
unsigned int GetRejectCode() const
Definition: validation.h:91
bool LoadBlockIndexGuts(const Consensus::Params &consensusParams, std::function< CBlockIndex *(const uint256 &)> insertBlockIndex)
Definition: txdb.cpp:460
CBlockTreeDB * pblocktree
Global variable that points to the active block tree (protected by cs_main)
Definition: validation.cpp:224
boost::signals2::signal< bool(const std::string &message, const std::string &caption, unsigned int style), boost::signals2::last_value< bool > > ThreadSafeMessageBox
Show message box.
Definition: ui_interface.h:76
bool WriteFlag(const std::string &name, bool fValue)
Definition: txdb.cpp:448
bool CheckTxAssets(const CTransaction &tx, CValidationState &state, const CCoinsViewCache &inputs, CAssetsCache *assetCache, bool fCheckMempool, std::vector< std::pair< std::string, uint256 > > &vPairReissueAssets, const bool fRunningUnitTests=false, std::set< CMessage > *setMessages=nullptr, int64_t nBlocktime=0)
RVN START.
Definition: tx_verify.cpp:570
A mutable version of CTransaction.
Definition: transaction.h:389
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:231
int64_t time
Definition: txmempool.h:45
size_type size() const
Definition: prevector.h:283
bool nBIP66Enabled
Definition: params.h:53
uint32_t nRuleChangeActivationThreshold
Block height at which BIP65 becomes active.
Definition: params.h:64
CTransactionRef get(const uint256 &hash) const
Definition: txmempool.cpp:1191
bool CorruptionPossible() const
Definition: validation.h:85
bool getSpentIndex(CSpentIndexKey &key, CSpentIndexValue &value)
Definition: txmempool.cpp:590
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
Check if transaction is final and can be included in a block with the specified height and time...
Definition: tx_verify.cpp:26
All validity bits.
Definition: chain.h:153
void SetBestChain(const CBlockLocator &)
boost::signals2::signal< void(CTransactionRef, MemPoolRemovalReason)> NotifyEntryRemoved
Definition: txmempool.h:673
const fs::path & GetDataDir(bool fNetSpecific)
Definition: util.cpp:572
bool addUnchecked(const uint256 &hash, const CTxMemPoolEntry &entry, bool validFeeEstimate=true)
Definition: txmempool.cpp:1311
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: chain.cpp:121
bool IsReissueAsset() const
Definition: assets.cpp:1384
FlushStateMode
Definition: validation.cpp:241
std::map< std::string, std::set< uint256 > > mapAssetMarkedGlobalFrozen
Definition: txmempool.h:480
int64_t GetTime()
GetTimeMicros() and GetTimeMillis() both return the system time, but in different units...
Definition: utiltime.cpp:20
std::string ToString() const
Definition: feerate.cpp:41
void BlockDisconnected(const std::shared_ptr< const CBlock > &)
CClientUIInterface uiInterface
Definition: ui_interface.cpp:9
size_t DynamicMemoryUsage() const
Definition: txmempool.h:786
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
CLRUCache< std::string, CDatabasedAssetData > * passetsCache
Global variable that point to the assets metadata LRU Cache (protected by cs_main) ...
Definition: validation.cpp:228
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:185
WarningBitsConditionChecker(int bitIn)
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:61
std::unordered_map< uint256, CBlockIndex *, BlockHasher > BlockMap
Definition: validation.h:184
bool ReadBlockFromDisk(CBlock &block, const CDiskBlockPos &pos, const Consensus::Params &consensusParams)
Functions for disk access for blocks.
CCoinsViewDB * pcoinsdbview
Global variable that points to the coins database (protected by cs_main)
Definition: validation.cpp:222
#define MICRO
Definition: validation.cpp:64
BIP9Stats VersionBitsStatistics(const CBlockIndex *pindexPrev, const Consensus::Params &params, Consensus::DeploymentPos pos)
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:208
std::vector< PerBlockConnectTrace > & GetBlocksConnected()
void PruneAndFlush()
Prune block files and flush state to disk.
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:84
bool fChecked
Definition: block.h:80
full block available in blk*.dat
Definition: chain.h:156
DisconnectResult
bool IsAsset() const
Definition: coins.h:82
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, uint256 blockHash, bool check, CAssetsCache *assetsCache, std::pair< std::string, CBlockAssetUndo > *undoAssetData)
Utility function to add all of a transaction&#39;s outputs to a cache.
Definition: coins.cpp:97
CBlockIndex * InsertBlockIndex(uint256 hash)
Create a new block index entry for a given block hash.
unsigned int GetMaxBlockSerializedSize()
Definition: consensus.cpp:19
bool Invalid(bool ret=false, unsigned int _chRejectCode=0, const std::string &_strRejectReason="", const std::string &_strDebugMessage="")
Definition: validation.h:58
CTxDestination destination
Definition: wallet.h:195
Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from...
Definition: streams.h:565
A hasher class for SHA-256.
Definition: sha256.h:14
uint64_t CalculateCurrentUsage()
BLOCK PRUNING CODE.
void addAddressIndex(const CTxMemPoolEntry &entry, const CCoinsViewCache &view)
Definition: txmempool.cpp:421
std::vector< CTxUndo > vtxundo
Definition: undo.h:113
int nFile
Definition: chain.h:89
std::string SanitizeString(const std::string &str, int rule)
Remove unsafe chars.
COutPoint prevout
Definition: transaction.h:70
std::string ToString() const
ThresholdState VersionBitsState(const CBlockIndex *pindexPrev, const Consensus::Params &params, Consensus::DeploymentPos pos, VersionBitsCache &cache)
bool IsNewUniqueAsset() const
Make sure to call VerifyNewUniqueAsset if this call returns true.
Definition: assets.cpp:900
void SetfLargeWorkForkFound(bool flag)
Definition: warnings.cpp:23
Removed for conflict with in-block transaction.
bool UpdateSpentIndex(const std::vector< std::pair< CSpentIndexKey, CSpentIndexValue > > &vect)
Definition: txdb.cpp:261
boost::signals2::signal< void(const std::string &title, int nProgress, bool resume_possible)> ShowProgress
Show progress e.g.
Definition: ui_interface.h:102
CBlockIndex * maxInputBlock
Definition: txmempool.h:49
fs::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix)
Translation to a filesystem path.
bool ReadAddressIndex(uint160 addressHash, int type, std::string assetName, std::vector< std::pair< CAddressIndexKey, CAmount > > &addressIndex, int start=0, int end=0)
Definition: txdb.cpp:354
const CBlockIndex * FindFork(const CBlockIndex *pindex) const
Find the last common block between this chain and a block index entry.
Definition: chain.cpp:52
CCoinsView that brings transactions from a memorypool into view.
Definition: txmempool.h:725
int64_t GetTransactionSigOpCost(const CTransaction &tx, const CCoinsViewCache &inputs, int flags)
Compute total signature operation cost of a transaction.
Definition: tx_verify.cpp:147
bool RewindBlockIndex(const CChainParams &params)
When there are blocks in the active chain with missing data, rewind the chainstate and remove them fr...
double getdouble() const
int32_t nVersion
Definition: block.h:25
CFeeRate incrementalRelayFee
Definition: policy.cpp:262
bool IsPayToPublicKeyHash() const
Definition: script.cpp:210
iterator find(const K &key)
Definition: indirectmap.h:37
void NewPoWValidBlock(const CBlockIndex *, const std::shared_ptr< const CBlock > &)
void PrioritiseTransaction(const uint256 &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:1209
bool IsNullGlobalRestrictionAssetTxDataScript() const
Definition: script.cpp:338
BlockMap mapBlockIndex
Definition: validation.cpp:74
void removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight, int flags)
Definition: txmempool.cpp:748
bool WriteTxIndex(const std::vector< std::pair< uint256, CDiskTxPos > > &vect)
Definition: txdb.cpp:250
void removeEntry(indexed_disconnected_transactions::index< insertion_order >::type::iterator entry)
Definition: txmempool.h:813
Access to the block database (blocks/index/)
Definition: assetdb.h:60
unsigned int nTx
Number of transactions in this block.
Definition: chain.h:201
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:21
bool LoadChainTip(const CChainParams &chainparams)
Update the chain tip based on database information.
uint256 hashGenesisBlock
Definition: params.h:48
void UpdateUncommittedBlockStructures(CBlock &block, const CBlockIndex *pindexPrev, const Consensus::Params &consensusParams)
Update uncommitted block structures (currently: only the witness nonce).
Non-refcounted RAII wrapper for FILE*.
Definition: streams.h:456
size_t nCoinCacheUsage
Definition: validation.cpp:94
CLRUCache< std::string, int > * pMessagesSeenAddressCache
Global variable that points to the address seen LRU Cache (protected by cs_main)
Definition: validation.cpp:231
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result...
Definition: util.h:66
Wrapped boost mutex: supports recursive locking, but no waiting TODO: We should move away from using ...
Definition: sync.h:92
UniValue reissue(const JSONRPCRequest &request)
Definition: assets.cpp:1465
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason=MemPoolRemovalReason::UNKNOWN)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:1287
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params &params)
Return the time it would take to redo the work difference between from and to, assuming the current h...
Definition: chain.cpp:136
uint32_t nBits
Definition: block.h:29
CConditionVariable cvBlockChange
Definition: validation.cpp:78
bool IsAssetNameAnRestricted(const std::string &name)
Check if an asset is a restricted asset.
Definition: assets.cpp:301
bool IsChannelSubscribed(const std::string &name)
Definition: messages.cpp:67
Used to track blocks whose transactions were applied to the UTXO state as a part of a single Activate...
bool IsAssetNameAnOwner(const std::string &name)
Check if an asset is an owner.
Definition: assets.cpp:296
CAmount GetFee(size_t nBytes) const
Return the fee in satoshis for the given size in bytes.
Definition: feerate.cpp:24
bool HaveCoin(const COutPoint &outpoint) const override
Just check whether a given outpoint is unspent.
Definition: coins.cpp:399
bool eof() const
Definition: streams.h:625
bool IsScriptNewUniqueAsset(const CScript &scriptPubKey)
Check script and see if it matches the unquie issuance template.
Definition: assets.cpp:3117
void TransactionAddedToMempool(const CTransactionRef &)
uint256 hash
Definition: transaction.h:25
const uint256 * phashBlock
pointer to the hash of the block, if any. Memory is owned by this CBlockIndex
Definition: chain.h:176
Threshold condition checker that triggers when unknown versionbits are seen on the network...