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