Skip to content

Commit

Permalink
feat(idea/squid): optional blockRange params (#1616)
Browse files Browse the repository at this point in the history
  • Loading branch information
osipov-mit authored Aug 13, 2024
1 parent b0e4aa7 commit 04c29c3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 23 deletions.
2 changes: 2 additions & 0 deletions idea/squid/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
2 changes: 1 addition & 1 deletion idea/squid/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ const handler = async (ctx: ProcessorContext<Store>) => {
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,
Expand Down
2 changes: 1 addition & 1 deletion idea/squid/src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof processor>;
export type Block = BlockHeader<Fields>;
Expand Down
56 changes: 35 additions & 21 deletions idea/squid/src/temp-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,6 +50,10 @@ export class TempState {
private messagesToProgram: Map<string, MessageToProgram>;
private events: Map<string, Event>;
private _ctx: ProcessorContext<Store>;
private _metadata: Metadata;
private _registry: TypeRegistry;
private _specVersion: number;
private _programStorageTy: string;

constructor() {
this.programs = new Map();
Expand Down Expand Up @@ -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<any>(ty.lookupName, storage);
const decoded = this._registry.createType<any>(this._programStorageTy, storage);

return decoded.isActive ? decoded.asActive.codeHash.toHex() : '0x';
}
Expand Down

0 comments on commit 04c29c3

Please sign in to comment.