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

Prepare for next hardhat version #796

Merged
merged 1 commit into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,17 @@ class API {
}

// Hardhat
attachToHardhatVM(provider){
async attachToHardhatVM(provider){
const self = this;
this.collector = new DataCollector(this.instrumenter.instrumentationData);

if ('init' in provider) {
// Newer versions of Hardhat initialize the provider lazily, so we need to
// call provider.init() explicitly. This is a no-op if the provider is
// already initialized.
await provider.init();
}

Copy link
Member

Choose a reason for hiding this comment

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

👍

let cur = provider;

// Go down to core HardhatNetworkProvider
Expand Down
8 changes: 4 additions & 4 deletions plugins/hardhat.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,19 @@ task("coverage", "Generates a code coverage report for tests")
// ==============
// Server launch
// ==============
let network = nomiclabsUtils.setupHardhatNetwork(env, api, ui);
let network = await nomiclabsUtils.setupHardhatNetwork(env, api, ui);

if (network.isHardhatEVM){
accounts = await utils.getAccountsHardhat(network.provider);
nodeInfo = await utils.getNodeInfoHardhat(network.provider);

// Note: this only works if the reset block number is before any transactions have fired on the fork.
// e.g you cannot fork at block 1, send some txs (blocks 2,3,4) and reset to block 2
env.network.provider.on(HARDHAT_NETWORK_RESET_EVENT, () => {
api.attachToHardhatVM(env.network.provider);
env.network.provider.on(HARDHAT_NETWORK_RESET_EVENT, async () => {
await api.attachToHardhatVM(env.network.provider);
Copy link
Collaborator Author

@fvictorio fvictorio May 25, 2023

Choose a reason for hiding this comment

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

I'm not super confident about this change. I think it's working (I ran coverage in a test that uses hardhat_reset), but it seems brittle to me.

Copy link
Member

Choose a reason for hiding this comment

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

@fvictorio Is your concern that the event callback won't be awaited by EventEmitter? Or something else?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't have a specific concern tbh. It's just that something that used to happen "immediately" after a reset now will not. There might be a hidden assumption that is now broken, but it's hard to know.

});

api.attachToHardhatVM(network.provider);
await api.attachToHardhatVM(network.provider);

ui.report('hardhat-network', [
nodeInfo.split('/')[1],
Expand Down
33 changes: 25 additions & 8 deletions plugins/resources/nomiclabs.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ function setupBuidlerNetwork(env, api, ui){
)
}

function setupHardhatNetwork(env, api, ui){
async function setupHardhatNetwork(env, api, ui){
const hardhatPackage = require('hardhat/package.json');
const { createProvider } = require("hardhat/internal/core/providers/construction");
const { HARDHAT_NETWORK_NAME } = require("hardhat/plugins")

// after 2.15.0, the internal createProvider function has a different signature
const newCreateProviderSignature = semver.satisfies(hardhatPackage.version, "^2.15.0");

let provider, networkName, networkConfig;
let isHardhatEVM = false;

Expand All @@ -91,12 +95,20 @@ function setupHardhatNetwork(env, api, ui){
networkConfig = env.network.config;
configureHardhatEVMGas(networkConfig, api);

provider = createProvider(
networkName,
networkConfig,
env.config.paths,
env.artifacts,
)
if (newCreateProviderSignature) {
provider = await createProvider(
env.config,
networkName,
env.artifacts,
)
} else {
provider = createProvider(
networkName,
networkConfig,
env.config.paths,
env.artifacts,
)
}

// HttpProvider
} else {
Expand All @@ -106,7 +118,12 @@ function setupHardhatNetwork(env, api, ui){
networkConfig = env.config.networks[networkName]
configureNetworkGas(networkConfig, api);
configureHttpProvider(networkConfig, api, ui)
provider = createProvider(networkName, networkConfig);

if (newCreateProviderSignature) {
provider = await createProvider(env.config, networkName);
} else {
provider = createProvider(networkName, networkConfig);
}
}

return configureNetworkEnv(
Expand Down