Skip to content

Commit

Permalink
fixed provider propagation fixes #958 #966
Browse files Browse the repository at this point in the history
  • Loading branch information
frozeman committed Aug 3, 2017
1 parent 2d63f61 commit aa31def
Show file tree
Hide file tree
Showing 14 changed files with 344 additions and 99 deletions.
9 changes: 5 additions & 4 deletions packages/web3-bzz/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ var Bzz = function Bzz(provider) {

this.givenProvider = Bzz.givenProvider;

// if new Web3(provider), use the given provider
if (provider && provider._requestManager) {
provider = provider.currentProvider;
}
Expand All @@ -47,7 +46,7 @@ var Bzz = function Bzz(provider) {
/* jshint ignore:start */
Bzz.givenProvider = null;
if(typeof ethereumProvider !== 'undefined' && ethereumProvider.bzz) {
Bzz.givenProvider = ethereumProvider;
Bzz.givenProvider = ethereumProvider.bzz;
}
/* jshint ignore:end */

Expand All @@ -56,14 +55,16 @@ Bzz.prototype.setProvider = function(provider) {
if(_.isObject(provider) && _.isString(provider.bzz)) {
provider = provider.bzz;
// is no string, set default
} else if(!_.isString(provider)) {
provider = 'http://swarm-gateways.net'; // default to gateway
}
// else if(!_.isString(provider)) {
// provider = 'http://swarm-gateways.net'; // default to gateway
// }


if(_.isString(provider)) {
this.currentProvider = provider;
} else {
this.currentProvider = null;
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/web3-core-requestmanager/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "web3-requestmanager",
"name": "web3-core-requestmanager",
"version": "1.0.0-beta.14",
"description": "Web3 module to handle requests to external providers.",
"repository": "https://github.com/ethereum/web3.js/tree/master/packages/web3-requestmanager",
"repository": "https://github.com/ethereum/web3.js/tree/master/packages/web3-core-requestmanager",
"license": "LGPL-3.0",
"main": "src/index.js",
"dependencies": {
Expand Down
108 changes: 55 additions & 53 deletions packages/web3-core-requestmanager/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,61 @@ RequestManager.providers = {
};



/**
* Should be used to set provider of request manager
*
* @method setProvider
* @param {Object} p
*/
RequestManager.prototype.setProvider = function (p, net) {
var _this = this;

// autodetect provider
if(p && typeof p === 'string' && this.providers) {

// HTTP
if(/^http:\/\//i.test(p)) {
p = new this.providers.HttpProvider(p);

// WS
} else if(/^ws:\/\//i.test(p)) {
p = new this.providers.WebsocketProvider(p);

// IPC
} else if(p && typeof net === 'object' && typeof net.connect === 'function') {
p = new this.providers.IpcProvider(p, net);

} else if(p) {
throw new Error('Can\'t autodetect provider for "'+ p +'"');
}
}

// reset the old one before changing
if(this.provider)
this.clearSubscriptions();


this.provider = p || null;

// listen to incoming notifications
if(this.provider && this.provider.on) {
this.provider.on('data', function requestManagerNotification(err, result){
if(!err) {
if(_this.subscriptions[result.params.subscription] && _this.subscriptions[result.params.subscription].callback)
_this.subscriptions[result.params.subscription].callback(null, result.params.result);
} else {

Object.keys(_this.subscriptions).forEach(function(id){
if(_this.subscriptions[id].callback)
_this.subscriptions[id].callback(err);
});
}
});
}
};


/**
* Should be used to asynchronously send request
*
Expand Down Expand Up @@ -161,59 +216,6 @@ RequestManager.prototype.removeSubscription = function (id, callback) {
}
};

/**
* Should be used to set provider of request manager
*
* @method setProvider
* @param {Object}
*/
RequestManager.prototype.setProvider = function (p, net) {
var _this = this;

// autodetect provider
if(p && typeof p === 'string' && this.providers) {

// HTTP
if(/^http:\/\//i.test(p)) {
p = new this.providers.HttpProvider(p);

// WS
} else if(/^ws:\/\//i.test(p)) {
p = new this.providers.WebsocketProvider(p);

// IPC
} else if(p && typeof net === 'object' && typeof net.connect === 'function') {
p = new this.providers.IpcProvider(p, net);

} else if(p) {
throw new Error('Can\'t autodetect provider for "'+ p +'"');
}
}

// reset the old one before changing
if(this.provider)
this.clearSubscriptions();


this.provider = p;

// listen to incoming notifications
if(this.provider && this.provider.on) {
this.provider.on('data', function requestManagerNotification(err, result){
if(!err) {
if(_this.subscriptions[result.params.subscription] && _this.subscriptions[result.params.subscription].callback)
_this.subscriptions[result.params.subscription].callback(null, result.params.result);
} else {

Object.keys(_this.subscriptions).forEach(function(id){
if(_this.subscriptions[id].callback)
_this.subscriptions[id].callback(err);
});
}
});
}
};

/**
* Should be called to reset the subscriptions
*
Expand Down
13 changes: 10 additions & 3 deletions packages/web3-core-subscriptions/src/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,18 @@ Subscription.prototype.subscribe = function() {
return this;
}

if(!this.options.requestManager.provider) {
var err1 = new Error('No provider set.');
this.callback(err1, null, this);
this.emit('error', err1);
return this;
}

// throw error, if provider doesnt support subscriptions
if(!this.options.requestManager.provider.on) {
var err = new Error('The current provider doesn\'t support subscriptions'+ this.options.requestManager.provider.constructor.name);
this.callback(err, null, this);
this.emit('error', err);
var err2 = new Error('The current provider doesn\'t support subscriptions'+ this.options.requestManager.provider.constructor.name);
this.callback(err2, null, this);
this.emit('error', err2);
return this;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/web3-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"dependencies": {
"web3-core-helpers": "^1.0.0-beta.14",
"web3-core-method": "^1.0.0-beta.14",
"web3-requestmanager": "^1.0.0-beta.14",
"web3-core-requestmanager": "^1.0.0-beta.14",
"web3-utils": "^1.0.0-beta.14"
}
}
28 changes: 14 additions & 14 deletions packages/web3-core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"use strict";


var requestManager = require('web3-requestmanager');
var requestManager = require('web3-core-requestmanager');
var extend = require('./extend.js');

module.exports = {
Expand All @@ -34,19 +34,17 @@ module.exports = {
throw new Error('You need to instantiate using the "new" keyword.');
}

// if (!args[0]) {
// throw new Error('You must pass in a provider as argument!');
// }

// make write only property of pkg.provider
// make property of pkg._provider, which can properly set providers
Object.defineProperty(pkg, 'currentProvider', {
get: function () {
return pkg._provider;
},
set: function () {
return pkg._provider;
set: function (value) {
return pkg.setProvider(value);
},
enumerable: true
enumerable: true,
configurable: true
});

// inherit from web3 umbrella package
Expand All @@ -65,12 +63,14 @@ module.exports = {

pkg._provider = pkg._requestManager.provider;

// add SETPROVIDER function
pkg.setProvider = function (provider, net) {
pkg._requestManager.setProvider(provider, net);
pkg._provider = pkg._requestManager.provider;
return true;
};
// add SETPROVIDER function (don't overwrite if already existing)
if (!pkg.setProvider) {
pkg.setProvider = function (provider, net) {
pkg._requestManager.setProvider(provider, net);
pkg._provider = pkg._requestManager.provider;
return true;
};
}

// attach batch request creation
pkg.BatchRequest = requestManager.BatchManager.bind(null, pkg._requestManager);
Expand Down
4 changes: 3 additions & 1 deletion packages/web3-eth-contract/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ var Contract = function Contract(jsonInterface, address, options) {
};

Contract.setProvider = function(provider, accounts) {
Contract.currentProvider = provider;
// Contract.currentProvider = provider;
core.packageInit(Contract, [provider]);

Contract._ethAccounts = accounts;
};

Expand Down
12 changes: 11 additions & 1 deletion packages/web3-eth/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ var Eth = function Eth() {
// sets _requestmanager
core.packageInit(this, arguments);

// overwrite setProvider
var setProvider = this.setProvider;
this.setProvider = function () {
setProvider.apply(_this, arguments);
_this.net.setProvider.apply(_this, arguments);
_this.personal.setProvider.apply(_this, arguments);
_this.accounts.setProvider.apply(_this, arguments);
_this.Contract.setProvider(_this.currentProvider, _this.accounts);
};

this.clearSubscriptions = _this._requestManager.clearSubscriptions;

// add net
Expand All @@ -85,7 +95,7 @@ var Eth = function Eth() {

// add contract
this.Contract = Contract;
this.Contract.setProvider(this.currentProvider, _this.accounts);
this.Contract.setProvider(this.currentProvider, this.accounts);

// add IBAN
this.Iban = Iban;
Expand Down
7 changes: 7 additions & 0 deletions packages/web3-shh/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ var Shh = function Shh() {
// sets _requestmanager
core.packageInit(this, arguments);

// overwrite setProvider
var setProvider = this.setProvider;
this.setProvider = function () {
setProvider.apply(_this, arguments);
_this.net.setProvider.apply(_this, arguments);
};

this.clearSubscriptions = _this._requestManager.clearSubscriptions;

methods().forEach(function(method) {
Expand Down
10 changes: 4 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,28 @@ var utils = require('../packages/web3-utils');


var Web3 = function Web3() {
var _this = this;

// sets _requestmanager etc
core.packageInit(this, arguments);

this.version = version.version;

this.utils = utils;

this.eth = new Eth(this);
this.shh = new Shh(this);
this.bzz = new Bzz(this);

this.utils = utils;

// overwrite package setProvider
var setProvider = this.setProvider;
this.setProvider = function (provider, net) {
this._requestManager.setProvider(provider, net);
this._provider = this._requestManager.provider;
setProvider.apply(_this, arguments);

this.eth.setProvider(provider, net);
this.eth.net.setProvider(provider, net);
this.eth.personal.setProvider(provider, net);

this.shh.setProvider(provider, net);
this.shh.net.setProvider(provider, net);

this.bzz.setProvider(provider);

Expand Down
16 changes: 12 additions & 4 deletions test/1_givenProvider-ethereumProvider.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
var chai = require('chai');
var assert = chai.assert;

global.ethereumProvider = {test: 'ethereumProvider'};
global.ethereumProvider = {bzz: 'http://givenProvider:8500'};


describe('Web3.providers.givenProvide', function () {
describe('Web3.providers.givenProvider', function () {
describe('should be set if ethereumProvider is available ', function () {

it('when instantiating Web3', function () {

var Web3 = require('../src/index.js');

assert.deepEqual(Web3.givenProvider, {test: 'ethereumProvider'});
assert.deepEqual(Web3.givenProvider, global.ethereumProvider);

});

it('when instantiating Eth', function () {

var Eth = require('../packages/web3-eth');

assert.deepEqual(Eth.givenProvider, {test: 'ethereumProvider'});
assert.deepEqual(Eth.givenProvider, global.ethereumProvider);

});

it('when instantiating Bzz', function () {

var Bzz = require('../packages/web3-bzz');

assert.deepEqual(Bzz.givenProvider, global.ethereumProvider.bzz);

});

Expand Down
Loading

0 comments on commit aa31def

Please sign in to comment.