Asynchronous prefetching of block size with eth_getBlockByNumber (#173)

* Prefetch block size from eth_blockByHash

* Fix updating of blockSizeMapLatestHeight

* Move block size caching to separate file

* Remove timer logs from graph-node store set and get
This commit is contained in:
2022-09-07 18:29:04 +05:30
committed by GitHub
parent f3091dee3d
commit 73ca225779
4 changed files with 78 additions and 12 deletions
-4
View File
@@ -74,9 +74,7 @@ export const instantiate = async (
const entityId = __getString(id);
assert(context.block);
console.time(`time:loader#index.store.get-db-${entityName}`);
const entityData = await database.getEntity(entityName, entityId, context.block.blockHash);
console.timeEnd(`time:loader#index.store.get-db-${entityName}`);
if (!entityData) {
return null;
@@ -97,9 +95,7 @@ export const instantiate = async (
assert(context.block);
let dbData = await database.fromGraphEntity(instanceExports, context.block, entityName, entityInstance);
console.time(`time:loader#index.store.set-db-${entityName}`);
await database.saveEntity(entityName, dbData);
console.timeEnd(`time:loader#index.store.set-db-${entityName}`);
// Resolve any field name conflicts in the dbData for auto-diff.
dbData = resolveEntityFieldConflicts(dbData);
+9 -4
View File
@@ -124,6 +124,7 @@ export class GraphWatcher {
async handleEvent (eventData: any) {
const { contract, event, eventSignature, block, tx: { hash: txHash }, eventIndex } = eventData;
// Check if block data is already fetched by a previous event in the same block.
if (!this._context.block || this._context.block.blockHash !== block.hash) {
this._context.block = await getFullBlock(this._ethClient, this._ethProvider, block.hash);
}
@@ -185,9 +186,13 @@ export class GraphWatcher {
}
async handleBlock (blockHash: string) {
const blockData = await getFullBlock(this._ethClient, this._ethProvider, blockHash);
// Check if block data is already fetched in handleEvent method for the same block.
if (!this._context.block || this._context.block.blockHash !== blockHash) {
this._context.block = await getFullBlock(this._ethClient, this._ethProvider, blockHash);
}
this._context.block = blockData;
const blockData = this._context.block;
assert(blockData);
// Clear transactions map on handling new block.
this._transactionsMap.clear();
@@ -195,7 +200,7 @@ export class GraphWatcher {
// Call block handler(s) for each contract.
for (const dataSource of this._dataSources) {
// Reinstantiate WASM after every N blocks.
if (blockData.blockNumber % this._wasmRestartBlocksInterval === 0) {
if (Number(blockData.blockNumber) % this._wasmRestartBlocksInterval === 0) {
// The WASM instance allocates memory as required and the limit is 4GB.
// https://stackoverflow.com/a/40453962
// https://github.com/AssemblyScript/assemblyscript/pull/1268#issue-618411291
@@ -227,7 +232,7 @@ export class GraphWatcher {
assert(this._indexer?.getContractsByKind);
const watchedContracts = this._indexer.getContractsByKind(dataSource.name);
contractAddressList = watchedContracts.filter(contract => blockData.blockNumber >= contract.startingBlock)
contractAddressList = watchedContracts.filter(contract => Number(blockData.blockNumber) >= contract.startingBlock)
.map(contract => contract.address);
}