1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Merge pull request #1824 from MetaMask/tx-cont-fix

TransactionController & NetworkController fixes
This commit is contained in:
Frankie 2017-07-25 17:46:31 -04:00 committed by GitHub
commit 0c73d6d852
4 changed files with 35 additions and 24 deletions

View File

@ -28,9 +28,9 @@ module.exports = class NetworkController extends EventEmitter {
this._provider = provider this._provider = provider
} }
initializeProvider (opts) { initializeProvider (opts, providerContructor = MetaMaskProvider) {
this.providerInit = opts this.providerInit = opts
this._provider = MetaMaskProvider(opts) this._provider = providerContructor(opts)
this._proxy = new Proxy(this._provider, { this._proxy = new Proxy(this._provider, {
get: (obj, name) => { get: (obj, name) => {
if (name === 'on') return this._on.bind(this) if (name === 'on') return this._on.bind(this)
@ -38,6 +38,7 @@ module.exports = class NetworkController extends EventEmitter {
}, },
set: (obj, name, value) => { set: (obj, name, value) => {
this._provider[name] = value this._provider[name] = value
return value
}, },
}) })
this.provider.on('block', this._logBlock.bind(this)) this.provider.on('block', this._logBlock.bind(this))

View File

@ -458,7 +458,7 @@ module.exports = class TransactionController extends EventEmitter {
})) }))
} }
async _resubmitTx (txMeta, cb) { async _resubmitTx (txMeta) {
const address = txMeta.txParams.from const address = txMeta.txParams.from
const balance = this.ethStore.getState().accounts[address].balance const balance = this.ethStore.getState().accounts[address].balance
if (!('retryCount' in txMeta)) txMeta.retryCount = 0 if (!('retryCount' in txMeta)) txMeta.retryCount = 0
@ -467,17 +467,17 @@ module.exports = class TransactionController extends EventEmitter {
if (!this.txProviderUtils.sufficientBalance(txMeta.txParams, balance)) { if (!this.txProviderUtils.sufficientBalance(txMeta.txParams, balance)) {
const message = 'Insufficient balance.' const message = 'Insufficient balance.'
this.setTxStatusFailed(txMeta.id, { message }) this.setTxStatusFailed(txMeta.id, { message })
cb() log.error(message)
return log.error(message) return
} }
// Only auto-submit already-signed txs: // Only auto-submit already-signed txs:
if (!('rawTx' in txMeta)) return cb() if (!('rawTx' in txMeta)) return
// Increment a try counter. // Increment a try counter.
txMeta.retryCount++ txMeta.retryCount++
const rawTx = txMeta.rawTx const rawTx = txMeta.rawTx
return await this.txProviderUtils.publishTransaction(rawTx, cb) return await this.txProviderUtils.publishTransaction(rawTx)
} }
// checks the network for signed txs and // checks the network for signed txs and

View File

@ -3,6 +3,9 @@ const NetworkController = require('../../app/scripts/controllers/network')
describe('# Network Controller', function () { describe('# Network Controller', function () {
let networkController let networkController
const networkControllerProviderInit = {
getAccounts: () => {},
}
beforeEach(function () { beforeEach(function () {
networkController = new NetworkController({ networkController = new NetworkController({
@ -10,26 +13,13 @@ describe('# Network Controller', function () {
type: 'rinkeby', type: 'rinkeby',
}, },
}) })
// stub out provider
networkController._provider = new Proxy({}, {
get: (obj, name) => {
return () => {}
},
})
networkController.providerInit = {
getAccounts: () => {},
}
networkController.ethQuery = new Proxy({}, { networkController.initializeProvider(networkControllerProviderInit, dummyProviderConstructor)
get: (obj, name) => {
return () => {}
},
})
}) })
describe('network', function () { describe('network', function () {
describe('#provider', function () { describe('#provider', function () {
it('provider should be updatable without reassignment', function () { it('provider should be updatable without reassignment', function () {
networkController.initializeProvider(networkController.providerInit) networkController.initializeProvider(networkControllerProviderInit, dummyProviderConstructor)
const provider = networkController.provider const provider = networkController.provider
networkController._provider = {test: true} networkController._provider = {test: true}
assert.ok(provider.test) assert.ok(provider.test)
@ -75,3 +65,19 @@ describe('# Network Controller', function () {
}) })
}) })
}) })
function dummyProviderConstructor() {
return {
// provider
sendAsync: noop,
// block tracker
start: noop,
stop: noop,
on: noop,
addListener: noop,
once: noop,
removeAllListeners: noop,
}
}
function noop() {}

View File

@ -343,13 +343,17 @@ describe('Transaction Controller', function () {
// Adding the fake tx: // Adding the fake tx:
txController.addTx(clone(txMeta)) txController.addTx(clone(txMeta))
txController._resubmitTx(txMeta, function (err) { txController._resubmitTx(txMeta)
assert.ifError(err, 'should not throw an error') .then(() => {
const updatedMeta = txController.getTx(txMeta.id) const updatedMeta = txController.getTx(txMeta.id)
assert.notEqual(updatedMeta.status, txMeta.status, 'status changed.') assert.notEqual(updatedMeta.status, txMeta.status, 'status changed.')
assert.equal(updatedMeta.status, 'failed', 'tx set to failed.') assert.equal(updatedMeta.status, 'failed', 'tx set to failed.')
done() done()
}) })
.catch((err) => {
assert.ifError(err, 'should not throw an error')
done()
})
}) })
}) })
}) })