fix(@embark/test-runner): fix reporter to only catch gas for txs

Before, we added the gas for all receipts that came in because they
had a `gasUsed`, instead of adding the gas for receipts that came
with a transaction
This commit is contained in:
Jonathan Rainville 2020-02-04 11:37:46 -05:00
parent a016fa8fb9
commit 0e30bf3926
1 changed files with 17 additions and 1 deletions

View File

@ -1,4 +1,5 @@
const chalk = require('chalk'); const chalk = require('chalk');
const { blockchain: blockchainConstants } = require('embark-core/constants');
class Reporter { class Reporter {
constructor(embark, options) { constructor(embark, options) {
@ -9,17 +10,32 @@ class Reporter {
this.fails = 0; this.fails = 0;
this.gasAccumulator = 0; this.gasAccumulator = 0;
// Keep track of TXs because otherwise, we can intercept random receipts
this.transactionsHashes = [];
this.wireGasUsage(); this.wireGasUsage();
} }
wireGasUsage() { wireGasUsage() {
const {events} = this.embark; const {events} = this.embark;
events.on('blockchain:proxy:response', (params) => { events.on('blockchain:proxy:response', (params) => {
const { result } = params.response; if (params.request.method === blockchainConstants.transactionMethods.eth_sendTransaction) {
// We just gather data and wait for the receipt
return this.transactionsHashes.push(params.response.result);
} else if (params.request.method === blockchainConstants.transactionMethods.eth_sendRawTransaction) {
return this.transactionsHashes.push(params.response.result);
}
const {result} = params.response;
if (!result || !result.gasUsed) { if (!result || !result.gasUsed) {
return; return;
} }
const hashIndex = this.transactionsHashes.indexOf(result.transactionHash);
if (hashIndex === -1) {
// Probably just a normal receipt
return;
}
this.transactionsHashes.splice(hashIndex, 1);
const gas = parseInt(result.gasUsed, 16); const gas = parseInt(result.gasUsed, 16);
this.gasAccumulator += gas; this.gasAccumulator += gas;