From 3dcc199845f4026cc4d02f9f760332ac9752ff69 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 4 Aug 2017 13:27:08 -0400 Subject: rename the test description Transaction Manger -> Message Manger --- test/unit/message-manager-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/unit') diff --git a/test/unit/message-manager-test.js b/test/unit/message-manager-test.js index 30cb4f067..9b76241ed 100644 --- a/test/unit/message-manager-test.js +++ b/test/unit/message-manager-test.js @@ -1,7 +1,7 @@ const assert = require('assert') const MessageManger = require('../../app/scripts/lib/message-manager') -describe('Transaction Manager', function () { +describe('Message Manager', function () { let messageManager beforeEach(function () { -- cgit From 4ac0b972025ddd55a9a42b4a15db0a5f207f4078 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 4 Aug 2017 14:36:27 -0400 Subject: test for SufficientBalance --- test/unit/util-test.js | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 test/unit/util-test.js (limited to 'test/unit') diff --git a/test/unit/util-test.js b/test/unit/util-test.js new file mode 100644 index 000000000..7716e4872 --- /dev/null +++ b/test/unit/util-test.js @@ -0,0 +1,41 @@ +const assert = require('assert') +const { sufficientBalance } = require('../../app/scripts/lib/util') + + +describe.only('SufficientBalance', function () { + it('returns true if max tx cost is equal to balance.', function () { + const tx = { + 'value': '0x1', + 'gas': '0x2', + 'gasPrice': '0x3', + } + const balance = '0x8' + + const result = sufficientBalance(tx, balance) + assert.ok(result, 'sufficient balance found.') + }) + + it('returns true if max tx cost is less than balance.', function () { + const tx = { + 'value': '0x1', + 'gas': '0x2', + 'gasPrice': '0x3', + } + const balance = '0x9' + + const result = sufficientBalance(tx, balance) + assert.ok(result, 'sufficient balance found.') + }) + + it('returns false if max tx cost is more than balance.', function () { + const tx = { + 'value': '0x1', + 'gas': '0x2', + 'gasPrice': '0x3', + } + const balance = '0x6' + + const result = sufficientBalance(tx, balance) + assert.ok(!result, 'insufficient balance found.') + }) +}) \ No newline at end of file -- cgit From 54739cb798b70e14d7774d9e92745188afca5461 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Fri, 4 Aug 2017 14:40:22 -0400 Subject: test for pending tx watcher --- test/unit/pending-tx-test.js | 271 ++++++++++++++++++++++++++++++++++++++++ test/unit/tx-controller-test.js | 49 +------- test/unit/tx-utils-test.js | 58 +-------- test/unit/util-test.js | 2 +- 4 files changed, 280 insertions(+), 100 deletions(-) create mode 100644 test/unit/pending-tx-test.js (limited to 'test/unit') diff --git a/test/unit/pending-tx-test.js b/test/unit/pending-tx-test.js new file mode 100644 index 000000000..5103a9b62 --- /dev/null +++ b/test/unit/pending-tx-test.js @@ -0,0 +1,271 @@ +const assert = require('assert') +const ethUtil = require('ethereumjs-util') +const EthTx = require('ethereumjs-tx') +const ObservableStore = require('obs-store') +const clone = require('clone') +const PendingTransactionWatcher = require('../../app/scripts/lib/pending-tx-watchers') +const noop = () => true +const currentNetworkId = 42 +const otherNetworkId = 36 +const privKey = new Buffer('8718b9618a37d1fc78c436511fc6df3c8258d3250635bba617f33003270ec03e', 'hex') + +describe('PendingTransactionWatcher', function () { + let pendingTxWatcher, txMeta, txMetaNoHash, txMetaNoRawTx + this.timeout(10000) + beforeEach(function () { + txMeta = { + id: 1, + hash: '0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eebBlock', + status: 'signed', + txParams: { + from: '0x1678a085c290ebd122dc42cba69373b5953b831d', + nonce: '0x1', + value: '0xfffff', + }, + rawTx: '0xf86c808504a817c800827b0d940c62bb85faa3311a998d3aba8098c1235c564966880de0b6b3a7640000802aa08ff665feb887a25d4099e40e11f0fef93ee9608f404bd3f853dd9e84ed3317a6a02ec9d3d1d6e176d4d2593dd760e74ccac753e6a0ea0d00cc9789d0d7ff1f471d', + } + txMetaNoHash = { + id: 2, + status: 'signed', + txParams: { from: '0x1678a085c290ebd122dc42cba69373b5953b831d'}, + } + txMetaNoRawTx = { + hash: '0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eebBlock', + status: 'signed', + txParams: { from: '0x1678a085c290ebd122dc42cba69373b5953b831d'}, + } + + pendingTxWatcher = new PendingTransactionWatcher({ + provider: { sendAsync: noop }, + getBalance: () => {}, + nonceTracker: { + getGlobalLock: async () => { + return { releaseLock: () => {} } + } + }, + getPendingTransactions: () => {return []}, + sufficientBalance: () => {}, + publishTransaction: () => {}, + }) + + pendingTxWatcher.query = new Proxy({}, { + get: (queryStubResult, key) => { + if (key === 'stubResult') { + return function (method, ...args) { + queryStubResult[method] = args + } + } else { + const returnValues = queryStubResult[key] + return () => Promise.resolve(...returnValues) + } + }, + }) + + }) + + describe('#checkForTxInBlock', function () { + it('should return if no pending transactions', function () { + // throw a type error if it trys to do anything on the block + // thus failing the test + const block = Proxy.revocable({}, {}).revoke() + pendingTxWatcher.checkForTxInBlock(block) + }) + it('should emit \'txFailed\' if the txMeta does not have a hash', function (done) { + const block = Proxy.revocable({}, {}).revoke() + pendingTxWatcher.getPendingTransactions = () => [txMetaNoHash] + pendingTxWatcher.once('txFailed', (txId, err) => { + assert(txId, txMetaNoHash.id, 'should pass txId') + done() + }) + pendingTxWatcher.checkForTxInBlock(block) + }) + it('should emit \'txConfirmed\' if the tx is in the block', function (done) { + const block = { transactions: [txMeta]} + pendingTxWatcher.getPendingTransactions = () => [txMeta] + pendingTxWatcher.once('txConfirmed', (txId) => { + assert(txId, txMeta.id, 'should pass txId') + done() + }) + pendingTxWatcher.once('txFailed', (_, err) => { done(err) }) + pendingTxWatcher.checkForTxInBlock(block) + }) + }) + describe('#queryPendingTxs', function () { + it('should call #_checkPendingTxs if their is no oldBlock', function (done) { + let newBlock, oldBlock + newBlock = { number: '0x01' } + pendingTxWatcher._checkPendingTxs = done + pendingTxWatcher.queryPendingTxs({oldBlock, newBlock}) + }) + it('should call #_checkPendingTxs if oldBlock and the newBlock have a diff of greater then 1', function (done) { + let newBlock, oldBlock + oldBlock = { number: '0x01' } + newBlock = { number: '0x03' } + pendingTxWatcher._checkPendingTxs = done + pendingTxWatcher.queryPendingTxs({oldBlock, newBlock}) + }) + it('should not call #_checkPendingTxs if oldBlock and the newBlock have a diff of 1 or less', function (done) { + let newBlock, oldBlock + oldBlock = { number: '0x1' } + newBlock = { number: '0x2' } + pendingTxWatcher._checkPendingTxs = () => { + const err = new Error('should not call #_checkPendingTxs if oldBlock and the newBlock have a diff of 1 or less') + done(err) + } + pendingTxWatcher.queryPendingTxs({oldBlock, newBlock}) + done() + }) + }) + + describe('#_checkPendingTx', function () { + it('should emit \'txFailed\' if the txMeta does not have a hash', function (done) { + pendingTxWatcher.once('txFailed', (txId, err) => { + assert(txId, txMetaNoHash.id, 'should pass txId') + done() + }) + pendingTxWatcher._checkPendingTx(txMetaNoHash) + }) + + it('should should return if query does not return txParams', function () { + pendingTxWatcher.query.stubResult('getTransactionByHash', null) + pendingTxWatcher._checkPendingTx(txMeta) + }) + + it('should emit \'txConfirmed\'', function (done) { + pendingTxWatcher.query.stubResult('getTransactionByHash', {blockNumber: '0x01'}) + pendingTxWatcher.once('txConfirmed', (txId) => { + assert(txId, txMeta.id, 'should pass txId') + done() + }) + pendingTxWatcher.once('txFailed', (_, err) => { done(err) }) + pendingTxWatcher._checkPendingTx(txMeta) + }) + }) + + describe('#_checkPendingTxs', function () { + beforeEach(function () { + const txMeta2 = txMeta3 = txMeta + txMeta2.id = 2 + txMeta3.id = 3 + txList = [txMeta, txMeta2, txMeta3].map((tx) => { + tx.processed = new Promise ((resolve) => { tx.resolve = resolve }) + return tx + }) + }) + + it('should warp all txMeta\'s in #_checkPendingTx', function (done) { + pendingTxWatcher.getPendingTransactions = () => txList + pendingTxWatcher._checkPendingTx = (tx) => { tx.resolve(tx) } + const list = txList.map + Promise.all(txList.map((tx) => tx.processed)) + .then((txCompletedList) => done()) + .catch(done) + + pendingTxWatcher._checkPendingTxs() + }) + }) + + describe('#resubmitPendingTxs', function () { + beforeEach(function () { + const txMeta2 = txMeta3 = txMeta + txList = [txMeta, txMeta2, txMeta3].map((tx) => { + tx.processed = new Promise ((resolve) => { tx.resolve = resolve }) + return tx + }) + }) + + it('should return if no pending transactions', function () { + pendingTxWatcher.resubmitPendingTxs() + }) + it('should call #_resubmitTx for all pending tx\'s', function (done) { + pendingTxWatcher.getPendingTransactions = () => txList + pendingTxWatcher._resubmitTx = async (tx) => { tx.resolve(tx) } + Promise.all(txList.map((tx) => tx.processed)) + .then((txCompletedList) => done()) + .catch(done) + pendingTxWatcher.resubmitPendingTxs() + }) + it('should not emit \'txFailed\' if the txMeta throws a known txError', function (done) { + knownErrors =[ + // geth + ' Replacement transaction Underpriced ', + ' known transaction', + // parity + 'Gas price too low to replace ', + ' transaction with the sAme hash was already imported', + // other + ' gateway timeout', + ' noncE too low ', + ] + const enoughForAllErrors = txList.concat(txList) + + pendingTxWatcher.on('txFailed', (_, err) => done(err)) + + pendingTxWatcher.getPendingTransactions = () => enoughForAllErrors + pendingTxWatcher._resubmitTx = async (tx) => { + tx.resolve() + throw new Error(knownErrors.pop()) + } + Promise.all(txList.map((tx) => tx.processed)) + .then((txCompletedList) => done()) + .catch(done) + + pendingTxWatcher.resubmitPendingTxs() + }) + it('should emit \'txFailed\' if it encountered a real error', function (done) { + pendingTxWatcher.once('txFailed', (id, err) => err.message === 'im some real error' ? txList[id - 1].resolve() : done(err)) + + pendingTxWatcher.getPendingTransactions = () => txList + pendingTxWatcher._resubmitTx = async (tx) => { throw new TypeError('im some real error') } + Promise.all(txList.map((tx) => tx.processed)) + .then((txCompletedList) => done()) + .catch(done) + + pendingTxWatcher.resubmitPendingTxs() + }) + }) + describe('#_resubmitTx with a too-low balance', function () { + it('should return before publishing the transaction because to low of balance', function (done) { + const lowBalance = '0x0' + pendingTxWatcher.getBalance = (address) => { + assert.equal(address, txMeta.txParams.from, 'Should pass the address') + return lowBalance + } + pendingTxWatcher.publishTransaction = async (rawTx) => { + done(new Error('tried to publish transaction')) + } + + // Stubbing out current account state: + // Adding the fake tx: + pendingTxWatcher.once('txWarning', (txMeta) => { + assert(txMeta.warning.message, 'Should have a warning message') + done() + }) + pendingTxWatcher._resubmitTx(txMeta) + .catch((err) => { + assert.ifError(err, 'should not throw an error') + done(err) + }) + }) + + it('should publishing the transaction', function (done) { + const enoughBalance = '0x100000' + pendingTxWatcher.getBalance = (address) => { + assert.equal(address, txMeta.txParams.from, 'Should pass the address') + return enoughBalance + } + pendingTxWatcher.publishTransaction = async (rawTx) => { + assert.equal(rawTx, txMeta.rawTx, 'Should pass the rawTx') + } + + // Stubbing out current account state: + // Adding the fake tx: + pendingTxWatcher._resubmitTx(txMeta) + .then(() => done()) + .catch((err) => { + assert.ifError(err, 'should not throw an error') + done(err) + }) + }) + }) +}) \ No newline at end of file diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index f290088a1..e54dc9719 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -27,7 +27,8 @@ describe('Transaction Controller', function () { }), }) txController.nonceTracker.getNonceLock = () => Promise.resolve({ nextNonce: 0, releaseLock: noop }) - txController.query = new Proxy({}, { + txController.txProviderUtils = new TxProvideUtils(txController.provider) + txController.query = txController.txProviderUtils.query = new Proxy({}, { get: (queryStubResult, key) => { if (key === 'stubResult') { return function (method, ...args) { @@ -39,7 +40,7 @@ describe('Transaction Controller', function () { } }, }) - txController.txProviderUtils = new TxProvideUtils(txController.query) + }) describe('#newUnapprovedTransaction', function () { @@ -76,7 +77,6 @@ describe('Transaction Controller', function () { it('should resolve when finished and status is submitted and resolve with the hash', function (done) { txController.once('newUnaprovedTx', (txMetaFromEmit) => { setTimeout(() => { - console.log('HELLLO') txController.setTxHash(txMetaFromEmit.id, '0x0') txController.setTxStatusSubmitted(txMetaFromEmit.id) }, 10) @@ -93,7 +93,6 @@ describe('Transaction Controller', function () { it('should reject when finished and status is rejected', function (done) { txController.once('newUnaprovedTx', (txMetaFromEmit) => { setTimeout(() => { - console.log('HELLLO') txController.setTxStatusRejected(txMetaFromEmit.id) }, 10) }) @@ -429,46 +428,4 @@ describe('Transaction Controller', function () { }).catch(done) }) }) - - describe('#_resubmitTx with a too-low balance', function () { - it('should fail the transaction', function (done) { - const from = '0xda0da0' - const txMeta = { - id: 1, - status: 'submitted', - metamaskNetworkId: currentNetworkId, - txParams: { - from, - nonce: '0x1', - value: '0xfffff', - }, - } - - const lowBalance = '0x0' - const fakeStoreState = { accounts: {} } - fakeStoreState.accounts[from] = { - balance: lowBalance, - nonce: '0x0', - } - - // Stubbing out current account state: - const getStateStub = sinon.stub(txController.ethStore, 'getState') - .returns(fakeStoreState) - - // Adding the fake tx: - txController.addTx(clone(txMeta)) - - txController._resubmitTx(txMeta) - .then(() => { - const updatedMeta = txController.getTx(txMeta.id) - assert.notEqual(updatedMeta.status, txMeta.status, 'status changed.') - assert.equal(updatedMeta.status, 'failed', 'tx set to failed.') - done() - }) - .catch((err) => { - assert.ifError(err, 'should not throw an error') - done(err) - }) - }) - }) }) \ No newline at end of file diff --git a/test/unit/tx-utils-test.js b/test/unit/tx-utils-test.js index a43bcfb35..43128b977 100644 --- a/test/unit/tx-utils-test.js +++ b/test/unit/tx-utils-test.js @@ -1,7 +1,7 @@ const assert = require('assert') -const ethUtil = require('ethereumjs-util') -const BN = ethUtil.BN +const BN = require('bn.js') +const { hexToBn, bnToHex } = require('../../app/scripts/lib/util') const TxUtils = require('../../app/scripts/lib/tx-utils') @@ -16,44 +16,6 @@ describe('txUtils', function () { })) }) - describe('#sufficientBalance', function () { - it('returns true if max tx cost is equal to balance.', function () { - const tx = { - 'value': '0x1', - 'gas': '0x2', - 'gasPrice': '0x3', - } - const balance = '0x8' - - const result = txUtils.sufficientBalance(tx, balance) - assert.ok(result, 'sufficient balance found.') - }) - - it('returns true if max tx cost is less than balance.', function () { - const tx = { - 'value': '0x1', - 'gas': '0x2', - 'gasPrice': '0x3', - } - const balance = '0x9' - - const result = txUtils.sufficientBalance(tx, balance) - assert.ok(result, 'sufficient balance found.') - }) - - it('returns false if max tx cost is more than balance.', function () { - const tx = { - 'value': '0x1', - 'gas': '0x2', - 'gasPrice': '0x3', - } - const balance = '0x6' - - const result = txUtils.sufficientBalance(tx, balance) - assert.ok(!result, 'insufficient balance found.') - }) - }) - describe('chain Id', function () { it('prepares a transaction with the provided chainId', function () { const txParams = { @@ -96,7 +58,7 @@ describe('txUtils', function () { assert(outputBn.eq(expectedBn), 'returns the original estimatedGas value') }) - it('buffers up to reccomend gas limit reccomended ceiling', function () { + it('buffers up to recommend gas limit recommended ceiling', function () { // naive estimatedGas: 0x16e360 (1.5 mil) const inputHex = '0x16e360' // dummy gas limit: 0x1e8480 (2 mil) @@ -107,17 +69,7 @@ describe('txUtils', function () { // const inputBn = hexToBn(inputHex) // const outputBn = hexToBn(output) const expectedHex = bnToHex(ceilGasLimitBn) - assert.equal(output, expectedHex, 'returns the gas limit reccomended ceiling value') + assert.equal(output, expectedHex, 'returns the gas limit recommended ceiling value') }) }) -}) - -// util - -function hexToBn (inputHex) { - return new BN(ethUtil.stripHexPrefix(inputHex), 16) -} - -function bnToHex (inputBn) { - return ethUtil.addHexPrefix(inputBn.toString(16)) -} +}) \ No newline at end of file diff --git a/test/unit/util-test.js b/test/unit/util-test.js index 7716e4872..6da185b2c 100644 --- a/test/unit/util-test.js +++ b/test/unit/util-test.js @@ -2,7 +2,7 @@ const assert = require('assert') const { sufficientBalance } = require('../../app/scripts/lib/util') -describe.only('SufficientBalance', function () { +describe('SufficientBalance', function () { it('returns true if max tx cost is equal to balance.', function () { const tx = { 'value': '0x1', -- cgit From e7f838e626ff1ce29490a081d7ee3f917745dd62 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Mon, 7 Aug 2017 14:56:17 -0400 Subject: fix test --- test/unit/pending-tx-test.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'test/unit') diff --git a/test/unit/pending-tx-test.js b/test/unit/pending-tx-test.js index 5103a9b62..d0606bb17 100644 --- a/test/unit/pending-tx-test.js +++ b/test/unit/pending-tx-test.js @@ -224,7 +224,7 @@ describe('PendingTransactionWatcher', function () { pendingTxWatcher.resubmitPendingTxs() }) }) - describe('#_resubmitTx with a too-low balance', function () { + describe.only('#_resubmitTx with a too-low balance', function () { it('should return before publishing the transaction because to low of balance', function (done) { const lowBalance = '0x0' pendingTxWatcher.getBalance = (address) => { @@ -237,8 +237,8 @@ describe('PendingTransactionWatcher', function () { // Stubbing out current account state: // Adding the fake tx: - pendingTxWatcher.once('txWarning', (txMeta) => { - assert(txMeta.warning.message, 'Should have a warning message') + pendingTxWatcher.once('txFailed', (txId, err) => { + assert(err, 'Should have a error') done() }) pendingTxWatcher._resubmitTx(txMeta) -- cgit From 28fbdca83006fa32c9411081f2152560005167ad Mon Sep 17 00:00:00 2001 From: frankiebee Date: Mon, 7 Aug 2017 16:41:54 -0400 Subject: remove .only --- test/unit/pending-tx-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/unit') diff --git a/test/unit/pending-tx-test.js b/test/unit/pending-tx-test.js index d0606bb17..6e50dae6a 100644 --- a/test/unit/pending-tx-test.js +++ b/test/unit/pending-tx-test.js @@ -224,7 +224,7 @@ describe('PendingTransactionWatcher', function () { pendingTxWatcher.resubmitPendingTxs() }) }) - describe.only('#_resubmitTx with a too-low balance', function () { + describe('#_resubmitTx with a too-low balance', function () { it('should return before publishing the transaction because to low of balance', function (done) { const lowBalance = '0x0' pendingTxWatcher.getBalance = (address) => { -- cgit From e761fb0ef7d7d658bd6e558fe2fcbe69d8eb4999 Mon Sep 17 00:00:00 2001 From: frankiebee Date: Tue, 8 Aug 2017 18:30:22 -0400 Subject: use provider stub --- test/unit/pending-tx-test.js | 119 ++++++++++++++++++---------------------- test/unit/tx-controller-test.js | 34 ++++-------- 2 files changed, 65 insertions(+), 88 deletions(-) (limited to 'test/unit') diff --git a/test/unit/pending-tx-test.js b/test/unit/pending-tx-test.js index 6e50dae6a..8c6d287f8 100644 --- a/test/unit/pending-tx-test.js +++ b/test/unit/pending-tx-test.js @@ -3,19 +3,20 @@ const ethUtil = require('ethereumjs-util') const EthTx = require('ethereumjs-tx') const ObservableStore = require('obs-store') const clone = require('clone') -const PendingTransactionWatcher = require('../../app/scripts/lib/pending-tx-watchers') +const { createStubedProvider } = require('../stub/provider') +const PendingTransactionTracker = require('../../app/scripts/lib/pending-tx-tracker') const noop = () => true const currentNetworkId = 42 const otherNetworkId = 36 const privKey = new Buffer('8718b9618a37d1fc78c436511fc6df3c8258d3250635bba617f33003270ec03e', 'hex') -describe('PendingTransactionWatcher', function () { - let pendingTxWatcher, txMeta, txMetaNoHash, txMetaNoRawTx +describe('PendingTransactionTracker', function () { + let pendingTxTracker, txMeta, txMetaNoHash, txMetaNoRawTx, providerResultStub, provider this.timeout(10000) beforeEach(function () { txMeta = { id: 1, - hash: '0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eebBlock', + hash: '0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb', status: 'signed', txParams: { from: '0x1678a085c290ebd122dc42cba69373b5953b831d', @@ -30,13 +31,15 @@ describe('PendingTransactionWatcher', function () { txParams: { from: '0x1678a085c290ebd122dc42cba69373b5953b831d'}, } txMetaNoRawTx = { - hash: '0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eebBlock', + hash: '0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb', status: 'signed', txParams: { from: '0x1678a085c290ebd122dc42cba69373b5953b831d'}, } + providerResultStub = {} + provider = createStubedProvider(providerResultStub) - pendingTxWatcher = new PendingTransactionWatcher({ - provider: { sendAsync: noop }, + pendingTxTracker = new PendingTransactionTracker({ + provider, getBalance: () => {}, nonceTracker: { getGlobalLock: async () => { @@ -47,20 +50,6 @@ describe('PendingTransactionWatcher', function () { sufficientBalance: () => {}, publishTransaction: () => {}, }) - - pendingTxWatcher.query = new Proxy({}, { - get: (queryStubResult, key) => { - if (key === 'stubResult') { - return function (method, ...args) { - queryStubResult[method] = args - } - } else { - const returnValues = queryStubResult[key] - return () => Promise.resolve(...returnValues) - } - }, - }) - }) describe('#checkForTxInBlock', function () { @@ -68,77 +57,77 @@ describe('PendingTransactionWatcher', function () { // throw a type error if it trys to do anything on the block // thus failing the test const block = Proxy.revocable({}, {}).revoke() - pendingTxWatcher.checkForTxInBlock(block) + pendingTxTracker.checkForTxInBlock(block) }) it('should emit \'txFailed\' if the txMeta does not have a hash', function (done) { const block = Proxy.revocable({}, {}).revoke() - pendingTxWatcher.getPendingTransactions = () => [txMetaNoHash] - pendingTxWatcher.once('txFailed', (txId, err) => { + pendingTxTracker.getPendingTransactions = () => [txMetaNoHash] + pendingTxTracker.once('txFailed', (txId, err) => { assert(txId, txMetaNoHash.id, 'should pass txId') done() }) - pendingTxWatcher.checkForTxInBlock(block) + pendingTxTracker.checkForTxInBlock(block) }) it('should emit \'txConfirmed\' if the tx is in the block', function (done) { const block = { transactions: [txMeta]} - pendingTxWatcher.getPendingTransactions = () => [txMeta] - pendingTxWatcher.once('txConfirmed', (txId) => { + pendingTxTracker.getPendingTransactions = () => [txMeta] + pendingTxTracker.once('txConfirmed', (txId) => { assert(txId, txMeta.id, 'should pass txId') done() }) - pendingTxWatcher.once('txFailed', (_, err) => { done(err) }) - pendingTxWatcher.checkForTxInBlock(block) + pendingTxTracker.once('txFailed', (_, err) => { done(err) }) + pendingTxTracker.checkForTxInBlock(block) }) }) describe('#queryPendingTxs', function () { it('should call #_checkPendingTxs if their is no oldBlock', function (done) { let newBlock, oldBlock newBlock = { number: '0x01' } - pendingTxWatcher._checkPendingTxs = done - pendingTxWatcher.queryPendingTxs({oldBlock, newBlock}) + pendingTxTracker._checkPendingTxs = done + pendingTxTracker.queryPendingTxs({oldBlock, newBlock}) }) it('should call #_checkPendingTxs if oldBlock and the newBlock have a diff of greater then 1', function (done) { let newBlock, oldBlock oldBlock = { number: '0x01' } newBlock = { number: '0x03' } - pendingTxWatcher._checkPendingTxs = done - pendingTxWatcher.queryPendingTxs({oldBlock, newBlock}) + pendingTxTracker._checkPendingTxs = done + pendingTxTracker.queryPendingTxs({oldBlock, newBlock}) }) it('should not call #_checkPendingTxs if oldBlock and the newBlock have a diff of 1 or less', function (done) { let newBlock, oldBlock oldBlock = { number: '0x1' } newBlock = { number: '0x2' } - pendingTxWatcher._checkPendingTxs = () => { + pendingTxTracker._checkPendingTxs = () => { const err = new Error('should not call #_checkPendingTxs if oldBlock and the newBlock have a diff of 1 or less') done(err) } - pendingTxWatcher.queryPendingTxs({oldBlock, newBlock}) + pendingTxTracker.queryPendingTxs({oldBlock, newBlock}) done() }) }) describe('#_checkPendingTx', function () { it('should emit \'txFailed\' if the txMeta does not have a hash', function (done) { - pendingTxWatcher.once('txFailed', (txId, err) => { + pendingTxTracker.once('txFailed', (txId, err) => { assert(txId, txMetaNoHash.id, 'should pass txId') done() }) - pendingTxWatcher._checkPendingTx(txMetaNoHash) + pendingTxTracker._checkPendingTx(txMetaNoHash) }) it('should should return if query does not return txParams', function () { - pendingTxWatcher.query.stubResult('getTransactionByHash', null) - pendingTxWatcher._checkPendingTx(txMeta) + providerResultStub.eth_getTransactionByHash = null + pendingTxTracker._checkPendingTx(txMeta) }) it('should emit \'txConfirmed\'', function (done) { - pendingTxWatcher.query.stubResult('getTransactionByHash', {blockNumber: '0x01'}) - pendingTxWatcher.once('txConfirmed', (txId) => { + providerResultStub.eth_getTransactionByHash = {blockNumber: '0x01'} + pendingTxTracker.once('txConfirmed', (txId) => { assert(txId, txMeta.id, 'should pass txId') done() }) - pendingTxWatcher.once('txFailed', (_, err) => { done(err) }) - pendingTxWatcher._checkPendingTx(txMeta) + pendingTxTracker.once('txFailed', (_, err) => { done(err) }) + pendingTxTracker._checkPendingTx(txMeta) }) }) @@ -154,14 +143,14 @@ describe('PendingTransactionWatcher', function () { }) it('should warp all txMeta\'s in #_checkPendingTx', function (done) { - pendingTxWatcher.getPendingTransactions = () => txList - pendingTxWatcher._checkPendingTx = (tx) => { tx.resolve(tx) } + pendingTxTracker.getPendingTransactions = () => txList + pendingTxTracker._checkPendingTx = (tx) => { tx.resolve(tx) } const list = txList.map Promise.all(txList.map((tx) => tx.processed)) .then((txCompletedList) => done()) .catch(done) - pendingTxWatcher._checkPendingTxs() + pendingTxTracker._checkPendingTxs() }) }) @@ -175,15 +164,15 @@ describe('PendingTransactionWatcher', function () { }) it('should return if no pending transactions', function () { - pendingTxWatcher.resubmitPendingTxs() + pendingTxTracker.resubmitPendingTxs() }) it('should call #_resubmitTx for all pending tx\'s', function (done) { - pendingTxWatcher.getPendingTransactions = () => txList - pendingTxWatcher._resubmitTx = async (tx) => { tx.resolve(tx) } + pendingTxTracker.getPendingTransactions = () => txList + pendingTxTracker._resubmitTx = async (tx) => { tx.resolve(tx) } Promise.all(txList.map((tx) => tx.processed)) .then((txCompletedList) => done()) .catch(done) - pendingTxWatcher.resubmitPendingTxs() + pendingTxTracker.resubmitPendingTxs() }) it('should not emit \'txFailed\' if the txMeta throws a known txError', function (done) { knownErrors =[ @@ -199,10 +188,10 @@ describe('PendingTransactionWatcher', function () { ] const enoughForAllErrors = txList.concat(txList) - pendingTxWatcher.on('txFailed', (_, err) => done(err)) + pendingTxTracker.on('txFailed', (_, err) => done(err)) - pendingTxWatcher.getPendingTransactions = () => enoughForAllErrors - pendingTxWatcher._resubmitTx = async (tx) => { + pendingTxTracker.getPendingTransactions = () => enoughForAllErrors + pendingTxTracker._resubmitTx = async (tx) => { tx.resolve() throw new Error(knownErrors.pop()) } @@ -210,38 +199,38 @@ describe('PendingTransactionWatcher', function () { .then((txCompletedList) => done()) .catch(done) - pendingTxWatcher.resubmitPendingTxs() + pendingTxTracker.resubmitPendingTxs() }) it('should emit \'txFailed\' if it encountered a real error', function (done) { - pendingTxWatcher.once('txFailed', (id, err) => err.message === 'im some real error' ? txList[id - 1].resolve() : done(err)) + pendingTxTracker.once('txFailed', (id, err) => err.message === 'im some real error' ? txList[id - 1].resolve() : done(err)) - pendingTxWatcher.getPendingTransactions = () => txList - pendingTxWatcher._resubmitTx = async (tx) => { throw new TypeError('im some real error') } + pendingTxTracker.getPendingTransactions = () => txList + pendingTxTracker._resubmitTx = async (tx) => { throw new TypeError('im some real error') } Promise.all(txList.map((tx) => tx.processed)) .then((txCompletedList) => done()) .catch(done) - pendingTxWatcher.resubmitPendingTxs() + pendingTxTracker.resubmitPendingTxs() }) }) describe('#_resubmitTx with a too-low balance', function () { it('should return before publishing the transaction because to low of balance', function (done) { const lowBalance = '0x0' - pendingTxWatcher.getBalance = (address) => { + pendingTxTracker.getBalance = (address) => { assert.equal(address, txMeta.txParams.from, 'Should pass the address') return lowBalance } - pendingTxWatcher.publishTransaction = async (rawTx) => { + pendingTxTracker.publishTransaction = async (rawTx) => { done(new Error('tried to publish transaction')) } // Stubbing out current account state: // Adding the fake tx: - pendingTxWatcher.once('txFailed', (txId, err) => { + pendingTxTracker.once('txFailed', (txId, err) => { assert(err, 'Should have a error') done() }) - pendingTxWatcher._resubmitTx(txMeta) + pendingTxTracker._resubmitTx(txMeta) .catch((err) => { assert.ifError(err, 'should not throw an error') done(err) @@ -250,17 +239,17 @@ describe('PendingTransactionWatcher', function () { it('should publishing the transaction', function (done) { const enoughBalance = '0x100000' - pendingTxWatcher.getBalance = (address) => { + pendingTxTracker.getBalance = (address) => { assert.equal(address, txMeta.txParams.from, 'Should pass the address') return enoughBalance } - pendingTxWatcher.publishTransaction = async (rawTx) => { + pendingTxTracker.publishTransaction = async (rawTx) => { assert.equal(rawTx, txMeta.rawTx, 'Should pass the rawTx') } // Stubbing out current account state: // Adding the fake tx: - pendingTxWatcher._resubmitTx(txMeta) + pendingTxTracker._resubmitTx(txMeta) .then(() => done()) .catch((err) => { assert.ifError(err, 'should not throw an error') diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index e54dc9719..57d7deccd 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -10,16 +10,20 @@ const noop = () => true const currentNetworkId = 42 const otherNetworkId = 36 const privKey = new Buffer('8718b9618a37d1fc78c436511fc6df3c8258d3250635bba617f33003270ec03e', 'hex') +const { createStubedProvider } = require('../stub/provider') describe('Transaction Controller', function () { - let txController + let txController, engine, provider, providerResultStub beforeEach(function () { + providerResultStub = {} + provider = createStubedProvider(providerResultStub) + txController = new TransactionController({ + provider, networkStore: new ObservableStore(currentNetworkId), txHistoryLimit: 10, blockTracker: { getCurrentBlock: noop, on: noop, once: noop }, - provider: { sendAsync: noop }, ethStore: { getState: noop }, signTransaction: (ethTx) => new Promise((resolve) => { ethTx.sign(privKey) @@ -28,19 +32,6 @@ describe('Transaction Controller', function () { }) txController.nonceTracker.getNonceLock = () => Promise.resolve({ nextNonce: 0, releaseLock: noop }) txController.txProviderUtils = new TxProvideUtils(txController.provider) - txController.query = txController.txProviderUtils.query = new Proxy({}, { - get: (queryStubResult, key) => { - if (key === 'stubResult') { - return function (method, ...args) { - queryStubResult[method] = args - } - } else { - const returnValues = queryStubResult[key] - return () => Promise.resolve(...returnValues) - } - }, - }) - }) describe('#newUnapprovedTransaction', function () { @@ -132,11 +123,9 @@ describe('Transaction Controller', function () { 'to':'0xc684832530fcbddae4b4230a47e991ddcec2831d', }, } - - txController.query.stubResult('gasPrice', '0x4a817c800') - txController.query.stubResult('getBlockByNumber', { gasLimit: '0x47b784' }) - txController.query.stubResult('estimateGas', '0x5209') - + providerResultStub.eth_gasPrice = '4a817c800' + providerResultStub.eth_getBlockByNumber = { gasLimit: '47b784' } + providerResultStub.eth_estimateGas = '5209' txController.addTxDefaults(txMeta) .then((txMetaWithDefaults) => { assert(txMetaWithDefaults.txParams.value, '0x0','should have added 0x0 as the value') @@ -393,9 +382,8 @@ describe('Transaction Controller', function () { const wrongValue = '0x05' txController.addTx(txMeta) - - txController.query.stubResult('estimateGas', wrongValue) - txController.query.stubResult('gasPrice', wrongValue) + providerResultStub.eth_gasPrice = wrongValue + providerResultStub.eth_estimateGas = '0x5209' const signStub = sinon.stub(txController, 'signTransaction').callsFake(() => Promise.resolve()) -- cgit