From d779c7a9d6022204464e552240ea6088fb34aab5 Mon Sep 17 00:00:00 2001 From: cgewecke Date: Mon, 12 Oct 2020 19:08:59 -0700 Subject: [PATCH] Fix ethers wrapped provider data collection / re-enable independent node tests (#30) --- scripts/run-tests.sh | 41 ++++++++++++++++++- src/index.ts | 19 +++++++-- src/types.ts | 2 +- .../buidler-truffle-project/buidler.config.js | 6 +++ test/buidlerevm.node.ts | 12 ++++++ test/ganache.node.ts | 14 +++++++ test/helpers.ts | 7 +++- 7 files changed, 93 insertions(+), 8 deletions(-) create mode 100644 test/buidlerevm.node.ts create mode 100644 test/ganache.node.ts diff --git a/scripts/run-tests.sh b/scripts/run-tests.sh index 496d23f..22629c4 100755 --- a/scripts/run-tests.sh +++ b/scripts/run-tests.sh @@ -1,9 +1,46 @@ #!/usr/bin/env bash set -o errexit +trap cleanup EXIT -# Truffle tests +cleanup() { + if [ -n "$ganache_pid" ] && ps -p $ganache_pid > /dev/null; then + echo "Killing ganache." + kill -9 $ganache_pid + fi + + if [ -n "$buidlerevm_pid" ] && ps -p $buidlerevm_pid > /dev/null; then + echo "Killing buidlerevm." + kill -9 $buidlerevm_pid + fi +} + +start_ganache() { + echo "Launching ganache..." + node_modules/.bin/ganache-cli > /dev/null & + ganache_pid=$! + sleep 4 +} + +start_buidlerevm() { + echo "Launching buidlerevm..." + node_modules/.bin/buidler node > /dev/null & + buidlerevm_pid=$! + sleep 4 +} + + +# Truffle + BuidlerEVM npx mocha test/truffle.ts --timeout 100000 --exit -# Ethers tests +# Ethers + BuidlerEVM npx mocha test/ethers.ts --timeout 100000 --exit + +# Ethers + Buidler Node +start_buidlerevm +npx mocha test/buidlerevm.node.ts --timeout 100000 --exit +cleanup + +# Truffle + Ganache +start_ganache +npx mocha test/ganache.node.ts --timeout 100000 --exit diff --git a/src/index.ts b/src/index.ts index 26c70bc..13e7b55 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,10 @@ import { readFileSync } from "fs"; import { TASK_TEST_RUN_MOCHA_TESTS } from "@nomiclabs/buidler/builtin-tasks/task-names"; import { internalTask } from "@nomiclabs/buidler/config"; -import { ensurePluginLoadedWithUsePlugin, BUIDLEREVM_NETWORK_NAME } from "@nomiclabs/buidler/plugins"; +import { + ensurePluginLoadedWithUsePlugin, + BUIDLEREVM_NETWORK_NAME +} from "@nomiclabs/buidler/plugins"; import { wrapSend } from "@nomiclabs/buidler/internal/core/providers/wrapper"; import AsyncProvider from "./provider"; @@ -71,7 +74,6 @@ function getDefaultOptions( return { artifactType: artifactor.bind(null, config.paths.artifacts), enabled: true, - fast: true, url: url, metadata: { compiler: { @@ -104,14 +106,23 @@ function createGasMeasuringProvider( provider: IEthereumProvider ){ return wrapSend(provider, async (method, params) => { + // Truffle if (method === "eth_getTransactionReceipt") { const receipt = await provider.send(method, params); - if (receipt.status && receipt.transactionHash){ const tx = await provider.send("eth_getTransactionByHash", [receipt.transactionHash]); await mochaConfig.attachments.recordTransaction(receipt, tx); } return receipt; + + // Ethers: will get run twice for deployments (e.g both receipt and txhash are fetched) + } else if (method === 'eth_getTransactionByHash'){ + const receipt = await provider.send("eth_getTransactionReceipt", params) + const tx = await provider.send(method, params) + if (receipt.status){ + await mochaConfig.attachments.recordTransaction(receipt, tx) + } + return tx; } return provider.send(method, params); }); @@ -132,7 +143,7 @@ export default function() { mochaConfig.reporter = "eth-gas-reporter"; mochaConfig.reporterOptions = options; - if (options.fast){ + if (bre.network.name === BUIDLEREVM_NETWORK_NAME || options.fast){ bre.network.provider = createGasMeasuringProvider(bre.network.provider); mochaConfig.reporterOptions.provider = new AsyncProvider(bre.network.provider); mochaConfig.reporterOptions.blockLimit = (bre.network.config).blockGasLimit as number; diff --git a/src/types.ts b/src/types.ts index e4b5440..2f95bcf 100644 --- a/src/types.ts +++ b/src/types.ts @@ -16,5 +16,5 @@ export interface EthGasReporterConfig { metadata: any; artifactType: any; url: string; - fast: boolean; + fast?: boolean; } diff --git a/test/buidler-truffle-project/buidler.config.js b/test/buidler-truffle-project/buidler.config.js index d5e073b..71c5344 100644 --- a/test/buidler-truffle-project/buidler.config.js +++ b/test/buidler-truffle-project/buidler.config.js @@ -14,6 +14,12 @@ module.exports = { runs: 100 } }, + networks: { + development: { + gas: 5000000, + url: "http://localhost:8545" + } + }, gasReporter: { onlyCalledMethods: false } diff --git a/test/buidlerevm.node.ts b/test/buidlerevm.node.ts new file mode 100644 index 0000000..4981831 --- /dev/null +++ b/test/buidlerevm.node.ts @@ -0,0 +1,12 @@ +import { TASK_TEST } from "@nomiclabs/buidler/builtin-tasks/task-names"; +// tslint:disable-next-line no-implicit-dependencies +import { assert } from "chai"; +import { useEnvironment } from "./helpers"; + +describe("Ethers plugin", function() { + useEnvironment(__dirname + "/buidler-ethers-project", "localhost"); + + it("no options", async function() { + await this.env.run(TASK_TEST, { testFiles: [] }); + }); +}); diff --git a/test/ganache.node.ts b/test/ganache.node.ts new file mode 100644 index 0000000..a6aba8b --- /dev/null +++ b/test/ganache.node.ts @@ -0,0 +1,14 @@ +import { TASK_TEST } from "@nomiclabs/buidler/builtin-tasks/task-names"; +// tslint:disable-next-line no-implicit-dependencies +import { assert } from "chai"; + +import { useEnvironment } from "./helpers"; + +describe("Truffle plugin", function() { + useEnvironment(__dirname + "/buidler-truffle-project", "development"); + + it("default", async function() { + this.env.config.gasReporter.onlyCalledMethods = true; + await this.env.run(TASK_TEST, { testFiles: [] }); + }); +}); diff --git a/test/helpers.ts b/test/helpers.ts index 5c10ee7..eaa9587 100644 --- a/test/helpers.ts +++ b/test/helpers.ts @@ -7,12 +7,17 @@ declare module "mocha" { } } -export function useEnvironment(projectPath: string) { +export function useEnvironment(projectPath: string, networkName?: string) { let previousCWD: string; beforeEach("Loading buidler environment", function() { previousCWD = process.cwd(); process.chdir(projectPath); + + if (networkName !== undefined){ + process.env.BUIDLER_NETWORK = networkName; + } + this.env = require("@nomiclabs/buidler"); });