Skip to content

Commit

Permalink
Get first fallback scenario on getCode working with passing test.
Browse files Browse the repository at this point in the history
  • Loading branch information
John McDowall committed Jul 13, 2016
1 parent ec999b5 commit b194ba9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
node_modules
TODO
*.log
.eslintrc.js
.tern-project
23 changes: 12 additions & 11 deletions lib/blockchain_double.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,27 +247,28 @@ BlockchainDouble.prototype.getTransactionCount = function(address, callback) {
}

BlockchainDouble.prototype.getCode = function(address, callback) {
address = new Buffer(utils.stripHexPrefix(address), "hex");

if(this.fallbackEnabled) {
console.log('`getCode` called for ' + address + ": falling back to " + this.fallbackAddress);
this.vm.stateManager.getContractCode(address, function(err, result) {
if (err != null) {
console.log(err);
callback(err);
} else {
callback(null, to.hex(result));
}
});
return;

if(this.fallbackEnabled) {
var web3Fallback = new Web3();
web3Fallback.setProvider(new Web3.providers.HttpProvider(this.falbackAddress));
web3Fallback.setProvider(new Web3.providers.HttpProvider(this.fallbackAddress));
web3Fallback.eth.getCode(address, function(err, result) {
if(err) callback(err);
callback(null, result);
});
return;
}

address = new Buffer(utils.stripHexPrefix(address), "hex");
this.vm.stateManager.getContractCode(address, function(err, result) {
if (err != null) {
callback(err);
} else {
callback(null, to.hex(result));
}
});
}

BlockchainDouble.prototype.getBlockByNumber = function(number) {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"yargs": "^3.29.0"
},
"devDependencies": {
"eslint": "^3.0.1",
"eslint-config-standard": "^5.3.5",
"eslint-plugin-standard": "^1.3.3",
"mocha": "^2.2.5",
"solc": "^0.3.0-1"
},
Expand Down
7 changes: 3 additions & 4 deletions test/contract_fallback.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var fs = require("fs");
var solc = require("solc");

var logger = {
log: function() {}
log: function(msg) { /*noop*/ }
};

var source = fs.readFileSync("./test/Example.sol", {encoding: "utf8"});
Expand Down Expand Up @@ -50,7 +50,6 @@ describe("Contract Fallback", function() {
server.listen(21345, function() {
web3.setProvider(new Web3.providers.HttpProvider(fallbackTargetUrl));


// Deploy the test contract into the fallback testrpc
web3.eth.getAccounts(function(err, accounts) {
if (err) return done(err);
Expand All @@ -74,12 +73,12 @@ describe("Contract Fallback", function() {
it("should fetch a contract from the fallback when called and not present in the testrpc", function(done) {
var web3 = new Web3();
var server = TestRPC.server({fallback: fallbackTargetUrl, logger: logger});
var port = 12345;
var port = 21346;

server.listen(port, function() {
web3.setProvider(new Web3.providers.HttpProvider("http://localhost:" + port));
web3.eth.getCode(contractAddress, function(err, result) {
if (err) return done(err);
console.log(result);
assert.notEqual(result, null);
assert.notEqual(result, "0x");
done();
Expand Down

0 comments on commit b194ba9

Please sign in to comment.