Skip to content

Commit

Permalink
[gear-idea] Find block with uploaded program or code (#1391)
Browse files Browse the repository at this point in the history
Co-authored-by: sergey filyanin <filianin.sergey@gmail.com>
  • Loading branch information
osipov-mit and sergeyfilyanin authored Sep 12, 2023
1 parent 52a5c19 commit 24c4b34
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 44 deletions.
20 changes: 0 additions & 20 deletions idea/indexer/src/gear/helper.ts

This file was deleted.

1 change: 0 additions & 1 deletion idea/indexer/src/gear/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './indexer';
export * from './connect';
export * from './helper';
80 changes: 62 additions & 18 deletions idea/indexer/src/gear/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,38 @@ export class GearIndexer {
if (extrinsics.length === 0) {
return;
}

for (const tx of extrinsics) {
const event = filterEvents(tx.hash, block, block.events, status).events.find(({ event }) =>
this.api.events.gear.CodeChanged.is(event),
);

if (!event) {
continue;
}

const {
data: { id, change },
} = event.event as CodeChanged;
const codeId = id.toHex();
const metahash = await getMetahash(this.api.code, codeId);

const codeStatus = change.isActive ? CodeStatus.ACTIVE : change.isInactive ? CodeStatus.INACTIVE : null;

this.tempState.addCode(
new Code({
id: codeId,
name: codeId,
genesis: this.genesis,
status: codeStatus,
timestamp: new Date(timestamp),
blockHash: block.block.header.hash.toHex(),
expiration: change.isActive ? change.asActive.expiration.toString() : null,
uploadedBy: tx.signer.inner.toHex(),
metahash,
}),
);
}
}

private async handleProgramExtrinsics(
Expand Down Expand Up @@ -654,31 +686,37 @@ export class GearIndexer {
return null;
}

public async indexBlockWithMissedProgram(programId: HexString): Promise<Program | null> {
const progStorage = (await this.api.query.gearProgram.programStorage(programId)) as Option<IProgram>;

if (progStorage.isSome) {
const blockNumber = progStorage.unwrap()[1].toNumber();

await this.indexMissedBlock(blockNumber);

return this.tempState.getProgram(programId);
}

logger.error('Program not found in storage', { id: programId });
return null;
}

private getProgramWithoutIndexing(id: HexString): Promise<Program> {
return this.tempState.getProgram(id);
}

async findBlockWith(programOrCodeId: HexString, curBlockHash: HexString, with_: 'program' | 'code') {
const bn = await this.api.blocks.getBlockNumber(curBlockHash);
let low = 1;
let high = bn.toNumber();
let mid = 0;
const key = this.api.query.gearProgram[with_ === 'program' ? 'programStorage' : 'codeStorage'].key(programOrCodeId);
while (low <= high) {
mid = Math.round((low + high) / 2);
const blockHash = (await this.api.rpc.chain.getBlockHash(mid)).toHex();
const storage = await this.api.rpc.state.queryStorageAt<Option<any>>([key], blockHash);
if (storage[0].isSome) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return low;
}

private async getProgram(id: HexString, blockHash: HexString, msgId: HexString): Promise<Program> {
let program = await this.tempState.getProgram(id);
if (!program) {
logger.error('Failed to retrieve program', { id, blockHash, msgId });
try {
program = await this.indexBlockWithMissedProgram(id);
const blockWithProgram = await this.findBlockWith(id, blockHash, 'program');
await this.indexMissedBlock(blockWithProgram);
program = await this.tempState.getProgram(id);
} catch (err) {
logger.error('Failed to index block', { method: 'getProgram', blockHash, program: id, msg: msgId });
}
Expand All @@ -689,8 +727,14 @@ export class GearIndexer {
private async getCode(id: HexString, blockHash: HexString, programId: HexString): Promise<Code> {
let code = await this.tempState.getCode(id);
if (!code) {
logger.error('Unable to retrieve code', { id, programId, blockHash });
code = await this.indexBlockWithMissedCode(id);
logger.error('Failed to retrieve code', { id, programId, blockHash });
try {
const blockWithCode = await this.findBlockWith(id, blockHash, 'code');
await this.indexMissedBlock(blockWithCode);
code = await this.tempState.getCode(id);
} catch (err) {
logger.error('Failed to index block', { method: 'getCode', blockHash, code: id, program: programId });
}
}
return code;
}
Expand Down
4 changes: 0 additions & 4 deletions idea/indexer/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { MessageService } from './services';
import { ProgramService } from './services';
import { StateService } from './services';
import { GearIndexer, connectToNode } from './gear';
import { GearHelper } from './gear';

async function bootstrap() {
runHealthcheckServer();
Expand All @@ -23,8 +22,6 @@ async function bootstrap() {

await waitReady();

const helper = new GearHelper();

const blockService = new BlockService(dataSource);
const codeService = new CodeService(dataSource);
const programService = new ProgramService(dataSource);
Expand All @@ -45,7 +42,6 @@ async function bootstrap() {
await rmq.deleteGenesisQueue(genesis);
}
});
helper.initialize(indexer);
}

bootstrap();
2 changes: 1 addition & 1 deletion k8s/gear-node/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:22.10
FROM ubuntu:22.04
MAINTAINER GEAR
WORKDIR opt
RUN apt update
Expand Down

0 comments on commit 24c4b34

Please sign in to comment.