Bitcoin Core  25.99.0
P2P Digital Currency
modaloverlay.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2022 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <qt/modaloverlay.h>
6 #include <qt/forms/ui_modaloverlay.h>
7 
8 #include <chainparams.h>
9 #include <qt/guiutil.h>
10 
11 #include <QEasingCurve>
12 #include <QPropertyAnimation>
13 #include <QResizeEvent>
14 
15 ModalOverlay::ModalOverlay(bool enable_wallet, QWidget* parent)
16  : QWidget(parent),
17  ui(new Ui::ModalOverlay),
18  bestHeaderDate(QDateTime())
19 {
20  ui->setupUi(this);
21  connect(ui->closeButton, &QPushButton::clicked, this, &ModalOverlay::closeClicked);
22  if (parent) {
23  parent->installEventFilter(this);
24  raise();
25  }
26 
27  blockProcessTime.clear();
28  setVisible(false);
29  if (!enable_wallet) {
30  ui->infoText->setVisible(false);
31  ui->infoTextStrong->setText(tr("%1 is currently syncing. It will download headers and blocks from peers and validate them until reaching the tip of the block chain.").arg(PACKAGE_NAME));
32  }
33 
34  m_animation.setTargetObject(this);
35  m_animation.setPropertyName("pos");
36  m_animation.setDuration(300 /* ms */);
37  m_animation.setEasingCurve(QEasingCurve::OutQuad);
38 }
39 
41 {
42  delete ui;
43 }
44 
45 bool ModalOverlay::eventFilter(QObject * obj, QEvent * ev) {
46  if (obj == parent()) {
47  if (ev->type() == QEvent::Resize) {
48  QResizeEvent * rev = static_cast<QResizeEvent*>(ev);
49  resize(rev->size());
50  if (!layerIsVisible)
51  setGeometry(0, height(), width(), height());
52 
53  if (m_animation.endValue().toPoint().y() > 0) {
54  m_animation.setEndValue(QPoint(0, height()));
55  }
56  }
57  else if (ev->type() == QEvent::ChildAdded) {
58  raise();
59  }
60  }
61  return QWidget::eventFilter(obj, ev);
62 }
63 
65 bool ModalOverlay::event(QEvent* ev) {
66  if (ev->type() == QEvent::ParentAboutToChange) {
67  if (parent()) parent()->removeEventFilter(this);
68  }
69  else if (ev->type() == QEvent::ParentChange) {
70  if (parent()) {
71  parent()->installEventFilter(this);
72  raise();
73  }
74  }
75  return QWidget::event(ev);
76 }
77 
78 void ModalOverlay::setKnownBestHeight(int count, const QDateTime& blockDate, bool presync)
79 {
80  if (!presync && count > bestHeaderHeight) {
82  bestHeaderDate = blockDate;
84  }
85  if (presync) {
86  UpdateHeaderPresyncLabel(count, blockDate);
87  }
88 }
89 
90 void ModalOverlay::tipUpdate(int count, const QDateTime& blockDate, double nVerificationProgress)
91 {
92  QDateTime currentDate = QDateTime::currentDateTime();
93 
94  // keep a vector of samples of verification progress at height
95  blockProcessTime.push_front(qMakePair(currentDate.toMSecsSinceEpoch(), nVerificationProgress));
96 
97  // show progress speed if we have more than one sample
98  if (blockProcessTime.size() >= 2) {
99  double progressDelta = 0;
100  double progressPerHour = 0;
101  qint64 timeDelta = 0;
102  qint64 remainingMSecs = 0;
103  double remainingProgress = 1.0 - nVerificationProgress;
104  for (int i = 1; i < blockProcessTime.size(); i++) {
105  QPair<qint64, double> sample = blockProcessTime[i];
106 
107  // take first sample after 500 seconds or last available one
108  if (sample.first < (currentDate.toMSecsSinceEpoch() - 500 * 1000) || i == blockProcessTime.size() - 1) {
109  progressDelta = blockProcessTime[0].second - sample.second;
110  timeDelta = blockProcessTime[0].first - sample.first;
111  progressPerHour = (progressDelta > 0) ? progressDelta / (double)timeDelta * 1000 * 3600 : 0;
112  remainingMSecs = (progressDelta > 0) ? remainingProgress / progressDelta * timeDelta : -1;
113  break;
114  }
115  }
116  // show progress increase per hour
117  ui->progressIncreasePerH->setText(QString::number(progressPerHour * 100, 'f', 2)+"%");
118 
119  // show expected remaining time
120  if(remainingMSecs >= 0) {
121  ui->expectedTimeLeft->setText(GUIUtil::formatNiceTimeOffset(remainingMSecs / 1000.0));
122  } else {
123  ui->expectedTimeLeft->setText(QObject::tr("unknown"));
124  }
125 
126  static const int MAX_SAMPLES = 5000;
127  if (blockProcessTime.count() > MAX_SAMPLES) {
128  blockProcessTime.remove(MAX_SAMPLES, blockProcessTime.count() - MAX_SAMPLES);
129  }
130  }
131 
132  // show the last block date
133  ui->newestBlockDate->setText(blockDate.toString());
134 
135  // show the percentage done according to nVerificationProgress
136  ui->percentageProgress->setText(QString::number(nVerificationProgress*100, 'f', 2)+"%");
137 
138  if (!bestHeaderDate.isValid())
139  // not syncing
140  return;
141 
142  // estimate the number of headers left based on nPowTargetSpacing
143  // and check if the gui is not aware of the best header (happens rarely)
144  int estimateNumHeadersLeft = bestHeaderDate.secsTo(currentDate) / Params().GetConsensus().nPowTargetSpacing;
145  bool hasBestHeader = bestHeaderHeight >= count;
146 
147  // show remaining number of blocks
148  if (estimateNumHeadersLeft < HEADER_HEIGHT_DELTA_SYNC && hasBestHeader) {
149  ui->numberOfBlocksLeft->setText(QString::number(bestHeaderHeight - count));
150  } else {
152  ui->expectedTimeLeft->setText(tr("Unknown…"));
153  }
154 }
155 
157  int est_headers_left = bestHeaderDate.secsTo(QDateTime::currentDateTime()) / Params().GetConsensus().nPowTargetSpacing;
158  ui->numberOfBlocksLeft->setText(tr("Unknown. Syncing Headers (%1, %2%)…").arg(bestHeaderHeight).arg(QString::number(100.0 / (bestHeaderHeight + est_headers_left) * bestHeaderHeight, 'f', 1)));
159 }
160 
161 void ModalOverlay::UpdateHeaderPresyncLabel(int height, const QDateTime& blockDate) {
162  int est_headers_left = blockDate.secsTo(QDateTime::currentDateTime()) / Params().GetConsensus().nPowTargetSpacing;
163  ui->numberOfBlocksLeft->setText(tr("Unknown. Pre-syncing Headers (%1, %2%)…").arg(height).arg(QString::number(100.0 / (height + est_headers_left) * height, 'f', 1)));
164 }
165 
167 {
168  showHide(layerIsVisible, true);
169  if (!layerIsVisible)
170  userClosed = true;
171 }
172 
173 void ModalOverlay::showHide(bool hide, bool userRequested)
174 {
175  if ( (layerIsVisible && !hide) || (!layerIsVisible && hide) || (!hide && userClosed && !userRequested))
176  return;
177 
178  Q_EMIT triggered(hide);
179 
180  if (!isVisible() && !hide)
181  setVisible(true);
182 
183  m_animation.setStartValue(QPoint(0, hide ? 0 : height()));
184  // The eventFilter() updates the endValue if it is required for QEvent::Resize.
185  m_animation.setEndValue(QPoint(0, hide ? height() : 0));
186  m_animation.start(QAbstractAnimation::KeepWhenStopped);
187  layerIsVisible = !hide;
188 }
189 
191 {
192  showHide(true);
193  userClosed = true;
194 }
#define PACKAGE_NAME
const CChainParams & Params()
Return the currently selected parameters.
const Consensus::Params & GetConsensus() const
Definition: chainparams.h:88
Modal overlay to display information about the chain-sync state.
Definition: modaloverlay.h:21
void showHide(bool hide=false, bool userRequested=false)
bool event(QEvent *ev) override
Tracks parent widget changes.
void tipUpdate(int count, const QDateTime &blockDate, double nVerificationProgress)
void UpdateHeaderSyncLabel()
ModalOverlay(bool enable_wallet, QWidget *parent)
void toggleVisibility()
QDateTime bestHeaderDate
Definition: modaloverlay.h:49
bool layerIsVisible
Definition: modaloverlay.h:51
void triggered(bool hidden)
Ui::ModalOverlay * ui
Definition: modaloverlay.h:47
void closeClicked()
void setKnownBestHeight(int count, const QDateTime &blockDate, bool presync)
QVector< QPair< qint64, double > > blockProcessTime
Definition: modaloverlay.h:50
int bestHeaderHeight
Definition: modaloverlay.h:48
bool eventFilter(QObject *obj, QEvent *ev) override
void UpdateHeaderPresyncLabel(int height, const QDateTime &blockDate)
QPropertyAnimation m_animation
Definition: modaloverlay.h:53
static constexpr int HEADER_HEIGHT_DELTA_SYNC
The required delta of headers to the estimated number of available headers until we show the IBD prog...
Definition: modaloverlay.h:13
QString formatNiceTimeOffset(qint64 secs)
Definition: guiutil.cpp:776
int64_t nPowTargetSpacing
Definition: params.h:112
static int count