Socket
Socket
Sign inDemoInstall

eth-gas-reporter

Package Overview
Dependencies
Maintainers
1
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eth-gas-reporter - npm Package Compare versions

Comparing version 0.2.0 to 0.2.1

lib/artifactor.js

7

CHANGELOG.md
## Changelog: eth-gas-reporter
0.2.0 / 2019-05-07
==================
* Add E2E tests in CI
* Restore logic that matches tx signatures to contracts as a fallback when it's impossible to
be certain which contract was called (contribution @ItsNickBarry)
* Fix bug which crashed reporter when migrations linked un-deployed contracts
0.1.12 / 2018-09-14

@@ -4,0 +11,0 @@ ===================

286

index.js

@@ -1,218 +0,126 @@

const mocha = require('mocha')
const inherits = require('util').inherits
const sync = require('./sync')
const stats = require('./gasStats.js')
const reqCwd = require('req-cwd')
const sha1 = require('sha1')
const Base = mocha.reporters.Base
const color = Base.color
const log = console.log
const mocha = require("mocha");
const inherits = require("util").inherits;
const Base = mocha.reporters.Base;
const color = Base.color;
const log = console.log;
const utils = require("./lib/utils");
const Config = require("./lib/config");
const TransactionWatcher = require("./lib/transactionWatcher");
const GasTable = require("./lib/gasTable");
const SyncRequest = require("./lib/syncRequest");
// Based on the 'Spec' reporter
function Gas (runner, options) {
if (!(web3.currentProvider.connection || web3.currentProvider.host)) {
console.log('the provider use for the test does not support synchronous call but eth-gas-reporter requires it \n falling back on the Spec reporter');
mocha.reporters.Spec.call(this, runner);
return;
}
/**
* Based on the Mocha 'Spec' reporter. Watches an Ethereum test suite run
* and collects data about method & deployments gas usage. Mocha executes the hooks
* in this reporter synchronously so any client calls here should be executed
* via low-level RPC interface using sync-request. (see /lib/syncRequest)
* An exception is made for fetching gas & currency price data from coinmarketcap and
* ethgasstation (we hope that single call will complete by the time the tests finish running)
*
* @param {Object} runner mocha's runner
* @param {Object} options reporter.options (see README example usage)
*/
function Gas(runner, options) {
// Spec reporter
Base.call(this, runner);
const self = this;
const self = this
let indents = 0
let n = 0
let indents = 0;
let n = 0;
let failed = false;
let startBlock
let deployStartBlock
let methodMap
let deployMap
let contractNameFromCodeHash;
let indent = () => Array(indents).join(" ");
// Load config / keep .ethgas.js for backward compatibility
let config;
if (options && options.reporterOptions){
config = options.reporterOptions
} else {
config = reqCwd.silent('./.ethgas.js') || {}
}
// Gas reporter setup
const config = new Config(options.reporterOptions);
const sync = new SyncRequest(config.url);
const watch = new TransactionWatcher(config);
const table = new GasTable(config);
config.src = config.src || 'contracts'; // default contracts folder
// TODO grab the contract srcpath from truffle / truffle config ?
// This calls the cloud, start running it.
utils.setGasAndPriceRates(config);
// Start getting this data when the reporter loads.
stats.getGasAndPriceRates(config);
// ------------------------------------ Runners -------------------------------------------------
// ------------------------------------ Helpers -------------------------------------------------
const indent = () => Array(indents).join(' ')
runner.on("start", () => {
watch.data.initialize(config);
});
const methodAnalytics = (methodMap) => {
let gasUsed = 0
const endBlock = sync.blockNumber();
runner.on("suite", suite => {
++indents;
log(color("suite", "%s%s"), indent(), suite.title);
});
while (startBlock <= endBlock) {
let block = sync.getBlockByNumber(startBlock);
if (block) {
// Add to running tally for this test
gasUsed += parseInt(block.gasUsed, 16);
// Compile per method stats
methodMap && block.transactions.forEach(tx => {
const transaction = sync.getTransactionByHash(tx);
const receipt = sync.getTransactionReceipt(tx);
// Don't count methods that throw
const threw = parseInt(receipt.status) === 0 || receipt.status === false;
if (threw) return
const code = sync.getCode(transaction.to);
const hash = sha1(code);
let contractName = contractNameFromCodeHash[hash];
// Handle cases where we don't have a deployment record for the contract
// or where we *do* (from migrations) but tx actually interacts with a
// proxy / something doesn't match.
let isProxied = false;
if (contractName) {
let candidateId = stats.getMethodID(contractName, transaction.input)
isProxied = !methodMap[candidateId]
}
// If unfound, search by fnHash alone instead of contract_fnHash
if (!contractName || isProxied ) {
let key = transaction.input.slice(2, 10);
let matches = Object.values(methodMap).filter(el => el.key === key);
if (matches.length >= 1) {
contractName = matches[0].contract;
}
}
const id = stats.getMethodID(contractName, transaction.input)
if (methodMap[id]) {
methodMap[id].gasData.push(parseInt(receipt.gasUsed, 16))
methodMap[id].numberOfCalls++
}
})
}
startBlock++
runner.on("suite end", () => {
--indents;
if (indents === 1) {
log();
}
return gasUsed
}
});
const deployAnalytics = (deployMap) => {
const endBlock = sync.blockNumber();
runner.on("pending", test => {
let fmt = indent() + color("pending", " - %s");
log(fmt, test.title);
});
while (deployStartBlock <= endBlock) {
let block = sync.getBlockByNumber(deployStartBlock);
runner.on("test", () => {
watch.beforeStartBlock = sync.blockNumber();
});
block && block.transactions.forEach(tx => {
const receipt = sync.getTransactionReceipt(tx);
const threw = parseInt(receipt.status) === 0 || receipt.status === false;
runner.on("hook end", () => {
watch.itStartBlock = sync.blockNumber() + 1;
});
if (receipt.contractAddress && !threw) {
const transaction = sync.getTransactionByHash(tx)
runner.on("pass", test => {
let fmt;
let fmtArgs;
let gasUsedString;
let consumptionString;
let timeSpentString = color(test.speed, "%dms");
const matches = deployMap.filter(contract => {
return stats.matchBinaries(transaction.input, contract.binary);
})
const gasUsed = watch.blocks();
if(matches && matches.length){
const match = matches.find(item => item.binary !== '0x');
if (match) {
// We have to get code that might be linked here in
// in order to match correctly at the method ids
const code = sync.getCode(receipt.contractAddress);
const hash = sha1(code);
match.gasData.push(parseInt(receipt.gasUsed, 16));
contractNameFromCodeHash[hash] = match.name;
}
}
}
})
deployStartBlock++
}
}
// ------------------------------------ Runners -------------------------------------------------
runner.on('start', () => {
({ methodMap, deployMap, contractNameFromCodeHash } = stats.mapMethodsToContracts(artifacts, config.src))
})
runner.on('suite', suite => {
++indents
log(color('suite', '%s%s'), indent(), suite.title)
})
runner.on('suite end', () => {
--indents
if (indents === 1) {
log()
}
})
runner.on('pending', test => {
let fmt = indent() + color('pending', ' - %s')
log(fmt, test.title)
})
runner.on('test', () => { deployStartBlock = sync.blockNumber() })
runner.on('hook end', () => { startBlock = sync.blockNumber() + 1 })
runner.on('pass', test => {
let fmt
let fmtArgs
let gasUsedString
deployAnalytics(deployMap)
let gasUsed = methodAnalytics(methodMap)
let showTimeSpent = config.showTimeSpent || false
let timeSpentString = color(test.speed, '%dms')
let consumptionString
if (gasUsed) {
gasUsedString = color('checkmark', '%d gas')
gasUsedString = color("checkmark", "%d gas");
if (showTimeSpent) {
consumptionString = ' (' + timeSpentString + ', ' + gasUsedString + ')'
fmtArgs = [test.title, test.duration, gasUsed]
if (config.showTimeSpent) {
consumptionString = " (" + timeSpentString + ", " + gasUsedString + ")";
fmtArgs = [test.title, test.duration, gasUsed];
} else {
consumptionString = ' (' + gasUsedString + ')'
fmtArgs = [test.title, gasUsed]
consumptionString = " (" + gasUsedString + ")";
fmtArgs = [test.title, gasUsed];
}
fmt = indent() +
color('checkmark', ' ' + Base.symbols.ok) +
color('pass', ' %s') +
consumptionString
fmt =
indent() +
color("checkmark", " " + Base.symbols.ok) +
color("pass", " %s") +
consumptionString;
} else {
if (showTimeSpent) {
consumptionString = ' (' + timeSpentString + ')'
fmtArgs = [test.title, test.duration]
if (config.showTimeSpent) {
consumptionString = " (" + timeSpentString + ")";
fmtArgs = [test.title, test.duration];
} else {
consumptionString = ''
fmtArgs = [test.title]
consumptionString = "";
fmtArgs = [test.title];
}
fmt = indent() +
color('checkmark', ' ' + Base.symbols.ok) +
color('pass', ' %s') +
consumptionString
fmt =
indent() +
color("checkmark", " " + Base.symbols.ok) +
color("pass", " %s") +
consumptionString;
}
log.apply(null, [fmt, ...fmtArgs])
})
log.apply(null, [fmt, ...fmtArgs]);
});
runner.on('fail', test => {
runner.on("fail", test => {
failed = true;
let fmt = indent() + color('fail', ' %d) %s')
log()
log(fmt, ++n, test.title)
})
let fmt = indent() + color("fail", " %d) %s");
log();
log(fmt, ++n, test.title);
});
runner.on('end', () => {
stats.generateGasStatsReport(methodMap, deployMap, contractNameFromCodeHash)
self.epilogue()
runner.on("end", () => {
table.generate(watch.data, config);
self.epilogue();
});

@@ -224,4 +132,4 @@ }

*/
inherits(Gas, Base)
inherits(Gas, Base);
module.exports = Gas
module.exports = Gas;
module.exports = {
networks: {
development: {
host: 'localhost',
host: "localhost",
port: 8545,
network_id: '*',
websockets: true
network_id: "*",
websockets: process.env.TEST === "integration" ? true : false
}
},
mocha: {
reporter: 'eth-gas-reporter',
reporter: "eth-gas-reporter",
reporterOptions: {

@@ -18,6 +18,8 @@ currency: "chf",

rst: true,
rstTitle: 'Gas Usage',
showTimeSpent: true
rstTitle: "Gas Usage",
showTimeSpent: true,
excludeContracts: ["Migrations"],
proxyResolver: "EtherRouter"
}
}
}
};
{
"name": "eth-gas-reporter",
"version": "0.1.12",
"version": "0.2.0",
"description": "Mocha reporter which shows gas used per unit test.",

@@ -8,5 +8,10 @@ "main": "index.js",

"geth": "./scripts/geth.sh",
"test": "./mock/test.sh",
"test": "./mock/scripts/test.sh",
"ci": "./scripts/ci.sh"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
},
"repository": {

@@ -33,2 +38,3 @@ "type": "git",

"colors": "^1.1.2",
"ethers": "^4.0.28",
"lodash": "^4.17.11",

@@ -45,5 +51,14 @@ "mocha": "^4.1.0",

"devDependencies": {
"ganache-cli": "^6.4.3",
"truffle": "^5.0.15"
"@nomiclabs/buidler": "^1.0.0-beta.7",
"@nomiclabs/buidler-truffle5": "^1.0.0-beta.7",
"@nomiclabs/buidler-web3": "^1.0.0-beta.7",
"ganache-cli": "6.4.3",
"geth-dev-assistant": "^0.0.3",
"husky": "^2.3.0",
"prettier": "1.17.1",
"pretty-quick": "^1.11.0",
"truffle": "5.0.12",
"web3": "1.0.0-beta.37",
"yarn": "^1.16.0"
}
}
module.exports = {
networks: {
development: {
host: 'localhost',
host: "localhost",
port: 8545,
network_id: '*' // Match any network id
network_id: "*" // Match any network id
}
},
mocha: {
reporter: 'eth-gas-reporter',
reporter: "eth-gas-reporter"
}
}
};
{
"name": "eth-gas-reporter",
"version": "0.2.0",
"version": "0.2.1",
"description": "Mocha reporter which shows gas used per unit test.",

@@ -8,5 +8,10 @@ "main": "index.js",

"geth": "./scripts/geth.sh",
"test": "./mock/test.sh",
"test": "./mock/scripts/test.sh",
"ci": "./scripts/ci.sh"
},
"husky": {
"hooks": {
"pre-commit": "pretty-quick --staged"
}
},
"repository": {

@@ -33,4 +38,5 @@ "type": "git",

"colors": "^1.1.2",
"ethers": "^4.0.28",
"lodash": "^4.17.11",
"mocha": "^4.1.0",
"mocha": "^5.2.0",
"req-cwd": "^2.0.0",

@@ -45,5 +51,15 @@ "request": "^2.88.0",

"devDependencies": {
"ganache-cli": "^6.4.3",
"truffle": "^5.0.15"
"@nomiclabs/buidler": "^1.0.0-beta.7",
"@nomiclabs/buidler-truffle5": "^1.0.0-beta.7",
"@nomiclabs/buidler-web3": "^1.0.0-beta.7",
"fp-ts": "^1.19.0",
"ganache-cli": "6.4.3",
"geth-dev-assistant": "^0.0.3",
"husky": "^2.3.0",
"prettier": "1.17.1",
"pretty-quick": "^1.11.0",
"truffle": "5.0.12",
"web3": "1.0.0-beta.37",
"yarn": "^1.16.0"
}
}

@@ -7,12 +7,13 @@ # eth-gas-reporter

A mocha reporter for Truffle.
+ Gas usage per unit test.
+ Average gas usage per method.
+ Contract deployment costs.
+ Real currency costs.
- Gas usage per unit test.
- Average gas usage per method.
- Contract deployment costs.
- Real currency costs.
![screen shot 2018-08-01 at 10 13 56 am](https://user-images.githubusercontent.com/7332026/43537357-f64f031e-9573-11e8-9348-315d9d4a8476.png)
![screen shot 2018-08-01 at 10 13 34 am](https://user-images.githubusercontent.com/7332026/43537353-f1889f70-9573-11e8-82c3-f93d3901db64.png)
### Install
### Install
```javascript

@@ -23,2 +24,3 @@ npm install --save-dev eth-gas-reporter

### Truffle config
```javascript

@@ -41,40 +43,48 @@ module.exports = {

| Option | Type | Default | Description |
| ------ | ---- | ------- | ----------- |
| currency | *String* | 'EUR' | National currency to represent gas costs in. Exchange rates loaded at runtime from the `coinmarketcap` api. Available currency codes can be found [here](https://coinmarketcap.com/api/). |
| gasPrice | *Number* | (varies) | Denominated in `gwei`. Default is loaded at runtime from the `eth gas station` api |
| outputFile | *String* | stdout | File path to write report output to |
| noColors | *Boolean* | false | Suppress report color. Useful if you are printing to file b/c terminal colorization corrupts the text. |
| onlyCalledMethods | *Boolean* | true | Omit methods that are never called from report. |
| rst | *Boolean* | false | Output with a reStructured text code-block directive. Useful if you want to include report in RTD |
| rstTitle | *String* | "" | Title for reStructured text header (See Travis for example output) |
| showTimeSpent | *Boolean* | false | Show the amount of time spent as well as the gas consumed |
| src | *String* | "contracts" | Folder in root directory to begin search for `.sol` files. This can also be a path to a subfolder relative to the root, e.g. "planets/annares/contracts" |
| Option | Type | Default | Description |
| ----------------- | ---------- | ------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| currency | _String_ | 'EUR' | National currency to represent gas costs in. Exchange rates loaded at runtime from the `coinmarketcap` api. Available currency codes can be found [here](https://coinmarketcap.com/api/). |
| gasPrice | _Number_ | (varies) | Denominated in `gwei`. Default is loaded at runtime from the `eth gas station` api |
| outputFile | _String_ | stdout | File path to write report output to |
| noColors | _Boolean_ | false | Suppress report color. Useful if you are printing to file b/c terminal colorization corrupts the text. |
| onlyCalledMethods | _Boolean_ | true | Omit methods that are never called from report. |
| rst | _Boolean_ | false | Output with a reStructured text code-block directive. Useful if you want to include report in RTD |
| rstTitle | _String_ | "" | Title for reStructured text header (See Travis for example output) |
| showTimeSpent | _Boolean_ | false | Show the amount of time spent as well as the gas consumed |
| excludeContracts | _String[]_ | [] | Contract names to exclude from report. Ex: `['Migrations']` |
| src | _String_ | "contracts" | Folder in root directory to begin search for `.sol` files. This can also be a path to a subfolder relative to the root, e.g. "planets/annares/contracts" |
| url | _String_ | value of `web3.currentProvider.host` | RPC client url (e.g. "http://localhost:8545") |
| proxyResolver | _Function_ | none | Custom method to resolve identity of methods managed by a proxy contract. |
### Examples
+ [gnosis/gnosis-contracts](https://github.com/cgewecke/eth-gas-reporter/blob/master/docs/gnosis.md)
+ [windingtree/LifToken](https://github.com/cgewecke/eth-gas-reporter/blob/master/docs/lifToken.md)
- [gnosis/gnosis-contracts](https://github.com/cgewecke/eth-gas-reporter/blob/master/docs/gnosis.md)
- [windingtree/LifToken](https://github.com/cgewecke/eth-gas-reporter/blob/master/docs/lifToken.md)
### Usage Notes
+ Requires Node >= 8.
+ Method calls that throw are filtered from the stats.
+ Contracts that are only ever created by other contracts within Solidity are not shown in the deployments table.
- Requires Node >= 8.
- Method calls that throw are filtered from the stats.
- Contracts that are only ever created by other contracts within Solidity are not shown in the deployments table.
### Contributions
Please feel free to open a PR (or an issue) for anything. The units are an integration test and one of them is expected to fail, verifying that the table prints at the end of a suite even when there are errors. If you're adding an option, you can vaildate it in CI by adding it to the mock options config located [here](https://github.com/cgewecke/eth-gas-reporter/blob/master/mock/config-template.js#L13-L19).
Feel free to open PRs or issues. There is an integration test and one of the mock test cases is expected to fail. If you're adding an option, you can vaildate it in CI by adding it to the mock options config located [here](https://github.com/cgewecke/eth-gas-reporter/blob/master/mock/config-template.js#L13-L19).
### Credits
All the ideas in this utility have been borrowed from elsewhere. Many thanks to:
+ [@maurelian](https://github.com/maurelian) - Mocha reporting gas instead of time is his idea.
+ [@cag](https://github.com/cag) - The table borrows from / is based his gas statistics work for the Gnosis contracts.
+ [Neufund](https://github.com/Neufund/ico-contracts) - Block limit size ratios for contract deployments and euro pricing are borrowed from their `ico-contracts` test suite.
- [@maurelian](https://github.com/maurelian) - Mocha reporting gas instead of time is his idea.
- [@cag](https://github.com/cag) - The table borrows from / is based his gas statistics work for the Gnosis contracts.
- [Neufund](https://github.com/Neufund/ico-contracts) - Block limit size ratios for contract deployments and euro pricing are borrowed from their `ico-contracts` test suite.
### Contributors
+ [@cgewecke](https://github.com/cgewecke)
+ [@rmuslimov](https://github.com/rmuslimov)
+ [@area](https://github.com/area)
+ [@ldub](https://github.com/ldub)
+ [@ben-kaufman](https://github.com/ben-kaufman)
+ [@wighawag](https://github.com/wighawag)
- [@cgewecke](https://github.com/cgewecke)
- [@rmuslimov](https://github.com/rmuslimov)
- [@area](https://github.com/area)
- [@ldub](https://github.com/ldub)
- [@ben-kaufman](https://github.com/ben-kaufman)
- [@wighawag](https://github.com/wighawag)
- [@ItsNickBarry](https://github.com/ItsNickBarry)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc