2018-08-08 18:26:40 +00:00
|
|
|
/*global web3*/
|
|
|
|
const process = require('process');
|
|
|
|
|
2018-08-09 19:46:10 +00:00
|
|
|
const fs = require('../../core/fs');
|
2018-08-07 19:26:39 +00:00
|
|
|
const ContractSources = require('./contract_sources');
|
|
|
|
|
2018-08-08 18:26:40 +00:00
|
|
|
// Set up the web3 extension
|
|
|
|
web3.extend({
|
|
|
|
property: 'debug',
|
|
|
|
methods: [{name: 'traceTransaction', call: 'debug_traceTransaction', params: 2}]
|
|
|
|
});
|
|
|
|
|
2018-08-01 15:48:17 +00:00
|
|
|
class CodeCoverage {
|
2018-08-01 16:08:47 +00:00
|
|
|
constructor(embark, _options) {
|
2018-08-01 15:48:17 +00:00
|
|
|
this.events = embark.events;
|
|
|
|
this.logger = embark.logger;
|
2018-08-01 16:08:47 +00:00
|
|
|
|
2018-08-07 19:26:39 +00:00
|
|
|
embark.events.on('contracts:compile:solc', this.compileSolc.bind(this));
|
|
|
|
embark.events.on('contracts:compiled:solc', this.compiledSolc.bind(this));
|
|
|
|
embark.events.on('contracts:run:solc', this.runSolc.bind(this));
|
|
|
|
embark.events.on('block:header', this.runSolc.bind(this));
|
2018-08-08 18:26:40 +00:00
|
|
|
|
|
|
|
this.seenTransactions = {};
|
|
|
|
|
|
|
|
var self = this;
|
2018-08-09 19:46:10 +00:00
|
|
|
|
|
|
|
process.on('exit', (_code) => {
|
|
|
|
const coverageReportPath = fs.dappPath('.embark', 'coverage.json');
|
|
|
|
fs.writeFileSync(coverageReportPath, JSON.stringify(self.coverageReport));
|
2018-08-08 18:26:40 +00:00
|
|
|
});
|
2018-08-07 19:26:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
compileSolc(input) {
|
2018-08-10 14:40:58 +00:00
|
|
|
var sources = Object.keys(input.sources).reduce((sum, elm, _i) => {
|
|
|
|
sum[elm] = input.sources[elm].content;
|
|
|
|
return sum;
|
|
|
|
}, {});
|
2018-08-07 19:26:39 +00:00
|
|
|
|
|
|
|
this.contractSources = new ContractSources(sources);
|
|
|
|
}
|
|
|
|
|
|
|
|
compiledSolc(output) {
|
|
|
|
this.contractSources.parseSolcOutput(output);
|
|
|
|
}
|
|
|
|
|
2018-08-08 18:26:40 +00:00
|
|
|
async runSolc(receipt) {
|
2018-08-09 20:46:20 +00:00
|
|
|
let block = await web3.eth.getBlock(receipt.number);
|
|
|
|
if(block.transactions.length == 0) return;
|
|
|
|
|
|
|
|
let requests = [];
|
|
|
|
for(let i in block.transactions) {
|
2018-08-08 18:26:40 +00:00
|
|
|
var txHash = block.transactions[i];
|
2018-08-01 16:08:47 +00:00
|
|
|
|
2018-08-08 18:26:40 +00:00
|
|
|
if(this.seenTransactions[txHash]) return;
|
|
|
|
|
|
|
|
this.seenTransactions[txHash] = true;
|
2018-08-09 20:46:20 +00:00
|
|
|
requests.push(web3.debug.traceTransaction(txHash, {}));
|
|
|
|
}
|
|
|
|
|
|
|
|
let traces = await Promise.all(requests);
|
|
|
|
|
|
|
|
for(let i in traces) {
|
|
|
|
var cov = this.contractSources.generateCodeCoverage(traces[i]);
|
2018-08-08 18:26:40 +00:00
|
|
|
this.coverageReport = cov;
|
|
|
|
}
|
2018-08-01 15:48:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 16:08:47 +00:00
|
|
|
module.exports = CodeCoverage;
|