Skip to content
This repository has been archived by the owner on Mar 17, 2024. It is now read-only.

Optimize transaction watcher #167

Merged
merged 3 commits into from
Jul 30, 2019
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
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
16 changes: 16 additions & 0 deletions lib/gasData.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const Artifactor = require("./artifactor");
*/
class GasData {
constructor() {
this.addressCache = {};
this.methods = {};
this.deployments = [];
this.codeHashMap = {};
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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];
Expand Down Expand Up @@ -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;
7 changes: 6 additions & 1 deletion lib/syncRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 2 additions & 3 deletions lib/transactionWatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion mock/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
17 changes: 14 additions & 3 deletions scripts/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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