Skip to content
This repository has been archived by the owner on Jun 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #95 from ryepdx/master
Browse files Browse the repository at this point in the history
Fix error caused by old "from" addresses sometimes getting used in ne…
  • Loading branch information
Ryan committed Jan 24, 2016
2 parents 7e1fa08 + b6aecd4 commit fbafd4c
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 50 deletions.
70 changes: 58 additions & 12 deletions doc/dist/lib/vmtest.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,67 @@

<span class="hljs-keyword">var</span> contractClass = that.web3.eth.contract(that.contract.abi);

that.deploy(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, receipt</span>) </span>{
<span class="hljs-keyword">var</span> runTestOn = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">contract</span>) </span>{
that.runInstanceTestByName(contract, that.tests[testIndex], cb);
};

<span class="hljs-keyword">var</span> setUpHandlerFor = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">contract</span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, txHash</span>) </span>{
<span class="hljs-keyword">if</span> (err) {
<span class="hljs-keyword">return</span> cb(err);
}

that.web3.eth.getTransactionReceipt(txHash, <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err, receipt</span>) </span>{
<span class="hljs-keyword">if</span>( receipt === <span class="hljs-literal">null</span> &amp;&amp; err === <span class="hljs-literal">null</span>) {
err = <span class="hljs-string">"setUp failed - exception thrown"</span>;
}

<span class="hljs-keyword">if</span> (err) {
<span class="hljs-keyword">var</span> testResult = {
title: <span class="hljs-string">"setUp failed"</span>,
message: err,
logs: [],
failed: <span class="hljs-literal">true</span>
};
cb(err, testResult);
<span class="hljs-keyword">return</span>;
}

runTestOn(contract);
});
};
};

<span class="hljs-keyword">var</span> getCodeHandlerFor = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">address</span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, code</span>) </span>{
<span class="hljs-keyword">if</span> (err) {
<span class="hljs-keyword">return</span> cb(err);
}

<span class="hljs-keyword">if</span> (code === <span class="hljs-string">'0x'</span>) {
<span class="hljs-keyword">return</span> cb(<span class="hljs-string">"Contract failed to deploy."</span>);
}

<span class="hljs-keyword">var</span> contract = contractClass.at(address);

<span class="hljs-keyword">if</span>( contract.setUp !== <span class="hljs-literal">undefined</span> ) {
contract.setUp(setUpHandlerFor(contract));
}
<span class="hljs-keyword">else</span> {
runTestOn(contract);
}
};
};

<span class="hljs-keyword">var</span> deployHandler = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, receipt</span>) </span>{
<span class="hljs-keyword">if</span> (err) {
<span class="hljs-keyword">return</span> cb(err);
}
that.web3.eth.getCode(receipt.contractAddress,
getCodeHandlerFor(receipt.contractAddress));
};

that.runInstanceTestByName(
contractClass.at(receipt.contractAddress),
that.tests[testIndex], cb);
});
that.deploy(deployHandler);
}

testCount() {
Expand Down Expand Up @@ -111,7 +163,7 @@
<span class="hljs-keyword">if</span>( receipt == <span class="hljs-literal">null</span> ) {
failed = <span class="hljs-literal">true</span>;
logs = [];
message = <span class="hljs-string">"Transaction failed - no logs available."</span>;
message = <span class="hljs-string">"test failed - exception thrown"</span>;
} <span class="hljs-keyword">else</span> {
failed = failed || <span class="hljs-built_in">Boolean</span>(err);
message = err ? err : (failed ? <span class="hljs-string">"Failed!"</span> : <span class="hljs-string">"Passed!"</span>);
Expand All @@ -133,12 +185,6 @@
});
};

<span class="hljs-keyword">if</span>( contractInstance.setUp !== <span class="hljs-literal">undefined</span> ) {
contractInstance.setUp(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{}); <span class="hljs-comment">// No-op function is</span></div></div></div><div class="segment"><div class="comments "><div class="wrapper"><p>a workaround for a
bug that needs to
be properly handled.
(TODO)</p></div></div><div class="code"><div class="wrapper"> }

contractInstance[testFunction]({
from: that.web3.eth.defaultAccount,
gas: DEFAULT_GAS
Expand Down
50 changes: 26 additions & 24 deletions lib/streams/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,53 @@ module.exports = function (opts) {
return through.obj(function (file, enc, cb) {
var that = this;
var classes = JSON.parse(String(file.contents));

// Skip if Test contract isn't found
if( !("Test" in classes) ) return cb()

// Load the Test contract
try {
var testContract = new Contract(classes['Test']);
} catch (err) {
return cb(err);
}


var web3;
if (opts.web3 == 'internal') {
web3 = Web3Factory.EVM();
} else {
try {
web3 = Web3Factory.JSONRPC(opts.web3);
} catch (e) {
this.push(new File({
path: "JSON-RPC Connection/Can't connect.stderr",
contents: new Buffer(String(e))
}));
cb();
return;
}
}

for (var className in classes) {

// Filter classNames if a filter is present if a filter is present
if ( opts.nameFilter && !opts.nameFilter.test( className ) ) {
continue;
}

try {
var contract = new Contract(classes[className]);
} catch(err) {
return cb(err);
}

// way to determine if the class is a test,
// iff it has implemented the Test interface
if (_.intersection( contract.signatures, testContract.signatures ).length != testContract.signatures.length) {
if (_.intersection(contract.signatures, testContract.signatures)
.length != testContract.signatures.length) {
continue;
}

var web3;
if (opts.web3 == 'internal') {
web3 = Web3Factory.EVM();
} else {
try {
web3 = Web3Factory.JSONRPC(opts.web3);
} catch (e) {
this.push(new File({
path: "JSON-RPC Connection/Can't connect.stderr",
contents: new Buffer(String(e))
}));
cb();
return;
}
}

// **TODO**: Run all tests in chain forks at the same height.
var remaining = -1;
var logTranslator = new LogTranslator(contract.abi);
Expand All @@ -75,7 +76,7 @@ module.exports = function (opts) {
var errored = false;

var logTestResult = function(err, result) {
if (errored) return;
if (errored || that.isPaused()) return;

if (err) {
that.push(new File({
Expand Down Expand Up @@ -118,6 +119,7 @@ module.exports = function (opts) {
contents: new Buffer(output)
});
that.push(file);

remaining = remaining - 1;
errored = result.failed;
};
Expand Down
72 changes: 58 additions & 14 deletions lib/vmtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,67 @@ module.exports = class VMTest {

var contractClass = that.web3.eth.contract(that.contract.abi);

that.deploy(function (err, receipt) {
var runTestOn = function (contract) {
that.runInstanceTestByName(contract, that.tests[testIndex], cb);
};

var setUpHandlerFor = function (contract) {
return function (err, txHash) {
if (err) {
return cb(err);
}

that.web3.eth.getTransactionReceipt(txHash, function(err, receipt) {
if( receipt === null && err === null) {
err = "setUp failed - exception thrown";
}

if (err) {
var testResult = {
title: "setUp failed",
message: err,
logs: [],
failed: true
};
cb(err, testResult);
return;
}

runTestOn(contract);
});
};
};

var getCodeHandlerFor = function(address) {
return function (err, code) {
if (err) {
return cb(err);
}

if (code === '0x') {
return cb("Contract failed to deploy.");
}

var contract = contractClass.at(address);

if( contract.setUp !== undefined ) {
contract.setUp(setUpHandlerFor(contract));
}
else {
runTestOn(contract);
}
};
};

var deployHandler = function (err, receipt) {
if (err) {
return cb(err);
}
that.web3.eth.getCode(receipt.contractAddress,
getCodeHandlerFor(receipt.contractAddress));
};

that.runInstanceTestByName(
contractClass.at(receipt.contractAddress),
that.tests[testIndex], cb);
});
that.deploy(deployHandler);
}

testCount() {
Expand Down Expand Up @@ -126,7 +178,7 @@ module.exports = class VMTest {
if( receipt == null ) {
failed = true;
logs = [];
message = "Transaction failed - no logs available.";
message = "test failed - exception thrown";
} else {
failed = failed || Boolean(err);
message = err ? err : (failed ? "Failed!" : "Passed!");
Expand All @@ -148,14 +200,6 @@ module.exports = class VMTest {
});
};

if( contractInstance.setUp !== undefined ) {
contractInstance.setUp(function () {}); // No-op function is
// a workaround for a
// bug that needs to
// be properly handled.
// (TODO)
}

contractInstance[testFunction]({
from: that.web3.eth.defaultAccount,
gas: DEFAULT_GAS
Expand Down

0 comments on commit fbafd4c

Please sign in to comment.