forked from subquery/subql
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP update to node core with blockchain service
- Loading branch information
Showing
22 changed files
with
953 additions
and
1,003 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// Copyright 2020-2025 SubQuery Pte Ltd authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
import { Inject } from '@nestjs/common'; | ||
import { | ||
EthereumHandlerKind, | ||
EthereumRuntimeDataSourceImpl, | ||
isCustomDs, | ||
isRuntimeDs, | ||
SubqlEthereumDataSource, | ||
} from '@subql/common-ethereum'; | ||
import { | ||
DatasourceParams, | ||
Header, | ||
IBlock, | ||
IBlockchainService, | ||
} from '@subql/node-core'; | ||
import { | ||
EthereumBlock, | ||
LightEthereumBlock, | ||
SubqlCustomDatasource, | ||
SubqlCustomHandler, | ||
SubqlMapping, | ||
} from '@subql/types-ethereum'; | ||
import { plainToClass } from 'class-transformer'; | ||
import { validateSync } from 'class-validator'; | ||
import { SubqueryProject } from './configure/SubqueryProject'; | ||
import { EthereumApiService } from './ethereum'; | ||
import SafeEthProvider from './ethereum/safe-api'; | ||
import { calcInterval, ethereumBlockToHeader } from './ethereum/utils.ethereum'; | ||
import { BlockContent, getBlockSize } from './indexer/types'; | ||
import { IIndexerWorker } from './indexer/worker/worker'; | ||
|
||
const BLOCK_TIME_VARIANCE = 5000; | ||
|
||
const INTERVAL_PERCENT = 0.9; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const { version: packageVersion } = require('../package.json'); | ||
|
||
export class BlockchainService | ||
implements | ||
IBlockchainService< | ||
SubqlEthereumDataSource, | ||
SubqlCustomDatasource, | ||
SubqueryProject, | ||
SafeEthProvider, | ||
LightEthereumBlock, | ||
EthereumBlock, | ||
IIndexerWorker | ||
> | ||
{ | ||
blockHandlerKind = EthereumHandlerKind.Block; | ||
isCustomDs = isCustomDs; | ||
isRuntimeDs = isRuntimeDs; | ||
packageVersion = packageVersion; | ||
|
||
constructor(@Inject('APIService') private apiService: EthereumApiService) {} | ||
|
||
async fetchBlocks( | ||
blockNums: number[], | ||
): Promise<IBlock<EthereumBlock>[] | IBlock<LightEthereumBlock>[]> { | ||
return this.apiService.fetchBlocks(blockNums); | ||
} | ||
|
||
async fetchBlockWorker( | ||
worker: IIndexerWorker, | ||
blockNum: number, | ||
context: { workers: IIndexerWorker[] }, | ||
): Promise<Header> { | ||
return worker.fetchBlock(blockNum, context); | ||
} | ||
|
||
getBlockSize(block: IBlock<BlockContent>): number { | ||
return getBlockSize(block.block); | ||
} | ||
|
||
async getFinalizedHeader(): Promise<Header> { | ||
const block = await this.apiService.api.getFinalizedBlock(); | ||
return ethereumBlockToHeader(block); | ||
} | ||
|
||
async getBestHeight(): Promise<number> { | ||
return this.apiService.api.getBestBlockHeight(); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async getChainInterval(): Promise<number> { | ||
const CHAIN_INTERVAL = calcInterval(this.apiService.api) * INTERVAL_PERCENT; | ||
|
||
return Math.min(BLOCK_TIME_VARIANCE, CHAIN_INTERVAL); | ||
} | ||
|
||
async getHeaderForHash(hash: string): Promise<Header> { | ||
const block = await this.apiService.api.getBlockByHeightOrHash(hash); | ||
return ethereumBlockToHeader(block); | ||
} | ||
|
||
async getHeaderForHeight(height: number): Promise<Header> { | ||
const block = await this.apiService.api.getBlockByHeightOrHash(height); | ||
return ethereumBlockToHeader(block); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async getSafeApi(block: BlockContent): Promise<SafeEthProvider> { | ||
return this.apiService.safeApi(block.number); | ||
} | ||
|
||
async getBlockTimestamp(height: number): Promise<Date | undefined> { | ||
const block = await this.apiService.unsafeApi.api.getBlock(height); | ||
|
||
return new Date(block.timestamp * 1000); // TODO test and make sure its in MS not S | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/require-await | ||
async updateDynamicDs( | ||
params: DatasourceParams, | ||
dsObj: SubqlEthereumDataSource | SubqlCustomDatasource, | ||
): Promise<void> { | ||
if (isCustomDs(dsObj)) { | ||
dsObj.processor.options = { | ||
...dsObj.processor.options, | ||
...params.args, | ||
}; | ||
// TODO how to retain this functionality | ||
// await this.dsProcessorService.validateCustomDs([dsObj]); | ||
} else if (isRuntimeDs(dsObj)) { | ||
dsObj.options = { | ||
...dsObj.options, | ||
...params.args, | ||
}; | ||
|
||
const parsedDs = plainToClass(EthereumRuntimeDataSourceImpl, dsObj); | ||
|
||
const errors = validateSync(parsedDs, { | ||
whitelist: true, | ||
forbidNonWhitelisted: false, | ||
}); | ||
if (errors.length) { | ||
throw new Error( | ||
`Dynamic ds is invalid\n${errors | ||
.map((e) => e.toString()) | ||
.join('\n')}`, | ||
); | ||
} | ||
} | ||
// return dsObj; | ||
} | ||
|
||
onProjectChange(project: SubqueryProject): Promise<void> | void { | ||
this.apiService.updateBlockFetching(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 0 additions & 72 deletions
72
packages/node/src/indexer/blockDispatcher/block-dispatcher.service.ts
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
packages/node/src/indexer/blockDispatcher/ethereum-block-dispatcher.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.