Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions packages/api/src/executor/__tests__/StorageQueryExecutorV2.spec.ts
Original file line number Diff line number Diff line change
@@ -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']]);
});
});
2 changes: 1 addition & 1 deletion packages/api/src/executor/v2/StorageQueryExecutorV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Loading