aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/nonce-tracker-test.js
blob: 5a27882ef5636753e4ec71435456695a7cc9fd07 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
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('sentry issue 476304902', function () {
      beforeEach(function () {
        const txGen = new MockTxGen()
        pendingTxs = txGen.generate({ status: 'submitted' }, {
          fromNonce: 3,
          count: 29,
        })
        nonceTracker = generateNonceTrackerWith(pendingTxs, [], '0x3')
      })

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

    describe('issue 3670', function () {
      beforeEach(function () {
        const txGen = new MockTxGen()
        pendingTxs = txGen.generate({ status: 'submitted' }, {
          fromNonce: 6,
          count: 3,
        })
        nonceTracker = generateNonceTrackerWith(pendingTxs, [], '0x6')
      })

      it('should return 9', async function () {
        this.timeout(15000)
        const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926')
        assert.equal(nonceLock.nextNonce, '9', `nonce should be 9 got ${nonceLock.nextNonce}`)
        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()
      })
    })

    describe('Faq issue 67', function () {
      beforeEach(function () {
        const txGen = new MockTxGen()
        const confirmedTxs = txGen.generate({ status: 'confirmed' }, { count: 64 })
        const pendingTxs = txGen.generate({
          status: 'submitted',
        }, { count: 10 })
                                         // 0x40 is 64 in hex:
        nonceTracker = generateNonceTrackerWith(pendingTxs, [], '0x40')
      })

      it('should return nonce after network nonce', async function () {
        this.timeout(15000)
        const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926')
        assert.equal(nonceLock.nextNonce, '74', `nonce should be 74 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,
  })
}