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

Define the HookContext as a partial view of the HRE #5776

Merged
merged 1 commit into from
Sep 26, 2024

Conversation

alcuadrado
Copy link
Member

This PR addresses a limitation in Hardhat's core architecture discovered by @ChristopherDedominici.

Explanation of the problem

Chris is working on the first few hook handlers that use the builtin plugins, and in particular, the extensions to the HRE that they make.

This was problematic, as HookHandlers don't receive the HRE, but a HookContext instead. The problem was that any extension to the HRE wasn't reflected in the HookContext.

The' HookContext' intends to prevent HookHandler's from accessing the TaskManager and running tasks, as these are separate concerns in v3, and not to prevent them from accessing the builtin plugins.

Proposed solution

This solution has a type-level and runtime-level component.

Types

Instead of defining the HookContext as a separate interface, define it as a derived type of the HRE:

export type HookContext = Omit<HardhatRuntimeEnvironment, "tasks">;

This way, every extension to the HardhatRuntimeEnvironment is also available in the HookContext.

Runtime

To make the values (as in runtime values, not types) of the extensions available through the HookContext, without creating a new hook nor restructuring the system, I used Object.create to create the HookContext, and overwrote tasks with undefined.

Contrary to making a shallow copy at initialization, defining the HRE as its prototype means that any future modification to the HRE will also be visible.

Copy link

changeset-bot bot commented Sep 25, 2024

⚠️ No Changeset found

Latest commit: 46d675d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Sep 25, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
hardhat ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 25, 2024 7:29pm

@github-actions github-actions bot added the status:ready This issue is ready to be worked on label Sep 25, 2024
@alcuadrado alcuadrado added the v-next A Hardhat v3 development task label Sep 25, 2024
@alcuadrado alcuadrado changed the title Define the HookContext as partial view of the HRE Define the HookContext as a partial view of the HRE Sep 25, 2024
Copy link
Member

@schaable schaable left a comment

Choose a reason for hiding this comment

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

Looks good! Using Object.create was really smart 💯

@alcuadrado alcuadrado added this pull request to the merge queue Sep 26, 2024
Merged via the queue into v-next with commit 57f9e87 Sep 26, 2024
34 checks passed
@alcuadrado alcuadrado deleted the hook-context-fix branch September 26, 2024 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:ready This issue is ready to be worked on v-next A Hardhat v3 development task
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants