refactor: Make BitcoinCore class reusable #381

pull hebasto wants to merge 4 commits into bitcoin-core:master from hebasto:210714-core changing 7 files +147 −108
  1. hebasto commented at 11:02 am on July 14, 2021: member

    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 https://github.com/bitcoin-core/gui-qml/pull/10. The example of the re-using of the BitcoinCore class is https://github.com/bitcoin-core/gui-qml/pull/11.

  2. in src/qt/bitcoincore.cpp:1 in 7495725a7f outdated
    0@@ -0,0 +1,66 @@
    1+// Copyright (c) 2014-2021 The Bitcoin Core developers
    


    hebasto commented at 11:04 am on July 14, 2021:
    The initial code was contributed by @laanwj back in 2014.
  3. hebasto commented at 11:08 am on July 14, 2021: member

    Begging my fellow colleagues for review :

  4. ryanofsky commented at 11:36 am on July 14, 2021: 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.)

  5. hebasto force-pushed on Jul 14, 2021
  6. hebasto commented at 12:58 pm on July 14, 2021: member

    @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?

  7. in build_msvc/libbitcoin_qt/libbitcoin_qt.vcxproj:17 in 6c2969da1d outdated
    13@@ -14,6 +14,7 @@
    14     <ClCompile Include="..\..\src\qt\bitcoin.cpp" />
    15     <ClCompile Include="..\..\src\qt\bitcoinaddressvalidator.cpp" />
    16     <ClCompile Include="..\..\src\qt\bitcoinamountfield.cpp" />
    17+    <ClCompile Include="..\..\src\qt\bitcoincore.cpp" />
    


    ryanofsky commented at 2:40 pm on July 14, 2021:

    In commit “qt, refactor: Fix code styling of moved BitcoinCore class” (6c2969da1dd164c6d7acfeb45b55ed95d530c76e)

    I think these changes were probably meant for the earlier commit introducing this file instead of this commit


    hebasto commented at 4:24 pm on July 14, 2021:
    Thanks! Updated.
  8. in src/qt/bitcoin.cpp:304 in 6b769d9e9e outdated
    300@@ -291,22 +301,14 @@ bool BitcoinApplication::baseInitialize()
    301 
    302 void BitcoinApplication::startThread()
    303 {
    304-    if(coreThread)
    305-        return;
    306-    coreThread = new QThread(this);
    307-    BitcoinCore *executor = new BitcoinCore(node());
    308-    executor->moveToThread(coreThread);
    309+    m_core = std::make_unique<BitcoinCore>(node());
    


    ryanofsky commented at 2:48 pm on July 14, 2021:

    In commit “qt: Add BitcoinCore::m_thread member” (6b769d9e9e1c73cef37826e3c842fe9b65737456)

    Might be good to assert(!m_core) before setting this. Also I think m_executor would be a more descriptive name than m_core


    hebasto commented at 4:24 pm on July 14, 2021:
    Thanks! Updated.
  9. in src/qt/bitcoin.cpp:296 in 6b769d9e9e outdated
    300@@ -291,22 +301,14 @@ bool BitcoinApplication::baseInitialize()
    301 
    302 void BitcoinApplication::startThread()
    303 {
    304-    if(coreThread)
    305-        return;
    306-    coreThread = new QThread(this);
    


    ryanofsky commented at 3:01 pm on July 14, 2021:

    In commit “qt: Add BitcoinCore::m_thread member” (6b769d9e9e1c73cef37826e3c842fe9b65737456)

    The coreThread member doesn’t actually do anything after this change. Would be less confusing to squash the next commit into this one and delete coreThread entirely instead of keeping it half-used.


    hebasto commented at 4:24 pm on July 14, 2021:
    Thanks! Updated.
  10. in src/qt/bitcoin.cpp:168 in 6b769d9e9e outdated
    163+}
    164+
    165+BitcoinCore::~BitcoinCore()
    166+{
    167+    qDebug() << __func__ << ": Stopping thread";
    168+    m_thread.quit();
    


    ryanofsky commented at 3:05 pm on July 14, 2021:

    In commit “qt: Add BitcoinCore::m_thread member” (6b769d9e9e1c73cef37826e3c842fe9b65737456)

    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.


    hebasto commented at 4:24 pm on July 14, 2021:
    Thanks! Updated.
  11. ryanofsky approved
  12. ryanofsky commented at 3:19 pm on July 14, 2021: contributor

    Code review ACK 7ec552676c66488fe00fb503d02ec4a389a715b7

    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? :shrug: 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.

  13. hebasto force-pushed on Jul 14, 2021
  14. hebasto commented at 4:23 pm on July 14, 2021: member

    Updated 6c2969da1dd164c6d7acfeb45b55ed95d530c76e -> 0c7667f522f7da8155b7e959e550868210dbf033 (pr381.02 -> pr381.03, diff). @ryanofsky

    Thank you for your review! Your comments are addressed.

  15. in src/qt/bitcoin.h:119 in efacacba98 outdated
    115@@ -112,7 +116,7 @@ public Q_SLOTS:
    116     void windowShown(BitcoinGUI* window);
    117 
    118 private:
    119-    QThread *coreThread;
    120+    std::unique_ptr<BitcoinCore> m_executor;
    


    ryanofsky commented at 6:00 pm on July 14, 2021:

    In commit “qt: Add BitcoinCore::m_thread member” (efacacba98aa6d9d0794f62032eb353ced1e0791)

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


    hebasto commented at 6:58 pm on July 14, 2021:
    Thank! Updated.
  16. ryanofsky approved
  17. ryanofsky commented at 6:02 pm on July 14, 2021: contributor
    Code review ACK 0c7667f522f7da8155b7e959e550868210dbf033. Thanks! Just renaming and other suggested changes since last review
  18. qt: Add BitcoinCore::m_thread member
    This change makes BitcoinCore self-contained to improve its
    re-usability.
    
    BitcoinApplication::coreThread member is now unused, and removed.
    19a1d00831
  19. scripted-diff: Rename BitcoinCore class to InitExecutor
    -BEGIN VERIFY SCRIPT-
    sed -i -e 's/BitcoinCore/InitExecutor/g' src/qt/bitcoin.*
    -END VERIFY SCRIPT-
    dbcf56b6c6
  20. qt, refactor: Move InitExecutor class into its own module
    This change makes InitExecutor class re-usable by an alternative GUI,
    e.g., QML-based one.
    c82165a557
  21. qt, refactor: Fix code styling of moved InitExecutor class 8169fc4e73
  22. hebasto force-pushed on Jul 14, 2021
  23. hebasto commented at 6:58 pm on July 14, 2021: member

    Updated 0c7667f522f7da8155b7e959e550868210dbf033 -> 8169fc4e73a87331e02502fc24e293831765c8b1 (pr381.03 -> pr381.04, diff):

  24. hebasto added the label Refactoring on Jul 14, 2021
  25. bitcoin-core deleted a comment on Jul 16, 2021
  26. bitcoin-core deleted a comment on Jul 17, 2021
  27. bitcoin-core deleted a comment on Jul 17, 2021
  28. bitcoin-core deleted a comment on Jul 17, 2021
  29. RandyMcMillan commented at 1:01 am on July 17, 2021: contributor

    MacOS 11.4

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

  30. laanwj commented at 4:27 pm on July 21, 2021: member

    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)

  31. ryanofsky commented at 5:06 pm on July 21, 2021: 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.
  32. ryanofsky approved
  33. ryanofsky commented at 6:47 pm on July 21, 2021: contributor
    Code review ACK 8169fc4e73a87331e02502fc24e293831765c8b1. Only change is switching from m_executor from pointer to optional type (thanks for update!)
  34. laanwj commented at 7:57 am on July 22, 2021: member

    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 8169fc4e73a87331e02502fc24e293831765c8b1

  35. laanwj merged this on Jul 22, 2021
  36. laanwj closed this on Jul 22, 2021

  37. promag commented at 10:47 am on July 22, 2021: contributor
    Code review ACK 8169fc4e73.
  38. hebasto deleted the branch on Jul 22, 2021
  39. sidhujag referenced this in commit cd5840deae on Jul 23, 2021
  40. bitcoin-core locked this on Aug 16, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/gui. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-11-21 12:20 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me