diff options
-rw-r--r-- | packages/subproviders/CHANGELOG.json | 10 | ||||
-rw-r--r-- | packages/subproviders/src/index.ts | 2 | ||||
-rw-r--r-- | packages/subproviders/src/subproviders/redundant_subprovider.ts (renamed from packages/subproviders/src/subproviders/redundant_rpc.ts) | 25 | ||||
-rw-r--r-- | packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts | 17 | ||||
-rw-r--r-- | packages/website/ts/blockchain.ts | 25 |
5 files changed, 49 insertions, 30 deletions
diff --git a/packages/subproviders/CHANGELOG.json b/packages/subproviders/CHANGELOG.json index 54eb32378..ca3889202 100644 --- a/packages/subproviders/CHANGELOG.json +++ b/packages/subproviders/CHANGELOG.json @@ -1,5 +1,15 @@ [ { + "version": "0.9.0", + "changes": [ + { + "note": + "Refactor RedundantRPCSubprovider into RedundantSubprovider where it now accepts an array of subproviders rather then an array of RPC endpoints", + "pr": 500 + } + ] + }, + { "timestamp": 1522673609, "version": "0.8.4", "changes": [ diff --git a/packages/subproviders/src/index.ts b/packages/subproviders/src/index.ts index 9786347e6..1711387f1 100644 --- a/packages/subproviders/src/index.ts +++ b/packages/subproviders/src/index.ts @@ -7,7 +7,7 @@ import { LedgerEthereumClient } from './types'; export { EmptyWalletSubprovider } from './subproviders/empty_wallet_subprovider'; export { FakeGasEstimateSubprovider } from './subproviders/fake_gas_estimate_subprovider'; export { InjectedWeb3Subprovider } from './subproviders/injected_web3'; -export { RedundantRPCSubprovider } from './subproviders/redundant_rpc'; +export { RedundantSubprovider } from './subproviders/redundant_subprovider'; export { LedgerSubprovider } from './subproviders/ledger'; export { GanacheSubprovider } from './subproviders/ganache'; export { Subprovider } from './subproviders/subprovider'; diff --git a/packages/subproviders/src/subproviders/redundant_rpc.ts b/packages/subproviders/src/subproviders/redundant_subprovider.ts index f8ff0915d..37c8bba5a 100644 --- a/packages/subproviders/src/subproviders/redundant_rpc.ts +++ b/packages/subproviders/src/subproviders/redundant_subprovider.ts @@ -1,7 +1,6 @@ import { JSONRPCRequestPayload } from '@0xproject/types'; import { promisify } from '@0xproject/utils'; import * as _ from 'lodash'; -import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); import { Callback } from '../types'; @@ -12,17 +11,17 @@ import { Subprovider } from './subprovider'; * It attempts to handle each JSON RPC request by sequentially attempting to receive a valid response from one of a * set of JSON RPC endpoints. */ -export class RedundantRPCSubprovider extends Subprovider { - private _rpcs: RpcSubprovider[]; +export class RedundantSubprovider extends Subprovider { + private _subproviders: Subprovider[]; private static async _firstSuccessAsync( - rpcs: RpcSubprovider[], + subproviders: Subprovider[], payload: JSONRPCRequestPayload, next: Callback, ): Promise<any> { let lastErr: Error | undefined; - for (const rpc of rpcs) { + for (const subprovider of subproviders) { try { - const data = await promisify(rpc.handleRequest.bind(rpc))(payload, next); + const data = await promisify(subprovider.handleRequest.bind(subprovider))(payload, next); return data; } catch (err) { lastErr = err; @@ -34,16 +33,12 @@ export class RedundantRPCSubprovider extends Subprovider { } } /** - * Instantiates a new RedundantRPCSubprovider + * Instantiates a new RedundantSubprovider * @param endpoints JSON RPC endpoints to attempt. Attempts are made in the order of the endpoints. */ - constructor(endpoints: string[]) { + constructor(subproviders: Subprovider[]) { super(); - this._rpcs = _.map(endpoints, endpoint => { - return new RpcSubprovider({ - rpcUrl: endpoint, - }); - }); + this._subproviders = subproviders; } /** * This method conforms to the web3-provider-engine interface. @@ -59,9 +54,9 @@ export class RedundantRPCSubprovider extends Subprovider { next: Callback, end: (err: Error | null, data?: any) => void, ): Promise<void> { - const rpcsCopy = this._rpcs.slice(); + const subprovidersCopy = this._subproviders.slice(); try { - const data = await RedundantRPCSubprovider._firstSuccessAsync(rpcsCopy, payload, next); + const data = await RedundantSubprovider._firstSuccessAsync(subprovidersCopy, payload, next); end(null, data); } catch (err) { end(err); diff --git a/packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts b/packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts index e7ca6d496..e25cb7eb7 100644 --- a/packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts +++ b/packages/subproviders/test/unit/redundant_rpc_subprovider_test.ts @@ -5,7 +5,8 @@ import Web3 = require('web3'); import Web3ProviderEngine = require('web3-provider-engine'); import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); -import { RedundantRPCSubprovider } from '../../src'; +import { RedundantSubprovider } from '../../src'; +import { Subprovider } from '../../src/subproviders/subprovider'; import { DoneCallback } from '../../src/types'; import { chaiSetup } from '../chai_setup'; import { ganacheSubprovider } from '../utils/ganache_subprovider'; @@ -14,14 +15,12 @@ import { reportCallbackErrors } from '../utils/report_callback_errors'; const expect = chai.expect; chaiSetup.configure(); -describe('RedundantRpcSubprovider', () => { +describe('RedundantSubprovider', () => { let provider: Web3ProviderEngine; it('succeeds when supplied a healthy endpoint', (done: DoneCallback) => { provider = new Web3ProviderEngine(); - const endpoints = ['http://localhost:8545']; - const redundantSubprovider = new RedundantRPCSubprovider(endpoints); - // Hack: Hot-swap rpc with ganacheSubprovider - (redundantSubprovider as any)._rpcs = [ganacheSubprovider]; + const subproviders = [ganacheSubprovider]; + const redundantSubprovider = new RedundantSubprovider(subproviders); provider.addProvider(redundantSubprovider); provider.start(); @@ -40,13 +39,11 @@ describe('RedundantRpcSubprovider', () => { }); it('succeeds when supplied at least one healthy endpoint', (done: DoneCallback) => { provider = new Web3ProviderEngine(); - const endpoints = ['http://does-not-exist:3000', 'http://localhost:8545']; - const redundantSubprovider = new RedundantRPCSubprovider(endpoints); - // Hack: Hot-swap rpcs with [nonExistentSubprovider, ganacheSubprovider] const nonExistentSubprovider = new RpcSubprovider({ rpcUrl: 'http://does-not-exist:3000', }); - (redundantSubprovider as any)._rpcs = [nonExistentSubprovider, ganacheSubprovider]; + const subproviders = [nonExistentSubprovider as Subprovider, ganacheSubprovider]; + const redundantSubprovider = new RedundantSubprovider(subproviders); provider.addProvider(redundantSubprovider); provider.start(); diff --git a/packages/website/ts/blockchain.ts b/packages/website/ts/blockchain.ts index fd34ab82d..7d79542a3 100644 --- a/packages/website/ts/blockchain.ts +++ b/packages/website/ts/blockchain.ts @@ -21,7 +21,8 @@ import { ledgerEthereumBrowserClientFactoryAsync, LedgerSubprovider, LedgerWalletSubprovider, - RedundantRPCSubprovider, + RedundantSubprovider, + Subprovider, } from '@0xproject/subproviders'; import { Provider } from '@0xproject/types'; import { BigNumber, intervalUtils, logUtils, promisify } from '@0xproject/utils'; @@ -54,6 +55,7 @@ import { utils } from 'ts/utils/utils'; import Web3 = require('web3'); import ProviderEngine = require('web3-provider-engine'); import FilterSubprovider = require('web3-provider-engine/subproviders/filters'); +import RpcSubprovider = require('web3-provider-engine/subproviders/rpc'); import * as MintableArtifacts from '../contracts/Mintable.json'; @@ -98,7 +100,12 @@ export class Blockchain { provider = new ProviderEngine(); provider.addProvider(new InjectedWeb3Subprovider(injectedWeb3.currentProvider)); provider.addProvider(new FilterSubprovider()); - provider.addProvider(new RedundantRPCSubprovider(publicNodeUrlsIfExistsForNetworkId)); + const rpcSubproviders = _.map(publicNodeUrlsIfExistsForNetworkId, publicNodeUrl => { + return new RpcSubprovider({ + rpcUrl: publicNodeUrl, + }); + }); + provider.addProvider(new RedundantSubprovider(rpcSubproviders as Subprovider[])); provider.start(); } else if (doesInjectedWeb3Exist) { // Since no public node for this network, all requests go to injectedWeb3 instance @@ -110,7 +117,12 @@ export class Blockchain { provider = new ProviderEngine(); provider.addProvider(new FilterSubprovider()); const networkId = configs.IS_MAINNET_ENABLED ? constants.NETWORK_ID_MAINNET : constants.NETWORK_ID_KOVAN; - provider.addProvider(new RedundantRPCSubprovider(configs.PUBLIC_NODE_URLS_BY_NETWORK_ID[networkId])); + const rpcSubproviders = _.map(configs.PUBLIC_NODE_URLS_BY_NETWORK_ID[networkId], publicNodeUrl => { + return new RpcSubprovider({ + rpcUrl: publicNodeUrl, + }); + }); + provider.addProvider(new RedundantSubprovider(rpcSubproviders as Subprovider[])); provider.start(); } @@ -201,7 +213,12 @@ export class Blockchain { this._ledgerSubprovider = new LedgerSubprovider(ledgerWalletConfigs); provider.addProvider(this._ledgerSubprovider); provider.addProvider(new FilterSubprovider()); - provider.addProvider(new RedundantRPCSubprovider(configs.PUBLIC_NODE_URLS_BY_NETWORK_ID[networkId])); + const rpcSubproviders = _.map(configs.PUBLIC_NODE_URLS_BY_NETWORK_ID[networkId], publicNodeUrl => { + return new RpcSubprovider({ + rpcUrl: publicNodeUrl, + }); + }); + provider.addProvider(new RedundantSubprovider(rpcSubproviders as Subprovider[])); provider.start(); this.networkId = networkId; this._dispatcher.updateNetworkId(this.networkId); |