Skip to content

Commit e7f1255

Browse files
committed
Merge #11237: qt: Fixing division by zero in time remaining
c8d38ab Refactor tipUpdate as per style guide (MeshCollider) 3b69a08 Fix division by zero in time remaining (MeshCollider) Pull request description: Fixes bitcoin/bitcoin#10291, bitcoin/bitcoin#11265 progressDelta may be 0 (or even negative according to 11265), this checks for that and prints unknown if it is, because we cannot calculate an estimate for the time remaining (would be infinite or negative). Tree-SHA512: bc5708e5ed6e4670d008219558c5fbb25709bd99a32c98ec39bb74f94a0b7fa058f3d03389ccdd39e6723e6b5b48e34b13ceee7c051c2db631e51d8ec3e1d68c
2 parents a3624dd + c8d38ab commit e7f1255

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/qt/modaloverlay.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,36 +82,38 @@ void ModalOverlay::tipUpdate(int count, const QDateTime& blockDate, double nVeri
8282
blockProcessTime.push_front(qMakePair(currentDate.toMSecsSinceEpoch(), nVerificationProgress));
8383

8484
// show progress speed if we have more then one sample
85-
if (blockProcessTime.size() >= 2)
86-
{
87-
double progressStart = blockProcessTime[0].second;
85+
if (blockProcessTime.size() >= 2) {
8886
double progressDelta = 0;
8987
double progressPerHour = 0;
9088
qint64 timeDelta = 0;
9189
qint64 remainingMSecs = 0;
9290
double remainingProgress = 1.0 - nVerificationProgress;
93-
for (int i = 1; i < blockProcessTime.size(); i++)
94-
{
91+
for (int i = 1; i < blockProcessTime.size(); i++) {
9592
QPair<qint64, double> sample = blockProcessTime[i];
9693

9794
// take first sample after 500 seconds or last available one
9895
if (sample.first < (currentDate.toMSecsSinceEpoch() - 500 * 1000) || i == blockProcessTime.size() - 1) {
99-
progressDelta = progressStart-sample.second;
96+
progressDelta = blockProcessTime[0].second - sample.second;
10097
timeDelta = blockProcessTime[0].first - sample.first;
101-
progressPerHour = progressDelta/(double)timeDelta*1000*3600;
102-
remainingMSecs = remainingProgress / progressDelta * timeDelta;
98+
progressPerHour = progressDelta / (double) timeDelta * 1000 * 3600;
99+
remainingMSecs = (progressDelta > 0) ? remainingProgress / progressDelta * timeDelta : -1;
103100
break;
104101
}
105102
}
106103
// show progress increase per hour
107-
ui->progressIncreasePerH->setText(QString::number(progressPerHour*100, 'f', 2)+"%");
104+
ui->progressIncreasePerH->setText(QString::number(progressPerHour * 100, 'f', 2)+"%");
108105

109106
// show expected remaining time
110-
ui->expectedTimeLeft->setText(GUIUtil::formatNiceTimeOffset(remainingMSecs/1000.0));
107+
if(remainingMSecs >= 0) {
108+
ui->expectedTimeLeft->setText(GUIUtil::formatNiceTimeOffset(remainingMSecs / 1000.0));
109+
} else {
110+
ui->expectedTimeLeft->setText(QObject::tr("unknown"));
111+
}
111112

112113
static const int MAX_SAMPLES = 5000;
113-
if (blockProcessTime.count() > MAX_SAMPLES)
114-
blockProcessTime.remove(MAX_SAMPLES, blockProcessTime.count()-MAX_SAMPLES);
114+
if (blockProcessTime.count() > MAX_SAMPLES) {
115+
blockProcessTime.remove(MAX_SAMPLES, blockProcessTime.count() - MAX_SAMPLES);
116+
}
115117
}
116118

117119
// show the last block date

0 commit comments

Comments
 (0)