Skip to content

Commit

Permalink
Fix indexer prepareStateEntry method
Browse files Browse the repository at this point in the history
  • Loading branch information
nikugogoi committed May 12, 2023
1 parent fd32286 commit 0cef700
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 42 deletions.
26 changes: 19 additions & 7 deletions packages/util/src/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand All @@ -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);
Expand Down
53 changes: 18 additions & 35 deletions packages/util/src/state-helper.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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
}

Expand Down Expand Up @@ -114,28 +115,10 @@ export const getResultState = (state: StateInterface): ResultState => {

export const createOrUpdateStateData = async (
data: StateData,
contractAddress: string,
block: Partial<BlockProgressInterface>,
kind: StateKind,
parentState?: StateInterface
meta?: StateDataMeta
): Promise<{ cid: CID, data: StateData, bytes: codec.ByteView<StateData> }> => {
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.
Expand Down

0 comments on commit 0cef700

Please sign in to comment.