diff --git a/packages/api/src/executor/__tests__/StorageQueryExecutorV2.spec.ts b/packages/api/src/executor/__tests__/StorageQueryExecutorV2.spec.ts new file mode 100644 index 00000000..5d68f00b --- /dev/null +++ b/packages/api/src/executor/__tests__/StorageQueryExecutorV2.spec.ts @@ -0,0 +1,44 @@ +import { BlockHash, PortableRegistry, StorageKey } from '@dedot/codecs'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; +import { QueryableStorage } from '../../storage/QueryableStorage.js'; +import { StorageQueryExecutorV2 } from '../v2/StorageQueryExecutorV2.js'; + +const historicalHash = `0x${'12'.repeat(32)}` as BlockHash; + +const createMockClient = () => ({ + rpcVersion: 'v2' as const, + atBlockHash: historicalHash, + registry: {} as PortableRegistry, +}); + +const createMockEntry = () => + ({ + prefixKey: '0xprefix' as StorageKey, + encodeKey: vi.fn(), + decodeKey: vi.fn((key: StorageKey) => `decoded_${key}`), + decodeValue: vi.fn((value: StorageKey) => `decoded_${value}`), + }) as unknown as QueryableStorage; + +describe('StorageQueryExecutorV2', () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + it('queries map entries at the client historical block', async () => { + const chainHead = { + storage: vi.fn().mockResolvedValue([{ key: '0x01', value: '0x02' }]), + }; + const executor = new StorageQueryExecutorV2(createMockClient() as any, chainHead as any); + const entry = createMockEntry(); + + const methods = (executor as any).exposeStorageMapMethods(entry); + const result = await methods.entries(); + + expect(chainHead.storage).toHaveBeenCalledWith( + [{ type: 'descendantsValues', key: entry.prefixKey }], + undefined, + historicalHash, + ); + expect(result).toEqual([['decoded_0x01', 'decoded_0x02']]); + }); +}); diff --git a/packages/api/src/executor/v2/StorageQueryExecutorV2.ts b/packages/api/src/executor/v2/StorageQueryExecutorV2.ts index 2d4f15b6..50bc7fe6 100644 --- a/packages/api/src/executor/v2/StorageQueryExecutorV2.ts +++ b/packages/api/src/executor/v2/StorageQueryExecutorV2.ts @@ -28,7 +28,7 @@ export class StorageQueryExecutorV2 extends StorageQueryExecutor { const withArgs = !!args && args.length > 0; const key = withArgs ? entry.encodeKey(args, true) : entry.prefixKey; - const results = await this.chainHead.storage([{ type: 'descendantsValues', key }]); + const results = await this.chainHead.storage([{ type: 'descendantsValues', key }], undefined, this.atBlockHash); return results.map(({ key, value }) => [ entry.decodeKey(key as HexString), entry.decodeValue(value as HexString),