mirror of
https://github.com/cerc-io/watcher-ts
synced 2025-01-21 10:39:06 +00:00
Rename variables and fix comments (#243)
* Rename entity to entityType * Rename variables and fix comments
This commit is contained in:
parent
662c79a5e7
commit
a0ba657d17
@ -233,14 +233,14 @@ export class Database {
|
||||
|
||||
async getEntityWithRelations<Entity> (
|
||||
queryRunner: QueryRunner,
|
||||
entity: (new () => Entity),
|
||||
entityType: (new () => Entity),
|
||||
id: string,
|
||||
relationsMap: Map<any, { [key: string]: any }>,
|
||||
block: BlockHeight = {},
|
||||
selections: ReadonlyArray<SelectionNode> = []
|
||||
): Promise<Entity | undefined> {
|
||||
let { hash: blockHash, number: blockNumber } = block;
|
||||
const repo = queryRunner.manager.getRepository(entity);
|
||||
const repo = queryRunner.manager.getRepository(entityType);
|
||||
const whereOptions: any = { id };
|
||||
|
||||
if (blockNumber) {
|
||||
@ -268,7 +268,7 @@ export class Database {
|
||||
|
||||
// Get relational fields
|
||||
if (entityData) {
|
||||
entityData = await this.loadEntityRelations(queryRunner, block, relationsMap, entity, entityData, selections);
|
||||
entityData = await this.loadEntityRelations(queryRunner, block, relationsMap, entityType, entityData, selections);
|
||||
}
|
||||
|
||||
return entityData;
|
||||
@ -278,10 +278,10 @@ export class Database {
|
||||
queryRunner: QueryRunner,
|
||||
block: BlockHeight,
|
||||
relationsMap: Map<any, { [key: string]: any }>,
|
||||
entity: new () => Entity, entityData: any,
|
||||
entityType: new () => Entity, entityData: any,
|
||||
selections: ReadonlyArray<SelectionNode> = []
|
||||
): Promise<Entity> {
|
||||
const relations = relationsMap.get(entity);
|
||||
const relations = relationsMap.get(entityType);
|
||||
if (relations === undefined) {
|
||||
return entityData;
|
||||
}
|
||||
@ -364,7 +364,7 @@ export class Database {
|
||||
|
||||
async getEntities<Entity> (
|
||||
queryRunner: QueryRunner,
|
||||
entity: new () => Entity,
|
||||
entityType: new () => Entity,
|
||||
relationsMap: Map<any, { [key: string]: any }>,
|
||||
block: BlockHeight = {},
|
||||
where: Where = {},
|
||||
@ -372,15 +372,15 @@ export class Database {
|
||||
selections: ReadonlyArray<SelectionNode> = []
|
||||
): Promise<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) {
|
||||
// Use lateral query for entities with latest entity table.
|
||||
entities = await this.getEntitiesLateral(
|
||||
queryRunner,
|
||||
entity,
|
||||
latestEntity,
|
||||
entityType,
|
||||
latestEntityType,
|
||||
block,
|
||||
where,
|
||||
queryOptions
|
||||
@ -389,8 +389,8 @@ export class Database {
|
||||
// Use latest entity tables if block height not passed.
|
||||
entities = await this.getEntitiesLatest(
|
||||
queryRunner,
|
||||
entity,
|
||||
latestEntity,
|
||||
entityType,
|
||||
latestEntityType,
|
||||
where,
|
||||
queryOptions,
|
||||
selections
|
||||
@ -398,23 +398,23 @@ export class Database {
|
||||
}
|
||||
} else {
|
||||
// Use different suitable query patterns based on entities.
|
||||
switch (this._entityQueryTypeMap.get(entity)) {
|
||||
switch (this._entityQueryTypeMap.get(entityType)) {
|
||||
case ENTITY_QUERY_TYPE.SINGULAR:
|
||||
entities = await this.getEntitiesSingular(queryRunner, entity, block, where);
|
||||
entities = await this.getEntitiesSingular(queryRunner, entityType, block, where);
|
||||
break;
|
||||
|
||||
case ENTITY_QUERY_TYPE.UNIQUE:
|
||||
entities = await this.getEntitiesUnique(queryRunner, entity, block, where, queryOptions);
|
||||
entities = await this.getEntitiesUnique(queryRunner, entityType, block, where, queryOptions);
|
||||
break;
|
||||
|
||||
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;
|
||||
|
||||
case ENTITY_QUERY_TYPE.GROUP_BY:
|
||||
default:
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
@ -423,7 +423,7 @@ export class Database {
|
||||
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.
|
||||
entities = entities.map(entity => resolveEntityFieldConflicts(entity));
|
||||
|
||||
@ -432,12 +432,12 @@ export class Database {
|
||||
|
||||
async getEntitiesGroupBy<Entity> (
|
||||
queryRunner: QueryRunner,
|
||||
entity: new () => Entity,
|
||||
entityType: new () => Entity,
|
||||
block: BlockHeight,
|
||||
where: Where = {},
|
||||
queryOptions: QueryOptions = {}
|
||||
): Promise<Entity[]> {
|
||||
const repo = queryRunner.manager.getRepository(entity);
|
||||
const repo = queryRunner.manager.getRepository(entityType);
|
||||
const { tableName } = repo.metadata;
|
||||
|
||||
let subQuery = repo.createQueryBuilder('subTable')
|
||||
@ -496,12 +496,12 @@ export class Database {
|
||||
|
||||
async getEntitiesDistinctOn<Entity> (
|
||||
queryRunner: QueryRunner,
|
||||
entity: new () => Entity,
|
||||
entityType: new () => Entity,
|
||||
block: BlockHeight,
|
||||
where: Where = {},
|
||||
queryOptions: QueryOptions = {}
|
||||
): Promise<Entity[]> {
|
||||
const repo = queryRunner.manager.getRepository(entity);
|
||||
const repo = queryRunner.manager.getRepository(entityType);
|
||||
|
||||
let subQuery = repo.createQueryBuilder('subTable')
|
||||
.distinctOn(['subTable.id'])
|
||||
@ -560,11 +560,11 @@ export class Database {
|
||||
|
||||
async getEntitiesSingular<Entity> (
|
||||
queryRunner: QueryRunner,
|
||||
entity: new () => Entity,
|
||||
entityType: new () => Entity,
|
||||
block: BlockHeight,
|
||||
where: Where = {}
|
||||
): Promise<Entity[]> {
|
||||
const repo = queryRunner.manager.getRepository(entity);
|
||||
const repo = queryRunner.manager.getRepository(entityType);
|
||||
const { tableName } = repo.metadata;
|
||||
|
||||
let selectQueryBuilder = repo.createQueryBuilder(tableName)
|
||||
@ -595,12 +595,12 @@ export class Database {
|
||||
|
||||
async getEntitiesUnique<Entity> (
|
||||
queryRunner: QueryRunner,
|
||||
entity: new () => Entity,
|
||||
entityType: new () => Entity,
|
||||
block: BlockHeight,
|
||||
where: Where = {},
|
||||
queryOptions: QueryOptions = {}
|
||||
): Promise<Entity[]> {
|
||||
const repo = queryRunner.manager.getRepository(entity);
|
||||
const repo = queryRunner.manager.getRepository(entityType);
|
||||
const { tableName } = repo.metadata;
|
||||
|
||||
let selectQueryBuilder = repo.createQueryBuilder(tableName)
|
||||
@ -643,13 +643,13 @@ export class Database {
|
||||
|
||||
async getEntitiesLatest<Entity> (
|
||||
queryRunner: QueryRunner,
|
||||
entity: new () => Entity,
|
||||
entityType: new () => Entity,
|
||||
latestEntity: new () => any,
|
||||
where: Where = {},
|
||||
queryOptions: QueryOptions = {},
|
||||
selections: ReadonlyArray<SelectionNode> = []
|
||||
): Promise<Entity[]> {
|
||||
const entityRepo = queryRunner.manager.getRepository(entity);
|
||||
const entityRepo = queryRunner.manager.getRepository(entityType);
|
||||
const latestEntityRepo = queryRunner.manager.getRepository(latestEntity);
|
||||
const latestEntityFields = latestEntityRepo.metadata.columns.map(column => column.propertyName);
|
||||
|
||||
@ -697,13 +697,13 @@ export class Database {
|
||||
|
||||
async getEntitiesLateral<Entity> (
|
||||
queryRunner: QueryRunner,
|
||||
entity: new () => Entity,
|
||||
entityType: new () => Entity,
|
||||
latestEntity: new () => any,
|
||||
block: BlockHeight,
|
||||
where: Where = {},
|
||||
queryOptions: QueryOptions = {}
|
||||
): Promise<Entity[]> {
|
||||
const entityRepo = queryRunner.manager.getRepository(entity);
|
||||
const entityRepo = queryRunner.manager.getRepository(entityType);
|
||||
const latestEntityRepo = queryRunner.manager.getRepository(latestEntity);
|
||||
|
||||
let subQuery = entityRepo.createQueryBuilder('subTable')
|
||||
@ -937,16 +937,16 @@ export class Database {
|
||||
});
|
||||
}
|
||||
|
||||
async saveEntity (entity: string, data: any): Promise<any> {
|
||||
const repo = this._conn.getRepository(entity);
|
||||
async saveEntity (entityType: string, data: any): Promise<any> {
|
||||
const repo = this._conn.getRepository(entityType);
|
||||
|
||||
const dbEntity: any = repo.create(data);
|
||||
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.
|
||||
const repo = this._conn.getRepository(entity);
|
||||
const repo = this._conn.getRepository(entityName);
|
||||
const entityFields = repo.metadata.columns;
|
||||
|
||||
const { Entity } = instanceExports;
|
||||
@ -977,9 +977,9 @@ export class Database {
|
||||
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.
|
||||
const repo = this._conn.getRepository(entity);
|
||||
const repo = this._conn.getRepository(entityName);
|
||||
const entityFields = repo.metadata.columns;
|
||||
|
||||
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 {
|
||||
const repo = this._conn.getRepository(entity);
|
||||
fromState (block: BlockProgressInterface, entityName: string, stateEntity: any, relations: { [key: string]: any } = {}): any {
|
||||
const repo = this._conn.getRepository(entityName);
|
||||
const entityFields = repo.metadata.columns;
|
||||
|
||||
return this.getStateEntityValues(block, stateEntity, entityFields, relations);
|
||||
@ -1164,8 +1164,8 @@ export class Database {
|
||||
return transformer.transform(rawResults, qb.expressionMap.mainAlias);
|
||||
}
|
||||
|
||||
async updateEntity<Entity> (queryRunner: QueryRunner, entity: new () => Entity, criteria: any, update: any): Promise<UpdateResult> {
|
||||
const repo = queryRunner.manager.getRepository(entity);
|
||||
async updateEntity<Entity> (queryRunner: QueryRunner, entityType: new () => Entity, criteria: any, update: any): Promise<UpdateResult> {
|
||||
const repo = queryRunner.manager.getRepository(entityType);
|
||||
return repo.createQueryBuilder()
|
||||
.update()
|
||||
.set(update)
|
||||
|
@ -11,7 +11,9 @@ import {
|
||||
ContractInterface,
|
||||
StateStatus,
|
||||
StateSyncStatusInterface,
|
||||
StateInterface
|
||||
StateInterface,
|
||||
getResultEvent,
|
||||
ResultEvent
|
||||
} from '@cerc-io/util';
|
||||
import { EthClient } from '@cerc-io/ipld-eth-client';
|
||||
import { GetStorageAt, getStorageValue, MappingKey, StorageLayout } from '@cerc-io/solidity-mapper';
|
||||
@ -36,6 +38,10 @@ export class Indexer implements IndexerInterface {
|
||||
return this._storageLayoutMap;
|
||||
}
|
||||
|
||||
getResultEvent (event: EventInterface): ResultEvent {
|
||||
return getResultEvent(event);
|
||||
}
|
||||
|
||||
async getStorageValue (storageLayout: StorageLayout, blockHash: string, contractAddress: string, variable: string, ...mappingKeys: MappingKey[]): Promise<ValueResult> {
|
||||
return getStorageValue(
|
||||
storageLayout,
|
||||
|
@ -30,7 +30,7 @@ export interface PrefetchedBlock {
|
||||
* @param jobQueue
|
||||
* @param blockNumber
|
||||
*/
|
||||
export const processBlockByNumberWithCache = async (
|
||||
export const processBlockByNumber = async (
|
||||
jobQueue: JobQueue,
|
||||
blockNumber: number
|
||||
): Promise<void> => {
|
||||
@ -247,11 +247,9 @@ export const processBatchEvents = async (indexer: IndexerInterface, block: Block
|
||||
|
||||
console.time('time:common#processBatchEvents-processing_events_batch');
|
||||
|
||||
// Process events in loop
|
||||
for (let event of events) {
|
||||
// Process events in loop
|
||||
|
||||
const eventIndex = event.index;
|
||||
// log(`Processing event ${event.id} index ${eventIndex}`);
|
||||
|
||||
// Check that events are processed in order.
|
||||
if (eventIndex <= block.lastProcessedEventIndex) {
|
||||
@ -269,14 +267,7 @@ export const processBatchEvents = async (indexer: IndexerInterface, block: Block
|
||||
}
|
||||
}
|
||||
|
||||
let watchedContract;
|
||||
|
||||
if (!indexer.isWatchedContract) {
|
||||
// uni-info-watcher indexer doesn't have watched contracts implementation.
|
||||
watchedContract = true;
|
||||
} else {
|
||||
watchedContract = indexer.isWatchedContract(event.contract);
|
||||
}
|
||||
const watchedContract = indexer.isWatchedContract(event.contract);
|
||||
|
||||
if (watchedContract) {
|
||||
// We might not have parsed this event yet. This can happen if the contract was added
|
||||
|
@ -11,10 +11,9 @@ import { EthClient } from '@cerc-io/ipld-eth-client';
|
||||
import { JobQueue } from './job-queue';
|
||||
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 { createPruningJob, processBlockByNumberWithCache } from './common';
|
||||
import { createPruningJob, processBlockByNumber } from './common';
|
||||
import { UpstreamConfig } from './config';
|
||||
import { OrderDirection } from './database';
|
||||
import { getResultEvent } from './misc';
|
||||
|
||||
const EVENT = 'event';
|
||||
|
||||
@ -65,8 +64,7 @@ export class EventWatcher {
|
||||
startBlockNumber = syncStatus.chainHeadBlockNumber + 1;
|
||||
}
|
||||
|
||||
// Wait for block processing as blockProgress event might process the same block.
|
||||
await processBlockByNumberWithCache(this._jobQueue, startBlockNumber);
|
||||
await processBlockByNumber(this._jobQueue, startBlockNumber);
|
||||
|
||||
// Creating an AsyncIterable from AsyncIterator to iterate over the values.
|
||||
// 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;
|
||||
|
||||
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> {
|
||||
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}`);
|
||||
|
||||
|
@ -7,7 +7,7 @@ import debug from 'debug';
|
||||
import { JobQueue } from './job-queue';
|
||||
import { EventWatcherInterface, IndexerInterface } from './types';
|
||||
import { wait } from './misc';
|
||||
import { processBlockByNumberWithCache } from './common';
|
||||
import { processBlockByNumber } from './common';
|
||||
import { DEFAULT_PREFETCH_BATCH_SIZE } from './constants';
|
||||
|
||||
const log = debug('vulcanize:fill');
|
||||
@ -58,7 +58,7 @@ export const fillBlocks = async (
|
||||
|
||||
const numberOfBlocks = endBlock - startBlock + 1;
|
||||
|
||||
processBlockByNumberWithCache(jobQueue, startBlock);
|
||||
processBlockByNumber(jobQueue, startBlock);
|
||||
|
||||
// Creating an AsyncIterable from AsyncIterator to iterate over the values.
|
||||
// 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);
|
||||
log(`Processed ${blocksProcessed} of ${numberOfBlocks} blocks (${completePercentage}%)`);
|
||||
|
||||
await processBlockByNumberWithCache(jobQueue, blockNumber + 1);
|
||||
await processBlockByNumber(jobQueue, blockNumber + 1);
|
||||
|
||||
if (blockNumber + 1 >= endBlock) {
|
||||
// Break the async loop when blockProgress event is for the endBlock and processing is complete.
|
||||
|
@ -121,6 +121,7 @@ export interface IndexerInterface {
|
||||
updateStateStatusMap (address: string, stateStatus: StateStatus): void
|
||||
getStateData (state: StateInterface): any
|
||||
resetWatcherToBlock (blockNumber: number): Promise<void>
|
||||
getResultEvent (event: EventInterface): any
|
||||
}
|
||||
|
||||
export interface EventWatcherInterface {
|
||||
|
Loading…
Reference in New Issue
Block a user