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

feat: [v0.8-develop] Simplify hook declaration and storage #51

Merged
merged 3 commits into from
May 8, 2024

Conversation

adam-alchemy
Copy link
Contributor

Motivation

Since v0.7, execution hooks can’t be dependencies anymore. This means that pre and post hooks have to be defined by the same plugin.

We can make a simplification similar to the “validation types” merges (#40, #46) , by treating pre- and post- hooks as an interface, and require pre/post hooks to share a function id.

If there is a many-to-one relationship between pre and post hooks, the plugins can still handle that routing internally.

Solution

  • Simplify the manifest structure to not use ManifestAssociatedFunction types within ManifestExecutionHook, instead just declaring the selector, functionId, and pre/post status
  • Simplify hook storage from 3 structures (preHooks, associatedPostHooks, and postOnlyHooks) to just 1 structure (executionHooks).
  • Update IAccountLoupe interface to the new format (FunctionReference + pre/post status).
  • Update tests, and remove now-impossible test failure cases 😄

Notes

  • Eventually, we should create a scalable way to pack fields in the manifest & storage.
    • Right now, the 4 fields within ManifestExecutionHook take up 4 words in memory (4 * 32 = 128 bytes), despite fitting into a single bytes32 word. This is an unfortunate limitation of abi encoding & solidity conventions.
    • Similarly, the HookData struct used within the account is kept in memory and occupies more space than is needed.
    • We've already set some precedent for using packed types with FunctionReference - does this warrant creating a new type for hook function references? How do we handle this going into the future?
    • For now, I left the encoding efficiency problem as a todo in the interface spec. We can further address this with work connected to manifest simplification.

@adam-alchemy adam-alchemy requested a review from a team May 3, 2024 19:59
Copy link
Contributor

@huaweigu huaweigu left a comment

Choose a reason for hiding this comment

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

LGTM!


// HookData layout:
// 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF______________________ Hook Function Reference
// 0x__________________________________________AA____________________ is pre hook
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we could precede the hook function with the type identifier or implement a sorting byte. This way, in storage, all prehooks could be organized before posthooks, allowing the loop to terminate early.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree this would be useful in practice. Currently though, the spec doesn't mandate any ordering for hooks, so can we address this problem in a different PR, for this issue? erc6900/resources#5

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 thanks!

@huaweigu
Copy link
Contributor

huaweigu commented May 3, 2024

Since v0.7, execution hooks can’t be dependencies anymore. This means that pre and post hooks have to be defined by the same plugin.

I'm wondering whether we should include this in the specifications to prevent developers from making incorrect assumptions. Please disregard this if it has already been mentioned in the spec.

@huaweigu
Copy link
Contributor

huaweigu commented May 3, 2024

(I almost pressed the green merge button since it's situated very close to the comment section :) It might be a good idea to update the GitHub repository configuration to require at least one approval before merging.)

@jaypaik
Copy link
Collaborator

jaypaik commented May 3, 2024

(I almost pressed the green merge button since it's situated very close to the comment section :) It might be a good idea to update the GitHub repository configuration to require at least one approval before merging.)

This is because branch protections rules were only applied to main, and this is a PR against v0.8-develop. Good flag, I added a rule for merges against feature branch.

@adam-alchemy
Copy link
Contributor Author

I'm wondering whether we should include this in the specifications to prevent developers from making incorrect assumption

This is described in the 6900 spec, although rather briefly:

enum ManifestAssociatedFunctionType {
    // Function is not defined.
    NONE,
    // Function belongs to this plugin.
    SELF,
    // Function belongs to an external plugin provided as a dependency during plugin installation. Plugins MAY depend
    // on external validation functions. It MUST NOT depend on external hooks, or installation will fail.
    DEPENDENCY,
    //...
}

Additionally, there are a few locations where dependencies are referenced as "validation dependencies". This is somewhat descriptive, but I agree we should update the spec to be more explicit about it. Though, the parameter format for depdendencies may change with erc6900/resources#9.

Copy link
Collaborator

@jaypaik jaypaik left a comment

Choose a reason for hiding this comment

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

Love the simplifications! Left a request and a question.

src/account/AccountStorage.sol Outdated Show resolved Hide resolved
Comment on lines +108 to +109
isPreHook = (uint256(setValue) >> 80) & 0xFF == 1;
isPostHook = (uint256(setValue) >> 72) & 0xFF == 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Curious why 0xFF over 0x01?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used 0xFF because I wanted to just compare the least significant byte, and I could have also used 0x01 here. It would behave the same if you only use toHookData and toSetValue to convert between the types, it could differ if someone manually dirties the upper bits of either of the two bytes though.

@adam-alchemy adam-alchemy merged commit 0f28a13 into v0.8-develop May 8, 2024
3 checks passed
@adam-alchemy adam-alchemy deleted the adam/simplify-hooks-2 branch May 8, 2024 23:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants