Merge pull request #158 from ethereum/watchImprovement2

Looks fine
This commit is contained in:
Fabian Vogelsteller 2015-04-16 12:25:47 +02:00
commit ac696408f2
13 changed files with 2908 additions and 2760 deletions

74
dist/web3-light.js vendored
View File

@ -1621,24 +1621,24 @@ module.exports = {
* @date 2015
*/
var utils = require('../utils/utils');
module.exports = {
InvalidNumberOfParams: new Error('Invalid number of input parameters'),
InvalidProvider: new Error('Providor not set or invalid'),
InvalidNumberOfParams: function () {
return new Error('Invalid number of input parameters');
},
InvalidConnection: function (host){
return new Error('CONNECTION ERROR: Couldn\'t connect to node '+ host +', is it running?');
},
InvalidProvider: function () {
return new Error('Providor not set or invalid');
},
InvalidResponse: function (result){
var message = 'Invalid JSON RPC response';
if(utils.isObject(result) && result.error && result.error.message) {
message = result.error.message;
}
var message = !!result && !!result.error && !!result.error.message ? result.error.message : 'Invalid JSON RPC response';
return new Error(message);
}
};
},{"../utils/utils":7}],13:[function(require,module,exports){
},{}],13:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2123,6 +2123,20 @@ Filter.prototype.watch = function (callback) {
});
};
// call getFilterLogs on start
if (!utils.isString(this.options)) {
this.get(function (err, messages) {
// don't send all the responses to all the watches again... just to this one
if (err) {
callback(err);
}
messages.forEach(function (message) {
callback(null, message);
});
});
}
RequestManager.getInstance().startPolling({
method: this.implementation.poll.call,
params: [this.filterId],
@ -2135,12 +2149,24 @@ Filter.prototype.stopWatching = function () {
this.callbacks = [];
};
Filter.prototype.get = function () {
var logs = this.implementation.getLogs(this.filterId);
Filter.prototype.get = function (callback) {
var self = this;
if (utils.isFunction(callback)) {
this.implementation.getLogs(this.filterId, function(err, res){
if (err) {
callback(err);
} else {
callback(null, res.map(function (log) {
return self.formatter ? self.formatter(log) : log;
}));
}
});
} else {
var logs = this.implementation.getLogs(this.filterId);
return logs.map(function (log) {
return self.formatter ? self.formatter(log) : log;
});
}
};
module.exports = Filter;
@ -2387,6 +2413,7 @@ module.exports = {
"use strict";
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
var errors = require('./errors');
var HttpProvider = function (host) {
this.host = host || 'http://localhost:8080';
@ -2396,7 +2423,13 @@ HttpProvider.prototype.send = function (payload) {
var request = new XMLHttpRequest();
request.open('POST', this.host, false);
try {
request.send(JSON.stringify(payload));
} catch(error) {
throw errors.InvalidConnection(this.host);
}
// check request.status
// TODO: throw an error here! it cannot silently fail!!!
@ -2416,13 +2449,18 @@ HttpProvider.prototype.sendAsync = function (payload, callback) {
};
request.open('POST', this.host, true);
try {
request.send(JSON.stringify(payload));
} catch(error) {
callback(errors.InvalidConnection(this.host));
}
};
module.exports = HttpProvider;
},{"xmlhttprequest":5}],18:[function(require,module,exports){
},{"./errors":12,"xmlhttprequest":5}],18:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2584,7 +2622,7 @@ Method.prototype.extractCallback = function (args) {
*/
Method.prototype.validateArgs = function (args) {
if (args.length !== this.params) {
throw errors.InvalidNumberOfParams;
throw errors.InvalidNumberOfParams();
}
};
@ -2935,7 +2973,7 @@ RequestManager.getInstance = function () {
*/
RequestManager.prototype.send = function (data) {
if (!this.provider) {
console.error(errors.InvalidProvider);
console.error(errors.InvalidProvider());
return null;
}
@ -2958,7 +2996,7 @@ RequestManager.prototype.send = function (data) {
*/
RequestManager.prototype.sendAsync = function (data, callback) {
if (!this.provider) {
return callback(errors.InvalidProvider);
return callback(errors.InvalidProvider());
}
var payload = Jsonrpc.getInstance().toPayload(data.method, data.params);
@ -3049,7 +3087,7 @@ RequestManager.prototype.poll = function () {
}
if (!this.provider) {
console.error(errors.InvalidProvider);
console.error(errors.InvalidProvider());
return;
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

140
dist/web3.js vendored
View File

@ -1621,24 +1621,24 @@ module.exports = {
* @date 2015
*/
var utils = require('../utils/utils');
module.exports = {
InvalidNumberOfParams: new Error('Invalid number of input parameters'),
InvalidProvider: new Error('Providor not set or invalid'),
InvalidNumberOfParams: function () {
return new Error('Invalid number of input parameters');
},
InvalidConnection: function (host){
return new Error('CONNECTION ERROR: Couldn\'t connect to node '+ host +', is it running?');
},
InvalidProvider: function () {
return new Error('Providor not set or invalid');
},
InvalidResponse: function (result){
var message = 'Invalid JSON RPC response';
if(utils.isObject(result) && result.error && result.error.message) {
message = result.error.message;
}
var message = !!result && !!result.error && !!result.error.message ? result.error.message : 'Invalid JSON RPC response';
return new Error(message);
}
};
},{"../utils/utils":7}],13:[function(require,module,exports){
},{}],13:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2123,6 +2123,20 @@ Filter.prototype.watch = function (callback) {
});
};
// call getFilterLogs on start
if (!utils.isString(this.options)) {
this.get(function (err, messages) {
// don't send all the responses to all the watches again... just to this one
if (err) {
callback(err);
}
messages.forEach(function (message) {
callback(null, message);
});
});
}
RequestManager.getInstance().startPolling({
method: this.implementation.poll.call,
params: [this.filterId],
@ -2135,12 +2149,24 @@ Filter.prototype.stopWatching = function () {
this.callbacks = [];
};
Filter.prototype.get = function () {
var logs = this.implementation.getLogs(this.filterId);
Filter.prototype.get = function (callback) {
var self = this;
if (utils.isFunction(callback)) {
this.implementation.getLogs(this.filterId, function(err, res){
if (err) {
callback(err);
} else {
callback(null, res.map(function (log) {
return self.formatter ? self.formatter(log) : log;
}));
}
});
} else {
var logs = this.implementation.getLogs(this.filterId);
return logs.map(function (log) {
return self.formatter ? self.formatter(log) : log;
});
}
};
module.exports = Filter;
@ -2387,6 +2413,7 @@ module.exports = {
"use strict";
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
var errors = require('./errors');
var HttpProvider = function (host) {
this.host = host || 'http://localhost:8080';
@ -2396,7 +2423,13 @@ HttpProvider.prototype.send = function (payload) {
var request = new XMLHttpRequest();
request.open('POST', this.host, false);
try {
request.send(JSON.stringify(payload));
} catch(error) {
throw errors.InvalidConnection(this.host);
}
// check request.status
// TODO: throw an error here! it cannot silently fail!!!
@ -2416,13 +2449,18 @@ HttpProvider.prototype.sendAsync = function (payload, callback) {
};
request.open('POST', this.host, true);
try {
request.send(JSON.stringify(payload));
} catch(error) {
callback(errors.InvalidConnection(this.host));
}
};
module.exports = HttpProvider;
},{"xmlhttprequest":5}],18:[function(require,module,exports){
},{"./errors":12,"xmlhttprequest":5}],18:[function(require,module,exports){
/*
This file is part of ethereum.js.
@ -2584,7 +2622,7 @@ Method.prototype.extractCallback = function (args) {
*/
Method.prototype.validateArgs = function (args) {
if (args.length !== this.params) {
throw errors.InvalidNumberOfParams;
throw errors.InvalidNumberOfParams();
}
};
@ -2935,7 +2973,7 @@ RequestManager.getInstance = function () {
*/
RequestManager.prototype.send = function (data) {
if (!this.provider) {
console.error(errors.InvalidProvider);
console.error(errors.InvalidProvider());
return null;
}
@ -2958,7 +2996,7 @@ RequestManager.prototype.send = function (data) {
*/
RequestManager.prototype.sendAsync = function (data, callback) {
if (!this.provider) {
return callback(errors.InvalidProvider);
return callback(errors.InvalidProvider());
}
var payload = Jsonrpc.getInstance().toPayload(data.method, data.params);
@ -3049,7 +3087,7 @@ RequestManager.prototype.poll = function () {
}
if (!this.provider) {
console.error(errors.InvalidProvider);
console.error(errors.InvalidProvider());
return;
}
@ -3308,13 +3346,13 @@ module.exports = {
},{"./method":19}],27:[function(require,module,exports){
},{}],"bignumber.js":[function(require,module,exports){
/*! bignumber.js v2.0.3 https://github.com/MikeMcl/bignumber.js/LICENCE */
/*! bignumber.js v2.0.7 https://github.com/MikeMcl/bignumber.js/LICENCE */
;(function (global) {
'use strict';
/*
bignumber.js v2.0.3
bignumber.js v2.0.7
A JavaScript library for arbitrary-precision arithmetic.
https://github.com/MikeMcl/bignumber.js
Copyright (c) 2015 Michael Mclaughlin <M8ch88l@gmail.com>
@ -4146,10 +4184,12 @@ module.exports = {
i = 0;
s += 2;
// Normalise xc and yc so highest order digit of yc is >= base/2
// Normalise xc and yc so highest order digit of yc is >= base / 2.
n = mathfloor( base / ( yc[0] + 1 ) );
// Not necessary, but to handle odd bases where yc[0] == ( base / 2 ) - 1.
// if ( n > 1 || n++ == 1 && yc[0] < base / 2 ) {
if ( n > 1 ) {
yc = multiply( yc, n, base );
xc = multiply( xc, n, base );
@ -4167,6 +4207,8 @@ module.exports = {
yz.unshift(0);
yc0 = yc[0];
if ( yc[1] >= base / 2 ) yc0++;
// Not necessary, but to prevent trial digit n > base, when using base 3.
// else if ( base == 3 && yc0 == 1 ) yc0 = 1 + 1e-15;
do {
n = 0;
@ -4194,6 +4236,8 @@ module.exports = {
// 6. If remainder > divisor: remainder -= divisor, n++
if ( n > 1 ) {
// n may be > base only when base is 3.
if (n >= base) n = base - 1;
// product = divisor * trial digit.
@ -4202,58 +4246,66 @@ module.exports = {
remL = rem.length;
// Compare product and remainder.
cmp = compare( prod, rem, prodL, remL );
// product > remainder.
if ( cmp == 1 ) {
// If product > remainder.
// Trial digit n too high.
// n is 1 too high about 5% of the time, and is not known to have
// ever been more than 1 too high.
while ( compare( prod, rem, prodL, remL ) == 1 ) {
n--;
// Subtract divisor from product.
subtract( prod, yL < prodL ? yz : yc, prodL, base );
prodL = prod.length;
cmp = 1;
}
} else {
// cmp is -1.
// If n is 0, there is no need to compare yc and rem again
// below, so change cmp to 1 to avoid it.
// If n is 1, compare yc and rem again below.
if ( n == 0 ) cmp = n = 1;
prod = yc.slice();
// n is 0 or 1, cmp is -1.
// If n is 0, there is no need to compare yc and rem again below,
// so change cmp to 1 to avoid it.
// If n is 1, leave cmp as -1, so yc and rem are compared again.
if ( n == 0 ) {
// divisor < remainder, so n must be at least 1.
cmp = n = 1;
}
// product = divisor
prod = yc.slice();
prodL = prod.length;
}
if ( prodL < remL ) prod.unshift(0);
// Subtract product from remainder.
subtract( rem, prod, remL, base );
// If product was < previous remainder.
if ( cmp == -1 ) {
remL = rem.length;
// Compare divisor and new remainder.
cmp = compare( yc, rem, yL, remL );
// If product was < remainder.
if ( cmp == -1 ) {
// Compare divisor and new remainder.
// If divisor < new remainder, subtract divisor from remainder.
if ( cmp < 1 ) {
// Trial digit n too low.
// n is 1 too low about 5% of the time, and very rarely 2 too low.
while ( compare( yc, rem, yL, remL ) < 1 ) {
n++;
// Subtract divisor from remainder.
subtract( rem, yL < remL ? yz : yc, remL, base );
}
}
remL = rem.length;
}
}
} else if ( cmp === 0 ) {
n++;
rem = [0];
}
// if cmp === 1, n will be 0
} // else cmp === 1 and n will be 0
// Add the next digit, n, to the result array.
qc[i++] = n;
// Update the remainder.
if ( cmp && rem[0] ) {
if ( rem[0] ) {
rem[remL++] = xc[xi] || 0;
} else {
rem = [ xc[xi] ];
@ -4424,11 +4476,11 @@ module.exports = {
// Handle values that fail the validity test in BigNumber.
parseNumeric = (function () {
var basePrefix = /^(-?)0([xbo])(?=\w[\w.]*$)/i,
var basePrefix = /^(-?)0([xbo])/i,
dotAfter = /^([^.]+)\.$/,
dotBefore = /^\.([^.]+)$/,
isInfinityOrNaN = /^-?(Infinity|NaN)$/,
whitespaceOrPlus = /^\s*\+(?=[\w.])|^\s+|\s+$/g;
whitespaceOrPlus = /^\s*\+|^\s+|\s+$/g;
return function ( x, str, num, b ) {
var base,

16
dist/web3.js.map vendored

File diff suppressed because one or more lines are too long

5
dist/web3.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -20,18 +20,18 @@
* @date 2015
*/
var utils = require('../utils/utils');
module.exports = {
InvalidNumberOfParams: new Error('Invalid number of input parameters'),
InvalidProvider: new Error('Providor not set or invalid'),
InvalidNumberOfParams: function () {
return new Error('Invalid number of input parameters');
},
InvalidConnection: function (host){
return new Error('CONNECTION ERROR: Couldn\'t connect to node '+ host +', is it running?');
},
InvalidProvider: function () {
return new Error('Providor not set or invalid');
},
InvalidResponse: function (result){
var message = 'Invalid JSON RPC response';
if(utils.isObject(result) && result.error && result.error.message) {
message = result.error.message;
}
var message = !!result && !!result.error && !!result.error.message ? result.error.message : 'Invalid JSON RPC response';
return new Error(message);
}
};

View File

@ -86,6 +86,20 @@ Filter.prototype.watch = function (callback) {
});
};
// call getFilterLogs on start
if (!utils.isString(this.options)) {
this.get(function (err, messages) {
// don't send all the responses to all the watches again... just to this one
if (err) {
callback(err);
}
messages.forEach(function (message) {
callback(null, message);
});
});
}
RequestManager.getInstance().startPolling({
method: this.implementation.poll.call,
params: [this.filterId],
@ -98,12 +112,24 @@ Filter.prototype.stopWatching = function () {
this.callbacks = [];
};
Filter.prototype.get = function () {
var logs = this.implementation.getLogs(this.filterId);
Filter.prototype.get = function (callback) {
var self = this;
if (utils.isFunction(callback)) {
this.implementation.getLogs(this.filterId, function(err, res){
if (err) {
callback(err);
} else {
callback(null, res.map(function (log) {
return self.formatter ? self.formatter(log) : log;
}));
}
});
} else {
var logs = this.implementation.getLogs(this.filterId);
return logs.map(function (log) {
return self.formatter ? self.formatter(log) : log;
});
}
};
module.exports = Filter;

View File

@ -25,6 +25,7 @@
"use strict";
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; // jshint ignore:line
var errors = require('./errors');
var HttpProvider = function (host) {
this.host = host || 'http://localhost:8080';
@ -34,7 +35,13 @@ HttpProvider.prototype.send = function (payload) {
var request = new XMLHttpRequest();
request.open('POST', this.host, false);
try {
request.send(JSON.stringify(payload));
} catch(error) {
throw errors.InvalidConnection(this.host);
}
// check request.status
// TODO: throw an error here! it cannot silently fail!!!
@ -54,7 +61,12 @@ HttpProvider.prototype.sendAsync = function (payload, callback) {
};
request.open('POST', this.host, true);
try {
request.send(JSON.stringify(payload));
} catch(error) {
callback(errors.InvalidConnection(this.host));
}
};
module.exports = HttpProvider;

View File

@ -66,7 +66,7 @@ Method.prototype.extractCallback = function (args) {
*/
Method.prototype.validateArgs = function (args) {
if (args.length !== this.params) {
throw errors.InvalidNumberOfParams;
throw errors.InvalidNumberOfParams();
}
};

View File

@ -65,7 +65,7 @@ RequestManager.getInstance = function () {
*/
RequestManager.prototype.send = function (data) {
if (!this.provider) {
console.error(errors.InvalidProvider);
console.error(errors.InvalidProvider());
return null;
}
@ -88,7 +88,7 @@ RequestManager.prototype.send = function (data) {
*/
RequestManager.prototype.sendAsync = function (data, callback) {
if (!this.provider) {
return callback(errors.InvalidProvider);
return callback(errors.InvalidProvider());
}
var payload = Jsonrpc.getInstance().toPayload(data.method, data.params);
@ -179,7 +179,7 @@ RequestManager.prototype.poll = function () {
}
if (!this.provider) {
console.error(errors.InvalidProvider);
console.error(errors.InvalidProvider());
return;
}

View File

@ -67,7 +67,22 @@ describe('web3.eth.contract', function () {
],
address: '0x1234567890123456789012345678901234567890'
});
} else if (step === 2 && utils.isArray(payload)) {
} else if (step === 2) {
step = 3;
provider.injectResult([{
address: address,
topics: [
sha3,
'0x0000000000000000000000001234567890123456789012345678901234567890',
'0x0000000000000000000000000000000000000000000000000000000000000001'
],
number: 2,
data: '0x0000000000000000000000000000000000000000000000000000000000000001' +
'0000000000000000000000000000000000000000000000000000000000000008'
}]);
assert.equal(payload.jsonrpc, '2.0');
assert.equal(payload.method, 'eth_getFilterLogs');
} else if (step === 3 && utils.isArray(payload)) {
provider.injectBatchResults([[{
address: address,
topics: [
@ -89,12 +104,16 @@ describe('web3.eth.contract', function () {
var Contract = web3.eth.contract(desc);
var contract = new Contract(address);
var res = 0;
contract.Changed({from: address}).watch(function(err, result) {
assert.equal(result.args.from, address);
assert.equal(result.args.amount, 1);
assert.equal(result.args.t1, 1);
assert.equal(result.args.t2, 8);
res++;
if (res === 2) {
done();
}
});
});

View File

@ -39,8 +39,8 @@ describe('lib/web3/method', function () {
var test2 = function () { method.validateArgs(args2); };
// then
assert.throws(test, errors.InvalidNumberOfParams);
assert.throws(test2, errors.InvalidNumberOfParams);
assert.throws(test, errors.InvalidNumberOfParams().message);
assert.throws(test2, errors.InvalidNumberOfParams().message);
});
});
});