Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Add test to ensure transaction hash is output on runtime errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoulter committed Aug 23, 2017
1 parent b8fa9ba commit 9216c29
Showing 1 changed file with 30 additions and 9 deletions.
39 changes: 30 additions & 9 deletions test/runtime_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var TestRPC = require("../index.js");
var fs = require("fs");
var path = require("path");
var solc = require("solc");
var RuntimeError = require("../lib/utils/runtimeerror");

// Thanks solc. At least this works!
// This removes solc's overzealous uncaughtException event handler.
Expand All @@ -12,9 +13,9 @@ process.removeAllListeners("uncaughtException");
describe("Runtime Errors", function() {
var web3 = new Web3(TestRPC.provider());
var accounts;
var RuntimeErrorContract;
var RuntimeError;
var source = fs.readFileSync(path.join(__dirname, "RuntimeError.sol"), "utf8");
var ErrorContract;
var errorInstance;
var code;

before("get accounts", function(done) {
web3.eth.getAccounts(function(err, accs) {
Expand All @@ -26,31 +27,51 @@ describe("Runtime Errors", function() {

before("compile source", function(done) {
this.timeout(10000);

var source = fs.readFileSync(path.join(__dirname, "RuntimeError.sol"), "utf8");
var result = solc.compile({sources: {"RuntimeError.sol": source}}, 1);

var code = "0x" + result.contracts.RuntimeError.bytecode;
code = "0x" + result.contracts.RuntimeError.bytecode;
var abi = JSON.parse(result.contracts.RuntimeError.interface);

RuntimeErrorContract = web3.eth.contract(abi);
RuntimeErrorContract._code = code;
RuntimeErrorContract.new({data: code, from: accounts[0], gas: 3141592}, function(err, instance) {
ErrorContract = web3.eth.contract(abi);
ErrorContract._code = code;
ErrorContract.new({data: code, from: accounts[0], gas: 3141592}, function(err, instance) {
if (err) return done(err);
if (!instance.address) return;

RuntimeError = instance;
errorInstance = instance;

done();
});
});

it("should output instruction index on runtime errors", function(done) {
// This should execute immediately.
RuntimeError.error({from: accounts[0], gas: 3141592}, function(err) {
errorInstance.error({from: accounts[0], gas: 3141592}, function(err) {
assert(err.hashes.length > 0);
assert(Object.keys(err.results).length > 0);
assert.equal(err.results[err.hashes[0]].program_counter, 44); // magic number, will change if compiler changes.
done();
});
});

it("should output the transaction hash even if a runtime error occurs", function(done) {
// we can't use `web3.eth.sendTransaction` because it will obfuscate the result
web3.currentProvider.sendAsync({
jsonrpc: "2.0",
method: "eth_sendTransaction",
params: [{
from: accounts[0],
data: code
}],
id: 1
}, function(err, result) {
assert(err != null);
assert(err instanceof RuntimeError);
assert.equal(result.result.length, 66); // transaction hash
done();
});
});

})

0 comments on commit 9216c29

Please sign in to comment.