2017-06-22 02:28:19 +02:00
|
|
|
const assert = require('assert')
|
|
|
|
const NonceTracker = require('../../app/scripts/lib/nonce-tracker')
|
|
|
|
|
|
|
|
describe('Nonce Tracker', function () {
|
2017-08-18 22:53:18 +02:00
|
|
|
let nonceTracker, provider
|
|
|
|
let getPendingTransactions, pendingTxs
|
|
|
|
let getConfirmedTransactions, confirmedTxs
|
|
|
|
let providerResultStub = {}
|
2017-06-22 02:28:19 +02:00
|
|
|
|
|
|
|
beforeEach(function () {
|
2017-06-22 04:51:00 +02:00
|
|
|
pendingTxs = [{
|
2017-06-22 02:28:19 +02:00
|
|
|
'status': 'submitted',
|
2017-08-18 22:53:18 +02:00
|
|
|
'txParams': {
|
|
|
|
'from': '0x7d3517b0d011698406d6e0aed8453f0be2697926',
|
|
|
|
'gas': '0x30d40',
|
|
|
|
'value': '0x0',
|
|
|
|
'nonce': '0x3',
|
|
|
|
},
|
|
|
|
}]
|
|
|
|
confirmedTxs = [{
|
|
|
|
'status': 'confirmed',
|
2017-06-22 02:28:19 +02:00
|
|
|
'txParams': {
|
|
|
|
'from': '0x7d3517b0d011698406d6e0aed8453f0be2697926',
|
|
|
|
'gas': '0x30d40',
|
|
|
|
'value': '0x0',
|
2017-06-22 04:51:00 +02:00
|
|
|
'nonce': '0x0',
|
2017-06-22 02:28:19 +02:00
|
|
|
},
|
2017-08-18 22:53:18 +02:00
|
|
|
}, {
|
|
|
|
'status': 'confirmed',
|
|
|
|
'txParams': {
|
|
|
|
'from': '0x7d3517b0d011698406d6e0aed8453f0be2697926',
|
|
|
|
'gas': '0x30d40',
|
|
|
|
'value': '0x0',
|
|
|
|
'nonce': '0x1',
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
'status': 'confirmed',
|
|
|
|
'txParams': {
|
|
|
|
'from': '0x7d3517b0d011698406d6e0aed8453f0be2697926',
|
|
|
|
'gas': '0x30d40',
|
|
|
|
'value': '0x0',
|
|
|
|
'nonce': '0x2',
|
|
|
|
},
|
2017-06-22 02:28:19 +02:00
|
|
|
}]
|
|
|
|
|
|
|
|
|
|
|
|
getPendingTransactions = () => pendingTxs
|
2017-08-18 22:53:18 +02:00
|
|
|
getConfirmedTransactions = () => confirmedTxs
|
|
|
|
providerResultStub.result = '0x3'
|
2017-08-04 00:05:32 +02:00
|
|
|
provider = {
|
2017-08-18 22:53:18 +02:00
|
|
|
sendAsync: (_, cb) => { cb(undefined, providerResultStub) },
|
2017-08-04 00:05:32 +02:00
|
|
|
_blockTracker: {
|
2017-06-22 04:51:00 +02:00
|
|
|
getCurrentBlock: () => '0x11b568',
|
2017-06-22 02:28:19 +02:00
|
|
|
},
|
2017-08-04 00:05:32 +02:00
|
|
|
}
|
|
|
|
nonceTracker = new NonceTracker({
|
2017-06-22 02:28:19 +02:00
|
|
|
provider,
|
|
|
|
getPendingTransactions,
|
2017-08-18 22:53:18 +02:00
|
|
|
getConfirmedTransactions,
|
2017-06-22 02:28:19 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('#getNonceLock', function () {
|
2017-08-04 01:34:38 +02:00
|
|
|
it('should work', async function () {
|
2017-06-22 02:28:19 +02:00
|
|
|
this.timeout(15000)
|
|
|
|
const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926')
|
2017-08-18 22:53:18 +02:00
|
|
|
assert.equal(nonceLock.nextNonce, '4', 'nonce should be 4')
|
|
|
|
await nonceLock.releaseLock()
|
|
|
|
})
|
|
|
|
|
|
|
|
it('should use localNonce if network returns a nonce lower then a confirmed tx in state', async function () {
|
|
|
|
this.timeout(15000)
|
|
|
|
providerResultStub.result = '0x1'
|
|
|
|
const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926')
|
|
|
|
assert.equal(nonceLock.nextNonce, '4', 'nonce should be 4')
|
2017-06-22 04:51:00 +02:00
|
|
|
await nonceLock.releaseLock()
|
2017-06-22 02:28:19 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|