Skip to content
This repository was archived by the owner on Nov 28, 2019. It is now read-only.

Commit 5652022

Browse files
committed
Merge #6: Fix progress reporting & initialblockdownload
ee1d706 Fix progress reporting issue (Steven Roose) 80282cc Fix Travis build (Steven Roose)
2 parents 3232aa8 + ee1d706 commit 5652022

File tree

8 files changed

+95
-26
lines changed

8 files changed

+95
-26
lines changed

qa/pull-tester/rpc-tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
'p2p-leaktests.py',
167167
'pak_tests.py',
168168
'signed_blockchain.py',
169+
'progress.py',
169170
]
170171
if ENABLE_ZMQ:
171172
testScripts.append('zmq_test.py')

qa/rpc-tests/blockchain.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ def _test_getblockchaininfo(self):
112112
assert_equal(res['headers'], 200)
113113
assert_equal(res['bestblockhash'], besthash)
114114
assert isinstance(res['mediantime'], int)
115-
assert_equal(res['verificationprogress'], 1)
115+
# see progress.py test
116+
#assert_equal(res['verificationprogress'], 1)
116117
assert_equal(res['pruned'], False)
117118
assert 'pruneheight' not in res
118119
assert 'softforks' not in res

qa/rpc-tests/progress.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2015-2016 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.
5+
6+
#
7+
# Test progress code
8+
#
9+
10+
import time
11+
12+
from test_framework.test_framework import BitcoinTestFramework
13+
from test_framework.util import (
14+
start_nodes,
15+
Decimal,
16+
)
17+
18+
def assert_close(f1, f2):
19+
assert(abs(Decimal(f1)-f2) < 0.1)
20+
21+
class ProgressTest(BitcoinTestFramework):
22+
def __init__(self):
23+
super().__init__()
24+
self.setup_clean_chain = True
25+
self.num_nodes = 2
26+
self.extra_args = [["-debug", "-con_npowtargetspacing=1", "-maxtimeadjustment=0"]] * self.num_nodes
27+
28+
def setup_network(self):
29+
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, extra_args=self.extra_args)
30+
self.is_network_split = True
31+
self.starttime = int(time.time())
32+
33+
def setmocktime(self, ntime):
34+
for node in self.nodes:
35+
node.setmocktime(self.starttime + ntime)
36+
37+
def run_test(self):
38+
node1 = self.nodes[0]
39+
node2 = self.nodes[1]
40+
self.setmocktime(0)
41+
42+
blocks = []
43+
for i in range(10):
44+
self.setmocktime(i)
45+
blocks.extend(node1.generate(1))
46+
47+
self.setmocktime(19)
48+
assert_close(0.5, node1.getblockchaininfo()["verificationprogress"])
49+
50+
assert(node2.getblockchaininfo()["initialblockdownload"])
51+
52+
self.setmocktime(10)
53+
for i in range(10):
54+
node2.submitblock(node1.getblock(blocks[i], False))
55+
progress = node2.getblockchaininfo()["verificationprogress"]
56+
assert_close(i/10.0, progress)
57+
58+
assert(not node2.getblockchaininfo()["initialblockdownload"])
59+
60+
if __name__ == '__main__':
61+
ProgressTest().main()

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 GuessVerificationProgress(Params().TxData(), tip);
133+
return GuessVerificationProgress(tip, Params().GetConsensus().nPowTargetSpacing);
134134
}
135135

136136
void ClientModel::updateTimer()

src/rpc/blockchain.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "sync.h"
2020
#include "txmempool.h"
2121
#include "util.h"
22+
#include "utiltime.h"
2223
#include "utilstrencodings.h"
2324
#include "hash.h"
2425

@@ -1048,6 +1049,7 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
10481049
" \"bestblockhash\": \"...\", (string) the hash of the currently best block\n"
10491050
" \"mediantime\": xxxxxx, (numeric) median time for the current best block\n"
10501051
" \"verificationprogress\": xxxx, (numeric) estimate of verification progress [0..1]\n"
1052+
" \"initialblockdownload\": xx, (boolean) whether or not the initial block download is still ongoing\n"
10511053
" \"pruned\": xx, (boolean) if the blocks are subject to pruning\n"
10521054
" \"pruneheight\": xxxxxx, (numeric) lowest-height complete block stored\n"
10531055
" \"bip9_softforks\": { (object) status of BIP9 softforks in progress\n"
@@ -1074,10 +1076,11 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
10741076
obj.push_back(Pair("headers", pindexBestHeader ? pindexBestHeader->nHeight : -1));
10751077
obj.push_back(Pair("bestblockhash", tip->GetBlockHash().GetHex()));
10761078
obj.push_back(Pair("mediantime", (int64_t)tip->GetMedianTimePast()));
1077-
obj.push_back(Pair("verificationprogress", GuessVerificationProgress(Params().TxData(), tip)));
1079+
obj.push_back(Pair("verificationprogress", GuessVerificationProgress(tip, Params().GetConsensus().nPowTargetSpacing)));
10781080
obj.push_back(Pair("pruned", fPruneMode));
10791081
obj.push_back(Pair("signblock_asm", ScriptToAsmStr(tip->proof.challenge)));
10801082
obj.push_back(Pair("signblock_hex", HexStr(tip->proof.challenge.begin(), tip->proof.challenge.end())));
1083+
obj.push_back(Pair("initialblockdownload", IsInitialBlockDownload()));
10811084

10821085
const Consensus::Params& consensusParams = Params().GetConsensus();
10831086
UniValue bip9_softforks(UniValue::VOBJ);

src/validation.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3042,7 +3042,8 @@ void static UpdateTip(CBlockIndex *pindexNew, const CChainParams& chainParams) {
30423042
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(), chainActive.Tip()->nVersion,
30433043
log(chainActive.Tip()->nChainWork.getdouble())/log(2.0), (unsigned long)chainActive.Tip()->nChainTx,
30443044
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
3045-
GuessVerificationProgress(chainParams.TxData(), chainActive.Tip()), pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
3045+
GuessVerificationProgress(chainActive.Tip(), chainParams.GetConsensus().nPowTargetSpacing),
3046+
pcoinsTip->DynamicMemoryUsage() * (1.0 / (1<<20)), pcoinsTip->GetCacheSize());
30463047
if (!warningMessages.empty())
30473048
LogPrintf(" warning='%s'", boost::algorithm::join(warningMessages, ", "));
30483049
LogPrintf("\n");
@@ -4609,10 +4610,10 @@ bool static LoadBlockIndexDB(const CChainParams& chainparams)
46094610

46104611
PruneBlockIndexCandidates();
46114612

4612-
LogPrintf("%s: hashBestChain=%s height=%d date=%s progress=%f\n", __func__,
4613+
LogPrintf("%s: hashBestChain=%s height=%d date=%s progress=%.3f\n", __func__,
46134614
chainActive.Tip()->GetBlockHash().ToString(), chainActive.Height(),
46144615
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", chainActive.Tip()->GetBlockTime()),
4615-
GuessVerificationProgress(chainparams.TxData(), chainActive.Tip()));
4616+
GuessVerificationProgress(chainActive.Tip(), chainparams.GetConsensus().nPowTargetSpacing));
46164617

46174618
return true;
46184619
}
@@ -5317,22 +5318,21 @@ void DumpMempool(void)
53175318
}
53185319
}
53195320

5320-
//! Guess how far we are in the verification process at the given block index
5321-
double GuessVerificationProgress(const ChainTxData& data, CBlockIndex *pindex) {
5322-
if (pindex == NULL)
5321+
// Guess how far we are in the verification process at the given block index.
5322+
// Since we have signed fixed-interval blocks, estimating progress is a very easy.
5323+
// We can extrapolate the last block time to the current time to estimate how many more blocks
5324+
// we expect.
5325+
double GuessVerificationProgress(CBlockIndex *pindex, int64_t blockInterval) {
5326+
if (pindex == NULL || pindex->nHeight < 1)
53235327
return 0.0;
53245328

5325-
int64_t nNow = time(NULL);
5326-
5327-
double fTxTotal;
5328-
5329-
if (pindex->nChainTx <= data.nTxCount) {
5330-
fTxTotal = data.nTxCount + (nNow - data.nTime) * data.dTxRate;
5331-
} else {
5332-
fTxTotal = pindex->nChainTx + (nNow - pindex->GetBlockTime()) * data.dTxRate;
5333-
}
5334-
5335-
return pindex->nChainTx / fTxTotal;
5329+
int64_t nNow = GetTime();
5330+
int64_t moreBlocksExpected = (nNow - pindex->GetBlockTime()) / blockInterval;
5331+
double progress = (pindex->nHeight + 0.0) / (pindex->nHeight + moreBlocksExpected);
5332+
// Round to 3 digits to avoid 0.999999 when finished.
5333+
progress = ceil(progress * 1000.0) / 1000.0;
5334+
// Avoid higher than one if last block is newer than current time.
5335+
return std::min(1.0, progress);
53365336
}
53375337

53385338
class CMainCleanup

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ bool ActivateBestChain(CValidationState& state, const CChainParams& chainparams,
287287
CAmount GetBlockSubsidy(int nHeight, const Consensus::Params& consensusParams);
288288

289289
/** Guess verification progress (as a fraction between 0.0=genesis and 1.0=current tip). */
290-
double GuessVerificationProgress(const ChainTxData& data, CBlockIndex* pindex);
290+
double GuessVerificationProgress(CBlockIndex* pindex, int64_t blockInterval);
291291

292292
/**
293293
* Prune block and undo files (blk???.dat and undo???.dat) so that the disk space used is less than a user-defined target.

src/wallet/wallet.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,12 +1692,14 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool f
16921692
pindex = chainActive.Next(pindex);
16931693

16941694
ShowProgress(_("Rescanning..."), 0); // show rescan progress in GUI as dialog or on splashscreen, if -rescan on startup
1695-
double dProgressStart = GuessVerificationProgress(chainParams.TxData(), pindex);
1696-
double dProgressTip = GuessVerificationProgress(chainParams.TxData(), chainActive.Tip());
1695+
double dProgressStart = GuessVerificationProgress(pindex, chainParams.GetConsensus().nPowTargetSpacing);
1696+
double dProgressTip = GuessVerificationProgress(chainActive.Tip(), chainParams.GetConsensus().nPowTargetSpacing);
16971697
while (pindex)
16981698
{
1699-
if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0)
1700-
ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((GuessVerificationProgress(chainParams.TxData(), pindex) - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
1699+
if (pindex->nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0) {
1700+
double progress = GuessVerificationProgress(pindex, chainParams.GetConsensus().nPowTargetSpacing);
1701+
ShowProgress(_("Rescanning..."), std::max(1, std::min(99, (int)((progress - dProgressStart) / (dProgressTip - dProgressStart) * 100))));
1702+
}
17011703

17021704
CBlock block;
17031705
if (ReadBlockFromDisk(block, pindex, Params().GetConsensus())) {
@@ -1713,7 +1715,8 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool f
17131715
pindex = chainActive.Next(pindex);
17141716
if (GetTime() >= nNow + 60) {
17151717
nNow = GetTime();
1716-
LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, GuessVerificationProgress(chainParams.TxData(), pindex));
1718+
double progress = GuessVerificationProgress(pindex, chainParams.GetConsensus().nPowTargetSpacing);
1719+
LogPrintf("Still rescanning. At block %d. Progress=%f\n", pindex->nHeight, progress);
17171720
}
17181721
}
17191722
ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI

0 commit comments

Comments
 (0)