Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync main 20240408 #110

Merged
merged 9 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion packages/common-algorand/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"main": "dist/index.js",
"license": "GPL-3.0",
"dependencies": {
"@subql/common": "^3.4.1",
"@subql/common": "^3.5.0",
"@subql/types-algorand": "workspace:*",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@
"@nestjs/event-emitter": "^2.0.0",
"@nestjs/platform-express": "^9.4.0",
"@nestjs/schedule": "^3.0.1",
"@subql/common": "^3.4.1",
"@subql/common": "^3.5.0",
"@subql/common-algorand": "workspace:*",
"@subql/node-core": "^7.4.2",
"@subql/node-core": "^8.0.1",
"@subql/types-algorand": "workspace:*",
"algosdk": "^2.2.0",
"axios": "^1.3.4",
Expand Down
12 changes: 6 additions & 6 deletions packages/node/src/algorand/api.algorand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: GPL-3.0

import { TokenHeader } from '@subql/common-algorand';
import { delay, getLogger } from '@subql/node-core';
import { delay, getLogger, IBlock } from '@subql/node-core';
import {
AlgorandBlock,
AlgorandTransaction,
Expand All @@ -11,7 +11,7 @@ import {
import algosdk, { Indexer } from 'algosdk';
import axios from 'axios';
import { omit } from 'lodash';
import { camelCaseObjectKey } from './utils.algorand';
import { camelCaseObjectKey, formatBlockUtil } from './utils.algorand';

const logger = getLogger('api.algorand');

Expand Down Expand Up @@ -170,7 +170,7 @@ export class AlgorandApi {
getSafeApi(height: number): SafeAPIService {
return new SafeAPIService(this, height, this.endpoint);
}
async fetchBlocks(blockNums: number[]): Promise<AlgorandBlock[]> {
async fetchBlocks(blockNums: number[]): Promise<IBlock<AlgorandBlock>[]> {
let blocks: AlgorandBlock[] = [];

for (let i = 0; i < blockNums.length; i++) {
Expand All @@ -185,14 +185,14 @@ export class AlgorandApi {

blocks = [...blocks, ...fetchedBlocks];

blocks = await Promise.all(
const formattedBlocks = await Promise.all(
blocks.map(async (block) => {
block.hash = await this.getBlockHash(block.round, blocks);
return block;
return formatBlockUtil(block);
}),
);

return blocks;
return formattedBlocks;
}

private blockInCache(number: number): AlgorandBlock {
Expand Down
10 changes: 7 additions & 3 deletions packages/node/src/algorand/api.connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ import {
IApiConnectionSpecific,
NetworkMetadataPayload,
getLogger,
IBlock,
} from '@subql/node-core';
import { BlockContent } from '../indexer/types';
import { AlgorandApi, SafeAPIService } from './api.algorand';

const logger = getLogger('AlgorandApiConnection');

type FetchFunc = (api: AlgorandApi, batch: number[]) => Promise<BlockContent[]>;
type FetchFunc = (
api: AlgorandApi,
batch: number[],
) => Promise<IBlock<BlockContent>[]>;

export class AlgorandApiConnection
implements
IApiConnectionSpecific<AlgorandApi, SafeAPIService, BlockContent[]>
IApiConnectionSpecific<AlgorandApi, SafeAPIService, IBlock<BlockContent>[]>
{
readonly networkMeta: NetworkMetadataPayload;

Expand Down Expand Up @@ -56,7 +60,7 @@ export class AlgorandApiConnection
logger.debug('apiDisconnect is not implemented');
}

async fetchBlocks(heights: number[]): Promise<BlockContent[]> {
async fetchBlocks(heights: number[]): Promise<IBlock<BlockContent>[]> {
const blocks = await this.fetchBlocksBatches(this.unsafeApi, heights);
return blocks;
}
Expand Down
14 changes: 9 additions & 5 deletions packages/node/src/algorand/api.service.algorand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@

import { Inject, Injectable } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { ApiService, ConnectionPoolService, getLogger } from '@subql/node-core';
import {
ApiService,
ConnectionPoolService,
getLogger,
IBlock,
} from '@subql/node-core';
import { SubqueryProject } from '../configure/SubqueryProject';
import { BlockContent } from '../indexer/types';
import { AlgorandApi, SafeAPIService } from './api.algorand';
Expand All @@ -15,7 +20,7 @@ const logger = getLogger('api');
export class AlgorandApiService extends ApiService<
AlgorandApi,
SafeAPIService,
BlockContent[]
IBlock<BlockContent>[]
> {
constructor(
@Inject('ISubqueryProject') private project: SubqueryProject,
Expand All @@ -41,8 +46,7 @@ export class AlgorandApiService extends ApiService<
AlgorandApiConnection.create(endpoint, this.fetchBlockBatches),
//eslint-disable-next-line @typescript-eslint/require-await
async (connection: AlgorandApiConnection) => {
const api = connection.unsafeApi;
return api.getGenesisHash();
return connection.unsafeApi.getGenesisHash();
},
);

Expand All @@ -56,7 +60,7 @@ export class AlgorandApiService extends ApiService<
async fetchBlockBatches(
api: AlgorandApi,
blocks: number[],
): Promise<BlockContent[]> {
): Promise<IBlock<BlockContent>[]> {
return api.fetchBlocks(blocks);
}
}
19 changes: 19 additions & 0 deletions packages/node/src/algorand/utils.algorand.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020-2023 SubQuery Pte Ltd authors & contributors
// SPDX-License-Identifier: GPL-3.0

import { Header, IBlock } from '@subql/node-core';
import {
AlgorandBlock,
AlgorandBlockFilter,
Expand All @@ -9,6 +10,24 @@ import {
} from '@subql/types-algorand';
import { Indexer, TransactionType } from 'algosdk';
import { camelCase, get } from 'lodash';
import { BlockContent } from '../indexer/types';

export function algorandBlockToHeader(block: BlockContent): Header {
return {
blockHeight: block.round,
blockHash: block.round.toString(),
parentHash: block.previousBlockHash,
};
}

export function formatBlockUtil<B extends AlgorandBlock = AlgorandBlock>(
block: B,
): IBlock<B> {
return {
block,
getHeader: () => algorandBlockToHeader(block),
};
}

export function camelCaseObjectKey(object: object) {
if (Array.isArray(object)) {
Expand Down
6 changes: 4 additions & 2 deletions packages/node/src/indexer/api.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ describe('ApiService', () => {

const block = (await apiService.api.fetchBlocks([50000]))[0];

expect(block.hash).toEqual('Gss169f22yVUBJzbNT9qXtQukjh0tgecvapaQY5NIRg=');
expect(block.block.hash).toEqual(
'Gss169f22yVUBJzbNT9qXtQukjh0tgecvapaQY5NIRg=',
);
});

it('waits on pending block to fetch hash', async () => {
Expand All @@ -83,6 +85,6 @@ describe('ApiService', () => {
expect(fetchLatestBlock).not.toThrow();

const block = await fetchLatestBlock();
expect(block.hash).toBeDefined();
expect(block.block.hash).toBeDefined();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// SPDX-License-Identifier: GPL-3.0

import { IBlockDispatcher } from '@subql/node-core';
import { AlgorandBlock } from '@subql/types-algorand';

export interface IAlgorandBlockDispatcher extends IBlockDispatcher {
export interface IAlgorandBlockDispatcher
extends IBlockDispatcher<AlgorandBlock> {
init(onDynamicDsCreated: (height: number) => Promise<void>): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
BlockDispatcher,
ProcessBlockResponse,
IProjectUpgradeService,
IBlock,
} from '@subql/node-core';
import { AlgorandBlock } from '@subql/types-algorand';
import { AlgorandApiService } from '../../algorand';
Expand Down Expand Up @@ -58,9 +59,7 @@ export class BlockDispatcherService
poiSyncService,
project,
dynamicDsService,
async (blockNums: number[]): Promise<AlgorandBlock[]> => {
return this.apiService.fetchBlocks(blockNums);
},
apiService.fetchBlocks.bind(apiService),
);
}

Expand All @@ -69,11 +68,13 @@ export class BlockDispatcherService
}

protected async indexBlock(
block: AlgorandBlock,
block: IBlock<AlgorandBlock>,
): Promise<ProcessBlockResponse> {
return this.indexerManager.indexBlock(
block,
await this.projectService.getDataSources(this.getBlockHeight(block)),
await this.projectService.getDataSources(
this.getBlockHeight(block.block),
jiqiang90 marked this conversation as resolved.
Show resolved Hide resolved
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
InMemoryCacheService,
createIndexerWorker,
} from '@subql/node-core';
import { AlgorandBlock } from '@subql/types-algorand';
import { AlgorandApiConnection } from '../../algorand';
import {
AlgorandProjectDs,
Expand All @@ -33,7 +34,7 @@ type IndexerWorker = IIndexerWorker & {

@Injectable()
export class WorkerBlockDispatcherService
extends WorkerBlockDispatcher<AlgorandProjectDs, IndexerWorker>
extends WorkerBlockDispatcher<AlgorandProjectDs, IndexerWorker, AlgorandBlock>
implements OnApplicationShutdown
{
constructor(
Expand Down Expand Up @@ -90,17 +91,5 @@ export class WorkerBlockDispatcherService
): Promise<void> {
// const start = new Date();
await worker.fetchBlock(height, null);
// const end = new Date();

// const waitTime = end.getTime() - start.getTime();
// if (waitTime > 1000) {
// logger.info(
// `Waiting to fetch block ${height}: ${chalk.red(`${waitTime}ms`)}`,
// );
// } else if (waitTime > 200) {
// logger.info(
// `Waiting to fetch block ${height}: ${chalk.yellow(`${waitTime}ms`)}`,
// );
// }
}
}
33 changes: 0 additions & 33 deletions packages/node/src/indexer/dictionary.service.spec.ts

This file was deleted.

50 changes: 0 additions & 50 deletions packages/node/src/indexer/dictionary.service.ts

This file was deleted.

Loading