From f6619057b86f3fc52b0b507cefb9a81ff68fb3ec Mon Sep 17 00:00:00 2001 From: Ermal Kaleci Date: Fri, 22 Mar 2024 18:35:46 +0100 Subject: [PATCH] fix getKeysPaged (#701) --- .../src/blockchain/get-keys-paged.test.ts | 23 +++++++++++++++++++ packages/core/src/blockchain/storage-layer.ts | 6 ++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/core/src/blockchain/get-keys-paged.test.ts b/packages/core/src/blockchain/get-keys-paged.test.ts index 0e29913e..69852bc2 100644 --- a/packages/core/src/blockchain/get-keys-paged.test.ts +++ b/packages/core/src/blockchain/get-keys-paged.test.ts @@ -18,6 +18,7 @@ describe('getKeysPaged', () => { '0x2222222222222222222222222222222222222222222222222222222222222222_21', '0x2222222222222222222222222222222222222222222222222222222222222222_23', '0x2222222222222222222222222222222222222222222222222222222222222222_26', + '0x94533e05c34400caee0d8976774f0dd064443ba500633e46053c7a0a68b8ef3392a72b59fc8b67b702000001a12dfa1fa4ab9a0000', ] Api.prototype['getKeysPaged'] = vi.fn(async (prefix, pageSize, startKey) => { @@ -253,5 +254,27 @@ describe('getKeysPaged', () => { '0x1111111111111111111111111111111111111111111111111111111111111111', ), ).toEqual([]) + + layer6.setAll([ + [ + '0x94533e05c34400caee0d8976774f0dd064443ba500633e46053c7a0a68b8ef3392a72b59fc8b67b7020000e8d2a526a4a22d1e0300000000', + '0x01', + ], + [ + '0x99971b5749ac43e0235e41b0d37869188ee7418a6531173d60d1f6a82d8f4d5143ce24f679759c60d1cd42f70aeae77f6d6f646c6163612f636470740000000000000000000000000000000000000000d67c5ba80ba065480001', + '0x01', + ], + ]) + + expect( + await layer6.getKeysPaged( + '0x94533e05c34400caee0d8976774f0dd064443ba500633e46053c7a0a68b8ef3392a72b59fc8b67b7020000', + 10, + '0x94533e05c34400caee0d8976774f0dd064443ba500633e46053c7a0a68b8ef3392a72b59fc8b67b7020000', + ), + ).toEqual([ + '0x94533e05c34400caee0d8976774f0dd064443ba500633e46053c7a0a68b8ef3392a72b59fc8b67b702000001a12dfa1fa4ab9a0000', + '0x94533e05c34400caee0d8976774f0dd064443ba500633e46053c7a0a68b8ef3392a72b59fc8b67b7020000e8d2a526a4a22d1e0300000000', + ]) }) }) diff --git a/packages/core/src/blockchain/storage-layer.ts b/packages/core/src/blockchain/storage-layer.ts index 0c32d521..766e914e 100644 --- a/packages/core/src/blockchain/storage-layer.ts +++ b/packages/core/src/blockchain/storage-layer.ts @@ -263,8 +263,8 @@ export class StorageLayer implements StorageLayerProvider { const res: string[] = [] const foundNextKey = (key: string) => { - // make sure keys are unique - if (!res.includes(key)) { + // make sure keys are unique and start with the prefix + if (!res.includes(key) && key.startsWith(prefix)) { res.push(key) } } @@ -285,7 +285,7 @@ export class StorageLayer implements StorageLayerProvider { if (idx !== -1) { if (includeFirst) { const key = this.#keys[idx] - if (key) { + if (key && key.startsWith(prefix)) { foundNextKey(key) } }