From acbafd0e39b6b409d2d53c7bbc5984c3101b8a54 Mon Sep 17 00:00:00 2001 From: classicalliu Date: Thu, 15 Aug 2019 17:57:38 +0800 Subject: [PATCH] fix: minBlockNumber select error --- .../src/services/indexer/queue.ts | 2 +- .../neuron-wallet/src/services/sync/utils.ts | 16 +++++++++++++ .../tests/services/sync/utils.test.ts | 24 +++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 packages/neuron-wallet/tests/services/sync/utils.test.ts diff --git a/packages/neuron-wallet/src/services/indexer/queue.ts b/packages/neuron-wallet/src/services/indexer/queue.ts index f503f528d2..3fe8b0e821 100644 --- a/packages/neuron-wallet/src/services/indexer/queue.ts +++ b/packages/neuron-wallet/src/services/indexer/queue.ts @@ -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 } diff --git a/packages/neuron-wallet/src/services/sync/utils.ts b/packages/neuron-wallet/src/services/sync/utils.ts index f52fd1d5b0..eeb6d3cd1f 100644 --- a/packages/neuron-wallet/src/services/sync/utils.ts +++ b/packages/neuron-wallet/src/services/sync/utils.ts @@ -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 + } } diff --git a/packages/neuron-wallet/tests/services/sync/utils.test.ts b/packages/neuron-wallet/tests/services/sync/utils.test.ts new file mode 100644 index 0000000000..212b19b815 --- /dev/null +++ b/packages/neuron-wallet/tests/services/sync/utils.test.ts @@ -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)) + }) + }) +})