-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathnodemodel.cpp
283 lines (241 loc) · 8.23 KB
/
nodemodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright (c) 2021 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <qml/models/nodemodel.h>
#include <interfaces/node.h>
#include <net.h>
#include <node/interface_ui.h>
#include <validation.h>
#include <cassert>
#include <chrono>
#include <QDateTime>
#include <QMetaObject>
#include <QTimerEvent>
#include <QString>
NodeModel::NodeModel(interfaces::Node& node)
: m_node{node}
{
ConnectToBlockTipSignal();
ConnectToNumConnectionsChangedSignal();
}
void NodeModel::setBlockTipHeight(int new_height)
{
if (new_height != m_block_tip_height) {
m_block_tip_height = new_height;
Q_EMIT blockTipHeightChanged();
}
}
void NodeModel::setNumOutboundPeers(int new_num)
{
if (new_num != m_num_outbound_peers) {
m_num_outbound_peers = new_num;
bool new_connected = (m_num_outbound_peers > 0);
if (new_connected != m_connected) {
setConnected(new_connected);
}
Q_EMIT numOutboundPeersChanged();
}
}
void NodeModel::setConnected(bool new_connected)
{
if (new_connected != m_connected) {
m_connected = new_connected;
Q_EMIT connectedChanged();
}
}
void NodeModel::setEstimatingSyncTime(bool new_estimating)
{
if (m_estimating_sync_time != new_estimating) {
m_estimating_sync_time = new_estimating;
Q_EMIT estimatingSyncTimeChanged();
}
}
void NodeModel::setRemainingSyncTime(double new_progress)
{
int currentTime = QDateTime::currentDateTime().toMSecsSinceEpoch();
// keep a vector of samples of verification progress at height
m_block_process_time.push_front(qMakePair(currentTime, new_progress));
// show progress speed if we have more than one sample
if (m_block_process_time.size() >= 2) {
double progressDelta = 0;
int timeDelta = 0;
int remainingMSecs = 0;
double remainingProgress = 1.0 - new_progress;
for (int i = 1; i < m_block_process_time.size(); i++) {
QPair<int, double> sample = m_block_process_time[i];
// take first sample after 500 seconds or last available one
if (sample.first < (currentTime - 500 * 1000) || i == m_block_process_time.size() - 1) {
progressDelta = m_block_process_time[0].second - sample.second;
timeDelta = m_block_process_time[0].first - sample.first;
remainingMSecs = (progressDelta > 0) ? remainingProgress / progressDelta * timeDelta : -1;
break;
}
}
if (remainingMSecs > 0 && m_block_process_time.count() % 1000 == 0) {
m_remaining_sync_time = remainingMSecs;
setFormattedRemainingSyncTime(m_remaining_sync_time);
Q_EMIT remainingSyncTimeChanged();
}
static const int MAX_SAMPLES = 5000;
if (m_block_process_time.count() > MAX_SAMPLES) {
m_block_process_time.remove(1, m_block_process_time.count() - 1);
}
} else {
m_remaining_sync_time = 0;
setFormattedRemainingSyncTime(m_remaining_sync_time);
Q_EMIT remainingSyncTimeChanged();
}
}
void NodeModel::setFormattedRemainingSyncTime(int new_time)
{
int minutes = new_time / 60000;
int seconds = (new_time % 60000) / 1000;
int weeks = minutes / 10080;
minutes %= 10080;
int days = minutes / 1440;
minutes %= 1440;
int hours = minutes / 60;
minutes %= 60;
if (weeks > 0) {
m_formatted_remaining_sync_time = QObject::tr("~%1 %2 left")
.arg(weeks)
.arg(weeks == 1 ? QObject::tr("week") : QObject::tr("weeks"));
setEstimatingSyncTime(false);
} else if (days > 0) {
m_formatted_remaining_sync_time = QObject::tr("~%1 %2 left")
.arg(days)
.arg(days == 1 ? QObject::tr("day") : QObject::tr("days"));
setEstimatingSyncTime(false);
} else if (hours >= 5) {
m_formatted_remaining_sync_time = QObject::tr("~%1 %2 left")
.arg(hours)
.arg(hours == 1 ? QObject::tr("hour") : QObject::tr("hours"));
setEstimatingSyncTime(false);
} else if (hours > 0) {
m_formatted_remaining_sync_time = QObject::tr("~%1h %2m left")
.arg(hours)
.arg(minutes);
setEstimatingSyncTime(false);
} else if (minutes >= 5) {
m_formatted_remaining_sync_time = QObject::tr("~%1 %2 left")
.arg(minutes)
.arg(minutes == 1 ? QObject::tr("minute") : QObject::tr("minutes"));
setEstimatingSyncTime(false);
} else if (minutes > 0) {
m_formatted_remaining_sync_time = QObject::tr("~%1m %2s left")
.arg(minutes)
.arg(seconds);
setEstimatingSyncTime(false);
} else if (seconds > 0) {
m_formatted_remaining_sync_time = QObject::tr("~%1 %2 left")
.arg(seconds)
.arg(seconds == 1 ? QObject::tr("second") : QObject::tr("seconds"));
setEstimatingSyncTime(false);
} else {
if (m_synced) {
setEstimatingSyncTime(false);
} else {
m_formatted_remaining_sync_time = QObject::tr("Estimating");
setEstimatingSyncTime(true);
}
}
}
void NodeModel::setVerificationProgress(double new_progress)
{
if (new_progress != m_verification_progress) {
setRemainingSyncTime(new_progress);
setFormattedVerificationProgress(new_progress);
if (new_progress >= 0.999) {
setSynced(true);
}
m_verification_progress = new_progress;
Q_EMIT verificationProgressChanged();
}
}
void NodeModel::setFormattedVerificationProgress(double new_progress)
{
double percentage = new_progress * 100.00;
if (percentage >= 99.99) {
m_formatted_verification_progress = QStringLiteral("100%");
} else if (percentage == 0.0) {
m_formatted_verification_progress = QStringLiteral("0%");
} else {
int decimal_places = (percentage >= 10.0) ? 1 : 2;
m_formatted_verification_progress = QString::number(percentage, 'f', decimal_places) + '%';
}
}
void NodeModel::setSynced(bool new_synced)
{
if (m_synced != new_synced) {
m_synced = new_synced;
Q_EMIT syncedChanged();
}
}
void NodeModel::setPause(bool new_pause)
{
if(m_pause != new_pause) {
m_pause = new_pause;
m_node.setNetworkActive(!new_pause);
Q_EMIT pauseChanged(new_pause);
}
}
void NodeModel::setErrorState(bool faulted)
{
if (m_faulted != faulted) {
m_faulted = faulted;
Q_EMIT errorStateChanged(faulted);
}
}
void NodeModel::startNodeInitializionThread()
{
Q_EMIT requestedInitialize();
}
void NodeModel::requestShutdown()
{
Q_EMIT requestedShutdown();
}
void NodeModel::initializeResult(bool success, interfaces::BlockAndHeaderTipInfo tip_info)
{
if (!success) {
setErrorState(true);
}
setBlockTipHeight(tip_info.block_height);
setVerificationProgress(tip_info.verification_progress);
Q_EMIT setTimeRatioListInitial();
}
void NodeModel::startShutdownPolling()
{
m_shutdown_polling_timer_id = startTimer(200ms);
}
void NodeModel::stopShutdownPolling()
{
killTimer(m_shutdown_polling_timer_id);
}
void NodeModel::timerEvent(QTimerEvent* event)
{
Q_UNUSED(event)
if (m_node.shutdownRequested()) {
stopShutdownPolling();
Q_EMIT requestedShutdown();
}
}
void NodeModel::ConnectToBlockTipSignal()
{
assert(!m_handler_notify_block_tip);
m_handler_notify_block_tip = m_node.handleNotifyBlockTip(
[this](SynchronizationState state, interfaces::BlockTip tip, double verification_progress) {
QMetaObject::invokeMethod(this, [=] {
setBlockTipHeight(tip.block_height);
setVerificationProgress(verification_progress);
Q_EMIT setTimeRatioList(tip.block_time);
});
});
}
void NodeModel::ConnectToNumConnectionsChangedSignal()
{
assert(!m_handler_notify_num_peers_changed);
m_handler_notify_num_peers_changed = m_node.handleNotifyNumConnectionsChanged(
[this](PeersNumByType new_num_peers) {
setNumOutboundPeers(new_num_peers.outbound_full_relay + new_num_peers.block_relay);
});
}