Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Throw error when network flag set / remove util support for http client
  • Loading branch information
cgewecke committed Feb 5, 2024
commit 8d637753f319c3788173e14c6a6b4fe926a8767a
5 changes: 5 additions & 0 deletions plugins/hardhat.plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ task("coverage", "Generates a code coverage report for tests")
}
env.hardhatArguments = Object.assign(env.hardhatArguments, flags)

// Error if --network flag is set
if (env.hardhatArguments.network){
throw new Error(ui.generate('network-fail'));
}

// ===========================
// Generate abi diff component
// (This flag only useful within codecheck context)
Expand Down
4 changes: 2 additions & 2 deletions plugins/resources/nomiclabs.ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ class PluginUI extends UI {
const x = ":x:";

const kinds = {
'network-fail': `${c.red('--network argument: ')}${args[0]}` +
`${c.red(' is not a defined network in hardhat.config.js.')}`,
'network-fail': `${c.red('--network cli flag is not supported for the coverage task. ')}` +
`${c.red('Beginning with v0.8.7, coverage must use the default "hardhat" network.')}`,

'sources-fail': `${c.red('Cannot locate expected contract sources folder: ')} ${args[0]}`,

Expand Down
82 changes: 18 additions & 64 deletions plugins/resources/nomiclabs.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,62 +64,34 @@ async function setupHardhatNetwork(env, api, ui){
const newCreateProviderSignature = semver.satisfies(hardhatPackage.version, "^2.15.0");

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

networkName = env.hardhatArguments.network || HARDHAT_NETWORK_NAME;

// HardhatEVM
if (networkName === HARDHAT_NETWORK_NAME){
isHardhatEVM = true;

networkConfig = env.network.config;
configureHardhatEVMGas(networkConfig, api);

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

// HttpProvider
networkConfig = env.network.config;
configureHardhatEVMGas(networkConfig, api);

if (newCreateProviderSignature) {
provider = await createProvider(
env.config,
HARDHAT_NETWORK_NAME,
env.artifacts,
)
} else {
if (!(env.config.networks && env.config.networks[networkName])){
throw new Error(ui.generate('network-fail', [networkName]))
}
networkConfig = env.config.networks[networkName]
configureNetworkGas(networkConfig, api);
configureHttpProvider(networkConfig, api, ui)

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

return configureNetworkEnv(
env,
networkName,
networkConfig,
provider,
isHardhatEVM
provider
)
}

function configureNetworkGas(networkConfig, api){
networkConfig.gas = api.gasLimit;
networkConfig.gasPrice = api.gasPrice;
}

function configureHardhatEVMGas(networkConfig, api){
networkConfig.allowUnlimitedContractSize = true;
networkConfig.blockGasLimit = api.gasLimitNumber;
Expand All @@ -128,15 +100,15 @@ function configureHardhatEVMGas(networkConfig, api){
networkConfig.initialBaseFeePerGas = 0;
}

function configureNetworkEnv(env, networkName, networkConfig, provider, isHardhatEVM){
function configureNetworkEnv(env, networkName, networkConfig, provider){
env.config.networks[networkName] = networkConfig;
env.config.defaultNetwork = networkName;

env.network = Object.assign(env.network, {
name: networkName,
config: networkConfig,
provider: provider,
isHardhatEVM: isHardhatEVM
isHardhatEVM: true
});

env.ethereum = provider;
Expand All @@ -145,24 +117,6 @@ function configureNetworkEnv(env, networkName, networkConfig, provider, isHardha
return env.network;
}

/**
* Extracts port from url / sets network.url
* @param {Object} networkConfig
* @param {SolidityCoverage} api
*/
function configureHttpProvider(networkConfig, api, ui){
const configPort = networkConfig.url.split(':')[2];

// Warn: port conflicts
if (api.port !== api.defaultPort && api.port !== configPort){
ui.report('port-clash', [ configPort ])
}

// Prefer network port
api.port = parseInt(configPort);
networkConfig.url = `http://${api.host}:${api.port}`;
}

/**
* Configures mocha to generate a json object which maps which tests
* hit which lines of code.
Expand Down
9 changes: 4 additions & 5 deletions test/integration/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ describe('Hardhat Plugin: error cases', function() {
}
});

it('tries to launch with a non-existent network', async function(){
it('tries to launch with the network flag', async function(){
const taskArgs = {
network: "does-not-exist"
network: "development"
}

mock.install('Simple', 'simple.js', solcoverConfig);
Expand All @@ -76,9 +76,8 @@ describe('Hardhat Plugin: error cases', function() {
assert.fail();
} catch(err){
assert(
err.message.includes('is not a defined network in hardhat.config.js') &&
err.message.includes('does-not-exist'),
`Should error missing network error: ${err.message}`
err.message.includes('--network cli flag is not supported') &&
`Should error network flag disallowed: ${err.message}`
)
}
});
Expand Down