@@ -186,8 +186,11 @@ enum FlushStateMode {
186186};
187187
188188// See definition for documentation
189- bool static FlushStateToDisk (CValidationState &state, FlushStateMode mode, int nManualPruneHeight=0 );
190- void FindFilesToPruneManual (std::set<int >& setFilesToPrune, int nManualPruneHeight);
189+ static bool FlushStateToDisk (const CChainParams& chainParams, CValidationState &state, FlushStateMode mode, int nManualPruneHeight=0 );
190+ static void FindFilesToPruneManual (std::set<int >& setFilesToPrune, int nManualPruneHeight);
191+ static void FindFilesToPrune (std::set<int >& setFilesToPrune, uint64_t nPruneAfterHeight);
192+ static bool CheckInputs (const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks , unsigned int flags, bool cacheStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks = NULL );
193+ static FILE* OpenUndoFile (const CDiskBlockPos &pos, bool fReadOnly = false );
191194
192195bool CheckFinalTx (const CTransaction &tx, int flags)
193196{
@@ -309,7 +312,7 @@ bool CheckSequenceLocks(const CTransaction &tx, int flags, LockPoints* lp, bool
309312 return EvaluateSequenceLocks (index, lockPair);
310313}
311314
312- void LimitMempoolSize (CTxMemPool& pool, size_t limit, unsigned long age) {
315+ static void LimitMempoolSize (CTxMemPool& pool, size_t limit, unsigned long age) {
313316 int expired = pool.Expire (GetTime () - age);
314317 if (expired != 0 ) {
315318 LogPrint (BCLog::MEMPOOL, " Expired %i transactions from the memory pool\n " , expired);
@@ -392,7 +395,7 @@ void UpdateMempoolForReorg(DisconnectedBlockTransactions &disconnectpool, bool f
392395 LimitMempoolSize (mempool, GetArg (" -maxmempool" , DEFAULT_MAX_MEMPOOL_SIZE) * 1000000 , GetArg (" -mempoolexpiry" , DEFAULT_MEMPOOL_EXPIRY) * 60 * 60 );
393396}
394397
395- bool AcceptToMemoryPoolWorker (CTxMemPool& pool, CValidationState& state, const CTransactionRef& ptx, bool fLimitFree ,
398+ static bool AcceptToMemoryPoolWorker (const CChainParams& chainparams, CTxMemPool& pool, CValidationState& state, const CTransactionRef& ptx, bool fLimitFree ,
396399 bool * pfMissingInputs, int64_t nAcceptTime, std::list<CTransactionRef>* plTxnReplaced,
397400 bool fOverrideMempoolLimit , const CAmount& nAbsurdFee, std::vector<COutPoint>& coins_to_uncache)
398401{
@@ -410,7 +413,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C
410413 return state.DoS (100 , false , REJECT_INVALID, " coinbase" );
411414
412415 // Reject transactions with witness before segregated witness activates (override with -prematurewitness)
413- bool witnessEnabled = IsWitnessEnabled (chainActive.Tip (), Params () .GetConsensus ());
416+ bool witnessEnabled = IsWitnessEnabled (chainActive.Tip (), chainparams .GetConsensus ());
414417 if (!GetBoolArg (" -prematurewitness" ,false ) && tx.HasWitness () && !witnessEnabled) {
415418 return state.DoS (0 , false , REJECT_NONSTANDARD, " no-witness-yet" , true );
416419 }
@@ -739,7 +742,7 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C
739742 }
740743
741744 unsigned int scriptVerifyFlags = STANDARD_SCRIPT_VERIFY_FLAGS;
742- if (!Params () .RequireStandard ()) {
745+ if (!chainparams .RequireStandard ()) {
743746 scriptVerifyFlags = GetArg (" -promiscuousmempoolflags" , scriptVerifyFlags);
744747 }
745748
@@ -809,27 +812,29 @@ bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState& state, const C
809812 return true ;
810813}
811814
812- bool AcceptToMemoryPoolWithTime (CTxMemPool& pool, CValidationState &state, const CTransactionRef &tx, bool fLimitFree ,
815+ /* * (try to) add transaction to memory pool with a specified acceptance time **/
816+ static bool AcceptToMemoryPoolWithTime (const CChainParams& chainparams, CTxMemPool& pool, CValidationState &state, const CTransactionRef &tx, bool fLimitFree ,
813817 bool * pfMissingInputs, int64_t nAcceptTime, std::list<CTransactionRef>* plTxnReplaced,
814818 bool fOverrideMempoolLimit , const CAmount nAbsurdFee)
815819{
816820 std::vector<COutPoint> coins_to_uncache;
817- bool res = AcceptToMemoryPoolWorker (pool, state, tx, fLimitFree , pfMissingInputs, nAcceptTime, plTxnReplaced, fOverrideMempoolLimit , nAbsurdFee, coins_to_uncache);
821+ bool res = AcceptToMemoryPoolWorker (chainparams, pool, state, tx, fLimitFree , pfMissingInputs, nAcceptTime, plTxnReplaced, fOverrideMempoolLimit , nAbsurdFee, coins_to_uncache);
818822 if (!res) {
819823 BOOST_FOREACH (const COutPoint& hashTx, coins_to_uncache)
820824 pcoinsTip->Uncache (hashTx);
821825 }
822826 // After we've (potentially) uncached entries, ensure our coins cache is still within its size limits
823827 CValidationState stateDummy;
824- FlushStateToDisk (stateDummy, FLUSH_STATE_PERIODIC);
828+ FlushStateToDisk (chainparams, stateDummy, FLUSH_STATE_PERIODIC);
825829 return res;
826830}
827831
828832bool AcceptToMemoryPool (CTxMemPool& pool, CValidationState &state, const CTransactionRef &tx, bool fLimitFree ,
829833 bool * pfMissingInputs, std::list<CTransactionRef>* plTxnReplaced,
830834 bool fOverrideMempoolLimit , const CAmount nAbsurdFee)
831835{
832- return AcceptToMemoryPoolWithTime (pool, state, tx, fLimitFree , pfMissingInputs, GetTime (), plTxnReplaced, fOverrideMempoolLimit , nAbsurdFee);
836+ const CChainParams& chainparams = Params ();
837+ return AcceptToMemoryPoolWithTime (chainparams, pool, state, tx, fLimitFree , pfMissingInputs, GetTime (), plTxnReplaced, fOverrideMempoolLimit , nAbsurdFee);
833838}
834839
835840/* * Return transaction in txOut, and if it was found inside a block, its hash is placed in hashBlock */
@@ -898,7 +903,7 @@ bool GetTransaction(const uint256 &hash, CTransactionRef &txOut, const Consensus
898903// CBlock and CBlockIndex
899904//
900905
901- bool WriteBlockToDisk (const CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart)
906+ static bool WriteBlockToDisk (const CBlock& block, CDiskBlockPos& pos, const CMessageHeader::MessageStartChars& messageStart)
902907{
903908 // Open history file to append
904909 CAutoFile fileout (OpenBlockFile (pos), SER_DISK, CLIENT_VERSION);
@@ -1011,7 +1016,7 @@ static void AlertNotify(const std::string& strMessage)
10111016 boost::thread t (runCommand, strCmd); // thread runs free
10121017}
10131018
1014- void CheckForkWarningConditions ()
1019+ static void CheckForkWarningConditions ()
10151020{
10161021 AssertLockHeld (cs_main);
10171022 // Before we get past initial download, we cannot reliably alert about forks
@@ -1052,7 +1057,7 @@ void CheckForkWarningConditions()
10521057 }
10531058}
10541059
1055- void CheckForkWarningConditionsOnNewFork (CBlockIndex* pindexNewForkTip)
1060+ static void CheckForkWarningConditionsOnNewFork (CBlockIndex* pindexNewForkTip)
10561061{
10571062 AssertLockHeld (cs_main);
10581063 // If we are on a fork that is sufficiently large, set a warning flag
@@ -1144,7 +1149,12 @@ int GetSpendHeight(const CCoinsViewCache& inputs)
11441149 return pindexPrev->nHeight + 1 ;
11451150}
11461151
1147- bool CheckInputs (const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks , unsigned int flags, bool cacheStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks)
1152+ /* *
1153+ * Check whether all inputs of this transaction are valid (no double spends, scripts & sigs, amounts)
1154+ * This does not modify the UTXO set. If pvChecks is not NULL, script checks are pushed onto it
1155+ * instead of being performed inline.
1156+ */
1157+ static bool CheckInputs (const CTransaction& tx, CValidationState &state, const CCoinsViewCache &inputs, bool fScriptChecks , unsigned int flags, bool cacheStore, PrecomputedTransactionData& txdata, std::vector<CScriptCheck> *pvChecks)
11481158{
11491159 if (!tx.IsCoinBase ())
11501160 {
@@ -1411,7 +1421,7 @@ void static FlushBlockFile(bool fFinalize = false)
14111421 }
14121422}
14131423
1414- bool FindUndoPos (CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
1424+ static bool FindUndoPos (CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize);
14151425
14161426static CCheckQueue<CScriptCheck> scriptcheckqueue (128 );
14171427
@@ -1730,9 +1740,8 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
17301740 * if they're too large, if it's been a while since the last write,
17311741 * or always and in all cases if we're in prune mode and are deleting files.
17321742 */
1733- bool static FlushStateToDisk (CValidationState &state, FlushStateMode mode, int nManualPruneHeight) {
1743+ bool static FlushStateToDisk (const CChainParams& chainparams, CValidationState &state, FlushStateMode mode, int nManualPruneHeight) {
17341744 int64_t nMempoolUsage = mempool.DynamicMemoryUsage ();
1735- const CChainParams& chainparams = Params ();
17361745 LOCK2 (cs_main, cs_LastBlockFile);
17371746 static int64_t nLastWrite = 0 ;
17381747 static int64_t nLastFlush = 0 ;
@@ -1836,13 +1845,15 @@ bool static FlushStateToDisk(CValidationState &state, FlushStateMode mode, int n
18361845
18371846void FlushStateToDisk () {
18381847 CValidationState state;
1839- FlushStateToDisk (state, FLUSH_STATE_ALWAYS);
1848+ const CChainParams& chainparams = Params ();
1849+ FlushStateToDisk (chainparams, state, FLUSH_STATE_ALWAYS);
18401850}
18411851
18421852void PruneAndFlush () {
18431853 CValidationState state;
18441854 fCheckForPruning = true ;
1845- FlushStateToDisk (state, FLUSH_STATE_NONE);
1855+ const CChainParams& chainparams = Params ();
1856+ FlushStateToDisk (chainparams, state, FLUSH_STATE_NONE);
18461857}
18471858
18481859static void DoWarning (const std::string& strWarning)
@@ -1939,7 +1950,7 @@ bool static DisconnectTip(CValidationState& state, const CChainParams& chainpara
19391950 }
19401951 LogPrint (BCLog::BENCH, " - Disconnect block: %.2fms\n " , (GetTimeMicros () - nStart) * 0.001 );
19411952 // Write the chain state to disk, if necessary.
1942- if (!FlushStateToDisk (state, FLUSH_STATE_IF_NEEDED))
1953+ if (!FlushStateToDisk (chainparams, state, FLUSH_STATE_IF_NEEDED))
19431954 return false ;
19441955
19451956 if (disconnectpool) {
@@ -2076,7 +2087,7 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,
20762087 int64_t nTime4 = GetTimeMicros (); nTimeFlush += nTime4 - nTime3;
20772088 LogPrint (BCLog::BENCH, " - Flush: %.2fms [%.2fs]\n " , (nTime4 - nTime3) * 0.001 , nTimeFlush * 0.000001 );
20782089 // Write the chain state to disk, if necessary.
2079- if (!FlushStateToDisk (state, FLUSH_STATE_IF_NEEDED))
2090+ if (!FlushStateToDisk (chainparams, state, FLUSH_STATE_IF_NEEDED))
20802091 return false ;
20812092 int64_t nTime5 = GetTimeMicros (); nTimeChainState += nTime5 - nTime4;
20822093 LogPrint (BCLog::BENCH, " - Writing chainstate: %.2fms [%.2fs]\n " , (nTime5 - nTime4) * 0.001 , nTimeChainState * 0.000001 );
@@ -2336,7 +2347,7 @@ bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams,
23362347 CheckBlockIndex (chainparams.GetConsensus ());
23372348
23382349 // Write changes periodically to disk, after relay.
2339- if (!FlushStateToDisk (state, FLUSH_STATE_PERIODIC)) {
2350+ if (!FlushStateToDisk (chainparams, state, FLUSH_STATE_PERIODIC)) {
23402351 return false ;
23412352 }
23422353
@@ -2453,7 +2464,7 @@ bool ResetBlockFailureFlags(CBlockIndex *pindex) {
24532464 return true ;
24542465}
24552466
2456- CBlockIndex* AddToBlockIndex (const CBlockHeader& block)
2467+ static CBlockIndex* AddToBlockIndex (const CBlockHeader& block)
24572468{
24582469 // Check for duplicate
24592470 uint256 hash = block.GetHash ();
@@ -2537,7 +2548,7 @@ static bool ReceivedBlockTransactions(const CBlock &block, CValidationState& sta
25372548 return true ;
25382549}
25392550
2540- bool FindBlockPos (CValidationState &state, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false )
2551+ static bool FindBlockPos (CValidationState &state, CDiskBlockPos &pos, unsigned int nAddSize, unsigned int nHeight, uint64_t nTime, bool fKnown = false )
25412552{
25422553 LOCK (cs_LastBlockFile);
25432554
@@ -2594,7 +2605,7 @@ bool FindBlockPos(CValidationState &state, CDiskBlockPos &pos, unsigned int nAdd
25942605 return true ;
25952606}
25962607
2597- bool FindUndoPos (CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
2608+ static bool FindUndoPos (CValidationState &state, int nFile, CDiskBlockPos &pos, unsigned int nAddSize)
25982609{
25992610 pos.nFile = nFile;
26002611
@@ -2625,7 +2636,7 @@ bool FindUndoPos(CValidationState &state, int nFile, CDiskBlockPos &pos, unsigne
26252636 return true ;
26262637}
26272638
2628- bool CheckBlockHeader (const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW )
2639+ static bool CheckBlockHeader (const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true )
26292640{
26302641 // Check proof of work matches claimed amount
26312642 if (fCheckPOW && !CheckProofOfWork (block.GetHash (), block.nBits , consensusParams))
@@ -2775,7 +2786,10 @@ std::vector<unsigned char> GenerateCoinbaseCommitment(CBlock& block, const CBloc
27752786 return commitment;
27762787}
27772788
2778- bool ContextualCheckBlockHeader (const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
2789+ /* * Context-dependent validity checks.
2790+ * By "context", we mean only the previous block headers, but not the UTXO
2791+ * set; UTXO-related validity checks are done in ConnectBlock(). */
2792+ static bool ContextualCheckBlockHeader (const CBlockHeader& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev, int64_t nAdjustedTime)
27792793{
27802794 assert (pindexPrev != NULL );
27812795 const int nHeight = pindexPrev->nHeight + 1 ;
@@ -2802,7 +2816,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
28022816 return true ;
28032817}
28042818
2805- bool ContextualCheckBlock (const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
2819+ static bool ContextualCheckBlock (const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, const CBlockIndex* pindexPrev)
28062820{
28072821 const int nHeight = pindexPrev == NULL ? 0 : pindexPrev->nHeight + 1 ;
28082822
@@ -3026,7 +3040,7 @@ static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidation
30263040 }
30273041
30283042 if (fCheckForPruning )
3029- FlushStateToDisk (state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
3043+ FlushStateToDisk (chainparams, state, FLUSH_STATE_NONE); // we just allocated more disk space for block files
30303044
30313045 return true ;
30323046}
@@ -3094,7 +3108,7 @@ bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams,
30943108 */
30953109
30963110/* Calculate the amount of disk space the block & undo files currently use */
3097- uint64_t CalculateCurrentUsage ()
3111+ static uint64_t CalculateCurrentUsage ()
30983112{
30993113 uint64_t retval = 0 ;
31003114 BOOST_FOREACH (const CBlockFileInfo &file, vinfoBlockFile) {
@@ -3147,7 +3161,7 @@ void UnlinkPrunedFiles(const std::set<int>& setFilesToPrune)
31473161}
31483162
31493163/* Calculate the block/rev files to delete based on height specified by user with RPC command pruneblockchain */
3150- void FindFilesToPruneManual (std::set<int >& setFilesToPrune, int nManualPruneHeight)
3164+ static void FindFilesToPruneManual (std::set<int >& setFilesToPrune, int nManualPruneHeight)
31513165{
31523166 assert (fPruneMode && nManualPruneHeight > 0 );
31533167
@@ -3172,11 +3186,26 @@ void FindFilesToPruneManual(std::set<int>& setFilesToPrune, int nManualPruneHeig
31723186void PruneBlockFilesManual (int nManualPruneHeight)
31733187{
31743188 CValidationState state;
3175- FlushStateToDisk (state, FLUSH_STATE_NONE, nManualPruneHeight);
3189+ const CChainParams& chainparams = Params ();
3190+ FlushStateToDisk (chainparams, state, FLUSH_STATE_NONE, nManualPruneHeight);
31763191}
31773192
3178- /* Calculate the block/rev files that should be deleted to remain under target*/
3179- void FindFilesToPrune (std::set<int >& setFilesToPrune, uint64_t nPruneAfterHeight)
3193+ /* *
3194+ * Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a user-defined target.
3195+ * The user sets the target (in MB) on the command line or in config file. This will be run on startup and whenever new
3196+ * space is allocated in a block or undo file, staying below the target. Changing back to unpruned requires a reindex
3197+ * (which in this case means the blockchain must be re-downloaded.)
3198+ *
3199+ * Pruning functions are called from FlushStateToDisk when the global fCheckForPruning flag has been set.
3200+ * Block and undo files are deleted in lock-step (when blk00003.dat is deleted, so is rev00003.dat.)
3201+ * Pruning cannot take place until the longest chain is at least a certain length (100000 on mainnet, 1000 on testnet, 1000 on regtest).
3202+ * Pruning will never delete a block within a defined distance (currently 288) from the active chain's tip.
3203+ * The block index is updated by unsetting HAVE_DATA and HAVE_UNDO for any blocks that were stored in the deleted files.
3204+ * A db flag records the fact that at least some block files have been pruned.
3205+ *
3206+ * @param[out] setFilesToPrune The set of file indices that can be unlinked will be returned
3207+ */
3208+ static void FindFilesToPrune (std::set<int >& setFilesToPrune, uint64_t nPruneAfterHeight)
31803209{
31813210 LOCK2 (cs_main, cs_LastBlockFile);
31823211 if (chainActive.Tip () == NULL || nPruneTarget == 0 ) {
@@ -3234,7 +3263,7 @@ bool CheckDiskSpace(uint64_t nAdditionalBytes)
32343263 return true ;
32353264}
32363265
3237- FILE* OpenDiskFile (const CDiskBlockPos &pos, const char *prefix, bool fReadOnly )
3266+ static FILE* OpenDiskFile (const CDiskBlockPos &pos, const char *prefix, bool fReadOnly )
32383267{
32393268 if (pos.IsNull ())
32403269 return NULL ;
@@ -3261,7 +3290,8 @@ FILE* OpenBlockFile(const CDiskBlockPos &pos, bool fReadOnly) {
32613290 return OpenDiskFile (pos, " blk" , fReadOnly );
32623291}
32633292
3264- FILE* OpenUndoFile (const CDiskBlockPos &pos, bool fReadOnly ) {
3293+ /* * Open an undo file (rev?????.dat) */
3294+ static FILE* OpenUndoFile (const CDiskBlockPos &pos, bool fReadOnly ) {
32653295 return OpenDiskFile (pos, " rev" , fReadOnly );
32663296}
32673297
@@ -3533,7 +3563,7 @@ bool RewindBlockIndex(const CChainParams& params)
35333563 return error (" RewindBlockIndex: unable to disconnect block at height %i" , pindex->nHeight );
35343564 }
35353565 // Occasionally flush state to disk.
3536- if (!FlushStateToDisk (state, FLUSH_STATE_PERIODIC))
3566+ if (!FlushStateToDisk (params, state, FLUSH_STATE_PERIODIC))
35373567 return false ;
35383568 }
35393569
@@ -3582,7 +3612,7 @@ bool RewindBlockIndex(const CChainParams& params)
35823612
35833613 CheckBlockIndex (params.GetConsensus ());
35843614
3585- if (!FlushStateToDisk (state, FLUSH_STATE_ALWAYS)) {
3615+ if (!FlushStateToDisk (params, state, FLUSH_STATE_ALWAYS)) {
35863616 return false ;
35873617 }
35883618
@@ -3655,7 +3685,7 @@ bool InitBlockIndex(const CChainParams& chainparams)
36553685 if (!ReceivedBlockTransactions (block, state, pindex, blockPos, chainparams.GetConsensus ()))
36563686 return error (" LoadBlockIndex(): genesis block not accepted" );
36573687 // Force a chainstate write so that when we VerifyDB in a moment, it doesn't check stale data
3658- return FlushStateToDisk (state, FLUSH_STATE_ALWAYS);
3688+ return FlushStateToDisk (chainparams, state, FLUSH_STATE_ALWAYS);
36593689 } catch (const std::runtime_error& e) {
36603690 return error (" LoadBlockIndex(): failed to initialize block database: %s" , e.what ());
36613691 }
@@ -3997,6 +4027,7 @@ static const uint64_t MEMPOOL_DUMP_VERSION = 1;
39974027
39984028bool LoadMempool (void )
39994029{
4030+ const CChainParams& chainparams = Params ();
40004031 int64_t nExpiryTimeout = GetArg (" -mempoolexpiry" , DEFAULT_MEMPOOL_EXPIRY) * 60 * 60 ;
40014032 FILE* filestr = fsbridge::fopen (GetDataDir () / " mempool.dat" , " rb" );
40024033 CAutoFile file (filestr, SER_DISK, CLIENT_VERSION);
@@ -4033,7 +4064,7 @@ bool LoadMempool(void)
40334064 CValidationState state;
40344065 if (nTime + nExpiryTimeout > nNow) {
40354066 LOCK (cs_main);
4036- AcceptToMemoryPoolWithTime (mempool, state, tx, true , NULL , nTime);
4067+ AcceptToMemoryPoolWithTime (chainparams, mempool, state, tx, true , NULL , nTime, NULL , false , 0 );
40374068 if (state.IsValid ()) {
40384069 ++count;
40394070 } else {
0 commit comments