aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/nonce-tracker-test.js
blob: 3312a3bd08de3f94092ec1aac3a650491d053b76 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const assert = require('assert')
const NonceTracker = require('../../app/scripts/lib/nonce-tracker')
const MockTxGen = require('../lib/mock-tx-gen')
let providerResultStub = {}

describe('Nonce Tracker', function () {
  let nonceTracker, provider
  let getPendingTransactions, pendingTxs
  let getConfirmedTransactions, confirmedTxs

  describe('#getNonceLock', function () {

    describe('with 3 confirmed and 1 pending', function () {
      beforeEach(function () {
        const txGen = new MockTxGen()
        confirmedTxs = txGen.generate({ status: 'confirmed' }, { count: 3 })
        pendingTxs = txGen.generate({ status: 'submitted' }, { count: 1 })
        nonceTracker = generateNonceTrackerWith(pendingTxs, confirmedTxs, '0x1')
      })

      it('should return 4', async function () {
        this.timeout(15000)
        const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926')
        assert.equal(nonceLock.nextNonce, '4', `nonce should be 4 got ${nonceLock.nextNonce}`)
        await nonceLock.releaseLock()
      })

      it('should use localNonce if network returns a nonce lower then a confirmed tx in state', async function () {
        this.timeout(15000)
        const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926')
        assert.equal(nonceLock.nextNonce, '4', 'nonce should be 4')
        await nonceLock.releaseLock()
      })
    })

    describe('with no previous txs', function () {
      beforeEach(function () {
        nonceTracker = generateNonceTrackerWith([], [])
      })

      it('should return 0', async function () {
        this.timeout(15000)
        const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926')
        assert.equal(nonceLock.nextNonce, '0', `nonce should be 0 returned ${nonceLock.nextNonce}`)
        await nonceLock.releaseLock()
      })
    })

    describe('with multiple previous txs with same nonce', function () {
      beforeEach(function () {
        const txGen = new MockTxGen()
        confirmedTxs = txGen.generate({ status: 'confirmed' }, { count: 1 })
        pendingTxs = txGen.generate({
          status: 'submitted',
          txParams: { nonce: '0x01' },
        }, { count: 5 })

        nonceTracker = generateNonceTrackerWith(pendingTxs, confirmedTxs, '0x0')
      })

      it('should return nonce after those', async function () {
        this.timeout(15000)
        const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926')
        assert.equal(nonceLock.nextNonce, '2', `nonce should be 2 got ${nonceLock.nextNonce}`)
        await nonceLock.releaseLock()
      })
    })

    describe('when local confirmed count is higher than network nonce', function () {
      beforeEach(function () {
        const txGen = new MockTxGen()
        confirmedTxs = txGen.generate({ status: 'confirmed' }, { count: 3 })
        nonceTracker = generateNonceTrackerWith([], confirmedTxs, '0x1')
      })

      it('should return nonce after those', async function () {
        this.timeout(15000)
        const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926')
        assert.equal(nonceLock.nextNonce, '3', `nonce should be 3 got ${nonceLock.nextNonce}`)
        await nonceLock.releaseLock()
      })
    })

    describe('when local pending count is higher than other metrics', function () {
      beforeEach(function () {
        const txGen = new MockTxGen()
        pendingTxs = txGen.generate({ status: 'submitted' }, { count: 2 })
        nonceTracker = generateNonceTrackerWith(pendingTxs, [])
      })

      it('should return nonce after those', async function () {
        this.timeout(15000)
        const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926')
        assert.equal(nonceLock.nextNonce, '2', `nonce should be 2 got ${nonceLock.nextNonce}`)
        await nonceLock.releaseLock()
      })
    })

    describe('when provider nonce is higher than other metrics', function () {
      beforeEach(function () {
        const txGen = new MockTxGen()
        pendingTxs = txGen.generate({ status: 'submitted' }, { count: 2 })
        nonceTracker = generateNonceTrackerWith(pendingTxs, [], '0x05')
      })

      it('should return nonce after those', async function () {
        this.timeout(15000)
        const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926')
        assert.equal(nonceLock.nextNonce, '5', `nonce should be 5 got ${nonceLock.nextNonce}`)
        await nonceLock.releaseLock()
      })
    })

    describe('when there are some pending nonces below the remote one and some over.', function () {
      beforeEach(function () {
        const txGen = new MockTxGen()
        pendingTxs = txGen.generate({ status: 'submitted' }, { count: 5 })
        nonceTracker = generateNonceTrackerWith(pendingTxs, [], '0x03')
      })

      it('should return nonce after those', async function () {
        this.timeout(15000)
        const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926')
        assert.equal(nonceLock.nextNonce, '5', `nonce should be 5 got ${nonceLock.nextNonce}`)
        await nonceLock.releaseLock()
      })
    })

    describe('when there are pending nonces non sequentially over the network nonce.', function () {
      beforeEach(function () {
        const txGen = new MockTxGen()
        txGen.generate({ status: 'submitted' }, { count: 5 })
        // 5 over that number
        pendingTxs = txGen.generate({ status: 'submitted' }, { count: 5 })
        nonceTracker = generateNonceTrackerWith(pendingTxs, [], '0x00')
      })

      it('should return nonce after network nonce', async function () {
        this.timeout(15000)
        const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926')
        assert.equal(nonceLock.nextNonce, '0', `nonce should be 0 got ${nonceLock.nextNonce}`)
        await nonceLock.releaseLock()
      })
    })

    describe('When all three return different values', function () {
      beforeEach(function () {
        const txGen = new MockTxGen()
        const confirmedTxs = txGen.generate({ status: 'confirmed' }, { count: 10 })
        const pendingTxs = txGen.generate({
          status: 'submitted',
          nonce: 100,
        }, { count: 1 })
                                         // 0x32 is 50 in hex:
        nonceTracker = generateNonceTrackerWith(pendingTxs, confirmedTxs, '0x32')
      })

      it('should return nonce after network nonce', async function () {
        this.timeout(15000)
        const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926')
        assert.equal(nonceLock.nextNonce, '50', `nonce should be 50 got ${nonceLock.nextNonce}`)
        await nonceLock.releaseLock()
      })
    })
  })
})

function generateNonceTrackerWith (pending, confirmed, providerStub = '0x0') {
  const getPendingTransactions = () => pending
  const getConfirmedTransactions = () => confirmed
  providerResultStub.result = providerStub
  const provider = {
    sendAsync: (_, cb) => { cb(undefined, providerResultStub) },
    _blockTracker: {
      getCurrentBlock: () => '0x11b568',
    },
  }
  return new NonceTracker({
    provider,
    getPendingTransactions,
    getConfirmedTransactions,
  })
}