Skip to content

Commit

Permalink
update: handle indexer requests
Browse files Browse the repository at this point in the history
  • Loading branch information
osipov-mit committed Jul 19, 2023
1 parent 30d75cd commit b43f01d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 42 deletions.
5 changes: 3 additions & 2 deletions idea/api-gateway/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
IRpcRequest,
IRpcResponse,
JSONRPC_ERRORS,
API_GATEWAY_METHODS,
} from '@gear-js/common';
import { nanoid } from 'nanoid';

Expand Down Expand Up @@ -123,11 +124,11 @@ export class Server {
return getResponse(procedure, JSONRPC_ERRORS.MethodNotFound.name);
}

if (method === TEST_BALANCE_METHODS.TEST_BALANCE_AVAILABLE) {
if (method === API_GATEWAY_METHODS.TEST_BALANCE_AVAILABLE) {
return getResponse(procedure, null, this.rmq.isExistTBChannel(params.genesis));
}

if (procedure.method === INDEXER_METHODS.NETWORK_DATA_AVAILABLE) {
if (method === API_GATEWAY_METHODS.NETWORK_DATA_AVAILABLE) {
return getResponse(procedure, null, this.rmq.isExistIndexerChannel(params.genesis));
}

Expand Down
7 changes: 5 additions & 2 deletions idea/common/src/enums/api-methods.enum.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
export enum API_GATEWAY_METHODS {
NETWORK_DATA_AVAILABLE = 'networkData.available',
TEST_BALANCE_AVAILABLE = 'testBalance.available',
}

export enum INDEXER_METHODS {
BLOCKS_STATUS = 'blocks.status',
CODE_ALL = 'code.all',
Expand All @@ -6,7 +11,6 @@ export enum INDEXER_METHODS {
CODE_STATE_GET = 'code.state.get',
MESSAGE_ALL = 'message.all',
MESSAGE_DATA = 'message.data',
NETWORK_DATA_AVAILABLE = 'networkData.available',
PROGRAM_ALL = 'program.all',
PROGRAM_DATA = 'program.data',
PROGRAM_NAME_ADD = 'program.name.add',
Expand All @@ -27,5 +31,4 @@ export enum META_STORAGE_INTERNAL_METHODS {

export enum TEST_BALANCE_METHODS {
TEST_BALANCE_GET = 'testBalance.get',
TEST_BALANCE_AVAILABLE = 'testBalance.available',
}
4 changes: 2 additions & 2 deletions idea/common/src/interfaces/rpc-request/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { INDEXER_METHODS, META_STORAGE_METHODS, TEST_BALANCE_METHODS } from '../../enums';
import { API_GATEWAY_METHODS, INDEXER_METHODS, META_STORAGE_METHODS, TEST_BALANCE_METHODS } from '../../enums';

export * from './indexer';
export * from './meta-storage';
Expand All @@ -7,6 +7,6 @@ export * from './test-balance';
export interface IRpcRequest {
jsonrpc: '2.0';
id: number;
method: INDEXER_METHODS | META_STORAGE_METHODS | TEST_BALANCE_METHODS;
method: INDEXER_METHODS | META_STORAGE_METHODS | TEST_BALANCE_METHODS | API_GATEWAY_METHODS;
params: any;
}
2 changes: 1 addition & 1 deletion idea/indexer/src/one-time-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ async function bootstrap() {
fromBlock = Number(status.height) + 1;
}

const lastBlock = await blockService.getLastBlock(genesis);
const lastBlock = await blockService.getLastBlock({ genesis });
const toBlock = Number(lastBlock.number);

let syncedBlocks = await blockService.getSyncedBlockNumbers(fromBlock, toBlock, genesis);
Expand Down
49 changes: 19 additions & 30 deletions idea/indexer/src/rabbitmq/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
import { Channel, connect, Connection } from 'amqplib';
import {
AddCodeNameParams,
AddProgramNameParams,
AddStateParams,
INDEXER_METHODS,
FindMessageParams,
FindProgramParams,
GetAllCodeParams,
GetAllProgramsParams,
GetAllStateParams,
GetCodeParams,
GetMessagesParams,
GetStateByCodeParams,
GetStateParams,
RabbitMQExchanges,
RabbitMQueues,
RMQServiceActions,
Expand All @@ -29,14 +17,31 @@ export class RMQService {
private mainChannel: Channel;
private topicChannel: Channel;
private connection: Connection;
private methods: Record<INDEXER_METHODS, (params: any) => void>;

constructor(
private blockService?: BlockService,
private codeService?: CodeService,
private messageService?: MessageService,
private programService?: ProgramService,
private stateService?: StateService,
) {}
) {
this.methods = {
[INDEXER_METHODS.BLOCKS_STATUS]: () => this.blockService.getLastBlock.bind(this.blockService),
[INDEXER_METHODS.CODE_ALL]: () => this.codeService.getMany.bind(this.codeService),
[INDEXER_METHODS.CODE_DATA]: () => this.codeService.get.bind(this.codeService),
[INDEXER_METHODS.CODE_NAME_ADD]: () => this.codeService.setName.bind(this.codeService),
[INDEXER_METHODS.CODE_STATE_GET]: () => this.stateService.getByCodeIdAndStateId.bind(this.stateService),
[INDEXER_METHODS.MESSAGE_ALL]: () => this.messageService.getMany.bind(this.messageService),
[INDEXER_METHODS.MESSAGE_DATA]: () => this.messageService.get.bind(this.messageService),
[INDEXER_METHODS.PROGRAM_ALL]: () => this.programService.getAllPrograms.bind(this.programService),
[INDEXER_METHODS.PROGRAM_DATA]: () => this.programService.getWithMessages.bind(this.programService),
[INDEXER_METHODS.PROGRAM_NAME_ADD]: () => this.programService.setName.bind(this.programService),
[INDEXER_METHODS.PROGRAM_STATE_ALL]: () => this.stateService.listByProgramId.bind(this.stateService),
[INDEXER_METHODS.PROGRAM_STATE_ADD]: () => this.stateService.create.bind(this.stateService),
[INDEXER_METHODS.STATE_GET]: () => this.stateService.get.bind(this.stateService),
};
}

public async init(consumeMessages = true): Promise<void> {
this.connection = await connect(config.rabbitmq.url);
Expand Down Expand Up @@ -146,23 +151,7 @@ export class RMQService {

@FormResponse
private async handleIncomingMsg(method: INDEXER_METHODS, params: any): Promise<any> {
const methods = {
[INDEXER_METHODS.BLOCKS_STATUS]: () => this.blockService.getLastBlock(params.genesis as string),
[INDEXER_METHODS.CODE_ALL]: () => this.codeService.getMany(params as GetAllCodeParams),
[INDEXER_METHODS.CODE_DATA]: () => this.codeService.get(params as GetCodeParams),
[INDEXER_METHODS.CODE_NAME_ADD]: () => this.codeService.setName(params as AddCodeNameParams),
[INDEXER_METHODS.CODE_STATE_GET]: () => this.stateService.getByCodeIdAndStateId(params as GetStateByCodeParams),
[INDEXER_METHODS.MESSAGE_ALL]: () => this.messageService.getMany(params as GetMessagesParams),
[INDEXER_METHODS.MESSAGE_DATA]: () => this.messageService.get(params as FindMessageParams),
[INDEXER_METHODS.PROGRAM_ALL]: () => this.programService.getAllPrograms(params as GetAllProgramsParams),
[INDEXER_METHODS.PROGRAM_DATA]: () => this.programService.getWithMessages(params as FindProgramParams),
[INDEXER_METHODS.PROGRAM_NAME_ADD]: () => this.programService.setName(params as AddProgramNameParams),
[INDEXER_METHODS.PROGRAM_STATE_ALL]: () => this.stateService.listByProgramId(params as GetAllStateParams),
[INDEXER_METHODS.PROGRAM_STATE_ADD]: () => this.stateService.create(params as AddStateParams),
[INDEXER_METHODS.STATE_GET]: () => this.stateService.get(params as GetStateParams),
};

return methods[method]();
return this.methods[method](params);
}

public async sendMsgToMetaStorage(metahashes: Map<string, Set<string>>) {
Expand Down
2 changes: 1 addition & 1 deletion idea/indexer/src/services/block.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class BlockService {
this.repo = dataSource.getRepository(Block);
}

public async getLastBlock(genesis: string): Promise<Block> {
public async getLastBlock({ genesis }: { genesis: string }): Promise<Block> {
const [block] = await this.repo.find({
where: {
genesis,
Expand Down
8 changes: 4 additions & 4 deletions idea/tests/src/e2e/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
errorStateAlreadyExists,
unknownNetworkError,
} from './json-rpc.errors';
import { INDEXER_METHODS, META_STORAGE_METHODS, TEST_BALANCE_METHODS } from '@gear-js/common';
import { API_GATEWAY_METHODS, INDEXER_METHODS, META_STORAGE_METHODS, TEST_BALANCE_METHODS } from '@gear-js/common';

let genesis: HexString;
let prepared: IPrepared;
Expand Down Expand Up @@ -71,7 +71,7 @@ afterAll(async () => {
});

describe('Indexer methods', () => {
test(INDEXER_METHODS.NETWORK_DATA_AVAILABLE, async () => {
test(API_GATEWAY_METHODS.NETWORK_DATA_AVAILABLE, async () => {
expect(await networkDataAvailable(genesis)).toBeTruthy();
});

Expand Down Expand Up @@ -214,8 +214,8 @@ describe('Meta storage methods', () => {
test(META_STORAGE_METHODS.CODE_META_GET, () => {});
});

describe.skip('Test balance methods', () => {
test(TEST_BALANCE_METHODS.TEST_BALANCE_AVAILABLE, async () => {
describe('Test balance methods', () => {
test(API_GATEWAY_METHODS.TEST_BALANCE_AVAILABLE, async () => {
expect(await getTestBalance(genesis)).toBeTruthy();
expect(await getTestBalanceSeveralTimesAtATime(genesis)).toBeTruthy();
});
Expand Down

0 comments on commit b43f01d

Please sign in to comment.