From 4e284f5e6cbe48b307f3d3163dd7f158c56a5e82 Mon Sep 17 00:00:00 2001 From: Jacob Evans Date: Mon, 5 Feb 2018 16:31:51 -0800 Subject: Refactor tests for reuse of the fixture subprovider --- .../test/unit/nonce_tracker_subprovider_test.ts | 156 +++++++-------------- packages/testnet-faucets/Dockerfile | 3 + packages/testnet-faucets/src/ts/global.d.ts | 9 +- 3 files changed, 61 insertions(+), 107 deletions(-) diff --git a/packages/subproviders/test/unit/nonce_tracker_subprovider_test.ts b/packages/subproviders/test/unit/nonce_tracker_subprovider_test.ts index 9bb8268cb..cffb4cdee 100644 --- a/packages/subproviders/test/unit/nonce_tracker_subprovider_test.ts +++ b/packages/subproviders/test/unit/nonce_tracker_subprovider_test.ts @@ -14,86 +14,79 @@ chaiSetup.configure(); describe('NonceTrackerSubprovider', () => { let provider: Web3ProviderEngine; + const getTransactionCountPayload = { + jsonrpc: '2.0', + method: 'eth_getTransactionCount', + params: ['0x0', 'pending'], + id: 1, + }; + const sendTransactionPayload = { + jsonrpc: '2.0', + method: 'eth_sendRawTransaction', + params: [], + id: 1, + }; + const txParams = [ + '0x', + '0x09184e72a000', + '0x2710', + '0x0000000000000000000000000000000000000000', + '0x', + '0x7f7465737432000000000000000000000000000000000000000000000000000000600057', + '0x1c', + '0x5e1d3a76fbf824220eafc8c79ad578ad2b67d01b0c2425eb1f1347e8f50882ab', + '0x5bd428537f05f9830e93792f90ea6a3e2d1ee84952dd96edbae9f658f831ab13', + ]; + function createFixtureSubprovider() { + let called = false; + const fixedBlockNumberAndTransactionCountProvider = new FixtureSubprovider({ + eth_getBlockByNumber: '0x01', + eth_getTransactionCount: (data: any, next: any, end: any) => { + if (called) { + return end(null, '0x99'); + } else { + called = true; + end(null, '0x00'); + } + }, + }); + return fixedBlockNumberAndTransactionCountProvider; + } it('successfully caches the transaction count', async () => { provider = new Web3ProviderEngine(); - let called = false; const nonceTrackerSubprovider = new NonceTrackerSubprovider(); provider.addProvider(nonceTrackerSubprovider); - provider.addProvider( - new FixtureSubprovider({ - eth_getBlockByNumber: '0x01', - eth_getTransactionCount: (data: any, next: any, end: any) => { - if (called) { - return end(null, '0x99'); - } else { - called = true; - end(null, '0x01'); - } - }, - }), - ); + provider.addProvider(createFixtureSubprovider()); provider.start(); - const payload = { - jsonrpc: '2.0', - method: 'eth_getTransactionCount', - params: ['0x0', 'pending'], - id: 1, - }; + const payload = { ...getTransactionCountPayload, params: ['0x0', 'pending'] }; const response = await promisify(provider.sendAsync, provider)(payload); - expect(response.result).to.be.eq('0x01'); + expect(response.result).to.be.eq('0x00'); const secondResponse = await promisify(provider.sendAsync, provider)(payload); - expect(secondResponse.result).to.be.eq('0x01'); + expect(secondResponse.result).to.be.eq('0x00'); }); it('does not cache the result for latest transaction count', async () => { provider = new Web3ProviderEngine(); - let called = false; const nonceTrackerSubprovider = new NonceTrackerSubprovider(); provider.addProvider(nonceTrackerSubprovider); - provider.addProvider( - new FixtureSubprovider({ - eth_getBlockByNumber: '0x01', - eth_getTransactionCount: (data: any, next: any, end: any) => { - if (called) { - return end(null, '0x99'); - } else { - called = true; - end(null, '0x01'); - } - }, - }), - ); + provider.addProvider(createFixtureSubprovider()); provider.start(); - const payload = { - jsonrpc: '2.0', - method: 'eth_getTransactionCount', - params: ['0x0', 'latest'], - id: 1, - }; + const payload = { ...getTransactionCountPayload, params: ['0x0', 'latest'] }; const response = await promisify(provider.sendAsync, provider)(payload); - expect(response.result).to.be.eq('0x01'); + expect(response.result).to.be.eq('0x00'); const secondResponse = await promisify(provider.sendAsync, provider)(payload); expect(secondResponse.result).to.be.eq('0x99'); }); it('clears the cache on a Nonce Too Low Error', async () => { provider = new Web3ProviderEngine(); - let called = false; const nonceTrackerSubprovider = new NonceTrackerSubprovider(); provider.addProvider(nonceTrackerSubprovider); + provider.addProvider(createFixtureSubprovider()); provider.addProvider( new FixtureSubprovider({ - eth_getBlockByNumber: '0x01', - eth_getTransactionCount: (data: any, next: any, end: any) => { - if (called) { - return end(null, '0x99'); - } else { - called = true; - end(null, '0x01'); - } - }, eth_sendRawTransaction: (data: any, next: any, end: any) => { end(new Error('Transaction nonce is too low')); }, @@ -102,35 +95,19 @@ describe('NonceTrackerSubprovider', () => { provider.start(); const noncePayload = { - jsonrpc: '2.0', - method: 'eth_getTransactionCount', + ...getTransactionCountPayload, params: ['0x1f36f546477cda21bf2296c50976f2740247906f', 'pending'], - id: 1, }; - const txParams = [ - '0x', - '0x09184e72a000', - '0x2710', - '0x0000000000000000000000000000000000000000', - '0x', - '0x7f7465737432000000000000000000000000000000000000000000000000000000600057', - '0x1c', - '0x5e1d3a76fbf824220eafc8c79ad578ad2b67d01b0c2425eb1f1347e8f50882ab', - '0x5bd428537f05f9830e93792f90ea6a3e2d1ee84952dd96edbae9f658f831ab13', - ]; - const transaction = new EthereumTx(txParams); const txPayload = { - jsonrpc: '2.0', - method: 'eth_sendRawTransaction', + ...sendTransactionPayload, params: [transaction.serialize()], - id: 1, }; const response = await promisify(provider.sendAsync, provider)(noncePayload); - expect(response.result).to.be.eq('0x01'); + expect(response.result).to.be.eq('0x00'); const secondResponse = await promisify(provider.sendAsync, provider)(noncePayload); - expect(secondResponse.result).to.be.eq('0x01'); + expect(secondResponse.result).to.be.eq('0x00'); try { await promisify(provider.sendAsync, provider)(txPayload); } catch (err) { @@ -138,22 +115,13 @@ describe('NonceTrackerSubprovider', () => { expect(thirdResponse.result).to.be.eq('0x99'); } }); - it('increments the used nonce', async () => { + it('increments the used nonce when a transaction successfully submits', async () => { provider = new Web3ProviderEngine(); - let called = false; const nonceTrackerSubprovider = new NonceTrackerSubprovider(); provider.addProvider(nonceTrackerSubprovider); + provider.addProvider(createFixtureSubprovider()); provider.addProvider( new FixtureSubprovider({ - eth_getBlockByNumber: '0x01', - eth_getTransactionCount: (data: any, next: any, end: any) => { - if (called) { - return end(null, '0x99'); - } else { - called = true; - end(null, '0x00'); - } - }, eth_sendRawTransaction: (data: any, next: any, end: any) => { end(null); }, @@ -162,29 +130,13 @@ describe('NonceTrackerSubprovider', () => { provider.start(); const noncePayload = { - jsonrpc: '2.0', - method: 'eth_getTransactionCount', + ...getTransactionCountPayload, params: ['0x1f36f546477cda21bf2296c50976f2740247906f', 'pending'], - id: 1, }; - const txParams = [ - '0x', - '0x09184e72a000', - '0x2710', - '0x0000000000000000000000000000000000000000', - '0x', - '0x7f7465737432000000000000000000000000000000000000000000000000000000600057', - '0x1c', - '0x5e1d3a76fbf824220eafc8c79ad578ad2b67d01b0c2425eb1f1347e8f50882ab', - '0x5bd428537f05f9830e93792f90ea6a3e2d1ee84952dd96edbae9f658f831ab13', - ]; - const transaction = new EthereumTx(txParams); const txPayload = { - jsonrpc: '2.0', - method: 'eth_sendRawTransaction', + ...sendTransactionPayload, params: [transaction.serialize()], - id: 1, }; const response = await promisify(provider.sendAsync, provider)(noncePayload); diff --git a/packages/testnet-faucets/Dockerfile b/packages/testnet-faucets/Dockerfile index 346f461d8..bd1dcc4a4 100644 --- a/packages/testnet-faucets/Dockerfile +++ b/packages/testnet-faucets/Dockerfile @@ -2,7 +2,10 @@ FROM node WORKDIR /src +# Ledger Provider (in the Subproviders package) requires node-hid at dependency install time +# which compiles and expects certain USB developer library packages to be present RUN apt-get -qq update && apt-get install -y libhidapi-dev libusb-1.0-0-dev +# Our fork of ledgerco disables requiring node-hid at run time if CIRCLECI is set to true ENV CIRCLECI=true COPY package.json . RUN npm i diff --git a/packages/testnet-faucets/src/ts/global.d.ts b/packages/testnet-faucets/src/ts/global.d.ts index b9188bbf6..3d9998698 100644 --- a/packages/testnet-faucets/src/ts/global.d.ts +++ b/packages/testnet-faucets/src/ts/global.d.ts @@ -1,4 +1,7 @@ declare module 'rollbar'; +declare module 'web3-provider-engine/subproviders/rpc'; +declare module 'web3-provider-engine/subproviders/nonce-tracker'; +declare module 'web3-provider-engine/subproviders/hooked-wallet'; declare module '*.json' { const json: any; @@ -33,8 +36,4 @@ declare module 'web3-provider-engine' { } export = Web3ProviderEngine; } -/* tslint:enable */ - -declare module 'web3-provider-engine/subproviders/rpc'; -declare module 'web3-provider-engine/subproviders/nonce-tracker'; -declare module 'web3-provider-engine/subproviders/hooked-wallet'; +/* tslint:enable */ \ No newline at end of file -- cgit v1.2.3