forked from bitcoin/bitcoin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge bitcoin-core/gui#381: refactor: Make BitcoinCore class reusable
8169fc4 qt, refactor: Fix code styling of moved InitExecutor class (Hennadii Stepanov) c82165a qt, refactor: Move InitExecutor class into its own module (Hennadii Stepanov) dbcf56b scripted-diff: Rename BitcoinCore class to InitExecutor (Hennadii Stepanov) 19a1d00 qt: Add BitcoinCore::m_thread member (Hennadii Stepanov) Pull request description: This PR makes the `BitcoinCore` class reusable, i.e., it can be used by the widget-based GUI or by the [QML-based](https://github.com/bitcoin-core/gui-qml/tree/main/src/qml) one, and it makes the divergence between these two repos minimal. The small benefit to the current branch is more structured code. Actually, this PR is ported from bitcoin-core/gui-qml#10. The example of the re-using of the `BitcoinCore` class is bitcoin-core/gui-qml#11. ACKs for top commit: laanwj: ACK 8169fc4 ryanofsky: Code review ACK 8169fc4. Only change is switching from `m_executor` from pointer to optional type (thanks for update!) Tree-SHA512: a0552c32d26d9acf42921eb12bcdf68f02d52f7183c688c43257b1a58679f64e45f193ee2d316850c7f0f516561e17abe989fe545bfa05e158ad3f4c66d19bca
- Loading branch information
Showing
7 changed files
with
147 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright (c) 2014-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 <qt/initexecutor.h> | ||
|
||
#include <interfaces/node.h> | ||
#include <util/system.h> | ||
#include <util/threadnames.h> | ||
|
||
#include <exception> | ||
|
||
#include <QDebug> | ||
#include <QObject> | ||
#include <QString> | ||
#include <QThread> | ||
|
||
InitExecutor::InitExecutor(interfaces::Node& node) | ||
: QObject(), m_node(node) | ||
{ | ||
this->moveToThread(&m_thread); | ||
m_thread.start(); | ||
} | ||
|
||
InitExecutor::~InitExecutor() | ||
{ | ||
qDebug() << __func__ << ": Stopping thread"; | ||
m_thread.quit(); | ||
m_thread.wait(); | ||
qDebug() << __func__ << ": Stopped thread"; | ||
} | ||
|
||
void InitExecutor::handleRunawayException(const std::exception* e) | ||
{ | ||
PrintExceptionContinue(e, "Runaway exception"); | ||
Q_EMIT runawayException(QString::fromStdString(m_node.getWarnings().translated)); | ||
} | ||
|
||
void InitExecutor::initialize() | ||
{ | ||
try { | ||
util::ThreadRename("qt-init"); | ||
qDebug() << __func__ << ": Running initialization in thread"; | ||
interfaces::BlockAndHeaderTipInfo tip_info; | ||
bool rv = m_node.appInitMain(&tip_info); | ||
Q_EMIT initializeResult(rv, tip_info); | ||
} catch (const std::exception& e) { | ||
handleRunawayException(&e); | ||
} catch (...) { | ||
handleRunawayException(nullptr); | ||
} | ||
} | ||
|
||
void InitExecutor::shutdown() | ||
{ | ||
try { | ||
qDebug() << __func__ << ": Running Shutdown in thread"; | ||
m_node.appShutdown(); | ||
qDebug() << __func__ << ": Shutdown finished"; | ||
Q_EMIT shutdownResult(); | ||
} catch (const std::exception& e) { | ||
handleRunawayException(&e); | ||
} catch (...) { | ||
handleRunawayException(nullptr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// Copyright (c) 2014-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. | ||
|
||
#ifndef BITCOIN_QT_INITEXECUTOR_H | ||
#define BITCOIN_QT_INITEXECUTOR_H | ||
|
||
#include <interfaces/node.h> | ||
|
||
#include <exception> | ||
|
||
#include <QObject> | ||
#include <QThread> | ||
|
||
QT_BEGIN_NAMESPACE | ||
class QString; | ||
QT_END_NAMESPACE | ||
|
||
/** Class encapsulating Bitcoin Core startup and shutdown. | ||
* Allows running startup and shutdown in a different thread from the UI thread. | ||
*/ | ||
class InitExecutor : public QObject | ||
{ | ||
Q_OBJECT | ||
public: | ||
explicit InitExecutor(interfaces::Node& node); | ||
~InitExecutor(); | ||
|
||
public Q_SLOTS: | ||
void initialize(); | ||
void shutdown(); | ||
|
||
Q_SIGNALS: | ||
void initializeResult(bool success, interfaces::BlockAndHeaderTipInfo tip_info); | ||
void shutdownResult(); | ||
void runawayException(const QString& message); | ||
|
||
private: | ||
/// Pass fatal exception message to UI thread | ||
void handleRunawayException(const std::exception* e); | ||
|
||
interfaces::Node& m_node; | ||
QThread m_thread; | ||
}; | ||
|
||
#endif // BITCOIN_QT_INITEXECUTOR_H |
Oops, something went wrong.