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 /** The maximum size for transactions we're willing to relay/mine */
56 static const unsigned int MAX_STANDARD_TX_SIZE
= 100000;
57 /** Maximum number of signature check operations in an IsStandard() P2SH script */
58 static const unsigned int MAX_P2SH_SIGOPS
= 15;
59 /** The maximum number of sigops we're willing to relay/mine in a single tx */
60 static const unsigned int MAX_STANDARD_TX_SIGOPS
= MAX_BLOCK_SIGOPS
/5;
61 /** Default for -maxorphantx, maximum number of orphan transactions kept in memory */
62 static const unsigned int DEFAULT_MAX_ORPHAN_TRANSACTIONS
= 100;
63 /** The maximum size of a blk?????.dat file (since 0.8) */
64 static const unsigned int MAX_BLOCKFILE_SIZE
= 0x8000000; // 128 MiB
65 /** The pre-allocation chunk size for blk?????.dat files (since 0.8) */
66 static const unsigned int BLOCKFILE_CHUNK_SIZE
= 0x1000000; // 16 MiB
67 /** The pre-allocation chunk size for rev?????.dat files (since 0.8) */
68 static const unsigned int UNDOFILE_CHUNK_SIZE
= 0x100000; // 1 MiB
69 /** Maximum number of script-checking threads allowed */
70 static const int MAX_SCRIPTCHECK_THREADS
= 16;
71 /** -par default (number of script-checking threads, 0 = auto) */
72 static const int DEFAULT_SCRIPTCHECK_THREADS
= 0;
73 /** Number of blocks that can be requested at any given time from a single peer. */
74 static const int MAX_BLOCKS_IN_TRANSIT_PER_PEER
= 16;
75 /** Timeout in seconds during which a peer must stall block download progress before being disconnected. */
76 static const unsigned int BLOCK_STALLING_TIMEOUT
= 2;
77 /** Number of headers sent in one getheaders result. We rely on the assumption that if a peer sends
78 * less than this number, we reached their tip. Changing this value is a protocol upgrade. */
79 static const unsigned int MAX_HEADERS_RESULTS
= 2000;
80 /** Size of the "block download window": how far ahead of our current height do we fetch?
81 * Larger windows tolerate larger download speed differences between peer, but increase the potential
82 * degree of disordering of blocks on disk (which make reindexing and in the future perhaps pruning
83 * harder). We'll probably want to make this a per-peer adaptive value at some point. */
84 static const unsigned int BLOCK_DOWNLOAD_WINDOW
= 1024;
85 /** Time to wait (in seconds) between writing blockchain state to disk. */
86 static const unsigned int DATABASE_WRITE_INTERVAL
= 3600;
87 /** Maximum length of reject messages. */
88 static const unsigned int MAX_REJECT_MESSAGE_LENGTH
= 111;
90 /** "reject" message codes */
91 static const unsigned char REJECT_MALFORMED
= 0x01;
92 static const unsigned char REJECT_INVALID
= 0x10;
93 static const unsigned char REJECT_OBSOLETE
= 0x11;
94 static const unsigned char REJECT_DUPLICATE
= 0x12;
95 static const unsigned char REJECT_NONSTANDARD
= 0x40;
96 static const unsigned char REJECT_DUST
= 0x41;
97 static const unsigned char REJECT_INSUFFICIENTFEE
= 0x42;
98 static const unsigned char REJECT_CHECKPOINT
= 0x43;
102 size_t operator()(const uint256
& hash
) const { return hash
.GetCheapHash(); }
105 extern CScript COINBASE_FLAGS
;
106 extern CCriticalSection cs_main
;
107 extern CTxMemPool mempool
;
108 typedef boost::unordered_map
<uint256
, CBlockIndex
*, BlockHasher
> BlockMap
;
109 extern BlockMap mapBlockIndex
;
110 extern uint64_t nLastBlockTx
;
111 extern uint64_t nLastBlockSize
;
112 extern const std::string strMessageMagic
;
113 extern CWaitableCriticalSection csBestBlock
;
114 extern CConditionVariable cvBlockChange
;
115 extern bool fImporting
;
116 extern bool fReindex
;
117 extern int nScriptCheckThreads
;
118 extern bool fTxIndex
;
119 extern bool fIsBareMultisigStd
;
120 extern bool fCheckBlockIndex
;
121 extern unsigned int nCoinCacheSize
;
122 extern CFeeRate minRelayTxFee
;
124 /** Best header we've seen so far (used for getheaders queries' starting points). */
125 extern CBlockIndex
*pindexBestHeader
;
127 /** Minimum disk space required - used in CheckDiskSpace() */
128 static const uint64_t nMinDiskSpace
= 52428800;
130 /** Pruning-related variables and constants */
131 /** True if any block files have ever been pruned. */
132 extern bool fHavePruned
;
133 /** True if we're running in -prune mode. */
134 extern bool fPruneMode
;
135 /** Number of MiB of block files that we're trying to stay below. */
136 extern uint64_t nPruneTarget
;
137 /** Block files containing a block-height within MIN_BLOCKS_TO_KEEP of chainActive.Tip() will not be pruned. */
138 static const signed int MIN_BLOCKS_TO_KEEP
= 288;
140 // Require that user allocate at least 550MB for block & undo files (blk???.dat and rev???.dat)
141 // At 1MB per block, 288 blocks = 288MB.
142 // Add 15% for Undo data = 331MB
143 // Add 20% for Orphan block rate = 397MB
144 // We want the low water mark after pruning to be at least 397 MB and since we prune in
145 // full block file chunks, we need the high water mark which triggers the prune to be
146 // one 128MB block file + added 15% undo data = 147MB greater for a total of 545MB
147 // Setting the target to > than 550MB will make it likely we can respect the target.
148 static const signed int MIN_DISK_SPACE_FOR_BLOCK_FILES
= 550 * 1024 * 1024;
150 /** Register with a network node to receive its signals */
151 void RegisterNodeSignals(CNodeSignals
& nodeSignals
);
152 /** Unregister a network node */
153 void UnregisterNodeSignals(CNodeSignals
& nodeSignals
);
156 * Process an incoming block. This only returns after the best known valid
157 * block is made active. Note that it does not, however, guarantee that the
158 * specific block passed to it has been checked for validity!
160 * @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.
161 * @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.
162 * @param[in] pblock The block we want to process.
163 * @param[out] dbp If pblock is stored to disk (or already there), this will be set to its location.
164 * @return True if state.IsValid()
166 bool ProcessNewBlock(CValidationState
&state
, CNode
* pfrom
, CBlock
* pblock
, CDiskBlockPos
*dbp
= NULL
);
167 /** Check whether enough disk space is available for an incoming block */
168 bool CheckDiskSpace(uint64_t nAdditionalBytes
= 0);
169 /** Open a block file (blk?????.dat) */
170 FILE* OpenBlockFile(const CDiskBlockPos
&pos
, bool fReadOnly
= false);
171 /** Open an undo file (rev?????.dat) */
172 FILE* OpenUndoFile(const CDiskBlockPos
&pos
, bool fReadOnly
= false);
173 /** Translation to a filesystem path */
174 boost::filesystem::path
GetBlockPosFilename(const CDiskBlockPos
&pos
, const char *prefix
);
175 /** Import blocks from an external file */
176 bool LoadExternalBlockFile(FILE* fileIn
, CDiskBlockPos
*dbp
= NULL
);
177 /** Initialize a new block tree database + block data on disk */
178 bool InitBlockIndex();
179 /** Load the block tree and coins database from disk */
180 bool LoadBlockIndex();
181 /** Unload database information */
182 void UnloadBlockIndex();
183 /** Process protocol messages received from a given node */
184 bool ProcessMessages(CNode
* pfrom
);
186 * Send queued protocol messages to be sent to a give node.
188 * @param[in] pto The node which we are sending messages to.
189 * @param[in] fSendTrickle When true send the trickled data, otherwise trickle the data until true.
191 bool SendMessages(CNode
* pto
, bool fSendTrickle
);
192 /** Run an instance of the script checking thread */
193 void ThreadScriptCheck();
194 /** Check whether we are doing an initial block download (synchronizing from disk or network) */
195 bool IsInitialBlockDownload();
196 /** Format a string that describes several potential problems detected by the core */
197 std::string
GetWarnings(std::string strFor
);
198 /** Retrieve a transaction (from memory pool, or from disk, if possible) */
199 bool GetTransaction(const uint256
&hash
, CTransaction
&tx
, uint256
&hashBlock
, bool fAllowSlow
= false);
200 /** Find the best known block, and make it the tip of the block chain */
201 bool ActivateBestChain(CValidationState
&state
, CBlock
*pblock
= NULL
);
202 CAmount
GetBlockValue(int nHeight
, const CAmount
& nFees
);
205 * Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a user-defined target.
206 * The user sets the target (in MB) on the command line or in config file. This will be run on startup and whenever new
207 * space is allocated in a block or undo file, staying below the target. Changing back to unpruned requires a reindex
208 * (which in this case means the blockchain must be re-downloaded.)
210 * Pruning functions are called from FlushStateToDisk when the global fCheckForPruning flag has been set.
211 * Block and undo files are deleted in lock-step (when blk00003.dat is deleted, so is rev00003.dat.)
212 * Pruning cannot take place until the longest chain is at least a certain length (100000 on mainnet, 1000 on testnet, 10 on regtest).
213 * Pruning will never delete a block within a defined distance (currently 288) from the active chain's tip.
214 * The block index is updated by unsetting HAVE_DATA and HAVE_UNDO for any blocks that were stored in the deleted files.
215 * A db flag records the fact that at least some block files have been pruned.
217 * @param[out] setFilesToPrune The set of file indices that can be unlinked will be returned
219 void FindFilesToPrune(std::set
<int>& setFilesToPrune
);
222 * Actually unlink the specified files
224 void UnlinkPrunedFiles(std::set
<int>& setFilesToPrune
);
226 /** Create a new block index entry for a given block hash */
227 CBlockIndex
* InsertBlockIndex(uint256 hash
);
228 /** Abort with a message */
229 bool AbortNode(const std::string
&msg
, const std::string
&userMessage
="");
230 /** Get statistics from node state */
231 bool GetNodeStateStats(NodeId nodeid
, CNodeStateStats
&stats
);
232 /** Increase a node's misbehavior score. */
233 void Misbehaving(NodeId nodeid
, int howmuch
);
234 /** Flush all state, indexes and buffers to disk. */
235 void FlushStateToDisk();
236 /** Prune block files and flush state to disk. */
237 void PruneAndFlush();
239 /** (try to) add transaction to memory pool **/
240 bool AcceptToMemoryPool(CTxMemPool
& pool
, CValidationState
&state
, const CTransaction
&tx
, bool fLimitFree
,
241 bool* pfMissingInputs
, bool fRejectAbsurdFee
=false);
244 struct CNodeStateStats
{
248 std::vector
<int> vHeightInFlight
;
251 struct CDiskTxPos
: public CDiskBlockPos
253 unsigned int nTxOffset
; // after header
255 ADD_SERIALIZE_METHODS
;
257 template <typename Stream
, typename Operation
>
258 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
259 READWRITE(*(CDiskBlockPos
*)this);
260 READWRITE(VARINT(nTxOffset
));
263 CDiskTxPos(const CDiskBlockPos
&blockIn
, unsigned int nTxOffsetIn
) : CDiskBlockPos(blockIn
.nFile
, blockIn
.nPos
), nTxOffset(nTxOffsetIn
) {
271 CDiskBlockPos::SetNull();
277 CAmount
GetMinRelayFee(const CTransaction
& tx
, unsigned int nBytes
, bool fAllowFree
);
280 * Check transaction inputs, and make sure any
281 * pay-to-script-hash transactions are evaluating IsStandard scripts
283 * Why bother? To avoid denial-of-service attacks; an attacker
284 * can submit a standard HASH... OP_EQUAL transaction,
285 * which will get accepted into blocks. The redemption
286 * script can be anything; an attacker could use a very
287 * expensive-to-check-upon-redemption script like:
288 * DUP CHECKSIG DROP ... repeated 100 times... OP_1
292 * Check for standard transaction types
293 * @param[in] mapInputs Map of previous transactions that have outputs we're spending
294 * @return True if all inputs (scriptSigs) use only standard transaction forms
296 bool AreInputsStandard(const CTransaction
& tx
, const CCoinsViewCache
& mapInputs
);
299 * Count ECDSA signature operations the old-fashioned (pre-0.6) way
300 * @return number of sigops this transaction's outputs will produce when spent
301 * @see CTransaction::FetchInputs
303 unsigned int GetLegacySigOpCount(const CTransaction
& tx
);
306 * Count ECDSA signature operations in pay-to-script-hash inputs.
308 * @param[in] mapInputs Map of previous transactions that have outputs we're spending
309 * @return maximum number of sigops required to validate this transaction's inputs
310 * @see CTransaction::FetchInputs
312 unsigned int GetP2SHSigOpCount(const CTransaction
& tx
, const CCoinsViewCache
& mapInputs
);
316 * Check whether all inputs of this transaction are valid (no double spends, scripts & sigs, amounts)
317 * This does not modify the UTXO set. If pvChecks is not NULL, script checks are pushed onto it
318 * instead of being performed inline.
320 bool CheckInputs(const CTransaction
& tx
, CValidationState
&state
, const CCoinsViewCache
&view
, bool fScriptChecks
,
321 unsigned int flags
, bool cacheStore
, std::vector
<CScriptCheck
> *pvChecks
= NULL
);
323 /** Apply the effects of this transaction on the UTXO set represented by view */
324 void UpdateCoins(const CTransaction
& tx
, CValidationState
&state
, CCoinsViewCache
&inputs
, int nHeight
);
326 /** Context-independent validity checks */
327 bool CheckTransaction(const CTransaction
& tx
, CValidationState
& state
);
329 /** Check for standard transaction types
330 * @return True if all outputs (scriptPubKeys) use only standard transaction forms
332 bool IsStandardTx(const CTransaction
& tx
, std::string
& reason
);
334 bool IsFinalTx(const CTransaction
&tx
, int nBlockHeight
= 0, int64_t nBlockTime
= 0);
337 * Closure representing one script verification
338 * Note that this stores references to the spending transaction
343 CScript scriptPubKey
;
344 const CTransaction
*ptxTo
;
351 CScriptCheck(): ptxTo(0), nIn(0), nFlags(0), cacheStore(false), error(SCRIPT_ERR_UNKNOWN_ERROR
) {}
352 CScriptCheck(const CCoins
& txFromIn
, const CTransaction
& txToIn
, unsigned int nInIn
, unsigned int nFlagsIn
, bool cacheIn
) :
353 scriptPubKey(txFromIn
.vout
[txToIn
.vin
[nInIn
].prevout
.n
].scriptPubKey
),
354 ptxTo(&txToIn
), nIn(nInIn
), nFlags(nFlagsIn
), cacheStore(cacheIn
), error(SCRIPT_ERR_UNKNOWN_ERROR
) { }
358 void swap(CScriptCheck
&check
) {
359 scriptPubKey
.swap(check
.scriptPubKey
);
360 std::swap(ptxTo
, check
.ptxTo
);
361 std::swap(nIn
, check
.nIn
);
362 std::swap(nFlags
, check
.nFlags
);
363 std::swap(cacheStore
, check
.cacheStore
);
364 std::swap(error
, check
.error
);
367 ScriptError
GetScriptError() const { return error
; }
371 /** Functions for disk access for blocks */
372 bool WriteBlockToDisk(CBlock
& block
, CDiskBlockPos
& pos
);
373 bool ReadBlockFromDisk(CBlock
& block
, const CDiskBlockPos
& pos
);
374 bool ReadBlockFromDisk(CBlock
& block
, const CBlockIndex
* pindex
);
377 /** Functions for validating blocks and updating the block tree */
379 /** Undo the effects of this block (with given index) on the UTXO set represented by coins.
380 * In case pfClean is provided, operation will try to be tolerant about errors, and *pfClean
381 * will be true if no problems were found. Otherwise, the return value will be false in case
382 * of problems. Note that in any case, coins may be modified. */
383 bool DisconnectBlock(CBlock
& block
, CValidationState
& state
, CBlockIndex
* pindex
, CCoinsViewCache
& coins
, bool* pfClean
= NULL
);
385 /** Apply the effects of this block (with given index) on the UTXO set represented by coins */
386 bool ConnectBlock(const CBlock
& block
, CValidationState
& state
, CBlockIndex
* pindex
, CCoinsViewCache
& coins
, bool fJustCheck
= false);
388 /** Context-independent validity checks */
389 bool CheckBlockHeader(const CBlockHeader
& block
, CValidationState
& state
, bool fCheckPOW
= true);
390 bool CheckBlock(const CBlock
& block
, CValidationState
& state
, bool fCheckPOW
= true, bool fCheckMerkleRoot
= true);
392 /** Context-dependent validity checks */
393 bool ContextualCheckBlockHeader(const CBlockHeader
& block
, CValidationState
& state
, CBlockIndex
*pindexPrev
);
394 bool ContextualCheckBlock(const CBlock
& block
, CValidationState
& state
, CBlockIndex
*pindexPrev
);
396 /** Check a block is completely valid from start to finish (only works on top of our current best block, with cs_main held) */
397 bool TestBlockValidity(CValidationState
&state
, const CBlock
& block
, CBlockIndex
*pindexPrev
, bool fCheckPOW
= true, bool fCheckMerkleRoot
= true);
399 /** Store block on disk. If dbp is provided, the file is known to already reside on disk */
400 bool AcceptBlock(CBlock
& block
, CValidationState
& state
, CBlockIndex
**pindex
, CDiskBlockPos
* dbp
= NULL
);
401 bool AcceptBlockHeader(const CBlockHeader
& block
, CValidationState
& state
, CBlockIndex
**ppindex
= NULL
);
408 unsigned int nBlocks
; //! number of blocks stored in file
409 unsigned int nSize
; //! number of used bytes of block file
410 unsigned int nUndoSize
; //! number of used bytes in the undo file
411 unsigned int nHeightFirst
; //! lowest height of block in file
412 unsigned int nHeightLast
; //! highest height of block in file
413 uint64_t nTimeFirst
; //! earliest time of block in file
414 uint64_t nTimeLast
; //! latest time of block in file
416 ADD_SERIALIZE_METHODS
;
418 template <typename Stream
, typename Operation
>
419 inline void SerializationOp(Stream
& s
, Operation ser_action
, int nType
, int nVersion
) {
420 READWRITE(VARINT(nBlocks
));
421 READWRITE(VARINT(nSize
));
422 READWRITE(VARINT(nUndoSize
));
423 READWRITE(VARINT(nHeightFirst
));
424 READWRITE(VARINT(nHeightLast
));
425 READWRITE(VARINT(nTimeFirst
));
426 READWRITE(VARINT(nTimeLast
));
443 std::string
ToString() const;
445 /** update statistics (does not update nSize) */
446 void AddBlock(unsigned int nHeightIn
, uint64_t nTimeIn
) {
447 if (nBlocks
==0 || nHeightFirst
> nHeightIn
)
448 nHeightFirst
= nHeightIn
;
449 if (nBlocks
==0 || nTimeFirst
> nTimeIn
)
450 nTimeFirst
= nTimeIn
;
452 if (nHeightIn
> nHeightLast
)
453 nHeightLast
= nHeightIn
;
454 if (nTimeIn
> nTimeLast
)
459 /** Capture information about block/transaction validation */
460 class CValidationState
{
463 MODE_VALID
, //! everything ok
464 MODE_INVALID
, //! network rule violation (DoS value may be set)
465 MODE_ERROR
, //! run-time error
468 std::string strRejectReason
;
469 unsigned char chRejectCode
;
470 bool corruptionPossible
;
472 CValidationState() : mode(MODE_VALID
), nDoS(0), chRejectCode(0), corruptionPossible(false) {}
473 bool DoS(int level
, bool ret
= false,
474 unsigned char chRejectCodeIn
=0, std::string strRejectReasonIn
="",
475 bool corruptionIn
=false) {
476 chRejectCode
= chRejectCodeIn
;
477 strRejectReason
= strRejectReasonIn
;
478 corruptionPossible
= corruptionIn
;
479 if (mode
== MODE_ERROR
)
485 bool Invalid(bool ret
= false,
486 unsigned char _chRejectCode
=0, std::string _strRejectReason
="") {
487 return DoS(0, ret
, _chRejectCode
, _strRejectReason
);
489 bool Error(std::string strRejectReasonIn
="") {
490 if (mode
== MODE_VALID
)
491 strRejectReason
= strRejectReasonIn
;
495 bool Abort(const std::string
&msg
) {
499 bool IsValid() const {
500 return mode
== MODE_VALID
;
502 bool IsInvalid() const {
503 return mode
== MODE_INVALID
;
505 bool IsError() const {
506 return mode
== MODE_ERROR
;
508 bool IsInvalid(int &nDoSOut
) const {
515 bool CorruptionPossible() const {
516 return corruptionPossible
;
518 unsigned char GetRejectCode() const { return chRejectCode
; }
519 std::string
GetRejectReason() const { return strRejectReason
; }
522 /** RAII wrapper for VerifyDB: Verify consistency of the block and coin databases */
527 bool VerifyDB(CCoinsView
*coinsview
, int nCheckLevel
, int nCheckDepth
);
530 /** Find the last common block between the parameter chain and a locator. */
531 CBlockIndex
* FindForkInGlobalIndex(const CChain
& chain
, const CBlockLocator
& locator
);
533 /** Mark a block as invalid. */
534 bool InvalidateBlock(CValidationState
& state
, CBlockIndex
*pindex
);
536 /** Remove invalidity status from a block and its descendants. */
537 bool ReconsiderBlock(CValidationState
& state
, CBlockIndex
*pindex
);
539 /** The currently-connected chain of blocks. */
540 extern CChain chainActive
;
542 /** Global variable that points to the active CCoinsView (protected by cs_main) */
543 extern CCoinsViewCache
*pcoinsTip
;
545 /** Global variable that points to the active block tree (protected by cs_main) */
546 extern CBlockTreeDB
*pblocktree
;
548 #endif // BITCOIN_MAIN_H