Add a table for entites in frothy region for subgraph watchers (#231)

* Add a table for entites in frothy region and update it in a subscriber

* Accommodate changes to other watchers and codegen
This commit is contained in:
prathamesh0
2022-11-16 17:12:54 +05:30
committed by GitHub
parent 7e5974ccf7
commit 408a3927c0
32 changed files with 321 additions and 54 deletions
+5
View File
@@ -1256,6 +1256,11 @@ export class Database {
await Promise.all(updatePromises);
}
async pruneFrothyEntities<Entity> (queryRunner: QueryRunner, frothyEntityType: new () => Entity, blockNumber: number): Promise<void> {
// Remove frothy entity entries at | below the prune block height
return this._baseDatabase.removeEntities(queryRunner, frothyEntityType, { where: { blockNumber: LessThanOrEqual(blockNumber) } });
}
_measureCachedPrunedEntities () {
const totalEntities = Array.from(this.cachedEntities.latestPrunedEntities.values())
.reduce((acc, idEntitiesMap) => acc + idEntitiesMap.size, 0);
+2 -1
View File
@@ -3,5 +3,6 @@ export * from './database';
export {
prepareEntityState,
updateEntitiesFromState,
resolveEntityFieldConflicts
resolveEntityFieldConflicts,
afterEntityInsertOrUpdate
} from './utils';
+28 -1
View File
@@ -3,9 +3,10 @@ import path from 'path';
import fs from 'fs-extra';
import debug from 'debug';
import yaml from 'js-yaml';
import { ValueTransformer } from 'typeorm';
import { EntityTarget, InsertEvent, UpdateEvent, ValueTransformer } from 'typeorm';
import { ColumnMetadata } from 'typeorm/metadata/ColumnMetadata';
import assert from 'assert';
import _ from 'lodash';
import { GraphDecimal, IndexerInterface, jsonBigIntStringReplacer, StateInterface } from '@cerc-io/util';
import { MappingKey, StorageLayout } from '@cerc-io/solidity-mapper';
@@ -897,3 +898,29 @@ export const updateEntitiesFromState = async (database: Database, indexer: Index
console.timeEnd(`time:watcher#GraphWatcher-updateEntitiesFromState-update-entity-${entityName}`);
}
};
export const afterEntityInsertOrUpdate = async<Entity> (frothyEntityType: EntityTarget<Entity>, entities: Set<any>, event: InsertEvent<any> | UpdateEvent<any>): Promise<void> => {
const entity = event.entity;
// TODO: Check and return if entity is being pruned (is_pruned flag update)
// Insert the entity details in FrothyEntity table
if (entities.has(entity.constructor)) {
const frothyEntity = event.manager.create(
frothyEntityType,
{
..._.pick(entity, ['id', 'blockHash', 'blockNumber']),
...{ name: entity.constructor.name }
}
);
await event.manager.createQueryBuilder()
.insert()
.into(frothyEntityType)
.values(frothyEntity as any)
.orIgnore()
.execute();
}
// TOOD: Update latest entity tables
};
+14
View File
@@ -372,6 +372,20 @@ export class GraphWatcher {
}
}
async pruneFrothyEntities<Entity> (frothyEntityType: new () => Entity, blockNumber: number): Promise<void> {
const dbTx = await this._database.createTransactionRunner();
try {
await this._database.pruneFrothyEntities(dbTx, frothyEntityType, blockNumber);
dbTx.commitTransaction();
} catch (error) {
await dbTx.rollbackTransaction();
throw error;
} finally {
await dbTx.release();
}
}
pruneEntityCacheFrothyBlocks (canonicalBlockHash: string, canonicalBlockNumber: number) {
this._database.pruneEntityCacheFrothyBlocks(canonicalBlockHash, canonicalBlockNumber);
}