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

Fix hardhat_reset (#667) and add unit tests #681

Merged
merged 6 commits into from
Jan 17, 2022
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
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@
"@nomiclabs/buidler": "^1.3.6",
"@nomiclabs/buidler-truffle5": "^1.3.4",
"@nomiclabs/buidler-web3": "^1.3.4",
"@nomiclabs/hardhat-ethers": "^2.0.4",
"@nomiclabs/hardhat-truffle5": "^2.0.0",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@nomiclabs/hardhat-web3": "^2.0.0",
"@truffle/contract": "^4.0.36",
"buidler-gas-reporter": "^0.1.3",
"chai": "^4.3.4",
"decache": "^4.5.1",
"ethereum-waffle": "^3.4.0",
"ethers": "^5.5.3",
"hardhat": "^2.8.2",
"hardhat-gas-reporter": "^1.0.1",
"mocha": "5.2.0",
Expand Down
10 changes: 8 additions & 2 deletions plugins/hardhat.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const path = require('path');

const { task, types } = require("hardhat/config");
const { HardhatPluginError } = require("hardhat/plugins")

const {HARDHAT_NETWORK_RESET_EVENT} = require("hardhat/internal/constants");
const {
TASK_TEST,
TASK_COMPILE,
Expand Down Expand Up @@ -162,12 +162,18 @@ task("coverage", "Generates a code coverage report for tests")
// ==============
// Server launch
// ==============
const network = nomiclabsUtils.setupHardhatNetwork(env, api, ui);
let network = 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);
});

api.attachToHardhatVM(network.provider);

ui.report('hardhat-network', [
Expand Down
8 changes: 8 additions & 0 deletions test/integration/projects/hardhat-reset/.solcover.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Testing hooks
const fn = (msg, config) => config.logger.log(msg);

module.exports = {
skipFiles: ['Migrations.sol'],
silent: process.env.SILENT ? true : false,
istanbulReporter: ['json-summary', 'text'],
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
pragma solidity ^0.7.0;


contract ContractA {
uint x;
constructor() public {
}

function sendFn() public {
x = 1;
}

function sendFn2() public {
x = 2;
}

function callFn() public pure returns (uint){
uint y = 5;
return y;
}

function callFn2() public pure returns (uint){
uint y = 5;
return y;
}
}
28 changes: 28 additions & 0 deletions test/integration/projects/hardhat-reset/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-ethers");
require(__dirname + "/../plugins/nomiclabs.plugin");

if (!process.env.ALCHEMY_TOKEN){
throw new Error(
"This test requires that you set ALCHEMY_TOKEN to a valid token in your development env"
);
}

module.exports = {
solidity: {
version: "0.7.0"
},
networks: {
hardhat: {
timeout: 100000,
forking: {
url: `https://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_TOKEN}`,
blockNumber: 14000000,
}
}
},
mocha: {
timeout: 100000
},
logger: process.env.SILENT ? { log: () => {} } : console,
};
43 changes: 43 additions & 0 deletions test/integration/projects/hardhat-reset/test/testReset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("contractA", function() {
let instance;
let startBlockNumber;

beforeEach(async () => {
startBlockNumber = await ethers.provider.getBlockNumber();

await hre.network.provider.request({
method: "hardhat_reset",
params: [
{
forking: {
jsonRpcUrl: `https://eth-mainnet.alchemyapi.io/v2/${process.env.ALCHEMY_TOKEN}`,
blockNumber: startBlockNumber,
},
},
],
});

const factory = await ethers.getContractFactory("ContractA");
instance = await factory.deploy();
});

it('sends', async function(){
await instance.sendFn();
});

it('sends 2', async function(){
await instance.sendFn2();
});

it('calls', async function(){
await instance.callFn();
});

it('calls 2', async function(){
await instance.callFn2();
});
});

16 changes: 16 additions & 0 deletions test/units/hardhat/standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,22 @@ describe('Hardhat Plugin: standard use cases', function() {
verify.lineCoverage(expectedLine);
});

it('hardhat_reset preserves coverage between resets', async function(){
mock.installFullProject('hardhat-reset');
mock.hardhatSetupEnv(this);

await this.env.run("coverage");

const expected = [
{
file: mock.pathToContract(hardhatConfig, 'ContractAReset.sol'),
pct: 100
}
];

verify.lineCoverage(expected);
})

// This test freezes when gas-reporter is not disabled
it('disables hardhat-gas-reporter', async function() {
mock.installFullProject('hardhat-gas-reporter');
Expand Down
Loading