Rename variables and fix comments (#243)

* Rename entity to entityType

* Rename variables and fix comments
This commit is contained in:
nikugogoi 2022-11-18 19:16:42 +05:30 committed by GitHub
parent 662c79a5e7
commit a0ba657d17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 58 additions and 62 deletions

View File

@ -233,14 +233,14 @@ export class Database {
async getEntityWithRelations<Entity> ( async getEntityWithRelations<Entity> (
queryRunner: QueryRunner, queryRunner: QueryRunner,
entity: (new () => Entity), entityType: (new () => Entity),
id: string, id: string,
relationsMap: Map<any, { [key: string]: any }>, relationsMap: Map<any, { [key: string]: any }>,
block: BlockHeight = {}, block: BlockHeight = {},
selections: ReadonlyArray<SelectionNode> = [] selections: ReadonlyArray<SelectionNode> = []
): Promise<Entity | undefined> { ): Promise<Entity | undefined> {
let { hash: blockHash, number: blockNumber } = block; let { hash: blockHash, number: blockNumber } = block;
const repo = queryRunner.manager.getRepository(entity); const repo = queryRunner.manager.getRepository(entityType);
const whereOptions: any = { id }; const whereOptions: any = { id };
if (blockNumber) { if (blockNumber) {
@ -268,7 +268,7 @@ export class Database {
// Get relational fields // Get relational fields
if (entityData) { if (entityData) {
entityData = await this.loadEntityRelations(queryRunner, block, relationsMap, entity, entityData, selections); entityData = await this.loadEntityRelations(queryRunner, block, relationsMap, entityType, entityData, selections);
} }
return entityData; return entityData;
@ -278,10 +278,10 @@ export class Database {
queryRunner: QueryRunner, queryRunner: QueryRunner,
block: BlockHeight, block: BlockHeight,
relationsMap: Map<any, { [key: string]: any }>, relationsMap: Map<any, { [key: string]: any }>,
entity: new () => Entity, entityData: any, entityType: new () => Entity, entityData: any,
selections: ReadonlyArray<SelectionNode> = [] selections: ReadonlyArray<SelectionNode> = []
): Promise<Entity> { ): Promise<Entity> {
const relations = relationsMap.get(entity); const relations = relationsMap.get(entityType);
if (relations === undefined) { if (relations === undefined) {
return entityData; return entityData;
} }
@ -364,7 +364,7 @@ export class Database {
async getEntities<Entity> ( async getEntities<Entity> (
queryRunner: QueryRunner, queryRunner: QueryRunner,
entity: new () => Entity, entityType: new () => Entity,
relationsMap: Map<any, { [key: string]: any }>, relationsMap: Map<any, { [key: string]: any }>,
block: BlockHeight = {}, block: BlockHeight = {},
where: Where = {}, where: Where = {},
@ -372,15 +372,15 @@ export class Database {
selections: ReadonlyArray<SelectionNode> = [] selections: ReadonlyArray<SelectionNode> = []
): Promise<Entity[]> { ): Promise<Entity[]> {
let entities: Entity[] = []; let entities: Entity[] = [];
const latestEntity = this._entityToLatestEntityMap.get(entity); const latestEntityType = this._entityToLatestEntityMap.get(entityType);
if (latestEntity) { if (latestEntityType) {
if (Object.keys(block).length) { if (Object.keys(block).length) {
// Use lateral query for entities with latest entity table. // Use lateral query for entities with latest entity table.
entities = await this.getEntitiesLateral( entities = await this.getEntitiesLateral(
queryRunner, queryRunner,
entity, entityType,
latestEntity, latestEntityType,
block, block,
where, where,
queryOptions queryOptions
@ -389,8 +389,8 @@ export class Database {
// Use latest entity tables if block height not passed. // Use latest entity tables if block height not passed.
entities = await this.getEntitiesLatest( entities = await this.getEntitiesLatest(
queryRunner, queryRunner,
entity, entityType,
latestEntity, latestEntityType,
where, where,
queryOptions, queryOptions,
selections selections
@ -398,23 +398,23 @@ export class Database {
} }
} else { } else {
// Use different suitable query patterns based on entities. // Use different suitable query patterns based on entities.
switch (this._entityQueryTypeMap.get(entity)) { switch (this._entityQueryTypeMap.get(entityType)) {
case ENTITY_QUERY_TYPE.SINGULAR: case ENTITY_QUERY_TYPE.SINGULAR:
entities = await this.getEntitiesSingular(queryRunner, entity, block, where); entities = await this.getEntitiesSingular(queryRunner, entityType, block, where);
break; break;
case ENTITY_QUERY_TYPE.UNIQUE: case ENTITY_QUERY_TYPE.UNIQUE:
entities = await this.getEntitiesUnique(queryRunner, entity, block, where, queryOptions); entities = await this.getEntitiesUnique(queryRunner, entityType, block, where, queryOptions);
break; break;
case ENTITY_QUERY_TYPE.DISTINCT_ON: case ENTITY_QUERY_TYPE.DISTINCT_ON:
entities = await this.getEntitiesDistinctOn(queryRunner, entity, block, where, queryOptions); entities = await this.getEntitiesDistinctOn(queryRunner, entityType, block, where, queryOptions);
break; break;
case ENTITY_QUERY_TYPE.GROUP_BY: case ENTITY_QUERY_TYPE.GROUP_BY:
default: default:
// Use group by query if entity query type is not specified in map. // Use group by query if entity query type is not specified in map.
entities = await this.getEntitiesGroupBy(queryRunner, entity, block, where, queryOptions); entities = await this.getEntitiesGroupBy(queryRunner, entityType, block, where, queryOptions);
break; break;
} }
} }
@ -423,7 +423,7 @@ export class Database {
return []; return [];
} }
entities = await this.loadEntitiesRelations(queryRunner, block, relationsMap, entity, entities, selections); entities = await this.loadEntitiesRelations(queryRunner, block, relationsMap, entityType, entities, selections);
// Resolve any field name conflicts in the entity result. // Resolve any field name conflicts in the entity result.
entities = entities.map(entity => resolveEntityFieldConflicts(entity)); entities = entities.map(entity => resolveEntityFieldConflicts(entity));
@ -432,12 +432,12 @@ export class Database {
async getEntitiesGroupBy<Entity> ( async getEntitiesGroupBy<Entity> (
queryRunner: QueryRunner, queryRunner: QueryRunner,
entity: new () => Entity, entityType: new () => Entity,
block: BlockHeight, block: BlockHeight,
where: Where = {}, where: Where = {},
queryOptions: QueryOptions = {} queryOptions: QueryOptions = {}
): Promise<Entity[]> { ): Promise<Entity[]> {
const repo = queryRunner.manager.getRepository(entity); const repo = queryRunner.manager.getRepository(entityType);
const { tableName } = repo.metadata; const { tableName } = repo.metadata;
let subQuery = repo.createQueryBuilder('subTable') let subQuery = repo.createQueryBuilder('subTable')
@ -496,12 +496,12 @@ export class Database {
async getEntitiesDistinctOn<Entity> ( async getEntitiesDistinctOn<Entity> (
queryRunner: QueryRunner, queryRunner: QueryRunner,
entity: new () => Entity, entityType: new () => Entity,
block: BlockHeight, block: BlockHeight,
where: Where = {}, where: Where = {},
queryOptions: QueryOptions = {} queryOptions: QueryOptions = {}
): Promise<Entity[]> { ): Promise<Entity[]> {
const repo = queryRunner.manager.getRepository(entity); const repo = queryRunner.manager.getRepository(entityType);
let subQuery = repo.createQueryBuilder('subTable') let subQuery = repo.createQueryBuilder('subTable')
.distinctOn(['subTable.id']) .distinctOn(['subTable.id'])
@ -560,11 +560,11 @@ export class Database {
async getEntitiesSingular<Entity> ( async getEntitiesSingular<Entity> (
queryRunner: QueryRunner, queryRunner: QueryRunner,
entity: new () => Entity, entityType: new () => Entity,
block: BlockHeight, block: BlockHeight,
where: Where = {} where: Where = {}
): Promise<Entity[]> { ): Promise<Entity[]> {
const repo = queryRunner.manager.getRepository(entity); const repo = queryRunner.manager.getRepository(entityType);
const { tableName } = repo.metadata; const { tableName } = repo.metadata;
let selectQueryBuilder = repo.createQueryBuilder(tableName) let selectQueryBuilder = repo.createQueryBuilder(tableName)
@ -595,12 +595,12 @@ export class Database {
async getEntitiesUnique<Entity> ( async getEntitiesUnique<Entity> (
queryRunner: QueryRunner, queryRunner: QueryRunner,
entity: new () => Entity, entityType: new () => Entity,
block: BlockHeight, block: BlockHeight,
where: Where = {}, where: Where = {},
queryOptions: QueryOptions = {} queryOptions: QueryOptions = {}
): Promise<Entity[]> { ): Promise<Entity[]> {
const repo = queryRunner.manager.getRepository(entity); const repo = queryRunner.manager.getRepository(entityType);
const { tableName } = repo.metadata; const { tableName } = repo.metadata;
let selectQueryBuilder = repo.createQueryBuilder(tableName) let selectQueryBuilder = repo.createQueryBuilder(tableName)
@ -643,13 +643,13 @@ export class Database {
async getEntitiesLatest<Entity> ( async getEntitiesLatest<Entity> (
queryRunner: QueryRunner, queryRunner: QueryRunner,
entity: new () => Entity, entityType: new () => Entity,
latestEntity: new () => any, latestEntity: new () => any,
where: Where = {}, where: Where = {},
queryOptions: QueryOptions = {}, queryOptions: QueryOptions = {},
selections: ReadonlyArray<SelectionNode> = [] selections: ReadonlyArray<SelectionNode> = []
): Promise<Entity[]> { ): Promise<Entity[]> {
const entityRepo = queryRunner.manager.getRepository(entity); const entityRepo = queryRunner.manager.getRepository(entityType);
const latestEntityRepo = queryRunner.manager.getRepository(latestEntity); const latestEntityRepo = queryRunner.manager.getRepository(latestEntity);
const latestEntityFields = latestEntityRepo.metadata.columns.map(column => column.propertyName); const latestEntityFields = latestEntityRepo.metadata.columns.map(column => column.propertyName);
@ -697,13 +697,13 @@ export class Database {
async getEntitiesLateral<Entity> ( async getEntitiesLateral<Entity> (
queryRunner: QueryRunner, queryRunner: QueryRunner,
entity: new () => Entity, entityType: new () => Entity,
latestEntity: new () => any, latestEntity: new () => any,
block: BlockHeight, block: BlockHeight,
where: Where = {}, where: Where = {},
queryOptions: QueryOptions = {} queryOptions: QueryOptions = {}
): Promise<Entity[]> { ): Promise<Entity[]> {
const entityRepo = queryRunner.manager.getRepository(entity); const entityRepo = queryRunner.manager.getRepository(entityType);
const latestEntityRepo = queryRunner.manager.getRepository(latestEntity); const latestEntityRepo = queryRunner.manager.getRepository(latestEntity);
let subQuery = entityRepo.createQueryBuilder('subTable') let subQuery = entityRepo.createQueryBuilder('subTable')
@ -937,16 +937,16 @@ export class Database {
}); });
} }
async saveEntity (entity: string, data: any): Promise<any> { async saveEntity (entityType: string, data: any): Promise<any> {
const repo = this._conn.getRepository(entity); const repo = this._conn.getRepository(entityType);
const dbEntity: any = repo.create(data); const dbEntity: any = repo.create(data);
return repo.save(dbEntity); return repo.save(dbEntity);
} }
async toGraphEntity (instanceExports: any, entity: string, data: any, entityTypes: { [key: string]: string }): Promise<any> { async toGraphEntity (instanceExports: any, entityName: string, data: any, entityTypes: { [key: string]: string }): Promise<any> {
// TODO: Cache schema/columns. // TODO: Cache schema/columns.
const repo = this._conn.getRepository(entity); const repo = this._conn.getRepository(entityName);
const entityFields = repo.metadata.columns; const entityFields = repo.metadata.columns;
const { Entity } = instanceExports; const { Entity } = instanceExports;
@ -977,9 +977,9 @@ export class Database {
return entityInstance; return entityInstance;
} }
async fromGraphEntity (instanceExports: any, block: Block, entity: string, entityInstance: any): Promise<{ [key: string]: any } > { async fromGraphEntity (instanceExports: any, block: Block, entityName: string, entityInstance: any): Promise<{ [key: string]: any } > {
// TODO: Cache schema/columns. // TODO: Cache schema/columns.
const repo = this._conn.getRepository(entity); const repo = this._conn.getRepository(entityName);
const entityFields = repo.metadata.columns; const entityFields = repo.metadata.columns;
return this.getEntityValues(instanceExports, block, entityInstance, entityFields); return this.getEntityValues(instanceExports, block, entityInstance, entityFields);
@ -1021,8 +1021,8 @@ export class Database {
}, {}); }, {});
} }
fromState (block: BlockProgressInterface, entity: string, stateEntity: any, relations: { [key: string]: any } = {}): any { fromState (block: BlockProgressInterface, entityName: string, stateEntity: any, relations: { [key: string]: any } = {}): any {
const repo = this._conn.getRepository(entity); const repo = this._conn.getRepository(entityName);
const entityFields = repo.metadata.columns; const entityFields = repo.metadata.columns;
return this.getStateEntityValues(block, stateEntity, entityFields, relations); return this.getStateEntityValues(block, stateEntity, entityFields, relations);
@ -1164,8 +1164,8 @@ export class Database {
return transformer.transform(rawResults, qb.expressionMap.mainAlias); return transformer.transform(rawResults, qb.expressionMap.mainAlias);
} }
async updateEntity<Entity> (queryRunner: QueryRunner, entity: new () => Entity, criteria: any, update: any): Promise<UpdateResult> { async updateEntity<Entity> (queryRunner: QueryRunner, entityType: new () => Entity, criteria: any, update: any): Promise<UpdateResult> {
const repo = queryRunner.manager.getRepository(entity); const repo = queryRunner.manager.getRepository(entityType);
return repo.createQueryBuilder() return repo.createQueryBuilder()
.update() .update()
.set(update) .set(update)

View File

@ -11,7 +11,9 @@ import {
ContractInterface, ContractInterface,
StateStatus, StateStatus,
StateSyncStatusInterface, StateSyncStatusInterface,
StateInterface StateInterface,
getResultEvent,
ResultEvent
} from '@cerc-io/util'; } from '@cerc-io/util';
import { EthClient } from '@cerc-io/ipld-eth-client'; import { EthClient } from '@cerc-io/ipld-eth-client';
import { GetStorageAt, getStorageValue, MappingKey, StorageLayout } from '@cerc-io/solidity-mapper'; import { GetStorageAt, getStorageValue, MappingKey, StorageLayout } from '@cerc-io/solidity-mapper';
@ -36,6 +38,10 @@ export class Indexer implements IndexerInterface {
return this._storageLayoutMap; return this._storageLayoutMap;
} }
getResultEvent (event: EventInterface): ResultEvent {
return getResultEvent(event);
}
async getStorageValue (storageLayout: StorageLayout, blockHash: string, contractAddress: string, variable: string, ...mappingKeys: MappingKey[]): Promise<ValueResult> { async getStorageValue (storageLayout: StorageLayout, blockHash: string, contractAddress: string, variable: string, ...mappingKeys: MappingKey[]): Promise<ValueResult> {
return getStorageValue( return getStorageValue(
storageLayout, storageLayout,

View File

@ -30,7 +30,7 @@ export interface PrefetchedBlock {
* @param jobQueue * @param jobQueue
* @param blockNumber * @param blockNumber
*/ */
export const processBlockByNumberWithCache = async ( export const processBlockByNumber = async (
jobQueue: JobQueue, jobQueue: JobQueue,
blockNumber: number blockNumber: number
): Promise<void> => { ): Promise<void> => {
@ -247,11 +247,9 @@ export const processBatchEvents = async (indexer: IndexerInterface, block: Block
console.time('time:common#processBatchEvents-processing_events_batch'); console.time('time:common#processBatchEvents-processing_events_batch');
for (let event of events) {
// Process events in loop // Process events in loop
for (let event of events) {
const eventIndex = event.index; const eventIndex = event.index;
// log(`Processing event ${event.id} index ${eventIndex}`);
// Check that events are processed in order. // Check that events are processed in order.
if (eventIndex <= block.lastProcessedEventIndex) { if (eventIndex <= block.lastProcessedEventIndex) {
@ -269,14 +267,7 @@ export const processBatchEvents = async (indexer: IndexerInterface, block: Block
} }
} }
let watchedContract; const watchedContract = indexer.isWatchedContract(event.contract);
if (!indexer.isWatchedContract) {
// uni-info-watcher indexer doesn't have watched contracts implementation.
watchedContract = true;
} else {
watchedContract = indexer.isWatchedContract(event.contract);
}
if (watchedContract) { if (watchedContract) {
// We might not have parsed this event yet. This can happen if the contract was added // We might not have parsed this event yet. This can happen if the contract was added

View File

@ -11,10 +11,9 @@ import { EthClient } from '@cerc-io/ipld-eth-client';
import { JobQueue } from './job-queue'; import { JobQueue } from './job-queue';
import { BlockProgressInterface, EventInterface, IndexerInterface } from './types'; import { BlockProgressInterface, EventInterface, IndexerInterface } from './types';
import { MAX_REORG_DEPTH, JOB_KIND_PRUNE, JOB_KIND_INDEX, UNKNOWN_EVENT_NAME, JOB_KIND_EVENTS, QUEUE_BLOCK_PROCESSING, QUEUE_EVENT_PROCESSING } from './constants'; import { MAX_REORG_DEPTH, JOB_KIND_PRUNE, JOB_KIND_INDEX, UNKNOWN_EVENT_NAME, JOB_KIND_EVENTS, QUEUE_BLOCK_PROCESSING, QUEUE_EVENT_PROCESSING } from './constants';
import { createPruningJob, processBlockByNumberWithCache } from './common'; import { createPruningJob, processBlockByNumber } from './common';
import { UpstreamConfig } from './config'; import { UpstreamConfig } from './config';
import { OrderDirection } from './database'; import { OrderDirection } from './database';
import { getResultEvent } from './misc';
const EVENT = 'event'; const EVENT = 'event';
@ -65,8 +64,7 @@ export class EventWatcher {
startBlockNumber = syncStatus.chainHeadBlockNumber + 1; startBlockNumber = syncStatus.chainHeadBlockNumber + 1;
} }
// Wait for block processing as blockProgress event might process the same block. await processBlockByNumber(this._jobQueue, startBlockNumber);
await processBlockByNumberWithCache(this._jobQueue, startBlockNumber);
// Creating an AsyncIterable from AsyncIterator to iterate over the values. // Creating an AsyncIterable from AsyncIterator to iterate over the values.
// https://www.codementor.io/@tiagolopesferreira/asynchronous-iterators-in-javascript-jl1yg8la1#for-wait-of // https://www.codementor.io/@tiagolopesferreira/asynchronous-iterators-in-javascript-jl1yg8la1#for-wait-of
@ -81,7 +79,7 @@ export class EventWatcher {
const { onBlockProgressEvent: { blockNumber, isComplete } } = data; const { onBlockProgressEvent: { blockNumber, isComplete } } = data;
if (isComplete) { if (isComplete) {
await processBlockByNumberWithCache(this._jobQueue, blockNumber + 1); await processBlockByNumber(this._jobQueue, blockNumber + 1);
} }
} }
} }
@ -181,7 +179,7 @@ export class EventWatcher {
async publishEventToSubscribers (dbEvent: EventInterface, timeElapsedInSeconds: number): Promise<void> { async publishEventToSubscribers (dbEvent: EventInterface, timeElapsedInSeconds: number): Promise<void> {
if (dbEvent && dbEvent.eventName !== UNKNOWN_EVENT_NAME) { if (dbEvent && dbEvent.eventName !== UNKNOWN_EVENT_NAME) {
const resultEvent = getResultEvent(dbEvent); const resultEvent = this._indexer.getResultEvent(dbEvent);
log(`pushing event to GQL subscribers (${timeElapsedInSeconds}s elapsed): ${resultEvent.event.__typename}`); log(`pushing event to GQL subscribers (${timeElapsedInSeconds}s elapsed): ${resultEvent.event.__typename}`);

View File

@ -7,7 +7,7 @@ import debug from 'debug';
import { JobQueue } from './job-queue'; import { JobQueue } from './job-queue';
import { EventWatcherInterface, IndexerInterface } from './types'; import { EventWatcherInterface, IndexerInterface } from './types';
import { wait } from './misc'; import { wait } from './misc';
import { processBlockByNumberWithCache } from './common'; import { processBlockByNumber } from './common';
import { DEFAULT_PREFETCH_BATCH_SIZE } from './constants'; import { DEFAULT_PREFETCH_BATCH_SIZE } from './constants';
const log = debug('vulcanize:fill'); const log = debug('vulcanize:fill');
@ -58,7 +58,7 @@ export const fillBlocks = async (
const numberOfBlocks = endBlock - startBlock + 1; const numberOfBlocks = endBlock - startBlock + 1;
processBlockByNumberWithCache(jobQueue, startBlock); processBlockByNumber(jobQueue, startBlock);
// Creating an AsyncIterable from AsyncIterator to iterate over the values. // Creating an AsyncIterable from AsyncIterator to iterate over the values.
// https://www.codementor.io/@tiagolopesferreira/asynchronous-iterators-in-javascript-jl1yg8la1#for-wait-of // https://www.codementor.io/@tiagolopesferreira/asynchronous-iterators-in-javascript-jl1yg8la1#for-wait-of
@ -79,7 +79,7 @@ export const fillBlocks = async (
const completePercentage = Math.round(blocksProcessed / numberOfBlocks * 100); const completePercentage = Math.round(blocksProcessed / numberOfBlocks * 100);
log(`Processed ${blocksProcessed} of ${numberOfBlocks} blocks (${completePercentage}%)`); log(`Processed ${blocksProcessed} of ${numberOfBlocks} blocks (${completePercentage}%)`);
await processBlockByNumberWithCache(jobQueue, blockNumber + 1); await processBlockByNumber(jobQueue, blockNumber + 1);
if (blockNumber + 1 >= endBlock) { if (blockNumber + 1 >= endBlock) {
// Break the async loop when blockProgress event is for the endBlock and processing is complete. // Break the async loop when blockProgress event is for the endBlock and processing is complete.

View File

@ -121,6 +121,7 @@ export interface IndexerInterface {
updateStateStatusMap (address: string, stateStatus: StateStatus): void updateStateStatusMap (address: string, stateStatus: StateStatus): void
getStateData (state: StateInterface): any getStateData (state: StateInterface): any
resetWatcherToBlock (blockNumber: number): Promise<void> resetWatcherToBlock (blockNumber: number): Promise<void>
getResultEvent (event: EventInterface): any
} }
export interface EventWatcherInterface { export interface EventWatcherInterface {