Gas Price not modified by coverage

This commit is contained in:
Anthony Laibe 2018-09-17 14:12:20 +01:00
parent a43627de5b
commit 18a9349d77
3 changed files with 41 additions and 30 deletions

View File

@ -7,6 +7,7 @@ class EmbarkSpec extends Base {
super(runner, options); super(runner, options);
const self = this; const self = this;
self.listenForGas = true;
self.embarkEvents = options.reporterOptions.events; self.embarkEvents = options.reporterOptions.events;
self.gasDetails = options.reporterOptions.gasDetails; self.gasDetails = options.reporterOptions.gasDetails;
self.gasLimit = options.reporterOptions.gasLimit; self.gasLimit = options.reporterOptions.gasLimit;
@ -27,6 +28,9 @@ class EmbarkSpec extends Base {
} }
function onBlockHeader(blockHeader) { function onBlockHeader(blockHeader) {
if(!self.listenForGas) {
return;
}
self.stats.totalGasCost += blockHeader.gasUsed; self.stats.totalGasCost += blockHeader.gasUsed;
self.stats.test.gasUsed += blockHeader.gasUsed; self.stats.test.gasUsed += blockHeader.gasUsed;
} }
@ -35,6 +39,9 @@ class EmbarkSpec extends Base {
self.embarkEvents.on("deploy:contract:receipt", onContractReceipt); self.embarkEvents.on("deploy:contract:receipt", onContractReceipt);
} }
self.embarkEvents.on("block:header", onBlockHeader); self.embarkEvents.on("block:header", onBlockHeader);
self.embarkEvents.setCommandHandler("reporter:toggleGasListener", () => {
self.listenForGas = !self.listenForGas;
});
function indent() { function indent() {
return Array(indents).join(' '); return Array(indents).join(' ');

View File

@ -61,7 +61,7 @@ module.exports = {
}, },
function setupGlobalNamespace(files, next) { function setupGlobalNamespace(files, next) {
// TODO put default config // TODO put default config
const test = new Test({loglevel, node: options.node}); const test = new Test({loglevel, node: options.node, coverage: options.coverage});
global.embark = test; global.embark = test;
global.assert = assert; global.assert = assert;
global.config = test.config.bind(test); global.config = test.config.bind(test);
@ -105,6 +105,7 @@ module.exports = {
gasDetails: options.gasDetails, gasDetails: options.gasDetails,
gasLimit: 6000000 gasLimit: 6000000
}); });
mocha.addFile(file); mocha.addFile(file);
mocha.suite.timeout(0); mocha.suite.timeout(0);

View File

@ -102,37 +102,40 @@ class Test {
let simProvider = this.sim.provider(this.simOptions); let simProvider = this.sim.provider(this.simOptions);
// Here we patch the sendAsync method on the provider. The goal behind this is to force pure/constant/view calls to become if (this.options.coverage) {
// transactions, so that we can pull in execution traces and account for those executions in code coverage. // Here we patch the sendAsync method on the provider. The goal behind this is to force pure/constant/view calls to become
// // transactions, so that we can pull in execution traces and account for those executions in code coverage.
// Instead of a simple call, here's what happens: //
// // Instead of a simple call, here's what happens:
// 1) A transaction is sent with the same payload, and a pre-defined gas price; //
// 2) We wait for the transaction to be mined by asking for the receipt; // 1) A transaction is sent with the same payload, and a pre-defined gas price;
// 3) Once we get the receipt back, we dispatch the real call and pass the original callback; // 2) We wait for the transaction to be mined by asking for the receipt;
// // 3) Once we get the receipt back, we dispatch the real call and pass the original callback;
// This will still allow tests to get the return value from the call and run contracts unmodified. //
simProvider.realSendAsync = simProvider.sendAsync.bind(simProvider); // This will still allow tests to get the return value from the call and run contracts unmodified.
simProvider.sendAsync = function(payload, cb) { simProvider.realSendAsync = simProvider.sendAsync.bind(simProvider);
if(payload.method !== 'eth_call') { simProvider.sendAsync = function(payload, cb) {
return simProvider.realSendAsync(payload, cb); if(payload.method !== 'eth_call') {
} return simProvider.realSendAsync(payload, cb);
}
self.engine.events.request('reporter:toggleGasListener');
let newParams = Object.assign({}, payload.params[0], {gasPrice: '0x77359400'});
let newPayload = {
id: payload.id + 1,
method: 'eth_sendTransaction',
params: [newParams],
jsonrpc: payload.jsonrpc
};
let newParams = Object.assign({}, payload.params[0], {gasPrice: '0x77359400'}); simProvider.realSendAsync(newPayload, (_err, response) => {
let newPayload = { let txHash = response.result;
id: payload.id + 1, self.web3.eth.getTransactionReceipt(txHash, (_err, _res) => {
method: 'eth_sendTransaction', self.engine.events.request('reporter:toggleGasListener');
params: [newParams], simProvider.realSendAsync(payload, cb);
jsonrpc: payload.jsonrpc });
};
simProvider.realSendAsync(newPayload, (_err, response) => {
let txHash = response.result;
self.web3.eth.getTransactionReceipt(txHash, (_err, _res) => {
simProvider.realSendAsync(payload, cb);
}); });
}); };
}; }
this.web3.setProvider(simProvider); this.web3.setProvider(simProvider);
callback(); callback();