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

Commit

Permalink
Improve error handling for multiple requests. Add tests for error cas…
Browse files Browse the repository at this point in the history
…es. Fixes #4
  • Loading branch information
tcoulter committed Aug 17, 2017
1 parent a804547 commit 888a290
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 10 deletions.
29 changes: 19 additions & 10 deletions lib/subproviders/geth_api_double.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ GethApiDouble.prototype.eth_getBlockByHash = function(tx_hash, include_full_tran

GethApiDouble.prototype.eth_getBlockTransactionCountByNumber = function(block_number, callback) {
this.state.blockchain.getBlock(block_number, function(err, block) {
if (err) {
if (err.message.indexOf("index out of range")) {
// block doesn't exist
return callback(null, 0);
} else {
return callback(err);
}
}
callback(null, block.transactions.length);
});
}
Expand Down Expand Up @@ -214,7 +222,14 @@ GethApiDouble.prototype.eth_getTransactionByBlockHashAndIndex = function(hash_or
index = to.number(index);

this.state.getBlock(hash_or_number, function(err, block) {
if (err) return callback(err);
if (err) {
// block doesn't exist by that hash
if (err.notFound) {
return callback(null, null);
} else {
return callback(err);
}
}

if (index >= block.transactions.length) {
return callback(new Error("Transaction at index " + to.hex(index) + " does not exist in block."));
Expand All @@ -231,8 +246,6 @@ GethApiDouble.prototype.eth_getTransactionByBlockNumberAndIndex = function(hash_
this.eth_getTransactionByBlockHashAndIndex(hash_or_number, index, callback);
};



GethApiDouble.prototype.eth_getTransactionCount = function(address, block_number, callback) {
this.state.getTransactionCount(address, block_number, callback);
}
Expand Down Expand Up @@ -377,9 +390,8 @@ GethApiDouble.prototype.personal_sendTransaction = function(tx_data, password, c

var self = this;
self.personal_unlockAccount(from, password, null, function(err) {
if (err) {
return callback(err)
}
if (err) return callback(err);

self.state.queueTransaction("eth_sendTransaction", tx_data, function(err, ret) {
self.state.unlocked_accounts[from] = false;
callback(err, ret);
Expand All @@ -402,10 +414,7 @@ GethApiDouble.prototype.evm_increaseTime = function(seconds, callback) {
};

GethApiDouble.prototype.evm_mine = function(callback) {
this.state.blockchain.processNextBlock(function(err) {
// Remove the VM result objects from the return value.
callback(err);
});
this.state.blockchain.processNextBlock(callback);
};

GethApiDouble.prototype.debug_traceTransaction = function(tx_hash, params, callback) {
Expand Down
23 changes: 23 additions & 0 deletions test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,29 @@ var tests = function(web3, EventTest) {
done();
});
});

it("will return an empty array if logs are requested when fromBlock doesn't exist", function(done) {
var event = instance.ExampleEvent({}, {fromBlock: 100000});

// fromBlock doesn't exist, hence no logs.
event.get(function(err, logs) {
if (err) return done(err);
assert(logs.length == 0);
done();
});
});

it("will return an empty array if logs are requested when toBlock doesn't exist", function(done) {
var expected_value = 6;
var event = instance.ExampleEvent({}, {toBlock: 100000});

// fromBlock doesn't exist, hence no logs.
event.get(function(err, logs) {
if (err) return done(err);
assert(logs.length == 0);
done();
});
});
})
};

Expand Down
30 changes: 30 additions & 0 deletions test/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,22 @@ var tests = function(web3) {
web3.eth.getBlock("latest", true, function(err, block) {
if (err) return done(err);
web3.eth.getBlockTransactionCount(block.number , function(err, blockTransactionCount) {
if (err) return done(err);
assert.equal(block.transactions.length, blockTransactionCount, "Block transaction count should be 1.");
assert.equal(1, blockTransactionCount, "Block transaction count should be 1.");
done();
});
});
});
});

it("should return 0 transactions when the block doesn't exist", function(done) {
web3.eth.getBlockTransactionCount(1000000, function(err, blockTransactionCount) {
if (err) return done(err);
assert.equal(0, blockTransactionCount, "Block transaction count should be 0.");
done();
});
});
});

// Dependent upon validity of eth_getBlockTransactionCountByNumber
Expand Down Expand Up @@ -757,6 +766,16 @@ var tests = function(web3) {
});
});

it("should return null if transaction doesn't exist (eth_getTransactionByHash)", function(done) {
web3.eth.getTransaction("0x401b8ebb563ec9425b052aba8896cb74e07635563111b5a0663289d1baa8eb12", function(err, result) {
if (err) return done(err);

assert.equal(result, null, "Receipt should be null");

done();
});
});

it("should verify there's code at the address (eth_getCode)", function(done) {
web3.eth.getCode(contractAddress, function(err, result) {
if (err) return done(err);
Expand All @@ -782,6 +801,17 @@ var tests = function(web3) {
});
});

it("should return null if block doesn't exist (eth_getTransactionByBlockHashAndIndex)", function(done) {
var badBlockHash = "0xaaaaaaeb03ec5e3c000d150df2c9e7ffc31e728d12aaaedc5f6cccaca5aaaaaa";
web3.eth.getTransactionFromBlock(badBlockHash, 0, function(err, result) {
if (err) return done(err);

assert.equal(result, null);

done();
});
});

it("should be able to get the transaction from the block (eth_getTransactionByBlockNumberAndIndex)", function(done) {
web3.eth.getTransactionFromBlock(blockNumber, 0, function(err, result) {
if (err) return done(err);
Expand Down

0 comments on commit 888a290

Please sign in to comment.