Skip to content

Commit

Permalink
Merge pull request #3190 from ethereum/provider-related-improvements
Browse files Browse the repository at this point in the history
Provider related improvements
  • Loading branch information
holgerd77 authored Apr 6, 2020
2 parents ad00335 + 2eb3ed1 commit cae5334
Show file tree
Hide file tree
Showing 27 changed files with 1,592 additions and 447 deletions.
72 changes: 71 additions & 1 deletion docs/include_package-core.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ setProvider
Will change the provider for its module.

.. note:: When called on the umbrella package ``web3`` it will also set the provider for all sub modules ``web3.eth``, ``web3.shh``, etc EXCEPT ``web3.bzz`` which needs a separate provider at all times.
.. note::
When called on the umbrella package ``web3`` it will also set the provider for all sub modules ``web3.eth``, ``web3.shh``, etc EXCEPT ``web3.bzz`` which needs a separate provider at all times.

----------
Parameters
Expand Down Expand Up @@ -98,6 +99,75 @@ Example
// on windows the path is: "\\\\.\\pipe\\geth.ipc"
// on linux the path is: "/users/myuser/.ethereum/geth.ipc"
-------------
Configuration
-------------

.. code-block:: javascript
// ====
// Http
// ====
var Web3HttpProvider = require('web3-providers-http');
var options = {
keepAlive: true,
withCredentials: false,
timeout: 20000, // ms
headers: [
{
name: 'Access-Control-Allow-Origin',
value: '*'
},
{
...
}
],
agent: {
http: http.Agent(...),
baseUrl: ''
}
};
var provider = new Web3HttpProvider('http://localhost:8545', options);
// ==========
// Websockets
// ==========
var Web3WsProvider = require('web3-providers-ws');
var options = {
timeout: 30000, // ms
// Useful for credentialed urls, e.g: ws://username:password@localhost:8546
headers: {
authorization: 'Basic username:password'
},
// Useful if requests result are large
clientConfig: {
maxReceivedFrameSize: 100000000, // bytes - default: 1MiB
maxReceivedMessageSize: 100000000, // bytes - default: 8MiB
},
// Enable auto reconnection
reconnect: {
auto: true,
delay: 5000, // ms
maxAttempts: 5,
onTimeout: false
}
};
var ws = new Web3WsProvider('ws://localhost:8546', options);
More information for the Http and Websocket provider modules can be found here:

- `HttpProvider <https://github.com/ethereum/web3.js/tree/1.x/packages/web3-providers-http#usage>`_
- `WebsocketProvider <https://github.com/ethereum/web3.js/tree/1.x/packages/web3-providers-ws#usage>`_

------------------------------------------------------------------------------

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"docs": "cd docs; make html;",
"lint": "jshint *.js packages",
"test": "mocha --grep E2E --invert; jshint *.js packages",
"test:unit": "nyc --no-clean --silent _mocha -- -R spec --grep E2E --invert",
"test:unit": "nyc --no-clean --silent _mocha -- -R spec --grep E2E --invert --exit",
"test:cov": "npm run cov:clean; npm run test:unit; npm run test:e2e:clients; npm run cov:html",
"dtslint": "lerna run dtslint",
"depcheck": "lerna exec dependency-check -- --missing --verbose .",
Expand Down Expand Up @@ -134,6 +134,7 @@
"karma-spec-reporter": "0.0.32",
"lerna": "^3.18.3",
"mocha": "^6.2.1",
"pify": "^4.0.1",
"nyc": "^14.1.1",
"puppeteer": "^1.20.0",
"sandboxed-module": "^2.0.3",
Expand Down
34 changes: 32 additions & 2 deletions packages/web3-core-helpers/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ module.exports = {
InvalidNumberOfParams: function (got, expected, method) {
return new Error('Invalid number of parameters for "'+ method +'". Got '+ got +' expected '+ expected +'!');
},
InvalidConnection: function (host){
return new Error('CONNECTION ERROR: Couldn\'t connect to node '+ host +'.');
InvalidConnection: function (host, event){
return this.ConnectionError('CONNECTION ERROR: Couldn\'t connect to node '+ host +'.', event);
},
InvalidProvider: function () {
return new Error('Provider not set or invalid');
Expand All @@ -44,6 +44,36 @@ module.exports = {
ConnectionTimeout: function (ms){
return new Error('CONNECTION TIMEOUT: timeout of ' + ms + ' ms achived');
},
ConnectionNotOpenError: function (event){
return this.ConnectionError('connection not open on send()', event);
},
ConnectionCloseError: function (event){
if (typeof event === 'object' && event.code && event.reason) {
return this.ConnectionError(
'CONNECTION ERROR: The connection got closed with ' +
'the close code `' + event.code + '` and the following ' +
'reason string `' + event.reason + '`',
event
);
}

return new Error('CONNECTION ERROR: The connection closed unexpectedly');
},
MaxAttemptsReachedOnReconnectingError: function (){
return new Error('Maximum number of reconnect attempts reached!');
},
PendingRequestsOnReconnectingError: function (){
return new Error('CONNECTION ERROR: Provider started to reconnect before the response got received!');
},
ConnectionError: function (msg, event){
const error = new Error(msg);
if (event) {
error.code = event.code;
error.reason = event.reason;
}

return error;
},
RevertInstructionError: function(reason, signature) {
var error = new Error('Your request got reverted with the following reason string: ' + reason);
error.reason = reason;
Expand Down
40 changes: 35 additions & 5 deletions packages/web3-core-helpers/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ export class errors {
expected: number,
method: string
): Error;
static InvalidConnection(host: string): Error;
static InvalidConnection(host: string, event?: WebSocketEvent): ConnectionError;
static InvalidProvider(): Error;
static InvalidResponse(result: Error): Error;
static ConnectionTimeout(ms: string): Error;
static ConnectionNotOpenError(): Error;
static ConnectionCloseError(event: WebSocketEvent | boolean): Error | ConnectionError;
static MaxAttemptsReachedOnReconnectingError(): Error;
static PendingRequestsOnReconnectingError(): Error;
static ConnectionError(msg: string, event?: WebSocketEvent): ConnectionError;
static RevertInstructionError(reason: string, signature: string): RevertInstructionError
static TransactionRevertInstructionError(reason: string, signature: string, receipt: object): TransactionRevertInstructionError
static TransactionError(message: string, receipt: object): TransactionError
Expand All @@ -82,13 +87,11 @@ export class WebsocketProviderBase {

isConnecting(): boolean;

responseCallbacks: any;
notificationCallbacks: any;
requestQueue: Map<string, RequestItem>;
responseQueue: Map<string, RequestItem>;
connected: boolean;
connection: any;

addDefaultEvents(): void;

supportsSubscriptions(): boolean;

send(
Expand All @@ -107,6 +110,10 @@ export class WebsocketProviderBase {
reset(): void;

disconnect(code: number, reason: string): void;

connect(): void;

reconnect(): void;
}

export class IpcProviderBase {
Expand Down Expand Up @@ -183,6 +190,19 @@ export interface WebsocketProviderOptions {
clientConfig?: string;
requestOptions?: any;
origin?: string;
reconnect?: ReconnectOptions;
}

export interface ReconnectOptions {
auto?: boolean;
delay?: number;
maxAttempts?: number;
onTimeout?: boolean;
}

export interface RequestItem {
payload: JsonRpcPayload;
callback: (error: any, result: any) => void;
}

export interface JsonRpcPayload {
Expand Down Expand Up @@ -212,3 +232,13 @@ export interface TransactionRevertInstructionError extends Error {
export interface TransactionError extends Error {
receipt: object;
}

export interface ConnectionError extends Error {
code: string | undefined;
reason: string | undefined;
}

export interface WebSocketEvent {
code?: number;
reason?: string;
}
38 changes: 36 additions & 2 deletions packages/web3-core-helpers/types/tests/errors-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
* @date 2019
*/

import { errors } from 'web3-core-helpers';
import { errors, WebSocketEvent } from 'web3-core-helpers';

// $ExpectType Error
errors.ErrorResponse(new Error('hey'));

// $ExpectType Error
errors.InvalidNumberOfParams(1, 3, 'method');

// $ExpectType Error
// $ExpectType ConnectionError
errors.InvalidConnection('https://localhost:2345432');

// $ExpectType Error
Expand All @@ -37,8 +37,42 @@ errors.InvalidResponse(new Error('hey'));
// $ExpectType Error
errors.ConnectionTimeout('timeout');

// $ExpectType Error
errors.ConnectionNotOpenError();

// $ExpectType Error
errors.MaxAttemptsReachedOnReconnectingError();

// $ExpectType Error
errors.PendingRequestsOnReconnectingError();

const event: WebSocketEvent = {code: 100, reason: 'reason'};
// $ExpectType ConnectionError
errors.ConnectionError('msg', event);

// $ExpectType Error | ConnectionError
errors.ConnectionCloseError(event);

// $ExpectType Error | ConnectionError
errors.ConnectionCloseError(true);

// $ExpectType RevertInstructionError
errors.RevertInstructionError('reason', 'signature');

// $ExpectType TransactionRevertInstructionError
errors.TransactionRevertInstructionError('reason', 'signature', {});

// $ExpectType TransactionError
errors.TransactionError('reason', {});

// $ExpectType TransactionError
errors.NoContractAddressFoundError({});

// $ExpectType TransactionError
errors.ContractCodeNotStoredError({});

// $ExpectType TransactionError
errors.TransactionRevertedWithoutReasonError({});

// $ExpectType TransactionError
errors.TransactionOutOfGasError({});
Loading

0 comments on commit cae5334

Please sign in to comment.