From 04c29c39cecd2332177120e46b8e9cdad0773842 Mon Sep 17 00:00:00 2001 From: Dmitryii Osipov Date: Tue, 13 Aug 2024 15:19:18 +0300 Subject: [PATCH] feat(idea/squid): optional blockRange params (#1616) --- idea/squid/src/config.ts | 2 ++ idea/squid/src/main.ts | 2 +- idea/squid/src/processor.ts | 2 +- idea/squid/src/temp-state.ts | 56 ++++++++++++++++++++++-------------- 4 files changed, 39 insertions(+), 23 deletions(-) diff --git a/idea/squid/src/config.ts b/idea/squid/src/config.ts index e018cf38db..3d0aba7a5f 100644 --- a/idea/squid/src/config.ts +++ b/idea/squid/src/config.ts @@ -7,5 +7,7 @@ export const config = { gateway: process.env.ARCHIVE_GATEWAY, rpc: process.env.RPC_ENDPOINT, rate: parseInt(process.env.RATE_LIMIT || '10'), + fromBlock: parseInt(process.env.FROM_BLOCK || '0'), + toBlock: parseInt(process.env.TO_BLOCK) || undefined, }, }; diff --git a/idea/squid/src/main.ts b/idea/squid/src/main.ts index 7b698236b6..6a0da1667b 100644 --- a/idea/squid/src/main.ts +++ b/idea/squid/src/main.ts @@ -124,7 +124,7 @@ const handler = async (ctx: ProcessorContext) => { new Program({ ...common, id, - codeId: await tempState.getCodeId(id, common.blockHash), + codeId: await tempState.getCodeId(id, block.header), owner: null, name: id, status: ProgramStatus.ProgramSet, diff --git a/idea/squid/src/processor.ts b/idea/squid/src/processor.ts index 6e3776e9ec..0e44aec0a0 100644 --- a/idea/squid/src/processor.ts +++ b/idea/squid/src/processor.ts @@ -48,7 +48,7 @@ export const processor = new SubstrateBatchProcessor() timestamp: true, }, }) - .setBlockRange({ from: 11102562 }); + .setBlockRange({ from: config.squid.fromBlock, to: config.squid.toBlock }); export type Fields = SubstrateBatchProcessorFields; export type Block = BlockHeader; diff --git a/idea/squid/src/temp-state.ts b/idea/squid/src/temp-state.ts index 8b9ed25991..9ffb4cfb63 100644 --- a/idea/squid/src/temp-state.ts +++ b/idea/squid/src/temp-state.ts @@ -15,9 +15,14 @@ import { MessageToProgram, Program, } from './model'; -import { ProcessorContext } from './processor'; +import { Block, ProcessorContext } from './processor'; import { MessageStatus } from './common'; +const gearProgramModule = xxhashAsHex('GearProgram', 128); +const programStorageMethod = xxhashAsHex('ProgramStorage', 128); + +const PROGRAM_STORAGE_PREFIX = gearProgramModule + programStorageMethod.slice(2); + function getServiceAndFn(payload: string) { let service: string = null; let name: string = null; @@ -45,6 +50,10 @@ export class TempState { private messagesToProgram: Map; private events: Map; private _ctx: ProcessorContext; + private _metadata: Metadata; + private _registry: TypeRegistry; + private _specVersion: number; + private _programStorageTy: string; constructor() { this.programs = new Map(); @@ -206,35 +215,40 @@ export class TempState { } } - async getCodeId(programId: string, blockhash: string) { - const module = xxhashAsHex('GearProgram', 128); - const method = xxhashAsHex('ProgramStorage', 128); + async getCodeId(programId: string, block: Block) { + const param = PROGRAM_STORAGE_PREFIX + programId.slice(2); + + const calls = [{ method: 'state_getStorage', params: [param, block.hash] }]; - const storagePrefix = module + method.slice(2); + const isMetadataRequired = !this._specVersion || block.specVersion !== this._specVersion; - const param = storagePrefix + programId.slice(2); + if (isMetadataRequired) { + calls.push({ method: 'state_getMetadata', params: [block.hash] }); + } + + const [storage, metadata] = await this._ctx._chain.rpc.batchCall(calls); - const [storage, runtimeMetadata] = await this._ctx._chain.rpc.batchCall([ - { method: 'state_getStorage', params: [param, blockhash] }, - { method: 'state_getMetadata', params: [blockhash] }, - ]); + if (isMetadataRequired) { + this._registry = new TypeRegistry(); + this._metadata = new Metadata(this._registry, metadata); + this._specVersion = block.specVersion; - const registry = new TypeRegistry(); - const meta = new Metadata(registry, runtimeMetadata); + const gearProgramPallet = this._metadata.asLatest.pallets.find(({ name }) => name.toString() === 'GearProgram'); + const programStorage = gearProgramPallet.storage + .unwrap() + .items.find(({ name }) => name.toString() === 'ProgramStorage'); - const gearProgramPallet = meta.asLatest.pallets.find(({ name }) => name.toString() === 'GearProgram'); - const programStorage = gearProgramPallet.storage - .unwrap() - .items.find(({ name }) => name.toString() === 'ProgramStorage'); + const tydef = this._metadata.asLatest.lookup.getTypeDef(programStorage.type.asMap.value); - const ty = meta.asLatest.lookup.getTypeDef(programStorage.type.asMap.value); + this._programStorageTy = tydef.lookupName; - const types = getAllNeccesaryTypes(meta, programStorage.type.asMap.value); + const types = getAllNeccesaryTypes(this._metadata, programStorage.type.asMap.value); - registry.register(types); - registry.setKnownTypes(types); + this._registry.register(types); + this._registry.setKnownTypes(types); + } - const decoded = registry.createType(ty.lookupName, storage); + const decoded = this._registry.createType(this._programStorageTy, storage); return decoded.isActive ? decoded.asActive.codeHash.toHex() : '0x'; }