From 0cef7003ef842ce0954985cae54bd16796901e5e Mon Sep 17 00:00:00 2001 From: nabarun Date: Fri, 12 May 2023 14:59:49 +0530 Subject: [PATCH] Fix indexer prepareStateEntry method --- packages/util/src/indexer.ts | 26 +++++++++++---- packages/util/src/state-helper.ts | 53 +++++++++++-------------------- 2 files changed, 37 insertions(+), 42 deletions(-) diff --git a/packages/util/src/indexer.ts b/packages/util/src/indexer.ts index 31b0c0d28..69f0a5a7e 100644 --- a/packages/util/src/indexer.ts +++ b/packages/util/src/indexer.ts @@ -27,7 +27,7 @@ import { UNKNOWN_EVENT_NAME, JOB_KIND_CONTRACT, QUEUE_EVENT_PROCESSING, DIFF_MER import { JobQueue } from './job-queue'; import { Where, QueryOptions } from './database'; import { ServerConfig } from './config'; -import { createOrUpdateStateData } from './state-helper'; +import { createOrUpdateStateData, StateDataMeta } from './state-helper'; const DEFAULT_MAX_EVENTS_BLOCK_RANGE = 1000; @@ -872,7 +872,7 @@ export class Indexer { currentState = currentStates[0]; } - let parentState: StateInterface | undefined; + let stateDataMeta: StateDataMeta | undefined; if (currentState) { // Update current State of same kind if it exists. @@ -886,15 +886,27 @@ export class Indexer { stateEntry = this._db.getNewState(); // Fetch the parent State entry. - parentState = await this._db.getLatestState(contractAddress, null, block.blockNumber); + const parentState = await this._db.getLatestState(contractAddress, null, block.blockNumber); + + // Setting the meta-data for a State entry (done only once per State entry). + stateDataMeta = { + id: contractAddress, + kind, + parent: { + '/': parentState ? parentState.cid : null + }, + ethBlock: { + cid: { + '/': block.cid + }, + num: block.blockNumber + } + }; } const { cid, data: { meta }, bytes } = await createOrUpdateStateData( data, - contractAddress, - block, - kind, - parentState + stateDataMeta ); assert(meta); diff --git a/packages/util/src/state-helper.ts b/packages/util/src/state-helper.ts index f55bf5f6d..6d98ebd2d 100644 --- a/packages/util/src/state-helper.ts +++ b/packages/util/src/state-helper.ts @@ -1,6 +1,5 @@ import _ from 'lodash'; import debug from 'debug'; -import assert from 'assert'; import { sha256 } from 'multiformats/hashes/sha2'; import { CID } from 'multiformats/cid'; @@ -12,20 +11,22 @@ import { ResultState } from './indexer'; const log = debug('vulcanize:state-helper'); -interface StateData { - meta?: { - id: string - kind: StateKind - parent: { - '/': string | null +export interface StateDataMeta { + id: string + kind: StateKind + parent: { + '/': string | null + }, + ethBlock: { + cid: { + '/': string }, - ethBlock: { - cid: { - '/': string - }, - num: number - } - }; + num: number + } +} + +interface StateData { + meta?: StateDataMeta; state: any } @@ -114,28 +115,10 @@ export const getResultState = (state: StateInterface): ResultState => { export const createOrUpdateStateData = async ( data: StateData, - contractAddress: string, - block: Partial, - kind: StateKind, - parentState?: StateInterface + meta?: StateDataMeta ): Promise<{ cid: CID, data: StateData, bytes: codec.ByteView }> => { - if (!data.meta) { - assert(block.cid); - assert(block.blockNumber); - // Setting the meta-data for a State entry (done only once per State entry). - data.meta = { - id: contractAddress, - kind, - parent: { - '/': parentState ? parentState.cid : null - }, - ethBlock: { - cid: { - '/': block.cid - }, - num: block.blockNumber - } - }; + if (meta) { + data.meta = meta; } // Encoding the data using dag-cbor codec.