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

Plugins: Fix to @hook / @run_hook mechanism #1669

Merged
merged 5 commits into from
Nov 13, 2019
Merged

Plugins: Fix to @hook / @run_hook mechanism #1669

merged 5 commits into from
Nov 13, 2019

Conversation

cculianu
Copy link
Collaborator

@cculianu cculianu commented Nov 13, 2019

As per a Telegram chat with @markblundeberg -- we realized that:

The @hook decorator was registering functions globally for all plugins,
even ones not decorated with @hook would get picked up.

This change fixes it so that only functions decorated with @hook get
picked up.

This commit is not 100% ready for merge to mainline. I still have to do
some due diligence and verify no extant plugins DEPENDED on the old
behavior for correct operation!

EDIT: It turns out this PR is safe to merge. I ran through all the code and verified all hook-like-functions are indeed decorated with @hook.

@cculianu cculianu added Do Not Merge (Yet!) Pull requests that we intend to merge in the future but that should not yet be merged. Nits / Refactoring PRs and issues related to things like code formatting and small nits labels Nov 13, 2019
The @hook decorator was registering functions globally for all plugins,
even ones not decorated with @hook would get picked up.

This change fixes it so that only functions decorated with @hook get
picked up.

This commit is not 100% ready for merge to mainline.  I still have to do
some due diligence and verify no extant plugins DEPENDED on the old
behavior for correct operation!
lib/plugins.py Outdated Show resolved Hide resolved
lib/plugins.py Outdated Show resolved Hide resolved
What we were doing before could trigger an @Property instance property
to execute a function as part of our getattr() check in _is_hook.

This commit handles the property situation correctly.
This may be faster, and was suggested by Mark.
We just pick the first one and print a warning to the console.
@cculianu
Copy link
Collaborator Author

Ok, I ran through all the plugins shipped with EC (plus a few extra popular plugins I know people use). I used fancy grepping technology to detect any plugin that had a hook function (I got the master list of hook functions by grepping for all run_hooks..).

Conclusion: There are no hook function that lack @hook. They all assumed @hook works the way we thought it should work before discovering this "bug".

So this change is safe to merge.

@cculianu cculianu removed the Do Not Merge (Yet!) Pull requests that we intend to merge in the future but that should not yet be merged. label Nov 13, 2019
@markblundeberg
Copy link

Ok, I ran through all the plugins shipped with EC (plus a few extra popular plugins I know people use). I used fancy grepping technology to detect any plugin that had a hook function (I got the master list of hook functions by grepping for all run_hooks..).

Conclusion: There are no hook function that lack @hook. They all assumed @hook works the way we thought it should work before discovering this "bug".

So this change is safe to merge.

very nice, thanks for doing this search!

for name, func in self._hooks_i_registered:
l = hooks.get(name, [])
try: l.remove((self, func))
except ValueError: pass # this should never happen but it pays to be paranoid.

Choose a reason for hiding this comment

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

these 2 lines still counts as 4 lines of code in my mind :-P

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Are you saying I should reformat it? or? I'm a C++ guy. It's hard for me to waste so many lines. I can do so.. if you suggest it more clearly. I'm not sure I understand.. ha ha.

Choose a reason for hiding this comment

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

Hehe just bugging you, it's fine :D

@markblundeberg
Copy link

Yeah this looks good 👍

@cculianu
Copy link
Collaborator Author

note: 1 more commit added to alphabeticize the imports.. ahh.

@cculianu cculianu merged commit 5cea486 into master Nov 13, 2019
@cculianu cculianu deleted the fix_hook branch November 13, 2019 21:50
cculianu added a commit to simpleledger/Electron-Cash-SLP that referenced this pull request Nov 13, 2019
* Fix to plugin run_hook mechanism

The @hook decorator was registering functions globally for all plugins,
even ones not decorated with @hook would get picked up.

This change fixes it so that only functions decorated with @hook get
picked up.

This commit is not 100% ready for merge to mainline.  I still have to do
some due diligence and verify no extant plugins DEPENDED on the old
behavior for correct operation!

* Follow-up to @hook fix: Deal with @Property attributes properly

What we were doing before could trigger an @Property instance property
to execute a function as part of our getattr() check in _is_hook.

This commit handles the property situation correctly.

* @hook fix follow-up: Check for _is_ec_plugin_hook on class-level

This may be faster, and was suggested by Mark.

* Nit: Allow run_hook hooks to return more than 1 result

We just pick the first one and print a warning to the console.

* plugins.py nit: Alphabeticized the imports

My OCD is satisfied.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Nits / Refactoring PRs and issues related to things like code formatting and small nits
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants