Skip to content
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

refactor: Make BitcoinCore class reusable #381

Merged
merged 4 commits into from
Jul 22, 2021

Conversation

hebasto
Copy link
Member

@hebasto hebasto commented Jul 14, 2021

This PR makes the BitcoinCore class reusable, i.e., it can be used by the widget-based GUI or by the QML-based 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.

@@ -0,0 +1,66 @@
// Copyright (c) 2014-2021 The Bitcoin Core developers
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The initial code was contributed by @laanwj back in 2014.

@hebasto
Copy link
Member Author

hebasto commented Jul 14, 2021

Begging my fellow colleagues for review :

@ryanofsky
Copy link
Contributor

This seems good. Also maybe if BitcoinCore class is moving to it's own file, it could be renamed to something that better describes what it does like InitExecutor (similar to RPCExecutor) or InitActivity (like CreateWalletActivity and OpenWalletActivity) or just InitThread InitTask.

(Longer term, I think these asynchronous executor / activity classes are mostly unnecessary and could be replaced with an AsyncUpdate or similar utility, but that's beyond the scope of this PR.)

@hebasto
Copy link
Member Author

hebasto commented Jul 14, 2021

@ryanofsky

This seems good. Also maybe if BitcoinCore class is moving to it's own file, it could be renamed to something that better describes what it does like InitExecutor (similar to RPCExecutor) or InitActivity (like CreateWalletActivity and OpenWalletActivity) or just InitThread InitTask.

Many thanks for feedback. Which name could also describe that this class is also used to provide a shutdown activity?

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 7ec5526

Many thanks for feedback. Which name could also describe that this class is also used to provide a shutdown activity?

Personally I like InitExecutor. Shutdown just seems like part of initialization. Maybe StartupExecutor? 🤷 I do think pretty much any name would be more descriptive than a class or file called bitcoin core. But feel free to keep the current name or choose any name that seems good to you.

build_msvc/libbitcoin_qt/libbitcoin_qt.vcxproj Outdated Show resolved Hide resolved
src/qt/bitcoin.cpp Outdated Show resolved Hide resolved
src/qt/bitcoin.cpp Show resolved Hide resolved
BitcoinCore::~BitcoinCore()
{
qDebug() << __func__ << ": Stopping thread";
m_thread.quit();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "qt: Add BitcoinCore::m_thread member" (6b769d9)

I'm not sure if it matters but a possible concern is that when bitcoinapplication is destroyed, this thread wait now happens after the window and platform objects are instead of before they are deleted in ~BitcoinApplication. This may be safe but it might be safer to make sure these objects are not deleted while the thread is running and maybe working. You could do this by adding m_core.reset() to the beginning of ~BitcoinApplication. But fine to skip this if we know the thread will already be done working when ~BitcoinApplication runs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Updated.

@hebasto
Copy link
Member Author

hebasto commented Jul 14, 2021

Updated 6c2969d -> 0c7667f (pr381.02 -> pr381.03, diff).

@ryanofsky

Thank you for your review! Your comments are addressed.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 0c7667f. Thanks! Just renaming and other suggested changes since last review

src/qt/bitcoin.h Outdated
@@ -112,7 +116,7 @@ public Q_SLOTS:
void windowShown(BitcoinGUI* window);

private:
QThread *coreThread;
std::unique_ptr<BitcoinCore> m_executor;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit "qt: Add BitcoinCore::m_thread member" (efacacb)

Not important, but this could use std::optional instead of std::unique_ptr to avoid a dynamic allocation

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank! Updated.

This change makes BitcoinCore self-contained to improve its
re-usability.

BitcoinApplication::coreThread member is now unused, and removed.
-BEGIN VERIFY SCRIPT-
sed -i -e 's/BitcoinCore/InitExecutor/g' src/qt/bitcoin.*
-END VERIFY SCRIPT-
This change makes InitExecutor class re-usable by an alternative GUI,
e.g., QML-based one.
@hebasto
Copy link
Member Author

hebasto commented Jul 14, 2021

Updated 0c7667f -> 8169fc4 (pr381.03 -> pr381.04, diff):

@bitcoin-core bitcoin-core deleted a comment Jul 16, 2021
@bitcoin-core bitcoin-core deleted a comment Jul 17, 2021
@bitcoin-core bitcoin-core deleted a comment Jul 17, 2021
@bitcoin-core bitcoin-core deleted a comment Jul 17, 2021
@RandyMcMillan
Copy link
Contributor

MacOS 11.4

Remote Desktop Picture July 16, 2021 at 8 50 16 PM EDT

@laanwj
Copy link
Member

laanwj commented Jul 21, 2021

I do think pretty much any name would be more descriptive than a class or file called bitcoin core.

Originally I called the class "BitcoinCore", not because the software is called like that (not sure if it even did at the time?), but because it wraps an instance of the core (=non-gui) functionality, to be executed in its own thread. But like bitcoind's main thread, it does nothing but initialization and shutdown.

(I don't mind it being renamed, but it was not as silly as a "let's call a class the same name as the software" at the time)

@ryanofsky
Copy link
Contributor

Yeah I figured "core" was a synonym for "init". Just makes sense to make the old name more descriptive when the class is moved and more widely exposed now.

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 8169fc4. Only change is switching from m_executor from pointer to optional type (thanks for update!)

@laanwj
Copy link
Member

laanwj commented Jul 22, 2021

Yeah I figured "core" was a synonym for "init". Just makes sense to make the old name more descriptive when the class is moved and more widely exposed now.

Well not "init", more like "root model object" (e.g. I think it was supposed to create and manage ClientModel, WalletModels etc). But it's okay, I'm fine with naming it after what it is now doing.

ACK 8169fc4

@laanwj laanwj merged commit 36aee0f into bitcoin-core:master Jul 22, 2021
Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review ACK 8169fc4.

@hebasto hebasto deleted the 210714-core branch July 22, 2021 14:32
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants