diff --git a/index.js b/index.js index b16de84..b32c315 100644 --- a/index.js +++ b/index.js @@ -64,10 +64,13 @@ function Gas(runner, options) { runner.on("test", () => { watch.beforeStartBlock = sync.blockNumber(); + watch.data.resetAddressCache(); }); - runner.on("hook end", () => { - watch.itStartBlock = sync.blockNumber() + 1; + runner.on("hook end", hook => { + if (hook.title.includes("before each")) { + watch.itStartBlock = sync.blockNumber() + 1; + } }); runner.on("pass", test => { diff --git a/lib/gasData.js b/lib/gasData.js index 5b7c809..d1e8cbc 100644 --- a/lib/gasData.js +++ b/lib/gasData.js @@ -9,6 +9,7 @@ const Artifactor = require("./artifactor"); */ class GasData { constructor() { + this.addressCache = {}; this.methods = {}; this.deployments = []; this.codeHashMap = {}; @@ -102,9 +103,12 @@ class GasData { * @param {String} address contract address */ trackNameByAddress(name, address) { + if (this.addressIsCached(address)) return; + const code = this.sync.getCode(address); const hash = code ? sha1(code) : null; this.codeHashMap[hash] = name; + this.addressCache[address] = name; } /** @@ -113,6 +117,10 @@ class GasData { * @return {String} contract name */ getNameByAddress(address) { + if (this.addressIsCached(address)) { + return this.addressCache[address]; + } + const code = this.sync.getCode(address); const hash = code ? sha1(code) : null; return this.codeHashMap[hash]; @@ -166,6 +174,14 @@ class GasData { getAllContractsWithMethod(signature) { return Object.values(this.methods).filter(el => el.key === signature); } + + addressIsCached(address) { + return Object.keys(this.addressCache).includes(address); + } + + resetAddressCache() { + this.addressCache = {}; + } } module.exports = GasData; diff --git a/lib/syncRequest.js b/lib/syncRequest.js index 180ae61..0d2dd25 100644 --- a/lib/syncRequest.js +++ b/lib/syncRequest.js @@ -21,13 +21,18 @@ class Sync { return this.request("eth_getCode", [address, "latest"]); } + blockNumber() { + const val = this.request("eth_blockNumber", []); + return parseInt(val, 16); + } + getLatestBlock() { return this.request("eth_getBlockByNumber", ["latest", false]); } getBlockByNumber(number) { const hexNumber = `0x${number.toString(16)}`; - return this.request("eth_getBlockByNumber", [hexNumber, false]); + return this.request("eth_getBlockByNumber", [hexNumber, true]); } blockNumber() { diff --git a/lib/transactionWatcher.js b/lib/transactionWatcher.js index 522372c..43809e5 100644 --- a/lib/transactionWatcher.js +++ b/lib/transactionWatcher.js @@ -36,9 +36,8 @@ class TransactionWatcher { } // Collect methods and deployments data - block.transactions.forEach(tx => { - const transaction = this.sync.getTransactionByHash(tx); - const receipt = this.sync.getTransactionReceipt(tx); + block.transactions.forEach(transaction => { + const receipt = this.sync.getTransactionReceipt(transaction.hash); // Omit transactions that throw if (parseInt(receipt.status) === 0) return; diff --git a/mock/package.json b/mock/package.json index 560dd54..f8e90fe 100644 --- a/mock/package.json +++ b/mock/package.json @@ -1,6 +1,6 @@ { "name": "eth-gas-reporter", - "version": "0.2.7", + "version": "0.2.8", "description": "Mocha reporter which shows gas used per unit test.", "main": "index.js", "scripts": { diff --git a/scripts/ci.sh b/scripts/ci.sh index 00f84e5..79b990a 100755 --- a/scripts/ci.sh +++ b/scripts/ci.sh @@ -26,13 +26,24 @@ elif [ "$TEST" = "colony" ]; then yarn yarn remove -W eth-gas-reporter --dev + SLUG="$TRAVIS_REPO_SLUG" + BRANCH="$TRAVIS_BRANCH" + + if test -z "$TRAVIS_PULL_REQUEST_SLUG"; then + SLUG="$TRAVIS_PULL_REQUEST_SLUG" + fi + + if test -z "$TRAVIS_PULL_REQUEST_BRANCH"; then + BRANCH="$TRAVIS_PULL_REQUEST_BRANCH" + fi + echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" - echo "TESTING BRANCH: https://github.com/$TRAVIS_PULL_REQUEST_SLUG.git#$TRAVIS_PULL_REQUEST_BRANCH" + echo "TESTING BRANCH: https://github.com/$SLUG.git#$BRANCH" echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" - yarn add -W https://github.com/$TRAVIS_PULL_REQUEST_SLUG.git#$TRAVIS_PULL_REQUEST_BRANCH + yarn add -W https://github.com/"$SLUG".git#"$BRANCH" git submodule update --init yarn run provision:token:contracts DEBUG_CODECHECKS_TABLE=true yarn run test:contracts:gasCosts -fi +fi \ No newline at end of file