From a5da25351373500fcaf564609c8c4efbf6453d71 Mon Sep 17 00:00:00 2001 From: Albrecht <14820950+weichweich@users.noreply.github.com> Date: Tue, 17 Mar 2020 12:01:05 +0100 Subject: [PATCH] fix: include accumulator at stop index (#17) * fix: include accumulator at stop index * test: add tests for getAccumulatorArray Co-authored-by: William --- src/blockchain/Blockchain.spec.ts | 28 ++++++++++++++++++++++++++++ src/blockchain/Blockchain.ts | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/blockchain/Blockchain.spec.ts b/src/blockchain/Blockchain.spec.ts index 662c9bd1..a879b738 100644 --- a/src/blockchain/Blockchain.spec.ts +++ b/src/blockchain/Blockchain.spec.ts @@ -61,8 +61,36 @@ describe('chain mocks', () => { ) expect(latestAccumulator.valueOf()).toBe(newAccumulator.valueOf()) }) + it('Should getAccumulatorArray', async () => { + api.query.portablegabi.accumulatorList.mockResolvedValue( + (stringToHex('currAccumulator') as unknown) as Promise + ) + const currAccumulators = await BlockchainMock.getAccumulatorArray( + dummyAddress, + 1, + 4 + ) + expect(Array.isArray(currAccumulators)).toBeTruthy() + expect(currAccumulators.length).toEqual(4) + }) }) describe('Negative tests', () => { + it('Should throw error for swapped indices when requesting accumulator array', async () => { + api.query.portablegabi.accumulatorList.mockResolvedValue( + (stringToHex('currAccumulator') as unknown) as Promise + ) + expect( + BlockchainMock.getAccumulatorArray(dummyAddress, 4, 1) + ).rejects.toThrowError() + }) + it('Should throw error when accumulator array index out is of range', async () => { + api.query.portablegabi.accumulatorList.mockResolvedValue( + (stringToHex('currAccumulator') as unknown) as Promise + ) + expect( + BlockchainMock.getAccumulatorArray(dummyAddress, 999999999) + ).rejects.toThrowError() + }) it('Should throw for empty accumulatorList (maxIndex === -1)', async () => { const accCount = 0 const requestedIndex = 1 diff --git a/src/blockchain/Blockchain.ts b/src/blockchain/Blockchain.ts index 2d11a863..1e2778d6 100644 --- a/src/blockchain/Blockchain.ts +++ b/src/blockchain/Blockchain.ts @@ -145,7 +145,7 @@ export default class Blockchain implements IBlockchainApi { } const endIndex = _endIndex || (await this.getMaxIndex(address)) // create [[address, startIndex], ..., [address, endIndex]] for multi query - const multiQuery = new Array(endIndex - startIndex) + const multiQuery = new Array(endIndex - startIndex + 1) .fill(startIndex) .map((x, i) => [address, x + i])