Skip to content

Commit

Permalink
fix: minBlockNumber select error
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Aug 15, 2019
1 parent 6be360e commit acbafd0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/services/indexer/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export default class Queue {
.map(state => state.blockNumber)
const uniqueBlockNumbers = [...new Set(blockNumbers)]
const blockNumbersBigInt = uniqueBlockNumbers.map(num => BigInt(num))
const minBlockNumber = blockNumbersBigInt.sort()[0]
const minBlockNumber = Utils.min(blockNumbersBigInt)
return minBlockNumber
}

Expand Down
16 changes: 16 additions & 0 deletions packages/neuron-wallet/src/services/sync/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@ export default class Utils {
}
return result
}

public static min = (array: bigint[]): bigint | undefined => {
let minValue = array[0]
if (!minValue) {
return undefined
}

for (let i = 1; i < array.length; ++i) {
const value = array[i]
if (value < minValue) {
minValue = value
}
}

return minValue
}
}
24 changes: 24 additions & 0 deletions packages/neuron-wallet/tests/services/sync/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Utils from '../../../src/services/sync/utils'

describe('Key tests', () => {
describe('min', () => {
it('with empty array', () => {
const arr: bigint[] = []
const minValue = Utils.min(arr)
expect(minValue).toBe(undefined)
})

it('only one value', () => {
const value = BigInt(1)
const arr = [value]
const minValue = Utils.min(arr)
expect(minValue).toEqual(value)
})

it('two value', () => {
const arr = [BigInt(38050), BigInt(4058)]
const minValue = Utils.min(arr)
expect(minValue).toEqual(BigInt(4058))
})
})
})

0 comments on commit acbafd0

Please sign in to comment.