text: // Copyright (c) 2019 The BCD Core developers
  
  #ifndef BITCOINDIAMOND_VALIDATIONCOMMON_H
  #define BITCOINDIAMOND_VALIDATIONCOMMON_H
  #include <arith_uint256.h>
  #include <chain.h>
  #include <chainparams.h>
  #include <checkpoints.h>
  #include <checkqueue.h>
  #include <consensus/consensus.h>
  #include <consensus/merkle.h>
  #include <consensus/tx_verify.h>
  #include <consensus/validation.h>
  #include <cuckoocache.h>
  #include <hash.h>
  #include <index/txindex.h>
  #include <policy/fees.h>
  #include <policy/policy.h>
  #include <policy/rbf.h>
  #include <pow.h>
  #include <primitives/block.h>
  #include <primitives/transaction.h>
  #include <random.h>
  #include <reverse_iterator.h>
  #include <script/script.h>
  #include <script/sigcache.h>
  #include <script/standard.h>
  #include <shutdown.h>
  #include <timedata.h>
  #include <tinyformat.h>
  #include <txdb.h>
  #include <txmempool.h>
  #include <ui_interface.h>
  #include <undo.h>
  #include <util/util.h>
  #include <util/utilmoneystr.h>
  #include <util/utilstrencodings.h>
  #include <validate/validationinterface.h>
  #include <warnings.h>
  
  #include <future>
  #include <sstream>
  
  #include <boost/algorithm/string/replace.hpp>
  #include <boost/thread.hpp>
  
  CCriticalSection cs_main;
  
  
  
  /** Default for -whitelistrelay. */
  static const bool DEFAULT_WHITELISTRELAY = true;
  /** Default for -whitelistforcerelay. */
  static const bool DEFAULT_WHITELISTFORCERELAY = true;
  /** Default for -minrelaytxfee, minimum relay fee for transactions */
  static const unsigned int DEFAULT_MIN_RELAY_TX_FEE = 1000;
  //! -maxtxfee default
  static const CAmount DEFAULT_TRANSACTION_MAXFEE = 0.1 * COIN;
  //! Discourage users to set fees higher than this amount (in satoshis) per kB
  static const CAmount HIGH_TX_FEE_PER_KB = 0.01 * COIN;
  //! -maxtxfee will warn if called with a higher fee than this amount (in satoshis)
  static const CAmount HIGH_MAX_TX_FEE = 100 * HIGH_TX_FEE_PER_KB;
  /** Default for -limitancestorcount, max number of in-mempool ancestors */
  static const unsigned int DEFAULT_ANCESTOR_LIMIT = 25;
  /** Default for -limitancestorsize, maximum kilobytes of tx + all in-mempool ancestors */
  static const unsigned int DEFAULT_ANCESTOR_SIZE_LIMIT = 101;
  /** Default for -limitdescendantcount, max number of in-mempool descendants */
  static const unsigned int DEFAULT_DESCENDANT_LIMIT = 25;
  /** Default for -limitdescendantsize, maximum kilobytes of in-mempool descendants */
  static const unsigned int DEFAULT_DESCENDANT_SIZE_LIMIT = 101;
  /** Default for -mempoolexpiry, expiration time for mempool transactions in hours */
  static const unsigned int DEFAULT_MEMPOOL_EXPIRY = 336;
  /** Maximum kilobytes for transactions to store for processing during reorg */
  static const unsigned int MAX_DISCONNECTED_TX_POOL_SIZE = 20000;
  /** The maximum size of a blk?????.dat file (since 0.8) */
  static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
  /** The pre-allocation chunk size for blk?????.dat files (since 0.8) */
  static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
  /** The pre-allocation chunk size for rev?????.dat files (since 0.8) */
  static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
  
  /** Maximum number of script-checking threads allowed */
  static const int MAX_SCRIPTCHECK_THREADS = 16;
  /** -par default (number of script-checking threads, 0 = auto) */
  static const int DEFAULT_SCRIPTCHECK_THREADS = 0;
  /** Number of blocks that can be requested at any given time from a single peer. */
  static const int MAX_BLOCKS_IN_TRANSIT_PER_PEER = 16;
  /** Timeout in seconds during which a peer must stall block download progress before being disconnected. */
  static const unsigned int BLOCK_STALLING_TIMEOUT = 2;
  /** Number of headers sent in one getheaders result. We rely on the assumption that if a peer sends
   *  less than this number, we reached its tip. Changing this value is a protocol upgrade. */
  static const unsigned int MAX_HEADERS_RESULTS = 2000;
  /** Maximum depth of blocks we're willing to serve as compact blocks to peers
   *  when requested. For older blocks, a regular BLOCK response will be sent. */
  static const int MAX_CMPCTBLOCK_DEPTH = 5;
  /** Maximum depth of blocks we're willing to respond to GETBLOCKTXN requests for. */
  static const int MAX_BLOCKTXN_DEPTH = 10;
  /** Size of the "block download window": how far ahead of our current height do we fetch?
   *  Larger windows tolerate larger download speed differences between peer, but increase the potential
   *  degree of disordering of blocks on disk (which make reindexing and pruning harder). We'll probably
   *  want to make this a per-peer adaptive value at some point. */
  static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024;
  /** Time to wait (in seconds) between writing blocks/block index to disk. */
  static const unsigned int DATABASE_WRITE_INTERVAL = 60 * 60;
  /** Time to wait (in seconds) between flushing chainstate to disk. */
  static const unsigned int DATABASE_FLUSH_INTERVAL = 24 * 60 * 60;
  /** Maximum length of reject messages. */
  static const unsigned int MAX_REJECT_MESSAGE_LENGTH = 111;
  /** Block download timeout base, expressed in millionths of the block interval (i.e. 10 min) */
  static const int64_t BLOCK_DOWNLOAD_TIMEOUT_BASE = 1000000;
  /** Additional block download timeout per parallel downloading peer (i.e. 5 min) */
  static const int64_t BLOCK_DOWNLOAD_TIMEOUT_PER_PEER = 500000;
  
  static const int64_t DEFAULT_MAX_TIP_AGE = 24 * 60 * 60;
  /** Maximum age of our tip in seconds for us to be considered current for fee estimation */
  static const int64_t MAX_FEE_ESTIMATION_TIP_AGE = 3 * 60 * 60;
  
  /** Default for -permitbaremultisig */
  static const bool DEFAULT_PERMIT_BAREMULTISIG = true;
  static const bool DEFAULT_CHECKPOINTS_ENABLED = true;
  static const bool DEFAULT_TXINDEX = false;
  static const unsigned int DEFAULT_BANSCORE_THRESHOLD = 100;
  /** Default for -persistmempool */
  static const bool DEFAULT_PERSIST_MEMPOOL = true;
  /** Default for -mempoolreplacement */
  static const bool DEFAULT_ENABLE_REPLACEMENT = true;
  /** Default for using fee filter */
  static const bool DEFAULT_FEEFILTER = true;
  
  /** Maximum number of headers to announce when relaying blocks with headers message.*/
  static const unsigned int MAX_BLOCKS_TO_ANNOUNCE = 8;
  
  /** Maximum number of unconnecting headers announcements before DoS score */
  static const int MAX_UNCONNECTING_HEADERS = 10;
  
  static const bool DEFAULT_PEERBLOOMFILTERS = true;
  
  /** Block files containing a block-height within MIN_BLOCKS_TO_KEEP of chainActive.Tip() will not be pruned. */
  static const unsigned int MIN_BLOCKS_TO_KEEP = 288;
  
  /** Default for -stopatheight */
  static const int DEFAULT_STOPATHEIGHT = 0;
  
  /** True if we're running in -prune mode. */
  extern bool fPruneMode;
  /** Best header we've seen so far (used for getheaders queries' starting points). */
  extern CBlockIndex *pindexBestHeader;
  extern int nScriptCheckThreads;
  /** Reject codes greater or equal to this can be returned by AcceptToMemPool
   * for transactions, to signal internal conditions. They cannot and should not
   * be sent over the P2P network.
   */
  static const unsigned int REJECT_INTERNAL = 0x100;
  /** Too high fee. Can not be triggered by P2P transactions */
  static const unsigned int REJECT_HIGHFEE = 0x100;
  
  /** Minimum disk space required - used in CheckDiskSpace() */
  static const uint64_t nMinDiskSpace = 52428800;
  
  struct BlockHasher
  {
      size_t operator()(const uint256& hash) const { return hash.GetCheapHash(); }
  };
  typedef std::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
  
  enum class FlushStateMode {
      NONE,
      IF_NEEDED,
      PERIODIC,
      ALWAYS
  };
  
  /** Check whether we are doing an initial block download (synchronizing from disk or network) */
  bool IsInitialBlockDownload();
  
  void AlertNotify(const std::string& strMessage);
  
  
  /** Convert CValidationState to a human-readable message for logging */
  std::string FormatStateMessage(const CValidationState &state);
  
  /** Abort with a message */
  bool AbortNode(const std::string& strMessage, const std::string& userMessage="");
  
  bool AbortNode(CValidationState& state, const std::string& strMessage, const std::string& userMessage="");
  
  void NotifyHeaderTip() LOCKS_EXCLUDED(cs_main);
  
  #endif //BITCOINDIAMOND_VALIDATIONCOMMON_H
  