Skip to content

The Block Clock #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ share/qt/Info.plist

src/qt/*.moc
src/qt/moc_*.cpp
src/qml/components/moc_*.cpp
src/qt/forms/ui_*.h

src/qt/test/moc*.cpp
Expand Down
7 changes: 7 additions & 0 deletions src/Makefile.qt.include
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ QT_FORMS_UI = \
qt/forms/transactiondescdialog.ui

QT_MOC_CPP = \
qml/components/moc_blockclockdial.cpp \
qml/moc_appmode.cpp \
qml/moc_chainmodel.cpp \
qml/moc_nodemodel.cpp \
qml/moc_options_model.cpp \
qt/moc_addressbookpage.cpp \
Expand Down Expand Up @@ -111,6 +113,8 @@ QT_QRC_LOCALE = qt/bitcoin_locale.qrc
BITCOIN_QT_H = \
qml/appmode.h \
qml/bitcoin.h \
qml/chainmodel.h \
qml/components/blockclockdial.h \
qml/imageprovider.h \
qml/nodemodel.h \
qml/options_model.h \
Expand Down Expand Up @@ -291,6 +295,8 @@ BITCOIN_QT_WALLET_CPP = \

BITCOIN_QML_BASE_CPP = \
qml/bitcoin.cpp \
qml/chainmodel.cpp \
qml/components/blockclockdial.cpp \
qml/imageprovider.cpp \
qml/nodemodel.cpp \
qml/options_model.cpp \
Expand Down Expand Up @@ -322,6 +328,7 @@ QML_QRC_CPP = qml/qrc_bitcoin.cpp
QML_QRC = qml/bitcoin_qml.qrc
QML_RES_QML = \
qml/components/AboutOptions.qml \
qml/components/BlockClock.qml \
qml/components/BlockCounter.qml \
qml/components/ConnectionOptions.qml \
qml/components/ConnectionSettings.qml \
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ class Chain
//! Get block hash. Height must be valid or this function will abort.
virtual uint256 getBlockHash(int height) = 0;

//! Get block time, Height must be valid or this function will abort.
virtual int64_t getBlockTime(int height) = 0;

//! Check that the block is available on disk (i.e. has not been
//! pruned), and contains transactions.
virtual bool haveBlockOnDisk(int height) = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/node/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,11 @@ class ChainImpl : public Chain
LOCK(::cs_main);
return Assert(chainman().ActiveChain()[height])->GetBlockHash();
}
int64_t getBlockTime(int height) override
{
LOCK(::cs_main);
return Assert(chainman().ActiveChain()[height])->GetBlockTime();
}
bool haveBlockOnDisk(int height) override
{
LOCK(::cs_main);
Expand Down
11 changes: 11 additions & 0 deletions src/qml/bitcoin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@

#include <chainparams.h>
#include <init.h>
#include <interfaces/chain.h>
#include <interfaces/init.h>
#include <interfaces/node.h>
#include <logging.h>
#include <node/interface_ui.h>
#include <noui.h>
#include <qml/appmode.h>
#include <qml/chainmodel.h>
#include <qml/components/blockclockdial.h>
#include <qml/imageprovider.h>
#include <qml/nodemodel.h>
#include <qml/options_model.h>
Expand Down Expand Up @@ -155,6 +158,7 @@ int QmlGuiMain(int argc, char* argv[])
GUIUtil::LogQtInfo();

std::unique_ptr<interfaces::Node> node = init->makeNode();
std::unique_ptr<interfaces::Chain> chain = init->makeChain();
if (!node->baseInitialize()) {
// A dialog with detailed error will have been shown by InitError().
return EXIT_FAILURE;
Expand All @@ -170,6 +174,11 @@ int QmlGuiMain(int argc, char* argv[])
QObject::connect(&init_executor, &InitExecutor::shutdownResult, qGuiApp, &QGuiApplication::quit, Qt::QueuedConnection);
// QObject::connect(&init_executor, &InitExecutor::runawayException, &node_model, &NodeModel::handleRunawayException);

ChainModel chain_model{*chain};

QObject::connect(&node_model, &NodeModel::setTimeRatioList, &chain_model, &ChainModel::setTimeRatioList);
QObject::connect(&node_model, &NodeModel::setTimeRatioListInitial, &chain_model, &ChainModel::setTimeRatioListInitial);

qGuiApp->setQuitOnLastWindowClosed(false);
QObject::connect(qGuiApp, &QGuiApplication::lastWindowClosed, [&] {
node->startShutdown();
Expand All @@ -185,6 +194,7 @@ int QmlGuiMain(int argc, char* argv[])
engine.addImageProvider(QStringLiteral("images"), new ImageProvider{network_style.data()});

engine.rootContext()->setContextProperty("nodeModel", &node_model);
engine.rootContext()->setContextProperty("chainModel", &chain_model);

OptionsQmlModel options_model{*node};
engine.rootContext()->setContextProperty("optionsModel", &options_model);
Expand All @@ -196,6 +206,7 @@ int QmlGuiMain(int argc, char* argv[])
#endif // __ANDROID__

qmlRegisterSingletonInstance<AppMode>("org.bitcoincore.qt", 1, 0, "AppMode", &app_mode);
qmlRegisterType<BlockClockDial>("org.bitcoincore.qt", 1, 0, "BlockClockDial");

engine.load(QUrl(QStringLiteral("qrc:///qml/pages/main.qml")));
if (engine.rootObjects().isEmpty()) {
Expand Down
1 change: 1 addition & 0 deletions src/qml/bitcoin_qml.qrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/qml">
<file>components/AboutOptions.qml</file>
<file>components/BlockClock.qml</file>
<file>components/BlockCounter.qml</file>
<file>components/ConnectionOptions.qml</file>
<file>components/ConnectionSettings.qml</file>
Expand Down
91 changes: 91 additions & 0 deletions src/qml/chainmodel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2022 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/chainmodel.h>

#include <QDateTime>
#include <QThread>
#include <QTime>
#include <interfaces/chain.h>

ChainModel::ChainModel(interfaces::Chain& chain)
: m_chain{chain}
{
QTimer* timer = new QTimer();
connect(timer, &QTimer::timeout, this, &ChainModel::setCurrentTimeRatio);
timer->start(1000);

QThread* timer_thread = new QThread;
timer->moveToThread(timer_thread);
timer_thread->start();
}

void ChainModel::setTimeRatioList(int new_time)
{
if (m_time_ratio_list.isEmpty()) {
setTimeRatioListInitial();
}
int time_at_meridian = timestampAtMeridian();

if (new_time < time_at_meridian) {
return;
}
m_time_ratio_list.push_back(double(new_time - time_at_meridian) / SECS_IN_12_HOURS);

Q_EMIT timeRatioListChanged();
}

int ChainModel::timestampAtMeridian()
{
int secs_since_meridian = (QTime::currentTime().msecsSinceStartOfDay() / 1000) % SECS_IN_12_HOURS;
int current_timestamp = QDateTime::currentSecsSinceEpoch();

return current_timestamp - secs_since_meridian;
}

void ChainModel::setTimeRatioListInitial()
{
int time_at_meridian = timestampAtMeridian();
m_time_ratio_list.clear();
/* m_time_ratio_list[0] = current_time_ratio
* m_time_ratio_list[1] = 0
* These two positions remain fixed for these
* values in m_time_ratio_list */
m_time_ratio_list.push_back(double(QDateTime::currentSecsSinceEpoch() - time_at_meridian) / SECS_IN_12_HOURS);
m_time_ratio_list.push_back(0);

int first_block_height;
int active_chain_height = m_chain.getHeight().value();
bool success = m_chain.findFirstBlockWithTimeAndHeight(/*min_time=*/time_at_meridian, /*min_height=*/0, interfaces::FoundBlock().height(first_block_height));

if (!success) {
Q_EMIT timeRatioListChanged();
return;
}

for (int height = first_block_height; height < active_chain_height + 1; height++) {
m_time_ratio_list.push_back(double(m_chain.getBlockTime(height) - time_at_meridian) / SECS_IN_12_HOURS);
}

Q_EMIT timeRatioListChanged();
}

void ChainModel::setCurrentTimeRatio()
{
int secs_since_meridian = (QTime::currentTime().msecsSinceStartOfDay() / 1000) % SECS_IN_12_HOURS;
double current_time_ratio = double(secs_since_meridian) / SECS_IN_12_HOURS;

if (current_time_ratio < m_time_ratio_list[0].toDouble()) { // That means time has crossed a meridian
m_time_ratio_list.clear();
}

if (m_time_ratio_list.isEmpty()) {
m_time_ratio_list.push_back(current_time_ratio);
m_time_ratio_list.push_back(0);
} else {
m_time_ratio_list[0] = current_time_ratio;
}

Q_EMIT timeRatioListChanged();
}
54 changes: 54 additions & 0 deletions src/qml/chainmodel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2022 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#ifndef BITCOIN_QML_CHAINMODEL_H
#define BITCOIN_QML_CHAINMODEL_H

#include <interfaces/chain.h>

#include <QObject>
#include <QTimer>
#include <QVariant>

namespace interfaces {
class FoundBlock;
class Chain;
} // namespace interfaces

static const int SECS_IN_12_HOURS = 43200;

class ChainModel : public QObject
{
Q_OBJECT
Q_PROPERTY(QVariantList timeRatioList READ timeRatioList NOTIFY timeRatioListChanged)

public:
explicit ChainModel(interfaces::Chain& chain);

QVariantList timeRatioList() const { return m_time_ratio_list; };

int timestampAtMeridian();

void setCurrentTimeRatio();

public Q_SLOTS:
void setTimeRatioList(int new_time);
void setTimeRatioListInitial();

Q_SIGNALS:
void timeRatioListChanged();

private:
/* time_ratio: Ratio between the time at which an event
* happened and 12 hours. So, for example, if a block is
* found at 4 am or pm, the time_ratio would be 0.3.
* The m_time_ratio_list stores the time ratio value for
* the current_time and the time at which the blocks in
* the last 12 hours were mined. */
QVariantList m_time_ratio_list{0.0};

interfaces::Chain& m_chain;
};

#endif // BITCOIN_QML_CHAINMODEL_H
Loading