1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
9 #if defined(HAVE_CONFIG_H)
10 #include "config/bitcoin-config.h"
15 #include "chainparams.h"
17 #include "consensus/consensus.h"
19 #include "primitives/block.h"
20 #include "primitives/transaction.h"
21 #include "script/script.h"
22 #include "script/sigcache.h"
23 #include "script/standard.h"
25 #include "tinyformat.h"
26 #include "txmempool.h"
38 #include <boost/unordered_map.hpp>
45 class CValidationInterface;
46 class CValidationState;
48 struct CNodeStateStats;
50 /** Default for -blockmaxsize and -blockminsize, which control the range of sizes the mining code will create **/
51 static const unsigned int DEFAULT_BLOCK_MAX_SIZE = 750000;
52 static const unsigned int DEFAULT_BLOCK_MIN_SIZE = 0;
53 /** Default for -blockprioritysize, maximum space for zero/low-fee transactions **/
54 static const unsigned int DEFAULT_BLOCK_PRIORITY_SIZE = 50000;
55 /** Default for accepting alerts from the P2P network. */
56 static const bool DEFAULT_ALERTS = true;
57 /** Minimum alert priority for enabling safe mode. */
58 static const int ALERT_PRIORITY_SAFE_MODE = 4000;
59 /** Maximum number of signature check operations in an IsStandard() P2SH script */
60 static const unsigned int MAX_P2SH_SIGOPS = 15;
61 /** The maximum number of sigops we're willing to relay/mine in a single tx */
62 static const unsigned int MAX_STANDARD_TX_SIGOPS = MAX_BLOCK_SIGOPS/5;
63 /** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
64 static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS = 100;
65 /** The maximum size of a blk?????.dat file (since 0.8) */
66 static const unsigned int MAX_BLOCKFILE_SIZE = 0x8000000; // 128 MiB
67 /** The pre-allocation chunk size for blk?????.dat files (since 0.8) */
68 static const unsigned int BLOCKFILE_CHUNK_SIZE = 0x1000000; // 16 MiB
69 /** The pre-allocation chunk size for rev?????.dat files (since 0.8) */
70 static const unsigned int UNDOFILE_CHUNK_SIZE = 0x100000; // 1 MiB
71 /** Maximum number of script-checking threads allowed */
72 static const int MAX_SCRIPTCHECK_THREADS = 16;
73 /** -par default (number of script-checking threads, 0 = auto) */
74 static const int DEFAULT_SCRIPTCHECK_THREADS = 0;
75 /** Number of blocks that can be requested at any given time from a single peer. */
76 static const int MAX_BLOCKS_IN_TRANSIT_PER_PEER = 16;
77 /** Timeout in seconds during which a peer must stall block download progress before being disconnected. */
78 static const unsigned int BLOCK_STALLING_TIMEOUT = 2;
79 /** Number of headers sent in one getheaders result. We rely on the assumption that if a peer sends
80 * less than this number, we reached its tip. Changing this value is a protocol upgrade. */
81 static const unsigned int MAX_HEADERS_RESULTS = 160;
82 /** Size of the "block download window": how far ahead of our current height do we fetch?
83 * Larger windows tolerate larger download speed differences between peer, but increase the potential
84 * degree of disordering of blocks on disk (which make reindexing and in the future perhaps pruning
85 * harder). We'll probably want to make this a per-peer adaptive value at some point. */
86 static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024;
87 /** Time to wait (in seconds) between writing blocks/block index to disk. */
88 static const unsigned int DATABASE_WRITE_INTERVAL = 60 * 60;
89 /** Time to wait (in seconds) between flushing chainstate to disk. */
90 static const unsigned int DATABASE_FLUSH_INTERVAL = 24 * 60 * 60;
91 /** Maximum length of reject messages. */
92 static const unsigned int MAX_REJECT_MESSAGE_LENGTH = 111;
94 #define equihash_parameters_acceptable(N, K) \
95 ((CBlockHeader::HEADER_SIZE + equihash_solution_size(N, K))*MAX_HEADERS_RESULTS < \
96 MAX_PROTOCOL_MESSAGE_LENGTH-1000)
100 size_t operator()(const uint256& hash) const { return hash.GetCheapHash(); }
103 extern CScript COINBASE_FLAGS;
104 extern CCriticalSection cs_main;
105 extern CTxMemPool mempool;
106 typedef boost::unordered_map<uint256, CBlockIndex*, BlockHasher> BlockMap;
107 extern BlockMap mapBlockIndex;
108 extern uint64_t nLastBlockTx;
109 extern uint64_t nLastBlockSize;
110 extern const std::string strMessageMagic;
111 extern CWaitableCriticalSection csBestBlock;
112 extern CConditionVariable cvBlockChange;
113 extern bool fImporting;
114 extern bool fReindex;
115 extern int nScriptCheckThreads;
116 extern bool fTxIndex;
117 extern bool fIsBareMultisigStd;
118 extern bool fCheckBlockIndex;
119 extern bool fCheckpointsEnabled;
120 // TODO: remove this flag by structuring our code such that
121 // it is unneeded for testing
122 extern bool fCoinbaseEnforcedProtectionEnabled;
123 extern size_t nCoinCacheUsage;
124 extern CFeeRate minRelayTxFee;
127 /** Best header we've seen so far (used for getheaders queries' starting points). */
128 extern CBlockIndex *pindexBestHeader;
130 /** Minimum disk space required - used in CheckDiskSpace() */
131 static const uint64_t nMinDiskSpace = 52428800;
133 /** Pruning-related variables and constants */
134 /** True if any block files have ever been pruned. */
135 extern bool fHavePruned;
136 /** True if we're running in -prune mode. */
137 extern bool fPruneMode;
138 /** Number of MiB of block files that we're trying to stay below. */
139 extern uint64_t nPruneTarget;
140 /** Block files containing a block-height within MIN_BLOCKS_TO_KEEP of chainActive.Tip() will not be pruned. */
141 static const signed int MIN_BLOCKS_TO_KEEP = 288;
143 // Require that user allocate at least 550MB for block & undo files (blk???.dat and rev???.dat)
144 // At 1MB per block, 288 blocks = 288MB.
145 // Add 15% for Undo data = 331MB
146 // Add 20% for Orphan block rate = 397MB
147 // We want the low water mark after pruning to be at least 397 MB and since we prune in
148 // full block file chunks, we need the high water mark which triggers the prune to be
149 // one 128MB block file + added 15% undo data = 147MB greater for a total of 545MB
150 // Setting the target to > than 550MB will make it likely we can respect the target.
151 static const signed int MIN_DISK_SPACE_FOR_BLOCK_FILES = 550 * 1024 * 1024;
153 /** Register with a network node to receive its signals */
154 void RegisterNodeSignals(CNodeSignals& nodeSignals);
155 /** Unregister a network node */
156 void UnregisterNodeSignals(CNodeSignals& nodeSignals);
159 * Process an incoming block. This only returns after the best known valid
160 * block is made active. Note that it does not, however, guarantee that the
161 * specific block passed to it has been checked for validity!
163 * @param[out] state This may be set to an Error state if any error occurred processing it, including during validation/connection/etc of otherwise unrelated blocks during reorganisation; or it may be set to an Invalid state if pblock is itself invalid (but this is not guaranteed even when the block is checked). If you want to *possibly* get feedback on whether pblock is valid, you must also install a CValidationInterface (see validationinterface.h) - this will have its BlockChecked method called whenever *any* block completes validation.
164 * @param[in] pfrom The node which we are receiving the block from; it is added to mapBlockSource and may be penalised if the block is invalid.
165 * @param[in] pblock The block we want to process.
166 * @param[in] fForceProcessing Process this block even if unrequested; used for non-network block sources and whitelisted peers.
167 * @param[out] dbp If pblock is stored to disk (or already there), this will be set to its location.
168 * @return True if state.IsValid()
170 bool ProcessNewBlock(int32_t height,CValidationState &state, CNode* pfrom, CBlock* pblock, bool fForceProcessing, CDiskBlockPos *dbp);
171 /** Check whether enough disk space is available for an incoming block */
172 bool CheckDiskSpace(uint64_t nAdditionalBytes = 0);
173 /** Open a block file (blk?????.dat) */
174 FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly = false);
175 /** Open an undo file (rev?????.dat) */
176 FILE* OpenUndoFile(const CDiskBlockPos &pos, bool fReadOnly = false);
177 /** Translation to a filesystem path */
178 boost::filesystem::path GetBlockPosFilename(const CDiskBlockPos &pos, const char *prefix);
179 /** Import blocks from an external file */
180 bool LoadExternalBlockFile(FILE* fileIn, CDiskBlockPos *dbp = NULL);
181 /** Initialize a new block tree database + block data on disk */
182 bool InitBlockIndex();
183 /** Load the block tree and coins database from disk */
184 bool LoadBlockIndex();
185 /** Unload database information */
186 void UnloadBlockIndex();
187 /** Process protocol messages received from a given node */
188 bool ProcessMessages(CNode* pfrom);
190 * Send queued protocol messages to be sent to a give node.
192 * @param[in] pto The node which we are sending messages to.
193 * @param[in] fSendTrickle When true send the trickled data, otherwise trickle the data until true.
195 bool SendMessages(CNode* pto, bool fSendTrickle);
196 /** Run an instance of the script checking thread */
197 void ThreadScriptCheck();
198 /** Try to detect Partition (network isolation) attacks against us */
199 void PartitionCheck(bool (*initialDownloadCheck)(), CCriticalSection& cs, const CBlockIndex *const &bestHeader, int64_t nPowTargetSpacing);
200 /** Check whether we are doing an initial block download (synchronizing from disk or network) */
201 bool IsInitialBlockDownload();
202 /** Format a string that describes several potential problems detected by the core */
203 std::string GetWarnings(std::string strFor);
204 /** Retrieve a transaction (from memory pool, or from disk, if possible) */
205 bool GetTransaction(const uint256 &hash, CTransaction &tx, uint256 &hashBlock, bool fAllowSlow = false);
206 /** Find the best known block, and make it the tip of the block chain */
207 bool ActivateBestChain(CValidationState &state, CBlock *pblock = NULL);
208 CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
211 * Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a user-defined target.
212 * The user sets the target (in MB) on the command line or in config file. This will be run on startup and whenever new
213 * space is allocated in a block or undo file, staying below the target. Changing back to unpruned requires a reindex
214 * (which in this case means the blockchain must be re-downloaded.)
216 * Pruning functions are called from FlushStateToDisk when the global fCheckForPruning flag has been set.
217 * Block and undo files are deleted in lock-step (when blk00003.dat is deleted, so is rev00003.dat.)
218 * Pruning cannot take place until the longest chain is at least a certain length (100000 on mainnet, 1000 on testnet, 10 on regtest).
219 * Pruning will never delete a block within a defined distance (currently 288) from the active chain's tip.
220 * The block index is updated by unsetting HAVE_DATA and HAVE_UNDO for any blocks that were stored in the deleted files.
221 * A db flag records the fact that at least some block files have been pruned.
223 * @param[out] setFilesToPrune The set of file indices that can be unlinked will be returned
225 void FindFilesToPrune(std::set<int>& setFilesToPrune);
228 * Actually unlink the specified files
230 void UnlinkPrunedFiles(std::set<int>& setFilesToPrune);
232 /** Create a new block index entry for a given block hash */
233 CBlockIndex * InsertBlockIndex(uint256 hash);
234 /** Get statistics from node state */
235 bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
236 /** Increase a node's misbehavior score. */
237 void Misbehaving(NodeId nodeid, int howmuch);
238 /** Flush all state, indexes and buffers to disk. */
239 void FlushStateToDisk();
240 /** Prune block files and flush state to disk. */
241 void PruneAndFlush();
243 /** (try to) add transaction to memory pool **/
244 bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,
245 bool* pfMissingInputs, bool fRejectAbsurdFee=false);
248 struct CNodeStateStats {
252 std::vector<int> vHeightInFlight;
255 struct CDiskTxPos : public CDiskBlockPos
257 unsigned int nTxOffset; // after header
259 ADD_SERIALIZE_METHODS;
261 template <typename Stream, typename Operation>
262 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
263 READWRITE(*(CDiskBlockPos*)this);
264 READWRITE(VARINT(nTxOffset));
267 CDiskTxPos(const CDiskBlockPos &blockIn, unsigned int nTxOffsetIn) : CDiskBlockPos(blockIn.nFile, blockIn.nPos), nTxOffset(nTxOffsetIn) {
275 CDiskBlockPos::SetNull();
281 CAmount GetMinRelayFee(const CTransaction& tx, unsigned int nBytes, bool fAllowFree);
284 * Check transaction inputs, and make sure any
285 * pay-to-script-hash transactions are evaluating IsStandard scripts
287 * Why bother? To avoid denial-of-service attacks; an attacker
288 * can submit a standard HASH... OP_EQUAL transaction,
289 * which will get accepted into blocks. The redemption
290 * script can be anything; an attacker could use a very
291 * expensive-to-check-upon-redemption script like:
292 * DUP CHECKSIG DROP ... repeated 100 times... OP_1
296 * Check for standard transaction types
297 * @param[in] mapInputs Map of previous transactions that have outputs we're spending
298 * @return True if all inputs (scriptSigs) use only standard transaction forms
300 bool AreInputsStandard(const CTransaction& tx, const CCoinsViewCache& mapInputs);
303 * Count ECDSA signature operations the old-fashioned (pre-0.6) way
304 * @return number of sigops this transaction's outputs will produce when spent
305 * @see CTransaction::FetchInputs
307 unsigned int GetLegacySigOpCount(const CTransaction& tx);
310 * Count ECDSA signature operations in pay-to-script-hash inputs.
312 * @param[in] mapInputs Map of previous transactions that have outputs we're spending
313 * @return maximum number of sigops required to validate this transaction's inputs
314 * @see CTransaction::FetchInputs
316 unsigned int GetP2SHSigOpCount(const CTransaction& tx, const CCoinsViewCache& mapInputs);
320 * Check whether all inputs of this transaction are valid (no double spends, scripts & sigs, amounts)
321 * This does not modify the UTXO set. If pvChecks is not NULL, script checks are pushed onto it
322 * instead of being performed inline.
324 bool ContextualCheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &view, bool fScriptChecks,
325 unsigned int flags, bool cacheStore, const Consensus::Params& consensusParams,
326 std::vector<CScriptCheck> *pvChecks = NULL);
328 bool NonContextualCheckInputs(const CTransaction& tx, CValidationState &state, const CCoinsViewCache &view, bool fScriptChecks,
329 unsigned int flags, bool cacheStore, const Consensus::Params& consensusParams,
330 std::vector<CScriptCheck> *pvChecks = NULL);
332 /** Apply the effects of this transaction on the UTXO set represented by view */
333 void UpdateCoins(const CTransaction& tx, CValidationState &state, CCoinsViewCache &inputs, int nHeight);
335 /** Context-independent validity checks */
336 bool CheckTransaction(const CTransaction& tx, CValidationState& state);
337 bool CheckTransactionWithoutProofVerification(const CTransaction& tx, CValidationState &state);
339 /** Check for standard transaction types
340 * @return True if all outputs (scriptPubKeys) use only standard transaction forms
342 bool IsStandardTx(const CTransaction& tx, std::string& reason);
345 * Check if transaction is final and can be included in a block with the
346 * specified height and time. Consensus critical.
348 bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime);
351 * Check if transaction will be final in the next block to be created.
353 * Calls IsFinalTx() with current block height and appropriate block time.
355 * See consensus/consensus.h for flag definitions.
357 bool CheckFinalTx(const CTransaction &tx, int flags = -1);
360 * Closure representing one script verification
361 * Note that this stores references to the spending transaction
366 CScript scriptPubKey;
367 const CTransaction *ptxTo;
374 CScriptCheck(): ptxTo(0), nIn(0), nFlags(0), cacheStore(false), error(SCRIPT_ERR_UNKNOWN_ERROR) {}
375 CScriptCheck(const CCoins& txFromIn, const CTransaction& txToIn, unsigned int nInIn, unsigned int nFlagsIn, bool cacheIn) :
376 scriptPubKey(txFromIn.vout[txToIn.vin[nInIn].prevout.n].scriptPubKey),
377 ptxTo(&txToIn), nIn(nInIn), nFlags(nFlagsIn), cacheStore(cacheIn), error(SCRIPT_ERR_UNKNOWN_ERROR) { }
381 void swap(CScriptCheck &check) {
382 scriptPubKey.swap(check.scriptPubKey);
383 std::swap(ptxTo, check.ptxTo);
384 std::swap(nIn, check.nIn);
385 std::swap(nFlags, check.nFlags);
386 std::swap(cacheStore, check.cacheStore);
387 std::swap(error, check.error);
390 ScriptError GetScriptError() const { return error; }
394 /** Functions for disk access for blocks */
395 bool WriteBlockToDisk(CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart);
396 bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos);
397 bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex);
400 /** Functions for validating blocks and updating the block tree */
402 /** Undo the effects of this block (with given index) on the UTXO set represented by coins.
403 * In case pfClean is provided, operation will try to be tolerant about errors, and *pfClean
404 * will be true if no problems were found. Otherwise, the return value will be false in case
405 * of problems. Note that in any case, coins may be modified. */
406 bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& coins, bool* pfClean = NULL);
408 /** Apply the effects of this block (with given index) on the UTXO set represented by coins */
409 bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& coins, bool fJustCheck = false);
411 /** Context-independent validity checks */
412 bool CheckBlockHeader(int32_t height,CBlockIndex *pindex,const CBlockHeader& blockhdr, CValidationState& state, bool fCheckPOW = true);
413 bool CheckBlock(int32_t height,CBlockIndex *pindex,const CBlock& block, CValidationState& state, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
415 /** Context-dependent validity checks */
416 bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex *pindexPrev);
417 bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex *pindexPrev);
419 /** Check a block is completely valid from start to finish (only works on top of our current best block, with cs_main held) */
420 bool TestBlockValidity(CValidationState &state, const CBlock& block, CBlockIndex *pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
422 /** Store block on disk. If dbp is non-NULL, the file is known to already reside on disk */
423 bool AcceptBlock(CBlock& block, CValidationState& state, CBlockIndex **pindex, bool fRequested, CDiskBlockPos* dbp);
424 bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state, CBlockIndex **ppindex= NULL);
431 unsigned int nBlocks; //! number of blocks stored in file
432 unsigned int nSize; //! number of used bytes of block file
433 unsigned int nUndoSize; //! number of used bytes in the undo file
434 unsigned int nHeightFirst; //! lowest height of block in file
435 unsigned int nHeightLast; //! highest height of block in file
436 uint64_t nTimeFirst; //! earliest time of block in file
437 uint64_t nTimeLast; //! latest time of block in file
439 ADD_SERIALIZE_METHODS;
441 template <typename Stream, typename Operation>
442 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) {
443 READWRITE(VARINT(nBlocks));
444 READWRITE(VARINT(nSize));
445 READWRITE(VARINT(nUndoSize));
446 READWRITE(VARINT(nHeightFirst));
447 READWRITE(VARINT(nHeightLast));
448 READWRITE(VARINT(nTimeFirst));
449 READWRITE(VARINT(nTimeLast));
466 std::string ToString() const;
468 /** update statistics (does not update nSize) */
469 void AddBlock(unsigned int nHeightIn, uint64_t nTimeIn) {
470 if (nBlocks==0 || nHeightFirst > nHeightIn)
471 nHeightFirst = nHeightIn;
472 if (nBlocks==0 || nTimeFirst > nTimeIn)
473 nTimeFirst = nTimeIn;
475 if (nHeightIn > nHeightLast)
476 nHeightLast = nHeightIn;
477 if (nTimeIn > nTimeLast)
482 /** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */
487 bool VerifyDB(CCoinsView *coinsview, int nCheckLevel, int nCheckDepth);
490 /** Find the last common block between the parameter chain and a locator. */
491 CBlockIndex* FindForkInGlobalIndex(const CChain& chain, const CBlockLocator& locator);
493 /** Mark a block as invalid. */
494 bool InvalidateBlock(CValidationState& state, CBlockIndex *pindex);
496 /** Remove invalidity status from a block and its descendants. */
497 bool ReconsiderBlock(CValidationState& state, CBlockIndex *pindex);
499 /** The currently-connected chain of blocks. */
500 extern CChain chainActive;
502 /** Global variable that points to the active CCoinsView (protected by cs_main) */
503 extern CCoinsViewCache *pcoinsTip;
505 /** Global variable that points to the active block tree (protected by cs_main) */
506 extern CBlockTreeDB *pblocktree;
508 #endif // BITCOIN_MAIN_H