Skip to content

Commit a4bac66

Browse files
committed
[MOVEONLY] Move progress estimation out of checkpoints
1 parent 7dac1e5 commit a4bac66

File tree

7 files changed

+52
-50
lines changed

7 files changed

+52
-50
lines changed

src/checkpoints.cpp

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,6 @@
1515

1616
namespace Checkpoints {
1717

18-
/**
19-
* How many times slower we expect checking transactions after the last
20-
* checkpoint to be (from checking signatures, which is skipped up to the
21-
* last checkpoint). This number is a compromise, as it can't be accurate
22-
* for every system. When reindexing from a fast disk with a slow CPU, it
23-
* can be up to 20, while when downloading from a slow network with a
24-
* fast multicore CPU, it won't be much higher than 1.
25-
*/
26-
static const double SIGCHECK_VERIFICATION_FACTOR = 5.0;
27-
28-
//! Guess how far we are in the verification process at the given block index
29-
double GuessVerificationProgress(const CCheckpointData& data, CBlockIndex *pindex, bool fSigchecks) {
30-
if (pindex==NULL)
31-
return 0.0;
32-
33-
int64_t nNow = time(NULL);
34-
35-
double fSigcheckVerificationFactor = fSigchecks ? SIGCHECK_VERIFICATION_FACTOR : 1.0;
36-
double fWorkBefore = 0.0; // Amount of work done before pindex
37-
double fWorkAfter = 0.0; // Amount of work left after pindex (estimated)
38-
// Work is defined as: 1.0 per transaction before the last checkpoint, and
39-
// fSigcheckVerificationFactor per transaction after.
40-
41-
if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
42-
double nCheapBefore = pindex->nChainTx;
43-
double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
44-
double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint)/86400.0*data.fTransactionsPerDay;
45-
fWorkBefore = nCheapBefore;
46-
fWorkAfter = nCheapAfter + nExpensiveAfter*fSigcheckVerificationFactor;
47-
} else {
48-
double nCheapBefore = data.nTransactionsLastCheckpoint;
49-
double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
50-
double nExpensiveAfter = (nNow - pindex->GetBlockTime())/86400.0*data.fTransactionsPerDay;
51-
fWorkBefore = nCheapBefore + nExpensiveBefore*fSigcheckVerificationFactor;
52-
fWorkAfter = nExpensiveAfter*fSigcheckVerificationFactor;
53-
}
54-
55-
return fWorkBefore / (fWorkBefore + fWorkAfter);
56-
}
57-
5818
CBlockIndex* GetLastCheckpoint(const CCheckpointData& data)
5919
{
6020
const MapCheckpoints& checkpoints = data.mapCheckpoints;

src/checkpoints.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ namespace Checkpoints
2222
//! Returns last CBlockIndex* in mapBlockIndex that is a checkpoint
2323
CBlockIndex* GetLastCheckpoint(const CCheckpointData& data);
2424

25-
double GuessVerificationProgress(const CCheckpointData& data, CBlockIndex* pindex, bool fSigchecks = true);
26-
2725
} //namespace Checkpoints
2826

2927
#endif // BITCOIN_CHECKPOINTS_H

src/qt/clientmodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ double ClientModel::getVerificationProgress(const CBlockIndex *tipIn) const
130130
LOCK(cs_main);
131131
tip = chainActive.Tip();
132132
}
133-
return Checkpoints::GuessVerificationProgress(Params().Checkpoints(), tip);
133+
return GuessVerificationProgress(Params().Checkpoints(), tip);
134134
}
135135

136136
void ClientModel::updateTimer()

src/rpc/blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1073,7 +1073,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
10731073
obj.push_back(Pair("bestblockhash", chainActive.Tip()->GetBlockHash().GetHex()));
10741074
obj.push_back(Pair("difficulty", (double)GetDifficulty()));
10751075
obj.push_back(Pair("mediantime", (int64_t)chainActive.Tip()->GetMedianTimePast()));
1076-
obj.push_back(Pair("verificationprogress", Checkpoints::GuessVerificationProgress(Params().Checkpoints(), chainActive.Tip())));
1076+
obj.push_back(Pair("verificationprogress", GuessVerificationProgress(Params().Checkpoints(), chainActive.Tip())));
10771077
obj.push_back(Pair("chainwork", chainActive.Tip()->nChainWork.GetHex()));
10781078
obj.push_back(Pair("pruned", fPruneMode));
10791079

src/validation.cpp

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2080,7 +2080,7 @@ void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
20802080
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), chainActive.Tip()->nVersion,
20812081
log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
20822082
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
2083-
Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip()), pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
2083+
GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip()), pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
20842084
if (!warningMessages.empty())
20852085
LogPrintf(" warning='%s'", boost::algorithm::join(warningMessages, ", "));
20862086
LogPrintf("\n");
@@ -3445,7 +3445,7 @@ bool static LoadBlockIndexDB(const CChainParams& chainparams)
34453445
LogPrintf("%s: hashBestChain=%s height=%d date=%s progress=%f\n", __func__,
34463446
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
34473447
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
3448-
Checkpoints::GuessVerificationProgress(chainparams.Checkpoints(), chainActive.Tip()));
3448+
GuessVerificationProgress(chainparams.Checkpoints(), chainActive.Tip()));
34493449

34503450
return true;
34513451
}
@@ -4141,6 +4141,46 @@ void DumpMempool(void)
41414141
}
41424142
}
41434143

4144+
/**
4145+
* How many times slower we expect checking transactions after the last
4146+
* checkpoint to be (from checking signatures, which is skipped up to the
4147+
* last checkpoint). This number is a compromise, as it can't be accurate
4148+
* for every system. When reindexing from a fast disk with a slow CPU, it
4149+
* can be up to 20, while when downloading from a slow network with a
4150+
* fast multicore CPU, it won't be much higher than 1.
4151+
*/
4152+
static const double SIGCHECK_VERIFICATION_FACTOR = 5.0;
4153+
4154+
//! Guess how far we are in the verification process at the given block index
4155+
double GuessVerificationProgress(const CCheckpointData& data, CBlockIndex *pindex, bool fSigchecks) {
4156+
if (pindex==NULL)
4157+
return 0.0;
4158+
4159+
int64_t nNow = time(NULL);
4160+
4161+
double fSigcheckVerificationFactor = fSigchecks ? SIGCHECK_VERIFICATION_FACTOR : 1.0;
4162+
double fWorkBefore = 0.0; // Amount of work done before pindex
4163+
double fWorkAfter = 0.0; // Amount of work left after pindex (estimated)
4164+
// Work is defined as: 1.0 per transaction before the last checkpoint, and
4165+
// fSigcheckVerificationFactor per transaction after.
4166+
4167+
if (pindex->nChainTx <= data.nTransactionsLastCheckpoint) {
4168+
double nCheapBefore = pindex->nChainTx;
4169+
double nCheapAfter = data.nTransactionsLastCheckpoint - pindex->nChainTx;
4170+
double nExpensiveAfter = (nNow - data.nTimeLastCheckpoint)/86400.0*data.fTransactionsPerDay;
4171+
fWorkBefore = nCheapBefore;
4172+
fWorkAfter = nCheapAfter + nExpensiveAfter*fSigcheckVerificationFactor;
4173+
} else {
4174+
double nCheapBefore = data.nTransactionsLastCheckpoint;
4175+
double nExpensiveBefore = pindex->nChainTx - data.nTransactionsLastCheckpoint;
4176+
double nExpensiveAfter = (nNow - pindex->GetBlockTime())/86400.0*data.fTransactionsPerDay;
4177+
fWorkBefore = nCheapBefore + nExpensiveBefore*fSigcheckVerificationFactor;
4178+
fWorkAfter = nExpensiveAfter*fSigcheckVerificationFactor;
4179+
}
4180+
4181+
return fWorkBefore / (fWorkBefore + fWorkAfter);
4182+
}
4183+
41444184
class CMainCleanup
41454185
{
41464186
public:

src/validation.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class CScriptCheck;
4242
class CTxMemPool;
4343
class CValidationInterface;
4444
class CValidationState;
45+
class CCheckpointData;
4546

4647
struct PrecomputedTransactionData;
4748
struct LockPoints;
@@ -279,6 +280,9 @@ bool GetTransaction(const uint256 &hash, CTransactionRef &tx, const Consensus::P
279280
bool ActivateBestChain(CValidationState& state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock = std::shared_ptr<const CBlock>());
280281
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
281282

283+
/** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */
284+
double GuessVerificationProgress(const CCheckpointData& data, CBlockIndex* pindex, bool fSigchecks = true);
285+
282286
/**
283287
* Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a user-defined target.
284288
* The user sets the target (in MB) on the command line or in config file. This will be run on startup and whenever new

src/wallet/wallet.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,12 +1479,12 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
14791479
pindex = chainActive.Next(pindex);
14801480

14811481
ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
1482-
double dProgressStart = Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex, false);
1483-
double dProgressTip = Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip(), false);
1482+
double dProgressStart = GuessVerificationProgress(chainParams.Checkpoints(), pindex, false);
1483+
double dProgressTip = GuessVerificationProgress(chainParams.Checkpoints(), chainActive.Tip(), false);
14841484
while (pindex)
14851485
{
14861486
if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0)
1487-
ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex, false) - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
1487+
ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((GuessVerificationProgress(chainParams.Checkpoints(), pindex, false) - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
14881488

14891489
CBlock block;
14901490
ReadBlockFromDisk(block, pindex, Params().GetConsensus());
@@ -1497,7 +1497,7 @@ int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
14971497
pindex = chainActive.Next(pindex);
14981498
if (GetTime() >= nNow + 60) {
14991499
nNow = GetTime();
1500-
LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, Checkpoints::GuessVerificationProgress(chainParams.Checkpoints(), pindex));
1500+
LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, GuessVerificationProgress(chainParams.Checkpoints(), pindex));
15011501
}
15021502
}
15031503
ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI

0 commit comments

Comments
 (0)