Skip to content

Commit 5c87268

Browse files
johnny9shaavan
authored andcommitted
qml: add sync time related properties to NodeModel
The properties will allow the gui to show an estimate for the time remaining before the node is synced. Github-Pull: #220 Rebased-From: eb945ac Co-authored-by: shaavan <shaavan.github@gmail.com>
1 parent 76209dd commit 5c87268

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/qml/BitcoinApp/nodemodel.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cassert>
1111
#include <chrono>
1212

13+
#include <QDateTime>
1314
#include <QMetaObject>
1415
#include <QTimerEvent>
1516

@@ -27,9 +28,46 @@ void NodeModel::setBlockTipHeight(int new_height)
2728
}
2829
}
2930

31+
void NodeModel::setRemainingSyncTime(double new_progress)
32+
{
33+
int currentTime = QDateTime::currentDateTime().toMSecsSinceEpoch();
34+
35+
// keep a vector of samples of verification progress at height
36+
m_block_process_time.push_front(qMakePair(currentTime, new_progress));
37+
38+
// show progress speed if we have more than one sample
39+
if (m_block_process_time.size() >= 2) {
40+
double progressDelta = 0;
41+
int timeDelta = 0;
42+
int remainingMSecs = 0;
43+
double remainingProgress = 1.0 - new_progress;
44+
for (int i = 1; i < m_block_process_time.size(); i++) {
45+
QPair<int, double> sample = m_block_process_time[i];
46+
47+
// take first sample after 500 seconds or last available one
48+
if (sample.first < (currentTime - 500 * 1000) || i == m_block_process_time.size() - 1) {
49+
progressDelta = m_block_process_time[0].second - sample.second;
50+
timeDelta = m_block_process_time[0].first - sample.first;
51+
remainingMSecs = (progressDelta > 0) ? remainingProgress / progressDelta * timeDelta : -1;
52+
break;
53+
}
54+
}
55+
if (remainingMSecs > 0 && m_block_process_time.count() % 1000 == 0) {
56+
m_remaining_sync_time = remainingMSecs;
57+
58+
Q_EMIT remainingSyncTimeChanged();
59+
}
60+
static const int MAX_SAMPLES = 5000;
61+
if (m_block_process_time.count() > MAX_SAMPLES) {
62+
m_block_process_time.remove(1, m_block_process_time.count() - 1);
63+
}
64+
}
65+
}
3066
void NodeModel::setVerificationProgress(double new_progress)
3167
{
3268
if (new_progress != m_verification_progress) {
69+
setRemainingSyncTime(new_progress);
70+
3371
m_verification_progress = new_progress;
3472
Q_EMIT verificationProgressChanged();
3573
}
@@ -54,6 +92,8 @@ void NodeModel::initializeResult([[maybe_unused]] bool success, interfaces::Bloc
5492
// TODO: Handle the `success` parameter,
5593
setBlockTipHeight(tip_info.block_height);
5694
setVerificationProgress(tip_info.verification_progress);
95+
96+
Q_EMIT setTimeRatioListInitial();
5797
}
5898

5999
void NodeModel::startShutdownPolling()
@@ -84,6 +124,8 @@ void NodeModel::ConnectToBlockTipSignal()
84124
QMetaObject::invokeMethod(this, [this, tip, verification_progress] {
85125
setBlockTipHeight(tip.block_height);
86126
setVerificationProgress(verification_progress);
127+
128+
Q_EMIT setTimeRatioList(tip.block_time);
87129
});
88130
});
89131
}

src/qml/BitcoinApp/nodemodel.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class NodeModel : public QObject
2525
{
2626
Q_OBJECT
2727
Q_PROPERTY(int blockTipHeight READ blockTipHeight NOTIFY blockTipHeightChanged)
28+
Q_PROPERTY(int remainingSyncTime READ remainingSyncTime NOTIFY remainingSyncTimeChanged)
2829
Q_PROPERTY(double verificationProgress READ verificationProgress NOTIFY verificationProgressChanged)
2930
Q_PROPERTY(bool pause READ pause WRITE setPause NOTIFY pauseChanged)
3031

@@ -33,6 +34,8 @@ class NodeModel : public QObject
3334

3435
int blockTipHeight() const { return m_block_tip_height; }
3536
void setBlockTipHeight(int new_height);
37+
int remainingSyncTime() const { return m_remaining_sync_time; }
38+
void setRemainingSyncTime(double new_progress);
3639
double verificationProgress() const { return m_verification_progress; }
3740
void setVerificationProgress(double new_progress);
3841
bool pause() const { return m_pause; }
@@ -48,22 +51,29 @@ public Q_SLOTS:
4851

4952
Q_SIGNALS:
5053
void blockTipHeightChanged();
54+
void remainingSyncTimeChanged();
5155
void requestedInitialize();
5256
void requestedShutdown();
5357
void verificationProgressChanged();
5458
void pauseChanged(bool new_pause);
5559

60+
void setTimeRatioList(int new_time);
61+
void setTimeRatioListInitial();
62+
5663
protected:
5764
void timerEvent(QTimerEvent* event) override;
5865

5966
private:
6067
// Properties that are exposed to QML.
6168
int m_block_tip_height{0};
69+
int m_remaining_sync_time{0};
6270
double m_verification_progress{0.0};
6371
bool m_pause{false};
6472

6573
int m_shutdown_polling_timer_id{0};
6674

75+
QVector<QPair<int, double>> m_block_process_time;
76+
6777
interfaces::Node& m_node;
6878
std::unique_ptr<interfaces::Handler> m_handler_notify_block_tip;
6979

0 commit comments

Comments
 (0)