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

gui: Refactor OpenWalletActivity #16261

Merged
merged 1 commit into from
Sep 7, 2019

Conversation

promag
Copy link
Member

@promag promag commented Jun 21, 2019

This PR consists in 3 refactors:

  1. Split from OpenWalletActivity a base class WalletControllerActivity to simplify new activities, like the upcoming CreateWalletActivity;
  2. Move from BitcoinGUI the details of the wallet opening;
  3. Change threading model - WalletControllerActivity instances belong to the GUI thread and some code (the blocking code) is now executed in the worker thread asynchronously, which allows for a responsive GUI.

@DrahtBot
Copy link
Contributor

DrahtBot commented Jun 21, 2019

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

No conflicts as of last run.

src/qt/walletcontroller.cpp Outdated Show resolved Hide resolved
@fanquake
Copy link
Member

fanquake commented Jul 5, 2019

@promag Can you rebase this on master; as it contains some bug fixes that would be nice to have when testing this PR, such as #16231.

@promag
Copy link
Member Author

promag commented Jul 5, 2019

@fanquake done.

@hebasto
Copy link
Member

hebasto commented Jul 5, 2019

Concept ACK.

@fanquake
Copy link
Member

fanquake commented Jul 6, 2019

I had a quick test, and this PR on top of master (4f378ac) doesn't fix #15453.


if (wallet) m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet));

QTimer::singleShot(500, this, &OpenWalletActivity::finish);
Copy link
Member

Choose a reason for hiding this comment

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

Same here.

Copy link
Member

Choose a reason for hiding this comment

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

OpenWalletActivity::finish() could be inlined here.
OpenWalletActivity::open() will remain small enough and readability will be improved, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

OpenWalletActivity::finish() could be inlined here.

At the time I had 2 reasons to not do that:

  1. finish could be called from multiple places, also initially it was a virtual method in the super class;
  2. wanted to avoid nesting lambdas.

Copy link
Member

Choose a reason for hiding this comment

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

Could it be just

        finish();

?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, this finish() is not thread safe - it would be called from the worker thread.

Copy link
Member

Choose a reason for hiding this comment

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

... this finish() is not thread safe - it would be called from the worker thread.

Right. But thread safety is not required for finish(), IMO.

Copy link
Member Author

Choose a reason for hiding this comment

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

But it is, see OpenWalletActivity::finish(), it must be called on the GUI thread.

Copy link
Member

Choose a reason for hiding this comment

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

Correct.

src/qt/walletcontroller.cpp Outdated Show resolved Hide resolved
m_activity_thread.wait();
m_activity_thread->quit();
m_activity_thread->wait();
delete m_activity_worker;
Copy link
Member

Choose a reason for hiding this comment

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

Could naked delete be avoided in the new code? There are some techniques to achieve this.
E.g., m_activity_worker could have a parent QObject, like m_activity_thread has.

Copy link
Member Author

Choose a reason for hiding this comment

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

No object could be m_activity_worker parent because it's the only object on its thread. Could connect WalletController::destroyed to its deleteLater or could use some smart pointer but honestly I think this is better.

Copy link
Member

Choose a reason for hiding this comment

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

You could use QThread::finished signal. See: https://github.com/bitcoin/bitcoin/pull/16261/files#r303227866

{}
WalletControllerActivity::~WalletControllerActivity()
{
delete m_progress_dialog;
Copy link
Member

Choose a reason for hiding this comment

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

Could naked delete be avoided in the new code?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ownership is given to m_parent_widget so that it shows up properly. If the activity finishes first then we delete it - note lines L174-L176 below.

Copy link
Member

Choose a reason for hiding this comment

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

@@ -29,15 +33,17 @@ WalletController::WalletController(interfaces::Node& node, const PlatformStyle*
getOrCreateWallet(std::move(wallet));
}

m_activity_thread.start();
m_activity_worker->moveToThread(m_activity_thread);
Copy link
Member

Choose a reason for hiding this comment

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

Could add

    connect(m_activity_thread, &QThread::finished, m_activity_worker, &QObject::deleteLater);

and remove following

   delete m_activity_worker;

?

Copy link
Member Author

Choose a reason for hiding this comment

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

Why? The thread is explicitly stopped (actually the event loop is stopped and the thread finishes) in ~WalletController() so what better place to also delete the worker? I think explicit teardown is preferable.

Copy link
Member

Choose a reason for hiding this comment

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

Why?

Your approach binds the life of m_activity_worker to the WalletController instance lifetime.
IMO, as m_activity_worker lives in m_activity_thread, connecting to the QThread::finished signal seems more appropriate.

src/qt/walletcontroller.cpp Show resolved Hide resolved
src/qt/walletcontroller.cpp Outdated Show resolved Hide resolved
@jonasschnelli
Copy link
Contributor

Looks like the wallet encryption test fails...

@promag
Copy link
Member Author

promag commented Jul 17, 2019

@jonasschnelli it was failing because of build timeouts.

@promag promag force-pushed the 2019-06-refactor-open-wallet branch from b83c3e1 to 7afbd0c Compare August 29, 2019 13:29
Copy link
Member

@hebasto hebasto left a comment

Choose a reason for hiding this comment

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

ACK 7afbd0c, I have tested the code: no change in behavior is observed (compared to the master 6519be6).

@promag promag force-pushed the 2019-06-refactor-open-wallet branch from 7afbd0c to 845d57f Compare September 5, 2019 23:04
@promag promag force-pushed the 2019-06-refactor-open-wallet branch from 845d57f to bc6d8a3 Compare September 5, 2019 23:05
@promag
Copy link
Member Author

promag commented Sep 6, 2019

@hebasto rebased and removed the unused declaration std::vector<std::string> getWalletsAvailableToOpen() const; - left over from other work.

@fanquake fanquake merged commit bc6d8a3 into bitcoin:master Sep 7, 2019
@promag promag deleted the 2019-06-refactor-open-wallet branch September 7, 2019 09:01
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants